TX1 node

Revision:
19:e65ed29fd4d1
Parent:
1:6919289a5946
diff -r e6a835c40639 -r e65ed29fd4d1 RPCVariable.h
--- a/RPCVariable.h	Mon Jun 13 22:40:45 2016 +0000
+++ b/RPCVariable.h	Thu Jun 16 08:37:15 2016 +0000
@@ -82,6 +82,58 @@
     return &c;
 }
 
+
+/// Specialisation of the RPCVariable class to handle strings.
+/// The class is passed a pointer to the buffer and its size
+/// to avoid buffer overruns.
+template<>
+class RPCVariable<char *>: public RPC {
+public:
+    /**
+     * Constructor
+     *
+     *@param var Pointer to the char buffer used to hold the string being exposed by RPC.
+     *@param bufSize Size of the buffer in chars.
+     *@param name The name of that this object will be over RPC
+     */
+    RPCVariable(char *var, int bufSize, const char * name = NULL) : RPC(name), _var(var), _size(bufSize - 1) {
+    }
+    
+    /**
+     *Read the variable over RPC.
+     *
+     *@return The value of the buffer
+     */
+    char * read() {
+        return (_var);
+    }
+    /**
+     *Write a value to the variable over RPC
+     *
+     *@param The value to be written to the buffer.
+     */
+    void write(char *value) {
+        strncpy(_var, value, _size);
+        _var[_size] = 0;
+    }
+
+
+    virtual const rpc_method *get_rpc_methods() {
+        static const rpc_method rpc_methods[] = {
+            {"read" , rpc_method_caller<char *, RPCVariable, &RPCVariable::read> },
+            {"write", rpc_method_caller<RPCVariable, char *, &RPCVariable::write> },
+            RPC_METHOD_END
+        };
+        return rpc_methods;
+    }
+
+private:
+    char *_var;
+    
+    // Holds the number of chars excluding the terminating null.
+    int _size;
+};
+
 }
 
 #endif  //RPCVARIABLE_H_