Friendly LWM2M client
Lwm2mAccessControl.cpp
Go to the documentation of this file.
1 /*
2  * Lwm2mAccessControl
3  * Generated on: 2023-11-03 15:05:47
4  * Created by: Sinai RnD
5  */
6 
8 
9 #include "Resource.h"
10 #include "ItemOp.h"
11 #include "WppTypes.h"
12 #include "WppLogs.h"
13 #include "WppClient.h"
14 
15 /* --------------- Code_cpp block 0 start --------------- */
16 #define AC_OBJ_ID_MIN 0
17 #define AC_OBJ_ID_MAX (ID_T_MAX_VAL-1)
18 #define AC_OBJ_INST_ID_MIN 0
19 #define AC_OBJ_INST_ID_MAX ID_T_MAX_VAL
20 #if RES_2_2
21 #define ACL_MIN 0
22 #define ACL_MAX 0x32
23 #endif
24 #define AC_OWNER_MIN 0
25 #define AC_OWNER_MAX ID_T_MAX_VAL
26 /* --------------- Code_cpp block 0 end --------------- */
27 
28 #define TAG "Lwm2mAccessControl"
29 
30 namespace wpp {
31 
32 Lwm2mAccessControl::Lwm2mAccessControl(lwm2m_context_t &context, const OBJ_LINK_T &id): Instance(context, id) {
33 
34  /* --------------- Code_cpp block 1 start --------------- */
35  lwm2m_ac_request_update_policy(&context, false);
36  /* --------------- Code_cpp block 1 end --------------- */
37 
38  resourcesCreate();
39  resourcesInit();
40 
41  /* --------------- Code_cpp block 2 start --------------- */
42  /* --------------- Code_cpp block 2 end --------------- */
43 }
44 
46  /* --------------- Code_cpp block 3 start --------------- */
47  lwm2m_ac_request_update_policy(&getContext(), false);
48  /* --------------- Code_cpp block 3 end --------------- */
49 }
50 
52  return ctx.registry().lwm2mAccessControl();
53 }
54 
56  Instance *inst = ctx.registry().lwm2mAccessControl().instance(instId);
57  if (!inst) return NULL;
58  return static_cast<Lwm2mAccessControl*>(inst);
59 }
60 
62  Instance *inst = ctx.registry().lwm2mAccessControl().createInstance(instId);
63  if (!inst) return NULL;
64  return static_cast<Lwm2mAccessControl*>(inst);
65 }
66 
68  return ctx.registry().lwm2mAccessControl().remove(instId);
69 }
70 
71 void Lwm2mAccessControl::serverOperationNotifier(Instance *securityInst, ItemOp::TYPE type, const ResLink &resLink) {
72  /* --------------- Code_cpp block 4 start --------------- */
73  lwm2m_ac_request_update_policy(&getContext(), false);
74  /* --------------- Code_cpp block 4 end --------------- */
75 
76  operationNotify(*this, resLink, type);
77 
78  /* --------------- Code_cpp block 5 start --------------- */
79  /* --------------- Code_cpp block 5 end --------------- */
80 }
81 
83  if (type == ItemOp::WRITE || type == ItemOp::DELETE) notifyResChanged(resLink.resId, resLink.resInstId);
84 
85  /* --------------- Code_cpp block 6 start --------------- */
86  lwm2m_ac_request_update_policy(&getContext(), false);
87  /* --------------- Code_cpp block 6 end --------------- */
88 }
89 
90 void Lwm2mAccessControl::resourcesCreate() {
91  std::vector<Resource> resources = {
94  #if RES_2_2
96  #endif
98  };
99  setupResources(std::move(resources));
100 }
101 
102 void Lwm2mAccessControl::resourcesInit() {
103  /* --------------- Code_cpp block 7 start --------------- */
104 
106  resource(OBJECT_ID_0)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return AC_OBJ_ID_MIN <= value && value <= AC_OBJ_ID_MAX; });
107 
109  resource(OBJECT_INSTANCE_ID_1)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return AC_OBJ_INST_ID_MIN <= value && value <= AC_OBJ_INST_ID_MAX; });
110 
111  #if RES_2_2
112  resource(ACL_2)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return ACL_MIN <= value && value < ACL_MAX; });
113  #endif
114 
116  resource(ACCESS_CONTROL_OWNER_3)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return AC_OWNER_MIN <= value && value <= AC_OWNER_MAX; });
117 
118  /* --------------- Code_cpp block 7 end --------------- */
119 }
120 
121 /* --------------- Code_cpp block 8 start --------------- */
122 Lwm2mAccessControl * Lwm2mAccessControl::getAcInstForTarget(Object &acObj, ID_T objId, ID_T objInstId) {
123  // Find the ac object instance for the target object
124  for (auto &inst : acObj.instances()) {
125  INT_T tmpObjId = inst->get<INT_T>(OBJECT_ID_0);
126  INT_T tmpObjInstId = inst->get<INT_T>(OBJECT_INSTANCE_ID_1);
127  if (tmpObjId == objId && tmpObjInstId == objInstId) return static_cast<Lwm2mAccessControl*>(inst);
128  }
129  return NULL;
130 }
131 
132 Lwm2mAccessControl * Lwm2mAccessControl::create(Object &targetObj, uint8_t defaultAcl) {
133  WppRegistry &registry = targetObj.getRegistry();
134  auto &acObj = registry.lwm2mAccessControl();
135 
136  // Check if the object instance already exists
137  if (getAcInstForTarget(acObj, targetObj.getObjectID(), AC_OBJ_INST_NOT_SET)) return NULL;
138 
139  Lwm2mAccessControl *acInst = static_cast<Lwm2mAccessControl *>(acObj.createInstance());
140  acInst->set<INT_T>(OBJECT_ID_0, targetObj.getObjectID());
142  #if RES_2_2
143  acInst->set<INT_T>(ACL_2, AC_ACL_DEFAULT_ID, defaultAcl & ALL_OBJ_RIGHTS);
144  #endif
146 
147  return acInst;
148 }
149 
151  WppRegistry &registry = targetObj.getRegistry();
152  auto &acObj = registry.lwm2mAccessControl();
153  Lwm2mAccessControl *acInst = getAcInstForTarget(acObj, targetObj.getObjectID(), AC_OBJ_INST_NOT_SET);
154 
155  if (acInst) acObj.remove(acInst->getInstanceID());
156 }
157 
158 #if RES_2_2
159 bool Lwm2mAccessControl::addAcl(Object &targetObj, ID_T serverShortId, uint8_t acl) {
160  WppRegistry &registry = targetObj.getRegistry();
161  auto &acObj = registry.lwm2mAccessControl();
162  Lwm2mAccessControl *acInst = getAcInstForTarget(acObj, targetObj.getObjectID(), AC_OBJ_INST_NOT_SET);
163 
164  if (!acInst) return false;
165 
166  acInst->set<INT_T>(ACL_2, serverShortId, acl & ALL_OBJ_RIGHTS);
167 
168  return true;
169 }
170 
171 void Lwm2mAccessControl::removeAcl(Object &targetObj, ID_T serverShortId) {
172  WppRegistry &registry = targetObj.getRegistry();
173  auto &acObj = registry.lwm2mAccessControl();
174  Lwm2mAccessControl *acInst = getAcInstForTarget(acObj, targetObj.getObjectID(), AC_OBJ_INST_NOT_SET);
175 
176  if (!acInst || !acInst->isExist(ACL_2, serverShortId)) return;
177  acInst->removeRes(ACL_2, serverShortId);
178 }
179 #endif
180 
181 Lwm2mAccessControl * Lwm2mAccessControl::create(Instance &targetInst, ID_T owner, uint8_t defaultAcl) {
182  WppRegistry &registry = targetInst.getRegistry();
183  auto &acObj = registry.lwm2mAccessControl();
184 
185  // Check if the object instance already exists
186  if (getAcInstForTarget(acObj, targetInst.getObjectID(), targetInst.getInstanceID())) return NULL;
187 
188  Lwm2mAccessControl *acInst = static_cast<Lwm2mAccessControl *>(acObj.createInstance());
189  acInst->set<INT_T>(OBJECT_ID_0, targetInst.getObjectID());
190  acInst->set<INT_T>(OBJECT_INSTANCE_ID_1, targetInst.getInstanceID());
191  #if RES_2_2
192  acInst->set<INT_T>(ACL_2, AC_ACL_DEFAULT_ID, defaultAcl & ALL_INST_RIGHTS);
193  #endif
194  acInst->set<INT_T>(ACCESS_CONTROL_OWNER_3, owner);
195 
196  return acInst;
197 }
198 
200  WppRegistry &registry = targetInst.getRegistry();
201  auto &acObj = registry.lwm2mAccessControl();
202  Lwm2mAccessControl *acInst = getAcInstForTarget(acObj, targetInst.getObjectID(), targetInst.getInstanceID());
203 
204  if (acInst) acObj.remove(acInst->getInstanceID());
205 }
206 
207 #if RES_2_2
208 bool Lwm2mAccessControl::addAcl(Instance &targetInst, ID_T serverShortId, uint8_t acl) {
209  WppRegistry &registry = targetInst.getRegistry();
210  auto &acObj = registry.lwm2mAccessControl();
211  Lwm2mAccessControl *acInst = getAcInstForTarget(acObj, targetInst.getObjectID(), targetInst.getInstanceID());
212 
213  if (!acInst) return false;
214  acInst->set<INT_T>(ACL_2, serverShortId, acl & ALL_INST_RIGHTS);
215 
216  return true;
217 }
218 
219 void Lwm2mAccessControl::removeAcl(Instance &targetInst, ID_T serverShortId) {
220  WppRegistry &registry = targetInst.getRegistry();
221  auto &acObj = registry.lwm2mAccessControl();
222  Lwm2mAccessControl *acInst = getAcInstForTarget(acObj, targetInst.getObjectID(), targetInst.getInstanceID());
223 
224  if (!acInst || !acInst->isExist(ACL_2, serverShortId)) return;
225  acInst->removeRes(ACL_2, serverShortId);
226 }
227 #endif
228 /* --------------- Code_cpp block 8 end --------------- */
229 
230 } /* namespace wpp */
#define AC_OBJ_INST_ID_MAX
#define AC_OBJ_ID_MIN
#define AC_OWNER_MAX
#define AC_OBJ_ID_MAX
#define AC_OWNER_MIN
#define AC_OBJ_INST_ID_MIN
#define AC_CLIENT_OWNER
#define AC_ACL_DEFAULT_ID
#define AC_OBJ_INST_NOT_SET
void operationNotify(Instance &inst, const ResLink &resLink, ItemOp::TYPE type)
Notifies the observers about an operation on an instance resource.
Definition: InstSubject.h:78
Instance is interface class that implements manipulation with derived class resources....
Definition: Instance.h:40
OBJ_ID getObjectID() const
Definition: Instance.h:52
WppRegistry & getRegistry()
Helpfull methods to get registry instances.
Definition: Instance.cpp:58
lwm2m_context_t & getContext()
Return context that can be used by derived class.
Definition: Instance.cpp:50
void notifyResChanged(ID_T resId, ID_T resInstId=ID_T_MAX_VAL)
Notify server about resource value change.
Definition: Instance.cpp:17
ID_T getInstanceID() const
Definition: Instance.h:53
Lwm2mAccessControl(lwm2m_context_t &context, const OBJ_LINK_T &id)
static bool removeInst(WppClient &ctx, ID_T instId)
Removes an instance of the object.
static Lwm2mAccessControl * createInst(WppClient &ctx, ID_T instId=ID_T_MAX_VAL)
Creates an instance of the object.
void serverOperationNotifier(Instance *securityInst, ItemOp::TYPE type, const ResLink &resLink) override
This method must be implemented by the derived class, and handle information about resource operation...
static Object & object(WppClient &ctx)
Gets the Object reference.
void userOperationNotifier(ItemOp::TYPE type, const ResLink &resLink) override
This method must be implemented by the derived class, and handle information about resource operation...
static void remove(Object &targetObj)
Delete Lwm2mAccessControl object instance for target object.
static Lwm2mAccessControl * create(Object &targetObj, uint8_t defaultAcl=NO_ACCESS)
Create Lwm2mAccessControl object instance for target object.
static Lwm2mAccessControl * instance(WppClient &ctx, ID_T instId=ID_T_MAX_VAL)
Gets an instance of the object.
static void removeAcl(Object &targetObj, ID_T serverShortId)
Remove ACL for target object.
static bool addAcl(Object &targetObj, ID_T serverShortId, uint8_t acl=ALL_OBJ_RIGHTS)
Add ACL for target object.
The Object class implements manipulation with Instance interface class and its inheritors.
Definition: Object.h:32
Instance * instance(ID_T instanceID=ID_T_MAX_VAL)
Gets an instance of the object.
Definition: Object.cpp:102
virtual Instance * createInstance(ID_T instanceID=ID_T_MAX_VAL)=0
Creates an instance of the object.
WppRegistry & getRegistry()
Helpfull methods to get registry instances.
Definition: Object.cpp:57
bool remove(ID_T instanceID)
Removes an instance of the object.
Definition: Object.cpp:82
OBJ_ID getObjectID() const
Gets the object ID.
Definition: Object.cpp:37
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< Resource > & resources()
This method return list with all resources that has been defined.
bool set(ID_T resId, const T &value)
Set data value by copy for the resource.
bool set(const T &value, ID_T resInstId=SINGLE_INSTANCE_ID)
Set data value by copy for the resource (instance)
Definition: Resource.h:253
bool setDataVerifier(const DATA_VERIFIER_T &verifier)
Set data verifier for the resource.
Definition: Resource.cpp:164
Represents a client interface for Wpp library.
Definition: WppClient.h:37
WppRegistry & registry()
Gets the WppRegistry associated with the WppClient.
Definition: WppClient.cpp:93
The WppRegistry class represents a registry for managing LWM2M objects.
Definition: WppRegistry.h:53
Object & lwm2mAccessControl()
Definition: WppRegistry.cpp:99
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14
int64_t INT_T
Definition: WppTypes.h:41
uint16_t ID_T
Definition: WppTypes.h:15
std::function< bool(const INT_T &)> VERIFY_INT_T
Data validation function types.
Definition: WppTypes.h:94
The ItemOp struct represents the operations that can be performed on a instance/resource.
Definition: ItemOp.h:24
TYPE
Enum representing the different types of operations.
Definition: ItemOp.h:29
@ DELETE
Definition: ItemOp.h:36