⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 macs.c

📁 无线传感器星型网络的节点的基站程序,采用802.15.4协议
💻 C
字号:
#include "mac.h"
extern MAC_STATE_TYPE     mac_current_state;
extern MAC_PIB  mac_pib_data;
extern uint8_t                                         current_pending_num;
extern SHORT_ADDR  default_short_addr;

void mlmeScanConfirm(MLME_SCAN_CONFIRM  scan_result_info)
{
       //根据scan_result_info来修改对应的mac层和PHY层的pib信息
       //本来有个复杂的选择算法,这里简单处理第一个
     //printf("\n-------it is in scan confirm---------\n");
	if((scan_result_info.status==SUCCESS)&&(scan_result_info.scanType==ORPHAN_SCAN))
	{
	   //printf("\n----SUCCESSFULL ORPHAN SCAN----\n");
	     mac_current_state=MAC_STATE_IDLE;
	     return;	 
	}
       if((scan_result_info.status==SUCCESS)&&(scan_result_info.scanType==ACTIVE_PASSIVE_SCAN))
       {
	   printf("\n-------it is in scan confirm---------\n");
           mac_current_state=MAC_STATE_ACTIVE_PASSIVE_SCAN_FINISHED;
           //printf("\n-----the macbeacon is %02x\n",scan_result_info.pPANDescriptorList[0].superframeSpec.SF.bytes.LSB);
	    mac_pib_data.macBeaconOrder=((scan_result_info.pPANDescriptorList[0].superframeSpec.SF.bytes.LSB)&0x0f);
	    mac_pib_data.macSuperframeOrder=((scan_result_info.pPANDescriptorList[0].superframeSpec.SF.bytes.LSB)>>4);
	    mac_pib_data.macPANId.Val=scan_result_info.pPANDescriptorList[0].coordinator.panID.Val; 		
	    if(scan_result_info.pPANDescriptorList[0].coordinator.addrMode==0x02)	
	    	{
	           mac_pib_data.macCoordShortAddress.Val=scan_result_info.pPANDescriptorList[0].coordinator.shortAddr.Val;
	    	}
	    else if(scan_result_info.pPANDescriptorList[0].coordinator.addrMode==0x03)	
	    	{
	    	    mac_pib_data.macCoordExtendedAddress.longaddr=scan_result_info.pPANDescriptorList[0].coordinator.longAddr.longaddr;	
	    	}
	    ppib.phyCurrentChanel=scan_result_info.pPANDescriptorList[0].logicalChannel;
	    //PLME_SET_request(phyCurrentChanel_id, ppib.phyCurrentChanel);
	    //OS_post(synrequest_task);
       }
 	else
 	{
 	   if(!IS_COORDINATOR)
 	   	{
 	   mac_current_state=MAC_STATE_UNSTARTED;
	 //printf("\n--PASSIVE SCAN FAILURE-----\n");
 	   	}
	   else
	   	{
	   mac_current_state=MAC_STATE_ACTIVE_PASSIVE_SCAN_FINISHED;
	 //printf("\n---doesn't exist any 802.15.4 network ----\n");
	 //printf("---I will be the coordinator----");
	   	}
	   //OS_post(scan_task);	
 	} 
       
}

void mlmeStartConfirm(void)
{
	//通知高层发送完毕。
	//可以通过post高层任务来完成高层的函数任务
	//空函数,高层实现
}


