diff --git a/docs/radio_profiles.md b/docs/radio_profiles.md index 6447194dd..2c12b7016 100644 --- a/docs/radio_profiles.md +++ b/docs/radio_profiles.md @@ -428,10 +428,13 @@ its local chat with this notice, using `#EMERGENCY` as the sender: This is a local onboarding message, not an RF transmission or an emergency alert. It does not change the node's name. It is added only after the channel is successfully saved, not on every connection, reboot, key edit, or alias -rename. Deleting and adding the channel again adds a fresh notice. If the -receive queue is full, the notice waits for free space rather than replacing -an actual message. As with other pending received messages, it is RAM-backed -until downloaded by the app; a reboot before download can discard it. +rename. Deleting and adding the channel again adds a fresh notice. Queued +real messages are delivered first: the notice waits for an empty receive +inbox and for held one-key DMs to be released, retrying as the app downloads +messages. If later traffic fills the inbox, the local notice is the first +channel message eligible for eviction. As with other pending received +messages, it is RAM-backed until downloaded by the app; a reboot before +download can discard it. ### Meshtext channel chat diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index dc8f53814..bb0bde18d 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -2167,13 +2167,13 @@ void MyMesh::seedEmergencyChannelNotice(uint8_t index, const ChannelDetails& pre if (!isEmergencyNoticeChannelName(previous.name)) pending |= bit; if (!(pending & bit)) return; // Edits and reapplying a saved channel are not joins. - int capacity = getOfflineQueueCapacity(); -#if ONE_KEY_DM_SHARED_OFFLINE_QUEUE - capacity -= held_dm_count; + // Drain real traffic first. Starting with an empty inbox also makes this + // notice the oldest evictable channel frame if more traffic arrives later. + // Do not take a newly freed slot while a real one-key DM is still held. + if (offline_queue_len != 0) return; +#if MESH_ENABLE_ONE_KEY_DM + if (held_dm_count != 0) return; #endif - // A welcome notice must never evict an actual message. Retry after sync - // frees queue space, including space temporarily reserved for held DMs. - if (offline_queue_len >= capacity) return; if (queueChannelText(index, channel.channel.secret, 0, 0xFF, getRTCClock()->getCurrentTime(), "#EMERGENCY: This is an emergency channel. Do not use for testing or small talk.", @@ -11428,10 +11428,10 @@ void MyMesh::loop() { #endif } servicePendingRadioParamApply(); - if (!mesh::morseTimingActive(_radio)) servicePendingChannelNotices(); #if MESH_ENABLE_ONE_KEY_DM if (held_dm_count != 0 || verified_pending_count != 0) releaseHeldOneKeyDMs(); #endif + if (!mesh::morseTimingActive(_radio)) servicePendingChannelNotices(); #if defined(ESP32_PLATFORM) servicePersistence(); diff --git a/test/test_companion_channel_alias.py b/test/test_companion_channel_alias.py index d16b9ef9c..b4a6d70a6 100644 --- a/test/test_companion_channel_alias.py +++ b/test/test_companion_channel_alias.py @@ -416,8 +416,14 @@ using std::isfinite; #define DISPLAY_CLASS NoticeDisplay #define MESH_DEBUG_PRINTLN(...) ((void)0) #define OFFLINE_QUEUE_SIZE 4 +#define MESH_ENABLE_ONE_KEY_DM @ONE_KEY@ #define ONE_KEY_DM_SHARED_OFFLINE_QUEUE @HELD@ #define MESH_ENABLE_MESHTEXT_CHAT 1 +#define PUB_KEY_SIZE 32 +#define MAX_TEXT_LEN 160 +#define ONE_KEY_DM_ID_SIZE 8 +#define ROUTE_TYPE_DIRECT 2 +#define ROUTE_TYPE_FLOOD 1 static bool checking_ack_order=false,ack_written=false; struct NoticeCommandScope { NoticeCommandScope(){checking_ack_order=true;ack_written=false;} @@ -445,6 +451,13 @@ struct NoticeTerminal {unsigned echoes=0;void printf(const char*,...){++echoes;} struct NoticeTerminalDisplay {bool shouldShowChannel(bool)const{return true;}}; ''' +NOTICE_DM_STUBS = r''' +struct ContactInfo { + struct {uint8_t pub_key[PUB_KEY_SIZE]={};} id; + const uint8_t* getSharedSecret(int)const{return id.pub_key;} +}; +''' + NOTICE_MEMBERS = r''' NoticeUI ui; NoticeUI* _ui=&ui; NoticeNotifications _notifications; @@ -453,10 +466,35 @@ NOTICE_MEMBERS = r''' bool hasTerminalOutput()const{return true;}NoticeTerminal& terminalOutput(){return terminal;} uint8_t app_target_ver=3,out_frame[MAX_FRAME_SIZE]={}; uint8_t pending_emergency_notices[(MAX_GROUP_CHANNELS+7)/8]={}; +#if MESH_ENABLE_ONE_KEY_DM unsigned held_dm_count=0; + unsigned verified_pending_count=0; + uint8_t verified_pending_keys[15][PUB_KEY_SIZE]={}; + struct HeldOneKeyDM { + uint8_t sender_key[PUB_KEY_SIZE]={},id[8]={}; + mesh::Packet packet; + } held_dms[15]; + ContactInfo held_contact; + int self_id=0; + unsigned held_deliveries=0,held_acks=0; + ContactInfo* lookupPersistentContactByPubKey(const uint8_t*,size_t){return &held_contact;} + void forgetVerifiedPendingSender(const uint8_t*){verified_pending_count=0;} + bool wasDeliveredOneKeyDM(const uint8_t*,const uint8_t*)const{return false;} + void rememberDeliveredOneKeyDM(const uint8_t*,const uint8_t*){} + void onMessageRecv(const ContactInfo&,mesh::Packet*,uint32_t,const char*){ + const uint8_t dm[]={7,99};assert(addToOfflineQueue(dm,sizeof(dm)));++held_deliveries; + } + void sendAckTo(const ContactInfo&,const uint8_t*,size_t){++held_acks;} + void releaseHeldOneKeyDMs(); +#endif struct Frame {uint8_t len=0,buf[MAX_FRAME_SIZE]={};bool isChannelMsg()const;}; Frame offline_queue[OFFLINE_QUEUE_SIZE]; int offline_queue_len=0,offline_queue_head=0; + bool reject_next_enqueue=false; +#if ONE_KEY_DM_SHARED_OFFLINE_QUEUE + Frame& heldDMFrameAt(uint8_t); + void removeHeldOneKeyDM(uint8_t); +#endif int getOfflineQueueCapacity()const; Frame& offlineQueueFrameAt(int); bool addToOfflineQueue(const uint8_t*,int); @@ -509,6 +547,14 @@ static void fillRealMessages(MyMesh& node){ assert(node.addToOfflineQueue(frame,sizeof(frame))); } } +static void drainRealMessages(MyMesh& node,unsigned first,unsigned count){ + uint8_t frame[MAX_FRAME_SIZE]={}; + for(unsigned i=0;i= 2)", "if (cmd_frame[0] == CMD_SET_CHANNEL && len >= 2 + 32 + 32)", @@ -875,7 +996,22 @@ class CompanionChannelAliasTest(unittest.TestCase): "void writeOKFrame() { ok = true; ack_written = true; }") fixture = RUNTIME_HARNESS[:RUNTIME_HARNESS.index("\nint main()")] fixture = fixture.replace("#define MAX_GROUP_CHANNELS 5", "#define MAX_GROUP_CHANNELS 17") - fixture = fixture.replace("class MyMesh :", COMMAND_STUBS + "\nclass MyMesh :") + fixture = fixture.replace("class MyMesh :", COMMAND_STUBS + NOTICE_DM_STUBS + "\nclass MyMesh :") + fixture = fixture.replace( + "uint8_t radio_origin = 0, tx_radio = RADIO_TX_AUTO;", + "uint8_t radio_origin = 0, tx_radio = RADIO_TX_AUTO, header = ROUTE_TYPE_FLOOD, path_len = 0;\n" + " int8_t _snr = 0; bool isRouteFlood()const{return header==ROUTE_TYPE_FLOOD;}") + fixture = fixture.replace( + " static bool isZeroes(", + " static void sha256(uint8_t* out,size_t n,const uint8_t*,size_t,const uint8_t*,size_t){memset(out,0,n);}\n" + " static bool isZeroes(") + # Held-message release retains the real queue and FIFO control flow. + # The cipher boundary uses an explicitly prepared plaintext packet. + fixture = fixture.replace( + "static int MACThenDecrypt(const uint8_t* key, uint8_t*, const uint8_t* data, int length) {\n" + " return length == 16 && !memcmp(key, data, 16) ? 1 : 0;", + "static int MACThenDecrypt(const uint8_t* key, uint8_t* out, const uint8_t* data, int length) {\n" + " memcpy(out,data,length);return length;") fixture = fixture.replace("@SAVE_GETTER@", save_getter + "\n" + members + NOTICE_MEMBERS) command_helpers = COMMAND_HARNESS[:COMMAND_HARNESS.index("\nint main()")] command_helpers = command_helpers.replace("uint8_t out_frame[128] = {};", @@ -885,22 +1021,33 @@ class CompanionChannelAliasTest(unittest.TestCase): program = (shared + constants + "\n" + NOTICE_STUBS + fixture + command_helpers + NOTICE_CASES) program = program.replace("@VECTORS@", vectors).replace("@KEY_VECTORS@", key_vectors) program = program.replace("@METHODS@", methods).replace("@COMMAND_BRANCHES@", branches) - for held in (0, 1): - with self.subTest(shared_held_dm_queue=held): - self.run_native_program(program.replace("@HELD@", str(held)), "notice", + for one_key, held in ((0, 0), (1, 0), (1, 1)): + with self.subTest(one_key_dm=one_key, shared_held_dm_queue=held): + configured = program.replace("@ONE_KEY@", str(one_key)).replace("@HELD@", str(held)) + self.run_native_program(configured, "notice", extra_flags=("-Wno-unused-parameter",)) - # Prove the executable fixture catches the two dangerous regressions: - # treating a local seed like a real audible message, or letting the - # general queue policy evict real traffic to make room for the seed. + configured = program.replace("@ONE_KEY@", "1").replace("@HELD@", "0") + # Prove that quiet delivery, an empty inbox, and real held-message + # priority are each enforced by the executable fixture. self.assertIn("true, false)) pending", program) - self.run_native_program(program.replace("@HELD@", "0").replace( + self.run_native_program(configured.replace( "true, false)) pending", "true, true)) pending", 1), "notice", extra_flags=("-Wno-unused-parameter",), expect_failure="node.ui.last_quiet") - self.assertIn("if (offline_queue_len >= capacity) return;", program) - self.run_native_program(program.replace("@HELD@", "1").replace( - "if (offline_queue_len >= capacity) return;", - "if (offline_queue_len > capacity) return;", 1), "notice", - extra_flags=("-Wno-unused-parameter",), expect_failure="pending(node,16)") + self.assertIn("if (offline_queue_len != 0) return;", program) + self.run_native_program(configured.replace( + "if (offline_queue_len != 0) return;", + "if (offline_queue_len >= getOfflineQueueCapacity()) return;", 1), "notice", + extra_flags=("-Wno-unused-parameter",), expect_failure="node.ui.messages==0") + self.assertIn("if (held_dm_count != 0) return;", program) + self.run_native_program(configured.replace( + "if (held_dm_count != 0) return;", "", 1), "notice", + extra_flags=("-Wno-unused-parameter",), expect_failure="pending(node,9) && node.offline_queue_len==0") + self.run_native_program(configured.replace( + "true, false)) pending &= ~bit;", "true, false)); pending &= ~bit;", 1), "notice", + extra_flags=("-Wno-unused-parameter", "-Wno-empty-body", "-Wno-misleading-indentation"), + expect_failure="node.ok && pending(node,9)") + loop = body(companion, "void MyMesh::loop()") + self.assertLess(loop.index("releaseHeldOneKeyDMs()"), loop.index("servicePendingChannelNotices()")) def run_native_program(self, source_text, expected, extra_flags=(), expect_failure=None): compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")