📄 myapp_ex08a.c
字号:
/******************************************************************************
* MyApp_Ex01.c - Initialization and main loop.
* MyApp_Ex02.c - Energy Detection Scan
* MyApp_Ex03a.c - A PAN Coordinator is started
* MyApp_Ex03b.c - Device locates coordinator using Active Scan
* MyApp_Ex04a.c - Coordinator responds to an Associate request
* MyApp_Ex04b.c - Device Associates to the PAN coordinator
* MyApp_Ex05a.c - Coordinator receives data from device
* MyApp_Ex05b.c - Device sends direct data to the coordinator
* MyApp_Ex06a.c - Coordinator sends indirect data to device
* MyApp_Ex06b.c - Device polls for data from the coordinator
* MyApp_Ex07a.c - Coordinator starts a beaconed network
* MyApp_Ex07b.c - Device receives data using automatic polling
* = MyApp_Ex08a.c - Coordinator uses security = This file ==
* MyApp_Ex08b.c - Device uses security
*
* This demo application builds on MyApp_Ex07a.c which demonstrated how
* to start and use a beaconed network.
*
* It it assumed that the demo application on the device side is Ex08b for this
* example. The Ex08b application has been configured to apply security to the
* data frames that it is sending.
*
* To send data frames using security the security PIB attributes must be set
* up prior to sending the data frames. To have security applied to the data
* frames they must be sent with the SecurityEnable bit (bit 3) in the TxOptions
* parameter of the MCPS-DATA.request message. The coordinator has sufficient information
* about the device once it receives the association indication from the device.
* Hence, once the association indication is received the coordinator sets up
* the appropriate security PIB attributes. In this example the coordinator
* will communicate with the device in secured mode (2), using security level 6
* (ENC-MIC-64).
*
* To test the data transfer from the coordinator to the device, both should be
* connected to a PC with an RS232 terminal at 19200bps, 8N1. When sending an
* ASCII file (send as text) from the coordinators terminal, the file will be
* printed to the terminal connected to the device. It resembles a bidirectional
* wireless RS232 cable replacement (though, without error checking and flow
* control in this simple example).
*
* The steps required for applying security to the data frames are:
* 1) Set the relevant PIB attributes.
* 2) Set the SecurityEnable bit (bit 3) in the TxOptions of the MCPS-DATA.request.
*
******************************************************************************/
#include "802_15_4.h" /* Include everything related to the 802.15.4 interface*/
#include "Uart.h" /* Defines the interface of the demo UART. */
#include "ToolBox.h" /* Defines for memcpy, memcmp. */
/* Defines the channels to scan. Each bit represents one channel. Use
0x07FFF800 to scan all 16 802.15.4 channels in the 2.4GHz band. */
#define SCAN_CHANNELS 0x07FFF800
/* Defines the beaconed network configuration. The values shown
are suitable for transfer of 19200bps bidirectional UART data. */
#define BEACON_ORDER 1
#define SUPERFRAME_ORDER 1
/* Maximum number of outstanding packets */
#define MAX_PENDING_DATA_PACKETS 2
/* Default size of data payload in MCPS-Data.request.
The length has no real meaning since MCPS-Data.requests
are always sent in one-size buffers big enough to hold
a maximum length data frame. */
#define DEFAULT_DATA_LENGTH 20
/* Forward declarations of helper functions */
uint8_t App_StartScan(uint8_t scanType);
void App_HandleScanEdConfirm(nwkMessage_t *pMsg);
uint8_t App_StartCoordinator(void);
uint8_t App_HandleMlmeInput(nwkMessage_t *pMsg);
uint8_t App_SendAssociateResponse(nwkMessage_t *pMsgIn);
void App_HandleMcpsInput(mcpsToNwkMessage_t *pMsgIn);
void App_TransmitUartData(void);
uint8_t App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType);
void App_InitSecurity(void);
/* The various states of the application state machine. */
enum {
stateInit,
stateScanEdStart,
stateScanEdWaitConfirm,
stateStartCoordinator,
stateStartCoordinatorWaitConfirm,
stateListen,
stateTerminate
};
/* Error codes */
enum {
errorNoError,
errorWrongConfirm,
errorNotSuccessful,
errorNoMessage,
errorAllocFailed,
errorInvalidParameter,
errorNoScanResults
};
/* The current state of the applications state machine */
uint8_t state;
/* The status parameter of the latest confirm message from the MLME */
uint8_t confirmStatus;
/* The current logical channel (frequency band) */
uint8_t logicalChannel;
/* These byte arrays stores an associated
devices long and short addresses. */
uint8_t deviceShortAddress[2];
uint8_t deviceLongAddress[8];
/* We want the coordinators short address to be 0xCAFE. */
const uint8_t shortAddress[2] = { 0xFE, 0xCA };
/* PAN ID is 0xBEEF */
const uint8_t panId[2] = { 0xEF, 0xBE };
/* The security level */
const uint8_t securityLevel = 6;
/* The security mode */
const uint8_t securityMode = 2;
/* Data request packet for sending UART input to the coordinator */
nwkToMcpsMessage_t *pPacket;
/* The MSDU handle is a unique data packet identifier */
uint8_t msduHandle;
/* Number of pending data packets */
uint8_t numPendingPackets;
/* Application input queues */
anchor_t mMlmeNwkInputQueue;
anchor_t mMcpsNwkInputQueue;
/* Application Main Loop */
void main(void)
{
/* Pointer for storing the messages from MLME */
void *pMsgIn;
/* Stores the error/success code returned by some functions. */
uint8_t ret;
/* return value of Mlme_Main() - not used yet */
uint8_t macStatus;
/* Initialize variables */
state = stateInit;
/* Prepare input queues.*/
MSG_InitQueue(&mMlmeNwkInputQueue);
MSG_InitQueue(&mMcpsNwkInputQueue);
/* Execute the application state machine */
while(state < stateTerminate)
{
/* Preset return code to contain the success code */
ret = errorNoError;
/* Try to get a message from MLME */
if(MSG_Pending(&mMlmeNwkInputQueue))
pMsgIn = MSG_DeQueue(&mMlmeNwkInputQueue);
else
pMsgIn = NULL;
switch(state)
{
case stateInit:
/* Initialize the UART so that we can print out status messages */
Uart_Init();
/* Initialize the 802.15.4 stack */
Init_802_15_4();
/* Goto Energy Detection state. */
state = stateScanEdStart;
/* Print a welcome message to the UART */
Uart_Print("The Myapp_Ex08a demo application is initialized and ready.\n\n");
break;
case stateScanEdStart:
/* Start the Energy Detection scan, and goto wait for confirm state. */
Uart_Print("Initiating the Energy Detection Scan\n");
ret = App_StartScan(gScanModeED_c);
if(ret == errorNoError)
{
state = stateScanEdWaitConfirm;
}
break;
case stateScanEdWaitConfirm:
/* Stay in this state until the MLME Scan confirm message arrives,
and has been processed. Then goto Start Coordinator state. */
ret = App_WaitMsg(pMsgIn, gNwkScanCnf_c);
if(ret == errorNoError)
{
/* Process the ED scan confirm. The logical
channel is selected by this function. */
App_HandleScanEdConfirm(pMsgIn);
state = stateStartCoordinator;
}
break;
case stateStartCoordinator:
/* Start up as a PAN Coordinator on the selected channel. */
Uart_Print("\nStarting as PAN coordinator on channel 0x");
Uart_PrintHex(&logicalChannel, 1, FALSE);
Uart_Print("\n");
ret = App_StartCoordinator();
if(ret == errorNoError)
{
/* If the Start request was sent successfully to
the MLME, then goto Wait for confirm state. */
state = stateStartCoordinatorWaitConfirm;
}
break;
case stateStartCoordinatorWaitConfirm:
/* Stay in this state until the Start confirm message
arrives, and then goto the Listen state. */
ret = App_WaitMsg(pMsgIn, gNwkStartCnf_c);
if(ret == errorNoError)
{
Uart_Print("Started the coordinator with PAN ID 0x");
Uart_PrintHex((uint8_t *)panId, 2, 0);
Uart_Print(", and short address 0x");
Uart_PrintHex((uint8_t *)shortAddress, 2, 0);
Uart_Print(".\n\nReady to send and receive data over the UART.\n\n");
state = stateListen;
}
break;
case stateListen:
/* Stay in this state forever. Handles associate, disassociate etc. */
ret = App_HandleMlmeInput(pMsgIn);
break;
}
if(pMsgIn)
{
/* Messages from the MLME must always be freed. */
MSG_Free(pMsgIn);
}
/* If we are associated then check MCPS queue and UART data buffer. */
if(state == stateListen)
{
/* Check for input from MCPS (data related)*/
if(MSG_Pending(&mMcpsNwkInputQueue))
{
/* Get the message from MCPS */
pMsgIn = MSG_DeQueue(&mMcpsNwkInputQueue);
/* Process the message */
App_HandleMcpsInput(pMsgIn);
/* Messages from the MCPS must always be freed. */
MSG_Free(pMsgIn);
}
/* Check if the UART buffer has data to be sent to the device. */
App_TransmitUartData();
}
/* Call the MAC main function continuously. */
macStatus = Mlme_Main();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -