support aliyun sdk on TencentOS tiny

sample: examples\aliyun_iotkit_csdk_mqtt
project: board\TencentOS_tiny_EVB_MX_Plus\KEIL\aliyun_iotkit_csdk_mqtt
This commit is contained in:
dcxajichu
2019-10-31 16:36:28 +08:00
parent 30ea36a7ab
commit 8c24d921b0
692 changed files with 199829 additions and 0 deletions

View File

@@ -0,0 +1,250 @@
#include "dev_sign_api.h"
#include "mqtt_api.h"
char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
void *HAL_Malloc(uint32_t size);
void HAL_Free(void *ptr);
void HAL_Printf(const char *fmt, ...);
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
uint64_t HAL_UptimeMs(void);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
#define EXAMPLE_TRACE(fmt, ...) \
do { \
HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
HAL_Printf(fmt, ##__VA_ARGS__); \
HAL_Printf("%s", "\r\n"); \
} while(0)
void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt) msg->msg;
switch (msg->event_type) {
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
/* print topic name and topic message */
EXAMPLE_TRACE("Message Arrived:");
EXAMPLE_TRACE("Topic : %.*s", topic_info->topic_len, topic_info->ptopic);
EXAMPLE_TRACE("Payload: %.*s", topic_info->payload_len, topic_info->payload);
EXAMPLE_TRACE("\n");
break;
default:
break;
}
}
int example_subscribe(void *handle)
{
int res = 0;
const char *fmt = "/%s/%s/user/get";
char *topic = NULL;
int topic_len = 0;
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
if (res < 0) {
EXAMPLE_TRACE("subscribe failed");
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}
int example_publish(void *handle)
{
int res = 0;
const char *fmt = "/%s/%s/user/get";
char *topic = NULL;
int topic_len = 0;
char *payload = "{\"message\":\"hello!\"}";
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
if (res < 0) {
EXAMPLE_TRACE("publish failed, res = %d", res);
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}
void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
EXAMPLE_TRACE("msg->event_type : %d", msg->event_type);
}
/*
* NOTE: About demo topic of /${productKey}/${deviceName}/user/get
*
* The demo device has been configured in IoT console (https://iot.console.aliyun.com)
* so that its /${productKey}/${deviceName}/user/get can both be subscribed and published
*
* We design this to completely demonstrate publish & subscribe process, in this way
* MQTT client can receive original packet sent by itself
*
* For new devices created by yourself, pub/sub privilege also requires being granted
* to its /${productKey}/${deviceName}/user/get for successfully running whole example
*/
int main(int argc, char *argv[])
{
void *pclient = NULL;
int res = 0;
int loop_cnt = 0;
iotx_mqtt_param_t mqtt_params;
HAL_GetProductKey(DEMO_PRODUCT_KEY);
HAL_GetDeviceName(DEMO_DEVICE_NAME);
HAL_GetDeviceSecret(DEMO_DEVICE_SECRET);
EXAMPLE_TRACE("mqtt example");
/* Initialize MQTT parameter */
/*
* Note:
*
* If you did NOT set value for members of mqtt_params, SDK will use their default values
* If you wish to customize some parameter, just un-comment value assigning expressions below
*
**/
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
/**
*
* MQTT connect hostname string
*
* MQTT server's hostname can be customized here
*
* default value is ${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com
*/
/* mqtt_params.host = "something.iot-as-mqtt.cn-shanghai.aliyuncs.com"; */
/**
*
* MQTT connect port number
*
* TCP/TLS port which can be 443 or 1883 or 80 or etc, you can customize it here
*
* default value is 1883 in TCP case, and 443 in TLS case
*/
/* mqtt_params.port = 1883; */
/**
*
* MQTT request timeout interval
*
* MQTT message request timeout for waiting ACK in MQTT Protocol
*
* default value is 2000ms.
*/
/* mqtt_params.request_timeout_ms = 2000; */
/**
*
* MQTT clean session flag
*
* If CleanSession is set to 0, the Server MUST resume communications with the Client based on state from
* the current Session (as identified by the Client identifier).
*
* If CleanSession is set to 1, the Client and Server MUST discard any previous Session and Start a new one.
*
* default value is 0.
*/
/* mqtt_params.clean_session = 0; */
/**
*
* MQTT keepAlive interval
*
* KeepAlive is the maximum time interval that is permitted to elapse between the point at which
* the Client finishes transmitting one Control Packet and the point it starts sending the next.
*
* default value is 60000.
*/
/* mqtt_params.keepalive_interval_ms = 60000; */
/**
*
* MQTT write buffer size
*
* Write buffer is allocated to place upstream MQTT messages, MQTT client will be limitted
* to send packet no longer than this to Cloud
*
* default value is 1024.
*
*/
/* mqtt_params.write_buf_size = 1024; */
/**
*
* MQTT read buffer size
*
* Write buffer is allocated to place downstream MQTT messages, MQTT client will be limitted
* to recv packet no longer than this from Cloud
*
* default value is 1024.
*
*/
/* mqtt_params.read_buf_size = 1024; */
/**
*
* MQTT event callback function
*
* Event callback function will be called by SDK when it want to notify user what is happening inside itself
*
* default value is NULL, which means PUB/SUB event won't be exposed.
*
*/
mqtt_params.handle_event.h_fp = example_event_handle;
pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient) {
EXAMPLE_TRACE("MQTT construct failed");
return -1;
}
res = example_subscribe(pclient);
if (res < 0) {
IOT_MQTT_Destroy(&pclient);
return -1;
}
while (1) {
if (0 == loop_cnt % 20) {
example_publish(pclient);
}
IOT_MQTT_Yield(pclient, 200);
loop_cnt += 1;
}
return 0;
}

View File

@@ -0,0 +1,212 @@
#include <stdio.h>
#include <string.h>
#include "dev_sign_api.h"
#include "mqtt_api.h"
#ifdef ATM_ENABLED
#include "at_api.h"
#endif
static char g_topic_name[CONFIG_MQTT_TOPIC_MAXLEN];
void HAL_Printf(const char *fmt, ...);
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN]);
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN]);
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
uint64_t HAL_UptimeMs(void);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
#define EXAMPLE_TRACE(fmt, ...) \
do { \
HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
HAL_Printf(fmt, ##__VA_ARGS__); \
HAL_Printf("%s", "\r\n"); \
} while(0)
void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt) msg->msg;
switch (msg->event_type) {
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
/* print topic name and topic message */
HAL_Printf("Message Arrived: \n");
HAL_Printf("Topic : %.*s\n", topic_info->topic_len, topic_info->ptopic);
HAL_Printf("Payload: %.*s\n", topic_info->payload_len, topic_info->payload);
HAL_Printf("\n");
break;
default:
break;
}
}
int example_subscribe(void *handle)
{
int res = 0;
char product_key[IOTX_PRODUCT_KEY_LEN] = {0};
char device_name[IOTX_DEVICE_NAME_LEN] = {0};
const char *fmt = "/%s/%s/user/get";
char *topic = NULL;
int topic_len = 0;
HAL_GetProductKey(product_key);
HAL_GetDeviceName(device_name);
topic_len = strlen(fmt) + strlen(product_key) + strlen(device_name) + 1;
if (topic_len > CONFIG_MQTT_TOPIC_MAXLEN) {
HAL_Printf("topic too long\n");
return -1;
}
topic = g_topic_name;
memset(topic, 0, CONFIG_MQTT_TOPIC_MAXLEN);
HAL_Snprintf(topic, topic_len, fmt, product_key, device_name);
res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
if (res < 0) {
HAL_Printf("subscribe failed\n");
return -1;
}
return 0;
}
int example_publish(void *handle)
{
int res = 0;
iotx_mqtt_topic_info_t topic_msg;
char product_key[IOTX_PRODUCT_KEY_LEN] = {0};
char device_name[IOTX_DEVICE_NAME_LEN] = {0};
const char *fmt = "/%s/%s/user/get";
char *topic = NULL;
int topic_len = 0;
char *payload = "hello,world";
HAL_GetProductKey(product_key);
HAL_GetDeviceName(device_name);
topic_len = strlen(fmt) + strlen(product_key) + strlen(device_name) + 1;
if (topic_len > CONFIG_MQTT_TOPIC_MAXLEN) {
HAL_Printf("topic too long\n");
return -1;
}
topic = g_topic_name;
memset(topic, 0, CONFIG_MQTT_TOPIC_MAXLEN);
HAL_Snprintf(topic, topic_len, fmt, product_key, device_name);
memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
topic_msg.qos = IOTX_MQTT_QOS0;
topic_msg.retain = 0;
topic_msg.dup = 0;
topic_msg.payload = (void *)payload;
topic_msg.payload_len = strlen(payload);
res = IOT_MQTT_Publish(handle, topic, &topic_msg);
if (res < 0) {
HAL_Printf("publish failed\n");
return -1;
}
return 0;
}
void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
HAL_Printf("msg->event_type : %d\n", msg->event_type);
}
/*
* NOTE: About demo topic of /${productKey}/${deviceName}/user/get
*
* The demo device has been configured in IoT console (https://iot.console.aliyun.com)
* so that its /${productKey}/${deviceName}/user/get can both be subscribed and published
*
* We design this to completely demostrate publish & subscribe process, in this way
* MQTT client can receive original packet sent by itself
*
* For new devices created by yourself, pub/sub privilege also required to be granted
* to its /${productKey}/${deviceName}/user/get to run whole example
*/
int main(int argc, char *argv[])
{
void *pclient = NULL;
int res = 0;
int loop_cnt = 0;
iotx_mqtt_region_types_t region = IOTX_CLOUD_REGION_SHANGHAI;
iotx_sign_mqtt_t sign_mqtt;
iotx_dev_meta_info_t meta;
iotx_mqtt_param_t mqtt_params;
#ifdef ATM_ENABLED
if (IOT_ATM_Init() < 0) {
HAL_Printf("IOT ATM init failed!\n");
return -1;
}
#endif
HAL_Printf("mqtt example\n");
memset(&meta, 0, sizeof(iotx_dev_meta_info_t));
HAL_GetProductKey(meta.product_key);
HAL_GetDeviceName(meta.device_name);
HAL_GetDeviceSecret(meta.device_secret);
memset(&sign_mqtt, 0x0, sizeof(iotx_sign_mqtt_t));
if (IOT_Sign_MQTT(region, &meta, &sign_mqtt) < 0) {
return -1;
}
#if 0 /* Uncomment this to show more information */
HAL_Printf("sign_mqtt.hostname: %s\n", sign_mqtt.hostname);
HAL_Printf("sign_mqtt.port : %d\n", sign_mqtt.port);
HAL_Printf("sign_mqtt.username: %s\n", sign_mqtt.username);
HAL_Printf("sign_mqtt.password: %s\n", sign_mqtt.password);
HAL_Printf("sign_mqtt.clientid: %s\n", sign_mqtt.clientid);
#endif
/* Initialize MQTT parameter */
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.port = sign_mqtt.port;
mqtt_params.host = sign_mqtt.hostname;
mqtt_params.client_id = sign_mqtt.clientid;
mqtt_params.username = sign_mqtt.username;
mqtt_params.password = sign_mqtt.password;
mqtt_params.request_timeout_ms = 2000;
mqtt_params.clean_session = 0;
mqtt_params.keepalive_interval_ms = 60000;
mqtt_params.read_buf_size = 1024;
mqtt_params.write_buf_size = 1024;
mqtt_params.handle_event.h_fp = example_event_handle;
mqtt_params.handle_event.pcontext = NULL;
pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient) {
EXAMPLE_TRACE("MQTT construct failed");
return -1;
}
res = example_subscribe(pclient);
if (res < 0) {
IOT_MQTT_Destroy(&pclient);
return -1;
}
while (1) {
if (0 == loop_cnt % 20) {
example_publish(pclient);
}
IOT_MQTT_Yield(pclient, 200);
loop_cnt += 1;
}
return 0;
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef MQTTCONNECT_H_
#define MQTTCONNECT_H_
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
#define MQTT_CONN_FLAG_USER_NAME (0x80)
#define MQTT_CONN_FLAG_PASSWORD (0x40)
#define MQTT_CONN_FLAG_WILL_RETAIN (0x20)
#define MQTT_CONN_FLAG_WILL_QOS (0x18)
#define MQTT_CONN_FLAG_WILL_FLAG (0x04)
#define MQTT_CONN_FLAG_CLEAN_SESSION (0x02)
typedef union {
unsigned char all; /**< all connect flags */
} MQTTConnectFlags; /**< connect flags byte */
/**
* Defines the MQTT "Last Will and Testament" (LWT) settings for
* the connect packet.
*/
typedef struct {
/** The eyecatcher for this structure. must be MQTW. */
char struct_id[4];
/** The version number of this structure. Must be 0 */
int struct_version;
/** The LWT topic to which the LWT message will be published. */
MQTTString topicName;
/** The LWT payload. */
MQTTString message;
/**
* The retained flag for the LWT message (see MQTTAsync_message.retained).
*/
unsigned char retained;
/**
* The quality of service setting for the LWT message (see
* MQTTAsync_message.qos and @ref qos).
*/
char qos;
} MQTTPacket_willOptions;
#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
typedef struct {
/** The eyecatcher for this structure. must be MQTC. */
char struct_id[4];
/** The version number of this structure. Must be 0 */
int struct_version;
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
*/
unsigned char MQTTVersion;
MQTTString clientID;
unsigned short keepAliveInterval; /* 单位s */
unsigned char cleansession;
unsigned char willFlag;
MQTTPacket_willOptions will;
MQTTString username;
MQTTString password;
} MQTTPacket_connectData;
typedef union {
unsigned char all; /**< all connack flags */
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
struct {
unsigned int sessionpresent : 1; /**< session present flag */
unsigned int : 7; /**< unused */
} bits;
#else
struct {
unsigned int : 7; /**< unused */
unsigned int sessionpresent : 1; /**< session present flag */
} bits;
#endif
} MQTTConnackFlags; /**< connack flags byte */
#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN, 1, 0, \
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
DLLExport int MQTTSerialize_connect(unsigned char *buf, int buflen, MQTTPacket_connectData *options);
DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData *data, unsigned char *buf, int len);
DLLExport int MQTTSerialize_connack(unsigned char *buf, int buflen, unsigned char connack_rc,
unsigned char sessionPresent);
DLLExport int MQTTDeserialize_connack(unsigned char *sessionPresent, unsigned char *connack_rc, unsigned char *buf,
int buflen);
DLLExport int MQTTSerialize_disconnect(unsigned char *buf, int buflen);
DLLExport int MQTTSerialize_pingreq(unsigned char *buf, int buflen);
#endif /* MQTTCONNECT_H_ */

