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 |
|
|
44 |
time_t now = time(NULL); |
|
45 |
localtime_r(&now, ptm); |
|
46 |
|
|
47 |
return 0; |
|
48 |
} |
|
49 |
|
|
50 |
int packet_segmented_pack(pack_info_t *pack_info, char *pack_buf, int size) |
|
51 |
{ |
|
52 |
char strtime[TIME_LEN] = {'\0'}; |
|
53 |
struct tm *ptm; |
|
54 |
|
|
55 |
if( !pack_info || !pack_buf || size<=0 ) |
|
56 |
{ |
|
57 |
log_error("Invalid input arguments\n"); |
|
58 |
return -1; |
|
59 |
} |
|
60 |
|
|
61 |
ptm = &pack_info->sample_time; |
|
62 |
snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d", |
|
63 |
ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, |
|
64 |
ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
|
65 |
|
|
66 |
|
|
67 |
memset(pack_buf, 0, size); |
|
68 |
snprintf(pack_buf, size, "%s|%s|%.3f", pack_info->devid, strtime, pack_info->temper); |
|
69 |
|
|
70 |
return strlen(pack_buf); |
|
71 |
} |
|
72 |
|
|
73 |
int packet_json_pack(pack_info_t *pack_info, char *pack_buf, int size) |
|
74 |
{ |
|
75 |
char strtime[TIME_LEN] = {'\0'}; |
|
76 |
struct tm *ptm; |
|
77 |
|
|
78 |
if( !pack_info || !pack_buf || size<=0 ) |
|
79 |
{ |
|
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 |
|
|
89 |
memset(pack_buf, 0, size); |
|
90 |
snprintf(pack_buf, size, "{\"devid\":\"%s\", \"time\":\"%s\",\"temperature\":\"%.3f\"}", |
|
91 |
pack_info->devid, strtime, pack_info->temper); |
|
92 |
|
|
93 |
return strlen(pack_buf); |
|
94 |
} |
|
95 |
|