-
Notifications
You must be signed in to change notification settings - Fork 62
/
cmd_lwm2m.c
252 lines (216 loc) · 6.48 KB
/
cmd_lwm2m.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* ------------------------------------------
* Copyright (c) 2017, Synopsys, Inc. All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1) Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
--------------------------------------------- */
/**
* \file
* \ingroup EMBARC_APP_LWIP_FREERTOS_LWM2M_CLIENT
* \brief NTShell Command:lwm2m_client implementation
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "embARC.h"
#include "embARC_debug.h"
#include "lwm2mclient.h"
#include "lwip_wifi.h"
#ifndef USE_NTSHELL_EXTOBJ /* don't use ntshell extobj */
#define CMD_DEBUG(fmt, ...) EMBARC_PRINTF(fmt, ##__VA_ARGS__)
#endif
#define BOOTLOADER_STARTADDRESS 0x17f00000
#define TSKPRI_LWM2M_CLIENT (configMAX_PRIORITIES-2) /**< lwm2m task priority */
/* lwM2M client info */
static lwm2m_client_info c_info;
static int lwm2m_client_conn_stat = 0;
static int lwm2m_client_start_flag = 0;
static TaskHandle_t task_lwm2m_client_handle = NULL;
typedef int (*fp_t)(void);
void go_bootloader(){
fp_t fp;
fp = (fp_t)(BOOTLOADER_STARTADDRESS+4);
cpu_lock();
for (int i = NUM_EXC_CPU; i < NUM_EXC_ALL; i++) {
int_disable(i);
}
// set up an enviroment for application
if ((_arc_aux_read(AUX_BCR_I_CACHE) & 0x7) >= 0x2) {
//icache_invalidate();
}
if ((_arc_aux_read(AUX_BCR_D_CACHE) & 0x7) >= 0x2) {
//dcache_flush();
}
fp();
}
void task_lwm2m_client(void *par)
{
unsigned int cpu_status;
while (1) {
lwm2m_client_conn_stat = 1;
if (lwm2mclient(&c_info) == 0) {
EMBARC_PRINTF("LwM2M client end successfully\r\n");
} else {
EMBARC_PRINTF("LwM2M client end failed\r\n");
}
cpu_status = cpu_lock_save();
if (c_info.server != NULL) free((void *)(c_info.server));
if (c_info.ep_name != NULL) free((void *)(c_info.ep_name));
if (c_info.serverPort != NULL) free((void *)(c_info.serverPort));
lwm2m_client_conn_stat = 0;
cpu_unlock_restore(cpu_status);
vTaskSuspend(NULL);
}
}
void task_lwm2m_update(void *par)
{
handle_sigint(2);
vTaskDelay(2000 / portTICK_RATE_MS);
go_bootloader();
}
#define min(x,y) ((x) < (y) ? (x) : (y))
/* show help of command */
static void cmd_lwm2m_client_help(char *cmd_name, void *extobj)
{
NTSHELL_IO_PREDEF;
VALID_EXTOBJ_NORTN(extobj);
NTSHELL_IO_GET(extobj);
if (cmd_name == NULL) {
/* cmd_name not valid */
return;
}
CMD_DEBUG("usage: %s \r\n"
" -q quit client\r\n"
" -s <server_url> set the lwM2M server URL\r\n"
" -c <client_name> set the lwM2M client endpoint name\r\n"
" -p <server port> set the LwM2M server port\r\n", cmd_name);
error_exit:
return;
}
static char *malloc_and_copy(char *chr)
{
int len = 0;
char *p_str = NULL;
len = strlen(chr) + 1;
p_str = (char *)malloc(len);
if (p_str == NULL) {
EMBARC_PRINTF("Memory malloc failed.\r\n");
} else {
memcpy(p_str, chr, len);
}
return p_str;
}
static int cmd_lwm2m_client(int argc, char **argv, void *extobj)
{
int ercd = E_OK;
int opt;
int opt_enter_cnt = 0;
int c_quit = 0;
char *p_server = NULL;
char *p_client_name = NULL;
char *p_port = NULL;
NTSHELL_IO_PREDEF;
VALID_EXTOBJ(extobj, -1);
NTSHELL_IO_GET(extobj);
opterr = 0;
optind = 1;
if (argc < 2) {
cmd_lwm2m_client_help(argv[0], extobj);
return E_OK;
}
if (!lwip_wifi_isup()) {
CMD_DEBUG("Wifi is not ready for lwM2M client.\r\n");
goto error_exit;
}
while ((opt=getopt(argc, argv, "s:c:p:qhH?")) != -1) {
opt_enter_cnt ++;
switch (opt) {
case 'h':
case '?':
case 'H':
cmd_lwm2m_client_help(argv[0], extobj);
goto error_exit;
break;
case 'q':
c_quit = 1;
break;
case 's':
if (p_server == NULL) {
p_server = malloc_and_copy(optarg);
}
break;
case 'c':
if (p_client_name == NULL) {
p_client_name = malloc_and_copy(optarg);
}
break;
case 'p':
if (p_port == NULL) {
p_port = malloc_and_copy(optarg);
}
break;
default:
opt_enter_cnt --;
CMD_DEBUG("unrecognized option:%c\r\n", opt);
break;
}
}
if (c_quit == 1) {
CMD_DEBUG("Try to exit existing client.\r\n");
handle_sigint(2);
goto error_exit;
}
if (lwm2m_client_conn_stat == 1) {
CMD_DEBUG("LwM2M client is already running.\r\n");
goto error_exit;
}
if (p_server == NULL) {
CMD_DEBUG("Server Url is not specified, please check it.\r\n");
goto error_exit;
}
c_info.server = p_server;
c_info.ep_name = p_client_name;
c_info.serverPort = p_port;
lwm2m_client_start_flag = 1;
CMD_DEBUG("Start LwM2M client.");
if (task_lwm2m_client_handle == NULL) {
if (xTaskCreate(task_lwm2m_client, "lwm2m client", 20480, (void *)1,
TSKPRI_LWM2M_CLIENT, &task_lwm2m_client_handle) != pdPASS) {
EMBARC_PRINTF("create lwm2m client failed\r\n");
}
} else {
vTaskResume(task_lwm2m_client_handle);
}
return E_OK;
error_exit:
if (p_server != NULL) free((void *)p_server);
if (p_client_name != NULL) free((void *)p_client_name);
if (p_port != NULL) free((void *)p_port);
return ercd;
}
static CMD_TABLE_T lwm2m_client_cmd = {"lwm2m_client", "connect to LwM2M server", cmd_lwm2m_client, NULL};
/**
* register lwm2m_client command
*/
CMD_TABLE_T * register_ntshell_cmd_lwm2m_client(CMD_TABLE_T *prev)
{
return ntshell_usrcmd_register(&lwm2m_client_cmd, prev);
}