Merge branch 'master' of ssh://master.iot-yun.club:2280/raspberrypi
| | |
| | | |
| | | void print_usage(char *progname) |
| | | { |
| | | printf("%s usage: \n", progname); |
| | | printf("-i(--ipaddr) : sepcify server IP address\n"); |
| | | printf("-p(--port) : sepcify server port.\n"); |
| | | printf("-I(--interval): sepcify report interval time\n"); |
| | | printf("-s(--sn) : sepcify device serial number\n"); |
| | | printf("-d(--debug) : run as debug mode\n"); |
| | | printf("-h(--Help) : print this help information.\n"); |
| | | printf("%s usage: \n", progname); |
| | | printf("-i(--ipaddr) : sepcify server IP address\n"); |
| | | printf("-p(--port) : sepcify server port.\n"); |
| | | printf("-I(--interval): sepcify report interval time\n"); |
| | | printf("-s(--sn) : sepcify device serial number\n"); |
| | | printf("-d(--debug) : run as debug mode\n"); |
| | | printf("-h(--Help) : print this help information.\n"); |
| | | |
| | | return ; |
| | | return ; |
| | | } |
| | | |
| | | static int check_sample_time(time_t *last_time, int interval); |
| | | |
| | | int main(int argc, char **argv) |
| | | { |
| | | int rv = -1; |
| | | int port; |
| | | char *serverip; |
| | | int interval = 30; /* default report termperature every 30 seconds */ |
| | | int sn = 1; /* default serial number for device ID */ |
| | | int debug = 0; /* running in debug mode or not */ |
| | | int loglevel = LOG_LEVEL_INFO; |
| | | char *logfile="client.log"; |
| | | int rv = -1; |
| | | int port; |
| | | char *serverip; |
| | | int interval = 30; /* default report termperature every 30 seconds */ |
| | | int sn = 1; /* default serial number for device ID */ |
| | | int debug = 0; /* running in debug mode or not */ |
| | | int loglevel = LOG_LEVEL_INFO; |
| | | char *logfile="client.log"; |
| | | |
| | | socket_ctx_t sock; |
| | | time_t last_time = 0; |
| | | int sample_flag = 0; |
| | | socket_ctx_t sock; |
| | | time_t last_time = 0; |
| | | int sample_flag = 0; |
| | | |
| | | char pack_buf[1024]; |
| | | int pack_bytes; |
| | | pack_info_t pack_info; |
| | | pack_proc_t pack_proc = packet_string_pack; /* use string packet */ |
| | | char pack_buf[1024]; |
| | | int pack_bytes; |
| | | pack_info_t pack_info; |
| | | pack_proc_t pack_proc = packet_segmented_pack; /* use string packet */ |
| | | |
| | | struct option opts[] = { |
| | | {"ipaddr", required_argument, NULL, 'i'}, |
| | | {"port", required_argument, NULL, 'p'}, |
| | | {"interval", no_argument, NULL, 'I'}, |
| | | {"debug", no_argument, NULL, 'd'}, |
| | | {"sn", required_argument, NULL, 's'}, |
| | | {"help", no_argument, NULL, 'h'}, |
| | | {NULL, 0, NULL, 0} |
| | | }; |
| | | struct option opts[] = { |
| | | {"ipaddr", required_argument, NULL, 'i'}, |
| | | {"port", required_argument, NULL, 'p'}, |
| | | {"interval", no_argument, NULL, 'I'}, |
| | | {"debug", no_argument, NULL, 'd'}, |
| | | {"sn", required_argument, NULL, 's'}, |
| | | {"help", no_argument, NULL, 'h'}, |
| | | {NULL, 0, NULL, 0} |
| | | }; |
| | | |
| | | while( (rv=getopt_long(argc, argv, "i:p:dI:s:h", opts, NULL)) != -1 ) |
| | | { |
| | | switch(rv) |
| | | { |
| | | case 'i': |
| | | serverip=optarg; |
| | | break; |
| | | while( (rv=getopt_long(argc, argv, "i:p:dI:s:h", opts, NULL)) != -1 ) |
| | | { |
| | | switch(rv) |
| | | { |
| | | case 'i': |
| | | serverip=optarg; |
| | | break; |
| | | |
| | | case 'p': |
| | | port=atoi(optarg); |
| | | break; |
| | | case 'p': |
| | | port=atoi(optarg); |
| | | break; |
| | | |
| | | case 'd': |
| | | debug=1; |
| | | case 'd': |
| | | debug=1; |
| | | logfile="stdout"; |
| | | loglevel=LOG_LEVEL_DEBUG; |
| | | break; |
| | | break; |
| | | |
| | | case 'I': |
| | | interval=atoi(optarg); |
| | | break; |
| | | case 'I': |
| | | interval=atoi(optarg); |
| | | break; |
| | | |
| | | case 's': |
| | | sn=atoi(optarg); |
| | | break; |
| | | case 's': |
| | | sn=atoi(optarg); |
| | | break; |
| | | |
| | | case 'h': |
| | | print_usage(argv[0]); |
| | | return 0; |
| | | } |
| | | case 'h': |
| | | print_usage(argv[0]); |
| | | return 0; |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | if( !serverip || !port ) |
| | | { |
| | | print_usage(argv[0]); |
| | | return 0; |
| | | if( !serverip || !port ) |
| | | { |
| | | print_usage(argv[0]); |
| | | return 0; |
| | | } |
| | | |
| | | /* set logger to $logfile with level info */ |
| | |
| | | return 1; |
| | | } |
| | | |
| | | if( !debug ) |
| | | { |
| | | log_info("set program running on background now.\n"); |
| | | daemon(1, 1); /* don't change work path, don't close opened file descriptor */ |
| | | } |
| | | if( !debug ) |
| | | { |
| | | log_info("set program running on background now.\n"); |
| | | daemon(1, 1); /* don't change work path, don't close opened file descriptor */ |
| | | } |
| | | |
| | | install_default_signal(); |
| | | install_default_signal(); |
| | | |
| | | log_info("program start running.\n"); |
| | | log_info("program start running.\n"); |
| | | |
| | | if( database_init("client.db") < 0 ) |
| | | { |
| | | return 2; |
| | | } |
| | | if( database_init("client.db") < 0 ) |
| | | { |
| | | return 2; |
| | | } |
| | | |
| | | socket_init(&sock, serverip, port); |
| | | socket_init(&sock, serverip, port); |
| | | |
| | | while( !g_signal.stop ) |
| | | { |
| | | /* +----------------------------------+ |
| | | * | check and sample temperature | |
| | | * +----------------------------------+*/ |
| | | while( !g_signal.stop ) |
| | | { |
| | | /* +----------------------------------+ |
| | | * | check and sample temperature | |
| | | * +----------------------------------+*/ |
| | | |
| | | sample_flag = 0; /* clear sample flag */ |
| | | sample_flag = 0; /* clear sample flag */ |
| | | |
| | | if( check_sample_time(&last_time, interval) ) |
| | | { |
| | | log_debug("start DS18B20 sample termperature\n"); |
| | | if( check_sample_time(&last_time, interval) ) |
| | | { |
| | | log_debug("start DS18B20 sample termperature\n"); |
| | | |
| | | if( (rv=ds18b20_get_temperature(&pack_info.temper)) < 0 ) |
| | | { |
| | | log_error("DS18B20 sample temperature failure, rv=%d\n", rv); |
| | | continue; |
| | | } |
| | | log_info("DS18B20 sample termperature %d.%d oC\n", |
| | | temper_integer(pack_info.temper), temper_fract(pack_info.temper)); |
| | | if( (rv=ds18b20_get_temperature(&pack_info.temper)) < 0 ) |
| | | { |
| | | log_error("DS18B20 sample temperature failure, rv=%d\n", rv); |
| | | continue; |
| | | } |
| | | log_info("DS18B20 sample termperature %d.%d oC\n", |
| | | 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_devid(pack_info.devid, DEVID_LEN, sn); |
| | | get_time(&pack_info.sample_time); |
| | | |
| | | pack_bytes = pack_proc(&pack_info, pack_buf, sizeof(pack_buf)); |
| | | sample_flag = 1; /* set sample flag */ |
| | | } |
| | | pack_bytes = pack_proc(&pack_info, pack_buf, sizeof(pack_buf)); |
| | | sample_flag = 1; /* set sample flag */ |
| | | } |
| | | |
| | | /* +---------------------------------+ |
| | | * | check and do socket connect | |
| | | * +---------------------------------+*/ |
| | | /* +---------------------------------+ |
| | | * | check and do socket connect | |
| | | * +---------------------------------+*/ |
| | | |
| | | /* start connect to server if not connected */ |
| | | if( sock.fd < 0 ) |
| | | { |
| | | socket_connect(&sock); |
| | | } |
| | | /* start connect to server if not connected */ |
| | | if( sock.fd < 0 ) |
| | | { |
| | | socket_connect(&sock); |
| | | } |
| | | |
| | | /* check socket connected or not */ |
| | | if( sock_check_connect(sock.fd) < 0 ) |
| | | { |
| | | if( sock.fd > 0 ) |
| | | { |
| | | log_error("socket got disconnected, terminate it and reconnect now.\n"); |
| | | /* check socket connected or not */ |
| | | if( sock_check_connect(sock.fd) < 0 ) |
| | | { |
| | | if( sock.fd > 0 ) |
| | | { |
| | | log_error("socket got disconnected, terminate it and reconnect now.\n"); |
| | | socket_term(&sock); /* close the soket */ |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /* +-------------------------------+ |
| | | * | socket disconnect | |
| | |
| | | msleep(100); |
| | | } |
| | | |
| | | socket_term(&sock); |
| | | database_term(); |
| | | socket_term(&sock); |
| | | database_term(); |
| | | |
| | | return 0; |
| | | return 0; |
| | | } |
| | | |
| | | int check_sample_time(time_t *last_time, int interval) |
| | | { |
| | | int need = 0; /* no need sample now */ |
| | | time_t now; |
| | | int need = 0; /* no need sample now */ |
| | | time_t now; |
| | | |
| | | time(&now); |
| | | time(&now); |
| | | |
| | | if( now >= *last_time+interval ) |
| | | { |
| | | need = 1; /* need sample now */ |
| | | *last_time = now; |
| | | } |
| | | if( now >= *last_time+interval ) |
| | | { |
| | | need = 1; /* need sample now */ |
| | | *last_time = now; |
| | | } |
| | | |
| | | return need; |
| | | return need; |
| | | } |
| | |
| | | * |
| | | * 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> |
| | |
| | | |
| | | 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); |
| | | } |
| | | |
| | |
| | | #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 */ |
| | | uint16_t temper; /* sample temperature */ |
| | | char devid[DEVID_LEN]; /* device ID */ |
| | | struct tm sample_time; /* sample time */ |
| | | uint16_t temper; /* sample temperature */ |
| | | } pack_info_t; |
| | | |
| | | /* packet function pointer type */ |
| | |
| | | */ |
| | | 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_ ----- */ |