TX1 node

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Arguments.cpp Source File

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 "mbed.h"
00017 #include "Arguments.h"
00018 #include "pinmap.h"
00019 
00020 using namespace std;
00021 
00022 namespace mbed {
00023 
00024 Arguments::Arguments(const char* rqs) {
00025     obj_name = NULL;
00026     method_name = NULL;
00027     argc = 0;
00028 
00029     // This copy can be removed if we can assume the request string is
00030     // persistent and writable for the duration of the call
00031     strcpy(request, rqs);
00032 
00033     // Initial '/'
00034     char* p = request;
00035     if (*p != '/') return;
00036     p++;
00037 
00038     // Object Name
00039     p = search_arg(&obj_name, p, '/');
00040     if (p == NULL) return;
00041 
00042     // Method Name
00043     p = search_arg(&method_name, p, ' ');
00044     if (p == NULL) return;
00045 
00046     // Arguments
00047     while (true) {
00048         argv[argc] = NULL;
00049         p = search_arg(&argv[argc], p, ' ');
00050         if (argv[argc] != NULL) argc++;
00051         if (p == NULL) break;
00052     }
00053 
00054     index = -1;
00055 }
00056 
00057 char* Arguments::search_arg(char **arg, char *p, char next_sep) {
00058     char *s = p;
00059     while (true) {
00060         if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break;
00061         p++;
00062     }
00063     if (p == s) return NULL;
00064     *arg = s;
00065     char separator = *p;
00066     *p = '\0';
00067     p++;
00068     return (separator == next_sep) ? (p) : (NULL);
00069 }
00070 
00071 template<> PinName Arguments::getArg<PinName>(void) {
00072     index++;
00073     return parse_pins(argv[index]);
00074 }
00075 
00076 template<> int Arguments::getArg<int>(void) {
00077     index++;
00078     char *pEnd;
00079     return strtol(argv[index], &pEnd, 10);
00080 }
00081 
00082 template<> unsigned short Arguments::getArg<unsigned short>(void) {
00083     index++;
00084     char *pEnd;
00085     return (unsigned short)strtol(argv[index], &pEnd, 10);
00086 }
00087 
00088 template<> const char* Arguments::getArg<const char*>(void) {
00089     index++;
00090     return argv[index];
00091 }
00092 
00093 template<> char* Arguments::getArg<char*>(void) {
00094     index++;
00095     if (index < argc) {
00096         return argv[index];
00097     }
00098     
00099     return 0;
00100 }
00101 
00102 template<> char Arguments::getArg<char>(void) {
00103     index++;
00104     return *argv[index];
00105 }
00106 
00107 template<> double Arguments::getArg<double>(void) {
00108     index++;
00109     return atof(argv[index]);
00110 }
00111 
00112 template<> float Arguments::getArg<float>(void) {
00113     index++;
00114     return atof(argv[index]);
00115 }
00116 
00117 Reply::Reply(char* r) {
00118     first = true;
00119     *r = '\0';
00120     reply = r;
00121 }
00122 
00123 void Reply::separator(void) {
00124     if (first) {
00125         first = false;
00126     } else {
00127         *reply = ' '; reply++;
00128     }
00129 }
00130 
00131 template<> void Reply::putData<const char*>(const char* s) {
00132     separator();
00133     reply += sprintf(reply, "%s", s);
00134 }
00135 
00136 template<> void Reply::putData<char*>(char* s) {
00137     separator();
00138     reply += sprintf(reply, "%s", s);
00139 }
00140 
00141 template<> void Reply::putData<char>(char c) {
00142     separator();
00143     reply += sprintf(reply, "%c", c);
00144 }
00145 
00146 template<> void Reply::putData<int>(int v) {
00147     separator();
00148     reply += sprintf(reply, "%d", v);
00149 }
00150 
00151 template<> void Reply::putData<unsigned short>(unsigned short uint16) {
00152     separator();
00153     reply += sprintf(reply, "%u", uint16);
00154 }
00155 
00156 template<> void Reply::putData<float>(float f) {
00157     separator();
00158     reply += sprintf(reply, "%.17g", f);
00159 }
00160 
00161 } // namespace mbed