Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /* mbed Microcontroller Library
bryantaylor 0:eafc3fd41f75 2 * Copyright (c) 2006-2013 ARM Limited
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 5 * you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 6 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 9 *
bryantaylor 0:eafc3fd41f75 10 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 11 * distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 13 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 14 * limitations under the License.
bryantaylor 0:eafc3fd41f75 15 */
bryantaylor 0:eafc3fd41f75 16 #include "rpc.h"
bryantaylor 0:eafc3fd41f75 17
bryantaylor 0:eafc3fd41f75 18 using namespace std;
bryantaylor 0:eafc3fd41f75 19
bryantaylor 0:eafc3fd41f75 20 namespace mbed {
bryantaylor 0:eafc3fd41f75 21
bryantaylor 0:eafc3fd41f75 22 RPC::RPC(const char *name) {
bryantaylor 0:eafc3fd41f75 23 _from_construct = false;
bryantaylor 0:eafc3fd41f75 24 if (name != NULL) {
bryantaylor 0:eafc3fd41f75 25 _name = new char[strlen(name) + 1];
bryantaylor 0:eafc3fd41f75 26 strcpy(_name, name);
bryantaylor 0:eafc3fd41f75 27 } else {
bryantaylor 0:eafc3fd41f75 28 _name = new char[12];
bryantaylor 0:eafc3fd41f75 29 sprintf(_name, "obj%p", this);
bryantaylor 0:eafc3fd41f75 30 }
bryantaylor 0:eafc3fd41f75 31 // put this object at head of the list
bryantaylor 0:eafc3fd41f75 32 _next = _head;
bryantaylor 0:eafc3fd41f75 33 _head = this;
bryantaylor 0:eafc3fd41f75 34 }
bryantaylor 0:eafc3fd41f75 35
bryantaylor 0:eafc3fd41f75 36 RPC::~RPC() {
bryantaylor 0:eafc3fd41f75 37 // remove this object from the list
bryantaylor 0:eafc3fd41f75 38 if (_head == this) { // first in the list, so just drop me
bryantaylor 0:eafc3fd41f75 39 _head = _next;
bryantaylor 0:eafc3fd41f75 40 } else { // find the object before me, then drop me
bryantaylor 0:eafc3fd41f75 41 RPC* p = _head;
bryantaylor 0:eafc3fd41f75 42 while (p->_next != this) {
bryantaylor 0:eafc3fd41f75 43 p = p->_next;
bryantaylor 0:eafc3fd41f75 44 }
bryantaylor 0:eafc3fd41f75 45 p->_next = _next;
bryantaylor 0:eafc3fd41f75 46 }
bryantaylor 0:eafc3fd41f75 47 }
bryantaylor 0:eafc3fd41f75 48
bryantaylor 0:eafc3fd41f75 49 const rpc_method *RPC::get_rpc_methods() {
bryantaylor 0:eafc3fd41f75 50 static const rpc_method methods[] = {
bryantaylor 0:eafc3fd41f75 51 {"delete", rpc_method_caller<RPC, &RPC::delete_self> },
bryantaylor 0:eafc3fd41f75 52 RPC_METHOD_END
bryantaylor 0:eafc3fd41f75 53 };
bryantaylor 0:eafc3fd41f75 54 return methods;
bryantaylor 0:eafc3fd41f75 55 }
bryantaylor 0:eafc3fd41f75 56
bryantaylor 0:eafc3fd41f75 57 RPC *RPC::lookup(const char *name) {
bryantaylor 0:eafc3fd41f75 58 size_t len = strlen(name);
bryantaylor 0:eafc3fd41f75 59 for (RPC *p = _head; p != NULL; p = p->_next) {
bryantaylor 0:eafc3fd41f75 60 /* Check that p->_name matches name and is the correct length */
bryantaylor 0:eafc3fd41f75 61 if (strncmp(p->_name, name, len) == 0 && (strlen(p->_name) == len)) {
bryantaylor 0:eafc3fd41f75 62 return p;
bryantaylor 0:eafc3fd41f75 63 }
bryantaylor 0:eafc3fd41f75 64 }
bryantaylor 0:eafc3fd41f75 65 return NULL;
bryantaylor 0:eafc3fd41f75 66 }
bryantaylor 0:eafc3fd41f75 67
bryantaylor 0:eafc3fd41f75 68 void RPC::delete_self() {
bryantaylor 0:eafc3fd41f75 69 delete[] _name;
bryantaylor 0:eafc3fd41f75 70 if (_from_construct) {
bryantaylor 0:eafc3fd41f75 71 delete this;
bryantaylor 0:eafc3fd41f75 72 }
bryantaylor 0:eafc3fd41f75 73 }
bryantaylor 0:eafc3fd41f75 74
bryantaylor 0:eafc3fd41f75 75 void RPC::list_objs(Arguments *args, Reply *result) {
bryantaylor 0:eafc3fd41f75 76 for (RPC *ptr = RPC::_head; ptr != NULL; ptr = ptr->_next) {
bryantaylor 0:eafc3fd41f75 77 if (ptr->_from_construct) {
bryantaylor 0:eafc3fd41f75 78 result->putData<const char*>(ptr->_name);
bryantaylor 0:eafc3fd41f75 79 }
bryantaylor 0:eafc3fd41f75 80 }
bryantaylor 0:eafc3fd41f75 81 }
bryantaylor 0:eafc3fd41f75 82
bryantaylor 0:eafc3fd41f75 83 void RPC::clear(Arguments*, Reply*) {
bryantaylor 0:eafc3fd41f75 84 RPC *ptr = RPC::_head;
bryantaylor 0:eafc3fd41f75 85 while (ptr != NULL) {
bryantaylor 0:eafc3fd41f75 86 RPC *tmp = ptr;
bryantaylor 0:eafc3fd41f75 87 ptr = ptr->_next;
bryantaylor 0:eafc3fd41f75 88 delete[] tmp->_name;
bryantaylor 0:eafc3fd41f75 89 if (tmp->_from_construct) {
bryantaylor 0:eafc3fd41f75 90 delete tmp;
bryantaylor 0:eafc3fd41f75 91 }
bryantaylor 0:eafc3fd41f75 92 }
bryantaylor 0:eafc3fd41f75 93 }
bryantaylor 0:eafc3fd41f75 94
bryantaylor 0:eafc3fd41f75 95 const rpc_function RPC::_RPC_funcs[] = {
bryantaylor 0:eafc3fd41f75 96 {"clear", &RPC::clear },
bryantaylor 0:eafc3fd41f75 97 { "objects", &RPC::list_objs },
bryantaylor 0:eafc3fd41f75 98 RPC_METHOD_END
bryantaylor 0:eafc3fd41f75 99 };
bryantaylor 0:eafc3fd41f75 100
bryantaylor 0:eafc3fd41f75 101 rpc_class RPC::_RPC_class = { "RPC", _RPC_funcs, NULL };
bryantaylor 0:eafc3fd41f75 102
bryantaylor 0:eafc3fd41f75 103 RPC *RPC::_head = NULL;
bryantaylor 0:eafc3fd41f75 104
bryantaylor 0:eafc3fd41f75 105 rpc_class *RPC::_classes = &_RPC_class;
bryantaylor 0:eafc3fd41f75 106
bryantaylor 0:eafc3fd41f75 107 bool RPC::call(const char *request, char *reply) {
bryantaylor 0:eafc3fd41f75 108 if (request == NULL) return false;
bryantaylor 0:eafc3fd41f75 109
bryantaylor 0:eafc3fd41f75 110 Arguments args(request);
bryantaylor 0:eafc3fd41f75 111 Reply r(reply);
bryantaylor 0:eafc3fd41f75 112
bryantaylor 0:eafc3fd41f75 113 /* If there's no name print object and class names to result */
bryantaylor 0:eafc3fd41f75 114 if (args.obj_name == NULL) {
bryantaylor 0:eafc3fd41f75 115 for (RPC *p = RPC::_head; p != NULL; p = p->_next) {
bryantaylor 0:eafc3fd41f75 116 r.putData<const char*>(p->_name);
bryantaylor 0:eafc3fd41f75 117 }
bryantaylor 0:eafc3fd41f75 118 for (rpc_class *c = RPC::_classes; c != NULL; c = c->next) {
bryantaylor 0:eafc3fd41f75 119 r.putData<const char*>(c->name);
bryantaylor 0:eafc3fd41f75 120 }
bryantaylor 0:eafc3fd41f75 121 return true;
bryantaylor 0:eafc3fd41f75 122 }
bryantaylor 0:eafc3fd41f75 123
bryantaylor 0:eafc3fd41f75 124 /* First try matching an instance */
bryantaylor 0:eafc3fd41f75 125 RPC *p = lookup(args.obj_name);
bryantaylor 0:eafc3fd41f75 126 if (p != NULL) {
bryantaylor 0:eafc3fd41f75 127 /* Get the list of methods we support */
bryantaylor 0:eafc3fd41f75 128 const rpc_method *cur_method = p->get_rpc_methods();
bryantaylor 0:eafc3fd41f75 129
bryantaylor 0:eafc3fd41f75 130 /* When there's no method print method names to result */
bryantaylor 0:eafc3fd41f75 131 if (args.method_name == NULL) {
bryantaylor 0:eafc3fd41f75 132 while (true) {
bryantaylor 0:eafc3fd41f75 133 for (; cur_method->name != NULL; cur_method++) {
bryantaylor 0:eafc3fd41f75 134 r.putData<const char*>(cur_method->name);
bryantaylor 0:eafc3fd41f75 135 }
bryantaylor 0:eafc3fd41f75 136
bryantaylor 0:eafc3fd41f75 137 /* write_name_arr's args are references, so result and cur_method will have changed */
bryantaylor 0:eafc3fd41f75 138 if (cur_method->super != 0) {
bryantaylor 0:eafc3fd41f75 139 cur_method = cur_method->super(p);
bryantaylor 0:eafc3fd41f75 140 } else {
bryantaylor 0:eafc3fd41f75 141 return true;
bryantaylor 0:eafc3fd41f75 142 }
bryantaylor 0:eafc3fd41f75 143 }
bryantaylor 0:eafc3fd41f75 144 }
bryantaylor 0:eafc3fd41f75 145
bryantaylor 0:eafc3fd41f75 146 /* Look through the methods for the one whose name matches */
bryantaylor 0:eafc3fd41f75 147 while (true) {
bryantaylor 0:eafc3fd41f75 148 for (; cur_method->name != NULL; cur_method++) {
bryantaylor 0:eafc3fd41f75 149 if (strcmp(cur_method->name, args.method_name) == 0) {
bryantaylor 0:eafc3fd41f75 150 (cur_method->method_caller)(p, &args, &r);
bryantaylor 0:eafc3fd41f75 151 return true;
bryantaylor 0:eafc3fd41f75 152 }
bryantaylor 0:eafc3fd41f75 153 }
bryantaylor 0:eafc3fd41f75 154
bryantaylor 0:eafc3fd41f75 155 if (cur_method->super != 0) {
bryantaylor 0:eafc3fd41f75 156 cur_method = cur_method->super(p);
bryantaylor 0:eafc3fd41f75 157 } else {
bryantaylor 0:eafc3fd41f75 158 /* end of methods and no match */
bryantaylor 0:eafc3fd41f75 159 return false;
bryantaylor 0:eafc3fd41f75 160 }
bryantaylor 0:eafc3fd41f75 161
bryantaylor 0:eafc3fd41f75 162 }
bryantaylor 0:eafc3fd41f75 163 }
bryantaylor 0:eafc3fd41f75 164
bryantaylor 0:eafc3fd41f75 165 /* Then try a class */
bryantaylor 0:eafc3fd41f75 166 for (const rpc_class *q = _classes; q != NULL; q = q->next) {
bryantaylor 0:eafc3fd41f75 167 if (strcmp(q->name, args.obj_name) == 0) {
bryantaylor 0:eafc3fd41f75 168 /* Matched the class name, so get its functions */
bryantaylor 0:eafc3fd41f75 169 const rpc_function *cur_func = q->static_functions;
bryantaylor 0:eafc3fd41f75 170 if (args.method_name == NULL) {
bryantaylor 0:eafc3fd41f75 171 for (; cur_func->name != NULL; cur_func++) {
bryantaylor 0:eafc3fd41f75 172 r.putData<const char*>(cur_func->name);
bryantaylor 0:eafc3fd41f75 173 }
bryantaylor 0:eafc3fd41f75 174 return true;
bryantaylor 0:eafc3fd41f75 175 } else {
bryantaylor 0:eafc3fd41f75 176 /* Otherwise call the appropriate function */
bryantaylor 0:eafc3fd41f75 177 for (; cur_func->name != NULL; cur_func++) {
bryantaylor 0:eafc3fd41f75 178 if (strcmp(cur_func->name, args.method_name) == 0) {
bryantaylor 0:eafc3fd41f75 179 (cur_func->function_caller)(&args, &r);
bryantaylor 0:eafc3fd41f75 180 return true;
bryantaylor 0:eafc3fd41f75 181 }
bryantaylor 0:eafc3fd41f75 182 }
bryantaylor 0:eafc3fd41f75 183 return false;
bryantaylor 0:eafc3fd41f75 184 }
bryantaylor 0:eafc3fd41f75 185 }
bryantaylor 0:eafc3fd41f75 186 }
bryantaylor 0:eafc3fd41f75 187
bryantaylor 0:eafc3fd41f75 188 return false;
bryantaylor 0:eafc3fd41f75 189 }
bryantaylor 0:eafc3fd41f75 190
bryantaylor 0:eafc3fd41f75 191 } // namespace mbed