APUE course source code
guowenxue
2 days ago 9c22371ef5059a2e46226ee90a0667ffad65b574
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*********************************************************************************
 *      Copyright:  (C) 2022 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  packet.c
 *    Description:  This 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:30:25"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "packet.h"
#include "logger.h"
#include "ds18b20.h"
 
int get_devid(char *devid, int size, int sn)
{
    if( !devid || size<DEVID_LEN )
    {
        log_error("Invalid input arugments\n");
        return -1;
    }
 
    memset(devid, 0, size);
    snprintf(devid, size, "RPI#%04d", sn);
    return 0;
}
 
int get_time(struct tm *ptm)
{
    if( !ptm )
    {
        log_error("Invalid input arugments\n");
        return -1;
    }
 
    time_t now = time(NULL);
    localtime_r(&now, ptm);
 
    return 0;
}
 
int packet_segmented_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
{
    char              strtime[TIME_LEN] = {'\0'};
    struct tm        *ptm;
    char             *buf = (char *)pack_buf;
 
    if( !pack_info || !buf || size<=0 )
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
 
    ptm = &pack_info->sample_time;
    snprintf(strtime, sizeof(strtime), "%04d-%02d-%02d %02d:%02d:%02d",
            ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
            ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
 
    memset(buf, 0, size);
    snprintf(buf, size, "%s|%s|%.3f", pack_info->devid, strtime, pack_info->temper);
 
    return strlen(buf);
}
 
int packet_json_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
{
    char              strtime[TIME_LEN] = {'\0'};
    struct tm        *ptm;
    char             *buf = (char *)pack_buf;
 
    if( !pack_info || !buf || size<=0 )
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
 
    ptm = &pack_info->sample_time;
    snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d",
            ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
            ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
 
    memset(buf, 0, size);
    snprintf(buf, size, "{\"devid\":\"%s\", \"time\":\"%s\",\"temperature\":\"%.3f\"}",
            pack_info->devid, strtime, pack_info->temper);
 
    return strlen(buf);
}
 
/*
 * 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
 */
 
/*
 * The CRC-ITU-T, also known as CRC-16-ITU-T or CRC-16-V.41, uses the polynomial:
 *     0x1021:  x^16 + x^12 + x^5 + 1
 */
#define CRC16_ITU_T_POLY 0x1021   /* Define the CRC-ITU-T polynomial */
uint16_t crc_itu_t(const uint8_t *data, size_t length)
{
    uint16_t          crc = 0xFFFF;
    size_t            i, j;
 
    for (i=0; i<length; i++)
    {
        crc ^= ((uint16_t)data[i] << 8);
 
        for(j=0; j<8; j++)
        {
            if (crc & 0x8000)
            {
                crc = (crc << 1) ^ CRC16_ITU_T_POLY;
            }
            else
            {
                crc <<= 1;
            }
        }
    }
 
    return crc;
}
 
int ushort_to_bytes(uint16_t val, uint8_t *bytes)
{
    int              size  = sizeof (uint16_t);
    int              i = 0;
 
    if( !bytes )
        return 0;
 
    while (size)
    {
        *(bytes + --size) = (val >> ((8 * i++) & 0xFF));
    }
 
    return sizeof(uint16_t);
}
 
uint16_t bytes_to_ushort(uint8_t *bytes, int len)
{
    int            i = 0;
    uint16_t       val = 0;
 
    if( !bytes || len > sizeof(uint16_t) )
        return 0;
 
    for(i=0; i<len; i++)
    {
        val += bytes[i];
 
        if (i < len - 1)
            val = val << 8;
    }
 
    return val;
}
 
/* TLV(Tag Length Value) PDU(Protocal Data Unit) format:
 *
 * +-----------+-----------+------------+-------------+-------------+
 * | Header(2B)|  Tag(1B)  | Length(2B) |  Value(nB)  |  CRC16(2B)  |
 * +-----------+-----------+------------+-------------+-------------+
 *
 * Header(2B): 0xFE 0xFD
 *    Tag(1B): 0x01->temperature 0x02->Humidity 0x03->Noisy ...
 * Length(2B): Data value length, include Header+Tag+Length+Value+CRC16
 *  Value(nB): Data value
 *  CRC16(2B): CRC from Header to Value
 */
 
/*  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)
{
    struct tm          *ptm;
    int                 ofset = 0;
    uint16_t            len;
    uint16_t            crc;
 
    if( !pack_info || !pack_buf || size<TLV_FIXSIZE )
 
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
 
    /*+----------------------+
     *|    TLV Header(2B)    |
     *+----------------------+*/
    ofset += ushort_to_bytes(TLV_HEADER, pack_buf);
 
    /*+----------------------+
     *|      TLV Tag(1B)     |
     *+----------------------+*/
    pack_buf[ofset++] = TAG_TEMPERATURE;
 
    /* Skip data length here, we will calculate it later */
    ofset += 2;
 
    /*+----------------------+
     *|  payload data value  |
     *+----------------------+*/
 
    /* 6 bytes sample time */
    ptm = &pack_info->sample_time;
    pack_buf[ofset++] = (uint8_t)(ptm->tm_year+1900-2000);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_mon+1);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_mday);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_hour);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_min);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_sec);
 
    /* 8 bytes device SN */
    strncpy((char *)(pack_buf+ofset), pack_info->devid, DEVID_LEN);
    ofset += DEVID_LEN;
 
    /* 2 bytes temperature value */
    pack_buf[ofset++] = (int)pack_info->temper;  /* integer part */
    pack_buf[ofset++] = (((int)(pack_info->temper*100))%100); /* fractional part */
 
    /*+----------------------+
     *|    TLV Length(2B)    |
     *+----------------------+*/
    len = ofset + 2; /* include CRC */
    ushort_to_bytes(len, pack_buf+3);
 
    /*+----------------------+
     *|      TLV CRC(2B)     |
     *+----------------------+*/
    crc = crc_itu_t(pack_buf, ofset); /* from Header to payload data */
    ofset += ushort_to_bytes(crc, pack_buf+ofset);
 
    return ofset;
}
 
