Skip to content

Commit

Permalink
changed name of MQTTFlash to MQTTStorage as it abstracts both flash & SD
Browse files Browse the repository at this point in the history
  • Loading branch information
christof.strackSAG authored and christof.strackSAG committed Mar 6, 2020
1 parent 482728d commit 600384f
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 101 deletions.
21 changes: 20 additions & 1 deletion resources/performanceReportHeap_V01.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,23 @@ everFreeHeap, freeHeap, stackHighWaterMarkMain, stackHighWaterMarkApp, stackSize
[4752], [7544], [??],[1014], 1000, 1400, 70 * 1024
[7152], [9944], [??], [614], 0800, 1200, 70 * 1024
[6352], [9144], [34], [804], 0800, 1000, 70 * 1024
[6752], [9544],[134], [596], 0900, 1000, 70 * 1024
[6752], [9544],[134], [596], 0900, 1000, 70 * 1024


stackHighWaterMark
/* The minimum amount of stack space that has remained for the task since
the task was created. The closer this value is to zero the closer the task
has come to overflowing its stack. */

#define TASK_STACK_SIZE_MAIN_CMD_PROCESSOR (UINT32_C(1200))
INFO | XDK DEVICE 3: MQTTOperation_SensorUpdate: Memory stat: everFreeHeap:[5720], freeHeap:[8168], stackHighWaterMarkApp:[624],stackHighWaterMarkMain:[434]

#define TASK_STACK_SIZE_MAIN_CMD_PROCESSOR (UINT32_C(1000))
INFO | XDK DEVICE 3: MQTTOperation_SensorUpdate: Memory stat: everFreeHeap:[6520], freeHeap:[8968], stackHighWaterMarkApp:[620],stackHighWaterMarkMain:[234]

#define TASK_STACK_SIZE_MAIN_CMD_PROCESSOR (UINT32_C(900))
INFO | XDK DEVICE 3: MQTTOperation_SensorUpdate: Memory stat: everFreeHeap:[6920], freeHeap:[9368], stackHighWaterMarkApp:[612],stackHighWaterMarkMain:[134]

#define TASK_STACK_SIZE_MAIN_CMD_PROCESSOR (UINT32_C(800))
INFO | XDK DEVICE 3: MQTTOperation_SensorUpdate: Memory stat: everFreeHeap:[7320], freeHeap:[9768], stackHighWaterMarkApp:[620],stackHighWaterMarkMain:[34]

147 changes: 58 additions & 89 deletions source/AppController.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ static WLAN_Setup_T WLANSetupInfo = { .IsEnterprise = false, .IsHostPgmEnabled =
.IsStatic = WLAN_STATIC_IP, .IpAddr = WLAN_IP_ADDR, .GwAddr =
WLAN_GW_ADDR, .DnsAddr = WLAN_DNS_ADDR, .Mask = WLAN_MASK, };

static SNTP_Setup_T SNTPSetupInfo;/**< SNTP setup parameters */
static SNTP_Setup_T SNTPSetupInfo; /**< SNTP setup parameters */

static CmdProcessor_T * AppCmdProcessor;/**< Handle to store the main Command processor handle to be used by run-time event driven threads */
static CmdProcessor_T * AppCmdProcessor; /**< Handle to store the main Command processor handle to be used by run-time event driven threads */

static void AppController_Enable(void *, uint32_t);
static void AppController_Fire(void *);
static void AppController_SetClientId(void);
static void AppController_StartLEDBlinkTimer(int);
static void AppController_Enable (void *, uint32_t);
static void AppController_SetClientId (const char * clientId);
static void AppController_StartLEDBlinkTimer (int);

/* global variables ********************************************************* */
char clientId[14] = { 0 }; // MAC address 6*2 + \0 'terminating'
Expand Down Expand Up @@ -143,14 +142,25 @@ static void AppController_Setup(void * param1, uint32_t param2) {
}

retcode = MQTTCfgParser_Init();
// sett boot mode: operation or registration

// set boot mode: operation or registration
boot_mode = MQTTCfgParser_GetMode();
if (retcode != RETCODE_OK) {
LOG_AT_ERROR(
("AppController_Setup: Boot error. Inconsistent configuration!\r\n"));
assert(0);
}

