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