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)
28 USB_LOG("FAIL:USB Accessory is not available with emulator.");
29 return USB_ERROR_NOT_SUPPORTED;
33 return USB_ERROR_INVALID_PARAMETER;
35 if (!cloned_handle || *cloned_handle)
36 return USB_ERROR_INVALID_PARAMETER;
38 *cloned_handle = (usb_accessory_h)malloc(sizeof(struct usb_accessory_s));
39 if (!(*cloned_handle)) {
40 USB_LOG("FAIL: malloc()");
41 return USB_ERROR_OPERATION_FAILED;
44 (*cloned_handle)->manufacturer = strdup(handle->manufacturer);
45 (*cloned_handle)->model = strdup(handle->model);
46 (*cloned_handle)->description = strdup(handle->description);
47 (*cloned_handle)->version = strdup(handle->version);
48 (*cloned_handle)->uri = strdup(handle->uri);
49 (*cloned_handle)->serial = strdup(handle->serial);
50 (*cloned_handle)->accPermission = false;
53 return USB_ERROR_NONE;
56 int usb_accessory_destroy(usb_accessory_h handle)
61 USB_LOG("FAIL:USB Accessory is not available with emulator.");
62 return USB_ERROR_NOT_SUPPORTED;
66 return USB_ERROR_INVALID_PARAMETER;
68 free_accessory(handle);
71 return USB_ERROR_NONE;
74 int usb_accessory_foreach_attached(usb_accessory_attached_cb callback, void *user_data)
78 struct usb_accessory_list *accList;
79 struct usb_accessory_list *tmpList;
83 USB_LOG("FAIL:USB Accessory is not available with emulator.");
84 return USB_ERROR_NOT_SUPPORTED;
88 return USB_ERROR_INVALID_PARAMETER;
90 accList = (struct usb_accessory_list *)malloc(sizeof(struct usb_accessory_list));
92 USB_LOG("FAIL: malloc()");
93 return USB_ERROR_OPERATION_FAILED;
96 ret = get_acc_list(&accList);
98 USB_LOG("FAIL: getAccList()");
99 free_acc_list(accList);
100 return USB_ERROR_OPERATION_FAILED;
106 if (!tmpList || !(tmpList->accessory)) {
109 ret = callback(tmpList->accessory, user_data);
111 tmpList = tmpList->next;
115 free_acc_list(accList);
118 return USB_ERROR_NONE;
121 int usb_accessory_set_connection_changed_cb(usb_accessory_connection_changed_cb callback,
129 USB_LOG("FAIL:USB Accessory is not available with emulator.");
130 return USB_ERROR_NOT_SUPPORTED;
134 return USB_ERROR_INVALID_PARAMETER;
136 accCbData = (struct AccCbData *)malloc(sizeof(struct AccCbData));
138 USB_LOG("FAIL: malloc()");
139 return USB_ERROR_OPERATION_FAILED;
142 accCbData->user_data = user_data;
143 accCbData->connection_cb_func = callback;
145 ret = vconf_notify_key_changed(VCONFKEY_USB_ACCESSORY_STATUS,
146 accessory_status_changed_cb, accCbData);
148 USB_LOG("FAIL: vconf_notify_key_changed()");
149 return USB_ERROR_OPERATION_FAILED;
153 return USB_ERROR_NONE;
156 int usb_accessory_connection_unset_cb()
163 USB_LOG("FAIL:USB Accessory is not available with emulator.");
164 return USB_ERROR_NOT_SUPPORTED;
169 ret = vconf_ignore_key_changed(VCONFKEY_USB_ACCESSORY_STATUS, accessory_status_changed_cb);
171 USB_LOG("FAIL: vconf_ignore_key_changed()");
172 return USB_ERROR_OPERATION_FAILED;
176 return USB_ERROR_NONE;
179 int usb_accessory_has_permission(usb_accessory_h accessory, bool* is_granted)
186 char buf[SOCK_STR_LEN];
190 USB_LOG("FAIL:USB Accessory is not available with emulator.");
191 return USB_ERROR_NOT_SUPPORTED;
195 return USB_ERROR_INVALID_PARAMETER;
197 if (accessory->accPermission) {
199 return USB_ERROR_NONE;
202 appId = get_app_id();
204 USB_LOG("FAIL: get_app_id()");
206 return USB_ERROR_NONE;
209 sockRemote = ipc_request_client_init();
210 if (sockRemote < 0) {
211 USB_LOG("FAIL: ipc_request_client_init()");
213 return USB_ERROR_IPC_FAILED;
216 ret = request_to_usb_server(sockRemote, HAS_ACC_PERMISSION, appId, buf, sizeof(buf));
218 USB_LOG("FAIL: request_to_usb_server()");
221 return USB_ERROR_IPC_FAILED;
227 USB_LOG("Permission: %s", buf);
228 ipcResult = atoi(buf);
229 if (IPC_SUCCESS == ipcResult) {
230 accessory->accPermission = true;
233 accessory->accPermission = false;
237 return USB_ERROR_NONE;
240 int usb_accessory_open(usb_accessory_h accessory, FILE **fd)
245 USB_LOG("FAIL:USB Accessory is not available with emulator.");
246 return USB_ERROR_NOT_SUPPORTED;
250 return USB_ERROR_INVALID_PARAMETER;
252 if (accessory->accPermission) {
253 *fd = fopen(USB_ACCESSORY_NODE, "r+");
254 USB_LOG("file pointer: %d", *fd);
256 USB_LOG("Permission is not allowed");
261 return USB_ERROR_NONE;
265 int usb_accessory_get_description(usb_accessory_h accessory, char** description)
270 USB_LOG("FAIL:USB Accessory is not available with emulator.");
271 return USB_ERROR_NOT_SUPPORTED;
275 return USB_ERROR_INVALID_PARAMETER;
277 *description = strdup(accessory->description);
280 return USB_ERROR_NONE;
284 int usb_accessory_get_manufacturer(usb_accessory_h accessory, char** manufacturer)
289 USB_LOG("FAIL:USB Accessory is not available with emulator.");
290 return USB_ERROR_NOT_SUPPORTED;
294 return USB_ERROR_INVALID_PARAMETER;
296 *manufacturer = strdup(accessory->manufacturer);
299 return USB_ERROR_NONE;
303 int usb_accessory_get_model(usb_accessory_h accessory, char** model)
308 USB_LOG("FAIL:USB Accessory is not available with emulator.");
309 return USB_ERROR_NOT_SUPPORTED;
313 return USB_ERROR_INVALID_PARAMETER;
315 *model = strdup(accessory->model);
318 return USB_ERROR_NONE;
322 int usb_accessory_get_serial(usb_accessory_h accessory, char** serial)
327 USB_LOG("FAIL:USB Accessory is not available with emulator.");
328 return USB_ERROR_NOT_SUPPORTED;
332 return USB_ERROR_INVALID_PARAMETER;
334 *serial = strdup(accessory->serial);
337 return USB_ERROR_NONE;
341 int usb_accessory_get_version(usb_accessory_h accessory, char** version)
346 USB_LOG("FAIL:USB Accessory is not available with emulator.");
347 return USB_ERROR_NOT_SUPPORTED;
351 return USB_ERROR_INVALID_PARAMETER;
353 *version = strdup(accessory->version);
356 return USB_ERROR_NONE;
359 int usb_accessory_is_connected(usb_accessory_h accessory, bool* is_connected)
367 USB_LOG("FAIL:USB Accessory is not available with emulator.");
368 return USB_ERROR_NOT_SUPPORTED;
371 ret = vconf_get_int(VCONFKEY_USB_ACCESSORY_STATUS, &val);
373 USB_LOG("FAIL: vconf_get_int()");
374 return USB_ERROR_OPERATION_FAILED;
378 case VCONFKEY_USB_ACCESSORY_STATUS_CONNECTED:
379 *is_connected = true;
381 case VCONFKEY_USB_ACCESSORY_STATUS_DISCONNECTED:
382 *is_connected = false;
385 USB_LOG("vconf key value is improper");
386 return USB_ERROR_OPERATION_FAILED;
390 return USB_ERROR_NONE;
393 int usb_accessory_request_permission(usb_accessory_h accessory,
394 usb_accessory_permission_response_cb callback,
403 char buf[SOCK_STR_LEN];
410 USB_LOG("FAIL:USB Accessory is not available with emulator.");
411 return USB_ERROR_NOT_SUPPORTED;
415 return USB_ERROR_INVALID_PARAMETER;
417 return USB_ERROR_INVALID_PARAMETER;
419 return USB_ERROR_INVALID_PARAMETER;
421 accCbData->user_data = accessory;
422 accCbData->request_perm_cb_func = callback;
423 accCbData->accessory = accessory;
425 fd = ipc_noti_client_init();
427 USB_LOG("FAIL: ipc_noti_client_init()");
431 gioCh = g_io_channel_unix_new(fd);
433 USB_LOG("FAIL: g_io_channel_unix_new()");
437 gret = g_io_add_watch(gioCh, G_IO_IN, ipc_noti_client_cb, (gpointer)accCbData);
439 USB_LOG("FAIL: g_io_add_watch(g_io_ch, G_IO_IN)");
443 sockRemote = ipc_request_client_init();
445 USB_LOG("FAIL: ipc_request_client_init(&sock_remote)");
449 appId = get_app_id();
450 USB_LOG("App ID: %s", appId);
452 USB_LOG("FAIL: get_app_id()");
453 goto out_ipc_request;
456 ret = request_to_usb_server(sockRemote, REQ_ACC_PERMISSION, appId, buf, sizeof(buf));
458 USB_LOG("FAIL: request_to_usb_server(REQ_ACC_PERMISSION)");
466 return USB_ERROR_NONE;
473 gioret = g_io_channel_shutdown(gioCh, TRUE, &err);
474 if (G_IO_STATUS_ERROR == gioret)
475 USB_LOG("ERROR: g_io_channel_shutdown()");
476 g_io_channel_unref(gioCh);
481 return USB_ERROR_IPC_FAILED;