diff options
author | Johan Hedberg <johan.hedberg@nokia.com> | 2008-08-05 21:11:18 +0200 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2008-08-05 21:11:18 +0200 |
commit | a32da554cb16e4fe676705edd16eea0bf89a2f0b (patch) | |
tree | 67e74e542eb124e764595fb7014900996bec2075 | |
parent | 515eb3abe9771d5509a84df57ae8ecb17e0254b4 (diff) |
Add support for main.conf configuration file
-rw-r--r-- | src/hcid.h | 6 | ||||
-rw-r--r-- | src/main.c | 111 | ||||
-rw-r--r-- | src/parser.y | 29 | ||||
-rw-r--r-- | src/security.c | 43 |
4 files changed, 90 insertions, 99 deletions
@@ -101,8 +101,6 @@ struct hcid_opts { int offmode; char deviceid[15]; - char *config_file; - uint8_t pin_code[16]; int pin_len; @@ -141,18 +139,14 @@ void hci_req_queue_remove(int dev_id, bdaddr_t *dba); #define HCID_OFFMODE_DEVDOWN 0 #define HCID_OFFMODE_NOSCAN 1 -int read_config(char *file); - struct device_opts *alloc_device_opts(char *ref); uint8_t get_startup_scan(int hdev); uint8_t get_startup_mode(int hdev); int get_discoverable_timeout(int dev_id); -void init_security_data(void); void start_security_manager(int hdev); void stop_security_manager(int hdev); -void toggle_pairing(int enable); void set_pin_length(bdaddr_t *sba, int length); @@ -60,7 +60,6 @@ struct hcid_opts hcid; struct device_opts default_device; -struct device_opts *parser_device; static struct device_list *device_list = NULL; static int child_pipe[2]; @@ -83,6 +82,84 @@ static GKeyFile *load_config(const char *file) return keyfile; } +static void parse_config(GKeyFile *config) +{ + GError *err = NULL; + char *str; + int val; + + if (!config) + return; + + debug("parsing main.conf"); + + str = g_key_file_get_string(config, "General", + "OffMode", &err); + if (err) { + debug("%s", err->message); + g_error_free(err); + err = NULL; + } else { + debug("offmode=%s", str); + if (g_str_equal(str, "DevDown")) + hcid.offmode = HCID_OFFMODE_DEVDOWN; + g_free(str); + } + + val = g_key_file_get_integer(config, "General", + "DiscoverableTimeout", + &err); + if (err) { + debug("%s", err->message); + g_error_free(err); + err = NULL; + } else { + debug("discovto=%d", val); + default_device.discovto = val; + default_device.flags |= 1 << HCID_SET_DISCOVTO; + } + + val = g_key_file_get_integer(config, "General", + "PageTimeout", + &err); + if (err) { + debug("%s", err->message); + g_error_free(err); + err = NULL; + } else { + debug("pageto=%d", val); + default_device.pageto = val; + default_device.flags |= 1 << HCID_SET_PAGETO; + } + + str = g_key_file_get_string(config, "General", + "Name", &err); + if (err) { + debug("%s", err->message); + g_error_free(err); + err = NULL; + } else { + debug("name=%s", str); + g_free(default_device.name); + default_device.name = g_strdup(str); + default_device.flags |= 1 << HCID_SET_NAME; + g_free(str); + } + + str = g_key_file_get_string(config, "General", + "Class", &err); + if (err) { + debug("%s", err->message); + g_error_free(err); + err = NULL; + } else { + debug("class=%s", str); + default_device.class = strtol(str, NULL, 16); + default_device.flags |= 1 << HCID_SET_CLASS; + g_free(str); + } +} + static inline void init_device_defaults(struct device_opts *device_opts) { memset(device_opts, 0, sizeof(*device_opts)); @@ -751,22 +828,6 @@ static void sig_term(int sig) g_main_loop_quit(event_loop); } -static void sig_hup(int sig) -{ - info("Reloading config file"); - - free_device_opts(); - - init_defaults(); - - if (read_config(hcid.config_file) < 0) - error("Config reload failed"); - - init_security_data(); - - init_all_devices(hcid.sock); -} - static void sig_debug(int sig) { toggle_debug(); @@ -776,7 +837,7 @@ static void usage(void) { printf("Bluetooth daemon ver %s\n\n", VERSION); printf("Usage:\n"); - printf("\tbluetoothd [-n] [-d] [-m mtu] [-f config file]\n"); + printf("\tbluetoothd [-n] [-d] [-m mtu]\n"); } int main(int argc, char *argv[]) @@ -792,7 +853,6 @@ int main(int argc, char *argv[]) /* Default HCId settings */ memset(&hcid, 0, sizeof(hcid)); hcid.auto_init = 1; - hcid.config_file = HCID_CONFIG_FILE; hcid.security = HCID_SEC_AUTO; hcid.pairing = HCID_PAIRING_MULTI; hcid.offmode = HCID_OFFMODE_NOSCAN; @@ -805,7 +865,7 @@ int main(int argc, char *argv[]) init_defaults(); - while ((opt = getopt(argc, argv, "ndm:f:")) != EOF) { + while ((opt = getopt(argc, argv, "ndm:")) != EOF) { switch (opt) { case 'n': daemonize = 0; @@ -819,10 +879,6 @@ int main(int argc, char *argv[]) mtu = atoi(optarg); break; - case 'f': - hcid.config_file = g_strdup(optarg); - break; - default: usage(); exit(1); @@ -843,8 +899,6 @@ int main(int argc, char *argv[]) sa.sa_handler = sig_term; sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); - sa.sa_handler = sig_hup; - sigaction(SIGHUP, &sa, NULL); sa.sa_handler = sig_debug; sigaction(SIGUSR2, &sa, NULL); @@ -884,8 +938,7 @@ int main(int argc, char *argv[]) config = load_config(CONFIGDIR "/main.conf"); - if (read_config(hcid.config_file) < 0) - error("Config load failed"); + parse_config(config); if (pipe(child_pipe) < 0) { error("pipe(): %s (%d)", strerror(errno), errno); @@ -915,8 +968,6 @@ int main(int argc, char *argv[]) * daemon needs to be re-worked. */ plugin_init(config); - init_security_data(); - event_loop = g_main_loop_new(NULL, FALSE); ctl_io = g_io_channel_unix_new(hcid.sock); diff --git a/src/parser.y b/src/parser.y index c8b9a12d..c2978612 100644 --- a/src/parser.y +++ b/src/parser.y @@ -91,15 +91,12 @@ statement: device: K_DEVICE { - parser_device = &default_device; } | K_DEVICE hci { - parser_device = alloc_device_opts($2); } | K_DEVICE bdaddr { - parser_device = alloc_device_opts($2); } ; @@ -185,59 +182,33 @@ device_options: '{' device_opts '}'; device_opts: | device_opt ';' | error ';' | device_opts device_opt ';'; device_opt: K_PTYPE pkt_type { - parser_device->flags |= (1 << HCID_SET_PTYPE); - parser_device->pkt_type = $2; } | K_LM link_mode { - parser_device->flags |= (1 << HCID_SET_LM); - parser_device->link_mode = $2; } | K_LP link_policy { - parser_device->flags |= (1 << HCID_SET_LP); - parser_device->link_policy = $2; } | K_NAME dev_name { - if (parser_device->name) - g_free(parser_device->name); - parser_device->flags |= (1 << HCID_SET_NAME); - parser_device->name = g_strdup($2); } | K_CLASS NUM { - parser_device->flags |= (1 << HCID_SET_CLASS); - parser_device->class = $2; } | K_VOICE NUM { - parser_device->flags |= (1 << HCID_SET_VOICE); - parser_device->voice = $2; } | K_PAGETO NUM { - parser_device->flags |= (1 << HCID_SET_PAGETO); - parser_device->pageto = $2; } | K_DISCOVTO NUM { - parser_device->flags |= (1 << HCID_SET_DISCOVTO); - parser_device->discovto = $2; } | K_ISCAN bool { - if ($2) - parser_device->scan |= SCAN_INQUIRY; - else - parser_device->scan &= ~SCAN_INQUIRY; } | K_PSCAN bool { - if ($2) - parser_device->scan |= SCAN_PAGE; - else - parser_device->scan &= ~SCAN_PAGE; } | WORD { diff --git a/src/security.c b/src/security.c index 0bfbcbec..20739065 100644 --- a/src/security.c +++ b/src/security.c @@ -62,8 +62,6 @@ struct g_io_info { static struct g_io_info io_data[HCI_MAX_DEV]; -static int pairing = HCID_PAIRING_MULTI; - static GSList *hci_req_queue = NULL; struct hci_req_data *hci_req_data_new(int dev_id, const bdaddr_t *dba, uint16_t ogf, uint16_t ocf, int event, const void *cparam, int clen) @@ -467,9 +465,8 @@ static void pin_code_request(int dev, bdaddr_t *sba, bdaddr_t *dba) pin_code_reply_cp pr; struct hci_conn_info_req *cr; struct hci_conn_info *ci; - unsigned char key[16]; char sa[18], da[18], pin[17]; - int err, pinlen; + int pinlen; memset(&pr, 0, sizeof(pr)); bacpy(&pr.bdaddr, dba); @@ -490,34 +487,16 @@ static void pin_code_request(int dev, bdaddr_t *sba, bdaddr_t *dba) memset(pin, 0, sizeof(pin)); pinlen = read_pin_code(sba, dba, pin); - if (pairing == HCID_PAIRING_ONCE) { - err = read_link_key(sba, dba, key, NULL); - if (!err) { - ba2str(dba, da); - error("PIN code request for already paired device %s", da); - goto reject; - } - } else if (pairing == HCID_PAIRING_NONE) - goto reject; - - if (hcid.security == HCID_SEC_AUTO && !ci->out) { - set_pin_length(sba, hcid.pin_len); - memcpy(pr.pin_code, hcid.pin_code, hcid.pin_len); - pr.pin_len = hcid.pin_len; + if (pinlen > 0) { + set_pin_length(sba, pinlen); + memcpy(pr.pin_code, pin, pinlen); + pr.pin_len = pinlen; hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_REPLY, - PIN_CODE_REPLY_CP_SIZE, &pr); + PIN_CODE_REPLY_CP_SIZE, &pr); } else { - if (pinlen > 0) { - set_pin_length(sba, pinlen); - memcpy(pr.pin_code, pin, pinlen); - pr.pin_len = pinlen; - hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_REPLY, - PIN_CODE_REPLY_CP_SIZE, &pr); - } else { - /* Request PIN from passkey agent */ - if (hcid_dbus_request_pin(dev, sba, ci) < 0) - goto reject; - } + /* Request PIN from passkey agent */ + if (hcid_dbus_request_pin(dev, sba, ci) < 0) + goto reject; } g_free(cr); @@ -1031,7 +1010,3 @@ void stop_security_manager(int hdev) io_data[hdev].pin_length = -1; } -void init_security_data(void) -{ - pairing = hcid.pairing; -} |