Return error when using the apis with emulator
[framework/api/usb-accessory.git] / src / usb_accessory.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17 #include "usb_accessory.h"
18 #include "usb_accessory_private.h"
19
20 struct AccCbData *accCbData;
21
22 int usb_accessory_clone(usb_accessory_h handle, usb_accessory_h* cloned_handle)
23 {
24         __USB_FUNC_ENTER__ ;
25         if (!handle) return USB_ERROR_INVALID_PARAMETER;
26         if (is_emul_bin()) {
27                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
28                 return USB_ERROR_NOT_SUPPORTED;
29         }
30         if (!cloned_handle || *cloned_handle) return USB_ERROR_INVALID_PARAMETER;
31         *cloned_handle = (usb_accessory_h)malloc(sizeof(struct usb_accessory_s));
32         snprintf((*cloned_handle)->manufacturer, strlen(handle->manufacturer), "%s", handle->manufacturer);
33         snprintf((*cloned_handle)->model, strlen(handle->model), "%s", handle->model);
34         snprintf((*cloned_handle)->description, strlen(handle->description), "%s", handle->description);
35         snprintf((*cloned_handle)->version, strlen(handle->version), "%s", handle->version);
36         snprintf((*cloned_handle)->uri, strlen(handle->uri), "%s", handle->uri);
37         snprintf((*cloned_handle)->serial, strlen(handle->serial), "%s", handle->serial);
38
39         (*cloned_handle)->accPermission = false;
40
41         __USB_FUNC_EXIT__ ;
42     return USB_ERROR_NONE;
43 }
44
45 int usb_accessory_destroy(usb_accessory_h handle)
46 {
47     __USB_FUNC_ENTER__ ;
48         if (!handle) return USB_ERROR_INVALID_PARAMETER;
49         if (is_emul_bin()) {
50                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
51                 return USB_ERROR_NOT_SUPPORTED;
52         }
53         FREE(handle);
54         __USB_FUNC_EXIT__ ;
55         return USB_ERROR_NONE;
56 }
57
58 int usb_accessory_foreach_attached(usb_accessory_attached_cb callback, void *user_data)
59 {
60         __USB_FUNC_ENTER__ ;
61         if (callback == NULL) return USB_ERROR_INVALID_PARAMETER;
62         if (is_emul_bin()) {
63                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
64                 return USB_ERROR_NOT_SUPPORTED;
65         }
66         struct usb_accessory_list *accList = NULL;
67         struct usb_accessory_list *tmpList = NULL;
68         bool ret = false;
69         ret = getAccList(&accList);
70         um_retvm_if(ret == false, -1, "FAIL: getAccList(accList)\n");
71         um_retvm_if(accList == NULL, -1, "ERROR: accList == NULL\n");
72
73         ret = true;
74         tmpList = accList;
75         while (ret) {
76                 if (tmpList == NULL || tmpList->accessory == NULL) {
77                         break;
78                 }
79                 ret = callback(tmpList->accessory, user_data);
80                 if (ret) {
81                         tmpList = tmpList->next;
82                 }
83         }
84
85         ret = freeAccList(accList);
86         um_retvm_if(ret == false, USB_ERROR_OPERATION_FAILED, "FAIL: freeAccList(accList)\n");
87
88         __USB_FUNC_EXIT__ ;
89     return USB_ERROR_NONE;
90 }
91
92
93 int usb_accessory_set_connection_changed_cb(usb_accessory_connection_changed_cb callback, void* user_data)
94 {
95         __USB_FUNC_ENTER__ ;
96         if (is_emul_bin()) {
97                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
98                 return USB_ERROR_NOT_SUPPORTED;
99         }
100         int ret = -1;
101         accCbData = (struct AccCbData *)malloc(sizeof(struct AccCbData));
102         accCbData->user_data = user_data;
103         accCbData->connection_cb_func = callback;
104         ret = vconf_notify_key_changed(VCONFKEY_USB_ACCESSORY_STATUS, accessory_status_changed_cb, accCbData);
105         um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_notify_key_changed(VCONFKEY_USB_ACCESSORY_STATUS)\n");
106         __USB_FUNC_EXIT__ ;
107     return USB_ERROR_NONE;
108 }
109  
110
111 int usb_accessory_connection_unset_cb()
112 {
113         __USB_FUNC_ENTER__ ;
114         if (is_emul_bin()) {
115                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
116                 return USB_ERROR_NOT_SUPPORTED;
117         }
118         if (accCbData != NULL) {
119                 FREE(accCbData);
120                 int ret = vconf_ignore_key_changed(VCONFKEY_USB_ACCESSORY_STATUS, accessory_status_changed_cb);
121                 um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_ignore_key_changed(VCONFKEY_USB_ACCESSORY_status");
122         }
123         __USB_FUNC_EXIT__ ;
124
125     return USB_ERROR_NONE;
126 }
127  
128
129 int usb_accessory_has_permission(usb_accessory_h accessory, bool* is_granted)
130 {
131         __USB_FUNC_ENTER__ ;
132         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
133         if (is_emul_bin()) {
134                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
135                 return USB_ERROR_NOT_SUPPORTED;
136         }
137         if (accessory->accPermission == true) {
138                 __USB_FUNC_EXIT__ ;
139                 return USB_ERROR_NONE;
140         } else {
141                 int ret = -1;
142                 int ipc_result = -1;
143                 int sock_remote;
144                 char buf[SOCK_STR_LEN];
145                 char *app_id = get_app_id();
146                 if(app_id == NULL) {
147                         USB_LOG("FAIL: get_app_id()\n");
148                         *is_granted = false;
149                         return USB_ERROR_NONE;
150                 }
151
152                 ret = ipc_request_client_init(&sock_remote);
153                 um_retvm_if(ret < 0, USB_ERROR_PERMISSION_DENIED, "FAIL: ipc_request_client_init()\n");
154
155                 ret = request_to_usb_server(sock_remote, HAS_ACC_PERMISSION, buf, app_id);
156                 um_retvm_if(ret < 0, USB_ERROR_PERMISSION_DENIED, "FAIL: request_to_usb_server()\n");
157
158                 ret = ipc_request_client_close(&sock_remote);
159                 um_retvm_if(ret < 0, USB_ERROR_PERMISSION_DENIED, "FAIL: ipc_request_client_close()\n");
160
161                 FREE(app_id);
162
163                 USB_LOG("Permission: %s\n", buf);
164                 ipc_result = atoi(buf);
165                 if (IPC_SUCCESS == ipc_result) {
166                         accessory->accPermission = true;
167                         *is_granted = true;
168                 } else {
169                         accessory->accPermission = false;
170                         *is_granted = false;
171                 }
172                 __USB_FUNC_EXIT__ ;
173                 return USB_ERROR_NONE;
174         }
175 }
176
177
178 int usb_accessory_open(usb_accessory_h accessory, FILE **fd)
179 {
180         __USB_FUNC_ENTER__ ;
181         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
182         if (is_emul_bin()) {
183                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
184                 return USB_ERROR_NOT_SUPPORTED;
185         }
186         if (accessory->accPermission == true) {
187                 *fd = fopen(USB_ACCESSORY_NODE, "r+");
188                 USB_LOG("file pointer: %d", *fd);
189         } else {
190                 USB_LOG("Permission is not allowed");
191                 *fd = NULL;
192         }
193         __USB_FUNC_EXIT__ ;
194     return USB_ERROR_NONE;
195 }
196
197
198 int usb_accessory_get_description(usb_accessory_h accessory, char** description)
199 {
200         __USB_FUNC_ENTER__ ;
201         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
202         if (is_emul_bin()) {
203                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
204                 return USB_ERROR_NOT_SUPPORTED;
205         }
206         *description = strdup(accessory->description);
207         __USB_FUNC_ENTER__ ;
208     return USB_ERROR_NONE;
209 }
210
211
212 int usb_accessory_get_manufacturer(usb_accessory_h accessory, char** manufacturer)
213 {
214         __USB_FUNC_ENTER__ ;
215         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
216         if (is_emul_bin()) {
217                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
218                 return USB_ERROR_NOT_SUPPORTED;
219         }
220         *manufacturer = strdup(accessory->manufacturer);
221         __USB_FUNC_ENTER__ ;
222     return USB_ERROR_NONE;
223 }
224
225
226 int usb_accessory_get_model(usb_accessory_h accessory, char** model)
227 {
228         __USB_FUNC_ENTER__ ;
229         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
230         if (is_emul_bin()) {
231                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
232                 return USB_ERROR_NOT_SUPPORTED;
233         }
234         *model = strdup(accessory->model);
235         __USB_FUNC_ENTER__ ;
236     return USB_ERROR_NONE;
237 }
238
239
240 int usb_accessory_get_serial(usb_accessory_h accessory, char** serial)
241 {
242         __USB_FUNC_ENTER__ ;
243         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
244         if (is_emul_bin()) {
245                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
246                 return USB_ERROR_NOT_SUPPORTED;
247         }
248         *serial = strdup(accessory->serial);
249         __USB_FUNC_ENTER__ ;
250     return USB_ERROR_NONE;
251 }
252
253
254 int usb_accessory_get_version(usb_accessory_h accessory, char** version)
255 {
256         __USB_FUNC_ENTER__ ;
257         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
258         if (is_emul_bin()) {
259                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
260                 return USB_ERROR_NOT_SUPPORTED;
261         }
262         *version = strdup(accessory->version);
263         __USB_FUNC_ENTER__ ;
264     return USB_ERROR_NONE;
265 }
266
267
268 int usb_accessory_is_connected(usb_accessory_h accessory, bool* is_connected)
269 {
270         __USB_FUNC_ENTER__ ;
271         if (is_emul_bin()) {
272                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
273                 return USB_ERROR_NOT_SUPPORTED;
274         }
275         int ret = -1;
276         int val = -1;
277         ret = vconf_get_int(VCONFKEY_USB_ACCESSORY_STATUS, &val);
278         um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_get_int(VCONFKEY_USB_ACCESSORY_STATUS)\n");
279         switch (val) {
280         case VCONFKEY_USB_ACCESSORY_STATUS_CONNECTED:
281                 *is_connected = true;
282                 break;
283         case VCONFKEY_USB_ACCESSORY_STATUS_DISCONNECTED:
284                 *is_connected = false;
285                 break;
286         }
287         __USB_FUNC_EXIT__ ;
288     return USB_ERROR_NONE;
289 }
290
291 int usb_accessory_request_permission(usb_accessory_h accessory, usb_accessory_permission_response_cb callback, void* user_data)
292 {
293         __USB_FUNC_ENTER__ ;
294         if (!accessory) return USB_ERROR_INVALID_PARAMETER;
295         if (!callback) return USB_ERROR_INVALID_PARAMETER;
296         if (is_emul_bin()) {
297                 USB_LOG("FAIL:USB Accessory is not available with emulator.");
298                 return USB_ERROR_NOT_SUPPORTED;
299         }
300         int ret = -1;
301         guint g_ret = 0;
302         int sock_remote;
303         char buf[SOCK_STR_LEN];
304         char *app_id = get_app_id();
305         accCbData->user_data = accessory;
306         accCbData->request_perm_cb_func = callback;
307         accCbData->accessory = accessory;
308         GError *err;
309         GIOStatus gio_ret;
310
311         int fd = ipc_noti_client_init();
312         GIOChannel *g_io_ch = g_io_channel_unix_new(fd);
313         g_ret = g_io_add_watch(g_io_ch, G_IO_IN, ipc_noti_client_cb, (gpointer)accCbData);
314         um_retvm_if (0 == g_ret, USB_ERROR_PERMISSION_DENIED, "FAIL: g_io_add_watch(g_io_ch, G_IO_IN)\n");
315
316         ret = ipc_request_client_init(&sock_remote);
317         if(ret < 0) {
318                 USB_LOG("FAIL: ipc_request_client_init(&sock_remote)\n");
319                 ret = ipc_request_client_close(&sock_remote);
320                 if (ret < 0) USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
321                 ret = ipc_noti_client_close(&fd);
322                 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
323                 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
324                 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
325                 g_io_channel_unref(g_io_ch);
326                 FREE(app_id);
327                 return USB_ERROR_PERMISSION_DENIED;
328         }
329
330         ret = request_to_usb_server(sock_remote, REQ_ACC_PERMISSION, buf, app_id);
331         if(ret < 0) {
332                 USB_LOG("FAIL: request_to_usb_server(REQ_ACC_PERMISSION)\n");
333                 ret = ipc_request_client_close(&sock_remote);
334                 if (ret < 0) USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
335                 ret = ipc_noti_client_close(&fd);
336                 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
337                 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
338                 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
339                 g_io_channel_unref(g_io_ch);
340                 FREE(app_id);
341                 return USB_ERROR_PERMISSION_DENIED;
342         }
343
344         FREE(app_id);
345
346         ret = ipc_request_client_close(&sock_remote);
347         if (ret < 0) {
348                 USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
349                 ret = ipc_noti_client_close(&fd);
350                 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
351                 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
352                 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
353                 g_io_channel_unref(g_io_ch);
354                 return USB_ERROR_PERMISSION_DENIED;
355         }
356
357         __USB_FUNC_EXIT__ ;
358     return USB_ERROR_NONE;
359 }