当前位置:网站首页>13. Tencent cloud IOT device side learning - data template function and Implementation

13. Tencent cloud IOT device side learning - data template function and Implementation

2022-06-24 03:26:00 fancyxu

The main purpose of this series is to record the learning notes of Tencent cloud Internet of things device , And on the device side SDK Make a supplementary explanation .

brief introduction

The concepts related to data templates and message formats have been introduced in previous articles , No more details here , Please see the Data template

The data template is designed as follows :

Data template design and implementation

Design thinking

According to data template protocol , It is divided into 5 File :

Uplink message

Mainly through publish Send , I won't elaborate here . Among them, the attribute will involve a variety of uplink messages , Here you can go through Joint type To unify the interface :

typedef union {
 const char *json;
 int code;
 struct {
 int code;
 UtilsJsonValue client_token;
    } control_reply;
} PropertyPublishParams;

Downlink message

among , Each is different topic(property/event/action) Will register a context Used as a context for managing downlink messages , The design is as follows :

typedef struct {
 union {
 PropertyMessageCallback property_callback;
 EventMessageCallback event_callback;
 ActionMessageCallback action_callback;
    };
 void *usr_data;
} DataTemplateContext;

It mainly includes the corresponding callback and the user data to be used for the callback , It's used here Joint type , Thus, too many formal parameter types are avoided .

Memory optimization

Version before refactoring , In the use of , Because every time json Parsing all Dynamic applications To pass nodes , For resource constrained devices, it will bring great memory consumption and development difficulties .

Compared to the version before refactoring , This version is mainly for json The library has been transformed , When the corresponding message is delivered to the upper layer , use The pointer + length Instead of the previous Dynamic applications The way , This reduces memory . Here's why :

  1. Practical application , Most of the messages are consumed in the callback , So you can use mqtt The storage space of the package itself
  2. When processing string data , at present CI The tool recommends using safety functions such as (strncpy etc. ), So use The pointer + length More convenient
  3. The data retained after the callback is over should be handed over to the user for processing , instead of sdk Internal application to release , thus Reduce the risk of memory leaks

Please refer to utils_json_value_get

原网站

版权声明
本文为[fancyxu]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/10/20211008015025782q.html