// Bluez 5.x has new storage structure, incompatible with Bluez 4.x static int linux_bluez5_register_psmove(struct linux_info_t *linux_info, char *addr, char *bluetooth_dir) { char *info_dir = NULL; char *cache_dir = NULL; int errors = 0; struct stat st; info_dir = bt_path_join(bluetooth_dir, addr); if (stat(info_dir, &st) != 0) { linux_bluetoothd_control(linux_info, 0); // create info directory if (mkdir(info_dir, 0700) != 0) { LINUXPAIR_DEBUG("Cannot create directory: %s\n", info_dir); errors++; goto cleanup; } errors += linux_bluez5_write_info(info_dir); } cache_dir = bt_path_join(bluetooth_dir, BLUEZ5_CACHE_DIR); if (stat(cache_dir, &st) != 0) { linux_bluetoothd_control(linux_info, 0); // create cache directory if (mkdir(cache_dir, 0700) != 0) { LINUXPAIR_DEBUG("Cannot create directory: %s\n", info_dir); errors++; goto cleanup; } } errors += linux_bluez5_write_cache(linux_info, cache_dir, addr); linux_bluetoothd_control(linux_info, 1); cleanup: if (info_dir != NULL) { free(info_dir); } if (cache_dir != NULL) { free(cache_dir); } return (errors == 0); }
static void linux_bluetoothd_control(struct linux_info_t *info, int start) { char *cmd = NULL; if (start) { // start request if (!(info->bluetoothd_stopped)) { // already running return; } info->bluetoothd_stopped = 0; LINUXPAIR_DEBUG("Trying to start bluetoothd...\n"); } else { // stop request if (info->bluetoothd_stopped) { // already stopped return; } info->bluetoothd_stopped = 1; LINUXPAIR_DEBUG("Trying to stop bluetoothd...\n"); } switch (info->init_type) { case LINUX_SYSTEMD: cmd = start ? "systemctl start bluetooth.service" : "systemctl stop bluetooth.service"; LINUXPAIR_DEBUG("Using systemd...\n"); break; case LINUX_UPSTART: cmd = start ? "service bluetooth start" : "service bluetooth stop"; LINUXPAIR_DEBUG("Using upstart...\n"); break; case LINUX_SYSVINIT: cmd = start ? "/etc/init.d/bluetooth start" : "/etc/init.d/bluetooth stop"; LINUXPAIR_DEBUG("Using sysvinit...\n"); default: break; } if (system(cmd) != 0) { LINUXPAIR_DEBUG("Automatic starting/stopping of bluetoothd failed.\n" "You might have to start/stop it manually.\n"); } else { LINUXPAIR_DEBUG("Succeeded\n"); } }
static int linux_bluez5_write_entry(char *path, char *contents) { FILE *fp = NULL; int errors = 0; fp = fopen(path, "w"); if (fp == NULL) { LINUXPAIR_DEBUG("Cannot open file for writing: %s\n", path); errors++; } else { fwrite((const void *)contents, 1, strlen(contents), fp); fclose(fp); } return errors; }
static int write_entry_list(const char *filename, struct entry_list_t *list) { // XXX: Create backup file of entry list FILE *fp = fopen(filename, "w"); if (fp == NULL) { LINUXPAIR_DEBUG("Can't open file for writing: '%s'\n", filename); return 1; } struct entry_list_t *cur = list; while (cur != NULL) { fprintf(fp, "%s%s\n", cur->addr, cur->entry); cur = cur->next; } fclose(fp); return 0; }
static int linux_bluez_register_psmove(const char *addr, const char *host) { int errors = 0; char *base = NULL; struct linux_info_t linux_info; linux_info_init(&linux_info); char *controller_addr = _psmove_normalize_btaddr(addr, 0, ':'); char *host_addr = _psmove_normalize_btaddr(host, 0, ':'); if (controller_addr == NULL) { LINUXPAIR_DEBUG("Cannot parse controller address: '%s'\n", addr); errors++; goto cleanup; } if (host_addr == NULL) { LINUXPAIR_DEBUG("Cannot parse host address: '%s'\n", host); errors++; goto cleanup; } base = malloc(strlen(BLUEZ_CONFIG_DIR) + strlen(host_addr) + 1); strcpy(base, BLUEZ_CONFIG_DIR); strcat(base, host_addr); struct stat st; if (stat(base, &st) != 0 || !S_ISDIR(st.st_mode)) { LINUXPAIR_DEBUG("Not a directory: %s\n", base); errors++; goto cleanup; } #ifdef PSMOVE_BLUEZ5_SUPPORT errors = linux_bluez5_register_psmove(&linux_info, controller_addr, base); goto cleanup; #endif // First, let's check if the entries are already okay.. errors = for_all_entries(check_entry_in_file, base, controller_addr); if (errors) { // In this case, we have missing or invalid values and need to update // the Bluetooth configuration files and restart Bluez' bluetoothd linux_bluetoothd_control(&linux_info, 0); errors = for_all_entries(write_entry_to_file, base, controller_addr); linux_bluetoothd_control(&linux_info, 1); } cleanup: free(base); free(host_addr); free(controller_addr); return (errors == 0); }
int linux_bluez_register_psmove(char *addr, char *host) { int errors = 0; char *base = NULL; char *controller_addr = _psmove_normalize_btaddr(addr, 0, ':'); char *host_addr = _psmove_normalize_btaddr(host, 0, ':'); if (controller_addr == NULL) { LINUXPAIR_DEBUG("Cannot parse controller address: '%s'\n", addr); errors++; goto cleanup; } if (host_addr == NULL) { LINUXPAIR_DEBUG("Cannot parse host address: '%s'\n", host); errors++; goto cleanup; } base = malloc(strlen(BLUEZ_CONFIG_DIR) + strlen(host_addr) + 1); strcpy(base, BLUEZ_CONFIG_DIR); strcat(base, host_addr); struct stat st; if (stat(base, &st) != 0 || !S_ISDIR(st.st_mode)) { LINUXPAIR_DEBUG("Not a directory: %s\n", base); errors++; goto cleanup; } // First, let's check if the entries are already okay.. errors = for_all_entries(check_entry_in_file, base, controller_addr); if (errors) { // In this case, we have missing or invalid values and need to update // the Bluetooth configuration files and restart Bluez' bluetoothd // FIXME: This is Ubuntu-specific if (system("service bluetooth stop") != 0) { LINUXPAIR_DEBUG("Automatic stopping of bluetoothd failed.\n" "You might have to stop it manually before pairing.\n"); } errors = for_all_entries(write_entry_to_file, base, controller_addr); // FIXME: This is Ubuntu-specific if (system("service bluetooth start") != 0) { LINUXPAIR_DEBUG("Automatic starting of bluetoothd failed.\n" "You might have to start it manually after pairing.\n"); } } cleanup: free(base); free(host_addr); free(controller_addr); return (errors == 0); }