From 8fce4645f96514daeea47e361c881b8ba8c11439 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 13 Nov 2009 12:49:19 +0000 Subject: options parser for new serialclient added serialclient to Makefile diff --git a/Makefile b/Makefile index 1686450..cee4877 100644 --- a/Makefile +++ b/Makefile @@ -23,24 +23,29 @@ ifneq ($(MAKECMDGOALS),distclean) include include.mk endif -EXECUTABLE := switchctl +EXE_SWITCHCTL := switchctl +EXE_SERIALCLIENT := serialclient -OBJ := log.o \ - sig_handler.o \ - options.o \ - string_list.o \ - key_value_storage.o \ - command_queue.o \ - client_list.o \ - utils.o \ - switchctl.o +COMMONOBJ := log.o \ + sig_handler.o \ + string_list.o \ + key_value_storage.o \ + utils.o \ +SWITCHCTLOBJ := command_queue.o \ + client_list.o \ + opt-switchctl.o \ + switchctl.o -SRC := $(OBJ:%.o=%.c) +SERIALCLIENTOBJ := opt-serialclient.o \ + serialclient.o + + +SRC := $(COMMONOBJ:%.o=%.c) $(SWITCHCTLOBJ:%.o=%.c) $(SERIALCLIENTOBJ:%.o=%.c) options.c .PHONY: clean distclean -all: $(EXECUTABLE) +all: $(EXE_SWITCHCTL) $(EXE_SERIALCLIENT) %.d: %.c @set -e; rm -f $@; \ @@ -52,8 +57,17 @@ ifneq ($(MAKECMDGOALS),distclean) -include $(SRC:%.c=%.d) endif -$(EXECUTABLE): $(OBJ) - $(CC) $(OBJ) -o $@ $(LDFLAGS) +$(EXE_SWITCHCTL): $(COMMONOBJ) $(SWITCHCTLOBJ) + $(CC) $(COMMONOBJ) $(SWITCHCTLOBJ) -o $@ $(LDFLAGS) + +$(EXE_SERIALCLIENT): $(COMMONOBJ) $(SERIALCLIENTOBJ) + $(CC) $(COMMONOBJ) $(SERIALCLIENTOBJ) -o $@ $(LDFLAGS) + +opt-switchctl.o: options.c + $(CC) $(CFLAGS) -DOPT_SWITCHCTL -o $@ -c $< + +opt-serialclient.o: options.c + $(CC) $(CFLAGS) -DOPT_SERIALCLIENT -o $@ -c $< %.o: %.c $(CC) $(CFLAGS) -c $< @@ -68,5 +82,5 @@ clean: rm -f *.o rm -f *.d rm -f *.d.* - rm -f $(EXECUTABLE) - + rm -f $(EXE_SWITCHCTL) + rm -f $(EXE_SERIALCLIENT) diff --git a/options.c b/options.c index 839909e..5ee3e50 100644 --- a/options.c +++ b/options.c @@ -175,11 +175,17 @@ int options_parse(options_t* opt, int argc, char* argv[]) PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_) PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_) PARSE_STRING_LIST("-L","--log", opt->log_targets_) - PARSE_STRING_PARAM("-f","--config", opt->conf_file_) PARSE_STRING_PARAM("-s","--socket", opt->command_sock_) +#ifdef OPT_SWITCHCTL + PARSE_STRING_PARAM("-f","--config", opt->conf_file_) PARSE_STRING_PARAM("-d","--device", opt->switch_dev_) PARSE_STRING_PARAM("-m","--mode", mode) PARSE_STRING_PARAM("-c","--channel", channel) +#endif +#ifdef OPT_SERIALCLIENT + PARSE_STRING_PARAM("-d","--device", opt->serial_dev_) + PARSE_STRING_PARAM("-t","--type", opt->type_) +#endif else return i; } @@ -217,6 +223,7 @@ int options_parse_post(options_t* opt) return -1; // nothing to do +#ifdef OPT_SWITCHCTL FILE* conf_file = fopen(opt->conf_file_, "r"); if(conf_file) { char buf[100]; @@ -242,6 +249,7 @@ int options_parse_post(options_t* opt) log_printf(ERROR,"unable to open conf file (%s): %s", opt->conf_file_, strerror(errno)); return -1; } +#endif return 0; } @@ -251,6 +259,7 @@ void options_default(options_t* opt) if(!opt) return; +/* common */ opt->progname_ = strdup("rhctl"); opt->daemonize_ = 1; opt->username_ = NULL; @@ -259,12 +268,18 @@ void options_default(options_t* opt) opt->pid_file_ = NULL; string_list_init(&opt->log_targets_); + opt->command_sock_ = strdup("/var/run/rhctl/switchctl.sock"); + +/* switchctl */ opt->mode_ = MODE_MASTER; opt->channel_ = CHAN_MAIN; opt->conf_file_ = strdup("/etc/rhctl/switchctl.conf"); - opt->command_sock_ = strdup("/var/run/rhctl/switchctl.sock"); opt->switch_dev_ = strdup("/dev/audioswitch"); key_value_storage_init(&opt->alias_table_); + +/* serialclient */ + opt->serial_dev_ = strdup("/dev/ttyUSB0"); + opt->type_ = NULL; } void options_clear(options_t* opt) @@ -272,6 +287,7 @@ void options_clear(options_t* opt) if(!opt) return; +/* common */ if(opt->progname_) free(opt->progname_); if(opt->username_) @@ -284,13 +300,21 @@ void options_clear(options_t* opt) free(opt->pid_file_); string_list_clear(&opt->log_targets_); - if(opt->conf_file_) - free(opt->conf_file_); if(opt->command_sock_) free(opt->command_sock_); + +/* switchctl */ + if(opt->conf_file_) + free(opt->conf_file_); if(opt->switch_dev_) free(opt->switch_dev_); key_value_storage_clear(&opt->alias_table_); + +/* serialclient */ + if(opt->serial_dev_) + free(opt->serial_dev_); + if(opt->type_) + free(opt->type_); } void options_print_usage() @@ -304,11 +328,17 @@ void options_print_usage() printf(" [-P|--write-pid] write pid to this file\n"); printf(" [-L|--log] :[,[,..]]\n"); printf(" add a log target, can be invoked several times\n"); - printf(" [-f|--config] the configuration file e.g. /etc/rhctl/switchctl.conf\n"); printf(" [-s|--command-sock] the command socket e.g. /var/run/rhctl/switchctl.sock\n"); - printf(" [-d|--device] the tty the audio switch is connected to\n"); - printf(" [-m|--mode] the initial mode to use\n"); - printf(" [-c|--channel] the initial channel to use\n"); +#ifdef OPT_SWITCHCTL + printf(" [-f|--config] the configuration file e.g. /etc/rhctl/switchctl.conf\n"); + printf(" [-d|--device] the tty the audio switch is connected to e.g. /dev/audioswitch\n"); + printf(" [-m|--mode] the initial mode to use e.g. master\n"); + printf(" [-c|--channel] the initial channel to use e.g. main\n"); +#endif +#ifdef OPT_SERIALCLIENT + printf(" [-d|--device] the tty to connect to e.g. /dev/ttyUSB0\n"); + printf(" [-t|--type] use this client type\n"); +#endif } void options_print(options_t* opt) @@ -325,11 +355,19 @@ void options_print(options_t* opt) printf("log_targets: \n"); string_list_print(&opt->log_targets_, " '", "'\n"); + printf("command_sock: '%s'\n", opt->command_sock_); + +#ifdef OPT_SWITCHCTL printf("mode: '%s'\n", opt->mode_ == MODE_MASTER ? "master" : "standby"); printf("channel: '%s'\n", opt->channel_ == CHAN_MAIN ? "main" : "music"); printf("conf_file: '%s'\n", opt->conf_file_); - printf("command_sock: '%s'\n", opt->command_sock_); printf("switch_dev: '%s'\n", opt->switch_dev_); printf("alias_table: \n"); key_value_storage_print(&opt->alias_table_, " '", "' -> '", "'\n"); +#endif + +#ifdef OPT_SERIALCLIENT + printf("serial_dev: '%s'\n", opt->serial_dev_); + printf("type: '%s'\n", opt->type_); +#endif } diff --git a/options.h b/options.h index 4be144a..88a786d 100644 --- a/options.h +++ b/options.h @@ -32,6 +32,7 @@ enum channel_enum { CHAN_MAIN, CHAN_MUSIC }; typedef enum channel_enum channel_t; struct options_struct { +/* common */ char* progname_; int daemonize_; char* username_; @@ -40,12 +41,18 @@ struct options_struct { char* pid_file_; string_list_t log_targets_; + char* command_sock_; + +/* switchctl */ mode_t mode_; channel_t channel_; char* conf_file_; - char* command_sock_; char* switch_dev_; key_value_storage_t alias_table_; + +/* serialclient */ + char* serial_dev_; + char* type_; }; typedef struct options_struct options_t; diff --git a/serialclient.c b/serialclient.c index 1e5adb8..f374774 100644 --- a/serialclient.c +++ b/serialclient.c @@ -33,6 +33,10 @@ #include "daemon.h" #include "utils.h" +int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) +{ +} + int process_data(int src_fd, int dest_fd) { char* buffer[100]; @@ -80,7 +84,7 @@ int main_loop(int serial_fd, int cmd_fd, options_t* opt) int return_value = 0; - return_value = send_string(cmd_fd, opt->type); + return_value = send_string(cmd_fd, opt->type_); while(!return_value) { memcpy(&tmpfds, &readfds, sizeof(tmpfds)); @@ -207,7 +211,7 @@ int main(int argc, char* argv[]) fclose(pid_file); } - int cmd_fd = init_command_socket(opt.command_sock_); + int cmd_fd = connect_command_socket(opt.command_sock_); if(cmd_fd < 0) { options_clear(&opt); log_close(); @@ -224,7 +228,7 @@ int main(int argc, char* argv[]) if(ret) ret = 2; else - ret = main_loop(serial_fd, cmd_listen_fd, &opt); + ret = main_loop(serial_fd, cmd_fd, &opt); } if(ret == 2) { @@ -237,7 +241,7 @@ int main(int argc, char* argv[]) break; } - close(cmd_listen_fd); + close(cmd_fd); if(serial_fd > 0) close(serial_fd); diff --git a/utils.c b/utils.c index 6e786cf..f2ca2a7 100644 --- a/utils.c +++ b/utils.c @@ -26,8 +26,6 @@ #include #include -#include "command_queue.h" -#include "client_list.h" #include "log.h" #include "utils.h" @@ -66,6 +64,11 @@ int init_command_socket(const char* path) return fd; } +int connect_command_socket(const char* path) +{ + return -1; +} + int send_string(int fd, const char* string) { int len = strlen(string); diff --git a/utils.h b/utils.h index 39dae46..4c57ae7 100644 --- a/utils.h +++ b/utils.h @@ -22,10 +22,13 @@ #ifndef RHCTL_utils_h_INCLUDED #define RHCTL_utils_h_INCLUDED +#include "command_queue.h" +#include "client_list.h" #include "options.h" #include int init_command_socket(const char* path); +int connect_command_socket(const char* path); int send_string(int fd, const char* string); int nonblock_recvline(read_buffer_t* buffer, int fd, cmd_t** cmd_q, client_t* client_lst, options_t* opt); int setup_tty(int fd, speed_t speed); -- cgit v0.10.2