Added NUCLEO-F103RB support
Fork of mbed-rpc by
Arguments.cpp@20:635b0fd3d1bd, 2016-06-16 (annotated)
- Committer:
- olympux
- Date:
- Thu Jun 16 19:21:33 2016 +0000
- Revision:
- 20:635b0fd3d1bd
- Parent:
- 19:e65ed29fd4d1
Added support unsigned short for both getArg and putReply
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:efe8172b4113 | 1 | /* mbed Microcontroller Library |
emilmont | 1:6919289a5946 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 0:efe8172b4113 | 3 | * |
emilmont | 1:6919289a5946 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
emilmont | 1:6919289a5946 | 5 | * you may not use this file except in compliance with the License. |
emilmont | 1:6919289a5946 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:efe8172b4113 | 7 | * |
emilmont | 1:6919289a5946 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:efe8172b4113 | 9 | * |
emilmont | 1:6919289a5946 | 10 | * Unless required by applicable law or agreed to in writing, software |
emilmont | 1:6919289a5946 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
emilmont | 1:6919289a5946 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
emilmont | 1:6919289a5946 | 13 | * See the License for the specific language governing permissions and |
emilmont | 1:6919289a5946 | 14 | * limitations under the License. |
mbed_official | 0:efe8172b4113 | 15 | */ |
mbed_official | 11:325e3da833e1 | 16 | #include "mbed.h" |
mbed_official | 0:efe8172b4113 | 17 | #include "Arguments.h" |
mbed_official | 0:efe8172b4113 | 18 | #include "pinmap.h" |
mbed_official | 0:efe8172b4113 | 19 | |
mbed_official | 0:efe8172b4113 | 20 | using namespace std; |
mbed_official | 0:efe8172b4113 | 21 | |
mbed_official | 0:efe8172b4113 | 22 | namespace mbed { |
mbed_official | 0:efe8172b4113 | 23 | |
mbed_official | 0:efe8172b4113 | 24 | Arguments::Arguments(const char* rqs) { |
mbed_official | 0:efe8172b4113 | 25 | obj_name = NULL; |
mbed_official | 0:efe8172b4113 | 26 | method_name = NULL; |
mbed_official | 0:efe8172b4113 | 27 | argc = 0; |
emilmont | 1:6919289a5946 | 28 | |
mbed_official | 0:efe8172b4113 | 29 | // This copy can be removed if we can assume the request string is |
mbed_official | 0:efe8172b4113 | 30 | // persistent and writable for the duration of the call |
mbed_official | 0:efe8172b4113 | 31 | strcpy(request, rqs); |
emilmont | 1:6919289a5946 | 32 | |
mbed_official | 0:efe8172b4113 | 33 | // Initial '/' |
mbed_official | 0:efe8172b4113 | 34 | char* p = request; |
mbed_official | 0:efe8172b4113 | 35 | if (*p != '/') return; |
mbed_official | 0:efe8172b4113 | 36 | p++; |
emilmont | 1:6919289a5946 | 37 | |
mbed_official | 0:efe8172b4113 | 38 | // Object Name |
mbed_official | 0:efe8172b4113 | 39 | p = search_arg(&obj_name, p, '/'); |
mbed_official | 0:efe8172b4113 | 40 | if (p == NULL) return; |
emilmont | 1:6919289a5946 | 41 | |
mbed_official | 0:efe8172b4113 | 42 | // Method Name |
mbed_official | 0:efe8172b4113 | 43 | p = search_arg(&method_name, p, ' '); |
mbed_official | 0:efe8172b4113 | 44 | if (p == NULL) return; |
emilmont | 1:6919289a5946 | 45 | |
mbed_official | 0:efe8172b4113 | 46 | // Arguments |
mbed_official | 0:efe8172b4113 | 47 | while (true) { |
mbed_official | 0:efe8172b4113 | 48 | argv[argc] = NULL; |
mbed_official | 0:efe8172b4113 | 49 | p = search_arg(&argv[argc], p, ' '); |
mbed_official | 0:efe8172b4113 | 50 | if (argv[argc] != NULL) argc++; |
mbed_official | 0:efe8172b4113 | 51 | if (p == NULL) break; |
mbed_official | 0:efe8172b4113 | 52 | } |
emilmont | 1:6919289a5946 | 53 | |
mbed_official | 0:efe8172b4113 | 54 | index = -1; |
mbed_official | 0:efe8172b4113 | 55 | } |
mbed_official | 0:efe8172b4113 | 56 | |
mbed_official | 0:efe8172b4113 | 57 | char* Arguments::search_arg(char **arg, char *p, char next_sep) { |
mbed_official | 0:efe8172b4113 | 58 | char *s = p; |
mbed_official | 0:efe8172b4113 | 59 | while (true) { |
mbed_official | 0:efe8172b4113 | 60 | if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break; |
mbed_official | 0:efe8172b4113 | 61 | p++; |
mbed_official | 0:efe8172b4113 | 62 | } |
mbed_official | 0:efe8172b4113 | 63 | if (p == s) return NULL; |
mbed_official | 0:efe8172b4113 | 64 | *arg = s; |
mbed_official | 0:efe8172b4113 | 65 | char separator = *p; |
mbed_official | 0:efe8172b4113 | 66 | *p = '\0'; |
mbed_official | 0:efe8172b4113 | 67 | p++; |
mbed_official | 0:efe8172b4113 | 68 | return (separator == next_sep) ? (p) : (NULL); |
mbed_official | 0:efe8172b4113 | 69 | } |
mbed_official | 0:efe8172b4113 | 70 | |
mbed_official | 0:efe8172b4113 | 71 | template<> PinName Arguments::getArg<PinName>(void) { |
mbed_official | 0:efe8172b4113 | 72 | index++; |
mbed_official | 0:efe8172b4113 | 73 | return parse_pins(argv[index]); |
mbed_official | 0:efe8172b4113 | 74 | } |
mbed_official | 0:efe8172b4113 | 75 | |
mbed_official | 0:efe8172b4113 | 76 | template<> int Arguments::getArg<int>(void) { |
mbed_official | 0:efe8172b4113 | 77 | index++; |
mbed_official | 0:efe8172b4113 | 78 | char *pEnd; |
mbed_official | 0:efe8172b4113 | 79 | return strtol(argv[index], &pEnd, 10); |
mbed_official | 0:efe8172b4113 | 80 | } |
mbed_official | 0:efe8172b4113 | 81 | |
olympux | 20:635b0fd3d1bd | 82 | template<> unsigned short Arguments::getArg<unsigned short>(void) { |
olympux | 20:635b0fd3d1bd | 83 | index++; |
olympux | 20:635b0fd3d1bd | 84 | char *pEnd; |
olympux | 20:635b0fd3d1bd | 85 | return (unsigned short)strtol(argv[index], &pEnd, 10); |
olympux | 20:635b0fd3d1bd | 86 | } |
olympux | 20:635b0fd3d1bd | 87 | |
mbed_official | 0:efe8172b4113 | 88 | template<> const char* Arguments::getArg<const char*>(void) { |
mbed_official | 0:efe8172b4113 | 89 | index++; |
mbed_official | 0:efe8172b4113 | 90 | return argv[index]; |
mbed_official | 0:efe8172b4113 | 91 | } |
mbed_official | 0:efe8172b4113 | 92 | |
olympux | 19:e65ed29fd4d1 | 93 | template<> char* Arguments::getArg<char*>(void) { |
olympux | 19:e65ed29fd4d1 | 94 | index++; |
olympux | 19:e65ed29fd4d1 | 95 | if (index < argc) { |
olympux | 19:e65ed29fd4d1 | 96 | return argv[index]; |
olympux | 19:e65ed29fd4d1 | 97 | } |
olympux | 19:e65ed29fd4d1 | 98 | |
olympux | 19:e65ed29fd4d1 | 99 | return 0; |
olympux | 19:e65ed29fd4d1 | 100 | } |
olympux | 19:e65ed29fd4d1 | 101 | |
mbed_official | 0:efe8172b4113 | 102 | template<> char Arguments::getArg<char>(void) { |
mbed_official | 0:efe8172b4113 | 103 | index++; |
mbed_official | 0:efe8172b4113 | 104 | return *argv[index]; |
mbed_official | 0:efe8172b4113 | 105 | } |
mbed_official | 0:efe8172b4113 | 106 | |
mbed_official | 0:efe8172b4113 | 107 | template<> double Arguments::getArg<double>(void) { |
mbed_official | 0:efe8172b4113 | 108 | index++; |
mbed_official | 0:efe8172b4113 | 109 | return atof(argv[index]); |
mbed_official | 0:efe8172b4113 | 110 | } |
mbed_official | 0:efe8172b4113 | 111 | |
mbed_official | 0:efe8172b4113 | 112 | template<> float Arguments::getArg<float>(void) { |
mbed_official | 0:efe8172b4113 | 113 | index++; |
mbed_official | 0:efe8172b4113 | 114 | return atof(argv[index]); |
mbed_official | 0:efe8172b4113 | 115 | } |
mbed_official | 0:efe8172b4113 | 116 | |
mbed_official | 0:efe8172b4113 | 117 | Reply::Reply(char* r) { |
mbed_official | 0:efe8172b4113 | 118 | first = true; |
mbed_official | 0:efe8172b4113 | 119 | *r = '\0'; |
mbed_official | 0:efe8172b4113 | 120 | reply = r; |
mbed_official | 0:efe8172b4113 | 121 | } |
mbed_official | 0:efe8172b4113 | 122 | |
mbed_official | 0:efe8172b4113 | 123 | void Reply::separator(void) { |
mbed_official | 0:efe8172b4113 | 124 | if (first) { |
mbed_official | 0:efe8172b4113 | 125 | first = false; |
mbed_official | 0:efe8172b4113 | 126 | } else { |
mbed_official | 0:efe8172b4113 | 127 | *reply = ' '; reply++; |
mbed_official | 0:efe8172b4113 | 128 | } |
mbed_official | 0:efe8172b4113 | 129 | } |
mbed_official | 0:efe8172b4113 | 130 | |
mbed_official | 0:efe8172b4113 | 131 | template<> void Reply::putData<const char*>(const char* s) { |
mbed_official | 0:efe8172b4113 | 132 | separator(); |
mbed_official | 0:efe8172b4113 | 133 | reply += sprintf(reply, "%s", s); |
mbed_official | 0:efe8172b4113 | 134 | } |
mbed_official | 0:efe8172b4113 | 135 | |
mbed_official | 0:efe8172b4113 | 136 | template<> void Reply::putData<char*>(char* s) { |
mbed_official | 0:efe8172b4113 | 137 | separator(); |
mbed_official | 0:efe8172b4113 | 138 | reply += sprintf(reply, "%s", s); |
mbed_official | 0:efe8172b4113 | 139 | } |
mbed_official | 0:efe8172b4113 | 140 | |
mbed_official | 0:efe8172b4113 | 141 | template<> void Reply::putData<char>(char c) { |
mbed_official | 0:efe8172b4113 | 142 | separator(); |
mbed_official | 0:efe8172b4113 | 143 | reply += sprintf(reply, "%c", c); |
mbed_official | 0:efe8172b4113 | 144 | } |
mbed_official | 0:efe8172b4113 | 145 | |
mbed_official | 0:efe8172b4113 | 146 | template<> void Reply::putData<int>(int v) { |
mbed_official | 0:efe8172b4113 | 147 | separator(); |
mbed_official | 0:efe8172b4113 | 148 | reply += sprintf(reply, "%d", v); |
mbed_official | 0:efe8172b4113 | 149 | } |
mbed_official | 0:efe8172b4113 | 150 | |
olympux | 18:e6a835c40639 | 151 | template<> void Reply::putData<unsigned short>(unsigned short uint16) { |
olympux | 18:e6a835c40639 | 152 | separator(); |
olympux | 18:e6a835c40639 | 153 | reply += sprintf(reply, "%u", uint16); |
olympux | 18:e6a835c40639 | 154 | } |
olympux | 18:e6a835c40639 | 155 | |
mbed_official | 0:efe8172b4113 | 156 | template<> void Reply::putData<float>(float f) { |
mbed_official | 0:efe8172b4113 | 157 | separator(); |
mbed_official | 0:efe8172b4113 | 158 | reply += sprintf(reply, "%.17g", f); |
mbed_official | 0:efe8172b4113 | 159 | } |
mbed_official | 0:efe8172b4113 | 160 | |
mbed_official | 0:efe8172b4113 | 161 | } // namespace mbed |