]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rpc/rpc.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rpc / rpc.cpp
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
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 "rpc.h"
17
18 using namespace std;
19
20 namespace mbed {
21
22 RPC::RPC(const char *name) {
23     _from_construct = false;
24     if (name != NULL) {
25         _name = new char[strlen(name) + 1];
26         strcpy(_name, name);
27     } else {
28         _name = new char[12];
29         sprintf(_name, "obj%p", this);
30     }
31     // put this object at head of the list
32     _next = _head;
33     _head = this;
34 }
35
36 RPC::~RPC() {
37     // remove this object from the list
38     if (_head == this) { // first in the list, so just drop me
39         _head = _next;
40     } else {            // find the object before me, then drop me
41         RPC* p = _head;
42         while (p->_next != this) {
43             p = p->_next;
44         }
45         p->_next = _next;
46     }
47 }
48
49 const rpc_method *RPC::get_rpc_methods() {
50     static const rpc_method methods[] = {
51         {"delete", rpc_method_caller<RPC, &RPC::delete_self> },
52         RPC_METHOD_END
53     };
54     return methods;
55 }
56
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)) {
62             return p;
63         }
64     }
65     return NULL;
66 }
67
68 void RPC::delete_self() {
69     delete[] _name;
70     if (_from_construct) {
71         delete this;
72     }
73 }
74
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);
79         }
80     }
81 }
82
83 void RPC::clear(Arguments*, Reply*) {
84     RPC *ptr = RPC::_head;
85     while (ptr != NULL) {
86         RPC *tmp = ptr;
87         ptr = ptr->_next;
88         delete[] tmp->_name;
89         if (tmp->_from_construct) {
90             delete tmp;
91         }
92     }
93 }
94
95 const rpc_function RPC::_RPC_funcs[] = {
96     {"clear", &RPC::clear },
97     { "objects", &RPC::list_objs },
98     RPC_METHOD_END
99 };
100
101 rpc_class RPC::_RPC_class = { "RPC", _RPC_funcs, NULL };
102
103 RPC *RPC::_head = NULL;
104
105 rpc_class *RPC::_classes = &_RPC_class;
106
107 bool RPC::call(const char *request, char *reply) {
108     if (request == NULL) return false;
109
110     Arguments args(request);
111     Reply r(reply);
112
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);
117         }
118         for (rpc_class *c = RPC::_classes; c != NULL; c = c->next) {
119             r.putData<const char*>(c->name);
120         }
121         return true;
122     }
123
124     /* First try matching an instance */
125     RPC *p = lookup(args.obj_name);
126     if (p != NULL) {
127         /* Get the list of methods we support */
128         const rpc_method *cur_method = p->get_rpc_methods();
129
130         /* When there's no method print method names to result */
131         if (args.method_name == NULL) {
132             while (true) {
133                 for (; cur_method->name != NULL; cur_method++) {
134                     r.putData<const char*>(cur_method->name);
135                 }
136
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);
140                 } else {
141                     return true;
142                 }
143             }
144         }
145
146         /* Look through the methods for the one whose name matches */
147         while (true) {
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);
151                     return true;
152                 }
153             }
154
155             if (cur_method->super != 0) {
156                 cur_method = cur_method->super(p);
157             } else {
158                 /* end of methods and no match */
159                 return false;
160             }
161
162         }
163     }
164
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);
173                 }
174                 return true;
175             } else {
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);
180                         return true;
181                     }
182                 }
183                 return false;
184             }
185         }
186     }
187
188     return false;
189 }
190
191 } // namespace mbed