📄 mapp.c
字号:
pScanReq->scanChannels[3] = (uint8_t)((mDefaultValueOfChannel_c>>24) & 0xFF);
/* Duration per channel 0-14 (dc). T[sec] = (16*960*((2^dc)+1))/1000000.
A scan duration of 3 on 16 channels approximately takes 2 secs. */
pScanReq->scanDuration = 3;
/* Send the Scan request to the MLME. */
if(MSG_Send(NWK_MLME, pMsg) == gSuccess_c)
{
UartUtil_Print("Done\n");
return errorNoError;
}
else
{
UartUtil_Print("Invalid parameter!\n");
return errorInvalidParameter;
}
}
else
{
/* Allocation of a message buffer failed. */
UartUtil_Print("Message allocation failed!\n");
return errorAllocFailed;
}
}
/******************************************************************************
* The App_HandleScanEdConfirm(nwkMessage_t *pMsg) function will handle the
* ED scan confirm message received from the MLME when the ED scan has completed.
* The message contains the ED scan result list. This function will search the
* list in order to select the logical channel with the least energy. The
* selected channel is stored in the global variable called 'mLogicalChannel'.
*
******************************************************************************/
static void App_HandleScanEdConfirm(nwkMessage_t *pMsg)
{
uint8_t n, minEnergy;
uint8_t *pEdList;
uint8_t ChannelMask;
UartUtil_Print("Recevied the MLME-Scan Confirm message from the MAC\n");
/* Get a pointer to the energy detect results */
pEdList = pMsg->msgData.scanCnf.resList.pEnergyDetectList;
/* Set the minimum energy to a large value */
minEnergy = 0xFF;
/* Select default channel */
mLogicalChannel = 11;
/* Search for the channel with least energy */
for(n=0; n<16; n++)
{
ChannelMask = n + 11;
if((pEdList[n] < minEnergy)&&((uint8_t)((mDefaultValueOfChannel_c>>ChannelMask) & 0x1)))
{
minEnergy = pEdList[n];
/* Channel numbering is 11 to 26 both inclusive */
mLogicalChannel = n + 11;
}
}
/* Print out the result of the ED scan */
UartUtil_Print("ED scan returned the following results:\n [");
UartUtil_PrintHex(pEdList, 16, gPrtHexBigEndian_c | gPrtHexCommas_c);
UartUtil_Print("]\n\n");
/* Print out the selected logical channel */
UartUtil_Print("Based on the ED scan the logical channel 0x");
UartUtil_PrintHex(&mLogicalChannel, 1, 0);
UartUtil_Print(" was selected\n");
/*print a message on the LCD also*/
LCD_ClearDisplay();
LCD_WriteString(1,"Energy Detection");
LCD_WriteString(2,"Scan..successful");
/* The list of detected energies must be freed. */
MSG_Free(pEdList);
}
/******************************************************************************
* The App_StartScan(scanType) function will start the scan process of the
* specified type in the MAC. This is accomplished by allocating a MAC message,
* which is then assigned the desired scan parameters and sent to the MLME
* service access point. The MAC PIB attributes "macShortAddress", and
* "macAssociatePermit" are modified.
*
* The function may return either of the following values:
* errorNoError: The Scan message was sent successfully.
* errorInvalidParameter: The MLME service access point rejected the
* message due to an invalid parameter.
* errorAllocFailed: A message buffer could not be allocated.
*
******************************************************************************/
static uint8_t App_StartCoordinator(void)
{
/* Message for the MLME will be allocated and attached to this pointer */
mlmeMessage_t *pMsg;
UartUtil_Print("Sending the MLME-Start Request message to the MAC...");
/* Allocate a message for the MLME (We should check for NULL). */
pMsg = MSG_AllocType(mlmeMessage_t);
if(pMsg != NULL)
{
/* Pointer which is used for easy access inside the allocated message */
mlmeStartReq_t *pStartReq;
/* Return value from MSG_send - used for avoiding compiler warnings */
uint8_t ret;
/* Boolean value that will be written to the MAC PIB */
uint8_t boolFlag;
/* Set-up MAC PIB attributes. Please note that Set, Get,
and Reset messages are not freed by the MLME. */
/* We must always set the short address to something
else than 0xFFFF before starting a PAN. */
pMsg->msgType = gMlmeSetReq_c;
pMsg->msgData.setReq.pibAttribute = gMPibShortAddress_c;
pMsg->msgData.setReq.pibAttributeValue = (uint8_t *)maShortAddress;
ret = MSG_Send(NWK_MLME, pMsg);
/* We must set the Association Permit flag to TRUE
in order to allow devices to associate to us. */
pMsg->msgType = gMlmeSetReq_c;
pMsg->msgData.setReq.pibAttribute = gMPibAssociationPermit_c;
boolFlag = TRUE;
pMsg->msgData.setReq.pibAttributeValue = &boolFlag;
ret = MSG_Send(NWK_MLME, pMsg);
/* This is a MLME-START.req command */
pMsg->msgType = gMlmeStartReq_c;
/* Create the Start request message data. */
pStartReq = &pMsg->msgData.startReq;
/* PAN ID - LSB, MSB. The example shows a PAN ID of 0xBEEF. */
FLib_MemCpy(pStartReq->panId, (void *)maPanId, 2);
/* Logical Channel - the default of 11 will be overridden */
pStartReq->logicalChannel = mLogicalChannel;
/* Beacon Order - 0xF = turn off beacons */
pStartReq->beaconOrder = 0x0F;
/* Superframe Order - 0xF = turn off beacons */
pStartReq->superFrameOrder = 0x0F;
/* Be a PAN coordinator */
pStartReq->panCoordinator = TRUE;
/* Dont use battery life extension */
pStartReq->batteryLifeExt = FALSE;
/* This is not a Realignment command */
pStartReq->coordRealignment = FALSE;
/* Dont use security */
pStartReq->securityEnable = FALSE;
/* Send the Start request to the MLME. */
if(MSG_Send(NWK_MLME, pMsg) == gSuccess_c)
{
UartUtil_Print("Done\n");
return errorNoError;
}
else
{
/* One or more parameters in the Start Request message were invalid. */
UartUtil_Print("Invalid parameter!\n");
return errorInvalidParameter;
}
}
else
{
/* Allocation of a message buffer failed. */
UartUtil_Print("Message allocation failed!\n");
return errorAllocFailed;
}
}
/******************************************************************************
* The App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType) function does not, as
* the name implies, wait for a message, thus blocking the execution of the
* state machine. Instead the function analyzes the supplied message to determine
* whether or not the message is of the expected type.
* The function may return either of the following values:
* errorNoError: The message was of the expected type.
* errorNoMessage: The message pointer is NULL.
* errorWrongConfirm: The message is not of the expected type.
*
******************************************************************************/
static uint8_t App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType)
{
/* Do we have a message? If not, the exit with error code */
if(pMsg == NULL)
return errorNoMessage;
/* Is it the expected message type? If not then exit with error code */
if(pMsg->msgType != msgType)
return errorWrongConfirm;
/* Found the expected message. Return with success code */
return errorNoError;
}
/*****************************************************************************
* Handles all key events for this device.
* Interface assumptions: None
* Return value: None
*****************************************************************************/
static void App_HandleKeys
(
key_event_t events /*IN: Events from keyboard modul */
)
{
switch ( events )
{
case gKBD_EventSW1_c:
case gKBD_EventSW2_c:
case gKBD_EventSW3_c:
case gKBD_EventSW4_c:
case gKBD_EventLongSW1_c:
case gKBD_EventLongSW2_c:
case gKBD_EventLongSW3_c:
case gKBD_EventLongSW4_c:
if(gState == stateInit)
{
StopLed1Flashing;
StopLed2Flashing;
StopLed3Flashing;
StopLed4Flashing;
Led1Off;
Led2Off;
Led3Off;
Led4Off;
LCD_ClearDisplay();
LCD_WriteString(1,"Application");
LCD_WriteString(2," started");
TS_SendEvent(gZappTaskID_c, evtInit);
}
break;
}
}
/*****************************************************************************
* The DeepSleepWakeupStackProc(void) function is called each time the
* application exits the DeepSleep mode .
*
* Return value:
* None
*****************************************************************************/
void DeepSleepWakeupStackProc(void){
return;
}
/******************************************************************************
* The following functions are called by the MAC to put messages into the
* Application's queue. They need to be defined even if they are not used
* in order to avoid linker errors.
******************************************************************************/
uint8_t MLME_NWK_SapHandler(nwkMessage_t * pMsg)
{
/* Put the incoming MLME message in the applications input queue. */
MSG_Queue(&mMlmeNwkInputQueue, pMsg);
TS_SendEvent(gZappTaskID_c, evtMessageFromMLME);
return gSuccess_c;
}
uint8_t MCPS_NWK_SapHandler(mcpsToNwkMessage_t *pMsg)
{
/* If the message is not handled anywhere it must be freed. */
MSG_Free(pMsg);
return gSuccess_c;
}
uint8_t ASP_APP_SapHandler(aspToAppMsg_t *pMsg)
{
/* If the message is not handled anywhere it must be freed. */
MSG_Free(pMsg);
return gSuccess_c;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -