From 5b381c80976c76bb7e0d779f1ff878416c93db78 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Thu, 11 Apr 2024 11:31:56 +0800
Subject: [PATCH] Add TLV packet support

---
 project/socketd/booster/packet.h |   47 ++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/project/socketd/booster/packet.h b/project/socketd/booster/packet.h
index 6344e22..70e556a 100644
--- a/project/socketd/booster/packet.h
+++ b/project/socketd/booster/packet.h
@@ -18,18 +18,18 @@
 #include <stdint.h>
 #include <time.h>
 
-#define DEVID_LEN          16
+#define DEVID_LEN          8
 #define TIME_LEN           32
 
 typedef struct pack_info_s
 {
-    char          devid[DEVID_LEN];  /* device ID  */
+    char          devid[DEVID_LEN+1];  /* device ID  */
     struct tm     sample_time;       /* sample time  */
     float         temper;            /* sample temperature */
 } pack_info_t;
 
 /* packet function pointer type */
-typedef int (* pack_proc_t)(pack_info_t *pack_info, char *pack_buf, int size);
+typedef int (* pack_proc_t)(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
 /*  description: get device ID
  *   input args:
@@ -54,7 +54,7 @@
  *               $size     :  packet output buffer size
  * return value: <0: failure   >0: packet bytes
  */
-extern int packet_segmented_pack(pack_info_t *pack_info, char *pack_buf, int size);
+extern int packet_segmented_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
 
 /*  description: package a json string packet: {"devid":"xxx", "time":"xxx", "temperature":"xxx"}
@@ -64,7 +64,44 @@
  *               $size     :  packet output buffer size
  * return value: <0: failure   >0: packet bytes
  */
-extern int packet_json_pack(pack_info_t *pack_info, char *pack_buf, int size);
+extern int packet_json_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size);
 
 
+/* TLV(Tag Length Value) PDU(Protocal Data Unit) format:
+ *
+ * +-----------+-----------+------------+-------------+-------------+
+ * | Header(2B)|  Tag(1B)  | Length(2B) |  Value(nB)  |  CRC16(2B)  |
+ * +-----------+-----------+------------+-------------+-------------+
+ *
+ * Header(2B): 0xFE 0xED
+ *    Tag(1B): 0x01->temperature 0x02->Humidity 0x03->Noisy ...
+ * Length(2B): Data length
+ *  Value(nB): Data value
+ *  CRC16(2B): CRC from Header to Value
+ */
+
+/* TLV Header */
+#define TLV_HEADER          0xFEED
+
+/* TLV bytes without payload: Header(2B)+Tag(1B)+Length(2B)+CRC16(2B) */
+#define TLV_MINSIZE         7
+
+/* TVL Tag definition */
+enum
+{
+    TAG_TEMPERATURE = 1,
+    TAG_HUMIDITY,
+    TAG_NOISY,
+};
+
+/*  description: package a TLV packet: 0xFD 0xFE
+ *   input args:
+ *               $pack_info:  packet data contains devid, time and temperature
+ *               $pack_buf :  packet output buffer
+ *               $size     :  packet output buffer size
+ * return value: <0: failure   >0: packet bytes
+ */
+
+int packet_tlv_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size);
+
 #endif   /* ----- #ifndef _PACKET_H_  ----- */

--
Gitblit v1.9.1