end node on synchronous star LoRa network.

Dependencies:   SX127x sx12xx_hal TSL2561

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This project for use with LoRaWAN_singlechannel_gateway project.

Alternately gateway running on raspberry pi can be used as gateway.

LoRaWAN on single radio channel

Network description is at gateway project page. Synchronous star network.

Hardware Support

This project supports SX1276 and SX1272, sx126x kit, sx126x shield, and sx128x 2.4GHz. The ST board B-L072Z-LRWAN1 is also supported (TypeABZ module). When B-L072Z-LRWAN1 target is selected, TARGET_DISCO_L072CZ_LRWAN1 is defined by tools, allowing correct radio driver configuration for this platform. Alternately, any mbed board that can use LoRa radio shield board should work, but NUCLEO boards are tested.

End-node Unique ID

DevEUI is created from CPU serial number. AppEUI and AppKey are declared as software constants.

End-node Configuration

Data rate definition LORAMAC_DEFAULT_DATARATE configured in LoRaMac-definitions.h. See gateway project page for configuration of gateway.
LoRaWAN addressing is configured in Comissioning.h; only OTA mode is functional.
Header file board/lora_config.h, selects application layer options (i.e. sensors) to be compiled in.

Serial Interface

Serial port operates at 115200bps.
Application layer single_us915_main.cpp User button triggers uplink (i.e. blue button on nucleo board), or jumper enables continuously sends repeated uplink packets. The MAC layer holds each uplink request until the allocated timeslot.

commandargumentsdescription
?-print available commands
. (period)-print status (DevEUI, DevAddr, etc)
ullength integerset payload length of test uplink packets

sensor demo

Selected grove sensors may be plugged into SX1272 shield.
To enable, edit lora_config.h to define SENSORS.

Sensor connections on SX1272MB2xAS:

D8 D9: buttonRX TX: (unused)A3 A4: Rotary Angle Sensor
D6 D7: RGB LEDSCL SDA: digital light sensorA1 A2: Rotary Angle Sensor

Digital input pin, state reported via uplink: PC8
Digital output pin, controlled via downlink: PC6
PWM out: PB_10

Jumper enables auto-repeated transmit: PC10 and PC12 on NUCLEO board, located on end of morpho headers nearby JP4.

app/single_us915_main.cpp

Committer:
dudmuck
Date:
2017-08-04
Revision:
21:500ff43d8424
Parent:
20:42839629a5dc
Child:
22:999a7e7698a8

File content as of revision 21:500ff43d8424:

/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
    (C)2015 Semtech

Description: LoRaMac classA device implementation

License: Revised BSD License, see LICENSE.TXT file include in the project

Maintainer: Miguel Luis and Gregory Cristian
*/
#include "mbed.h"
#include "board.h"
#include "radio.h"

#include "LoRaMac.h"
#include "Commissioning.h"
#include "commands.h"

#if defined(ENABLE_SX1272) && defined(SENSORS)
    #include "ChainableLED.h"
    #include "TSL2561.h"
    InterruptIn user_button(USER_BUTTON);
    #define RED_LED_RATE_US     333333  /* third of second */
    uint32_t button_cnt;
    ChainableLED rgb(D6, D7, 1); // https://developer.mbed.org/components/Grove-Chainable-RGB-LED/
    AnalogIn a1(A1); // https://developer.mbed.org/teams/Seeed/wiki/Potentiometer
    AnalogIn a3(A3); // https://developer.mbed.org/teams/Seeed/wiki/Potentiometer
    DigitalIn pc8_in(PC_8);
    DigitalOut pc6_out(PC_6);   
    //TSL2561 tsl2561(TSL2561_ADDR_FLOAT); // https://developer.mbed.org/components/Grove-Digital-Light-Sensor/ 
    TSL2561 tsl2561(TSL2561_ADDR_LOW);
    PwmOut pwm(PB_11);
#endif

DigitalOut pc2(PC_2);

DigitalOut jumper_out(PC_10);
InterruptIn jumper_in(PC_12);

char pcbuf[128];
int pcbuf_len;

/*!
 * Defines the application data transmission duty cycle. 5s, value in [ms].
 */
#define APP_TX_DUTYCYCLE                            5000

/*!
 * Defines a random delay for application data transmission duty cycle. 1s,
 * value in [ms].
 */
#define APP_TX_DUTYCYCLE_RND                        1000

/*!
 * Default datarate
 */
#define LORAWAN_DEFAULT_DATARATE                    DR_10

/*!
 * LoRaWAN confirmed messages
 */
#define LORAWAN_CONFIRMED_MSG_ON                    true

/*!
 * LoRaWAN application port
 */
#define LORAWAN_APP_PORT                            15

