mbed RPC
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<> double Arguments::getArg<double>(void) { 00092 index++; 00093 return atof(argv[index]); 00094 } 00095 00096 template<> float Arguments::getArg<float>(void) { 00097 index++; 00098 return atof(argv[index]); 00099 } 00100 00101 Reply::Reply(char* r) { 00102 first = true; 00103 *r = '\0'; 00104 reply = r; 00105 } 00106 00107 void Reply::separator(void) { 00108 if (first) { 00109 first = false; 00110 } else { 00111 *reply = ' '; reply++; 00112 } 00113 } 00114 00115 template<> void Reply::putData<const char*>(const char* s) { 00116 separator(); 00117 reply += sprintf(reply, "%s", s); 00118 } 00119 00120 template<> void Reply::putData<char*>(char* s) { 00121 separator(); 00122 reply += sprintf(reply, "%s", s); 00123 } 00124 00125 template<> void Reply::putData<char>(char c) { 00126 separator(); 00127 reply += sprintf(reply, "%c", c); 00128 } 00129 00130 template<> void Reply::putData<int>(int v) { 00131 separator(); 00132 reply += sprintf(reply, "%d", v); 00133 } 00134 00135 template<> void Reply::putData<float>(float f) { 00136 separator(); 00137 reply += sprintf(reply, "%.17g", f); 00138 } 00139 00140 } // namespace mbed
Generated on Sun Jul 24 2022 15:11:06 by 1.7.2