diff options
author | Jun Wako <wakojun@gmail.com> | 2015-04-24 16:26:14 +0900 |
---|---|---|
committer | Jun Wako <wakojun@gmail.com> | 2015-04-24 16:26:14 +0900 |
commit | 1fe4406f374291ab2e86e95a97341fd9c475fcb8 (patch) | |
tree | 1be0e16b4b07b5a31ea97ec50a9eb13a288c3d27 /tool/mbed/mbed-sdk/libraries/rpc | |
parent | a20ef7052c6e937d2f7672dd59456e55a5c08296 (diff) |
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
b9e0ea0 Merge commit '7fa9d8bdea3773d1195b04d98fcf27cf48ddd81d' as 'tool/mbed/mbed-sdk'
7fa9d8b Squashed 'tool/mbed/mbed-sdk/' content from commit 7c21ce5
git-subtree-dir: tmk_core
git-subtree-split: b9e0ea08cb940de20b3610ecdda18e9d8cd7c552
Diffstat (limited to 'tool/mbed/mbed-sdk/libraries/rpc')
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/Arguments.cpp | 140 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/Arguments.h | 64 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.cpp | 42 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.h | 59 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/RPCVariable.h | 87 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/RpcClasses.h | 308 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/mbed_rpc.h | 25 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/parse_pins.cpp | 105 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/parse_pins.h | 27 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/rpc.cpp | 191 | ||||
-rw-r--r-- | tool/mbed/mbed-sdk/libraries/rpc/rpc.h | 313 |
11 files changed, 1361 insertions, 0 deletions
diff --git a/tool/mbed/mbed-sdk/libraries/rpc/Arguments.cpp b/tool/mbed/mbed-sdk/libraries/rpc/Arguments.cpp new file mode 100644 index 0000000000..b14f0b6957 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/Arguments.cpp @@ -0,0 +1,140 @@ +/* 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 + strcpy(request, rqs); + + // 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 (true) { + 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++; + return parse_pins(argv[index]); +} + +template<> int Arguments::getArg<int>(void) { + index++; + char *pEnd; + return strtol(argv[index], &pEnd, 10); +} + +template<> const char* Arguments::getArg<const char*>(void) { + index++; + return argv[index]; +} + +template<> char Arguments::getArg<char>(void) { + index++; + return *argv[index]; +} + +template<> double Arguments::getArg<double>(void) { + index++; + return atof(argv[index]); +} + +template<> float Arguments::getArg<float>(void) { + index++; + return atof(argv[index]); +} + +Reply::Reply(char* r) { + first = true; + *r = '\0'; + reply = r; +} + +void Reply::separator(void) { + if (first) { + first = false; + } else { + *reply = ' '; reply++; + } +} + +template<> void Reply::putData<const char*>(const char* s) { + separator(); + reply += sprintf(reply, "%s", s); +} + +template<> void Reply::putData<char*>(char* s) { + separator(); + reply += sprintf(reply, "%s", s); +} + +template<> void Reply::putData<char>(char c) { + separator(); + reply += sprintf(reply, "%c", c); +} + +template<> void Reply::putData<int>(int v) { + separator(); + reply += sprintf(reply, "%d", v); +} + +template<> void Reply::putData<float>(float f) { + separator(); + reply += sprintf(reply, "%.17g", f); +} + +} // namespace mbed diff --git a/tool/mbed/mbed-sdk/libraries/rpc/Arguments.h b/tool/mbed/mbed-sdk/libraries/rpc/Arguments.h new file mode 100644 index 0000000000..fc5aad1da6 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/Arguments.h @@ -0,0 +1,64 @@ +/* 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. + */ +#ifndef ARGUMENTS_H +#define ARGUMENTS_H + +#include "platform.h" +#include "parse_pins.h" + +namespace mbed { + +#define RPC_MAX_STRING 128 +#define RPC_MAX_ARGS 16 + +class Arguments { +public: + Arguments(const char* rqs); + + template<typename Arg> + Arg getArg(void); + + char *obj_name; + char *method_name; + + int argc; + char* argv[RPC_MAX_ARGS]; + +private: + // This copy can be removed if we can assume the request string is + // persistent and writable for the duration of the call + char request[RPC_MAX_STRING]; + int index; + char* search_arg(char **arg, char *p, char next_sep); +}; + +class Reply { +public: + Reply(char* r); + + template<typename Data> + void putData(Data d); + +private: + void separator(void); + bool first; + char* reply; +}; + + +} // Namespace mbed + +#endif diff --git a/tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.cpp b/tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.cpp new file mode 100644 index 0000000000..390e9f65c8 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.cpp @@ -0,0 +1,42 @@ +/* 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 "RPCFunction.h" + +namespace mbed { + +//Custom rpc method caller for execute so that the string will not be delimited by anything +void rpc_method_caller_run(RPC *this_ptr, Arguments *arguments, Reply *result) { + ((static_cast<RPCFunction*>(this_ptr))->run)(arguments, result); +} + +RPCFunction::RPCFunction(void (*f)(Arguments*, Reply*), const char* name) : RPC(name) { + _ftr = f; +} + +//Just run the attached function using the string thats in private memory - or just using null values, +void RPCFunction::run(Arguments* args, Reply* r) { + (*_ftr)(args, r); +} + +const rpc_method *RPCFunction::get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"run", rpc_method_caller_run }, //Run using custom caller, all characters accepted in string + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; +} + +} diff --git a/tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.h b/tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.h new file mode 100644 index 0000000000..14543a923a --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/RPCFunction.h @@ -0,0 +1,59 @@ +/* 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. + */ +#ifndef RPCFUNCTION_RPC +#define RPCFUNCTION_RPC + +#include "rpc.h" +#define STR_LEN 64 + +namespace mbed { + +/** + * + *Class to call custom functions over RPC + * + */ +class RPCFunction: public RPC { +public: + /** + * Constructor + * + *@param f Pointer to the function to call. the function must be of the form void foo(char * input, char * output) + *@param name The name of this object + */ + RPCFunction(void (*f)(Arguments*, Reply*), const char* = NULL); + + /** + *run + * + *Calls the attached function passing the string in but doesn't return the result. + *@param str The string to be passed into the attached function. This string can consist of any ASCII characters apart from escape codes. The usual limtations on argument content for RPC strings has been removed + *@return A string output from the function + */ + void run(Arguments* args, Reply* r); + + virtual const struct rpc_method *get_rpc_methods(); + +private: + void (*_ftr)(Arguments*, Reply*); + + char _input[STR_LEN]; + char _output[STR_LEN]; +}; + +} + +#endif diff --git a/tool/mbed/mbed-sdk/libraries/rpc/RPCVariable.h b/tool/mbed/mbed-sdk/libraries/rpc/RPCVariable.h new file mode 100644 index 0000000000..ee66809b8f --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/RPCVariable.h @@ -0,0 +1,87 @@ +/* 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. + */ +#ifndef RPCVARIABLE_H_ +#define RPCVARIABLE_H_ + +#include "rpc.h" + +namespace mbed { + +/** + *Class to read and set an attached variable using the RPC + * + */ +template<class T> +class RPCVariable: public RPC { +public: + /** + * Constructor + * + *@param ptr Pointer to the variable to make accessible over RPC. Any type of + *variable can be connected + *@param name The name of that this object will be over RPC + */ + template<class A> + RPCVariable(A * ptr, const char * name) : RPC(name) { + _ptr = ptr; + } + /** + *Read the variable over RPC. + * + *@return The value of the variable + */ + T read() { + return (*_ptr); + } + /** + *Write a value to the variable over RPC + * + *@param The value to be written to the attached variable. + */ + void write(T value) { + *_ptr = value; + } + + virtual const struct rpc_method *get_rpc_methods(); + static struct rpc_class *get_rpc_class(); + +private: + T * _ptr; +}; + +template<class T> +const rpc_method *RPCVariable<T>::get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"read" , rpc_method_caller<T, RPCVariable, &RPCVariable::read> }, + {"write", rpc_method_caller<RPCVariable, T, &RPCVariable::write> }, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; +} + +template<class T> +rpc_class *RPCVariable<T>::get_rpc_class() { + static const rpc_function funcs[] = { + "new", rpc_function_caller<const char*, T, const char*, &RPC::construct<RPCVariable, T, const char*> > , + RPC_METHOD_END + }; + static rpc_class c = {"RPCVariable", funcs, NULL}; + return &c; +} + +} + +#endif //RPCVARIABLE_H_ diff --git a/tool/mbed/mbed-sdk/libraries/rpc/RpcClasses.h b/tool/mbed/mbed-sdk/libraries/rpc/RpcClasses.h new file mode 100644 index 0000000000..0f50c44d12 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/RpcClasses.h @@ -0,0 +1,308 @@ +/* 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. + */ +#ifndef MBED_CLASSES_H +#define MBED_CLASSES_H + +#include "rpc.h" + +namespace mbed { + +class RpcDigitalOut : public RPC { +public: + RpcDigitalOut(PinName a0, const char *name=NULL) : RPC(name), o(a0) {} + + void write(int a0) {o.write(a0);} + int read(void) {return o.read();} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"write", rpc_method_caller<RpcDigitalOut, int, &RpcDigitalOut::write>}, + {"read", rpc_method_caller<int, RpcDigitalOut, &RpcDigitalOut::read>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, const char*, &RPC::construct<RpcDigitalOut, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"DigitalOut", funcs, NULL}; + return &c; + } +private: + DigitalOut o; +}; + +class RpcDigitalIn : public RPC { +public: + RpcDigitalIn(PinName a0, const char *name=NULL) : RPC(name), o(a0) {} + + int read(void) {return o.read();} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"read", rpc_method_caller<int, RpcDigitalIn, &RpcDigitalIn::read>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, const char*, &RPC::construct<RpcDigitalIn, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"DigitalIn", funcs, NULL}; + return &c; + } +private: + DigitalIn o; +}; + +class RpcDigitalInOut : public RPC { +public: + RpcDigitalInOut(PinName a0, const char *name=NULL) : RPC(name), o(a0) {} + + int read(void) {return o.read();} + void write(int a0) {o.write(a0);} + void input(void) {o.input();} + void output(void) {o.output();} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"read", rpc_method_caller<int, RpcDigitalInOut, &RpcDigitalInOut::read>}, + {"write", rpc_method_caller<RpcDigitalInOut, int, &RpcDigitalInOut::write>}, + {"input", rpc_method_caller<RpcDigitalInOut, &RpcDigitalInOut::input>}, + {"output", rpc_method_caller<RpcDigitalInOut, &RpcDigitalInOut::output>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, const char*, &RPC::construct<RpcDigitalInOut, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"DigitalInOut", funcs, NULL}; + return &c; + } +private: + DigitalInOut o; +}; + +#if DEVICE_ANALOGIN +class RpcAnalogIn : public RPC { +public: + RpcAnalogIn(PinName a0, const char *name=NULL) : RPC(name), o(a0) {} + + float read(void) {return o.read();} + unsigned short read_u16(void) {return o.read_u16();} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"read", rpc_method_caller<float, RpcAnalogIn, &RpcAnalogIn::read>}, + {"read_u16", rpc_method_caller<unsigned short, RpcAnalogIn, &RpcAnalogIn::read_u16>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, const char*, &RPC::construct<RpcAnalogIn, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"AnalogIn", funcs, NULL}; + return &c; + } +private: + AnalogIn o; +}; +#endif + +#if DEVICE_ANALOGOUT +class RpcAnalogOut : public RPC { +public: + RpcAnalogOut(PinName a0, const char *name=NULL) : RPC(name), o(a0) {} + + float read(void) {return o.read();} + void write(float a0) {o.write(a0);} + void write_u16(unsigned short a0) {o.write_u16(a0);} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"read", rpc_method_caller<float, RpcAnalogOut, &RpcAnalogOut::read>}, + {"write", rpc_method_caller<RpcAnalogOut, float, &RpcAnalogOut::write>}, + {"write_u16", rpc_method_caller<RpcAnalogOut, unsigned short, &RpcAnalogOut::write_u16>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, const char*, &RPC::construct<RpcAnalogOut, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"AnalogOut", funcs, NULL}; + return &c; + } +private: + AnalogOut o; +}; +#endif + +#if DEVICE_PWMOUT +class RpcPwmOut : public RPC { +public: + RpcPwmOut(PinName a0, const char *name=NULL) : RPC(name), o(a0) {} + + float read(void) {return o.read();} + void write(float a0) {o.write(a0);} + void period(float a0) {o.period(a0);} + void period_ms(int a0) {o.period_ms(a0);} + void pulsewidth(float a0) {o.pulsewidth(a0);} + void pulsewidth_ms(int a0) {o.pulsewidth_ms(a0);} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"read", rpc_method_caller<float, RpcPwmOut, &RpcPwmOut::read>}, + {"write", rpc_method_caller<RpcPwmOut, float, &RpcPwmOut::write>}, + {"period", rpc_method_caller<RpcPwmOut, float, &RpcPwmOut::period>}, + {"period_ms", rpc_method_caller<RpcPwmOut, int, &RpcPwmOut::period_ms>}, + {"pulsewidth", rpc_method_caller<RpcPwmOut, float, &RpcPwmOut::pulsewidth>}, + {"pulsewidth_ms", rpc_method_caller<RpcPwmOut, int, &RpcPwmOut::pulsewidth_ms>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, const char*, &RPC::construct<RpcPwmOut, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"PwmOut", funcs, NULL}; + return &c; + } +private: + PwmOut o; +}; +#endif + +#if DEVICE_SPI +class RpcSPI : public RPC { +public: + RpcSPI(PinName a0, PinName a1, PinName a2, const char *name=NULL) : RPC(name), o(a0, a1, a2) {} + + void format(int a0, int a1) {o.format(a0, a1);} + void frequency(int a0) {o.frequency(a0);} + int write(int a0) {return o.write(a0);} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"format", rpc_method_caller<RpcSPI, int, int, &RpcSPI::format>}, + {"frequency", rpc_method_caller<RpcSPI, int, &RpcSPI::frequency>}, + {"write", rpc_method_caller<int, RpcSPI, int, &RpcSPI::write>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, PinName, PinName, const char*, &RPC::construct<RpcSPI, PinName, PinName, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"SPI", funcs, NULL}; + return &c; + } +private: + SPI o; +}; +#endif + +#if DEVICE_SERIAL +class RpcSerial : public RPC { +public: + RpcSerial(PinName a0, PinName a1, const char *name=NULL) : RPC(name), o(a0, a1) {} + + void baud(int a0) {o.baud(a0);} + int readable(void) {return o.readable();} + int writeable(void) {return o.writeable();} + int putc(int a0) {return o.putc(a0);} + int getc(void) {return o.getc();} + int puts(const char * a0) {return o.puts(a0);} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"baud", rpc_method_caller<RpcSerial, int, &RpcSerial::baud>}, + {"readable", rpc_method_caller<int, RpcSerial, &RpcSerial::readable>}, + {"writeable", rpc_method_caller<int, RpcSerial, &RpcSerial::writeable>}, + {"putc", rpc_method_caller<int, RpcSerial, int, &RpcSerial::putc>}, + {"getc", rpc_method_caller<int, RpcSerial, &RpcSerial::getc>}, + {"puts", rpc_method_caller<int, RpcSerial, const char *, &RpcSerial::puts>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, PinName, PinName, const char*, &RPC::construct<RpcSerial, PinName, PinName, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"Serial", funcs, NULL}; + return &c; + } +private: + Serial o; +}; +#endif + +class RpcTimer : public RPC { +public: + RpcTimer(const char *name=NULL) : RPC(name), o() {} + + void start(void) {o.start();} + void stop(void) {o.stop();} + void reset(void) {o.reset();} + float read(void) {return o.read();} + int read_ms(void) {return o.read_ms();} + int read_us(void) {return o.read_us();} + + virtual const struct rpc_method *get_rpc_methods() { + static const rpc_method rpc_methods[] = { + {"start", rpc_method_caller<RpcTimer, &RpcTimer::start>}, + {"stop", rpc_method_caller<RpcTimer, &RpcTimer::stop>}, + {"reset", rpc_method_caller<RpcTimer, &RpcTimer::reset>}, + {"read", rpc_method_caller<float, RpcTimer, &RpcTimer::read>}, + {"read_ms", rpc_method_caller<int, RpcTimer, &RpcTimer::read_ms>}, + {"read_us", rpc_method_caller<int, RpcTimer, &RpcTimer::read_us>}, + RPC_METHOD_SUPER(RPC) + }; + return rpc_methods; + } + static struct rpc_class *get_rpc_class() { + static const rpc_function funcs[] = { + {"new", rpc_function_caller<const char*, const char*, &RPC::construct<RpcTimer, const char*> >}, + RPC_METHOD_END + }; + static rpc_class c = {"Timer", funcs, NULL}; + return &c; + } +private: + Timer o; +}; + +} + +#endif diff --git a/tool/mbed/mbed-sdk/libraries/rpc/mbed_rpc.h b/tool/mbed/mbed-sdk/libraries/rpc/mbed_rpc.h new file mode 100644 index 0000000000..d830069020 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/mbed_rpc.h @@ -0,0 +1,25 @@ +/* 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. + */ +#ifndef MBED_RPC_H +#define MBED_RPC_H + +#include "rpc.h" +#include "RPCVariable.h" +#include "RPCFunction.h" +#include "RpcClasses.h" +#include "Arguments.h" + +#endif diff --git a/tool/mbed/mbed-sdk/libraries/rpc/parse_pins.cpp b/tool/mbed/mbed-sdk/libraries/rpc/parse_pins.cpp new file mode 100644 index 0000000000..e68471e9ac --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/parse_pins.cpp @@ -0,0 +1,105 @@ +/* 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 "port_api.h" + +namespace mbed { + +PinName parse_pins(const char *str) { +#if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC2368) + static 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}; +#elif defined(TARGET_LPC1114) + static const PinName pin_names[] = {dp1, dp2, dp4, dp5, dp6, dp9, dp10, dp11 + , dp13, dp14, dp15, dp16, dp17, dp18, dp23 + , dp24, dp25, dp26, dp27, dp28}; +#elif defined(TARGET_LPC4088) + static const PinName pin_names[] = {NC, NC, NC, NC, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14 + , p15, p16, p17, p18, p19, p20, NC, NC, p23 + , p24, p25, p26, p27, p28, p29, p30, p31, p32 + , p33, p34, NC, NC, p37, p38, p39, NC, NC, NC, NC, NC, NC, NC}; +#elif defined(TARGET_LPC4088_DM) + static const PinName pin_names[] = {p1, p2, p3, p4, NC, NC, p7, p8, p9, p10, p11, p12, p13, p14 + , p15, p16, p17, p18, p19, p20, p21, p22, p23 + , p24, p25, p26, NC, NC, p29, p30, NC, NC + , NC, NC, NC, NC, NC, NC, NC, NC, p41, p42, p43, p44, p45, p46}; +#endif + +#if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC2368) || defined(TARGET_LPC812) || defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM) || defined(TARGET_LPC1114) + 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 port_pin((PortName)port, pin); + +#elif defined(TARGET_KL25Z) || defined(TARGET_KL05Z) || defined(TARGET_KL46Z) || defined(TARGET_K64F) + if (str[0] == 'P' && str[1] == 'T') { // PTxn + uint32_t port = str[2] - 'A'; + uint32_t pin = str[3] - '0'; // PTxn + uint32_t pin2 = str[4] - '0'; // PTxnn + + if (pin2 <= 9) { + pin = pin * 10 + pin2; + } + return port_pin((PortName)port, pin); +#endif + +#if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC2368) + } 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]; +#elif defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM) + } 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 < 1 || pin > 46) { + return NC; + } + return pin_names[pin - 1]; +#endif + + } 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; +} + +} diff --git a/tool/mbed/mbed-sdk/libraries/rpc/parse_pins.h b/tool/mbed/mbed-sdk/libraries/rpc/parse_pins.h new file mode 100644 index 0000000000..eddea2f39c --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/parse_pins.h @@ -0,0 +1,27 @@ +/* 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. + */ +#ifndef MBED_PINMAP_H +#define MBED_PINMAP_H + +#include "PinNames.h" + +namespace mbed { + +PinName parse_pins(const char *str); + +} + +#endif diff --git a/tool/mbed/mbed-sdk/libraries/rpc/rpc.cpp b/tool/mbed/mbed-sdk/libraries/rpc/rpc.cpp new file mode 100644 index 0000000000..60b5d764c7 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/rpc.cpp @@ -0,0 +1,191 @@ +/* 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 "rpc.h" + +using namespace std; + +namespace mbed { + +RPC::RPC(const char *name) { + _from_construct = false; + if (name != NULL) { + _name = new char[strlen(name) + 1]; + strcpy(_name, name); + } else { + _name = new char[12]; + sprintf(_name, "obj%p", this); + } + // put this object at head of the list + _next = _head; + _head = this; +} + +RPC::~RPC() { + // remove this object from the list + if (_head == this) { // first in the list, so just drop me + _head = _next; + } else { // find the object before me, then drop me + RPC* p = _head; + while (p->_next != this) { + p = p->_next; + } + p->_next = _next; + } +} + +const rpc_method *RPC::get_rpc_methods() { + static const rpc_method methods[] = { + {"delete", rpc_method_caller<RPC, &RPC::delete_self> }, + RPC_METHOD_END + }; + return methods; +} + +RPC *RPC::lookup(const char *name) { + size_t len = strlen(name); + for (RPC *p = _head; p != NULL; p = p->_next) { + /* Check that p->_name matches name and is the correct length */ + if (strncmp(p->_name, name, len) == 0 && (strlen(p->_name) == len)) { + return p; + } + } + return NULL; +} + +void RPC::delete_self() { + delete[] _name; + if (_from_construct) { + delete this; + } +} + +void RPC::list_objs(Arguments *args, Reply *result) { + for (RPC *ptr = RPC::_head; ptr != NULL; ptr = ptr->_next) { + if (ptr->_from_construct) { + result->putData<const char*>(ptr->_name); + } + } +} + +void RPC::clear(Arguments*, Reply*) { + RPC *ptr = RPC::_head; + while (ptr != NULL) { + RPC *tmp = ptr; + ptr = ptr->_next; + delete[] tmp->_name; + if (tmp->_from_construct) { + delete tmp; + } + } +} + +const rpc_function RPC::_RPC_funcs[] = { + {"clear", &RPC::clear }, + { "objects", &RPC::list_objs }, + RPC_METHOD_END +}; + +rpc_class RPC::_RPC_class = { "RPC", _RPC_funcs, NULL }; + +RPC *RPC::_head = NULL; + +rpc_class *RPC::_classes = &_RPC_class; + +bool RPC::call(const char *request, char *reply) { + if (request == NULL) return false; + + Arguments args(request); + Reply r(reply); + + /* If there's no name print object and class names to result */ + if (args.obj_name == NULL) { + for (RPC *p = RPC::_head; p != NULL; p = p->_next) { + r.putData<const char*>(p->_name); + } + for (rpc_class *c = RPC::_classes; c != NULL; c = c->next) { + r.putData<const char*>(c->name); + } + return true; + } + + /* First try matching an instance */ + RPC *p = lookup(args.obj_name); + if (p != NULL) { + /* Get the list of methods we support */ + const rpc_method *cur_method = p->get_rpc_methods(); + + /* When there's no method print method names to result */ + if (args.method_name == NULL) { + while (true) { + for (; cur_method->name != NULL; cur_method++) { + r.putData<const char*>(cur_method->name); + } + + /* write_name_arr's args are references, so result and cur_method will have changed */ + if (cur_method->super != 0) { + cur_method = cur_method->super(p); + } else { + return true; + } + } + } + + /* Look through the methods for the one whose name matches */ + while (true) { + for (; cur_method->name != NULL; cur_method++) { + if (strcmp(cur_method->name, args.method_name) == 0) { + (cur_method->method_caller)(p, &args, &r); + return true; + } + } + + if (cur_method->super != 0) { + cur_method = cur_method->super(p); + } else { + /* end of methods and no match */ + return false; + } + + } + } + + /* Then try a class */ + for (const rpc_class *q = _classes; q != NULL; q = q->next) { + if (strcmp(q->name, args.obj_name) == 0) { + /* Matched the class name, so get its functions */ + const rpc_function *cur_func = q->static_functions; + if (args.method_name == NULL) { + for (; cur_func->name != NULL; cur_func++) { + r.putData<const char*>(cur_func->name); + } + return true; + } else { + /* Otherwise call the appropriate function */ + for (; cur_func->name != NULL; cur_func++) { + if (strcmp(cur_func->name, args.method_name) == 0) { + (cur_func->function_caller)(&args, &r); + return true; + } + } + return false; + } + } + } + + return false; +} + +} // namespace mbed diff --git a/tool/mbed/mbed-sdk/libraries/rpc/rpc.h b/tool/mbed/mbed-sdk/libraries/rpc/rpc.h new file mode 100644 index 0000000000..8ed63c65aa --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/rpc/rpc.h @@ -0,0 +1,313 @@ +/* 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. + */ +#ifndef RPC_H +#define RPC_H + +#include "mbed.h" +#include "Arguments.h" + +namespace mbed { + +#define RPC_MAX_STRING 128 + +struct rpc_function { + const char *name; + void (*function_caller)(Arguments*, Reply*); +}; + +struct rpc_class { + const char *name; + const rpc_function *static_functions; + struct rpc_class *next; +}; + +/* Class RPC + * The RPC class for most things + */ +class RPC { + +public: + + RPC(const char *name = NULL); + + virtual ~RPC(); + + /* 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 RPC { + * > 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<int, Example, int, int, &Example::foo> }, + * > RPC_METHOD_SUPER(RPC) + * > }; + * > return rpc_methods; + * > } + * > }; + */ + virtual const struct rpc_method *get_rpc_methods(); + + static bool call(const char *buf, char *result); + + /* Function lookup + * Lookup and return the object that has the given name. + * + * Variables + * name - the name to lookup. + */ + static RPC *lookup(const char *name); + +protected: + static RPC *_head; + RPC *_next; + char *_name; + bool _from_construct; + +private: + static rpc_class *_classes; + + static const rpc_function _RPC_funcs[]; + static rpc_class _RPC_class; + + void delete_self(); + static void list_objs(Arguments *args, Reply *result); + static void clear(Arguments *args, Reply *result); + +public: + /* 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<class C> + static void add_rpc_class() { + rpc_class *c = C::get_rpc_class(); + c->next = _classes; + _classes = c; + } + + template<class C> + static const char *construct() { + RPC *p = new C(); + p->_from_construct = true; + return p->_name; + } + + template<class C, typename A1> + static const char *construct(A1 arg1) { + RPC *p = new C(arg1); + p->_from_construct = true; + return p->_name; + } + + template<class C, typename A1, typename A2> + static const char *construct(A1 arg1, A2 arg2) { + RPC *p = new C(arg1, arg2); + p->_from_construct = true; + return p->_name; + } + + template<class C, typename A1, typename A2, typename A3> + static const char *construct(A1 arg1, A2 arg2, A3 arg3) { + RPC *p = new C(arg1, arg2, arg3); + p->_from_construct = true; + return p->_name; + } + + template<class C, typename A1, typename A2, typename A3, typename A4> + static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) { + RPC *p = new C(arg1, arg2, arg3, arg4); + p->_from_construct = true; + return p->_name; + } +}; + +/* 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 + +/* Function rpc_method_caller + */ +template<class T, void(T::*member)(const char *, char *)> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + (static_cast<T*>(this_ptr)->*member)(arguments, result); +} + +/* Function rpc_method_caller + */ +template<class T, void(T::*member)()> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + (static_cast<T*>(this_ptr)->*member)(); +} + +/* Function rpc_method_caller + */ +template<class T, typename A1, void(T::*member)(A1)> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + + (static_cast<T*>(this_ptr)->*member)(arg1); +} + +/* Function rpc_method_caller + */ +template<class T, typename A1, typename A2, void(T::*member)(A1, A2)> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + A2 arg2 = arguments->getArg<A2>(); + + (static_cast<T*>(this_ptr)->*member)(arg1, arg2); +} + +/* Function rpc_method_caller + */ +template<class T, typename A1, typename A2, typename A3, void(T::*member)(A1, A2, A3)> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + A2 arg2 = arguments->getArg<A2>(); + A3 arg3 = arguments->getArg<A3>(); + + (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3); +} + +/* Function rpc_method_caller + */ +template<typename R, class T, R(T::*member)()> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + R res = (static_cast<T*>(this_ptr)->*member)(); + result->putData<R>(res); +} + +/* Function rpc_method_caller + */ +template<typename R, class T, typename A1, R(T::*member)(A1)> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + + R res = (static_cast<T*>(this_ptr)->*member)(arg1); + result->putData<R>(res); +} + +/* Function rpc_method_caller + */ +template<typename R, class T, typename A1, typename A2, R(T::*member)(A1, A2)> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + A2 arg2 = arguments->getArg<A2>(); + + R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2); + result->putData<R>(res); +} + +/* Function rpc_method_caller + */ +template<typename R, class T, typename A1, typename A2, typename A3, R(T::*member)(A1, A2, A3)> +void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + A2 arg2 = arguments->getArg<A2>(); + A3 arg3 = arguments->getArg<A3>(); + + R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3); + result->putData<R>(res); +} + +/* Function rpc_function caller + */ +template<typename R, R(*func)()> +void rpc_function_caller(Arguments *arguments, Reply *result) { + R res = (*func)(); + result->putData<R>(res); +} + +/* Function rpc_function caller + */ +template<typename R, typename A1, R(*func)(A1)> +void rpc_function_caller(Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + R res = (*func)(arg1); + result->putData<R>(res); +} + +/* Function rpc_function caller + */ +template<typename R, typename A1, typename A2, R(*func)(A1, A2)> +void rpc_function_caller(Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + A2 arg2 = arguments->getArg<A2>(); + + R res = (*func)(arg1, arg2); + result->putData<R>(res); +} + +/* Function rpc_function caller + */ +template<typename R, typename A1, typename A2, typename A3, R(*func)(A1, A2, A3)> +void rpc_function_caller(Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + A2 arg2 = arguments->getArg<A2>(); + A3 arg3 = arguments->getArg<A3>(); + + R res = (*func)(arg1, arg2, arg3); + result->putData<R>(res); +} + +/* Function rpc_function caller + */ +template<typename R, typename A1, typename A2, typename A3, typename A4, R(*func)(A1, A2, A3, A4)> +void rpc_function_caller(Arguments *arguments, Reply *result) { + A1 arg1 = arguments->getArg<A1>(); + A2 arg2 = arguments->getArg<A2>(); + A3 arg3 = arguments->getArg<A3>(); + A4 arg4 = arguments->getArg<A4>(); + + R res = (*func)(arg1, arg2, arg3, arg4); + result->putData<R>(res); +} + +struct rpc_method { + const char *name; + typedef void (*method_caller_t)(RPC*, Arguments*, Reply*); + typedef const struct rpc_method *(*super_t)(RPC*); + union { + method_caller_t method_caller; + super_t super; + }; +}; + +template<class C> +const struct rpc_method *rpc_super(RPC *this_ptr) { + return static_cast<C*>(this_ptr)->C::get_rpc_methods(); +} + +#define RPC_METHOD_END { NULL, NULL } +#define RPC_METHOD_SUPER(C) { NULL, (rpc_method::method_caller_t)rpc_super<C> } + +} // namespace mbed + +#endif |