/*!
 * User application data buffer size
 */
#if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
#define LORAWAN_APP_DATA_SIZE                       6

#else
#define LORAWAN_APP_DATA_SIZE                       1

#endif

static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;

#if( OVER_THE_AIR_ACTIVATION == 0 )

static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
static uint8_t AppSKey[] = LORAWAN_APPSKEY;

/*!
 * Device address
 */
static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;

#endif

/*!
 * Application port
 */
#if defined(SENSORS) && defined(ENABLE_SX1272)
    static uint8_t AppPort = SENSOR_PORT;
#else
    static uint8_t AppPort = LORAWAN_APP_PORT;
#endif

/*!
 * User application data size
 */
static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
static unsigned int uplink_length;   // user assigned

/*!
 * User application data buffer size
 */
#define LORAWAN_APP_DATA_MAX_SIZE                           128

/*!
 * User application data
 */
static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];

/*!
 * Indicates if the node is sending confirmed or unconfirmed messages
 */
static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;

/*!
 * Defines the application data transmission duty cycle
 */
static uint32_t TxDutyCycleTime;

/*!
 * Timer to handle the application data transmission duty cycle
 */
LowPowerTimeout TxNextPacketTimeout;

volatile bool send_at_beacon;
volatile bool awaiting_mcps_indic;
bool join_retry;
/*!
 * Specifies the state of the application LED
 */
static bool AppLedStateOn = false;

/*!
 * Device states
 */
static enum eDeviceState
{
    DEVICE_STATE_INIT,
    DEVICE_STATE_JOIN,
    DEVICE_STATE_CYCLE,
    DEVICE_STATE_SLEEP,
    DEVICE_STATE_REJOIN,
    DEVICE_STATE_UPLINK
}DeviceState;

/*!
 * Strucure containing the Uplink status
 */
struct sLoRaMacUplinkStatus
{
    uint8_t Acked;
    //int8_t Datarate;
    uint16_t UplinkCounter;
    uint8_t Port;
    uint8_t *Buffer;
    uint8_t BufferSize;
}LoRaMacUplinkStatus;
//volatile bool UplinkStatusUpdated = false;

/*!
 * Strucure containing the Downlink status
 */
struct sLoRaMacDownlinkStatus
{
    int16_t Rssi;
    int8_t Snr;
    uint16_t DownlinkCounter;
    bool RxData;
    uint8_t Port;
    uint8_t *Buffer;
    uint8_t BufferSize;
}LoRaMacDownlinkStatus;

static uint8_t missed_count;


/*!
 * \brief   Prepares the payload of the frame
 */
static void PrepareTxFrame( uint8_t port )
{
    switch( port )
    {
    case 15:
        {
            AppData[0] = AppLedStateOn;
            if( IsTxConfirmed == true )
            {
                AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
                AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
                AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
                AppData[4] = LoRaMacDownlinkStatus.Rssi;
                AppData[5] = LoRaMacDownlinkStatus.Snr;
            }
        }
        break;
#if defined(SENSORS) && defined(ENABLE_SX1272)
    case SENSOR_PORT:
        {
            uint16_t x = tsl2561.getLuminosity(TSL2561_VISIBLE);
            uint16_t a_1 = a1.read_u16();
            uint16_t a_3 = a3.read_u16();
            uint8_t status;
            status = pc8_in.read();
            status <<= 1;
            status |= pc6_out.read();
            AppData[0] = a_1 >> 8;
            AppData[1] = a_1 & 0xff;
            AppData[2] = a_3 >> 8;
            AppData[3] = a_3 & 0xff;
            AppData[4] = x >> 8;
            AppData[5] = x & 0xff;
            AppData[6] = status;
            AppDataSize = uplink_length;
            //printf("visible:%u\r\n", x);
        }
        break;
#endif /* SENSORS && ENABLE_SX1272 */
    default:
        break;
    }
}

