Friendly LWM2M client
ResourceContainer.cpp
Go to the documentation of this file.
1 #include "ResourceContainer.h"
2 
3 namespace wpp {
4 
6  auto res = resource(resId);
7  if (res == NULL) {
8  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
9  return false;
10  }
11  return res->isSingle();
12 }
13 
15  auto res = resource(resId);
16  if (res == NULL) {
17  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
18  return false;
19  }
20  return res->isMultiple();
21 }
22 
24  return resource(resId) != NULL;
25 }
26 
27 bool ResourceContainer::isExist(ID_T resId, ID_T resInstId) {
28  auto res = resource(resId);
29  if (res == NULL) {
30  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
31  return false;
32  }
33  return res->isExist(resInstId);
34 }
35 
37  auto res = resource(resId);
38  if (res == NULL) {
39  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
40  return 0;
41  }
42  return res->instCount();
43 }
44 
45 std::vector<ID_T> ResourceContainer::instIds(ID_T resId) {
46  auto res = resource(resId);
47  if (res == NULL) {
48  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
49  return std::vector<ID_T>();
50  }
51  return res->instIds();
52 }
53 
55  auto res = resource(resId);
56  if (res == NULL) {
57  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
58  return ID_T_MAX_VAL;
59  }
60  return res->newInstId();
61 }
62 
63 bool ResourceContainer::removeRes(ID_T resId, ID_T resInstId) {
64  auto res = resource(resId);
65  if (res == NULL) {
66  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
67  return false;
68  }
69  if (!res->remove(resInstId)) return false;
70  resourceOperationNotifier(ItemOp::DELETE, resId, resInstId);
71  return true;
72 }
73 
75  auto res = resource(resId);
76  if (res == NULL) {
77  WPP_LOGW(TAG_WPP_RES_CON, "Resource[%d] not found", resId);
78  return false;
79  }
80  if (!res->clear()) return false;
82  return true;
83 }
84 
85 void ResourceContainer::setupResources(const std::vector<Resource> &resources) {
86  _resources = resources;
87 }
88 
89 void ResourceContainer::setupResources(std::vector<Resource> &&resources) {
90  _resources = std::move(resources);
91 }
92 
94  auto finder = [&resId](const Resource &res) -> bool { return res.getId() == resId; };
95  auto res = std::find_if(_resources.begin(), _resources.end(), finder);
96  return res != _resources.end()? &(*res) : NULL;
97 }
98 
99 std::vector<Resource> & ResourceContainer::resources() {
100  return _resources;
101 }
102 
103 } // namespace wpp
#define TAG_WPP_RES_CON
Definition: WppLogs.h:14
#define WPP_LOGW(TAG, FMT,...)
Definition: WppLogs.h:43
#define ID_T_MAX_VAL
Definition: WppTypes.h:16
void setupResources(const std::vector< Resource > &resources)
This methods setup resources list.
Resource * resource(ID_T resId)
This method return resource ptr if it exists. If resources does not exist then return NULL.
std::vector< ID_T > instIds(ID_T resId)
Returns vector with available ids of resource instances.
std::vector< Resource > & resources()
This method return list with all resources that has been defined.
size_t instCount(ID_T resId)
Get the number of resource instances.
virtual void resourceOperationNotifier(ItemOp::TYPE type, ID_T resId, ID_T resInstId)=0
This method must be implemented by the derived class, and handle information about resource operation...
bool removeRes(ID_T resId, ID_T resInstId)
Remove resource instance if resource is MULTIPLE and instance exists, if the resource is SINGLE remov...
bool clearRes(ID_T resId)
Remove all instances.
bool isMultiple(ID_T resId)
Check if the resource is MULTIPLE.
ID_T newInstId(ID_T resId)
Find first available instance ID that is not used.
bool isExist(ID_T resId)
Check if the instance ID is exist.
bool isSingle(ID_T resId)
Check if the resource is SINGLE.
The Resource class in the wpp namespace is a comprehensive and flexible class designed to handle diff...
Definition: Resource.h:49
ID_T getId() const
Definition: Resource.cpp:72
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14
uint16_t ID_T
Definition: WppTypes.h:15
@ DELETE
Definition: ItemOp.h:36