void mlmeAssociateConfirm(SHORT_ADDR AssocShortAddress, BYTE status)
{
	//printf("\n------success associate------ %d\n",status);
	//printf("\nthe allocated short address is %04x\n",AssocShortAddress.Val);
	if(status==NO_ACK)
		{
		
		printf("\n----the association request frame is no acked-----\n");
		
		//mac_current_state=MAC
		}
	//两种情况为NO_DATA
	if(status==NO_DATA)
		printf("\n---didn't received the association response frame---\n");
		
	if(status==SUCCESS)
	{
	mac_current_state=MAC_STATE_ASSOC_FINISHED;
	//mac_current_state=MAC_STATE_IDLE;
	//mac_current_state_saved=MAC_STATE_IDLE;
	phy_channel_state=PHY_IDLE;
	PLME_SET_TRX_STATE_request(RX_ON);
	mac_pib_data.macShortAddress=AssocShortAddress;
	cc2420_setshortaddress((uint16_t)mac_pib_data.macShortAddress.Val);
       cc2420_setpanid((uint16_t)mac_pib_data.macPANId.Val);		
	}
	else 
	{
	   //主要工作由网络层决定
	   mac_current_state=MAC_STATE_ACTIVE_PASSIVE_SCAN_FINISHED;
	}
}

//**************************//
void mlmeAssociateIndication(MLME_ASSOCIATE_INDICATION   mlme_associate_indication_info)
{
	//协调者在收到associate request帧后通知高层
	//可以post高层任务
	BYTE status;
	SHORT_ADDR  shortaddr_alloc;
	//主要任务是调用mlmeassociateresponse原语
	//------------------------------------------------------
	//---------------待添加------------------------------
	//--------网络层是否决定关联-------------------
	//--------修改status,分配shortaddr--------------------
     //printf("\n------success received an association request frame--------\n");
	//for(uint8_t i=0;i<8;i++)
	//	printf("\nthe addr is v[%d]=%d\n",i,mlme_associate_indication_info.Deviceaddr.v[i]);
	//shortaddr_alloc.Val=RFD_SHORT_ADDRESS;
	shortaddr_alloc=short_address_allocate_req(mlme_associate_indication_info.Deviceaddr);
	printf("\n---allocated short addr is %04x---\n",shortaddr_alloc.Val);
	status=0x00;
	mlmeAssociateResponse(mlme_associate_indication_info.Deviceaddr, shortaddr_alloc,status, FALSE);
}
//**************************//
void mlmeAssociateResponse(LONG_ADDR deviceAddress, SHORT_ADDR assocShortAddress, BYTE status, bool securityEnable)
{
	//协调者的高层在收到associate indication原语后响应MAC层的原语
	//MAC层调用发送任务发送associate response帧
	FRAME_CONTROL framectl_field;
	NODE_INFO dst; 
	NODE_INFO src; 
	//下面不用做.
	dst.addrMode=0x03;
	src.addrMode=0x03;
	dst.panID=mac_pib_data.macPANId;
	src.panID=mac_pib_data.macPANId;
       src.longAddr=mac_pib_data.macExtAddress;	
	dst.longAddr=deviceAddress;
	framectl_field.v[0]=0x23;
	framectl_field.v[1]=0xcc;
	
	//
	if((IS_FFD!=0x00)&&(mac_current_state==MAC_STATE_IDLE)&&(current_pending_num<MAX_PENDING_LIST_SIZE))
		{
		    add_pending_queue(0, framectl_field, dst,src,0, NULL, status, TRUE, deviceAddress,assocShortAddress);
		}
	else if(current_pending_num>=MAX_PENDING_LIST_SIZE)	
		{
		   //printf("\npending full\n");
		     //向上层报告
		     //mlme_comm_status()
		}
	//--------------------------------------------------
       //In order to test the disassociation frame from the coordinator
       //LONG_ADDR  RFD_LONG_ADDR;

	//test the coordinator want the RFD leave the network
       //mlmeDisassociateRequest(deviceAddress,0x01, FALSE);

      //---------------------------------------------------
	
		
}

