APUE Learning Example Source Code
guowenxue
2020-04-29 b8fb7f30f9d42d69bf25734bf0ee351809e62198
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
/*********************************************************************************
 *      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月14日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2020年04月14日 00时52分56秒"
 *                 
 ********************************************************************************/
 
#include <stdio.h>
#include <string.h>
 
#include "tlv_pack.h"
#include "crc-itu-t.h"
 
 
int packtlv_ack(char *buf, int size, int ack)
{
    unsigned short      crc16 = 0;
    int                 pack_len = 0;
    int                 ofset = 0; /* index position for the buf */
 
    if(!buf || size<TLV_MIN_SIZE )
    {
        printf("Invalid input arguments\n");
        return 0;
    }
 
    /*+-------------------------+
     *|   1.fill packet Header  | 
     *+-------------------------+*/
 
    buf[ofset] = PACK_HEADER;
    ofset += 1;
 
    /*+-------------------------+
     *|   2.fill packet Tag     | 
     *+-------------------------+*/
 
    buf[ofset] = TAG_ACK;
    ofset += 1;
 
    /*+-------------------------+
     *|   3.fill packet Length  | 
     *+-------------------------+*/
 
    /* ACK message only get 1B payload data */
    pack_len = TLV_FIXED_SIZE + 1;
    buf[ofset] = pack_len;
    ofset += 1;
 
    /*+-------------------------+
     *|   4.fill packet Value   | 
     *+-------------------------+*/
 
    if( ack )
        buf[ofset++] = 1; /* 1 for ACK */
    else
        buf[ofset++] = 0; /* 0 for NAK */
 
    /*+-------------------------+
     *|   5.fill packet CRC     | 
     *+-------------------------+*/
 
    /* Calc CRC16 checksum value from Packet Head(buf[0]) ~ Value(buf[ofset]) */
    crc16 = crc_itu_t(MAGIC_CRC, buf, ofset);
 
    /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */
    ushort_to_bytes(&buf[ofset], crc16);
    ofset += 2;
 
    /* ofset value is the TLV packet length  */
    return ofset;
}
 
 
int packtlv_sn(char *buf, int size, char *sn)
{
    unsigned short      crc16 = 0;
    int                 payload_len = 0;
    int                 pack_len = 0;
    int                 ofset = 0; /* index position for the buf */
 
    if(!buf || size<TLV_MIN_SIZE )
    {
        printf("Invalid input arguments\n");
        return 0;
    }
 
    /*+-------------------------+
     *|   1.fill packet Header  | 
     *+-------------------------+*/
    buf[ofset] = PACK_HEADER;
    ofset += 1;
 
    /*+-------------------------+
     *|   2.fill packet Tag     | 
     *+-------------------------+*/
    buf[ofset] = TAG_SN;
    ofset += 1;
 
    /*+-------------------------+
     *|   3.fill packet Length  | 
     *+-------------------------+*/
 
    /* $sn too long maybe result buffer overflow, so we need check the buffer 
     * is large enuf or not. If not enuf we will truncate $sn string 
     */
    if( strlen(sn) <= size-TLV_FIXED_SIZE )
        payload_len = strlen(sn);
    else
        payload_len = size-TLV_FIXED_SIZE;
 
    /*  TLV packet length is SN length+5Byte () */ 
    pack_len = payload_len + TLV_FIXED_SIZE;
 
    buf[ofset] = pack_len;
    ofset += 1;
 
    /*+-------------------------+
     *|   4.fill packet Value   | 
     *+-------------------------+*/
    memcpy(&buf[ofset], sn, payload_len);
    ofset += payload_len;
 
    /*+-------------------------+
     *|   5.fill packet CRC     | 
     *+-------------------------+*/
 
    /* Calc CRC16 checksum value from Packet Head(buf[0]) ~ Value(buf[ofset]) */
    crc16 = crc_itu_t(MAGIC_CRC, buf, ofset);
 
    /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */
    ushort_to_bytes(&buf[ofset], crc16);
    ofset += 2;
 
    /* ofset value is the TLV packet length  */
    return ofset;
}
 
 
int packtlv_temp(char *buf, int size, uint16_t temp)
{
    unsigned short      crc16 = 0;
    int                 pack_len = 0;
    int                 ofset = 0; /* index position for the buf */
 
    if(!buf || size<TLV_FIXED_SIZE+2 )
    {
        printf("Invalid input arguments\n");
        return 0;
    }
 
    /*+-------------------------+
     *|   1.fill packet Header  | 
     *+-------------------------+*/
 
    buf[ofset] = PACK_HEADER;
    ofset += 1;
 
    /*+-------------------------+
     *|   2.fill packet Tag     | 
     *+-------------------------+*/
 
    buf[ofset] = TAG_TEMP;
    ofset += 1;
 
    /*+-------------------------+
     *|   3.fill packet Length  | 
     *+-------------------------+*/
 
    /* termperature message get 2B payload data */
    pack_len = TLV_FIXED_SIZE + 2;
    buf[ofset] = pack_len;
    ofset += 1;
 
    /*+-------------------------+
     *|   4.fill packet Value   | 
     *+-------------------------+*/
 
    /* temperature get 2 bytes, byte[0]:integer part, byte[1]:fractional part  */
    memcpy(&buf[ofset], &temp, 2);
    ofset += 2;
 
    /*+-------------------------+
     *|   5.fill packet CRC     | 
     *+-------------------------+*/
 
    /* Calc CRC16 checksum value from Packet Head(buf[0]) ~ Value(buf[ofset]) */
    crc16 = crc_itu_t(MAGIC_CRC, buf, ofset);
 
    /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */
    ushort_to_bytes(&buf[ofset], crc16);
    ofset += 2;
 
    /* ofset value is the TLV packet length  */
    return ofset;
}
 
