diff --git a/le_bas.c b/le_bas.c index 6726edc..465eedb 100644 --- a/le_bas.c +++ b/le_bas.c @@ -14,17 +14,41 @@ extern "C" #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" - #include "nrf_sdh.h" #include "nrf_sdh_ble.h" #include "nrf_sdh_freertos.h" #include "ble_srv_common.h" +#include "FreeRTOS.h" +#include "task.h" + #ifdef __cplusplus } #endif -uint16_t bas_volt_read(void) { - return 3100; -} \ No newline at end of file +static uint16_t volt = 3100; + +uint16_t bas_volt_read(void) +{ + return volt; +} + +void battery_task(void *pArg) +{ + for (;;) + { + // battery psuedo code + vTaskDelay(pdMS_TO_TICKS(1000)); + volt = volt ? volt - 1 : 3100; + + // update battery volt + extern void le_edc_bat_volt_update(uint16_t bat_volt); + le_edc_bat_volt_update(volt); + } +} + +void bas_volt_init(void) +{ + xTaskCreate(battery_task, "bat_task", 160, NULL, 2, NULL); +} diff --git a/le_edc_srv.c b/le_edc_srv.c index 7088c5a..8d794dc 100644 --- a/le_edc_srv.c +++ b/le_edc_srv.c @@ -2,6 +2,7 @@ #if NRF_MODULE_ENABLED(BLE_EDC) #include "app_error.h" +#include "ble_conn_state.h" #include "ble_gatts.h" #include "ble_srv_common.h" #include "nrf_sdh_ble.h" @@ -37,16 +38,18 @@ typedef struct uint16_t battery_volt; } ble_edc_init_t; -static ble_edc_t ble_edc; +static ble_edc_t ble_edc = { + .conn_handle = BLE_CONN_HANDLE_INVALID, +}; static void on_connect(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt) { - // TODO... + p_edc->conn_handle = p_ble_evt->evt.common_evt.conn_handle; } static void on_disconnect(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt) { - // TODO... + p_edc->conn_handle = BLE_CONN_HANDLE_INVALID; } static void edc_ccc_update(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt) @@ -149,10 +152,51 @@ void le_edc_init(void) ble_edc_init_t edc_init; memset(&edc_init, 0x00, sizeof(edc_init)); + extern void bas_volt_init(void); + bas_volt_init(); + extern uint16_t bas_volt_read(void); edc_init.battery_volt = bas_volt_read(); ret_code_t err_code = ble_edc_init(&edc_init); APP_ERROR_CHECK(err_code); } +void le_edc_bat_volt_update(uint16_t bat_volt) +{ + ret_code_t err_code = NRF_SUCCESS; + + if (bat_volt == ble_edc.bat_volt) + { + return; + } + + // update database. + ble_gatts_value_t gatts_value = { + .p_value = (void *)&bat_volt, + .offset = 0, + .len = sizeof(bat_volt), + }; + err_code = sd_ble_gatts_value_set(BLE_CONN_HANDLE_INVALID, ble_edc.bat_volt_handles.value_handle, &gatts_value); + APP_ERROR_CHECK(err_code); + ble_edc.bat_volt = bat_volt; + + // send value if connected and notifying. + if (ble_edc.bat_volt_notify_en) + { + ble_gatts_hvx_params_t hvx_params = { + .handle = ble_edc.bat_volt_handles.value_handle, + .type = BLE_GATT_HVX_NOTIFICATION, + .offset = gatts_value.offset, + .p_len = &gatts_value.len, + .p_data = gatts_value.p_value, + }; + + if (ble_conn_state_status(ble_edc.conn_handle) == BLE_CONN_STATUS_CONNECTED) + { + err_code = sd_ble_gatts_hvx(ble_edc.conn_handle, &hvx_params); + APP_ERROR_CHECK(err_code); + } + } +} + #endif // NRF_MODULE_ENABLED(BLE_EDC)