AWS IoT Firmware
Firmware code for AWS IoT Devices
Files | Classes | Macros | Typedefs | Enumerations | Functions
Base Framework

Files

file  aws_iot_fmt.hpp
 used for storing aws iot communication format
 
file  event_handler_util.cpp
 folder carrying utils functions that are helpful either to the master handler or to the child handler
 
file  event_handler_util.hpp
 header file for event handler utils
 
file  master_handler.cpp
 Source files for master handler class.
 
file  master_handler.hpp
 header file for master handler class
 
file  mqtt.hpp
 header file for mqtt functionalities
 
file  parent_handler.cpp
 contain the source files for the ParentHandler class
 
file  parent_handler.hpp
 contain class declaration for the ParentHandler class
 

Classes

struct  ErrDescript
 used for storing the error code and its corresponding error message More...
 
struct  frozen
 
struct  fstate
 
struct  scan_array_info
 
struct  json_scanf_info
 
struct  json_setf_data
 
struct  prettify_data
 
struct  next_data
 
struct  json_token
 
struct  json_out
 
class  ParentHandler
 the parent handler class that is the blueprint for other child handlers to implement, it also defines a couple functions that child handler can use More...
 

Macros

#define PAYLOAD_BEGIN   "{payload:"
 
#define PAYLOAD_END   "}"
 
#define PWR_CTRL_FMT   "{%Q: %Q, %Q: %Q, %Q: %Q}"
 power controller devices reply format
 
#define PWR_CTRL_TOTAL_ARG   6
 power controller total arguments
 
#define SPKR_CTRL_FMT   "{%Q: %Q, %Q: %Q, %Q: %d}, {%Q: %Q, %Q: %Q, %Q: %B}"
 
#define SPKR_CTRL_TOTAL_ARG   12
 
#define SPKR_ADJUST_VOL_PAYLOAD_FMT   PAYLOAD_BEGIN "{volume:%d, volumeDefault:%B}" PAYLOAD_END
 
#define SPKR_ADJUST_VOL_PAYLOAD_TOTAL_ARG   2
 
#define SPKR_SET_MUTE_PAYLOAD_FMT   PAYLOAD_BEGIN "{mute:%B}" PAYLOAD_END
 
#define SPKR_SET_MUTE_PAYLOAD_TOTAL_ARG   1
 
#define SPKR_SET_VOL_PAYLOAD_FMT   PAYLOAD_BEGIN "{volume:%d}" PAYLOAD_END
 
#define SPKR_SET_VOL_PAYLOAD_TOTAL_ARG   1
 
#define TEMP_SENSOR_FMT   "{%Q: %Q, %Q: %Q, %Q: {%Q: %.1f, %Q: %Q}}"
 temperature controller devices reply format
 
#define TEMP_SENSOR_TOTAL_ARG   9
 temperature sensor reply total arguments
 
#define MAX_COMMAND_LEN   30
 maximum length in characters of command name sent by the server
 
#define MAX_NAMESPACE_LEN   30
 maximum length in characters of the namespace name sent by the server
 
#define HANDLER_TRY_FUNC(funcToExecute)
 helper function used to create error message and exit upon errors More...
 
#define MAX_PAYLOAD_SIZE   150
 Maximum characters that the payload received by the mqtt will have.
 
#define MAX_RESPONSE_LEN   1500
 Maximum length in character of the response.
 
#define SUB_QOS   1
 
#define PUB_QOS   1
 
#define _CRT_SECURE_NO_WARNINGS   /* Disable deprecation warning in VS2005+ */
 
#define WEAK
 
#define __STDC_FORMAT_MACROS
 
#define INT64_FMT   PRId64
 
#define UINT64_FMT   PRIu64
 
#define va_copy(x, y)   x = y
 
#define JSON_MAX_PATH_LEN   256
 
#define SET_STATE(fr, ptr, str, len)
 
#define CALL_BACK(fr, tok, value, len)
 
#define EXPECT(cond, err_code)
 
#define TRY(expr)
 
#define END_OF_STRING   (-1)
 
#define JSON_INVALID_TOKEN   { 0, 0, JSON_TYPE_INVALID }
 
#define JSON_STRING_INVALID   -1
 
#define JSON_STRING_INCOMPLETE   -2
 
#define JSON_OUT_BUF(buf, len)
 
#define JSON_OUT_FILE(fp)
 
#define TEMP_BUF_LEN   700
 length in characters of the temporary buffer used for holding a temp json string before it can be copied to the string returned to the master handler
 