void
LoRaMacEventInfoStatus_to_string(LoRaMacEventInfoStatus_t status, char* dst)
{
    const char* ptr = NULL;

    switch (status) {
        case LORAMAC_EVENT_INFO_STATUS_RX_ERROR: ptr = "RX_ERROR"; break;
        case LORAMAC_EVENT_INFO_STATUS_OK: ptr = "OK"; break;
        case LORAMAC_EVENT_INFO_STATUS_ERROR_RX_MTYPE: ptr = "ERROR_RX_MTYPE"; break;
        case LORAMAC_EVENT_INFO_STATUS_ERROR_SEND: ptr = "ERROR_SEND"; break;
        case LORAMAC_EVENT_INFO_STATUS_ERROR_JOIN_ACCEPT: ptr = "ERROR_JOIN_ACCEPT"; break;
        case LORAMAC_EVENT_INFO_STATUS_ERROR_MLMEREQ: ptr = "ERROR_MLMEREQ"; break;
        case LORAMAC_EVENT_INFO_STATUS_ERROR_MCPSREQ: ptr = "ERROR_MCPSREQ"; break;
        //case LORAMAC_EVENT_INFO_STATUS_ERROR: ptr = "ERROR"; break;
        
        case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT: ptr = "TX_TIMEOUT"; break;
        case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT: ptr = "RX_TIMEOUT"; break;
        case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL: ptr = "JOIN_FAIL"; break;
        case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED: ptr = "DOWNLINK_REPEATED"; break;
        case LORAMAC_EVENT_INFO_STATUS_TX_DR_PAYLOAD_SIZE_ERROR: ptr = "TX_DR_PAYLOAD_SIZE_ERROR"; break;
        case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS: ptr = "DOWNLINK_TOO_MANY_FRAMES_LOSS"; break;
        case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL: ptr = "ADDRESS_FAIL"; break;
        case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL: ptr = "MIC_FAIL"; break;
        case LORAMAC_EVENT_INFO_STATUS_BEACON_LOCKED: ptr = "BEACON_LOCKED"; break;
        case LORAMAC_EVENT_INFO_STATUS_BEACON_LOST: ptr = "BEACON_LOST"; break;
        case LORAMAC_EVENT_INFO_STATUS_BEACON_NOT_FOUND: ptr = "BEACON_NOT_FOUND"; break;
        case LORAMAC_EVENT_INFO_STATUS_ERROR_INCOMPLETE: ptr = "INCOMPLETE"; break;
        case LORAMAC_EVENT_INFO_STATUS_STOP: ptr = "STOP"; break;
    }

    if (ptr != NULL)
        strcpy(dst, ptr);
}

void
LoRaMacStatus_to_string(LoRaMacStatus_t status, char* dst)
{
    const char* ptr = NULL;

    switch (status) {
        case LORAMAC_STATUS_OK: ptr = "OK"; break;
        case LORAMAC_STATUS_BUSY: ptr = "BUSY"; break;
        case LORAMAC_STATUS_SERVICE_UNKNOWN: ptr = "SERVICE_UNKNOWN"; break;
        case LORAMAC_STATUS_PARAMETER_INVALID: ptr = "PARAMETER_INVALID"; break;
        case LORAMAC_STATUS_FREQUENCY_INVALID: ptr = "FREQUENCY_INVALID"; break;
        case LORAMAC_STATUS_DATARATE_INVALID: ptr = "DATARATE_INVALID"; break;
        case LORAMAC_STATUS_FREQ_AND_DR_INVALID: ptr = "FREQ_AND_DR_INVALID"; break;
        case LORAMAC_STATUS_NO_NETWORK_JOINED: ptr = "NO_NETWORK_JOINED"; break;
        case LORAMAC_STATUS_LENGTH_ERROR: ptr = "LENGTH_ERROR"; break;
        case LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR: ptr = "MAC_CMD_LENGTH_ERROR"; break;
        case LORAMAC_STATUS_DEVICE_OFF: ptr = "DEVICE_OFF"; break;
    }
    if (ptr != NULL)
        strcpy(dst, ptr);
}

static void OnTxNextPacketTimerEvent( void );

/*!
 * \brief   Prepares the payload of the frame
 *
 * \retval  [0: frame could be send, 1: error]
 */
static bool SendFrame(uint8_t port)
{
    McpsReq_t mcpsReq;
    LoRaMacTxInfo_t txInfo;
    LoRaMacStatus_t status;
    char str[64];

    if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
    {
        // Send empty frame in order to flush MAC commands
        mcpsReq.Type = MCPS_UNCONFIRMED;
        mcpsReq.Req.Unconfirmed.fBuffer = NULL;
        mcpsReq.Req.Unconfirmed.fBufferSize = 0;

        LoRaMacUplinkStatus.Acked = false;
        LoRaMacUplinkStatus.Port = 0;
        LoRaMacUplinkStatus.Buffer = NULL;
        LoRaMacUplinkStatus.BufferSize = 0;
    }
    else
    {
        LoRaMacUplinkStatus.Acked = false;
        LoRaMacUplinkStatus.Port = port;
        LoRaMacUplinkStatus.Buffer = AppData;
        LoRaMacUplinkStatus.BufferSize = AppDataSize;

        if( IsTxConfirmed == false )
        {
            mcpsReq.Type = MCPS_UNCONFIRMED;
            mcpsReq.Req.Unconfirmed.fPort = port;
            mcpsReq.Req.Unconfirmed.fBuffer = AppData;
            mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
            //mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
        }
        else
        {
            mcpsReq.Type = MCPS_CONFIRMED;
            mcpsReq.Req.Confirmed.fPort = port;
            mcpsReq.Req.Confirmed.fBuffer = AppData;
            mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
            mcpsReq.Req.Confirmed.NbTrials = 8;
            //mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
        }
    }

    status = LoRaMacMcpsRequest( &mcpsReq );
    if( status == LORAMAC_STATUS_OK )
    {
        awaiting_mcps_indic = true;
        return false;
    }
    LoRaMacStatus_to_string(status, str);
    isr_printf("send failed:%s\r\n", str);

    if (status == LORAMAC_STATUS_NO_NETWORK_JOINED) {
        join_retry = true;
    } else {
        AppPort = port;
        TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);      
    }

    return true;
}

