1
2 /*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18 #include <unistd.h>
19 #include <errno.h>
20
21 #include "vold.h"
22 #include "cmd_dispatch.h"
23 #include "ums.h"
24 #include "volmgr.h"
25
26 struct cmd_dispatch {
27 char *cmd;
28 int (* dispatch) (char *);
29 };
30
31 static void dispatch_cmd(char *cmd);
32 static int do_send_ums_status(char *cmd);
33 static int do_set_ums_enable(char *cmd);
34 static int do_mount_volume(char *cmd);
35 static int do_eject_media(char *cmd);
36 static int do_format_media(char *cmd);
37
38 static struct cmd_dispatch dispatch_table[] = {
39 { VOLD_CMD_ENABLE_UMS, do_set_ums_enable },
40 { VOLD_CMD_DISABLE_UMS, do_set_ums_enable },
41 { VOLD_CMD_SEND_UMS_STATUS, do_send_ums_status },
42 { VOLD_CMD_MOUNT_VOLUME, do_mount_volume },
43 { VOLD_CMD_EJECT_MEDIA, do_eject_media },
44 { VOLD_CMD_FORMAT_MEDIA, do_format_media },
45 { NULL, NULL }
46 };
47
process_framework_command(int socket)48 int process_framework_command(int socket)
49 {
50 int rc;
51 char buffer[101];
52
53 if ((rc = read(socket, buffer, sizeof(buffer) -1)) < 0) {
54 LOGE("Unable to read framework command (%s)", strerror(errno));
55 return -errno;
56 } else if (!rc)
57 return -ECONNRESET;
58
59 int start = 0;
60 int i;
61
62 buffer[rc] = 0;
63
64 for (i = 0; i < rc; i++) {
65 if (buffer[i] == 0) {
66 dispatch_cmd(buffer + start);
67 start = i + 1;
68 }
69 }
70 return 0;
71 }
72
dispatch_cmd(char * cmd)73 static void dispatch_cmd(char *cmd)
74 {
75 struct cmd_dispatch *c;
76
77 LOG_VOL("dispatch_cmd(%s):", cmd);
78
79 for (c = dispatch_table; c->cmd != NULL; c++) {
80 if (!strncmp(c->cmd, cmd, strlen(c->cmd))) {
81 c->dispatch(cmd);
82 return;
83 }
84 }
85
86 LOGE("No cmd handlers defined for '%s'", cmd);
87 }
88
do_send_ums_status(char * cmd)89 static int do_send_ums_status(char *cmd)
90 {
91 return ums_send_status();
92 }
93
do_set_ums_enable(char * cmd)94 static int do_set_ums_enable(char *cmd)
95 {
96 if (!strcmp(cmd, VOLD_CMD_ENABLE_UMS))
97 return volmgr_enable_ums(true);
98
99 return volmgr_enable_ums(false);
100 }
101
do_mount_volume(char * cmd)102 static int do_mount_volume(char *cmd)
103 {
104 return volmgr_start_volume_by_mountpoint(&cmd[strlen("mount_volume:")]);
105 }
106
do_format_media(char * cmd)107 static int do_format_media(char *cmd)
108 {
109 return volmgr_format_volume(&cmd[strlen("format_media:")]);
110 }
111
do_eject_media(char * cmd)112 static int do_eject_media(char *cmd)
113 {
114 return volmgr_stop_volume_by_mountpoint(&cmd[strlen("eject_media:")]);
115 }
116