Skip to content

Commit

Permalink
Modify comment
Browse files Browse the repository at this point in the history
  • Loading branch information
moimo committed Oct 12, 2015
1 parent 1f71cec commit 0b1f347
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 63 deletions.
26 changes: 16 additions & 10 deletions MqttPayload/sub_device_protocol.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// Copyright (c) 2015 Pando. All rights reserved.
// PtotoBuf: ProtocolBuffer.h
//
// Create By ZhaoWenwu On 15/01/24.
/*******************************************************
* File name:sub_device_protocol.c
* Author: Zhao Wenwu
* Versions: 0.1
* Description: APIs for sub device.
* History:
* 1.Date:
* Author:
* Modification:
*********************************************************/


#include "sub_device_protocol.h"

Expand All @@ -22,15 +29,14 @@ struct property_indicator

static struct property_indicator s_current_property;
static struct params_block_indicator s_current_param;
struct sub_device_base_params base_params; //子设备的基本参数
static uint16_t current_tlv_block_size = 0; //当前信息区的大小,包含count的大小
static uint16_t tlv_block_buffer_size;
struct sub_device_base_params base_params; //Basic param of sub device
static uint16_t current_tlv_block_size = 0; //Current params block size, include count.
static uint16_t tlv_block_buffer_size; //Size of buffer pre-malloced to contain params block.

static uint16_t get_tlv_count(struct TLVs *params_block);
static uint16_t get_tlv_type(struct TLV *params_in);
static uint16_t get_tlv_len(struct TLV *params_in);
static struct TLV * get_tlv_value(struct TLV *params_in, void *value);
static uint16_t get_sub_device_payloadtype(struct sub_device_buffer *package);
static struct TLV *get_tlv_param(struct TLV *params_in, uint16_t *type, uint16_t *length, void *value);


Expand Down Expand Up @@ -155,7 +161,7 @@ struct TLVs * FUNCTION_ATTRIBUTE create_params_block()
struct TLVs *tlv_block = NULL;
uint8_t need_length;

current_tlv_block_size = 0; //确保每次新建tlv信息区时,信息区大小的计数都是正确的
current_tlv_block_size = 0;

