/*************************************************************************************************** * @fn MT_OtaCommandProcessing * * @brief Process all the MT OTA commands that are issued by the OTA Console tool * * @param pBuf - pointer to the msg buffer * * | LEN | CMD0 | CMD1 | DATA | * | 1 | 1 | 1 | 0-255 | * * @return status ***************************************************************************************************/ uint8 MT_OtaCommandProcessing(uint8* pBuf) { uint8 status = MT_RPC_SUCCESS; uint8 len; OTA_MtMsg_t *pMsg; uint8 cmd = pBuf[MT_RPC_POS_CMD1]; if (cmd == MT_OTA_FILE_READ_RSP || cmd == MT_OTA_NEXT_IMG_RSP) { // Forward the message to the task if (OTA_Task != 0xff) { len = pBuf[MT_RPC_POS_LEN]; pMsg = (OTA_MtMsg_t*) osal_msg_allocate(len + sizeof(OTA_MtMsg_t)); if (pMsg) { pMsg->hdr.event = MT_SYS_OTA_MSG; pMsg->cmd = cmd; osal_memcpy(pMsg->data, &pBuf[MT_RPC_POS_DAT0], len); osal_msg_send(OTA_Task, (uint8*) pMsg); } } } else { status = MT_RPC_ERR_COMMAND_ID; } return status; }
/********************************************************************* * @fn debug_str * * @brief * * This feature allows modules to display a debug text string as * applications execute in real-time. This feature will output to * the serial port for display in the Z-Test tool. * * This feature will most likely be compiled out in the production * code in order to save code space. * * @param byte *str_ptr - pointer to null-terminated string * * @return void */ void debug_str( byte *str_ptr ) { mtDebugStr_t *msg; byte mln; byte strLen; // Text string length strLen = (byte)osal_strlen( (void*)str_ptr ); // Debug string message length mln = sizeof ( mtDebugStr_t ) + strLen; // Get a message buffer to build the debug message msg = (mtDebugStr_t *)osal_msg_allocate( mln ); if ( msg ) { // Message type, length msg->hdr.event = CMD_DEBUG_STR; msg->strLen = strLen; // Append message, no terminator msg->pString = (uint8 *)(msg+1); osal_memcpy ( msg->pString, str_ptr, strLen ); osal_msg_send( MT_TaskID, (uint8 *)msg ); } } // debug_str()
void MT_UartProcessZToolData ( uint8 port, uint8 event ) { uint8 flag=0,i,j=0; //flag是判断有没有收到数据,j记录数据长度 uint8 buf[128]; //串口buffer最大缓冲默认是128,我们这里用128. (void)event; // Intentionally unreferenced parameter while (Hal_UART_RxBufLen(port)) //检测串口数据是否接收完成 { HalUARTRead (port,&buf[j], 1); //把数据接收放到buf中 j++; //记录字符数 flag=1; //已经从串口接收到信息 } if(flag==1) //已经从串口接收到信息 { /* Allocate memory for the data */ //分配内存空间,为机构体内容+数据内容+1个记录长度的数据 pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof ( mtOSALSerialData_t )+j+1); //事件号用原来的CMD_SERIAL_MSG pMsg->hdr.event = CMD_SERIAL_MSG; pMsg->msg = (uint8*)(pMsg+1); // 把数据定位到结构体数据部分 pMsg->msg [0]= j; //给上层的数据第一个是长度 for(i=0;i<j;i++) //从第二个开始记录数据 pMsg->msg [i+1]= buf[i]; osal_msg_send( App_TaskID, (byte *)pMsg ); //登记任务,发往上层 /* deallocate the msg */ osal_msg_deallocate ( (uint8 *)pMsg ); //释放内存 } }
/********************************************************************* * @fn ZDO_SendMsgCBs * * @brief De-mux the ZDO_CB_MSG from the ZNP. * * @param inMsg - pointer to the incoming message buffer. * * @return none */ static void ZDO_SendMsgCBs(zdoIncomingMsg_t *inMsg) { ZDO_MsgCB_t *pList = zdoMsgCBs; while ( pList ) { if ( pList->clusterID == inMsg->clusterID ) { zdoIncomingMsg_t *msgPtr; // Send the address to the task msgPtr = (zdoIncomingMsg_t *)osal_msg_allocate( sizeof( zdoIncomingMsg_t ) + inMsg->asduLen ); if ( msgPtr ) { // copy struct osal_memcpy( msgPtr, inMsg, sizeof( zdoIncomingMsg_t )); if ( inMsg->asduLen ) { msgPtr->asdu = (byte*)(((byte*)msgPtr) + sizeof( zdoIncomingMsg_t )); osal_memcpy( msgPtr->asdu, inMsg->asdu, inMsg->asduLen ); } msgPtr->hdr.event = ZDO_CB_MSG; osal_msg_send( pList->taskID, (uint8 *)msgPtr ); } } pList = (ZDO_MsgCB_t *)pList->next; } }
/********************************************************************* * @fn debug_msg * * @brief * * This feature allows modules to display debug information as * applications execute in real-time. This feature will work similar * to "printf()" but will output to the serial port for display in * the Z-Test tool. * * This feature will most likely be compiled out in the production code * to save code space. * * @param byte compID - Component ID * @param byte severity - CRITICAL(0x01), ERROR(0x02), INFORMATION(0x03) * or TRACE(0x04) * @param byte numParams - number of parameter fields (param1-3) * @param UINT16 param1 - user defined data * @param UINT16 param2 - user defined data * @param UINT16 param3 - user defined data * * @return void */ void debug_msg( byte compID, byte severity, byte numParams, UINT16 param1, UINT16 param2, UINT16 param3 ) { mtDebugMsg_t *mtDebugMsg; UINT16 timestamp; if ( debugThreshold == 0 || debugCompId != compID ) return; // Fill in the timestamp timestamp = 0; // Get a message buffer to build the debug message mtDebugMsg = (mtDebugMsg_t *)osal_msg_allocate( sizeof( mtDebugMsg_t ) ); if ( mtDebugMsg ) { mtDebugMsg->hdr.event = CMD_DEBUG_MSG; mtDebugMsg->compID = compID; mtDebugMsg->severity = severity; mtDebugMsg->numParams = numParams; mtDebugMsg->param1 = param1; mtDebugMsg->param2 = param2; mtDebugMsg->param3 = param3; mtDebugMsg->timestamp = timestamp; osal_msg_send( MT_TaskID, (uint8 *)mtDebugMsg ); } } /* debug_msg() */
/********************************************************************* * @fn afBuildMSGIncoming * * @brief Build the message for the app * * @param * * @return pointer to next in data buffer */ static void afBuildMSGIncoming( aps_FrameFormat_t *aff, endPointDesc_t *epDesc, zAddrType_t *SrcAddress, uint16 SrcPanId, NLDE_Signal_t *sig, uint8 nwkSeqNum, uint8 SecurityUse, uint32 timestamp, uint8 radius ) { afIncomingMSGPacket_t *MSGpkt; const uint8 len = sizeof( afIncomingMSGPacket_t ) + aff->asduLength; uint8 *asdu = aff->asdu; MSGpkt = (afIncomingMSGPacket_t *)osal_msg_allocate( len ); if ( MSGpkt == NULL ) { return; } MSGpkt->hdr.event = AF_INCOMING_MSG_CMD; MSGpkt->groupId = aff->GroupID; MSGpkt->clusterId = aff->ClusterID; afCopyAddress( &MSGpkt->srcAddr, SrcAddress ); MSGpkt->srcAddr.endPoint = aff->SrcEndPoint; MSGpkt->endPoint = epDesc->endPoint; MSGpkt->wasBroadcast = aff->wasBroadcast; MSGpkt->LinkQuality = sig->LinkQuality; MSGpkt->correlation = sig->correlation; MSGpkt->rssi = sig->rssi; MSGpkt->SecurityUse = SecurityUse; MSGpkt->timestamp = timestamp; MSGpkt->nwkSeqNum = nwkSeqNum; MSGpkt->macSrcAddr = aff->macSrcAddr; MSGpkt->macDestAddr = aff->macDestAddr; MSGpkt->srcAddr.panId = SrcPanId; MSGpkt->cmd.TransSeqNumber = 0; MSGpkt->cmd.DataLength = aff->asduLength; MSGpkt->radius = radius; if ( MSGpkt->cmd.DataLength ) { MSGpkt->cmd.Data = (uint8 *)(MSGpkt + 1); osal_memcpy( MSGpkt->cmd.Data, asdu, MSGpkt->cmd.DataLength ); } else { MSGpkt->cmd.Data = NULL; } #if defined ( MT_AF_CB_FUNC ) // If ZDO or SAPI have registered for this endpoint, dont intercept it here if (AFCB_CHECK(CB_ID_AF_DATA_IND, *(epDesc->task_id))) { MT_AfIncomingMsg( (void *)MSGpkt ); // Release the memory. osal_msg_deallocate( (void *)MSGpkt ); } else #endif { // Send message through task message. osal_msg_send( *(epDesc->task_id), (uint8 *)MSGpkt ); } }
/*************************************************************************************************** * @fn MT_UartProcessZAppData * * @brief | SOP | CMD | Data Length | FSC | * | 1 | 2 | 1 | 1 | * * Parses the data and determine either is SPI or just simply serial data * then send the data to correct place (MT or APP) * * @param port - UART port * event - Event that causes the callback * * * @return None ***************************************************************************************************/ void MT_UartProcessZAppData ( uint8 port, uint8 event ) { osal_event_hdr_t *msg_ptr; uint16 length = 0; uint16 rxBufLen = Hal_UART_RxBufLen(MT_UART_DEFAULT_PORT); /* If maxZAppBufferLength is 0 or larger than current length the entire length of the current buffer is returned. */ if ((MT_UartMaxZAppBufLen != 0) && (MT_UartMaxZAppBufLen <= rxBufLen)) { length = MT_UartMaxZAppBufLen; } else { length = rxBufLen; } /* Verify events */ if (event == HAL_UART_TX_FULL) { // Do something when TX if full return; } if (event & ( HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) { if ( App_TaskID ) { /* If Application is ready to receive and there is something in the Rx buffer then send it up */ if ((MT_UartZAppRxStatus == MT_UART_ZAPP_RX_READY ) && (length != 0)) { /* Disable App flow control until it processes the current data */ MT_UartAppFlowControl (MT_UART_ZAPP_RX_NOT_READY); /* 2 more bytes are added, 1 for CMD type, other for length */ msg_ptr = (osal_event_hdr_t *)osal_msg_allocate( length + sizeof(osal_event_hdr_t) ); if ( msg_ptr ) { msg_ptr->event = SPI_INCOMING_ZAPP_DATA; msg_ptr->status = length; /* Read the data of Rx buffer */ HalUARTRead( MT_UART_DEFAULT_PORT, (uint8 *)(msg_ptr + 1), length ); /* Send the raw data to application...or where ever */ osal_msg_send( App_TaskID, (uint8 *)msg_ptr ); } } } } }
/****************************************************************************** * @fn StubAPS_ZMacCallback * * @brief This function accepts an inter-PAN message from ZMac. * * @param msgPtr - received message * * @return TRUE if message is processed. FALSE otherwise. */ uint8 StubAPS_ZMacCallback( uint8 *msgPtr ) { uint16 nwk_fc; uint8 aps_fc; uint8 frameType; uint8 *buf = NULL; uint8 event = ((osal_event_hdr_t *)msgPtr)->event; if ( event == MAC_MCPS_DATA_IND ) { buf = ((macMcpsDataInd_t *)msgPtr)->msdu.p; } else if ( event == MAC_MCPS_DATA_CNF ) { buf = ((macMcpsDataCnf_t *)msgPtr)->pDataReq->msdu.p; } if ( buf ) { // get the NWK frame control nwk_fc = BUILD_UINT16( buf[NWK_HDR_FRAME_CTRL_LSB], buf[NWK_HDR_FRAME_CTRL_MSB] ); // frame type frameType = (uint8)((nwk_fc >> NWK_FC_FRAME_TYPE) & NWK_FC_FRAME_TYPE_MASK); // check if incoming frame is of the right type if ( frameType != STUB_NWK_FRAME_TYPE ) { // message doesn't belong to Stub APS return ( FALSE ); } // get the APS frame control aps_fc = buf[STUB_APS_HDR_FRAME_CTRL]; // frame type frameType = aps_fc & APS_FRAME_TYPE_MASK; // check if incoming frame is of the right type if ( frameType != STUB_APS_FRAME ) { // message doesn't belong to Stub APS return ( FALSE ); } // message belongs to Stub APS osal_msg_send( StubAPS_TaskID, (uint8 *)msgPtr ); return ( TRUE ); } // message doesn't belong to Stub APS return ( FALSE ); } /* StubAPS_ZMacCallback */
/************************************************************************************************** * @fn afRetrieve * * @brief This function retrieves the data of a huge incoming message. On an failure during * the retrieval, the incoming message is freed. Otherwise, the incoming message is * forwarded to the corresponding task. * * input parameters * * @param pMsg - Pointer to the incoming AF message. * @param taskId - The task ID corresponding to the destination endpoint of the message. * * output parameters * * @param pMsg->cmd.Data - The incoming message data buffer member is filled. * * @return None. ************************************************************************************************** */ static void afRetrieve(uint8 taskId, afIncomingMSGPacket_t *pMsg) { #define ZAP_AF_RTV_MSG_HDR 7 // Retrieve message header length. #define ZAP_AF_RTV_RPY_HDR 2 // Retrieve-reply message header length. #define ZAP_AF_RTV_DAT_MAX (MT_RPC_DATA_MAX - ZAP_AF_RTV_RPY_HDR) uint16 idx = 0, len = pMsg->cmd.DataLength; uint8 *pBuf, rtrn, tmpLen = 0; do { /* This trick to pre-decrement (with zero on the first pass) allows the while() test to * succeed and loop to send a zero data length message which will trigger the ZNP to * de-allocate the huge incoming message being held. */ len -= tmpLen; idx += tmpLen; if (len > ZAP_AF_RTV_DAT_MAX) { tmpLen = ZAP_AF_RTV_DAT_MAX; } else { tmpLen = len; } if ((pBuf = zap_msg_allocate(ZAP_AF_RTV_MSG_HDR, ((uint8)MT_RPC_SYS_AF | MT_RPC_CMD_SREQ), MT_AF_DATA_RETRIEVE)) == NULL) { rtrn = afStatus_MEM_FAIL; break; } pBuf[0] = BREAK_UINT32(pMsg->timestamp, 0); pBuf[1] = BREAK_UINT32(pMsg->timestamp, 1); pBuf[2] = BREAK_UINT32(pMsg->timestamp, 2); pBuf[3] = BREAK_UINT32(pMsg->timestamp, 3); pBuf[4] = LO_UINT16(idx); pBuf[5] = HI_UINT16(idx); pBuf[6] = tmpLen; zapPhySend(zapAppPort, pBuf); rtrn = (afStatus_t)ZAP_SRSP_STATUS(pBuf); (void)osal_memcpy(pMsg->cmd.Data+idx, pBuf+ZAP_AF_RTV_RPY_HDR, tmpLen); zap_msg_deallocate(&pBuf); } while ((rtrn == afStatus_SUCCESS) && len); if (rtrn == afStatus_SUCCESS) { (void)osal_msg_send(taskId, (uint8 *)pMsg); } else { (void)osal_msg_deallocate((uint8 *)pMsg); } }
void zapPhySpiPoll(uint8 port) { #if ZAP_SBL_PROXY extern uint8 zapSBL_Active; #endif uint8 pPoll[MT_RPC_FRAME_HDR_SZ]; #if ZAP_SBL_PROXY if (!HAL_ZNP_SRDY_SET() || zapSBL_Active) #else if (!HAL_ZNP_SRDY_SET()) #endif { return; } HAL_ZNP_MRDY_SET(); // MRDY must be set before talking to the slave. osal_memset(pPoll, 0, MT_RPC_FRAME_HDR_SZ); HalSpiWrite(port, pPoll, MT_RPC_FRAME_HDR_SZ); if (getSRDY2(port)) { osal_memset(pPoll, 0, MT_RPC_FRAME_HDR_SZ); HalSpiWrite(port, pPoll, MT_RPC_FRAME_HDR_SZ); if (MT_RPC_CMD_AREQ == (pPoll[MT_RPC_POS_CMD0] & MT_RPC_CMD_TYPE_MASK)) { mtOSALSerialData_t *pMsg = (mtOSALSerialData_t *)osal_msg_allocate(sizeof(mtOSALSerialData_t) + MT_RPC_FRAME_HDR_SZ + *pPoll); if (NULL != pMsg) { pMsg->hdr.event = CMD_SERIAL_MSG; pMsg->hdr.status = port; pMsg->msg = (uint8 *)(pMsg + 1); osal_memcpy(pMsg->msg, pPoll, MT_RPC_FRAME_HDR_SZ); if (*pPoll) { HalSpiWrite(port, pMsg->msg+MT_RPC_POS_DAT0, *pPoll); } HAL_ZNP_MRDY_CLR(); // Faster release of ZNP with redundant mrdy clear here. osal_msg_send(zapTaskId, (uint8 *)pMsg); } else // Cannot leave the slave hanging in its "ready-to-send" state. { HalSpiFlush(port, *pPoll); } } } HAL_ZNP_MRDY_CLR(); }
/************************************************************************************************** * * @fn HalUARTCBack * * @brief This routine handles events of UART * * @param port - serial port that has the event * event - Event that cause the callback * * @return * **************************************************************************************************/ void HalUARTCBack (uint8 port, uint8 event){ /*only idle timeout on rx buffer is handled*/ //printvalue("UART callback evt",event); if ((port == HAL_UART_PORT) && (event == HAL_UART_RX_TIMEOUT)){ mymessage = (uint8*) osal_msg_allocate(sizeof (uint8)); if (mymessage!= NULL){ *mymessage = MSA_UART_RX_TIMEOUT; osal_msg_send(MSA_TaskId,mymessage); } } }
/************************************************************************************************** * @fn afRecv * * @brief This function de-muxes an incoming AF data message. * * input parameters * * @param pBuf - Pointer to the RPC message buffer. * * output parameters * * None. * * @return None. ************************************************************************************************** */ static void afRecv(uint8 *pBuf) { #define ZAP_AF_INC_MSG_HDR 27 #define ZAP_AF_INC_DAT_MAX (MT_RPC_DATA_MAX - ZAP_AF_INC_MSG_HDR) afIncomingMSGPacket_t *pMsg; epList_t *pEP; uint16 tmp; uint8 cmd1 = pBuf[MT_RPC_POS_CMD1]; pBuf += MT_RPC_FRAME_HDR_SZ; if (cmd1 == MT_AF_INCOMING_MSG) { pEP = afFindEndPointDescList(pBuf[7]); tmp = pBuf[16]; } else { pEP = afFindEndPointDescList(pBuf[16]); tmp = BUILD_UINT16(pBuf[25], pBuf[26]); } if ((pEP == NULL) || (NULL == (pMsg = (afIncomingMSGPacket_t *)osal_msg_allocate(sizeof(afIncomingMSGPacket_t) + tmp)))) { return; } pMsg->hdr.event = AF_INCOMING_MSG_CMD; pBuf = afIncMsgPktParse(cmd1, pBuf, pMsg); #if ZAP_AF_DATA_REQ_FRAG if (pMsg->cmd.DataLength > ZAP_AF_INC_DAT_MAX) { afRetrieve(*(pEP->epDesc->task_id), pMsg); } else #endif { if (pMsg->cmd.DataLength) { (void)osal_memcpy(pMsg->cmd.Data, pBuf, pMsg->cmd.DataLength); } else { pMsg->cmd.Data = NULL; } (void)osal_msg_send(*(pEP->epDesc->task_id), (uint8 *)pMsg); } }
/************************************************************************************************** * @fn zapUtilParseKeyInd * * @brief This function parses a packed keyEstablishmentInd_t. * * input parameters * * @param pBuf - A buffer containing a packed keyEstablishmentInd_t. * * output parameters * * None. * * @return None. ************************************************************************************************** */ static void zapUtilParseKeyInd(uint8 *pBuf) { keyEstablishmentInd_t *pInd; // Send osal message to the application. if (NULL != (pInd = (keyEstablishmentInd_t *)osal_msg_allocate(sizeof(keyEstablishmentInd_t)))) { pInd->hdr.event = pBuf[1]; pInd->hdr.status = pBuf[2]; pInd->waitTime = pBuf[3]; pInd->keyEstablishmentSuite = BUILD_UINT16(pBuf[4], pBuf[5]); osal_msg_send(pBuf[0], (uint8 *)pInd); } }
/****************************************************************************** * @fn StubAPS_NotifyApp * * @brief This function sends an OSAL message to the Application task. * * @param status - command status * * @return none */ static void StubAPS_NotifyApp( uint8 status ) { osal_event_hdr_t *msgPtr; // Notify the application task msgPtr = (osal_event_hdr_t *)osal_msg_allocate( sizeof(osal_event_hdr_t) ); if ( msgPtr ) { msgPtr->event = SAPS_CHANNEL_CHANGE; msgPtr->status = status; osal_msg_send( appTaskID, (uint8 *)msgPtr ); } } /* StubAPS_NotifyApp */
/********************************************************************* * @fn SAPI_SendCback * * @brief Sends a message to the sapi task ( itself ) so that a * callback can be generated later. * * @return none */ void SAPI_SendCback( uint8 event, uint8 status, uint16 data ) { sapi_CbackEvent_t *pMsg; pMsg = (sapi_CbackEvent_t *)osal_msg_allocate( sizeof(sapi_CbackEvent_t) ); if( pMsg ) { pMsg->hdr.event = event; pMsg->hdr.status = status; pMsg->data = data; osal_msg_send( sapi_TaskID, (uint8 *)pMsg ); } }
uint8 OnBoard_SendHall( void ) { uint8 *msgPtr; if (registeredHallTaskID != NO_TASK_ID) { msgPtr = (uint8 *)osal_msg_allocate(1); if (msgPtr) { osal_msg_send(registeredHallTaskID, msgPtr); //HalLedBlink(HAL_LED_2, 5, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME); } return (SUCCESS); } else return (FAILURE); }
/********************************************************************* * @fn gapCentralRole_timerCB * * @brief OSAL timer callback function * * @param pData - Data pointer * * @return none */ static void gapCentralRole_timerCB( uint8 *pData ) { gapCentralRoleRssiEvent_t *pMsg; // Timer has expired so clear timer ID ((gapCentralRoleRssi_t *) pData)->timerId = INVALID_TIMER_ID; // Send OSAL message pMsg = (gapCentralRoleRssiEvent_t *) osal_msg_allocate( sizeof(gapCentralRoleRssiEvent_t) ); if ( pMsg ) { pMsg->hdr.event = GAPCENTRALROLE_RSSI_MSG_EVT; pMsg->pRssi = (gapCentralRoleRssi_t *) pData; osal_msg_send ( gapCentralRoleTaskId, (uint8 *) pMsg ); } }
/*************************************************************************************************** * @fn MT_AppPB_ZCLCfg * * @brief Process MT_APP_PB_ZCL_CFG command * * @param pBuf - pointer to the received buffer * * @return void ***************************************************************************************************/ static void MT_AppPB_ZCLCfg( uint8 *pBuf ) { uint8 retValue = ZFailure; uint8 appEP; endPointDesc_t *epDesc; mtAppPB_ZCLCfg_t *cmd; uint8 cmdId; /* Parse the RPC header */ cmdId = pBuf[MT_RPC_POS_CMD1]; pBuf += MT_RPC_FRAME_HDR_SZ; /* Application End Point */ appEP = *pBuf++; /* Look up the endpoint */ epDesc = afFindEndPointDesc( appEP ); if ( epDesc ) { /* Build and send the message to the APP */ cmd = (mtAppPB_ZCLCfg_t *)osal_msg_allocate( sizeof( mtAppPB_ZCLCfg_t ) ); if ( cmd ) { /* Build and send message to the app */ cmd->hdr.event = MT_SYS_APP_PB_ZCL_CMD; /* PB ZCL command type*/ cmd->type = MT_APP_PB_ZCL_CMD_CFG; /* PB ZCL Config Mode */ cmd->mode = *pBuf++; /* Send the message */ osal_msg_send( *(epDesc->task_id), (uint8 *)cmd ); /* Info for response */ retValue = ZSuccess; } } /* Build and send back the response */ MT_BuildAndSendZToolResponse( ( (uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_APP ), cmdId, 1, &retValue ); }
void Send_DisplayTask_Msg(dis_event_t event, uint8 value) { uint8 result = 0; gDisplay = (Dis_Msg_t *)osal_msg_allocate(sizeof(Dis_Msg_t)); if(NULL == gDisplay) { SYS_TRACE("no memory for dispaly msg\r\n"); return; } gDisplay->hdr.event = (uint8)event; gDisplay->value = value; result = osal_msg_send(gDisTaskID,(uint8 *)gDisplay); if(result != SUCCESS) { SYS_TRACE("send display msg fail\r\n"); } }
void Send_MdcTask_Msg(Mdc_event_t event, uint8 value) { uint8 result = 0; gMdcMsg_t = (Mdc_Task_Msg_t *)osal_msg_allocate(sizeof(Mdc_Task_Msg_t)); if(NULL == gMdcMsg_t) { SYS_TRACE("no memory for mdc msg\r\n"); return; } gMdcMsg_t->hdr.event = (uint8)event; gMdcMsg_t->value = value; result = osal_msg_send(gMdcTaskID,(uint8 *)gMdcMsg_t); if(result != SUCCESS) { SYS_TRACE("send mdc msg fail\r\n"); } }
/************************************************************************************************** * @fn afCnf * * @brief This function de-muxes an incoming AF data confirm message. * * input parameters * * @param pBuf - Pointer to the RPC message buffer. * * output parameters * * None. * * @return None. ************************************************************************************************** */ static void afCnf(uint8 *pBuf) { pBuf += MT_RPC_FRAME_HDR_SZ; epList_t *pEP = afFindEndPointDescList(pBuf[1]); if (NULL != pEP) { afDataConfirm_t *pMsg = (afDataConfirm_t *)osal_msg_allocate(sizeof(afDataConfirm_t)); if (NULL != pMsg) { pMsg->hdr.event = AF_DATA_CONFIRM_CMD; pMsg->hdr.status = *pBuf++; pMsg->endpoint = *pBuf++; pMsg->transID = *pBuf; osal_msg_send(*(pEP->epDesc->task_id), (uint8 *)pMsg); } } }
/*************************************************************************************************** * @fn MT_AppMsg * * @brief Process APP_MSG command * * @param pBuf - pointer to the received buffer * * @return void ***************************************************************************************************/ static void MT_AppMsg(uint8 *pBuf) { uint8 retValue = ZFailure; uint8 endpoint; endPointDesc_t *epDesc; mtSysAppMsg_t *msg; uint8 cmdId, dataLen; /* parse header */ dataLen = pBuf[MT_RPC_POS_LEN]; cmdId = pBuf[MT_RPC_POS_CMD1]; pBuf += MT_RPC_FRAME_HDR_SZ; /* Get the endpoint and skip past it.*/ endpoint = *pBuf++; dataLen--; /* Look up the endpoint */ epDesc = afFindEndPointDesc( endpoint ); if (epDesc) { /* Build and send the message to the APP */ msg = (mtSysAppMsg_t *)osal_msg_allocate(sizeof(mtSysAppMsg_t) + (dataLen)); if ( msg ) { /* Build and send message up the app */ msg->hdr.event = MT_SYS_APP_MSG; msg->endpoint = endpoint; msg->appDataLen = dataLen; msg->appData = (uint8*)(msg+1); osal_memcpy( msg->appData, pBuf, dataLen); osal_msg_send( *(epDesc->task_id), (uint8 *)msg ); /* Info for response */ retValue = ZSuccess; } } /* Build and send back the response */ MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_APP), cmdId, 1, &retValue); }
/********************************************************************* * @fn OnBoard_SendKeys * * @brief Send "Key Pressed" message to application. * * @param keys - keys that were pressed * state - shifted * * @return status *********************************************************************/ uint8 OnBoard_SendKeys( uint8 keys, uint8 state ) { keyChange_t *msgPtr; if ( registeredKeysTaskID != NO_TASK_ID ) { // Send the address to the task msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) ); if ( msgPtr ) { msgPtr->hdr.event = KEY_CHANGE; msgPtr->state = state; msgPtr->keys = keys; osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr ); } return ( ZSuccess ); } else return ( ZFailure ); }
/********************************************************************* * @fn afReflectError * * @brief This function will generate the Reflect Error message to * the application. * * @param srcEP - Source Endpoint * @param dstAddrMode - mode of dstAdd - 0 - normal short addr, 1 - group Address * @param dstAddr - intended destination * @param dstEP - Destination Endpoint * @param transID - transaction ID from APSDE_DATA_REQUEST * @param status - status of APSDE_DATA_REQUEST * * @return none */ void afReflectError( uint8 srcEP, uint8 dstAddrMode, uint16 dstAddr, uint8 dstEP, uint8 transID, ZStatus_t status ) { endPointDesc_t *epDesc; afReflectError_t *msgPtr; // Find the endpoint description epDesc = afFindEndPointDesc( srcEP ); if ( epDesc == NULL ) return; // Determine the incoming command type msgPtr = (afReflectError_t *)osal_msg_allocate( sizeof(afReflectError_t) ); if ( msgPtr ) { // Build the Data Confirm message msgPtr->hdr.event = AF_REFLECT_ERROR_CMD; msgPtr->hdr.status = status; msgPtr->endpoint = dstEP; msgPtr->transID = transID; msgPtr->dstAddrMode = dstAddrMode; msgPtr->dstAddr = dstAddr; #if defined ( MT_AF_CB_FUNC ) /* If MT has subscribed for this callback, don't send as a message. */ if ( AFCB_CHECK( CB_ID_AF_REFLECT_ERROR, *(epDesc->task_id) ) ) { /* Send callback if it's subscribed */ MT_AfReflectError( (void *)msgPtr ); /* Release the memory. */ osal_msg_deallocate( (void *)msgPtr ); } else #endif { /* send message through task message */ osal_msg_send( *(epDesc->task_id), (uint8 *)msgPtr ); } } }
/*************************************************************************************************** * @fn MT_ProcessSrngEvent * * @brief * * Process SRNG Event Messages. * * @param None * * @return None ***************************************************************************************************/ void MT_ProcessSrngEvent(void) { uint8 * msg; osal_event_hdr_t * msg_ptr; msg_ptr = (osal_event_hdr_t *)osal_msg_allocate( MT_RPC_FRAME_HDR_SZ + 4 + sizeof(osal_event_hdr_t) ); msg = msg_ptr + sizeof(osal_event_hdr_t) + 4; if ( msg_ptr ) { msg_ptr->event = CMD_SERIAL_MSG; msg_ptr->status = 4; if(msg) { msg[MT_RPC_POS_LEN] = 4 + MT_RPC_FRAME_HDR_SZ; msg[MT_RPC_POS_CMD0] = MT_RPC_SYS_UTIL; msg[MT_RPC_POS_CMD1] = MT_UTIL_SRNG_GENERATE; } osal_memcpy(msg_ptr + sizeof(osal_event_hdr_t), &msg, 4); } osal_msg_send( MT_TaskID, (uint8 *)msg_ptr ); }
/********************************************************************* * @fn afDataConfirm * * @brief This function will generate the Data Confirm back to * the application. * * @param endPoint - confirm end point * @param transID - transaction ID from APSDE_DATA_REQUEST * @param status - status of APSDE_DATA_REQUEST * * @return none */ void afDataConfirm( uint8 endPoint, uint8 transID, ZStatus_t status ) { endPointDesc_t *epDesc; afDataConfirm_t *msgPtr; // Find the endpoint description epDesc = afFindEndPointDesc( endPoint ); if ( epDesc == NULL ) return; // Determine the incoming command type msgPtr = (afDataConfirm_t *)osal_msg_allocate( sizeof(afDataConfirm_t) ); if ( msgPtr ) { // Build the Data Confirm message msgPtr->hdr.event = AF_DATA_CONFIRM_CMD; msgPtr->hdr.status = status; msgPtr->endpoint = endPoint; msgPtr->transID = transID; #if defined ( MT_AF_CB_FUNC ) /* If MT has subscribed for this callback, don't send as a message. */ if ( AFCB_CHECK(CB_ID_AF_DATA_CNF,*(epDesc->task_id)) ) { /* Send callback if it's subscribed */ MT_AfDataConfirm ((void *)msgPtr); /* Release the memory. */ osal_msg_deallocate( (void *)msgPtr ); } else #endif { /* send message through task message */ osal_msg_send( *(epDesc->task_id), (byte *)msgPtr ); } } }
/************************************************************************************************** * @fn ZDO_UpdateNwkStatus * * @brief This function sends a ZDO_STATE_CHANGE message to the task of every EndPoint * registered with AF (except, of course, the ZDO_EP). Even if a single task has more * than one registered EndPoint, it will only receive one notification per state * change. Although the device may go through a sequence of state changes, the * Application task may only receive notification of the final, steady-state state * because it has the lowest priority and never even runs to receive the intermediate * state change notifications. * * input parameters * * @param state - The current device state. * * output parameters * * None. * * @return None. ************************************************************************************************** */ void ZDO_UpdateNwkStatus(devStates_t state) { epList_t *pItem = epList; while (pItem != NULL) { if (pItem->epDesc->endPoint != ZDO_EP) { osal_event_hdr_t *pMsg = (osal_event_hdr_t *)osal_msg_find(*(pItem->epDesc->task_id), ZDO_STATE_CHANGE); if (NULL == pMsg) { if (NULL == (pMsg = (osal_event_hdr_t *)osal_msg_allocate(sizeof(osal_event_hdr_t)))) { // Upon failure to notify any EndPoint of the state change, re-set the ZDO event to // try again later when more Heap may be available. osal_set_event(zapTaskId, ZAP_APP_ZDO_STATE_CHANGE_EVT); } else { pMsg->event = ZDO_STATE_CHANGE; pMsg->status = (uint8)state; (void)osal_msg_send(*(pItem->epDesc->task_id), (uint8 *)pMsg); } } else { // Modify in place the status of an existing ZDO_STATE_CHANGE message to the EndPoint. pMsg->status = (uint8)state; } } pItem = pItem->nextDesc; } }
/*************************************************************************************************** * @fn MT_UartProcessZToolData * * @brief | SOP | Data Length | CMD | Data | FCS | * | 1 | 1 | 2 | 0-Len | 1 | * * Parses the data and determine either is SPI or just simply serial data * then send the data to correct place (MT or APP) * * @param port - UART port * event - Event that causes the callback * * * @return None ***************************************************************************************************/ void MT_UartProcessZToolData( uint8 port, uint8 event ) { uint8 ch; (void)event; // Intentionally unreferenced parameter uint8 i,j,flag = 0; uint8 str[128]; while (Hal_UART_RxBufLen(port)) { HalUARTRead (port, &ch, 1); j++; str[j] = ch; flag = 1; } if(flag == 1) { pMsg = (mtOSALSerialData_t *) osal_msg_allocate(sizeof(mtOSALSerialData_t) + j +1); pMsg->hdr.event = CMD_SERIAL_MSG; pMsg->msg = (uint8 *)(pMsg + 1); pMsg->msg[0] = j; for(i = 1; i <= j; i++) { pMsg->msg[i] = str[i]; } osal_msg_send(App_TaskID,(byte *) pMsg); osal_msg_deallocate( (uint8 *)pMsg); } }
u16 APP_ProcessEvent( u8 task_id, u16 events ) { loraMAC_msg_t* pMsgSend = NULL; loraMAC_msg_t* pMsgRecieve = NULL; u8 tmp_buf[64]; u8 len = 0 ; //system event if(events & SYS_EVENT_MSG) { //receive msg loop while(NULL != (pMsgRecieve = (loraMAC_msg_t*)osal_msg_receive(APP_taskID))) { //pMsgRecieve[0] is system event type switch(pMsgRecieve->msgID) { //tx done case TXDONE : //调度下一次发送 //RedLED(ON); HalLedSet (HAL_LED_1, HAL_LED_MODE_ON); if(Txpacket_count > 0) { Txpacket_count--; HAL_UART_SendBytes(uart1_rxBuf,uart1_Rxcount); pMsgSend = (loraMAC_msg_t*)osal_msg_allocate(72); if(pMsgSend != NULL) { osal_memset(pMsgSend,0,72);//需要全部置0,要不然会出现发完串口数据包后不再进入TXDONE pMsgSend->msgID = TXREQUEST; pMsgSend->msgLen = uart1_Rxcount+2; osal_memcpy(pMsgSend->msgData,uart1_rxBuf,uart1_Rxcount); osal_msg_send(LoraMAC_taskID,(u8*)pMsgSend); osal_msg_deallocate((u8*)pMsgSend); } } else { //send a packet to LoRaMac osal (then can be send by the radio) pMsgSend = (loraMAC_msg_t*)osal_msg_allocate(72); if(pMsgSend != NULL) { osal_memset(pMsgSend,0,72); pMsgSend->msgID = TXREQUEST; pMsgSend->msgLen = 70; for(u8 dataCount = 0; dataCount < 70; dataCount++) { pMsgSend->msgData[dataCount] = dataCount; } osal_msg_send(LoraMAC_taskID,(u8*)pMsgSend); osal_msg_deallocate((u8*)pMsgSend); HAL_UART_SendBytes("app send\n", osal_strlen("app send\n")); } } //RedLED(OFF); HalLedSet (HAL_LED_1, HAL_LED_MODE_OFF); break; //rx done case RXDONE: HalLedSet (HAL_LED_2, HAL_LED_MODE_ON); OLED_Clear_Half();//先把屏幕下一半清空 APP_ShowMoteID(g_appData.devAddr); len = 0 ; g_number++ ; memset(Rx_buf , 0 ,sizeof(Rx_buf)); osal_memcpy(Rx_buf,pMsgRecieve->msgData,pMsgRecieve->msgLen); len = pMsgRecieve->msgLen; Rx_buf[len] = 0; OLED_ShowString( 0,36, (u8*)Rx_buf,12 ); OLED_Refresh_Gram(); HAL_UART_SendBytes("\n",1); HAL_UART_SendBytes((uint8_t *)Rx_buf,strlen(Rx_buf)); HalLedSet (HAL_LED_2, HAL_LED_MODE_OFF); break; case TXERR_STATUS: { //TODO MOTE send packet error deal memset( tmp_buf ,0 ,sizeof(tmp_buf) ); sprintf( (char *)tmp_buf,"send err ret=%d,no=%d",pMsgRecieve->msgData[0], pMsgRecieve->msgData[1]+( pMsgRecieve->msgData[2]<<8 ) ); OLED_ShowString( 0,36,tmp_buf,12 ); OLED_Refresh_Gram(); break; } default: break; } osal_msg_deallocate((u8*)pMsgRecieve); } return (events ^ SYS_EVENT_MSG); } //send a packet event if(events & APP_PERIOD_SEND) { //RedLED(OFF); HalLedSet (HAL_LED_1, HAL_LED_MODE_OFF); //send a packet to LoRaMac osal (then can be send by the radio) pMsgSend = (loraMAC_msg_t*)osal_msg_allocate(72); if(pMsgSend != NULL) { osal_memset(pMsgSend,0,72); pMsgSend->msgID = TXREQUEST; pMsgSend->msgLen = 70; for(u8 dataCount = 0; dataCount < 70; dataCount++) { pMsgSend->msgData[dataCount] = dataCount; } osal_msg_send(LoraMAC_taskID,(u8*)pMsgSend); } //osal_start_timerEx(APP_taskID, APP_PERIOD_SEND,1000);//延时继续发送 return (events ^ APP_PERIOD_SEND); } //uart test event if(events & APP_TEST_UART) { Txpacket_count = 1;//串口收到一个数据包,就发一遍无线包出去add by hxz //HAL_UART_SendBytes("hello,world!", 10); //osal_start_timerEx(APP_taskID, APP_TEST_UART,1000);//延时继续发送 return (events ^ APP_TEST_UART); } return 0 ; }
// ************************ USB interrupt event processing ************************* void usbirqHookProcessEvents(void) { T1CNTL=0; if (usbirqData.eventMask & USBIRQ_EVENT_EP5IN){ if (isFifoEmpty()==0){ struct UsbISR * msg = (struct UsbISR *)osal_msg_allocate(sizeof(struct UsbISR) ); msg->msg.event = EVENT_USB_ISR; msg->isr = eventSendFifo; osal_msg_send(zusbTaskId, (uint8 *)msg); } } if (usbirqData.eventMask & USBIRQ_EVENT_EP2OUT){ uint8 oldEndpoint = USBFW_GET_SELECTED_ENDPOINT(); USBFW_SELECT_ENDPOINT(2); uint8 length = USBFW_GET_OUT_ENDPOINT_COUNT_LOW(); if (length > MAX_DATE_SIZE_2){ length = MAX_DATE_SIZE_2; } if (length) { struct UsbISR * msg =NULL; uint8 code = USBF2; switch( code){ case ENABLE_INFO_MESSAGE: usbOn=1; break; case REQ_RESET: msg = (struct UsbISR *)osal_msg_allocate(sizeof(struct UsbISR) ); msg->msg.event = EVENT_USB_ISR; msg->isr = eventReset; break; case REQ_BIND_TABLE: { uint8 addr[2]; addr[0] = USBF2; addr[1] = USBF2; struct BindTableRequestMsg * msgReq = (struct BindTableRequestMsg *)osal_msg_allocate(sizeof(struct BindTableRequestMsg) ); msg = &(msgReq->isr); msg->isr = eventBindReq; msg->msg.event = EVENT_USB_ISR; msgReq->afAddrType.addrMode = Addr16Bit; msgReq->afAddrType.addr.shortAddr = *(uint16 *)(addr); } break; case REQ_ACTIVE_EP:{ struct ReqActiveEndpointsEvent * msgEP = (struct ReqActiveEndpointsEvent *)osal_msg_allocate(sizeof(struct ReqActiveEndpointsEvent) ); msg = &(msgEP->isr); msg->isr = eventActiveEP; msg->msg.event = EVENT_USB_ISR; msgEP->data[0] = USBF2; msgEP->data[1] = USBF2; } break; case REQ_ADD_BIND_TABLE_ENTRY: msg = createMsgForBind(); msg->isr = eventBindRequest; break; case REQ_REMOVE_BIND_TABLE_ENTRY: msg = createMsgForBind(); msg->isr = eventUnbindRequest; break; case NODE_POWER_REQUEST:{ struct ReqPowerNodeMsg * msgReq =(struct ReqPowerNodeMsg *)osal_msg_allocate(sizeof(struct ReqPowerNodeMsg) ); msg = &(msgReq->isr); msg->isr = eventReqPowerNode; msg->msg.event = EVENT_USB_ISR; msgReq->data[0] = USBF2; msgReq->data[1] = USBF2; } break; case REQ_IEEE_ADDRESS:{ struct ReqIeeeAddrMsg * msgReq = (struct ReqIeeeAddrMsg *)osal_msg_allocate(sizeof(struct ReqIeeeAddrMsg) ); msg = &(msgReq->isr); msg->isr = eventReqIeeeAddr; msg->msg.event = EVENT_USB_ISR; msgReq->data[0] = USBF2; msgReq->data[1] = USBF2; msgReq->requestType = USBF2; msgReq->startIndex = USBF2; break; } case WRITE_ATTRIBUTE_VALUE:{ struct WriteAttributeValueUsbMsg usbMsg; uint8 * data = (uint8 *)(&usbMsg); uint8 i; for(i=0; i < sizeof(struct WriteAttributeValueUsbMsg); i++){ *data = USBF2; data++; } struct WriteAttributeValueMsg * msgCmd = (struct WriteAttributeValueMsg *)osal_msg_allocate(sizeof(struct WriteAttributeValueMsg) +sizeof(zclWriteRec_t) + usbMsg.dataValueLen ); msg = &(msgCmd->isr); msg->isr = eventWriteValue; msg->msg.event = EVENT_USB_ISR; msgCmd->afAddrType.addrMode=afAddr16Bit; msgCmd->afAddrType.addr.shortAddr=usbMsg.nwkAddr; msgCmd->afAddrType.endPoint=usbMsg.endpoint; msgCmd->cluster = usbMsg.cluster; msgCmd->writeCmd.numAttr=1; msgCmd->writeCmd.attrList->attrID = usbMsg.attributeId; msgCmd->writeCmd.attrList->dataType=usbMsg.dataType; data = ((uint8 *)msgCmd) + sizeof(struct WriteAttributeValueMsg) +sizeof(zclWriteRec_t); msgCmd->writeCmd.attrList->attrData = data; for(i=0; i < usbMsg.dataValueLen; i++){ *data = USBF2; data++; } } break; case SEND_CMD:{ struct SendCmdUsbMsg usbMsg; uint8 * data = (uint8 *)(&usbMsg); uint8 i; for(i=0; i < sizeof(struct SendCmdUsbMsg); i++){ *data = USBF2; data++; } struct SendCmdMsg * msgCmd = (struct SendCmdMsg *)osal_msg_allocate(sizeof(struct SendCmdMsg) +usbMsg.dataLen ); msg = &(msgCmd->isr); msg->isr = eventSendCmd; msg->msg.event = EVENT_USB_ISR; msgCmd->cluster =usbMsg.cluster; msgCmd->cmdClusterId = usbMsg.cmdClusterId; msgCmd->afAddrType.addr.shortAddr= usbMsg.nwkAddr; msgCmd->afAddrType.addrMode = afAddr16Bit; msgCmd->afAddrType.endPoint = usbMsg.endpoint; msgCmd->dataLen = usbMsg.dataLen; data = (uint8 *)(msgCmd->data); for(i=0; i < usbMsg.dataLen; i++){ *data = USBF2; data++; } } break; case REQ_ATTRIBUTE_VALUES: { struct ReqAttributeValueMsg attr; uint8 * data = (uint8 *)(&attr); uint8 i; for(i=0; i < sizeof(struct ReqAttributeValueMsg); i++){ *data = USBF2; data++; } struct ReqAttributeMsg * msgAttr = (struct ReqAttributeMsg *)osal_msg_allocate(sizeof(struct ReqAttributeMsg) +attr.numAttributes* sizeof(uint16) ); msg = &(msgAttr->isr); msg->isr = attributeValue; msg->msg.event = EVENT_USB_ISR; msgAttr->afAddrType.addr.shortAddr = attr.nwkAddr; msgAttr->afAddrType.addrMode = afAddr16Bit; msgAttr->afAddrType.endPoint = attr.endpoint; msgAttr->numAttr = attr.numAttributes; data = (uint8 *)&msgAttr->attrID; for (uint8 i=0; i < attr.numAttributes; i++){ *data = USBF2; data++; *data = USBF2; data++; } msgAttr->cluster = attr.cluster; osal_msg_send(zusbTaskId, (uint8 *)msg); break; } case REQ_DEVICE_INFO:{ struct ReqDeviceInformationEvent * msgEP = (struct ReqDeviceInformationEvent *)osal_msg_allocate(sizeof(struct ReqDeviceInformationEvent) ); msg = &(msgEP->isr); msg->isr = eventDeviceInfo; msg->msg.event = EVENT_USB_ISR; msgEP->data[0] = USBF2; msgEP->data[1] = USBF2; } break; } if (msg != NULL) { uint8 low = T1CNTL; uint8 hi = T1CNTH; msg->time=BUILD_UINT16(low,hi); osal_msg_send(zusbTaskId, (uint8 *)msg); } /*uint8 __generic *pTemp = rxData; do { *(pTemp++) = USBF2; } while (--length);*/ } USBFW_ARM_OUT_ENDPOINT(); USBFW_SELECT_ENDPOINT(oldEndpoint); } }