Skip to content
Snippets Groups Projects
Commit 4f655c60 authored by Moritz Perschke's avatar Moritz Perschke
Browse files

host change should now be handled properly

parent 051d7b75
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client
...@@ -22,7 +22,7 @@ void get_host_mac(uint8_t *destination) ...@@ -22,7 +22,7 @@ void get_host_mac(uint8_t *destination)
preferences.getBytes("host", destination, sizeof(uint8_t) * 6); preferences.getBytes("host", destination, sizeof(uint8_t) * 6);
} else { } else {
memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC)); memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC));
ESP_LOGE(TAG, "Backup MAC address used"); ESP_LOGI(TAG, "Backup MAC address used");
} }
preferences.end(); preferences.end();
} }
...@@ -53,47 +53,54 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) ...@@ -53,47 +53,54 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
// assume host change not happening, rare event // assume host change not happening, rare event
// => on host change, broadcast // => on host change, broadcast
ESP_LOGE(TAG, "Message recieved"); ESP_LOGE(TAG, "Message recieved");
preferences.begin("config", false);
config received_msg; config received_msg;
memcpy(&received_msg, incomingData, sizeof(received_msg)); // TODO: check for valid mac memcpy(&received_msg, incomingData, sizeof(received_msg)); // TODO: check for valid mac
// all the esp32 macs so far use the same first 3(?) bytes so maybe use that // all the esp32 macs so far use the same first 3(?) bytes so maybe use that
switch (received_msg.type){ switch (received_msg.type){
case dataAck:{ case dataAck:{
ESP_LOGI(TAG, "dataAck received.");
msg_recv = true; msg_recv = true;
Time::getInstance().setTime( Time::getInstance().setTime(
received_msg.epoch_seconds); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset received_msg.epoch_seconds); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset
Serial.println(Time::getInstance().getEpochSeconds()); Serial.println(Time::getInstance().getEpochSeconds());
preferences.begin("config", false);
if (!preferences.isKey("host")) { if (!preferences.isKey("host")) {
preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)); if(preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)) > 0){;
ESP_LOGI(TAG, "host MAC address saved to flash"); ESP_LOGI(TAG, "host MAC address saved to flash");
}
// add host to peers // add host to peers
add_host_to_peers(received_msg); add_host_to_peers(received_msg);
} }
preferences.end();
} }
case hostChange:{ case hostChange:{
ESP_LOGI(TAG, "hostChange received");
Time::getInstance().setTime(received_msg.epoch_seconds); Time::getInstance().setTime(received_msg.epoch_seconds);
// delete old host // delete old host
preferences.begin("config", false);
if(preferences.isKey("host")){ if(preferences.isKey("host")){
ESP_LOGI(TAG, "removing old host");
uint8_t old[6]; uint8_t old[6];
get_host_mac(old); preferences.end();
get_host_mac(old); // maybe problem here, re-opening preferences
esp_now_del_peer(old); esp_now_del_peer(old);
} }
// add new host // add new host
preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)); preferences.begin("config", false);
ESP_LOGI(TAG, "Host Mac address saved to flash:"); if(preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)) > 0){
for(int i=0; i<6; i++){ ESP_LOGI(TAG, "Host Mac address saved to flash:");
Serial.print(received_msg.host[i], HEX);
Serial.print(":");
} }
Serial.println(); else{
ESP_LOGI(TAG, "Couldn't save Host Mac to flash");
}
preferences.end();
add_host_to_peers(received_msg); add_host_to_peers(received_msg);
} }
default:{ default:{
break; break;
} }
} }
preferences.end();
} }
......
...@@ -139,7 +139,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) ...@@ -139,7 +139,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
if(!esp_now_is_peer_exist(mac)){ if(!esp_now_is_peer_exist(mac)){
esp_now_peer_info_t client = {}; esp_now_peer_info_t client = {};
memcpy(client.peer_addr, mac, 6); memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6);
client.encrypt = false; client.encrypt = false;
client.channel = 0; client.channel = 0;
...@@ -171,13 +171,13 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) ...@@ -171,13 +171,13 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
queue.push(lineData); queue.push(lineData);
xSemaphoreGive(xMutex); xSemaphoreGive(xMutex);
response response; response response = {};
response.type = dataAck; response.type = dataAck;
esp_read_mac(response.mac, ESP_MAC_WIFI_STA); esp_read_mac(response.mac, ESP_MAC_WIFI_STA);
response.time = rtc.getEpoch(); response.time = rtc.getEpoch();
esp_err_t success = esp_now_send(mac, (uint8_t*) &response, sizeof(response)); esp_err_t success = esp_now_send(mac, (uint8_t*) &response, sizeof(response));
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(),
(success == ESP_OK) ? "Response sent." : "Failed to respond"); (success == ESP_OK) ? "Response sent\n" : "Failed to respond\n");
} }
String documentToLineProtocolString(const DynamicJsonDocument &doc) String documentToLineProtocolString(const DynamicJsonDocument &doc)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment