Learning how to use RPC.

Committer:
MichaelW
Date:
Mon Sep 20 14:26:52 2010 +0000
Revision:
1:67aefdc74b32
Parent:
0:9232f9e1178d
Child:
4:05f0d66bee57

        

Who changed what in which revision?

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