Increasing limit on 4 arguments with rpc
I've been trying to increase the number of arguments you can use with rpc. The reason for this was that I wanted to use rpc to send some numbers to other components using the I2C bus. The rpc_method_caller is overloaded to enable multiple argument types but this has only been limited up to 4 (which includes a function name).
Just FYI, I've been using the rpc class that was implemented here by Ilya - therefore I wanted to be able to browse to this address:
http://mbed_ip_address/rpc/myrpc1/i2c,160,1,2,3
Which ran the function called i2c and passed the 4 arguments. With 3 arguments it runs with no problem but more than that and the error message - No instance of overloaded function "mbed::rpc_method_caller" matches the required type (E386).
To increase you need to do the following....
1. Insert a modified Base.h file (we'll call it Base_extended.h) into your project and include that instead. Therefore, replace:
#include "Base.h"
with
#include "Base_extended.h"
within any project files. Create the file "Base_extended.h" and copy the modified code:
/* mbed Microcontroller Library - Base * Copyright (c) 2006-2008 ARM Limited. All rights reserved. * sford, jbrawn */ #ifndef MBED_BASE_H #define MBED_BASE_H #include "platform.h" #include "PinNames.h" #include "PeripheralNames.h" #include #include "DirHandle.h" namespace mbed { #ifdef MBED_RPC struct rpc_function { const char *name; void (*caller)(const char*, char*); }; struct rpc_class { const char *name; const rpc_function *static_functions; struct rpc_class *next; }; #endif /* Class Base * The base class for most things */ class Base { public: Base(const char *name = NULL); virtual ~Base(); /* Function register_object * Registers this object with the given name, so that it can be * looked up with lookup. If this object has already been * registered, then this just changes the name. * * Variables * name - The name to give the object. If NULL we do nothing. */ void register_object(const char *name); /* Function name * Returns the name of the object, or NULL if it has no name. */ const char *name(); #ifdef MBED_RPC /* Function rpc * Call the given method with the given arguments, and write the * result into the string pointed to by result. The default * implementation calls rpc_methods to determine the supported * methods. * * Variables * method - The name of the method to call. * arguments - A list of arguments separated by spaces. * result - A pointer to a string to write the result into. May * be NULL, in which case nothing is written. * * Returns * true if method corresponds to a valid rpc method, or * false otherwise. */ virtual bool rpc(const char *method, const char *arguments, char *result); /* Function get_rpc_methods * Returns a pointer to an array describing the rpc methods * supported by this object, terminated by either * RPC_METHOD_END or RPC_METHOD_SUPER(Superclass). * * Example * > class Example : public Base { * > int foo(int a, int b) { return a + b; } * > virtual const struct rpc_method *get_rpc_methods() { * > static const rpc_method rpc_methods[] = { * > { "foo", generic_caller }, * > RPC_METHOD_SUPER(Base) * > }; * > return rpc_methods; * > } * > }; */ virtual const struct rpc_method *get_rpc_methods(); /* Function rpc * Use the lookup function to lookup an object and, if * successful, call its rpc method * * Variables * returns - false if name does not correspond to an object, * otherwise the return value of the call to the object's rpc * method. */ static bool rpc(const char *name, const char *method, const char *arguments, char *result); #endif /* Function lookup * Lookup and return the object that has the given name. * * Variables * name - the name to lookup. * len - the length of name. */ static Base *lookup(const char *name, unsigned int len); static DirHandle *opendir(); friend class BaseDirHandle; protected: static Base *_head; Base *_next; const char *_name; bool _from_construct; private: #ifdef MBED_RPC static rpc_class *_classes; static const rpc_function _base_funcs[]; static rpc_class _base_class; #endif void delete_self(); static void list_objs(const char *arguments, char *result); static void clear(const char*,char*); static char *new_name(Base *p); public: #ifdef MBED_RPC /* Function add_rpc_class * Add the class to the list of classes which can have static * methods called via rpc (the static methods which can be called * are defined by that class' get_rpc_class() static method). */ template static void add_rpc_class() { rpc_class *c = C::get_rpc_class(); c->next = _classes; _classes = c; } template static const char *construct() { Base *p = new C(); p->_from_construct = true; if(p->_name==NULL) { p->register_object(new_name(p)); } return p->_name; } template static const char *construct(A1 arg1) { Base *p = new C(arg1); p->_from_construct = true; if(p->_name==NULL) { p->register_object(new_name(p)); } return p->_name; } template static const char *construct(A1 arg1, A2 arg2) { Base *p = new C(arg1,arg2); p->_from_construct = true; if(p->_name==NULL) { p->register_object(new_name(p)); } return p->_name; } template static const char *construct(A1 arg1, A2 arg2, A3 arg3) { Base *p = new C(arg1,arg2,arg3); p->_from_construct = true; if(p->_name==NULL) { p->register_object(new_name(p)); } return p->_name; } template static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) { Base *p = new C(arg1,arg2,arg3,arg4); p->_from_construct = true; if(p->_name==NULL) { p->register_object(new_name(p)); } return p->_name; } /* extended by mjt (18/5/10) to allow more arguments */ template static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5) { Base *p = new C(arg1,arg2,arg3,arg4,arg5); p->_from_construct = true; if(p->_name==NULL) { p->register_object(new_name(p)); } return p->_name; } template static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5, A6 arg6) { Base *p = new C(arg1,arg2,arg3,arg4,arg5, arg6); p->_from_construct = true; if(p->_name==NULL) { p->register_object(new_name(p)); } return p->_name; } #endif }; /* Macro MBED_OBJECT_NAME_MAX * The maximum size of object name (including terminating null byte) * that will be recognised when using fopen to open a FileLike * object, or when using the rpc function. */ #define MBED_OBJECT_NAME_MAX 32 /* Macro MBED_METHOD_NAME_MAX * The maximum size of rpc method name (including terminating null * byte) that will be recognised by the rpc function (in rpc.h). */ #define MBED_METHOD_NAME_MAX 32 } // namespace mbed #endif
You'll notice towards the end of the file I've added 2 extra templates which handle 5 and 6 arguments - if you want more than this then just add further templates.
2. You're going to need to do a very similar thing with rpc.h. Create a new file called 'rpc_extended.h' within your project and paste the following code. You'll see the modified code about two thirds down which extends the rpc_method_caller. Remember to replace '#include rpc.h' with '#include rpc_extended.h' in all your project code
/* mbed Microcontroller Library - RPC * Copyright (c) 2008-2009 ARM Limited. All rights reserved. * sford */ #ifndef MBED_RPC_H #define MBED_RPC_H /* Section rpc * Helpers for rpc handling. */ #include #include #include #include #include "Base.h" #include "PinNames.h" #include namespace mbed { /* Function parse_arg * Parses and returns a value from a string. * * Variable * arg - The string to pase * next - If not NULL a pointer to after the last * character parsed is written here */ template T parse_arg(const char *arg, const char **next); inline char parse_char(const char *arg, const char **next) { char c = *arg++; if(c == '\\') { c = *arg++; switch(c) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 't': c = '\t'; break; case 'n': c = '\n'; break; case 'v': c = '\v'; break; case 'f': c = '\f'; break; case 'r': c = '\r'; break; case 'x': { /* two-character hexadecimal */ char buf[3]; buf[0] = *arg++; buf[1] = *arg++; buf[2] = 0; c = strtol(buf, NULL, 16); } break; default: if(isdigit(c)) { /* three-character octal */ char buf[4]; buf[0] = c; buf[1] = *arg++; buf[2] = *arg++; buf[3] = 0; c = strtol(buf, NULL, 8); } break; } } *next = arg; return c; } /* signed integer types */ template<> inline int parse_arg(const char *arg, const char **next) { if(arg[0] == '\'') { char c = parse_char(arg+1, &arg); if(next != NULL) *next = arg+1; return c; } else { return strtol(arg, const_cast(next), 0); } } template<> inline char parse_arg(const char *arg, const char **next) { return parse_arg(arg,next); } template<> inline short int parse_arg(const char *arg, const char **next) { return parse_arg(arg,next); } template<> inline long int parse_arg(const char *arg, const char **next) { return parse_arg(arg,next); } template<> inline long long parse_arg(const char *arg, const char **next) { return strtoll(arg, const_cast(next), 0); } /* unsigned integer types */ template<> inline unsigned int parse_arg(const char *arg, const char **next) { if(arg[0] == '\'') { char c = parse_char(arg+1, &arg); if(next != NULL) *next = arg+1; return c; } else { return strtoul(arg, const_cast(next), 0); } } template<> inline unsigned char parse_arg(const char *arg, const char **next) { return parse_arg(arg,next); } template<> inline unsigned short int parse_arg(const char *arg, const char **next) { return parse_arg(arg,next); } template<> inline unsigned long int parse_arg(const char *arg, const char **next) { return parse_arg(arg,next); } template<> inline unsigned long long parse_arg(const char *arg, const char **next) { return strtoull(arg, const_cast(next), 0); } /* floating types */ template<> inline float parse_arg(const char *arg, const char **next) { #if !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 410000 return strtof(arg,const_cast(next)); #elif __ARMCC_VERSION >= 310000 /* bug in header means no using declaration for strtof */ return std::strtof(arg,const_cast(next)); #else /* strtof not supported */ return strtod(arg,const_cast(next)); #endif } template<> inline double parse_arg(const char *arg, const char **next) { return strtod(arg,const_cast(next)); } template<> inline long double parse_arg(const char *arg, const char **next) { return strtod(arg,const_cast(next)); } /* string */ template<> inline char *parse_arg(const char *arg, const char **next) { const char *ptr = arg; char *res = NULL; if(*arg == '"') { /* quoted string */ ptr = ++arg; int len = 0; /* find the end (and length) of the quoted string */ for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) { len++; if(c == '\\') { ptr++; } } /* copy the quoted string, and unescape characters */ if(len != 0) { res = new char[len+1]; char *resptr = res; while(arg != ptr) { *resptr++ = parse_char(arg, &arg); } *resptr = 0; } } else { /* unquoted string */ while(isalnum(*ptr) || *ptr=='_') { ptr++; } int len = ptr-arg; if(len!=0) { res = new char[len+1]; memcpy(res, arg, len); res[len] = 0; } } if(next != NULL) { *next = ptr; } return res; } template<> inline const char *parse_arg(const char *arg, const char **next) { return parse_arg(arg,next); } /* Pins */ inline PinName parse_pins(const char *str) { const PinName pin_names[] = {p5, p6, p7, p8, p9, p10, p11, p12, p13, p14 , p15, p16, p17, p18, p19, p20, p21, p22, p23 , p24, p25, p26, p27, p28, p29, p30}; if(str[0] == 'P') { // Pn_n uint32_t port = str[1] - '0'; uint32_t pin = str[3] - '0'; // Pn_n uint32_t pin2 = str[4] - '0'; // Pn_nn if(pin2 <= 9) { pin = pin * 10 + pin2; } return (PinName)(LPC_GPIO0_BASE + port * 32 + pin); } else if(str[0] == 'p') { // pn uint32_t pin = str[1] - '0'; // pn uint32_t pin2 = str[2] - '0'; // pnn if(pin2 <= 9) { pin = pin * 10 + pin2; } if(pin < 5 || pin > 30) { return NC; } return pin_names[pin - 5]; } else if(str[0] == 'L') { // LEDn switch(str[3]) { case '1' : return LED1; case '2' : return LED2; case '3' : return LED3; case '4' : return LED4; } } else if(str[0] == 'U') { // USB?X switch(str[3]) { case 'T' : return USBTX; case 'R' : return USBRX; } } return NC; } template<> inline PinName parse_arg(const char *arg, const char **next) { const char *ptr = arg; PinName pinname = NC; while(isalnum(*ptr) || *ptr=='_') { ptr++; } int len = ptr-arg; if(len!=0) { pinname = parse_pins(arg); } if(next != NULL) { *next = ptr; } return pinname; } /* Function write_result * Writes a value in to a result string in an appropriate manner * * Variable * val - The value to write * result - A pointer to the array to write the value into */ template void write_result(T val, char *result); /* signed integer types */ template<> inline void write_result(char val, char *result) { result[0] = val; result[1] = '\0'; } template<> inline void write_result(short int val, char *result) { sprintf(result, "%hi", val); } template<> inline void write_result(int val, char *result) { sprintf(result, "%i", val); } template<> inline void write_result(long int val, char *result) { sprintf(result, "%li", val); } template<> inline void write_result(long long int val, char *result) { sprintf(result, "%lli", val); } /* unsigned integer types */ template<> inline void write_result(unsigned char val, char *result) { result[0] = val; result[1] = '\0'; } template<> inline void write_result(unsigned short int val, char *result) { sprintf(result, "%hu", val); } template<> inline void write_result(unsigned int val, char *result) { sprintf(result, "%u", val); } template<> inline void write_result(unsigned long int val, char *result) { sprintf(result, "%lu", val); } template<> inline void write_result(unsigned long long int val, char *result) { sprintf(result, "%llu", val); } /* floating types */ template<> inline void write_result(float val, char *result) { sprintf(result, "%.17g", val); } template<> inline void write_result(double val, char *result) { sprintf(result, "%.17g", val); } template<> inline void write_result(long double val, char *result) { sprintf(result, "%.17Lg", val); } /* string */ template<> inline void write_result(char *val, char *result) { if(val==NULL) { result[0] = 0; } else { strcpy(result, val); } } template<> inline void write_result(const char *val, char *result) { if(val==NULL) { result[0] = 0; } else { strcpy(result, val); } } inline const char *next_arg(const char* next) { while(*next == ' ') next++; if(*next == ',' || *next == '?') next++; while(*next == ' ') next++; return next; } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { (static_cast(this_ptr)->*member)(arguments,result); } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { (static_cast(this_ptr)->*member)(); if(result != NULL) { result[0] = '\0'; } } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),NULL); (static_cast(this_ptr)->*member)(arg1); if(result != NULL) { result[0] = '\0'; } } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),NULL); (static_cast(this_ptr)->*member)(arg1,arg2); if(result != NULL) { result[0] = '\0'; } } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),&next); A3 arg3 = parse_arg(next_arg(next),NULL); (static_cast(this_ptr)->*member)(arg1,arg2,arg3); if(result != NULL) { result[0] = '\0'; } } /* Function rpc_method_caller added by mjt (18/5/10) to enable four arguments in rpc method call */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),&next); A3 arg3 = parse_arg(next_arg(next),&next); A4 arg4 = parse_arg(next_arg(next),NULL); (static_cast(this_ptr)->*member)(arg1,arg2,arg3,arg4); if(result != NULL) { result[0] = '\0'; } } /* Function rpc_method_caller added by mjt (18/5/10) to enable five arguments in rpc method call */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),&next); A3 arg3 = parse_arg(next_arg(next),&next); A4 arg4 = parse_arg(next_arg(next),&next); A5 arg5 = parse_arg(next_arg(next),NULL); (static_cast(this_ptr)->*member)(arg1,arg2,arg3,arg4,arg5); if(result != NULL) { result[0] = '\0'; } } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { R res = (static_cast(this_ptr)->*member)(); if(result != NULL) { write_result(res, result); } } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),NULL); R res = (static_cast(this_ptr)->*member)(arg1); if(result != NULL) { write_result(res, result); } } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),NULL); R res = (static_cast(this_ptr)->*member)(arg1,arg2); if(result != NULL) { write_result(res, result); } } /* Function rpc_method_caller */ template void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),&next); A3 arg3 = parse_arg(next_arg(next),NULL); R res = (static_cast(this_ptr)->*member)(arg1,arg2,arg3); if(result != NULL) { write_result(res, result); } } /* Function rpc_function caller */ template void rpc_function_caller(const char *arguments, char *result) { R res = (*func)(); if(result != NULL) { write_result(res, result); } } /* Function rpc_function caller */ template void rpc_function_caller(const char *arguments, char *result) { A1 arg1 = parse_arg(next_arg(arguments),NULL); R res = (*func)(arg1); if(result != NULL) { write_result(res, result); } } /* Function rpc_function caller */ template void rpc_function_caller(const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),NULL); R res = (*func)(arg1,arg2); if(result != NULL) { write_result(res, result); } } /* Function rpc_function caller */ template void rpc_function_caller(const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),&next); A3 arg3 = parse_arg(next_arg(next),NULL); R res = (*func)(arg1,arg2,arg3); if(result != NULL) { write_result(res, result); } } /* Function rpc_function caller */ template void rpc_function_caller(const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),&next); A3 arg3 = parse_arg(next_arg(next),&next); A4 arg4 = parse_arg(next_arg(next),NULL); R res = (*func)(arg1,arg2,arg3,arg4); if(result != NULL) { write_result(res, result); } } template void rpc_function_caller(const char *arguments, char *result) { const char *next = arguments; A1 arg1 = parse_arg(next_arg(next),&next); A2 arg2 = parse_arg(next_arg(next),&next); A3 arg3 = parse_arg(next_arg(next),&next); A4 arg4 = parse_arg(next_arg(next),&next); A5 arg5 = parse_arg(next_arg(next),NULL); R res = (*func)(arg1,arg2,arg3,arg4,arg5); if(result != NULL) { write_result(res, result); } } struct rpc_method { const char *name; typedef void (*caller_t)(Base*, const char*, char*); typedef const struct rpc_method *(*super_t)(Base*); union { caller_t caller; super_t super; }; }; template const struct rpc_method *rpc_super(Base *this_ptr) { return static_cast(this_ptr)->C::get_rpc_methods(); } #define RPC_METHOD_END { NULL, NULL } #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::caller_t)(rpc_method::super_t)rpc_super } /* Function rpc * Parse a string describing a call and then do it * * Variables * call - A pointer to a string describing the call, which has * the form /object/method arg ... argn. Arguments are * delimited by space characters, and the string is terminated * by a null character. * result - A pointer to an array to write the result into. */ bool rpc(const char *buf, char *result = 0); } // namespace mbed #endif
0 comments
You need to log in to post a comment