/********************************************************************* * @fn thermometer_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void thermometer_HandleKeys( uint8 shift, uint8 keys ) { bStatus_t status; uint8 notify_interval; if ( keys & HAL_KEY_SW_1 ) { // set simulated measurement flag index thermometerFlagsIdx+=1; if (thermometerFlagsIdx == FLAGS_IDX_MAX) { thermometerFlagsIdx = 0; } } //read stored interval value Thermometer_GetParameter( THERMOMETER_INTERVAL, ¬ify_interval ); if(notify_interval == 0) { thMeasTimerRunning = FALSE; } if ( keys & HAL_KEY_SW_2 ) { // if device is not in a connection, pressing the right key should toggle // advertising on and off. If timer is running, then will adv when meas is ready if((gapProfileState != GAPROLE_CONNECTED) && (thMeasTimerRunning == FALSE)) { uint8 current_adv_enabled_status; uint8 new_adv_enabled_status; //Find the current GAP advertisement status GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, ¤t_adv_enabled_status ); if( current_adv_enabled_status == FALSE ) { new_adv_enabled_status = TRUE; } else { new_adv_enabled_status = FALSE; } //change the GAP advertisement status to opposite of current status GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &new_adv_enabled_status ); } else //timer is running, so allow simulated changes { //change temperature, remove single precision if((thermometerCelcius) < 0X000175) { thermometerCelcius +=1; } else { uint8 thInterval = 30; attHandleValueInd_t intervalIndication; thermometerCelcius = 0X000173; //Simulate interval change Thermometer_SetParameter( THERMOMETER_INTERVAL, THERMOMETER_INTERVAL_LEN, &thInterval ); if(temperatureIntervalConfig == true) { intervalIndication.value[0] = thInterval; intervalIndication.handle = THERMOMETER_INTERVAL_VALUE_POS; status = Thermometer_IntervalIndicate( gapConnHandle, &intervalIndication, thermometerTaskId ); // we can fail if there was pending meas or not connected if (status != SUCCESS) { //queue indication thermometerStoreIndications(&intervalIndication); } } } } } }
/********************************************************************* * @fn Thermometer_handleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. * * @return none */ static void Thermometer_handleKeys(uint8_t shift, uint8_t keys) { bStatus_t status; uint8_t notify_interval; if (keys & KEY_UP) { // Set simulated measurement flag index. thermometerFlagsIdx+=1; if (thermometerFlagsIdx == FLAGS_IDX_MAX) { thermometerFlagsIdx = 0; } } // Read stored interval value. Thermometer_GetParameter(THERMOMETER_INTERVAL, ¬ify_interval); if(notify_interval == 0) { thMeasTimerRunning = FALSE; } if (keys & KEY_RIGHT) { // If device is not in a connection, pressing the right key should toggle // advertising on and off. If timer is running, then will adv when meas is // ready. if((gapProfileState != GAPROLE_CONNECTED) && (thMeasTimerRunning == FALSE)) { uint8_t current_adv_enabled_status; uint8_t new_adv_enabled_status; // Find the current GAP advertisement status. GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED, ¤t_adv_enabled_status); if(current_adv_enabled_status == FALSE) { new_adv_enabled_status = TRUE; } else { new_adv_enabled_status = FALSE; } // Change the GAP advertisement status to opposite of current status. GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &new_adv_enabled_status); } // Timer is running, so allow simulated changes. else { // Change temperature, remove single precision. if((thermometerCelcius) < 0x000175) { thermometerCelcius += 1; } else { uint16_t thInterval = 30; thermometerCelcius = 0x000173; // Simulate interval change. Thermometer_SetParameter(THERMOMETER_INTERVAL, THERMOMETER_INTERVAL_LEN, &thInterval); if(temperatureIntervalConfig == true) { attHandleValueInd_t intervalInd; intervalInd.pValue = GATT_bm_alloc(thermometer_connHandle, ATT_HANDLE_VALUE_IND, THERMOMETER_INTERVAL_LEN, NULL); if (intervalInd.pValue != NULL) { intervalInd.len = THERMOMETER_INTERVAL_LEN; intervalInd.pValue[0] = LO_UINT16(thInterval); intervalInd.pValue[1] = HI_UINT16(thInterval); intervalInd.handle = THERMOMETER_INTERVAL_VALUE_POS; status = Thermometer_IntervalIndicate(thermometer_connHandle, &intervalInd, ICall_getEntityId()); // We can fail if there was pending meas or not connected. if (status != SUCCESS) { // Queue indication. Thermometer_storeIndications(&intervalInd); } } } } } } }
/********************************************************************* * @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 ) { thermometerTaskId = task_id; /* Use for testing if chip default is 0xFFFFFF static uint8 bdAddress[6] = {0x1,0x2,0x3,0x4,0x5,0x6}; HCI_EXT_SetBDADDRCmd(bdAddress); */ // Setup the GAP Peripheral Role Profile { // Device doesn't start advertising until button is pressed uint8 initial_advertising_enable = FALSE; // 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 gapRole_AdvertOffTime = 0; uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST; uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY; uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT; // Set the GAP Role Parameters GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable ); GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime ); GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanResponseData ), scanResponseData ); GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData ); GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request ); GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval ); GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval ); GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency ); GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout ); } // Set the GAP Characteristics GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName ); // Setup the GAP Bond Manager { uint32 passkey = 0; // passkey "000000" uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; uint8 mitm = FALSE; uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; uint8 bonding = TRUE; 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 ); } // Setup the Thermometer Characteristic Values { uint8 thermometerSite = THERMOMETER_TYPE_MOUTH; Thermometer_SetParameter( THERMOMETER_TYPE, sizeof ( uint8 ), &thermometerSite ); thermometerIRange_t thermometerIRange= {4,60}; Thermometer_SetParameter( THERMOMETER_IRANGE, sizeof ( uint16 ), &thermometerIRange ); } // Stop config reads when done timeConfigDone = FALSE; // Initialize GATT Client VOID GATT_InitClient(); // Register to receive incoming ATT Indications/Notifications GATT_RegisterForInd( thermometerTaskId ); // Initialize GATT attributes GGS_AddService( GATT_ALL_SERVICES ); // GAP GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes Thermometer_AddService( GATT_ALL_SERVICES ); DevInfo_AddService( ); // Register for Thermometer service callback Thermometer_Register ( thermometerCB ); // Register for all key events - This app will handle all key events RegisterForKeys( thermometerTaskId ); #if defined( CC2540_MINIDK ) // makes sure LEDs are off HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); // For keyfob board set GPIO pins into a power-optimized state // Note that there is still some leakage current from the buzzer, // accelerometer, LEDs, and buttons on the PCB. P0SEL = 0; // Configure Port 0 as GPIO P1SEL = 0; // Configure Port 1 as GPIO P2SEL = 0; // Configure Port 2 as GPIO P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons), // all others (P0.2-P0.7) as output P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons) P1 = 0; // All pins on port 1 to low P2 = 0; // All pins on port 2 to low #endif // #if defined( CC2540_MINIDK ) // Setup a delayed profile startup osal_set_event( thermometerTaskId, TH_START_DEVICE_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 * notification ...). * * @param None. * * @return None. */ void Thermometer_init(void) { // ****************************************************************** // 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); // Hard code the DB Address till CC2650 board gets its own IEEE address. //uint8_t bdAddress[B_ADDR_LEN] = { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }; //HCI_EXT_SetBDADDRCmd(bdAddress); // Set device's Sleep Clock Accuracy //HCI_EXT_SetSCACmd(40); // Create an RTOS queue for message from profile to be sent to app. appMsgQueue = Util_constructQueue(&appMsg); // Setup clocks. Util_constructClock(&startDiscoveryClock, Thermometer_clockHandler, DEFAULT_DISCOVERY_DELAY, 0, false, THERMOMETER_START_DISC_EVT); Util_constructClock(&disconnectClock, Thermometer_clockHandler, DEFAULT_TERMINATE_DELAY, 0, false, THERMOMETER_DISCONNECT_EVT); Util_constructClock(&perMeasClock, Thermometer_clockHandler, DEFAULT_THERMOMETER_MEAS_DELAY, 0, false, THERMOMETER_PERIODIC_MEAS_EVT); Util_constructClock(&perIMeasClock, Thermometer_clockHandler, DEFAULT_THERMOMETER_IMEAS_DELAY, 0, false, THERMOMETER_PERIODIC_IMEAS_EVT); // Initialize keys on the SRF06. Board_initKeys(Thermometer_keyPressHandler); // Setup the GAP Peripheral Role Profile. { // Device doesn't start advertising until button is pressed. uint8_t initial_advertising_enable = FALSE; // 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 gapRole_AdvertOffTime = 0; uint8_t enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST; uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; uint16_t desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY; uint16_t desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT; // Set the GAP Role Parameters. GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable); GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t), &gapRole_AdvertOffTime); GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof (scanResponseData), scanResponseData); GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData); GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t), &enable_update_request); GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval); GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval); GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t), &desired_slave_latency); GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t), &desired_conn_timeout); } // Set the GAP Characteristics. GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName); // Setup the GAP Bond Manager. { uint32_t passkey = 0; // passkey "000000" uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; uint8_t mitm = FALSE; uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; uint8_t bonding = TRUE; GAPBondMgr_SetParameter(GAPBOND_DEFAULT_PASSCODE, sizeof (uint32_t), &passkey); GAPBondMgr_SetParameter(GAPBOND_PAIRING_MODE, sizeof(uint8_t), &pairMode); GAPBondMgr_SetParameter(GAPBOND_MITM_PROTECTION, sizeof(uint8_t), &mitm); GAPBondMgr_SetParameter(GAPBOND_IO_CAPABILITIES, sizeof(uint8_t), &ioCap); GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof(uint8_t), &bonding); } // Initialize GATT Client. VOID GATT_InitClient(); // Register to receive incoming ATT Indications/Notifications. GATT_RegisterForInd(selfEntity); // Initialize GATT attributes. GGS_AddService(GATT_ALL_SERVICES); // GAP GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes // Add Thermometer services. Thermometer_AddService(GATT_ALL_SERVICES); // Add device info services. DevInfo_AddService(); // Register for Thermometer service callback. Thermometer_Register (Thermometer_serviceCB); // Setup the Thermometer Characteristic Values. { uint8_t thermometerSite = THERMOMETER_TYPE_MOUTH; thermometerIRange_t thermometerIRange= {4,60000}; Thermometer_SetParameter(THERMOMETER_TYPE, sizeof(uint8_t), &thermometerSite); Thermometer_SetParameter(THERMOMETER_IRANGE, sizeof(thermometerIRange_t), &thermometerIRange); } // Initialize measurement storage table memset(thStoreMeas, 0, (sizeof(attHandleValueInd_t) * TH_STORE_MAX)); // Start the Device. VOID GAPRole_StartDevice(&thermometer_PeripheralCBs); // Register with bond manager after starting device. VOID GAPBondMgr_Register((gapBondCBs_t *)&thermometer_BondMgrCBs); }