2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "usb_accessory.h"
18 #include "usb_accessory_private.h"
20 struct AccCbData *accCbData;
22 int usb_accessory_clone(usb_accessory_h handle, usb_accessory_h* cloned_handle)
25 if (!handle) return USB_ERROR_INVALID_PARAMETER;
26 if (!cloned_handle || *cloned_handle) return USB_ERROR_INVALID_PARAMETER;
27 *cloned_handle = (usb_accessory_h)malloc(sizeof(struct usb_accessory_s));
28 snprintf((*cloned_handle)->manufacturer, strlen(handle->manufacturer), "%s", handle->manufacturer);
29 snprintf((*cloned_handle)->model, strlen(handle->model), "%s", handle->model);
30 snprintf((*cloned_handle)->description, strlen(handle->description), "%s", handle->description);
31 snprintf((*cloned_handle)->version, strlen(handle->version), "%s", handle->version);
32 snprintf((*cloned_handle)->uri, strlen(handle->uri), "%s", handle->uri);
33 snprintf((*cloned_handle)->serial, strlen(handle->serial), "%s", handle->serial);
35 (*cloned_handle)->accPermission = false;
38 return USB_ERROR_NONE;
41 int usb_accessory_destroy(usb_accessory_h handle)
44 if (!handle) return USB_ERROR_INVALID_PARAMETER;
47 return USB_ERROR_NONE;
50 int usb_accessory_foreach_attached(usb_accessory_attached_cb callback, void *user_data)
53 struct usb_accessory_list *accList = NULL;
54 struct usb_accessory_list *tmpList = NULL;
56 ret = getAccList(&accList);
57 um_retvm_if(ret == false, -1, "FAIL: getAccList(accList)\n");
58 um_retvm_if(accList == NULL, -1, "ERROR: accList == NULL\n");
63 if (tmpList == NULL || tmpList->accessory == NULL) {
66 ret = callback(tmpList->accessory, user_data);
68 tmpList = tmpList->next;
72 ret = freeAccList(accList);
73 um_retvm_if(ret == false, USB_ERROR_OPERATION_FAILED, "FAIL: freeAccList(accList)\n");
76 return USB_ERROR_NONE;
80 int usb_accessory_set_connection_changed_cb(usb_accessory_connection_changed_cb callback, void* user_data)
84 accCbData = (struct AccCbData *)malloc(sizeof(struct AccCbData));
85 accCbData->user_data = user_data;
86 accCbData->connection_cb_func = callback;
87 ret = vconf_notify_key_changed(VCONFKEY_USB_ACCESSORY_STATUS, accessory_status_changed_cb, accCbData);
88 um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_notify_key_changed(VCONFKEY_USB_ACCESSORY_STATUS)\n");
90 return USB_ERROR_NONE;
94 int usb_accessory_connection_unset_cb()
97 if (accCbData != NULL) {
99 int ret = vconf_ignore_key_changed(VCONFKEY_USB_ACCESSORY_STATUS, accessory_status_changed_cb);
100 um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_ignore_key_changed(VCONFKEY_USB_ACCESSORY_status");
104 return USB_ERROR_NONE;
108 int usb_accessory_has_permission(usb_accessory_h accessory, bool* is_granted)
111 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
112 if (accessory->accPermission == true) {
114 return USB_ERROR_NONE;
119 char buf[SOCK_STR_LEN];
120 char *app_id = get_app_id();
122 USB_LOG("FAIL: get_app_id()\n");
124 return USB_ERROR_NONE;
127 ret = ipc_request_client_init(&sock_remote);
128 um_retvm_if(ret < 0, USB_ERROR_PERMISSION_DENIED, "FAIL: ipc_request_client_init(&sock_remote)\n");
130 ret = request_to_usb_server(sock_remote, HAS_PERMISSION, buf, app_id);
131 um_retvm_if(ret < 0, USB_ERROR_PERMISSION_DENIED, "FAIL: request_to_usb_server(HAS_PERMISSION)\n");
133 ret = ipc_request_client_close(&sock_remote);
134 um_retvm_if(ret < 0, USB_ERROR_PERMISSION_DENIED, "FAIL: ipc_request_client_close(&sock_remote)\n");
138 USB_LOG("Permission: %s\n", buf);
139 ipc_result = atoi(buf);
140 if (IPC_SUCCESS == ipc_result) {
141 accessory->accPermission = true;
144 accessory->accPermission = false;
148 return USB_ERROR_NONE;
153 int usb_accessory_open(usb_accessory_h accessory, FILE **fd)
156 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
157 if (accessory->accPermission == true) {
158 *fd = fopen(USB_ACCESSORY_NODE, "r+");
159 USB_LOG("file pointer: %d", *fd);
161 USB_LOG("Permission is not allowed");
165 return USB_ERROR_NONE;
169 int usb_accessory_get_description(usb_accessory_h accessory, char** description)
172 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
173 *description = strdup(accessory->description);
175 return USB_ERROR_NONE;
179 int usb_accessory_get_manufacturer(usb_accessory_h accessory, char** manufacturer)
182 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
183 *manufacturer = strdup(accessory->manufacturer);
185 return USB_ERROR_NONE;
189 int usb_accessory_get_model(usb_accessory_h accessory, char** model)
192 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
193 *model = strdup(accessory->model);
195 return USB_ERROR_NONE;
199 int usb_accessory_get_serial(usb_accessory_h accessory, char** serial)
202 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
203 *serial = strdup(accessory->serial);
205 return USB_ERROR_NONE;
209 int usb_accessory_get_version(usb_accessory_h accessory, char** version)
212 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
213 *version = strdup(accessory->version);
215 return USB_ERROR_NONE;
219 int usb_accessory_is_connected(usb_accessory_h accessory, bool* is_connected)
224 ret = vconf_get_int(VCONFKEY_USB_ACCESSORY_STATUS, &val);
225 um_retvm_if(ret < 0, USB_ERROR_OPERATION_FAILED, "FAIL: vconf_get_int(VCONFKEY_USB_ACCESSORY_STATUS)\n");
227 case VCONFKEY_USB_ACCESSORY_STATUS_CONNECTED:
228 *is_connected = true;
230 case VCONFKEY_USB_ACCESSORY_STATUS_DISCONNECTED:
231 *is_connected = false;
235 return USB_ERROR_NONE;
238 int usb_accessory_request_permission(usb_accessory_h accessory, usb_accessory_permission_response_cb callback, void* user_data)
241 if (!accessory) return USB_ERROR_INVALID_PARAMETER;
242 if (!callback) return USB_ERROR_INVALID_PARAMETER;
246 char buf[SOCK_STR_LEN];
247 char *app_id = get_app_id();
248 accCbData->user_data = accessory;
249 accCbData->request_perm_cb_func = callback;
250 accCbData->accessory = accessory;
254 int fd = ipc_noti_client_init();
255 GIOChannel *g_io_ch = g_io_channel_unix_new(fd);
256 g_ret = g_io_add_watch(g_io_ch, G_IO_IN, ipc_noti_client_cb, (gpointer)accCbData);
257 um_retvm_if (0 == g_ret, USB_ERROR_PERMISSION_DENIED, "FAIL: g_io_add_watch(g_io_ch, G_IO_IN)\n");
259 ret = ipc_request_client_init(&sock_remote);
261 USB_LOG("FAIL: ipc_request_client_init(&sock_remote)\n");
262 ret = ipc_request_client_close(&sock_remote);
263 if (ret < 0) USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
264 ret = ipc_noti_client_close(&fd);
265 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
266 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
267 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
268 g_io_channel_unref(g_io_ch);
270 return USB_ERROR_PERMISSION_DENIED;
273 ret = request_to_usb_server(sock_remote, REQUEST_PERMISSION, buf, app_id);
275 USB_LOG("FAIL: request_to_usb_server(REQUEST_PERMISSION)\n");
276 ret = ipc_request_client_close(&sock_remote);
277 if (ret < 0) USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
278 ret = ipc_noti_client_close(&fd);
279 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
280 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
281 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
282 g_io_channel_unref(g_io_ch);
284 return USB_ERROR_PERMISSION_DENIED;
289 ret = ipc_request_client_close(&sock_remote);
291 USB_LOG("FAIL: ipc_request_client_close(&sock_remote)\n");
292 ret = ipc_noti_client_close(&fd);
293 if (ret < 0) USB_LOG("FAIL: ipc_noti_client_close(&fd)\n");
294 gio_ret = g_io_channel_shutdown(g_io_ch, TRUE, &err);
295 if (G_IO_STATUS_ERROR == gio_ret) USB_LOG("ERROR: g_io_channel_shutdown(g_io_ch)\n");
296 g_io_channel_unref(g_io_ch);
297 return USB_ERROR_PERMISSION_DENIED;
301 return USB_ERROR_NONE;