TX1 node
Embed:
(wiki syntax)
Show/hide line numbers
RPCVariable.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef RPCVARIABLE_H_ 00017 #define RPCVARIABLE_H_ 00018 00019 #include "rpc.h" 00020 00021 namespace mbed { 00022 00023 /** 00024 *Class to read and set an attached variable using the RPC 00025 * 00026 */ 00027 template<class T> 00028 class RPCVariable: public RPC { 00029 public: 00030 /** 00031 * Constructor 00032 * 00033 *@param ptr Pointer to the variable to make accessible over RPC. Any type of 00034 *variable can be connected 00035 *@param name The name of that this object will be over RPC 00036 */ 00037 template<class A> 00038 RPCVariable(A * ptr, const char * name) : RPC(name) { 00039 _ptr = ptr; 00040 } 00041 /** 00042 *Read the variable over RPC. 00043 * 00044 *@return The value of the variable 00045 */ 00046 T read() { 00047 return (*_ptr); 00048 } 00049 /** 00050 *Write a value to the variable over RPC 00051 * 00052 *@param The value to be written to the attached variable. 00053 */ 00054 void write(T value) { 00055 *_ptr = value; 00056 } 00057 00058 virtual const struct rpc_method *get_rpc_methods(); 00059 static struct rpc_class *get_rpc_class(); 00060 00061 private: 00062 T * _ptr; 00063 }; 00064 00065 template<class T> 00066 const rpc_method *RPCVariable<T>::get_rpc_methods() { 00067 static const rpc_method rpc_methods[] = { 00068 {"read" , rpc_method_caller<T, RPCVariable, &RPCVariable::read> }, 00069 {"write", rpc_method_caller<RPCVariable, T, &RPCVariable::write> }, 00070 RPC_METHOD_SUPER(RPC) 00071 }; 00072 return rpc_methods; 00073 } 00074 00075 template<class T> 00076 rpc_class *RPCVariable<T>::get_rpc_class() { 00077 static const rpc_function funcs[] = { 00078 "new", rpc_function_caller<const char*, T, const char*, &RPC::construct<RPCVariable, T, const char*> > , 00079 RPC_METHOD_END 00080 }; 00081 static rpc_class c = {"RPCVariable", funcs, NULL}; 00082 return &c; 00083 } 00084 00085 00086 /// Specialisation of the RPCVariable class to handle strings. 00087 /// The class is passed a pointer to the buffer and its size 00088 /// to avoid buffer overruns. 00089 template<> 00090 class RPCVariable<char *>: public RPC { 00091 public: 00092 /** 00093 * Constructor 00094 * 00095 *@param var Pointer to the char buffer used to hold the string being exposed by RPC. 00096 *@param bufSize Size of the buffer in chars. 00097 *@param name The name of that this object will be over RPC 00098 */ 00099 RPCVariable(char *var, int bufSize, const char * name = NULL) : RPC(name), _var(var), _size(bufSize - 1) { 00100 } 00101 00102 /** 00103 *Read the variable over RPC. 00104 * 00105 *@return The value of the buffer 00106 */ 00107 char * read() { 00108 return (_var); 00109 } 00110 /** 00111 *Write a value to the variable over RPC 00112 * 00113 *@param The value to be written to the buffer. 00114 */ 00115 void write(char *value) { 00116 strncpy(_var, value, _size); 00117 _var[_size] = 0; 00118 } 00119 00120 00121 virtual const rpc_method *get_rpc_methods() { 00122 static const rpc_method rpc_methods[] = { 00123 {"read" , rpc_method_caller<char *, RPCVariable, &RPCVariable::read> }, 00124 {"write", rpc_method_caller<RPCVariable, char *, &RPCVariable::write> }, 00125 RPC_METHOD_END 00126 }; 00127 return rpc_methods; 00128 } 00129 00130 private: 00131 char *_var; 00132 00133 // Holds the number of chars excluding the terminating null. 00134 int _size; 00135 }; 00136 00137 } 00138 00139 #endif //RPCVARIABLE_H_
Generated on Tue Jul 19 2022 16:21:08 by 1.7.2