San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <errno.h> |
San Mehat | 3d40729 | 2009-05-07 08:49:30 -0700 | [diff] [blame] | 17 | #include <string.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 18 | |
| 19 | #define LOG_TAG "FrameworkListener" |
| 20 | |
| 21 | #include <cutils/log.h> |
| 22 | |
| 23 | #include <sysutils/FrameworkListener.h> |
| 24 | #include <sysutils/FrameworkCommand.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 25 | #include <sysutils/SocketClient.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 26 | |
| 27 | FrameworkListener::FrameworkListener(const char *socketName) : |
| 28 | SocketListener(socketName, true) { |
| 29 | mCommands = new FrameworkCommandCollection(); |
| 30 | } |
| 31 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 32 | bool FrameworkListener::onDataAvailable(SocketClient *c) { |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 33 | char buffer[255]; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 34 | int len; |
| 35 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 36 | if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 37 | LOGE("read() failed (%s)", strerror(errno)); |
| 38 | return errno; |
San Mehat | c73a3a5 | 2009-06-15 14:06:03 -0700 | [diff] [blame^] | 39 | } else if (!len) |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 40 | return false; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 41 | |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 42 | int offset = 0; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 43 | int i; |
| 44 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 45 | for (i = 0; i < len; i++) { |
San Mehat | c73a3a5 | 2009-06-15 14:06:03 -0700 | [diff] [blame^] | 46 | if (buffer[i] == '\0') { |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 47 | dispatchCommand(c, buffer + offset); |
| 48 | offset = i + 1; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | void FrameworkListener::registerCmd(FrameworkCommand *cmd) { |
| 55 | mCommands->push_back(cmd); |
| 56 | } |
| 57 | |
San Mehat | c73a3a5 | 2009-06-15 14:06:03 -0700 | [diff] [blame^] | 58 | void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) { |
| 59 | int argc; |
| 60 | char *argv[FrameworkListener::CMD_ARGS_MAX]; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 61 | |
San Mehat | c73a3a5 | 2009-06-15 14:06:03 -0700 | [diff] [blame^] | 62 | if (!index(data, '"')) { |
| 63 | char *next = data; |
| 64 | char *field; |
| 65 | int i; |
| 66 | |
| 67 | for (i = 0; (i < FrameworkListener::CMD_ARGS_MAX) && |
| 68 | (argv[i] = strsep(&next, " ")); i++); |
| 69 | argc = i+1; |
| 70 | } else { |
| 71 | LOGD("blehhh not supported"); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 75 | FrameworkCommandCollection::iterator i; |
| 76 | |
| 77 | for (i = mCommands->begin(); i != mCommands->end(); ++i) { |
| 78 | FrameworkCommand *c = *i; |
| 79 | |
San Mehat | c73a3a5 | 2009-06-15 14:06:03 -0700 | [diff] [blame^] | 80 | if (!strcmp(argv[0], c->getCommand())) { |
| 81 | if (c->runCommand(cli, argc, argv)) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 82 | LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno)); |
| 83 | } |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 88 | cli->sendMsg(500, "Command not recognized", false); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 89 | return; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 90 | } |