The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 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); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 36 | static int do_format_media(char *cmd); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 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 }, |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 42 | { VOLD_CMD_MOUNT_VOLUME, do_mount_volume }, |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 43 | { VOLD_CMD_EJECT_MEDIA, do_eject_media }, |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 44 | { VOLD_CMD_FORMAT_MEDIA, do_format_media }, |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 45 | { NULL, NULL } |
| 46 | }; |
| 47 | |
| 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) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 54 | LOGE("Unable to read framework command (%s)", strerror(errno)); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 55 | return -errno; |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 56 | } else if (!rc) |
| 57 | return -ECONNRESET; |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 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 | |
| 73 | static void dispatch_cmd(char *cmd) |
| 74 | { |
| 75 | struct cmd_dispatch *c; |
| 76 | |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 77 | LOG_VOL("dispatch_cmd(%s):", cmd); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 86 | LOGE("No cmd handlers defined for '%s'", cmd); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int do_send_ums_status(char *cmd) |
| 90 | { |
| 91 | return ums_send_status(); |
| 92 | } |
| 93 | |
| 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 | |
| 102 | static int do_mount_volume(char *cmd) |
| 103 | { |
| 104 | return volmgr_start_volume_by_mountpoint(&cmd[strlen("mount_volume:")]); |
| 105 | } |
| 106 | |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 107 | static int do_format_media(char *cmd) |
| 108 | { |
| 109 | return volmgr_format_volume(&cmd[strlen("format_media:")]); |
| 110 | } |
| 111 | |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 112 | static int do_eject_media(char *cmd) |
| 113 | { |
| 114 | return volmgr_stop_volume_by_mountpoint(&cmd[strlen("eject_media:")]); |
| 115 | } |