Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-rpc by
Arguments.cpp@17:09a13faa045c, 2016-05-31 (annotated)
- Committer:
- abuchan
- Date:
- Tue May 31 16:25:05 2016 +0000
- Revision:
- 17:09a13faa045c
- Parent:
- 11:325e3da833e1
Publishing for Biomimetics.
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 | |
| mbed_official | 0:efe8172b4113 | 82 | template<> const char* Arguments::getArg<const char*>(void) { |
| mbed_official | 0:efe8172b4113 | 83 | index++; |
| mbed_official | 0:efe8172b4113 | 84 | return argv[index]; |
| mbed_official | 0:efe8172b4113 | 85 | } |
| mbed_official | 0:efe8172b4113 | 86 | |
| mbed_official | 0:efe8172b4113 | 87 | template<> char Arguments::getArg<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<> double Arguments::getArg<double>(void) { |
| mbed_official | 0:efe8172b4113 | 93 | index++; |
| mbed_official | 0:efe8172b4113 | 94 | return atof(argv[index]); |
| mbed_official | 0:efe8172b4113 | 95 | } |
| mbed_official | 0:efe8172b4113 | 96 | |
| mbed_official | 0:efe8172b4113 | 97 | template<> float Arguments::getArg<float>(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 | Reply::Reply(char* r) { |
| mbed_official | 0:efe8172b4113 | 103 | first = true; |
| mbed_official | 0:efe8172b4113 | 104 | *r = '\0'; |
| mbed_official | 0:efe8172b4113 | 105 | reply = r; |
| mbed_official | 0:efe8172b4113 | 106 | } |
| mbed_official | 0:efe8172b4113 | 107 | |
| mbed_official | 0:efe8172b4113 | 108 | void Reply::separator(void) { |
| mbed_official | 0:efe8172b4113 | 109 | if (first) { |
| mbed_official | 0:efe8172b4113 | 110 | first = false; |
| mbed_official | 0:efe8172b4113 | 111 | } else { |
| mbed_official | 0:efe8172b4113 | 112 | *reply = ' '; reply++; |
| mbed_official | 0:efe8172b4113 | 113 | } |
| mbed_official | 0:efe8172b4113 | 114 | } |
| mbed_official | 0:efe8172b4113 | 115 | |
| mbed_official | 0:efe8172b4113 | 116 | template<> void Reply::putData<const char*>(const char* s) { |
| mbed_official | 0:efe8172b4113 | 117 | separator(); |
| mbed_official | 0:efe8172b4113 | 118 | reply += sprintf(reply, "%s", s); |
| mbed_official | 0:efe8172b4113 | 119 | } |
| mbed_official | 0:efe8172b4113 | 120 | |
| mbed_official | 0:efe8172b4113 | 121 | template<> void Reply::putData<char*>(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 c) { |
| mbed_official | 0:efe8172b4113 | 127 | separator(); |
| mbed_official | 0:efe8172b4113 | 128 | reply += sprintf(reply, "%c", c); |
| mbed_official | 0:efe8172b4113 | 129 | } |
| mbed_official | 0:efe8172b4113 | 130 | |
| mbed_official | 0:efe8172b4113 | 131 | template<> void Reply::putData<int>(int v) { |
| mbed_official | 0:efe8172b4113 | 132 | separator(); |
| mbed_official | 0:efe8172b4113 | 133 | reply += sprintf(reply, "%d", v); |
| mbed_official | 0:efe8172b4113 | 134 | } |
| mbed_official | 0:efe8172b4113 | 135 | |
| mbed_official | 0:efe8172b4113 | 136 | template<> void Reply::putData<float>(float f) { |
| mbed_official | 0:efe8172b4113 | 137 | separator(); |
| mbed_official | 0:efe8172b4113 | 138 | reply += sprintf(reply, "%.17g", f); |
| mbed_official | 0:efe8172b4113 | 139 | } |
| mbed_official | 0:efe8172b4113 | 140 | |
| abuchan | 17:09a13faa045c | 141 | // put by me to accomodate read_u16() of AnalogIn |
| abuchan | 17:09a13faa045c | 142 | template<> void Reply::putData<unsigned short>(unsigned short uint16) { |
| abuchan | 17:09a13faa045c | 143 | separator(); |
| abuchan | 17:09a13faa045c | 144 | reply += sprintf(reply, "%u", uint16); |
| abuchan | 17:09a13faa045c | 145 | } |
| abuchan | 17:09a13faa045c | 146 | |
| mbed_official | 0:efe8172b4113 | 147 | } // namespace mbed |
