Added Wifi credntials store to NVM
This commit is contained in:
parent
96178a4450
commit
00b7d2b29a
374
main/adc_ifx.c
374
main/adc_ifx.c
@ -1,187 +1,187 @@
|
||||
/*
|
||||
* adc_ifc.c
|
||||
*
|
||||
* Created on: Jan 17, 2023
|
||||
* Author: Partha
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include "esp_log.h"
|
||||
//#include "driver/adc.h"
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_adc/adc_cali.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
//#include "esp_adc_cal.h"
|
||||
#include "data_processing.h"
|
||||
#include "hmi.h"
|
||||
#include "ulp_main.h"
|
||||
|
||||
static const char* TAG = "ADC_IF";
|
||||
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
||||
|
||||
//static esp_adc_cal_characteristics_t adc1_chars;
|
||||
RTC_DATA_ATTR uint32_t light_data3;
|
||||
RTC_DATA_ATTR uint32_t prev_light_data3;
|
||||
|
||||
adc_oneshot_unit_handle_t adc1_handle;
|
||||
adc_cali_handle_t adc1_cali_handle = NULL;
|
||||
static int adc_raw[2];
|
||||
static int voltage[2];
|
||||
|
||||
static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle)
|
||||
{
|
||||
adc_cali_handle_t handle = NULL;
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
bool calibrated = false;
|
||||
|
||||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
if (!calibrated) {
|
||||
ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting");
|
||||
adc_cali_curve_fitting_config_t cali_config = {
|
||||
.unit_id = unit,
|
||||
.atten = atten,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
|
||||
if (ret == ESP_OK) {
|
||||
calibrated = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
||||
if (!calibrated) {
|
||||
ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting");
|
||||
adc_cali_line_fitting_config_t cali_config = {
|
||||
.unit_id = unit,
|
||||
.atten = atten,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
|
||||
if (ret == ESP_OK) {
|
||||
calibrated = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
*out_handle = handle;
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Calibration Success");
|
||||
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
|
||||
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Invalid arg or no memory");
|
||||
}
|
||||
|
||||
return calibrated;
|
||||
}
|
||||
|
||||
void adc_ifx_init(void)
|
||||
{
|
||||
/*esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
|
||||
|
||||
//Check type of calibration value used to characterize ADC
|
||||
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF)
|
||||
{
|
||||
printf("ADC: eFuse Vref");
|
||||
}
|
||||
else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP)
|
||||
{
|
||||
printf("ADC: Two Point");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ADC: Default");
|
||||
}
|
||||
adc1_ulp_enable();
|
||||
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));*/
|
||||
|
||||
//-------------ADC1 Init---------------//
|
||||
adc_oneshot_unit_init_cfg_t init_config1 = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
.ulp_mode = ADC_ULP_MODE_DISABLE
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
|
||||
|
||||
//-------------ADC1 Config---------------//
|
||||
adc_oneshot_chan_cfg_t config = {
|
||||
.bitwidth = ADC_BITWIDTH_13,
|
||||
.atten = ADC_ATTEN_DB_11,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_0, &config));
|
||||
//ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_1, &config));
|
||||
|
||||
//-------------ADC1 Calibration Init---------------//
|
||||
if(false == adc_calibration_init(ADC_UNIT_1, ADC_ATTEN_DB_11, &adc1_cali_handle))
|
||||
{
|
||||
ESP_LOGI(TAG, "ADC Calibration NOT Supported");
|
||||
}
|
||||
}
|
||||
|
||||
static void adc_cal_deinit(adc_cali_handle_t handle)
|
||||
{
|
||||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
ESP_LOGI(TAG, "deregister %s calibration scheme", "Curve Fitting");
|
||||
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
|
||||
|
||||
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
||||
ESP_LOGI(TAG, "deregister %s calibration scheme", "Line Fitting");
|
||||
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
|
||||
#endif
|
||||
}
|
||||
|
||||
void adx_ifx_deinit(void)
|
||||
{
|
||||
//adc_cal_deinit(adc1_cali_handle);
|
||||
adc_oneshot_del_unit(adc1_handle);
|
||||
}
|
||||
|
||||
uint32_t adc_if_get_batt_mV(void)
|
||||
{
|
||||
//uint32_t val = 4*esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_0), &adc1_chars);
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_0, &adc_raw[0]));
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0], &voltage[0]));
|
||||
return 4*voltage[0];
|
||||
}
|
||||
|
||||
#if 0
|
||||
uint32_t adc_if_get_light_mV(void)
|
||||
{
|
||||
uint32_t test_val = 0;
|
||||
//uint32_t val = esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_1), &adc1_chars);
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_1, &adc_raw[1]));
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[1], &voltage[1]));
|
||||
|
||||
uint32_t val = voltage[1];
|
||||
|
||||
ESP_LOGI(TAG, "Light mV: %ld", val);
|
||||
test_val = (val*255)/2504;
|
||||
//if(data_get_light_sensor_gp() && data_get_light_sensor_gn())
|
||||
{
|
||||
/*if the new light_sensor value is greater than GB_threshold or lower than GN_threshold*/
|
||||
//if((data_get_light_sensor_gn() > test_val) || (data_get_light_sensor_gp() < test_val))
|
||||
{
|
||||
/* set the prev_light_data to the previous reading */
|
||||
//ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, ulp_raw_light_data, (int *)&prev_light_data));
|
||||
//prev_light_data = ulp_light_data;
|
||||
//ulp_prev_light_data=ulp_light_data;//light_data;
|
||||
//prev_light_data = (255*prev_light_data)/2504;
|
||||
//ulp_light_data = (255*val)/2504;
|
||||
//ESP_LOGI(TAG,"LIGHT_GN = %ld\nLIGHT_GP = %ld",data_get_light_sensor_gn(),data_get_light_sensor_gp());
|
||||
ESP_LOGI(TAG,"Updating Prev_light_data");
|
||||
prev_light_data3 = light_data3;
|
||||
light_data3 = test_val;
|
||||
//ulp_light_data = val;
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
uint32_t adc_if_get_prev_light_mV(void)
|
||||
{
|
||||
return prev_light_data3;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* adc_ifc.c
|
||||
*
|
||||
* Created on: Jan 17, 2023
|
||||
* Author: Partha
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include "esp_log.h"
|
||||
//#include "driver/adc.h"
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_adc/adc_cali.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
//#include "esp_adc_cal.h"
|
||||
#include "data_processing.h"
|
||||
#include "hmi.h"
|
||||
#include "ulp_main.h"
|
||||
|
||||
static const char* TAG = "ADC_IF";
|
||||
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
||||
|
||||
//static esp_adc_cal_characteristics_t adc1_chars;
|
||||
RTC_DATA_ATTR uint32_t light_data3;
|
||||
RTC_DATA_ATTR uint32_t prev_light_data3;
|
||||
|
||||
adc_oneshot_unit_handle_t adc1_handle;
|
||||
adc_cali_handle_t adc1_cali_handle = NULL;
|
||||
static int adc_raw[2];
|
||||
static int voltage[2];
|
||||
|
||||
static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle)
|
||||
{
|
||||
adc_cali_handle_t handle = NULL;
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
bool calibrated = false;
|
||||
|
||||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
if (!calibrated) {
|
||||
ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting");
|
||||
adc_cali_curve_fitting_config_t cali_config = {
|
||||
.unit_id = unit,
|
||||
.atten = atten,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
|
||||
if (ret == ESP_OK) {
|
||||
calibrated = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
||||
if (!calibrated) {
|
||||
ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting");
|
||||
adc_cali_line_fitting_config_t cali_config = {
|
||||
.unit_id = unit,
|
||||
.atten = atten,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
|
||||
if (ret == ESP_OK) {
|
||||
calibrated = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
*out_handle = handle;
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Calibration Success");
|
||||
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
|
||||
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Invalid arg or no memory");
|
||||
}
|
||||
|
||||
return calibrated;
|
||||
}
|
||||
|
||||
void adc_ifx_init(void)
|
||||
{
|
||||
/*esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
|
||||
|
||||
//Check type of calibration value used to characterize ADC
|
||||
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF)
|
||||
{
|
||||
printf("ADC: eFuse Vref");
|
||||
}
|
||||
else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP)
|
||||
{
|
||||
printf("ADC: Two Point");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ADC: Default");
|
||||
}
|
||||
adc1_ulp_enable();
|
||||
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));*/
|
||||
|
||||
//-------------ADC1 Init---------------//
|
||||
adc_oneshot_unit_init_cfg_t init_config1 = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
.ulp_mode = ADC_ULP_MODE_DISABLE
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
|
||||
|
||||
//-------------ADC1 Config---------------//
|
||||
adc_oneshot_chan_cfg_t config = {
|
||||
.bitwidth = ADC_BITWIDTH_13,
|
||||
.atten = ADC_ATTEN_DB_11,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_0, &config));
|
||||
//ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_1, &config));
|
||||
|
||||
//-------------ADC1 Calibration Init---------------//
|
||||
if(false == adc_calibration_init(ADC_UNIT_1, ADC_ATTEN_DB_11, &adc1_cali_handle))
|
||||
{
|
||||
ESP_LOGI(TAG, "ADC Calibration NOT Supported");
|
||||
}
|
||||
}
|
||||
|
||||
static void adc_cal_deinit(adc_cali_handle_t handle)
|
||||
{
|
||||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
ESP_LOGI(TAG, "deregister %s calibration scheme", "Curve Fitting");
|
||||
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
|
||||
|
||||
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
||||
ESP_LOGI(TAG, "deregister %s calibration scheme", "Line Fitting");
|
||||
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
|
||||
#endif
|
||||
}
|
||||
|
||||
void adx_ifx_deinit(void)
|
||||
{
|
||||
//adc_cal_deinit(adc1_cali_handle);
|
||||
adc_oneshot_del_unit(adc1_handle);
|
||||
}
|
||||
|
||||
uint32_t adc_if_get_batt_mV(void)
|
||||
{
|
||||
//uint32_t val = 4*esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_0), &adc1_chars);
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_0, &adc_raw[0]));
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0], &voltage[0]));
|
||||
return 4*voltage[0];
|
||||
}
|
||||
|
||||
#if 0
|
||||
uint32_t adc_if_get_light_mV(void)
|
||||
{
|
||||
uint32_t test_val = 0;
|
||||
//uint32_t val = esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_1), &adc1_chars);
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_1, &adc_raw[1]));
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[1], &voltage[1]));
|
||||
|
||||
uint32_t val = voltage[1];
|
||||
|
||||
ESP_LOGI(TAG, "Light mV: %ld", val);
|
||||
test_val = (val*255)/2504;
|
||||
//if(data_get_light_sensor_gp() && data_get_light_sensor_gn())
|
||||
{
|
||||
/*if the new light_sensor value is greater than GB_threshold or lower than GN_threshold*/
|
||||
//if((data_get_light_sensor_gn() > test_val) || (data_get_light_sensor_gp() < test_val))
|
||||
{
|
||||
/* set the prev_light_data to the previous reading */
|
||||
//ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, ulp_raw_light_data, (int *)&prev_light_data));
|
||||
//prev_light_data = ulp_light_data;
|
||||
//ulp_prev_light_data=ulp_light_data;//light_data;
|
||||
//prev_light_data = (255*prev_light_data)/2504;
|
||||
//ulp_light_data = (255*val)/2504;
|
||||
//ESP_LOGI(TAG,"LIGHT_GN = %ld\nLIGHT_GP = %ld",data_get_light_sensor_gn(),data_get_light_sensor_gp());
|
||||
ESP_LOGI(TAG,"Updating Prev_light_data");
|
||||
prev_light_data3 = light_data3;
|
||||
light_data3 = test_val;
|
||||
//ulp_light_data = val;
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
uint32_t adc_if_get_prev_light_mV(void)
|
||||
{
|
||||
return prev_light_data3;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,18 +1,18 @@
|
||||
/*
|
||||
* adc_ifx.h
|
||||
*
|
||||
* Created on: Jan 17, 2023
|
||||
* Author: Partha
|
||||
*/
|
||||
|
||||
#ifndef MAIN_ADC_IFX_H_
|
||||
#define MAIN_ADC_IFX_H_
|
||||
|
||||
void adc_ifx_init(void);
|
||||
void adx_ifx_deinit(void);
|
||||
uint32_t adc_if_get_batt_mV(void);
|
||||
uint32_t adc_if_get_light_mV(void);
|
||||
uint32_t adc_if_get_prev_light_mV(void);
|
||||
|
||||
|
||||
#endif /* MAIN_ADC_IFX_H_ */
|
||||
/*
|
||||
* adc_ifx.h
|
||||
*
|
||||
* Created on: Jan 17, 2023
|
||||
* Author: Partha
|
||||
*/
|
||||
|
||||
#ifndef MAIN_ADC_IFX_H_
|
||||
#define MAIN_ADC_IFX_H_
|
||||
|
||||
void adc_ifx_init(void);
|
||||
void adx_ifx_deinit(void);
|
||||
uint32_t adc_if_get_batt_mV(void);
|
||||
uint32_t adc_if_get_light_mV(void);
|
||||
uint32_t adc_if_get_prev_light_mV(void);
|
||||
|
||||
|
||||
#endif /* MAIN_ADC_IFX_H_ */
|
||||
|
4250
main/comms.c
4250
main/comms.c
File diff suppressed because it is too large
Load Diff
566
main/comms.h
566
main/comms.h
@ -1,283 +1,283 @@
|
||||
/*
|
||||
* comms.h
|
||||
*
|
||||
* Created on: Jan 16, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_COMMS_H_
|
||||
#define MAIN_COMMS_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "main.h"
|
||||
/* comms.h
|
||||
*
|
||||
* Copyright 2020 HAE Innovations
|
||||
*
|
||||
* Celluar Communications process manager.
|
||||
*
|
||||
* Handles communication to/from server over cell modem,
|
||||
* collecting and sending events from queue, processing configuration
|
||||
* and firmware updates from server, etc.
|
||||
*
|
||||
* Author: E. Ross
|
||||
*/
|
||||
|
||||
#define COMMS_STATUS_UPDATED 1
|
||||
#define COMMS_STATUS_OK 0
|
||||
#define COMMS_STATUS_ERROR -1
|
||||
|
||||
|
||||
#define MCU_BASE_URL1 "https://testdevice.tempstickapi.com"
|
||||
#define MCU_BASE_URL2 "https://testdevice.tempstickapi.com"
|
||||
#define MCU_BASE_URL3 "https://testdevice.tempstickapi.com"
|
||||
#define MCU_BASE_URL4 "http://testdevice.tempstickapi.com"
|
||||
|
||||
|
||||
#define MCU_ONBOARDING_URL "https://device-status.idealsciences%u.com"
|
||||
#define POSTING_ONBOARDING "POST /sensor-account.php HTTP/1.0"
|
||||
#define ON_BOARDING_SERVER_DOMAIN_NAME "device-status.idealsciences.com"
|
||||
#define USER_AGENT_ONBOARDING "SensorDHT/1.0"
|
||||
#define AUTHERIZATION_VALUE_ONBOARDING "Basic ZGV2OmRldjEyMw=="
|
||||
#define CONTENT_TYPE_VALUE_ONBOARDING "application/x-www-form-urlencoded"
|
||||
#define ONBOARDING_SERVER_POSTURL_MAX 3
|
||||
#define ONBOARDING_SERVER_POSTURL_MIN 1
|
||||
|
||||
|
||||
#define PARTHA_SERVER_BASE_URL "https://www.parthasarathimishra.com"
|
||||
#define TESTING_SERVER_BASE_URL "http://3.90.70.38:8080"
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
COMMS_OVER_CELL_WIFI_BACKUP = 1,
|
||||
COMMS_OVER_WIFI_CELL_BACKUP,
|
||||
COMMS_OVER_CELL,
|
||||
COMMS_OVER_WIFI,
|
||||
COMMS_MODE_5 //wifi as default for all readings /cellular for alerts /wifi back up for alerts
|
||||
}comms_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
COMMS_MEDIUM_CELL,
|
||||
COMMS_MEDIUM_WIFI,
|
||||
}comms_medium_t;
|
||||
|
||||
#if (TEMPSTICK_SERVER == 1)
|
||||
|
||||
|
||||
/*HTTP POST header component for tempstick server*/
|
||||
#define END_POINT_POSTING1 "POST / HTTP/1.1\r\n"
|
||||
#define END_POINT_AUTHERIZATION "Authorization: Bearer 19730e88-886d-4c94-89d6-3066b79b7630\r\n"
|
||||
#define AUTHERIZATION_VALUE "Bearer 19730e88-886d-4c94-89d6-3066b79b7630"
|
||||
#define END_POINT_HOST "Host: testdevice.tempstickapi.com\r\n"
|
||||
#define HOST_VALUE "testdevice.tempstickapi.com"
|
||||
#define END_POINT_USER_AGENT "User-Agent: \"SensorDHT\\/1.0\"\r\n"
|
||||
#define USER_AGENT_VALUE "\"SensorDHT\\/1.0\""
|
||||
#define END_POINT_CONTENT_TYPE "Content-Type: application/json\r\n"
|
||||
#define CONTENT_TYPE_VALUE "application/json"
|
||||
#define END_POINT_CONTENT_LEN "Content-Length: %u\r\n\r\n"
|
||||
|
||||
#define HTTP_POST_HEADER END_POINT_POSTING1 END_POINT_AUTHERIZATION END_POINT_HOST END_POINT_USER_AGENT END_POINT_CONTENT_TYPE END_POINT_CONTENT_LEN
|
||||
|
||||
|
||||
// URL to post on servers 1, 2, and 3
|
||||
#define MCU_POST_URL1 MCU_BASE_URL1
|
||||
#define MCU_POST_URL2 MCU_BASE_URL2
|
||||
#define MCU_POST_URL3 MCU_BASE_URL3
|
||||
|
||||
//URL to get configuration flags from servers 1, 2, and 3
|
||||
#define MCU_GET_CONFIG_FLAGS_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL1//config/flag/server1
|
||||
#define MCU_GET_CONFIG_FLAGS_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL2//config/flag/server2
|
||||
#define MCU_GET_CONFIG_FLAGS_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL3//config/flag/server3
|
||||
|
||||
/* this endpoint not implemented yet in the code (customer pending) */
|
||||
// if the config_update flag is set
|
||||
// URL to get settings from servers 1, 2, and 3
|
||||
#define MCU_GET_SETTINGS_URL1 TESTING_SERVER_BASE_URL//settings/server1
|
||||
#define MCU_GET_SETTINGS_URL2 TESTING_SERVER_BASE_URL//settings/server2
|
||||
#define MCU_GET_SETTINGS_URL3 TESTING_SERVER_BASE_URL//settings/server3
|
||||
|
||||
//URL to get MCU FW version from servers 1, 2, and 3
|
||||
#define MCU_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"/*MCU_BASE_URL1//mcu/fw/version/server1*/
|
||||
#define MCU_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL2//mcu/fw/version/server2
|
||||
#define MCU_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL3//mcu/fw/version/server3
|
||||
|
||||
//URL to get MCU FW bin file from servers 1, 2, and 3
|
||||
#define MCU_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL1//mcu/fw/bin/file/server1
|
||||
#define MCU_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL2//mcu/fw/bin/file/server2
|
||||
#define MCU_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL3//mcu/fw/bin/file/server3
|
||||
|
||||
//URL to get MODEM FW version from servers 1, 2, and 3
|
||||
#define MODEM_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL1//modem/fw/version/server1
|
||||
#define MODEM_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL2//modem/fw/version/server2
|
||||
#define MODEM_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL3//modem/fw/version/server3
|
||||
|
||||
//URL to get MODEM FW bin file from servers 1, 2, and 3
|
||||
#define MODEM_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL1//modem/fw/bin/file/server1
|
||||
#define MODEM_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL2//modem/fw/bin/file/server2
|
||||
#define MODEM_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL3//modem/fw/bin/file/server3
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#if (TESTING_SERVER==1)
|
||||
|
||||
/*HTTP POST header component for Partha server*/
|
||||
#define END_POINT_POSTING1 "POST /hae/tempstick/send HTTP/1.1\r\n"
|
||||
#define END_POINT_AUTHERIZATION "Authorization: Bearer 19730e88-886d-4c94-89d6-3066b79b7630\r\n"
|
||||
#define AUTHERIZATION_VALUE "Bearer 19730e88-886d-4c94-89d6-3066b79b7630"
|
||||
#define END_POINT_HOST "Host: 3.90.70.38:8080\r\n"
|
||||
#define HOST_VALUE "3.90.70.38:8080"
|
||||
#define END_POINT_USER_AGENT "User-Agent: \"SensorDHT\\/1.0\"\r\n"
|
||||
#define USER_AGENT_VALUE "\"SensorDHT\\/1.0\""
|
||||
#define END_POINT_CONTENT_TYPE "Content-Type: application/json\r\n"
|
||||
#define CONTENT_TYPE_VALUE "application/json"
|
||||
#define END_POINT_CONTENT_LEN "Content-Length: %u\r\n\r\n"
|
||||
/*HTTP POST header */
|
||||
#define HTTP_POST_HEADER END_POINT_POSTING1 END_POINT_AUTHERIZATION END_POINT_HOST END_POINT_USER_AGENT END_POINT_CONTENT_TYPE END_POINT_CONTENT_LEN
|
||||
|
||||
|
||||
// URL to post on servers 1, 2, and 3
|
||||
#define MCU_POST_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/send"
|
||||
#define MCU_POST_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/send"
|
||||
#define MCU_POST_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/send"
|
||||
|
||||
//URL to get configuration flags from servers 1, 2, and 3
|
||||
#define MCU_GET_CONFIG_FLAGS_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL1//config/flag/server1
|
||||
#define MCU_GET_CONFIG_FLAGS_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL2//config/flag/server2
|
||||
#define MCU_GET_CONFIG_FLAGS_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL3//config/flag/server3
|
||||
|
||||
/* this endpoint not implemented yet in the code (customer pending) */
|
||||
// if the config_update flag is set
|
||||
// URL to get settings from servers 1, 2, and 3
|
||||
#define MCU_GET_SETTINGS_URL1 TESTING_SERVER_BASE_URL//settings/server1
|
||||
#define MCU_GET_SETTINGS_URL2 TESTING_SERVER_BASE_URL//settings/server2
|
||||
#define MCU_GET_SETTINGS_URL3 TESTING_SERVER_BASE_URL//settings/server3
|
||||
|
||||
//URL to get MCU FW version from servers 1, 2, and 3
|
||||
#define MCU_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"/*MCU_BASE_URL1//mcu/fw/version/server1*/
|
||||
#define MCU_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL2//mcu/fw/version/server2
|
||||
#define MCU_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL3//mcu/fw/version/server3
|
||||
|
||||
//URL to get MCU FW bin file from servers 1, 2, and 3
|
||||
#define MCU_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL1//mcu/fw/bin/file/server1
|
||||
#define MCU_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL2//mcu/fw/bin/file/server2
|
||||
#define MCU_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL3//mcu/fw/bin/file/server3
|
||||
|
||||
//URL to get MODEM FW version from servers 1, 2, and 3
|
||||
#define MODEM_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL1//modem/fw/version/server1
|
||||
#define MODEM_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL2//modem/fw/version/server2
|
||||
#define MODEM_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL3//modem/fw/version/server3
|
||||
|
||||
//URL to get MODEM FW bin file from servers 1, 2, and 3
|
||||
#define MODEM_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL1//modem/fw/bin/file/server1
|
||||
#define MODEM_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL2//modem/fw/bin/file/server2
|
||||
#define MODEM_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL3//modem/fw/bin/file/server3
|
||||
|
||||
|
||||
#else
|
||||
/*HTTP POST header component for Partha server*/
|
||||
#define END_POINT_POSTING1 "POST /hae/tempstick/send.php HTTP/1.1\r\n"
|
||||
#define END_POINT_AUTHERIZATION "Authorization: Bearer 19730e88-886d-4c94-89d6-3066b79b7630\r\n"
|
||||
#define END_POINT_HOST "Host: www.parthasarathimishra.com\r\n"
|
||||
#define END_POINT_USER_AGENT "User-Agent: \"SensorDHT\\/1.0\"\r\n"
|
||||
#define END_POINT_CONTENT_TYPE "Content-Type: application/json\r\n"
|
||||
#define END_POINT_CONTENT_LEN "Content-Length: %u\r\n\r\n"
|
||||
/*HTTP POST header */
|
||||
#define HTTP_POST_HEADER END_POINT_POSTING1 END_POINT_AUTHERIZATION END_POINT_HOST END_POINT_USER_AGENT END_POINT_CONTENT_TYPE END_POINT_CONTENT_LEN
|
||||
|
||||
|
||||
// URL to post on servers 1, 2, and 3
|
||||
#define MCU_POST_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/send.php"
|
||||
#define MCU_POST_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/send.php"
|
||||
#define MCU_POST_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/send.php"
|
||||
|
||||
//URL to get configuration flags from servers 1, 2, and 3
|
||||
#define MCU_GET_CONFIG_FLAGS_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL1//config/flag/server1
|
||||
#define MCU_GET_CONFIG_FLAGS_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL2//config/flag/server2
|
||||
#define MCU_GET_CONFIG_FLAGS_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL3//config/flag/server3
|
||||
|
||||
/* this endpoint not implemented yet in the code (customer pending) */
|
||||
// if the config_update flag is set
|
||||
// URL to get settings from servers 1, 2, and 3
|
||||
#define MCU_GET_SETTINGS_URL1 MCU_BASE_URL1//settings/server1
|
||||
#define MCU_GET_SETTINGS_URL2 MCU_BASE_URL2//settings/server2
|
||||
#define MCU_GET_SETTINGS_URL3 MCU_BASE_URL3//settings/server3
|
||||
|
||||
//URL to get MCU FW version from servers 1, 2, and 3
|
||||
#define MCU_FW_VERSION_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"/*MCU_BASE_URL1//mcu/fw/version/server1*/
|
||||
#define MCU_FW_VERSION_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL2//mcu/fw/version/server2
|
||||
#define MCU_FW_VERSION_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL3//mcu/fw/version/server3
|
||||
|
||||
//URL to get MCU FW bin file from servers 1, 2, and 3
|
||||
#define MCU_FW_BIN_FILE_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL1//mcu/fw/bin/file/server1
|
||||
#define MCU_FW_BIN_FILE_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL2//mcu/fw/bin/file/server2
|
||||
#define MCU_FW_BIN_FILE_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL3//mcu/fw/bin/file/server3
|
||||
|
||||
//URL to get MODEM FW version from servers 1, 2, and 3
|
||||
#define MODEM_FW_VERSION_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL1//modem/fw/version/server1
|
||||
#define MODEM_FW_VERSION_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL2//modem/fw/version/server2
|
||||
#define MODEM_FW_VERSION_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL3//modem/fw/version/server3
|
||||
|
||||
//URL to get MODEM FW bin file from servers 1, 2, and 3
|
||||
#define MODEM_FW_BIN_FILE_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL1//modem/fw/bin/file/server1
|
||||
#define MODEM_FW_BIN_FILE_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL2//modem/fw/bin/file/server2
|
||||
#define MODEM_FW_BIN_FILE_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL3//modem/fw/bin/file/server3
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* TEMPSTICK servers URL's */
|
||||
typedef enum {
|
||||
TEMPSTICK_SERVER1=1,
|
||||
TEMPSTICK_SERVER2,
|
||||
TEMPSTICK_SERVER3
|
||||
}serverUrl_t;
|
||||
|
||||
/* Operation complete indication callback */
|
||||
typedef void (*comms_op_cb_t)(int status);
|
||||
|
||||
/* Initialize process. */
|
||||
int comms_init(void);
|
||||
|
||||
/* Start process.
|
||||
* The server is contacted for configuration.
|
||||
* When this processes completes, the callback is notified.
|
||||
*/
|
||||
int comms_start(comms_op_cb_t start_cb, comms_op_cb_t connect_cb);
|
||||
|
||||
/* Stop process. */
|
||||
int comms_stop(void);
|
||||
|
||||
/* Poll process. */
|
||||
int comms_poll(void);
|
||||
|
||||
/* Check if the process needs to be polled. */
|
||||
bool comms_needs_poll(void);
|
||||
|
||||
/* Connect to server once process has completed startup and is idle
|
||||
* Optional callback is invoked when process completes.
|
||||
*/
|
||||
int comms_connect(comms_op_cb_t cb);
|
||||
|
||||
/* Check if the process is idle and not doing any communications. */
|
||||
bool comms_is_idle(void);
|
||||
|
||||
/* Check if the process is started */
|
||||
bool comms_is_started(void);
|
||||
|
||||
/* Get the status of the last communication attempt.
|
||||
* OK - everything completed.
|
||||
* ERROR - something failed.
|
||||
*/
|
||||
int comms_get_status(void);
|
||||
|
||||
#endif /* MAIN_COMMS_H_ */
|
||||
/*
|
||||
* comms.h
|
||||
*
|
||||
* Created on: Jan 16, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_COMMS_H_
|
||||
#define MAIN_COMMS_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "main.h"
|
||||
/* comms.h
|
||||
*
|
||||
* Copyright 2020 HAE Innovations
|
||||
*
|
||||
* Celluar Communications process manager.
|
||||
*
|
||||
* Handles communication to/from server over cell modem,
|
||||
* collecting and sending events from queue, processing configuration
|
||||
* and firmware updates from server, etc.
|
||||
*
|
||||
* Author: E. Ross
|
||||
*/
|
||||
|
||||
#define COMMS_STATUS_UPDATED 1
|
||||
#define COMMS_STATUS_OK 0
|
||||
#define COMMS_STATUS_ERROR -1
|
||||
|
||||
|
||||
#define MCU_BASE_URL1 "https://testdevice.tempstickapi.com"
|
||||
#define MCU_BASE_URL2 "https://testdevice.tempstickapi.com"
|
||||
#define MCU_BASE_URL3 "https://testdevice.tempstickapi.com"
|
||||
#define MCU_BASE_URL4 "http://testdevice.tempstickapi.com"
|
||||
|
||||
|
||||
#define MCU_ONBOARDING_URL "https://device-status.idealsciences%u.com"
|
||||
#define POSTING_ONBOARDING "POST /sensor-account.php HTTP/1.0"
|
||||
#define ON_BOARDING_SERVER_DOMAIN_NAME "device-status.idealsciences.com"
|
||||
#define USER_AGENT_ONBOARDING "SensorDHT/1.0"
|
||||
#define AUTHERIZATION_VALUE_ONBOARDING "Basic ZGV2OmRldjEyMw=="
|
||||
#define CONTENT_TYPE_VALUE_ONBOARDING "application/x-www-form-urlencoded"
|
||||
#define ONBOARDING_SERVER_POSTURL_MAX 3
|
||||
#define ONBOARDING_SERVER_POSTURL_MIN 1
|
||||
|
||||
|
||||
#define PARTHA_SERVER_BASE_URL "https://www.parthasarathimishra.com"
|
||||
#define TESTING_SERVER_BASE_URL "http://3.90.70.38:8080"
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
COMMS_OVER_CELL_WIFI_BACKUP = 1,
|
||||
COMMS_OVER_WIFI_CELL_BACKUP,
|
||||
COMMS_OVER_CELL,
|
||||
COMMS_OVER_WIFI,
|
||||
COMMS_MODE_5 //wifi as default for all readings /cellular for alerts /wifi back up for alerts
|
||||
}comms_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
COMMS_MEDIUM_CELL,
|
||||
COMMS_MEDIUM_WIFI,
|
||||
}comms_medium_t;
|
||||
|
||||
#if (TEMPSTICK_SERVER == 1)
|
||||
|
||||
|
||||
/*HTTP POST header component for tempstick server*/
|
||||
#define END_POINT_POSTING1 "POST / HTTP/1.1\r\n"
|
||||
#define END_POINT_AUTHERIZATION "Authorization: Bearer 19730e88-886d-4c94-89d6-3066b79b7630\r\n"
|
||||
#define AUTHERIZATION_VALUE "Bearer 19730e88-886d-4c94-89d6-3066b79b7630"
|
||||
#define END_POINT_HOST "Host: testdevice.tempstickapi.com\r\n"
|
||||
#define HOST_VALUE "testdevice.tempstickapi.com"
|
||||
#define END_POINT_USER_AGENT "User-Agent: \"SensorDHT\\/1.0\"\r\n"
|
||||
#define USER_AGENT_VALUE "\"SensorDHT\\/1.0\""
|
||||
#define END_POINT_CONTENT_TYPE "Content-Type: application/json\r\n"
|
||||
#define CONTENT_TYPE_VALUE "application/json"
|
||||
#define END_POINT_CONTENT_LEN "Content-Length: %u\r\n\r\n"
|
||||
|
||||
#define HTTP_POST_HEADER END_POINT_POSTING1 END_POINT_AUTHERIZATION END_POINT_HOST END_POINT_USER_AGENT END_POINT_CONTENT_TYPE END_POINT_CONTENT_LEN
|
||||
|
||||
|
||||
// URL to post on servers 1, 2, and 3
|
||||
#define MCU_POST_URL1 MCU_BASE_URL1
|
||||
#define MCU_POST_URL2 MCU_BASE_URL2
|
||||
#define MCU_POST_URL3 MCU_BASE_URL3
|
||||
|
||||
//URL to get configuration flags from servers 1, 2, and 3
|
||||
#define MCU_GET_CONFIG_FLAGS_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL1//config/flag/server1
|
||||
#define MCU_GET_CONFIG_FLAGS_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL2//config/flag/server2
|
||||
#define MCU_GET_CONFIG_FLAGS_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL3//config/flag/server3
|
||||
|
||||
/* this endpoint not implemented yet in the code (customer pending) */
|
||||
// if the config_update flag is set
|
||||
// URL to get settings from servers 1, 2, and 3
|
||||
#define MCU_GET_SETTINGS_URL1 TESTING_SERVER_BASE_URL//settings/server1
|
||||
#define MCU_GET_SETTINGS_URL2 TESTING_SERVER_BASE_URL//settings/server2
|
||||
#define MCU_GET_SETTINGS_URL3 TESTING_SERVER_BASE_URL//settings/server3
|
||||
|
||||
//URL to get MCU FW version from servers 1, 2, and 3
|
||||
#define MCU_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"/*MCU_BASE_URL1//mcu/fw/version/server1*/
|
||||
#define MCU_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL2//mcu/fw/version/server2
|
||||
#define MCU_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL3//mcu/fw/version/server3
|
||||
|
||||
//URL to get MCU FW bin file from servers 1, 2, and 3
|
||||
#define MCU_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL1//mcu/fw/bin/file/server1
|
||||
#define MCU_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL2//mcu/fw/bin/file/server2
|
||||
#define MCU_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL3//mcu/fw/bin/file/server3
|
||||
|
||||
//URL to get MODEM FW version from servers 1, 2, and 3
|
||||
#define MODEM_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL1//modem/fw/version/server1
|
||||
#define MODEM_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL2//modem/fw/version/server2
|
||||
#define MODEM_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL3//modem/fw/version/server3
|
||||
|
||||
//URL to get MODEM FW bin file from servers 1, 2, and 3
|
||||
#define MODEM_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL1//modem/fw/bin/file/server1
|
||||
#define MODEM_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL2//modem/fw/bin/file/server2
|
||||
#define MODEM_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL3//modem/fw/bin/file/server3
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#if (TESTING_SERVER==1)
|
||||
|
||||
/*HTTP POST header component for Partha server*/
|
||||
#define END_POINT_POSTING1 "POST /hae/tempstick/send HTTP/1.1\r\n"
|
||||
#define END_POINT_AUTHERIZATION "Authorization: Bearer 19730e88-886d-4c94-89d6-3066b79b7630\r\n"
|
||||
#define AUTHERIZATION_VALUE "Bearer 19730e88-886d-4c94-89d6-3066b79b7630"
|
||||
#define END_POINT_HOST "Host: 3.90.70.38:8080\r\n"
|
||||
#define HOST_VALUE "3.90.70.38:8080"
|
||||
#define END_POINT_USER_AGENT "User-Agent: \"SensorDHT\\/1.0\"\r\n"
|
||||
#define USER_AGENT_VALUE "\"SensorDHT\\/1.0\""
|
||||
#define END_POINT_CONTENT_TYPE "Content-Type: application/json\r\n"
|
||||
#define CONTENT_TYPE_VALUE "application/json"
|
||||
#define END_POINT_CONTENT_LEN "Content-Length: %u\r\n\r\n"
|
||||
/*HTTP POST header */
|
||||
#define HTTP_POST_HEADER END_POINT_POSTING1 END_POINT_AUTHERIZATION END_POINT_HOST END_POINT_USER_AGENT END_POINT_CONTENT_TYPE END_POINT_CONTENT_LEN
|
||||
|
||||
|
||||
// URL to post on servers 1, 2, and 3
|
||||
#define MCU_POST_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/send"
|
||||
#define MCU_POST_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/send"
|
||||
#define MCU_POST_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/send"
|
||||
|
||||
//URL to get configuration flags from servers 1, 2, and 3
|
||||
#define MCU_GET_CONFIG_FLAGS_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL1//config/flag/server1
|
||||
#define MCU_GET_CONFIG_FLAGS_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL2//config/flag/server2
|
||||
#define MCU_GET_CONFIG_FLAGS_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL3//config/flag/server3
|
||||
|
||||
/* this endpoint not implemented yet in the code (customer pending) */
|
||||
// if the config_update flag is set
|
||||
// URL to get settings from servers 1, 2, and 3
|
||||
#define MCU_GET_SETTINGS_URL1 TESTING_SERVER_BASE_URL//settings/server1
|
||||
#define MCU_GET_SETTINGS_URL2 TESTING_SERVER_BASE_URL//settings/server2
|
||||
#define MCU_GET_SETTINGS_URL3 TESTING_SERVER_BASE_URL//settings/server3
|
||||
|
||||
//URL to get MCU FW version from servers 1, 2, and 3
|
||||
#define MCU_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"/*MCU_BASE_URL1//mcu/fw/version/server1*/
|
||||
#define MCU_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL2//mcu/fw/version/server2
|
||||
#define MCU_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL3//mcu/fw/version/server3
|
||||
|
||||
//URL to get MCU FW bin file from servers 1, 2, and 3
|
||||
#define MCU_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL1//mcu/fw/bin/file/server1
|
||||
#define MCU_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL2//mcu/fw/bin/file/server2
|
||||
#define MCU_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL3//mcu/fw/bin/file/server3
|
||||
|
||||
//URL to get MODEM FW version from servers 1, 2, and 3
|
||||
#define MODEM_FW_VERSION_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL1//modem/fw/version/server1
|
||||
#define MODEM_FW_VERSION_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL2//modem/fw/version/server2
|
||||
#define MODEM_FW_VERSION_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL3//modem/fw/version/server3
|
||||
|
||||
//URL to get MODEM FW bin file from servers 1, 2, and 3
|
||||
#define MODEM_FW_BIN_FILE_URL1 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL1//modem/fw/bin/file/server1
|
||||
#define MODEM_FW_BIN_FILE_URL2 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL2//modem/fw/bin/file/server2
|
||||
#define MODEM_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL3//modem/fw/bin/file/server3
|
||||
|
||||
|
||||
#else
|
||||
/*HTTP POST header component for Partha server*/
|
||||
#define END_POINT_POSTING1 "POST /hae/tempstick/send.php HTTP/1.1\r\n"
|
||||
#define END_POINT_AUTHERIZATION "Authorization: Bearer 19730e88-886d-4c94-89d6-3066b79b7630\r\n"
|
||||
#define END_POINT_HOST "Host: www.parthasarathimishra.com\r\n"
|
||||
#define END_POINT_USER_AGENT "User-Agent: \"SensorDHT\\/1.0\"\r\n"
|
||||
#define END_POINT_CONTENT_TYPE "Content-Type: application/json\r\n"
|
||||
#define END_POINT_CONTENT_LEN "Content-Length: %u\r\n\r\n"
|
||||
/*HTTP POST header */
|
||||
#define HTTP_POST_HEADER END_POINT_POSTING1 END_POINT_AUTHERIZATION END_POINT_HOST END_POINT_USER_AGENT END_POINT_CONTENT_TYPE END_POINT_CONTENT_LEN
|
||||
|
||||
|
||||
// URL to post on servers 1, 2, and 3
|
||||
#define MCU_POST_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/send.php"
|
||||
#define MCU_POST_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/send.php"
|
||||
#define MCU_POST_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/send.php"
|
||||
|
||||
//URL to get configuration flags from servers 1, 2, and 3
|
||||
#define MCU_GET_CONFIG_FLAGS_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL1//config/flag/server1
|
||||
#define MCU_GET_CONFIG_FLAGS_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL2//config/flag/server2
|
||||
#define MCU_GET_CONFIG_FLAGS_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/config/"//MCU_BASE_URL3//config/flag/server3
|
||||
|
||||
/* this endpoint not implemented yet in the code (customer pending) */
|
||||
// if the config_update flag is set
|
||||
// URL to get settings from servers 1, 2, and 3
|
||||
#define MCU_GET_SETTINGS_URL1 MCU_BASE_URL1//settings/server1
|
||||
#define MCU_GET_SETTINGS_URL2 MCU_BASE_URL2//settings/server2
|
||||
#define MCU_GET_SETTINGS_URL3 MCU_BASE_URL3//settings/server3
|
||||
|
||||
//URL to get MCU FW version from servers 1, 2, and 3
|
||||
#define MCU_FW_VERSION_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"/*MCU_BASE_URL1//mcu/fw/version/server1*/
|
||||
#define MCU_FW_VERSION_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL2//mcu/fw/version/server2
|
||||
#define MCU_FW_VERSION_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/version/"//MCU_BASE_URL3//mcu/fw/version/server3
|
||||
|
||||
//URL to get MCU FW bin file from servers 1, 2, and 3
|
||||
#define MCU_FW_BIN_FILE_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL1//mcu/fw/bin/file/server1
|
||||
#define MCU_FW_BIN_FILE_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL2//mcu/fw/bin/file/server2
|
||||
#define MCU_FW_BIN_FILE_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/mcu_pgm_download/OTA.bin"//MCU_BASE_URL3//mcu/fw/bin/file/server3
|
||||
|
||||
//URL to get MODEM FW version from servers 1, 2, and 3
|
||||
#define MODEM_FW_VERSION_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL1//modem/fw/version/server1
|
||||
#define MODEM_FW_VERSION_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL2//modem/fw/version/server2
|
||||
#define MODEM_FW_VERSION_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/version/"//MCU_BASE_URL3//modem/fw/version/server3
|
||||
|
||||
//URL to get MODEM FW bin file from servers 1, 2, and 3
|
||||
#define MODEM_FW_BIN_FILE_URL1 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL1//modem/fw/bin/file/server1
|
||||
#define MODEM_FW_BIN_FILE_URL2 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL2//modem/fw/bin/file/server2
|
||||
#define MODEM_FW_BIN_FILE_URL3 PARTHA_SERVER_BASE_URL"/hae/tempstick/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL3//modem/fw/bin/file/server3
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* TEMPSTICK servers URL's */
|
||||
typedef enum {
|
||||
TEMPSTICK_SERVER1=1,
|
||||
TEMPSTICK_SERVER2,
|
||||
TEMPSTICK_SERVER3
|
||||
}serverUrl_t;
|
||||
|
||||
/* Operation complete indication callback */
|
||||
typedef void (*comms_op_cb_t)(int status);
|
||||
|
||||
/* Initialize process. */
|
||||
int comms_init(void);
|
||||
|
||||
/* Start process.
|
||||
* The server is contacted for configuration.
|
||||
* When this processes completes, the callback is notified.
|
||||
*/
|
||||
int comms_start(comms_op_cb_t start_cb, comms_op_cb_t connect_cb);
|
||||
|
||||
/* Stop process. */
|
||||
int comms_stop(void);
|
||||
|
||||
/* Poll process. */
|
||||
int comms_poll(void);
|
||||
|
||||
/* Check if the process needs to be polled. */
|
||||
bool comms_needs_poll(void);
|
||||
|
||||
/* Connect to server once process has completed startup and is idle
|
||||
* Optional callback is invoked when process completes.
|
||||
*/
|
||||
int comms_connect(comms_op_cb_t cb);
|
||||
|
||||
/* Check if the process is idle and not doing any communications. */
|
||||
bool comms_is_idle(void);
|
||||
|
||||
/* Check if the process is started */
|
||||
bool comms_is_started(void);
|
||||
|
||||
/* Get the status of the last communication attempt.
|
||||
* OK - everything completed.
|
||||
* ERROR - something failed.
|
||||
*/
|
||||
int comms_get_status(void);
|
||||
|
||||
#endif /* MAIN_COMMS_H_ */
|
||||
|
96
main/main.c
96
main/main.c
@ -1,48 +1,48 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include "esp_log.h"
|
||||
#include "nvm.h"
|
||||
#include "port.h"
|
||||
#include "uart_ifx.h"
|
||||
#include "wifi_webServer.h"
|
||||
#include "comms.h"
|
||||
#include "main.h"
|
||||
|
||||
static const char* TAG = "MAIN";
|
||||
uint8_t comms_mode = DEFAULT_COMMS_MODE;
|
||||
void app_main(void)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG,"*** Starting app_main ***");
|
||||
|
||||
/* Initialize the dedicated NVS partition */
|
||||
nvm_init();
|
||||
port_init();
|
||||
uart_ifx_init();
|
||||
|
||||
/* Create the UART tasks for both UART0 and UART1 */
|
||||
uart_create_rx_tasks();
|
||||
|
||||
/* read the comms-mode from the NVS (if any) */
|
||||
comms_mode = nvm_read_comms_mode();
|
||||
|
||||
if((COMMS_OVER_CELL != comms_mode))
|
||||
{
|
||||
/* Start On-Boarding process */
|
||||
webserver_start();
|
||||
|
||||
/* Wait till the onboarding process is completed */
|
||||
while(!webserver_get_status())
|
||||
{
|
||||
//ESP_LOGI(TAG," -------------> PASSWORD is: %s",wifi_get_pswd());
|
||||
vTaskDelay(750/portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG," -------------> Wifi Connected ... :)");
|
||||
while(1);
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include "esp_log.h"
|
||||
#include "nvm.h"
|
||||
#include "port.h"
|
||||
#include "uart_ifx.h"
|
||||
#include "wifi_webServer.h"
|
||||
#include "comms.h"
|
||||
#include "main.h"
|
||||
|
||||
static const char* TAG = "MAIN";
|
||||
uint8_t comms_mode = DEFAULT_COMMS_MODE;
|
||||
void app_main(void)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG,"*** Starting app_main ***");
|
||||
|
||||
/* Initialize the dedicated NVS partition */
|
||||
nvm_init();
|
||||
port_init();
|
||||
uart_ifx_init();
|
||||
|
||||
/* Create the UART tasks for both UART0 and UART1 */
|
||||
uart_create_rx_tasks();
|
||||
|
||||
/* read the comms-mode from the NVS (if any) */
|
||||
comms_mode = nvm_read_comms_mode();
|
||||
|
||||
if((COMMS_OVER_CELL != comms_mode))
|
||||
{
|
||||
/* Start On-Boarding process */
|
||||
webserver_start();
|
||||
|
||||
/* Wait till the onboarding process is completed */
|
||||
while(!webserver_get_status())
|
||||
{
|
||||
//ESP_LOGI(TAG," -------------> PASSWORD is: %s",wifi_get_pswd());
|
||||
vTaskDelay(750/portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG," -------------> Wifi Connected ... :)");
|
||||
|
||||
while(1);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
2234
main/nvm.c
2234
main/nvm.c
File diff suppressed because it is too large
Load Diff
148
main/nvm.h
148
main/nvm.h
@ -1,74 +1,74 @@
|
||||
/*
|
||||
* nvm.h
|
||||
*
|
||||
* Created on: Aug 14, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_NVM_H_
|
||||
#define MAIN_NVM_H_
|
||||
|
||||
#include "data_processing.h"
|
||||
|
||||
#define NVM_CELL_ONBOARDING_KEY "on_boarding"
|
||||
#define NVM_WIFI_ONBOARDING_KEY "wifi_onboarding"
|
||||
#define NVM_WIFI_SSID_KEY "wifi_ssid"
|
||||
#define NVM_WIFI_SSID_LENGTH_KEY "wifi_ssid_len"
|
||||
#define NVM_WIFI_PSWD_KEY "wifi_pswd"
|
||||
#define NVM_WIFI_PSWD_LENGTH_KEY "wifi_pswd_len"
|
||||
#define NVM_COMMS_MODE_KEY "commsMode"
|
||||
#define NVM_HISTORY_DATA_KEY "history_data%u"
|
||||
|
||||
#define NVM_HISTORY_SECTOR1 "history_data1"
|
||||
#define NVM_HISTORY_SECTOR2 "history_data2"
|
||||
#define NVM_HISTORY_SECTOR3 "history_data3"
|
||||
#define NVM_HISTORY_SECTOR4 "history_data4"
|
||||
#define NVM_HISTORY_SECTOR5 "history_data5"
|
||||
#define NVM_HISTORY_SECTOR6 "history_data6"
|
||||
#define NVM_HISTORY_SECTOR7 "history_data7"
|
||||
#define NVM_HISTORY_SECTOR8 "history_data8"
|
||||
|
||||
#define NVM_ONBOARDING_SET_VAL 1
|
||||
#define NVM_ONBOARDING_NOT_SET_VAL 0
|
||||
#define NVM_ONBOARDING_NOT_STORED_VAL 2
|
||||
|
||||
#define NVM_MAX_NUMBER_OF_ALL_READINGS 750 //max number of sensor_readings in all NVM sector
|
||||
#define NVM_MAX_NUMBER_OF_IN_ONE_SECTOR 20 //max number of readings in one NVM/NVS sector
|
||||
#define NVM_NUMBER_OF_SECTORS ((NVM_MAX_NUMBER_OF_ALL_READINGS) / NVM_MAX_NUMBER_OF_IN_ONE_SECTOR) // ----> number_of_sectors = max_num_readings / number of readings in one NVM sector
|
||||
#define NVM_ONE_SECOTR_SIZE (37 * (NVM_NUMBER_OF_SECTORS))
|
||||
|
||||
#define NVM_HISTORY_ALL_SECTORS (NVM_NUMBER_OF_SECTORS+1)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIFI_ONBOARDING_KEY,
|
||||
CELL_ONBOARDING_KEY
|
||||
}onboarding_type_t;
|
||||
|
||||
|
||||
void nvm_init(void);
|
||||
void nvm_clear(void);
|
||||
|
||||
|
||||
uint8_t nvm_read_onboarding_flag(onboarding_type_t flag_key);
|
||||
void nvm_write_onboarding_flag(onboarding_type_t flag_key, uint8_t flag_value);
|
||||
|
||||
|
||||
uint8_t nvm_read_comms_mode(void);
|
||||
void nvm_write_comms_mode(uint8_t commsMode);
|
||||
|
||||
|
||||
esp_err_t nvm_read_wifi_credentials(char* ssid, char* pswd);
|
||||
void nvm_write_wifi_credentials(char* ssid, uint8_t ssid_len, char* pswd, uint8_t pswd_len);
|
||||
|
||||
|
||||
void nvm_write_history_data(historyLog_t* history_data);
|
||||
size_t nvm_read_history_data(historyLog_t* history_data, uint8_t sector_number);
|
||||
void nvm_clear_history_sector(uint8_t sector_num);
|
||||
uint8_t nvm_get_last_written_history_sector(void);
|
||||
|
||||
|
||||
void nvm_set_last_posted_history_sector(uint8_t sector_num);
|
||||
uint8_t nvm_get_last_posted_history_sector(void);
|
||||
|
||||
#endif /* MAIN_NVM_H_ */
|
||||
/*
|
||||
* nvm.h
|
||||
*
|
||||
* Created on: Aug 14, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_NVM_H_
|
||||
#define MAIN_NVM_H_
|
||||
|
||||
#include "data_processing.h"
|
||||
|
||||
#define NVM_CELL_ONBOARDING_KEY "on_boarding"
|
||||
#define NVM_WIFI_ONBOARDING_KEY "wifi_onboarding"
|
||||
#define NVM_WIFI_SSID_KEY "wifi_ssid"
|
||||
#define NVM_WIFI_SSID_LENGTH_KEY "wifi_ssid_len"
|
||||
#define NVM_WIFI_PSWD_KEY "wifi_pswd"
|
||||
#define NVM_WIFI_PSWD_LENGTH_KEY "wifi_pswd_len"
|
||||
#define NVM_COMMS_MODE_KEY "commsMode"
|
||||
#define NVM_HISTORY_DATA_KEY "history_data%u"
|
||||
|
||||
#define NVM_HISTORY_SECTOR1 "history_data1"
|
||||
#define NVM_HISTORY_SECTOR2 "history_data2"
|
||||
#define NVM_HISTORY_SECTOR3 "history_data3"
|
||||
#define NVM_HISTORY_SECTOR4 "history_data4"
|
||||
#define NVM_HISTORY_SECTOR5 "history_data5"
|
||||
#define NVM_HISTORY_SECTOR6 "history_data6"
|
||||
#define NVM_HISTORY_SECTOR7 "history_data7"
|
||||
#define NVM_HISTORY_SECTOR8 "history_data8"
|
||||
|
||||
#define NVM_ONBOARDING_SET_VAL 1
|
||||
#define NVM_ONBOARDING_NOT_SET_VAL 0
|
||||
#define NVM_ONBOARDING_NOT_STORED_VAL 2
|
||||
|
||||
#define NVM_MAX_NUMBER_OF_ALL_READINGS 750 //max number of sensor_readings in all NVM sector
|
||||
#define NVM_MAX_NUMBER_OF_IN_ONE_SECTOR 20 //max number of readings in one NVM/NVS sector
|
||||
#define NVM_NUMBER_OF_SECTORS ((NVM_MAX_NUMBER_OF_ALL_READINGS) / NVM_MAX_NUMBER_OF_IN_ONE_SECTOR) // ----> number_of_sectors = max_num_readings / number of readings in one NVM sector
|
||||
#define NVM_ONE_SECOTR_SIZE (37 * (NVM_NUMBER_OF_SECTORS))
|
||||
|
||||
#define NVM_HISTORY_ALL_SECTORS (NVM_NUMBER_OF_SECTORS+1)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIFI_ONBOARDING_KEY,
|
||||
CELL_ONBOARDING_KEY
|
||||
}onboarding_type_t;
|
||||
|
||||
|
||||
void nvm_init(void);
|
||||
void nvm_clear(void);
|
||||
|
||||
|
||||
uint8_t nvm_read_onboarding_flag(onboarding_type_t flag_key);
|
||||
void nvm_write_onboarding_flag(onboarding_type_t flag_key, uint8_t flag_value);
|
||||
|
||||
|
||||
uint8_t nvm_read_comms_mode(void);
|
||||
void nvm_write_comms_mode(uint8_t commsMode);
|
||||
|
||||
|
||||
esp_err_t nvm_read_wifi_credentials(char* ssid, char* pswd);
|
||||
void nvm_write_wifi_credentials(char* ssid, uint8_t ssid_len, char* pswd, uint8_t pswd_len);
|
||||
|
||||
|
||||
void nvm_write_history_data(historyLog_t* history_data);
|
||||
size_t nvm_read_history_data(historyLog_t* history_data, uint8_t sector_number);
|
||||
void nvm_clear_history_sector(uint8_t sector_num);
|
||||
uint8_t nvm_get_last_written_history_sector(void);
|
||||
|
||||
|
||||
void nvm_set_last_posted_history_sector(uint8_t sector_num);
|
||||
uint8_t nvm_get_last_posted_history_sector(void);
|
||||
|
||||
#endif /* MAIN_NVM_H_ */
|
||||
|
848
main/ota.c
848
main/ota.c
@ -1,424 +1,424 @@
|
||||
/*
|
||||
* ota.c
|
||||
*
|
||||
* Created on: Feb 13, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
|
||||
/* OTA example
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_app_format.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_flash_partitions.h"
|
||||
#include "esp_partition.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "ota.h"
|
||||
|
||||
|
||||
#define BUFFSIZE 2048
|
||||
#define HASH_LEN 32 /* SHA-256 digest length */
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
||||
|
||||
static const char *TAG = "OTA";
|
||||
|
||||
/*an ota data write buffer ready to write to the flash*/
|
||||
static char *ota_write_data;
|
||||
static ota_bytes_status_t ota_bytes = OTA_BYTES_WRITTEN_WAIT;
|
||||
static uint32_t ota_file_size = OTA_WITH_SEQUENTIAL_WRITES;
|
||||
|
||||
esp_ota_handle_t update_handle;
|
||||
const esp_partition_t *update_partition = NULL;
|
||||
const esp_partition_t *configured = NULL;
|
||||
const esp_partition_t *running = NULL;
|
||||
|
||||
static uint8_t ota_needed = OTA_NOT_NEEDED;
|
||||
static int data_read = -1;
|
||||
int binary_file_length;
|
||||
bool image_header_was_checked;
|
||||
|
||||
#define OTA_URL_SIZE 256
|
||||
|
||||
|
||||
/******************************************************/
|
||||
/********** Static Functions for OTA-Process **********/
|
||||
/******************************************************/
|
||||
static void print_sha256 (const uint8_t *image_hash, const char *label)
|
||||
{
|
||||
char hash_print[HASH_LEN * 2 + 1];
|
||||
hash_print[HASH_LEN * 2] = 0;
|
||||
for (int i = 0; i < HASH_LEN; ++i) {
|
||||
sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
|
||||
}
|
||||
ESP_LOGI(TAG, "%s: %s", label, hash_print);
|
||||
}
|
||||
|
||||
static void ota_start_session(void)
|
||||
{
|
||||
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
|
||||
update_handle = 0;
|
||||
update_partition = NULL;
|
||||
|
||||
/* Get partition info of currently configured boot app (the partition for current app)*/
|
||||
configured = esp_ota_get_boot_partition();
|
||||
/* Get partition info of currently running app (the partition for current app)*/
|
||||
running = esp_ota_get_running_partition();
|
||||
|
||||
|
||||
ESP_LOGI(TAG, "Starting OTA session");
|
||||
|
||||
/* The result of esp_ota_get_boot_partition() is usually the same as esp_ota_get_running_partition().
|
||||
* The two results are not equal if the configured boot partition does not contain a valid app (meaning that
|
||||
* the running partition will be an app that the bootloader chose via fallback) */
|
||||
if (configured != running) {
|
||||
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
|
||||
(unsigned int)configured->address, (unsigned int)running->address);
|
||||
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
|
||||
}
|
||||
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
|
||||
running->type, running->subtype, (unsigned int)running->address);
|
||||
|
||||
/* The following line returns the next OTA app partition which should be written with a new firmware*/
|
||||
/* So, update_partition is the partition where the new image will be written*/
|
||||
update_partition = esp_ota_get_next_update_partition(NULL);
|
||||
|
||||
assert(update_partition != NULL);
|
||||
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
|
||||
update_partition->subtype, (unsigned int)update_partition->address);
|
||||
|
||||
/*deal with all receive packet*/
|
||||
image_header_was_checked = false;
|
||||
|
||||
}
|
||||
|
||||
static void ota_process_incoming_image(void)
|
||||
{
|
||||
esp_err_t err;
|
||||
|
||||
if ((data_read < 0) && (OTA_NEEDED == ota_needed)) {
|
||||
ESP_LOGE(TAG, "Error: No OTA data have been received yet");
|
||||
}
|
||||
else if ((data_read > 0) && (OTA_NEEDED == ota_needed)) {
|
||||
if (image_header_was_checked == false) {
|
||||
esp_app_desc_t new_app_info;
|
||||
if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
|
||||
// check current version with downloading
|
||||
/* 1- Check the firmware version of the new fetched-app*/
|
||||
memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
|
||||
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
|
||||
|
||||
/* 2- Check the firmware version of the current running app*/
|
||||
esp_app_desc_t running_app_info;
|
||||
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
|
||||
}
|
||||
|
||||
/* 3- Check the firmware version of the last disabled/aborted/invalid app (which is an app that )*/
|
||||
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
|
||||
esp_app_desc_t invalid_app_info;
|
||||
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
|
||||
}
|
||||
|
||||
// check current version with last invalid partition
|
||||
/* 4- Compare the new fetched-image with the last invalid-image*/
|
||||
if (last_invalid_app != NULL) {
|
||||
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGW(TAG, "New version is the same as invalid version.");
|
||||
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
|
||||
ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 5- Compare the new-fetched-image with the current running-image*/
|
||||
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 7- Start an OTA-update writing to the specified partition.
|
||||
The specified partition is erased to the specified image size.
|
||||
If image size is not yet known, pass OTA_SIZE_UNKNOWN which will cause the entire partition to be erased.*/
|
||||
err = esp_ota_begin(update_partition, ota_file_size, &update_handle);
|
||||
if (err != ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 6- Set the image_header_checked flag to be true*/
|
||||
image_header_was_checked = true;
|
||||
|
||||
ESP_LOGI(TAG, "esp_ota_begin succeeded");
|
||||
} else {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGE(TAG, "received package is not fit len");
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* 8- Write OTA update data to partition.
|
||||
* This function can be called multiple times as data is received during the OTA operation.
|
||||
* Data is written sequentially to the partition */
|
||||
err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
|
||||
if (err != ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
/* 9- Set the binary file length*/
|
||||
binary_file_length += data_read;
|
||||
/* Reset data_read variable to 0*/
|
||||
data_read = 0;
|
||||
ESP_LOGD(TAG, "Written bytes length %d", binary_file_length);
|
||||
ota_bytes = OTA_BYTES_WRITTEN_SUCCESS;
|
||||
}
|
||||
else if ((data_read == 0) && (OTA_FINISHED == ota_needed)) {
|
||||
ESP_LOGI(TAG, "OTA session finished");
|
||||
ota_bytes = OTA_BYTES_WRITTEN_SUCCESS;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void ota_end_session(void)
|
||||
{
|
||||
esp_err_t err;
|
||||
if(OTA_BYTES_WRITTEN_SUCCESS == ota_bytes)
|
||||
{
|
||||
ESP_LOGI(TAG, "Total Write binary data length: %d", binary_file_length);
|
||||
/* 10- Checks if entire data in the response has been read without any error */
|
||||
if ( OTA_FINISHED != ota_needed ) {
|
||||
ESP_LOGI(TAG, "OTA session stopped for errors but didn't get finished");
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
/* EXIT OTA task*/
|
||||
return ;
|
||||
}
|
||||
|
||||
/* 11- Finish OTA update and validate newly written app image*/
|
||||
err = esp_ota_end(update_handle);
|
||||
if (err != ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
|
||||
ESP_LOGI(TAG, "Image validation failed, image is corrupted");
|
||||
} else {
|
||||
ESP_LOGI(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
|
||||
}
|
||||
/* EXIT OTA task*/
|
||||
return ;
|
||||
}
|
||||
|
||||
/* 12- Set the new validated-written image to be the boot partition (Configure OTA data for a new boot partition)
|
||||
* On the next restart, ESP will boot from that new partition*/
|
||||
err = esp_ota_set_boot_partition(update_partition);
|
||||
if (err != ESP_OK) {
|
||||
/*Stop OTA update*/
|
||||
ESP_LOGI(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
|
||||
|
||||
/* EXIT OTA task*/
|
||||
return ;
|
||||
}
|
||||
/* 13- Restart the device (ESP32)*/
|
||||
ESP_LOGI(TAG, "Prepare to restart system!");
|
||||
esp_restart();
|
||||
return ;
|
||||
}
|
||||
else if(OTA_BYTES_WRITTEN_FAILED == ota_bytes)
|
||||
{
|
||||
ESP_LOGI(TAG,"No Changing in boot_partition");
|
||||
}
|
||||
}
|
||||
|
||||
static bool diagnostic(void)
|
||||
{
|
||||
/* gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
ESP_LOGI(TAG, "Diagnostics (5 sec)...");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
|
||||
bool diagnostic_is_ok = gpio_get_level(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
|
||||
|
||||
gpio_reset_pin(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);*/
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ota_set_needed(void)
|
||||
{
|
||||
ota_needed = OTA_NEEDED;
|
||||
}
|
||||
|
||||
void ota_set_not_needed(void)
|
||||
{
|
||||
ota_needed = OTA_NOT_NEEDED;
|
||||
}
|
||||
|
||||
void ota_set_finished(void)
|
||||
{
|
||||
ota_needed = OTA_FINISHED;
|
||||
}
|
||||
|
||||
/******************************************************/
|
||||
/* Functions that gonna be used in comms state machine*/
|
||||
/******************************************************/
|
||||
esp_err_t ota_init(uint32_t fileSize)
|
||||
{
|
||||
/*This function should be invoked in the ota_file_open_cb() in comms.c (instead of the OTA_Offline_Init())*/
|
||||
uint8_t sha_256[HASH_LEN] = { 0 };
|
||||
esp_partition_t partition;
|
||||
|
||||
// get sha256 digest for the partition table
|
||||
partition.address = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_MAX_LEN;
|
||||
partition.type = ESP_PARTITION_TYPE_DATA;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for the partition table: ");
|
||||
|
||||
// get sha256 digest for bootloader
|
||||
partition.address = ESP_BOOTLOADER_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.type = ESP_PARTITION_TYPE_APP;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for bootloader: ");
|
||||
|
||||
// get sha256 digest for running partition
|
||||
esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for current firmware: ");
|
||||
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
esp_ota_img_states_t ota_state;
|
||||
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
|
||||
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
|
||||
// run diagnostic function ...
|
||||
bool diagnostic_is_ok = diagnostic();
|
||||
if (diagnostic_is_ok) {
|
||||
ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution ...");
|
||||
esp_ota_mark_app_valid_cancel_rollback();
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version ...");
|
||||
esp_ota_mark_app_invalid_rollback_and_reboot();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*set the ota file size */
|
||||
if(0 == fileSize)
|
||||
fileSize = OTA_WITH_SEQUENTIAL_WRITES;
|
||||
else
|
||||
ota_file_size = fileSize;
|
||||
|
||||
/* Initialize OTA session*/
|
||||
ota_start_session();
|
||||
|
||||
/* */
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ota_get_data_to_buffer(const char* buf, int buf_len)
|
||||
{
|
||||
/* This function should be called in comms.c inside the callback function ota_file_data_cb() */
|
||||
/* This function should copy the bytes that we get from qfread command via uart*/
|
||||
/* handle of qfread cmd stores the incoming bytes from modem via uart in buf */
|
||||
/* After that we gonna use ota-buffer in ota_process_incoming_image() function to start ota-session*/
|
||||
|
||||
esp_err_t retval = ESP_FAIL;
|
||||
int index = 0;
|
||||
|
||||
/*Check if the number of bytes to be processed is greater than ota-buffer size*/
|
||||
/*if(BUFFSIZE < buf_len)
|
||||
{
|
||||
ESP_LOGI(TAG, "Number of bytes to be processed is greater than the size of OTA buffer");
|
||||
return retval;
|
||||
}*/
|
||||
|
||||
/* Set the status of ota-bytes to be processed ---> OTA_BYTES_WRITTEN_WAIT*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_WAIT;
|
||||
|
||||
/* Reset the number of bytes that have been read to 0 (because the initial value is -1)*/
|
||||
data_read = 0;
|
||||
|
||||
/*Copy the incoming bytes (from uart) to ota-buffer */
|
||||
//for(index = 0; index < buf_len; index++)
|
||||
{
|
||||
ota_write_data = (char*)buf;
|
||||
data_read = buf_len;
|
||||
}
|
||||
|
||||
/*Start OTA_Writing session*/
|
||||
ota_set_needed();
|
||||
|
||||
/*OTA process and writing for the copied bytes*/
|
||||
ota_process_incoming_image();
|
||||
|
||||
/* Check the status of the bytes after OTA-processing/OTA-writing */
|
||||
if(OTA_BYTES_WRITTEN_SUCCESS == ota_bytes)
|
||||
retval = ESP_OK;
|
||||
else if(OTA_BYTES_WRITTEN_FAILED == ota_bytes)
|
||||
retval = ESP_FAIL;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void ota_finish_processing(void)
|
||||
{
|
||||
/*This function should be invoked in the COMMS_STATE_WAIT_MCU_UPDATE state in comms state machine
|
||||
* (instead of the OTA_terminate_connection() )*/
|
||||
|
||||
/* Reset data_read variable to 0*/
|
||||
data_read = 0;
|
||||
|
||||
/* Rise ota_finish flag*/
|
||||
ota_set_finished();
|
||||
|
||||
/* Rise the bytes_success flag*/
|
||||
ota_process_incoming_image();
|
||||
|
||||
/* End the ota_session by validating the image and then rebooting */
|
||||
ota_end_session();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* ota.c
|
||||
*
|
||||
* Created on: Feb 13, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
|
||||
/* OTA example
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_app_format.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_flash_partitions.h"
|
||||
#include "esp_partition.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "ota.h"
|
||||
|
||||
|
||||
#define BUFFSIZE 2048
|
||||
#define HASH_LEN 32 /* SHA-256 digest length */
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
||||
|
||||
static const char *TAG = "OTA";
|
||||
|
||||
/*an ota data write buffer ready to write to the flash*/
|
||||
static char *ota_write_data;
|
||||
static ota_bytes_status_t ota_bytes = OTA_BYTES_WRITTEN_WAIT;
|
||||
static uint32_t ota_file_size = OTA_WITH_SEQUENTIAL_WRITES;
|
||||
|
||||
esp_ota_handle_t update_handle;
|
||||
const esp_partition_t *update_partition = NULL;
|
||||
const esp_partition_t *configured = NULL;
|
||||
const esp_partition_t *running = NULL;
|
||||
|
||||
static uint8_t ota_needed = OTA_NOT_NEEDED;
|
||||
static int data_read = -1;
|
||||
int binary_file_length;
|
||||
bool image_header_was_checked;
|
||||
|
||||
#define OTA_URL_SIZE 256
|
||||
|
||||
|
||||
/******************************************************/
|
||||
/********** Static Functions for OTA-Process **********/
|
||||
/******************************************************/
|
||||
static void print_sha256 (const uint8_t *image_hash, const char *label)
|
||||
{
|
||||
char hash_print[HASH_LEN * 2 + 1];
|
||||
hash_print[HASH_LEN * 2] = 0;
|
||||
for (int i = 0; i < HASH_LEN; ++i) {
|
||||
sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
|
||||
}
|
||||
ESP_LOGI(TAG, "%s: %s", label, hash_print);
|
||||
}
|
||||
|
||||
static void ota_start_session(void)
|
||||
{
|
||||
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
|
||||
update_handle = 0;
|
||||
update_partition = NULL;
|
||||
|
||||
/* Get partition info of currently configured boot app (the partition for current app)*/
|
||||
configured = esp_ota_get_boot_partition();
|
||||
/* Get partition info of currently running app (the partition for current app)*/
|
||||
running = esp_ota_get_running_partition();
|
||||
|
||||
|
||||
ESP_LOGI(TAG, "Starting OTA session");
|
||||
|
||||
/* The result of esp_ota_get_boot_partition() is usually the same as esp_ota_get_running_partition().
|
||||
* The two results are not equal if the configured boot partition does not contain a valid app (meaning that
|
||||
* the running partition will be an app that the bootloader chose via fallback) */
|
||||
if (configured != running) {
|
||||
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
|
||||
(unsigned int)configured->address, (unsigned int)running->address);
|
||||
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
|
||||
}
|
||||
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
|
||||
running->type, running->subtype, (unsigned int)running->address);
|
||||
|
||||
/* The following line returns the next OTA app partition which should be written with a new firmware*/
|
||||
/* So, update_partition is the partition where the new image will be written*/
|
||||
update_partition = esp_ota_get_next_update_partition(NULL);
|
||||
|
||||
assert(update_partition != NULL);
|
||||
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
|
||||
update_partition->subtype, (unsigned int)update_partition->address);
|
||||
|
||||
/*deal with all receive packet*/
|
||||
image_header_was_checked = false;
|
||||
|
||||
}
|
||||
|
||||
static void ota_process_incoming_image(void)
|
||||
{
|
||||
esp_err_t err;
|
||||
|
||||
if ((data_read < 0) && (OTA_NEEDED == ota_needed)) {
|
||||
ESP_LOGE(TAG, "Error: No OTA data have been received yet");
|
||||
}
|
||||
else if ((data_read > 0) && (OTA_NEEDED == ota_needed)) {
|
||||
if (image_header_was_checked == false) {
|
||||
esp_app_desc_t new_app_info;
|
||||
if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
|
||||
// check current version with downloading
|
||||
/* 1- Check the firmware version of the new fetched-app*/
|
||||
memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
|
||||
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
|
||||
|
||||
/* 2- Check the firmware version of the current running app*/
|
||||
esp_app_desc_t running_app_info;
|
||||
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
|
||||
}
|
||||
|
||||
/* 3- Check the firmware version of the last disabled/aborted/invalid app (which is an app that )*/
|
||||
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
|
||||
esp_app_desc_t invalid_app_info;
|
||||
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
|
||||
}
|
||||
|
||||
// check current version with last invalid partition
|
||||
/* 4- Compare the new fetched-image with the last invalid-image*/
|
||||
if (last_invalid_app != NULL) {
|
||||
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGW(TAG, "New version is the same as invalid version.");
|
||||
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
|
||||
ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 5- Compare the new-fetched-image with the current running-image*/
|
||||
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 7- Start an OTA-update writing to the specified partition.
|
||||
The specified partition is erased to the specified image size.
|
||||
If image size is not yet known, pass OTA_SIZE_UNKNOWN which will cause the entire partition to be erased.*/
|
||||
err = esp_ota_begin(update_partition, ota_file_size, &update_handle);
|
||||
if (err != ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 6- Set the image_header_checked flag to be true*/
|
||||
image_header_was_checked = true;
|
||||
|
||||
ESP_LOGI(TAG, "esp_ota_begin succeeded");
|
||||
} else {
|
||||
/*---> Stop OTA updating*/
|
||||
ESP_LOGE(TAG, "received package is not fit len");
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* 8- Write OTA update data to partition.
|
||||
* This function can be called multiple times as data is received during the OTA operation.
|
||||
* Data is written sequentially to the partition */
|
||||
err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
|
||||
if (err != ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
|
||||
/* EXIT OTA task*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_FAILED;
|
||||
return;
|
||||
}
|
||||
/* 9- Set the binary file length*/
|
||||
binary_file_length += data_read;
|
||||
/* Reset data_read variable to 0*/
|
||||
data_read = 0;
|
||||
ESP_LOGD(TAG, "Written bytes length %d", binary_file_length);
|
||||
ota_bytes = OTA_BYTES_WRITTEN_SUCCESS;
|
||||
}
|
||||
else if ((data_read == 0) && (OTA_FINISHED == ota_needed)) {
|
||||
ESP_LOGI(TAG, "OTA session finished");
|
||||
ota_bytes = OTA_BYTES_WRITTEN_SUCCESS;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void ota_end_session(void)
|
||||
{
|
||||
esp_err_t err;
|
||||
if(OTA_BYTES_WRITTEN_SUCCESS == ota_bytes)
|
||||
{
|
||||
ESP_LOGI(TAG, "Total Write binary data length: %d", binary_file_length);
|
||||
/* 10- Checks if entire data in the response has been read without any error */
|
||||
if ( OTA_FINISHED != ota_needed ) {
|
||||
ESP_LOGI(TAG, "OTA session stopped for errors but didn't get finished");
|
||||
esp_ota_abort(update_handle);
|
||||
ESP_LOGI(TAG, "Aborting the update-data partition");
|
||||
/* EXIT OTA task*/
|
||||
return ;
|
||||
}
|
||||
|
||||
/* 11- Finish OTA update and validate newly written app image*/
|
||||
err = esp_ota_end(update_handle);
|
||||
if (err != ESP_OK) {
|
||||
/*---> Stop OTA updating*/
|
||||
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
|
||||
ESP_LOGI(TAG, "Image validation failed, image is corrupted");
|
||||
} else {
|
||||
ESP_LOGI(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
|
||||
}
|
||||
/* EXIT OTA task*/
|
||||
return ;
|
||||
}
|
||||
|
||||
/* 12- Set the new validated-written image to be the boot partition (Configure OTA data for a new boot partition)
|
||||
* On the next restart, ESP will boot from that new partition*/
|
||||
err = esp_ota_set_boot_partition(update_partition);
|
||||
if (err != ESP_OK) {
|
||||
/*Stop OTA update*/
|
||||
ESP_LOGI(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
|
||||
|
||||
/* EXIT OTA task*/
|
||||
return ;
|
||||
}
|
||||
/* 13- Restart the device (ESP32)*/
|
||||
ESP_LOGI(TAG, "Prepare to restart system!");
|
||||
esp_restart();
|
||||
return ;
|
||||
}
|
||||
else if(OTA_BYTES_WRITTEN_FAILED == ota_bytes)
|
||||
{
|
||||
ESP_LOGI(TAG,"No Changing in boot_partition");
|
||||
}
|
||||
}
|
||||
|
||||
static bool diagnostic(void)
|
||||
{
|
||||
/* gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
ESP_LOGI(TAG, "Diagnostics (5 sec)...");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
|
||||
bool diagnostic_is_ok = gpio_get_level(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
|
||||
|
||||
gpio_reset_pin(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);*/
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ota_set_needed(void)
|
||||
{
|
||||
ota_needed = OTA_NEEDED;
|
||||
}
|
||||
|
||||
void ota_set_not_needed(void)
|
||||
{
|
||||
ota_needed = OTA_NOT_NEEDED;
|
||||
}
|
||||
|
||||
void ota_set_finished(void)
|
||||
{
|
||||
ota_needed = OTA_FINISHED;
|
||||
}
|
||||
|
||||
/******************************************************/
|
||||
/* Functions that gonna be used in comms state machine*/
|
||||
/******************************************************/
|
||||
esp_err_t ota_init(uint32_t fileSize)
|
||||
{
|
||||
/*This function should be invoked in the ota_file_open_cb() in comms.c (instead of the OTA_Offline_Init())*/
|
||||
uint8_t sha_256[HASH_LEN] = { 0 };
|
||||
esp_partition_t partition;
|
||||
|
||||
// get sha256 digest for the partition table
|
||||
partition.address = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_MAX_LEN;
|
||||
partition.type = ESP_PARTITION_TYPE_DATA;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for the partition table: ");
|
||||
|
||||
// get sha256 digest for bootloader
|
||||
partition.address = ESP_BOOTLOADER_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.type = ESP_PARTITION_TYPE_APP;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for bootloader: ");
|
||||
|
||||
// get sha256 digest for running partition
|
||||
esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for current firmware: ");
|
||||
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
esp_ota_img_states_t ota_state;
|
||||
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
|
||||
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
|
||||
// run diagnostic function ...
|
||||
bool diagnostic_is_ok = diagnostic();
|
||||
if (diagnostic_is_ok) {
|
||||
ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution ...");
|
||||
esp_ota_mark_app_valid_cancel_rollback();
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version ...");
|
||||
esp_ota_mark_app_invalid_rollback_and_reboot();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*set the ota file size */
|
||||
if(0 == fileSize)
|
||||
fileSize = OTA_WITH_SEQUENTIAL_WRITES;
|
||||
else
|
||||
ota_file_size = fileSize;
|
||||
|
||||
/* Initialize OTA session*/
|
||||
ota_start_session();
|
||||
|
||||
/* */
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ota_get_data_to_buffer(const char* buf, int buf_len)
|
||||
{
|
||||
/* This function should be called in comms.c inside the callback function ota_file_data_cb() */
|
||||
/* This function should copy the bytes that we get from qfread command via uart*/
|
||||
/* handle of qfread cmd stores the incoming bytes from modem via uart in buf */
|
||||
/* After that we gonna use ota-buffer in ota_process_incoming_image() function to start ota-session*/
|
||||
|
||||
esp_err_t retval = ESP_FAIL;
|
||||
int index = 0;
|
||||
|
||||
/*Check if the number of bytes to be processed is greater than ota-buffer size*/
|
||||
/*if(BUFFSIZE < buf_len)
|
||||
{
|
||||
ESP_LOGI(TAG, "Number of bytes to be processed is greater than the size of OTA buffer");
|
||||
return retval;
|
||||
}*/
|
||||
|
||||
/* Set the status of ota-bytes to be processed ---> OTA_BYTES_WRITTEN_WAIT*/
|
||||
ota_bytes = OTA_BYTES_WRITTEN_WAIT;
|
||||
|
||||
/* Reset the number of bytes that have been read to 0 (because the initial value is -1)*/
|
||||
data_read = 0;
|
||||
|
||||
/*Copy the incoming bytes (from uart) to ota-buffer */
|
||||
//for(index = 0; index < buf_len; index++)
|
||||
{
|
||||
ota_write_data = (char*)buf;
|
||||
data_read = buf_len;
|
||||
}
|
||||
|
||||
/*Start OTA_Writing session*/
|
||||
ota_set_needed();
|
||||
|
||||
/*OTA process and writing for the copied bytes*/
|
||||
ota_process_incoming_image();
|
||||
|
||||
/* Check the status of the bytes after OTA-processing/OTA-writing */
|
||||
if(OTA_BYTES_WRITTEN_SUCCESS == ota_bytes)
|
||||
retval = ESP_OK;
|
||||
else if(OTA_BYTES_WRITTEN_FAILED == ota_bytes)
|
||||
retval = ESP_FAIL;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void ota_finish_processing(void)
|
||||
{
|
||||
/*This function should be invoked in the COMMS_STATE_WAIT_MCU_UPDATE state in comms state machine
|
||||
* (instead of the OTA_terminate_connection() )*/
|
||||
|
||||
/* Reset data_read variable to 0*/
|
||||
data_read = 0;
|
||||
|
||||
/* Rise ota_finish flag*/
|
||||
ota_set_finished();
|
||||
|
||||
/* Rise the bytes_success flag*/
|
||||
ota_process_incoming_image();
|
||||
|
||||
/* End the ota_session by validating the image and then rebooting */
|
||||
ota_end_session();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
64
main/ota.h
64
main/ota.h
@ -1,32 +1,32 @@
|
||||
/*
|
||||
* ota.h
|
||||
*
|
||||
* Created on: Feb 13, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_OTA_H_
|
||||
#define MAIN_OTA_H_
|
||||
|
||||
typedef enum{
|
||||
OTA_NEEDED,
|
||||
OTA_NOT_NEEDED,
|
||||
OTA_FINISHED
|
||||
}ota_status_t;
|
||||
|
||||
|
||||
typedef enum{
|
||||
OTA_BYTES_WRITTEN_SUCCESS,
|
||||
OTA_BYTES_WRITTEN_FAILED,
|
||||
OTA_BYTES_WRITTEN_WAIT
|
||||
}ota_bytes_status_t;
|
||||
|
||||
void ota_set_needed(void);
|
||||
void ota_set_not_needed(void);
|
||||
void ota_set_finished(void);
|
||||
|
||||
esp_err_t ota_init(uint32_t fileSize);
|
||||
esp_err_t ota_get_data_to_buffer(const char* buf, int buf_len);
|
||||
void ota_finish_processing(void);
|
||||
|
||||
#endif /* MAIN_OTA_H_ */
|
||||
/*
|
||||
* ota.h
|
||||
*
|
||||
* Created on: Feb 13, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_OTA_H_
|
||||
#define MAIN_OTA_H_
|
||||
|
||||
typedef enum{
|
||||
OTA_NEEDED,
|
||||
OTA_NOT_NEEDED,
|
||||
OTA_FINISHED
|
||||
}ota_status_t;
|
||||
|
||||
|
||||
typedef enum{
|
||||
OTA_BYTES_WRITTEN_SUCCESS,
|
||||
OTA_BYTES_WRITTEN_FAILED,
|
||||
OTA_BYTES_WRITTEN_WAIT
|
||||
}ota_bytes_status_t;
|
||||
|
||||
void ota_set_needed(void);
|
||||
void ota_set_not_needed(void);
|
||||
void ota_set_finished(void);
|
||||
|
||||
esp_err_t ota_init(uint32_t fileSize);
|
||||
esp_err_t ota_get_data_to_buffer(const char* buf, int buf_len);
|
||||
void ota_finish_processing(void);
|
||||
|
||||
#endif /* MAIN_OTA_H_ */
|
||||
|
@ -1,50 +1,50 @@
|
||||
/*
|
||||
* tempstick_wifi_client.h
|
||||
*
|
||||
* Created on: Jul 28, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_WIFI_CLIENT_H_
|
||||
#define MAIN_WIFI_CLIENT_H_
|
||||
|
||||
#define WIFI_OK 0
|
||||
#define WIFI_FAIL -1
|
||||
#define WIFI_MCU_UPDATE_NEEDED 2
|
||||
|
||||
#define WIFI_OK 0
|
||||
#define WIFI_FAIL -1
|
||||
#define WIFI_MCU_UPDATE_NEEDED 2
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GET_CONFIGURATIONS,
|
||||
GET_MCU_UPDATE_VERSION,
|
||||
GET_MODEM_UPDATE_VERSION
|
||||
}get_req_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SERVER_ONE,
|
||||
SERVER_TWO,
|
||||
SERVER_THREE
|
||||
}server_type_t;
|
||||
|
||||
#include "wifi_Init.h"
|
||||
#include "main.h"
|
||||
#if (WIFI_NEEDED == 1)
|
||||
esp_err_t http_client_test(char *url);
|
||||
esp_err_t http_client_post(char *url, char *params, char *response);
|
||||
void http_client_post_stop(void);
|
||||
esp_err_t http_client_get(const char *url1, char *response);
|
||||
|
||||
esp_err_t http_client_do_get_request(get_req_type_t target_url, char* device_imei, char *response_buf);
|
||||
esp_err_t http_client_do_post_request(char *post_params, char *response_buf);
|
||||
void wifi_set_post_str_type(deviceToServer_msgType_t str);;
|
||||
|
||||
bool registerAcctWithServer(char *typeStr, char *customerAccountIdStr, char *customerAccountPasswordStr, char *customerAccountPasswordAgainStr, char *responseStr, uint16_t responseStrSize);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif /* MAIN_WIFI_CLIENT_H_ */
|
||||
/*
|
||||
* tempstick_wifi_client.h
|
||||
*
|
||||
* Created on: Jul 28, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_WIFI_CLIENT_H_
|
||||
#define MAIN_WIFI_CLIENT_H_
|
||||
|
||||
#define WIFI_OK 0
|
||||
#define WIFI_FAIL -1
|
||||
#define WIFI_MCU_UPDATE_NEEDED 2
|
||||
|
||||
#define WIFI_OK 0
|
||||
#define WIFI_FAIL -1
|
||||
#define WIFI_MCU_UPDATE_NEEDED 2
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GET_CONFIGURATIONS,
|
||||
GET_MCU_UPDATE_VERSION,
|
||||
GET_MODEM_UPDATE_VERSION
|
||||
}get_req_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SERVER_ONE,
|
||||
SERVER_TWO,
|
||||
SERVER_THREE
|
||||
}server_type_t;
|
||||
|
||||
#include "wifi_Init.h"
|
||||
#include "main.h"
|
||||
#if (WIFI_NEEDED == 1)
|
||||
esp_err_t http_client_test(char *url);
|
||||
esp_err_t http_client_post(char *url, char *params, char *response);
|
||||
void http_client_post_stop(void);
|
||||
esp_err_t http_client_get(const char *url1, char *response);
|
||||
|
||||
esp_err_t http_client_do_get_request(get_req_type_t target_url, char* device_imei, char *response_buf);
|
||||
esp_err_t http_client_do_post_request(char *post_params, char *response_buf);
|
||||
void wifi_set_post_str_type(deviceToServer_msgType_t str);;
|
||||
|
||||
bool registerAcctWithServer(char *typeStr, char *customerAccountIdStr, char *customerAccountPasswordStr, char *customerAccountPasswordAgainStr, char *responseStr, uint16_t responseStrSize);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif /* MAIN_WIFI_CLIENT_H_ */
|
||||
|
128
main/wifi_Init.h
128
main/wifi_Init.h
@ -1,64 +1,64 @@
|
||||
/*
|
||||
* tempstick_wifi.h
|
||||
*
|
||||
* Created on: Jul 28, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_WIFI_INIT_H_
|
||||
#define MAIN_WIFI_INIT_H_
|
||||
|
||||
#include "main.h"
|
||||
#if (WIFI_NEEDED == 1)
|
||||
#include "esp_wifi.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
CHECK_IN_STR,
|
||||
ON_BOARDING_STR
|
||||
}deviceToServer_msgType_t;
|
||||
|
||||
|
||||
#define STORING_TO_NVM
|
||||
|
||||
|
||||
#define WIFI_CLIENT_MODE 1
|
||||
#define WIFI_AP_MODE 2
|
||||
#define WIFI_CLIENT_AP_MODE 3
|
||||
|
||||
|
||||
esp_err_t Connect_wifi_sta(uint8_t mode);
|
||||
uint8_t wifi_station_connected(void);
|
||||
bool wifi_isStarted(void);
|
||||
void Wifi_Init_SoftAp(void);
|
||||
|
||||
esp_err_t wifi_first_init(void);
|
||||
esp_err_t wifi_stop(void);
|
||||
void wifi_register_event_handlers(void);
|
||||
void wifi_sta_disconnecting(void);
|
||||
void wifi_scan_start(uint16_t *apCount, wifi_ap_record_t **list);
|
||||
|
||||
|
||||
void wifi_update_credentials(char *ssid, char *pwd);
|
||||
char *wifi_get_ssid(void);
|
||||
char *wifi_get_pswd(void);
|
||||
void wifi_switchToPrimaryNetwork(void);
|
||||
|
||||
|
||||
char *wifi_get_ssidA(void);
|
||||
char *wifi_get_pswdA(void);
|
||||
void wifi_set_ssidA(char* ssid);
|
||||
void wifi_set_pswdA(char* pswd);
|
||||
|
||||
char *wifi_get_ssidB(void);
|
||||
char *wifi_get_pswdB(void);
|
||||
void wifi_set_ssidB(char* ssid);
|
||||
void wifi_set_pswdB(char* pswd);
|
||||
|
||||
bool safeStrCat(char *dest, uint16_t destSize, char *source);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif /* MAIN_WIFI_INIT_H_ */
|
||||
/*
|
||||
* tempstick_wifi.h
|
||||
*
|
||||
* Created on: Jul 28, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef MAIN_WIFI_INIT_H_
|
||||
#define MAIN_WIFI_INIT_H_
|
||||
|
||||
#include "main.h"
|
||||
#if (WIFI_NEEDED == 1)
|
||||
#include "esp_wifi.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
CHECK_IN_STR,
|
||||
ON_BOARDING_STR
|
||||
}deviceToServer_msgType_t;
|
||||
|
||||
|
||||
#define STORING_TO_NVM
|
||||
|
||||
|
||||
#define WIFI_CLIENT_MODE 1
|
||||
#define WIFI_AP_MODE 2
|
||||
#define WIFI_CLIENT_AP_MODE 3
|
||||
|
||||
|
||||
esp_err_t Connect_wifi_sta(uint8_t mode);
|
||||
uint8_t wifi_station_connected(void);
|
||||
bool wifi_isStarted(void);
|
||||
void Wifi_Init_SoftAp(void);
|
||||
|
||||
esp_err_t wifi_first_init(void);
|
||||
esp_err_t wifi_stop(void);
|
||||
void wifi_register_event_handlers(void);
|
||||
void wifi_sta_disconnecting(void);
|
||||
void wifi_scan_start(uint16_t *apCount, wifi_ap_record_t **list);
|
||||
|
||||
|
||||
void wifi_update_credentials(char *ssid, char *pwd);
|
||||
char *wifi_get_ssid(void);
|
||||
char *wifi_get_pswd(void);
|
||||
void wifi_switchToPrimaryNetwork(void);
|
||||
|
||||
|
||||
char *wifi_get_ssidA(void);
|
||||
char *wifi_get_pswdA(void);
|
||||
void wifi_set_ssidA(char* ssid);
|
||||
void wifi_set_pswdA(char* pswd);
|
||||
|
||||
char *wifi_get_ssidB(void);
|
||||
char *wifi_get_pswdB(void);
|
||||
void wifi_set_ssidB(char* ssid);
|
||||
void wifi_set_pswdB(char* pswd);
|
||||
|
||||
bool safeStrCat(char *dest, uint16_t destSize, char *source);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif /* MAIN_WIFI_INIT_H_ */
|
||||
|
566
main/wifi_OTA.c
566
main/wifi_OTA.c
@ -1,283 +1,283 @@
|
||||
/* OTA example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_crt_bundle.h"
|
||||
#include "esp_app_format.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "esp_flash_partitions.h"
|
||||
#include "esp_partition.h"
|
||||
#include "errno.h"
|
||||
#include "main.h"
|
||||
#include "comms.h"
|
||||
#include "wifi_OTA.h"
|
||||
|
||||
#define BUFFSIZE 1024
|
||||
#define HASH_LEN 32 /* SHA-256 digest length */
|
||||
|
||||
static const char *TAG = "WIFI_OTA";
|
||||
/*an ota data write buffer ready to write to the flash*/
|
||||
static char ota_write_data[BUFFSIZE + 1] = { 0 };
|
||||
|
||||
#define OTA_URL_SIZE 256
|
||||
|
||||
char ota_url[OTA_URL_SIZE];
|
||||
wifi_ota_status_t ota_process_state;
|
||||
|
||||
static void http_cleanup(esp_http_client_handle_t client)
|
||||
{
|
||||
esp_http_client_close(client);
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void task_fatal_error(void)
|
||||
{
|
||||
ESP_LOGE(TAG, "Exiting task due to fatal error during ota process...");
|
||||
(void)vTaskDelete(NULL);
|
||||
ota_process_state = OTA_FATAL_ERROR;
|
||||
}
|
||||
|
||||
static void print_sha256 (const uint8_t *image_hash, const char *label)
|
||||
{
|
||||
char hash_print[HASH_LEN * 2 + 1];
|
||||
hash_print[HASH_LEN * 2] = 0;
|
||||
for (int i = 0; i < HASH_LEN; ++i) {
|
||||
sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
|
||||
}
|
||||
ESP_LOGI(TAG, "%s: %s", label, hash_print);
|
||||
}
|
||||
|
||||
static void infinite_loop(void)
|
||||
{
|
||||
int i = 0;
|
||||
ESP_LOGI(TAG, "The firmware that is available on the server is an OLD image\n WIFI-OTA stopped.");
|
||||
ota_process_state = OTA_FATAL_ERROR;
|
||||
}
|
||||
|
||||
static void wifi_ota_task(void *pvParameter)
|
||||
{
|
||||
esp_err_t err;
|
||||
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
|
||||
esp_ota_handle_t update_handle = 0 ;
|
||||
const esp_partition_t *update_partition = NULL;
|
||||
|
||||
ESP_LOGI(TAG, "Starting WIFI Firmware Update Process");
|
||||
|
||||
const esp_partition_t *configured = esp_ota_get_boot_partition();
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
|
||||
if (configured != running) {
|
||||
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08"PRIx32", but running from offset 0x%08"PRIx32,configured->address, running->address);
|
||||
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
|
||||
}
|
||||
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08"PRIx32")",running->type, running->subtype, running->address);
|
||||
|
||||
esp_http_client_config_t config = {
|
||||
.url = ota_url,
|
||||
.crt_bundle_attach = esp_crt_bundle_attach,
|
||||
.timeout_ms = 5000,
|
||||
.keep_alive_enable = true,
|
||||
};
|
||||
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
if (client == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to initialise HTTP connection");
|
||||
task_fatal_error();
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG,"URL is : %s",config.url);
|
||||
}
|
||||
|
||||
err = esp_http_client_open(client, 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
||||
esp_http_client_cleanup(client);
|
||||
task_fatal_error();
|
||||
}
|
||||
esp_http_client_fetch_headers(client);
|
||||
|
||||
update_partition = esp_ota_get_next_update_partition(NULL);
|
||||
assert(update_partition != NULL);
|
||||
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%"PRIx32,update_partition->subtype, update_partition->address);
|
||||
|
||||
int binary_file_length = 0;
|
||||
/*deal with all receive packet*/
|
||||
bool image_header_was_checked = false;
|
||||
while (1) {
|
||||
int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
|
||||
if (data_read < 0) {
|
||||
ESP_LOGE(TAG, "Error: SSL data read error");
|
||||
http_cleanup(client);
|
||||
task_fatal_error();
|
||||
} else if (data_read > 0) {
|
||||
if (image_header_was_checked == false) {
|
||||
esp_app_desc_t new_app_info;
|
||||
if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
|
||||
// check current version with downloading
|
||||
memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
|
||||
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
|
||||
|
||||
esp_app_desc_t running_app_info;
|
||||
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
|
||||
}
|
||||
|
||||
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
|
||||
esp_app_desc_t invalid_app_info;
|
||||
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
|
||||
}
|
||||
|
||||
// check current version with last invalid partition
|
||||
if (last_invalid_app != NULL) {
|
||||
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
ESP_LOGW(TAG, "New version is the same as invalid version.");
|
||||
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
|
||||
ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
|
||||
http_cleanup(client);
|
||||
infinite_loop();
|
||||
}
|
||||
}
|
||||
#ifndef CONFIG_EXAMPLE_SKIP_VERSION_CHECK
|
||||
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
|
||||
http_cleanup(client);
|
||||
infinite_loop();
|
||||
}
|
||||
#endif
|
||||
|
||||
image_header_was_checked = true;
|
||||
|
||||
err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
ESP_LOGI(TAG, "esp_ota_begin succeeded");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "received package is not fit len");
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
}
|
||||
err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
|
||||
if (err != ESP_OK) {
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
binary_file_length += data_read;
|
||||
ESP_LOGI(TAG, "Written image length %d", binary_file_length);
|
||||
} else if (data_read == 0) {
|
||||
/*
|
||||
* As esp_http_client_read never returns negative error code, we rely on
|
||||
* `errno` to check for underlying transport connectivity closure if any
|
||||
*/
|
||||
if (errno == ECONNRESET || errno == ENOTCONN) {
|
||||
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
|
||||
break;
|
||||
}
|
||||
if (esp_http_client_is_complete_data_received(client) == true) {
|
||||
ESP_LOGI(TAG, "Connection closed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Total Write binary data length: %d", binary_file_length);
|
||||
if (esp_http_client_is_complete_data_received(client) != true) {
|
||||
ESP_LOGE(TAG, "Error in receiving complete file");
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
|
||||
err = esp_ota_end(update_handle);
|
||||
if (err != ESP_OK) {
|
||||
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
|
||||
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
|
||||
}
|
||||
http_cleanup(client);
|
||||
task_fatal_error();
|
||||
}
|
||||
|
||||
err = esp_ota_set_boot_partition(update_partition);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
|
||||
http_cleanup(client);
|
||||
task_fatal_error();
|
||||
}
|
||||
ESP_LOGI(TAG, "Prepare to restart system!");
|
||||
esp_restart();
|
||||
return ;
|
||||
}
|
||||
|
||||
wifi_ota_status_t wifi_ota_get_status(void)
|
||||
{
|
||||
return ota_process_state;
|
||||
}
|
||||
|
||||
void wifi_ota_start_firmware_update(char* device_imei)
|
||||
{
|
||||
ESP_LOGI(TAG, "/****************** WIFI-OTA ********************/");
|
||||
|
||||
sprintf(ota_url,MCU_FW_BIN_FILE_URL1,device_imei);
|
||||
|
||||
ota_process_state = OTA_IN_PROGRESS;
|
||||
|
||||
uint8_t sha_256[HASH_LEN] = { 0 };
|
||||
esp_partition_t partition;
|
||||
|
||||
// get sha256 digest for the partition table
|
||||
partition.address = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_MAX_LEN;
|
||||
partition.type = ESP_PARTITION_TYPE_DATA;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for the partition table: ");
|
||||
|
||||
// get sha256 digest for bootloader
|
||||
partition.address = ESP_BOOTLOADER_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.type = ESP_PARTITION_TYPE_APP;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for bootloader: ");
|
||||
|
||||
// get sha256 digest for running partition
|
||||
esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for current firmware: ");
|
||||
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
esp_ota_img_states_t ota_state;
|
||||
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
|
||||
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
|
||||
// run diagnostic function ...
|
||||
bool diagnostic_is_ok = true;
|
||||
if (diagnostic_is_ok) {
|
||||
ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution ...");
|
||||
esp_ota_mark_app_valid_cancel_rollback();
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version ...");
|
||||
esp_ota_mark_app_invalid_rollback_and_reboot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
xTaskCreate(&wifi_ota_task, "wifi_ota_task", 4096, NULL, 5, NULL);
|
||||
}
|
||||
/* OTA example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_crt_bundle.h"
|
||||
#include "esp_app_format.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "esp_flash_partitions.h"
|
||||
#include "esp_partition.h"
|
||||
#include "errno.h"
|
||||
#include "main.h"
|
||||
#include "comms.h"
|
||||
#include "wifi_OTA.h"
|
||||
|
||||
#define BUFFSIZE 1024
|
||||
#define HASH_LEN 32 /* SHA-256 digest length */
|
||||
|
||||
static const char *TAG = "WIFI_OTA";
|
||||
/*an ota data write buffer ready to write to the flash*/
|
||||
static char ota_write_data[BUFFSIZE + 1] = { 0 };
|
||||
|
||||
#define OTA_URL_SIZE 256
|
||||
|
||||
char ota_url[OTA_URL_SIZE];
|
||||
wifi_ota_status_t ota_process_state;
|
||||
|
||||
static void http_cleanup(esp_http_client_handle_t client)
|
||||
{
|
||||
esp_http_client_close(client);
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void task_fatal_error(void)
|
||||
{
|
||||
ESP_LOGE(TAG, "Exiting task due to fatal error during ota process...");
|
||||
(void)vTaskDelete(NULL);
|
||||
ota_process_state = OTA_FATAL_ERROR;
|
||||
}
|
||||
|
||||
static void print_sha256 (const uint8_t *image_hash, const char *label)
|
||||
{
|
||||
char hash_print[HASH_LEN * 2 + 1];
|
||||
hash_print[HASH_LEN * 2] = 0;
|
||||
for (int i = 0; i < HASH_LEN; ++i) {
|
||||
sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
|
||||
}
|
||||
ESP_LOGI(TAG, "%s: %s", label, hash_print);
|
||||
}
|
||||
|
||||
static void infinite_loop(void)
|
||||
{
|
||||
int i = 0;
|
||||
ESP_LOGI(TAG, "The firmware that is available on the server is an OLD image\n WIFI-OTA stopped.");
|
||||
ota_process_state = OTA_FATAL_ERROR;
|
||||
}
|
||||
|
||||
static void wifi_ota_task(void *pvParameter)
|
||||
{
|
||||
esp_err_t err;
|
||||
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
|
||||
esp_ota_handle_t update_handle = 0 ;
|
||||
const esp_partition_t *update_partition = NULL;
|
||||
|
||||
ESP_LOGI(TAG, "Starting WIFI Firmware Update Process");
|
||||
|
||||
const esp_partition_t *configured = esp_ota_get_boot_partition();
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
|
||||
if (configured != running) {
|
||||
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08"PRIx32", but running from offset 0x%08"PRIx32,configured->address, running->address);
|
||||
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
|
||||
}
|
||||
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08"PRIx32")",running->type, running->subtype, running->address);
|
||||
|
||||
esp_http_client_config_t config = {
|
||||
.url = ota_url,
|
||||
.crt_bundle_attach = esp_crt_bundle_attach,
|
||||
.timeout_ms = 5000,
|
||||
.keep_alive_enable = true,
|
||||
};
|
||||
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
if (client == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to initialise HTTP connection");
|
||||
task_fatal_error();
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG,"URL is : %s",config.url);
|
||||
}
|
||||
|
||||
err = esp_http_client_open(client, 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
||||
esp_http_client_cleanup(client);
|
||||
task_fatal_error();
|
||||
}
|
||||
esp_http_client_fetch_headers(client);
|
||||
|
||||
update_partition = esp_ota_get_next_update_partition(NULL);
|
||||
assert(update_partition != NULL);
|
||||
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%"PRIx32,update_partition->subtype, update_partition->address);
|
||||
|
||||
int binary_file_length = 0;
|
||||
/*deal with all receive packet*/
|
||||
bool image_header_was_checked = false;
|
||||
while (1) {
|
||||
int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
|
||||
if (data_read < 0) {
|
||||
ESP_LOGE(TAG, "Error: SSL data read error");
|
||||
http_cleanup(client);
|
||||
task_fatal_error();
|
||||
} else if (data_read > 0) {
|
||||
if (image_header_was_checked == false) {
|
||||
esp_app_desc_t new_app_info;
|
||||
if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
|
||||
// check current version with downloading
|
||||
memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
|
||||
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
|
||||
|
||||
esp_app_desc_t running_app_info;
|
||||
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
|
||||
}
|
||||
|
||||
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
|
||||
esp_app_desc_t invalid_app_info;
|
||||
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
|
||||
}
|
||||
|
||||
// check current version with last invalid partition
|
||||
if (last_invalid_app != NULL) {
|
||||
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
ESP_LOGW(TAG, "New version is the same as invalid version.");
|
||||
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
|
||||
ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
|
||||
http_cleanup(client);
|
||||
infinite_loop();
|
||||
}
|
||||
}
|
||||
#ifndef CONFIG_EXAMPLE_SKIP_VERSION_CHECK
|
||||
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
|
||||
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
|
||||
http_cleanup(client);
|
||||
infinite_loop();
|
||||
}
|
||||
#endif
|
||||
|
||||
image_header_was_checked = true;
|
||||
|
||||
err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
ESP_LOGI(TAG, "esp_ota_begin succeeded");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "received package is not fit len");
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
}
|
||||
err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
|
||||
if (err != ESP_OK) {
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
binary_file_length += data_read;
|
||||
ESP_LOGI(TAG, "Written image length %d", binary_file_length);
|
||||
} else if (data_read == 0) {
|
||||
/*
|
||||
* As esp_http_client_read never returns negative error code, we rely on
|
||||
* `errno` to check for underlying transport connectivity closure if any
|
||||
*/
|
||||
if (errno == ECONNRESET || errno == ENOTCONN) {
|
||||
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
|
||||
break;
|
||||
}
|
||||
if (esp_http_client_is_complete_data_received(client) == true) {
|
||||
ESP_LOGI(TAG, "Connection closed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Total Write binary data length: %d", binary_file_length);
|
||||
if (esp_http_client_is_complete_data_received(client) != true) {
|
||||
ESP_LOGE(TAG, "Error in receiving complete file");
|
||||
http_cleanup(client);
|
||||
esp_ota_abort(update_handle);
|
||||
task_fatal_error();
|
||||
}
|
||||
|
||||
err = esp_ota_end(update_handle);
|
||||
if (err != ESP_OK) {
|
||||
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
|
||||
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
|
||||
}
|
||||
http_cleanup(client);
|
||||
task_fatal_error();
|
||||
}
|
||||
|
||||
err = esp_ota_set_boot_partition(update_partition);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
|
||||
http_cleanup(client);
|
||||
task_fatal_error();
|
||||
}
|
||||
ESP_LOGI(TAG, "Prepare to restart system!");
|
||||
esp_restart();
|
||||
return ;
|
||||
}
|
||||
|
||||
wifi_ota_status_t wifi_ota_get_status(void)
|
||||
{
|
||||
return ota_process_state;
|
||||
}
|
||||
|
||||
void wifi_ota_start_firmware_update(char* device_imei)
|
||||
{
|
||||
ESP_LOGI(TAG, "/****************** WIFI-OTA ********************/");
|
||||
|
||||
sprintf(ota_url,MCU_FW_BIN_FILE_URL1,device_imei);
|
||||
|
||||
ota_process_state = OTA_IN_PROGRESS;
|
||||
|
||||
uint8_t sha_256[HASH_LEN] = { 0 };
|
||||
esp_partition_t partition;
|
||||
|
||||
// get sha256 digest for the partition table
|
||||
partition.address = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_MAX_LEN;
|
||||
partition.type = ESP_PARTITION_TYPE_DATA;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for the partition table: ");
|
||||
|
||||
// get sha256 digest for bootloader
|
||||
partition.address = ESP_BOOTLOADER_OFFSET;
|
||||
partition.size = ESP_PARTITION_TABLE_OFFSET;
|
||||
partition.type = ESP_PARTITION_TYPE_APP;
|
||||
esp_partition_get_sha256(&partition, sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for bootloader: ");
|
||||
|
||||
// get sha256 digest for running partition
|
||||
esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
|
||||
print_sha256(sha_256, "SHA-256 for current firmware: ");
|
||||
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
esp_ota_img_states_t ota_state;
|
||||
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
|
||||
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
|
||||
// run diagnostic function ...
|
||||
bool diagnostic_is_ok = true;
|
||||
if (diagnostic_is_ok) {
|
||||
ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution ...");
|
||||
esp_ota_mark_app_valid_cancel_rollback();
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version ...");
|
||||
esp_ota_mark_app_invalid_rollback_and_reboot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
xTaskCreate(&wifi_ota_task, "wifi_ota_task", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
* wifi_OTA.h
|
||||
*
|
||||
* Created on: Jul 31, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef WIFI_OTA_H_
|
||||
#define WIFI_OTA_H_
|
||||
|
||||
typedef enum
|
||||
{
|
||||
OTA_IS_STOPPED,
|
||||
OTA_IN_PROGRESS,
|
||||
OTA_FATAL_ERROR,
|
||||
OTA_COMPLETED
|
||||
}wifi_ota_status_t;
|
||||
|
||||
void wifi_ota_start_firmware_update(char* device_imei);
|
||||
wifi_ota_status_t wifi_ota_get_status(void);
|
||||
|
||||
#endif /* WIFI_OTA_H_ */
|
||||
/*
|
||||
* wifi_OTA.h
|
||||
*
|
||||
* Created on: Jul 31, 2023
|
||||
* Author: Sword
|
||||
*/
|
||||
|
||||
#ifndef WIFI_OTA_H_
|
||||
#define WIFI_OTA_H_
|
||||
|
||||
typedef enum
|
||||
{
|
||||
OTA_IS_STOPPED,
|
||||
OTA_IN_PROGRESS,
|
||||
OTA_FATAL_ERROR,
|
||||
OTA_COMPLETED
|
||||
}wifi_ota_status_t;
|
||||
|
||||
void wifi_ota_start_firmware_update(char* device_imei);
|
||||
wifi_ota_status_t wifi_ota_get_status(void);
|
||||
|
||||
#endif /* WIFI_OTA_H_ */
|
||||
|
Loading…
Reference in New Issue
Block a user