1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 RPC::RPC(const char *name) {
23 _from_construct = false;
25 _name = new char[strlen(name) + 1];
29 sprintf(_name, "obj%p", this);
31 // put this object at head of the list
37 // remove this object from the list
38 if (_head == this) { // first in the list, so just drop me
40 } else { // find the object before me, then drop me
42 while (p->_next != this) {
49 const rpc_method *RPC::get_rpc_methods() {
50 static const rpc_method methods[] = {
51 {"delete", rpc_method_caller<RPC, &RPC::delete_self> },
57 RPC *RPC::lookup(const char *name) {
58 size_t len = strlen(name);
59 for (RPC *p = _head; p != NULL; p = p->_next) {
60 /* Check that p->_name matches name and is the correct length */
61 if (strncmp(p->_name, name, len) == 0 && (strlen(p->_name) == len)) {
68 void RPC::delete_self() {
70 if (_from_construct) {
75 void RPC::list_objs(Arguments *args, Reply *result) {
76 for (RPC *ptr = RPC::_head; ptr != NULL; ptr = ptr->_next) {
77 if (ptr->_from_construct) {
78 result->putData<const char*>(ptr->_name);
83 void RPC::clear(Arguments*, Reply*) {
84 RPC *ptr = RPC::_head;
89 if (tmp->_from_construct) {
95 const rpc_function RPC::_RPC_funcs[] = {
96 {"clear", &RPC::clear },
97 { "objects", &RPC::list_objs },
101 rpc_class RPC::_RPC_class = { "RPC", _RPC_funcs, NULL };
103 RPC *RPC::_head = NULL;
105 rpc_class *RPC::_classes = &_RPC_class;
107 bool RPC::call(const char *request, char *reply) {
108 if (request == NULL) return false;
110 Arguments args(request);
113 /* If there's no name print object and class names to result */
114 if (args.obj_name == NULL) {
115 for (RPC *p = RPC::_head; p != NULL; p = p->_next) {
116 r.putData<const char*>(p->_name);
118 for (rpc_class *c = RPC::_classes; c != NULL; c = c->next) {
119 r.putData<const char*>(c->name);
124 /* First try matching an instance */
125 RPC *p = lookup(args.obj_name);
127 /* Get the list of methods we support */
128 const rpc_method *cur_method = p->get_rpc_methods();
130 /* When there's no method print method names to result */
131 if (args.method_name == NULL) {
133 for (; cur_method->name != NULL; cur_method++) {
134 r.putData<const char*>(cur_method->name);
137 /* write_name_arr's args are references, so result and cur_method will have changed */
138 if (cur_method->super != 0) {
139 cur_method = cur_method->super(p);
146 /* Look through the methods for the one whose name matches */
148 for (; cur_method->name != NULL; cur_method++) {
149 if (strcmp(cur_method->name, args.method_name) == 0) {
150 (cur_method->method_caller)(p, &args, &r);
155 if (cur_method->super != 0) {
156 cur_method = cur_method->super(p);
158 /* end of methods and no match */
165 /* Then try a class */
166 for (const rpc_class *q = _classes; q != NULL; q = q->next) {
167 if (strcmp(q->name, args.obj_name) == 0) {
168 /* Matched the class name, so get its functions */
169 const rpc_function *cur_func = q->static_functions;
170 if (args.method_name == NULL) {
171 for (; cur_func->name != NULL; cur_func++) {
172 r.putData<const char*>(cur_func->name);
176 /* Otherwise call the appropriate function */
177 for (; cur_func->name != NULL; cur_func++) {
178 if (strcmp(cur_func->name, args.method_name) == 0) {
179 (cur_func->function_caller)(&args, &r);