110110#include <sys/time.h>
111111#include <limits.h>
112112
113-
114113#include "aws_iot_config.h"
115114#include "aws_iot_log.h"
116115#include "aws_iot_version.h"
117116#include "aws_iot_mqtt_client_interface.h"
118117#include "aws_iot_shadow_interface.h"
119118
120119#include "embARC.h"
121- #include "device.h"
120+ #include "device_container.h"
121+ #include "secure/crypt_container.h"
122122
123123#if defined(AWSDEMO_HAVE_OLED ) && defined(MID_U8GLIB )
124124#include "u8g.h"
135135
136136#define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 512
137137
138+ // use crypt container to encrypt upload data
139+ #define ENCRYPT_UPLOAD_DATA true
140+
138141//#define SIMULATE_TEMPERATURE
139142
140143#define CERT_ROOTDIR "cert/smarthome"
@@ -149,6 +152,10 @@ static bool LivingRoomLights_updated = false;
149152
150153static char JsonDocumentBuffer [MAX_LENGTH_OF_UPDATE_JSON_BUFFER ];
151154static size_t sizeOfJsonDocumentBuffer = sizeof (JsonDocumentBuffer ) / sizeof (JsonDocumentBuffer [0 ]);
155+ #if ENCRYPT_UPLOAD_DATA
156+ static char JsonDocumentBufferCipher [MAX_LENGTH_OF_UPDATE_JSON_BUFFER ];
157+ static char JsonDocumentBufferDeCipher [MAX_LENGTH_OF_UPDATE_JSON_BUFFER ];
158+ #endif
152159
153160static float temperature = STARTING_ROOMTEMPERATURE ;
154161static bool DoorLocked = false;
@@ -201,8 +208,8 @@ static void u8g_draw(void)
201208
202209 u8g_DrawStr (& u8g , 35 , 0 , "SmartHome" );
203210
204- sprintf (strbuf , "Connection: %s" , ConnectionStatus ? "ok" : "lost" );
205- u8g_DrawStr (& u8g , 15 , 10 , (const char * )strbuf );
211+ sprintf (strbuf , "Connection: %s" , ConnectionStatus ? "ok" : "lost" );
212+ u8g_DrawStr (& u8g , 15 , 10 , (const char * )strbuf );
206213
207214 sprintf (strbuf , "Temperature: %.1fC" , temperature );
208215 u8g_DrawStr (& u8g , 15 , 20 , (const char * )strbuf );
@@ -261,8 +268,9 @@ void FrontDoorActuate_Callback(const char *pJsonString, uint32_t JsonStringDataL
261268 if (pContext != NULL ) {
262269 bool temp = * (bool * )(pContext -> pData );
263270 IOT_INFO ("Delta - FrontDoor state changed to %d" , temp );
264- // delete controlFrontDoor(DoorLocked );
271+ vTaskSuspendAll ( );
265272 container_call (device_container , controlFrontDoor , DoorLocked );
273+ xTaskResumeAll ();
266274 }
267275}
268276
@@ -271,8 +279,9 @@ void KitchenLights_Callback(const char *pJsonString, uint32_t JsonStringDataLen,
271279 if (pContext != NULL ) {
272280 bool temp = * (bool * )(pContext -> pData );
273281 IOT_INFO ("Delta - KitchenLights light state changed to %d" , temp );
274- //delete controlKitchenLights(KitchenLights );
282+ vTaskSuspendAll ( );
275283 container_call (device_container , controlKitchenLights , KitchenLights );
284+ xTaskResumeAll ();
276285 }
277286}
278287
@@ -281,8 +290,9 @@ void LivingRoomLights_Callback(const char *pJsonString, uint32_t JsonStringDataL
281290 if (pContext != NULL ) {
282291 bool temp = * (bool * )(pContext -> pData );
283292 IOT_INFO ("Delta - LivingRoomLights light state changed to %d" , temp );
284- //delete controlLivingRoomLights(LivingRoomLights );
293+ vTaskSuspendAll ( );
285294 container_call (device_container , controlLivingRoomLights , LivingRoomLights );
295+ xTaskResumeAll ();
286296 }
287297}
288298
@@ -378,7 +388,7 @@ void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data)
378388
379389 IOT_UNUSED (data );
380390
381- ConnectionStatus = false;
391+ ConnectionStatus = false;
382392
383393 if (aws_iot_is_autoreconnect_enabled (pClient )) {
384394 IOT_INFO ("Auto Reconnect is enabled, Reconnecting attempt will start now" );
@@ -407,7 +417,7 @@ int main(void)
407417 DoorLocked_updated = false;
408418 KitchenLights_updated = false;
409419 LivingRoomLights_updated = false;
410- ConnectionStatus = false;
420+ ConnectionStatus = false;
411421
412422 FrontDoorActuator .cb = FrontDoorActuate_Callback ;
413423 FrontDoorActuator .pData = & DoorLocked ;
@@ -504,19 +514,15 @@ int main(void)
504514 IOT_INFO ("Shadow Register Delta OK" );
505515 }
506516
517+ vTaskSuspendAll ();
507518#ifndef SIMULATE_TEMPERATURE
508- //delete getRoomTemperature(&temperature);
509519 container_call (device_container , getRoomTemperature , & temperature );
510520#endif
511- //delete smarthome_init();
512521 container_call (device_container , smarthome_init );
513-
514- // delete controlFrontDoor(DoorLocked);
515522 container_call (device_container , controlFrontDoor , DoorLocked );
516- //delete controlKitchenLights(KitchenLights);
517523 container_call (device_container , controlKitchenLights , KitchenLights );
518- //delete controlLivingRoomLights(LivingRoomLights);
519524 container_call (device_container , controlLivingRoomLights , LivingRoomLights );
525+ xTaskResumeAll ();
520526
521527 last_LivingRoomLights = !LivingRoomLights ;
522528 last_KitchenLights = !KitchenLights ;
@@ -531,32 +537,35 @@ int main(void)
531537 // If the client is attempting to reconnect we will skip the rest of the loop.
532538 continue ;
533539 }
534- ConnectionStatus = true;
540+ ConnectionStatus = true;
535541 /** Update desired messages */
536542 act_idx = 0 ;
537543 if (DoorLocked_updated ) {
538544 curActuator [act_idx ] = & FrontDoorActuator ;
539545 act_idx ++ ;
540546 DoorLocked = !DoorLocked ;
541547 DoorLocked_updated = false;
542- // delete controlFrontDoor(DoorLocked );
548+ vTaskSuspendAll ( );
543549 container_call (device_container , controlFrontDoor , DoorLocked );
550+ xTaskResumeAll ();
544551 }
545552 if (KitchenLights_updated ) {
546553 curActuator [act_idx ] = & KitchenLightsActuator ;
547554 act_idx ++ ;
548555 KitchenLights = !KitchenLights ;
549556 KitchenLights_updated = false;
550- //delete controlKitchenLights(KitchenLights );
557+ vTaskSuspendAll ( );
551558 container_call (device_container , controlKitchenLights , KitchenLights );
559+ xTaskResumeAll ();
552560 }
553561 if (LivingRoomLights_updated ) {
554562 curActuator [act_idx ] = & LivingRoomLightsActuator ;
555563 act_idx ++ ;
556564 LivingRoomLights = !LivingRoomLights ;
557565 LivingRoomLights_updated = false;
558- //delete controlLivingRoomLights(LivingRoomLights );
566+ vTaskSuspendAll ( );
559567 container_call (device_container , controlLivingRoomLights , LivingRoomLights );
568+ xTaskResumeAll ();
560569 }
561570 if (act_idx > 0 ) {
562571 rc = aws_iot_shadow_init_json_document (JsonDocumentBuffer , sizeOfJsonDocumentBuffer );
@@ -575,7 +584,17 @@ int main(void)
575584 rc = aws_iot_finalize_json_document (JsonDocumentBuffer , sizeOfJsonDocumentBuffer );
576585 if (rc == SUCCESS ) {
577586 IOT_INFO ("Update Shadow Desired: %s" , JsonDocumentBuffer );
587+ #if ENCRYPT_UPLOAD_DATA
588+ IOT_INFO ("now try to encrypt input:%s len:%d \r\n" , JsonDocumentBuffer , sizeOfJsonDocumentBuffer );
589+ vTaskSuspendAll ();
590+ container_call (crypt_container , operate_encrypt , JsonDocumentBuffer , sizeOfJsonDocumentBuffer , \
591+ JsonDocumentBufferCipher , & sizeOfJsonDocumentBuffer );
592+ xTaskResumeAll ();
593+ IOT_INFO ("result of encrypt output:%s len:%d \r\n" , JsonDocumentBufferCipher , sizeOfJsonDocumentBuffer );
594+ rc = aws_iot_shadow_update (& mqttClient , AWS_IOT_MY_THING_NAME , JsonDocumentBufferCipher , ShadowUpdateStatusCallback , NULL , 4 , true);
595+ #else
578596 rc = aws_iot_shadow_update (& mqttClient , AWS_IOT_MY_THING_NAME , JsonDocumentBuffer , ShadowUpdateStatusCallback , NULL , 4 , true);
597+ #endif
579598 }
580599 }
581600 }
@@ -586,7 +605,9 @@ int main(void)
586605 continue ;
587606 }
588607 /** Update reported messages */
608+ vTaskSuspendAll ();
589609 temperature_updated = container_call (device_container , getRoomTemperature , & temperature );
610+ xTaskResumeAll ();
590611
591612 rc = aws_iot_shadow_init_json_document (JsonDocumentBuffer , sizeOfJsonDocumentBuffer );
592613 if (rc == SUCCESS ) {
@@ -600,16 +621,17 @@ int main(void)
600621 }
601622 }
602623 }
603- u8g_draw ();
624+ u8g_draw ();
604625
605626 toggleHeartbeatLed ();
606627
607628 IOT_INFO ("*****************************************************************************************\n" );
608629 vTaskDelay (delay_ms );
609630 }
610631
611- //delete smarthome_close ();
632+ vTaskSuspendAll ();
612633 container_call (device_container , smarthome_close );
634+ xTaskResumeAll ();
613635 if (SUCCESS != rc ) {
614636 IOT_ERROR ("An error occurred in the loop %d" , rc );
615637 }
0 commit comments