Skip to content

Commit

Permalink
Region rework
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairjordan committed Jan 3, 2024
1 parent 1662e64 commit 437062c
Show file tree
Hide file tree
Showing 10 changed files with 1,877 additions and 708 deletions.
8 changes: 8 additions & 0 deletions sysdrv/tools/board/lorawan-bridge/LoraMac/LoRaMac.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "LoRaMacAdr.h"
#include "LoRaMacSerializer.h"
#include "radio.h"
#include <stdio.h>

#include "LoRaMac.h"

Expand Down Expand Up @@ -3758,6 +3759,7 @@ LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t* primitives, LoRaMacC
if( ( primitives == NULL ) ||
( callbacks == NULL ) )
{
printf("primitives or callbacks null");
return LORAMAC_STATUS_PARAMETER_INVALID;
}

Expand All @@ -3766,11 +3768,13 @@ LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t* primitives, LoRaMacC
( primitives->MacMlmeConfirm == NULL ) ||
( primitives->MacMlmeIndication == NULL ) )
{
printf("primitives null");
return LORAMAC_STATUS_PARAMETER_INVALID;
}
// Verify if the region is supported
if( RegionIsActive( region ) == false )
{
printf("region problem: %d", region);
return LORAMAC_STATUS_REGION_NOT_SUPPORTED;
}

Expand Down Expand Up @@ -3922,24 +3926,28 @@ LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t* primitives, LoRaMacC
// Initialize the Secure Element driver
if( SecureElementInit( &Nvm.SecureElement ) != SECURE_ELEMENT_SUCCESS )
{
printf("Secure Element Driver failed");
return LORAMAC_STATUS_CRYPTO_ERROR;
}

// Initialize Crypto module
if( LoRaMacCryptoInit( &Nvm.Crypto ) != LORAMAC_CRYPTO_SUCCESS )
{
printf("Crypto Driver failed");
return LORAMAC_STATUS_CRYPTO_ERROR;
}

// Initialize MAC commands module
if( LoRaMacCommandsInit( ) != LORAMAC_COMMANDS_SUCCESS )
{
printf("MAC Driver failed");
return LORAMAC_STATUS_MAC_COMMAD_ERROR;
}

// Set multicast downlink counter reference
if( LoRaMacCryptoSetMulticastReference( Nvm.MacGroup2.MulticastChannelList ) != LORAMAC_CRYPTO_SUCCESS )
{
printf("Multicast Downlink Driver failed");
return LORAMAC_STATUS_CRYPTO_ERROR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*
* \author Miguel Luis ( Semtech )
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
Expand Down Expand Up @@ -263,6 +264,7 @@ LmHandlerErrorStatus_t LmHandlerInit( LmHandlerCallbacks_t *handlerCallbacks,

if( LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks, LmHandlerParams->Region ) != LORAMAC_STATUS_OK )
{
printf("IT FAILDED HERE");
return LORAMAC_HANDLER_ERROR;
}

Expand Down Expand Up @@ -335,11 +337,14 @@ LmHandlerErrorStatus_t LmHandlerInit( LmHandlerCallbacks_t *handlerCallbacks,

LoRaMacTestSetDutyCycleOn( LmHandlerParams->DutyCycleEnabled );

printf("HERE1");
LoRaMacStart( );
printf("HERE2");

mibReq.Type = MIB_NETWORK_ACTIVATION;
if( LoRaMacMibGetRequestConfirm( &mibReq ) == LORAMAC_STATUS_OK )
{
printf("HERE3");
if( mibReq.Param.NetworkActivation == ACTIVATION_TYPE_NONE )
{
if( LmHandlerCallbacks->OnNetworkParametersChange != NULL )
Expand All @@ -348,6 +353,7 @@ LmHandlerErrorStatus_t LmHandlerInit( LmHandlerCallbacks_t *handlerCallbacks,
}
}
}
printf("HERE4");
return LORAMAC_HANDLER_SUCCESS;
}

Expand Down
10 changes: 6 additions & 4 deletions sysdrv/tools/board/lorawan-bridge/LoraMac/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/*! \file periodic-uplink/B-L072Z-LRWAN1/main.c */

#include <stdio.h>
#include <stdlib.h>
#include "utilities.h"
#include "board.h"
#include "RegionCommon.h"
Expand Down Expand Up @@ -274,20 +275,21 @@ int main( void )
// Fatal error, endless loop.
while ( 1 )
{
exit(-1); // This is linux now :)
}
}

// Set system maximum tolerated rx error in milliseconds
LmHandlerSetSystemMaxRxError( 20 );

printf("HERE5");
// The LoRa-Alliance Compliance protocol package should always be
// initialized and activated.
LmHandlerPackageRegister( PACKAGE_ID_COMPLIANCE, &LmhpComplianceParams );

printf("HERE6");
LmHandlerJoin( );

printf("HERE7");
StartTxProcess( LORAMAC_HANDLER_TX_ON_TIMER );

printf("HERE8");
// while( 1 )
// {
// // Process characters sent over the command line interface
Expand Down
2,455 changes: 1,765 additions & 690 deletions sysdrv/tools/board/lorawan-bridge/LoraMac/region/Region.c

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions sysdrv/tools/board/lorawan-bridge/LoraMac/rtc-board.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "timer.h"
#include "rtc-board.h"

