monoZ docs
MZ_lwm2m_example1.c
Go to the documentation of this file.
1/*
2 * Objectives covered in this Example :
3 * 1. Demonstrate how to create a simple recursive Timer using MonoZ_Lib
4 * 2. Demonstrate how to create a Application Thread using MonoZ_Lib
5 * 3. Demonstrate how to use lwm2m API's defined in MonoZ_Lib to set
6 * Object/Resource values using mz_set_value_Ob19_X_X API,
7 * Read the new data received from server(in write event) using
8 * mz_read_value_Ob19_X_X API, Send the sensor data using
9 * mz_set_and_notify_Ob19_X_X API.
10 * 4. Demonstrate how to handle post processing event from MonoZ_Lib lwm2m
11 * client.
12 * 5. Demonstrate how to read RTC time.
13 *
14 * Sources for this example :
15 * MZ_lwm2m_example1.c - Main Application file
16 * MZ_lwm2m_example1.h
17 *
18 * [Other Linked files]
19 * MZ_modem_config.c - Modem command integration file
20 */
21
22
31#include "MZ_sys_cmsis_os2.h"
32#include "MZ_lwm2m_example1.h"
33#include "MZ_timer.h"
34#include "MZ_print.h"
35#include "MZ_error_handler.h"
36#include "MZ_Lwm2m_public.h"
37#include "MZ_type_converter.h"
38#include "MZ_rtc.h"
39#include "stdio.h"
40
41#define LW_START_AT_INIT (0)
42#define LW_START_AT_THREAD (1)
43
44/*
45 * This is the example switch MACRO.
46 * 1. define LWM2M_EXAMPLE as LW_START_AT_INIT for example to start the timer
47 * without Application thread
48 * 2. define LWM2M_EXAMPLE as LW_START_AT_THREAD for example to start the timer
49 * inside Application thread
50 */
51#define LWM2M_EXAMPLE LW_START_AT_THREAD
52
53#define LWM2M_APP_STACK_SIZE (128)
54#define LWM2M_TX_TIME (pdMS_TO_TICKS(60000))
55
56static char sensor_data[11] = "";
57static char sensor_in[5] = "0000";
59static int copy_flag = 0;
60
67/*
68 * This Timer callback will be called after the timer is expired.
69 * The timer value is set as LWM2M_TX_TIME
70 */
71static void lwm2m_timer_cb(TimerHandle_t xTimer)
72{
73 /*
74 * If new Data has received from server - The copy has been done if
75 * Lwm2m write is received. Check lwm2m_event_process() API define in this
76 * file for more details.
77 */
78 if(copy_flag)
79 {
80 /* Process the data - Copy the data to Dummy buffer */
81 sensor_data[0] = '\0';
82 sensor_data[9] = '\0';
84 copy_flag = 0;
85 }
86
87 /* Increment the Dummy buffer */
88 sensor_in[3]++;
89
90 /* Convert the raw data to ascii string for sending to server */
92
93 sensor_data[0] = '\"';
94 sensor_data[9] = '\"';
95 sensor_data[10] = '\0';
96
97 /* Read the RTC time */
98 mzd_time _tm;
99 mz_error_t tm_rd_status = MZ_RTC_read(&_tm);
100
101 /* Print the RTC time on CLI */
102 if(MZ_OK == tm_rd_status)
103 {
104 char _tm_string[40] = {0};
105 sprintf(_tm_string,
106 "Time Now : %02d/%02d/%02d::%02d:%02d:%02d",
107 _tm.YY,
108 _tm.MM,
109 _tm.DD,
110 _tm.hh,
111 _tm.mm,
112 _tm.ss);
113 mz_puts(_tm_string);
114 mz_puts("\r\n");
115 }
116 else
117 {
118 mz_puts((char *)mz_error_to_str(tm_rd_status));
119 }
120
121 /* Send the Dummy data to Lwm2m server using Notify */
123 if(MZ_OK == status)
124 {
125 mz_puts("Notify send to SDK\r\n");
126 }
127 else
128 {
129 mz_puts((char *)mz_error_to_str(status));
130 }
131}
132
133/*
134 * When want the example to start the timer inside Application thread
135 * define LWM2M_EXAMPLE as LW_START_AT_THREAD
136 */
137#if(LWM2M_EXAMPLE == LW_START_AT_THREAD)
138
139static mz_thread_t lwm2m_thread_id = NULL; /* Thread id handler */
140static StaticTask_t lwm2m_cb_mem; /* Thread control block */
141static StackType_t lwm2m_stack[LWM2M_APP_STACK_SIZE]; /* Thread stack */
142
150static void lwm2m_app(void * arg)
151{
152 (void)arg;
153 /*
154 * create a recursive timer for sending sensor data.
155 * In-case of other type of timer, please refer MZ_timer.h
156 * The expire time for this timer set to LWM2M_TX_TIME.
157 * "lwm2m_timer_cb" is passed as an argument.
158 * When the timer expires, the "lwm2m_timer_cb" API will be
159 * processed.
160 */
161 if(MZ_OK != mz_tm_create_start_recursive("Lwm2m timer",
164 {
165 __NOP(); //No action
166 }
167
168 mz_puts("Lwm2m Timer Created and started at Application \r\n");
169
170 /*
171 * This is the infinite loop for this thread - the thread will execute this
172 * loop forever and not come outside of this loop
173 */
174 while(1)
175 {
176 /* Need to write the periodic executing logic in this loop block */
177
178
179 /* Put a delay to avoid blocking on this thread */
180 HAL_Delay(10);
181
182 }//End of while(1) - Do not place any code after this.
183
184}
185#endif //(LWM2M_EXAMPLE == LW_START_AT_THREAD)
186
187/*
188 * @fn mz_error_t lwm2m_app_init(void)
189 * @brief Initiate LWM2M App
190 * @return MZ_OK/MZ_FAIL
191 */
193{
194 mz_error_t _ret = MZ_OK;
195
196/*
197 * When want the example to start the timer without Application thread
198 * define LWM2M_EXAMPLE as LW_START_AT_INIT
199 */
200#if(LWM2M_EXAMPLE == LW_START_AT_INIT)
201 /*
202 * create a recursive timer for sending sensor data.
203 * In-case of other type of timer, please refer MZ_timer.h
204 * The expire time for this timer set to LWM2M_TX_TIME.
205 * "lwm2m_timer_cb" is passed as an argument.
206 * When the timer expires, the "lwm2m_timer_cb" API will be
207 * processed.
208 */
209 if(MZ_OK != mz_tm_create_start_recursive("My timer",
212 {
213 __NOP();
214 }
215
216 mz_puts("Timer Created and started at Initialization \r\n");
217
218
219/*
220 * When want the example to start the timer inside Application thread
221 * define LWM2M_EXAMPLE as LW_START_AT_THREAD
222 */
223#else
224 /* Create the application thread */
226 "Lwm2m Scheduler",
227 lwm2m_app,
228 NULL,
229 osPriorityNormal,
233 sizeof(lwm2m_cb_mem)))
234 {
236 }
237#endif // (LWM2M_EXAMPLE == LW_START_AT_INIT)
238
239 /*
240 * Set MonoZ_Lib lwm2m internal resources to default value before
241 * application start.
242 */
243 _ret = mz_set_value_Ob19_0_0("\"30303030\"");
244 if(MZ_OK != _ret) goto clean;
245 _ret = mz_set_value_Ob19_1_0("\"30303030\"");
246 if(MZ_OK != _ret) goto clean;
247
248 clean :
249 return _ret;
250}
251
252/*
253 * @fn void lwm2m_event_process(void * event)
254 * @brief Process LWM2M Event
255 * @param event void
256 */
257/*
258 * LWM2M user defined callback function. - START
259 *
260 * This Function will be called from MZ_callback.c mz_pro_default_callback().
261 * User need to check the events interested based on specific application
262 * requirement. If no application requirement exist for specific event, then
263 * no action need to be taken by this function
264 *
265 * NOTE :
266 * MonoZ_Lib will call the mz_pro_default_callback() API after processing all
267 * LWM2M server events. So the events received at this API are just to inform
268 * user application to perform any post processing tasks. MonoZ_Lib maintain
269 * server connection and Obj19 related data. The detailed event list refer
270 * MZ_Lwm2m_public.h
271 */
272void lwm2m_event_process(void * event)
273{
274 st_lw_event * e = event; //*((st_lw_event *)event);
275
276 /* Check post-processing events is send by MonoZ_Lib to user */
277 switch(e->lw_event)
278 {
279 /*
280 * This is a write event. The event has already been processed and the
281 * data from server is stored inside MonoZ_Lib. If any user
282 * action need to be taken then it can be performed here.
283 */
284 case LW_EV_WRITE_DATA:
285 {
286 /* print the write received on CLI */
287 mz_puts("Write received - Copy here\r\n");
288
289 /*
290 * Application need to read and copy the data to local buffer for
291 * processing
292 */
293 mz_error_t ret = MZ_FAIL;
294 char * data = mz_read_value_Ob19_0_0(&ret);
295 if(MZ_OK == ret)
296 {
297 strcpy(sensor_data,data);
298 copy_flag = 1;
299 mz_puts("Copy success\r\n");
300 }
301 }
302 break;
303 case LW_EV_EXECUTE:
304 /* Do something on Execute received */
305 ;
306 break;
307 /*
308 * This is a read event. The event has already been processed and the
309 * data stored inside MonoZ_Lib has already send to server. If any user
310 * action need to be taken then it can be performed here.
311 */
312 case LW_EV_READ:
313 /* print the read received on CLI */
314 mz_puts("Read received\r\n");
315 break;
316 /*
317 * This is a Observe start event. The event has already been processed
318 * and the token is already stored inside MonoZ_Lib. If any user action
319 * need to be taken then it can be performed here.
320 */
321 case LW_EV_OBSERVE:
322 /* print the observe received on CLI */
323 mz_puts("Observe received\r\n");
324 break;
325 /*
326 * This is a Observe cancel event. The event has already been processed
327 * and the token is already clear inside MonoZ_Lib. If any user action
328 * need to be taken then it can be performed here.
329 */
331 /* print the observe cancel received on CLI */
332 mz_puts("Observe cancel received\r\n");
333 break;
335 /* print when notify send to server */
336 mz_puts("Notify send to server\r\n");
337 break;
339 /* print when notify acknowledgment is received from server */
340 mz_puts("Notify send acknowledge\r\n");
341 break;
343 /* print when notify failed received from server */
344 mz_puts("Notify send failed\r\n");
345 break;
346 default:
347 break;
348 }
349}
monoZ LWM2M public Protocol
@ LW_EV_READ
@ LW_EV_WRITE_DATA
@ LW_EV_CLIENT_NOTIFY_SEND
@ LW_EV_OBSERVE_CANCEL
@ LW_EV_EXECUTE
@ LW_EV_CLIENT_NOTIFY_SEND_ACK
@ LW_EV_NOTIFY_FAIL
@ LW_EV_OBSERVE
monoZ Error Handler
mz_error_t
Enumeration of monoZ Error Flags.
@ MZ_THREAD_CREATE_FAIL
@ MZ_OK
@ MZ_FAIL
static int copy_flag
static void lwm2m_app(void *arg)
Create recursive timer create the timer and starts when main RTOS scheduler started and it will start...
static void lwm2m_timer_cb(TimerHandle_t xTimer)
timer callback function for lwm2m app
static StaticTask_t lwm2m_cb_mem
#define LWM2M_TX_TIME
Defines the Timer size in milliseconds.
static char sensor_in[5]
#define LWM2M_APP_STACK_SIZE
Defines the App stack size.
static StackType_t lwm2m_stack[LWM2M_APP_STACK_SIZE]
void lwm2m_event_process(void *event)
LWM2M user defined callback function.
static mz_thread_t lwm2m_thread_id
static char sensor_data[11]
mz_error_t lwm2m_app_init(void)
Initiate LWM2M App.
Monoz LWM2M example1.
monoZ Print
This file is responsible for real time clock operations.
monoZ system CMSIS OS2 This is an abstraction layer and includes FreeRTOS configuration,...
osThreadId_t mz_thread_t
mz_thread_t variable of type osThreadId_t
This file contains APIs for create,delete,start and stop of timers. Maximum 5 timers can be created....
monoZ Type Converter This file contains the APIs used to hex to ascii and ascii to hex conversion
const char * mz_error_to_str(mz_error_t e)
This function is used to convert the monoZ error code to corresponding error message.
mz_error_t mz_set_value_Ob19_0_0(char *src)
This function writes 19/0/0 value to internal resource structure.
mz_error_t mz_set_value_Ob19_1_0(char *src)
This function writes 19/1/0 value to internal resource structure.
mz_error_t mz_set_and_notify_Ob19_0_0(char *src)
This function writes 19/0/0 value to internal resource structure and then notify to server.
void * mz_read_value_Ob19_0_0(mz_error_t *ret)
This function Reads internal resource structure value of 19/0/0.
int mz_puts(void *__ch)
This function prints the given string.
mz_error_t MZ_RTC_read(mzd_time *_dt)
This function Reads the Real Time Clock value.
uint8_t mz_thread_create(mz_thread_t *t, const char *name, mz_fn thread_func, void *const arg, mz_thread_prio_t prio, StackType_t *stack_mem, size_t stack_size, StaticTask_t *cb_mem, uint32_t cb_size)
This function is used to check the message queue object validity Create a thread and add it to Active...
mz_error_t mz_tm_create_start_recursive(char *_name, TickType_t _tick, mz_tm_cb _cb)
Function to Create and Start One Shot Timers.
void mz_hex_to_ascii(void *in, void *out)
This function is used to convert the given Hex value to ASCII value.
void mz_ascii_to_hex(void *in, void *out)
This function is used to convert the given ASCII value to Hex value.
Structure for Defining the Date and time for monoZ.
Definition: MZ_rtc.h:21
char MM
Definition: MZ_rtc.h:23
char ss
Definition: MZ_rtc.h:27
char YY
Definition: MZ_rtc.h:22
char mm
Definition: MZ_rtc.h:26
char DD
Definition: MZ_rtc.h:24
char hh
Definition: MZ_rtc.h:25
Structure For LW Events.