Skip to content

Commit 869d366

Browse files
author
Wayne Ren
committed
example: update the lwm2m examples
* some code cleanup * reduce the task size of lwm2m client/server * allocate more memory for freertos Signed-off-by: Wayne Ren <[email protected]>
1 parent ad2085a commit 869d366

File tree

7 files changed

+69
-94
lines changed

7 files changed

+69
-94
lines changed

example/freertos/iot/lwm2m/lwm2m_client/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
8787
#define configMAX_PRIORITIES ( 10 )
8888
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 104 )
89-
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 10 * 1024 ) )
89+
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 16 * 1024 ) )
9090
#define configMAX_TASK_NAME_LEN ( 10 )
9191
#define configUSE_TRACE_FACILITY 0
9292
#define configUSE_16_BIT_TICKS 0

example/freertos/iot/lwm2m/lwm2m_client/cmd_lwm2m.c

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@
2828
*
2929
--------------------------------------------- */
3030

31-
/**
32-
* \file
33-
* \ingroup EMBARC_APP_LWIP_FREERTOS_LWM2M_CLIENT
34-
* \brief NTShell Command:lwm2m_client implementation
35-
*/
36-
3731
#include <stdlib.h>
3832
#include <string.h>
3933
#include <unistd.h>
@@ -45,7 +39,7 @@
4539
#include "lwip_wifi.h"
4640

4741
#ifndef USE_NTSHELL_EXTOBJ /* don't use ntshell extobj */
48-
#define CMD_DEBUG(fmt, ...) EMBARC_PRINTF(fmt, ##__VA_ARGS__)
42+
#define CMD_DEBUG(fmt, ...) EMBARC_PRINTF(fmt, ##__VA_ARGS__)
4943
#endif
5044

5145
#define TSKPRI_LWM2M_CLIENT (configMAX_PRIORITIES-2) /**< lwm2m task priority */
@@ -60,21 +54,16 @@ static TaskHandle_t task_lwm2m_client_handle = NULL;
6054
#define BOOTLOADER_STARTADDRESS 0x17f00000
6155
typedef int (*fp_t)(void);
6256

63-
void go_bootloader(){
57+
void go_bootloader(void)
58+
{
6459
fp_t fp;
65-
fp = (fp_t)(BOOTLOADER_STARTADDRESS+4);
60+
fp = (fp_t)(*((uint32_t *)BOOTLOADER_STARTADDRESS));
6661
cpu_lock();
6762

6863
for (int i = NUM_EXC_CPU; i < NUM_EXC_ALL; i++) {
6964
int_disable(i);
7065
}
71-
// set up an enviroment for application
72-
if ((_arc_aux_read(AUX_BCR_I_CACHE) & 0x7) >= 0x2) {
73-
//icache_invalidate();
74-
}
75-
if ((_arc_aux_read(AUX_BCR_D_CACHE) & 0x7) >= 0x2) {
76-
//dcache_flush();
77-
}
66+
7867
fp();
7968

8069
}
@@ -91,17 +80,30 @@ void task_lwm2m_update(void *par)
9180
void task_lwm2m_client(void *par)
9281
{
9382
unsigned int cpu_status;
83+
9484
while (1) {
9585
lwm2m_client_conn_stat = 1;
86+
9687
if (lwm2mclient(&c_info) == 0) {
9788
EMBARC_PRINTF("LwM2M client end successfully\r\n");
9889
} else {
9990
EMBARC_PRINTF("LwM2M client end failed\r\n");
10091
}
92+
10193
cpu_status = cpu_lock_save();
102-
if (c_info.server != NULL) free((void *)(c_info.server));
103-
if (c_info.ep_name != NULL) free((void *)(c_info.ep_name));
104-
if (c_info.serverPort != NULL) free((void *)(c_info.serverPort));
94+
95+
if (c_info.server != NULL) {
96+
free((void *)(c_info.server));
97+
}
98+
99+
if (c_info.ep_name != NULL) {
100+
free((void *)(c_info.ep_name));
101+
}
102+
103+
if (c_info.serverPort != NULL) {
104+
free((void *)(c_info.serverPort));
105+
}
106+
105107
lwm2m_client_conn_stat = 0;
106108
cpu_unlock_restore(cpu_status);
107109
vTaskSuspend(NULL);
@@ -122,11 +124,12 @@ static void cmd_lwm2m_client_help(char *cmd_name, void *extobj)
122124
/* cmd_name not valid */
123125
return;
124126
}
127+
125128
CMD_DEBUG("usage: %s \r\n"
126-
" -q quit client\r\n"
127-
" -s <server_url> set the lwM2M server URL\r\n"
128-
" -c <client_name> set the lwM2M client endpoint name\r\n"
129-
" -p <server port> set the LwM2M server port\r\n", cmd_name);
129+
" -q quit client\r\n"
130+
" -s <server_url> set the lwM2M server URL\r\n"
131+
" -c <client_name> set the lwM2M client endpoint name\r\n"
132+
" -p <server port> set the LwM2M server port\r\n", cmd_name);
130133

131134
error_exit:
132135
return;
@@ -139,11 +142,13 @@ static char *malloc_and_copy(char *chr)
139142

140143
len = strlen(chr) + 1;
141144
p_str = (char *)malloc(len);
145+
142146
if (p_str == NULL) {
143147
EMBARC_PRINTF("Memory malloc failed.\r\n");
144148
} else {
145149
memcpy(p_str, chr, len);
146150
}
151+
147152
return p_str;
148153
}
149154