/*!
 * \brief Function executed on TxNextPacket Timeout event
 */
static void OnTxNextPacketTimerEvent( void )
{
    MibRequestConfirm_t mibReq;
    LoRaMacStatus_t status;
    
    if (DeviceState == DEVICE_STATE_REJOIN) {
        isr_printf("unjoin\r\n");
        mibReq.Type = MIB_NETWORK_JOINED;
        mibReq.Param.IsNetworkJoined = false;
        if (LoRaMacMibSetRequestConfirm( &mibReq ) != LORAMAC_STATUS_OK) {
            TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, 500000);
            return;
        }
    }

    mibReq.Type = MIB_NETWORK_JOINED;
    status = LoRaMacMibGetRequestConfirm( &mibReq );

    isr_printf("OnTxNextPacketTimerEvent() ");
    if( status == LORAMAC_STATUS_OK )
    {
        if( mibReq.Param.IsNetworkJoined == true )
        {
            isr_printf("send");
            DeviceState = DEVICE_STATE_UPLINK;
        }
        else
        {
            isr_printf("join");
            DeviceState = DEVICE_STATE_JOIN;
        }
    }
    isr_printf("\r\n");
}

void
send_uplink()
{
    AppDataSize = uplink_length;
    DeviceState = DEVICE_STATE_UPLINK;
}

/*!
 * \brief   MCPS-Confirm event function
 *
 * \param   [IN] mcpsConfirm - Pointer to the confirm structure,
 *               containing confirm attributes.
 */
static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
{
    static uint8_t fail_count = 0;
    char str[64];
    
    isr_printf("McpsConfirm ");
    if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
    {
        fail_count = 0;
        isr_printf("OK ");
        switch( mcpsConfirm->McpsRequest )
        {
            case MCPS_UNCONFIRMED:
            {
                isr_printf("UNCONFIRMED");
                // Check Datarate
                // Check TxPower
                break;
            }
            case MCPS_CONFIRMED:
            {
                isr_printf("CONFIRMED");
                // Check Datarate
                // Check TxPower
                // Check AckReceived
                // Check NbTrials
                LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
                break;
            }
            case MCPS_PROPRIETARY:
            {
                break;
            }
            default:
                break;
        }
        //LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
        LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;

    } else {
        LoRaMacEventInfoStatus_to_string(mcpsConfirm->Status, str);
        isr_printf("%s ", str);

        /* mcpsIndication may not come. last uplink done, send another uplink */
        if (jumper_in.read()) {   /* jumper installed: auto uplink */
            TxNextPacketTimeout.attach_us(&send_uplink, 100000);
        }

        if (++fail_count > 10) {
            /* cause re-join */
            MibRequestConfirm_t mibReq;
            mibReq.Type = MIB_NETWORK_JOINED;
            mibReq.Param.IsNetworkJoined = false;
            LoRaMacMibSetRequestConfirm( &mibReq );
            fail_count = 0;
        }
    }

    isr_printf("\r\n");
}

/*!
 * \brief   MCPS-Indication event function
 *
 * \param   [IN] mcpsIndication - Pointer to the indication structure,
 *               containing indication attributes.
 */
