diff --git a/platformio.ini b/platformio.ini index 37e482e..36618b9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,8 +18,8 @@ platform = espressif8266 framework = arduino monitor_speed = 115200 upload_speed = 115200 -upload_port = COM6 -monitor_port = COM6 +upload_port = COM5 +monitor_port = COM5 board_build.flash_mode = dout build_flags = -Wno-unused-function diff --git a/src/IRBabyIRIS.cpp b/src/IRBabyIRIS.cpp index e1aaaf1..47e3490 100644 --- a/src/IRBabyIRIS.cpp +++ b/src/IRBabyIRIS.cpp @@ -51,12 +51,24 @@ char iris_credential_token[CREDENTIAL_MAX] = { 0 }; char iris_server_address[URL_SHORT_MAX] = { 0 }; - // private function declarations +static int processEvent(const char* event_name, const char* payload, int length); static String buildConnect(); - static String buildHeartBeat(); +static int handleHartBeat(const char* payload, int length); +static int handleEmit(const char* payload, int length); +// private variable definitions +event_handler_t event_handler_table[] = { + { + "__hb_response", + handleHartBeat, + }, + { + "__emmitCode", + handleEmit, + } +}; // public function definitions int getIRISKitVersion(char *buffer, int buffer_size) { @@ -154,11 +166,37 @@ void sendIrisKitHeartBeat() { } void handleIrisKitMessage(const char* data, int length) { + int ret = 0; + char* payload = (char*) malloc(length + 1); + if (NULL != payload) { + strncpy(payload, data, length); + payload[length] = '\0'; + INFOF("--> %s\n", payload); + if (OK == deserializeJson(iris_ind_doc, payload)) { + String event_name = iris_ind_doc["eventName"]; + INFOF("received ind : %s\n", event_name.c_str()); + ret = processEvent(event_name.c_str(), payload, length); + INFOF("event handle result = %d\n", ret); + } + } + if (NULL != payload) { + free(payload); + } } // private function definitions +static int processEvent(const char* event_name, const char* payload, int length) { + int event_table_length = sizeof(event_handler_table) / sizeof(event_handler_table[0]); + for (int i = 0; i < event_table_length; i++) { + if (0 == strcmp(event_name, event_handler_table[i].event_name)) { + return event_handler_table[i].handler(payload, length); + } + } + return -1; +} + static String buildConnect() { String connectMessage = ""; @@ -182,4 +220,19 @@ static String buildHeartBeat() { serializeJson(iris_msg_doc, heartBeatMessage); return heartBeatMessage; +} + +static int handleHartBeat(const char* payload, int length) { + // TODO: + // do nothing currently + (void) payload; + (void) length; + return 0; +} + +static int handleEmit(const char* payload, int length) { + // TODO: + (void) payload; + (void) length; + return 0; } \ No newline at end of file diff --git a/src/IRbabyAlink.cpp b/src/IRbabyAlink.cpp index 3fa13c5..321db58 100644 --- a/src/IRbabyAlink.cpp +++ b/src/IRbabyAlink.cpp @@ -31,8 +31,15 @@ #include "IRbaby.h" -#define TOPIC_NAME_MAX (64) -#define IOT_RETRY_MAX (3) +#define TOPIC_NAME_MAX (64) +#define IOT_RETRY_MAX (3) + +#define IRIS_KIT_PK_DEV "a1WlzsJh50b" +#define IRIS_KIT_PK_REL "a1ihYt1lqGH" +#define TOPIC_DOWNSTREAM_DEV "/user/iris/downstream_dev" +#define TOPIC_UPSTREAM_DEV "/user/iris/upstream_dev" +#define TOPIC_DOWNSTREAM_REL "/user/iris/downstream" +#define TOPIC_UPSTREAM_REL "/user/iris/upstream" String g_product_key = ""; String g_device_name = ""; @@ -54,6 +61,18 @@ static int iot_retry = 0; void connectToAliyunIoT() { downstream_topic_subscribed = false; + + if (g_product_key.equals(IRIS_KIT_PK_DEV)) { + g_upstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_UPSTREAM_DEV; + g_downstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_DOWNSTREAM_DEV; + } else if (g_product_key.equals(IRIS_KIT_PK_REL)) { + g_upstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_UPSTREAM_REL; + g_downstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_DOWNSTREAM_REL; + } else { + ERRORF("IRIS Kit release key is not supported yet\n"); + return; + } + INFOF("Try connecting to Aliyun IoT : %s, %s, %s, %s\n", g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(), g_region_id.c_str()); @@ -62,8 +81,7 @@ void connectToAliyunIoT() { sendIrisKitConnect(); } INFOLN("Aliyun IoT connect done"); - g_upstream_topic = g_product_key + "/" + g_device_name + "/user/iris/upstream"; - g_downstream_topic = g_product_key + "/" + g_device_name + "/user/iris/downstream"; + } void checkAlinkMQTT() { @@ -73,6 +91,7 @@ void checkAlinkMQTT() { if (0 == mqttStatus) { iot_retry = 0; if (false == downstream_topic_subscribed) { + INFOF("subscribe topic : %s\n", g_downstream_topic.c_str()); registerCallback(g_downstream_topic.c_str(), 0); downstream_topic_subscribed = true; } else { @@ -105,7 +124,7 @@ static void registerCallback(const char* topic, int qos) { } static void irisAlinkCallback(const char *topic, uint8_t *data, int length) { - INFOF("IRIS downstream message : topic = %s, length = %d, data = %s\n", topic, length, (char*) data); + INFOF("downstream message received, topic = %s, length = %d\n", topic, length); if (NULL != g_downstream_topic.c_str() && 0 == strcmp(topic, g_downstream_topic.c_str())) { handleIrisKitMessage((const char*) data, length); } diff --git a/src/IRbabyIRIS.h b/src/IRbabyIRIS.h index 9939711..70b2ece 100644 --- a/src/IRbabyIRIS.h +++ b/src/IRbabyIRIS.h @@ -37,6 +37,12 @@ #define EVENT_NAME_CONNECT "__connect" #define EVENT_HEART_BEAT_REQ "__hb_request" +typedef int (*eventHandler)(const char* payload, int length); +typedef struct { + const char* event_name; + eventHandler handler; +} event_handler_t; + int getIRISKitVersion(char *buffer, int buffer_size); int getDeviceID(char* buffer, int buffer_size);