This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/LAUNCHXL-CC1350: Encryption and rfpackettx not working

Part Number: LAUNCHXL-CC1350

Tool/software: TI-RTOS

Hello All,

I am trying to encrypt my packet data and then i am trying to send that encrypted data over the air. The program is not showing any errors but still packet is not getting transmitted. I am attaching the .c file here. Can anyone please check and let me know what is wrong with this program or why packet is not getting transmitted. There are no errors in this program.

/*
 * Copyright (c) 2015-2016, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/***** Includes *****/
#include <stdlib.h>
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>

#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>

/* Drivers */
#include <ti/drivers/rf/RF.h>
#include <ti/drivers/crypto/CryptoCC26XX.h>
#include <ti/drivers/PIN.h>
#include <ti/drivers/UART.h>

/* Board Header files */
#include "Board.h"

//#include "RFQueue.h"
#include "smartrf_settings/smartrf_settings.h"

#include <stdlib.h>

/* Pin driver handle */
static PIN_Handle ledPinHandle;
static PIN_State ledPinState;

/*
 * Application LED pin configuration table:
 *   - All LEDs board LEDs are off.
 */
PIN_Config pinTable[] =
{
    Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    PIN_TERMINATE
};



/***** Defines *****/

#define TX_TASK_STACK_SIZE 1024
#define TX_TASK_PRIORITY   3

#define AES_TASK_STACK_SIZE 1024
#define AES_TASK_PRIORITY   2


#define UART_TX_TASK_STACK_SIZE 1024
#define UART_TX_TASK_PRIORITY   1

/* ASCII values of some useful keys */
#define CHAR_LINEFEED                         0x0A
#define CHAR_LINE_END_1                       0x0D   // Enter
#define CHAR_LINE_END_2                       0x03   // Enter on numpad
#define CHAR_SPACE                            0x20
#define CHAR_ZERO                             0x30
#define CHAR_UPPERCASE_START                  0x40

/* Encryption Defines */

#define macLength           (4)
#define clearTextLength     (16)
#define cipherTextLength    (macLength + clearTextLength)
#define nonceLength         (12)
#define aadLength           (14)


/* TX Configuration */
#define PAYLOAD_LENGTH      20
#define PACKET_INTERVAL     (uint32_t)(4000000*0.5f) /* Set packet interval to 500ms */


// Holds the AES-CCM setup for this example
typedef struct
{
    uint8_t key[16];                                // A 128 Bit AES key
    CryptoCC26XX_KeyLocation keyLocation;           // One of 8 key locations in the hardware
    uint8_t clearAndCipherText[cipherTextLength];   // Holds the cleartext before, and the ciphertext
                                                    // after the encryption operation.
                                                    // Ciphertext = encrypted text + message authentication code (MAC).
    uint8_t nonce[nonceLength];  // A value that is used only once (cryptographic term 'nonce')
    uint8_t header[aadLength];   // A header that is not encrypted but is authenticated in the operation (AAD).
} AesCcmExample;

AesCcmExample ccmSetup =
{
    .key = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
             0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C },
    .keyLocation = CRYPTOCC26XX_KEY_0,
    .clearAndCipherText = { 't','h','i','s','i','s','a','p','l','a','i','n','t','e','x','t','0','0','0','0' },
   // .clearAndCipherText = txPacket[0],
    .nonce  = { 't','h','i','s','i','s','a','n','o','n','c','e' },
    .header = { 't','h','i','s','i','s','a','h','e','a','d','e','r','1' }
};


/***** Prototypes *****/
static void txTaskFunction(UArg arg0, UArg arg1);
static void aesTaskFunction(UArg arg0, UArg arg1);
static void uartTxTaskFunction(UArg arg0, UArg arg1);

/***** Variable declarations *****/
static Task_Params txTaskParams;
Task_Struct txTask;    /* not static so you can see in ROV */
static uint8_t txTaskStack[TX_TASK_STACK_SIZE];

static Task_Params uartTxTaskParams;
Task_Struct uartTxTask;    /* not static so you can see in ROV */
static uint8_t uartTxTaskStack[UART_TX_TASK_STACK_SIZE];

static Task_Params aesTaskParams;
Task_Struct aesTask;
static uint8_t aesTxTaskStack[AES_TASK_STACK_SIZE];

Semaphore_Struct semTxStruct;
Semaphore_Handle semTxHandle;

Semaphore_Struct semAesStruct;
Semaphore_Handle semAesHandle;


static RF_Object rfObject;
static RF_Handle rfHandle;
static RF_CmdHandle rfTxCmd;

UART_Handle uart = NULL;
UART_Params uartParams;

uint32_t time;
static uint8_t txPacket[PAYLOAD_LENGTH];
static PIN_Handle pinHandle;

/***** Function Initailizations *****/

void UartTxTask_init(PIN_Handle ledPinHandle) {
    pinHandle = ledPinHandle;

    Task_Params_init(&uartTxTaskParams);
    uartTxTaskParams.stackSize = UART_TX_TASK_STACK_SIZE;
    uartTxTaskParams.priority = UART_TX_TASK_PRIORITY;
    uartTxTaskParams.stack = &uartTxTaskStack;
    uartTxTaskParams.arg0 = (UInt)1000000;

    Task_construct(&uartTxTask, uartTxTaskFunction, &uartTxTaskParams, NULL);
}

void AesTask_init(PIN_Handle inPinHandle)
{
    pinHandle = inPinHandle;

    Task_Params_init(&uartTxTaskParams);
    uartTxTaskParams.stackSize = AES_TASK_STACK_SIZE;
    uartTxTaskParams.priority = AES_TASK_PRIORITY;
    uartTxTaskParams.stack = &aesTxTaskStack;
    uartTxTaskParams.arg0 = (UInt)1000000;

    Task_construct(&aesTask, aesTaskFunction, &aesTaskParams, NULL);
}


