blob: ab65fd9663d7b0ae1024635ff09ec734d6e320b6 [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);
The Android Open Source Project13f797d2009-02-10 15:44:07 -080036static int do_format_media(char *cmd);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080037
38static 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 },
The Android Open Source Project13f797d2009-02-10 15:44:07 -080042 { VOLD_CMD_MOUNT_VOLUME, do_mount_volume },
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080043 { VOLD_CMD_EJECT_MEDIA, do_eject_media },
The Android Open Source Project13f797d2009-02-10 15:44:07 -080044 { VOLD_CMD_FORMAT_MEDIA, do_format_media },
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080045 { NULL, NULL }
46};
47
48int process_framework_command(int socket)
49{
50 int rc;
51 char buffer[101];
52
53 if ((rc = read(socket, buffer, sizeof(buffer) -1)) < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080054 LOGE("Unable to read framework command (%s)", strerror(errno));
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080055 return -errno;
The Android Open Source Project13f797d2009-02-10 15:44:07 -080056 } else if (!rc)
57 return -ECONNRESET;
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080058
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
73static void dispatch_cmd(char *cmd)
74{
75 struct cmd_dispatch *c;
76
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080077 LOG_VOL("dispatch_cmd(%s):", cmd);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080078
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
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080086 LOGE("No cmd handlers defined for '%s'", cmd);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080087}
88
89static int do_send_ums_status(char *cmd)
90{
91 return ums_send_status();
92}
93
94static 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
102static int do_mount_volume(char *cmd)
103{
104 return volmgr_start_volume_by_mountpoint(&cmd[strlen("mount_volume:")]);
105}
106
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800107static int do_format_media(char *cmd)
108{
109 return volmgr_format_volume(&cmd[strlen("format_media:")]);
110}
111
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800112static int do_eject_media(char *cmd)
113{
114 return volmgr_stop_volume_by_mountpoint(&cmd[strlen("eject_media:")]);
115}