@@ -174,44 +179,56 @@ static int cmd_lwm2m_client(int argc, char **argv, void *extobj)
174179
CMD_DEBUG("Wifi is not ready for lwM2M client.\r\n");
175180
goto error_exit;
176181
}
182+
177183
while ((opt=getopt(argc, argv, "s:c:p:qhH?")) != -1) {
178184
opt_enter_cnt ++;
185+
179186
switch (opt) {
180187
case 'h':
181188
case '?':
182189
case 'H':
183190
cmd_lwm2m_client_help(argv[0], extobj);
184191
goto error_exit;
185192
break;
193+
186194
case 'q':
187195
c_quit = 1;
188196
break;
197+
189198
case 's':
190199
if (p_server == NULL) {
191200
p_server = malloc_and_copy(optarg);
192201
}
202+
193203
break;
204+
194205
case 'c':
195206
if (p_client_name == NULL) {
196207
p_client_name = malloc_and_copy(optarg);
197208
}
209+
198210
break;
211+
199212
case 'p':
200213
if (p_port == NULL) {
201214
p_port = malloc_and_copy(optarg);
202215
}
216+
203217
break;
218+
204219
default:
205220
opt_enter_cnt --;
206221
CMD_DEBUG("unrecognized option:%c\r\n", opt);
207222
break;
208223
}
209224
}
225+
210226
if (c_quit == 1) {
211227
CMD_DEBUG("Try to exit existing client.\r\n");
212228
handle_sigint(2);
213229
goto error_exit;
214230
}
231+
215232
if (lwm2m_client_conn_stat == 1) {
216233
CMD_DEBUG("LwM2M client is already running.\r\n");
217234
goto error_exit;
@@ -221,6 +238,7 @@ static int cmd_lwm2m_client(int argc, char **argv, void *extobj)
221238
CMD_DEBUG("Server Url is not specified, please check it.\r\n");
222239
goto error_exit;
223240
}
241+
224242
c_info.server = p_server;
225243
c_info.ep_name = p_client_name;
226244
c_info.serverPort = p_port;
@@ -229,8 +247,8 @@ static int cmd_lwm2m_client(int argc, char **argv, void *extobj)
229247
CMD_DEBUG("Start LwM2M client.");
230248

