MQTT version for enno SDK
Dependencies: EthernetInterface MQTT mbed-rtos mbed serialzation_lib
Fork of HelloMQTT by
Revision 21:c5f6350a292d, committed 2015-12-02
- Comitter:
- weiarm2015
- Date:
- Wed Dec 02 01:54:31 2015 +0000
- Parent:
- 20:3858e42b0cd3
- Commit message:
- This is a alpha version of enno SDK for mbed
Changed in this revision
diff -r 3858e42b0cd3 -r c5f6350a292d MQTT.lib --- a/MQTT.lib Wed Nov 25 07:07:31 2015 +0000 +++ b/MQTT.lib Wed Dec 02 01:54:31 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/mqtt/code/MQTT/#c299463ae853 +https://developer.mbed.org/users/weiarm2015/code/MQTT/#6769c1de66d6
diff -r 3858e42b0cd3 -r c5f6350a292d common/enno_error.h --- a/common/enno_error.h Wed Nov 25 07:07:31 2015 +0000 +++ b/common/enno_error.h Wed Dec 02 01:54:31 2015 +0000 @@ -8,7 +8,7 @@ #ifndef COM_ENNO_ERROR_H_ #define COM_ENNO_ERROR_H_ -#if definded(__cplusplus) +#if defined(__cplusplus) extern "C" { #endif
diff -r 3858e42b0cd3 -r c5f6350a292d enno_config.h --- a/enno_config.h Wed Nov 25 07:07:31 2015 +0000 +++ b/enno_config.h Wed Dec 02 01:54:31 2015 +0000 @@ -2,15 +2,20 @@ * enno_config.h * * Created on: 2015年11月20日 - * Author: wzy + * Author: v-chawei */ #ifndef ENNO_CONFIG_H_ #define ENNO_CONFIG_H_ +#ifdef __cplusplus +extern "C" { +#endif #define ENNO_IOT_TX_BUF_LEN 512 #define ENNO_IOT_RX_BUF_LEN 512 - +#ifdef __cplusplus +} +#endif #endif /* ENNO_CONFIG_H_ */
diff -r 3858e42b0cd3 -r c5f6350a292d main.cpp --- a/main.cpp Wed Nov 25 07:07:31 2015 +0000 +++ b/main.cpp Wed Dec 02 01:54:31 2015 +0000 @@ -1,15 +1,33 @@ +/* + * ennoForMbed.c + * + * Created on: 2015年11月20日 + * Author: v-chawei + */ #include "mbed.h" +#include "MQTTClient.h" #include "enno_api.h" + #define MQTTCLIENT_QOS2 1 DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); -int main(int argc, char* argv[]){ +int main(int argc, char* argv[]){ + Error_t rc = NONE_ERROR; float version = 0.5; + char *ennoCommandPrefix = "SiteWhere/commands/"; + char *ennoSystemPrefix = "SiteWhere/system/"; + char *ennoSystemInbound = "SiteWhere/input/protobuf"; + char *deviceId = "enno-xbed-test-30"; + char *topicType = "command"; + char *hostURL = "139.217.10.99"; + int port = 1883; + char *appKey = NULL; + led1=1;led2=1;led3=1;led4=1; //light on wait(0.5); led1=!led1;led2=!led2;led3=!led3;led4=!led4; //light off @@ -20,7 +38,51 @@ printf("HelloMQTT: version is %f\n", version); - return 0; + printf("Connecting to enno server %s:%d...\n",hostURL,port); + + rc = enno_connect(hostURL, port, appKey); + if (NONE_ERROR != rc) { + printf("Error(%d) on connecting.\n", rc); + exit(rc); + } + printf("connected.\n"); + + printf("Subscribing system topic..."); + char systemTopic[64]; + char commandTopic[64]; + strcpy(systemTopic,ennoSystemPrefix); + strcat(systemTopic,deviceId); + + rc = enno_subscribe(systemTopic, topicType); + if (NONE_ERROR != rc) { + printf("Error on subscrie system topic."); + exit(rc); + } + printf("subscribed.\n"); + + printf("Send device registeration...\n"); + + EnnoMessages Messages_enno = EnnoMessagesDefault; + Messages_enno.Register.pHardwareId = deviceId; + Messages_enno.messageType = 'R'; + rc = enno_publish(ennoSystemInbound, &Messages_enno); + if (NONE_ERROR != rc) { + printf("Error on Registering device.\n"); + exit(rc); + } + + printf("sent.\n"); + Messages_enno.messageType = 'L'; + // main loop + while (NONE_ERROR == rc) { + enno_loop(1000); + // sleep(1); + rc = enno_publish(ennoSystemInbound, &Messages_enno); + led3=!led3; + printf("Location updated.\n"); + } + + return rc; }
diff -r 3858e42b0cd3 -r c5f6350a292d protocol/CplusplusToC.cpp --- a/protocol/CplusplusToC.cpp Wed Nov 25 07:07:31 2015 +0000 +++ b/protocol/CplusplusToC.cpp Wed Dec 02 01:54:31 2015 +0000 @@ -1,13 +1,23 @@ +/* + * CplusplusToC.c + * + * Created on: 2015年11月20日 + * Author: v-chawei + */ +#include "MQTTEthernet.h" #include "MQTTClient.h" -#include "stdio.h" +#include "string.h" +#include "enno_api.h" static MQTTEthernet ipstack = MQTTEthernet(); static MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); +int symbol=0; +int arrivedcount=0; extern "C"{ - int trans_connect(char *hostURL, uint16_t port, char *appKey); - int trans_subscribe(); - int trans_publish(); + int trans_connect(char *hostURL, uint16_t port, char *appKey); + int trans_subscribe(char *topic, char *topicType); + int trans_publish(char *path, char *payload, int payloadlen); int trans_unsubscribe(char *pTopic); int trans_disconnect(); int trans_loop(int timeout); @@ -15,35 +25,90 @@ int trans_connect(char *hostURL, uint16_t port, char *appKey){ int i = 0; - - return i; -} -int trans_subscribe(){ - int i = 0; + if( symbol==0 ){ + ipstack.start(); + symbol=1; + } + i = ipstack.connect(hostURL, port); + if (i == 0 ){ + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "mbed-sample"; + data.username.cstring = NULL; + data.password.cstring = NULL; + if ((i = client.connect(data)) != 0) + printf("Error: connect failed %d\n", i); + } return i; } -int trans_publish(){ + + + void systemMessageHandler(MQTT::MessageData& md) + { + uint8_t buffer[512]; + MQTT::Message &message = md.message; + printf("system message received.\n"); + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); + printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); + ++arrivedcount; + ennoSystemMessageHandler(md.topicName.cstring, (char*)message.payload, message.payloadlen); + // extern "C"{ + // callbackRef(md.topicName.cstring, message.payload, message.payloadlen); + // } + } + void commandMessageHandler(MQTT::MessageData& md) + { + uint8_t buffer[512]; + MQTT::Message &message = md.message; + printf("command message received.\n"); + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); + printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); + ++arrivedcount; + } +int trans_subscribe(char *topic, char *topicType){ int i = 0; + if((strcmp(topicType,"system")) == 0){ + if ((i = client.subscribe(topic, MQTT::QOS2, systemMessageHandler)) != 0){ + printf("Error: subscribe failed %d\n", i); + } + }else{ + if ((i = client.subscribe(topic, MQTT::QOS2, commandMessageHandler)) != 0){ + printf("Error: subscribe failed %d\n", i); + } + } + return i; +} +int trans_publish(char *path, char *payload, int payloadlen){ + int i = 0; + MQTT::Message message; + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = payload; + message.payloadlen = payloadlen; + if ((i = client.publish(path, message)) != 0){ + printf("Error: publish failed %d\n", i); + } return i; } int trans_unsubscribe(char *pTopic){ int i = 0; if ((i = client.unsubscribe(pTopic))!= 0){ - printf("Error: unsubscribe fail %d\n", i); + printf("Error: unsubscribe failed %d\n", i); } - return i; + return i; } int trans_disconnect(){ int i = 0; if ((i = client.disconnect()) != 0) - printf("Error: disconnect fail %d\n", i); - if(i ==0 ) { + printf("Error: disconnect failed %d\n", i); + if(i == 0 ) { ipstack.disconnect(); - } + } return i; } int trans_loop(int timeout){ int i = 0; i = client.yield(timeout); return i; -} \ No newline at end of file +}
diff -r 3858e42b0cd3 -r c5f6350a292d protocol/enno_api.c --- a/protocol/enno_api.c Wed Nov 25 07:07:31 2015 +0000 +++ b/protocol/enno_api.c Wed Dec 02 01:54:31 2015 +0000 @@ -1,6 +1,44 @@ +/* + * enno_api.c + * + * Created on: 2015年11月20日 + * Author: v-chawei + */ #include <stdio.h> #include <stdbool.h> #include "enno_api.h" #include "sitewhere.pb.h" #include "sitewhere.h" #include "pb_decode.h" +//#include "custom.h" +//#include "raspberrypi.pb.h" + +bool registered = false; + +void ennoSystemMessageHandler(char* topic, char* payload, int length) { + Device_Header header; + + pb_istream_t stream = pb_istream_from_buffer(payload, length); + + if (pb_decode_delimited(&stream, Device_Header_fields, &header)) { + + if (header.command == Device_Command_REGISTER_ACK) { + Device_RegistrationAck ack; + if (pb_decode_delimited(&stream, Device_RegistrationAck_fields, &ack)) { + if (ack.state == Device_RegistrationAckState_NEW_REGISTRATION) { + printf("Registered new device.\n"); + registered = true; + } + else if (ack.state == Device_RegistrationAckState_ALREADY_REGISTERED) { + printf("Device has already registered.\n"); + } + else if (ack.state == Device_RegistrationAckState_REGISTRATION_ERROR) { + printf("Error rigistering device.\n"); + } + } + } + } else { + printf("Unable to decode system command.\n"); + } +} +
diff -r 3858e42b0cd3 -r c5f6350a292d protocol/enno_api.h --- a/protocol/enno_api.h Wed Nov 25 07:07:31 2015 +0000 +++ b/protocol/enno_api.h Wed Dec 02 01:54:31 2015 +0000 @@ -2,16 +2,12 @@ * enno_api.h * * Created on: 2015年11月20日 - * Author: wzy + * Author: v-chawei */ - #ifndef PROTOCOL_ENNO_API_H_ #define PROTOCOL_ENNO_API_H_ -// -// Created by root on 11/10/15. -// -#if definded(__cplusplus) +#if defined(__cplusplus) extern "C" { #endif @@ -68,25 +64,26 @@ Location_enno Location; Alert_enno Alert; } EnnoMessages; -//extern EnnoMessages EnnoMessagesDefault; -EnnoMessages EnnoMessagesDefault = { - .messageType = 'R', - .Register = {.pHardwareId = "enno-default-test-id-11", .pSpecificationToken = "417b36a8-21ef-4196-a8fe-cc756f994d0b", .pOriginator = NULL}, //7dfd6d63-5e8d-4380-be04-fc5c73801dfb - .Acknowledge = {.pMessage = NULL, .pOriginator = NULL}, - .Measurement = {.pName = "Temperature", .pOriginator = NULL, .value = 16, .eventDate = 0}, - .Location = {.pOriginator = NULL, .lat = 27.59, .lon = 86.5, .ele = 8844.43, .eventDate = 0}, - .Alert = {.pAlertType = "enno.info", .pAlertMessage = "I am alive", .pOriginator = NULL, .eventDate = 0} -}; +#define EnnoMessagesDefault {'R',{"enno-xbed-test-25", "417b36a8-21ef-4196-a8fe-cc756f994d0b", NULL},{"enno-xbed-test-25", NULL, NULL},{"enno-xbed-test-25", "temperature", NULL, 10, 0},{"enno-xbed-test-25", NULL, 27.59, 86.55, 8844.43, 0},{"enno-xbed-test-25", "enno.info", "I am alive", NULL, 0}} /** * @brief Handles the system callback message * - * params: topic the topic subscribed by client + * params: topic the system topic subscribed by client * payload the received payload * length the length of payload * */ void ennoSystemMessageHandler(char* topic, char* payload, int length); +/** + * @brief Handles the command callback message + * + * params: topic the command topic subscribed by client + * payload the received payload + * length the length of payload + * + */ +void ennoCommandMessageHandler(char* topic, char* payload, int length); /** * @brief Connection Function @@ -110,7 +107,7 @@ * callback Pointer to a callback function * @return An IoT Error Type defining successful/failed subscription */ -Error_t enno_subscribe(char *topic, void *callback); +Error_t enno_subscribe(char *topic, char *topicType); /** * @brief Publish a message to a path * @@ -120,8 +117,6 @@ * messages_enno Pointer to the published message * @return An IoT Error Type defining successful/failed publish */ - -//Error_t enno_publish(char path, EnnoParams *Params_enno, PublishParams *pParams); Error_t enno_publish(char *path, EnnoMessages *messages_enno); /** * @brief Unsubscribe to an topic.
diff -r 3858e42b0cd3 -r c5f6350a292d protocol/enno_api_mqtt_wrapper.c --- a/protocol/enno_api_mqtt_wrapper.c Wed Nov 25 07:07:31 2015 +0000 +++ b/protocol/enno_api_mqtt_wrapper.c Wed Dec 02 01:54:31 2015 +0000 @@ -1,13 +1,21 @@ -#include "mbed.h" -#include "MQTTEthernet.h" -#include "MQTTPacket.h" -#include "MQTTClient.h" +/* + * enno_api_mqtt_wrapper.c + * + * Created on: 2015年11月20日 + * Author: v-chawei + */ #include "pb_decode.h" #include "sitewhere.h" #include "sitewhere.pb.h" #include "enno_api.h" - - +/* + int trans_connect(char *hostURL, uint16_t port, char *appKey); + int trans_subscribe(char *topic, char *topicType); + int trans_publish(char *path, char *payload, int payloadlen); + int trans_unsubscribe(char *pTopic); + int trans_disconnect(); + int trans_loop(int timeout); +*/ //static iot_disconnect_handler clientDisconnectHandler; //warning defined but not used /** @@ -105,11 +113,6 @@ .disconnectHandler = NULL }; - - - - - static Error_t parseConnectParamsForError(char *hostURL) { Error_t rc = NONE_ERROR; if (NULL == hostURL) { @@ -119,27 +122,64 @@ return rc; } -static bool isPowerCycle = true; + Error_t enno_connect(char *hostURL, uint16_t port, char *appKey){ Error_t rc = NONE_ERROR; - - if + rc = parseConnectParamsForError(hostURL); + int i = 0; + /* if (NULL == hostURL) { + rc = NULL_VALUE_ERROR; + printf("ERROR: hostURL can not be empty!\n"); + }*/ + if (rc == NONE_ERROR){ + if((i=trans_connect(hostURL, port, appKey))!=0){ + // rc = CONNECTION_ERROR; + rc = i; + } + } return rc; } -Error_t enno_subscribe(char *topic, void *callback){ +Error_t enno_subscribe(char *topic, char *topicType){ Error_t rc = NONE_ERROR; - if (trans_subscribe()){ + if (0 !=trans_subscribe(topic, topicType)){ rc = SUBSCRIBE_ERROR; } return rc; } -*/ + Error_t enno_publish(char *path, EnnoMessages *messages_enno){ Error_t rc = NONE_ERROR; - if (0 != trans_publish()){ - rc = PUBLISH_ERROR; + unsigned int len = 0; + unsigned char buffer[512]; + + switch (messages_enno->messageType) { + case 'R': + len = sw_register(messages_enno->Register.pHardwareId, messages_enno->Register.pSpecificationToken, buffer, + 512, messages_enno->Register.pOriginator); + break; + case 'A': + len = sw_alert(messages_enno->Register.pHardwareId, messages_enno->Alert.pAlertType, + messages_enno->Alert.pAlertMessage, messages_enno->Alert.eventDate, buffer, 512, + messages_enno->Alert.pOriginator); + break; + case 'M': + len = sw_measurement(messages_enno->Register.pHardwareId, messages_enno->Measurement.pName, + messages_enno->Measurement.value, messages_enno->Measurement.eventDate, buffer, 512, + messages_enno->Measurement.pOriginator); + break; + case 'L': + len = sw_location(messages_enno->Register.pHardwareId, messages_enno->Location.lat, + messages_enno->Location.lon, messages_enno->Location.ele, messages_enno->Location.eventDate, + buffer, 512, messages_enno->Location.pOriginator); + break; + default: + printf("WARN:Nothing published.\n"); + break; + } + if (0 != trans_publish(path, buffer, len)){ + rc = PUBLISH_ERROR; } return rc; } @@ -155,7 +195,7 @@ Error_t enno_disconnect() { Error_t rc = NONE_ERROR; - if (0! =trans_disconnect()){ + if (0 != trans_disconnect()){ rc = DISCONNECT_ERROR; } return rc;
diff -r 3858e42b0cd3 -r c5f6350a292d serialzation_lib.lib --- a/serialzation_lib.lib Wed Nov 25 07:07:31 2015 +0000 +++ b/serialzation_lib.lib Wed Dec 02 01:54:31 2015 +0000 @@ -1,1 +1,1 @@ -serialzation_lib#056edc3ec132 +https://developer.mbed.org/users/weiarm2015/code/serialzation_lib/#056edc3ec132