void device_scan(void) { GSList *plugins, *l; struct device_plugin *plugin; int num_devices, num_probes, i; plugins = list_hwplugins(); /* * Initialize all plugins first. Since the init() call may involve * a firmware upload and associated delay, we may as well get all * of these out of the way first. */ for (l = plugins; l; l = l->next) { plugin = l->data; g_message("initializing %s plugin", plugin->name); num_devices = plugin->init(NULL); for (i = 0; i < num_devices; i++) { num_probes = (int)(unsigned long)plugin->get_device_info(i, DI_NUM_PROBES); device_new(plugin, i, num_probes); } } }
int device_mount (struct udev_device *dev) { struct device_t *device; char cmdline[256]; char id_fmt[256]; int needs_mount_id; device = device_new(dev); if (!device) return 0; /* If the device has no media or its already mounted return OK */ if (!device->has_media || device->mounted) return 1; mkdir(device->mountpoint, 777); /* Microsoft filesystems and filesystems used on optical * discs require the gid and uid to be passed as mount * arguments to allow the user to read and write, while * posix filesystems just need a chown after being mounted */ needs_mount_id = filesystem_needs_id_fix(device->filesystem); id_fmt[0] = 0; if (needs_mount_id) sprintf(id_fmt, ID_FMT, CONFIG_USER_UID, CONFIG_USER_GID); sprintf(cmdline, MOUNT_CMD, (device->fstab_entry) ? device->fstab_entry->type : device->filesystem, id_fmt, (device->fstab_entry) ? device->fstab_entry->opts : "defaults", device->devnode, device->mountpoint); if (system(cmdline)) { syslog(LOG_ERR, "Error while executing mount"); device_unmount(dev); return 0; } if (!needs_mount_id) { if (chown(device->mountpoint, CONFIG_USER_UID, CONFIG_USER_GID)) { syslog(LOG_ERR, "Cannot chown the mountpoint"); device_unmount(dev); return 0; } } device->mounted = device_is_mounted(device->devnode); return device->mounted; }
int device_change (struct udev_device *dev) { struct device_t *device; device = device_search((char *)udev_device_get_devnode(dev)); /* Unmount the old media... */ if (device) { if (device_is_mounted(device->devnode) && !device_unmount(dev)) return 0; } /* ...and mount the new one if present */ if (!device_new(dev)) return 0; if (!device_mount(dev)) return 0; return 1; }
static const gchar *_find_device_pin(const gchar *device_path) { if (_pin_hash_table) { GError *error = NULL; Device *device = device_new(device_path); const gchar *pin_by_addr = g_hash_table_lookup(_pin_hash_table, device_get_address(device, &error)); const gchar *pin_by_alias = g_hash_table_lookup(_pin_hash_table, device_get_alias(device, &error)); if(error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); } g_object_unref(device); const gchar *pin_all = g_hash_table_lookup(_pin_hash_table, "*"); if (pin_by_addr) return pin_by_addr; else if (pin_by_alias) return pin_by_alias; else if (pin_all) return pin_all; } return NULL; }
static void* pthread_a_work(void* args) { sorm_connection_t *_conn; int ret = sorm_open(DB_FILE, SORM_DB_SQLITE, 0, &rwlock, SORM_ENABLE_RWLOCK, &_conn); assert(ret == SORM_OK); device_t *device; sorm_iterator_t *iterator; ret = device_select_iterate_by_open(_conn, ALL_COLUMNS, NULL, &iterator); sorm_begin_read_transaction(_conn); int i; for (i = 0; i < 4; i ++) { device_select_iterate_by(iterator, &device); char string[1024]; printf("select : %s\n", device_to_string(device, string, 1024)); device_free(device); } sorm_begin_write_transaction(_conn); device = device_new(); device_set_id(device, 3); device_set_uuid(device, "uuid-3"); device_set_name(device, "name-3"); device_set_password(device, "passwd-3"); ret = device_insert(_conn, device); assert(ret == SORM_OK); device_free(device); printf("insert uuid-3\n"); pthread_mutex_lock(&mutex); condition_b = 1; pthread_mutex_unlock(&mutex); pthread_cond_signal(&cond_b); printf("wait for input\n"); ret = getchar(); printf("continue running\n"); sorm_commit_transaction(_conn); while (1) { device_select_iterate_by(iterator, &device); if (!device_select_iterate_more(iterator)) { break; } char string[1024]; printf("select : %s\n", device_to_string(device, string, 1024)); device_free(device); } sorm_commit_transaction(_conn); device_select_iterate_close(iterator); //sorm_commit_transaction(_conn); printf("a finish\n"); sorm_close(_conn); }
int main() { pthread_rwlock_init(&rwlock, NULL); pthread_t p1, p2; sorm_init(0); sorm_connection_t *_conn; int ret = sorm_open(DB_FILE, SORM_DB_SQLITE, 0, &rwlock, SORM_ENABLE_RWLOCK, &_conn); ret = device_create_table(_conn); assert(ret == SORM_OK); /* insert rows for select */ int i; device_t *device; for(i = 0; i < 10; i ++) { if (i == 3) { continue; } device = device_new(); device->id = i; device->id_stat = SORM_STAT_VALUED; sprintf(device->uuid,"uuid-%d", i); device->uuid_stat = SORM_STAT_VALUED; sprintf(device->name,"name-%d", i); device->name_stat = SORM_STAT_VALUED; sprintf(device->password, "passwd-%d", i); device->password_stat = SORM_STAT_VALUED; device_insert(_conn, device); device_free(device); } ret = pthread_create(&p1, NULL, pthread_a_work, (void*)_conn); assert(ret == 0); ret = pthread_create(&p2, NULL, pthread_b_work, (void*)_conn); assert(ret == 0); pthread_join(p1, NULL); pthread_join(p2, NULL); sorm_iterator_t *iterator; ret = device_select_iterate_by_open(_conn, ALL_COLUMNS, NULL, &iterator); while (1) { device_select_iterate_by(iterator, &device); if (!device_select_iterate_more(iterator)) { break; } char string[1024]; printf("select : %s\n", device_to_string(device, string, 1024)); device_free(device); } device_select_iterate_close(iterator); ret = device_delete_table(_conn); assert(ret == SORM_OK); sorm_close(_conn); }
int main(int argc, char *argv[]) { int opt; const char *opts = "hsit:"; struct option longopts[] = { { "help", no_argument, NULL, 'h' }, { "source", no_argument, NULL, 's' }, { "sink", no_argument, NULL, 'i' }, { "timeout", required_argument, NULL, 't' }, { 0, 0, 0, 0 }, }; int source = 0; int sink = 0; int timeout = 5; while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) switch (opt) { case 'h': printf("usage: %s [--source] [--sink] [--timeout SEC]\n", argv[0]); return EXIT_SUCCESS; case 's': source = 1; break; case 'i': sink = 1; break; case 't': timeout = atoi(optarg); break; default: fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } /* emulate dummy test HCI device */ strncpy(config.hci_dev.name, "hci-xxx", sizeof(config.hci_dev.name) - 1); assert(bluealsa_config_init() == 0); if ((bluealsa_ctl_thread_init() == -1)) { perror("ctl_thread_init"); return EXIT_FAILURE; } /* make sure to cleanup named pipes */ struct sigaction sigact = { .sa_handler = test_pcm_setup_free_handler }; sigaction(SIGINT, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL); atexit(test_pcm_setup_free); bdaddr_t addr; struct ba_device *d1, *d2; /* Connect two devices with the same name, but different MAC addresses. * This test will ensure, that it is possible to launch mixer plug-in. */ str2ba("12:34:56:78:9A:BC", &addr); assert((d1 = device_new(1, &addr, "Test Device With Long Name")) != NULL); g_hash_table_insert(config.devices, g_strdup("/device/1"), d1); str2ba("12:34:56:9A:BC:DE", &addr); assert((d2 = device_new(1, &addr, "Test Device With Long Name")) != NULL); g_hash_table_insert(config.devices, g_strdup("/device/2"), d2); if (source) { assert(transport_new_a2dp(d1, ":test", "/source/1", BLUETOOTH_PROFILE_A2DP_SOURCE, A2DP_CODEC_SBC, (uint8_t *)&cconfig, sizeof(cconfig)) != NULL); assert(transport_new_a2dp(d2, ":test", "/source/2", BLUETOOTH_PROFILE_A2DP_SOURCE, A2DP_CODEC_SBC, (uint8_t *)&cconfig, sizeof(cconfig)) != NULL); } if (sink) { assert(transport_new_a2dp(d1, ":test", "/sink/1", BLUETOOTH_PROFILE_A2DP_SINK, A2DP_CODEC_SBC, (uint8_t *)&cconfig, sizeof(cconfig)) != NULL); assert(transport_new_a2dp(d2, ":test", "/sink/2", BLUETOOTH_PROFILE_A2DP_SINK, A2DP_CODEC_SBC, (uint8_t *)&cconfig, sizeof(cconfig)) != NULL); assert(load_file(SRCDIR "/drum.raw", &drum_buffer, &drum_buffer_size) == 0); } sleep(timeout); return EXIT_SUCCESS; }
static void _bt_agent_method_call_func(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { // g_print("%s%s\n", method_name, g_variant_print(parameters, FALSE)); if (g_strcmp0(method_name, "AuthorizeService") == 0) { GError *error = NULL; Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL)); const char *uuid = g_variant_get_string(g_variant_get_child_value(parameters, 1), NULL); if (_interactive) g_print("Device: %s (%s) for UUID %s\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error), uuid); if (error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Internal error occurred"); return; } if (device_get_paired (device_obj, &error)) { g_dbus_method_invocation_return_value(invocation, NULL); } else if (error) { g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Internal error occurred"); g_error_free (error); } else { g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Service authorization rejected"); } } else if (g_strcmp0(method_name, "Cancel") == 0) { if (_interactive) g_print("Request canceled\n"); // Return void g_dbus_method_invocation_return_value(invocation, NULL); } else if (g_strcmp0(method_name, "DisplayPasskey") == 0) { GError *error = NULL; Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL)); const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj)); if (_interactive) g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error)); if (error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); } g_object_unref(device_obj); if (_interactive) { g_print("Passkey: %u, entered: %u\n", g_variant_get_uint32(g_variant_get_child_value(parameters, 1)), g_variant_get_uint16(g_variant_get_child_value(parameters, 2))); g_dbus_method_invocation_return_value(invocation, NULL); return; } else if (pin != NULL) { /* OK, device found */ g_dbus_method_invocation_return_value(invocation, NULL); return; } g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected"); } else if (g_strcmp0(method_name, "DisplayPinCode") == 0) { GError *error = NULL; Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL)); const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj)); const gchar *pincode = g_variant_get_string(g_variant_get_child_value(parameters, 1), NULL); if (_interactive) g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error)); if (error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); } g_object_unref(device_obj); /* Try to use found PIN */ if (pin != NULL) { if (g_strcmp0(pin, "*") == 0 || g_strcmp0(pin, pincode) == 0) { if (_interactive) g_print("Pin code confirmed\n"); g_dbus_method_invocation_return_value(invocation, NULL); } else g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match"); return; } else if (_interactive) { g_print("Confirm pin code: %s (yes/no)? ", pincode); gchar yn[4] = {0,}; errno = 0; if (scanf("%3s", yn) == EOF && errno) g_warning("%s\n", strerror(errno)); if(g_ascii_strcasecmp(yn, "yes") == 0 || g_ascii_strcasecmp(yn, "y") == 0) g_dbus_method_invocation_return_value(invocation, NULL); else g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match"); return; } g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected"); } else if (g_strcmp0(method_name, "Release") == 0) { agent_need_unregister = FALSE; if(_mainloop) if (g_main_loop_is_running(_mainloop)) g_main_loop_quit(_mainloop); g_print("Agent released\n"); // Return void g_dbus_method_invocation_return_value(invocation, NULL); } else if (g_strcmp0(method_name, "RequestAuthorization") == 0) { GError *error = NULL; Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL)); if (_interactive) g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error)); if(error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); } g_object_unref(device_obj); if (_interactive) { g_print("Authorize this device pairing (yes/no)? "); gchar yn[4] = {0,}; errno = 0; if (scanf("%3s", yn) == EOF && errno) g_warning("%s\n", strerror(errno)); if(g_ascii_strcasecmp(yn, "yes") == 0 || g_ascii_strcasecmp(yn, "y") == 0) g_dbus_method_invocation_return_value(invocation, NULL); else g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected"); return; } g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected"); } else if (g_strcmp0(method_name, "RequestConfirmation") == 0) { GError *error = NULL; Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL)); guint32 passkey = g_variant_get_uint32(g_variant_get_child_value(parameters, 1)); const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj)); if (_interactive) g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error)); if(error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); } g_object_unref(device_obj); /* Try to use found PIN */ if (pin != NULL) { guint32 passkey_t; sscanf(pin, "%u", &passkey_t); if (g_strcmp0(pin, "*") == 0 || passkey_t == passkey) { if (_interactive) g_print("Passkey confirmed\n"); g_dbus_method_invocation_return_value(invocation, NULL); } else g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match"); return; } else if (_interactive) { g_print("Confirm passkey: %u (yes/no)? ", passkey); gchar yn[4] = {0,}; errno = 0; if (scanf("%3s", yn) == EOF && errno) g_warning("%s\n", strerror(errno)); if(g_ascii_strcasecmp(yn, "yes") == 0 || g_ascii_strcasecmp(yn, "y") == 0) g_dbus_method_invocation_return_value(invocation, NULL); else g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match"); return; } g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match"); } else if (g_strcmp0(method_name, "RequestPasskey") == 0) { GError *error = NULL; Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL)); const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj)); guint32 ret = 0; gboolean invoke = FALSE; if (_interactive) g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error)); if(error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); } g_object_unref(device_obj); /* Try to use found PIN */ if (pin != NULL) { if (_interactive) g_print("Passkey found\n"); sscanf(pin, "%u", &ret); invoke = TRUE; } else if (_interactive) { g_print("Enter passkey: "); errno = 0; if (scanf("%u", &ret) == EOF && errno) g_warning("%s\n", strerror(errno)); invoke = TRUE; } if (invoke) { g_dbus_method_invocation_return_value(invocation, g_variant_new ("(u)", ret)); } else { g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "No passkey inputted"); } } else if (g_strcmp0(method_name, "RequestPinCode") == 0) { GError *error = NULL; Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL)); const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj)); gchar *ret = NULL; gboolean invoke = FALSE; if (_interactive) g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error)); if(error) { g_critical("Failed to get remote device's MAC address: %s", error->message); g_error_free(error); } g_object_unref(device_obj); /* Try to use found PIN */ if (pin != NULL) { if (_interactive) g_print("Passkey found\n"); sscanf(pin, "%ms", &ret); invoke = TRUE; } else if (_interactive) { g_print("Enter passkey: "); errno = 0; if (scanf("%ms", &ret) == EOF && errno) g_warning("%s\n", strerror(errno)); invoke = TRUE; } if (invoke) { g_dbus_method_invocation_return_value(invocation, g_variant_new ("(s)", ret)); } else { g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "No passkey inputted"); } if (ret) free(ret); } }