static void McpsIndication( McpsIndication_t *mcpsIndication )
{
    char str[64];
    isr_printf("McpsIndication ");

    /* last uplink done, send another uplink */
    if (jumper_in.read() && mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_STOP) {
        /* jumper installed: auto uplink */
        TxNextPacketTimeout.attach_us(&send_uplink, 100000);
    }

    if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
    {
        LoRaMacEventInfoStatus_to_string(mcpsIndication->Status, str);
        isr_printf("%s\r\n", str);
        return;
    }

    awaiting_mcps_indic = false;

    switch( mcpsIndication->McpsIndication )
    {
        /* this refers to the downlink from gateway, not the uplink that was sent to it */
        case MCPS_UNCONFIRMED:
        {
            isr_printf("UNCONFIRMED ");
            break;
        }
        case MCPS_CONFIRMED:
        {
            /* downlink has requested Ack */
            isr_printf("CONFIRMED ");
            break;
        }
        case MCPS_PROPRIETARY:
        {
            break;
        }
        case MCPS_MULTICAST:
        {
            isr_printf("MCPS_MULTICAST ");
            /*if (mcpsIndication->RxData) {
            }*/
            break;
        }
        default:
            break;
    }

    // Check Multicast
    // Check Port
    // Check Datarate
    // Check FramePending
    // Check Buffer
    // Check BufferSize
    // Check Rssi
    // Check Snr
    // Check RxSlot
    LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
    if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
    {
        // Invert and divide by 4
        LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
        LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
    }
    else
    {
        // Divide by 4
        LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
    }
    LoRaMacDownlinkStatus.DownlinkCounter++;
    LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
    LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
    LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
    LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;

    if( mcpsIndication->RxData == true )
    {
        int i;
        isr_printf("RxData %u ", mcpsIndication->BufferSize);
        for (i = 0; i < mcpsIndication->BufferSize; i++) {
            isr_printf("%02x ", mcpsIndication->Buffer[i]);
        }
        isr_printf("\r\n");
        
#if defined(SENSORS) && defined(ENABLE_SX1272)        
        switch (mcpsIndication->Buffer[0]) {
            default:
            case CMD_NONE: break;
            case CMD_LED_RGB:
                rgb.setColorRGB(0, 
                    mcpsIndication->Buffer[1],  // R
                    mcpsIndication->Buffer[2],  // G
                    mcpsIndication->Buffer[3]   // B
                );
                isr_printf("rgb %u %u %u\r\n",
                    mcpsIndication->Buffer[1],
                    mcpsIndication->Buffer[2],
                    mcpsIndication->Buffer[3]
                );
                break;
            case CMD_GPIO_OUT:
                pc6_out = mcpsIndication->Buffer[1];
                isr_printf("gpo %d\r\n", mcpsIndication->Buffer[1]);
                break;
            case CMD_PWM:
                pwm.period(1.0 / mcpsIndication->Buffer[1]);
                pwm.write(mcpsIndication->Buffer[2] / 255.0);
                isr_printf("pwm %u %u\r\n", mcpsIndication->Buffer[1], mcpsIndication->Buffer[2]);
                
                break;
        } // ..switch (mcpsIndication->Buffer[3])        
#endif /* SENSORS && ENABLE_SX1272 */

        switch( mcpsIndication->Port )
        {
        case 1: // The application LED can be controlled on port 1 or 2
        case 2:
            if( mcpsIndication->BufferSize == 1 )
            {
                AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
                //Led3StateChanged = true;
            }
            break;
        default:
            break;
        }
    }
    
    if (mcpsIndication->Status == LORAMAC_EVENT_INFO_STATUS_STOP) {
        isr_printf(" STOP-SCC");
        TxNextPacketTimeout.detach();
        DeviceState = DEVICE_STATE_SLEEP;
    }
    
    isr_printf("\r\n");
}

/*!
 * \brief   MLME-Confirm event function
 *
 * \param   [IN] mlmeConfirm - Pointer to the confirm structure,
 *               containing confirm attributes.
 */
static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
{
    char str[64];
    LoRaMacEventInfoStatus_to_string(mlmeConfirm->Status, str);
    isr_printf("MlmeConfirm %s ", str);

    switch( mlmeConfirm->MlmeRequest )
    {
        case MLME_JOIN:
        {
            isr_printf("MLME_JOIN ");
            if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
            {
                // Status is OK, node has joined the network
                DeviceState = DEVICE_STATE_SLEEP;
                missed_count = 0;
                if (jumper_in.read())  /* jumper installed: auto uplink */
                    TxNextPacketTimeout.attach_us(&send_uplink, 100000);            
            }
            else
            {
                // Join was not successful. Try to join again
                DeviceState = DEVICE_STATE_JOIN;
            }
            break;
        }
        case MLME_LINK_CHECK:
        {
            isr_printf("LINK_CHECK");
            if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
            {
                // Check DemodMargin
                //mlmeConfirm->DemodMargin;
                // Check NbGateways
                //mlmeConfirm->NbGateways;
            }
            break;
        }
        default:
            break;
    }

    isr_printf("\r\n");
}


