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
169
170
171
172
173
174
175
176
/*********************************************************************************
 *      Copyright:  (C) 2023 LingYun IoT System Studio.
 *                  All rights reserved.
 *
 *       Filename:  pack_seg.c
 *    Description:  This file is segement 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日 16时34分49秒"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
 
#define CONFIG_DEBUG
 
#ifdef CONFIG_DEBUG
#define dbg_print(format, args...)  printf(format, ##args)
#else
#define dbg_print(format, args...)  do {} while(0)
#endif
 
#define DEVSN_LEN     11
 
typedef struct data_s
{
    char        devsn[DEVSN_LEN+1];
    char        time[32];
    float       temp;
    float       rh;
} pack_t;
 
 
int pack_seg(char *buf, int size, char *devsn, char *time, float temp, float rh);
int unpack_seg(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_seg(buf, sizeof(buf), devsn, time, temp, rh)) < 0 )
    {
        printf("Package sample data message failed, rv=%d\n", rv);
        return rv;
    }
 
    if( (rv=unpack_seg(buf, rv, &data)) < 0 )
    {
        printf("Parser sample data message failed, rv=%d\n", rv);
        return rv;
    }
 
 
    return 0;
}
 
 
int pack_seg(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, "%s,%s,%.2f,%.2f", devsn, time, temp, rh);
 
    dbg_print("\033[1;32mPacket Message: %s\033[0m\n", buf);
 
    return strlen(buf);
}
 
 
int unpack_seg(char *buf, int len, pack_t *pack)
{
    char      *ptr_start = NULL;
    char      *ptr_end = NULL;
    int        bytes;
 
    if( !buf || len<=0 || !pack )
    {
        printf("\033[1;31m%s:%d %s() Invalid input arguments\033[0m\n", __FILE__, __LINE__, __func__);
        return -1;
    }
 
    dbg_print("\033[1;33mParser Message: %s\033[0m\n", buf);
 
    if( !(ptr_start=strstr(buf, "ISK")) )
    {
        printf("ERROR: Device SN not found\n");
        return -2;
    }
 
    memset(pack, 0, sizeof(*pack));
 
    /*+--------------------------+
     *| Start parser SN segement |
     *+--------------------------+*/
 
    if( !(ptr_end=strchr(ptr_start, ',')) )
    {
        printf("ERROR: Package not integrated \n");
        return -2;
    }
 
    bytes = ptr_end - ptr_start;
    bytes = bytes>DEVSN_LEN ? DEVSN_LEN : bytes;
 
    memcpy(pack->devsn, ptr_start, bytes);
    printf("Found devsn      : %s\n", pack->devsn);
 
    /*+----------------------------+
     *| Start parser Time segement |
     *+----------------------------+*/
 
    ptr_start = ptr_end+1;
 
    if( !(ptr_end=strchr(ptr_start, ',')) )
    {
        printf("ERROR: Package not integrated \n");
        return -2;
    }
 
    bytes = ptr_end - ptr_start;
    bytes = bytes>sizeof(pack->time) ? sizeof(pack->time) : bytes;
 
    memcpy(pack->time, ptr_start, bytes);
    printf("Found time       : %s\n", pack->time);
 
 
    /*+--------------------------+
     *| Start parser temperature |
     *+--------------------------+*/
 
    ptr_start = ptr_end+1;
 
    if( !(ptr_end=strchr(ptr_start, ',')) )
    {
        printf("ERROR: Package not integrated \n");
        return -2;
    }
 
    *ptr_end='\0';
    pack->temp = atof(ptr_start);
    *ptr_end=',';
 
    printf("Found temperature: %.2f\n", pack->temp);
 
    /*+-----------------------+
     *| Start parser humidity |
     *+-----------------------+*/
 
    ptr_start = ptr_end+1;
 
    if( !(ptr_end=strchr(ptr_start, '\0')) )
    {
        printf("ERROR: Package not integrated \n");
        return -2;
    }
 
    pack->temp = atof(ptr_start);
    printf("Found humidity   : %.2f\n", pack->temp);
 
    return 0;
}