|
50 | 50 | #define CMD_SEND_BINARY_REQ 50 |
51 | 51 | #define CMD_FACTORY_RESET 51 |
52 | 52 | #define CMD_SEND_PATH_DISCOVERY_REQ 52 |
53 | | -#define CMD_SET_FLOOD_SCOPE 54 // v8+ |
| 53 | +#define CMD_SET_FLOOD_SCOPE_KEY 54 // v8+ |
54 | 54 | #define CMD_SEND_CONTROL_DATA 55 // v8+ |
55 | 55 | #define CMD_GET_STATS 56 // v8+, second byte is stats type |
56 | 56 | #define CMD_SEND_ANON_REQ 57 |
|
59 | 59 | #define CMD_GET_ALLOWED_REPEAT_FREQ 60 |
60 | 60 | #define CMD_SET_PATH_HASH_MODE 61 |
61 | 61 | #define CMD_SEND_CHANNEL_DATA 62 |
| 62 | +#define CMD_SET_DEFAULT_FLOOD_SCOPE 63 |
| 63 | +#define CMD_GET_DEFAULT_FLOOD_SCOPE 64 |
62 | 64 |
|
63 | 65 | // Stats sub-types for CMD_GET_STATS |
64 | 66 | #define STATS_TYPE_CORE 0 |
|
93 | 95 | #define RESP_CODE_AUTOADD_CONFIG 25 |
94 | 96 | #define RESP_ALLOWED_REPEAT_FREQ 26 |
95 | 97 | #define RESP_CODE_CHANNEL_DATA_RECV 27 |
| 98 | +#define RESP_CODE_DEFAULT_FLOOD_SCOPE 28 |
96 | 99 |
|
97 | 100 | #define MAX_CHANNEL_DATA_LENGTH (MAX_FRAME_SIZE - 9) |
98 | 101 |
|
@@ -479,27 +482,32 @@ bool MyMesh::allowPacketForward(const mesh::Packet* packet) { |
479 | 482 | return _prefs.client_repeat != 0; |
480 | 483 | } |
481 | 484 |
|
482 | | -void MyMesh::sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis) { |
483 | | - // TODO: dynamic send_scope, depending on recipient and current 'home' Region |
484 | | - if (send_scope.isNull()) { |
| 485 | +void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis) { |
| 486 | + if (scope.isNull()) { |
485 | 487 | sendFlood(pkt, delay_millis, _prefs.path_hash_mode + 1); |
486 | 488 | } else { |
487 | 489 | uint16_t codes[2]; |
488 | | - codes[0] = send_scope.calcTransportCode(pkt); |
| 490 | + codes[0] = scope.calcTransportCode(pkt); |
489 | 491 | codes[1] = 0; // REVISIT: set to 'home' Region, for sender/return region? |
490 | 492 | sendFlood(pkt, codes, delay_millis, _prefs.path_hash_mode + 1); |
491 | 493 | } |
492 | 494 | } |
| 495 | + |
| 496 | +void MyMesh::sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis) { |
| 497 | + // TODO: dynamic send_scope, depending on recipient and current 'home' Region |
| 498 | + TransportKey default_scope; |
| 499 | + memcpy(&default_scope.key, _prefs.default_scope_key, sizeof(default_scope.key)); |
| 500 | + |
| 501 | + auto scope = send_scope.isNull() ? &default_scope : &send_scope; |
| 502 | + sendFloodScoped(*scope, pkt, delay_millis); |
| 503 | +} |
493 | 504 | void MyMesh::sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis) { |
494 | 505 | // TODO: have per-channel send_scope |
495 | | - if (send_scope.isNull()) { |
496 | | - sendFlood(pkt, delay_millis, _prefs.path_hash_mode + 1); |
497 | | - } else { |
498 | | - uint16_t codes[2]; |
499 | | - codes[0] = send_scope.calcTransportCode(pkt); |
500 | | - codes[1] = 0; // REVISIT: set to 'home' Region, for sender/return region? |
501 | | - sendFlood(pkt, codes, delay_millis, _prefs.path_hash_mode + 1); |
502 | | - } |
| 506 | + TransportKey default_scope; |
| 507 | + memcpy(&default_scope.key, _prefs.default_scope_key, sizeof(default_scope.key)); |
| 508 | + |
| 509 | + auto scope = send_scope.isNull() ? &default_scope : &send_scope; |
| 510 | + sendFloodScoped(*scope, pkt, delay_millis); |
503 | 511 | } |
504 | 512 |
|
505 | 513 | void MyMesh::onMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp, |
@@ -893,6 +901,17 @@ void MyMesh::begin(bool has_display) { |
893 | 901 | strcpy(_prefs.node_name, pub_key_hex); |
894 | 902 | #endif |
895 | 903 |
|
| 904 | + // if build provides default-scope, init with that |
| 905 | +#ifdef DEFAULT_FLOOD_SCOPE_NAME |
| 906 | + strcpy(_prefs.default_scope_name, DEFAULT_FLOOD_SCOPE_NAME); |
| 907 | + { |
| 908 | + TransportKeyStore temp; |
| 909 | + TransportKey key; |
| 910 | + temp.getAutoKeyFor(0, "#" DEFAULT_FLOOD_SCOPE_NAME, key); |
| 911 | + memcpy(_prefs.default_scope_key, key.key, sizeof(key.key)); |
| 912 | + } |
| 913 | +#endif |
| 914 | + |
896 | 915 | // load persisted prefs |
897 | 916 | _store->loadPrefs(_prefs, sensors.node_lat, sensors.node_lon); |
898 | 917 |
|
@@ -1210,7 +1229,9 @@ void MyMesh::handleCmdFrame(size_t len) { |
1210 | 1229 | if (pkt) { |
1211 | 1230 | if (len >= 2 && cmd_frame[1] == 1) { // optional param (1 = flood, 0 = zero hop) |
1212 | 1231 | unsigned long delay_millis = 0; |
1213 | | - sendFlood(pkt, delay_millis, _prefs.path_hash_mode + 1); |
| 1232 | + TransportKey default_scope; |
| 1233 | + memcpy(&default_scope.key, _prefs.default_scope_key, sizeof(default_scope.key)); |
| 1234 | + sendFloodScoped(default_scope, pkt, delay_millis); |
1214 | 1235 | } else { |
1215 | 1236 | sendZeroHop(pkt); |
1216 | 1237 | } |
@@ -1862,13 +1883,39 @@ void MyMesh::handleCmdFrame(size_t len) { |
1862 | 1883 | } else { |
1863 | 1884 | writeErrFrame(ERR_CODE_FILE_IO_ERROR); |
1864 | 1885 | } |
1865 | | - } else if (cmd_frame[0] == CMD_SET_FLOOD_SCOPE && len >= 2 && cmd_frame[1] == 0) { |
| 1886 | + } else if (cmd_frame[0] == CMD_SET_FLOOD_SCOPE_KEY && len >= 2 && cmd_frame[1] == 0) { |
1866 | 1887 | if (len >= 2 + 16) { |
1867 | 1888 | memcpy(send_scope.key, &cmd_frame[2], sizeof(send_scope.key)); // set curr scope TransportKey |
1868 | 1889 | } else { |
1869 | 1890 | memset(send_scope.key, 0, sizeof(send_scope.key)); // set scope to null |
1870 | 1891 | } |
1871 | 1892 | writeOKFrame(); |
| 1893 | + } else if (cmd_frame[0] == CMD_SET_DEFAULT_FLOOD_SCOPE && len >= 1) { |
| 1894 | + if (len >= 1+31+16) { |
| 1895 | + int n = strlen((char *) &cmd_frame[1]); |
| 1896 | + if (n > 0 && n < 31) { |
| 1897 | + strcpy(_prefs.default_scope_name, (char *) &cmd_frame[1]); |
| 1898 | + memcpy(_prefs.default_scope_key, &cmd_frame[1+31], 16); |
| 1899 | + savePrefs(); |
| 1900 | + writeOKFrame(); |
| 1901 | + } else { |
| 1902 | + writeErrFrame(ERR_CODE_ILLEGAL_ARG); |
| 1903 | + } |
| 1904 | + } else { |
| 1905 | + memset(_prefs.default_scope_name, 0, sizeof(_prefs.default_scope_name)); // set default scope to null |
| 1906 | + memset(_prefs.default_scope_key, 0, sizeof(_prefs.default_scope_key)); |
| 1907 | + savePrefs(); |
| 1908 | + writeOKFrame(); |
| 1909 | + } |
| 1910 | + } else if (cmd_frame[0] == CMD_GET_DEFAULT_FLOOD_SCOPE) { |
| 1911 | + out_frame[0] = RESP_CODE_DEFAULT_FLOOD_SCOPE; |
| 1912 | + if (strlen(_prefs.default_scope_name) > 0) { |
| 1913 | + memcpy(&out_frame[1], _prefs.default_scope_name, 31); |
| 1914 | + memcpy(&out_frame[1+31], _prefs.default_scope_key, 16); |
| 1915 | + _serial->writeFrame(out_frame, 1+31+16); |
| 1916 | + } else { |
| 1917 | + _serial->writeFrame(out_frame, 1); // no name or key means null |
| 1918 | + } |
1872 | 1919 | } else if (cmd_frame[0] == CMD_SEND_CONTROL_DATA && len >= 2 && (cmd_frame[1] & 0x80) != 0) { |
1873 | 1920 | auto resp = createControlData(&cmd_frame[1], len - 1); |
1874 | 1921 | if (resp) { |
|
0 commit comments