void RtcProcess( void )
{
// Not used on this platform.
}


uint32_t GettimeToTicks(struct timespec *ts) {
uint32_t time;
time += ts->tv_sec * 1000000000;
time += ts->tv_nsec;
return time;
}

struct timespec TicksToGettime(uint32_t time) {
struct timespec ts;
ts.tv_sec += time / 1000000000;
ts.tv_nsec += time % 1000000000;
return ts;
}

TimerTime_t RtcTempCompensation( TimerTime_t period, float temperature )
{
//aka, not implemented on this board.
TimerTime_t time = 0;
return time;
}

uint32_t RtcGetMinimumTimeout( void )
{
return 0; //( MIN_ALARM_DELAY );
}

uint32_t RtcGetTimerValue(void) {
uint32_t time;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
time = GettimeToTicks(&ts);
}

uint32_t RtcTick2Ms(uint32_t clock) {
return clock / 1000000;
}

uint32_t RtcMs2Tick(uint32_t clock) {
return clock * 1000000;
}

void RtcSetAlarm( uint32_t timeout ) {
struct timespec ts = TicksToGettime(timeout);
nanosleep(&ts, NULL);
}

void RtcStopAlarm(void) {
//not needed;
}
2 changes: 2 additions & 0 deletions sysdrv/tools/board/lorawan-bridge/LoraMac/rtc-board.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ TimerTime_t RtcTempCompensation( TimerTime_t period, float temperature );
*/
void RtcProcess( void );

uint32_t GettimeToTicks(struct timespec *ts);

#ifdef __cplusplus
}
#endif
Expand Down
29 changes: 17 additions & 12 deletions sysdrv/tools/board/lorawan-bridge/LoraMac/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "utilities.h"
#include "board.h"
#include "timer.h"
#include "rtc-board.h"

