Commit 8bfc3d37 authored by Guillaume Gonnet's avatar Guillaume Gonnet
Browse files

Add recv thread in LoRaRIOT.

parent 04726fd7
......@@ -2,6 +2,7 @@
Manage activation using OTAA / ABP.
Copyright (C) 2019, ENSIMAG students
This file is based on "semtech-loramac" module from RIOT OS, under the
LGPLv2.1 license.
......
......@@ -2,6 +2,7 @@
Controller for LoRaWAN adapter for RIOT.
Copyright (C) 2019, ENSIMAG students
This file is based on "semtech-loramac" module from RIOT OS, under the
LGPLv2.1 license.
......@@ -92,18 +93,6 @@ void *lorariot_event_loop(void *arg)
// mac->link_chk.available = true;
} break;
case LORARIOT_TYPE_LORAMAC_TX_STATUS: {
DEBUG("[lorariot] received TX status\n");
msg_t msg_ret;
msg_ret.type = msg.type;
msg_ret.content.value = msg.content.value;
msg_send(&msg_ret, lorariot.caller_pid);
} break;
case LORARIOT_TYPE_LORAMAC_RX: {
// TODO: call receive thread.
} break;
default:
DEBUG("[semtech-loramac] Unexpected msg type '%04x'\n", msg.type);
}
......
......@@ -2,6 +2,7 @@
Manage message sending.
Copyright (C) 2019, ENSIMAG students
This file is based on "semtech-loramac" module from RIOT OS, under the
LGPLv2.1 license.
......@@ -11,6 +12,7 @@ LGPLv2.1 license.
#include "lorariot.h"
#include "LoRaMac.h"
#include "LoRaMacTest.h"
#include <msg.h>
#include <net/netdev.h>
......
......@@ -2,6 +2,7 @@
Print LoRaRIOT debug messages.
Copyright (C) 2019, ENSIMAG students
This file is based on "semtech-loramac" module from RIOT OS, under the
LGPLv2.1 license.
......
......@@ -2,6 +2,7 @@
Manage message reception.
Copyright (C) 2019, ENSIMAG students
This file is based on "semtech-loramac" module from RIOT OS, under the
LGPLv2.1 license.
......@@ -17,6 +18,10 @@ LGPLv2.1 license.
#include <sx127x_netdev.h>
// PID of receive thread.
static volatile kernel_pid_t recv_thread = KERNEL_PID_UNDEF;
// Handle a NetDev event.
void lorariot_event_cb(netdev_t *dev, netdev_event_t event)
{
......@@ -87,27 +92,46 @@ void lorariot_mcps_indication(McpsIndication_t *indication)
{
DEBUG("[lorariot] MCPS indication event\n");
if (indication->Status != LORAMAC_EVENT_INFO_STATUS_OK) {
DEBUG("[lorariot] MCPS indication no OK\n");
DEBUG("[lorariot] MCPS indication error: %d\n", indication->Status);
return;
}
lorariot_debug_mcps(indication->McpsIndication);
msg_t msg;
if (indication->RxData) {
// We have received some data: send theme to receive thread.
if (indication->RxData && pid_is_valid(recv_thread)) {
DEBUG("[lorariot] MCPS indication: data received\n");
msg.type = LORARIOT_TYPE_LORAMAC_RX;
msg.content.ptr = indication;
} else {
DEBUG("[lorariot] MCPS indication: TX done\n");
msg.type = LORARIOT_TYPE_LORAMAC_TX_STATUS;
msg.content.value = LORARIOT_TX_DONE;
lorariot.dwlnk_data = (lorariot_dwlnk_t) {
indication->Port, indication->BufferSize, indication->Buffer
};
msg_t msg;
msg.type = LORARIOT_DATA_RECEIVED;
msg.content.ptr = &lorariot.dwlnk_data;
msg_try_send(&msg, recv_thread);
}
msg_send(&msg, lorariot_pid);
// Notify the caller thread that the request is done.
msg_t msg;
msg.type = LORARIOT_TYPE_LORAMAC_TX_STATUS;
msg.content.value = LORARIOT_TX_DONE;
msg_send(&msg, lorariot.caller_pid);
}
// Set receive thread PID.
void lorariot_set_recv_thread(kernel_pid_t pid)
{
// The global variable `recv_thread` is not protected by a mutex, which can
// cause a data race in the function above. Be carful to use this function
// only if you are sure that a message will not be received soon.
recv_thread = pid;
}
// MLME-Indication event function.
void lorariot_mlme_indication(MlmeIndication_t *indication)
{
......
......@@ -2,6 +2,7 @@
Manage message sending.
Copyright (C) 2019, ENSIMAG students
This file is based on "semtech-loramac" module from RIOT OS, under the
LGPLv2.1 license.
......@@ -24,7 +25,7 @@ void lorariot_mcps_confirm(McpsConfirm_t *confirm)
else
msg.content.value = LORARIOT_TX_DONE;
msg_send(&msg, lorariot_pid);
msg_send(&msg, lorariot.caller_pid);
}
......@@ -77,6 +78,9 @@ static void _send_mcps(lorariot_mcps_t *mcps)
req.Req.Confirmed.NbTrials = mcps->nb_trials;
req.Req.Confirmed.Datarate = mcps->dr;
// If we use a port >= 32, we skip reception windows.
LoRaMacTestRxWindowsOn(mcps->port < 32);
// Make the MCPS request.
uint8_t status = LoRaMacMcpsRequest(&req);
lorariot_debug_status("MCPS request", status);
......
......@@ -9,6 +9,8 @@ LGPLv2.1 license.
#pragma once
#include <kernel_types.h>
#include <stdint.h>
#include <stdbool.h>
......@@ -43,9 +45,17 @@ typedef struct {
bool available; // new link check information avalable
} lorariot_link_check_t;
// Downlink data.
typedef struct {
uint8_t port;
uint8_t length;
uint8_t *data;
} lorariot_dwlnk_t;
// Store data of LoRaRIOT controller.
typedef struct {
uint8_t caller_pid; // PID of caller thread
lorariot_dwlnk_t dwlnk_data; // downlink data
lorariot_link_check_t link_chk; // link check information
} lorariot_ctrl_t;
......@@ -62,7 +72,7 @@ typedef struct {
uint8_t sf; // from 6 to 12
int8_t dr; // DR from DR_CUSTOM to DR_5
uint8_t port; // application port
uint8_t port; // application port (if >= 32, skip reception window)
uint8_t len; // buffer length
uint8_t nb_trials; // 0 = unconfirmed, >= 1 nb of trials
uint8_t *buffer;
......@@ -72,13 +82,13 @@ typedef struct {
// Datarate for `dr` parameter in `lorariot_mcps_t`.
enum {
DR_CUSTOM = -1,
DR_0 = 0, // SF12, BW125
DR_1 = 1, // SF11, BW125
DR_2 = 2, // SF10, BW125
DR_3 = 3, // SF9, BW125
DR_4 = 4, // SF8, BW125
DR_5 = 5, // SF7, BW125
DR_6 = 6, // SF7, BW250
DR_0 = 0, // SF12, BW125
DR_1 = 1, // SF11, BW125
DR_2 = 2, // SF10, BW125
DR_3 = 3, // SF9, BW125
DR_4 = 4, // SF8, BW125
DR_5 = 5, // SF7, BW125
DR_6 = 6, // SF7, BW250
};
......@@ -133,6 +143,12 @@ uint8_t *loraraiot_retrieve_otaa(void);
void lorariot_replay_otaa(uint8_t *data);
/**
* @brief Set the thread that must receive downlink messages.
* @param pid PID of receive thread.
*/
void lorariot_set_recv_thread(kernel_pid_t pid);
/**
* @brief Call a function in LoRaRIOT event loop.
* @param func the function to call.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment