RaspberrPi project source code
Guo Wenxue
2024-04-11 1dab8d0c424af912c01eea9337e82250c5ef648f
commit | author | age
29b331 1 /*********************************************************************************
GW 2  *      Copyright:  (C) 2022 LingYun IoT System Studio
3  *                  All rights reserved.
4  *
5  *       Filename:  packet.c
6  *    Description:  This file is packet API functions
7  *
8  *        Version:  1.0.0(18/04/22)
9  *         Author:  Guo Wenxue <guowenxue@gmail.com>
10  *      ChangeLog:  1, Release initial version on "18/04/22 16:30:25"
11  *
12  ********************************************************************************/
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <time.h>
18 #include "packet.h"
19 #include "logger.h"
20 #include "ds18b20.h"
21
22 int get_devid(char *devid, int size, int sn)
23 {
24     if( !devid || size<DEVID_LEN )
25     {
26         log_error("Invalid input arugments\n");
27         return -1;
28     }
29
30     memset(devid, 0, size);
31     snprintf(devid, size, "rpi#%04d", sn);
32     return 0;
33 }
34
35 int get_time(struct tm *ptm)
36 {
37     if( !ptm )
38     {
39         log_error("Invalid input arugments\n");
40         return -1;
41     }
42
43     time_t now = time(NULL);
44     localtime_r(&now, ptm);
45
46     return 0;
47 }
48
5b381c 49 int packet_segmented_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
29b331 50 {
GW 51     char              strtime[TIME_LEN] = {'\0'};
52     struct tm        *ptm;
5b381c 53     char             *buf = (char *)pack_buf;
29b331 54
5b381c 55     if( !pack_info || !buf || size<=0 )
29b331 56     {
GW 57         log_error("Invalid input arguments\n");
58         return -1;
59     }
60
61     ptm = &pack_info->sample_time;
bffa2b 62     snprintf(strtime, sizeof(strtime), "%04d-%02d-%02d %02d:%02d:%02d",
29b331 63             ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
GW 64             ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
65
5b381c 66     memset(buf, 0, size);
GW 67     snprintf(buf, size, "%s|%s|%.3f", pack_info->devid, strtime, pack_info->temper);
29b331 68
5b381c 69     return strlen(buf);
29b331 70 }
GW 71
5b381c 72 int packet_json_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
29b331 73 {
GW 74     char              strtime[TIME_LEN] = {'\0'};
75     struct tm        *ptm;
5b381c 76     char             *buf = (char *)pack_buf;
29b331 77
5b381c 78     if( !pack_info || !buf || size<=0 )
29b331 79     {
GW 80         log_error("Invalid input arguments\n");
81         return -1;
82     }
83
84     ptm = &pack_info->sample_time;
85     snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d",
86             ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
87             ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
88
5b381c 89     memset(buf, 0, size);
GW 90     snprintf(buf, size, "{\"devid\":\"%s\", \"time\":\"%s\",\"temperature\":\"%.3f\"}",
29b331 91             pack_info->devid, strtime, pack_info->temper);
GW 92
5b381c 93     return strlen(buf);
GW 94 }
95
96 /*
97  * The CRC-ITU-T, also known as CRC-16-ITU-T or CRC-16-V.41, uses the polynomial:
98  *     0x1021:  x^16 + x^12 + x^5 + 1
99  */
100
101 #define CRC16_ITU_T_POLY 0x1021   /* Define the CRC-ITU-T polynomial */
102 static uint16_t crc_itu_t(const uint8_t *data, size_t length)
103 {
104     uint16_t          crc = 0xFFFF;
105     size_t            i, j;
106
107     for (i=0; i<length; i++)
108     {
109         crc ^= ((uint16_t)data[i] << 8);
110
111         for(j=0; j<8; j++)
112         {
113             if (crc & 0x8000)
114             {
115                 crc = (crc << 1) ^ CRC16_ITU_T_POLY;
116             }
117             else
118             {
119                 crc <<= 1;
120             }
121         }
122     }
123
124     return crc;
125 }
126
127 uint16_t to_big_endian(uint16_t num)
128 {
129     return (num << 8) | (num >> 8);
130 }
131
132 /* TLV(Tag Length Value) PDU(Protocal Data Unit) format:
133  *
134  * +-----------+-----------+------------+-------------+-------------+
135  * | Header(2B)|  Tag(1B)  | Length(2B) |  Value(nB)  |  CRC16(2B)  |
136  * +-----------+-----------+------------+-------------+-------------+
137  *
138  * Header(2B): 0xFE 0xFD
139  *    Tag(1B): 0x01->temperature 0x02->Humidity 0x03->Noisy ...
140  * Length(2B): Data value length
141  *  Value(nB): Data value
142  *  CRC16(2B): CRC from Header to Value
143  */
144
145 int packet_tlv_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
146 {
147     struct tm          *ptm;
148     int                 ofset = 0;
149     uint16_t            crc;
150
151     if( !pack_info || !pack_buf || size<TLV_MINSIZE )
152
153     {
154         log_error("Invalid input arguments\n");
155         return -1;
156     }
157
158     /*+----------------------+
159      *|    TLV Header(2B)    |
160      *+----------------------+*/
161     *(uint16_t *)pack_buf = to_big_endian(TLV_HEADER);
162     ofset += 2;
163
164     /*+----------------------+
165      *|      TLV Tag(1B)     |
166      *+----------------------+*/
167     pack_buf[ofset++] = TAG_TEMPERATURE;
168
169     /* Skip data length here, we will calculate it later */
170     ofset += 2;
171
172     /*+----------------------+
173      *|  payload data value  |
174      *+----------------------+*/
175
176     /* 6 bytes sample time */
177     ptm = &pack_info->sample_time;
178     pack_buf[ofset++] = (uint8_t)(ptm->tm_year+1900-2000);
179     pack_buf[ofset++] = (uint8_t)(ptm->tm_mon+1);
180     pack_buf[ofset++] = (uint8_t)(ptm->tm_mday);
181     pack_buf[ofset++] = (uint8_t)(ptm->tm_hour);
182     pack_buf[ofset++] = (uint8_t)(ptm->tm_min);
183     pack_buf[ofset++] = (uint8_t)(ptm->tm_sec);
184
185     /* 8 bytes device SN */
186     strncpy((char *)(pack_buf+ofset), pack_info->devid, DEVID_LEN);
187     ofset += DEVID_LEN;
188
189     /* 2 bytes temperature value */
190     pack_buf[ofset++] = (int)pack_info->temper;  /* integer part */
191     pack_buf[ofset++] = (((int)(pack_info->temper*100))%100); /* fractional part */
192
193     /*+----------------------+
194      *|    TLV Length(2B)    |
195      *+----------------------+*/
196     *(uint16_t *)(pack_buf+3) = (ofset-5);
197
198     /*+----------------------+
199      *|      TLV CRC(2B)     |
200      *+----------------------+*/
201     crc = crc_itu_t(pack_buf, ofset);
202     *(uint16_t *)(pack_buf+ofset) = crc;
203     ofset += 2;
204
205     return ofset;
29b331 206 }
GW 207