/* RetroArch - A frontend for libretro. * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- * ation, either version 3 of the License, or (at your option) any later version. * * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with RetroArch. * If not, see . */ #include #include #include #include #include #include #include #include "tasks_internal.h" #include "../msg_hash.h" #include "../bluetooth/bluetooth_driver.h" /* The first step starts discovery; the task is then parked, so other * tasks run in the meantime, and looked at every BLUETOOTH_SCAN_POLL_US * until the driver says it has found all it will or the window, * BLUETOOTH_SCAN_WINDOW_US, is out. The scan used to sleep through the * window inside one call, holding the task thread - and every * download, thumbnail and sync queued behind it - for ten seconds. A * cancel ends the window early. */ typedef struct { struct bluetooth_hold *hold; retro_time_t deadline; /* the window's end */ bool begun; } task_bluetooth_scan_t; static void task_bluetooth_scan_handler(retro_task_t *task) { task_bluetooth_scan_t *scan = (task_bluetooth_scan_t*)task->state; if (!scan->begun) { driver_bluetooth_scan_begin(scan->hold); scan->begun = true; scan->deadline = cpu_features_get_time_usec() + BLUETOOTH_SCAN_WINDOW_US; task_queue_park(task, BLUETOOTH_SCAN_POLL_US); return; } /* The rest of the window waited out parked, looked at every * BLUETOOTH_SCAN_POLL_US where the driver can say it has found all * it will. A cancel wakes it and ends the window. */ if ( !(task_get_flags(task) & RETRO_TASK_FLG_CANCELLED) && !driver_bluetooth_scan_done(scan->hold)) { retro_time_t now = cpu_features_get_time_usec(); /* the queue runs a parked task up to half a millisecond early: * what is left within a millisecond of the end is not waited */ if (now + 1000 < scan->deadline) { retro_time_t wait = scan->deadline - now; if (wait > BLUETOOTH_SCAN_POLL_US) wait = BLUETOOTH_SCAN_POLL_US; task_queue_park(task, wait); return; } } driver_bluetooth_scan_end(scan->hold); driver_bluetooth_scan_release(scan->hold); free(scan); task->state = NULL; task_set_progress(task, 100); task_free_title(task); task_set_title(task, strdup(msg_hash_to_str(MSG_BLUETOOTH_SCAN_COMPLETE))); task_set_flags(task, RETRO_TASK_FLG_FINISHED, true); } static bool task_bluetooth_scan_finder(retro_task_t *task, void *user_data) { (void)user_data; return task->handler == task_bluetooth_scan_handler && !(task_get_flags(task) & RETRO_TASK_FLG_FINISHED); } bool task_push_bluetooth_scan(retro_task_callback_t cb) { retro_task_t *task = task_init(); task_bluetooth_scan_t *scan; task_finder_data_t find; if (!task) return false; /* One scan at a time, among scans only: as a blocking task a scan, * up to BLUETOOTH_SCAN_WINDOW_US long, turned away every state save * and load pushed while it ran - Load State did nothing. */ find.func = task_bluetooth_scan_finder; find.userdata = NULL; if (task_queue_find(&find)) { free(task); return false; } if (!(scan = (task_bluetooth_scan_t*)calloc(1, sizeof(*scan)))) { free(task); return false; } task->type = TASK_TYPE_NONE; task->state = scan; task->handler = task_bluetooth_scan_handler; task->callback = cb; task->title = strdup(msg_hash_to_str( MSG_SCANNING_BLUETOOTH_DEVICES)); /* taken here, on the frontend's thread, where the driver is * stopped: the scan's thread cannot find it half gone */ scan->hold = driver_bluetooth_scan_hold(); if (task_queue_push(task)) return true; driver_bluetooth_scan_release(scan->hold); free(scan); task_free_title(task); free(task); return false; }