Modified to run on Renesas GR Peach
Fork of mbed-rpc by
Arguments.cpp@0:efe8172b4113, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:23:59 2012 +0000
- Revision:
- 0:efe8172b4113
- Child:
- 1:6919289a5946
mbed RPC
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mbed_official | 0:efe8172b4113 | 87 | template<> const char* Arguments::getArg<const char*>(void) { |
mbed_official | 0:efe8172b4113 | 88 | index++; |
mbed_official | 0:efe8172b4113 | 89 | return argv[index]; |
mbed_official | 0:efe8172b4113 | 90 | } |
mbed_official | 0:efe8172b4113 | 91 | |
mbed_official | 0:efe8172b4113 | 92 | template<> char Arguments::getArg<char>(void) { |
mbed_official | 0:efe8172b4113 | 93 | index++; |
mbed_official | 0:efe8172b4113 | 94 | return *argv[index]; |
mbed_official | 0:efe8172b4113 | 95 | } |
mbed_official | 0:efe8172b4113 | 96 | |
mbed_official | 0:efe8172b4113 | 97 | template<> double Arguments::getArg<double>(void) { |
mbed_official | 0:efe8172b4113 | 98 | index++; |
mbed_official | 0:efe8172b4113 | 99 | return atof(argv[index]); |
mbed_official | 0:efe8172b4113 | 100 | } |
mbed_official | 0:efe8172b4113 | 101 | |
mbed_official | 0:efe8172b4113 | 102 | template<> float Arguments::getArg<float>(void) { |
mbed_official | 0:efe8172b4113 | 103 | index++; |
mbed_official | 0:efe8172b4113 | 104 | return atof(argv[index]); |
mbed_official | 0:efe8172b4113 | 105 | } |
mbed_official | 0:efe8172b4113 | 106 | |
mbed_official | 0:efe8172b4113 | 107 | Reply::Reply(char* r) { |
mbed_official | 0:efe8172b4113 | 108 | first = true; |
mbed_official | 0:efe8172b4113 | 109 | *r = '\0'; |
mbed_official | 0:efe8172b4113 | 110 | reply = r; |
mbed_official | 0:efe8172b4113 | 111 | } |
mbed_official | 0:efe8172b4113 | 112 | |
mbed_official | 0:efe8172b4113 | 113 | void Reply::separator(void) { |
mbed_official | 0:efe8172b4113 | 114 | if (first) { |
mbed_official | 0:efe8172b4113 | 115 | first = false; |
mbed_official | 0:efe8172b4113 | 116 | } else { |
mbed_official | 0:efe8172b4113 | 117 | *reply = ' '; reply++; |
mbed_official | 0:efe8172b4113 | 118 | } |
mbed_official | 0:efe8172b4113 | 119 | } |
mbed_official | 0:efe8172b4113 | 120 | |
mbed_official | 0:efe8172b4113 | 121 | template<> void Reply::putData<const char*>(const char* s) { |
mbed_official | 0:efe8172b4113 | 122 | separator(); |
mbed_official | 0:efe8172b4113 | 123 | reply += sprintf(reply, "%s", s); |
mbed_official | 0:efe8172b4113 | 124 | } |
mbed_official | 0:efe8172b4113 | 125 | |
mbed_official | 0:efe8172b4113 | 126 | template<> void Reply::putData<char*>(char* s) { |
mbed_official | 0:efe8172b4113 | 127 | separator(); |
mbed_official | 0:efe8172b4113 | 128 | reply += sprintf(reply, "%s", s); |
mbed_official | 0:efe8172b4113 | 129 | } |
mbed_official | 0:efe8172b4113 | 130 | |
mbed_official | 0:efe8172b4113 | 131 | template<> void Reply::putData<char>(char c) { |
mbed_official | 0:efe8172b4113 | 132 | separator(); |
mbed_official | 0:efe8172b4113 | 133 | reply += sprintf(reply, "%c", c); |
mbed_official | 0:efe8172b4113 | 134 | } |
mbed_official | 0:efe8172b4113 | 135 | |
mbed_official | 0:efe8172b4113 | 136 | template<> void Reply::putData<int>(int v) { |
mbed_official | 0:efe8172b4113 | 137 | separator(); |
mbed_official | 0:efe8172b4113 | 138 | reply += sprintf(reply, "%d", v); |
mbed_official | 0:efe8172b4113 | 139 | } |
mbed_official | 0:efe8172b4113 | 140 | |
mbed_official | 0:efe8172b4113 | 141 | template<> void Reply::putData<float>(float f) { |
mbed_official | 0:efe8172b4113 | 142 | separator(); |
mbed_official | 0:efe8172b4113 | 143 | reply += sprintf(reply, "%.17g", f); |
mbed_official | 0:efe8172b4113 | 144 | } |
mbed_official | 0:efe8172b4113 | 145 | |
mbed_official | 0:efe8172b4113 | 146 | } // namespace mbed |