void TxTask_init(PIN_Handle inPinHandle)
{
    pinHandle = inPinHandle;

    Task_Params_init(&txTaskParams);
    txTaskParams.stackSize = TX_TASK_STACK_SIZE;
    txTaskParams.priority = TX_TASK_PRIORITY;
    txTaskParams.stack = &txTaskStack;
    txTaskParams.arg0 = (UInt)1000000;

    Task_construct(&txTask, txTaskFunction, &txTaskParams, NULL);
}

/***** Function definitions *****/

#define UART_TX_BUFFER_SIZE 256
char uartTxBuffer[UART_TX_BUFFER_SIZE];

static void uartTxTaskFunction(UArg arg0, UArg arg1)
{
    char input;
    uint8_t charIndex = 0;

    /* Open UART if not already open */
    if (uart == NULL) {
        /* Create a UART with data processing off. */
        UART_Params_init(&uartParams);
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.readReturnMode = UART_RETURN_FULL;
        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.baudRate = 115200;
        uart = UART_open(Board_UART0, &uartParams);

        if (uart == NULL) {
            System_abort("Error opening the UART");
        }
    }

    while (1) {

        UART_read(uart, &input, sizeof(input));
        UART_write(uart, &input, sizeof(input));


        /* Skip the line feed that appears after an enter as the first character of new line */
//        if (input == (char)CHAR_LINEFEED)
  //          charIndex--;


        if ((input == (char)CHAR_LINE_END_1) | (input == (char)CHAR_LINE_END_2)) // (charIndex < UART_SERIAL_LINE_SIZE))
        {

            /* Cancel Rx */
        //    Semaphore_post(semTxHandle);
            Semaphore_post(semAesHandle);
            /* reset index to zero to point to begining of the line */
            charIndex = 0;
        }
        else
        {
            /* Store the input character */
            txPacket[charIndex++] = input;
        }

    }
}


static void aesTaskFunction(UArg arg0, UArg arg1)
{
	int i = 0;
	for(i=0; i<20; i++){

		ccmSetup.clearAndCipherText[i] = txPacket[i];

	}

	CryptoCC26XX_Handle             handle;
	int32_t                         keyIndex;
	CryptoCC26XX_AESCCM_Transaction trans;
	int32_t                         status;

	// Initialize Crypto driver structures
	CryptoCC26XX_init();

	// Open the crypto hardware with non-exclusive access and default parameters.
	handle = CryptoCC26XX_open(Board_CRYPTO, false, NULL);
	if (handle == NULL) {
	    System_abort("CryptoCC26XX did not open");
	}

	// Allocate a key storage location in the hardware
	keyIndex = CryptoCC26XX_allocateKey(handle, ccmSetup.keyLocation, (const uint32_t *) ccmSetup.key);
	if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) {
	    System_abort("Key Location was not allocated.");
	}

    Semaphore_pend(semAesHandle, BIOS_WAIT_FOREVER);

	// Encrypt and authenticate the message
	CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_CCM);
	trans.keyIndex   = keyIndex;
	trans.authLength = macLength;
	trans.nonce  = (char *) ccmSetup.nonce;
	trans.header = (char *) ccmSetup.header;
	trans.fieldLength  = 3;
	trans.msgInLength  = clearTextLength;
	trans.headerLength = aadLength;
	trans.msgIn  = (char *) &(ccmSetup.clearAndCipherText[0]);                // Message is encrypted in place
//	trans.msgIn  = (char *) &(ccmSetup.txPacket[0]);                // Message is encrypted in place
	trans.msgOut = (char *) &(ccmSetup.clearAndCipherText[clearTextLength]);  // MAC will be written to this position
	status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *) &trans);

	// Do AES-CCM Encryption and authentication
	if (status != CRYPTOCC26XX_STATUS_SUCCESS) {
	    System_abort("Encryption and signing failed.");
	}
	// Decrypt and authenticate message
/*	CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_CCMINV);
	trans.keyIndex   = keyIndex;
	trans.authLength = macLength;
	trans.nonce  = (char *) ccmSetup.nonce;
	trans.header = (char *) ccmSetup.header;
	trans.fieldLength  = 3;
	trans.msgInLength  = cipherTextLength;
	trans.headerLength = aadLength;
	trans.msgIn  = (char *) &(ccmSetup.clearAndCipherText[0]);                // Message is decrypted in place
	trans.msgOut = (char *) &(ccmSetup.clearAndCipherText[clearTextLength]);  // Points to the MAC, is used as input here

	// Do AES-CCM decryption and authentication
	status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *) &trans);
	if(status != CRYPTOCC26XX_STATUS_SUCCESS){
	    System_abort("Decryption and authentication failed.");
	}*/

	Semaphore_post(semTxHandle);

	// Release the key location
	status = CryptoCC26XX_releaseKey(handle, &keyIndex);
	if (status != CRYPTOCC26XX_STATUS_SUCCESS) {
	    System_abort("Key release was not successful.");
	}

	//Semaphore_post(semTxHandle);
}


