Learning how to use RPC.

Committer:
MichaelW
Date:
Mon Sep 27 08:32:57 2010 +0000
Revision:
4:05f0d66bee57
Parent:
1:67aefdc74b32
Child:
5:56fd0b265aba
Initial publish in new library format

Who changed what in which revision?

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