Accepts RPC commands over bluetooth (RN42)

Dependencies:   mbed m3pi

Committer:
chris
Date:
Thu May 12 15:46:41 2011 +0000
Revision:
3:be428c849e1c

        

Who changed what in which revision?

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