View File

@@ -0,0 +1,200 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "MQTTPacket.h"
#include <string.h>
/**
* Determines the length of the MQTT connect packet that would be produced using the supplied connect options.
* @param options the options to be used to build the connect packet
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_connectLength(MQTTPacket_connectData *options)
{
int len = 0;
if (options->MQTTVersion == 3) {
len = 12; /* variable depending on MQTT or MQIsdp */
} else if (options->MQTTVersion == 4) {
len = 10;
}
len += MQTTstrlen(options->clientID) + 2;
if (options->willFlag) {
len += MQTTstrlen(options->will.topicName) + 2 + MQTTstrlen(options->will.message) + 2;
}
if (options->username.cstring || options->username.lenstring.data) {
len += MQTTstrlen(options->username) + 2;
}
if (options->password.cstring || options->password.lenstring.data) {
len += MQTTstrlen(options->password) + 2;
}
return len;
}
/**
* Serializes the connect options into the buffer.
* @param buf the buffer into which the packet will be serialized
* @param len the length in bytes of the supplied buffer
* @param options the options to be used to build the connect packet
* @return serialized length, or error if 0
*/
int MQTTSerialize_connect(unsigned char *buf, int buflen, MQTTPacket_connectData *options)
{
unsigned char *ptr = buf;
MQTTHeader header = {0};
MQTTConnectFlags flags = {0};
int len = 0;
int rc = -1;
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) {
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
MQTT_HEADER_SET_TYPE(header.byte, CONNECT);
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
if (options->MQTTVersion == 4) {
writeCString(&ptr, "MQTT");
writeChar(&ptr, (char) 4);
} else {
writeCString(&ptr, "MQIsdp");
writeChar(&ptr, (char) 3);
}
flags.all = 0;
flags.all |= (options->cleansession) ? MQTT_CONN_FLAG_CLEAN_SESSION : 0;
flags.all |= (options->willFlag) ? MQTT_CONN_FLAG_WILL_FLAG : 0;
if (flags.all & MQTT_CONN_FLAG_WILL_FLAG) {
flags.all |= ((options->will.qos & 0x03) << 3);
flags.all |= (options->will.retained) ? MQTT_CONN_FLAG_WILL_RETAIN : 0;
}
if (options->username.cstring || options->username.lenstring.data) {
flags.all |= MQTT_CONN_FLAG_USER_NAME;
}
if (options->password.cstring || options->password.lenstring.data) {
flags.all |= MQTT_CONN_FLAG_PASSWORD;
}
writeChar(&ptr, flags.all);
writeInt(&ptr, options->keepAliveInterval);
writeMQTTString(&ptr, options->clientID);
if (options->willFlag) {
writeMQTTString(&ptr, options->will.topicName);
writeMQTTString(&ptr, options->will.message);
}
if (flags.all & MQTT_CONN_FLAG_USER_NAME) {
writeMQTTString(&ptr, options->username);
}
if (flags.all & MQTT_CONN_FLAG_PASSWORD) {
writeMQTTString(&ptr, options->password);
}
rc = ptr - buf;
exit:
return rc;
}
/**
* Deserializes the supplied (wire) buffer into connack data - return code
* @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
* @param connack_rc returned integer value of the connack return code
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param len the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_connack(unsigned char *sessionPresent, unsigned char *connack_rc, unsigned char *buf, int buflen)
{
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
int rc = 0;
int mylen;
MQTTConnackFlags flags = {0};
header.byte = readChar(&curdata);
if (MQTT_HEADER_GET_TYPE(header.byte) != CONNACK) {
goto exit;
}
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (enddata - curdata < 2) {
goto exit;
}
flags.all = readChar(&curdata);
*sessionPresent = flags.bits.sessionpresent;
*connack_rc = readChar(&curdata);
rc = 1;
exit:
return rc;
}
/**
* Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
* @param packettype the message type
* @return serialized length, or error if 0
*/
int MQTTSerialize_zero(unsigned char *buf, int buflen, unsigned char packettype)
{
MQTTHeader header = {0};
int rc = -1;
unsigned char *ptr = buf;
if (buflen < 2) {
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
MQTT_HEADER_SET_TYPE(header.byte, packettype);
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
rc = ptr - buf;
exit:
return rc;
}
/**
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
* @return serialized length, or error if 0
*/
int MQTTSerialize_disconnect(unsigned char *buf, int buflen)
{
return MQTTSerialize_zero(buf, buflen, DISCONNECT);
}
/**
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
* @return serialized length, or error if 0
*/
int MQTTSerialize_pingreq(unsigned char *buf, int buflen)
{
return MQTTSerialize_zero(buf, buflen, PINGREQ);
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "MQTTPacket.h"
#include <string.h>
#define min(a, b) ((a < b) ? 1 : 0)
/**
* Deserializes the supplied (wire) buffer into publish data
* @param dup returned integer - the MQTT dup flag
* @param qos returned integer - the MQTT QoS value
* @param retained returned integer - the MQTT retained flag
* @param packetid returned integer - the MQTT packet identifier
* @param topicName returned MQTTString - the MQTT topic in the publish
* @param payload returned byte buffer - the MQTT publish payload
* @param payloadlen returned integer - the length of the MQTT payload
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success
*/
int MQTTDeserialize_publish(unsigned char *dup, int *qos, unsigned char *retained, unsigned short *packetid,
MQTTString *topicName,
unsigned char **payload, int *payloadlen, unsigned char *buf, int buflen)
{
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
int rc = 0;
int mylen = 0;
header.byte = readChar(&curdata);
if (MQTT_HEADER_GET_TYPE(header.byte) != PUBLISH) {
goto exit;
}
*dup = MQTT_HEADER_GET_DUP(header.byte);
*qos = MQTT_HEADER_GET_QOS(header.byte);
*retained = MQTT_HEADER_GET_RETAIN(header.byte);
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (!readMQTTLenString(topicName, &curdata, enddata) ||
enddata - curdata < 0) { /* do we have enough data to read the protocol version byte? */
goto exit;
}
if (*qos > 0) {
*packetid = readInt(&curdata);
}
*payloadlen = enddata - curdata;
*payload = curdata;
rc = 1;
exit:
return rc;
}
/**
* Deserializes the supplied (wire) buffer into an ack
* @param packettype returned integer - the MQTT packet type
* @param dup returned integer - the MQTT dup flag
* @param packetid returned integer - the MQTT packet identifier
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup, unsigned short *packetid, unsigned char *buf,
int buflen)
{
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
int rc = 0;
int mylen;
header.byte = readChar(&curdata);
*dup = MQTT_HEADER_GET_DUP(header.byte);
*packettype = MQTT_HEADER_GET_TYPE(header.byte);
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (enddata - curdata < 2) {
goto exit;
}
*packetid = readInt(&curdata);
rc = 1;
exit:
return rc;
}

View File

@@ -0,0 +1,263 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "MQTTPacket.h"
#include <string.h>
/**
* Encodes the message length according to the MQTT algorithm
* @param buf the buffer into which the encoded data is written
* @param length the length to be encoded
* @return the number of bytes written to buffer
*/
int MQTTPacket_encode(unsigned char *buf, int length)
{
int rc = 0;
do {
char d = length % 128;
length /= 128;
/* if there are more digits to encode, set the top bit of this digit */
if (length > 0) {
d |= 0x80;
}
buf[rc++] = d;
} while (length > 0);
return rc;
}
/**
* Decodes the message length according to the MQTT algorithm
* @param getcharfn pointer to function to read the next character from the data source
* @param value the decoded length returned
* @return the number of bytes read from the socket
*/
int MQTTPacket_decode(int (*getcharfn)(unsigned char *, int), int *value)
{
unsigned char c;
int multiplier = 1;
int len = 0;
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
*value = 0;
do {
int rc = MQTTPACKET_READ_ERROR;
if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES) {
rc = MQTTPACKET_READ_ERROR; /* bad data */
goto exit;
}
rc = (*getcharfn)(&c, 1);
if (rc != 1) {
goto exit;
}
*value += (c & 127) * multiplier;
multiplier *= 128;
} while ((c & 128) != 0);
exit:
return len;
}
int MQTTPacket_len(int rem_len)
{
rem_len += 1; /* header byte */
/* now remaining_length field */
if (rem_len < 128) {
rem_len += 1;
} else if (rem_len < 16384) {
rem_len += 2;
} else if (rem_len < 2097151) {
rem_len += 3;
} else {
rem_len += 4;
}
return rem_len;
}
static unsigned char *bufptr;
int bufchar(unsigned char *c, int count)
{
int i;
for (i = 0; i < count; ++i) {
*c = *bufptr++;
}
return count;
}
int MQTTPacket_decodeBuf(unsigned char *buf, int *value)
{
bufptr = buf;
return MQTTPacket_decode(bufchar, value);
}
/**
* Calculates an integer from two bytes read from the input buffer
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
* @return the integer value calculated
*/
int readInt(unsigned char **pptr)
{
unsigned char *ptr = *pptr;
int len = 256 * (*ptr) + (*(ptr + 1));
*pptr += 2;
return len;
}
/**
* Reads one character from the input buffer.
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
* @return the character read
*/
char readChar(unsigned char **pptr)
{
char c = **pptr;
(*pptr)++;
return c;
}
/**
* Writes one character to an output buffer.
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param c the character to write
*/
void writeChar(unsigned char **pptr, char c)
{
**pptr = c;
(*pptr)++;
}
/**
* Writes an integer as 2 bytes to an output buffer.
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param anInt the integer to write
*/
void writeInt(unsigned char **pptr, int anInt)
{
**pptr = (unsigned char)(anInt / 256);
(*pptr)++;
**pptr = (unsigned char)(anInt % 256);
(*pptr)++;
}
/**
* Writes a "UTF" string to an output buffer. Converts C string to length-delimited.
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param string the C string to write
*/
void writeCString(unsigned char **pptr, const char *string)
{
int len = strlen(string);
writeInt(pptr, len);
memcpy(*pptr, string, len);
*pptr += len;
}
int getLenStringLen(char *ptr)
{
int len = 256 * ((unsigned char)(*ptr)) + (unsigned char)(*(ptr + 1));
return len;
}
void writeMQTTString(unsigned char **pptr, MQTTString mqttstring)
{
if (mqttstring.lenstring.len > 0) {
writeInt(pptr, mqttstring.lenstring.len);
memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len);
*pptr += mqttstring.lenstring.len;
} else if (mqttstring.cstring) {
writeCString(pptr, mqttstring.cstring);
} else {
writeInt(pptr, 0);
}
}
/**
* @param mqttstring the MQTTString structure into which the data is to be read
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param enddata pointer to the end of the data: do not read beyond
* @return 1 if successful, 0 if not
*/
int readMQTTLenString(MQTTString *mqttstring, unsigned char **pptr, unsigned char *enddata)
{
int rc = 0;
/* the first two bytes are the length of the string */
if (enddata - (*pptr) > 1) { /* enough length to read the integer? */
mqttstring->lenstring.len = readInt(pptr); /* increments pptr to point past length */
if (&(*pptr)[mqttstring->lenstring.len] <= enddata) {
mqttstring->lenstring.data = (char *)*pptr;
*pptr += mqttstring->lenstring.len;
rc = 1;
}
}
mqttstring->cstring = NULL;
return rc;
}
/**
* Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string
* @param mqttstring the string to return the length of
* @return the length of the string
*/
int MQTTstrlen(MQTTString mqttstring)
{
int rc = 0;
if (mqttstring.cstring) {
rc = strlen(mqttstring.cstring);
} else {
rc = mqttstring.lenstring.len;
}
return rc;
}
/**
* Compares an MQTTString to a C string
* @param a the MQTTString to compare
* @param bptr the C string to compare
* @return int - equal or not
*/
int MQTTPacket_equals(MQTTString *a, char *bptr)
{
int alen = 0,
blen = 0;
char *aptr;
#if !(WITH_MQTT_ZIP_TOPIC)
if (a->cstring) {
aptr = a->cstring;
alen = strlen(a->cstring);
} else {
aptr = a->lenstring.data;
alen = a->lenstring.len;
}
blen = strlen(bptr);
#else
aptr = a->lenstring.data;
alen = a->lenstring.len;
blen = alen;
#endif
return (alen == blen) && (memcmp(aptr, bptr, alen) == 0);
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef MQTTPACKET_H_
#define MQTTPACKET_H_
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#if defined(WIN32_DLL) || defined(WIN64_DLL)
#define DLLImport __declspec(dllimport)
#define DLLExport __declspec(dllexport)
#elif defined(LINUX_SO)
#define DLLImport extern
#define DLLExport __attribute__ ((visibility ("default")))
#else
#define DLLImport
#define DLLExport
#endif
enum errors {
MQTTPACKET_BUFFER_TOO_SHORT = -2,
MQTTPACKET_READ_ERROR = -1,
MQTTPACKET_READ_COMPLETE
};
/* CPT, control packet type */
enum msgTypes {
MQTT_CPT_RESERVED = 0, CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
PINGREQ, PINGRESP, DISCONNECT
};
#define MQTT_HEADER_BIT_MASK_TYPE (0xF0)
#define MQTT_HEADER_BIT_MASK_DUP (0x08)
#define MQTT_HEADER_BIT_MASK_QOS (0x06)
#define MQTT_HEADER_BIT_MASK_RETAIN (0x01)
#define MQTT_HEADER_GET_TYPE(head) ((head & 0xF0) >> 4)
#define MQTT_HEADER_GET_DUP(head) ((head & 0x08) >> 3)
#define MQTT_HEADER_GET_QOS(head) ((head & 0x06) >> 1)
#define MQTT_HEADER_GET_RETAIN(head) (head & 0x01)
#define MQTT_HEADER_SET_TYPE(head, type) do {head |= ((type << 4) & 0xF0); } while (0)
#define MQTT_HEADER_SET_DUP(head, dup) do {head |= ((dup << 3) & 0x08); } while (0)
#define MQTT_HEADER_SET_QOS(head, qos) do {head |= ((qos << 1) & 0x06); } while (0)
#define MQTT_HEADER_SET_RETAIN(head, retain) do {head |= (retain & 0x01); } while (0)
/**
* Bitfields for the MQTT header byte.
*/
typedef union
{
unsigned char byte; /**< the whole byte */
} MQTTHeader;
typedef struct {
int len;
char *data;
} MQTTLenString;
typedef struct {
char *cstring;
MQTTLenString lenstring;
} MQTTString;
#define MQTTString_initializer {NULL, {0, NULL}}
int MQTTstrlen(MQTTString mqttstring);
#include "MQTTConnect.h"
#include "MQTTPublish.h"
#include "MQTTSubscribe.h"
#include "MQTTUnsubscribe.h"
int MQTTSerialize_ack(unsigned char *buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
int MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup, unsigned short *packetid, unsigned char *buf,
int buflen);
int MQTTPacket_len(int rem_len);
int MQTTPacket_equals(MQTTString *a, char *b);
int MQTTPacket_encode(unsigned char *buf, int length);
int MQTTPacket_decode(int (*getcharfn)(unsigned char *, int), int *value);
int MQTTPacket_decodeBuf(unsigned char *buf, int *value);
int readInt(unsigned char **pptr);
char readChar(unsigned char **pptr);
void writeChar(unsigned char **pptr, char c);
void writeInt(unsigned char **pptr, int anInt);
int readMQTTLenString(MQTTString *mqttstring, unsigned char **pptr, unsigned char *enddata);
void writeCString(unsigned char **pptr, const char *string);
void writeMQTTString(unsigned char **pptr, MQTTString mqttstring);
typedef struct {
int (*getfn)(void *, unsigned char *,
int); /* must return -1 for error, 0 for call again, or the number of bytes read */
void *sck; /* pointer to whatever the system may use to identify the transport */
int multiplier;
int rem_len;
int len;
char state;
} MQTTTransport;
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
}
#endif
#endif /* MQTTPACKET_H_ */

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef MQTTPUBLISH_H_
#define MQTTPUBLISH_H_
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
DLLExport int MQTTSerialize_publish(unsigned char *buf, int buflen, unsigned char dup, int qos, unsigned char retained,
unsigned short packetid,
MQTTString topicName, unsigned char *payload, int payloadlen);
DLLExport int MQTTDeserialize_publish(unsigned char *dup, int *qos, unsigned char *retained, unsigned short *packetid,
MQTTString *topicName,
unsigned char **payload, int *payloadlen, unsigned char *buf, int len);
/* DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid); */
DLLExport int MQTTSerialize_pubrel(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid);
DLLExport int MQTTSerialize_pubcomp(unsigned char *buf, int buflen, unsigned short packetid);
#endif /* MQTTPUBLISH_H_ */

