Learning how to use RPC.

Committer:
MichaelW
Date:
Thu Sep 16 13:27:57 2010 +0000
Revision:
0:9232f9e1178d
Child:
1:67aefdc74b32
Changed to RPCFunction and RPCVariable.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MichaelW 0:9232f9e1178d 1 /**
MichaelW 0:9232f9e1178d 2 *@section LICENSE
MichaelW 0:9232f9e1178d 3 *Copyright (c) 2010 ARM Ltd.
MichaelW 0:9232f9e1178d 4 *
MichaelW 0:9232f9e1178d 5 *Permission is hereby granted, free of charge, to any person obtaining a copy
MichaelW 0:9232f9e1178d 6 *of this software and associated documentation files (the "Software"), to deal
MichaelW 0:9232f9e1178d 7 *in the Software without restriction, including without limitation the rights
MichaelW 0:9232f9e1178d 8 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
MichaelW 0:9232f9e1178d 9 *copies of the Software, and to permit persons to whom the Software is
MichaelW 0:9232f9e1178d 10 *furnished to do so, subject to the following conditions:
MichaelW 0:9232f9e1178d 11 *
MichaelW 0:9232f9e1178d 12 *The above copyright notice and this permission notice shall be included in
MichaelW 0:9232f9e1178d 13 *all copies or substantial portions of the Software.
MichaelW 0:9232f9e1178d 14 *
MichaelW 0:9232f9e1178d 15 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MichaelW 0:9232f9e1178d 16 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MichaelW 0:9232f9e1178d 17 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
MichaelW 0:9232f9e1178d 18 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MichaelW 0:9232f9e1178d 19 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
MichaelW 0:9232f9e1178d 20 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
MichaelW 0:9232f9e1178d 21 *THE SOFTWARE.
MichaelW 0:9232f9e1178d 22 *
MichaelW 0:9232f9e1178d 23 *@section Description
MichaelW 0:9232f9e1178d 24 *This class provides an object to which a variable can be attached. Any type
MichaelW 0:9232f9e1178d 25 *for which a parse_args function specilisation exists can be attached. This includes
MichaelW 0:9232f9e1178d 26 *all of the standard types.
MichaelW 0:9232f9e1178d 27 *
MichaelW 0:9232f9e1178d 28 */
MichaelW 0:9232f9e1178d 29 #ifndef RPCVARIABLE_H_
MichaelW 0:9232f9e1178d 30 #define RPCVARIABLE_H_
MichaelW 0:9232f9e1178d 31 /**
MichaelW 0:9232f9e1178d 32 *Class to read and set an attached variable using the RPC
MichaelW 0:9232f9e1178d 33 *
MichaelW 0:9232f9e1178d 34 */
MichaelW 0:9232f9e1178d 35 template<class T>
MichaelW 0:9232f9e1178d 36 class RPCVariable : public Base{
MichaelW 0:9232f9e1178d 37 public:
MichaelW 0:9232f9e1178d 38 /**
MichaelW 0:9232f9e1178d 39 * Constructor
MichaelW 0:9232f9e1178d 40 *
MichaelW 0:9232f9e1178d 41 *@param ptr Pointer to the variable to make accessible over RPC. Any type of
MichaelW 0:9232f9e1178d 42 *variable can be connected
MichaelW 0:9232f9e1178d 43 *@param name The name of that this object will be over RPC
MichaelW 0:9232f9e1178d 44 */
MichaelW 0:9232f9e1178d 45 template<class A>
MichaelW 0:9232f9e1178d 46 RPCVariable(A * ptr, const char * name) : Base(name){
MichaelW 0:9232f9e1178d 47 _ptr = ptr;
MichaelW 0:9232f9e1178d 48 }
MichaelW 0:9232f9e1178d 49 /**
MichaelW 0:9232f9e1178d 50 *Read the variable over RPC.
MichaelW 0:9232f9e1178d 51 *
MichaelW 0:9232f9e1178d 52 *@return The value of the variable
MichaelW 0:9232f9e1178d 53 */
MichaelW 0:9232f9e1178d 54 T read(){
MichaelW 0:9232f9e1178d 55 return(*_ptr);
MichaelW 0:9232f9e1178d 56 }
MichaelW 0:9232f9e1178d 57 /**
MichaelW 0:9232f9e1178d 58 *Write a value to the variable over RPC
MichaelW 0:9232f9e1178d 59 *
MichaelW 0:9232f9e1178d 60 *@param The value to be written to the attached variable.
MichaelW 0:9232f9e1178d 61 */
MichaelW 0:9232f9e1178d 62 void write(T value){
MichaelW 0:9232f9e1178d 63 *_ptr = value;
MichaelW 0:9232f9e1178d 64 }
MichaelW 0:9232f9e1178d 65
MichaelW 0:9232f9e1178d 66 #ifdef MBED_RPC
MichaelW 0:9232f9e1178d 67 virtual const struct rpc_method *get_rpc_methods();
MichaelW 0:9232f9e1178d 68 static struct rpc_class *get_rpc_class();
MichaelW 0:9232f9e1178d 69 #endif
MichaelW 0:9232f9e1178d 70
MichaelW 0:9232f9e1178d 71 private:
MichaelW 0:9232f9e1178d 72 T * _ptr;
MichaelW 0:9232f9e1178d 73
MichaelW 0:9232f9e1178d 74 };
MichaelW 0:9232f9e1178d 75
MichaelW 0:9232f9e1178d 76 //Set up RPC methods
MichaelW 0:9232f9e1178d 77 #ifdef MBED_RPC
MichaelW 0:9232f9e1178d 78 template <class T>
MichaelW 0:9232f9e1178d 79 const rpc_method *RPCVariable<T>::get_rpc_methods() {
MichaelW 0:9232f9e1178d 80 static const rpc_method rpc_methods[] = {
MichaelW 0:9232f9e1178d 81 { "read", rpc_method_caller<T, RPCVariable, &RPCVariable::read> },
MichaelW 0:9232f9e1178d 82 { "write", rpc_method_caller<RPCVariable, T, &RPCVariable::write> },
MichaelW 0:9232f9e1178d 83 RPC_METHOD_SUPER(Base)
MichaelW 0:9232f9e1178d 84 };
MichaelW 0:9232f9e1178d 85 return rpc_methods;
MichaelW 0:9232f9e1178d 86 }
MichaelW 0:9232f9e1178d 87 template <class T>
MichaelW 0:9232f9e1178d 88 rpc_class *RPCVariable<T>::get_rpc_class() {
MichaelW 0:9232f9e1178d 89 static const rpc_function funcs[] = {"new", rpc_function_caller<const char*, T,const char* , &Base::construct<RemoteVar, T ,const char*> >,RPC_METHOD_END};
MichaelW 0:9232f9e1178d 90 static rpc_class c = { "RPCVariable", funcs, NULL };
MichaelW 0:9232f9e1178d 91 return &c;
MichaelW 0:9232f9e1178d 92 }
MichaelW 0:9232f9e1178d 93 #endif
MichaelW 0:9232f9e1178d 94
MichaelW 0:9232f9e1178d 95 //There could be specialisation for integer, to also give increment and decrements
MichaelW 0:9232f9e1178d 96
MichaelW 0:9232f9e1178d 97
MichaelW 0:9232f9e1178d 98 #endif //RPCVARIABLE_H_
MichaelW 0:9232f9e1178d 99
MichaelW 0:9232f9e1178d 100
MichaelW 0:9232f9e1178d 101
MichaelW 0:9232f9e1178d 102
MichaelW 0:9232f9e1178d 103
MichaelW 0:9232f9e1178d 104