231249
if (task_lwm2m_client_handle == NULL) {
232-
if (xTaskCreate(task_lwm2m_client, "lwm2m client", 20480, (void *)1,
233-
TSKPRI_LWM2M_CLIENT, &task_lwm2m_client_handle) != pdPASS) {
250+
if (xTaskCreate(task_lwm2m_client, "lwm2m client", 4096, (void *)1,
251+
TSKPRI_LWM2M_CLIENT, &task_lwm2m_client_handle) != pdPASS) {
234252
EMBARC_PRINTF("create lwm2m client failed\r\n");
235253
}
236254
} else {
@@ -240,17 +258,27 @@ static int cmd_lwm2m_client(int argc, char **argv, void *extobj)
240258
return E_OK;
241259

242260
error_exit:
243-
if (p_server != NULL) free((void *)p_server);
244-
if (p_client_name != NULL) free((void *)p_client_name);
245-
if (p_port != NULL) free((void *)p_port);
261+
262+
if (p_server != NULL) {
263+
free((void *)p_server);
264+
}
265+
266+
if (p_client_name != NULL) {
267+
free((void *)p_client_name);
268+
}
269+
270+
if (p_port != NULL) {
271+
free((void *)p_port);
272+
}
273+
246274
return ercd;
247275
}
248276

249277
static CMD_TABLE_T lwm2m_client_cmd = {"lwm2m_client", "connect to LwM2M server", cmd_lwm2m_client, NULL};
250278
/**
251279
* register lwm2m_client command
252280
*/
253-
CMD_TABLE_T * register_ntshell_cmd_lwm2m_client(CMD_TABLE_T *prev)
281+
CMD_TABLE_T *register_ntshell_cmd_lwm2m_client(CMD_TABLE_T *prev)
254282
{
255283
return ntshell_usrcmd_register(&lwm2m_client_cmd, prev);
256284
}

example/freertos/iot/lwm2m/lwm2m_client/main.c

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -28,69 +28,10 @@
2828
*
2929
--------------------------------------------- */
3030

31-
/**
32-
* \defgroup EMBARC_APP_FREERTOS_IOT_LWM2M_LWM2M_CLIENT embARC LwIP LwM2M Client Example
33-
* \ingroup EMBARC_APPS_TOTAL
34-
* \ingroup EMBARC_APPS_OS_FREERTOS
35-
* \ingroup EMBARC_APPS_MID_LWIP
36-
* \ingroup EMBARC_APPS_MID_LWM2M
37-
* \ingroup EMBARC_APPS_MID_NTSHELL
38-
* \ingroup EMBARC_APPS_MID_FATFS
39-
* \brief embARC Example for LwM2M Client on LwIP and FreeRTOS
40-
*
41-
* \details
42-
* ### Extra Required Tools
43-
*
44-
* ### Extra Required Peripherals
45-
* * Digilent PMOD WIFI(MRF24WG0MA)
46-
*
47-
* ### Design Concept
48-
* This example is designed to show how to use LwM2M client in FreeRTOS.
49-
*
50-
* ### Usage Manual
51-
* The Pmod modules should be connected to \ref EMBARC_BOARD_CONNECTION "EMSK".
52-
* This is an example running OMA LightweightM2M(LwM2M) client, provides core device management features(device, connectivity, etc). It provides a ntshell command called *lwm2m_client* to show this demo.
53-
* - About LwM2M provided in embARC
54-
* + LwM2M features supported by [eclipse wakaama project](https://projects.eclipse.org/projects/technology.wakaama)
55-
* + We only support FreeRTOS+LwIP
56-
* + LwM2M use UDP connections to exchange data
57-
* - First, you need to provide a LwM2M Server, here we take [Eclipse Leshan server](https://github.com/eclipse/leshan) for example.
58-
* + Eclipse provide a sandbox leshan server:http://leshan.eclipse.org/
59-
* + You can download a standalone leshan server binary from leshan website, and java environment(version >= 1.7.0_75) is required to run this binary
60-
* - When leshan server is running on your local PC(connected to the same wifi network which your board will connect), then download program to the board, and run *lwm2m_client* ntshell command.
61-
* + If you local PC IP is 192.168.43.4, then you need to run command *lwm2m_client -s 192.168.43.4* to connect to local leshan server.
62-
* + If connected to local leshan server, you will see the connected LwM2M client in http://127.0.0.1:8080/#/clients.
63-
* + Click on the client, then you will see many objects which the connected client provided.
64-
* + On this website, you can do many operations on this objects.
65-
* + Here you can read onboard switch and button value, and control onboard led using Object 1024.
66-
* * First read the Instance 0, then 4 resources will show. Resource 0 is button value, Resource 1 is switch value, Resource 2 is led control, Resource 3 is a float variable.
67-
* * Read Instance 0/1 will return the value of this resources in DEC format, you can press buttons or pull on/up the switches to see value changes.
68-
* * Write on Resource 2 in DEC format such as 34 will change the onboard led with value 34.
69-
* * Write on Resource 3 with a float value such as 1.45, then the float value is changed, you can read back to check it.
70-
*
71-
* ![ScreenShot of lwm2m client under freertos](pic/images/example/emsk/emsk_lwip_freertos_lwm2m_client.jpg)
72-
*
73-
* ### Extra Comments
74-
* - Sometimes the server may not respond to client, this may caused by the wifi connection is bad.
75-
* - In the case above, you need to disconnect all the power of your board, reconnect PMOD WIFI module and wait for a while, then restart this application.
76-
*
77-
*/
78-
79-
/**
80-
* \file
81-
* \ingroup EMBARC_APP_FREERTOS_IOT_LWM2M_LWM2M_CLIENT
82-
* \brief main source of LwM2M client example
83-
*/
84-
85-
/**
86-
* \addtogroup EMBARC_APP_FREERTOS_IOT_LWM2M_LWM2M_CLIENT
87-
* @{
88-
*/
89-
9031
#include "embARC.h"
9132
#include "embARC_debug.h"
9233

93-
extern CMD_TABLE_T * register_ntshell_cmd_lwm2m_client(CMD_TABLE_T *prev);
34+
extern CMD_TABLE_T *register_ntshell_cmd_lwm2m_client(CMD_TABLE_T *prev);
9435

9536
static volatile int main_run_flag = 0;
9637

@@ -100,11 +41,12 @@ int main(void)
10041
nt_io = get_ntshell_io(BOARD_ONBOARD_NTSHELL_ID);
10142

10243
EMBARC_PRINTF("Start FreeRTOS LwM2M Client Demo by run lwm2m_client command\r\n");
44+
10345
if (main_run_flag == 0) {
10446
main_run_flag = 1;
10547
register_ntshell_cmd_lwm2m_client(nt_io->cmd_tbl_head);
10648
}
10749

10850
return 0;
10951
}
110-
/** @} */
52+