tlv_block_buffer_size = DEFAULT_TLV_BLOCK_SIZE;
tlv_block = (struct TLVs *)pd_malloc(tlv_block_buffer_size);
Expand Down Expand Up @@ -329,7 +335,7 @@ struct TLVs * FUNCTION_ATTRIBUTE get_sub_device_command(
base_params.command_sequence = net32_to_host(head->frame_seq);
command_body->sub_device_id = net16_to_host(tmp_body->sub_device_id);

command_body->command_id = net16_to_host(tmp_body->command_id);
command_body->command_num = net16_to_host(tmp_body->command_num);
command_body->priority = net16_to_host(tmp_body->priority);
command_body->params->count = net16_to_host(tmp_body->params->count);

Expand Down
248 changes: 196 additions & 52 deletions MqttPayload/sub_device_protocol.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright (c) 2015 Pando. All rights reserved.
// PtotoBuf: ProtocolBuffer.h
//
// Create By ZhaoWenwu On 15/01/24.
/*******************************************************
* File name:sub_device_protocol.h
* Author: Zhao Wenwu
* Versions: 0.1
* Description: APIs for sub device.
* History:
* 1.Date:
* Author:
* Modification:
*********************************************************/

#ifndef SUB_DEVICE_PROTOCOL_TOOL_H
#define SUB_DEVICE_PROTOCOL_TOOL_H
Expand Down Expand Up @@ -37,53 +43,60 @@ extern "C"

#pragma pack(1)

/* a device packet is made up with header and payload */
struct device_header
{
uint8_t magic; /* 开始标志 (0x34) */
uint8_t crc; /* 校验和 */
uint16_t payload_type; /* 载荷类型 */
uint16_t payload_len; /* 载荷长度 */
uint16_t flags; /* 标志位 */
uint32_t frame_seq; /* 帧序列 */
uint8_t magic; /* magic number (0x34) */
uint8_t crc; /* crc of whole packet with crc is zero */
uint16_t payload_type; /* type of packet, event, command, data */
uint16_t payload_len; /* length of packet without header */
uint16_t flags; /* flags for extending protocol */
uint32_t frame_seq; /* frame sequence between device and gateway */
};

/*TLV信息区,包含count*/
/* TLV contains the real information of payload.
There are 13 types, such as int8, unint16, length is sizeof the type.
If type such as int32 can present length of itself, TLV is made up of type
and value.
*/
struct TLV
{
uint16_t type;
uint16_t length;
uint8_t value[];
};

/* Count presents packet how many tlv informations in a packet */
struct TLVs
{
uint16_t count;
//struct TLV tlv[];
};

/*命令,事件和数据具体的数据结构*/
/* Payload of command */
struct pando_command
{
uint16_t sub_device_id; /* 子设备ID */
uint16_t command_id; /* 命令ID */
uint16_t priority; /* 优先级 */
struct TLVs params[1]; /* 参数 */
uint16_t sub_device_id; /* ID of device */
uint16_t command_num; /* different command number presents different operation */
uint16_t priority; /* device should handle high priority command first */
struct TLVs params[1]; /* params for command operation */
};

/* Payload of event */
struct pando_event
{
uint16_t sub_device_id; /* 子设备ID */
uint16_t event_num; /* 事件ID */
uint16_t priority;
struct TLVs params[1]; /* 参数 */
uint16_t sub_device_id; /* ID of device */
uint16_t event_num; /* Different event number presents different type event */
uint16_t priority; /* Device should report high priority event first */
struct TLVs params[1]; /* Params for event to report */
};

/*属性的定义*/
/* Payload of data */
struct pando_property
{
uint16_t sub_device_id; /* 子设备ID */
uint16_t property_num; /* 属性编号 */
struct TLVs params[1]; /* 参数 */
uint16_t sub_device_id; /* ID of device */
uint16_t property_num; /* Different property number presents different type data property */
struct TLVs params[1]; /* Params for data to report */
};

struct sub_device_buffer
Expand All @@ -101,47 +114,170 @@ struct sub_device_base_params
#pragma pack()


//初始化子设备模块
/*******************************************************
* Description: Initialize sequence number to 0, it's unnecessary now.
* param
base_params: sequence of data, event, command.
* return: 0 if success, -1 if failed.
*********************************************************/
int init_sub_device(struct sub_device_base_params base_params);

//在创建数据包或者事件包前,先创建好参数的信息区, 同时添加第一个参数,待信息区被create_event等函数成功使用后,要将信息区delete
/*******************************************************
* Description: Create a block buffer before adding tlv params.
It's necessary to delete the buffer when complete creating package.
* param
* return: Buffer of params block.
*********************************************************/
struct TLVs *create_params_block();

//创建事件包,返回缓冲区,
//数据发送完成后,要将返回的缓冲区delete掉
/*******************************************************
* Description: Create package buffer, need add params block to finish package.
* param
flags: Extend for new features.
* return: Buffer of package.
*********************************************************/
struct sub_device_buffer *create_command_package(uint16_t flags);

/*******************************************************
* Description: Create package buffer, need add params block to finish package.
* param
flags: Extend for new features.
* return: Buffer of package.
*********************************************************/
struct sub_device_buffer *create_event_package(uint16_t flags);

/*******************************************************
* Description: Create package buffer, need add params block to finish package.
* param
flags: Extend for new features.
* return: Buffer of package.
*********************************************************/
struct sub_device_buffer *create_data_package(uint16_t flags);

/*******************************************************
* Description: Calculate payload length and crc to finish package construct.
* param
package_buf: buffer contains device package.
* return:
*********************************************************/
int finish_package(struct sub_device_buffer *package_buf);


/*******************************************************
* Description: Add data params block and property number to package.
* param
data_package: buffer contains device package.
property_num: property number, indicate data type.
next_data_params: all data params, count means how many params.
* return: 0 if success, -1 if failed.
*********************************************************/
int add_next_property(struct sub_device_buffer *data_package, uint16_t property_num,
struct TLVs *next_data_params);

/*******************************************************
* Description: Add command params block, priority and command number to package.
* param
command_package: buffer contains device package.
command_num: command number, indicate command type.
priority: high priority command should be processed first.
command_params: all command params, count means how many params.
* return: 0 if success, -1 if failed.
*********************************************************/
int add_command(struct sub_device_buffer *command_package, uint16_t command_num,
uint16_t priority, struct TLVs *command_params);

/*******************************************************
* Description: Add event params block, priority and event number to package.
* param
out:
event_package: buffer contains device package.
in:
event_num: event number, indicate event type.
priority: high priority event should be uploaded to server first.
event_params: all event params, count means how many params.
* return: 0 if success, -1 if failed.
*********************************************************/
int add_event(struct sub_device_buffer *event_package, uint16_t event_num,
uint16_t priority, struct TLVs *event_params);


//解析命令包,command_body传出command_id、参数个数等信息,返回第一个参数的指针,用于get_tlv_param获取参数
/*******************************************************
* Description: Get command from device buffer directly.
* param
in:
device_buffer: buffer contains device package.
out:
command_body: include command number, priority and sub device id.
* return: 0 if success, -1 if failed.
*********************************************************/
struct TLVs *get_sub_device_command(struct sub_device_buffer *device_buffer, struct pando_command *command_body);

/*******************************************************
* Description: Get event from device buffer directly.
* param
in:
device_buffer: buffer contains device package.
out:
event_body: include event number, priority and sub device id.
* return: 0 if success, -1 if failed.
*********************************************************/
struct TLVs *get_sub_device_event(struct sub_device_buffer *device_buffer, struct pando_event *event_body);

//get data's property id and property num with property_body, return tlv param block
/*******************************************************
* Description: Get data from device buffer directly, this function should be called
repeatedly to get all data property.
* param
in:
device_buffer: buffer contains device package.
out:
property_body: include data property number and sub device id.
* return: If all properties have been processed, return NULL, else return data params block.
*********************************************************/
struct TLVs *get_sub_device_property(struct sub_device_buffer *device_buffer, struct pando_property *property_body);


//删除子设备缓冲区,如果为参数创建过信息区,还需要删除信息区
/*******************************************************
* Description: Get type of package from device buffer directly.
* param
in:
device_buffer: buffer contains device package.
out:
* return: Type of package.
*********************************************************/
uint16_t get_sub_device_payloadtype(struct sub_device_buffer *package);

/*******************************************************
* Description: Delete device buffer after package has been sent to server.
* param
in:
device_buffer: buffer contains device package.
out:
* return:
*********************************************************/
void delete_device_package(struct sub_device_buffer *device_buffer);

/*******************************************************
* Description:
* param
* return:
*********************************************************/
void delete_params_block(struct TLVs *params_block);

/*******************************************************
* Description: Judge the command has file to process.
* param
in:
device_buffer: buffer contains device package.
* return: 1 if command with file, else 0.
*********************************************************/
int is_device_file_command(struct sub_device_buffer *device_buffer);



// tlv operation functions, maybe need move to other file.
// you must decode all the tlv params, otherwise can't decode next packet correctly.
/*******************************************************
* Description: Functions to get param from params block, all params must be decode
in order, it's not reentrant.
* param
in:
params: params block.
* return: param to be process.
*********************************************************/
uint8_t get_next_uint8(struct TLVs *params);
uint16_t get_next_uint16(struct TLVs *params);
uint32_t get_next_uint32(struct TLVs *params);
Expand All @@ -156,21 +292,29 @@ uint8_t get_next_bool(struct TLVs *params);
void *get_next_uri(struct TLVs *params, uint16_t *length);
void *get_next_bytes(struct TLVs *params, uint16_t *length);

//多次调用直至添加完所有参数
/*******************************************************
* Description: Functions to add param into params block.
* param
in:
next_value: value to be added.
out:
params_block:
* return: 0 if success, -1 if failed.
*********************************************************/
int add_next_param(struct TLVs *params_block, uint16_t next_type, uint16_t next_length, void *next_value);
int add_next_uint8(struct TLVs *params, uint8_t next_value);
int add_next_uint16(struct TLVs *params, uint16_t next_value);
int add_next_uint32(struct TLVs *params, uint32_t next_value);
int add_next_uint64(struct TLVs *params, uint64_t next_value);
int add_next_int8(struct TLVs *params, int8_t next_value);
int add_next_int16(struct TLVs *params, int16_t next_value);
int add_next_int32(struct TLVs *params, int32_t next_value);
int add_next_int64(struct TLVs *params, int64_t next_value);
int add_next_float32(struct TLVs *params, float next_value);
int add_next_float64(struct TLVs *params, double next_value);
int add_next_bool(struct TLVs *params, uint8_t next_value);
int add_next_uri(struct TLVs *params, uint16_t length, void *next_value);
int add_next_bytes(struct TLVs *params, uint16_t length, void *next_value);
int add_next_uint8(struct TLVs *params, uint8_t next_value);
int add_next_uint16(struct TLVs *params, uint16_t next_value);
int add_next_uint32(struct TLVs *params, uint32_t next_value);
int add_next_uint64(struct TLVs *params, uint64_t next_value);
int add_next_int8(struct TLVs *params, int8_t next_value);
int add_next_int16(struct TLVs *params, int16_t next_value);
int add_next_int32(struct TLVs *params, int32_t next_value);
int add_next_int64(struct TLVs *params, int64_t next_value);
int add_next_float32(struct TLVs *params, float next_value);
int add_next_float64(struct TLVs *params, double next_value);
int add_next_bool(struct TLVs *params, uint8_t next_value);
int add_next_uri(struct TLVs *params, uint16_t length, void *next_value);
int add_next_bytes(struct TLVs *params, uint16_t length, void *next_value);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion Test/stdoutsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void FUNCTION_ATTRIBUTE decode_command(struct pando_buffer *buf, uint16_t payloa
// 1.子设备解命令包, 返回参数区的起始位置
struct TLVs *cmd_params_block = get_sub_device_command(device_buffer, &cmd_body);
pd_printf("sub id %02x, cmd num %02x, pri %02x, count: %d\n",
cmd_body.sub_device_id, cmd_body.command_id,
cmd_body.sub_device_id, cmd_body.command_num,
cmd_body.priority, cmd_body.params->count);

// 2.子设备获取命令参数
Expand Down

0 comments on commit 0b1f347

Please sign in to comment.