* Companion: new CMD_GET_CHANNEL, CMD_SET_CHANNEL

This commit is contained in:
Scott Powell 2025-03-11 14:50:40 +11:00
parent f9b2428dcd
commit 8c68dbb6e9
4 changed files with 92 additions and 25 deletions

View file

@ -203,7 +203,7 @@ void BaseChatMesh::onAckRecv(mesh::Packet* packet, uint32_t ack_crc) {
#ifdef MAX_GROUP_CHANNELS
int BaseChatMesh::searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel dest[], int max_matches) {
int n = 0;
for (int i = 0; i < num_channels && n < max_matches; i++) {
for (int i = 0; i < MAX_GROUP_CHANNELS && n < max_matches; i++) {
if (channels[i].hash[0] == hash[0]) {
dest[n++] = channels[i];
}
@ -602,10 +602,46 @@ mesh::GroupChannel* BaseChatMesh::addChannel(const char* psk_base64) {
}
return NULL;
}
bool BaseChatMesh::getChannel(int idx, mesh::GroupChannel& dest) {
if (idx >= 0 && idx < MAX_GROUP_CHANNELS) {
dest = channels[idx];
return true;
}
return false;
}
bool BaseChatMesh::setChannel(int idx, const mesh::GroupChannel& src) {
static uint8_t zeroes[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
if (idx >= 0 && idx < MAX_GROUP_CHANNELS) {
channels[idx] = src;
if (memcmp(&src.secret[16], zeroes, 16) == 0) {
mesh::Utils::sha256(channels[idx].hash, sizeof(channels[idx].hash), src.secret, 16); // 128-bit key
} else {
mesh::Utils::sha256(channels[idx].hash, sizeof(channels[idx].hash), src.secret, 32); // 256-bit key
}
return true;
}
return false;
}
int BaseChatMesh::findChannelIdx(const mesh::GroupChannel& ch) {
for (int i = 0; i < MAX_GROUP_CHANNELS; i++) {
if (memcmp(ch.secret, channels[i].secret, sizeof(ch.secret)) == 0) return i;
}
return -1; // not found
}
#else
mesh::GroupChannel* BaseChatMesh::addChannel(const char* psk_base64) {
return NULL; // not supported
}
bool BaseChatMesh::getChannel(int idx, mesh::GroupChannel& dest) {
return false;
}
bool BaseChatMesh::setChannel(int idx, const mesh::GroupChannel& src) {
return false;
}
int BaseChatMesh::findChannelIdx(const mesh::GroupChannel& ch) {
return -1; // not found
}
#endif
ContactsIterator BaseChatMesh::startContactsIterator() {

View file

@ -75,7 +75,7 @@ class BaseChatMesh : public mesh::Mesh {
unsigned long txt_send_timeout;
#ifdef MAX_GROUP_CHANNELS
mesh::GroupChannel channels[MAX_GROUP_CHANNELS];
int num_channels;
int num_channels; // only for addChannel()
#endif
mesh::Packet* _pendingLoopback;
uint8_t temp_buf[MAX_TRANS_UNIT];
@ -89,6 +89,7 @@ protected:
{
num_contacts = 0;
#ifdef MAX_GROUP_CHANNELS
memset(channels, 0, sizeof(channels));
num_channels = 0;
#endif
txt_send_timeout = 0;
@ -152,6 +153,9 @@ public:
int getNumContacts() const { return num_contacts; }
ContactsIterator startContactsIterator();
mesh::GroupChannel* addChannel(const char* psk_base64);
bool getChannel(int idx, mesh::GroupChannel& dest);
bool setChannel(int idx, const mesh::GroupChannel& src);
int findChannelIdx(const mesh::GroupChannel& ch);
void loop();
};