A fork of the official mbed-rpc, with additional features.

Fork of mbed-rpc by mbed official

Committer:
baitzor
Date:
Tue Feb 12 19:54:33 2013 +0000
Revision:
1:afe5fe95394f
Parent:
0:efe8172b4113
Add support for unsigned int arguments and replies, also add support for up to 8 arguments when using rpc_method_caller.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:efe8172b4113 1 /* mbed Microcontroller Library
mbed_official 0:efe8172b4113 2 * Copyright (c) 2006-2012 ARM Limited
mbed_official 0:efe8172b4113 3 *
mbed_official 0:efe8172b4113 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
mbed_official 0:efe8172b4113 5 * of this software and associated documentation files (the "Software"), to deal
mbed_official 0:efe8172b4113 6 * in the Software without restriction, including without limitation the rights
mbed_official 0:efe8172b4113 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mbed_official 0:efe8172b4113 8 * copies of the Software, and to permit persons to whom the Software is
mbed_official 0:efe8172b4113 9 * furnished to do so, subject to the following conditions:
mbed_official 0:efe8172b4113 10 *
mbed_official 0:efe8172b4113 11 * The above copyright notice and this permission notice shall be included in
mbed_official 0:efe8172b4113 12 * all copies or substantial portions of the Software.
mbed_official 0:efe8172b4113 13 *
mbed_official 0:efe8172b4113 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mbed_official 0:efe8172b4113 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mbed_official 0:efe8172b4113 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mbed_official 0:efe8172b4113 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mbed_official 0:efe8172b4113 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:efe8172b4113 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
mbed_official 0:efe8172b4113 20 * SOFTWARE.
mbed_official 0:efe8172b4113 21 */
mbed_official 0:efe8172b4113 22 #include "Arguments.h"
mbed_official 0:efe8172b4113 23 #include "pinmap.h"
mbed_official 0:efe8172b4113 24
mbed_official 0:efe8172b4113 25 using namespace std;
mbed_official 0:efe8172b4113 26
mbed_official 0:efe8172b4113 27 namespace mbed {
mbed_official 0:efe8172b4113 28
mbed_official 0:efe8172b4113 29 Arguments::Arguments(const char* rqs) {
mbed_official 0:efe8172b4113 30 obj_name = NULL;
mbed_official 0:efe8172b4113 31 method_name = NULL;
mbed_official 0:efe8172b4113 32 argc = 0;
mbed_official 0:efe8172b4113 33
mbed_official 0:efe8172b4113 34 // This copy can be removed if we can assume the request string is
mbed_official 0:efe8172b4113 35 // persistent and writable for the duration of the call
mbed_official 0:efe8172b4113 36 strcpy(request, rqs);
mbed_official 0:efe8172b4113 37
mbed_official 0:efe8172b4113 38 // Initial '/'
mbed_official 0:efe8172b4113 39 char* p = request;
mbed_official 0:efe8172b4113 40 if (*p != '/') return;
mbed_official 0:efe8172b4113 41 p++;
mbed_official 0:efe8172b4113 42
mbed_official 0:efe8172b4113 43 // Object Name
mbed_official 0:efe8172b4113 44 p = search_arg(&obj_name, p, '/');
mbed_official 0:efe8172b4113 45 if (p == NULL) return;
mbed_official 0:efe8172b4113 46
mbed_official 0:efe8172b4113 47 // Method Name
mbed_official 0:efe8172b4113 48 p = search_arg(&method_name, p, ' ');
mbed_official 0:efe8172b4113 49 if (p == NULL) return;
mbed_official 0:efe8172b4113 50
mbed_official 0:efe8172b4113 51 // Arguments
mbed_official 0:efe8172b4113 52 while (true) {
mbed_official 0:efe8172b4113 53 argv[argc] = NULL;
mbed_official 0:efe8172b4113 54 p = search_arg(&argv[argc], p, ' ');
mbed_official 0:efe8172b4113 55 if (argv[argc] != NULL) argc++;
mbed_official 0:efe8172b4113 56 if (p == NULL) break;
mbed_official 0:efe8172b4113 57 }
mbed_official 0:efe8172b4113 58
mbed_official 0:efe8172b4113 59 index = -1;
mbed_official 0:efe8172b4113 60 }
mbed_official 0:efe8172b4113 61
mbed_official 0:efe8172b4113 62 char* Arguments::search_arg(char **arg, char *p, char next_sep) {
mbed_official 0:efe8172b4113 63 char *s = p;
mbed_official 0:efe8172b4113 64 while (true) {
mbed_official 0:efe8172b4113 65 if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break;
mbed_official 0:efe8172b4113 66 p++;
mbed_official 0:efe8172b4113 67 }
mbed_official 0:efe8172b4113 68 if (p == s) return NULL;
mbed_official 0:efe8172b4113 69 *arg = s;
mbed_official 0:efe8172b4113 70 char separator = *p;
mbed_official 0:efe8172b4113 71 *p = '\0';
mbed_official 0:efe8172b4113 72 p++;
mbed_official 0:efe8172b4113 73 return (separator == next_sep) ? (p) : (NULL);
mbed_official 0:efe8172b4113 74 }
mbed_official 0:efe8172b4113 75
mbed_official 0:efe8172b4113 76 template<> PinName Arguments::getArg<PinName>(void) {
mbed_official 0:efe8172b4113 77 index++;
mbed_official 0:efe8172b4113 78 return parse_pins(argv[index]);
mbed_official 0:efe8172b4113 79 }
mbed_official 0:efe8172b4113 80
mbed_official 0:efe8172b4113 81 template<> int Arguments::getArg<int>(void) {
mbed_official 0:efe8172b4113 82 index++;
mbed_official 0:efe8172b4113 83 char *pEnd;
mbed_official 0:efe8172b4113 84 return strtol(argv[index], &pEnd, 10);
mbed_official 0:efe8172b4113 85 }
mbed_official 0:efe8172b4113 86
baitzor 1:afe5fe95394f 87
baitzor 1:afe5fe95394f 88 template<> unsigned int Arguments::getArg<unsigned int>(void) {
baitzor 1:afe5fe95394f 89 index++;
baitzor 1:afe5fe95394f 90 char *pEnd;
baitzor 1:afe5fe95394f 91 return strtoul(argv[index], &pEnd, 10);
baitzor 1:afe5fe95394f 92 }
baitzor 1:afe5fe95394f 93
mbed_official 0:efe8172b4113 94 template<> const char* Arguments::getArg<const char*>(void) {
mbed_official 0:efe8172b4113 95 index++;
mbed_official 0:efe8172b4113 96 return argv[index];
mbed_official 0:efe8172b4113 97 }
mbed_official 0:efe8172b4113 98
mbed_official 0:efe8172b4113 99 template<> char Arguments::getArg<char>(void) {
mbed_official 0:efe8172b4113 100 index++;
mbed_official 0:efe8172b4113 101 return *argv[index];
mbed_official 0:efe8172b4113 102 }
mbed_official 0:efe8172b4113 103
mbed_official 0:efe8172b4113 104 template<> double Arguments::getArg<double>(void) {
mbed_official 0:efe8172b4113 105 index++;
mbed_official 0:efe8172b4113 106 return atof(argv[index]);
mbed_official 0:efe8172b4113 107 }
mbed_official 0:efe8172b4113 108
mbed_official 0:efe8172b4113 109 template<> float Arguments::getArg<float>(void) {
mbed_official 0:efe8172b4113 110 index++;
mbed_official 0:efe8172b4113 111 return atof(argv[index]);
mbed_official 0:efe8172b4113 112 }
mbed_official 0:efe8172b4113 113
mbed_official 0:efe8172b4113 114 Reply::Reply(char* r) {
mbed_official 0:efe8172b4113 115 first = true;
mbed_official 0:efe8172b4113 116 *r = '\0';
mbed_official 0:efe8172b4113 117 reply = r;
mbed_official 0:efe8172b4113 118 }
mbed_official 0:efe8172b4113 119
mbed_official 0:efe8172b4113 120 void Reply::separator(void) {
mbed_official 0:efe8172b4113 121 if (first) {
mbed_official 0:efe8172b4113 122 first = false;
mbed_official 0:efe8172b4113 123 } else {
mbed_official 0:efe8172b4113 124 *reply = ' '; reply++;
mbed_official 0:efe8172b4113 125 }
mbed_official 0:efe8172b4113 126 }
mbed_official 0:efe8172b4113 127
mbed_official 0:efe8172b4113 128 template<> void Reply::putData<const char*>(const char* s) {
mbed_official 0:efe8172b4113 129 separator();
mbed_official 0:efe8172b4113 130 reply += sprintf(reply, "%s", s);
mbed_official 0:efe8172b4113 131 }
mbed_official 0:efe8172b4113 132
mbed_official 0:efe8172b4113 133 template<> void Reply::putData<char*>(char* s) {
mbed_official 0:efe8172b4113 134 separator();
mbed_official 0:efe8172b4113 135 reply += sprintf(reply, "%s", s);
mbed_official 0:efe8172b4113 136 }
mbed_official 0:efe8172b4113 137
mbed_official 0:efe8172b4113 138 template<> void Reply::putData<char>(char c) {
mbed_official 0:efe8172b4113 139 separator();
mbed_official 0:efe8172b4113 140 reply += sprintf(reply, "%c", c);
mbed_official 0:efe8172b4113 141 }
mbed_official 0:efe8172b4113 142
mbed_official 0:efe8172b4113 143 template<> void Reply::putData<int>(int v) {
mbed_official 0:efe8172b4113 144 separator();
mbed_official 0:efe8172b4113 145 reply += sprintf(reply, "%d", v);
mbed_official 0:efe8172b4113 146 }
mbed_official 0:efe8172b4113 147
baitzor 1:afe5fe95394f 148 template<> void Reply::putData<unsigned int>(unsigned int v) {
baitzor 1:afe5fe95394f 149 separator();
baitzor 1:afe5fe95394f 150 reply += sprintf(reply, "%u", v);
baitzor 1:afe5fe95394f 151 }
baitzor 1:afe5fe95394f 152
mbed_official 0:efe8172b4113 153 template<> void Reply::putData<float>(float f) {
mbed_official 0:efe8172b4113 154 separator();
mbed_official 0:efe8172b4113 155 reply += sprintf(reply, "%.17g", f);
mbed_official 0:efe8172b4113 156 }
mbed_official 0:efe8172b4113 157
mbed_official 0:efe8172b4113 158 } // namespace mbed