Friendly LWM2M client
ObjectImpl.h
Go to the documentation of this file.
1 /*
2  * ObjectImpl.h
3  *
4  * Created on: 10 Jul 2023
5  * Author: valentin
6  */
7 
8 #ifndef WPP_OBJECT_IMPL_H_
9 #define WPP_OBJECT_IMPL_H_
10 
11 #include "Object.h"
12 
13 namespace wpp {
14 
24 template<typename T>
25 class ObjectImpl : public Object {
26 public:
30  ObjectImpl(lwm2m_context_t &context): Object(context) {}
36  ObjectImpl(lwm2m_context_t &context, const ObjectInfo &info): Object(context, info) {}
38 
39  /* ------------- ObjectImpl instance management ------------- */
40 
46  Instance* createInstance(ID_T instanceID = ID_T_MAX_VAL) override;
47 };
48 
49 /* ---------- Implementation of methods ----------*/
50 template<typename T>
52  // If object is single and instance has already exist, then we can not create new one and return NULL
53  if (getObjectInfo().isSingle == IS_SINGLE::SINGLE && instanceCnt() != 0) {
54  WPP_LOGW(TAG_WPP_OBJ, "Not possible to create instance %d:%d, object is single", getObjectID(), instanceId);
55  return NULL;
56  }
57  // If instanceId == ID_T_MAX_VAL, it is mean that user do not want to set its own identifier, so we will choose ours
58  if (instanceId == ID_T_MAX_VAL) instanceId = getFirstAvailableInstanceID();
59  // If ID has been already occupied, then we can not create new instance with such ID and returns NULL
60  if (instanceId == ID_T_MAX_VAL || isExist(instanceId)) {
61  WPP_LOGW(TAG_WPP_OBJ, "Not possible to create instance %d:%d, ID has been already occupied", getObjectID(), instanceId);
62  return NULL;
63  }
64 
65  WPP_LOGD(TAG_WPP_OBJ, "Creating instance %d:%d", getObjectID(), instanceId);
66  // Creation and registration new instance in core object
67  lwm2m_list_t *element = new lwm2m_list_t;
68  if (NULL == element) return NULL;
69  element->next = NULL;
70  element->id = instanceId;
71  _lwm2m_object.instanceList = LWM2M_LIST_ADD(_lwm2m_object.instanceList, element);
72 
73  // TODO: Add check to each new or malloc operation
74  // Creating new instance
75  T *inst = new T(_context, {(ID_T)_objInfo.objID, instanceId});
76  _instances.push_back(inst);
77 
78  // Update server registration
79  lwm2m_update_registration(&getContext(), 0, false, true);
80 
81  return inst;
82 }
83 
84 } // namespace wpp
85 #endif /* WPP_OBJECT_IMPL_H_ */
#define WPP_LOGW(TAG, FMT,...)
Definition: WppLogs.h:43
#define TAG_WPP_OBJ
Definition: WppLogs.h:11
#define WPP_LOGD(TAG, FMT,...)
Definition: WppLogs.h:31
#define ID_T_MAX_VAL
Definition: WppTypes.h:16
Instance is interface class that implements manipulation with derived class resources....
Definition: Instance.h:40
The ObjectImpl class is a template class that represents an implementation of an object in the Wakaam...
Definition: ObjectImpl.h:25
Instance * createInstance(ID_T instanceID=ID_T_MAX_VAL) override
Creates a new instance of the object.
Definition: ObjectImpl.h:51
ObjectImpl(lwm2m_context_t &context, const ObjectInfo &info)
Constructs an ObjectImpl object.
Definition: ObjectImpl.h:36
ObjectImpl(lwm2m_context_t &context)
Definition: ObjectImpl.h:30
The Object class implements manipulation with Instance interface class and its inheritors.
Definition: Object.h:32
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14
uint16_t ID_T
Definition: WppTypes.h:15
The ObjectInfo struct represents information about an object in the Wakaama data model.
Definition: ObjectInfo.h:16