Added NUCLEO-F103RB support
Fork of mbed-rpc by
RPCVariable.h@19:e65ed29fd4d1, 2016-06-16 (annotated)
- Committer:
- olympux
- Date:
- Thu Jun 16 08:37:15 2016 +0000
- Revision:
- 19:e65ed29fd4d1
- Parent:
- 1:6919289a5946
Added supporting RPC char*, but not tested yet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:efe8172b4113 | 1 | /* mbed Microcontroller Library |
emilmont | 1:6919289a5946 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 0:efe8172b4113 | 3 | * |
emilmont | 1:6919289a5946 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
emilmont | 1:6919289a5946 | 5 | * you may not use this file except in compliance with the License. |
emilmont | 1:6919289a5946 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:efe8172b4113 | 7 | * |
emilmont | 1:6919289a5946 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:efe8172b4113 | 9 | * |
emilmont | 1:6919289a5946 | 10 | * Unless required by applicable law or agreed to in writing, software |
emilmont | 1:6919289a5946 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
emilmont | 1:6919289a5946 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
emilmont | 1:6919289a5946 | 13 | * See the License for the specific language governing permissions and |
emilmont | 1:6919289a5946 | 14 | * limitations under the License. |
mbed_official | 0:efe8172b4113 | 15 | */ |
mbed_official | 0:efe8172b4113 | 16 | #ifndef RPCVARIABLE_H_ |
mbed_official | 0:efe8172b4113 | 17 | #define RPCVARIABLE_H_ |
mbed_official | 0:efe8172b4113 | 18 | |
mbed_official | 0:efe8172b4113 | 19 | #include "rpc.h" |
mbed_official | 0:efe8172b4113 | 20 | |
mbed_official | 0:efe8172b4113 | 21 | namespace mbed { |
mbed_official | 0:efe8172b4113 | 22 | |
mbed_official | 0:efe8172b4113 | 23 | /** |
mbed_official | 0:efe8172b4113 | 24 | *Class to read and set an attached variable using the RPC |
mbed_official | 0:efe8172b4113 | 25 | * |
mbed_official | 0:efe8172b4113 | 26 | */ |
mbed_official | 0:efe8172b4113 | 27 | template<class T> |
mbed_official | 0:efe8172b4113 | 28 | class RPCVariable: public RPC { |
mbed_official | 0:efe8172b4113 | 29 | public: |
mbed_official | 0:efe8172b4113 | 30 | /** |
mbed_official | 0:efe8172b4113 | 31 | * Constructor |
emilmont | 1:6919289a5946 | 32 | * |
emilmont | 1:6919289a5946 | 33 | *@param ptr Pointer to the variable to make accessible over RPC. Any type of |
mbed_official | 0:efe8172b4113 | 34 | *variable can be connected |
mbed_official | 0:efe8172b4113 | 35 | *@param name The name of that this object will be over RPC |
mbed_official | 0:efe8172b4113 | 36 | */ |
mbed_official | 0:efe8172b4113 | 37 | template<class A> |
mbed_official | 0:efe8172b4113 | 38 | RPCVariable(A * ptr, const char * name) : RPC(name) { |
mbed_official | 0:efe8172b4113 | 39 | _ptr = ptr; |
mbed_official | 0:efe8172b4113 | 40 | } |
mbed_official | 0:efe8172b4113 | 41 | /** |
mbed_official | 0:efe8172b4113 | 42 | *Read the variable over RPC. |
emilmont | 1:6919289a5946 | 43 | * |
mbed_official | 0:efe8172b4113 | 44 | *@return The value of the variable |
mbed_official | 0:efe8172b4113 | 45 | */ |
mbed_official | 0:efe8172b4113 | 46 | T read() { |
mbed_official | 0:efe8172b4113 | 47 | return (*_ptr); |
mbed_official | 0:efe8172b4113 | 48 | } |
mbed_official | 0:efe8172b4113 | 49 | /** |
mbed_official | 0:efe8172b4113 | 50 | *Write a value to the variable over RPC |
emilmont | 1:6919289a5946 | 51 | * |
mbed_official | 0:efe8172b4113 | 52 | *@param The value to be written to the attached variable. |
mbed_official | 0:efe8172b4113 | 53 | */ |
mbed_official | 0:efe8172b4113 | 54 | void write(T value) { |
mbed_official | 0:efe8172b4113 | 55 | *_ptr = value; |
mbed_official | 0:efe8172b4113 | 56 | } |
emilmont | 1:6919289a5946 | 57 | |
mbed_official | 0:efe8172b4113 | 58 | virtual const struct rpc_method *get_rpc_methods(); |
mbed_official | 0:efe8172b4113 | 59 | static struct rpc_class *get_rpc_class(); |
mbed_official | 0:efe8172b4113 | 60 | |
mbed_official | 0:efe8172b4113 | 61 | private: |
mbed_official | 0:efe8172b4113 | 62 | T * _ptr; |
mbed_official | 0:efe8172b4113 | 63 | }; |
mbed_official | 0:efe8172b4113 | 64 | |
mbed_official | 0:efe8172b4113 | 65 | template<class T> |
mbed_official | 0:efe8172b4113 | 66 | const rpc_method *RPCVariable<T>::get_rpc_methods() { |
mbed_official | 0:efe8172b4113 | 67 | static const rpc_method rpc_methods[] = { |
mbed_official | 0:efe8172b4113 | 68 | {"read" , rpc_method_caller<T, RPCVariable, &RPCVariable::read> }, |
mbed_official | 0:efe8172b4113 | 69 | {"write", rpc_method_caller<RPCVariable, T, &RPCVariable::write> }, |
mbed_official | 0:efe8172b4113 | 70 | RPC_METHOD_SUPER(RPC) |
mbed_official | 0:efe8172b4113 | 71 | }; |
mbed_official | 0:efe8172b4113 | 72 | return rpc_methods; |
mbed_official | 0:efe8172b4113 | 73 | } |
mbed_official | 0:efe8172b4113 | 74 | |
mbed_official | 0:efe8172b4113 | 75 | template<class T> |
mbed_official | 0:efe8172b4113 | 76 | rpc_class *RPCVariable<T>::get_rpc_class() { |
mbed_official | 0:efe8172b4113 | 77 | static const rpc_function funcs[] = { |
mbed_official | 0:efe8172b4113 | 78 | "new", rpc_function_caller<const char*, T, const char*, &RPC::construct<RPCVariable, T, const char*> > , |
mbed_official | 0:efe8172b4113 | 79 | RPC_METHOD_END |
mbed_official | 0:efe8172b4113 | 80 | }; |
mbed_official | 0:efe8172b4113 | 81 | static rpc_class c = {"RPCVariable", funcs, NULL}; |
mbed_official | 0:efe8172b4113 | 82 | return &c; |
mbed_official | 0:efe8172b4113 | 83 | } |
mbed_official | 0:efe8172b4113 | 84 | |
olympux | 19:e65ed29fd4d1 | 85 | |
olympux | 19:e65ed29fd4d1 | 86 | /// Specialisation of the RPCVariable class to handle strings. |
olympux | 19:e65ed29fd4d1 | 87 | /// The class is passed a pointer to the buffer and its size |
olympux | 19:e65ed29fd4d1 | 88 | /// to avoid buffer overruns. |
olympux | 19:e65ed29fd4d1 | 89 | template<> |
olympux | 19:e65ed29fd4d1 | 90 | class RPCVariable<char *>: public RPC { |
olympux | 19:e65ed29fd4d1 | 91 | public: |
olympux | 19:e65ed29fd4d1 | 92 | /** |
olympux | 19:e65ed29fd4d1 | 93 | * Constructor |
olympux | 19:e65ed29fd4d1 | 94 | * |
olympux | 19:e65ed29fd4d1 | 95 | *@param var Pointer to the char buffer used to hold the string being exposed by RPC. |
olympux | 19:e65ed29fd4d1 | 96 | *@param bufSize Size of the buffer in chars. |
olympux | 19:e65ed29fd4d1 | 97 | *@param name The name of that this object will be over RPC |
olympux | 19:e65ed29fd4d1 | 98 | */ |
olympux | 19:e65ed29fd4d1 | 99 | RPCVariable(char *var, int bufSize, const char * name = NULL) : RPC(name), _var(var), _size(bufSize - 1) { |
olympux | 19:e65ed29fd4d1 | 100 | } |
olympux | 19:e65ed29fd4d1 | 101 | |
olympux | 19:e65ed29fd4d1 | 102 | /** |
olympux | 19:e65ed29fd4d1 | 103 | *Read the variable over RPC. |
olympux | 19:e65ed29fd4d1 | 104 | * |
olympux | 19:e65ed29fd4d1 | 105 | *@return The value of the buffer |
olympux | 19:e65ed29fd4d1 | 106 | */ |
olympux | 19:e65ed29fd4d1 | 107 | char * read() { |
olympux | 19:e65ed29fd4d1 | 108 | return (_var); |
olympux | 19:e65ed29fd4d1 | 109 | } |
olympux | 19:e65ed29fd4d1 | 110 | /** |
olympux | 19:e65ed29fd4d1 | 111 | *Write a value to the variable over RPC |
olympux | 19:e65ed29fd4d1 | 112 | * |
olympux | 19:e65ed29fd4d1 | 113 | *@param The value to be written to the buffer. |
olympux | 19:e65ed29fd4d1 | 114 | */ |
olympux | 19:e65ed29fd4d1 | 115 | void write(char *value) { |
olympux | 19:e65ed29fd4d1 | 116 | strncpy(_var, value, _size); |
olympux | 19:e65ed29fd4d1 | 117 | _var[_size] = 0; |
olympux | 19:e65ed29fd4d1 | 118 | } |
olympux | 19:e65ed29fd4d1 | 119 | |
olympux | 19:e65ed29fd4d1 | 120 | |
olympux | 19:e65ed29fd4d1 | 121 | virtual const rpc_method *get_rpc_methods() { |
olympux | 19:e65ed29fd4d1 | 122 | static const rpc_method rpc_methods[] = { |
olympux | 19:e65ed29fd4d1 | 123 | {"read" , rpc_method_caller<char *, RPCVariable, &RPCVariable::read> }, |
olympux | 19:e65ed29fd4d1 | 124 | {"write", rpc_method_caller<RPCVariable, char *, &RPCVariable::write> }, |
olympux | 19:e65ed29fd4d1 | 125 | RPC_METHOD_END |
olympux | 19:e65ed29fd4d1 | 126 | }; |
olympux | 19:e65ed29fd4d1 | 127 | return rpc_methods; |
olympux | 19:e65ed29fd4d1 | 128 | } |
olympux | 19:e65ed29fd4d1 | 129 | |
olympux | 19:e65ed29fd4d1 | 130 | private: |
olympux | 19:e65ed29fd4d1 | 131 | char *_var; |
olympux | 19:e65ed29fd4d1 | 132 | |
olympux | 19:e65ed29fd4d1 | 133 | // Holds the number of chars excluding the terminating null. |
olympux | 19:e65ed29fd4d1 | 134 | int _size; |
olympux | 19:e65ed29fd4d1 | 135 | }; |
olympux | 19:e65ed29fd4d1 | 136 | |
mbed_official | 0:efe8172b4113 | 137 | } |
mbed_official | 0:efe8172b4113 | 138 | |
mbed_official | 0:efe8172b4113 | 139 | #endif //RPCVARIABLE_H_ |