example/freertos/iot/lwm2m/lwm2m_client/makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Application name
22
APPL ?= freertos_iot_lwm2m_client
33

4+
BOARD ?= emsk
5+
46
EXT_DEV_LIST += wifi/mrf24g
57

68
##
@@ -10,8 +12,9 @@ EXT_DEV_LIST += wifi/mrf24g
1012
HEAPSZ ?= 8192
1113
STACKSZ ?= 2048
1214

13-
15+
ifeq ($(TOOLCHAIN), gnu)
1416
OLEVEL = Os
17+
endif
1518

1619
#
1720
# root dir of embARC

example/freertos/iot/lwm2m/lwm2m_server/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
8787
#define configMAX_PRIORITIES ( 10 )
8888
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 104 )
89-
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 12 * 1024 ) )
89+
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 16 * 1024 ) )
9090
#define configMAX_TASK_NAME_LEN ( 10 )
9191
#define configUSE_TRACE_FACILITY 0
9292
#define configUSE_16_BIT_TICKS 0

example/freertos/iot/lwm2m/lwm2m_server/cmd_lwm2m.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static int cmd_lwm2m_server(int argc, char **argv, void *extobj)
163163
CMD_DEBUG("Start LwM2M server.");
164164

165165
if (task_lwm2m_server_handle == NULL) {
166-
if (xTaskCreate(task_lwm2m_server, "lwm2m server", 20480, \
166+
if (xTaskCreate(task_lwm2m_server, "lwm2m server", 8192, \
167167
(void *)1, TSKPRI_LWM2M_SERVER, &task_lwm2m_server_handle) != pdPASS) {
168168
EMBARC_PRINTF("create lwm2m server failed\r\n");
169169
}

example/freertos/iot/lwm2m/lwm2m_server/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ EXT_DEV_LIST += wifi/mrf24g
1010
HEAPSZ ?= 8192
1111
STACKSZ ?= 2048
1212

13+
ifeq ($(TOOLCHAIN), gnu)
1314
OLEVEL = Os
15+
endif
1416

1517
#
1618
# root dir of embARC

0 commit comments

Comments
 (0)