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