static void MlmeIndication( MlmeIndication_t *MlmeIndication )
{
    char str[64];

    isr_printf("MlmeIndication ");
    switch( MlmeIndication->MlmeIndication )
    {
        case MLME_BEACON:
        {
            isr_printf("missed:%u BEACON ", missed_count);
            LoRaMacEventInfoStatus_to_string(MlmeIndication->Status, str);
            isr_printf("%s ", str);

            if (send_at_beacon) {
                TxNextPacketTimeout.attach_us(&send_uplink, 100000);
                send_at_beacon = false;
            }

            if (LORAMAC_EVENT_INFO_STATUS_BEACON_LOCKED == MlmeIndication->Status)
                missed_count = 0;
            else {
                if (++missed_count > 4) {
                    /* cause re-join */
                    LoRaMacStatus_t status;
                    MibRequestConfirm_t mibReq;
                    mibReq.Type = MIB_NETWORK_JOINED;
                    mibReq.Param.IsNetworkJoined = false;
                    status = LoRaMacMibSetRequestConfirm(&mibReq);
                    if (status != LORAMAC_STATUS_OK)
                        DeviceState = DEVICE_STATE_REJOIN;
    
                    LoRaMacStatus_to_string(status, str);
                    isr_printf("app-rejoin %s\r\n", str);
                    TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);
                }
            }
            break;

        }
        case MLME_TXDONE:
            isr_printf("MLME_TXDONE ");
            break;
        default:
            isr_printf("<%d> ", MlmeIndication->MlmeIndication);
            break;
    }

    isr_printf("\r\n");
}

void
send_pcbuf_uplink()
{
    bool ret;
    memcpy(AppData, pcbuf, pcbuf_len);
    AppDataSize = pcbuf_len;

    ret = SendFrame(TEXT_PORT);
    isr_printf("%d = SendFrame()\r\n", ret);
}


void cmd_status(uint8_t idx)
{
    MibRequestConfirm_t mibReq;
    
    isr_printf("DevEUI ");
    for (int i = 0; i < 8; i++)
        isr_printf("%02x ", DevEui[i]);
    mibReq.Type = MIB_DEV_ADDR;
    LoRaMacMibGetRequestConfirm( &mibReq );         
    isr_printf(", DevAddr:%x\r\n", mibReq.Param.DevAddr);
    
#if defined(SENSORS) && defined(ENABLE_SX1272)
    isr_printf("a1:%u a3:%u\r\n", a1.read_u16(), a3.read_u16());
    isr_printf("button:%d,%u ", user_button.read(), button_cnt);
#endif
    isr_printf("DEVICE_STATE_");
    switch (DeviceState) {
        case DEVICE_STATE_JOIN: printf("JOIN"); break;
        case DEVICE_STATE_CYCLE: printf("CYCLE"); break;
        case DEVICE_STATE_SLEEP: printf("SLEEP"); break;
        case DEVICE_STATE_REJOIN: printf("REJOIN"); break;
        case DEVICE_STATE_INIT: printf("INIT"); break;
        case DEVICE_STATE_UPLINK: printf("UPLINK"); break;
    }
    isr_printf(" send_at_beacon:%d\r\n", send_at_beacon);
    isr_printf("awaiting_mcps_indic:%d\r\n", awaiting_mcps_indic);
    
    loramac_print_status();

}

void cmd_uplink_length(uint8_t idx)
{
    if (pcbuf[idx] >= '0' && pcbuf[idx] <= '9') {
        sscanf(pcbuf+idx, "%u", &uplink_length);
    }
    isr_printf("uplink_length:%u\r\n", uplink_length);
}

#if defined(SENSORS) && defined(ENABLE_SX1272)
void cmd_rgb(uint8_t idx)
{
    int r, g, b;
    sscanf(pcbuf+idx, "%d %d %d", &r, &g, &b);
    rgb.setColorRGB(0, r, g, b);
    isr_printf("\r\nrgb: %d %d %d\r\n", r, g, b);
}

void cmd_pwm(uint8_t idx)
{
    float period, duty;
    unsigned p, d;

    if (sscanf(pcbuf+idx, "%u %u", &p, &d) == 2) {
        period = 1.0 / p;
        duty = d / 255.0;
        pwm.period(period);
        pwm.write(duty);
        printf("pwm period:%f, duty:%f\r\n", period, duty);
    } else
        printf("pwm parse fail\r\n");
}

#endif /* SENSORS */

void cmd_ChannelsTxPower(uint8_t idx)
{
    MibRequestConfirm_t mibReq;    
    unsigned int i;
    
    if (pcbuf[idx] >= '0' && pcbuf[idx] <= '9') {
        sscanf(pcbuf+idx, "%u", &i);
        mibReq.Type = MIB_CHANNELS_TX_POWER;
        mibReq.Param.ChannelsTxPower = i;
        LoRaMacMibSetRequestConfirm( &mibReq );        
    }
    
    mibReq.Type = MIB_CHANNELS_TX_POWER;
    LoRaMacMibGetRequestConfirm( &mibReq );
    isr_printf("ChannelsTxPower:%u\r\n", mibReq.Param.ChannelsTxPower);
}