/*  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)
{
    int                 ofset = 0;
    uint16_t            crc;
    uint16_t            payload_len;
 
    uint16_t            pack_head;
    uint8_t             pack_tag;
    uint16_t            pack_len;
    uint16_t            pack_crc;
 
    if( !pack_info || !pack_buf )
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
 
    if( size < TLV_FIXSIZE )
    {
        log_warn("TLV packet bytes less than min. size\n");
        return 0;
    }
 
    /*+----------------------+
     *|    TLV Header(2B)    |
     *+----------------------+*/
    pack_head = bytes_to_ushort(pack_buf, 2);
    ofset += 2;
 
    if( TLV_HEADER != pack_head)
        return -2;
 
    /*+----------------------+
     *|      TLV Tag(1B)     |
     *+----------------------+*/
    pack_tag = pack_buf[ofset];
    ofset ++;
 
    /*+----------------------+
     *|    TLV Length(2B)    |
     *+----------------------+*/
    pack_len = bytes_to_ushort(pack_buf+ofset, 2);
    ofset += 2;
 
    log_debug("header: 0x%02x tag: 0x%02x len: 0x%02x\n", pack_head, pack_tag, pack_len);
 
    /* Packet length is from Header to CRC16(include) */
    if( pack_len > size )
    {
        log_warn("TLV packet bytes less than length\n");
        return 0;
    }
 
    /*+----------------------+
     *|      TLV CRC(2B)     |
     *+----------------------+*/
 
    /* packet CRC */
    pack_crc = bytes_to_ushort(pack_buf+pack_len-2, 2);
 
    /* calculate CRC  */
    payload_len = pack_len - TLV_FIXSIZE;
    crc = crc_itu_t(pack_buf, ofset+payload_len); /* from Header to payload data */
 
    if( crc != pack_crc)
    {
        log_error("calculate CRC[0x%04x] != packet CRC[0x%04x]\n", crc, pack_crc);
        return -3;
    }
 
    /*+----------------------+
     *|  payload data value  |
     *+----------------------+*/
 
    pack_info->tag = pack_tag;
 
    if( pack_tag == TAG_TEMPERATURE )
    {
        /* 6 bytes sample time */
        pack_info->sample_time.tm_year = pack_buf[ofset++] + 2000 - 1900;
        pack_info->sample_time.tm_mon = pack_buf[ofset++] - 1;
        pack_info->sample_time.tm_mday = pack_buf[ofset++] ;
        pack_info->sample_time.tm_hour = pack_buf[ofset++];
        pack_info->sample_time.tm_min = pack_buf[ofset++];
        pack_info->sample_time.tm_sec = pack_buf[ofset++];
 
        /* 8 bytes device SN */
        strncpy(pack_info->devid, (char *)(pack_buf+ofset), DEVID_LEN);
        ofset += DEVID_LEN;
 
        /* 2 bytes temperature value */
        pack_info->temper = (float)pack_buf[ofset] + (float)pack_buf[ofset+1]/100.0f;
    }
 
    return pack_len;
}