Robin Hourahane / Improved-mbed-rpc

Fork of mbed-rpc by mbed official

Arguments.cpp

Committer:
rhourahane
Date:
2015-03-30
Revision:
10:7511acfaa62d
Parent:
9:6ce5db613c77

File content as of revision 10:7511acfaa62d:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "Arguments.h"
#include "pinmap.h"

using namespace std;

namespace mbed {

Arguments::Arguments(const char* rqs) {
    obj_name = NULL;
    method_name = NULL;
    argc = 0;

    // This copy can be removed if we can assume the request string is
    // persistent and writable for the duration of the call
    strncpy(request, rqs, RPC_MAX_STRING);
    request[RPC_MAX_STRING] = '\0';

    // Initial '/'
    char* p = request;
    if (*p != '/') return;
    p++;

    // Object Name
    p = search_arg(&obj_name, p, '/');
    if (p == NULL) return;

    // Method Name
    p = search_arg(&method_name, p, ' ');
    if (p == NULL) return;

    // Arguments
    while (argc < RPC_MAX_ARGS) {
        argv[argc] = NULL;
        p = search_arg(&argv[argc], p, ' ');
        if (argv[argc] != NULL) {
             argc++;
        }
        if (p == NULL) {
            break;
        }
    }

    index = -1;
}

char* Arguments::search_arg(char **arg, char *p, char next_sep) {
    char *s = p;
    while (true) {
        if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break;
        p++;
    }
    if (p == s) return NULL;
    *arg = s;
    char separator = *p;
    *p = '\0';
    p++;
    return (separator == next_sep) ? (p) : (NULL);
}

template<> PinName Arguments::getArg<PinName>(void) {
    index++;
    if (index < argc) {
        return parse_pins(argv[index]);
    }
    
    return NC;
}

template<> int Arguments::getArg<int>(void) {
    index++;
    if (index < argc) {
        char *pEnd;
        return strtol(argv[index], &pEnd, 10);
    }
    
    return 0;
}

template<> const char* Arguments::getArg<const char*>(void) {
    index++;
    if (index < argc) {
        return argv[index];
    }
    
    return 0;
}

template<> char* Arguments::getArg<char*>(void) {
    index++;
    if (index < argc) {
        return argv[index];
    }
    
    return 0;
}

template<> char Arguments::getArg<char>(void) {
    index++;
    if (index < argc) {
        return *argv[index];
    }
    
    return '\0';
}

template<> double Arguments::getArg<double>(void) {
    index++;
    if (index < argc) {
        return atof(argv[index]);
    }
    
    return 0.0;
}

template<> float Arguments::getArg<float>(void) {
    index++;
    if (index < argc) {
        return atof(argv[index]);
    }
    
    return 0.0f;
}

template<> unsigned short Arguments::getArg<unsigned short>(void) {
    index++;
    if (index < RPC_MAX_ARGS) {
        char *pEnd;
        return strtoul(argv[index], &pEnd, 10);
    }
    
    return 0;
}

Reply::Reply(char* r) {
    first = true;
    *r = '\0';
    reply = r;
    space = RPC_MAX_STRING;
}

void Reply::separator(void) {
    if (first) {
        first = false;
    } else {
        *reply = ' '; reply++;
        --space;
    }
}

template<> void Reply::putData<const char*>(const char* s) {
    separator();
    int used = snprintf(reply, space, "%s", s);
    reply += used;
    space -= used;
}

template<> void Reply::putData<char*>(char* s) {
    separator();
    int used = snprintf(reply, space, "%s", s);
    reply += used;
    space -= used;
}

template<> void Reply::putData<char>(char c) {
    separator();
    int used = snprintf(reply, space, "%c", c);
    reply += used;
    space -= used;
}

template<> void Reply::putData<int>(int v) {
    separator();
    int used = snprintf(reply, space, "%d", v);
    reply += used;
    space -= used;
}

template<> void Reply::putData<float>(float f) {
    separator();
    int used = snprintf(reply, space, "%.17g", f);
    reply += used;
    space -= used;
}

template<> void Reply::putData<unsigned short>(unsigned short v) {
    separator();
    int used = snprintf(reply, space, "%u", v);
    reply += used;
    space -= used;
}

} // namespace mbed