/******************************************************************************* * @fn SensorTag_taskFxn * * @brief Application task entry point for the SensorTag * * @param a0, a1 (not used) * * @return none */ static void SensorTag_taskFxn(UArg a0, UArg a1) { // Initialize application SensorTag_init(); // Application main loop for (;;) { // Waits for a signal to the semaphore associated with the calling thread. // Note that the semaphore associated with a thread is signalled when a // message is queued to the message receive queue of the thread or when // ICall_signal() function is called onto the semaphore. ICall_Errno errno = ICall_wait(ICALL_TIMEOUT_FOREVER); if (errno == ICALL_ERRNO_SUCCESS) { ICall_EntityID dest; ICall_ServiceEnum src; ICall_HciExtEvt *pMsg = NULL; if (ICall_fetchServiceMsg(&src, &dest, (void **)&pMsg) == ICALL_ERRNO_SUCCESS) { if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity)) { // Process inter-task message SensorTag_processStackMsg((ICall_Hdr *)pMsg); } if (pMsg) { ICall_freeMsg(pMsg); } } // If RTOS queue is not empty, process app message. while (!Queue_empty(appMsgQueue)) { stEvt_t *pMsg = (stEvt_t *)Util_dequeueMsg(appMsgQueue); if (pMsg) { // Process message. SensorTag_processAppMsg(pMsg); // Free the space from the message. ICall_free(pMsg); } } // Process new data if available SensorTagKeys_processEvent(); SensorTagOpt_processSensorEvent(); SensorTagMov_processSensorEvent(); } if (!!(events & ST_PERIODIC_EVT)) { events &= ~ST_PERIODIC_EVT; if (gapProfileState == GAPROLE_CONNECTED || gapProfileState == GAPROLE_ADVERTISING) { Util_startClock(&periodicClock); } // Perform periodic application task if (gapProfileState == GAPROLE_CONNECTED) { SensorTag_performPeriodicTask(); } // Blink green LED when advertising if (gapProfileState == GAPROLE_ADVERTISING) { SensorTag_blinkLed(Board_LED2,1); #ifdef FEATURE_LCD SensorTag_displayBatteryVoltage(); #endif } } #ifdef FEATURE_OAD while (!Queue_empty(hOadQ)) { oadTargetWrite_t *oadWriteEvt = Queue_dequeue(hOadQ); // Identify new image. if (oadWriteEvt->event == OAD_WRITE_IDENTIFY_REQ) { OAD_imgIdentifyWrite(oadWriteEvt->connHandle, oadWriteEvt->pData); } // Write a next block request. else if (oadWriteEvt->event == OAD_WRITE_BLOCK_REQ) { OAD_imgBlockWrite(oadWriteEvt->connHandle, oadWriteEvt->pData); } // Free buffer. ICall_free(oadWriteEvt); } #endif //FEATURE_OAD } // task loop }
/******************************************************************************* * @fn SensorTag_processStateChangeEvt * * @brief Process a pending GAP Role state change event. * * @param newState - new state * * @return none */ static void SensorTag_processStateChangeEvt(gaprole_States_t newState) { #ifdef PLUS_BROADCASTER static bool firstConnFlag = false; #endif // PLUS_BROADCASTER switch (newState) { case GAPROLE_STARTED: { uint8_t ownAddress[B_ADDR_LEN]; uint8_t systemId[DEVINFO_SYSTEM_ID_LEN]; SensorTag_blinkLed(Board_LED2, 5); GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress); // use 6 bytes of device address for 8 bytes of system ID value systemId[0] = ownAddress[0]; systemId[1] = ownAddress[1]; systemId[2] = ownAddress[2]; // set middle bytes to zero systemId[4] = 0x00; systemId[3] = 0x00; // shift three bytes up systemId[7] = ownAddress[5]; systemId[6] = ownAddress[4]; systemId[5] = ownAddress[3]; DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId); LCD_WRITES_STATUS("Initialized"); } break; case GAPROLE_ADVERTISING: // Start the clock if (!Util_isActive(&periodicClock)) { Util_startClock(&periodicClock); } // Make sure key presses are not stuck sensorTag_updateAdvertisingData(0); LCD_WRITES_STATUS("Advertising"); break; case GAPROLE_CONNECTED: { // Start the clock if (!Util_isActive(&periodicClock)) { Util_startClock(&periodicClock); } // Turn of LEDs and buzzer PIN_setOutputValue(hGpioPin, Board_LED1, Board_LED_OFF); PIN_setOutputValue(hGpioPin, Board_LED2, Board_LED_OFF); PIN_setOutputValue(hGpioPin, Board_BUZZER, Board_BUZZER_OFF); #ifdef FEATURE_OAD SensorTagConnectionControl_update(); #endif #ifdef PLUS_BROADCASTER // Only turn advertising on for this state when we first connect // otherwise, when we go from connected_advertising back to this state // we will be turning advertising back on. if (firstConnFlag == false) { uint8_t advertEnabled = TRUE; // Turn on Advertising GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertEnabled); firstConnFlag = true; } #endif // PLUS_BROADCASTER } LCD_WRITES_STATUS("Connected"); break; case GAPROLE_CONNECTED_ADV: break; case GAPROLE_WAITING: case GAPROLE_WAITING_AFTER_TIMEOUT: SensorTag_resetAllSensors(); LCD_WRITES_STATUS("Waiting..."); break; case GAPROLE_ERROR: SensorTag_resetAllSensors(); PIN_setOutputValue(hGpioPin,Board_LED1, Board_LED_ON); LCD_WRITES_STATUS("Error"); break; default: break; } gapProfileState = newState; }
/******************************************************************************* * @fn SensorTag_init * * @brief Called during initialization and contains application * specific initialization (ie. hardware initialization/setup, * table initialization, power up notification, etc), and * profile initialization/setup. * * @param none * * @return none */ static void SensorTag_init(void) { uint8_t selfTestMap; // Setup I2C for sensors bspI2cInit(); // Handling of buttons, LED, relay hGpioPin = PIN_open(&pinGpioState, SensortagAppPinTable); PIN_registerIntCb(hGpioPin, SensorTag_callback); // *************************************************************************** // N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp // *************************************************************************** // Register the current thread as an ICall dispatcher application // so that the application can send and receive messages. ICall_registerApp(&selfEntity, &sem); // Create an RTOS queue for message from profile to be sent to app. appMsgQueue = Util_constructQueue(&appMsg); // Create one-shot clocks for internal periodic events. Util_constructClock(&periodicClock, SensorTag_clockHandler, ST_PERIODIC_EVT_PERIOD, 0, false, ST_PERIODIC_EVT); // Create periodic clocks for internal periodic events Util_constructClock(&UserDefined_periodicClock, SensorTag_clockHandler, ST_USER_DEFINED_PERIODIC_EVT_PERIOD, ST_USER_DEFINED_PERIODIC_EVT_PERIOD, false, ST_USER_DEFINED_PERIODIC_EVT); // Setup the GAP GAP_SetParamValue(TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL); // Setup the GAP Peripheral Role Profile { // For all hardware platforms, device starts advertising upon initialization uint8_t initialAdvertEnable = TRUE; // By setting this to zero, the device will go into the waiting state after // being discoverable for 30.72 second, and will not being advertising again // until the enabler is set back to TRUE uint16_t advertOffTime = 0; uint8_t enableUpdateRequest = DEFAULT_ENABLE_UPDATE_REQUEST; uint16_t desiredMinInterval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16_t desiredMaxInterval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; uint16_t desiredSlaveLatency = DEFAULT_DESIRED_SLAVE_LATENCY; uint16_t desiredConnTimeout = DEFAULT_DESIRED_CONN_TIMEOUT; // Set the GAP Role Parameters GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initialAdvertEnable); GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t), &advertOffTime); GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData); GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData); GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t), &enableUpdateRequest); GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desiredMinInterval); GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desiredMaxInterval); GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t), &desiredSlaveLatency); GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t), &desiredConnTimeout); } // Set the GAP Characteristics GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, (void*)attDeviceName); #ifdef FEATURE_OAD // Register connection parameter update GAPRole_RegisterAppCBs( ¶mUpdateCB); #endif // Set advertising interval { uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL; GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MIN, advInt); GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MAX, advInt); GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, advInt); GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, advInt); } // Initialize GATT attributes GGS_AddService(GATT_ALL_SERVICES); // GAP GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes DevInfo_AddService(); // Device Information Service // Add application specific device information SensorTag_setDeviceInfo(); // Power on self-test for sensors, flash and DevPack selfTestMap = sensorTestExecute(ST_TEST_MAP); if (selfTestMap == ST_TEST_MAP) { SensorTag_blinkLed(Board_LED2,TEST_INDICATION_BLINKS); } else { SensorTag_blinkLed(Board_LED1,TEST_INDICATION_BLINKS); } #ifdef FACTORY_IMAGE // Check if a factory image exists and apply current image if necessary if (!SensorTag_hasFactoryImage()) { SensorTag_saveFactoryImage(); } #endif // Initialize sensors who don't have their own tasks SensorTagMov_init(); // Movement processor SensorTagOpt_init(); // Light meter // Auxiliary services SensorTagKeys_init(); // Simple Keys SensorTagIO_init(); // IO (LED+buzzer+self test) #ifdef FEATURE_REGISTER_SERVICE Register_addService(); // Generic register access #endif #ifdef FEATURE_LCD SensorTagDisplay_init(); // Display service DevPack LCD #endif #ifdef FEATURE_OAD SensorTagConnectionControl_init(); // Connection control to // support OAD for iOs/Android OAD_addService(); // OAD Profile OAD_register((oadTargetCBs_t *)&simpleBLEPeripheral_oadCBs); hOadQ = Util_constructQueue(&oadQ); #endif // Start the Device GAPRole_StartDevice(&SensorTag_gapRoleCBs); // Start Bond Manager VOID GAPBondMgr_Register(NULL); // Enable interrupt handling for keys and relay PIN_registerIntCb(hGpioPin, SensorTag_callback); }