void mlmeDisassociateIndication(LONG_ADDR deviceAddress, BYTE disassociateReason, bool securityUse, BYTE aclEntry)
{
	//协调者或终端设备在收到对方发过来的disassociation notification帧后
	//通知高层。post高层任务
	//空函数,高层实现
	//------------------------------------------------------
	//---------------待添加------------------------------
	//--------网络层删除对应的associate记录--------
	//-------------------------------------------------------
       if(!IS_COORDINATOR)
       {
	 //printf("\n--the RFD received the disassociation indiation----\n");
	   mac_current_state=MAC_STATE_IDLE;
       }  
	else
		{   
		   
	           printf("\n----the coordinator received the disassociation indication----\n");	
	         
		}
	
}



void mlmeDisassociateConfirm(MAC_ENUM status)
{
	//在MAC层发送完成(无论成功与失败),将调用该原语通知高层
	//恢复系统的MAC层的状态变量
  //可以post一个高层任务
  //空函数,高层实现
     if(status==SUCCESS)
     {
         if(!IS_COORDINATOR)
         	{
                   printf("\n---success disassociated from the network-----\n");
		     
         	}
         else
         	{   
         	   
                   printf("\n---success disassociated the object RFD from the network------\n");	
		     
         	}
     }		 
}


//*************************//
void mlmeOrphanIndication(LONG_ADDR orphanAddress, bool securityUse, BYTE aclEntry)
{               
	//协调者或簇头节点在收到一个设备发送过来的orphan nitification帧时候通知高层的原语
	//完成高层任务,该任务主要是调用orphan response原语。
	SHORT_ADDR shortaddress;
	//查找对应orphanAddress的shortaddress.
	//-------------------------------------------
	//-------------------待添加--------------
	//--------------网络层的代码----------
	//shortaddress.Val=0xfc8b;
       //shortaddress.Val=RFD_SHORT_ADDRESS;
	printf("\nsuccess received a orphan notification frame\n");
	
	shortaddress=short_address_allocate_req(orphanAddress);
	mlmeOrphanResponse(orphanAddress, shortaddress, TRUE, FALSE);
}
//*************************//

void mlmeCommStatusIndication(WORD panId, BYTE srcAddrMode, ADDRESS *pSrcAddr, BYTE dstAddrMode, ADDRESS *pDstAddr, BYTE status)
{
	//通知高层显示response帧的传输情况
	//空函数
}


void mlmeRxEnableRequest(bool deferPermit, uint32_t rxOnTime, uint32_t rxOnDuration)
{
	//空函数
}
void mlmeRxEnableConfirm(MAC_ENUM status)
{
	//空函数
}

void mlmeSyncLossIndication(MAC_ENUM lossReason)
{
	//三种原因导致MAC层发送该原语通知高层。
	//可通过post一个高层任务。高层对丢失同步可能要重新做出处理。
	//空函数,高层实现
	NODE_INFO dst;
	//printf("\n--- a PAN ID conlict ----\n");
	if(IS_COORDINATOR)
	{
	//可能需要重新设置一下mac层网络参数
	coordinator_realignment_frame_send(0, dst, default_short_addr);
	mac_current_state=MAC_STATE_IDLE;
	}
	else
	{
	   if(lossReason==BEACON_LOSS)
	   	//网络重启动
	   	mac_current_state=MAC_STATE_UNSTARTED;
	      //或者
	      //mac_current_state=MAC_STATE_ENERGY_SCAN_FINISHED;
	}
}


void mcpsDataConfirm(MAC_ENUM status, BYTE msduHandle)
{
	//作为MAC层对mcpsdatarequest的一个响应,通知高层
	//空函数,高层实现
	printf("\n----sensor data is sent successfull-----\n");
	
	mac_current_state=MAC_STATE_START_TO_SLEEP;
	
}

void mcpsDataIndication(MCPS_DATA_INDICATION mcps_data_indication_info)
{
	//MAC接收到一个数据帧后,通知高层,可post一个高层任务
	//空函数,高层实现
	printf("\n-----success received the sensor data-------\n");
	
	if(IS_COORDINATOR)
	sensor_data_tx_to_pc(mcps_data_indication_info);
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -