123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823 |
- #include "OSAL.h"
- #include "AF.h"
- #include "ZDObject.h"
- #include "ZDProfile.h"
- #include "TransmitApp.h"
- #include "OnBoard.h"
- #include "DebugTrace.h"
- #include "hal_lcd.h"
- #include "hal_led.h"
- #include "hal_key.h"
- #include "hal_uart.h"
- #define TRANSMITAPP_STATE_WAITING 0
- #define TRANSMITAPP_STATE_SENDING 1
- #if !defined ( RTR_NWK )
-
-
- #define TRANSMITAPP_DELAY_SEND
- #define TRANSMITAPP_SEND_DELAY (RESPONSE_POLL_RATE * 2)
- #endif
- #define TRANSMITAPP_TX_OPTIONS AF_DISCV_ROUTE
- #define TRANSMITAPP_INITIAL_MSG_COUNT 2
- #define TRANSMITAPP_TRANSMIT_TIME 4
- #define TRANSMITAPP_DISPLAY_TIMER (2 * 1000)
- #if defined ( TRANSMITAPP_FRAGMENTED )
- #define TRANSMITAPP_MAX_DATA_LEN 225
- #else
- #define TRANSMITAPP_MAX_DATA_LEN 102
- #endif
- byte TransmitApp_Msg[ TRANSMITAPP_MAX_DATA_LEN ];
- const cId_t TransmitApp_ClusterList[TRANSMITAPP_MAX_CLUSTERS] =
- {
- TRANSMITAPP_CLUSTERID_TESTMSG
- };
- const SimpleDescriptionFormat_t TransmitApp_SimpleDesc =
- {
- TRANSMITAPP_ENDPOINT,
- TRANSMITAPP_PROFID,
- TRANSMITAPP_DEVICEID,
- TRANSMITAPP_DEVICE_VERSION,
- TRANSMITAPP_FLAGS,
- TRANSMITAPP_MAX_CLUSTERS,
- (cId_t *)TransmitApp_ClusterList,
- TRANSMITAPP_MAX_CLUSTERS,
- (cId_t *)TransmitApp_ClusterList
- };
- endPointDesc_t TransmitApp_epDesc;
- byte TransmitApp_TaskID;
- devStates_t TransmitApp_NwkState;
- static byte TransmitApp_TransID;
- afAddrType_t TransmitApp_DstAddr;
- byte TransmitApp_State;
- static uint32 clkShdw;
- static uint32 rxTotal, txTotal;
- static uint32 rxAccum, txAccum;
- static byte timerOn;
- static byte timesToSend;
- uint16 pktCounter;
- uint16 TransmitApp_MaxDataLength;
- void TransmitApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
- void TransmitApp_HandleKeys( byte shift, byte keys );
- void TransmitApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
- void TransmitApp_SendTheMessage( void );
- void TransmitApp_ChangeState( void );
- void TransmitApp_DisplayResults( void );
- void TransmitApp_Init( byte task_id )
- {
- #if !defined ( TRANSMITAPP_FRAGMENTED )
- afDataReqMTU_t mtu;
- #endif
- uint16 i;
- TransmitApp_TaskID = task_id;
- TransmitApp_NwkState = DEV_INIT;
- TransmitApp_TransID = 0;
- pktCounter = 0;
- TransmitApp_State = TRANSMITAPP_STATE_WAITING;
-
-
-
- TransmitApp_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
- TransmitApp_DstAddr.endPoint = 0;
- TransmitApp_DstAddr.addr.shortAddr = 0;
-
- TransmitApp_epDesc.endPoint = TRANSMITAPP_ENDPOINT;
- TransmitApp_epDesc.task_id = &TransmitApp_TaskID;
- TransmitApp_epDesc.simpleDesc
- = (SimpleDescriptionFormat_t *)&TransmitApp_SimpleDesc;
- TransmitApp_epDesc.latencyReq = noLatencyReqs;
-
- afRegister( &TransmitApp_epDesc );
-
- RegisterForKeys( TransmitApp_TaskID );
-
- #if defined ( LCD_SUPPORTED )
- HalLcdWriteString( "TransmitApp", HAL_LCD_LINE_2 );
- #endif
-
- #if defined ( TRANSMITAPP_FRAGMENTED )
- TransmitApp_MaxDataLength = TRANSMITAPP_MAX_DATA_LEN;
- #else
- mtu.kvp = FALSE;
- mtu.aps.secure = FALSE;
- TransmitApp_MaxDataLength = afDataReqMTU( &mtu );
- #endif
-
- for (i=0; i<TransmitApp_MaxDataLength; i++)
- {
- TransmitApp_Msg[i] = (uint8) i;
- }
- ZDO_RegisterForZDOMsg( TransmitApp_TaskID, End_Device_Bind_rsp );
- ZDO_RegisterForZDOMsg( TransmitApp_TaskID, Match_Desc_rsp );
- }
- UINT16 TransmitApp_ProcessEvent( byte task_id, UINT16 events )
- {
- afIncomingMSGPacket_t *MSGpkt;
- afDataConfirm_t *afDataConfirm;
- (void)task_id;
-
- ZStatus_t sentStatus;
- byte sentEP;
- if ( events & SYS_EVENT_MSG )
- {
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( TransmitApp_TaskID );
- while ( MSGpkt )
- {
- switch ( MSGpkt->hdr.event )
- {
- case ZDO_CB_MSG:
- TransmitApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
- break;
- case KEY_CHANGE:
- TransmitApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
- break;
- case AF_DATA_CONFIRM_CMD:
-
-
-
- afDataConfirm = (afDataConfirm_t *)MSGpkt;
- sentEP = afDataConfirm->endpoint;
- sentStatus = afDataConfirm->hdr.status;
- if ( (ZSuccess == sentStatus) &&
- (TransmitApp_epDesc.endPoint == sentEP) )
- {
- #if !defined ( TRANSMITAPP_RANDOM_LEN )
- txAccum += TransmitApp_MaxDataLength;
- #endif
- if ( !timerOn )
- {
- osal_start_timerEx( TransmitApp_TaskID,TRANSMITAPP_RCVTIMER_EVT,
- TRANSMITAPP_DISPLAY_TIMER);
- clkShdw = osal_GetSystemClock();
- timerOn = TRUE;
- }
- }
-
- TransmitApp_SetSendEvt();
- break;
- case AF_INCOMING_MSG_CMD:
- TransmitApp_MessageMSGCB( MSGpkt );
- break;
- case ZDO_STATE_CHANGE:
- TransmitApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
- break;
- default:
- break;
- }
-
- osal_msg_deallocate( (uint8 *)MSGpkt );
-
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( TransmitApp_TaskID );
- }
-
- (void)sentStatus;
- (void)sentEP;
-
- return (events ^ SYS_EVENT_MSG);
- }
-
- if ( events & TRANSMITAPP_SEND_MSG_EVT )
- {
- if ( TransmitApp_State == TRANSMITAPP_STATE_SENDING )
- {
- TransmitApp_SendTheMessage();
- }
-
- return (events ^ TRANSMITAPP_SEND_MSG_EVT);
- }
-
- if ( events & TRANSMITAPP_SEND_ERR_EVT )
- {
- TransmitApp_SetSendEvt();
-
- return (events ^ TRANSMITAPP_SEND_ERR_EVT);
- }
-
- if ( events & TRANSMITAPP_RCVTIMER_EVT )
- {
-
- osal_start_timerEx( TransmitApp_TaskID, TRANSMITAPP_RCVTIMER_EVT,
- TRANSMITAPP_DISPLAY_TIMER );
- TransmitApp_DisplayResults();
- return (events ^ TRANSMITAPP_RCVTIMER_EVT);
- }
-
- return 0;
- }
- void TransmitApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
- {
- switch ( inMsg->clusterID )
- {
- case End_Device_Bind_rsp:
- if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
- {
-
- HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
- }
- #if defined(BLINK_LEDS)
- else
- {
-
- HalLedSet ( HAL_LED_4, HAL_LED_MODE_FLASH );
- }
- #endif
- break;
- case Match_Desc_rsp:
- {
- ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
- if ( pRsp )
- {
- if ( pRsp->status == ZSuccess && pRsp->cnt )
- {
- TransmitApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
- TransmitApp_DstAddr.addr.shortAddr = pRsp->nwkAddr;
-
- TransmitApp_DstAddr.endPoint = pRsp->epList[0];
-
- HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
- }
- osal_mem_free( pRsp );
- }
- }
- break;
- }
- }
- void TransmitApp_HandleKeys( byte shift, byte keys )
- {
- zAddrType_t dstAddr;
-
- if ( shift )
- {
- if ( keys & HAL_KEY_SW_1 )
- {
- }
- if ( keys & HAL_KEY_SW_2 )
- {
- }
- if ( keys & HAL_KEY_SW_3 )
- {
- }
- if ( keys & HAL_KEY_SW_4 )
- {
- }
- }
- else
- {
- if ( keys & HAL_KEY_SW_1 )
- {
- TransmitApp_ChangeState();
- }
- if ( keys & HAL_KEY_SW_2 )
- {
- HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
-
- dstAddr.addrMode = Addr16Bit;
- dstAddr.addr.shortAddr = 0x0000;
- ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
- TransmitApp_epDesc.endPoint,
- TRANSMITAPP_PROFID,
- TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
- TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
- FALSE );
- }
- if ( keys & HAL_KEY_SW_3 )
- {
- rxTotal = txTotal = 0;
- rxAccum = txAccum = 0;
- TransmitApp_DisplayResults();
- }
- if ( keys & HAL_KEY_SW_4 )
- {
- HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
-
- dstAddr.addrMode = AddrBroadcast;
- dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
- ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
- TRANSMITAPP_PROFID,
- TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
- TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
- FALSE );
- }
- }
- }
- void TransmitApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
- {
- uint16 i;
- uint8 error = FALSE;
- switch ( pkt->clusterId )
- {
- case TRANSMITAPP_CLUSTERID_TESTMSG:
- #if !defined ( TRANSMITAPP_RANDOM_LEN )
- if (pkt->cmd.DataLength != TransmitApp_MaxDataLength)
- {
- error = TRUE;
- }
- #endif
- for (i=4; i<pkt->cmd.DataLength; i++)
- {
- if (pkt->cmd.Data[i] != i%256)
- error = TRUE;
- }
- if (error)
- {
-
- HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
- }
- else
- {
- if ( !timerOn )
- {
- osal_start_timerEx( TransmitApp_TaskID, TRANSMITAPP_RCVTIMER_EVT,
- TRANSMITAPP_DISPLAY_TIMER );
- clkShdw = osal_GetSystemClock();
- timerOn = TRUE;
- }
- rxAccum += pkt->cmd.DataLength;
- }
- break;
-
- default:
- break;
- }
- }
- void TransmitApp_SendTheMessage( void )
- {
- uint16 len;
- uint8 tmp;
- do {
-
- tmp = HI_UINT8( TransmitApp_TransID );
- tmp += (tmp <= 9) ? ('0') : ('A' - 0x0A);
- TransmitApp_Msg[2] = tmp;
- tmp = LO_UINT8( TransmitApp_TransID );
- tmp += (tmp <= 9) ? ('0') : ('A' - 0x0A);
- TransmitApp_Msg[3] = tmp;
- len = TransmitApp_MaxDataLength;
- #if defined ( TRANSMITAPP_RANDOM_LEN )
- len = (uint8)(osal_rand() & 0x7F);
- if( len > TransmitApp_MaxDataLength || len == 0 )
- len = TransmitApp_MaxDataLength;
- else if ( len < 4 )
- len = 4;
- #endif
-
- tmp = AF_DataRequest( &TransmitApp_DstAddr, &TransmitApp_epDesc,
- TRANSMITAPP_CLUSTERID_TESTMSG,
- len, TransmitApp_Msg,
- &TransmitApp_TransID,
- TRANSMITAPP_TX_OPTIONS,
- AF_DEFAULT_RADIUS );
- #if defined ( TRANSMITAPP_RANDOM_LEN )
- if ( tmp == afStatus_SUCCESS )
- {
- txAccum += len;
- }
- #endif
- if ( timesToSend )
- {
- timesToSend--;
- }
- } while ( (timesToSend != 0) && (afStatus_SUCCESS == tmp) );
- if ( afStatus_SUCCESS == tmp )
- {
- pktCounter++;
- }
- else
- {
-
- osal_start_timerEx( TransmitApp_TaskID, TRANSMITAPP_SEND_ERR_EVT, 10 );
- }
- }
- void TransmitApp_ChangeState( void )
- {
- if ( TransmitApp_State == TRANSMITAPP_STATE_WAITING )
- {
- TransmitApp_State = TRANSMITAPP_STATE_SENDING;
- TransmitApp_SetSendEvt();
- timesToSend = TRANSMITAPP_INITIAL_MSG_COUNT;
- }
- else
- {
- TransmitApp_State = TRANSMITAPP_STATE_WAITING;
- }
- }
- void TransmitApp_SetSendEvt( void )
- {
- #if defined( TRANSMITAPP_DELAY_SEND )
-
- osal_start_timerEx( TransmitApp_TaskID,
- TRANSMITAPP_SEND_MSG_EVT, TRANSMITAPP_SEND_DELAY );
- #else
-
- osal_set_event( TransmitApp_TaskID, TRANSMITAPP_SEND_MSG_EVT );
- #endif
- }
- void TransmitApp_DisplayResults( void )
- {
- #ifdef LCD_SUPPORTED
- #define LCD_W 16
- uint32 rxShdw, txShdw, tmp;
- byte lcd_buf[LCD_W+1];
- byte idx;
- #endif
-
- uint32 msecs = osal_GetSystemClock() - clkShdw;
- clkShdw = osal_GetSystemClock();
- rxTotal += rxAccum;
- txTotal += txAccum;
- #if defined ( LCD_SUPPORTED )
- rxShdw = (rxAccum * 1000 + msecs/2) / msecs;
- txShdw = (txAccum * 1000 + msecs/2) / msecs;
- osal_memset( lcd_buf, ' ', LCD_W );
- lcd_buf[LCD_W] = NULL;
- idx = 4;
- tmp = (rxShdw >= 100000) ? 99999 : rxShdw;
- do
- {
- lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
- tmp /= 10;
- } while ( tmp );
- idx = LCD_W-1;
- tmp = rxTotal;
- do
- {
- lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
- tmp /= 10;
- } while ( tmp );
- HalLcdWriteString( (char*)lcd_buf, HAL_LCD_LINE_1 );
- osal_memset( lcd_buf, ' ', LCD_W );
- idx = 4;
- tmp = (txShdw >= 100000) ? 99999 : txShdw;
- do
- {
- lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
- tmp /= 10;
- } while ( tmp );
- idx = LCD_W-1;
- tmp = txTotal;
- do
- {
- lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
- tmp /= 10;
- } while ( tmp );
- HalLcdWriteString( (char*)lcd_buf, HAL_LCD_LINE_2 );
- #elif defined( MT_TASK )
- DEBUG_INFO( COMPID_APP, SEVERITY_INFORMATION, 3,
- rxAccum, (uint16)msecs, (uint16)rxTotal );
- #else
- (void)msecs;
- #endif
- if ( (rxAccum == 0) && (txAccum == 0) )
- {
- osal_stop_timerEx( TransmitApp_TaskID, TRANSMITAPP_RCVTIMER_EVT );
- timerOn = FALSE;
- }
- rxAccum = txAccum = 0;
- }
|