475 lines
11 KiB
C
475 lines
11 KiB
C
/*
|
|
* data_processing.c
|
|
*
|
|
* Created on: Feb 3, 2023
|
|
* Author: Partha
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "nvs_flash.h"
|
|
#include "esp_system.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include <string.h>
|
|
#include "sys/time.h"
|
|
#include "driver/gpio.h"
|
|
#include "esp_log.h"
|
|
#include "math.h"
|
|
#include "sdkconfig.h"
|
|
#include "time.h"
|
|
#include "rtc_wdt.h"
|
|
#include "modem.h"
|
|
#include "esp_mac.h"
|
|
#include "uart_ifx.h"
|
|
#include "esp_task_wdt.h"
|
|
#include "port.h"
|
|
#include "adc_ifx.h"
|
|
#include "data_processing.h"
|
|
#include "main.h"
|
|
#include "hmi.h"
|
|
#include "comms.h"
|
|
#include "lwip/ip4_addr.h"
|
|
#include "wifi_Init.h"
|
|
#include "nvm.h"
|
|
#include "wifi_OTA.h"
|
|
static const char* TAG = "DATA";
|
|
|
|
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
|
#include "esp_log.h"
|
|
|
|
#define IMEI "353165803930522"
|
|
|
|
extern bool isCycleCompleted;
|
|
extern uint8_t comms_mode;
|
|
|
|
extern uint8_t checkin_cycle_counter;
|
|
extern uint8_t number_of_check_in_to_do;
|
|
|
|
static char thermocouple_type;
|
|
|
|
uint32_t g_version = 0;
|
|
uint32_t g_wifiConnectTime;
|
|
float g_voltage = 0;
|
|
bool g_usbConnected = 0;
|
|
bool g_enableButtonTiming;
|
|
RTC_DATA_ATTR static bool g_prevUsbConnected;
|
|
uint32_t light_mv = 0;
|
|
uint8_t checkInAttemptNumber = 1;
|
|
time_t g_wakeUpTime = 0;
|
|
uint32_t g_StatusFlags = 0;
|
|
uint32_t g_triggerFlags1 = 0;
|
|
uint32_t g_triggerFlags2 = 0;
|
|
|
|
// Configuration global variable
|
|
uint32_t g_server_time = 0;
|
|
uint32_t g_ID;
|
|
uint32_t g_SensorInt;
|
|
uint32_t g_ConfigUpdate;
|
|
uint32_t g_McuUpdate;
|
|
uint32_t g_ModemUpdate;
|
|
|
|
|
|
RTC_DATA_ATTR historyLog_t _logs[NVM_MAX_NUMBER_OF_IN_ONE_SECTOR] = {};
|
|
|
|
|
|
typedef struct
|
|
{
|
|
// Stored in RTC RAM during Deep Sleeps. This reduces wear on the FLASH.
|
|
|
|
// TIMING
|
|
uint32_t timeOfLastRadioCalibration;
|
|
uint32_t checkInClock;
|
|
uint32_t retry1Clock;
|
|
uint32_t retry2Clock;
|
|
int8_t lteRssi; // Modem signal strength
|
|
|
|
// WIFI CONFIG
|
|
uint8_t bssid[6];
|
|
esp_netif_ip_info_t localIp;
|
|
esp_netif_dns_info_t dns1;
|
|
esp_netif_dns_info_t dns2;
|
|
uint8_t wifiChannel;
|
|
int32_t wifi_rssi;
|
|
|
|
// OTHERS
|
|
uint32_t operatingMode;
|
|
uint32_t initializationVector[4];
|
|
bool initializationVectorCreated;
|
|
uint32_t checkInCount;
|
|
float temperatureAtLastRadioCalibration;
|
|
bool retriesAreInProgress;
|
|
}rtcRamData_t;
|
|
|
|
RTC_DATA_ATTR rtcRamData_t _rtcRamData;
|
|
|
|
static esp_err_t data_parse_float_value(float *target_buffer, const char needle[], const char *http_resp)
|
|
{
|
|
esp_err_t retval = ESP_FAIL;
|
|
|
|
char *ret = NULL;
|
|
char *start_add = NULL;
|
|
char *end_add = NULL;
|
|
char parse_value[] = {'0'};
|
|
|
|
int len = 0;
|
|
|
|
/*1- Extract temperature limits*/
|
|
ret = strstr(http_resp, needle);
|
|
if(ret)
|
|
{
|
|
/*2- get the starting address of the value*/
|
|
start_add = strstr(ret,(char*)":") + 1;
|
|
|
|
/*3- get the ending address of the value*/
|
|
end_add = strstr(ret,(char*)",") + 1;
|
|
|
|
if((char *)1 == end_add)
|
|
end_add = start_add + strlen(start_add) + 1;
|
|
|
|
/*4- Calculate the length of the value*/
|
|
len = end_add - start_add;
|
|
|
|
/*5- copy the value into the buffer*/
|
|
snprintf(parse_value, len, "%s", start_add);
|
|
|
|
/*6- Assign the double value to the target buffer*/
|
|
*target_buffer = atof(parse_value);
|
|
|
|
retval = ESP_OK;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
static esp_err_t data_parse_int_value(int32_t *target_buffer, const char needle[], const char *http_resp)
|
|
{
|
|
esp_err_t retval = ESP_FAIL;
|
|
|
|
char *ret = NULL;
|
|
char *start_add = NULL;
|
|
char *end_add = NULL;
|
|
char parse_value[] = {'0'};
|
|
|
|
int len = 0;
|
|
|
|
/*1- Extract temperature limits*/
|
|
ret = strstr(http_resp, needle);
|
|
if(ret)
|
|
{
|
|
/*2- get the starting address of the value*/
|
|
start_add = strstr(ret,(char*)":") + 1;
|
|
|
|
/*3- get the ending address of the value*/
|
|
end_add = strstr(ret,(char*)",") + 1;
|
|
|
|
if((char *)1 == end_add)
|
|
end_add = start_add + strlen(start_add) + 1;
|
|
|
|
/*4- Calculate the length of the value*/
|
|
len = end_add - start_add;
|
|
|
|
/*5- copy the value into the buffer*/
|
|
snprintf(parse_value, len, "%s", start_add);
|
|
|
|
/*6- Assign the double value to the target buffer*/
|
|
*target_buffer = atoi(parse_value);
|
|
|
|
retval = ESP_OK;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
static esp_err_t data_parse_uint8_value(uint8_t *target_buffer, const char needle[], const char *http_resp)
|
|
{
|
|
esp_err_t retval = ESP_FAIL;
|
|
|
|
char *ret = NULL;
|
|
char *start_add = NULL;
|
|
char *end_add = NULL;
|
|
char parse_value[] = {'0'};
|
|
|
|
int len = 0;
|
|
|
|
/*1- Extract temperature limits*/
|
|
ret = strstr(http_resp, needle);
|
|
if(ret)
|
|
{
|
|
/*2- get the starting address of the value*/
|
|
start_add = strstr(ret,(char*)":") + 1;
|
|
|
|
/*3- get the ending address of the value*/
|
|
end_add = strstr(ret,(char*)",") + 1;
|
|
|
|
if((char *)1 == end_add)
|
|
end_add = start_add + strlen(start_add) + 1;
|
|
|
|
/*4- Calculate the length of the value*/
|
|
len = end_add - start_add;
|
|
|
|
/*5- copy the value into the buffer*/
|
|
snprintf(parse_value, len, "%s", start_add);
|
|
|
|
/*6- Assign the double value to the target buffer*/
|
|
*target_buffer = atoi(parse_value);
|
|
|
|
retval = ESP_OK;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
static esp_err_t data_parse_uint16_value(uint16_t *target_buffer, const char needle[], const char *http_resp)
|
|
{
|
|
esp_err_t retval = ESP_FAIL;
|
|
|
|
char *ret = NULL;
|
|
char *start_add = NULL;
|
|
char *end_add = NULL;
|
|
char parse_value[] = {'0'};
|
|
|
|
int len = 0;
|
|
|
|
/*1- Extract temperature limits*/
|
|
ret = strstr(http_resp, needle);
|
|
if(ret)
|
|
{
|
|
/*2- get the starting address of the value*/
|
|
start_add = strstr(ret,(char*)":") + 1;
|
|
|
|
/*3- get the ending address of the value*/
|
|
end_add = strstr(ret,(char*)",") + 1;
|
|
|
|
if((char *)1 == end_add)
|
|
end_add = start_add + strlen(start_add) + 1;
|
|
|
|
/*4- Calculate the length of the value*/
|
|
len = end_add - start_add;
|
|
|
|
/*5- copy the value into the buffer*/
|
|
snprintf(parse_value, len, "%s", start_add);
|
|
|
|
/*6- Assign the double value to the target buffer*/
|
|
*target_buffer = atoi(parse_value);
|
|
|
|
retval = ESP_OK;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
static esp_err_t data_parse_uint_value(uint32_t *target_buffer, const char needle[], const char *http_resp)
|
|
{
|
|
esp_err_t retval = ESP_FAIL;
|
|
|
|
char *ret = NULL;
|
|
char *start_add = NULL;
|
|
char *end_add = NULL;
|
|
char parse_value[] = {'0'};
|
|
|
|
int len = 0;
|
|
|
|
/*1- Extract temperature limits*/
|
|
ret = strstr(http_resp, needle);
|
|
if(ret)
|
|
{
|
|
/*2- get the starting address of the value*/
|
|
start_add = strstr(ret,(char*)":") + 1;
|
|
|
|
/*3- get the ending address of the value*/
|
|
end_add = strstr(ret,(char*)",") + 1;
|
|
|
|
if((char *)1 == end_add)
|
|
end_add = start_add + strlen(start_add) + 1;
|
|
|
|
/*4- Calculate the length of the value*/
|
|
len = end_add - start_add;
|
|
|
|
/*5- copy the value into the buffer*/
|
|
snprintf(parse_value, len, "%s", start_add);
|
|
|
|
/*6- Assign the double value to the target buffer*/
|
|
*target_buffer = atoi(parse_value);
|
|
|
|
retval = ESP_OK;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
static esp_err_t data_parse_String_value(char *target_buffer, const char needle[], const char *http_resp)
|
|
{
|
|
esp_err_t retval = ESP_FAIL;
|
|
|
|
char *ret = NULL;
|
|
char *start_add = NULL;
|
|
char *end_add = NULL;
|
|
|
|
|
|
int len = 0;
|
|
|
|
/*1- Extract temperature limits*/
|
|
ret = strstr(http_resp, needle);
|
|
if(ret)
|
|
{
|
|
/*2- get the starting address of the value*/
|
|
start_add = strstr(ret,(char*)":") + 2;
|
|
|
|
/*3- get the ending address of the value*/
|
|
end_add = strstr(ret,(char*)",");
|
|
|
|
/*4- Calculate the length of the value*/
|
|
len = end_add - start_add;
|
|
|
|
/*5- copy the value into the buffer*/
|
|
snprintf(target_buffer, len, "%s", start_add);
|
|
|
|
retval = ESP_OK;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
|
|
void data_set_ap_mac_addr(uint8_t *bssid)
|
|
{
|
|
for(int i=0; i<6; i++)
|
|
{
|
|
_rtcRamData.bssid[i] = bssid[i];
|
|
}
|
|
}
|
|
|
|
uint8_t* data_get_ap_mac_addr(void)
|
|
{
|
|
return ((uint8_t*)&_rtcRamData.bssid[0]);
|
|
}
|
|
|
|
void data_set_local_ip_addr(esp_netif_ip_info_t* ip_infos)
|
|
{
|
|
_rtcRamData.localIp.ip.addr = ip_infos->ip.addr;
|
|
_rtcRamData.localIp.gw.addr = ip_infos->gw.addr;
|
|
_rtcRamData.localIp.netmask.addr = ip_infos->netmask.addr;
|
|
}
|
|
|
|
uint32_t data_get_ip_addr(void)
|
|
{
|
|
return _rtcRamData.localIp.ip.addr;
|
|
}
|
|
|
|
uint32_t data_get_gw_addr(void)
|
|
{
|
|
return _rtcRamData.localIp.gw.addr;
|
|
}
|
|
|
|
uint32_t data_get_netmask_addr(void)
|
|
{
|
|
return _rtcRamData.localIp.netmask.addr;
|
|
}
|
|
|
|
void data_set_wifi_channel(uint8_t channel)
|
|
{
|
|
_rtcRamData.wifiChannel = channel;
|
|
}
|
|
|
|
uint8_t data_get_wifi_channel(void)
|
|
{
|
|
return _rtcRamData.wifiChannel;
|
|
}
|
|
|
|
void data_set_wifi_rssi(int32_t rsi)
|
|
{
|
|
_rtcRamData.wifi_rssi = rsi;
|
|
}
|
|
|
|
int32_t data_get_wifi_rssi(void)
|
|
{
|
|
return _rtcRamData.wifi_rssi;
|
|
}
|
|
|
|
void data_set_main_and_backup_dns(esp_netif_dns_info_t* dns1, esp_netif_dns_info_t* dns2)
|
|
{
|
|
_rtcRamData.dns1.ip.u_addr.ip4.addr = dns1->ip.u_addr.ip4.addr;
|
|
_rtcRamData.dns2.ip.u_addr.ip4.addr = dns2->ip.u_addr.ip4.addr;
|
|
}
|
|
|
|
uint32_t data_get_mode(void)
|
|
{
|
|
return _rtcRamData.operatingMode;
|
|
}
|
|
|
|
void data_clearWifiConnectionSettings(void)
|
|
{
|
|
_rtcRamData.wifiChannel = 1; // FCC allows only channels 1 through 13 in the USA, but 12 and 13 must be low power, which I don't think we can control. My linksys router only allows channels 1-11.
|
|
memset(_rtcRamData.bssid, 0, sizeof(_rtcRamData.bssid));
|
|
_rtcRamData.localIp.ip.addr = 0;
|
|
_rtcRamData.localIp.gw.addr = 0;
|
|
_rtcRamData.localIp.netmask.addr = 0;
|
|
_rtcRamData.dns1.ip.u_addr.ip4.addr = 0;
|
|
_rtcRamData.dns2.ip.u_addr.ip4.addr = 0;
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
/* Buffer(Json Sample) */
|
|
/*{ "request": "settings", "time":1675859084, "id":10608508,
|
|
* "SensorInt":1, "ConfigUpdate":0, "McuUpdate:0", "ModemUpdate":0 }
|
|
*/
|
|
/*******************************************************************/
|
|
|
|
esp_err_t data_parsing_config(char *buff,int len){
|
|
esp_err_t retval = ESP_FAIL;
|
|
char server_request[11]={0};
|
|
// char setting_req[]="settings";
|
|
// ret = strstr(buff, (const char*)"settings");
|
|
// data_parse_String_value(server_request,"request",buff);
|
|
// if( ret != NULL ){
|
|
if(1){
|
|
//retval = data_parse_uint_value(&g_server_time,"time", buff);
|
|
|
|
retval = data_parse_uint_value(&g_ID,"id", buff);
|
|
retval = data_parse_uint_value(&g_SensorInt,"SensorInt", buff);
|
|
retval = data_parse_uint_value(&g_ConfigUpdate,"ConfigUpdate", buff);
|
|
retval = data_parse_uint_value(&g_McuUpdate,"McuUpdate", buff);
|
|
retval = data_parse_uint_value(&g_ModemUpdate,"ModemUpdate", buff);
|
|
ESP_LOGI(TAG, "ConfigUpdate: %luMcuUpdate: %lu ModemUpdate: %lu", g_ConfigUpdate, g_McuUpdate, g_ModemUpdate);
|
|
|
|
|
|
g_McuUpdate = 1;
|
|
//g_ConfigUpdate = 1;
|
|
if(g_ConfigUpdate == 1)
|
|
|
|
ESP_LOGI(TAG, "Configuration update = 1 ");
|
|
}
|
|
if(g_McuUpdate == 1){
|
|
|
|
ESP_LOGI(TAG, "g_McuUpdate update = 1 ");
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(TAG, "Invalid request :%s",server_request);
|
|
}
|
|
return retval;
|
|
}
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
/* function input
|
|
maxretrynum = HTTP_RETRY_COUNT = 5
|
|
httpRetryCnt is the current retry count
|
|
|
|
|
|
*/
|
|
esp_err_t check_in_attempts_count( int httpRetryCnt, int maxretrynum )
|
|
{
|
|
esp_err_t retval = ESP_OK;
|
|
|
|
checkInAttemptNumber = 1 + maxretrynum - httpRetryCnt;
|
|
|
|
return retval;
|
|
}
|
|
|
|
|
|
|