View File

@@ -0,0 +1,155 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "MQTTPacket.h"
#include <string.h>
/**
* Determines the length of the MQTT publish packet that would be produced using the supplied parameters
* @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
* @param topicName the topic name to be used in the publish
* @param payloadlen the length of the payload to be sent
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
{
int len = 0;
len += 2 + MQTTstrlen(topicName) + payloadlen;
if (qos > 0) {
len += 2; /* packetid */
}
return len;
}
/**
* Serializes the supplied publish data into the supplied buffer, ready for sending
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param dup integer - the MQTT dup flag
* @param qos integer - the MQTT QoS value
* @param retained integer - the MQTT retained flag
* @param packetid integer - the MQTT packet identifier
* @param topicName MQTTString - the MQTT topic in the publish
* @param payload byte buffer - the MQTT publish payload
* @param payloadlen integer - the length of the MQTT payload
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_publish(unsigned char *buf, int buflen, unsigned char dup, int qos, unsigned char retained,
unsigned short packetid,
MQTTString topicName, unsigned char *payload, int payloadlen)
{
unsigned char *ptr = buf;
MQTTHeader header = {0};
int rem_len = 0;
int rc = 0;
if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen) {
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
MQTT_HEADER_SET_TYPE(header.byte, PUBLISH);
MQTT_HEADER_SET_DUP(header.byte, dup);
MQTT_HEADER_SET_QOS(header.byte, qos);
MQTT_HEADER_SET_RETAIN(header.byte, retained);
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
writeMQTTString(&ptr, topicName);
if (qos > 0) {
writeInt(&ptr, packetid);
}
memcpy(ptr, payload, payloadlen);
ptr += payloadlen;
rc = ptr - buf;
exit:
return rc;
}
/**
* Serializes the ack packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param type the MQTT packet type
* @param dup the MQTT dup flag
* @param packetid the MQTT packet identifier
* @return serialized length, or error if 0
*/
int MQTTSerialize_ack(unsigned char *buf, int buflen, unsigned char packettype, unsigned char dup,
unsigned short packetid)
{
MQTTHeader header = {0};
int rc = 0;
unsigned char *ptr = buf;
if (buflen < 4) {
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
MQTT_HEADER_SET_TYPE(header.byte, packettype);
MQTT_HEADER_SET_DUP(header.byte, dup);
MQTT_HEADER_SET_QOS(header.byte, ((packettype == PUBREL) ? 1 : 0));
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
writeInt(&ptr, packetid);
rc = ptr - buf;
exit:
return rc;
}
/**
* Serializes a puback packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param packetid integer - the MQTT packet identifier
* @return serialized length, or error if 0
*/
/* int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) */
/* { */
/* return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid); */
/* } */
#if WITH_MQTT_QOS2_PACKET
/**
* Serializes a pubrel packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param dup integer - the MQTT dup flag
* @param packetid integer - the MQTT packet identifier
* @return serialized length, or error if 0
*/
int MQTTSerialize_pubrel(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid)
{
return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
}
/**
* Serializes a pubrel packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param packetid integer - the MQTT packet identifier
* @return serialized length, or error if 0
*/
int MQTTSerialize_pubcomp(unsigned char *buf, int buflen, unsigned short packetid)
{
return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
}
#endif /* #if WITH_MQTT_QOS2_PACKET */

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef MQTTSUBSCRIBE_H_
#define MQTTSUBSCRIBE_H_
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
DLLExport int MQTTSerialize_subscribe(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[], int requestedQoSs[]);
DLLExport int MQTTDeserialize_subscribe(unsigned char *dup, unsigned short *packetid,
int maxcount, int *count, MQTTString topicFilters[], int requestedQoSs[], unsigned char *buf, int len);
DLLExport int MQTTSerialize_suback(unsigned char *buf, int buflen, unsigned short packetid, int count,
int *grantedQoSs);
DLLExport int MQTTDeserialize_suback(unsigned short *packetid, int maxcount, int *count, int grantedQoSs[],
unsigned char *buf, int len);
#endif /* MQTTSUBSCRIBE_H_ */

View File

@@ -0,0 +1,122 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "MQTTPacket.h"
#include <string.h>
/**
* Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
* @param count the number of topic filter strings in topicFilters
* @param topicFilters the array of topic filter strings to be used in the publish
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
{
int i;
int len = 2; /* packetid */
for (i = 0; i < count; ++i) {
len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
}
return len;
}
/**
* Serializes the supplied subscribe data into the supplied buffer, ready for sending
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied bufferr
* @param dup integer - the MQTT dup flag
* @param packetid integer - the MQTT packet identifier
* @param count - number of members in the topicFilters and reqQos arrays
* @param topicFilters - array of topic filter names
* @param requestedQoSs - array of requested QoS
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_subscribe(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid, int count,
MQTTString topicFilters[], int requestedQoSs[])
{
unsigned char *ptr = buf;
MQTTHeader header = {0};
int rem_len = 0;
int rc = 0;
int i = 0;
if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) {
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
MQTT_HEADER_SET_TYPE(header.byte, SUBSCRIBE);
MQTT_HEADER_SET_DUP(header.byte, dup);
MQTT_HEADER_SET_QOS(header.byte, 1);
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
writeInt(&ptr, packetid);
for (i = 0; i < count; ++i) {
writeMQTTString(&ptr, topicFilters[i]);
writeChar(&ptr, requestedQoSs[i]);
}
rc = ptr - buf;
exit:
return rc;
}
/**
* Deserializes the supplied (wire) buffer into suback data
* @param packetid returned integer - the MQTT packet identifier
* @param maxcount - the maximum number of members allowed in the grantedQoSs array
* @param count returned integer - number of members in the grantedQoSs array
* @param grantedQoSs returned array of integers - the granted qualities of service
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_suback(unsigned short *packetid, int maxcount, int *count, int grantedQoSs[], unsigned char *buf,
int buflen)
{
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
int rc = 0;
int mylen;
header.byte = readChar(&curdata);
if (MQTT_HEADER_GET_TYPE(header.byte) != SUBACK) {
goto exit;
}
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (enddata - curdata < 2) {
goto exit;
}
*packetid = readInt(&curdata);
*count = 0;
while (curdata < enddata) {
if (*count >= maxcount) {
rc = -1;
goto exit;
}
grantedQoSs[(*count)++] = readChar(&curdata);
}
rc = 1;
exit:
return rc;
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef MQTTUNSUBSCRIBE_H_
#define MQTTUNSUBSCRIBE_H_
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
DLLExport int MQTTSerialize_unsubscribe(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[]);
DLLExport int MQTTDeserialize_unsubscribe(unsigned char *dup, unsigned short *packetid, int max_count, int *count,
MQTTString topicFilters[],
unsigned char *buf, int len);
DLLExport int MQTTSerialize_unsuback(unsigned char *buf, int buflen, unsigned short packetid);
DLLExport int MQTTDeserialize_unsuback(unsigned short *packetid, unsigned char *buf, int len);
#endif /* MQTTUNSUBSCRIBE_H_ */

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "MQTTPacket.h"
#include <string.h>
/**
* Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
* @param count the number of topic filter strings in topicFilters
* @param topicFilters the array of topic filter strings to be used in the publish
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
{
int i;
int len = 2; /* packetid */
for (i = 0; i < count; ++i) {
len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
}
return len;
}
/**
* Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @param dup integer - the MQTT dup flag
* @param packetid integer - the MQTT packet identifier
* @param count - number of members in the topicFilters array
* @param topicFilters - array of topic filter names
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_unsubscribe(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[])
{
unsigned char *ptr = buf;
MQTTHeader header = {0};
int rem_len = 0;
int rc = -1;
int i = 0;
if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) {
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
MQTT_HEADER_SET_TYPE(header.byte, UNSUBSCRIBE);
MQTT_HEADER_SET_DUP(header.byte, dup);
MQTT_HEADER_SET_QOS(header.byte, 1);
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
writeInt(&ptr, packetid);
for (i = 0; i < count; ++i) {
writeMQTTString(&ptr, topicFilters[i]);
}
rc = ptr - buf;
exit:
return rc;
}
/**
* Deserializes the supplied (wire) buffer into unsuback data
* @param packetid returned integer - the MQTT packet identifier
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_unsuback(unsigned short *packetid, unsigned char *buf, int buflen)
{
unsigned char type = 0;
unsigned char dup = 0;
int rc = 0;
rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
if (type == UNSUBACK) {
rc = 1;
}
return rc;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,170 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __IOTX_MQTT_H__
#define __IOTX_MQTT_H__
#include "infra_types.h"
#include "infra_list.h"
#include "infra_timer.h"
#include "iotx_mqtt_config.h"
#include "mqtt_api.h"
#include "MQTTPacket.h"
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#define mqtt_malloc(size) LITE_malloc(size, MEM_MAGIC, "mqtt")
#define mqtt_free(ptr) LITE_free(ptr)
#else
#define mqtt_malloc(size) HAL_Malloc(size)
#define mqtt_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
#endif
#define MQTT_DYNBUF_SEND_MARGIN (64)
#define MQTT_DYNBUF_RECV_MARGIN (8)
typedef enum {
IOTX_MC_CONNECTION_ACCEPTED = 0,
IOTX_MC_CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION = 1,
IOTX_MC_CONNECTION_REFUSED_IDENTIFIER_REJECTED = 2,
IOTX_MC_CONNECTION_REFUSED_SERVER_UNAVAILABLE = 3,
IOTX_MC_CONNECTION_REFUSED_BAD_USERDATA = 4,
IOTX_MC_CONNECTION_REFUSED_NOT_AUTHORIZED = 5
} iotx_mc_connect_ack_code_t;
/* State of MQTT client */
typedef enum {
IOTX_MC_STATE_INVALID = 0, /* MQTT in invalid state */
IOTX_MC_STATE_INITIALIZED = 1, /* MQTT in initializing state */
IOTX_MC_STATE_CONNECTED = 2, /* MQTT in connected state */
IOTX_MC_STATE_DISCONNECTED = 3, /* MQTT in disconnected state */
IOTX_MC_STATE_DISCONNECTED_RECONNECTING = 4, /* MQTT in reconnecting state */
IOTX_MC_STATE_CONNECT_BLOCK = 5 /* MQTT in connecting state when using async protocol stack */
} iotx_mc_state_t;
typedef enum MQTT_NODE_STATE {
IOTX_MC_NODE_STATE_NORMANL = 0,
IOTX_MC_NODE_STATE_INVALID,
} iotx_mc_node_t;
typedef enum {
TOPIC_NAME_TYPE = 0,
TOPIC_FILTER_TYPE
} iotx_mc_topic_type_t;
/* Handle structure of subscribed topic */
typedef struct iotx_mc_topic_handle_s {
iotx_mc_topic_type_t topic_type;
iotx_mqtt_event_handle_t handle;
#ifdef PLATFORM_HAS_DYNMEM
const char *topic_filter;
struct list_head linked_list;
#else
const char topic_filter[CONFIG_MQTT_TOPIC_MAXLEN];
int used;
#endif
} iotx_mc_topic_handle_t;
#if !WITH_MQTT_ONLY_QOS0
/* Information structure of published topic */
typedef struct REPUBLISH_INFO {
iotx_time_t pub_start_time; /* start time of publish request */
iotx_mc_node_t node_state; /* state of this node */
uint16_t msg_id; /* packet id of publish */
uint32_t len; /* length of publish message */
#ifdef PLATFORM_HAS_DYNMEM
unsigned char *buf; /* publish message */
struct list_head linked_list;
#else
unsigned char buf[IOTX_MC_TX_MAX_LEN]; /* publish message */
int used;
#endif
} iotx_mc_pub_info_t, *iotx_mc_pub_info_pt;
#endif
/* Reconnected parameter of MQTT client */
typedef struct {
iotx_time_t reconnect_next_time; /* the next time point of reconnect */
uint32_t reconnect_time_interval_ms; /* time interval of this reconnect */
} iotx_mc_reconnect_param_t;
typedef struct {
uintptr_t packet_id;
uint8_t ack_type;
iotx_mqtt_event_handle_func_fpt sub_state_cb;
#ifdef PLATFORM_HAS_DYNMEM
struct list_head linked_list;
#else
int used;
#endif
} mqtt_sub_sync_node_t;
/* structure of MQTT client */
typedef struct Client {
void *lock_generic; /* generic lock */
uint32_t packet_id; /* packet id */
uint32_t request_timeout_ms; /* request timeout in millisecond */
uint32_t cycle_timeout_ms;
uint32_t buf_size_send; /* send buffer size in byte */
#ifdef PLATFORM_HAS_DYNMEM
#if WITH_MQTT_DYN_BUF
uint32_t buf_size_send_max; /* send buffer size max limit in byte */
uint32_t buf_size_read_max; /* recv buffer size max limit in byte */
#endif
#endif
uint32_t buf_size_read; /* read buffer size in byte */
uint8_t keepalive_probes; /* keepalive probes */
#ifdef PLATFORM_HAS_DYNMEM
char *buf_send; /* pointer of send buffer */
char *buf_read; /* pointer of read buffer */
#else
char buf_send[IOTX_MC_TX_MAX_LEN];
char buf_read[IOTX_MC_RX_MAX_LEN];
#endif
#ifdef PLATFORM_HAS_DYNMEM
struct list_head list_sub_handle; /* list of subscribe handle */
#else
iotx_mc_topic_handle_t list_sub_handle[IOTX_MC_SUBHANDLE_LIST_MAX_LEN];
#endif
utils_network_t ipstack; /* network parameter */
iotx_time_t next_ping_time; /* next ping time */
iotx_mc_state_t client_state; /* state of MQTT client */
iotx_mc_reconnect_param_t reconnect_param; /* reconnect parameter */
MQTTPacket_connectData connect_data; /* connection parameter */
#if !WITH_MQTT_ONLY_QOS0
#ifdef PLATFORM_HAS_DYNMEM
struct list_head list_pub_wait_ack; /* list of wait publish ack */
#else
iotx_mc_pub_info_t list_pub_wait_ack[IOTX_MC_PUBWAIT_LIST_MAX_LEN];
#endif
#endif
#ifdef PLATFORM_HAS_DYNMEM
struct list_head list_sub_sync_ack;
#else
mqtt_sub_sync_node_t list_sub_sync_ack[IOTX_MC_SUBSYNC_LIST_MAX_LEN];
#endif
void *lock_list_pub; /* lock for list of QoS1 pub */
void *lock_write_buf; /* lock of write */
void *lock_read_buf; /* lock of write */
void *lock_yield;
iotx_mqtt_event_handle_t handle_event; /* event handle */
#ifndef PLATFORM_HAS_DYNMEM
int used;
#endif
} iotx_mc_client_t, *iotx_mc_client_pt;
/* Information structure of mutli-subscribe */
typedef struct {
const char *topicFilter;
iotx_mqtt_qos_t qos;
iotx_mqtt_event_handle_func_fpt messageHandler;
} iotx_mutli_sub_info_t, *iotx_mutli_sub_info_pt;
#endif /* __IOTX_MQTT_H__ */

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef IOTX_MQTT_CONFIG_H__
#define IOTX_MQTT_CONFIG_H__
#ifndef WITH_MQTT_DYN_BUF
#define WITH_MQTT_DYN_BUF (1)
#endif
#ifndef WITH_MQTT_QOS2_PACKET
#define WITH_MQTT_QOS2_PACKET (0)
#endif
#ifndef WITH_MQTT_FLOW_CTRL
#define WITH_MQTT_FLOW_CTRL (0)
#endif
#ifndef WITH_MQTT_ONLY_QOS0
#define WITH_MQTT_ONLY_QOS0 (0)
#endif
#ifndef WITH_MQTT_DYN_CONNINFO
#define WITH_MQTT_DYN_CONNINFO (1)
#endif
#ifndef WITH_MQTT_ZIP_TOPIC
#define WITH_MQTT_ZIP_TOPIC (0)
#endif
/* maximum republish elements in list */
#define IOTX_MC_REPUB_NUM_MAX (20)
/* MQTT client version number */
#define IOTX_MC_MQTT_VERSION (4)
/* maximum MQTT packet-id */
#define IOTX_MC_PACKET_ID_MAX (65535)
/* maximum number of simultaneously invoke subscribe request */
#define IOTX_MC_SUB_REQUEST_NUM_MAX (256)
/* Minimum interval of MQTT reconnect in millisecond */
#define IOTX_MC_RECONNECT_INTERVAL_MIN_MS (1000)
/* Maximum interval of MQTT reconnect in millisecond */
#define IOTX_MC_RECONNECT_INTERVAL_MAX_MS (60000)
/* Max times of keepalive which has been send and did not received response package */
#define IOTX_MC_KEEPALIVE_PROBE_MAX (2)
/* Linked List Params When PLATFORM_HAS_DYNMEN Disabled */
#ifndef PLATFORM_HAS_DYNMEN
/* mqtt pub wait list max length, for QoS 1 */
#ifndef IOTX_MC_PUBWAIT_LIST_MAX_LEN
#define IOTX_MC_PUBWAIT_LIST_MAX_LEN (5)
#endif
/* mqtt sub sync list max length */
#ifndef IOTX_MC_SUBSYNC_LIST_MAX_LEN
#define IOTX_MC_SUBSYNC_LIST_MAX_LEN (5)
#endif
/* mqtt sub handle list max length */
#ifndef IOTX_MC_SUBHANDLE_LIST_MAX_LEN
#define IOTX_MC_SUBHANDLE_LIST_MAX_LEN (5)
#endif
/* mqtt client max count */
#ifndef IOTX_MC_CLIENT_MAX_COUNT
#define IOTX_MC_CLIENT_MAX_COUNT (1)
#endif
#ifndef IOTX_MC_TX_MAX_LEN
#define IOTX_MC_TX_MAX_LEN (512)
#endif
#ifndef IOTX_MC_RX_MAX_LEN
#define IOTX_MC_RX_MAX_LEN (512)
#endif
#endif /* PLATFORM_HAS_DYNMEM */
#endif /* IOTX_MQTT_CONFIG_H__ */

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __IOTX_MQTT_INTERNAL_H__
#define __IOTX_MQTT_INTERNAL_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "infra_types.h"
#include "infra_defs.h"
#include "infra_string.h"
#include "infra_list.h"
#include "infra_report.h"
#include "infra_net.h"
#include "infra_sha256.h"
#include "dev_sign_api.h"
#include "mqtt_wrapper.h"
#include "iotx_mqtt_config.h"
#include "iotx_mqtt_client.h"
#include "MQTTPacket.h"
#ifdef INFRA_LOG
#include "infra_log.h"
#define mqtt_emerg(...) log_emerg("MQTT", __VA_ARGS__)
#define mqtt_crit(...) log_crit("MQTT", __VA_ARGS__)
#define mqtt_err(...) log_err("MQTT", __VA_ARGS__)
#define mqtt_warning(...) log_warning("MQTT", __VA_ARGS__)
#define mqtt_info(...) log_info("MQTT", __VA_ARGS__)
#define mqtt_debug(...) log_debug("MQTT", __VA_ARGS__)
#else
#define mqtt_emerg(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_crit(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_warning(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#endif
#endif /* __IOTX_MQTT_INTERNAL_H__ */

