From 68826376ee5f47783c644c6604f4411ec747cd7e Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Fri, 14 Nov 2025 23:52:16 +0800
Subject: [PATCH] Add UDP DNS client source code

---
 project/1.packet/pack_json.c |  169 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 169 insertions(+), 0 deletions(-)

diff --git a/project/1.packet/pack_json.c b/project/1.packet/pack_json.c
new file mode 100644
index 0000000..0a3370d
--- /dev/null
+++ b/project/1.packet/pack_json.c
@@ -0,0 +1,169 @@
+/*********************************************************************************
+ *      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;
+}
+

--
Gitblit v1.9.1