if (boot_mode == APP_STATUS_OPERATION_MODE) {
MqttCredentials.Username = MQTTCfgParser_GetMqttUser();
MqttCredentials.Password = MQTTCfgParser_GetMqttPassword();
MqttCredentials.Anonymous = MQTTCfgParser_IsMqttAnonymous();
} else {
MqttCredentials.Username = MQTT_REGISTRATION_USERNAME;
MqttCredentials.Password = MQTT_REGISTRATION_PASSWORD;
MqttCredentials.Anonymous = FALSE;
}

if (RETCODE_OK == retcode) {
// set cfg parameter for WIFI access
WLANSetupInfo.SSID = MQTTCfgParser_GetWlanSSID();
Expand Down Expand Up @@ -247,71 +257,44 @@ static void AppController_Enable(void * param1, uint32_t param2) {
MqttConnectInfo.BrokerPort = MQTTCfgParser_GetMqttBrokerPort();
MqttConnectInfo.CleanSession = true;
MqttConnectInfo.KeepAliveInterval = 100;
AppController_SetClientId();
AppController_SetClientId(MqttConnectInfo.ClientId);

LOG_AT_INFO(
("AppController_Enable: Device ID for registration in Cumulocity %s.\r\n", MqttConnectInfo.ClientId));

if (RETCODE_OK == retcode) {
BaseType_t task_result;
if (boot_mode == APP_STATUS_OPERATION_MODE) {
task_result = xTaskCreate(MQTTOperation_Init,
(const char * const ) "AppController", TASK_STACK_SIZE_APP_CONTROLLER, NULL,
TASK_PRIO_APP_CONTROLLER, &AppControllerHandle);
} else {
task_result = xTaskCreate(MQTTRegistration_Init,
(const char * const ) "AppController", TASK_STACK_SIZE_APP_CONTROLLER, NULL,
TASK_PRIO_APP_CONTROLLER, &AppControllerHandle);
}

if (pdPASS
!= xTaskCreate(AppController_Fire,
(const char * const ) "AppController",
TASK_STACK_SIZE_APP_CONTROLLER, NULL,
TASK_PRIO_APP_CONTROLLER, &AppControllerHandle)) {
retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_OUT_OF_RESOURCES);
!= task_result) {
LOG_AT_ERROR(
("AppController_Enable: Now calling SoftReset and reboot to recover\r\n"));
Retcode_RaiseError(retcode);
BSP_Board_SoftReset();
}
}

if (RETCODE_OK != retcode) {
LOG_AT_ERROR(
("AppController_Enable: Now calling SoftReset and reboot to recover\r\n"));
Retcode_RaiseError(retcode);
BSP_Board_SoftReset();
// assert(0);
}
Utils_PrintResetCause();
}

/**
* @brief Responsible for controlling the send data over MQTT application control flow.
*
* - Synchronize SNTP time stamp for the system if MQTT communication is secure
* - Connect to MQTT broker
* - Subscribe to MQTT topic
* - Read environmental sensor data
* - Publish data periodically for a MQTT topic
*
* @param[in] pvParameters
* Unused
*/

static void AppController_Fire(void* pvParameters) {
BCDS_UNUSED(pvParameters);

if (boot_mode == APP_STATUS_OPERATION_MODE) {
MqttCredentials.Username = MQTTCfgParser_GetMqttUser();
MqttCredentials.Password = MQTTCfgParser_GetMqttPassword();
MqttCredentials.Anonymous = MQTTCfgParser_IsMqttAnonymous();
MQTTOperation_Init();
} else {
MqttCredentials.Username = MQTT_REGISTRATION_USERNAME;
MqttCredentials.Password = MQTT_REGISTRATION_PASSWORD;
MqttCredentials.Anonymous = FALSE;
MQTTRegistration_Init();
}
}

