📄 myapp_ex08a.c
字号:
/******************************************************************************
* 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 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.
*
******************************************************************************/
uint8_t App_StartScan(uint8_t scanType)
{
mlmeMessage_t *pMsg;
mlmeScanReq_t *pScanReq;
Uart_Print("Sending the MLME-Scan Request message to the MAC...");
/* Allocate a message for the MLME (We should check for NULL). */
pMsg = MSG_AllocType(mlmeMessage_t);
if(pMsg != NULL)
{
/* This is a MLME-START.req command */
pMsg->msgType = gMlmeScanReq_c;
/* Create the Start request message data. */
pScanReq = &pMsg->msgData.scanReq;
/* gScanModeED_c, gScanModeActive_c, gScanModePassive_c, or gScanModeOrphan_c */
pScanReq->scanType = scanType;
/* ChannelsToScan & 0xFF - LSB, always 0x00 */
pScanReq->scanChannels[0] = (uint8_t)((SCAN_CHANNELS) & 0xFF);
/* ChannelsToScan>>8 & 0xFF */
pScanReq->scanChannels[1] = (uint8_t)((SCAN_CHANNELS>>8) & 0xFF);
/* ChannelsToScan>>16 & 0xFF */
pScanReq->scanChannels[2] = (uint8_t)((SCAN_CHANNELS>>16) & 0xFF);
/* ChannelsToScan>>24 & 0xFF - MSB */
pScanReq->scanChannels[3] = (uint8_t)((SCAN_CHANNELS>>24) & 0xFF);
/* Duration per channel 0-14 (dc). T[sec] = (16*960*((2^dc)+1))/1000000.
A scan duration of 5 on 16 channels approximately takes 8 secs. */
pScanReq->scanDuration = 5;
/* Send the Scan request to the MLME. */
if(MSG_Send(NWK_MLME, pMsg) == gSuccess_c)
{
Uart_Print("Done\n");
return errorNoError;
}
else
{
Uart_Print("Invalid parameter!\n");
return errorInvalidParameter;
}
}
else
{
/* Allocation of a message buffer failed. */
Uart_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 'logicalChannel'.
*
******************************************************************************/
void App_HandleScanEdConfirm(nwkMessage_t *pMsg)
{
uint8_t n, minEnergy;
uint8_t *pEdList;
Uart_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 */
logicalChannel = 11;
/* Search for the channel with least energy */
for(n=0; n<16; n++)
{
if(pEdList[n] < minEnergy)
{
minEnergy = pEdList[n];
/* Channel numbering is 11 to 26 both inclusive */
logicalChannel = n + 11;
}
}
/* Print out the result of the ED scan */
Uart_Print("ED scan returned the following results:\n [");
Uart_PrintHex(pEdList, 16, gPrtHexBigEndian_c | gPrtHexCommas_c);
Uart_Print("]\n\n");
/* Print out the selected logical channel */
Uart_Print("Based on the ED scan the logical channel 0x");
Uart_PrintHex(&logicalChannel, 1, 0);
Uart_Print(" was selected\n");
/* 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.
*
******************************************************************************/
uint8_t App_StartCoordinator(void)
{
/* Message for the MLME will be allocated and attached to this pointer */
mlmeMessage_t *pMsg;
Uart_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 = gMacPibShortAddress_c;
pMsg->msgData.setReq.pibAttributeValue = (uint8_t *)shortAddress;
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 = gMacPibAssociationPermit_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. */
memcpy(pStartReq->panId, (void *)panId, 2);
/* Logical Channel - the default of 11 will be overridden */
pStartReq->logicalChannel = logicalChannel;
/* Beacon Order: 0xF = turn off beacons, less than 0xF = turn on beacons */
pStartReq->beaconOrder = BEACON_ORDER;
/* Superframe Order: Must be equal or less than the beacon order */
pStartReq->superFrameOrder = SUPERFRAME_ORDER;
/* 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)
{
Uart_Print("Done\n");
return errorNoError;
}
else
{
/* One or more parameters in the Start Request message were invalid. */
Uart_Print("Invalid parameter!\n");
return errorInvalidParameter;
}
}
else
{
/* Allocation of a message buffer failed. */
Uart_Print("Message allocation failed!\n");
return errorAllocFailed;
}
}
/******************************************************************************
* The App_SendAssociateResponse(nwkMessage_t *pMsgIn) will create the response
* message to an Associate Indication (device sends an Associate Request to its
* MAC. The request is transmitted to the coordinator where it is converted into
* an Associate Indication). This function will extract the devices long address,
* and various other flags from the incoming indication message for building the
* response message.
*
* The function may return either of the following values:
* errorNoError: The Associate Response 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.
*
******************************************************************************/
uint8_t App_SendAssociateResponse(nwkMessage_t *pMsgIn)
{
mlmeMessage_t *pMsg;
mlmeAssociateRes_t *pAssocRes;
Uart_Print("Sending the MLME-Associate Response message to the MAC...\n");
/* Allocate a message for the MLME */
pMsg = MSG_AllocType(mlmeMessage_t);
if(pMsg != NULL)
{
/* This is a MLME-ASSOCIATE.res command */
pMsg->msgType = gMlmeAssociateRes_c;
/* Create the Associate response message data. */
pAssocRes = &pMsg->msgData.associateRes;
/* Assign a short address to the device. In this example we simply
choose 0x0001. Though, all devices and coordinators in a PAN must have
different short addresses. However, if a device do not want to use
short addresses at all in the PAN, a short address of 0xFFFE must
be assigned to it. */
if(pMsgIn->msgData.associateInd.capabilityInfo & gCapInfoAllocAddr_c)
{
/* Assign a unique short address less than 0xfffe if the device requests so. */
pAssocRes->assocShortAddress[0] = 0x01;
pAssocRes->assocShortAddress[1] = 0x00;
}
else
{
/* A short address of 0xfffe means that the device is granted access to
the PAN (Associate successful) but that long addressing is used.*/
pAssocRes->assocShortAddress[0] = 0xFE;
pAssocRes->assocShortAddress[1] = 0xFF;
}
/* Get the 64 bit address of the device requesting association. */
memcpy(pAssocRes->deviceAddress, pMsgIn->msgData.associateInd.deviceAddress, 8);
/* Association granted. May also be gPanAtCapacity_c or gPanAccessDenied_c. */
pAssocRes->status = gSuccess_c;
/* Do not use security */
pAssocRes->securityEnable = FALSE;
/* Save device info. */
memcpy(deviceShortAddress, pAssocRes->assocShortAddress, 2);
memcpy(deviceLongAddress, pAssocRes->deviceAddress, 8);
/* Start security for this device */
App_InitSecurity();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -