diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2005-10-27 00:28:36 +0000 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2005-10-27 00:28:36 +0000 |
commit | 7a58f1b38a86d30477048e84d979d627ebfec10b (patch) | |
tree | 539e64a957038d5bcce9aad8fd01c95108bd36ee /hcid | |
parent | 5b06ecd76bbb760cdee2e7b9e36cc3202db2e5db (diff) |
Add initial D-Bus device configuration support
Diffstat (limited to 'hcid')
-rw-r--r-- | hcid/dbus.c | 133 | ||||
-rw-r--r-- | hcid/hcid.h | 2 | ||||
-rw-r--r-- | hcid/main.c | 13 |
3 files changed, 85 insertions, 63 deletions
diff --git a/hcid/dbus.c b/hcid/dbus.c index ce6f4e70..672e1263 100644 --- a/hcid/dbus.c +++ b/hcid/dbus.c @@ -52,7 +52,7 @@ #include "dbus.h" static DBusConnection *connection; -static int num_adapters = 0; +static int up_adapters = 0; #define TIMEOUT (30 * 1000) /* 30 seconds */ #define BLUETOOTH_DEVICE_NAME_LEN (18) @@ -359,6 +359,39 @@ static void free_pin_req(void *req) free(req); } +static gboolean register_dbus_path(char *path, uint16_t id) +{ + struct hci_dbus_data *data; + syslog(LOG_INFO,"Registering DBUS Path: %s", path); + data = malloc(sizeof(struct hci_dbus_data)); + if (data == NULL) { + syslog(LOG_ERR,"Failed to alloc memory to DBUS path register data (%s)", path); + return FALSE; + } + data->id = id; + + if (!dbus_connection_register_object_path(connection, path, &obj_vtable, data)) { + syslog(LOG_ERR,"DBUS failed to register %s object", path); + free(data); + return FALSE; + } + return TRUE; +} + +static gboolean unregister_dbus_path(char *path) +{ + void *data; + syslog(LOG_INFO,"Unregistering DBUS Path: %s", path); + if (dbus_connection_get_object_path_data(connection, path, &data) && data) + free(data); + + if (!dbus_connection_unregister_object_path (connection, path)) { + syslog(LOG_ERR,"DBUS failed to unregister %s object", path); + return FALSE; + } + return TRUE; +} + void hcid_dbus_request_pin(int dev, struct hci_conn_info *ci) { DBusMessage *message; @@ -739,7 +772,7 @@ gboolean hcid_dbus_init(void) data->id = DEVICE_PATH_ID; - if (!dbus_connection_register_object_path(connection, DEVICE_PATH, + if (!dbus_connection_register_fallback(connection, DEVICE_PATH, &obj_vtable, data)) { syslog(LOG_ERR, "Can't register %s object", DEVICE_PATH); return FALSE; @@ -855,7 +888,32 @@ void hcid_dbus_exit(void) } } -gboolean hcid_dbus_register_device(uint16_t id) +gboolean hcid_dbus_register_device(uint16_t id) +{ + char path[MAX_PATH_LENGTH+1]; + char dev[BLUETOOTH_DEVICE_NAME_LEN+1]; + const char *pdev = dev; + + snprintf(dev, BLUETOOTH_DEVICE_NAME_LEN, HCI_DEVICE_NAME "%d", id); + snprintf(path, MAX_PATH_LENGTH, "%s/%s", DEVICE_PATH, pdev); + + /* register the default path*/ + return register_dbus_path(path, id); +} + +gboolean hcid_dbus_unregister_device(uint16_t id) +{ + char dev[BLUETOOTH_DEVICE_NAME_LEN+1]; + char path[MAX_PATH_LENGTH+1]; + const char *pdev = dev; + + snprintf(dev, BLUETOOTH_DEVICE_NAME_LEN, HCI_DEVICE_NAME "%d", id); + snprintf(path, MAX_PATH_LENGTH, "%s/%s", DEVICE_PATH, pdev); + + return unregister_dbus_path(path); +} + +gboolean hcid_dbus_register_manager(uint16_t id) { char dev[BLUETOOTH_DEVICE_NAME_LEN]; struct profile_obj_path_data *ptr = obj_path_table; @@ -873,7 +931,7 @@ gboolean hcid_dbus_register_device(uint16_t id) } if (!ret) - num_adapters++; + up_adapters++; message = dbus_message_new_signal(BLUEZ_HCI_PATH, BLUEZ_HCI_INTERFACE, BLUEZ_HCI_DEV_ADDED); @@ -883,7 +941,7 @@ gboolean hcid_dbus_register_device(uint16_t id) goto failed; } - sprintf(dev, "hci%d", id); + sprintf(dev, HCI_DEVICE_NAME "%d", id); dbus_message_iter_init_append(message, &iter); dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING ,&pdev); @@ -904,7 +962,7 @@ failed: return TRUE; } -gboolean hcid_dbus_unregister_device(uint16_t id) +gboolean hcid_dbus_unregister_manager(uint16_t id) { char dev[BLUETOOTH_DEVICE_NAME_LEN]; struct profile_obj_path_data *ptr = obj_path_table; @@ -917,8 +975,8 @@ gboolean hcid_dbus_unregister_device(uint16_t id) return FALSE; for (; ptr->name; ptr++) { - dft_unreg = (num_adapters > 1) ? 0 : 1; - num_adapters--; + dft_unreg = (up_adapters > 1) ? 0 : 1; + up_adapters--; ptr->unreg_func(connection, dft_unreg, id); if (dft_unreg ) @@ -933,7 +991,7 @@ gboolean hcid_dbus_unregister_device(uint16_t id) goto failed; } - sprintf(dev, "hci%d", id); + sprintf(dev, HCI_DEVICE_NAME "%d", id); dbus_message_iter_init_append(message, &iter); dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING ,&pdev); @@ -965,39 +1023,17 @@ failed: */ static int hci_dbus_reg_obj_path(DBusConnection *conn, int dft_reg, uint16_t id) { - struct hci_dbus_data *data; char path[MAX_PATH_LENGTH]; /* register the default path*/ if (!dft_reg) { - sprintf(path, "%s/%s/%s", MANAGER_PATH, HCI_DEFAULT_DEVICE_NAME, BLUEZ_HCI); - - data = malloc(sizeof(struct hci_dbus_data)); - if (data == NULL) - return -1; - - data->id = DEFAULT_DEVICE_PATH_ID; - - if (!dbus_connection_register_object_path(conn, path, &obj_vtable, data)) { - syslog(LOG_ERR,"DBUS failed to register %s object", path); - /* ignore, the default path was already registered */ - } + register_dbus_path(path, DEFAULT_DEVICE_PATH_ID); } - data = malloc(sizeof(struct hci_dbus_data)); - if (data == NULL) - return -1; - - data->id = id; - /* register the default path*/ sprintf(path, "%s/%s%d/%s", MANAGER_PATH, HCI_DEVICE_NAME, id, BLUEZ_HCI); - - if (!dbus_connection_register_object_path(conn, path, &obj_vtable, data)) { - syslog(LOG_ERR,"DBUS failed to register %s object", path); - /* ignore, the path was already registered */ - } + register_dbus_path(path, id); return 0; } @@ -1016,39 +1052,14 @@ static int hci_dbus_unreg_obj_path(DBusConnection *conn, int unreg_dft, uint16_t { int ret = 0; char path[MAX_PATH_LENGTH]; - char dft_path[MAX_PATH_LENGTH]; - void *data = NULL; if (unreg_dft) { - - sprintf(dft_path, "%s/%s/%s", MANAGER_PATH, HCI_DEFAULT_DEVICE_NAME, BLUEZ_HCI); - - if (!dbus_connection_unregister_object_path (connection, dft_path)) { - syslog(LOG_ERR,"DBUS failed to unregister %s object", dft_path); - ret = -1; - } else { - if (dbus_connection_get_object_path_data(conn, dft_path, &data)) { - if (data) { - free(data); - data = NULL; - } - } - } + sprintf(path, "%s/%s/%s", MANAGER_PATH, HCI_DEFAULT_DEVICE_NAME, BLUEZ_HCI); + unregister_dbus_path(path); } sprintf(path, "%s/%s%d/%s", MANAGER_PATH, HCI_DEVICE_NAME, id, BLUEZ_HCI); - - if (!dbus_connection_unregister_object_path (connection, path)) { - syslog(LOG_ERR,"DBUS failed to unregister %s object", path); - ret = -1; - } else { - if (dbus_connection_get_object_path_data(conn, path, &data)) { - if (data) { - free(data); - data = NULL; - } - } - } + unregister_dbus_path(path); return ret; } diff --git a/hcid/hcid.h b/hcid/hcid.h index 048a0aed..0e5a673e 100644 --- a/hcid/hcid.h +++ b/hcid/hcid.h @@ -126,7 +126,9 @@ void toggle_pairing(int enable); gboolean hcid_dbus_init(void); void hcid_dbus_exit(void); gboolean hcid_dbus_register_device(uint16_t id); +gboolean hcid_dbus_register_manager(uint16_t id); gboolean hcid_dbus_unregister_device(uint16_t id); +gboolean hcid_dbus_unregister_manager(uint16_t id); void hcid_dbus_request_pin(int dev, struct hci_conn_info *ci); void hcid_dbus_inquiry_start(bdaddr_t *local); void hcid_dbus_inquiry_complete(bdaddr_t *local); diff --git a/hcid/main.c b/hcid/main.c index b6891ee4..d3d285da 100644 --- a/hcid/main.c +++ b/hcid/main.c @@ -377,6 +377,9 @@ static void init_all_devices(int ctl) start_security_manager(dr->dev_id); #ifdef ENABLE_DBUS + if (hci_test_bit(HCI_UP, &dr->dev_opt)) + hcid_dbus_register_manager(dr->dev_id); + hcid_dbus_register_device(dr->dev_id); #endif } @@ -430,10 +433,16 @@ static inline void device_event(GIOChannel *chan, evt_stack_internal *si) syslog(LOG_INFO, "HCI dev %d registered", sd->dev_id); if (hcid.auto_init) init_device(sd->dev_id); +#ifdef ENABLE_DBUS + hcid_dbus_register_device(sd->dev_id); +#endif break; case HCI_DEV_UNREG: syslog(LOG_INFO, "HCI dev %d unregistered", sd->dev_id); +#ifdef ENABLE_DBUS + hcid_dbus_unregister_device(sd->dev_id); +#endif break; case HCI_DEV_UP: @@ -443,7 +452,7 @@ static inline void device_event(GIOChannel *chan, evt_stack_internal *si) if (hcid.security) start_security_manager(sd->dev_id); #ifdef ENABLE_DBUS - hcid_dbus_register_device(sd->dev_id); + hcid_dbus_register_manager(sd->dev_id); #endif break; @@ -452,7 +461,7 @@ static inline void device_event(GIOChannel *chan, evt_stack_internal *si) if (hcid.security) stop_security_manager(sd->dev_id); #ifdef ENABLE_DBUS - hcid_dbus_unregister_device(sd->dev_id); + hcid_dbus_unregister_manager(sd->dev_id); #endif break; } |