362#include "mmhal_app.h"
367#include "mqtt_agent_task.h"
368#if defined(ENABLE_SHADOW_APP) && ENABLE_SHADOW_APP
369#include "shadow_device_task.h"
371#if defined(ENABLE_PROVISIONING_APP) && ENABLE_PROVISIONING_APP
372#include "fleet_provisioning_task.h"
374#include "sntp_client.h"
375#include "core_json.h"
380#define NTP_MIN_TIMEOUT 3000
382#define NTP_MIN_BACKOFF 60000
384#define NTP_MIN_BACKOFF_JITTER 3000
386#define NTP_MAX_BACKOFF_JITTER 60000
407#define SHADOW_PUBLISH_JSON \
414 "\"clientToken\":\"%06lu\"" \
417#if defined(ENABLE_SHADOW_APP) && ENABLE_SHADOW_APP
420static uint32_t ulCurrentPowerOnState = 0;
423static uint32_t ulDesiredPowerOnState = 0;
426struct mmosal_sem *state_change_sem = NULL;
435void shadow_update_callback(
char *json,
size_t json_len,
enum shadow_update_status status)
438 int result = JSON_Validate(json, json_len);
440 if (result != JSONSuccess)
442 printf(
"ERR:Invalid JSON document received\n");
446 static uint32_t ulCurrentVersion = 0;
447 char *pcOutValue = NULL;
448 uint32_t ulOutValueLength = 0UL;
449 uint32_t ulVersion = 0;
474 result = JSON_Search(json,
479 (
size_t *)&ulOutValueLength);
481 if (result != JSONSuccess)
483 printf(
"ERR:Version field not found in JSON document\n");
487 ulVersion = (uint32_t)strtoul(pcOutValue, NULL, 10);
489 if (ulVersion <= ulCurrentVersion)
495 printf(
"ERR:Received unexpected delta update with version %u, "
496 "current version is %u\n",
497 (
unsigned int)ulVersion,
498 (
unsigned int)ulCurrentVersion);
502 ulCurrentVersion = ulVersion;
505 result = JSON_Search(json,
508 sizeof(
"state.powerOn") - 1,
510 (
size_t *)&ulOutValueLength);
512 if (result != JSONSuccess)
514 printf(
"ERR:powerOn field not found in JSON document\n");
519 ulDesiredPowerOnState = (uint32_t)strtoul(pcOutValue, NULL, 10);
526 case UPDATE_ACCEPTED:
557 case UPDATE_REJECTED:
568 result = JSON_Search(json,
573 (
size_t *)&ulOutValueLength);
576 ulCode = (uint32_t)strtoul(pcOutValue, NULL, 10);
578 printf(
"ERR:Received rejected response code %lu\n", ulCode);
581 result = JSON_Search(json,
584 sizeof(
"message") - 1,
586 (
size_t *)&ulOutValueLength);
587 printf(
" Message: %.*s\n", (
int)ulOutValueLength, pcOutValue);
598void aws_shadow_loop(
char *shadow_name)
604 if (ulDesiredPowerOnState == 1)
607 printf(
"INF:Setting powerOn state to 1.\n");
609 ulCurrentPowerOnState = ulDesiredPowerOnState;
611 else if (ulDesiredPowerOnState == 0)
614 printf(
"INF:Setting powerOn state to 0.\n");
616 ulCurrentPowerOnState = ulDesiredPowerOnState;
621 printf(
"ERR:Invalid power state %lu requested.\n", ulDesiredPowerOnState);
625 printf(
"INF:Reporting change in PowerOn state to %lu.\n", ulCurrentPowerOnState);
631 (void)memset(UpdateDocument, 0x00,
sizeof(UpdateDocument));
632 snprintf(UpdateDocument,
635 ulCurrentPowerOnState,
639 aws_publish_shadow(shadow_name, UpdateDocument);
641 printf(
"INF:Publishing to /update with following client token %lu.\n", ulClientToken);
652 printf(
"\n\nMorse AWS IoT Demo (Built " __DATE__
" " __TIME__
")\n\n");
659 char sntp_server[64];
660 strncpy(sntp_server,
"time.aws.com",
sizeof(sntp_server));
662 sntp_sync_with_backoff(sntp_server,
672 printf(
"Current Time (UTC) is : %s\r\n", ctime(&now));
675 start_mqtt_agent_task();
678 char *shadow_name = NULL;
684#if defined(ENABLE_PROVISIONING_APP) && ENABLE_PROVISIONING_APP
689 printf(
"Initiating fleet provisioning...\n");
690 do_fleet_provisioning();
691 printf(
"Failed to provision device, unable to continue.\n"
692 "Please see getting started guide on how to provision.\n");
696 printf(
"Device is not provisioned, "
697 "please see getting started guide on how to provision.\n");
701#if defined(ENABLE_SHADOW_APP) && ENABLE_SHADOW_APP
705 aws_create_shadow(shadow_name, shadow_update_callback);
706 aws_shadow_loop(shadow_name);
#define NTP_MIN_BACKOFF_JITTER
Minimum back-off jitter per attempt.
#define NTP_MIN_TIMEOUT
Minimum NTP timeout per attempt.
#define SHADOW_PUBLISH_JSON
Format string representing a Shadow document with a "reported" state.
#define NTP_MIN_BACKOFF
We need to back-off at least 60 seconds or most NTP Servers will tell us to go away.
void app_init(void)
Main entry point to the application.
#define NTP_MAX_BACKOFF_JITTER
Maximum back-off jitter per attempt.
Contains the configuration parameters for the AWS IoT application.
#define AWS_KEY_PROVISIONING_TEMPLATE
The config store key storing the fleet provisioning template name.
#define MAX_JSON_LEN
Maximum length of JSON strings.
#define AWS_KEY_SHADOW_NAME
The config store key storing the AWS shadow name.
#define AWS_KEY_THING_NAME
The config store key storing the thing name, if not found it is assumed the device needs provisioning...
int mmconfig_read_string(const char *key, char *buffer, int bufsize)
Returns the persistent store string value identified by the key.
int mmconfig_alloc_and_load(const char *key, void **data)
Allocates memory and loads the data from persistent memory into it returning a pointer.
#define LED_OFF
A value of 0 turns OFF an LED.
void mmhal_set_led(uint8_t led, uint8_t level)
Set the specified LED to the requested level.
time_t mmhal_get_time(void)
Returns the time of day as set in the RTC.
#define LED_ON
A value of 255 turns an LED ON fully.
bool mmosal_sem_give(struct mmosal_sem *sem)
Give a counting semaphore.
struct mmosal_sem * mmosal_sem_create(unsigned max_count, unsigned initial_count, const char *name)
Create a new counting semaphore.
bool mmosal_sem_wait(struct mmosal_sem *sem, uint32_t timeout_ms)
Wait for a counting semaphore.
uint32_t mmosal_get_time_ticks(void)
Get the system time in ticks.
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.