Remove unused code
[platform/core/system/deviced.git] / src / devicectl / usb.c
1 /*
2  * devicectl
3  *
4  * Copyright (c) 2012 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <shared/common.h>
25 #include "usb.h"
26
27 #define USB_SDB "sdb"
28 #define USB_SSH "ssh"
29
30 #define ARG_MAX 10
31 #define CMD_MAX 128
32
33 static struct usb_sysfs {
34         char *path;
35         char *value;
36 } usb_confs[] = {
37         { "/sys/class/usb_mode/usb0/enable",          "0"    },
38         { "/sys/class/usb_mode/usb0/idVendor",        "04e8" },
39         { "/sys/class/usb_mode/usb0/idProduct",       NULL   },
40         { "/sys/class/usb_mode/usb0/funcs_fconf",     NULL   },
41         { "/sys/class/usb_mode/usb0/funcs_sconf",     NULL   },
42         { "/sys/class/usb_mode/usb0/bDeviceClass",    "239"  },
43         { "/sys/class/usb_mode/usb0/bDeviceSubClass", "2"    },
44         { "/sys/class/usb_mode/usb0/bDeviceProtocol", "1"    },
45         { "/sys/class/usb_mode/usb0/enable",          "1"    },
46 };
47
48 static int launch_app(char **argv)
49 {
50         pid_t pid;
51
52         if (!argv || !argv[0])
53                 return -EINVAL;
54
55         pid = fork();
56
57         if (pid < 0) {
58                 printf("Failed to call fork().\n");
59                 return -ENOMEM;
60         }
61
62         if (pid > 0) { /*parent*/
63                 return pid;
64         }
65
66         /*child*/
67
68         if (execvp(argv[0], argv) < 0)
69                 printf("Failed to call execvp: %d\n", errno);
70
71         return 0;
72 }
73
74 static int write_sysfs(char *path, char *value)
75 {
76         FILE *fp;
77         int ret;
78
79         if (!path || !value)
80                 return -ENOMEM;
81
82         fp = fopen(path, "w");
83         if (!fp) {
84                 printf("Failed to open '%s'.\n", path);
85                 return -ENOMEM;
86         }
87
88         ret = fwrite(value, sizeof(char), strlen(value), fp);
89         fclose(fp);
90         if (ret < strlen(value)) {
91                 printf("Failed to write '%s'.\n", value);
92                 ret = -ENOMEM;
93         }
94
95         return ret;
96 }
97
98 static int set_usb_configuration(char *idproduct, char *fconf, char *sconf)
99 {
100         int i, ret;
101
102         usb_confs[2].value = idproduct;
103         usb_confs[3].value = fconf;
104         usb_confs[4].value = sconf;
105
106         for (i = 0; i < ARRAY_SIZE(usb_confs); i++) {
107                 ret = write_sysfs(usb_confs[i].path, usb_confs[i].value);
108                 if (ret < 0) {
109                         printf("Failed to write usb setting. path=%s value=%s\n", usb_confs[i].path, usb_confs[i].value);
110                         return ret;
111                 }
112         }
113
114         return 0;
115 }
116
117 static int divide_cmd(char **command, int len, char *cmd)
118 {
119         char *param, *next, *term;
120         int cnt = 0;
121
122         if (!cmd)
123                 return -EINVAL;
124
125         term = strchr(cmd, '\0');
126         if (!term)
127                 return -EINVAL;
128
129         memset(command, 0, len);
130
131         param = cmd;
132         while (1) {
133                 if (*param == '\0')
134                         break;
135                 if (*param == ' ') {
136                         param++;
137                         continue;
138                 }
139
140                 next = strchr(param, ' ');
141                 if (!next) {
142                         command[cnt++] = param;
143                         break;
144                 }
145
146                 if (next == param) {
147                         param++;
148                         continue;
149                 }
150
151                 *next = '\0';
152                 command[cnt++] = param;
153                 param = next + 1;
154         }
155
156         return 0;
157 }
158
159 static int run_cmd(char *cmd)
160 {
161         int ret;
162         char *command[ARG_MAX];
163         char in_cmd[CMD_MAX];
164
165         if (!cmd)
166                 return -EINVAL;
167
168         snprintf(in_cmd, sizeof(in_cmd), "%s", cmd);
169
170         ret = divide_cmd(command, sizeof(command), in_cmd);
171         if (ret < 0)
172                 return ret;
173
174         ret = launch_app(command);
175         if (ret < 0)
176                 return ret;
177
178         return 0;
179
180 }
181
182 static int load_sdb(void)
183 {
184         int ret;
185
186         ret = set_usb_configuration("6860", "mtp", "mtp,acm,sdb");
187         if (ret < 0)
188                 return ret;
189
190         return run_cmd("/usr/bin/systemctl start sdbd.service");
191 }
192
193 static int load_ssh(void)
194 {
195         int ret;
196
197         ret = set_usb_configuration("6863", "rndis", " ");
198         if (ret < 0)
199                 return ret;
200
201         ret = run_cmd("/sbin/ifconfig usb0 192.168.129.3 up");
202         if (ret < 0)
203                 return ret;
204
205         ret = run_cmd("/sbin/route add -net 192.168.129.0 netmask 255.255.255.0 dev usb0");
206         if (ret < 0)
207                 return ret;
208
209         ret = run_cmd("/usr/bin/systemctl start sshd.service");
210         if (ret < 0)
211                 return ret;
212
213         return 0;
214 }
215
216 static int unload_sdb(void)
217 {
218         int ret;
219
220         ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
221         if (ret < 0)
222                 return ret;
223
224         ret = run_cmd("/usr/bin/systemctl stop sdbd.service");
225         if (ret < 0)
226                 return ret;
227
228         return 0;
229 }
230
231 static int unload_ssh(void)
232 {
233         int ret;
234
235         ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
236         if (ret < 0)
237                 return ret;
238
239         ret = run_cmd("/sbin/ifconfig usb0 down");
240         if (ret < 0)
241                 return ret;
242
243         ret = run_cmd("/usr/bin/systemctl stop sshd.service");
244         if (ret < 0)
245                 return ret;
246
247         return 0;
248 }
249
250 int load_usb_mode(char *opt)
251 {
252         if (!opt) {
253                 printf("Failed: Forth parameter is NULL.\n");
254                 return -EINVAL;
255         }
256
257         if (!strncmp(opt, USB_SDB, strlen(opt)))
258                 return load_sdb();
259
260         if (!strncmp(opt, USB_SSH, strlen(opt)))
261                 return load_ssh();
262
263         printf("Failed: Forth parameter(%s) is invalid.\n", opt);
264         return -EINVAL;
265 }
266
267 int unload_usb_mode(char *opt)
268 {
269         if (!opt) {
270                 printf("Failed: Forth parameter is NULL.\n");
271                 return -EINVAL;
272         }
273
274         if (!strncmp(opt, USB_SDB, strlen(opt)))
275                 return unload_sdb();
276
277         if (!strncmp(opt, USB_SSH, strlen(opt)))
278                 return unload_ssh();
279
280         printf("Failed: Forth parameter(%s) is invalid.\n", opt);
281         return -EINVAL;
282 }