Friendly LWM2M client
Resource.cpp
Go to the documentation of this file.
1 /*
2  * Resource.cpp
3  *
4  * Created on: 10 Jul 2023
5  * Author: valentin
6  */
7 
8 #include "Resource.h"
9 
10 namespace wpp {
11 
12 /* ---------- Public methods for common usage ----------*/
13 
15  _id(ID_T_MAX_VAL), _operation(), _isSingle(IS_SINGLE::MULTIPLE), _isMandatory(IS_MANDATORY::OPTIONAL), _typeID(TYPE_ID::UNDEFINED) {
16 }
17 
18 Resource::Resource(ID_T id, const ItemOp &operation, IS_SINGLE isSingle, IS_MANDATORY isMandatory, TYPE_ID dataType):
19  _id(id), _operation(operation), _isSingle(isSingle), _isMandatory(isMandatory), _typeID(dataType) {
20 }
21 
22 Resource::Resource(const Resource& resource) {
23  _id = resource._id;
24  _operation = resource._operation;
25  _isSingle = resource._isSingle;
26  _isMandatory = resource._isMandatory;
27  _typeID = resource._typeID;
28  _instances = resource._instances;
29  _dataVerifier = resource._dataVerifier;
30 }
31 
33  _id = resource._id;
34  _operation = resource._operation;
35  _isSingle = resource._isSingle;
36  _isMandatory = resource._isMandatory;
37  _typeID = resource._typeID;
38  _instances = std::move(resource._instances);
39  resource._instances.clear();
40  _dataVerifier = resource._dataVerifier;
41 }
42 
44  if (this == &resource) return *this;
45 
46  _id = resource._id;
47  _operation = resource._operation;
48  _isSingle = resource._isSingle;
49  _isMandatory = resource._isMandatory;
50  _typeID = resource._typeID;
51  _instances = resource._instances;
52  _dataVerifier = resource._dataVerifier;
53 
54  return *this;
55 }
56 
58  if (this == &resource) return *this;
59 
60  _id = resource._id;
61  _operation = resource._operation;
62  _isSingle = resource._isSingle;
63  _isMandatory = resource._isMandatory;
64  _typeID = resource._typeID;
65  _instances = std::move(resource._instances);
66  resource._instances.clear();
67  _dataVerifier = resource._dataVerifier;
68 
69  return *this;
70 }
71 
73  return _id;
74 }
75 
77  return _typeID;
78 }
79 
81  return _operation;
82 }
83 
84 bool Resource::isMandatory() const {
85  return _isMandatory == IS_MANDATORY::MANDATORY;
86 }
87 
88 bool Resource::isOptional() const {
89  return _isMandatory == IS_MANDATORY::OPTIONAL;
90 }
91 
92 bool Resource::isSingle() const {
93  return _isSingle == IS_SINGLE::SINGLE;
94 }
95 
96 bool Resource::isMultiple() const {
97  return _isSingle == IS_SINGLE::MULTIPLE;
98 }
99 
100 bool Resource::isInstanceIdPossible(ID_T resInstId) const {
101  return isMultiple() || resInstId == SINGLE_INSTANCE_ID;
102 }
103 
104 bool Resource::isExist(ID_T resInstId) const {
105  return getInstIter(resInstId) != _instances.end();
106 }
107 
108 bool Resource::isTypeIdCompatible(TYPE_ID type) const {
109  switch (type) {
110  case TYPE_ID::INT:
111  case TYPE_ID::TIME:
112  return _typeID == TYPE_ID::INT || _typeID == TYPE_ID::TIME;
113 
114  case TYPE_ID::STRING:
115  case TYPE_ID::CORE_LINK:
116  return _typeID == TYPE_ID::STRING || _typeID == TYPE_ID::CORE_LINK;
117  default: break;
118  }
119 
120  return _typeID == type;
121 }
122 
123 size_t Resource::instCount() const {
124  return _instances.size();
125 }
126 
127 std::vector<ID_T> Resource::instIds() const {
128  std::vector<ID_T> ids;
129  ids.reserve(_instances.size());
130  std::transform(_instances.begin(), _instances.end(), std::back_inserter(ids), [](const auto& inst) { return inst.id; });
131  return ids;
132 }
133 
135  // Usually, each subsequent free index will be equal to the number of created instances
136  if (!isExist(_instances.size())) return _instances.size();
137  // If there are no free indexes, we will search for the first free index
138  ID_T id = 0;
139  while (isExist(id) && id != ID_T_MAX_VAL) id++;
140  return id;
141 }
142 
143 /* ---------- Methods for get and set resource value ----------*/
144 bool Resource::remove(ID_T resInstId) {
145  if (isSingle() || !isExist(resInstId)) {
146  WPP_LOGW(TAG_WPP_RES, "Resource[%d], instance with ID %d not found or resource is SINGLE", _id, resInstId);
147  return false;
148  }
149  auto instForRemove = getInstIter(resInstId);
150  _instances.erase(instForRemove);
151 
152  return true;
153 }
154 
156  if (isSingle()) {
157  WPP_LOGW(TAG_WPP_RES, "Resource[%d] is SINGLE", _id);
158  return false;
159  }
160  _instances.clear();
161  return true;
162 }
163 
165  if (!isDataVerifierValid(verifier)) {
166  WPP_LOGW(TAG_WPP_RES, "Resource[%d] verifier is not valid", _id);
167  return false;
168  }
169  _dataVerifier = verifier;
170  return true;
171 }
172 
173 bool Resource::isDataVerifierValid(const DATA_VERIFIER_T &verifier) const {
174  if (std::holds_alternative<VERIFY_BOOL_T>(verifier) && std::get<VERIFY_BOOL_T>(verifier)) return _typeID == TYPE_ID::BOOL;
175  else if (std::holds_alternative<VERIFY_INT_T>(verifier) && std::get<VERIFY_INT_T>(verifier)) return _typeID == TYPE_ID::INT;
176  else if (std::holds_alternative<VERIFY_UINT_T>(verifier) && std::get<VERIFY_UINT_T>(verifier)) return _typeID == TYPE_ID::UINT;
177  else if (std::holds_alternative<VERIFY_FLOAT_T>(verifier) && std::get<VERIFY_FLOAT_T>(verifier)) return _typeID == TYPE_ID::FLOAT;
178  else if (std::holds_alternative<VERIFY_OPAQUE_T>(verifier) && std::get<VERIFY_OPAQUE_T>(verifier)) return _typeID == TYPE_ID::OPAQUE;
179  else if (std::holds_alternative<VERIFY_OBJ_LINK_T>(verifier) && std::get<VERIFY_OBJ_LINK_T>(verifier)) return _typeID == TYPE_ID::OBJ_LINK;
180  // VERIFY_CORE_LINK_T the same as VERIFY_STRING_T therefore we use only VERIFY_STRING_T
181  else if (std::holds_alternative<VERIFY_STRING_T>(verifier) && std::get<VERIFY_STRING_T>(verifier)) return _typeID == TYPE_ID::STRING || _typeID == TYPE_ID::CORE_LINK;
182  else if (std::holds_alternative<VERIFY_EXECUTE_T>(verifier) && std::get<VERIFY_EXECUTE_T>(verifier)) return _typeID == TYPE_ID::EXECUTE;
183  else return false;
184 }
185 
186 std::vector<Resource::ResInst>::iterator Resource::getInstIter(ID_T resInstId) const {
187  auto finder = [&resInstId](const ResInst &inst) -> bool { return inst.id == resInstId; };
188  return std::find_if(_instances.begin(), _instances.end(), finder);
189 }
190 
191 } //namespace wpp
192 
193 
#define TAG_WPP_RES
Definition: WppLogs.h:13
#define WPP_LOGW(TAG, FMT,...)
Definition: WppLogs.h:43
#define SINGLE_INSTANCE_ID
Definition: WppTypes.h:13
#define ID_T_MAX_VAL
Definition: WppTypes.h:16
The Resource class in the wpp namespace is a comprehensive and flexible class designed to handle diff...
Definition: Resource.h:49
bool clear()
Remove all instances.
Definition: Resource.cpp:155
bool isExist(ID_T resInstId) const
Check if the instance ID is exist.
Definition: Resource.cpp:104
bool isMultiple() const
Definition: Resource.cpp:96
Resource & operator=(const Resource &other)
Definition: Resource.cpp:43
bool remove(ID_T resInstId)
Remove resource instance if resource is MULTIPLE and instance exists, if the resource is SINGLE remov...
Definition: Resource.cpp:144
ID_T getId() const
Definition: Resource.cpp:72
std::variant< VERIFY_INT_T, VERIFY_UINT_T, VERIFY_FLOAT_T, VERIFY_OPAQUE_T, VERIFY_BOOL_T, VERIFY_OBJ_LINK_T, VERIFY_STRING_T, VERIFY_EXECUTE_T > DATA_VERIFIER_T
Universal type for data validation functions.
Definition: Resource.h:68
bool isSingle() const
Definition: Resource.cpp:92
bool isMandatory() const
Definition: Resource.cpp:84
const ItemOp & getOperation() const
Definition: Resource.cpp:80
size_t instCount() const
Get the number of resource instances.
Definition: Resource.cpp:123
ID_T newInstId() const
Find first available instance ID that is not used.
Definition: Resource.cpp:134
std::vector< ID_T > instIds() const
Returns vector with available ids of resource instances.
Definition: Resource.cpp:127
bool setDataVerifier(const DATA_VERIFIER_T &verifier)
Set data verifier for the resource.
Definition: Resource.cpp:164
TYPE_ID getTypeId() const
Definition: Resource.cpp:76
bool isOptional() const
Definition: Resource.cpp:88
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14
IS_MANDATORY
Definition: WppTypes.h:116
uint16_t ID_T
Definition: WppTypes.h:15
IS_SINGLE
Definition: WppTypes.h:111
TYPE_ID
Wpp data types ID.
Definition: WppTypes.h:23
The ItemOp struct represents the operations that can be performed on a instance/resource.
Definition: ItemOp.h:24