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_seg.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 176 insertions(+), 0 deletions(-)
diff --git a/project/1.packet/pack_seg.c b/project/1.packet/pack_seg.c
new file mode 100644
index 0000000..f4bb50b
--- /dev/null
+++ b/project/1.packet/pack_seg.c
@@ -0,0 +1,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;
+}
--
Gitblit v1.9.1