/****************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { if(event & SYS_EVENT_MSG) { } if ( event & MY_START_EVT ) { zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if (appState == APP_BINDED) { } else { osal_set_event( sapi_TaskID, MY_FIND_COLLECTOR_EVT); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // Find and bind to a gateway device (if this node is not gateway) zb_BindDevice( TRUE, DUMMY_REPORT_CMD_ID, (uint8 *)NULL ); } }
/********************************************************************* * @fn oadManagerSvcDiscoveryMsg * * @brief Process GATT Primary Service discovery message * * @return none */ static void oadManagerSvcDiscoveryMsg(gattMsgEvent_t *pMsg) { if ( pMsg->hdr.status == SUCCESS ) // Characteristic found, the store handle. { attFindByTypeValueRsp_t *pRsp = &(pMsg->msg.findByTypeValueRsp); if ( pRsp->numInfo > 0 ) { oadSvcStartHdl = pRsp->handlesInfo[0].handle; oadSvcEndHdl = pRsp->handlesInfo[0].grpEndHandle; #if (defined HAL_LCD) && (HAL_LCD == TRUE) LCD_WRITE_STRING("OAD Svc Found!", HAL_LCD_LINE_1); #endif // OAD service found (void)osal_set_event(oadManagerTaskId, CCC_DISCOVERY_EVT); } } else if ( pMsg->hdr.status == bleProcedureComplete ) { if ( oadSvcStartHdl == 0 ) { #if (defined HAL_LCD) && (HAL_LCD == TRUE) LCD_WRITE_STRING("OAD SvcNotFound", HAL_LCD_LINE_3); #endif } } }
/****************************************************************************** * @fn zb_SendDataConfirm * * @brief The zb_SendDataConfirm callback function is called by the * ZigBee after a send data operation completes * * @param handle - The handle identifying the data transmission. * status - The status of the operation. * * @return none */ void zb_SendDataConfirm( uint8 handle, uint8 status ) { if(status != ZB_SUCCESS) { if ( ++reportFailureNr >= REPORT_FAILURE_LIMIT ) { // Stop reporting osal_stop_timerEx( sapi_TaskID, MY_REPORT_EVT ); // After failure reporting start automatically when the device // is binded to a new gateway reportState=TRUE; // Try binding to a new gateway osal_set_event( sapi_TaskID, MY_FIND_COLLECTOR_EVT ); reportFailureNr=0; } } // status == SUCCESS else { // Reset failure counter reportFailureNr=0; } }
/********************************************************************* * @fn OADManager_Init * * @brief Initialization function for the OAD Manager App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notification). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void OADManager_Init( uint8 task_id ) { oadManagerTaskId = task_id; // Setup Central Profile { uint8 scanRes = DEFAULT_MAX_SCAN_RES; GAPCentralRole_SetParameter ( GAPCENTRALROLE_MAX_SCAN_RES, sizeof( uint8 ), &scanRes ); } // Setup GAP GAP_SetParamValue( TGAP_GEN_DISC_SCAN, DEFAULT_SCAN_DURATION ); GAP_SetParamValue( TGAP_LIM_DISC_SCAN, DEFAULT_SCAN_DURATION ); GAP_SetParamValue( TGAP_REJECT_CONN_PARAMS, DEFAULT_OAD_REJECT_CONN_PARAMS ); GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, (uint8 *) oadManagerDeviceName ); // Initialize GATT Client VOID GATT_InitClient(); // Register to receive incoming ATT Indications/Notifications GATT_RegisterForInd( task_id ); #if (defined HAL_KEY) && (HAL_KEY == TRUE) // Register for all key events - This app will handle all key events RegisterForKeys( task_id ); #endif #if (defined HAL_LED) && (HAL_LED == TRUE) HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); #endif // Setup a delayed profile startup osal_set_event( task_id, START_DEVICE_EVT ); }
/********************************************************************* * @fn bedChangeCB * * @brief Callback from BedProfile indicating a value change * * @param paramID - parameter ID of the value that was changed. * * @return none */ static void bedChangeCB( uint8 paramID ) { /* if (paramID == BED_CONF) { uint8 newValue; Gyro_GetParameter( BED_CONF, &newValue ); if (newValue == 0) { // Put sensor to sleep if (bedEnabled) { bedEnabled = FALSE; osal_set_event( sensorTag_TaskID, ST_BED_SENSOR_EVT); } } else { */ bedEnabled = TRUE; osal_set_event( sensorTag_TaskID, ST_BED_SENSOR_EVT); //} //} // should not get here }
/************************************************************************************************** * @fn zapZdoProcessIncoming * * @brief This function processes the ZDO sub-system response from the ZNP. * * input parameters * * @param port - Port Id corresponding to the ZNP that sent the message. * @param pBuf - A pointer to the RPC response. * * output parameters * * None. * * @return None. ************************************************************************************************** */ void zapZdoProcessIncoming(uint8 port, uint8 *pBuf) { uint8 len = pBuf[MT_RPC_POS_LEN]; uint8 cmd1 = pBuf[MT_RPC_POS_CMD1]; pBuf += MT_RPC_FRAME_HDR_SZ; switch (cmd1) { case MT_ZDO_STATUS_ERROR_RSP: // TODO: fill-in when/if needed. break; case MT_ZDO_STATE_CHANGE_IND: // Especially for UART transport, allow time for multiple got syncs before acting on it. if (ZSuccess != osal_start_timerEx(zapTaskId, ZAP_APP_ZDO_STATE_CHANGE_EVT, ZAP_APP_ZDO_STATE_CHANGE_DLY)) { (void)osal_set_event(zapTaskId, ZAP_APP_ZDO_STATE_CHANGE_EVT); } break; case MT_ZDO_MATCH_DESC_RSP_SENT: // TODO: fill-in when/if needed. break; case MT_ZDO_SRC_RTG_IND: // TODO: fill-in when/if needed. break; case MT_ZDO_JOIN_CNF: // TODO: fill-in when/if needed. break; case MT_ZDO_CONCENTRATOR_IND_CB: zapZDO_ConcentratorIndicationCB(pBuf); break; case MT_ZDO_MSG_CB_INCOMING: { zdoIncomingMsg_t inMsg; // Assuming exclusive use of network short addresses. inMsg.srcAddr.addrMode = Addr16Bit; inMsg.srcAddr.addr.shortAddr = BUILD_UINT16(pBuf[0], pBuf[1]); pBuf += 2; inMsg.wasBroadcast = *pBuf++; inMsg.clusterID = BUILD_UINT16(pBuf[0], pBuf[1]); pBuf += 2; inMsg.SecurityUse = *pBuf++; inMsg.TransSeq = *pBuf++; inMsg.asduLen = len-9; inMsg.macDestAddr = BUILD_UINT16(pBuf[0], pBuf[1]); pBuf += 2; inMsg.asdu = pBuf; ZDO_SendMsgCBs(&inMsg); } break; default: break; } }
HAL_ISR_FUNCTION(port2Isr, P2INT_VECTOR) { unsigned char status; unsigned char i; HAL_ENTER_ISR(); status = P2IFG; status &= P2IEN; if (status) { P2IFG = ~status; P2IF = 0; for (i = 0; i < OS_MAX_INTERRUPT; i++) { if (PIN_MAJOR(blueBasic_interrupts[i].pin) == 2 && (status & (1 << PIN_MINOR(blueBasic_interrupts[i].pin)))) { osal_set_event(blueBasic_TaskID, BLUEBASIC_EVENT_INTERRUPT << i); } } } HAL_EXIT_ISR(); }
/********************************************************************* * @fn pairStateCB * * @brief Pairing state callback. * * @return none */ static void timeAppPairStateCB( uint16 connHandle, uint8 state, uint8 status ) { if ( state == GAPBOND_PAIRING_STATE_STARTED ) { timeAppPairingStarted = TRUE; } else if ( state == GAPBOND_PAIRING_STATE_COMPLETE ) { timeAppPairingStarted = FALSE; if ( status == SUCCESS ) { linkDBItem_t *pItem; if ( (pItem = linkDB_Find( gapConnHandle )) != NULL ) { // Store bonding state of pairing timeAppBonded = ( (pItem->stateFlags & LINK_BOUND) == LINK_BOUND ); if ( timeAppBonded ) { osal_memcpy( timeAppBondedAddr, pItem->addr, B_ADDR_LEN ); } } // If discovery was postponed start discovery if ( timeAppDiscPostponed && timeAppDiscoveryCmpl == FALSE ) { timeAppDiscPostponed = FALSE; osal_set_event( bloodPressureTaskId, BP_START_DISCOVERY_EVT ); } } } }
/********************************************************************* * @fn oadManagerCharDiscovery * * @brief OAD Characteristics service discovery. * * @return none */ static void oadManagerCharDiscovery(void) { attReadByTypeReq_t req; req.startHandle = oadSvcStartHdl; req.endHandle = oadSvcEndHdl; req.type.len = ATT_UUID_SIZE; if ( oadManagerDiscIdx == 0 ) { uint8 oadCharUUID[ATT_UUID_SIZE] = { TI_BASE_UUID_128( OAD_IMG_IDENTIFY_UUID ) }; (void)osal_memcpy(req.type.uuid, oadCharUUID, ATT_UUID_SIZE); } else { uint8 oadCharUUID[ATT_UUID_SIZE] = { TI_BASE_UUID_128( OAD_IMG_BLOCK_UUID ) }; (void)osal_memcpy(req.type.uuid, oadCharUUID, ATT_UUID_SIZE); } if (GATT_DiscCharsByUUID(oadManagerConnHandle, &req, oadManagerTaskId) != SUCCESS) { (void)osal_set_event(oadManagerTaskId, CHAR_DISCOVERY_EVT); } }
static void pulseNodeCheckIn(void) { static bool Flag; afAddrType_t addr; //AF address stucture defined for info on the destination Endpoint object that data will be sent to osal_stop_timerEx(pulseTaskId, PULSE_EVT_REQ); //Stop pulseDataReq() task since no pulse data is being measured PulseEvtReq_sync = FALSE; Flag = FALSE; addr.addr.shortAddr = pulseAddr; //loading short address (16-bit) with pulse address addr.addrMode = afAddr16Bit; //Set to directly sent to a node addr.endPoint = PULSE_ENDPOINT; //Sets the endpoint of the final destination (coordinator?) pulseDat[PULSE_CHECK_IN] = CHECK_IN_ACTIVE; //Flag is set and will notify coordinator that node is currently sending check in data HalLcdWriteString("BPMsensor Inacti",HAL_LCD_LINE_5); if (afStatus_SUCCESS != AF_DataRequest(&addr, (endPointDesc_t *)&PULSE_epDesc, PULSE_CLUSTER_ID, PULSE_DAT_LEN, pulseDat, &pulseTSN, AF_DISCV_ROUTE,AF_DEFAULT_RADIUS)) { //if data transfer is unsuccessful place event immediately back into queue to attempt to send again osal_set_event(pulseTaskId, PULSE_EVT_CHECKIN); } else { pulseCnt++; } if((QS == FALSE) && (Flag == FALSE)){ osal_start_timerEx(pulseTaskId, PULSE_EVT_CHECKIN, PULSE_DLY_CHECKIN); //send check in dummy packet every 10 seconds Flag = TRUE; //to prevent restarting of timer if existing already running } }
/********************************************************************* * @fn barometerChangeCB * * @brief Callback from Barometer Service indicating a value change * * @param paramID - parameter ID of the value that was changed. * * @return none */ static void barometerChangeCB( uint8 paramID ) { uint8 newValue; switch( paramID ) { case SENSOR_CONF: Barometer_GetParameter( SENSOR_CONF, &newValue ); switch ( newValue) { case ST_CFG_SENSOR_DISABLE: if (barEnabled) { barEnabled = FALSE; osal_set_event( sensorTag_TaskID, ST_BAROMETER_SENSOR_EVT); } break; case ST_CFG_SENSOR_ENABLE: if(!barEnabled) { barEnabled = TRUE; osal_set_event( sensorTag_TaskID, ST_BAROMETER_SENSOR_EVT); } break; case ST_CFG_CALIBRATE: readBarCalibration(); break; default: break; } break; case SENSOR_PERI: Barometer_GetParameter( SENSOR_PERI, &newValue ); sensorBarPeriod = newValue*SENSOR_PERIOD_RESOLUTION; break; default: // should not get here! break; } }
/*************************************************************************************************** * @fn HalLedBlink * * @brief Blink the leds * * @param leds - bit mask value of leds to be blinked * numBlinks - number of blinks * percent - the percentage in each period where the led * will be on * period - length of each cycle in milliseconds * * @return None ***************************************************************************************************/ void HalLedBlink (uint8 leds, uint8 numBlinks, uint8 percent, uint16 period) { #if (defined (BLINK_LEDS)) && (HAL_LED == TRUE) uint8 led; HalLedControl_t *sts; if (leds && percent && period) { if (percent < 100) { led = HAL_LED_1; leds &= HAL_LED_ALL; sts = HalLedStatusControl.HalLedControlTable; while (leds) { if (leds & led) { /* Store the current state of the led before going to blinking if not already blinking */ if(sts->mode < HAL_LED_MODE_BLINK ) preBlinkState |= (led & HalLedState); sts->mode = HAL_LED_MODE_OFF; /* Stop previous blink */ sts->time = period; /* Time for one on/off cycle */ sts->onPct = percent; /* % of cycle LED is on */ sts->left = numBlinks; /* Number of blink cycles */ if (!numBlinks) sts->mode |= HAL_LED_MODE_FLASH; /* Continuous */ sts->next = osal_GetSystemClock(); /* Start now */ sts->mode |= HAL_LED_MODE_BLINK; /* Enable blinking */ leds ^= led; } led <<= 1; sts++; } // Cancel any overlapping timer for blink events osal_stop_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT); osal_set_event (Hal_TaskID, HAL_LED_BLINK_EVENT); } else { HalLedSet (leds, HAL_LED_MODE_ON); /* >= 100%, turn on */ } } else { HalLedSet (leds, HAL_LED_MODE_OFF); /* No on time, turn off */ } #elif (HAL_LED == TRUE) percent = (leds & HalLedState) ? HAL_LED_MODE_OFF : HAL_LED_MODE_ON; HalLedOnOff (leds, percent); /* Toggle */ #else // HAL LED is disabled, suppress unused argument warnings (void) leds; (void) numBlinks; (void) percent; (void) period; #endif /* BLINK_LEDS && HAL_LED */ }
/********************************************************************* * @fn SimpleBLECentral_Init * * @brief Initialization function for the Simple BLE Central App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notification). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SimpleBLECentral_Init( uint8 task_id ) { simpleBLETaskId = task_id; // 串口初始化 NPI_InitTransport(uart_NpiSerialCallback); //NPI_WriteTransport("SimpleBLECentral_Init\r\n", 23); Get_IMEI(); //halSleep(100); //Get_Test(); // Setup Central Profile { uint8 scanRes = DEFAULT_MAX_SCAN_RES; GAPCentralRole_SetParameter ( GAPCENTRALROLE_MAX_SCAN_RES, sizeof( uint8 ), &scanRes ); } // Setup GAP GAP_SetParamValue( TGAP_GEN_DISC_SCAN, DEFAULT_SCAN_DURATION ); GAP_SetParamValue( TGAP_LIM_DISC_SCAN, DEFAULT_SCAN_DURATION ); GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, (uint8 *) simpleBLEDeviceName ); // Setup the GAP Bond Manager { uint32 passkey = DEFAULT_PASSCODE; uint8 pairMode = DEFAULT_PAIRING_MODE; uint8 mitm = DEFAULT_MITM_MODE; uint8 ioCap = DEFAULT_IO_CAPABILITIES; uint8 bonding = DEFAULT_BONDING_MODE; GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof( uint32 ), &passkey ); GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof( uint8 ), &pairMode ); GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof( uint8 ), &mitm ); GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof( uint8 ), &ioCap ); GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof( uint8 ), &bonding ); } // Initialize GATT Client VOID GATT_InitClient(); // Register to receive incoming ATT Indications/Notifications GATT_RegisterForInd( simpleBLETaskId ); // Initialize GATT attributes GGS_AddService( GATT_ALL_SERVICES ); // GAP GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes // Register for all key events - This app will handle all key events RegisterForKeys( simpleBLETaskId ); // makes sure LEDs are off HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); // Setup a delayed profile startup osal_set_event( simpleBLETaskId, START_DEVICE_EVT ); }
/*************************************************************************************************** * LOCAL FUNCTIONS ***************************************************************************************************/ static void rxCB( uint8 port, uint8 event ) { extern uint8 SampleApp_TaskID; rxlen=Hal_UART_RxBufLen(SERIAL_APP_PORT); //接收缓冲区数据长度,字节为单位 readbuf(); //读取buf的数值,并判断时候用ID功能 if(rxlen==0) osal_mem_free( rbuf ); //释放内存 else osal_set_event(SampleApp_TaskID,UART_RX_CB_EVT); }
/********************************************************************* * @fn readAccData * * @brief Read accelerometer data * * @param none * * @return none */ static void readAccData(void) { // uint8 aData[ACCELEROMETER_DATA_LEN]; // if (HalAccRead(aData)) // { // Accel_SetParameter( ACCELEROMETER_DATA, ACCELEROMETER_DATA_LEN, aData); // } // readGyroData(); osal_set_event( sensorTag_TaskID, ST_GYROSCOPE_SENSOR_EVT ); }
void OS_type(char c) { switch (c) { case 0xff: break; case NL: case CR: #ifdef ENABLE_CONSOLE_ECHO OS_putchar('\n'); #endif // ENABLE_CONSOLE_ECHO *input.ptr = NL; input.mode = MODE_GOT_INPUT; // Wake the interpreter now we have something new to do osal_set_event(blueBasic_TaskID, BLUEBASIC_INPUT_AVAILABLE); break; case CTRLH: if(input.ptr == input.start) { break; } if (*--input.ptr == input.quote) { input.quote = 0; } #ifdef ENABLE_CONSOLE_ECHO OS_putchar(CTRLH); OS_putchar(' '); OS_putchar(CTRLH); #endif // ENABLE_CONSOLE_ECHO break; default: if(input.ptr != input.end) { // Are we in a quoted string? if(c == input.quote) { input.quote = 0; } else if (c == DQUOTE || c == SQUOTE) { input.quote = c; } else if (input.quote == 0 && c >= 'a' && c <= 'z') { c = c + 'A' - 'a'; } *input.ptr++ = c; #ifdef ENABLE_CONSOLE_ECHO OS_putchar(c); #endif } break; } }
/************************************************************************************************** * @fn npSpiRxIsr * * @brief This function handles the DMA Rx complete interrupt. * * input parameters * * None. * * output parameters * * None. * * @return None. ************************************************************************************************** */ void npSpiRxIsr(void) { uint8 *pBuf; mtRpcCmdType_t type = (mtRpcCmdType_t)(npSpiBuf[1] & MT_RPC_CMD_TYPE_MASK); NP_SPI_ASSERT(npSpiState == NP_SPI_WAIT_RX); switch (type) { case MT_RPC_CMD_POLL: npSpiState = NP_SPI_WAIT_TX; if ((pBuf = npSpiPollCallback()) != NULL) { /* Send TX packet using uDMA */ spi_tx(pBuf); osal_msg_deallocate((uint8 *)pBuf); } else { /* If there is no data in the queue, send 3 zeros. */ pBuf = npSpiBuf; npSpiBuf[0] = npSpiBuf[1] = npSpiBuf[2] = 0; /* Send TX packet using uDMA */ spi_tx(pBuf); } break; case MT_RPC_CMD_SREQ: npSpiState = NP_SPI_WAIT_SREQ; osal_set_event(znpTaskId, ZNP_SPI_RX_SREQ_EVENT); break; case MT_RPC_CMD_AREQ: npSpiState = NP_SPI_WAIT_AREQ; osal_set_event(znpTaskId, ZNP_SPI_RX_AREQ_EVENT); break; default: npSpiState = NP_SPI_IDLE; break; } }
/************************************************************************************************** * @fn halProcessMotionDetectInterrupt * * @brief Processes motion detection interrupt by informing the entity that * enabled motion detection that it has occurred. Also disables further * motion detection interrupts. * * @param * * @return **************************************************************************************************/ static void halAccelProcessMotionDetectInterrupt( void ) { /* Disable port interrupt */ P1IEN &= (uint8) ~HAL_ACCEL_P1_INTERRUPT_PINS; /* Disable P1 interrupts */ IEN2 &= ~(BV( 4 )); /* Set event to process motion detection not in interrupt context */ osal_set_event( Hal_TaskID, HAL_MOTION_DETECTED_EVENT ); }
/********************************************************************* * @fn TransmitApp_SetSendEvt * * @brief Set the event flag * * @param none * * @return none */ void TransmitApp_SetSendEvt( void ) { #if defined( TRANSMITAPP_DELAY_SEND ) // Adds a delay to sending the data osal_start_timerEx( TransmitApp_TaskID, TRANSMITAPP_SEND_MSG_EVT, TRANSMITAPP_SEND_DELAY ); #else // No Delay - just send the data osal_set_event( TransmitApp_TaskID, TRANSMITAPP_SEND_MSG_EVT ); #endif }
/********************************************************************* * @fn rxCB * * @brief Process UART Rx event handling. * May be triggered by an Rx timer expiration - less than max * Rx bytes have arrived within the Rx max age time. * May be set by failure to alloc max Rx byte-buffer for the DMA Rx - * system resources are too low, so set flow control? * * @param none * * @return none */ static void rxCB( uint8 port, uint8 event ) { uint8 *buf, len; /* While awaiting retries/response, only buffer 1 next buffer: otaBuf2. * If allow the DMA Rx to continue to run, allocating Rx buffers, the heap * will become so depleted that an incoming OTA response cannot be received. * When the Rx data available is not read, the DMA Rx Machine automatically * sets flow control off - it is automatically re-enabled upon Rx data read. * When the back-logged otaBuf2 is sent OTA, an Rx data read is scheduled. */ if ( otaBuf2 ) { return; } if ( !(buf = osal_mem_alloc( SERIAL_APP_RX_CNT )) ) { return; } /* HAL UART Manager will turn flow control back on if it can after read. * Reserve 1 byte for the 'sequence number'. */ len = HalUARTRead( port, buf+1, SERIAL_APP_RX_CNT-1 ); if ( !len ) // Length is not expected to ever be zero. { osal_mem_free( buf ); return; } //else // HalLedSet(HAL_LED_2,HAL_LED_MODE_ON); /* If the local global otaBuf is in use, then either the response handshake * is being awaited or retries are being attempted. When the wait/retries * process has been exhausted, the next OTA msg will be attempted from * otaBuf2, if it is not NULL. */ if ( otaBuf ) { otaBuf2 = buf; otaLen2 = len; } else { otaBuf = buf; otaLen = len; /* Don't call SerialApp_SendData() from here in the callback function. * Set the event so SerialApp_SendData() runs during this task's time slot. */ osal_set_event( SerialApp_TaskID, SERIALAPP_MSG_SEND_EVT ); } }
/********************************************************************* * @fn Thermometer_Init * * @brief Initialization function for the Thermometer 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 Thermometer_Init(uint8 task_id) { struct ther_info *ti = &ther_info; osal_memset(ti, 0, sizeof(struct ther_info)); ti->task_id = task_id; ti->power_mode = PM_ACTIVE; osal_set_event(ti->task_id, TH_POWER_ON_EVT); }
/********************************************************************* * @fn ZDOInitDevice * * @brief Start the device in the network. This function will read * ZCD_NV_STARTUP_OPTION (NV item) to determine whether or not to * restore the network state of the device. * * @param startDelay - timeDelay to start device (in milliseconds). * There is a jitter added to this delay: * ((NWK_START_DELAY + startDelay) * + (osal_rand() & EXTENDED_JOINING_RANDOM_MASK)) * * NOTE: If the application would like to force a "new" join, the * application should set the ZCD_STARTOPT_DEFAULT_NETWORK_STATE * bit in the ZCD_NV_STARTUP_OPTION NV item before calling * this function. "new" join means to not restore the network * state of the device. Use zgWriteStartupOptions() to set these * options. * * @return * ZDO_INITDEV_RESTORED_NETWORK_STATE - The device's network state was * restored. * ZDO_INITDEV_NEW_NETWORK_STATE - The network state was initialized. * This could mean that ZCD_NV_STARTUP_OPTION said to not restore, or * it could mean that there was no network state to restore. * ZDO_INITDEV_LEAVE_NOT_STARTED - Before the reset, a network leave was issued * with the rejoin option set to TRUE. So, the device was not * started in the network (one time only). The next time this * function is called it will start. * 0xFF for failure. */ uint8 ZDOInitDevice(uint16 startDelay) { uint8 *pBuf; #if !ZAP_ZDO_STARTUP_AREQ uint8 rtrn; #endif (void)startDelay; // ZNP MT_ZDO_STARTUP_FROM_APP processing forces delay 0. zb_GetDeviceInfo(ZB_INFO_DEV_STATE, &devState); if ((DEV_HOLD != devState) && (DEV_INIT != devState) && (DEV_NWK_ORPHAN != devState)) { return FAILURE; } #if ZAP_ZDO_STARTUP_AREQ pBuf = zap_msg_allocate(0, (uint8)MT_RPC_SYS_ZDO | (uint8)MT_RPC_CMD_AREQ, (uint8)MT_ZDO_STARTUP_FROM_APP); #else pBuf = zap_msg_allocate(0, (uint8)MT_RPC_SYS_ZDO | (uint8)MT_RPC_CMD_SREQ, (uint8)MT_ZDO_STARTUP_FROM_APP); #endif if (NULL == pBuf) { return 0xFF; } zapPhySend(zapAppPort, pBuf); #if !ZAP_ZDO_STARTUP_AREQ if (ZSuccess == (rtrn = ZAP_SRSP_STATUS(pBuf))) #endif // Need to locally enter the discovery state to holdoff calls to ZDOInitDevice() until the ZAP // monitoring task requests the actual ZNP state. devState = DEV_NWK_DISC; zap_msg_deallocate(&pBuf); // Joining can take some time - especially with > 1 scan channel. if (ZSuccess != osal_start_timerEx(zapTaskId, ZAP_APP_TMR_EVT, ZAP_APP_JOIN_DLY)) { (void)osal_set_event(zapTaskId, ZAP_APP_TMR_EVT); } #if ZAP_ZDO_STARTUP_AREQ // Made into an AREQ after empirical results showed > 400 msec delay on SRSP. #if ZAP_NV_RESTORE return ZDO_INITDEV_RESTORED_NETWORK_STATE; #else return ZDO_INITDEV_NEW_NETWORK_STATE; #endif #else return rtrn; #endif }
/*************************************************************************************************** * @fn MT_TaskInit * * @brief MonitorTest Task Initialization. This function is put into the * task table. * * @param task_id - task ID of the MT Task * * @return void ***************************************************************************************************/ void MT_TaskInit(uint8 task_id) { MT_TaskID = task_id; /* Initialize the Serial port */ MT_UartInit(); /* Register taskID - Do this after UartInit() because it will reset the taskID */ MT_UartRegisterTaskID(task_id); osal_set_event(task_id, MT_SECONDARY_INIT_EVENT); }
/********************************************************************* * @fn oadManagerCCCDiscovery * * @brief OAD Client Characteristic Configuration discovery. * * @return none */ static void oadManagerCCCDiscovery(void) { // Initialize CCCD discovery variable oadManagerCCCIdx = 0; // Discover characteristic descriptors if (GATT_DiscAllCharDescs(oadManagerConnHandle, oadSvcStartHdl, oadSvcEndHdl, oadManagerTaskId) != SUCCESS) { (void)osal_set_event(oadManagerTaskId, CCC_DISCOVERY_EVT); } }
/********************************************************************* * @fn accelChangeCB * * @brief Callback from Acceleromter Service indicating a value change * * @param paramID - parameter ID of the value that was changed. * * @return none */ static void accelChangeCB( uint8 paramID ) { uint8 newValue; switch (paramID) { case SENSOR_CONF: Accel_GetParameter( SENSOR_CONF, &newValue ); if ( newValue == ST_CFG_SENSOR_DISABLE) { // Put sensor to sleep if (accConfig != ST_CFG_SENSOR_DISABLE) { accConfig = ST_CFG_SENSOR_DISABLE; osal_set_event( sensorTag_TaskID, ST_ACCELEROMETER_SENSOR_EVT); } } else { if (accConfig == ST_CFG_SENSOR_DISABLE) { // Start scheduling only on change disabled -> enabled osal_set_event( sensorTag_TaskID, ST_ACCELEROMETER_SENSOR_EVT); } // Scheduled already, so just change range accConfig = newValue; HalAccSetRange(accConfig); } break; case SENSOR_PERI: Accel_GetParameter( SENSOR_PERI, &newValue ); sensorAccPeriod = newValue*SENSOR_PERIOD_RESOLUTION; break; default: // Should not get here break; } }
/********************************************************************* * @fn humidityChangeCB * * @brief Callback from Humidity Service indicating a value change * * @param paramID - parameter ID of the value that was changed. * * @return none */ static void humidityChangeCB( uint8 paramID ) { uint8 newValue; switch ( paramID) { case SENSOR_CONF: Humidity_GetParameter( SENSOR_CONF, &newValue ); if ( newValue == ST_CFG_SENSOR_DISABLE) { if (humiEnabled) { humiEnabled = FALSE; osal_set_event( sensorTag_TaskID, ST_HUMIDITY_SENSOR_EVT); } } if ( newValue == ST_CFG_SENSOR_ENABLE ) { if (!humiEnabled) { humiEnabled = TRUE; humiState = 0; osal_set_event( sensorTag_TaskID, ST_HUMIDITY_SENSOR_EVT); } } break; case SENSOR_PERI: Humidity_GetParameter( SENSOR_PERI, &newValue ); sensorHumPeriod = newValue*SENSOR_PERIOD_RESOLUTION; break; default: // Should not get here break; } }
/************************************************************************************************** * @fn zapSBL_RxExt * * @brief This function is the registered callback for the UART to the external application * that is driving the serial boot load to the ZNP. * * input parameters * * @param port - Don't care. * @param event - Don't care. * * output parameters * * None. * * @return None. ************************************************************************************************** */ static void zapSBL_RxExt(uint8 port, uint8 event) { uint8 ch; (void)port; (void)event; // Use external UART Rx as the trigger to start an SBL session. if (!zapSBL_Active) { // Usurp and save to restore the currently registered UART transport callback. usurpedCB = uartRecord.callBackFunc; uartRecord.callBackFunc = zapSBL_Rx; znpSystemReset(ZNP_RESET_SOFT); HalBoardDelay(1000, TRUE); ch = SB_FORCE_BOOT; (void)HalUARTWrite(ZAP_SBL_ZNP_PORT, &ch, 1); (void)HalUARTWrite(ZAP_SBL_ZNP_PORT, &ch, 1); zapSBL_Active = TRUE; if (ZSuccess != osal_start_timerEx(zapSBL_TaskId, ZAP_SBL_EVT_EXIT, ZAP_SBL_DLY_EXIT)) { (void)osal_set_event(zapSBL_TaskId, ZAP_SBL_EVT_EXIT); } } while (HalUARTRead(ZAP_SBL_EXT_PORT, &ch, 1)) { (void)HalUARTWrite(ZAP_SBL_ZNP_PORT, &ch, 1); if (ZSuccess != osal_start_timerEx(zapSBL_TaskId, ZAP_SBL_EVT_EXIT, ZAP_SBL_DLY_EXIT)) { (void)osal_set_event(zapSBL_TaskId, ZAP_SBL_EVT_EXIT); } } }
/** * @brief Este callback es llamado por el stack de ZigBee luego de que la * operación de envío de datos es completada. * @param handle El handle que identifica la transmisión de datos. * @param status El estado de la operación. */ void zb_SendDataConfirm(uint8 handle, uint8 status) { if (status != ZB_SUCCESS) { if (++reportFailureNr >= REPORT_FAILURE_LIMIT) { // máx. de envíos fallidos alcanzado, reinicia la red HalLcdWriteString("REINICIANDO...", HAL_LCD_LINE_1); osal_set_event(sapi_TaskID, MY_START_EVT); reportFailureNr = 0; } } }
/********************************************************************* * @fn oadManagerSvcDiscovery * * @brief OAD Service discovery. * * @return none */ static void oadManagerSvcDiscovery(void) { uint8 oadServUUID[ATT_UUID_SIZE] = { TI_BASE_UUID_128( OAD_SERVICE_UUID ) }; // Initialize service discovery variables oadSvcStartHdl = oadSvcEndHdl = 0; if (GATT_DiscPrimaryServiceByUUID(oadManagerConnHandle, oadServUUID, ATT_UUID_SIZE, oadManagerTaskId) != SUCCESS) { (void)osal_set_event(oadManagerTaskId, SVC_DISCOVERY_EVT); } }
/********************************************************************* * @fn simpleProfileChangeCB * * @brief Callback from SimpleBLEProfile indicating a value change * * @param paramID - parameter ID of the value that was changed. * * @return none */ static void simpleProfileChangeCB( uint8 paramID ) { uint8 newValue, intval[3]; switch( paramID ) { case SIMPLEPROFILE_CHAR1: SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue ); #if (defined HAL_LCD) && (HAL_LCD == TRUE) HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10, HAL_LCD_LINE_3 ); #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) //发射功率有四个级别,-23dbm,-6dbm,0dbm, 4dbm, newValue为0~3,当连接的时候,只能设置-6, 0 if(newValue == LL_EXT_TX_POWER_MINUS_6_DBM || newValue == LL_EXT_TX_POWER_0_DBM) { HCI_EXT_SetTxPowerCmd(newValue); ProxReporter_SetParameter( PP_TX_POWER_LEVEL, sizeof ( int8 ), &newValue ); } break; case SIMPLEPROFILE_CHAR2: SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR2, &newValue ); #if (defined HAL_LCD) && (HAL_LCD == TRUE) HalLcdWriteStringValue( "Char 2:", (uint16)(newValue), 10, HAL_LCD_LINE_3 ); #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) if( newValue == 1 && gOpenDoorStep == 0 ){ gOpenDoorStep++; osal_set_event( simpleBLEPeripheral_TaskID, SBP_OPENDOOR_DEVICE_EVT ); } break; case SIMPLEPROFILE_CHAR3: SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, intval ); #if (defined HAL_LCD) && (HAL_LCD == TRUE) HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10, HAL_LCD_LINE_3 ); #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) advert_internal = (intval[0] - 0x30) *100 + (intval[1] - 0x30) * 10 + (intval[2] - 0x30); break; default: // should not reach here! break; } }