From fbbb2d0b2aba4ddc42651096b20dd4a7ca4a33f2 Mon Sep 17 00:00:00 2001 From: Guo Wenxue <guowenxue@gmail.com> Date: Wed, 21 Sep 2022 23:04:57 +0800 Subject: [PATCH] Merge branch 'master' of ssh://master.iot-yun.club:2280/raspberrypi --- apue/project_socket/src/packet.c | 93 ++++++++++++++++++++++++++++++---------------- 1 files changed, 61 insertions(+), 32 deletions(-) diff --git a/apue/project_socket/src/packet.c b/apue/project_socket/src/packet.c index 1f1d9f9..9bb680f 100644 --- a/apue/project_socket/src/packet.c +++ b/apue/project_socket/src/packet.c @@ -4,11 +4,11 @@ * * Filename: packet.c * Description: This file is packet API functions - * + * * Version: 1.0.0(18/04/22) * Author: Guo Wenxue <guowenxue@gmail.com> * ChangeLog: 1, Release initial version on "18/04/22 16:30:25" - * + * ********************************************************************************/ #include <stdio.h> @@ -21,47 +21,76 @@ int get_devid(char *devid, int size, int sn) { - if( !devid || size<DEVID_LEN ) - { - log_error("Invalid input arugments\n"); - return -1; - } + if( !devid || size<DEVID_LEN ) + { + log_error("Invalid input arugments\n"); + return -1; + } - memset(devid, 0, size); - snprintf(devid, size, "rpi#%04d", sn); - return 0; + memset(devid, 0, size); + snprintf(devid, size, "rpi#%04d", sn); + return 0; } -int get_time(char *strtime, int size) +int get_time(struct tm *ptm) { + if( !ptm ) + { + log_error("Invalid input arugments\n"); + return -1; + } + + time_t now = time(NULL); - struct tm *tnow = localtime(&now); + localtime_r(&now, ptm); - if( !strtime || size<TIME_LEN ) - { - log_error("Invalid input arugments\n"); - return -1; - } - - snprintf(strtime, size, "%04d-%02d-%2d %02d:%02d:%02d", - tnow->tm_year+1900, tnow->tm_mon+1, tnow->tm_mday, - tnow->tm_hour, tnow->tm_min, tnow->tm_sec); - - return 0; + return 0; } -int packet_string_pack(pack_info_t *pack_info, char *pack_buf, int size) +int packet_segmented_pack(pack_info_t *pack_info, char *pack_buf, int size) { - if( !pack_info || !pack_buf || size<=0 ) - { - log_error("Invalid input arguments\n"); - return -1; - } + char strtime[TIME_LEN] = {'\0'}; + struct tm *ptm; - snprintf(pack_buf, size, "%s|%s|%d.%d", pack_info->devid, pack_info->strtime, - temper_integer(pack_info->temper), temper_fract(pack_info->temper)); + if( !pack_info || !pack_buf || size<=0 ) + { + log_error("Invalid input arguments\n"); + return -1; + } - return strlen(pack_buf); + ptm = &pack_info->sample_time; + snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d", + ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, + ptm->tm_hour, ptm->tm_min, ptm->tm_sec); + + + memset(pack_buf, 0, size); + snprintf(pack_buf, size, "%s|%s|%d.%d", pack_info->devid, strtime, + temper_integer(pack_info->temper), temper_fract(pack_info->temper)); + + return strlen(pack_buf); } +int packet_json_pack(pack_info_t *pack_info, char *pack_buf, int size) +{ + char strtime[TIME_LEN] = {'\0'}; + struct tm *ptm; + + if( !pack_info || !pack_buf || size<=0 ) + { + log_error("Invalid input arguments\n"); + return -1; + } + + ptm = &pack_info->sample_time; + snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d", + ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, + ptm->tm_hour, ptm->tm_min, ptm->tm_sec); + + memset(pack_buf, 0, size); + snprintf(pack_buf, size, "{\"devid\":\"%s\", \"time\":\"%s\",\"temperature\":\"%d.%d\"}", + pack_info->devid, strtime, temper_integer(pack_info->temper), temper_fract(pack_info->temper)); + + return strlen(pack_buf); +} -- Gitblit v1.9.1