Merge branch 'master' of ssh://master.iot-yun.club:2280/raspberrypi
| | |
| | | char pack_buf[1024]; |
| | | int pack_bytes; |
| | | pack_info_t pack_info; |
| | | pack_proc_t pack_proc = packet_string_pack; /* use string packet */ |
| | | pack_proc_t pack_proc = packet_segmented_pack; /* use string packet */ |
| | | |
| | | struct option opts[] = { |
| | | {"ipaddr", required_argument, NULL, 'i'}, |
| | |
| | | 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 */ |
| | |
| | | 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_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) |
| | | { |
| | | char strtime[TIME_LEN] = {'\0'}; |
| | | struct tm *ptm; |
| | | |
| | | if( !pack_info || !pack_buf || size<=0 ) |
| | | { |
| | | log_error("Invalid input arguments\n"); |
| | | return -1; |
| | | } |
| | | |
| | | snprintf(pack_buf, size, "%s|%s|%d.%d", pack_info->devid, pack_info->strtime, |
| | | 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); |
| | | } |
| | | |
| | |
| | | #define _PACKET_H_ |
| | | |
| | | #include <stdint.h> |
| | | #include <time.h> |
| | | |
| | | #define DEVID_LEN 16 |
| | | #define TIME_LEN 32 |
| | |
| | | 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; |
| | | |
| | |
| | | */ |
| | | 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: |
| | |
| | | * $size : packet output buffer size |
| | | * return value: <0: failure >0: packet bytes |
| | | */ |
| | | extern int packet_string_pack(pack_info_t *pack_info, char *pack_buf, int size); |
| | | extern int packet_segmented_pack(pack_info_t *pack_info, char *pack_buf, int size); |
| | | |
| | | |
| | | /* description: package a json string packet: {"devid":"xxx", "time":"xxx", "temperature":"xxx"} |
| | | * 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 |
| | | */ |
| | | extern int packet_json_pack(pack_info_t *pack_info, char *pack_buf, int size); |
| | | |
| | | |
| | | #endif /* ----- #ifndef _PACKET_H_ ----- */ |