/********************************************************************* * INCLUDES */ #include "OSAL.h" #include "ZGlobals.h" #include "AF.h" #include "aps_groups.h" #include "ZDApp.h" #include "SampleApp.h" #include "SampleAppHw.h" #include "OnBoard.h" /* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "MT_UART.h" #include "MT.h" #include "sapi.h" #include /*#include "ZComDef.h" #include "MT.h" #include "MT_NWK.h" #include "NLMEDE.h" #include "nwk.h" #include "nwk_globals.h" #include "nwk_util.h" #include "OSAL.h" #include "ZDApp.h"*/ /********************************************************************* * MACROS */ /********************************************************************* * CONSTANTS */ /********************************************************************* * TYPEDEFS */ /********************************************************************* * GLOBAL VARIABLES */ // This list should be filled with Application specific Cluster IDs. const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] = { SAMPLEAPP_PERIODIC_CLUSTERID, //SAMPLEAPP_PERIODIC_CLUSTERID 1 SAMPLEAPP_FLASH_CLUSTERID //SAMPLEAPP_FLASH_CLUSTERID 2 }; const SimpleDescriptionFormat_t SampleApp_SimpleDesc = { SAMPLEAPP_ENDPOINT, // int Endpoint; //SAMPLEAPP_ENDPOINT---20 SAMPLEAPP_PROFID, // uint16 AppProfId[2]; //SAMPLEAPP_PROFID---0x0F08 SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2]; //SAMPLEAPP_DEVICEID---0x0001 SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4; //SAMPLEAPP_DEVICE_VERSION---0 SAMPLEAPP_FLAGS, // int AppFlags:4; //SAMPLEAPP_FLAGS---0 SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; //SAMPLEAPP_MAX_CLUSTERS---2 (cId_t *)SampleApp_ClusterList, // uint8 *pAppInClusterList; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; //SAMPLEAPP_MAX_CLUSTERS---2 (cId_t *)SampleApp_ClusterList // uint8 *pAppInClusterList; }; // This is the Endpoint/Interface description. It is defined here, but // filled-in in SampleApp_Init(). Another way to go would be to fill // in the structure here and make it a "const" (in code space). The // way it's defined in this sample app it is define in RAM. endPointDesc_t SampleApp_epDesc; /********************************************************************* * EXTERNAL VARIABLES */ /********************************************************************* * EXTERNAL FUNCTIONS */ /********************************************************************* * LOCAL VARIABLES */ uint8 SampleApp_TaskID; // Task ID for internal task/event processing // This variable will be received when // SampleApp_Init() is called. devStates_t SampleApp_NwkState; uint8 SampleApp_TransID; // This is the unique message ID (counter) afAddrType_t Point_To_Point_DstAddr; //点播 uint8 SampleAppPeriodicCounter = 0; uint8 SampleAppFlashCounter = 0; uint16 k; /********************************************************************* * LOCAL FUNCTIONS */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ); void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void SampleApp_SendPeriodicMessage( void ); void SampleApp_SendFlashMessage( uint16 flashTime ); void SampleApp_SerialCMD(mtOSALSerialData_t *cmdMsg); void display_hex_com(uint8 nameid); void SampleApp_SendPointToPointMessage( void ); void SampleApp_Key_Message( void ); /********************************************************************* * NETWORK LAYER CALLBACKS */ /********************************************************************* * PUBLIC FUNCTIONS */ /********************************************************************* * @fn SampleApp_Init * * @brief Initialization function for the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; HalLedSet(HAL_LED_1,1); //这两句是关闭LED的,函数里面的程序改了,没贴出来 HalLedSet(HAL_LED_2,0); #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, // the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to // start the device. ZDOInitDevice(0); #endif Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; Point_To_Point_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; Point_To_Point_DstAddr.addr.shortAddr = 0x0000; // Fill out the endpoint description. SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); } /********************************************************************* * @fn SampleApp_ProcessEvent * * @brief Generic Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return none */ uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) //SYS_EVENT_MSG---0x8000 // A message is waiting event { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { // case AF_INCOMING_MSG_CMD: // SampleApp_MessageMSGCB( MSGpkt ); // break; // Received whenever the device changes state in the network case ZDO_STATE_CHANGE: //ZDO_STATE_CHANGE---0xD1 // ZDO has changed the device's network state SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if ( (SampleApp_NwkState == DEV_ZB_COORD) //协调器 || (SampleApp_NwkState == DEV_ROUTER) //路由器 || (SampleApp_NwkState == DEV_END_DEVICE) ) //终端 { // Start sending the periodic message in a regular interval. osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, //SAMPLEAPP_SEND_PERIODIC_MSG_EVT---0x0001 ,同一任务下有多个事件,这是事件的号码,不能重复 30000 ); //SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT---5000 // Every 5 seconds } else { // Device is no longer in the network } break; default: break; } // Release the memory osal_msg_deallocate( (uint8 *)MSGpkt ); // Next - if one is available MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } // Send a message out - This event is generated by a timer // (setup in SampleApp_Init()). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) { // Send the periodic message HalLedBlink( HAL_LED_1, 4,200, 500 ); SampleApp_SendPointToPointMessage(); // Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (30000 ) ); //+ (osal_rand() & 0x00FF) // return unprocessed events return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); } // Discard unknown events return 0; } void display_hex_com(uint8 nameid) //display ieee.shortadress.panid.channel { uint8 channel,i,*extaddr,hex_value[19],asc16[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; uint16 panId,shortaddr; hex_value[0]='0'; hex_value[1]='x'; switch(nameid) { case 0: //IEEE HalUARTWrite(0,"My IEEE address is:",19); extaddr = NLME_GetExtAddr(); for(i=2;i<18;i++,i++) { hex_value[i] = asc16[extaddr[(16-i)/2]/16]; hex_value[i+1] = asc16[extaddr[(16-i)/2]%16]; } hex_value[18] = '\n'; HalUARTWrite(0,hex_value,19); break; case 1: //panid HalUARTWrite(0,"My network ID is:",17); panId=_NIB.nwkPanId; //osal_memcpy(&panId, &_NIB.nwkPanId, sizeof(uint16)); hex_value[2] = asc16[(panId/16/16/16)%16]; hex_value[3] = asc16[(panId/16/16)%16]; hex_value[4] = asc16[(panId/16)%16]; hex_value[5] = asc16[panId%16]; hex_value[6] = '\n'; HalUARTWrite(0,hex_value,7); break; case 2: //shortaddress shortaddr = NLME_GetShortAddr(); HalUARTWrite(0,"My shortaddr is:",16); hex_value[2] = asc16[(shortaddr/16/16/16)%16]; hex_value[3] = asc16[(shortaddr/16/16)%16]; hex_value[4] = asc16[(shortaddr/16)%16]; hex_value[5] = asc16[shortaddr%16]; hex_value[6] = '\n'; HalUARTWrite(0,hex_value,7); break; case 3: //information channel //osal_memcpy(&channel, &_NIB.nwkLogicalChannel, sizeof(uint8)); channel = _NIB.nwkLogicalChannel; HalUARTWrite(0,"My info channel is:",19); hex_value[2] = asc16[channel/16]; hex_value[3] = asc16[channel%16]; hex_value[4] = '\n'; HalUARTWrite(0,hex_value,5); break; default : HalUARTWrite(0,"Input error",5); } } void SampleApp_SendPointToPointMessage( void ) { uint8 asc16[10]={'0','1','2','3','4','5','6','7','8','9'}; //设备类型变量 uint8 device[13]="ED_N:01_D:000"; k++;if(1000==k)k=0; device[12]=asc16[k%10]; device[11]=asc16[k/10%10]; device[10]=asc16[k/100%10]; if ( AF_DataRequest( &Point_To_Point_DstAddr, //afAddrType_t *dstAddr, 目的地址。其中地址模式可以是: afAddrNotPresent 绑定模式地址、 afAddrGroup组播地址、 //afAddrBroadcast广播地址、afAddr16Bit直接传输(单播)地址。 &SampleApp_epDesc, // endPointDesc_t *srcEP, 源端点 SAMPLEAPP_POINT_TO_POINT_CLUSTERID, //自己定义的 簇ID号(Cluster ID) 13, //uint16 len, buf域的长度,发送数据的字节数。 device, //uint8 *buf, 准备发送的数据 &SampleApp_TransID, //uint8 *transID, 传输序列号 AF_DISCV_ROUTE, //uint8 options, 0x10:AF_ACK_REQUEST 、 0x20:AF_DISCV_ROUTE 、 0x40:AF_EN_SECURITY 、0x80:AF_SKIP_ROUTING AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) //afStatus_SUCCESS---ZSuccess---SUCCESS---0x00 //uint8 radius )最大的跳数30 { } else { // Error occurred in request to send. } } /********************************************************************* *********************************************************************/