diff --git a/main/BLE_UUID.h b/main/BLE_UUID.h index 2828ffa..e60e383 100644 --- a/main/BLE_UUID.h +++ b/main/BLE_UUID.h @@ -14,6 +14,7 @@ #define AUTO_RANGE_CHAR 0x2C15 #define CHAR_PRES_FORMAT 0x2904 +#define SOURCE_GAIN_DESCR 0x2920 #define PPM_UNIT_UUID 0x27C4 #define PERCENT_UNIT_UUID 0x27AD diff --git a/main/ble.c b/main/ble.c index d202ea2..64bd0a4 100644 --- a/main/ble.c +++ b/main/ble.c @@ -56,14 +56,6 @@ static struct ble_gatt_chr_def current_measure_char_template = { .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, .min_key_size = 0, .val_handle = cs_handle, - .descriptors = (struct ble_gatt_dsc_def[]){ - [0] = { - .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), - .att_flags = BLE_ATT_F_READ, - .access_cb = gatt_char_access_cs, - }, - { 0 }, - }, }; static struct ble_gatt_chr_def voltage_measure_char_template = { .uuid = BLE_UUID16_DECLARE(VOLTAGE_CHAR), @@ -72,19 +64,44 @@ static struct ble_gatt_chr_def voltage_measure_char_template = { .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, .min_key_size = 0, .val_handle = ev_handle, - .descriptors = (struct ble_gatt_dsc_def[]){ - [0] = { - .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), - .att_flags = BLE_ATT_F_READ, - .access_cb = gatt_char_access_ev, - }, - { 0 }, - }, }; +static struct ble_gatt_dsc_def cs_descr_template[] = { + [0] = { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_char_access_cs, + .arg = NULL, + }, + [1] = { + .uuid = BLE_UUID16_DECLARE(SOURCE_GAIN_DESCR), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_char_access_cs, + .arg = NULL, + }, + { 0 }, +}; +static struct ble_gatt_dsc_def ev_descr_template[] = { + [0] = { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_char_access_ev, + }, + [1] = { + .uuid = BLE_UUID16_DECLARE(SOURCE_GAIN_DESCR), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_char_access_ev, + }, + { 0 }, +}; + + static struct ble_gatt_chr_def cs_char_array[INPUTS_NUM + 1]; static struct ble_gatt_chr_def rvs_char_array[INPUTS_NUM + 1]; +static struct ble_gatt_dsc_def cs_descr_array[INPUTS_NUM + 1][3]; +static struct ble_gatt_dsc_def rvs_descr_array[INPUTS_NUM + 1][3]; + static struct ble_gatt_svc_def gatt_svr_svcs[] = { [CS_SVC_ID] = { @@ -267,16 +284,23 @@ static void generate_svc_defs(configuration* conf, measurements* meas, measureme memcpy(&cs_char_array[i], ¤t_measure_char_template, sizeof(current_measure_char_template)); memcpy(&rvs_char_array[i], &voltage_measure_char_template, sizeof(voltage_measure_char_template)); - + + memcpy(&cs_descr_array[i], &cs_descr_template, sizeof(cs_descr_template)); + memcpy(&rvs_descr_array[i], &ev_descr_template, sizeof(ev_descr_template)); + uint16_t ind = i; cs_ctxt[i].handle = ind; cs_char_array[i].arg = &cs_ctxt[i]; cs_char_array[i].val_handle = &cs_handle[i]; + cs_char_array[i].descriptors = (struct ble_gatt_dsc_def*)&cs_descr_array[i]; + cs_char_array[i].descriptors[1].arg = &cs_ctxt[i]; ev_ctxt[i].handle = ind; rvs_char_array[i].arg = &ev_ctxt[i]; rvs_char_array[i].val_handle = &ev_handle[i]; + rvs_char_array[i].descriptors = (struct ble_gatt_dsc_def*)&rvs_descr_array[i]; + rvs_char_array[i].descriptors[1].arg = &ev_ctxt[i]; } memset(&cs_char_array[INPUTS_NUM], 0, sizeof(cs_char_array[INPUTS_NUM])); diff --git a/main/gatt_svcs.c b/main/gatt_svcs.c index 0f353e8..cb02de9 100644 --- a/main/gatt_svcs.c +++ b/main/gatt_svcs.c @@ -99,7 +99,16 @@ int gatt_char_access_cs(uint16_t conn_handle, uint16_t attr_handle, struct ble_g rc = os_mbuf_append(ctxt->om, &data, sizeof(data)); break; case BLE_GATT_ACCESS_OP_READ_DSC: - rc = os_mbuf_append(ctxt->om, ¤t_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE); + ; + if(arg == NULL) // characteristic presentation format + rc = os_mbuf_append(ctxt->om, ¤t_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE); + else{ + struct ble_context *c_ctxt = arg; + uint16_t handle = c_ctxt->handle; + + unsigned int gain = c_ctxt->meas_ctxt->inputs[handle].gain; + rc = os_mbuf_append(ctxt->om, &gain, sizeof(gain)); + } break; } @@ -119,7 +128,15 @@ int gatt_char_access_ev(uint16_t conn_handle, uint16_t attr_handle, struct ble_g rc = os_mbuf_append(ctxt->om, &data, sizeof(data)); break; case BLE_GATT_ACCESS_OP_READ_DSC: - rc = os_mbuf_append(ctxt->om, &raw_voltage_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE); + if(arg == NULL) // characteristic presentation format + rc = os_mbuf_append(ctxt->om, &raw_voltage_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE); + else{ + struct ble_context *c_ctxt = arg; + uint16_t handle = c_ctxt->handle; + + unsigned int gain = c_ctxt->meas_ctxt->inputs[handle].gain; + rc = os_mbuf_append(ctxt->om, &gain, sizeof(gain)); + } break; }