diff --git a/ex_7/main.c b/ex_7/main.c index 9050615bd2671a279a73864adb185aaf1e08488a..672182f1ab42217264c616f2d8827547fddbe1b2 100644 --- a/ex_7/main.c +++ b/ex_7/main.c @@ -82,6 +82,10 @@ #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" +#include "nrf_temp.h" +#include "ble_hts.h" +#include "ble_bas.h" + #define DEVICE_NAME "Nordic_Template" /**< Name of device. Will be included in the advertising data. */ #define MANUFACTURER_NAME "NordicSemiconductor" /**< Manufacturer. Will be passed to Device Information Service. */ @@ -111,6 +115,9 @@ #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */ +// Determines if temperature type is given as characteristic (1) or as a field of measurement (0) +#define TEMP_TYPE_AS_CHARACTERISTIC 0 + NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */ NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/ @@ -118,6 +125,18 @@ BLE_ADVERTISING_DEF(m_advertising); static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ +BLE_HTS_DEF(m_hts); // Macro for defining a ble_hts instance +BLE_BAS_DEF(m_bas); // Macro for defining a ble_bas instance + +// Flag to keep track of when an indication confirmation is pending +static bool m_hts_meas_ind_conf_pending = false; +volatile uint32_t hts_counter; // hold dummy hts data + +// Function declarations +static void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt); +static void temperature_measurement_send(void); + + /* YOUR_JOB: Declare all services structure your application is using * BLE_XYZ_DEF(m_xyz); */ @@ -131,6 +150,79 @@ static ble_uuid_t m_adv_uuids[] = static void advertising_start(bool erase_bonds); +/* +* Function for generating a dummy temperature information packet. +*/ +static void generate_temperature(ble_hts_meas_t * p_meas) { + static ble_date_time_t time_stamp = { 2018, 16, 10, 16, 15, 0 }; + + uint32_t celciusX100; + + p_meas->temp_in_fahr_units = false; + p_meas->time_stamp_present = true; + p_meas->temp_type_present = (TEMP_TYPE_AS_CHARACTERISTIC ? false : true); + + celciusX100 = 2000+hts_counter++; // one unit is 0.01 Celcius + + p_meas->temp_in_celcius.exponent = -2; + p_meas->temp_in_celcius.mantissa = celciusX100; + p_meas->temp_in_fahr.exponent = -2; + p_meas->temp_in_fahr.mantissa = (32 * 100) + ((celciusX100 * 9) / 5); + p_meas->time_stamp = time_stamp; + p_meas->temp_type = BLE_HTS_TEMP_TYPE_FINGER; + + // update simulated time stamp + time_stamp.seconds += 27; + if (time_stamp.seconds > 59){ + time_stamp.seconds -= 60; + time_stamp.minutes++; + if (time_stamp.minutes > 59){ + time_stamp.minutes = 0; + } + } +} + +/* +* Function for handling the Health Thermometer Service events. +*/ +static void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt) { + switch (p_evt->evt_type) { + case BLE_HTS_EVT_INDICATION_ENABLED: + // Indication has been enabled, send a single temperature measurement + temperature_measurement_send(); + break; + case BLE_HTS_EVT_INDICATION_CONFIRMED: + m_hts_meas_ind_conf_pending = false; + break; + default: + // No implementation needed. + break; + } +} + +static void temperature_measurement_send(void) { + ble_hts_meas_t hts_meas; //Health Thermometer Service measurement structure + ret_code_t + err_code; + + if (!m_hts_meas_ind_conf_pending) { + generate_temperature(&hts_meas); + err_code = ble_hts_measurement_send(&m_hts, &hts_meas); + + switch (err_code) { + case NRF_SUCCESS: + // Measurement was successfully sent, wait for confirmation. + m_hts_meas_ind_conf_pending = true; + break; + case NRF_ERROR_INVALID_STATE: + // Ignore error. + break; + default: + APP_ERROR_HANDLER(err_code); + break; + } + } +} /**@brief Callback function for asserts in the SoftDevice. * @@ -278,15 +370,6 @@ static void on_yys_evt(ble_yy_service_t * p_yy_service, */ static void services_init(void) { - ret_code_t err_code; - nrf_ble_qwr_init_t qwr_init = {0}; - - // Initialize Queued Write Module. - qwr_init.error_handler = nrf_qwr_error_handler; - - err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init); - APP_ERROR_CHECK(err_code); - /* YOUR_JOB: Add code to initialize the services used by the application. ble_xxs_init_t xxs_init; ble_yys_init_t yys_init; @@ -309,6 +392,38 @@ static void services_init(void) err_code = ble_yy_service_init(&yys_init, &yy_init); APP_ERROR_CHECK(err_code); */ + ret_code_t err_code; + + ble_hts_init_t hts_init; + ble_bas_init_t bas_init; + + // Initialize Health Thermometer Service + memset(&hts_init, 0, sizeof(hts_init)); + hts_init.evt_handler = on_hts_evt; + hts_init.temp_type_as_characteristic = TEMP_TYPE_AS_CHARACTERISTIC; + hts_init.temp_type = BLE_HTS_TEMP_TYPE_BODY; + + // Here the sec level for the Health Thermometer Service can be changed/increased. + hts_init.ht_meas_cccd_wr_sec = SEC_JUST_WORKS; + hts_init.ht_type_rd_sec = SEC_OPEN; + err_code = ble_hts_init(&m_hts, &hts_init); + APP_ERROR_CHECK(err_code); + + // Initialize Battery Service. + memset(&bas_init, 0, sizeof(bas_init)); + + // Here the sec level for the Battery Service can be changed/increased. + bas_init.bl_rd_sec = SEC_OPEN; + bas_init.bl_cccd_wr_sec = SEC_OPEN; + bas_init.bl_report_rd_sec = SEC_OPEN; + + bas_init.evt_handler = NULL; + bas_init.support_notification = true; + bas_init.p_report_ref = NULL; + bas_init.initial_batt_level = 100; + + err_code = ble_bas_init(&m_bas, &bas_init); + APP_ERROR_CHECK(err_code); } @@ -594,6 +709,12 @@ static void bsp_event_handler(bsp_event_t event) } } break; // BSP_EVENT_KEY_0 + case BSP_EVENT_KEY_0: + if (m_conn_handle != BLE_CONN_HANDLE_INVALID) + { + temperature_measurement_send(); + } + break; default: break; diff --git a/ex_7/pca10040/s132/config/sdk_config.h b/ex_7/pca10040/s132/config/sdk_config.h index 2c632ab0b923dc641804317190778e15587fb03d..80de845ed90ace34d0335b24060192935a6c4a7c 100644 --- a/ex_7/pca10040/s132/config/sdk_config.h +++ b/ex_7/pca10040/s132/config/sdk_config.h @@ -245,7 +245,7 @@ // <e> BLE_BAS_ENABLED - ble_bas - Battery Service //========================================================== #ifndef BLE_BAS_ENABLED -#define BLE_BAS_ENABLED 0 +#define BLE_BAS_ENABLED 1 #endif // <e> BLE_BAS_CONFIG_LOG_ENABLED - Enables logging in the module. //========================================================== @@ -353,7 +353,7 @@ #ifndef BLE_HTS_ENABLED -#define BLE_HTS_ENABLED 0 +#define BLE_HTS_ENABLED 1 #endif // <q> BLE_IAS_C_ENABLED - ble_ias_c - Immediate Alert Service Client diff --git a/ex_7/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject b/ex_7/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject index f1e4416e007efc4ce1c5fefc933e178abfa6e89b..b5bb01f1cbe74db2754744a9637b1da78dd33c40 100644 --- a/ex_7/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject +++ b/ex_7/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject @@ -131,6 +131,8 @@ <file file_name="../../../../../nRF5_SDK_15.3.0_59ac345/components/softdevice/common/nrf_sdh_ble.c" /> <file file_name="../../../../../nRF5_SDK_15.3.0_59ac345/components/softdevice/common/nrf_sdh_soc.c" /> </folder> + <file file_name="../../../../../nRF5_SDK_15.3.0_59ac345/components/ble/ble_services/ble_hts/ble_hts.c" /> + <file file_name="../../../../../nRF5_SDK_15.3.0_59ac345/components/ble/ble_services/ble_bas/ble_bas.c" /> </project> <configuration Name="Release"