-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlora_app.c
More file actions
552 lines (486 loc) · 14.1 KB
/
lora_app.c
File metadata and controls
552 lines (486 loc) · 14.1 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <assert.h>
#include "lwip/pbuf.h" // For Lightweight IP Stack pbuf
#include "node/pbuf_queue.h" // For Packet Buffer Queue
#include "node/lora.h"
#include "node/lora_priv.h"
/* XXX: for now, auto-join not supported */
#if LORA_APP_AUTO_JOIN
#error "Auto-joining not supported"
#endif
/* Valid app port range */
#define LORA_APP_PORT_MAX_VAL (223)
/* Port structure */
struct lora_app_port
{
uint8_t port_num;
uint8_t opened;
uint8_t retries;
uint8_t pad8;
lora_rxd_func rxd_cb;
lora_txd_func txd_cb;
};
/* Port memory */
static struct lora_app_port lora_app_ports[LORA_APP_NUM_PORTS];
/* Lora APP Receive queue and event */
struct pbuf_queue lora_node_app_rx_q;
struct pbuf_queue lora_node_app_txd_q;
/* Lora app event queue pointer */
static struct ble_npl_eventq *lora_node_app_evq;
/* Join and link check callbacks and events */
lora_join_cb lora_join_cb_func;
lora_link_chk_cb lora_link_chk_cb_func;
struct ble_npl_event lora_app_join_ev;
struct ble_npl_event lora_app_link_chk_ev;
struct lora_app_join_ev_obj
{
uint8_t attempts;
LoRaMacEventInfoStatus_t status;
};
static struct lora_app_join_ev_obj lora_app_join_ev_data;
struct lora_app_link_chk_ev_obj
{
uint8_t num_gw;
uint8_t demod_margin;
LoRaMacEventInfoStatus_t status;
};
static struct lora_app_link_chk_ev_obj lora_app_link_chk_ev_data;
/* Internal protos */
static int lora_app_port_receive(struct pbuf *om);
static int lora_app_port_txd(struct pbuf *om);
/* Get the lora app event queue. */
static struct ble_npl_eventq *
lora_node_app_evq_get(void)
{
return lora_node_app_evq;
}
/**
* Find an open app port given its port number.
*
* @param port Port number
*
* @return struct lora_app_port*
* NULL if no open port found
* Pointer to port if opened port found.
*/
static struct lora_app_port *
lora_app_port_find_open(uint8_t port)
{
int i;
struct lora_app_port *lap;
lap = NULL;
for (i = 0; i < LORA_APP_NUM_PORTS; ++i) {
if ((lora_app_ports[i].opened != 0) &&
(lora_app_ports[i].port_num == port)) {
lap = &lora_app_ports[i];
break;
}
}
return lap;
}
/**
* Process received packet event. This event is processed by the task
* that handles the lora app.
*
* @param ev Pointer to event.
*/
static void
lora_node_proc_app_rxd_event(struct ble_npl_event *ev)
{
struct pbuf *om;
/* Go through packet queue and call rx callback for all */
while ((om = pbuf_queue_get(&lora_node_app_rx_q)) != NULL) {
lora_app_port_receive(om);
}
}
/**
* Process transmit done event. This event is processed by the task
* that handles the lora app.
*
* @param ev Pointer to event.
*/
static void
lora_node_proc_app_txd_event(struct ble_npl_event *ev)
{
struct pbuf *om;
/* Go through packet queue and call rx callback for all */
while ((om = pbuf_queue_get(&lora_node_app_txd_q)) != NULL) {
lora_app_port_txd(om);
}
}
/**
* Open a lora application port. This function will allocate a lora port, set
* port default values for datarate and retries, set the transmit done and
* received data callbacks, and add port to list of open ports.
*
* @param port Port number
* @param txd_cb Transmit done callback
* @param rxd_cb Receive data callback
*
* @return int A return code from set of lora return codes
*/
int
lora_app_port_open(uint8_t port, lora_txd_func txd_cb, lora_rxd_func rxd_cb)
{
int rc;
int i;
int avail;
/* Check valid range */
if ((port == 0) || (port > LORA_APP_PORT_MAX_VAL)) {
return LORA_APP_STATUS_INVALID_PORT;
}
/* Check other parameters */
if ((txd_cb == NULL) || (rxd_cb == NULL)) {
return LORA_APP_STATUS_INVALID_PARAM;
}
/* Make sure port is not opened */
avail = -1;
for (i = 0; i < LORA_APP_NUM_PORTS; ++i) {
/* If port not opened, remember first available */
if (lora_app_ports[i].opened == 0) {
if (avail < 0) {
avail = i;
}
} else {
/* Make sure port is not already opened */
if (lora_app_ports[i].port_num == port) {
return LORA_APP_STATUS_ALREADY_OPEN;
}
}
}
/* Open port if available */
if (avail >= 0) {
lora_app_ports[avail].port_num = port;
lora_app_ports[avail].rxd_cb = rxd_cb;
lora_app_ports[avail].txd_cb = txd_cb;
lora_app_ports[avail].retries = 8;
lora_app_ports[avail].opened = 1;
rc = LORA_APP_STATUS_OK;
} else {
rc = LORA_APP_STATUS_ENOMEM;
}
return rc;
}
/**
* Close an open lora port
*
* @param port Port number
*
* @return int A return code from set of lora return codes
*/
int
lora_app_port_close(uint8_t port)
{
int rc;
struct lora_app_port *lap;
rc = LORA_APP_STATUS_NO_PORT;
lap = lora_app_port_find_open(port);
if (lap) {
lap->opened = 0;
rc = LORA_APP_STATUS_OK;
}
return rc;
}
/**
* Configure an application port. This configures the number of retries for
* confirmed packets.
*
* NOTE: The port must be opened or this will return an error
*
* @param port Port number
* @param retries NUmmber of retries for confirmed packets
*
* @return int A return code from set of lora return codes
*/
int
lora_app_port_cfg(uint8_t port, uint8_t retries)
{
int rc;
struct lora_app_port *lap;
/* Verify datarate and retries */
if (retries > MAX_ACK_RETRIES) {
return LORA_APP_STATUS_INVALID_PARAM;
}
rc = LORA_APP_STATUS_NO_PORT;
lap = lora_app_port_find_open(port);
if (lap) {
lap->retries = retries;
rc = LORA_APP_STATUS_OK;
}
return rc;
}
/**
* Send a packet on a port. If this routine returns an error the "transmitted"
* callback will NOT be called; it is the callers responsibility to handle the
* packet appropriately (including freeing any memory if needed).
*
* @param port Port number
* @param pkt_type Type of packet
* @param om Pointer to packet
*
* @return int A return code from set of lora return codes
*/
int
lora_app_port_send(uint8_t port, Mcps_t pkt_type, struct pbuf *om)
{
int rc;
struct lora_app_port *lap;
struct lora_pkt_info *lpkt;
/* If no buffer to send, fine. */
if ((om == NULL) || (om->len == 0)) {
return LORA_APP_STATUS_INVALID_PARAM;
}
/* Check valid packet type. Only confirmed and unconfirmed for now. */
if ((pkt_type != MCPS_UNCONFIRMED) && (pkt_type != MCPS_CONFIRMED)) {
return LORA_APP_STATUS_INVALID_PARAM;
}
lap = lora_app_port_find_open(port);
if (lap) {
/* Set packet information required by MAC */
lpkt = (struct lora_pkt_info *) get_pbuf_header(om, sizeof(struct lora_pkt_info));
lpkt->port = port;
lpkt->pkt_type = pkt_type;
lpkt->txdinfo.retries = lap->retries;
lora_node_mcps_request(om);
rc = LORA_APP_STATUS_OK;
} else {
rc = LORA_APP_STATUS_NO_PORT;
}
return rc;
}
/**
* What's the maximum payload which can be sent on next frame
*
* @return int payload length, negative on error.
*/
int
lora_app_mtu(void)
{
return lora_node_mtu();
}
/**
* Called by lora task when a packet has been received for an application port.
*
* NOTE: This API is not to be called by an application; it is called
* by the task handling lora events.
*
* @param om Pointer to received packet
*
* @return int A return code from set of lora return codes
*/
static int
lora_app_port_receive(struct pbuf *om)
{
int rc;
struct lora_app_port *lap;
struct lora_pkt_info *lpkt;
lpkt = (struct lora_pkt_info *) get_pbuf_header(om, sizeof(struct lora_pkt_info));
lap = lora_app_port_find_open(lpkt->port);
if (lap) {
lap->rxd_cb(lpkt->port, lpkt->status, lpkt->pkt_type, om);
rc = LORA_APP_STATUS_OK;
} else {
pbuf_free(om);
rc = LORA_APP_STATUS_NO_PORT;
}
return rc;
}
/**
* Called by lora task when a packet has been transmitted.
*
* NOTE: This API is not to be called by an application; it is called
* by the task handling lora events.
*
* @param om Pointer to transmited packet
*
* @return int A return code from set of lora return codes
*/
static int
lora_app_port_txd(struct pbuf *om)
{
int rc;
struct lora_app_port *lap;
struct lora_pkt_info *lpkt;
lpkt = (struct lora_pkt_info *) get_pbuf_header(om, sizeof(struct lora_pkt_info));
lap = lora_app_port_find_open(lpkt->port);
if (lap) {
lap->txd_cb(lpkt->port, lpkt->status, lpkt->pkt_type, om);
rc = LORA_APP_STATUS_OK;
} else {
pbuf_free(om);
rc = LORA_APP_STATUS_NO_PORT;
}
return rc;
}
/**
* Called from lower layer when a packet has been received for an application
* port.
*
* @param om Pointer to received packet
*/
void
lora_app_mcps_indicate(struct pbuf *om)
{
int rc;
rc = pbuf_queue_put(&lora_node_app_rx_q, lora_node_app_evq_get(), om);
assert(rc == 0);
}
/**
* Interface between app and mac for MCPS confirmation. MAC layer calls
* this function when a packet has been transmitted or an error occurred.
*
* @param om Pointer to transmitted packet
*/
void
lora_app_mcps_confirm(struct pbuf *om)
{
int rc;
rc = pbuf_queue_put(&lora_node_app_txd_q, lora_node_app_evq_get(), om);
assert(rc == 0);
}
#if !LORA_APP_AUTO_JOIN
/* XXX: personalization? */
/**
* Join a lora network. When called this function will attempt to join
* if the end device is not already joined. Join status (success, failure)
* will be reported through the callback. If this function returns an error
* no callback will occur.
*
* @param dev_eui Pointer to device EUI
* @param app_eui Pointer to Application EUI
* @param app_key Pointer to application key
* @param trials Number of join attempts before failure
*
* @return int Lora app return code
*/
int
lora_app_join(uint8_t *dev_eui, uint8_t *app_eui, uint8_t *app_key,
uint8_t trials)
{
int rc;
/* Make sure parameters are valid */
if ((dev_eui == NULL) || (app_eui == NULL) || (app_key == NULL) ||
(trials == 0)) {
return LORA_APP_STATUS_INVALID_PARAM;
}
/* Tell device to start join procedure */
rc = lora_node_join(dev_eui, app_eui, app_key, trials);
return rc;
}
/* Performs a link check */
int
lora_app_link_check(void)
{
int rc;
rc = lora_node_link_check();
return rc;
}
#endif
/**
* Set the join callback. This will be called when joining succeeds or fails.
*
* @param join_cb Pointer to join callback function.
*
* @return int Lora return code
*/
int
lora_app_set_join_cb(lora_join_cb join_cb)
{
assert(join_cb != NULL);
lora_join_cb_func = join_cb;
return LORA_APP_STATUS_OK;
}
/* Set the link check callback */
int
lora_app_set_link_check_cb(lora_link_chk_cb link_chk_cb)
{
assert(link_chk_cb != NULL);
lora_link_chk_cb_func = link_chk_cb;
return LORA_APP_STATUS_OK;
}
void
lora_app_join_ev_cb(struct ble_npl_event *ev)
{
struct lora_app_join_ev_obj *evdata;
evdata = (struct lora_app_join_ev_obj *)ev->arg;
if (lora_join_cb_func) {
lora_join_cb_func(evdata->status, evdata->attempts);
}
}
void
lora_app_link_chk_ev_cb(struct ble_npl_event *ev)
{
struct lora_app_link_chk_ev_obj *evdata;
evdata = (struct lora_app_link_chk_ev_obj *)ev->arg;
if (lora_link_chk_cb_func) {
lora_link_chk_cb_func(evdata->status, evdata->num_gw,
evdata->demod_margin);
}
}
/**
* Called by the lora mac task when a join confirm occurs. This function fills
* out the event arguments and posts the appropriate event to the lora app task.
*
* @param status
* @param attempts
*/
void
lora_app_join_confirm(LoRaMacEventInfoStatus_t status, uint8_t attempts)
{
lora_app_join_ev_data.status = status;
lora_app_join_ev_data.attempts = attempts;
ble_npl_eventq_put(lora_node_app_evq, &lora_app_join_ev);
}
/**
* Called by the lora mac task when a link check confirm occurs. This function
* fills out the event arguments and posts the appropriate event to the lora app
* task.
*
* @param status
* @param num_gw
* @param demod_margin
*/
void
lora_app_link_chk_confirm(LoRaMacEventInfoStatus_t status, uint8_t num_gw,
uint8_t demod_margin)
{
lora_app_link_chk_ev_data.status = status;
lora_app_link_chk_ev_data.num_gw = num_gw;
lora_app_link_chk_ev_data.demod_margin = demod_margin;
ble_npl_eventq_put(lora_node_app_evq, &lora_app_link_chk_ev);
}
void
lora_app_init(void)
{
// Event Queue containing Events to be processed, defined in customer_app/sdk_app_lorawan/sdk_app_lorawan/demo.c.
// TODO: Move to header file.
extern struct ble_npl_eventq event_queue;
/* For now, the lora app event queue is the default event queue */
lora_node_app_evq = &event_queue;
/* Init join and link check events */
lora_app_join_ev.arg = &lora_app_join_ev_data;
lora_app_join_ev.fn = lora_app_join_ev_cb;
lora_app_link_chk_ev.arg = &lora_app_link_chk_ev_data;
lora_app_link_chk_ev.fn = lora_app_link_chk_ev_cb;
/* Set up receive queue and event */
pbuf_queue_init(&lora_node_app_rx_q, lora_node_proc_app_rxd_event, NULL, sizeof(struct lora_pkt_info));
/* Set up transmit done queue and event */
pbuf_queue_init(&lora_node_app_txd_q, lora_node_proc_app_txd_event, NULL, sizeof(struct lora_pkt_info));
}