mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
NOTE:
This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!
mbed-connector-interface/Resource.h@127:b4a661ff6fb9, 2017-09-26 (annotated)
- Committer:
- ansond
- Date:
- Tue Sep 26 16:01:31 2017 +0000
- Revision:
- 127:b4a661ff6fb9
- Parent:
- 47:ab0a62cf8d85
minor re-ordering of FCC init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:1f1f55e73248 | 1 | /** |
ansond | 0:1f1f55e73248 | 2 | * @file Resource.h |
ansond | 0:1f1f55e73248 | 3 | * @brief mbed CoAP Endpoint Resource base class template |
ansond | 0:1f1f55e73248 | 4 | * @author Doug Anson/Chris Paola |
ansond | 0:1f1f55e73248 | 5 | * @version 1.0 |
ansond | 0:1f1f55e73248 | 6 | * @see |
ansond | 0:1f1f55e73248 | 7 | * |
ansond | 0:1f1f55e73248 | 8 | * Copyright (c) 2014 |
ansond | 0:1f1f55e73248 | 9 | * |
ansond | 0:1f1f55e73248 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 0:1f1f55e73248 | 11 | * you may not use this file except in compliance with the License. |
ansond | 0:1f1f55e73248 | 12 | * You may obtain a copy of the License at |
ansond | 0:1f1f55e73248 | 13 | * |
ansond | 0:1f1f55e73248 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 0:1f1f55e73248 | 15 | * |
ansond | 0:1f1f55e73248 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 0:1f1f55e73248 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 0:1f1f55e73248 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 0:1f1f55e73248 | 19 | * See the License for the specific language governing permissions and |
ansond | 0:1f1f55e73248 | 20 | * limitations under the License. |
ansond | 0:1f1f55e73248 | 21 | */ |
ansond | 0:1f1f55e73248 | 22 | |
ansond | 0:1f1f55e73248 | 23 | #ifndef __RESOURCE_H__ |
ansond | 0:1f1f55e73248 | 24 | #define __RESOURCE_H__ |
ansond | 0:1f1f55e73248 | 25 | |
ansond | 0:1f1f55e73248 | 26 | // logging facility |
ansond | 0:1f1f55e73248 | 27 | #include "mbed-connector-interface/Logger.h" |
ansond | 0:1f1f55e73248 | 28 | |
ansond | 0:1f1f55e73248 | 29 | // mbed-client support |
ansond | 0:1f1f55e73248 | 30 | #include "mbed-client/m2minterfacefactory.h" |
ansond | 0:1f1f55e73248 | 31 | #include "mbed-client/m2minterfaceobserver.h" |
ansond | 0:1f1f55e73248 | 32 | #include "mbed-client/m2minterface.h" |
ansond | 0:1f1f55e73248 | 33 | #include "mbed-client/m2mobjectinstance.h" |
ansond | 0:1f1f55e73248 | 34 | #include "mbed-client/m2mresource.h" |
ansond | 33:1d0b855df5a5 | 35 | #include "mbed-client/m2mdevice.h" |
ansond | 33:1d0b855df5a5 | 36 | #include "mbed-client/m2mfirmware.h" |
ansond | 0:1f1f55e73248 | 37 | |
ansond | 0:1f1f55e73248 | 38 | // string support |
ansond | 0:1f1f55e73248 | 39 | #include <string> |
ansond | 0:1f1f55e73248 | 40 | |
ansond | 0:1f1f55e73248 | 41 | /** Resource class |
ansond | 0:1f1f55e73248 | 42 | */ |
ansond | 0:1f1f55e73248 | 43 | template <typename InnerType> class Resource |
ansond | 0:1f1f55e73248 | 44 | { |
ansond | 0:1f1f55e73248 | 45 | public: |
ansond | 27:b8aaf7dc7023 | 46 | // Available Resource Types |
ansond | 27:b8aaf7dc7023 | 47 | typedef enum { |
ansond | 27:b8aaf7dc7023 | 48 | STRING, |
ansond | 27:b8aaf7dc7023 | 49 | INTEGER, |
ansond | 27:b8aaf7dc7023 | 50 | FLOAT, |
ansond | 27:b8aaf7dc7023 | 51 | BOOLEAN, |
ansond | 27:b8aaf7dc7023 | 52 | OPAQUE, |
ansond | 27:b8aaf7dc7023 | 53 | TIME, |
ansond | 27:b8aaf7dc7023 | 54 | OBJLINK |
ansond | 27:b8aaf7dc7023 | 55 | }ResourceType; |
ansond | 27:b8aaf7dc7023 | 56 | |
ansond | 0:1f1f55e73248 | 57 | /** |
ansond | 0:1f1f55e73248 | 58 | Default constructor |
ansond | 0:1f1f55e73248 | 59 | @param logger input the Logger instance this Resource is a part of |
ansond | 0:1f1f55e73248 | 60 | @param obj_name input the Object Name |
ansond | 0:1f1f55e73248 | 61 | @param res_name input the Resource URI/Name |
ansond | 0:1f1f55e73248 | 62 | @param value input the Resource value |
ansond | 0:1f1f55e73248 | 63 | */ |
ansond | 0:1f1f55e73248 | 64 | Resource(const Logger *logger,const string obj_name,const string res_name,InnerType value) { |
ansond | 0:1f1f55e73248 | 65 | this->init(logger); |
ansond | 0:1f1f55e73248 | 66 | this->m_obj_name = obj_name; |
ansond | 0:1f1f55e73248 | 67 | this->m_res_name = res_name; |
ansond | 0:1f1f55e73248 | 68 | this->m_value = value; |
ansond | 0:1f1f55e73248 | 69 | this->m_implements_observation = false; |
ansond | 45:db754b994deb | 70 | this->m_instance_number = 0; |
ansond | 0:1f1f55e73248 | 71 | } |
ansond | 0:1f1f55e73248 | 72 | |
ansond | 0:1f1f55e73248 | 73 | /** |
ansond | 0:1f1f55e73248 | 74 | Copy constructor |
ansond | 0:1f1f55e73248 | 75 | @param resource input the Resource that is to be deep copied |
ansond | 0:1f1f55e73248 | 76 | */ |
ansond | 0:1f1f55e73248 | 77 | Resource(const Resource<InnerType> &resource) { |
ansond | 0:1f1f55e73248 | 78 | this->init(resource.m_logger); |
ansond | 0:1f1f55e73248 | 79 | this->m_endpoint = resource.m_endpoint; |
ansond | 0:1f1f55e73248 | 80 | this->m_obj_name = resource.m_obj_name; |
ansond | 0:1f1f55e73248 | 81 | this->m_res_name = resource.m_res_name; |
ansond | 0:1f1f55e73248 | 82 | this->m_value = resource.m_value; |
ansond | 0:1f1f55e73248 | 83 | this->m_implements_observation = resource.m_implements_observation; |
ansond | 45:db754b994deb | 84 | this->m_instance_number = resource.m_instance_number; |
ansond | 0:1f1f55e73248 | 85 | } |
ansond | 0:1f1f55e73248 | 86 | |
ansond | 0:1f1f55e73248 | 87 | /** |
ansond | 0:1f1f55e73248 | 88 | Destructor |
ansond | 0:1f1f55e73248 | 89 | */ |
ansond | 0:1f1f55e73248 | 90 | virtual ~Resource() { |
ansond | 0:1f1f55e73248 | 91 | } |
ansond | 0:1f1f55e73248 | 92 | |
ansond | 0:1f1f55e73248 | 93 | /** |
ansond | 0:1f1f55e73248 | 94 | Get the Object name |
ansond | 0:1f1f55e73248 | 95 | @return the name of the object |
ansond | 0:1f1f55e73248 | 96 | */ |
ansond | 0:1f1f55e73248 | 97 | string getObjName() { |
ansond | 0:1f1f55e73248 | 98 | return this->m_obj_name; |
ansond | 0:1f1f55e73248 | 99 | } |
ansond | 0:1f1f55e73248 | 100 | |
ansond | 45:db754b994deb | 101 | /** |
ansond | 0:1f1f55e73248 | 102 | Get the Resource name |
ansond | 0:1f1f55e73248 | 103 | @return the name of the resource |
ansond | 0:1f1f55e73248 | 104 | */ |
ansond | 0:1f1f55e73248 | 105 | string getResName() { |
ansond | 0:1f1f55e73248 | 106 | return this->m_res_name; |
ansond | 0:1f1f55e73248 | 107 | } |
ansond | 0:1f1f55e73248 | 108 | |
ansond | 45:db754b994deb | 109 | /** |
ansond | 0:1f1f55e73248 | 110 | Get the Full name |
ansond | 0:1f1f55e73248 | 111 | @return the name of the object |
ansond | 0:1f1f55e73248 | 112 | */ |
ansond | 0:1f1f55e73248 | 113 | string getFullName() { |
ansond | 45:db754b994deb | 114 | char buf[5]; |
ansond | 45:db754b994deb | 115 | memset(buf,0,5); |
ansond | 45:db754b994deb | 116 | sprintf(buf,"/%d/",this->m_instance_number); |
ansond | 45:db754b994deb | 117 | return this->m_obj_name + buf + this->m_res_name; |
ansond | 0:1f1f55e73248 | 118 | } |
ansond | 0:1f1f55e73248 | 119 | |
ansond | 0:1f1f55e73248 | 120 | /** |
ansond | 0:1f1f55e73248 | 121 | Get the resource value |
ansond | 0:1f1f55e73248 | 122 | @return the value of the resource |
ansond | 0:1f1f55e73248 | 123 | */ |
ansond | 0:1f1f55e73248 | 124 | InnerType getValue() { |
ansond | 0:1f1f55e73248 | 125 | return this->m_value; |
ansond | 0:1f1f55e73248 | 126 | } |
ansond | 0:1f1f55e73248 | 127 | |
ansond | 0:1f1f55e73248 | 128 | /** |
ansond | 0:1f1f55e73248 | 129 | Set the resource name |
ansond | 0:1f1f55e73248 | 130 | @param name input the resource name |
ansond | 0:1f1f55e73248 | 131 | */ |
ansond | 0:1f1f55e73248 | 132 | void setName(const string name) { |
ansond | 0:1f1f55e73248 | 133 | this->m_name = name; |
ansond | 0:1f1f55e73248 | 134 | } |
ansond | 0:1f1f55e73248 | 135 | |
ansond | 0:1f1f55e73248 | 136 | /** |
ansond | 0:1f1f55e73248 | 137 | Set the resource value |
ansond | 0:1f1f55e73248 | 138 | @param value input the resource value |
ansond | 0:1f1f55e73248 | 139 | */ |
ansond | 0:1f1f55e73248 | 140 | void setValue(const InnerType value) { |
ansond | 0:1f1f55e73248 | 141 | this->m_value = value; |
ansond | 0:1f1f55e73248 | 142 | } |
ansond | 0:1f1f55e73248 | 143 | |
ansond | 0:1f1f55e73248 | 144 | /** |
ansond | 0:1f1f55e73248 | 145 | Bind resource to endpoint |
ansond | 27:b8aaf7dc7023 | 146 | @param ep input pointer to the Endpoint instance |
ansond | 0:1f1f55e73248 | 147 | */ |
ansond | 27:b8aaf7dc7023 | 148 | virtual void bind(void *ep) = 0; |
ansond | 0:1f1f55e73248 | 149 | |
ansond | 0:1f1f55e73248 | 150 | // access the logger() |
ansond | 0:1f1f55e73248 | 151 | Logger *logger() { |
ansond | 0:1f1f55e73248 | 152 | return this->m_logger; |
ansond | 0:1f1f55e73248 | 153 | } |
ansond | 0:1f1f55e73248 | 154 | |
ansond | 0:1f1f55e73248 | 155 | /** |
ansond | 0:1f1f55e73248 | 156 | set the options |
ansond | 0:1f1f55e73248 | 157 | */ |
ansond | 0:1f1f55e73248 | 158 | void setOptions(const void *options) { |
ansond | 0:1f1f55e73248 | 159 | this->m_options = (void *)options; |
ansond | 0:1f1f55e73248 | 160 | } |
ansond | 18:996d04762f32 | 161 | |
ansond | 18:996d04762f32 | 162 | /** |
ansond | 18:996d04762f32 | 163 | set the endpoint |
ansond | 18:996d04762f32 | 164 | */ |
ansond | 18:996d04762f32 | 165 | void setEndpoint(const void *endpoint) { |
ansond | 18:996d04762f32 | 166 | this->m_endpoint = (void *)endpoint; |
ansond | 18:996d04762f32 | 167 | } |
ansond | 0:1f1f55e73248 | 168 | |
ansond | 0:1f1f55e73248 | 169 | // this resource implements its own observation handler |
ansond | 0:1f1f55e73248 | 170 | bool implementsObservation() { return this->m_implements_observation; } |
ansond | 45:db754b994deb | 171 | |
ansond | 45:db754b994deb | 172 | /** |
ansond | 45:db754b994deb | 173 | Get our Instance Number |
ansond | 45:db754b994deb | 174 | @return our instance number |
ansond | 45:db754b994deb | 175 | */ |
ansond | 47:ab0a62cf8d85 | 176 | int getInstanceNumber() { return this->m_instance_number; } |
ansond | 45:db754b994deb | 177 | |
ansond | 45:db754b994deb | 178 | /** |
ansond | 45:db754b994deb | 179 | Set our Instance Number |
ansond | 45:db754b994deb | 180 | @param instance_number input our designated instance number |
ansond | 45:db754b994deb | 181 | */ |
ansond | 45:db754b994deb | 182 | void setInstanceNumber(int instance_number) { this->m_instance_number = instance_number; } |
ansond | 0:1f1f55e73248 | 183 | |
ansond | 0:1f1f55e73248 | 184 | protected: |
ansond | 0:1f1f55e73248 | 185 | // initialize internals to Resource |
ansond | 0:1f1f55e73248 | 186 | void init(const Logger *logger) { |
ansond | 0:1f1f55e73248 | 187 | this->m_logger = (Logger *)logger; |
ansond | 0:1f1f55e73248 | 188 | this->m_endpoint = NULL; |
ansond | 0:1f1f55e73248 | 189 | this->m_obj_name = ""; |
ansond | 0:1f1f55e73248 | 190 | this->m_res_name = ""; |
ansond | 0:1f1f55e73248 | 191 | this->m_value = ""; |
ansond | 45:db754b994deb | 192 | this->m_instance_number = 0; |
ansond | 0:1f1f55e73248 | 193 | } |
ansond | 0:1f1f55e73248 | 194 | |
ansond | 0:1f1f55e73248 | 195 | // get our options |
ansond | 0:1f1f55e73248 | 196 | void *getOptions() { |
ansond | 0:1f1f55e73248 | 197 | return this->m_options; |
ansond | 0:1f1f55e73248 | 198 | } |
ansond | 0:1f1f55e73248 | 199 | |
ansond | 17:defb680f8fce | 200 | Logger *m_logger; |
ansond | 0:1f1f55e73248 | 201 | void *m_endpoint; |
ansond | 0:1f1f55e73248 | 202 | string m_obj_name; |
ansond | 0:1f1f55e73248 | 203 | string m_res_name; |
ansond | 17:defb680f8fce | 204 | InnerType m_value; |
ansond | 17:defb680f8fce | 205 | bool m_implements_observation; |
ansond | 17:defb680f8fce | 206 | void *m_options; |
ansond | 45:db754b994deb | 207 | int m_instance_number; |
ansond | 0:1f1f55e73248 | 208 | }; |
ansond | 0:1f1f55e73248 | 209 | |
ansond | 0:1f1f55e73248 | 210 | #endif // __RESOURCE_H__ |
ansond | 0:1f1f55e73248 | 211 |