Skip to content

Commit

Permalink
Merge pull request #35 from huaweicloud/revert-33-dev4
Browse files Browse the repository at this point in the history
新增MQTT5.0
  • Loading branch information
louiscrazy authored Oct 17, 2022
2 parents 9774426 + 67b06bc commit d38a112
Show file tree
Hide file tree
Showing 13 changed files with 932 additions and 126 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CFLAGS = -g -w -lrt -m64 -Wl,-z,relro,-z,now,-z,noexecstack -fno-strict-aliasing
#-D Linux=1
CXXFLAGS = -O2 -g -Wall -fmessage-length=0 -lrt -m64 -Wl,-z,relro,-z,now,-z,noexecstack -fno-strict-aliasing -fno-omit-frame-pointer -pipe -Wall -fPIC -MD -MP -fno-common -freg-struct-return -fno-inline -fno-exceptions -Wfloat-equal -Wshadow -Wformat=2 -Wextra -rdynamic -Wl,-z,relro,-z,noexecstack -fstack-protector-strong -fstrength-reduce -fno-builtin -fsigned-char -ffunction-sections -fdata-sections -Wpointer-arith -Wcast-qual -Waggregate-return -Winline -Wunreachable-code -Wcast-align -Wundef -Wredundant-decls -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs

OBJS = hmac_sha256.o mqtt_base.o log_util.o string_util.o cJSON.o json_util.o base.o callback_func.o login.o subscribe.o data_trans.o iota_init.o iota_login.o iota_datatrans.o device_demo.o
OBJS = hmac_sha256.o mqtt_base.o log_util.o string_util.o cJSON.o json_util.o base.o callback_func.o login.o subscribe.o data_trans.o iota_init.o iota_login.o iota_datatrans.o device_demo.o mqttv5_util.o
#generic_tcp_protocol.o gateway_server_demo.o
#bootstrap_demo.o
#$(warning "OS $(OS)")
Expand Down Expand Up @@ -41,7 +41,10 @@ log_util.o: $(SRC_PATH)/util/log_util.c

string_util.o: $(SRC_PATH)/util/string_util.c
$(CC) $(CFLAGS) -c $(SRC_PATH)/util/string_util.c -o string_util.o $(HEADER_PATH)/util/ $(HEADER_PATH)/agentlite/ $(HEADER_PATH)/third_party/zlib/


mqttv5_util.o: $(SRC_PATH)/util/mqttv5_util.c
$(CC) $(CFLAGS) -c $(SRC_PATH)/util/mqttv5_util.c -o mqttv5_util.o $(HEADER_PATH)/util/ $(HEADER_PATH)/agentlite/ $(HEADER_PATH)/third_party/zlib/

cJSON.o: $(SRC_PATH)/third_party/cjson/cJSON.c
$(CC) $(CFLAGS) -c $(SRC_PATH)/third_party/cjson/cJSON.c -o cJSON.o $(HEADER_PATH)/third_party/cjson/ $(HEADER_PATH)/agentlite/

Expand Down
16 changes: 12 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@

17、增加消息存储样例

18、增加文件上传/下载功能

19、增加MQTT5.0协议

如需回到旧版,请下载realeases版本 https://github.com/huaweicloud/huaweicloud-iot-device-sdk-c/releases

*2022/09/09*
*2022/10/17*

