4 * Copyright (c) 2012 - 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include "core/list.h"
24 #include "usb-client.h"
25 #include "core/config-parser.h"
29 #define USB_CLIENT_CONFIGURATION "/etc/deviced/usb-client-configuration.conf"
30 #define USB_CLIENT_OPERATION "/etc/deviced/usb-client-operation.conf"
32 #define SECTION_ROOT_PATH "RootPath"
33 #define NAME_PATH "path"
35 struct operation_data {
40 struct usb_client_mode {
45 struct usb_client_mode usb_mode_match[] = {
46 { SET_USB_NONE , "None" },
47 { SET_USB_DEFAULT , "Default" },
48 { SET_USB_SDB , "Sdb" },
49 { SET_USB_SDB_DIAG , "SdbDiag" },
50 { SET_USB_RNDIS , "Rndis" },
51 { SET_USB_RNDIS_TETHERING , "Rndis" },
52 { SET_USB_RNDIS_DIAG , "RndisDiag" },
53 { SET_USB_RNDIS_SDB , "RndisSdb" },
54 { SET_USB_DIAG_SDB , "DiagSdb" },
55 { SET_USB_DIAG_RMNET , "DiagRmnet" },
58 static dd_list *oper_list; /* Operations for USB mode */
59 static dd_list *conf_list; /* Configurations for usb mode */
60 static char root_path[BUF_MAX] = {0,};
63 int get_operations_list(dd_list **list)
72 int get_configurations_list(dd_list **list)
81 int get_root_path(char **path)
83 if (strlen(root_path) <= 0)
90 static int find_usb_mode_str(int usb_mode, char **name)
97 for (i = 0 ; i < ARRAY_SIZE(usb_mode_match) ; i++) {
98 if (usb_mode == usb_mode_match[i].mode) {
99 *name = usb_mode_match[i].name;
104 _E("Failed to find usb mode (%d)", usb_mode);
108 static int load_configuration_config(struct parse_result *result, void *user_data)
110 char *name = user_data;
112 size_t res_sec_len, res_name_len, name_len;
113 size_t root_path_len, path_len;
114 struct usb_configuration *conf;
119 res_sec_len = strlen(result->section);
121 root_path_len = strlen(SECTION_ROOT_PATH);
122 if (res_sec_len == root_path_len
123 && !strncmp(result->section, SECTION_ROOT_PATH, res_sec_len)) {
125 res_name_len = strlen(result->name);
126 path_len = strlen(NAME_PATH);
127 if (res_name_len == path_len
128 && !strncmp(result->name, NAME_PATH, res_name_len))
129 snprintf(root_path, sizeof(root_path), "%s", result->value);
134 name_len = strlen(name);
135 if (res_sec_len != name_len
136 || strncmp(result->section, name, res_sec_len))
139 conf = (struct usb_configuration *)malloc(sizeof(struct usb_configuration));
141 _E("malloc() failed");
145 snprintf(conf->name, sizeof(conf->name), "%s", result->name);
146 snprintf(conf->value, sizeof(conf->value), "%s", result->value);
148 DD_LIST_APPEND(conf_list, conf);
149 _I("USB configuration: name(%s), value(%s)", conf->name, conf->value);
154 int make_configuration_list(int usb_mode)
159 ret = find_usb_mode_str(usb_mode, &name);
161 _E("Failed to get usb mode string (%d)", ret);
165 ret = config_parse(USB_CLIENT_CONFIGURATION, load_configuration_config, name);
167 _E("Failed to load usb configurations");
172 void release_configuration_list(void)
175 struct usb_configuration *conf;
180 DD_LIST_FOREACH(conf_list, l, conf) {
185 DD_LIST_FREE_LIST(conf_list);
189 /* Getting operations for each usb mode */
190 static int load_operation_config(struct parse_result *result, void *user_data)
192 struct operation_data *op_data = user_data;
194 size_t res_sec_len, res_name_len, mode_len, action_len;
195 struct usb_operation *oper;
200 res_sec_len = strlen(result->section);
201 mode_len = strlen(op_data->mode);
202 if (res_sec_len != mode_len
203 || strncmp(result->section, op_data->mode, res_sec_len))
206 res_name_len = strlen(result->name);
207 action_len = strlen(op_data->action);
208 if (res_name_len != action_len
209 || strncmp(result->name, op_data->action, res_name_len))
212 oper = (struct usb_operation *)malloc(sizeof(struct usb_operation));
214 _E("malloc() failed");
218 snprintf(oper->name, sizeof(oper->name), "%s", result->name);
219 snprintf(oper->oper, sizeof(oper->oper), "%s", result->value);
221 DD_LIST_APPEND(oper_list, oper);
222 _I("USB operation: name(%s), value(%s)", oper->name, oper->oper);
227 int make_operation_list(int usb_mode, char *action)
231 struct operation_data op_data;
236 ret = find_usb_mode_str(usb_mode, &mode);
238 _E("Failed to get usb mode string (%d)", ret);
242 op_data.action = action;
245 ret = config_parse(USB_CLIENT_OPERATION, load_operation_config, &op_data);
247 _E("Failed to load usb configurations");
252 void release_operations_list(void)
255 struct usb_operation *oper;
260 DD_LIST_FOREACH(oper_list, l, oper) {
265 DD_LIST_FREE_LIST(oper_list);