static void AppController_SetClientId(void) {
static void AppController_SetClientId(const char * clientId) {
/* Initialize Variables */
uint8_t _macVal[WIFI_MAC_ADDR_LEN];
uint8_t _macVal[WIFI_MAC_ADDR_LEN + 1] = { 0 };
uint8_t _macAddressLen = WIFI_MAC_ADDR_LEN;

/* Get the MAC Address */
memset(_macVal, NUMBER_UINT8_ZERO, WIFI_MAC_ADDR_LEN);
sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &_macAddressLen,
(uint8_t *) _macVal);

//changed
sprintf(MqttConnectInfo.ClientId, "%02X%02X%02X%02X%02X%02X", _macVal[0],
sprintf(clientId, "%02X%02X%02X%02X%02X%02X", _macVal[0],
_macVal[1], _macVal[2], _macVal[3], _macVal[4], _macVal[5]);
}

Expand All @@ -320,57 +303,46 @@ static void AppController_ToogleLEDCallback(xTimerHandle xTimer) {

switch (app_status) {
case APP_STATUS_STARTED:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_TOGGLE);
break;
case APP_STATUS_OPERATING_STARTED:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_O,
(uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_O, (uint32_t) BSP_LED_COMMAND_TOGGLE);
break;
case APP_STATUS_OPERATING_STOPPED:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_O, (uint32_t) BSP_LED_COMMAND_ON);
break;
case APP_STATUS_ERROR:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_ON);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_O,
(uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_O, (uint32_t) BSP_LED_COMMAND_OFF);
break;
case APP_STATUS_REBOOT:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_O,
(uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_O, (uint32_t) BSP_LED_COMMAND_TOGGLE);
break;
case APP_STATUS_REGISTERED:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_Y, (uint32_t) BSP_LED_COMMAND_ON);
break;
case APP_STATUS_REGISTERING:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_Y,
(uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_OFF);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_Y, (uint32_t) BSP_LED_COMMAND_TOGGLE);

break;
default:
LOG_AT_WARNING(("AppController: Unknown app status\n"));
break;
} LOG_AT_TRACE(("STATUS %s\r\n", app_status_text[app_status]));
}
LOG_AT_TRACE(("STATUS %s\r\n", app_status_text[app_status]));

switch (cmd_status) {
case APP_STATUS_COMMAND_RECEIVED:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_TOGGLE);
cmd_status = APP_STATUS_COMMAND_CONFIRMED;
break;
case APP_STATUS_COMMAND_CONFIRMED:
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R,
(uint32_t) BSP_LED_COMMAND_TOGGLE);
BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_TOGGLE);
cmd_status = APP_STATUS_STARTED;
break;
case APP_STATUS_STARTED:
Expand Down Expand Up @@ -439,20 +411,17 @@ void AppController_Init(void * cmdProcessorHandle, uint32_t param2) {
* Initialialze global variables
*/

MqttSetupInfo = (MQTT_Setup_TZ ) { .IsSecure = APP_MQTT_SECURE_ENABLE, };/**< MQTT setup parameters */
MqttSetupInfo = (MQTT_Setup_TZ ) { .IsSecure = APP_MQTT_SECURE_ENABLE, };

MqttConnectInfo = (MQTT_Connect_TZ ) { .ClientId = clientId, .BrokerURL =
MQTT_BROKER_HOST_NAME, .BrokerPort =
MQTT_BROKER_HOST_PORT, .CleanSession = true, .KeepAliveInterval =
100, };/**< MQTT connect parameters */
MQTT_BROKER_HOST_NAME, .BrokerPort = MQTT_BROKER_HOST_PORT,
.CleanSession = true, .KeepAliveInterval = 100, };

MqttCredentials = (MQTT_Credentials_TZ ) { .Username = APP_MQTT_USERNAME,
.Password = APP_MQTT_PASSWORD, .Anonymous = false,

};/**< MQTT connect parameters */
.Password = APP_MQTT_PASSWORD, .Anonymous = false,};

StorageSetup =
(Storage_Setup_T ) { .SDCard = true, .WiFiFileSystem = true, };/**< Storage setup parameters */
(Storage_Setup_T ) { .SDCard = true, .WiFiFileSystem = true, };

SensorSetup = (Sensor_Setup_T ) { .CmdProcessorHandle = NULL, .Enable = {
.Accel = false, .Mag = false, .Gyro = false, .Humidity =
Expand All @@ -463,7 +432,7 @@ void AppController_Init(void * cmdProcessorHandle, uint32_t param2) {
.Type = SENSOR_GYRO_BMG160, .IsRawData = false, }, .Mag = {
.IsRawData = false }, .Light = { .IsInteruptEnabled = false,
.Callback = NULL, }, .Temp = { .OffsetCorrection =
APP_TEMPERATURE_OFFSET_CORRECTION, }, }, };/**< Sensor setup parameters */
APP_TEMPERATURE_OFFSET_CORRECTION, }, }, };