View File

@@ -0,0 +1,13 @@
LIBA_TARGET := libiot_mqtt.a
HDR_REFS := src/infra
LIB_SRCS_PATTERN := *.c
SRCS_mqtt-example := examples/mqtt_example.c
SRCS_mqtt-example-at := examples/mqtt_example_at.c
$(call Append_Conditional, LIB_SRCS_PATTERN, impl/*.c, MQTT_DEFAULT_IMPL)
$(call Append_Conditional, TARGET, mqtt-example, MQTT_COMM_ENABLED, ATM_ENABLED BUILD_AOS NO_EXECUTABLES)
$(call Append_Conditional, TARGET, mqtt-example-at, ATM_ENABLED BUILD_AOS NO_EXECUTABLES)
DEPENDS += external_libs/mbedtls
LDFLAGS += -liot_sdk -liot_hal -liot_tls

View File

@@ -0,0 +1,831 @@
#include "infra_types.h"
#include "infra_defs.h"
#include "infra_string.h"
#include "infra_list.h"
#include "infra_report.h"
#include "infra_sha256.h"
#include "infra_compat.h"
#include "dev_sign_api.h"
#include "mqtt_api.h"
#include "mqtt_wrapper.h"
#ifdef PLATFORM_HAS_DYNMEM
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#define mqtt_api_malloc(size) LITE_malloc(size, MEM_MAGIC, "mqtt-api")
#define mqtt_api_free(ptr) LITE_free(ptr)
#else
#define mqtt_api_malloc(size) HAL_Malloc(size)
#define mqtt_api_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
#endif
#else
static iotx_mqtt_param_t g_iotx_mqtt_param;
#endif
#ifdef INFRA_LOG
#include "infra_log.h"
#define mqtt_emerg(...) log_emerg("MQTT", __VA_ARGS__)
#define mqtt_crit(...) log_crit("MQTT", __VA_ARGS__)
#define mqtt_err(...) log_err("MQTT", __VA_ARGS__)
#define mqtt_warning(...) log_warning("MQTT", __VA_ARGS__)
#define mqtt_info(...) log_info("MQTT", __VA_ARGS__)
#define mqtt_debug(...) log_debug("MQTT", __VA_ARGS__)
#else
#define mqtt_emerg(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_crit(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_warning(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define mqtt_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#endif
static void *g_mqtt_client = NULL;
iotx_sign_mqtt_t g_default_sign;
static char iotx_ca_crt_itls[IOTX_PRODUCT_KEY_LEN + IOTX_PRODUCT_SECRET_LEN + 2] = {0};
/* Handle structure of subscribed topic */
typedef struct {
#ifdef PLATFORM_HAS_DYNMEM
char *topic_filter;
#else
char topic_filter[CONFIG_MQTT_TOPIC_MAXLEN];
#endif
iotx_mqtt_event_handle_func_fpt handle;
void *user_data;
iotx_mqtt_qos_t qos;
#ifdef PLATFORM_HAS_DYNMEM
struct list_head linked_list;
#else
int used;
#endif
} iotx_mc_offline_subs_t;
typedef struct {
int init;
void *mutex;
#ifdef PLATFORM_HAS_DYNMEM
struct list_head offline_sub_list;
#else
iotx_mc_offline_subs_t offline_sub_list[CONFIG_MQTT_OFFLINE_TOPIC_MAXNUM];
#endif
} offline_sub_list_t;
static offline_sub_list_t g_mqtt_offline_subs_list = {0};
static int _offline_subs_list_init(void)
{
if (g_mqtt_offline_subs_list.init) {
return SUCCESS_RETURN;
}
memset(&g_mqtt_offline_subs_list, 0, sizeof(offline_sub_list_t));
g_mqtt_offline_subs_list.init = 1;
#ifdef PLATFORM_HAS_DYNMEM
INIT_LIST_HEAD(&g_mqtt_offline_subs_list.offline_sub_list);
#endif
g_mqtt_offline_subs_list.mutex = HAL_MutexCreate();
return SUCCESS_RETURN;
}
static int _offline_subs_list_deinit(void)
{
#ifdef PLATFORM_HAS_DYNMEM
iotx_mc_offline_subs_t *node = NULL, *next_node = NULL;
list_for_each_entry_safe(node, next_node, &g_mqtt_offline_subs_list.offline_sub_list, linked_list,
iotx_mc_offline_subs_t) {
list_del(&node->linked_list);
mqtt_api_free(node->topic_filter);
mqtt_api_free(node);
}
#endif
if (g_mqtt_offline_subs_list.mutex) {
HAL_MutexDestroy(g_mqtt_offline_subs_list.mutex);
}
memset(&g_mqtt_offline_subs_list, 0, sizeof(offline_sub_list_t));
return 0;
}
static int iotx_mqtt_offline_subscribe(const char *topic_filter, iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func, void *pcontext)
{
int ret;
#ifdef PLATFORM_HAS_DYNMEM
iotx_mc_offline_subs_t *sub_info = NULL;
#else
int idx = 0;
#endif
if (topic_filter == NULL || topic_handle_func == NULL) {
return NULL_VALUE_ERROR;
}
_offline_subs_list_init();
#ifdef PLATFORM_HAS_DYNMEM
HAL_MutexLock(g_mqtt_offline_subs_list.mutex);
list_for_each_entry(sub_info, &g_mqtt_offline_subs_list.offline_sub_list, linked_list, iotx_mc_offline_subs_t) {
if ((strlen(sub_info->topic_filter) == strlen(topic_filter)) &&
memcmp(sub_info->topic_filter, topic_filter, strlen(topic_filter)) == 0) {
sub_info->qos = qos;
sub_info->handle = topic_handle_func;
sub_info->user_data = pcontext;
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
return SUCCESS_RETURN;
}
}
sub_info = mqtt_api_malloc(sizeof(iotx_mc_offline_subs_t));
if (sub_info == NULL) {
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
return ERROR_MALLOC;
}
memset(sub_info, 0, sizeof(iotx_mc_offline_subs_t));
sub_info->topic_filter = mqtt_api_malloc(strlen(topic_filter) + 1);
if (sub_info->topic_filter == NULL) {
mqtt_api_free(sub_info);
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
return ERROR_MALLOC;
}
memset(sub_info->topic_filter, 0, strlen(topic_filter) + 1);
strncpy(sub_info->topic_filter, topic_filter, strlen(topic_filter));
sub_info->qos = qos;
sub_info->handle = topic_handle_func;
sub_info->user_data = pcontext;
INIT_LIST_HEAD(&sub_info->linked_list);
list_add_tail(&sub_info->linked_list, &g_mqtt_offline_subs_list.offline_sub_list);
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
ret = SUCCESS_RETURN;
#else
if (strlen(topic_filter) >= CONFIG_MQTT_TOPIC_MAXLEN) {
return MQTT_TOPIC_LEN_TOO_SHORT;
}
HAL_MutexLock(g_mqtt_offline_subs_list.mutex);
for (idx = 0; idx < CONFIG_MQTT_OFFLINE_TOPIC_MAXNUM; idx++) {
if (g_mqtt_offline_subs_list.offline_sub_list[idx].used &&
(strlen(g_mqtt_offline_subs_list.offline_sub_list[idx].topic_filter) == strlen(topic_filter)) &&
memcmp(g_mqtt_offline_subs_list.offline_sub_list[idx].topic_filter, topic_filter, strlen(topic_filter)) == 0) {
g_mqtt_offline_subs_list.offline_sub_list[idx].qos = qos;
g_mqtt_offline_subs_list.offline_sub_list[idx].handle = topic_handle_func;
g_mqtt_offline_subs_list.offline_sub_list[idx].user_data = pcontext;
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
return SUCCESS_RETURN;
}
}
for (idx = 0; idx < CONFIG_MQTT_OFFLINE_TOPIC_MAXNUM; idx++) {
if (g_mqtt_offline_subs_list.offline_sub_list[idx].used == 0) {
memset(&g_mqtt_offline_subs_list.offline_sub_list[idx], 0, sizeof(iotx_mc_offline_subs_t));
memcpy(g_mqtt_offline_subs_list.offline_sub_list[idx].topic_filter, topic_filter, strlen(topic_filter));
g_mqtt_offline_subs_list.offline_sub_list[idx].qos = qos;
g_mqtt_offline_subs_list.offline_sub_list[idx].handle = topic_handle_func;
g_mqtt_offline_subs_list.offline_sub_list[idx].user_data = pcontext;
g_mqtt_offline_subs_list.offline_sub_list[idx].used = 1;
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
return SUCCESS_RETURN;
}
}
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
ret = MQTT_OFFLINE_LIST_LEN_TOO_SHORT;
#endif
return ret;
}
static int iotx_mqtt_deal_offline_subs(void *client)
{
#ifdef PLATFORM_HAS_DYNMEM
iotx_mc_offline_subs_t *node = NULL, *next_node = NULL;
#else
int idx;
#endif
if (g_mqtt_offline_subs_list.init == 0) {
return SUCCESS_RETURN;
}
HAL_MutexLock(g_mqtt_offline_subs_list.mutex);
#ifdef PLATFORM_HAS_DYNMEM
list_for_each_entry_safe(node, next_node, &g_mqtt_offline_subs_list.offline_sub_list, linked_list,
iotx_mc_offline_subs_t) {
list_del(&node->linked_list);
wrapper_mqtt_subscribe(client, node->topic_filter, node->qos, node->handle, node->user_data);
mqtt_api_free(node->topic_filter);
mqtt_api_free(node);
}
#else
for (idx = 0; idx < CONFIG_MQTT_OFFLINE_TOPIC_MAXNUM; idx++) {
if (g_mqtt_offline_subs_list.offline_sub_list[idx].used) {
wrapper_mqtt_subscribe(client, g_mqtt_offline_subs_list.offline_sub_list[idx].topic_filter,
g_mqtt_offline_subs_list.offline_sub_list[idx].qos,
g_mqtt_offline_subs_list.offline_sub_list[idx].handle,
g_mqtt_offline_subs_list.offline_sub_list[idx].user_data);
g_mqtt_offline_subs_list.offline_sub_list[idx].used = 0;
}
}
#endif
HAL_MutexUnlock(g_mqtt_offline_subs_list.mutex);
_offline_subs_list_deinit();
return SUCCESS_RETURN;
}
static void iotx_mqtt_report_funcs(void *pclient)
{
int err;
iotx_mqtt_deal_offline_subs(pclient);
#ifndef ATHOST_MQTT_REPORT_DISBALED
iotx_set_report_func(IOT_MQTT_Publish_Simple);
/* report module id */
err = iotx_report_mid(pclient);
if (SUCCESS_RETURN != err) {
#ifdef DEBUG_REPORT_MID_DEVINFO_FIRMWARE
mqtt_err("failed to report mid");
#endif
}
/* report device info */
err = iotx_report_devinfo(pclient);
if (SUCCESS_RETURN != err) {
#ifdef DEBUG_REPORT_MID_DEVINFO_FIRMWARE
mqtt_err("failed to report devinfo");
#endif
}
/* report firmware version */
#if !defined(BUILD_AOS) && !defined(MUTE_VERSION_REPORT)
err = iotx_report_firmware_version(pclient);
if (SUCCESS_RETURN != err) {
#ifdef DEBUG_REPORT_MID_DEVINFO_FIRMWARE
mqtt_err("failed to report firmware version");
#endif
}
#endif
#endif
}
#ifdef DYNAMIC_REGISTER
#include "dynreg_api.h"
int HAL_SetDeviceSecret(char *device_secret);
int HAL_GetProductSecret(char *product_secret);
int HAL_Kv_Set(const char *key, const void *val, int len, int sync);
int HAL_Kv_Get(const char *key, void *val, int *buffer_len);
#define DYNAMIC_REG_KV_PREFIX "DYNAMIC_REG_"
#define DYNAMIC_REG_KV_PREFIX_LEN 12
static int _iotx_dynamic_register(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta_info)
{
char device_secret_kv[IOTX_DEVICE_SECRET_LEN + 1] = {0};
int device_secret_len = IOTX_DEVICE_SECRET_LEN;
char kv_key[IOTX_DEVICE_NAME_LEN + DYNAMIC_REG_KV_PREFIX_LEN] = DYNAMIC_REG_KV_PREFIX;
int res = FAIL_RETURN;
memcpy(kv_key + strlen(kv_key), meta_info->device_name, strlen(meta_info->device_name));
/* Check if Device Secret exist in KV */
if (HAL_Kv_Get(kv_key, device_secret_kv, &device_secret_len) == 0) {
mqtt_info("Get DeviceSecret from KV succeed");
*(device_secret_kv + device_secret_len) = 0;
HAL_SetDeviceSecret(device_secret_kv);
memset(meta_info->device_secret, 0, IOTX_DEVICE_SECRET_LEN + 1);
memcpy(meta_info->device_secret, device_secret_kv, strlen(device_secret_kv));
} else {
char product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = {0};
/* KV not exit, goto dynamic register */
mqtt_info("DeviceSecret KV not exist, Now We Need Dynamic Register...");
res = IOT_Dynamic_Register(region, meta_info);
if (res != SUCCESS_RETURN) {
mqtt_err("Dynamic Register Failed");
return FAIL_RETURN;
}
device_secret_len = strlen(meta_info->device_secret);
if (HAL_Kv_Set(kv_key, meta_info->device_secret, device_secret_len, 1) != 0) {
mqtt_err("Save Device Secret to KV Failed");
return FAIL_RETURN;
}
HAL_SetDeviceSecret(meta_info->device_secret);
}
return SUCCESS_RETURN;
}
#endif /* #ifdef DYNAMIC_REGISTER */
#ifdef MQTT_PRE_AUTH
#include "infra_preauth.h"
extern int _iotx_generate_sign_string(const char *device_id, const char *device_name, const char *product_key,
const char *device_secret, char *sign_string);
static int _iotx_preauth(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *meta,
iotx_sign_mqtt_t *preauth_out)
{
uint16_t length = 0;
char device_id[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 1] = {0};
char sign_string[65] = {0};
int res;
memset(preauth_out, 0, sizeof(iotx_sign_mqtt_t));
/* setup device_id */
memcpy(device_id, meta->product_key, strlen(meta->product_key));
memcpy(device_id + strlen(device_id), ".", strlen("."));
memcpy(device_id + strlen(device_id), meta->device_name, strlen(meta->device_name));
/* setup sign_string */
res = _iotx_generate_sign_string(device_id, meta->device_name, meta->product_key, meta->device_secret, sign_string);
if (res < SUCCESS_RETURN) {
return res;
}
return preauth_get_connection_info(region, meta, sign_string, device_id, preauth_out);
}
#endif /* #ifdef MQTT_PRE_AUTH */
extern int _sign_get_clientid(char *clientid_string, const char *device_id, const char *custom_kv, uint8_t enable_itls);
/************************ Public Interface ************************/
void *IOT_MQTT_Construct(iotx_mqtt_param_t *pInitParams)
{
void *pclient;
iotx_dev_meta_info_t meta_info;
iotx_mqtt_param_t mqtt_params;
char device_id[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 1] = {0};
int region = 0;
int dynamic = 0;
uint8_t enalbe_itls = 0;
int ret;
void *callback;
if (g_mqtt_client != NULL) {
mqtt_err("Already exist default MQTT connection, won't proceed another one");
return g_mqtt_client;
}
/* get region */
IOT_Ioctl(IOTX_IOCTL_GET_REGION, (void *)&region);
/* get dynamic option */
IOT_Ioctl(IOTX_IOCTL_GET_DYNAMIC_REGISTER, (void *)&dynamic);
/* get meta_info from hal */
memset(&meta_info, 0, sizeof(iotx_dev_meta_info_t));
HAL_GetProductKey(meta_info.product_key);
HAL_GetDeviceName(meta_info.device_name);
if (meta_info.product_key[0] == '\0' || meta_info.product_key[IOTX_PRODUCT_KEY_LEN] != '\0') {
mqtt_err("Invalid product key, abort!");
return NULL;
}
if (meta_info.device_name[0] == '\0' || meta_info.device_name[IOTX_DEVICE_NAME_LEN] != '\0') {
mqtt_err("Invalid device name, abort!");
return NULL;
}
#ifdef DYNAMIC_REGISTER /* get device secret through https dynamic register */
if (dynamic) {
HAL_GetProductSecret(meta_info.product_secret);
if (meta_info.product_secret[0] == '\0' || meta_info.product_secret[IOTX_PRODUCT_SECRET_LEN] != '\0') {
mqtt_err("Product Secret doesn't exist");
return NULL;
}
ret = _iotx_dynamic_register(region, &meta_info);
if (ret < SUCCESS_RETURN) {
mqtt_err("ret = _iotx_dynamic_register() = %d, abort", ret);
return NULL;
}
} else {
HAL_GetDeviceSecret(meta_info.device_secret);
if (meta_info.device_secret[0] == '\0' || meta_info.device_secret[IOTX_DEVICE_SECRET_LEN] != '\0') {
mqtt_err("Invalid device secret, abort!");
return NULL;
}
}
#else /* get device secret from hal */
HAL_GetDeviceSecret(meta_info.device_secret);
if (meta_info.device_secret[0] == '\0' || meta_info.device_secret[IOTX_DEVICE_SECRET_LEN] != '\0') {
mqtt_err("Invalid device secret, abort!");
return NULL;
}
#endif /* #ifdef DYNAMIC_REGISTER */
#ifdef MQTT_PRE_AUTH /* preauth mode through https */
ret = _iotx_preauth(region, &meta_info, (iotx_sign_mqtt_t *)&g_default_sign); /* type convert */
if (ret < SUCCESS_RETURN) {
mqtt_err("ret = _iotx_preauth() = %d, abort", ret);
return NULL;
}
#else /* direct mode */
ret = IOT_Sign_MQTT(region, &meta_info, &g_default_sign);
if (ret < SUCCESS_RETURN) {
mqtt_err("ret = IOT_Sign_MQTT() = %d, abort", ret);
return NULL;
}
#endif /* #ifdef MQTT_PRE_AUTH */
/* setup device_id */
memcpy(device_id, meta_info.product_key, strlen(meta_info.product_key));
memcpy(device_id + strlen(device_id), ".", strlen("."));
memcpy(device_id + strlen(device_id), meta_info.device_name, strlen(meta_info.device_name));
/* reconfig clientid, append custome clientKV and itls switch flag */
if (pInitParams != NULL && pInitParams->customize_info != NULL) {
if (strstr(pInitParams->customize_info, "authtype=id2") != NULL) {
enalbe_itls = 1;
}
else {
enalbe_itls = 0;
}
}
if (_sign_get_clientid(g_default_sign.clientid, device_id,
(pInitParams != NULL) ? pInitParams->customize_info : NULL, enalbe_itls) != SUCCESS_RETURN) {
return NULL;
}
/* Initialize MQTT parameter */
memset(&mqtt_params, 0x0, sizeof(iotx_mqtt_param_t));
#ifdef SUPPORT_TLS
{
extern const char *iotx_ca_crt;
if (enalbe_itls == 0) {
mqtt_params.pub_key = iotx_ca_crt;
}
else {
memset(iotx_ca_crt_itls, 0, sizeof(iotx_ca_crt_itls));
HAL_GetProductKey(iotx_ca_crt_itls);
iotx_ca_crt_itls[strlen(iotx_ca_crt_itls)] = '.';
HAL_GetProductSecret(iotx_ca_crt_itls + strlen(iotx_ca_crt_itls));
mqtt_params.pub_key = iotx_ca_crt_itls;
}
}
#endif
mqtt_params.request_timeout_ms = CONFIG_MQTT_REQUEST_TIMEOUT;
mqtt_params.clean_session = 0;
mqtt_params.keepalive_interval_ms = CONFIG_MQTT_KEEPALIVE_INTERVAL * 1000;
mqtt_params.read_buf_size = CONFIG_MQTT_MESSAGE_MAXLEN;
mqtt_params.write_buf_size = CONFIG_MQTT_MESSAGE_MAXLEN;
mqtt_params.handle_event.h_fp = NULL;
mqtt_params.handle_event.pcontext = NULL;
/* optional configuration */
if (pInitParams != NULL) {
if (pInitParams->host && strlen(pInitParams->host)) {
mqtt_params.host = pInitParams->host;
} else {
mqtt_warning("Using default hostname: '%s'", g_default_sign.hostname);
mqtt_params.host = g_default_sign.hostname;
}
if (pInitParams->port) {
mqtt_params.port = pInitParams->port;
} else {
mqtt_warning("Using default port: [%d]", g_default_sign.port);
mqtt_params.port = g_default_sign.port;
}
if (pInitParams->client_id && strlen(pInitParams->client_id)) {
mqtt_params.client_id = pInitParams->client_id;
} else {
mqtt_warning("Using default client_id: %s", g_default_sign.clientid);
mqtt_params.client_id = g_default_sign.clientid;
}
if (pInitParams->username && strlen(pInitParams->username)) {
mqtt_params.username = pInitParams->username;
} else {
mqtt_warning("Using default username: %s", g_default_sign.username);
mqtt_params.username = g_default_sign.username;
}
if (pInitParams->password && strlen(pInitParams->password)) {
mqtt_params.password = pInitParams->password;
} else {
#if 1
mqtt_warning("Using default password: %s", "******");
#else
mqtt_warning("Using default password: %s", g_default_sign.password);
#endif
mqtt_params.password = g_default_sign.password;
}
if (pInitParams->request_timeout_ms < CONFIG_MQTT_REQ_TIMEOUT_MIN ||
pInitParams->request_timeout_ms > CONFIG_MQTT_REQ_TIMEOUT_MAX) {
mqtt_warning("Using default request_timeout_ms: %d, configured value(%d) out of [%d, %d]",
mqtt_params.request_timeout_ms,
pInitParams->request_timeout_ms,
CONFIG_MQTT_REQ_TIMEOUT_MIN,
CONFIG_MQTT_REQ_TIMEOUT_MAX);
} else {
mqtt_params.request_timeout_ms = pInitParams->request_timeout_ms;
}
if (pInitParams->clean_session == 0 || pInitParams->clean_session == 1) {
mqtt_params.clean_session = pInitParams->clean_session;
}
if (pInitParams->keepalive_interval_ms < CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN * 1000 ||
pInitParams->keepalive_interval_ms > CONFIG_MQTT_KEEPALIVE_INTERVAL_MAX * 1000) {
mqtt_warning("Using default keepalive_interval_ms: %d, configured value(%d) out of [%d, %d]",
mqtt_params.keepalive_interval_ms,
pInitParams->keepalive_interval_ms,
CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN * 1000,
CONFIG_MQTT_KEEPALIVE_INTERVAL_MAX * 1000);
} else {
mqtt_params.keepalive_interval_ms = pInitParams->keepalive_interval_ms;
}
if (!pInitParams->read_buf_size) {
mqtt_warning("Using default read_buf_size: %d", mqtt_params.read_buf_size);
} else {
mqtt_params.read_buf_size = pInitParams->read_buf_size;
}
if (!pInitParams->write_buf_size) {
mqtt_warning("Using default write_buf_size: %d", mqtt_params.write_buf_size);
} else {
mqtt_params.write_buf_size = pInitParams->write_buf_size;
}
if (pInitParams->handle_event.h_fp != NULL) {
mqtt_params.handle_event.h_fp = pInitParams->handle_event.h_fp;
}
if (pInitParams->handle_event.pcontext != NULL) {
mqtt_params.handle_event.pcontext = pInitParams->handle_event.pcontext;
}
} else {
mqtt_warning("Using default port: [%d]", g_default_sign.port);
mqtt_params.port = g_default_sign.port;
mqtt_warning("Using default hostname: '%s'", g_default_sign.hostname);
mqtt_params.host = g_default_sign.hostname;
mqtt_warning("Using default client_id: %s", g_default_sign.clientid);
mqtt_params.client_id = g_default_sign.clientid;
mqtt_warning("Using default username: %s", g_default_sign.username);
mqtt_params.username = g_default_sign.username;
#if 1
mqtt_warning("Using default password: %s", "******");
#else
mqtt_warning("Using default password: %s", g_default_sign.password);
#endif
mqtt_params.password = g_default_sign.password;
}
pclient = wrapper_mqtt_init(&mqtt_params);
if (pclient == NULL) {
mqtt_err("wrapper_mqtt_init error");
return NULL;
}
ret = wrapper_mqtt_connect(pclient);
if (SUCCESS_RETURN != ret) {
if (MQTT_CONNECT_BLOCK != ret) {
mqtt_err("wrapper_mqtt_connect failed");
wrapper_mqtt_release(&pclient);
return NULL;
}
}
#ifndef ASYNC_PROTOCOL_STACK
iotx_mqtt_report_funcs(pclient);
#endif
g_mqtt_client = pclient;
/* Mqtt Connect Callback */
callback = iotx_event_callback(ITE_MQTT_CONNECT_SUCC);
if (callback) {
((int (*)(void))callback)();
}
return pclient;
}
int IOT_MQTT_Destroy(void **phandler)
{
void *client;
if (phandler != NULL) {
client = *phandler;
*phandler = NULL;
} else {
client = g_mqtt_client;
}
if (client == NULL) {
mqtt_err("handler is null");
return NULL_VALUE_ERROR;
}
wrapper_mqtt_release(&client);
g_mqtt_client = NULL;
return SUCCESS_RETURN;
}
int IOT_MQTT_Yield(void *handle, int timeout_ms)
{
void *pClient = (handle ? handle : g_mqtt_client);
return wrapper_mqtt_yield(pClient, timeout_ms);
}
/* check whether MQTT connection is established or not */
int IOT_MQTT_CheckStateNormal(void *handle)
{
void *pClient = (handle ? handle : g_mqtt_client);
if (pClient == NULL) {
mqtt_err("handler is null");
return NULL_VALUE_ERROR;
}
return wrapper_mqtt_check_state(pClient);
}
int IOT_MQTT_Subscribe(void *handle,
const char *topic_filter,
iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func,
void *pcontext)
{
void *client = handle ? handle : g_mqtt_client;
if (client == NULL) { /* do offline subscribe */
return iotx_mqtt_offline_subscribe(topic_filter, qos, topic_handle_func, pcontext);
}
if (topic_filter == NULL || strlen(topic_filter) == 0 || topic_handle_func == NULL) {
mqtt_err("params err");
return NULL_VALUE_ERROR;
}
#ifdef SUB_PERSISTENCE_ENABLED
if (qos > IOTX_MQTT_QOS3_SUB_LOCAL) {
mqtt_warning("Invalid qos(%d) out of [%d, %d], using %d",
qos,
IOTX_MQTT_QOS0, IOTX_MQTT_QOS3_SUB_LOCAL, IOTX_MQTT_QOS0);
qos = IOTX_MQTT_QOS0;
}
#else
if (qos > IOTX_MQTT_QOS2) {
mqtt_warning("Invalid qos(%d) out of [%d, %d], using %d",
qos,
IOTX_MQTT_QOS0, IOTX_MQTT_QOS2, IOTX_MQTT_QOS0);
qos = IOTX_MQTT_QOS0;
}
#endif
return wrapper_mqtt_subscribe(client, topic_filter, qos, topic_handle_func, pcontext);
}
#define SUBSCRIBE_SYNC_TIMEOUT_MAX 10000
int IOT_MQTT_Subscribe_Sync(void *handle,
const char *topic_filter,
iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func,
void *pcontext,
int timeout_ms)
{
void *client = handle ? handle : g_mqtt_client;
if (client == NULL) { /* do offline subscribe */
return iotx_mqtt_offline_subscribe(topic_filter, qos, topic_handle_func, pcontext);
}
if (timeout_ms > SUBSCRIBE_SYNC_TIMEOUT_MAX) {
timeout_ms = SUBSCRIBE_SYNC_TIMEOUT_MAX;
}
if (topic_filter == NULL || strlen(topic_filter) == 0 || topic_handle_func == NULL) {
mqtt_err("params err");
return NULL_VALUE_ERROR;
}
#ifdef SUB_PERSISTENCE_ENABLED
if (qos > IOTX_MQTT_QOS3_SUB_LOCAL) {
mqtt_warning("Invalid qos(%d) out of [%d, %d], using %d",
qos,
IOTX_MQTT_QOS0, IOTX_MQTT_QOS3_SUB_LOCAL, IOTX_MQTT_QOS0);
qos = IOTX_MQTT_QOS0;
}
#else
if (qos > IOTX_MQTT_QOS2) {
mqtt_warning("Invalid qos(%d) out of [%d, %d], using %d",
qos,
IOTX_MQTT_QOS0, IOTX_MQTT_QOS2, IOTX_MQTT_QOS0);
qos = IOTX_MQTT_QOS0;
}
#endif
return wrapper_mqtt_subscribe_sync(client, topic_filter, qos, topic_handle_func, pcontext, timeout_ms);
}
int IOT_MQTT_Unsubscribe(void *handle, const char *topic_filter)
{
void *client = handle ? handle : g_mqtt_client;
if (client == NULL || topic_filter == NULL || strlen(topic_filter) == 0) {
mqtt_err("params err");
return NULL_VALUE_ERROR;
}
return wrapper_mqtt_unsubscribe(client, topic_filter);
}
int IOT_MQTT_Publish(void *handle, const char *topic_name, iotx_mqtt_topic_info_pt topic_msg)
{
void *client = handle ? handle : g_mqtt_client;
int rc = -1;
if (client == NULL || topic_name == NULL || strlen(topic_name) == 0) {
mqtt_err("params err");
return NULL_VALUE_ERROR;
}
rc = wrapper_mqtt_publish(client, topic_name, topic_msg);
return rc;
}
int IOT_MQTT_Publish_Simple(void *handle, const char *topic_name, int qos, void *data, int len)
{
iotx_mqtt_topic_info_t mqtt_msg;
void *client = handle ? handle : g_mqtt_client;
int rc = -1;
if (client == NULL || topic_name == NULL || strlen(topic_name) == 0) {
mqtt_err("params err");
return NULL_VALUE_ERROR;
}
memset(&mqtt_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
mqtt_msg.qos = qos;
mqtt_msg.retain = 0;
mqtt_msg.dup = 0;
mqtt_msg.payload = (void *)data;
mqtt_msg.payload_len = len;
rc = wrapper_mqtt_publish(client, topic_name, &mqtt_msg);
if (rc < 0) {
mqtt_err("IOT_MQTT_Publish failed\n");
return -1;
}
return rc;
}
int IOT_MQTT_Nwk_Event_Handler(void *handle, iotx_mqtt_nwk_event_t event, iotx_mqtt_nwk_param_t *param)
{
#ifdef ASYNC_PROTOCOL_STACK
void *client = handle ? handle : g_mqtt_client;
int rc = -1;
if (client == NULL || event >= IOTX_MQTT_SOC_MAX || param == NULL) {
mqtt_err("params err");
return NULL_VALUE_ERROR;
}
rc = wrapper_mqtt_nwk_event_handler(client, event, param);
if (rc < 0) {
mqtt_err("IOT_MQTT_Nwk_Event_Handler failed\n");
return -1;
}
switch (event) {
case IOTX_MQTT_SOC_CONNECTED: {
iotx_mqtt_report_funcs(client);
}
break;
default: {
}
break;
}
return rc;
#else
return -1;
#endif
}

View File

@@ -0,0 +1,386 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOT_EXPORT_MQTT_H_
#define _IOT_EXPORT_MQTT_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "infra_types.h"
#include "infra_defs.h"
#define MUTLI_SUBSCIRBE_MAX (5)
/* From mqtt_client.h */
typedef enum {
IOTX_MQTT_QOS0 = 0,
IOTX_MQTT_QOS1,
IOTX_MQTT_QOS2,
IOTX_MQTT_QOS3_SUB_LOCAL
} iotx_mqtt_qos_t;
typedef enum {
/* Undefined event */
IOTX_MQTT_EVENT_UNDEF = 0,
/* MQTT disconnect event */
IOTX_MQTT_EVENT_DISCONNECT = 1,
/* MQTT reconnect event */
IOTX_MQTT_EVENT_RECONNECT = 2,
/* A ACK to the specific subscribe which specify by packet-id be received */
IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS = 3,
/* No ACK to the specific subscribe which specify by packet-id be received in timeout period */
IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT = 4,
/* A failed ACK to the specific subscribe which specify by packet-id be received*/
IOTX_MQTT_EVENT_SUBCRIBE_NACK = 5,
/* A ACK to the specific unsubscribe which specify by packet-id be received */
IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS = 6,
/* No ACK to the specific unsubscribe which specify by packet-id be received in timeout period */
IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT = 7,
/* A failed ACK to the specific unsubscribe which specify by packet-id be received*/
IOTX_MQTT_EVENT_UNSUBCRIBE_NACK = 8,
/* A ACK to the specific publish which specify by packet-id be received */
IOTX_MQTT_EVENT_PUBLISH_SUCCESS = 9,
/* No ACK to the specific publish which specify by packet-id be received in timeout period */
IOTX_MQTT_EVENT_PUBLISH_TIMEOUT = 10,
/* A failed ACK to the specific publish which specify by packet-id be received*/
IOTX_MQTT_EVENT_PUBLISH_NACK = 11,
/* MQTT packet published from MQTT remote broker be received */
IOTX_MQTT_EVENT_PUBLISH_RECEIVED = 12,
/* MQTT packet buffer overflow which the remaining space less than to receive byte */
IOTX_MQTT_EVENT_BUFFER_OVERFLOW = 13,
} iotx_mqtt_event_type_t;
/* topic information */
typedef struct {
uint16_t packet_id;
uint8_t qos;
uint8_t dup;
uint8_t retain;
uint16_t topic_len;
uint32_t payload_len;
const char *ptopic;
const char *payload;
} iotx_mqtt_topic_info_t, *iotx_mqtt_topic_info_pt;
typedef struct {
/* Specify the event type */
iotx_mqtt_event_type_t event_type;
/*
* Specify the detail event information. @msg means different to different event types:
*
* 1) IOTX_MQTT_EVENT_UNKNOWN,
* IOTX_MQTT_EVENT_DISCONNECT,
* IOTX_MQTT_EVENT_RECONNECT :
* Its data type is string and the value is detail information.
*
* 2) IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS,
* IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT,
* IOTX_MQTT_EVENT_SUBCRIBE_NACK,
* IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS,
* IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT,
* IOTX_MQTT_EVENT_UNSUBCRIBE_NACK
* IOTX_MQTT_EVENT_PUBLISH_SUCCESS,
* IOTX_MQTT_EVENT_PUBLISH_TIMEOUT,
* IOTX_MQTT_EVENT_PUBLISH_NACK :
* Its data type is @uint32_t and the value is MQTT packet identifier.
*
* 3) IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
* Its data type is @iotx_mqtt_topic_info_pt and see detail at the declare of this type.
*
* */
void *msg;
} iotx_mqtt_event_msg_t, *iotx_mqtt_event_msg_pt;
/**
* @brief It define a datatype of function pointer.
* This type of function will be called when a related event occur.
*
* @param pcontext : The program context.
* @param pclient : The MQTT client.
* @param msg : The event message.
*
* @return none
*/
typedef void (*iotx_mqtt_event_handle_func_fpt)(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg);
/* The structure of MQTT event handle */
typedef struct {
iotx_mqtt_event_handle_func_fpt h_fp;
void *pcontext;
} iotx_mqtt_event_handle_t, *iotx_mqtt_event_handle_pt;
/* The structure of MQTT initial parameter */
typedef struct {
uint16_t port; /* Specify MQTT broker port */
const char *host; /* Specify MQTT broker host */
const char *client_id; /* Specify MQTT connection client id*/
const char *username; /* Specify MQTT user name */
const char *password; /* Specify MQTT password */
const char *customize_info; /* Specify User custom information */
/* Specify MQTT transport channel and key.
* If the value is NULL, it means that use TCP channel,
* If the value is NOT NULL, it means that use SSL/TLS channel and
* @pub_key point to the CA certification */
const char *pub_key;
uint8_t clean_session; /* Specify MQTT clean session or not*/
uint32_t request_timeout_ms; /* Specify timeout of a MQTT request in millisecond */
uint32_t keepalive_interval_ms; /* Specify MQTT keep-alive interval in millisecond */
uint32_t write_buf_size; /* Specify size of write-buffer in byte */
uint32_t read_buf_size; /* Specify size of read-buffer in byte */
iotx_mqtt_event_handle_t handle_event; /* Specify MQTT event handle */
} iotx_mqtt_param_t, *iotx_mqtt_param_pt;
typedef enum {
IOTX_MQTT_SOC_CONNECTED,
IOTX_MQTT_SOC_CLOSE,
IOTX_MQTT_SOC_READ,
IOTX_MQTT_SOC_WRITE,
IOTX_MQTT_SOC_MAX
} iotx_mqtt_nwk_event_t;
typedef struct {
uintptr_t fd;
} iotx_mqtt_nwk_param_t;
/** @defgroup group_api api
* @{
*/
/** @defgroup group_api_mqtt mqtt
* @{
*/
/**
* @brief Construct the MQTT client
* This function initialize the data structures, establish MQTT connection.
*
* @param [in] pInitParams: specify the MQTT client parameter.
*
* @retval NULL : Construct failed.
* @retval NOT_NULL : The handle of MQTT client.
* @see None.
*/
void *IOT_MQTT_Construct(iotx_mqtt_param_t *pInitParams);
/**
* @brief Deconstruct the MQTT client
* This function disconnect MQTT connection and release the related resource.
*
* @param [in] phandle: pointer of handle, specify the MQTT client.
*
* @retval 0 : Deconstruct success.
* @retval -1 : Deconstruct failed.
* @see None.
*/
int IOT_MQTT_Destroy(void **phandle);
/**
* @brief Handle MQTT packet from remote server and process timeout request
* which include the MQTT subscribe, unsubscribe, publish(QOS >= 1), reconnect, etc..
*
* @param [in] handle: specify the MQTT client.
* @param [in] timeout_ms: specify the timeout in millisecond in this loop.
*
* @return status.
* @see None.
*/
int IOT_MQTT_Yield(void *handle, int timeout_ms);
/**
* @brief check whether MQTT connection is established or not.
*
* @param [in] handle: specify the MQTT client.
*
* @retval true : MQTT in normal state.
* @retval false : MQTT in abnormal state.
* @see None.
*/
int IOT_MQTT_CheckStateNormal(void *handle);
/**
* @brief Subscribe MQTT topic.
*
* @param [in] handle: specify the MQTT client.
* @param [in] topic_filter: specify the topic filter.
* @param [in] qos: specify the MQTT Requested QoS.
* @param [in] topic_handle_func: specify the topic handle callback-function.
* @param [in] pcontext: specify context. When call 'topic_handle_func', it will be passed back.
*
* @retval -1 : Subscribe failed.
* @retval >=0 : Subscribe successful.
The value is a unique ID of this request.
The ID will be passed back when callback 'iotx_mqtt_param_t:handle_event'.
* @see None.
*/
int IOT_MQTT_Subscribe(void *handle,
const char *topic_filter,
iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func,
void *pcontext);
/**
* @brief Subscribe MQTT topic and wait suback.
*
* @param [in] handle: specify the MQTT client.
* @param [in] topic_filter: specify the topic filter.
* @param [in] qos: specify the MQTT Requested QoS.
* @param [in] topic_handle_func: specify the topic handle callback-function.
* @param [in] pcontext: specify context. When call 'topic_handle_func', it will be passed back.
* @param [in] timeout_ms: time in ms to wait.
*
* @retval -1 : Subscribe failed.
* @retval >=0 : Subscribe successful.
The value is a unique ID of this request.
The ID will be passed back when callback 'iotx_mqtt_param_t:handle_event'.
* @see None.
*/
int IOT_MQTT_Subscribe_Sync(void *handle,
const char *topic_filter,
iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func,
void *pcontext,
int timeout_ms);
/**
* @brief Unsubscribe MQTT topic.
*
* @param [in] handle: specify the MQTT client.
* @param [in] topic_filter: specify the topic filter.
*
* @retval -1 : Unsubscribe failed.
* @retval >=0 : Unsubscribe successful.
The value is a unique ID of this request.
The ID will be passed back when callback 'iotx_mqtt_param_t:handle_event'.
* @see None.
*/
int IOT_MQTT_Unsubscribe(void *handle, const char *topic_filter);
/**
* @brief Publish message to specific topic.
*
* @param [in] handle: specify the MQTT client.
* @param [in] topic_name: specify the topic name.
* @param [in] topic_msg: specify the topic message.
*
* @retval -1 : Publish failed.
* @retval 0 : Publish successful, where QoS is 0.
* @retval >0 : Publish successful, where QoS is >= 0.
The value is a unique ID of this request.
The ID will be passed back when callback 'iotx_mqtt_param_t:handle_event'.
* @see None.
*/
int IOT_MQTT_Publish(void *handle, const char *topic_name, iotx_mqtt_topic_info_pt topic_msg);
/**
* @brief Publish message to specific topic.
*
* @param [in] handle: specify the MQTT client.
* @param [in] topic_name: specify the topic name.
* @param [in] qos: specify the MQTT Requested QoS.
* @param [in] data: specify the topic message payload.
* @param [in] len: specify the topic message payload len.
*
* @retval -1 : Publish failed.
* @retval 0 : Publish successful, where QoS is 0.
* @retval >0 : Publish successful, where QoS is >= 0.
The value is a unique ID of this request.
The ID will be passed back when callback 'iotx_mqtt_param_t:handle_event'.
* @see None.
*/
int IOT_MQTT_Publish_Simple(void *handle, const char *topic_name, int qos, void *data, int len);
/* From mqtt_client.h */
/** @} */ /* end of api_mqtt */
/** @} */ /* end of api */
/**
* @brief Only used in async network stack and FEATURE_ASYNC_PROTOCOL_STACK must be selected
*
* @param [in] handle: specify the MQTT client.
* @param [in] event: specify the network event.
* @param [in] param: specify the network params.
*
* @retval -1 : Handle failed.
* @retval 0 : Handle successful.
*
*/
int IOT_MQTT_Nwk_Event_Handler(void *handle, iotx_mqtt_nwk_event_t event, iotx_mqtt_nwk_param_t *param);
/* MQTT Configurations
*
* These switches will affect mqtt_api.c and IOT_MQTT_XXX() functions' behaviour
*
*/
/* Default message length in bytes when PLATFORM_HAS_DYNMEM is not set */
#define CONFIG_MQTT_MESSAGE_MAXLEN (1024)
/* Default maximum length of topic name in byte when PLATFORM_HAS_DYNMEM is not set */
#ifdef PLATFORM_HAS_DYNMEM
#define CONFIG_MQTT_TOPIC_MAXLEN (128)
#else
#define CONFIG_MQTT_TOPIC_MAXLEN (50)
#endif
/* Default keepalive interval of MQTT request in second */
#define CONFIG_MQTT_KEEPALIVE_INTERVAL (60)
/* Default offline subscribe list max length when PLATFORM_HAS_DYNMEM is not set */
#ifndef CONFIG_MQTT_OFFLINE_TOPIC_MAXNUM
#define CONFIG_MQTT_OFFLINE_TOPIC_MAXNUM (5)
#endif
/* Default timeout interval of MQTT request in millisecond */
#define CONFIG_MQTT_REQUEST_TIMEOUT (2000)
/* Minimum timeout interval of MQTT request in millisecond */
#define CONFIG_MQTT_REQ_TIMEOUT_MIN (500)
/* Maximum timeout interval of MQTT request in millisecond */
#define CONFIG_MQTT_REQ_TIMEOUT_MAX (5000)
/* Minimum keepalive interval of MQTT request in second */
#define CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN (30)
/* Maximum keepalive interval of MQTT request in second */
#define CONFIG_MQTT_KEEPALIVE_INTERVAL_MAX (1200)
#if defined(__cplusplus)
}
#endif
#endif

View File

@@ -0,0 +1,65 @@
#include "infra_types.h"
#include "infra_defs.h"
#include "wrappers_defs.h"
#include "mqtt_api.h"
void *HAL_Malloc(uint32_t size);
void HAL_Free(void *ptr);
void HAL_Printf(const char *fmt, ...);
uint64_t HAL_UptimeMs(void);
void HAL_SleepMs(uint32_t ms);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
void *HAL_MutexCreate(void);
void HAL_MutexDestroy(void *mutex);
void HAL_MutexLock(void *mutex);
void HAL_MutexUnlock(void *mutex);
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1]);
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
int HAL_GetFirmwareVersion(char *version);
#ifdef DYNAMIC_REGISTER
int HAL_SetDeviceSecret(char *device_secret);
int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1]);
int HAL_Kv_Set(const char *key, const void *val, int len, int sync);
int HAL_Kv_Get(const char *key, void *val, int *buffer_len);
#endif
#ifdef SUPPORT_TLS
uintptr_t HAL_SSL_Establish(const char *host, uint16_t port, const char *ca_crt, uint32_t ca_crt_len);
int32_t HAL_SSL_Destroy(uintptr_t handle);
int HAL_SSL_Write(uintptr_t handle, const char *buf, int len, int timeout_ms);
int HAL_SSL_Read(uintptr_t handle, char *buf, int len, int timeout_ms);
#else
uintptr_t HAL_TCP_Establish(const char *host, uint16_t port);
int HAL_TCP_Destroy(uintptr_t fd);
int32_t HAL_TCP_Write(uintptr_t fd, const char *buf, uint32_t len, uint32_t timeout_ms);
int32_t HAL_TCP_Read(uintptr_t fd, char *buf, uint32_t len, uint32_t timeout_ms);
#endif
/* mqtt protocol wrapper */
void *wrapper_mqtt_init(iotx_mqtt_param_t *mqtt_params);
int wrapper_mqtt_connect(void *client);
int wrapper_mqtt_yield(void *client, int timeout_ms);
int wrapper_mqtt_check_state(void *client);
int wrapper_mqtt_subscribe(void *client,
const char *topicFilter,
iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func,
void *pcontext);
int wrapper_mqtt_subscribe_sync(void *client,
const char *topic_filter,
iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func,
void *pcontext,
int timeout_ms);
int wrapper_mqtt_unsubscribe(void *client, const char *topicFilter);
int wrapper_mqtt_publish(void *client, const char *topicName, iotx_mqtt_topic_info_pt topic_msg);
int wrapper_mqtt_release(void **pclient);
int wrapper_mqtt_nwk_event_handler(void *client, iotx_mqtt_nwk_event_t event, iotx_mqtt_nwk_param_t *param);