blob: 1f48cfca337d174039ca6ed0969f45e9ed750645 [file] [log] [blame]
The Android Open Source Project8ac3a132009-01-20 14:04:01 -08001
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
26struct cmd_dispatch {
27 char *cmd;
28 int (* dispatch) (char *);
29};
30
31static void dispatch_cmd(char *cmd);
32static int do_send_ums_status(char *cmd);
33static int do_set_ums_enable(char *cmd);
34static int do_mount_volume(char *cmd);
35static int do_eject_media(char *cmd);
36
37static struct cmd_dispatch dispatch_table[] = {
38 { VOLD_CMD_ENABLE_UMS, do_set_ums_enable },
39 { VOLD_CMD_DISABLE_UMS, do_set_ums_enable },
40 { VOLD_CMD_SEND_UMS_STATUS, do_send_ums_status },
41 { VOLD_CMD_MOUNT_VOLUME, do_mount_volume },
42 { VOLD_CMD_EJECT_MEDIA, do_eject_media },
43 { NULL, NULL }
44};
45
46int process_framework_command(int socket)
47{
48 int rc;
49 char buffer[101];
50
51 if ((rc = read(socket, buffer, sizeof(buffer) -1)) < 0) {
52 LOGE("Unable to read framework command (%s)\n", strerror(errno));
53 return -errno;
54 }
55
56 int start = 0;
57 int i;
58
59 buffer[rc] = 0;
60
61 for (i = 0; i < rc; i++) {
62 if (buffer[i] == 0) {
63 dispatch_cmd(buffer + start);
64 start = i + 1;
65 }
66 }
67 return 0;
68}
69
70static void dispatch_cmd(char *cmd)
71{
72 struct cmd_dispatch *c;
73
74 LOG_VOL("dispatch_cmd(%s):\n", cmd);
75
76 for (c = dispatch_table; c->cmd != NULL; c++) {
77 if (!strncmp(c->cmd, cmd, strlen(c->cmd))) {
78 c->dispatch(cmd);
79 return;
80 }
81 }
82
83 LOGE("No cmd handlers defined for '%s'\n", cmd);
84}
85
86static int do_send_ums_status(char *cmd)
87{
88 return ums_send_status();
89}
90
91static int do_set_ums_enable(char *cmd)
92{
93 if (!strcmp(cmd, VOLD_CMD_ENABLE_UMS))
94 return volmgr_enable_ums(true);
95
96 return volmgr_enable_ums(false);
97}
98
99static int do_mount_volume(char *cmd)
100{
101 return volmgr_start_volume_by_mountpoint(&cmd[strlen("mount_volume:")]);
102}
103
104static int do_eject_media(char *cmd)
105{
106 return volmgr_stop_volume_by_mountpoint(&cmd[strlen("eject_media:")]);
107}