92 lines
2.9 KiB
C

//
// Created by Kai Jan Kriegel on 06.02.23.
//
#include "ota.h"
#include <stdbool.h>
#include <mqtt_client.h>
#include <esp_log.h>
#include "pollingtagdetect.h"
#include "st_errno.h"
#include "rfal_nfc.h"
extern TagDetectState nfc_state;
bool activeOTA = false;
uint8_t* current_nfcId;
uint8_t current_nfcIdLen;
bool hasActiveOTA() {
return activeOTA;
}
void startOTA(esp_mqtt_client_handle_t client, uint8_t *nfcId, uint8_t nfcIdLen) {
activeOTA = true;
char topic_stopOTA[] = "fabreader/00000/startOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", CONFIG_FABREADER2_ID);
esp_mqtt_client_publish(client, topic_stopOTA, (const char*)nfcId, nfcIdLen, 0, 0);
current_nfcId = (uint8_t*)malloc(nfcIdLen);
memcpy(current_nfcId, nfcId, nfcIdLen);
current_nfcIdLen = nfcIdLen;
ESP_LOGI("OTA", "Started OTA communication");
}
void continueOTA(esp_mqtt_client_handle_t client, char* topic, char *payload, unsigned int length) {
char topic_requestOTA[] = "fabreader/00000/requestOTA";
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", CONFIG_FABREADER2_ID);
char topic_responseOTA[] = "fabreader/00000/responseOTA";
sprintf(topic_responseOTA, "fabreader/%05d/responseOTA", CONFIG_FABREADER2_ID);
char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", CONFIG_FABREADER2_ID);
char topic_restartOTA[] = "fabreader/00000/restartOTA";
sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", CONFIG_FABREADER2_ID);
if (strcmp(topic, topic_requestOTA) == 0) {
uint16_t *rxLen;
uint8_t *rxData;
ESP_LOGD("OTA", "Received OTA request, transmiting to card");
ReturnCode err = TagDetectTransceiveBlocking((uint8_t*)payload, length, &rxData, &rxLen, RFAL_FWT_NONE);
if (err == ERR_NONE) {
ESP_LOGD("OTA", "Transmit to card successful, sending response");
esp_mqtt_client_publish(client, topic_responseOTA, (const char*)rxData, *rxLen, 0, 0);
} else {
ESP_LOGD("OTA", "Transmit to card failed!");
cancelOTA(client);
}
esp_mqtt_client_publish(client, topic_responseOTA, payload, length, 0, 0);
} else if (strcmp(topic, topic_stopOTA) == 0) {
ESP_LOGD("OTA", "Received OTA stop");
free(current_nfcId);
rfalNfcDeactivate(false);
platformDelay(500);
activeOTA = false;
nfc_state = START_DISCOVERY;
}
}
void cancelOTA(esp_mqtt_client_handle_t client) {
activeOTA = false;
char topic_cancelOTA[] = "fabreader/00000/cancelOTA";
sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", CONFIG_FABREADER2_ID);
esp_mqtt_client_publish(client, topic_cancelOTA, (const char*)current_nfcId, current_nfcIdLen, 0, 0);
free(current_nfcId);
rfalNfcDeactivate(false);
platformDelay(500);
activeOTA = false;
nfc_state = START_DISCOVERY;
ESP_LOGI("OTA", "Canceled OTA communication");
}