diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 08362a80..cc5fd34f 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -364,6 +364,7 @@ protected: void onChannelMessageRecv(const mesh::GroupChannel& channel, int in_path_len, uint32_t timestamp, const char *text) override { int i = 0; out_frame[i++] = RESP_CODE_CHANNEL_MSG_RECV; + out_frame[i++] = 0; // FUTURE: channel_idx (will just be 'public' for now) out_frame[i++] = in_path_len < 0 ? 0xFF : in_path_len; out_frame[i++] = TXT_TYPE_PLAIN; memcpy(&out_frame[i], ×tamp, 4); i += 4; @@ -525,26 +526,18 @@ public: writeErrFrame(); // unknown recipient, or unsuported TXT_TYPE_* } } else if (cmd_frame[0] == CMD_SEND_CHANNEL_TXT_MSG) { // send GroupChannel msg - #if 0 //TODO - uint8_t temp[5+MAX_TEXT_LEN+32]; - uint32_t timestamp = getRTCClock()->getCurrentTime(); - memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique - temp[4] = 0; // attempt and flags + int i = 1; + uint8_t txt_type = cmd_frame[i++]; // should be TXT_TYPE_PLAIN + uint8_t channel_idx = cmd_frame[i++]; // reserved future + uint32_t msg_timestamp; + memcpy(&msg_timestamp, &cmd_frame[i], 4); i += 4; + const char *text = (char *) &cmd_frame[i]; - sprintf((char *) &temp[5], "%s: %s", self_name, &command[7]); // : - temp[5 + MAX_TEXT_LEN] = 0; // truncate if too long - - int len = strlen((char *) &temp[5]); - auto pkt = createGroupDatagram(PAYLOAD_TYPE_GRP_TXT, *_public, temp, 5 + len); - if (pkt) { - sendFlood(pkt); - Serial.println(" Sent."); + if (txt_type == TXT_TYPE_PLAIN && sendGroupMessage(msg_timestamp, *_public, _prefs.node_name, text, len - i)) { // hard-coded to 'public' channel for now + writeOKFrame(); } else { - Serial.println(" ERROR: unable to send"); + writeErrFrame(); } - #else - writeErrFrame(); - #endif } else if (cmd_frame[0] == CMD_GET_CONTACTS) { // get Contact list if (_iter_started) { writeErrFrame(); // iterator is currently busy diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index 20fbe9b8..760f6f0a 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -220,6 +220,27 @@ int BaseChatMesh::sendMessage(const ContactInfo& recipient, uint32_t timestamp, return rc; } +bool BaseChatMesh::sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len) { + uint8_t temp[5+MAX_TEXT_LEN+32]; + memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique + temp[4] = 0; // TXT_TYPE_PLAIN + + sprintf((char *) &temp[5], "%s: ", sender_name); // : + char *ep = strchr((char *) &temp[5], 0); + int prefix_len = ep - (char *) &temp[5]; + + if (text_len + prefix_len > MAX_TEXT_LEN) text_len = MAX_TEXT_LEN - prefix_len; + memcpy(ep, text, text_len); + ep[text_len] = 0; // null terminator + + auto pkt = createGroupDatagram(PAYLOAD_TYPE_GRP_TXT, channel, temp, 5 + prefix_len + text_len); + if (pkt) { + sendFlood(pkt); + return true; + } + return false; +} + void BaseChatMesh::resetPathTo(ContactInfo& recipient) { recipient.out_path_len = -1; } diff --git a/src/helpers/BaseChatMesh.h b/src/helpers/BaseChatMesh.h index cfa1c878..b0241f82 100644 --- a/src/helpers/BaseChatMesh.h +++ b/src/helpers/BaseChatMesh.h @@ -98,6 +98,7 @@ protected: public: mesh::Packet* createSelfAdvert(const char* name, double lat=0.0, double lon=0.0); int sendMessage(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& expected_ack, uint32_t& est_timeout); + bool sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len); void resetPathTo(ContactInfo& recipient); void scanRecentContacts(int last_n, ContactVisitor* visitor); ContactInfo* searchContactsByPrefix(const char* name_prefix);