Typedefs

typedef void(* json_walk_callback_t) (void *callback_data, const char *name, size_t name_len, const char *path, const struct json_token *token)
 
typedef int(* json_printf_callback_t) (struct json_out *, va_list *ap)
 
typedef void(* json_scanner_t) (const char *str, int len, void *user_data)
 

Enumerations

enum  HandlerError {
  HANDLER_NO_ERR = 0, MQTT_ERR_UNKNOWN_COMMAND, MQTT_ERR_GPIO, JSON_NOT_ENOUGH_ARG,
  MQTT_CONFIG_NOT_SET, JSON_UNKNOWN_NAMESPACE, JSON_VPRINTF_ERROR, JSON_VSCANF_ERROR,
  HANDLER_NULL
}
 enum defining errors that the system(including master handler and other child handler) may have
 
enum  json_token_type {
  JSON_TYPE_INVALID = 0, JSON_TYPE_STRING, JSON_TYPE_NUMBER, JSON_TYPE_TRUE,
  JSON_TYPE_FALSE, JSON_TYPE_NULL, JSON_TYPE_OBJECT_START, JSON_TYPE_OBJECT_END,
  JSON_TYPE_ARRAY_START, JSON_TYPE_ARRAY_END, JSON_TYPES_CNT
}
 

Functions

HandlerError create_error_message (const HandlerError error, struct mg_str *payload, char *errorMessage)
 create error message based on the error code given and stores it in the given buffer More...
 
HandlerError write_pin (const uint8_t pinNum, const uint8_t pinState)
 used for writing to a digital pin given its pin number and state More...
 
HandlerError read_pin (const uint8_t pinNum, uint8_t *pinState)
 used to reat the status of a digital pin More...
 
HandlerError getCommandInfo (struct mg_str *message, char *commandName, char *nameSpace)
 get command name as well as command namespace of messages sent from aws lambda More...
 
HandlerError getAwsPayload (const struct mg_str *message, const char *awsPayloadFormat, const uint8_t &totalArg,...)
 Get the Aws Payload json key. More...
 
HandlerError printStateReportOpen (char *jsonOut)
 print the beginning of an aws smarthome state report More...
 
HandlerError printStateReportDelimiter (char *jsonOut)
 print the delimiter between endpoints reports in aws iot state report More...
 
HandlerError printStateReportEnd (char *jsonOut)
 print the ending part of an aws iot state report More...
 
enum mgos_app_init_result mgos_app_init (void)
 the starting point of the program register the master handler as well as any interrupts define by the user More...
 
void handleAllReq (struct mg_connection *mgCon, int eventType, void *rawMessage, void *user_data)
 The entry point of all events, when an mcu receives an event, whether it's a request from aws or just a confirmation from the mqtt server, it is triaged here. More...
 
void handleAwsRequest (struct mg_connection *mgCon, struct mg_str *payload, char *jsonBuf, ParentHandler *_handlerList[])
 function used for finding the appropriate handler and hands the request to them or create error message to report to the server More...
 
void handlePolling (struct mg_connection *mgCon, ParentHandler *handlerList[], char *jsonBuf)
 dummy empty function since bed room light controller has no polling need More...
 
void sub (struct mg_connection *mgConnection, const char *fmt,...)
 function used for subscribing to mqtt channels, has variable arguments support More...
 
void pub (struct mg_connection *mgConnection, const char *pubTopic, const char *fmt,...)
 used for publishing to a specific channel of the mqtt server More...
 
int json_escape (struct json_out *out, const char *p, size_t len) WEAK
 
int json_printer_buf (struct json_out *out, const char *buf, size_t len) WEAK
 
int json_printer_file (struct json_out *out, const char *buf, size_t len) WEAK
 
int json_vprintf (struct json_out *out, const char *fmt, va_list xap) WEAK
 
int json_printf (struct json_out *out, const char *fmt,...) WEAK
 
int json_printf_array (struct json_out *out, va_list *ap) WEAK
 
int json_walk (const char *json_string, int json_string_length, json_walk_callback_t callback, void *callback_data) WEAK
 
int json_scanf_array_elem (const char *s, int len, const char *path, int idx, struct json_token *token) WEAK
 
int json_unescape (const char *src, int slen, char *dst, int dlen) WEAK
 
int json_vscanf (const char *s, int len, const char *fmt, va_list ap) WEAK
 
int json_scanf (const char *str, int len, const char *fmt,...) WEAK
 
