GuoWenxue
2022-04-29 665e1b5cb16d058f478867d0e8a4f8632e2cb02e
upate packet time to struct tm
3 files modified
50 ■■■■■ changed files
apue/project_socket/main/client_main.c 2 ●●● patch | view | raw | blame | history
apue/project_socket/src/packet.c 38 ●●●●● patch | view | raw | blame | history
apue/project_socket/src/packet.h 10 ●●●● patch | view | raw | blame | history
apue/project_socket/main/client_main.c
@@ -147,7 +147,7 @@
                    temper_integer(pack_info.temper), temper_fract(pack_info.temper));
            get_devid(pack_info.devid, DEVID_LEN, sn);
            get_time(pack_info.strtime, TIME_LEN);
            get_time(&pack_info.sample_time);
            pack_bytes = pack_proc(&pack_info, pack_buf, sizeof(pack_buf));
            sample_flag = 1; /* set sample flag */
apue/project_socket/src/packet.c
@@ -32,35 +32,40 @@
    return 0;
}
int get_time(char *strtime, int size)
int get_time(struct tm *ptm)
{
    time_t now = time(NULL);
    struct tm *tnow = localtime(&now);
    if( !strtime || size<TIME_LEN )
    if( !ptm )
    {
        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);
    time_t now = time(NULL);
    localtime_r(&now, ptm);
    return 0;
}
int packet_segmented_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;
    }
    memset(pack_buf, 0, size);
    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);
    snprintf(pack_buf, size, "%s|%s|%d.%d", pack_info->devid, pack_info->strtime,
    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);
@@ -68,19 +73,24 @@
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;
    }
    memset(pack_buf, 0, size);
    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, pack_info->strtime,
            temper_integer(pack_info->temper), temper_fract(pack_info->temper));
            pack_info->devid, strtime, temper_integer(pack_info->temper), temper_fract(pack_info->temper));
    return strlen(pack_buf);
}
apue/project_socket/src/packet.h
@@ -16,6 +16,7 @@
#define  _PACKET_H_
#include <stdint.h>
#include <time.h>
#define DEVID_LEN          16
#define TIME_LEN           32
@@ -23,7 +24,7 @@
typedef struct pack_info_s 
{
    char          devid[DEVID_LEN];  /* device ID  */
    char          strtime[TIME_LEN]; /* sample time  */
    struct tm     sample_time;       /* sample time  */
    uint16_t      temper;            /* sample temperature */
} pack_info_t; 
@@ -39,13 +40,12 @@
 */
extern int get_devid(char *devid, int size, int sn);
/*  description: get current system in format "YYYY-MM-DD HH:MM:SS"
/*  description: get current system in struct tm
 *   input args:
 *               $strtime:  time string output buf
 *               $size   :  time string output buffer size
 *               $sample_time:  sample time in struct tm
 * return value: <0: failure   0:ok
 */
extern int get_time(char *strtime, int size);
extern int get_time(struct tm *sample_time);
/*  description: package a string packet in format "devid|time|temper"
 *   input args: