lorawan: port oriented downlink callbacks
Add downlink callbacks on a per-port basis. A single message will be handled as many times as users have registered matching ports. Callbacks will also be run on "meta" downlink packets on port 0, such as confirmed uplink acknowledgements. Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
parent
6e9500852a
commit
bbd53dcde0
2 changed files with 56 additions and 8 deletions
|
@ -13,6 +13,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <device.h>
|
#include <device.h>
|
||||||
|
#include <sys/slist.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -103,6 +104,35 @@ struct lorawan_join_config {
|
||||||
enum lorawan_act_type mode;
|
enum lorawan_act_type mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define LW_RECV_PORT_ANY UINT16_MAX
|
||||||
|
|
||||||
|
struct lorawan_downlink_cb {
|
||||||
|
/* Port to handle messages for:
|
||||||
|
* Port 0: TX packet acknowledgements
|
||||||
|
* Ports 1-255: Standard downlink port
|
||||||
|
* LW_RECV_PORT_ANY: All downlinks
|
||||||
|
*/
|
||||||
|
uint16_t port;
|
||||||
|
/**
|
||||||
|
* @brief Callback function to run on downlink data
|
||||||
|
*
|
||||||
|
* @note Callbacks are run on the system workqueue,
|
||||||
|
* and should therefore be as short as possible.
|
||||||
|
*
|
||||||
|
* @param port Port message was sent on
|
||||||
|
* @param data_pending Network server has more downlink packets pending
|
||||||
|
* @param rssi Received signal strength in dBm
|
||||||
|
* @param snr Signal to Noise ratio in dBm
|
||||||
|
* @param len Length of data received, will be 0 for ACKs
|
||||||
|
* @param data Data received, will be NULL for ACKs
|
||||||
|
*/
|
||||||
|
void (*cb)(uint8_t port, bool data_pending,
|
||||||
|
int16_t rssi, int8_t snr,
|
||||||
|
uint8_t len, const uint8_t *data);
|
||||||
|
/** Node for callback list */
|
||||||
|
sys_snode_t node;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add battery level callback function.
|
* @brief Add battery level callback function.
|
||||||
*
|
*
|
||||||
|
@ -121,6 +151,13 @@ struct lorawan_join_config {
|
||||||
*/
|
*/
|
||||||
int lorawan_set_battery_level_callback(uint8_t (*battery_lvl_cb)(void));
|
int lorawan_set_battery_level_callback(uint8_t (*battery_lvl_cb)(void));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Register a callback to be run on downlink packets
|
||||||
|
*
|
||||||
|
* @param cb Pointer to structure containing callback parameters
|
||||||
|
*/
|
||||||
|
void lorawan_register_downlink_callback(struct lorawan_downlink_cb *cb);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Register a callback to be called when the datarate changes
|
* @brief Register a callback to be called when the datarate changes
|
||||||
*
|
*
|
||||||
|
|
|
@ -62,7 +62,7 @@ static enum lorawan_datarate current_datarate;
|
||||||
static uint8_t lorawan_conf_msg_tries = 1;
|
static uint8_t lorawan_conf_msg_tries = 1;
|
||||||
static bool lorawan_adr_enable;
|
static bool lorawan_adr_enable;
|
||||||
|
|
||||||
|
static sys_slist_t dl_callbacks;
|
||||||
|
|
||||||
static LoRaMacPrimitives_t macPrimitives;
|
static LoRaMacPrimitives_t macPrimitives;
|
||||||
static LoRaMacCallback_t macCallbacks;
|
static LoRaMacCallback_t macCallbacks;
|
||||||
|
@ -129,6 +129,8 @@ static void McpsConfirm(McpsConfirm_t *mcpsConfirm)
|
||||||
|
|
||||||
static void McpsIndication(McpsIndication_t *mcpsIndication)
|
static void McpsIndication(McpsIndication_t *mcpsIndication)
|
||||||
{
|
{
|
||||||
|
struct lorawan_downlink_cb *cb;
|
||||||
|
|
||||||
LOG_DBG("Received McpsIndication %d", mcpsIndication->McpsIndication);
|
LOG_DBG("Received McpsIndication %d", mcpsIndication->McpsIndication);
|
||||||
|
|
||||||
if (mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK) {
|
if (mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK) {
|
||||||
|
@ -142,17 +144,19 @@ static void McpsIndication(McpsIndication_t *mcpsIndication)
|
||||||
datarate_observe(false);
|
datarate_observe(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: Check MCPS Indication type */
|
/* Iterate over all registered downlink callbacks */
|
||||||
if (mcpsIndication->RxData == true) {
|
SYS_SLIST_FOR_EACH_CONTAINER(&dl_callbacks, cb, node) {
|
||||||
if (mcpsIndication->BufferSize != 0) {
|
if ((cb->port == LW_RECV_PORT_ANY) ||
|
||||||
LOG_DBG("Rx Data: %s",
|
(cb->port == mcpsIndication->Port)) {
|
||||||
log_strdup(mcpsIndication->Buffer));
|
cb->cb(mcpsIndication->Port,
|
||||||
|
!!mcpsIndication->FramePending,
|
||||||
|
mcpsIndication->Rssi, mcpsIndication->Snr,
|
||||||
|
mcpsIndication->BufferSize,
|
||||||
|
mcpsIndication->Buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
last_mcps_indication_status = mcpsIndication->Status;
|
last_mcps_indication_status = mcpsIndication->Status;
|
||||||
|
|
||||||
/* TODO: Compliance test based on FPort value*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void MlmeConfirm(MlmeConfirm_t *mlmeConfirm)
|
static void MlmeConfirm(MlmeConfirm_t *mlmeConfirm)
|
||||||
|
@ -523,6 +527,11 @@ int lorawan_set_battery_level_callback(uint8_t (*battery_lvl_cb)(void))
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lorawan_register_downlink_callback(struct lorawan_downlink_cb *cb)
|
||||||
|
{
|
||||||
|
sys_slist_append(&dl_callbacks, &cb->node);
|
||||||
|
}
|
||||||
|
|
||||||
void lorawan_register_dr_changed_callback(void (*cb)(enum lorawan_datarate))
|
void lorawan_register_dr_changed_callback(void (*cb)(enum lorawan_datarate))
|
||||||
{
|
{
|
||||||
dr_change_cb = cb;
|
dr_change_cb = cb;
|
||||||
|
@ -560,6 +569,8 @@ static int lorawan_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
LoRaMacStatus_t status;
|
LoRaMacStatus_t status;
|
||||||
|
|
||||||
|
sys_slist_init(&dl_callbacks);
|
||||||
|
|
||||||
macPrimitives.MacMcpsConfirm = McpsConfirm;
|
macPrimitives.MacMcpsConfirm = McpsConfirm;
|
||||||
macPrimitives.MacMcpsIndication = McpsIndication;
|
macPrimitives.MacMcpsIndication = McpsIndication;
|
||||||
macPrimitives.MacMlmeConfirm = MlmeConfirm;
|
macPrimitives.MacMlmeConfirm = MlmeConfirm;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue