Friendly LWM2M client
Device.cpp
Go to the documentation of this file.
1 /*
2  * Device
3  * Generated on: 2023-11-02 16:38:01
4  * Created by: Sinai RnD
5  */
6 
7 #include "m_3_device/Device.h"
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 #if RES_3_9
17 #define BAT_LVL_MIN 0
18 #define BAT_LVL_MAX 100
19 #endif
20 /* --------------- Code_cpp block 0 end --------------- */
21 
22 #define TAG "Device"
23 
24 namespace wpp {
25 
26 Device::Device(lwm2m_context_t &context, const OBJ_LINK_T &id): Instance(context, id) {
27 
28  /* --------------- Code_cpp block 1 start --------------- */
29  #if RES_3_13
30  _currentTimeTaskId = WPP_ERR_TASK_ID;
31  #endif
32  /* --------------- Code_cpp block 1 end --------------- */
33 
34  resourcesCreate();
35  resourcesInit();
36 
37  /* --------------- Code_cpp block 2 start --------------- */
38  /* --------------- Code_cpp block 2 end --------------- */
39 }
40 
42  /* --------------- Code_cpp block 3 start --------------- */
43  #if RES_3_13
44  WppTaskQueue::requestToRemoveTask(_currentTimeTaskId);
45  #endif
46  /* --------------- Code_cpp block 3 end --------------- */
47 }
48 
50  return ctx.registry().device();
51 }
52 
54  Instance *inst = ctx.registry().device().instance(instId);
55  if (!inst) return NULL;
56  return static_cast<Device*>(inst);
57 }
58 
60  Instance *inst = ctx.registry().device().createInstance(instId);
61  if (!inst) return NULL;
62  return static_cast<Device*>(inst);
63 }
64 
65 bool Device::removeInst(WppClient &ctx, ID_T instId) {
66  return ctx.registry().device().remove(instId);
67 }
68 
69 void Device::serverOperationNotifier(Instance *securityInst, ItemOp::TYPE type, const ResLink &resLink) {
70  /* --------------- Code_cpp block 4 start --------------- */
71  /* --------------- Code_cpp block 4 end --------------- */
72 
73  operationNotify(*this, resLink, type);
74 
75  /* --------------- Code_cpp block 5 start --------------- */
76  /* --------------- Code_cpp block 5 end --------------- */
77 }
78 
80  if (type == ItemOp::WRITE || type == ItemOp::DELETE) notifyResChanged(resLink.resId, resLink.resInstId);
81 
82  /* --------------- Code_cpp block 6 start --------------- */
83  /* --------------- Code_cpp block 6 end --------------- */
84 }
85 
86 void Device::resourcesCreate() {
87  std::vector<Resource> resources = {
88  #if RES_3_0
90  #endif
91  #if RES_3_1
93  #endif
94  #if RES_3_2
96  #endif
97  #if RES_3_3
99  #endif
101  #if RES_3_5
103  #endif
104  #if RES_3_6
105  {AVAILABLE_POWER_SOURCES_6, ItemOp(ItemOp::READ), IS_SINGLE::MULTIPLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
106  #endif
107  #if RES_3_7
108  {POWER_SOURCE_VOLTAGE_7, ItemOp(ItemOp::READ), IS_SINGLE::MULTIPLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
109  #endif
110  #if RES_3_8
111  {POWER_SOURCE_CURRENT_8, ItemOp(ItemOp::READ), IS_SINGLE::MULTIPLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
112  #endif
113  #if RES_3_9
114  {BATTERY_LEVEL_9, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
115  #endif
116  #if RES_3_10
117  {MEMORY_FREE_10, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
118  #endif
120  #if RES_3_12
121  {RESET_ERROR_CODE_12, ItemOp(ItemOp::EXECUTE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::EXECUTE },
122  #endif
123  #if RES_3_13
125  #endif
126  #if RES_3_14
128  #endif
129  #if RES_3_15
131  #endif
133  #if RES_3_17
135  #endif
136  #if RES_3_18
137  {HARDWARE_VERSION_18, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::STRING },
138  #endif
139  #if RES_3_19
140  {SOFTWARE_VERSION_19, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::STRING },
141  #endif
142  #if RES_3_20
143  {BATTERY_STATUS_20, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
144  #endif
145  #if RES_3_21
146  {MEMORY_TOTAL_21, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
147  #endif
148  #if RES_3_22
150  #endif
151  };
152  setupResources(std::move(resources));
153 }
154 
155 void Device::resourcesInit() {
156  /* --------------- Code_cpp block 7 start --------------- */
157  #if RES_3_0
159  #endif
160 
161  #if RES_3_1
163  #endif
164 
165  #if RES_3_2
167  #endif
168 
169  #if RES_3_3
171  #endif
172 
173  resource(REBOOT_4)->set<EXECUTE_T>([](Instance& inst, ID_T resId, const OPAQUE_T& data) { return true; });
174 
175  #if RES_3_5
176  resource(FACTORY_RESET_5)->set<EXECUTE_T>([](Instance& inst, ID_T resId, const OPAQUE_T& data) { return true; });
177  #endif
178 
179  #if RES_3_6
180  resource(AVAILABLE_POWER_SOURCES_6)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return DC <= value && value < PWR_SRC_MAX; });
181  #endif
182 
183  #if RES_3_9
184  resource(BATTERY_LEVEL_9)->set<INT_T>(BAT_LVL_MIN);
185  resource(BATTERY_LEVEL_9)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return BAT_LVL_MIN <= value && value <= BAT_LVL_MAX; });
186  #endif
187 
188  #if RES_3_10
189  resource(MEMORY_FREE_10)->set<INT_T>(0);
190  #endif
191 
192  resource(ERROR_CODE_11)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return NO_ERROR <= value && value < ERR_CODE_MAX; });
193 
194  #if RES_3_12
195  resource(RESET_ERROR_CODE_12)->set<EXECUTE_T>([this](Instance& inst, ID_T resId, const OPAQUE_T& buff) {
199  return true;
200  });
201  #endif
202 
203  #if RES_3_13
205  _currentTimeTaskId = WppTaskQueue::addTask(1, [this](WppClient &client, void *ctx) -> bool {
206  TIME_T currentTime = WppPlatform::getTime();
207  resource(CURRENT_TIME_13)->set<TIME_T>(currentTime);
209  return false;
210  });
211  #endif
212 
213  #if RES_3_14
214  resource(UTC_OFFSET_14)->set<STRING_T>("");
215  #endif
216 
217  #if RES_3_15
218  resource(TIMEZONE_15)->set<STRING_T>("");
219  #endif
220 
223 
224  #if RES_3_17
225  resource(DEVICE_TYPE_17)->set<STRING_T>("");
226  #endif
227 
228  #if RES_3_18
229  resource(HARDWARE_VERSION_18)->set<STRING_T>("");
230  #endif
231 
232  #if RES_3_19
233  resource(SOFTWARE_VERSION_19)->set<STRING_T>("");
234  #endif
235 
236  #if RES_3_20
237  resource(BATTERY_STATUS_20)->set<INT_T>(BAT_STATUS_MAX);
238  resource(BATTERY_STATUS_20)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return NORMAL <= value && value < BAT_STATUS_MAX; });
239  #endif
240 
241  #if RES_3_21
242  resource(MEMORY_TOTAL_21)->set<INT_T>(NO_ERROR);
243  #endif
244  /* --------------- Code_cpp block 7 end --------------- */
245 }
246 
247 /* --------------- Code_cpp block 8 start --------------- */
248 /* --------------- Code_cpp block 8 end --------------- */
249 
250 } /* namespace wpp */
#define WPP_ERR_TASK_ID
Definition: WppTaskQueue.h:22
static Device * instance(WppClient &ctx, ID_T instId=ID_T_MAX_VAL)
Gets an instance of the object.
Definition: Device.cpp:53
static bool removeInst(WppClient &ctx, ID_T instId)
Removes an instance of the object.
Definition: Device.cpp:65
void userOperationNotifier(ItemOp::TYPE type, const ResLink &resLink) override
This method must be implemented by the derived class, and handle information about resource operation...
Definition: Device.cpp:79
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...
Definition: Device.cpp:69
@ SUPPORTED_BINDING_AND_MODES_16
Definition: Device.h:75
@ CURRENT_TIME_13
Definition: Device.h:67
@ SERIAL_NUMBER_2
Definition: Device.h:38
@ MODEL_NUMBER_1
Definition: Device.h:35
@ REBOOT_4
Definition: Device.h:43
@ MANUFACTURER_0
Definition: Device.h:32
@ ERROR_CODE_11
Definition: Device.h:62
@ FIRMWARE_VERSION_3
Definition: Device.h:41
static Object & object(WppClient &ctx)
Gets the Object reference.
Definition: Device.cpp:49
@ NO_ERROR
Definition: Device.h:112
@ ERR_CODE_MAX
Definition: Device.h:121
static Device * createInst(WppClient &ctx, ID_T instId=ID_T_MAX_VAL)
Creates an instance of the object.
Definition: Device.cpp:59
Device(lwm2m_context_t &context, const OBJ_LINK_T &id)
Definition: Device.cpp:26
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
void notifyResChanged(ID_T resId, ID_T resInstId=ID_T_MAX_VAL)
Notify server about resource value change.
Definition: Instance.cpp:17
Instance(lwm2m_context_t &context, const OBJ_LINK_T &id)
Definition: Instance.h:42
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.
bool remove(ID_T instanceID)
Removes an instance of the object.
Definition: Object.cpp:82
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 clear()
Remove all instances.
Definition: Resource.cpp:155
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
static time_t getTime(void)
Returns the number of seconds elapsed since a specific origin.
Object & device()
Definition: WppRegistry.cpp:76
static task_id_t addTask(time_t delaySec, task_t task)
Add task to queue, ctx that passed to task equals to NULL.
static void requestToRemoveTask(task_id_t id)
This function does not immediately delete the task, it only marks it as one that should be deleted at...
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14
bool wppBindingValidate(const STRING_T &binding)
Definition: WppBindings.cpp:6
std::function< bool(Instance &, ID_T, const OPAQUE_T &)> EXECUTE_T
Definition: WppTypes.h:72
int64_t INT_T
Definition: WppTypes.h:41
uint16_t ID_T
Definition: WppTypes.h:15
std::function< bool(const STRING_T &)> VERIFY_STRING_T
Definition: WppTypes.h:100
std::function< bool(const INT_T &)> VERIFY_INT_T
Data validation function types.
Definition: WppTypes.h:94
std::string STRING_T
Definition: WppTypes.h:45
INT_T TIME_T
Definition: WppTypes.h:44
std::vector< uint8_t > OPAQUE_T
Opaque - represent buffer or string as lwm2m_data_t.value.asBuffer.
Definition: WppTypes.h:49
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
@ EXECUTE
Definition: ItemOp.h:33