mbed RPC
Fork of mbed-rpc by
Embed:
(wiki syntax)
Show/hide line numbers
Arguments.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "Arguments.h" 00017 #include "pinmap.h" 00018 00019 using namespace std; 00020 00021 namespace mbed { 00022 00023 Arguments::Arguments(const char* rqs) { 00024 obj_name = NULL; 00025 method_name = NULL; 00026 argc = 0; 00027 00028 // This copy can be removed if we can assume the request string is 00029 // persistent and writable for the duration of the call 00030 strcpy(request, rqs); 00031 00032 // Initial '/' 00033 char* p = request; 00034 if (*p != '/') return; 00035 p++; 00036 00037 // Object Name 00038 p = search_arg(&obj_name, p, '/'); 00039 if (p == NULL) return; 00040 00041 // Method Name 00042 p = search_arg(&method_name, p, ' '); 00043 if (p == NULL) return; 00044 00045 // Arguments 00046 while (true) { 00047 argv[argc] = NULL; 00048 p = search_arg(&argv[argc], p, ' '); 00049 if (argv[argc] != NULL) argc++; 00050 if (p == NULL) break; 00051 } 00052 00053 index = -1; 00054 } 00055 00056 char* Arguments::search_arg(char **arg, char *p, char next_sep) { 00057 char *s = p; 00058 while (true) { 00059 if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break; 00060 p++; 00061 } 00062 if (p == s) return NULL; 00063 *arg = s; 00064 char separator = *p; 00065 *p = '\0'; 00066 p++; 00067 return (separator == next_sep) ? (p) : (NULL); 00068 } 00069 00070 template<> PinName Arguments::getArg<PinName>(void) { 00071 index++; 00072 return parse_pins(argv[index]); 00073 } 00074 00075 template<> int Arguments::getArg<int>(void) { 00076 index++; 00077 char *pEnd; 00078 return strtol(argv[index], &pEnd, 10); 00079 } 00080 00081 template<> const char* Arguments::getArg<const char*>(void) { 00082 index++; 00083 return argv[index]; 00084 } 00085 00086 template<> char* Arguments::getArg<char*>(void) { 00087 index++; 00088 return argv[index]; 00089 } 00090 00091 template<> char Arguments::getArg<char>(void) { 00092 index++; 00093 return *argv[index]; 00094 } 00095 00096 template<> double Arguments::getArg<double>(void) { 00097 index++; 00098 return atof(argv[index]); 00099 } 00100 00101 template<> float Arguments::getArg<float>(void) { 00102 index++; 00103 return atof(argv[index]); 00104 } 00105 00106 template<> bool Arguments::getArg<bool>(void) { 00107 index++; 00108 return atof(argv[index]); 00109 } 00110 00111 Reply::Reply(char* r) { 00112 first = true; 00113 *r = '\0'; 00114 reply = r; 00115 } 00116 00117 void Reply::separator(void) { 00118 if (first) { 00119 first = false; 00120 } else { 00121 *reply = ' '; reply++; 00122 } 00123 } 00124 00125 template<> void Reply::putData<const char*>(const char* s) { 00126 separator(); 00127 reply += sprintf(reply, "%s", s); 00128 } 00129 00130 template<> void Reply::putData<char*>(char* s) { 00131 separator(); 00132 reply += sprintf(reply, "%s", s); 00133 } 00134 00135 template<> void Reply::putData<char>(char c) { 00136 separator(); 00137 reply += sprintf(reply, "%c", c); 00138 } 00139 00140 template<> void Reply::putData<int>(int v) { 00141 separator(); 00142 reply += sprintf(reply, "%d", v); 00143 } 00144 00145 template<> void Reply::putData<float>(float f) { 00146 separator(); 00147 reply += sprintf(reply, "%.17g", f); 00148 } 00149 00150 } // namespace mbed
Generated on Tue Jul 12 2022 21:49:03 by
![doxygen](doxygen.png)