int packtlv_time(char *buf, int size, struct tm *tm)
{
    unsigned short      crc16 = 0;
    int                 pack_len = 0;
    int                 ofset = 0; /* index position for the buf */
 
    if(!buf || size<TLV_FIXED_SIZE+6 )
    {
        printf("Invalid input arguments\n");
        return 0;
    }
 
    /*+-------------------------+
     *|   1.fill packet Header  | 
     *+-------------------------+*/
 
    buf[ofset] = PACK_HEADER;
    ofset += 1;
 
    /*+-------------------------+
     *|   2.fill packet Tag     | 
     *+-------------------------+*/
 
    buf[ofset] = TAG_TIME;
    ofset += 1;
 
    /*+-------------------------+
     *|   3.fill packet Length  | 
     *+-------------------------+*/
 
    /* date time message get 6B payload data */
    pack_len = TLV_FIXED_SIZE + 6;
    buf[ofset] = pack_len;
    ofset += 1;
 
    /*+-------------------------+
     *|   4.fill packet Value   | 
     *+-------------------------+*/ 
 
    buf[ofset++] = tm->tm_year-100; /* tm_year is since 1900, we change it it to 2000 */
    buf[ofset++] = tm->tm_mon+1;    /* tm_mon is from 0~11  */
    buf[ofset++] = tm->tm_mday;
    buf[ofset++] = tm->tm_hour;
    buf[ofset++] = tm->tm_min;
    buf[ofset++] = tm->tm_sec;
 
    /*+-------------------------+
     *|   5.fill packet CRC     | 
     *+-------------------------+*/
 
    /* Calc CRC16 checksum value from Packet Head(buf[0]) ~ Value(buf[ofset]) */
    crc16 = crc_itu_t(MAGIC_CRC, buf, ofset);
 
    /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */
    ushort_to_bytes(&buf[ofset], crc16);
    ofset += 2;
 
    /* ofset value is the TLV packet length  */
    return ofset;
}
 
 
 
/*+------------------------------+
 *|  dump_buf() implement code   |
 *+------------------------------+*/
 
#define LINELEN 81
#define CHARS_PER_LINE 16
 
static char *print_char =
    "                "
    "                "
    " !\"#$%&'()*+,-./"
    "0123456789:;<=>?"
    "@ABCDEFGHIJKLMNO"
    "PQRSTUVWXYZ[\\]^_"
    "`abcdefghijklmno"
    "pqrstuvwxyz{|}~ "
    "                "
    "                "
    " ???????????????"
    "????????????????"
    "????????????????"
    "????????????????"
    "????????????????"
    "????????????????";
 
/* print every byte in the buffer as HEX and corresponding charactor, which is not printable charactor will be instead as '?' */
void dump_buf(char *data, int len)
{
    int rc;
    int idx;
    char prn[LINELEN];
    char lit[CHARS_PER_LINE + 1];
    char hc[4];
    short line_done = 1;
 
    rc = len;
    idx = 0;
    lit[CHARS_PER_LINE] = '\0';
 
    while (rc > 0)
    {
        if (line_done)
            snprintf(prn, LINELEN, "%08X: ", idx);
        do
        {
            unsigned char c = data[idx];
            snprintf(hc, 4, "%02X ", c);
            strncat(prn, hc, 4);
            lit[idx % CHARS_PER_LINE] = print_char[c];
            ++idx;
        } while (--rc > 0 && (idx % CHARS_PER_LINE != 0));
 
        line_done = (idx % CHARS_PER_LINE) == 0;
 
        if (line_done)
            printf("%s  %s\n", prn, lit);
        else if (rc == 0)
            strncat(prn, "   ", LINELEN);
    }
 
    if (!line_done)
    {
        lit[(idx % CHARS_PER_LINE)] = '\0';
 
        while ((++idx % CHARS_PER_LINE) != 0)
            strncat(prn, "   ", LINELEN);
 
        printf("%s  %s\n", prn, lit);
 
    }
}