Add json support in packet.c
| | |
| | | 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 ) |
| | | { |
| | |
| | | return -1; |
| | | } |
| | | |
| | | memset(pack_buf, 0, size); |
| | | |
| | | snprintf(pack_buf, size, "%s|%s|%d.%d", pack_info->devid, pack_info->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) |
| | | { |
| | | if( !pack_info || !pack_buf || size<=0 ) |
| | | { |
| | | log_error("Invalid input arguments\n"); |
| | | return -1; |
| | | } |
| | | |
| | | 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)); |
| | | |
| | | return strlen(pack_buf); |
| | | } |
| | | |
| | | |
| | |
| | | * $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_ ----- */ |