<h1 id="1">1.前言</h1>
本文通过实例讲述huaweicloud-iot-device-sdk-c(以下简称SDK)帮助设备用MQTT协议快速连接到华为物联网平台。
Expand Down Expand Up @@ -572,9 +576,7 @@ void SetAuthConfig() {
新增代码实现了异常场景下的属性消息存储,并进行上报、重发的样例。 Test_PropertiesStoreData( )函数是具体实现入口。通过STORE_DATA_SWITCH来标识是否打开该功能,不注释#define STORE_DATA_SWITCH 是打开 ,注释时关闭,默认是关闭的。
该新增代码为样例代码,存储的容器采用的是动态二维数组,用户可以根据自己的业务逻辑来进行选择。建议设备采集到数据后就进行存储,设备链路正常的时候再进行重发。
基本逻辑如下:


- 上报数据前 存储传感器的数据(当前使用的是数组 用户可以自己选择)
- 上报数据前 存储传感器的数据(当前使用的是数组 用户可以自己选择)

![](./doc/doc_cn/存储.png)

Expand All @@ -584,6 +586,12 @@ void SetAuthConfig() {
- 等网络恢复后,再上报容器中遗留的数据。
![](./doc/doc_cn/重连.png)


- **MQTT5.0协议使用**

如果想使用MQTT5.0协议(默认为MQTT3.1.1),需要在文件./include/util/mqttv5_util.h 中取消 #define MQTTV5的备注。


## 开源协议

* 遵循BSD-3开源许可协议
Expand Down
16 changes: 16 additions & 0 deletions include/agentlite/iota_datatrans.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <openssl/ossl_typ.h>
#include "cJSON.h"
#include "hw_type.h"
#include "mqttv5_util.h"

#define MaxServiceReportNum 10 //set the max count of reported services
#define MaxSubDeviceCount 100
Expand Down Expand Up @@ -85,6 +86,15 @@ typedef struct {
HW_CHAR *delSubDevice[MaxDelSubDevCount]; //the array of deleted sub devices
} ST_IOTA_DEL_SUB_DEVICE;

typedef struct
{
HW_CHAR *object_device_id;
HW_CHAR *name;
HW_CHAR *id;
HW_CHAR *content;
HW_CHAR *topicParas;
} ST_IOTA_MESS_REP_INFO;


/**
* ST_IOTA_UPGRADE_STATUS_INFO:
Expand Down Expand Up @@ -161,6 +171,12 @@ HW_API_FUNC HW_INT IOTA_SubscribeBoostrap();
HW_API_FUNC HW_INT IOTA_AddSubDevice(ST_IOTA_SUB_DEVICE_INFO *subDevicesInfo, HW_INT deviceNum, void *context);
HW_API_FUNC HW_INT IOTA_DelSubDevice(ST_IOTA_DEL_SUB_DEVICE *delSubDevices, HW_INT deviceNum, void *context);
HW_API_FUNC HW_INT IOTA_ReportDeviceLog(HW_CHAR *type, HW_CHAR *content, HW_CHAR *timestamp, void *context);
#if defined(MQTTV5)
HW_API_FUNC HW_INT IOTA_CommandResponseV5(HW_CHAR *requestId, HW_INT result_code, HW_CHAR *response_name, HW_CHAR *pcCommandResponse, void *context, MQTTV5_DATA *properties);
HW_API_FUNC HW_INT IOTA_BatchPropertiesReportV5(ST_IOTA_DEVICE_DATA_INFO pDeviceData[], HW_INT deviceNum, HW_INT serviceLenList[], HW_INT compressFlag, void *context, MQTTV5_DATA *mqttv5);
HW_API_FUNC HW_INT IOTA_PropertiesReportV5(ST_IOTA_SERVICE_DATA_INFO pServiceData[], HW_INT serviceNum, HW_INT compressFlag, void *context, MQTTV5_DATA *mqttv5);
HW_API_FUNC HW_INT IOTA_MessageReportV5(ST_IOTA_MESS_REP_INFO mass, HW_INT compressFlag, void *context, MQTTV5_DATA *mqttv5);
#endif

#define SDK_VERSION "C_v1.0.0"
#define OTA_PORT 8943
Expand Down
4 changes: 2 additions & 2 deletions include/base/mqtt_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ typedef enum {
} ENUM_MQTT_BASE_CONFIG;

typedef void (*MQTT_BASE_CALLBACK_HANDLER)(EN_IOTA_MQTT_PROTOCOL_RSP *protocolRsp);
typedef void (*MQTT_BASE_CALLBACK_HANDLER_WITH_TOPIC)(void *context, int token, int code, const char *topic, char *message);
typedef void (*MQTT_BASE_CALLBACK_HANDLER_WITH_TOPIC)(void *context, int token, int code, const char *topic, char *message, void *mqttv5);
typedef void MqttBase_connectionLost(void *context, char *cause);
typedef void MqttBase_deliveryComplete(void *context, int token);
typedef void MqttBase_messageArrived(void *context, char *topic, char *message);
Expand All @@ -107,7 +107,7 @@ int MqttBase_SetCallbackWithTopic(int item, MQTT_BASE_CALLBACK_HANDLER_WITH_TOPI
int MqttBase_CreateConnection(void);
int MqttBase_ReleaseConnection(void);
int MqttBase_subscribe(const char *topic, const int qos);
int MqttBase_publish(const char *topic, void *payload, int len, void* context);
int MqttBase_publish(const char *topic, void *payload, int len, void* context, void *properties);
int MqttBase_StringLength(char *str);
int MqttBase_destory(void);

Expand Down
4 changes: 2 additions & 2 deletions include/service/callback_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef void (*LOG_CALLBACK_HANDLER)(int level, char *format, va_list args);
#define MAX_SERVICE_COUNT 50

void OnLoginSuccess(EN_IOTA_MQTT_PROTOCOL_RSP *rsp);
void OnMessageArrived(void *context, int token, int code, const char *topic, char *message);
void OnMessageArrived(void *context, int token, int code, const char *topic, char *message, void *mqttv5);

void SetLogCallback(LOG_CALLBACK_HANDLER logCallbackHandler);

Expand All @@ -51,7 +51,7 @@ int SetCmdCallbackV3(CMD_CALLBACK_HANDLER_V3 pfnCallbackHandler);
typedef void (*PROTOCOL_CALLBACK_HANDLER)(EN_IOTA_MQTT_PROTOCOL_RSP *protocolRsp);
int SetProtocolCallback(int item, PROTOCOL_CALLBACK_HANDLER pfnCallbackHandler);

typedef void (*MESSAGE_CALLBACK_HANDLER)(EN_IOTA_MESSAGE *protocolRsp);
typedef void (*MESSAGE_CALLBACK_HANDLER)(EN_IOTA_MESSAGE *protocolRsp, void *mqttv5);
int SetMessageCallback(MESSAGE_CALLBACK_HANDLER pfnCallbackHandler);

typedef void (*PROP_SET_CALLBACK_HANDLER)(EN_IOTA_PROPERTY_SET *rsp);
Expand Down
11 changes: 6 additions & 5 deletions include/service/data_trans.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@

#ifndef INCLUDE_DATATRANS_H_
#define INCLUDE_DATATRANS_H_
#include "mqttv5_util.h"

int ReportDeviceData(char *payload, char *topicParas, int compressFlag, void *context);
int ReportDeviceProperties(char *payload, int compressFlag, void *context);
int ReportBatchDeviceProperties(char *payload, int compressFlag, void *context);
int ReportData(char *topic, char *payload, void *context);
int ReportCommandReponse(char *requestId, char *pcCommandRespense, void *context);
int ReportDeviceData(char *payload, char *topicParas, int compressFlag, void *context, void *properties);
int ReportDeviceProperties(char *payload, int compressFlag, void *context, void *properties);
int ReportBatchDeviceProperties(char *payload, int compressFlag, void *context , void *properties);
int ReportData(char *topic, char *payload, void *context, void *properties);
int ReportCommandReponse(char *requestId, char *pcCommandRespense, void *context, void *properties);
int ReportPropSetReponse(char *requestId, char *pcCommandRespense, void *context);
int ReportPropGetReponse(char *requestId, char *pcCommandRespense, void *context);
int GetPropertiesRequest(char *requestId, char *pcCommandRespense, void *context);
Expand Down
74 changes: 74 additions & 0 deletions include/util/mqttv5_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

/*Copyright (c) <2020>, <Huawei Technologies Co., Ltd>
* All rights reserved.
* &Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* */
#ifndef INCLUDE_UTIL_MQTTV5_UTIL_H_
#define INCLUDE_UTIL_MQTTV5_UTIL_H_

//#define MQTTV5 //If not defined, use MQTT3, else MQTT5.0
#if defined(MQTTV5)
// ------------------------ Create Connection -------------------------------------
#define TOPIC_ALIAS_MAX 20 //MQTT v5 Maximum number of subject aliases

// ------------------------ Mqttv5 Publish ----------------------------------------
typedef struct{
char *key;
char *Value;
void *nex;
}MQTTV5_USER_PRO; //user propeties

typedef struct {
/*
*It consists of a user-defined UTF-8 key/value pair array.
*You can use user attributes to add metadata to mqtt
*messages and transfer information between publishers,
*mqtt servers and subscribers.
*/
MQTTV5_USER_PRO *properties;
/*
*The content type contained in the mqtt5.0 variable header
*is a UTF-8 encoded string, which is used to describe the
*contents of a will message or a publish message.
*A typical application of content type is to store MIME types,
*such as text/plain for text files and audio/AAC for audio files.
*/
char *contnt_type;
/*
*The responder will take appropriate actions according to the
*request message, and then publish the response message to the
*subject specified by the response subject attribute.
*/
char *response_topic;
/*
*The request response of mqtt is asynchronous.
*comparison_data can be used to correlate
*the response message with the request message
*/
char *correlation_data;
}MQTTV5_DATA;

int mqttV5_listFree(MQTTV5_USER_PRO *mqtt_data);

#define mqttv5_initializer {NULL, NULL, NULL, NULL }
#endif
#endif
Loading

0 comments on commit d38a112

Please sign in to comment.