/*!
* Safely execute call back
Expand Down Expand Up @@ -84,8 +85,9 @@ static bool TimerExists( TimerEvent_t *obj );

void TimerInit( TimerEvent_t *obj, void ( *callback )( void *context ) )
{
uint32_t time = RtcGetTimerValue();
obj->ReloadValue = time;
obj->Timestamp = 0;
obj->ReloadValue = 0;
obj->IsStarted = false;
obj->IsNext2Expire = false;
obj->Callback = callback;
Expand All @@ -100,7 +102,10 @@ void TimerSetContext( TimerEvent_t *obj, void* context )

void TimerStart( TimerEvent_t *obj )
{
uint32_t elapsedTime = 0;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
uint32_t time = GettimeToTicks(&ts);
uint32_t elapsedTime = time;

CRITICAL_SECTION_BEGIN( );

Expand All @@ -116,13 +121,13 @@ void TimerStart( TimerEvent_t *obj )

if( TimerListHead == NULL )
{
RtcSetTimerContext( );
//RtcSetTimerContext( );
// Inserts a timer at time now + obj->Timestamp
TimerInsertNewHeadTimer( obj );
}
else
{
elapsedTime = RtcGetTimerElapsedTime( );
elapsedTime = RtcGetTimerValue( );
obj->Timestamp += elapsedTime;

if( obj->Timestamp < TimerListHead->Timestamp )
Expand Down Expand Up @@ -184,8 +189,8 @@ void TimerIrqHandler( void )
TimerEvent_t* cur;
TimerEvent_t* next;

uint32_t old = RtcGetTimerContext( );
uint32_t now = RtcSetTimerContext( );
uint32_t old = RtcGetTimerValue( );
uint32_t now = RtcGetTimerValue( );
uint32_t deltaContext = now - old; // intentional wrap around

// Update timeStamp based upon new Time Reference
Expand Down Expand Up @@ -216,7 +221,7 @@ void TimerIrqHandler( void )
}

// Remove all the expired object from the list
while( ( TimerListHead != NULL ) && ( TimerListHead->Timestamp < RtcGetTimerElapsedTime( ) ) )
while( ( TimerListHead != NULL ) && ( TimerListHead->Timestamp < RtcGetTimerValue( ) ) )
{
cur = TimerListHead;
TimerListHead = TimerListHead->Next;
Expand Down Expand Up @@ -366,11 +371,11 @@ static void TimerSetTimeout( TimerEvent_t *obj )
int32_t minTicks= RtcGetMinimumTimeout( );
obj->IsNext2Expire = true;

// In case deadline too soon
if( obj->Timestamp < ( RtcGetTimerElapsedTime( ) + minTicks ) )
{
obj->Timestamp = RtcGetTimerElapsedTime( ) + minTicks;
}
// // In case deadline too soon
// if( obj->Timestamp < ( RtcGetTimerElapsedTime( ) + minTicks ) )
// {
// obj->Timestamp = RtcGetTimerElapsedTime( ) + minTicks;
// }
RtcSetAlarm( obj->Timestamp );
}

Expand Down
17 changes: 15 additions & 2 deletions sysdrv/tools/board/lorawan-bridge/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
#

#This makefile is used to test the other Makefiles
ifeq ($(SYSDRV_PARAM), )
SYSDRV_PARAM:=../../../Makefile.param
include $(SYSDRV_PARAM)
endif


PROG = rest_server
SRC = rest.c cJSON/cJSON.c cJSON/cJSON_Utils.c send_json.c lorawan_send.c b64.c/encode.c b64.c/decode.c b64.c/buffer.c
LORAWAN_SRC = LoraMac/spidev_lib/spidev_lib.c LoraMac/board.c LoraMac/main.c LoraMac/spi-board.c LoraMac/delay.c LoraMac/sx1276.c LoraMac/sx1276-board.c LoraMac/LoRaMac.c LoraMac/LoRaMacAdr.c LoraMac/LoRaMacClassB.c LoraMac/LoRaMacCommands.c LoraMac/LoRaMacConfirmQueue.c LoraMac/LoRaMacCrypto.c LoraMac/LoRaMacParser.c LoraMac/LoRaMacSerializer.c LoraMac/timer.c LoraMac/utilities.c LoraMac/systime.c
LORAWAN_SRC = LoraMac/spidev_lib/spidev_lib.c LoraMac/board.c LoraMac/main.c LoraMac/spi-board.c LoraMac/delay.c LoraMac/sx1276.c LoraMac/sx1276-board.c LoraMac/LoRaMac.c LoraMac/LoRaMacAdr.c LoraMac/LoRaMacClassB.c LoraMac/LoRaMacCommands.c LoraMac/LoRaMacConfirmQueue.c LoraMac/LoRaMacCrypto.c LoraMac/LoRaMacParser.c LoraMac/LoRaMacSerializer.c LoraMac/timer.c LoraMac/utilities.c LoraMac/systime.c LoraMac/rtc-board.c
LORAMAC_COMMON_SRC = LoraMac/common/LmHandler/LmHandler.c LoraMac/common/LmHandler/packages/FragDecoder.c LoraMac/common/LmHandler/packages/LmhpClockSync.c LoraMac/common/LmHandler/packages/LmhpCompliance.c LoraMac/common/LmHandler/packages/LmhpFragmentation.c LoraMac/common/LmHandler/packages/LmhpRemoteMcastSetup.c
LORAMAC_COMMON_SRC += LoraMac/common/CayenneLpp.c LoraMac/common/LmHandlerMsgDisplay.c
LORAMAC_SOFT_SE_SRC += LoraMac/soft-se/aes.c LoraMac/soft-se/cmac.c LoraMac/soft-se/soft-se-hal.c LoraMac/soft-se/soft-se.c
Expand All @@ -23,6 +27,13 @@ INCLUDE = -ILoraMac -ILoraMac/region -ILoraMac/common -ILoraMac/common/LmHandler

include $(TOP)/resources/Makefile.in-os

export LC_ALL=C
SHELL:=/bin/bash
# arch=arm default use thumb-2 compile
ifeq ($(SYSDRV_ARCH_TYPE),arm)
PKG_USE_THUMB2 ?= YES
endif

ifeq ($(TARGET_OS),LINUX)
LIBS += -ldl
LIBS += -lm
Expand All @@ -31,11 +42,13 @@ endif
all: $(PROG)

$(PROG): $(CIVETWEB_LIB) $(LORAWAN_SRC) $(LORAMAC_COMMON_SRC) $(LORAMAC_REGION_SRC) $(LORAMAC_SOFT_SE_SRC)
$(CC) -o $@ $(CFLAGS) $(INCLUDE) $(LDFLAGS) $(LORAWAN_SRC) $(LORAMAC_COMMON_SRC) $(LORAMAC_REGION_SRC) $(LORAMAC_SOFT_SE_SRC) $(CIVETWEB_LIB) $(LIBS)
$(SYSDRV_CROSS)-$(CC) -o $@ $(CFLAGS) $(INCLUDE) $(LDFLAGS) $(LORAWAN_SRC) $(LORAMAC_COMMON_SRC) $(LORAMAC_REGION_SRC) $(LORAMAC_SOFT_SE_SRC) $(CIVETWEB_LIB) $(LIBS)
$(call MAROC_COPY_PKG_TO_SYSDRV_OUTPUT, $(SYSDRV_DIR_OUT_ROOTFS), $(PKG_BIN))

$(CIVETWEB_LIB):
$(MAKE) -C $(TOP) WITH_IPV6=1 WITH_WEBSOCKET=1 COPT='-DNO_SSL -DMG_EXPERIMENTAL_INTERFACES' clean lib
cp $(TOP)/$(CIVETWEB_LIB) .
$(call MAROC_COPY_PKG_TO_SYSDRV_OUTPUT, $(SYSDRV_DIR_OUT_ROOTFS), $(PKG_BIN))

clean:
rm -f $(CIVETWEB_LIB) $(PROG)
Expand Down
Binary file modified sysdrv/tools/board/lorawan-bridge/libcivetweb.a
Binary file not shown.
Binary file added sysdrv/tools/board/lorawan-bridge/rest_server
Binary file not shown.

0 comments on commit 437062c

Please sign in to comment.