1 /* RetroArch - A frontend for libretro.
2 * Copyright (C) 2016-2017 - Jean-André Santoni
3 *
4 * RetroArch is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with RetroArch.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include <string.h>
17 #include <errno.h>
18 #include <compat/strl.h>
19 #include <retro_assert.h>
20 #include <retro_miscellaneous.h>
21 #include <string/stdstring.h>
22
23 #include "tasks_internal.h"
24
25 #include "../msg_hash.h"
26 #include "../verbosity.h"
27 #include "../wifi/wifi_driver.h"
28
29 #define FUNC_PUSH_TASK(funcname, handlerfunc, message) \
30 bool funcname(retro_task_callback_t cb) \
31 { \
32 retro_task_t *task = task_init(); \
33 if (!task) \
34 return false; \
35 task->type = TASK_TYPE_BLOCKING; \
36 task->state = NULL; \
37 task->handler = handlerfunc; \
38 task->callback = cb; \
39 task->title = strdup(msg_hash_to_str( \
40 message)); \
41 task_queue_push(task); \
42 return true; \
43 }
44
task_wifi_scan_handler(retro_task_t * task)45 static void task_wifi_scan_handler(retro_task_t *task)
46 {
47 if (!task)
48 return;
49
50 driver_wifi_scan();
51
52 task_set_progress(task, 100);
53 task_free_title(task);
54 task_set_title(task, strdup(msg_hash_to_str(MSG_WIFI_SCAN_COMPLETE)));
55 task_set_finished(task, true);
56 }
57
task_wifi_enable_handler(retro_task_t * task)58 static void task_wifi_enable_handler(retro_task_t *task)
59 {
60 if (!task)
61 return;
62
63 driver_wifi_enable(true);
64
65 task_set_progress(task, 100);
66 task_set_finished(task, true);
67 }
68
task_wifi_disable_handler(retro_task_t * task)69 static void task_wifi_disable_handler(retro_task_t *task)
70 {
71 if (!task)
72 return;
73
74 driver_wifi_enable(false);
75
76 task_set_progress(task, 100);
77 task_set_finished(task, true);
78 }
79
task_wifi_disconnect_handler(retro_task_t * task)80 static void task_wifi_disconnect_handler(retro_task_t *task)
81 {
82 wifi_network_info_t netinfo;
83 if (!task)
84 return;
85
86 if (driver_wifi_connection_info(&netinfo))
87 driver_wifi_disconnect_ssid(&netinfo);
88
89 task_set_progress(task, 100);
90 task_set_finished(task, true);
91 }
92
task_wifi_connect_handler(retro_task_t * task)93 static void task_wifi_connect_handler(retro_task_t *task)
94 {
95 if (!task)
96 return;
97
98 driver_wifi_connect_ssid((const wifi_network_info_t*)task->user_data);
99 free(task->user_data);
100
101 task_set_progress(task, 100);
102 task_set_finished(task, true);
103 }
104
task_push_wifi_connect(retro_task_callback_t cb,void * netptr)105 bool task_push_wifi_connect(retro_task_callback_t cb, void *netptr) {
106 char msg[128];
107 retro_task_t *task = task_init();
108 wifi_network_info_t *netinfo = (wifi_network_info_t*)netptr;
109 if (!task)
110 return false;
111
112 snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_WIFI_CONNECTING_TO), netinfo->ssid);
113
114 task->type = TASK_TYPE_BLOCKING;
115 task->state = NULL;
116 task->handler = task_wifi_connect_handler;
117 task->callback = cb;
118 task->title = strdup(msg);
119 task->user_data = malloc(sizeof(*netinfo));
120 memcpy(task->user_data, netinfo, sizeof(*netinfo));
121 task_queue_push(task);
122 return true;
123 }
124
125 FUNC_PUSH_TASK(task_push_wifi_scan, task_wifi_scan_handler, MSG_SCANNING_WIRELESS_NETWORKS)
126 FUNC_PUSH_TASK(task_push_wifi_enable, task_wifi_enable_handler, MSG_ENABLING_WIRELESS)
127 FUNC_PUSH_TASK(task_push_wifi_disable, task_wifi_disable_handler, MSG_DISABLING_WIRELESS)
128 FUNC_PUSH_TASK(task_push_wifi_disconnect, task_wifi_disconnect_handler, MSG_DISCONNECTING_WIRELESS)
129
130
131