/********************************************************************************
|
* Copyright: (C) 2020 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: tlv_pack.h
|
* Description: This head file is for TLV packet
|
*
|
* Version: 1.0.0(2020年04月15日)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "2020年04月15日 23时14分21秒"
|
*
|
********************************************************************************/
|
#ifndef _TLV_PACK_H_
|
#define _TLV_PACK_H_
|
|
#include <stdint.h>
|
#include <time.h>
|
|
/* TLV Packet format:
|
*
|
*+------------+---------+------------+-----------+-----------+
|
*| Header(1B) | Tag(1B) | Length(1B) | Value(1B) | CRC16(2B) |
|
*+------------+---------+------------+-----------+-----------+
|
*/
|
|
#define PACK_HEADER 0xFD
|
|
/* TLV packet fixed segement size, 1B Head, 1B Tag, 1B length, 2B CRC16, total 5B. */
|
#define TLV_FIXED_SIZE 5
|
|
/* TLV packet Minimum size is fixed bytes + 1 byte data */
|
#define TLV_MIN_SIZE (TLV_FIXED_SIZE+1)
|
#define TLV_MAX_SIZE 32 /* all TLV packet length are no more than 32 bytes */
|
|
|
/* Tag definition */
|
enum
|
{
|
TAG_ACK=1,
|
TAG_SN,
|
TAG_TEMP,
|
TAG_TIME,
|
};
|
|
#define TLV_BUFSIZE 256
|
|
#define TLV_FLAG_TX 1<<0 /* This TLV message need to be sent by socket */
|
#define TLV_FLAG_DB 1<<1 /* This TLV message need to be saved in database */
|
typedef struct tlv_buf_s
|
{
|
uint8_t flag; /* TLV message flags */
|
char buf[TLV_BUFSIZE];
|
int len; /* data length */
|
int size; /* buffer size */
|
} tlv_buf_t;
|
|
|
typedef struct tlv_pack_s
|
{
|
unsigned char header;
|
unsigned char tag;
|
char len;
|
char data[0];
|
/* followed: $len bytes payload data */
|
/* followed: 2 bytes CRC checksum */
|
} tlv_pack_t;
|
|
|
|
/* Packet TLV message for server reply ACK message: ACK(ack=1)/NAK(ack=0) */
|
#define ACK 1
|
#define NAK 0
|
int packtlv_ack (char *buf, int size, int ack);
|
|
/* Packet TLV message for SN */
|
int packtlv_sn (char *buf, int size, char *sn);
|
|
/* Packet TLV message for temperature */
|
int packtlv_temp(char *buf, int size, uint16_t temp);
|
|
/* Packet TLV message for date time */
|
int packtlv_time(char *buf, int size, struct tm *tm);
|
|
/* Packet 3 TLV message together: with SN, temperature, date&time */
|
int packtlv_msg(tlv_buf_t *tlv, char *sn, uint16_t temp, struct tm *tm);
|
|
/* print buf data in hex and string mode */
|
void dump_buf(char *data, int len);
|
|
#endif /* ----- #ifndef _TLV_PACK_H_ ----- */
|