Retcode_T retcode = RETCODE_OK;
BCDS_UNUSED(param2);
Expand Down
5 changes: 4 additions & 1 deletion source/MQTTOperation.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ extern MQTT_Setup_TZ MqttSetupInfo;
extern MQTT_Connect_TZ MqttConnectInfo;
extern MQTT_Credentials_TZ MqttCredentials;
extern Sensor_Setup_T SensorSetup;
extern xTaskHandle AppControllerHandle;
extern CmdProcessor_T MainCmdProcessor;

/* inline functions ********************************************************* */

Expand Down Expand Up @@ -1078,7 +1080,8 @@ void MQTTOperation_QueueCommand(void * param1, uint32_t param2) {
*
* @return NONE
*/
void MQTTOperation_Init(void) {
void MQTTOperation_Init(void* pvParameters) {
BCDS_UNUSED(pvParameters);

Retcode_T retcode = RETCODE_OK;
tickRateMS = (int) pdMS_TO_TICKS(MQTTCfgParser_GetStreamRate());
Expand Down
2 changes: 1 addition & 1 deletion source/MQTTOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@


/* global function prototype declarations */
void MQTTOperation_Init(void);
void MQTTOperation_Init(void* pvParameters);
void MQTTOperation_DeInit(void);
void MQTTOperation_QueueCommand(void * param1, uint32_t param2);

Expand Down
3 changes: 2 additions & 1 deletion source/MQTTRegistration.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ void MQTTRegistration_StartTimer(void) {
*
* @return NONE
*/
void MQTTRegistration_Init(void) {
void MQTTRegistration_Init(void* pvParameters) {
BCDS_UNUSED(pvParameters);

Retcode_T retcode = RETCODE_OK;

Expand Down
2 changes: 1 addition & 1 deletion source/MQTTRegistration.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define MQTTREGISTRATION_LOGPREFIX "MQTTRegistration"

/* global function prototype declarations */
void MQTTRegistration_Init(void);
void MQTTRegistration_Init(void* pvParameters);
void MQTTRegistration_DeInit(void);
void MQTTRegistration_StartTimer(void);
void MQTTRegistration_StopTimer(void);
Expand Down
6 changes: 1 addition & 5 deletions source/MQTTStorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ static Retcode_T MQTTStorage_SD_Write(const uint8_t* fileName,
/* Open the file to write */
if (f_open(&fileObject, (const TCHAR*) fileName, FA_OPEN_ALWAYS | FA_WRITE)
!= FR_Z_OK) {
/* Error. Cannot create the file *
LOG_AT_ERROR(("MQTTStorage: Error creating status"));
return RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_FAILURE);
}

/* Set the file write pointer at th eend to append */
if (f_lseek(&fileObject, f_size(&fileObject)) != FR_Z_OK) {
Expand Down Expand Up @@ -295,6 +291,7 @@ static void MQTTStorage_Flash_DeleteFile(const uint8_t* fileName) {
Retcode_T retcode = RETCODE_OK;
bool status = false;
uint32_t bytesToRead = 0;

/* Validating if WIFI storage medium is available */
retcode = Storage_IsAvailable(STORAGE_MEDIUM_WIFI_FILE_SYSTEM, &status);
if ((RETCODE_OK == retcode) && (true == status)) {
Expand All @@ -310,7 +307,6 @@ static void MQTTStorage_Flash_DeleteFile(const uint8_t* fileName) {
} else {
LOG_AT_ERROR(
("MQTTStorage: Deleted file failed:[%lu] \r\n", Retcode_GetCode(retcode)));
//assert(0);
}
} else {
LOG_AT_ERROR(
Expand Down
4 changes: 2 additions & 2 deletions source/XdkAppInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
/**< Main command processor task priority */
#define TASK_PRIO_MAIN_CMD_PROCESSOR (UINT32_C(3))
/**< Main command processor task stack size */
#define TASK_STACK_SIZE_MAIN_CMD_PROCESSOR (UINT32_C(1000))
#define TASK_STACK_SIZE_MAIN_CMD_PROCESSOR (UINT32_C(1200))
/**< Main command processor task queue length */
#define TASK_Q_LEN_MAIN_CMD_PROCESSOR (UINT32_C(10))
#define TASK_Q_LEN_MAIN_CMD_PROCESSOR (UINT32_C(8))

/**< Application controller task priority */
#define TASK_PRIO_APP_CONTROLLER (UINT32_C(3))
Expand Down

0 comments on commit 600384f

Please sign in to comment.