Accepts RPC commands over bluetooth (RN42)

Dependencies:   mbed m3pi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RPCVariable.h Source File

RPCVariable.h

00001  /**
00002 * @section LICENSE
00003 *Copyright (c) 2010 ARM Ltd.
00004 *
00005 *Permission is hereby granted, free of charge, to any person obtaining a copy
00006 *of this software and associated documentation files (the "Software"), to deal
00007 *in the Software without restriction, including without limitation the rights
00008 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 *copies of the Software, and to permit persons to whom the Software is
00010 *furnished to do so, subject to the following conditions:
00011 * 
00012 *The above copyright notice and this permission notice shall be included in
00013 *all copies or substantial portions of the Software.
00014 * 
00015 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 *THE SOFTWARE.
00022 *
00023 * @section Description
00024 *This class provides an object to which a variable can be attached. Any type 
00025 *for which a parse_args function specilisation exists can be attached. This includes 
00026 *all of the standard types.
00027 *
00028 */
00029  #ifndef RPCVARIABLE_H_  
00030  #define RPCVARIABLE_H_
00031 
00032 #include "mbed.h"
00033 #include "platform.h"
00034 #include "rpc.h"
00035  /**
00036  *Class to read and set an attached variable using the RPC
00037  *
00038  */
00039 template<class T>
00040 class RPCVariable : public Base{
00041 public:
00042     /**
00043     * Constructor
00044     * 
00045     *@param ptr Pointer to the variable to make accessible over RPC. Any type of 
00046     *variable can be connected
00047     *@param name The name of that this object will be over RPC
00048     */
00049     template<class A>
00050     RPCVariable(A * ptr, const char * name) : Base(name){
00051         _ptr = ptr;
00052     }
00053     /**
00054     *Read the variable over RPC.
00055     * 
00056     *@return The value of the variable
00057     */
00058     T read(){
00059         return(*_ptr);
00060     }
00061     /**
00062     *Write a value to the variable over RPC
00063     * 
00064     *@param The value to be written to the attached variable.
00065     */
00066     void write(T value){
00067         *_ptr = value;
00068     }
00069 
00070                                                                                    #ifdef MBED_RPC
00071     virtual const struct rpc_method *get_rpc_methods();    
00072     static struct rpc_class *get_rpc_class();
00073                      #endif
00074 
00075 private:
00076     T * _ptr;
00077                                            
00078 };
00079 
00080 //Set up RPC methods
00081 #ifdef MBED_RPC
00082 template <class T>
00083     const rpc_method *RPCVariable<T>::get_rpc_methods() {
00084        static const rpc_method rpc_methods[] = {
00085         { "read", rpc_method_caller<T, RPCVariable, &RPCVariable::read> },
00086         { "write", rpc_method_caller<RPCVariable, T, &RPCVariable::write> },
00087         RPC_METHOD_SUPER(Base)
00088       };
00089       return rpc_methods;
00090     }       
00091     template <class T>
00092     rpc_class *RPCVariable<T>::get_rpc_class() {
00093         static const rpc_function funcs[] = {"new", rpc_function_caller<const char*, T,const char* , &Base::construct<RemoteVar, T ,const char*> >,RPC_METHOD_END};
00094         static rpc_class c = { "RPCVariable", funcs, NULL };
00095         return &c;
00096     }
00097 #endif
00098 
00099 //There could be specialisation for integer, to also give increment and decrements
00100 
00101 
00102 #endif  //RPCVARIABLE_H_