Wifi_Onboarding_v1/main/comms.h
2025-02-21 15:00:04 +05:30

284 lines
15 KiB
C

/*
* 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_ */