typedef struct {
    const char* const cmd;
    void (*handler)(uint8_t args_at);
    const char* const arg_descr;
    const char* const description;
} menu_item_t;

void cmd_help(uint8_t args_at);

const menu_item_t menu_items[] = 
{   /* after first character, command names must be [A-Za-z] */
    { "?", cmd_help, "","show available commands"},
    { ".", cmd_status, "","print status"}, 
    { "ul", cmd_uplink_length, "%u","set uplink payload length"}, 
    { "ctxp", cmd_ChannelsTxPower, "%u","get/set ChannelsTxPower"},
#if defined(SENSORS) && defined(ENABLE_SX1272)
    { "rgb", cmd_rgb, "%d %d %d", "set led R G B"},
    { "p", cmd_pwm, "%u %u", "set pwm period, duty"},
#endif
    { NULL, NULL, NULL, NULL }
};

void cmd_help(uint8_t args_at)
{
    int i;
    
    for (i = 0; menu_items[i].cmd != NULL ; i++) {
        isr_printf("%s%s\t%s\r\n", menu_items[i].cmd, menu_items[i].arg_descr, menu_items[i].description);
    }
    
}

void
console()
{
    bool parsed;
    int i;
    uint8_t user_cmd_len;
    
    if (pcbuf_len < 0) {    // ctrl-C
        return;
    }
    if (pcbuf_len == 0)
        return;
        
    isr_printf("\r\n");
        
    /* get end of user-entered command */
    user_cmd_len = 1;   // first character can be any character
    for (i = 1; i <= pcbuf_len; i++) {
        if (pcbuf[i] < 'A' || (pcbuf[i] > 'Z' && pcbuf[i] < 'a') || pcbuf[i] > 'z') {
            user_cmd_len = i;
            break;
        }
    }

    parsed = false;
    for (i = 0; menu_items[i].cmd != NULL ; i++) {
        int mi_len = strlen(menu_items[i].cmd);

        if (menu_items[i].handler && user_cmd_len == mi_len && (strncmp(pcbuf, menu_items[i].cmd, mi_len) == 0)) {
            while (pcbuf[mi_len] == ' ')   // skip past spaces
                mi_len++;
            menu_items[i].handler(mi_len);
            parsed = true;
            break;
        }
    }

    if (!parsed)
        send_pcbuf_uplink();
   
    pcbuf_len = 0;
    isr_printf("> ");
    fflush(stdout); 
}

void rx_callback()
{
    static uint8_t pcbuf_idx = 0;
    static uint8_t prev_len = 0;;
    char c = pc.getc();

    if (c == 8) {
        if (pcbuf_idx > 0) {
            pc.putc(8);
            pc.putc(' ');
            pc.putc(8);
            pcbuf_idx--;
        }
    } else if (c == 3) {    // ctrl-C
        pcbuf_len = -1;
    } else if (c == '\r') {
        if (pcbuf_idx == 0) {
            pcbuf_len = prev_len;
        } else {
            pcbuf[pcbuf_idx] = 0;   // null terminate
            prev_len = pcbuf_idx;
            pcbuf_idx = 0;
            pcbuf_len = prev_len;
        }
    } else if (pcbuf_idx < sizeof(pcbuf)) {
        pcbuf[pcbuf_idx++] = c;
        pc.putc(c);
    }
}


#if defined(SENSORS) && defined(ENABLE_SX1272)
void button_isr()
{
    button_cnt++;
    isr_printf("button_isr\r\n");

    AppPort = SENSOR_PORT;
    send_uplink();
}
#endif /* SENSORS */

/**
 * Main application entry point.
 */
int main( void )
{
    LoRaMacPrimitives_t LoRaMacPrimitives;
    LoRaMacCallback_t LoRaMacCallbacks;
    MibRequestConfirm_t mibReq;

    pc.baud(38400);
    pc.attach(&rx_callback);
    isr_printf("\r\nreset %s\r\n", FW_VERS);
    
    BoardInit( );

    DeviceState = DEVICE_STATE_INIT;
    
#if defined(SENSORS) && defined(ENABLE_SX1272)
    while (!user_button) {
        printf("button-lo\r\n");
        wait(0.01);
    }
    user_button.fall(&button_isr);
    
    isr_printf("TSL2561 Sensor ");
    if (tsl2561.begin()) {    
        isr_printf("Found\r\n");        
    } else {    
        isr_printf("not-found\r\n");   
    }
    
    // You can change the gain on the fly, to adapt to brighter/dimmer tsl2561 situations
    tsl2561.setGain(TSL2561_GAIN_0X);         // set no gain (for bright situtations)
    //tsl2561.setGain(TSL2561_GAIN_16X);      // set 16x gain (for dim situations)
    
    // Changing the integration time gives you a longer time over which to sense tsl2561
    // longer timelines are slower, but are good in very low tsl2561 situtations!
    //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright tsl2561)
    //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium tsl2561)
    tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim tsl2561)    
