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