3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "usb_accessory.h"
19 #include "usb_accessory_private.h"
21 struct AccCbData *accCbData;
23 int usb_accessory_clone(usb_accessory_h handle, usb_accessory_h* cloned_handle)
26 if (!handle) return USB_ERROR_INVALID_PARAMETER;
28 USB_LOG("FAIL:USB Accessory is not available with emulator.");
29 return USB_ERROR_NOT_SUPPORTED;
31 if (!cloned_handle || *cloned_handle) return USB_ERROR_INVALID_PARAMETER;
32 *cloned_handle = (usb_accessory_h)malloc(sizeof(struct usb_accessory_s));
33 snprintf((*cloned_handle)->manufacturer, strlen(handle->manufacturer), "%s", handle->manufacturer);
34 snprintf((*cloned_handle)->model, strlen(handle->model), "%s", handle->model);
35 snprintf((*cloned_handle)->description, strlen(handle->description), "%s", handle->description);
36 snprintf((*cloned_handle)->version, strlen(handle->version), "%s", handle->version);
37 snprintf((*cloned_handle)->uri, strlen(handle->uri), "%s", handle->uri);
38 snprintf((*cloned_handle)->serial, strlen(handle->serial), "%s", handle->serial);
40 (*cloned_handle)->accPermission = false;
43 return USB_ERROR_NONE;
46 int usb_accessory_destroy(usb_accessory_h handle)
49 if (!handle) return USB_ERROR_INVALID_PARAMETER;
51 USB_LOG("FAIL:USB Accessory is not available with emulator.");
52 return USB_ERROR_NOT_SUPPORTED;
56 return USB_ERROR_NONE;
59 int usb_accessory_foreach_attached(usb_accessory_attached_cb callback, void *user_data)
62 if (callback == NULL) return USB_ERROR_INVALID_PARAMETER;
64 USB_LOG("FAIL:USB Accessory is not available with emulator.");
65 return USB_ERROR_NOT_SUPPORTED;
67 struct usb_accessory_list *accList = NULL;
68 struct usb_accessory_list *tmpList = NULL;
70 accList = (struct usb_accessory_list *)malloc(sizeof(struct usb_accessory_list));
72 USB_LOG("FAIL: malloc()");
73 return USB_ERROR_OPERATION_FAILED;
75 ret = getAccList(&accList);
77 USB_LOG("FAIL: getAccList(accList)");
85 if (tmpList == NULL || tmpList->accessory == NULL) {
88 ret = callback(tmpList->accessory, user_data);
90 tmpList = tmpList->next;
97 return USB_ERROR_NONE;
101 int usb_accessory_set_connection_changed_cb(usb_accessory_connection_changed_cb callback, void* user_data)
105 USB_LOG("FAIL:USB Accessory is not available with emulator.");
106 return USB_ERROR_NOT_SUPPORTED;
109 accCbData = (struct AccCbData *)malloc(sizeof(struct AccCbData));
110 accCbData->user_data = user_data;
111 accCbData->connection_cb_func = callback;
112 ret = vconf_notify_key_changed(VCONFKEY_USB_ACCESSORY_STATUS, accessory_status_changed_cb, accCbData);
113 um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_notify_key_changed(VCONFKEY_USB_ACCESSORY_STATUS)\n");
115 return USB_ERROR_NONE;
119 int usb_accessory_connection_unset_cb()
123 USB_LOG("FAIL:USB Accessory is not available with emulator.");
124 return USB_ERROR_NOT_SUPPORTED;
126 if (accCbData != NULL) {
128 int ret = vconf_ignore_key_changed(VCONFKEY_USB_ACCESSORY_STATUS, accessory_status_changed_cb);
129 um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_ignore_key_changed(VCONFKEY_USB_ACCESSORY_status");
133 return USB_ERROR_NONE;
137 int usb_accessory_has_permission(usb_accessory_h accessory, bool* is_granted)
140 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
142 USB_LOG("FAIL:USB Accessory is not available with emulator.");
143 return USB_ERROR_NOT_SUPPORTED;
145 if (accessory->accPermission == true) {
147 return USB_ERROR_NONE;
152 char buf[SOCK_STR_LEN];
153 char *app_id = get_app_id();
155 USB_LOG("FAIL: get_app_id()\n");
157 return USB_ERROR_NONE;
160 ret = ipc_request_client_init(&sock_remote);
161 um_retvm_if(ret < 0, USB_ERROR_IPC_FAILED, "FAIL: ipc_request_client_init()\n");
163 ret = request_to_usb_server(sock_remote, HAS_ACC_PERMISSION, buf, app_id);
164 um_retvm_if(ret < 0, USB_ERROR_IPC_FAILED, "FAIL: request_to_usb_server()\n");
166 ret = ipc_request_client_close(&sock_remote);
167 um_retvm_if(ret < 0, USB_ERROR_IPC_FAILED, "FAIL: ipc_request_client_close()\n");
171 USB_LOG("Permission: %s\n", buf);
172 ipc_result = atoi(buf);
173 if (IPC_SUCCESS == ipc_result) {
174 accessory->accPermission = true;
177 accessory->accPermission = false;
181 return USB_ERROR_NONE;
186 int usb_accessory_open(usb_accessory_h accessory, FILE **fd)
189 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
191 USB_LOG("FAIL:USB Accessory is not available with emulator.");
192 return USB_ERROR_NOT_SUPPORTED;
194 if (accessory->accPermission == true) {
195 *fd = fopen(USB_ACCESSORY_NODE, "r+");
196 USB_LOG("file pointer: %d", *fd);
198 USB_LOG("Permission is not allowed");
202 return USB_ERROR_NONE;
206 int usb_accessory_get_description(usb_accessory_h accessory, char** description)
209 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
211 USB_LOG("FAIL:USB Accessory is not available with emulator.");
212 return USB_ERROR_NOT_SUPPORTED;
214 *description = strdup(accessory->description);
216 return USB_ERROR_NONE;
220 int usb_accessory_get_manufacturer(usb_accessory_h accessory, char** manufacturer)
223 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
225 USB_LOG("FAIL:USB Accessory is not available with emulator.");
226 return USB_ERROR_NOT_SUPPORTED;
228 *manufacturer = strdup(accessory->manufacturer);
230 return USB_ERROR_NONE;
234 int usb_accessory_get_model(usb_accessory_h accessory, char** model)
237 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
239 USB_LOG("FAIL:USB Accessory is not available with emulator.");
240 return USB_ERROR_NOT_SUPPORTED;
242 *model = strdup(accessory->model);
244 return USB_ERROR_NONE;
248 int usb_accessory_get_serial(usb_accessory_h accessory, char** serial)
251 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
253 USB_LOG("FAIL:USB Accessory is not available with emulator.");
254 return USB_ERROR_NOT_SUPPORTED;
256 *serial = strdup(accessory->serial);
258 return USB_ERROR_NONE;
262 int usb_accessory_get_version(usb_accessory_h accessory, char** version)
265 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
267 USB_LOG("FAIL:USB Accessory is not available with emulator.");
268 return USB_ERROR_NOT_SUPPORTED;
270 *version = strdup(accessory->version);
272 return USB_ERROR_NONE;
276 int usb_accessory_is_connected(usb_accessory_h accessory, bool* is_connected)
280 USB_LOG("FAIL:USB Accessory is not available with emulator.");
281 return USB_ERROR_NOT_SUPPORTED;
285 ret = vconf_get_int(VCONFKEY_USB_ACCESSORY_STATUS, &val);
286 um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_get_int(VCONFKEY_USB_ACCESSORY_STATUS)\n");
288 case VCONFKEY_USB_ACCESSORY_STATUS_CONNECTED:
289 *is_connected = true;
291 case VCONFKEY_USB_ACCESSORY_STATUS_DISCONNECTED:
292 *is_connected = false;
296 return USB_ERROR_NONE;
299 int usb_accessory_request_permission(usb_accessory_h accessory, usb_accessory_permission_response_cb callback, void* user_data)
302 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
303 if (!callback) return USB_ERROR_INVALID_PARAMETER;
305 USB_LOG("FAIL:USB Accessory is not available with emulator.");
306 return USB_ERROR_NOT_SUPPORTED;
311 char buf[SOCK_STR_LEN];
313 accCbData->user_data = accessory;
314 accCbData->request_perm_cb_func = callback;
315 accCbData->accessory = accessory;
319 int fd = ipc_noti_client_init();
321 USB_LOG("FAIL: ipc_noti_client_init()");
322 return USB_ERROR_IPC_FAILED;
325 GIOChannel *g_io_ch = g_io_channel_unix_new(fd);
327 USB_LOG("FAIL: g_io_channel_unix_new()");
328 ret = ipc_noti_client_close(&fd);
329 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
330 return USB_ERROR_IPC_FAILED;
333 g_ret = g_io_add_watch(g_io_ch, G_IO_IN, ipc_noti_client_cb, (gpointer)accCbData);
335 USB_LOG("FAIL: g_io_add_watch(g_io_ch, G_IO_IN)\n");
336 ret = ipc_noti_client_close(&fd);
337 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
338 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
339 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
340 g_io_channel_unref(g_io_ch);
341 return USB_ERROR_IPC_FAILED;
344 ret = ipc_request_client_init(&sock_remote);
346 USB_LOG("FAIL: ipc_request_client_init(&sock_remote)\n");
347 ret = ipc_request_client_close(&sock_remote);
348 if (ret < 0) 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_IPC_FAILED;
357 app_id = get_app_id();
358 USB_LOG("App ID: %s", app_id);
360 USB_LOG("FAIL: get_app_id()");
361 ret = ipc_request_client_close(&sock_remote);
362 if (ret < 0) USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
363 ret = ipc_noti_client_close(&fd);
364 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
365 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
366 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
367 g_io_channel_unref(g_io_ch);
368 return USB_ERROR_OPERATION_FAILED;
371 ret = request_to_usb_server(sock_remote, REQ_ACC_PERMISSION, buf, app_id);
373 USB_LOG("FAIL: request_to_usb_server(REQ_ACC_PERMISSION)\n");
374 ret = ipc_request_client_close(&sock_remote);
375 if (ret < 0) USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
376 ret = ipc_noti_client_close(&fd);
377 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
378 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
379 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
380 g_io_channel_unref(g_io_ch);
382 return USB_ERROR_IPC_FAILED;
387 ret = ipc_request_client_close(&sock_remote);
389 USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
390 ret = ipc_noti_client_close(&fd);
391 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
392 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
393 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
394 g_io_channel_unref(g_io_ch);
395 return USB_ERROR_IPC_FAILED;
399 return USB_ERROR_NONE;