![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MQTT version for enno SDK
Dependencies: EthernetInterface MQTT mbed-rtos mbed serialzation_lib
Fork of HelloMQTT by
Diff: protocol/enno_api_mqtt_wrapper.c
- Revision:
- 21:c5f6350a292d
- Parent:
- 20:3858e42b0cd3
--- 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;