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
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
/*********************************************************************************
 *      Copyright:  (C) 2023 LingYun IoT System Studio.
 *                  All rights reserved.
 *
 *       Filename:  pack_json.c
 *    Description:  This file is JSON format string package and parser example
 *
 *        Version:  1.0.0(2023年07月10日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2023年07月10日 17时57分00秒"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
 
#define DEVSN_LEN     11
 
typedef struct data_s
{
        char        devsn[DEVSN_LEN+1];
        char        time[32];
        float       temp;
        float       rh;
} pack_t;
 
int pack_json(char *buf, int size, char *devsn, char *time, float temp, float rh);
int unpack_json(char *buf, int len, pack_t *pack);
 
int main (int argc, char **argv)
{
    char         buf[512];
    char        *devsn="ISK1023001";
    char        *time="2023-07-10 20:00:00";
    float        temp = 20.50;
    float        rh = 30.50;
    pack_t       data;
    int          rv;
 
    if( (rv=pack_json(buf, sizeof(buf), devsn, time, temp, rh)) < 0 )
    {
        printf("Package sample data message failed, rv=%d\n", rv);
        return rv;
    }
 
    if( (rv=unpack_json(buf, rv, &data)) < 0 )
    {
        printf("Parser sample data message failed, rv=%d\n", rv);
        return rv;
    }
 
    return 0;
}
 
 
int pack_json(char *buf, int size, char *devsn, char *time, float temp, float rh)
{
    if( !buf || size<=0 || !devsn || !time )
    {
        printf("\033[1;31m%s:%d %s() Invalid input arguments\033[0m\n", __FILE__, __LINE__, __func__);
        return -1;
    }
 
    memset(buf, 0, size);
    snprintf(buf, size, "{\"devsn\":\"%s\", \"time\":\"%s\", \"temperature\":%.2f, \"humidity\":%.2f}", devsn, time, temp, rh);
 
    printf("\033[1;32mPacket Message: %s\033[0m\n", buf);
 
    return strlen(buf);
}
 
 
int unpack_json(char *buf, int len, pack_t *pack)
{
    cJSON     *root = NULL;
    cJSON     *item;
    char      *value;
    int        bytes;
    int        rv = 0;
 
    if( !buf || len<=0 || !pack )
    {
        printf("\033[1;31m%s:%d %s() Invalid input arguments\033[0m\n", __FILE__, __LINE__, __func__);
        return -1;
    }
 
    printf("\033[1;33mParser Message: %s\033[0m\n", buf);
 
    root = cJSON_Parse(buf);
    if( !root )
    {
        printf("cJSON_Parse root failure: %s\n", cJSON_GetErrorPtr());
        return -2;
    }
 
    memset(pack, 0, sizeof(*pack));
 
    /*+--------------------------+
     *| Start parser SN segement |
     *+--------------------------+*/
 
    item = cJSON_GetObjectItem(root, "devsn");
    if( !item )
    {
        printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr());
        rv = -3;
        goto cleanup;
    }
 
    bytes = strlen(item->valuestring);
    bytes = bytes>sizeof(pack->devsn) ? sizeof(pack->devsn) : bytes;
    strncpy(pack->devsn, item->valuestring, bytes);
    printf("Found devsn      : %s\n", pack->devsn);
 
 
    /*+----------------------------+
     *| Start parser Time segement |
     *+----------------------------+*/
 
    item = cJSON_GetObjectItem(root, "time");
    if( !item )
    {
        printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr());
        rv = -4;
        goto cleanup;
    }
 
    bytes = strlen(item->valuestring);
    bytes = bytes>sizeof(pack->time) ? sizeof(pack->time) : bytes;
    strncpy(pack->time, item->valuestring, bytes);
    printf("Found time       : %s\n", pack->time);
 
    /*+--------------------------+
     *| Start parser temperature |
     *+--------------------------+*/
 
    item = cJSON_GetObjectItem(root, "temperature");
    if( !item )
    {
        printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr());
        rv = -5;
        goto cleanup;
    }
 
    pack->temp = item->valuedouble;
    printf("Found temperature: %.2f\n", pack->temp);
 
    /*+-----------------------+
     *| Start parser humidity |
     *+-----------------------+*/
 
    item = cJSON_GetObjectItem(root, "humidity");
    if( !item )
    {
        printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr());
        rv = -6;
        goto cleanup;
    }
 
    pack->rh = item->valuedouble;
    printf("Found humidity   : %.2f\n", pack->rh);
 
cleanup:
    cJSON_Delete(root); /* must delete it, or it will result memory leak */
    return rv;
}