static void txTaskFunction(UArg arg0, UArg arg1)
{
    uint32_t time;
    RF_Params rfParams;
    RF_Params_init(&rfParams);

    RF_cmdPropTx.pktLen = PAYLOAD_LENGTH;
   // RF_cmdPropTx.pPkt = txPacket;
    RF_cmdPropTx.pPkt = ccmSetup.clearAndCipherText;
    RF_cmdPropTx.startTrigger.triggerType = TRIG_ABSTIME;
    RF_cmdPropTx.startTrigger.pastTrig = 1;
    RF_cmdPropTx.startTime = 0;

    while(1)
    {
        Semaphore_pend(semTxHandle, BIOS_WAIT_FOREVER);

        if (!rfHandle) {
            /* Request access to the radio */
            rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);

            /* Set the frequency */
            RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
        }

        /* Get current time */
        time = RF_getCurrentTime();

        /* Set absolute TX time to utilize automatic power management */
        time += PACKET_INTERVAL;
        RF_cmdPropTx.startTime = time;

        /* Send packet */
        rfTxCmd = RF_postCmd(rfHandle, (RF_Op*)&RF_cmdPropTx, RF_PriorityNormal, NULL, 0);

        RF_EventMask result = RF_pendCmd(rfHandle, rfTxCmd, (RF_EventCmdDone | RF_EventCmdError | RF_EventLastCmdDone |
                RF_EventCmdAborted | RF_EventCmdCancelled | RF_EventCmdStopped));

        if (!(result & RF_EventLastCmdDone))
        {
            /* Error */
            while(1);
        }

        PIN_setOutputValue(pinHandle, Board_LED1,!PIN_getOutputValue(Board_LED1));

        /* clear txPacket buffer */
//        memset(txPacket, 0, sizeof(txPacket));
        memset(ccmSetup.clearAndCipherText, 0, sizeof(ccmSetup.clearAndCipherText));

    }
}

/*
 *  ======== main ========
 */
