abc

Dependencies:   LMiC SX1276Lib mbed

Fork of LoRaWAN-lmic-app by Semtech

main.cpp

Committer:
mluis
Date:
2015-01-22
Revision:
0:a2929fa6e4f0
Child:
1:60184eda0066

File content as of revision 0:a2929fa6e4f0:

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

Description: MBED example application

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

Maintainer: Miguel Luis and Gregory Cristian
*/
#include "lmic.h"

/*!
 * When set to 1 the application uses the Over-the-Air activation procedure
 * When set to 0 the application uses the Personalization activation procedure
 */
#define OVER_THE_AIR_ACTIVATION                     0

#define APP_DATA_SIZE                               1

//////////////////////////////////////////////////
// CONFIGURATION (FOR APPLICATION CALLBACKS BELOW)
//////////////////////////////////////////////////

// application router ID (LSBF)
static const u1_t AppEui[8] =
{
    0xAA, 0xCC, 0x11, 0x00, 0xCC, 0xEE, 0x77, 0xEE
};

// unique device ID (LSBF)
static const u1_t DevEui[8] =
{
    0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
};

// device-specific AES key (derived from device EUI)
static const u1_t DevKey[16] =
{
    0xAB, 0x89, 0xEF, 0xCD, 0x23, 0x01, 0x67, 0x45,
    0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA
};

static uint8_t NwkSKey[] = 
{ 
    0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
    0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
};

static uint8_t ArtSKey[] = 
{ 
    0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
    0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
};

// LEDs and Frame jobs
osjob_t rxLedJob;
osjob_t txLedJob;
osjob_t sendFrameJob;

static bool AppLedStateOn = false;

//////////////////////////////////////////////////
// APPLICATION CALLBACKS
//////////////////////////////////////////////////

// provide application router ID (8 bytes, LSBF)
void os_getArtEui( u1_t* buf )
{
    memcpy( buf, AppEui, 8 );
}

// provide device ID (8 bytes, LSBF)
void os_getDevEui( u1_t* buf )
{
    memcpy( buf, DevEui, 8 );
}

// provide device key (16 bytes)
void os_getDevKey( u1_t* buf )
{
    memcpy( buf, DevKey, 16 );
}

//////////////////////////////////////////////////
// MAIN - INITIALIZATION AND STARTUP
//////////////////////////////////////////////////

// Initialization job
static void onInit( osjob_t* j )
{
    // reset MAC state
    LMIC_reset( );
    LMIC_setAdrMode( 1 );
    LMIC_setDrTxpow( DR_FSK, 14 );
    // start joining
#if( OVER_THE_AIR_ACTIVATION != 0 )
    LMIC_startJoining( );
#else    
    LMIC_startABP( 0, 0x44332211, NwkSKey, ArtSKey );
#endif
    // init done - onEvent() callback will be invoked...
}

static void onRxLed( osjob_t* j )
{
    DEBUG_VAL("LED2 = ", 1 );
}

static void onTxLed( osjob_t* j )
{
    DEBUG_VAL("LED1 = ", 1 );
}

static void prepareTxFrame( void )
{
    LMIC.frame[0] = AppLedStateOn;
}

void processRxFrame( void )
{
    switch( LMIC.frame[LMIC.dataBeg - 1] ) // Check Rx port number
    {
        case 1: // The application LED can be controlled on port 1 or 2
        case 2:
            if( LMIC.dataLen == 1 )
            {
                AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01;
                DEBUG_VAL( "LED3 = ", AppLedStateOn ? 0 : 1 );
            }
            break;
        default:
            break;
    }
}

static void onSendFrame( osjob_t* j )
{
    prepareTxFrame( );
    LMIC_setTxData2( 1, LMIC.frame, APP_DATA_SIZE, 1 );
}

int main(void)
{
    osjob_t initjob;

    // initialize runtime env
    os_init( );
    // setup initial job
    os_setCallback( &initjob, onInit );
    // execute scheduled jobs and events
    os_runloop( );
    // (not reached)
}

//////////////////////////////////////////////////
// LMIC EVENT CALLBACK
//////////////////////////////////////////////////
void onEvent( ev_t ev )
{
    bool txOn = false;

    DEBUG_EVENT( ev );

    switch( ev ) 
    {
    // network joined, session established
    case EV_JOINED:
        DEBUG_VAL( "Net ID = ", LMIC.netid );
        txOn = true;
        break;
    // scheduled data sent (optionally data received)
    case EV_TXCOMPLETE:
        // Check if we have a downlink on either Rx1 or Rx2 windows
        if( ( LMIC.txrxFlags & ( TXRX_DNW1 | TXRX_DNW2 ) )!= 0 )
        {
            DEBUG_VAL( "LED2 = ", 0 );
            os_setTimedCallback( &rxLedJob, os_getTime( ) + ms2osticks( 15 ), onRxLed );

            if( LMIC.dataLen != 0 ) 
            { // data received in rx slot after tx
                DEBUG_BUF( LMIC.frame + LMIC.dataBeg, LMIC.dataLen );
                processRxFrame( );
            }
        }
        txOn = true;
        break;
    default:
        break;
    }
    if( txOn == true )
    {
        //os_setTimedCallback( &sendFrameJob, os_getTime( ) + sec2osticks( 5 ), onSendFrame );
        onSendFrame( NULL );
        
        // Blink Tx LED
        DEBUG_VAL( "LED1 = ", 0 );
        os_setTimedCallback( &txLedJob, os_getTime( ) + ms2osticks( 25 ), onTxLed );
    }
}