APUE Learning Example Source Code
guowenxue
2020-06-06 cd10eef059e9d9c34c927f3ac53ac43f4a9716c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/********************************************************************************
 *      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)
 
 
/* 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;  
 
/* 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_  ----- */