#include "options.h"
#include "log.h"
+#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
{
log_printf(NOTICE, "autosample process just started");
+ int device_num = key_value_storage_length(&opt->autosampledevs_);
+ if(device_num <= 0) {
+ log_printf(WARNING, "autosample no devices to sample, exiting");
+ return 0;
+ }
+
+ autosample_device_t* devices = malloc(sizeof(autosample_device_t)*device_num);
+ if(!devices) {
+ log_printf(WARNING, "autosample memory error, exiting");
+ return -3;
+ }
+
+ int i = 0;
+ string_list_element_t* k = opt->autosampledevs_.keys_.first_;
+ string_list_element_t* v = opt->autosampledevs_.values_.first_;
+ while(k && v) {
+ devices[i].delay_ = atoi(v->string_);
+ devices[i].cnt_ = 0;
+ devices[i].device_name_ = k->string_;
+ k = k->next_;
+ v = v->next_;
+ }
+
int sig_fd = signal_init();
if(sig_fd < 0)
return -3;
if(ret == -1)
continue;
if(!ret) {
- // timout has expired...
- write(pipefd, "sample temp0", 12);
- char c = '\n';
- write(pipefd, &c, 1);
+ int i;
+ for(i = 0; i < device_num; i++) {
+ devices[i].cnt_++;
+ if(devices[i].cnt_ >= devices[i].delay_) {
+ log_printf(DEBUG, "autosample send sample command for '%s'", devices[i].device_name_);
+ // call send sample
+ devices[i].cnt_ = 0;
+ }
+ }
}
if(FD_ISSET(sig_fd, &readfds)) {
}
signal_stop();
+ free(devices);
return return_value;
}
i++; \
}
+#define PARSE_KEY_VALUE(SHORT, LONG, SEP, KV) \
+ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
+ { \
+ if(argc < 1 || argv[i+1][0] == '-') \
+ return i; \
+ char* value = strchr(argv[i+1], SEP); \
+ if(!value || value[1] == 0) \
+ return i+1; \
+ value[0] = 0; \
+ value++; \
+ if(key_value_storage_add(&KV, argv[i+1], value)) \
+ return -2; \
+ argc--; \
+ i++; \
+ }
+
int options_parse_hex_string(const char* hex, buffer_t* buffer)
{
if(!hex || !buffer)
PARSE_STRING_PARAM("-s","--socket", opt->command_sock_)
PARSE_STRING_PARAM("-p","--powerid-file", opt->powerid_file_)
PARSE_STRING_PARAM("-a","--sampledev-file", opt->sampledev_file_)
+ PARSE_STRING_PARAM("-a","--sampledev-file", opt->sampledev_file_)
+ PARSE_KEY_VALUE("-A","--autosampledev", ',', opt->autosampledevs_)
else
return i;
}
if(ret)
return ret;
}
+
+ string_list_element_t* k = opt->autosampledevs_.keys_.first_;
+ string_list_element_t* v = opt->autosampledevs_.values_.first_;
+ while(k && v) {
+ if(!key_value_storage_find(&opt->sampledevs_, k->string_)) {
+ log_printf(ERROR, "sample dev '%s' not in file '%s'", k->string_, opt->sampledev_file_);
+ return -1;
+ }
+ if(atoi(v->string_) <= 0) {
+ log_printf(ERROR, "invalid rate '%s' for sample dev '%s'", v->string_, k->string_);
+ return -1;
+ }
+ k = k->next_;
+ v = v->next_;
+ }
+
+ return 0;
}
void options_default(options_t* opt)
key_value_storage_init(&opt->powerids_);
opt->sampledev_file_ = NULL;
key_value_storage_init(&opt->sampledevs_);
+ key_value_storage_init(&opt->autosampledevs_);
}
void options_clear(options_t* opt)
if(opt->sampledev_file_)
free(opt->sampledev_file_);
key_value_storage_clear(&opt->sampledevs_);
+ key_value_storage_clear(&opt->autosampledevs_);
}
void options_print_usage()
printf(" [-s|--command-sock] <unix sock> the command socket e.g. /var/run/powersensordaemon/cmd.sock\n");
printf(" [-p|--powerid-file] <path> file that contains the power ids\n");
printf(" [-a|--sampledev-file] <path> file that contains all sample devices\n");
+ printf(" [-A|--autosampledev] <device>,<delay between samples in seconds>\n");
+ printf(" automatic sample this devices, can be invoked several times\n");
}
void options_print(options_t* opt)
printf("sampledev_file: '%s'\n", opt->sampledev_file_);
printf("sampledevs: \n");
key_value_storage_print(&opt->sampledevs_, " '", "' -> '", "'\n");
+ key_value_storage_print(&opt->autosampledevs_, " '", "' -> '", "'\n");
}