#endif /* SENSORS && ENABLE_SX1272 */

    jumper_out = 1;
    jumper_in.mode(PullDown);
    pc2 = 0;

    while( 1 )
    {
        console();
        
        switch( DeviceState )
        {
            case DEVICE_STATE_INIT:
            {
                isr_printf("DEVICE_STATE_INIT\r\n");
#if defined(SENSORS) && defined(ENABLE_SX1272)
                uplink_length = 7;
#else                
                uplink_length = 2;
#endif
                LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
                LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
                LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
                LoRaMacPrimitives.MacMlmeIndication = MlmeIndication;
                LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
                if (LORAMAC_STATUS_OK != LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks )) {
                    isr_printf("LoRaMacInitialization() failed\r\n");
                    for (;;) ;
                }
                isr_printf("INIT-ok\r\n");

                mibReq.Type = MIB_PUBLIC_NETWORK;
                mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
                LoRaMacMibSetRequestConfirm( &mibReq );

                LoRaMacDownlinkStatus.DownlinkCounter = 0;

                DeviceState = DEVICE_STATE_JOIN;
                break;
            }
            case DEVICE_STATE_JOIN:
            {
                if (jumper_in.read())   /* if jumper installed: auto uplink */
                    send_at_beacon = true;
#if( OVER_THE_AIR_ACTIVATION != 0 )
                MlmeReq_t mlmeReq;
                // override software definition with hardware value
                BoardGetUniqueId(DevEui);
                isr_printf("DevEUI ");
                for (int i = 0; i < 8; i++)
                    isr_printf("%02x ", DevEui[i]);
                isr_printf("\r\n");

                mlmeReq.Type = MLME_JOIN;

                mlmeReq.Req.Join.DevEui = DevEui;
                mlmeReq.Req.Join.AppEui = AppEui;
                mlmeReq.Req.Join.AppKey = AppKey;
                mlmeReq.Req.Join.NbTrials = 255;

                LoRaMacStatus_t status = LoRaMacMlmeRequest( &mlmeReq );
                if (status != LORAMAC_STATUS_OK) {
                    char str[48];
                    LoRaMacStatus_to_string(status, str);
                    isr_printf("join-req-failed:%s\r\n", str);
                }
                DeviceState = DEVICE_STATE_SLEEP;
#else
                mibReq.Type = MIB_NET_ID;
                mibReq.Param.NetID = LORAWAN_NETWORK_ID;
                LoRaMacMibSetRequestConfirm( &mibReq );

                mibReq.Type = MIB_DEV_ADDR;
                mibReq.Param.DevAddr = DevAddr;
                LoRaMacMibSetRequestConfirm( &mibReq );

                mibReq.Type = MIB_NWK_SKEY;
                mibReq.Param.NwkSKey = NwkSKey;
                LoRaMacMibSetRequestConfirm( &mibReq );

                mibReq.Type = MIB_APP_SKEY;
                mibReq.Param.AppSKey = AppSKey;
                LoRaMacMibSetRequestConfirm( &mibReq );

                mibReq.Type = MIB_NETWORK_JOINED;
                mibReq.Param.IsNetworkJoined = true;
                LoRaMacMibSetRequestConfirm( &mibReq );
#endif
                join_retry = false;
                break;
            }
            case DEVICE_STATE_CYCLE:
            {
                DeviceState = DEVICE_STATE_SLEEP;

                // Schedule next packet transmission
                TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, TxDutyCycleTime * 1000);
                break;
            }
            case DEVICE_STATE_SLEEP:
            {
                bottom_half();
                LoRaMacBottomHalf();
                // Wake up through events
                pc2 = 1;
                sleep();
                pc2 = 0;
                break;
            }
            case DEVICE_STATE_UPLINK:
                PrepareTxFrame(AppPort);
                SendFrame(AppPort);
                DeviceState = DEVICE_STATE_SLEEP;
                break;
            default:
            {
                DeviceState = DEVICE_STATE_INIT;
                break;
            }
        }

        bottom_half();
        LoRaMacBottomHalf();

        if (join_retry) {
            isr_printf("join_retry\r\n");
            DeviceState = DEVICE_STATE_JOIN;
            join_retry = false;
        }

    } // ..while( 1 )
}