int json_vfprintf (const char *file_name, const char *fmt, va_list ap) WEAK
 
int json_fprintf (const char *file_name, const char *fmt,...) WEAK
 
char * json_fread (const char *path) WEAK
 
int json_vsetf (const char *s, int len, struct json_out *out, const char *json_path, const char *json_fmt, va_list ap) WEAK
 
int json_setf (const char *s, int len, struct json_out *out, const char *json_path, const char *json_fmt,...) WEAK
 
int json_prettify (const char *s, int len, struct json_out *out) WEAK
 
int json_prettify_file (const char *file_name) WEAK
 
void * json_next_key (const char *s, int len, void *handle, const char *path, struct json_token *key, struct json_token *val) WEAK
 
void * json_next_elem (const char *s, int len, void *handle, const char *path, int *idx, struct json_token *val) WEAK
 
char * json_vasprintf (const char *fmt, va_list ap) WEAK
 
char * json_asprintf (const char *fmt,...) WEAK
 
HandlerError ParentHandler::createReport (char *report, const char *fmt, uint8_t totalArg,...)
 used for generating a json report string based on the format given, usually child handler uses this to prepare replies to the aws lambda server More...
 
bool ParentHandler::canHandleRequest (char *requestNameSpace)
 used to check if a handler object can handle a request with a specific namespace More...
 
char * ParentHandler::getNameSpace (void)
 return the command namespace that the handler support More...
 

Detailed Description

Macro Definition Documentation

#define CALL_BACK (   fr,
  tok,
  value,
  len 
)
Value:
do { \
if ((fr)->callback && \
((fr)->path_len == 0 || (fr)->path[(fr)->path_len - 1] != '.')) { \
struct json_token t = {(value), (len), (tok)}; \
\
/* Call the callback with the given value and current name */ \
(fr)->callback((fr)->callback_data, (fr)->cur_name, (fr)->cur_name_len, \
(fr)->path, &t); \
\
/* Reset the name */ \
(fr)->cur_name = NULL; \
(fr)->cur_name_len = 0; \
} \
} while (0)
Definition: frozen.h:62
#define EXPECT (   cond,
  err_code 
)
Value:
do { \
if (!(cond)) return (err_code); \
} while (0)
#define HANDLER_TRY_FUNC (   funcToExecute)
Value:
do { \
HandlerError errCode; \
errCode = funcToExecute; \
if (errCode != HANDLER_NO_ERR) { \
create_error_message(errCode, payload, jsonBuf); \
return; \
} \
} while (0)
HandlerError
enum defining errors that the system(including master handler and other child handler) may have ...
Definition: event_handler_util.hpp:37
HandlerError create_error_message(const HandlerError error, struct mg_str *payload, char *errorMessage)
create error message based on the error code given and stores it in the given buffer ...
Definition: event_handler_util.cpp:38

helper function used to create error message and exit upon errors

#define JSON_OUT_BUF (   buf,
  len 
)
Value:
{ \
json_printer_buf, { \
{ buf, len, 0 } \
} \
}
#define JSON_OUT_FILE (   fp)
Value:
{ \
json_printer_file, { \
{ (char *) fp, 0, 0 } \
} \
}
#define SET_STATE (   fr,
  ptr,
  str,
  len 
)
Value:
struct fstate fstate = {(ptr), (fr)->path_len}; \
append_to_path((fr), (str), (len));
Definition: frozen.c:93
#define TRY (   expr)
Value:
do { \
int _n = expr; \
if (_n < 0) return _n; \
} while (0)

Function Documentation

bool ParentHandler::canHandleRequest ( char *  requestNameSpace)

used to check if a handler object can handle a request with a specific namespace

Parameters
requestNameSpacethe namespace extracted from the message
Returns
true if can handle the request
false if can not handle the request

compare the namespace given with the namespace that the object says it supports and return result

HandlerError create_error_message ( const HandlerError  error,
struct mg_str *  payload,
char *  errorMessage 
)

create error message based on the error code given and stores it in the given buffer

Parameters
errorerror code of the problems
payloadthe payload of the message received by the mcu
errorMessagethe buffer to store the error message
Returns
return any errors that are encountered

The function stores a list of struct that stores the error code and their corresponding error message, the function searches through the struct to find the correct message

HandlerError ParentHandler::createReport ( char *  report,
const char *  fmt,
uint8_t  totalArg,
  ... 
)

used for generating a json report string based on the format given, usually child handler uses this to prepare replies to the aws lambda server

