From e53f0d0725a5b15c68bc88f550924cccb0fb5474 Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Wed, 29 Jan 2025 02:11:46 +1100 Subject: [PATCH] * refactored BaseChatMesh::sendMessage(), added timestamp param --- examples/companion_radio/main.cpp | 4 +++- examples/simple_secure_chat/main.cpp | 2 +- src/helpers/BaseChatMesh.cpp | 7 +++---- src/helpers/BaseChatMesh.h | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index b50cf1c4..573cb0ea 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -378,13 +378,15 @@ public: } else if (cmd_frame[0] == CMD_SEND_TXT_MSG && len >= 9) { int i = 1; uint8_t attempt_and_flags = cmd_frame[i++]; + uint32_t msg_timestamp; + memcpy(&msg_timestamp, &cmd_frame[i], 4); i += 4; uint8_t* pub_key_prefix = &cmd_frame[i]; i += 6; ContactInfo* recipient = lookupContactByPubKey(pub_key_prefix, 6); if (recipient && (attempt_and_flags >> 2) == TXT_TYPE_PLAIN) { char *text = (char *) &cmd_frame[i]; int tlen = len - i; text[tlen] = 0; // ensure null - int result = sendMessage(*recipient, attempt_and_flags, text, expected_ack_crc); + int result = sendMessage(*recipient, msg_timestamp, attempt_and_flags, text, expected_ack_crc); // TODO: add expected ACK to table if (result == MSG_SEND_FAILED) { writeErrFrame(); diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index cd76f517..8df6183a 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -276,7 +276,7 @@ public: if (memcmp(command, "send ", 5) == 0) { if (curr_recipient) { const char *text = &command[5]; - int result = sendMessage(*curr_recipient, 0, text, expected_ack_crc); + int result = sendMessage(*curr_recipient, getRTCClock()->getCurrentTime(), 0, text, expected_ack_crc); if (result == MSG_SEND_FAILED) { Serial.println(" ERROR: unable to send."); } else { diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index afa00a53..a3b34cd1 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -179,12 +179,11 @@ void BaseChatMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mes } } -mesh::Packet* BaseChatMesh::composeMsgPacket(const ContactInfo& recipient, uint8_t attempt, const char *text, uint32_t& expected_ack) { +mesh::Packet* BaseChatMesh::composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack) { int text_len = strlen(text); if (text_len > MAX_TEXT_LEN) return NULL; uint8_t temp[5+MAX_TEXT_LEN+1]; - uint32_t timestamp = getRTCClock()->getCurrentTime(); memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique temp[4] = (attempt & 3); memcpy(&temp[5], text, text_len + 1); @@ -195,8 +194,8 @@ mesh::Packet* BaseChatMesh::composeMsgPacket(const ContactInfo& recipient, uint8 return createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, 5 + text_len); } -int BaseChatMesh::sendMessage(const ContactInfo& recipient, uint8_t attempt, const char* text, uint32_t& expected_ack) { - mesh::Packet* pkt = composeMsgPacket(recipient, attempt, text, expected_ack); +int BaseChatMesh::sendMessage(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& expected_ack) { + mesh::Packet* pkt = composeMsgPacket(recipient, timestamp, attempt, text, expected_ack); if (pkt == NULL) return MSG_SEND_FAILED; uint32_t t = _radio->getEstAirtimeFor(pkt->payload_len + pkt->path_len + 2); diff --git a/src/helpers/BaseChatMesh.h b/src/helpers/BaseChatMesh.h index b9a91f08..366cb2bd 100644 --- a/src/helpers/BaseChatMesh.h +++ b/src/helpers/BaseChatMesh.h @@ -59,7 +59,7 @@ class BaseChatMesh : public mesh::Mesh { int num_channels; #endif - mesh::Packet* composeMsgPacket(const ContactInfo& recipient, uint8_t attempt, const char *text, uint32_t& expected_ack); + mesh::Packet* composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack); protected: BaseChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::PacketManager& mgr, mesh::MeshTables& tables) @@ -96,7 +96,7 @@ protected: public: mesh::Packet* createSelfAdvert(const char* name); - int sendMessage(const ContactInfo& recipient, uint8_t attempt, const char* text, uint32_t& expected_ack); + int sendMessage(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& expected_ack); void resetPathTo(ContactInfo& recipient); void scanRecentContacts(int last_n, ContactVisitor* visitor); ContactInfo* searchContactsByPrefix(const char* name_prefix);