int main(void)
{
    Semaphore_Params semParams;

    /* Call board init functions. */
    Board_initGeneral();
    Board_initUART();

    /* Open LED pins */
    ledPinHandle = PIN_open(&ledPinState, pinTable);
    if(!ledPinHandle)
    {
        System_abort("Error initializing board LED pins\n");
    }

    /* Construct a Semaphore object to be used as a resource lock, initial count 0 */
    Semaphore_Params_init(&semParams);
    Semaphore_construct(&semTxStruct, 0, &semParams);
    Semaphore_construct(&semAesStruct, 0, &semParams);


    /* Obtain instance handle */
    semTxHandle = Semaphore_handle(&semTxStruct);
    semAesHandle = Semaphore_handle(&semAesStruct);
    /* Initialize task */
    TxTask_init(ledPinHandle);

    UartTxTask_init(ledPinHandle);

    AesTask_init(ledPinHandle);

    /* Start BIOS */
    BIOS_start();

    return (0);
}

  • Inside AesTask_init, you are initializing uartTxTaskParams instead of aesTaskParams. Because aesTaskParams is uninitialized, the aes task is priority 0, meaning it is idle behind the uart task. Changing this in AesTask_init should solve the issue.

    Your aes task function doesn't contain a while loop either - you should put a while loop around the Semaphore_pend and the code afterwards if you want to continuously send packets.

    Skyler
  • Thank You Sir.
  • Hello Sir,

    Thanks a lot for looking into my code and telling about what the bug is. Now i am not able to decrypt back that data. Can You please look what I am doing wrong in this. Please help me with this sir.

    /*
     * Copyright (c) 2015-2016, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /***** Includes *****/
    #include <stdlib.h>
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>
    
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #include <ti/sysbios/knl/Semaphore.h>
    
    /* Drivers */
    #include <ti/drivers/rf/RF.h>
    #include <ti/drivers/crypto/CryptoCC26XX.h>
    #include <ti/drivers/PIN.h>
    #include <driverlib/rf_prop_mailbox.h>
    #include <ti/drivers/UART.h>
    
    /* Board Header files */
    #include "Board.h"
    
    #include "RFQueue.h"
    #include "smartrf_settings/smartrf_settings.h"
    
    #include <stdlib.h>
    
    /* Pin driver handle */
    static PIN_Handle ledPinHandle;
    static PIN_State ledPinState;
    
    /*
     * Application LED pin configuration table:
     *   - All LEDs board LEDs are off.
     */
    PIN_Config pinTable[] =
    {
        Board_LED2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        PIN_TERMINATE
    };
    
    
    /* Packet RX Configuration */
    #define DATA_ENTRY_HEADER_SIZE 8  /* Constant header size of a Generic Data Entry */
    #define MAX_LENGTH             30 /* Max length byte the radio will accept */
    #define NUM_DATA_ENTRIES       2  /* NOTE: Only two data entries supported at the moment */
    #define NUM_APPENDED_BYTES     2  /* The Data Entries data field will contain:
                                       * 1 Header byte (RF_cmdPropRx.rxConf.bIncludeHdr = 0x1)
                                       * Max 30 payload bytes
                                       * 1 status byte (RF_cmdPropRx.rxConf.bAppendStatus = 0x1) */
    
    
    /***** Defines *****/
    #define UARTCC26XX_CMD_RX_FIFO_FLUSH            UART_CMD_RESERVED + 2
    
    
    #define RX_TASK_STACK_SIZE 1024
    #define RX_TASK_PRIORITY   1
    
    #define AES_TASK_STACK_SIZE 1024
    #define AES_TASK_PRIORITY  2
    
    #define UART_RX_TASK_STACK_SIZE 1024
    #define UART_RX_TASK_PRIORITY   3
    
    
    /* ASCII values of some useful keys */
    #define CHAR_LINEFEED                         0x0A
    #define CHAR_LINE_END_1                       0x0D   // Enter
    #define CHAR_LINE_END_2                       0x03   // Enter on numpad
    #define CHAR_SPACE                            0x20
    #define CHAR_ZERO                             0x30
    #define CHAR_UPPERCASE_START                  0x40
    
    /* Encryption Defines */
    
    #define macLength           (4)
    #define clearTextLength     (16)
    #define cipherTextLength    (macLength + clearTextLength)
    #define nonceLength         (12)
    #define aadLength           (14)
    
    
    // Holds the AES-CCM setup for this example
    typedef struct
    {
        uint8_t key[16];                                // A 128 Bit AES key
        CryptoCC26XX_KeyLocation keyLocation;           // One of 8 key locations in the hardware
        uint8_t clearAndCipherText[cipherTextLength];   // Holds the cleartext before, and the ciphertext
                                                        // after the encryption operation.
                                                        // Ciphertext = encrypted text + message authentication code (MAC).
        uint8_t nonce[nonceLength];  // A value that is used only once (cryptographic term 'nonce')
        uint8_t header[aadLength];   // A header that is not encrypted but is authenticated in the operation (AAD).
    } AesCcmExample;
    
    AesCcmExample ccmSetup =
    {
        .key = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
                 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C },
        .keyLocation = CRYPTOCC26XX_KEY_0,
        .clearAndCipherText = { 't','h','i','s','i','s','a','p','l','a','i','n','t','e','x','t','0','0','0','0' },
        .nonce  = { 't','h','i','s','i','s','a','n','o','n','c','e' },
        .header = { 't','h','i','s','i','s','a','h','e','a','d','e','r','1' }
    };
    
    
    /***** Prototypes *****/
    static void rxTaskFunction(UArg arg0, UArg arg1);
    static void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e);
    static void uartRxTaskFunction(UArg arg0, UArg arg1);
    void uart_writePayLoad(uint8_t *packet, uint8_t length);
    static void aesTaskFunction(UArg arg0, UArg arg1);
    /***** Variable declarations *****/
    static Task_Params rxTaskParams;
    Task_Struct rxTask;    /* not static so you can see in ROV */
    static uint8_t rxTaskStack[RX_TASK_STACK_SIZE];
    
    static Task_Params aesTaskParams;
    Task_Struct aesTask;
    static uint8_t aesTaskStack[AES_TASK_STACK_SIZE];
    
    static Task_Params uartRxTaskParams;
    Task_Struct uartRxTask;    /* not static so you can see in ROV */
    static uint8_t uartRxTaskStack[UART_RX_TASK_STACK_SIZE];
    
    Semaphore_Struct semRxStruct;
    Semaphore_Handle semRxHandle;
    
    Semaphore_Struct semUartRxStruct;
    Semaphore_Handle semUartRxHandle;
    
    Semaphore_Struct semAesStruct;
    Semaphore_Handle semAesHandle;
    
    static RF_Object rfObject;
    static RF_Handle rfHandle;
    static RF_CmdHandle rfRxCmd;
    
    UART_Handle uart = NULL;
    UART_Params uartParams;
    
    
    /* Buffer which contains all Data Entries for receiving data.
     * Pragmas are needed to make sure this buffer is 4 byte aligned (requirement from the RF Core) */
    #if defined(__TI_COMPILER_VERSION__)
        #pragma DATA_ALIGN (rxDataEntryBuffer, 4);
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__IAR_SYSTEMS_ICC__)
        #pragma data_alignment = 4
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__GNUC__)
            static uint8_t rxDataEntryBuffer [RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                MAX_LENGTH, NUM_APPENDED_BYTES)] __attribute__ ((aligned (4)));
    #else
        #error This compiler is not supported.
    #endif
    
    /* Receive dataQueue for RF Core to fill in data */
    static dataQueue_t dataQueue;
    static rfc_dataEntryGeneral_t* currentDataEntry;
    uint8_t packetReady = 0;
    static uint8_t packetLength;
    static uint8_t* packetDataPointer;
    
    static PIN_Handle pinHandle;
    
    static uint8_t packet[MAX_LENGTH + NUM_APPENDED_BYTES - 1]; /* The length byte is stored in a separate variable */
    
    
    /***** Function definitions *****/
    void UartRxTask_init(PIN_Handle ledPinHandle) {
        pinHandle = ledPinHandle;
    
        Task_Params_init(&uartRxTaskParams);
        uartRxTaskParams.stackSize = UART_RX_TASK_STACK_SIZE;
        uartRxTaskParams.priority = UART_RX_TASK_PRIORITY;
        uartRxTaskParams.stack = &uartRxTaskStack;
        uartRxTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&uartRxTask, uartRxTaskFunction, &uartRxTaskParams, NULL);
    }
    
    void AesRxTask_init(PIN_Handle ledPinHandle) {
    	pinHandle = ledPinHandle;
    
    	Task_Params_init(&aesTaskParams);
        aesTaskParams.stackSize = AES_TASK_STACK_SIZE;
        aesTaskParams.priority = AES_TASK_PRIORITY;
        aesTaskParams.stack = &aesTaskStack;
        aesTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&aesTask, aesTaskFunction, &aesTaskParams, NULL);
    }
    
    void RxTask_init(PIN_Handle ledPinHandle) {
        pinHandle = ledPinHandle;
    
        Task_Params_init(&rxTaskParams);
        rxTaskParams.stackSize = RX_TASK_STACK_SIZE;
        rxTaskParams.priority = RX_TASK_PRIORITY;
        rxTaskParams.stack = &rxTaskStack;
        rxTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&rxTask, rxTaskFunction, &rxTaskParams, NULL);
    }
    
    /* Function Definations */
    
    static void rxTaskFunction(UArg arg0, UArg arg1)
    {
        RF_Params rfParams;
        RF_Params_init(&rfParams);
    
        if( RFQueue_defineQueue(&dataQueue,
                                rxDataEntryBuffer,
                                sizeof(rxDataEntryBuffer),
                                NUM_DATA_ENTRIES,
                                MAX_LENGTH + NUM_APPENDED_BYTES))
        {
            /* Failed to allocate space for all data entries */
            while(1);
        }
    
        /* Modify CMD_PROP_RX command for application needs */
        RF_cmdPropRx.pQueue = &dataQueue;           /* Set the Data Entity queue for received data */
        RF_cmdPropRx.rxConf.bAutoFlushIgnored = 1;  /* Discard ignored packets from Rx queue */
        RF_cmdPropRx.rxConf.bAutoFlushCrcErr = 1;   /* Discard packets with CRC error from Rx queue */
        RF_cmdPropRx.maxPktLen = MAX_LENGTH;        /* Implement packet length filtering to avoid PROP_ERROR_RXBUF */
        RF_cmdPropRx.pktConf.bRepeatOk = 1;
        RF_cmdPropRx.pktConf.bRepeatNok = 1;
    
        if (!rfHandle) {
            /* Request access to the radio */
            rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
    
            /* Set the frequency */
            RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
        }
    
        while (1) {
        /* Enter RX mode and stay forever in RX */
            rfRxCmd = RF_postCmd(rfHandle, (RF_Op*)&RF_cmdPropRx, RF_PriorityNormal, &callback, IRQ_RX_ENTRY_DONE);
    
        }
    
    }
    
    void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e)
    {
        if (e & RF_EventRxEntryDone)
        {
            /* Toggle pin to indicate RX */
            PIN_setOutputValue(pinHandle, Board_LED2,!PIN_getOutputValue(Board_LED2));
    
            /* Get current unhandled data entry */
            currentDataEntry = RFQueue_getDataEntry();
    
            /* Handle the packet data, located at &currentDataEntry->data:
             * - Length is the first byte with the current configuration
             * - Data starts from the second byte */
            packetLength      = *(uint8_t*)(&currentDataEntry->data);
            packetDataPointer = (uint8_t*)(&currentDataEntry->data + 1);
    
            /* Copy the payload + the status byte to the packet variable */
            memcpy(packet, packetDataPointer, (packetLength + 1));
            memcpy(ccmSetup.clearAndCipherText, packetDataPointer, (packetLength + 1));
    
    
            packetReady = 1;
    
            RFQueue_nextEntry();
    
            Semaphore_post(semAesHandle);
        }
    }
    
    static void aesTaskFunction(UArg arg0, UArg arg1)
    {
    //	int i = 0;
    	//for(i=0; i<20; i++){
    
    		//ccmSetup.clearAndCipherText[i] = packet[i];
    
    	//}
    
    	CryptoCC26XX_Handle             handle;
    	int32_t                         keyIndex;
    	CryptoCC26XX_AESCCM_Transaction trans;
    	int32_t                         status;
    
    	// Initialize Crypto driver structures
    	CryptoCC26XX_init();
    
    	// Open the crypto hardware with non-exclusive access and default parameters.
    	handle = CryptoCC26XX_open(Board_CRYPTO, false, NULL);
    	if (handle == NULL) {
    	    System_abort("CryptoCC26XX did not open");
    	}
    
    	// Allocate a key storage location in the hardware
    	keyIndex = CryptoCC26XX_allocateKey(handle, ccmSetup.keyLocation, (const uint32_t *) ccmSetup.key);
    	if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) {
    	    System_abort("Key Location was not allocated.");
    	}
    
        while(1)
        {
    
        Semaphore_pend(semAesHandle, BIOS_WAIT_FOREVER);
    
    	// Decrypt and authenticate message
    	CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_CCMINV);
    	trans.keyIndex   = keyIndex;
    	trans.authLength = macLength;
    	trans.nonce  = (char *) ccmSetup.nonce;
    	trans.header = (char *) ccmSetup.header;
    	trans.fieldLength  = 3;
    	trans.msgInLength  = cipherTextLength;
    	trans.headerLength = aadLength;
    	trans.msgIn  = (char *) &(ccmSetup.clearAndCipherText[0]);                // Message is decrypted in place
    	trans.msgOut = (char *) &(ccmSetup.clearAndCipherText[clearTextLength]);  // Points to the MAC, is used as input here
    //	trans.msgIn  = (char *) &(packet[0]);                // Message is decrypted in place
    //	trans.msgOut = (char *) &(packet[20]);  // Points to the MAC, is used as input here
    
    	// Do AES-CCM decryption and authentication
    	status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *) &trans);
    	if(status != CRYPTOCC26XX_STATUS_SUCCESS){
    	    System_abort("Decryption and authentication failed.");
    	}
    
    	packetDataPointer = ccmSetup.clearAndCipherText;
    	Semaphore_post(semUartRxHandle);
    
    
    	// Release the key location
    	status = CryptoCC26XX_releaseKey(handle, &keyIndex);
    	if (status != CRYPTOCC26XX_STATUS_SUCCESS) {
    	    System_abort("Key release was not successful.");
    	}
    	}
    }
    
    
    static void uartRxTaskFunction(UArg arg0, UArg arg1)
    {
        if (uart == NULL) {
            /* Create a UART with data processing off. */
            UART_Params_init(&uartParams);
            uartParams.writeDataMode = UART_DATA_BINARY;
            uartParams.readDataMode = UART_DATA_BINARY;
            uartParams.readReturnMode = UART_RETURN_FULL;
            uartParams.readEcho = UART_ECHO_OFF;
            uartParams.baudRate = 115200;
            uart = UART_open(Board_UART0, &uartParams);
    
            if (uart == NULL) {
                System_abort("Error opening the UART");
            }
        }
    
        while (1) {
            Semaphore_pend(semUartRxHandle, BIOS_WAIT_FOREVER);
            if (packetReady) {
                UART_write(uart, ccmSetup.clearAndCipherText, 20);
                uart_writePayLoad(packetDataPointer, packetLength);
               // uart_writePayLoad(ccmSetup.clearAndCipherText, packetLength);
    
                packetReady = 0;
            }
        }
    }
    
    
    //*****************************************************************************
    //
    //! Print payload to UART
    //
    //*****************************************************************************
    
    void uart_writePayLoad(uint8_t *packet, uint8_t length) {
       // char output[2];
    
        UART_control(uart, UARTCC26XX_CMD_RX_FIFO_FLUSH, 0);
        UART_write(uart, "rx data: ", 9);
        UART_write(uart, packet, length);
        UART_control(uart, UARTCC26XX_CMD_RX_FIFO_FLUSH, 0);
    
        /* Output a carriage return */
       // output[0] = CHAR_LINE_END_1;
       // UART_write(uart, output, 1);
    
    }
    
    
    /*
     *  ======== main ========
     */
    int main(void)
    {
        Semaphore_Params semParams;
    
        /* Call board init functions. */
        Board_initGeneral();
        Board_initUART();
    
        /* Open LED pins */
        ledPinHandle = PIN_open(&ledPinState, pinTable);
        if(!ledPinHandle)
        {
            System_abort("Error initializing board LED pins\n");
        }
    
        /* Construct a Semaphore object to be used as a resource lock, inital count 0 */
        Semaphore_Params_init(&semParams);
        Semaphore_construct(&semRxStruct, 0, &semParams);
        Semaphore_construct(&semUartRxStruct, 0, &semParams);
        Semaphore_construct(&semAesStruct, 0, &semParams);
    
    
        /* Obtain instance handle */
        semRxHandle = Semaphore_handle(&semRxStruct);
        semUartRxHandle = Semaphore_handle(&semUartRxStruct);
        semAesHandle = Semaphore_handle(&semAesStruct);
        /* Initialize task */
        RxTask_init(ledPinHandle);
    
        UartRxTask_init(ledPinHandle);
    
        AesRxTask_init(ledPinHandle);
        /* Start BIOS */
        BIOS_start();
    
        return (0);
    }
    
    
    

  • Hello Sir,

    I am uploading the updated file. Can you please look into this and tell me what i am doing wrong. I am trying since morning but i am not able to find what the bug is. Please help me with this sir.

    /*
     * Copyright (c) 2015-2016, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /***** Includes *****/
    #include <stdlib.h>
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>
    
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #include <ti/sysbios/knl/Semaphore.h>
    
    /* Drivers */
    #include <ti/drivers/rf/RF.h>
    #include <ti/drivers/PIN.h>
    #include <ti/drivers/crypto/CryptoCC26XX.h>
    #include <driverlib/rf_prop_mailbox.h>
    #include <ti/drivers/UART.h>
    
    /* Board Header files */
    #include "Board.h"
    
    #include "RFQueue.h"
    #include "smartrf_settings/smartrf_settings.h"
    
    #include <stdlib.h>
    
    /* Pin driver handle */
    static PIN_Handle ledPinHandle;
    static PIN_State ledPinState;
    
    /*
     * Application LED pin configuration table:
     *   - All LEDs board LEDs are off.
     */
    PIN_Config pinTable[] =
    {
        Board_LED2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        PIN_TERMINATE
    };
    
    
    /* Packet RX Configuration */
    #define DATA_ENTRY_HEADER_SIZE 8  /* Constant header size of a Generic Data Entry */
    #define MAX_LENGTH             30 /* Max length byte the radio will accept */
    #define NUM_DATA_ENTRIES       2  /* NOTE: Only two data entries supported at the moment */
    #define NUM_APPENDED_BYTES     2  /* The Data Entries data field will contain:
                                       * 1 Header byte (RF_cmdPropRx.rxConf.bIncludeHdr = 0x1)
                                       * Max 30 payload bytes
                                       * 1 status byte (RF_cmdPropRx.rxConf.bAppendStatus = 0x1) */
    
    
    /***** Defines *****/
    #define UARTCC26XX_CMD_RX_FIFO_FLUSH            UART_CMD_RESERVED + 2
    
    
    #define RX_TASK_STACK_SIZE 1024
    #define RX_TASK_PRIORITY   1
    
    #define AES_TASK_STACK_SIZE 1024
    #define AES_TASK_PRIORITY   2
    
    #define UART_RX_TASK_STACK_SIZE 1024
    #define UART_RX_TASK_PRIORITY   3
    
    
    /* ASCII values of some useful keys */
    #define CHAR_LINEFEED                         0x0A
    #define CHAR_LINE_END_1                       0x0D   // Enter
    #define CHAR_LINE_END_2                       0x03   // Enter on numpad
    #define CHAR_SPACE                            0x20
    #define CHAR_ZERO                             0x30
    #define CHAR_UPPERCASE_START                  0x40
    
    /* Encryption Defines */
    
    #define macLength           (4)
    #define clearTextLength     (16)
    #define cipherTextLength    (macLength + clearTextLength)
    #define nonceLength         (12)
    #define aadLength           (14)
    
    
    // Holds the AES-CCM setup for this example
    typedef struct
    {
        uint8_t key[16];                                // A 128 Bit AES key
        CryptoCC26XX_KeyLocation keyLocation;           // One of 8 key locations in the hardware
        uint8_t clearAndCipherText[cipherTextLength];   // Holds the cleartext before, and the ciphertext
                                                        // after the encryption operation.
                                                        // Ciphertext = encrypted text + message authentication code (MAC).
        uint8_t nonce[nonceLength];  // A value that is used only once (cryptographic term 'nonce')
        uint8_t header[aadLength];   // A header that is not encrypted but is authenticated in the operation (AAD).
    } AesCcmExample;
    
    AesCcmExample ccmSetup =
    {
        .key = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
                 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C },
        .keyLocation = CRYPTOCC26XX_KEY_0,
        .clearAndCipherText = { 't','h','i','s','i','s','a','p','l','a','i','n','t','e','x','t','0','0','0','0' },
       // .clearAndCipherText = txPacket[0],
        .nonce  = { 't','h','i','s','i','s','a','n','o','n','c','e' },
        .header = { 't','h','i','s','i','s','a','h','e','a','d','e','r','1' }
    };
    
    
    /***** Prototypes *****/
    static void rxTaskFunction(UArg arg0, UArg arg1);
    static void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e);
    static void aesTaskFunction(UArg arg0, UArg arg1);
    static void uartRxTaskFunction(UArg arg0, UArg arg1);
    void uart_writePayLoad(uint8_t *packet, uint8_t length);
    
    /***** Variable declarations *****/
    static Task_Params rxTaskParams;
    Task_Struct rxTask;    /* not static so you can see in ROV */
    static uint8_t rxTaskStack[RX_TASK_STACK_SIZE];
    
    static Task_Params uartRxTaskParams;
    Task_Struct uartRxTask;    /* not static so you can see in ROV */
    static uint8_t uartRxTaskStack[UART_RX_TASK_STACK_SIZE];
    
    static Task_Params aesTaskParams;
    Task_Struct aesTask;
    static uint8_t aesTxTaskStack[AES_TASK_STACK_SIZE];
    
    
    Semaphore_Struct semRxStruct;
    Semaphore_Handle semRxHandle;
    
    Semaphore_Struct semAesStruct;
    Semaphore_Handle semAesHandle;
    
    
    Semaphore_Struct semUartRxStruct;
    Semaphore_Handle semUartRxHandle;
    
    static RF_Object rfObject;
    static RF_Handle rfHandle;
    static RF_CmdHandle rfRxCmd;
    
    UART_Handle uart = NULL;
    UART_Params uartParams;
    
    
    /* Buffer which contains all Data Entries for receiving data.
     * Pragmas are needed to make sure this buffer is 4 byte aligned (requirement from the RF Core) */
    #if defined(__TI_COMPILER_VERSION__)
        #pragma DATA_ALIGN (rxDataEntryBuffer, 4);
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__IAR_SYSTEMS_ICC__)
        #pragma data_alignment = 4
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__GNUC__)
            static uint8_t rxDataEntryBuffer [RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                MAX_LENGTH, NUM_APPENDED_BYTES)] __attribute__ ((aligned (4)));
    #else
        #error This compiler is not supported.
    #endif
    
    /* Receive dataQueue for RF Core to fill in data */
    static dataQueue_t dataQueue;
    static rfc_dataEntryGeneral_t* currentDataEntry;
    uint8_t packetReady = 0;
    static uint8_t packetLength;
    static uint8_t* packetDataPointer;
    
    static PIN_Handle pinHandle;
    
    static uint8_t packet[MAX_LENGTH + NUM_APPENDED_BYTES - 1]; /* The length byte is stored in a separate variable */
    
    
    /***** Function definitions *****/
    void UartRxTask_init(PIN_Handle ledPinHandle) {
        pinHandle = ledPinHandle;
    
        Task_Params_init(&uartRxTaskParams);
        uartRxTaskParams.stackSize = UART_RX_TASK_STACK_SIZE;
        uartRxTaskParams.priority = UART_RX_TASK_PRIORITY;
        uartRxTaskParams.stack = &uartRxTaskStack;
        uartRxTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&uartRxTask, uartRxTaskFunction, &uartRxTaskParams, NULL);
    }
    
    void AesTask_init(PIN_Handle inPinHandle)
    {
        pinHandle = inPinHandle;
    
        Task_Params_init(&aesTaskParams);
        aesTaskParams.stackSize = AES_TASK_STACK_SIZE;
        aesTaskParams.priority = AES_TASK_PRIORITY;
        aesTaskParams.stack = &aesTxTaskStack;
        aesTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&aesTask, aesTaskFunction, &aesTaskParams, NULL);
    }
    
    
    void RxTask_init(PIN_Handle ledPinHandle) {
        pinHandle = ledPinHandle;
    
        Task_Params_init(&rxTaskParams);
        rxTaskParams.stackSize = RX_TASK_STACK_SIZE;
        rxTaskParams.priority = RX_TASK_PRIORITY;
        rxTaskParams.stack = &rxTaskStack;
        rxTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&rxTask, rxTaskFunction, &rxTaskParams, NULL);
    }
    
    static void rxTaskFunction(UArg arg0, UArg arg1)
    {
        RF_Params rfParams;
        RF_Params_init(&rfParams);
    
        if( RFQueue_defineQueue(&dataQueue,
                                rxDataEntryBuffer,
                                sizeof(rxDataEntryBuffer),
                                NUM_DATA_ENTRIES,
                                MAX_LENGTH + NUM_APPENDED_BYTES))
        {
            /* Failed to allocate space for all data entries */
            while(1);
        }
    
        /* Modify CMD_PROP_RX command for application needs */
        RF_cmdPropRx.pQueue = &dataQueue;           /* Set the Data Entity queue for received data */
        RF_cmdPropRx.rxConf.bAutoFlushIgnored = 1;  /* Discard ignored packets from Rx queue */
        RF_cmdPropRx.rxConf.bAutoFlushCrcErr = 1;   /* Discard packets with CRC error from Rx queue */
        RF_cmdPropRx.maxPktLen = MAX_LENGTH;        /* Implement packet length filtering to avoid PROP_ERROR_RXBUF */
        RF_cmdPropRx.pktConf.bRepeatOk = 1;
        RF_cmdPropRx.pktConf.bRepeatNok = 1;
    
        if (!rfHandle) {
            /* Request access to the radio */
            rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
    
            /* Set the frequency */
            RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
        }
    
        while (1) {
        /* Enter RX mode and stay forever in RX */
            rfRxCmd = RF_postCmd(rfHandle, (RF_Op*)&RF_cmdPropRx, RF_PriorityNormal, &callback, IRQ_RX_ENTRY_DONE);
    
        }
    
    }
    
    void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e)
    {
        if (e & RF_EventRxEntryDone)
        {
            /* Toggle pin to indicate RX */
            PIN_setOutputValue(pinHandle, Board_LED2,!PIN_getOutputValue(Board_LED2));
    
            /* Get current unhandled data entry */
            currentDataEntry = RFQueue_getDataEntry();
    
            /* Handle the packet data, located at &currentDataEntry->data:
             * - Length is the first byte with the current configuration
             * - Data starts from the second byte */
            packetLength      = *(uint8_t*)(&currentDataEntry->data);
            packetDataPointer = (uint8_t*)(&currentDataEntry->data + 1);
    
            /* Copy the payload + the status byte to the packet variable */
            memcpy(packet, packetDataPointer, (packetLength + 1));
    
            	int i = 0;
            	for(i=0; i<20; i++){
    
            		ccmSetup.clearAndCipherText[i] = packet[i];
    
            	}
    
    
            packetReady = 1;
    
            RFQueue_nextEntry();
    
            Semaphore_post(semAesHandle);
        }
    }
    
    static void aesTaskFunction(UArg arg0, UArg arg1)
    {
    /*	int i = 0;
    	for(i=0; i<20; i++){
    
    		ccmSetup.clearAndCipherText[i] = txPacket[i];
    
    	}*/
    
    	CryptoCC26XX_Handle             handle;
    	int32_t                         keyIndex;
    	CryptoCC26XX_AESCCM_Transaction trans;
    	int32_t                         status;
    
    	// Initialize Crypto driver structures
    	CryptoCC26XX_init();
    
    	// Open the crypto hardware with non-exclusive access and default parameters.
    	handle = CryptoCC26XX_open(Board_CRYPTO, false, NULL);
    	if (handle == NULL) {
    	    System_abort("CryptoCC26XX did not open");
    	}
    
    	// Allocate a key storage location in the hardware
    	keyIndex = CryptoCC26XX_allocateKey(handle, ccmSetup.keyLocation, (const uint32_t *) ccmSetup.key);
    	if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) {
    	    System_abort("Key Location was not allocated.");
    	}
    
    	while(1){
    
        Semaphore_pend(semAesHandle, BIOS_WAIT_FOREVER);
    
    
        // Decrypt and authenticate message
        CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_CCMINV);
        trans.keyIndex   = keyIndex;
        trans.authLength = macLength;
        trans.nonce  = (char *) ccmSetup.nonce;
        trans.header = (char *) ccmSetup.header;
        trans.fieldLength  = 3;
        trans.msgInLength  = cipherTextLength;
        trans.headerLength = aadLength;
        trans.msgIn  = (char *) &(ccmSetup.clearAndCipherText[0]);                // Message is decrypted in place
        trans.msgOut = (char *) &(ccmSetup.clearAndCipherText[clearTextLength]);  // Points to the MAC, is used as input here
    
    
        // Do AES-CCM decryption and authentication
        status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *) &trans);
        if(status != CRYPTOCC26XX_STATUS_SUCCESS){
            System_abort("Decryption and authentication failed.");
        }
    
    
    	Semaphore_post(semUartRxHandle);
    
    
    	// Release the key location
    	status = CryptoCC26XX_releaseKey(handle, &keyIndex);
    	if (status != CRYPTOCC26XX_STATUS_SUCCESS) {
    	    System_abort("Key release was not successful.");
    	}
    	}
    	//	Semaphore_post(semUartRxHandle);
    
    }
    
    
    static void uartRxTaskFunction(UArg arg0, UArg arg1)
    {
        if (uart == NULL) {
            /* Create a UART with data processing off. */
            UART_Params_init(&uartParams);
            uartParams.writeDataMode = UART_DATA_BINARY;
            uartParams.readDataMode = UART_DATA_BINARY;
            uartParams.readReturnMode = UART_RETURN_FULL;
            uartParams.readEcho = UART_ECHO_OFF;
            uartParams.baudRate = 115200;
            uart = UART_open(Board_UART0, &uartParams);
    
            if (uart == NULL) {
                System_abort("Error opening the UART");
            }
        }
    
        while (1) {
            Semaphore_pend(semUartRxHandle, BIOS_WAIT_FOREVER);
            if (packetReady) {
                UART_write(uart, ccmSetup.clearAndCipherText, sizeof(ccmSetup.clearAndCipherText));
    
                uart_writePayLoad(packetDataPointer, packetLength);
                packetReady = 0;
            }
        }
    }
    
    
    //*****************************************************************************
    //
    //! Print payload to UART
    //
    //*****************************************************************************
    
    void uart_writePayLoad(uint8_t *packet, uint8_t length) {
     //   char output[2];
    
        UART_control(uart, UARTCC26XX_CMD_RX_FIFO_FLUSH, 0);
        UART_write(uart, "rx data: ", 9);
        UART_write(uart, packet, length);
        UART_control(uart, UARTCC26XX_CMD_RX_FIFO_FLUSH, 0);
    
        /* Output a carriage return */
       // output[0] = CHAR_LINE_END_1;
       // UART_write(uart, output, 1);
    
    }
    
    
    /*
     *  ======== main ========
     */
    int main(void)
    {
        Semaphore_Params semParams;
    
        /* Call board init functions. */
        Board_initGeneral();
        Board_initUART();
    
        /* Open LED pins */
        ledPinHandle = PIN_open(&ledPinState, pinTable);
        if(!ledPinHandle)
        {
            System_abort("Error initializing board LED pins\n");
        }
    
        /* Construct a Semaphore object to be used as a resource lock, inital count 0 */
        Semaphore_Params_init(&semParams);
        Semaphore_construct(&semRxStruct, 0, &semParams);
        Semaphore_construct(&semUartRxStruct, 0, &semParams);
        Semaphore_construct(&semAesStruct, 0, &semParams);
    
    
        /* Obtain instance handle */
        semRxHandle = Semaphore_handle(&semRxStruct);
        semUartRxHandle = Semaphore_handle(&semUartRxStruct);
        semAesHandle = Semaphore_handle(&semAesStruct);
    
    
        /* Initialize task */
        RxTask_init(ledPinHandle);
    
        /* Initialize task */
        UartRxTask_init(ledPinHandle);
    
        AesTask_init(ledPinHandle);
    
    
        /* Start BIOS */
        BIOS_start();
    
        return (0);
    }