notifications

This commit is contained in:
leo 2023-06-14 22:00:42 +02:00
parent 451251ee7e
commit a18274bec0
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
3 changed files with 55 additions and 1 deletions

View File

@ -5,10 +5,12 @@
#include "host/ble_gap.h"
#include "host/ble_gatt.h"
#include "host/ble_hs_adv.h"
#include "host/ble_hs_mbuf.h"
#include "host/ble_uuid.h"
#include "measure.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "os/os_mbuf.h"
#include "power_profiler.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
@ -31,10 +33,15 @@ static struct ble_gap_adv_params adv_params = {
.disc_mode = BLE_GAP_DISC_MODE_GEN,
};
static uint16_t conn_handle;
static uint16_t cs_handle[INPUTS_NUM];
static uint16_t ev_handle[INPUTS_NUM];
static uint16_t settings_handle[SETTINGS_CHRS_NUM];
static uint8_t cs_notify_state[INPUTS_NUM];
static uint8_t ev_notify_state[INPUTS_NUM];
static struct ble_context ev_ctxt[INPUTS_NUM];
static struct ble_context cs_ctxt[INPUTS_NUM];
static struct ble_context settings_ctxt[SETTINGS_CHRS_NUM];
@ -120,9 +127,10 @@ static void ble_host_task(){
nimble_port_freertos_deinit();
}
static int ble_gap_event(struct ble_gap_event *event, void* arg);
static void ble_advertise(void){
ESP_ERROR_CHECK(ble_gap_adv_set_fields(&adv_fields));
ESP_ERROR_CHECK(ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, NULL, NULL));
ESP_ERROR_CHECK(ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, ble_gap_event, NULL));
}
static void gatt_svr_init(void){
@ -141,6 +149,28 @@ static void ble_on_reset(int p){
}
static int ble_gap_event(struct ble_gap_event *event, void* arg){
switch(event->type){
case BLE_GAP_EVENT_CONNECT:
if(event->connect.status)
ble_advertise();
else
conn_handle = event->connect.conn_handle;
break;
case BLE_GAP_EVENT_SUBSCRIBE:
ESP_LOGI(TAG, "Subscribe %d", event->subscribe.attr_handle);
for(int i = 0; i < INPUTS_NUM; i++){
if(event->subscribe.attr_handle == cs_handle[i])
cs_notify_state[i] = event->subscribe.cur_notify;
if(event->subscribe.attr_handle == ev_handle[i])
ev_notify_state[i] = event->subscribe.cur_notify;
}
break;
}
return ESP_OK;
}
// Generate a characteristic for each inputs
static void generate_svc_defs(configuration* conf, measurements* meas){
for(int i = 0; i < INPUTS_NUM; i++){
@ -157,9 +187,11 @@ static void generate_svc_defs(configuration* conf, measurements* meas){
cs_ctxt[i].handle = ind;
cs_char_array[i].arg = &cs_ctxt[i];
cs_char_array[i].val_handle = &cs_handle[i];
ev_ctxt[i].handle = ind;
rvs_char_array[i].arg = &ev_ctxt[i];
rvs_char_array[i].val_handle = &ev_handle[i];
}
memset(&cs_char_array[INPUTS_NUM], 0, sizeof(cs_char_array[INPUTS_NUM]));
@ -184,3 +216,20 @@ void initBLE(configuration* conf, measurements* meas){
ESP_ERROR_CHECK(ble_svc_gap_device_name_set("Power Profiler"));
nimble_port_freertos_init(ble_host_task);
}
void notify_update(measurements* meas){
for(int i = 0; i < INPUTS_NUM; i++){
if(cs_notify_state[i]){
ESP_LOGI(TAG, "notify current %d", i);
uint32_t data = meas->amperes[i];
struct os_mbuf* om = ble_hs_mbuf_from_flat(&data, sizeof(data));
ESP_ERROR_CHECK(ble_gattc_notify_custom(conn_handle, cs_handle[i], om));
}
if(ev_notify_state[i]){
ESP_LOGI(TAG, "notify voltage %d", i);
uint32_t data = meas->raw_volt[i];
struct os_mbuf* om = ble_hs_mbuf_from_flat(&data, sizeof(data));
ESP_ERROR_CHECK(ble_gattc_notify_custom(conn_handle, ev_handle[i], om));
}
}
}

View File

@ -5,6 +5,8 @@
#include "configuration.h"
#include "measure.h"
#define TAG "BLE"
struct ble_context {
uint16_t handle;
configuration *conf;
@ -12,3 +14,4 @@ struct ble_context {
};
void initBLE(configuration* conf, measurements* meas);
void notify_update(measurements* meas);

View File

@ -159,6 +159,8 @@ void app_main(void){
meas_volts[i] = mv;
}
notify_update(&meas);
ESP_LOGI(TAG, "overrange %d", overrange_flag);
overrange_flag = 0;