Parameters
reportthe string to write the report to
fmtformat of the report, can be found in aws_iot_fmt.hpp
totalArgtotal arguments to be printed for the report string
...
Returns
HandlerError errors that functions encountered
HandlerError getAwsPayload ( const struct mg_str *  message,
const char *  awsPayloadFormat,
const uint8_t &  totalArg,
  ... 
)

Get the Aws Payload json key.

Parameters
messagemesssage received by mongoose OS from mqtt server
awsPayloadFormatthe payload format of the specific aws device type such as speaker, power controller, etc
totalArghow many arg to parse in the payload format
Returns
HandlerError return error code in case that there is not enough elements to parse
HandlerError getCommandInfo ( struct mg_str *  message,
char *  commandName,
char *  nameSpace 
)

get command name as well as command namespace of messages sent from aws lambda

Parameters
messagemessage received by the mcu through mqtt
commandNamename of the command like "TurnOff"
nameSpacenamespace of the command like "Alexa.PowerController"
Returns
HandlerError error code
char * ParentHandler::getNameSpace ( void  )

return the command namespace that the handler support

Returns
char* the namespace that the command support
void handleAllReq ( struct mg_connection *  mgCon,
int  eventType,
void *  rawMessage,
void *  user_data 
)

The entry point of all events, when an mcu receives an event, whether it's a request from aws or just a confirmation from the mqtt server, it is triaged here.

Parameters
mgConstruct representing connection to server
eventTypetype of event that is being processed
rawMessagethe raw message given to the handler
user_datauser data, this is rarely used and is here to only fulill the prototype request for a handler
void handleAwsRequest ( struct mg_connection *  mgCon,
struct mg_str *  payload,
char *  jsonBuf,
ParentHandler _handlerList[] 
)

function used for finding the appropriate handler and hands the request to them or create error message to report to the server

Parameters
mgConstruct representing connection to server
payloadpayload of message given to the function by the handleAllReq function
jsonBufbuffer to store repy to the aws lambda
void handlePolling ( struct mg_connection *  mgCon,
ParentHandler handlerList[],
char *  jsonBuf 
)

dummy empty function since bed room light controller has no polling need

Parameters
mgConmongoose network connection
handlerListlist of pointer to handler object
jsonBufjson buffer for writing data

dummy empty function since bed room light controller has no polling need

Parameters
mgConmongoose network connection
handlerListlist of pointer to handler object
jsonBufjson buffer for writing data

dummy empty function since bed room light controller has no polling need

Parameters
mgConobject representing connection to the internet
_handlerListthe list of handler that user defined
jsonBufif you write something to this buffer, it will be published to the mqtt server of the device
enum mgos_app_init_result mgos_app_init ( void  )

the starting point of the program register the master handler as well as any interrupts define by the user

Returns
enum mgos_app_init
HandlerError printStateReportDelimiter ( char *  jsonOut)

print the delimiter between endpoints reports in aws iot state report

Parameters
jsonOutbuffer to store the json report
Returns
HandlerError problem error code
HandlerError printStateReportEnd ( char *  jsonOut)

print the ending part of an aws iot state report

Parameters
jsonOutbuffer to store the json report
Returns
HandlerError problem error code
HandlerError printStateReportOpen ( char *  jsonOut)

print the beginning of an aws smarthome state report

Parameters
jsonOutthe buffer to store the report
Returns
HandlerError problem error code
void pub ( struct mg_connection *  mgConnection,
const char *  pubTopic,
const char *  fmt,
  ... 
)

used for publishing to a specific channel of the mqtt server

Parameters
mgConnectionstruct represents connection to server
pubTopictopic to publish to
fmtformat of the message to be published
...variable arguments for the format specified
HandlerError read_pin ( const uint8_t  pinNum,
uint8_t *  pinState 
)

used to reat the status of a digital pin

Parameters
pinNumpin number to read
pinStatepointer to write the result of the read to
Returns
Handler Error problem error code
void sub ( struct mg_connection *  mgConnection,
const char *  fmt,
  ... 
)

function used for subscribing to mqtt channels, has variable arguments support

Parameters
mgConnectionstruct represents connection to server
fmtformat of the string to be sent to server for subscribing
...variable arguments for the format specified
HandlerError write_pin ( const uint8_t  pinNum,
const uint8_t  pinState 
)

used for writing to a digital pin given its pin number and state

Parameters
pinNumpin to write
pinStatestate of the pin, 0 for off, 1 for on
Returns
HandlerError return error code