APUE course source code
guowenxue
2 days ago 68826376ee5f47783c644c6604f4411ec747cd7e
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/********************************************************************************
 *      Copyright:  (C) 2022 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  packet.h
 *    Description:  This head file is packet API functions.
 *
 *        Version:  1.0.0(18/04/22)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "18/04/22 16:24:40"
 *
 ********************************************************************************/
#ifndef  _PACKET_H_
#define  _PACKET_H_
 
#include <stdint.h>
#include <time.h>
 
#define DEVID_LEN          8
#define TIME_LEN           32
 
typedef struct pack_info_s
{
    uint8_t       tag;                 /* Tag for TLV */
    char          devid[DEVID_LEN+1];  /* device ID  */
    struct tm     sample_time;         /* sample time  */
    float         temper;              /* sample temperature */
} pack_info_t;
 
/* packet function pointer type */
typedef int (* pack_proc_t)(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
/*  description: get device ID
 *   input args:
 *               $devid :  device ID string
 *               $size  :  device ID output buffer size
 *               $sn    :  serial number
 * return value: <0: failure   0:ok
 */
extern int get_devid(char *devid, int size, int sn);
 
/*  description: get current system in struct tm
 *   input args:
 *               $sample_time:  sample time in struct tm
 * return value: <0: failure   0:ok
 */
extern int get_time(struct tm *sample_time);
 
/*  description: package a string packet in format "devid|time|temper"
 *   input args:
 *               $pack_info:  packet data contains devid, time and temperature
 *               $pack_buf :  packet output buffer
 *               $size     :  packet output buffer size
 * return value: <0: failure   >0: packet bytes
 */
extern int packet_segmented_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
 
/*  description: package a json string packet: {"devid":"xxx", "time":"xxx", "temperature":"xxx"}
 *   input args:
 *               $pack_info:  packet data contains devid, time and temperature
 *               $pack_buf :  packet output buffer
 *               $size     :  packet output buffer size
 * return value: <0: failure   >0: packet bytes
 */
extern int packet_json_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
 
/* TLV(Tag Length Value) PDU(Protocal Data Unit) format:
 *
 * +-----------+-----------+------------+-------------+-------------+
 * | Header(2B)|  Tag(1B)  | Length(2B) |  Value(nB)  |  CRC16(2B)  |
 * +-----------+-----------+------------+-------------+-------------+
 *
 * Header(2B): 0xFE 0xED
 *    Tag(1B): 0x01->temperature 0x02->Humidity 0x03->Noisy ...
 * Length(2B): Data length
 *  Value(nB): Data value
 *  CRC16(2B): CRC from Header to Value
 */
 
/* TLV Header */
#define TLV_HEADER          0xFEED
 
/* TLV bytes without payload: Header(2B)+Tag(1B)+Length(2B)+CRC16(2B) */
#define TLV_FIXSIZE         7
 
/* TLV packet length are no more than 32 bytes */
#define TLV_MAXSIZE         32
 
/* TVL Tag definition */
enum
{
    TAG_TEMPERATURE = 1,
    TAG_HUMIDITY,
    TAG_NOISY,
};
 
typedef struct tlv_pack_s
{
    uint16_t            header;
    uint8_t             tag;
    uint16_t            len;
    uint8_t             data[0]; /* Zero length array */
    /* followed: $len bytes payload data */
    /* followed: 2 bytes CRC checksum    */
} tlv_pack_t;
 
extern int ushort_to_bytes(uint16_t val, uint8_t *bytes);
extern uint16_t bytes_to_ushort(uint8_t *bytes, int len);
 
/*
 * description: compute the CRC-ITU-T for the data buffer
 *   input args:
 *               $data  :  data pointer
 *               $length:  number of bytes in the buffer
 * return value: the 16-bits CRC value
 */
extern uint16_t crc_itu_t(const uint8_t *data, size_t length);
 
/*  description: package a TLV packet
 *   input args:
 *               $pack_info:  packet data contains devid, time and temperature
 *               $pack_buf :  packet output buffer
 *               $size     :  packet output buffer size
 * return value: <0: failure   >0: packet bytes
 */
 
int packet_tlv_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
/*  description: package a TLV packet
 *   input args:
 *               $pack_info:  packet data contains devid, time and temperature
 *               $pack_buf :  packet output buffer
 *               $size     :  packet output buffer size
 * return value: <0: failure  0: not integrated  >0: processed packet len
 */
 
int parser_tlv_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
#endif   /* ----- #ifndef _PACKET_H_  ----- */