From a3b8ed0a54b599d2baae99ccd2b99f5fa1548833 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 20 Dec 2022 17:24:27 +0100 Subject: [PATCH] moar services --- components/BTlib/BTlib_nimble.c | 247 +++++++++++++++++- components/BTlib/include/BTlib_nimble.h | 31 +++ components/MMC56x3/MMC56x3.c | 6 + components/MMC56x3/include/MMC56x3.h | 3 +- components/battery/battery.c | 1 + components/battery/include/battery.h | 1 + components/configuration/CMakeLists.txt | 2 +- components/configuration/configuration.c | 5 + .../configuration/include/configuration.h | 2 + default_conf.csv | 6 +- main/CO2_Sense.c | 2 +- nvs.bin | Bin 24576 -> 24576 bytes 12 files changed, 297 insertions(+), 9 deletions(-) diff --git a/components/BTlib/BTlib_nimble.c b/components/BTlib/BTlib_nimble.c index ad1efa8..aa88d9d 100644 --- a/components/BTlib/BTlib_nimble.c +++ b/components/BTlib/BTlib_nimble.c @@ -1,7 +1,12 @@ +#include "battery.h" +#include "host/ble_att.h" #include "host/ble_gap.h" +#include "host/ble_gatt.h" +#include "host/ble_uuid.h" #include "nimble/hci_common.h" #include "os/os_mbuf.h" #include "sdkconfig.h" +#include #ifdef CONFIG_BT_NIMBLE_ENABLED #include "BTlib_nimble.h" @@ -19,6 +24,8 @@ static uint8_t ble_addr_type; static uint16_t conn_handle; static uint16_t es_handle[ES_CHAR_NB]; static uint16_t batt_handle[BATT_CHAR_NB]; +static uint16_t pow_handle[POWER_CHAR_NB]; +static uint16_t magfield_handle[MAGFIELD_CHAR_NB]; static uint8_t notify_state[ES_CHAR_NB]; static int ble_gap_event(struct ble_gap_event *event, void* arg); @@ -29,6 +36,16 @@ static int gatt_svr_chr_access_hum(uint16_t conn_handle, uint16_t attr_handle, s static int gatt_svr_chr_access_batt_level(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); +static int gatt_svr_chr_access_power_voltage(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); +static int gatt_svr_chr_access_power_pin(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); +static int gatt_svr_chr_access_power_pout(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); +static int gatt_svr_chr_access_power_format(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); + +static int gatt_svr_chr_access_magfield_x(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); +static int gatt_svr_chr_access_magfield_y(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); +static int gatt_svr_chr_access_magfield_z(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); +static int gatt_svr_chr_access_magfield_format(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); + static int gatt_svr_init(void); ES_char_descr_t CO2_char_descr = { @@ -72,6 +89,30 @@ char_pres_format_t batt_level_char_pres_format = { .descr = NSP_DESC_MAIN, }; +char_pres_format_t voltage_char_pres_format = { + .format = FORMAT_UINT16, + .exponent = -3, + .unit = VOLTAGE_UNIT_UUID, + .namespc = 1, + .descr = NSP_DESC_MAIN, +}; + +char_pres_format_t power_char_pres_format = { + .format = FORMAT_INT16, + .exponent = -3, + .unit = POWER_UNIT_UUID, + .namespc = 1, + .descr = NSP_DESC_MAIN, +}; + +char_pres_format_t magfield_char_pres_format = { + .format = FORMAT_INT32, + .exponent = -12, + .unit = TESLA_UNIT_UUID, + .namespc = 1, + .descr = NSP_DESC_INSIDE, +}; + struct ble_hs_adv_fields adv_fields = { .flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP, .tx_pwr_lvl_is_present = 1, @@ -92,12 +133,12 @@ struct ble_gap_adv_params adv_params = { }; struct ble_gap_upd_params conn_params = { - .itvl_min = 3200, //Minimum value for connection interval in 1.25ms units min=6 (7.5ms) max=3200 (4s) - .itvl_max = 3200, + .itvl_min = 100, //Minimum value for connection interval in 1.25ms units min=6 (7.5ms) max=3200 (4s) + .itvl_max = 1000, //3200, .latency = 0, .supervision_timeout = 3200, //Supervision timeout in 10ms units min=10 max=3200 .min_ce_len = 0, //Minimum length of connection event in 0.625ms units - .max_ce_len = 0, + .max_ce_len = 10, }; static struct ble_gatt_svc_def gatt_svr_svcs[] = { @@ -170,7 +211,7 @@ static struct ble_gatt_svc_def gatt_svr_svcs[] = { .uuid = BLE_UUID16_DECLARE(BATT_LEVEL_UUID), .access_cb = gatt_svr_chr_access_batt_level, .val_handle = &batt_handle[BATT_CHAR_LEVEL], - .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, + .flags = BLE_GATT_CHR_F_READ, .descriptors = (struct ble_gatt_dsc_def[]){ { .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), @@ -183,6 +224,106 @@ static struct ble_gatt_svc_def gatt_svr_svcs[] = { { 0 }, }, }, + [POWER_SVC_IDX] = { + // power status service + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID32_DECLARE(POWER_SVC_UUID), + .characteristics = (struct ble_gatt_chr_def[]){ + [POWER_CHAR_VOLTAGE] = { + .uuid = BLE_UUID16_DECLARE(BATT_VOLTAGE_UUID), + .access_cb = gatt_svr_chr_access_power_voltage, + .val_handle = &pow_handle[POWER_CHAR_VOLTAGE], + .flags = BLE_GATT_CHR_F_READ, + .descriptors = (struct ble_gatt_dsc_def[]){ + { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_svr_chr_access_power_voltage, + }, + { 0 }, + }, + }, + [POWER_CHAR_PIN] = { + .uuid = BLE_UUID16_DECLARE(ALIM_POWER_IN_UUID), + .access_cb = gatt_svr_chr_access_power_pin, + .val_handle = &pow_handle[POWER_CHAR_PIN], + .flags = BLE_GATT_CHR_F_READ, + .descriptors = (struct ble_gatt_dsc_def[]){ + { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_svr_chr_access_power_format, + }, + { 0 }, + }, + }, + [POWER_CHAR_POUT] = { + .uuid = BLE_UUID16_DECLARE(ALIM_POWER_OUT_UUID), + .access_cb = gatt_svr_chr_access_power_pout, + .val_handle = &pow_handle[POWER_CHAR_POUT], + .flags = BLE_GATT_CHR_F_READ, + .descriptors = (struct ble_gatt_dsc_def[]){ + { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_svr_chr_access_power_format, + }, + { 0 }, + }, + }, + { 0 }, + }, + }, + [MAGFIELD_SVC_UUID] = { + // magnetic field measure (debug?) + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID32_DECLARE(MAGFIELD_SVC_UUID), + .characteristics = (struct ble_gatt_chr_def[]){ + [MAGFIELD_CHAR_X] = { + .uuid = BLE_UUID16_DECLARE(MAGFIELD_X_UUID), + .access_cb = gatt_svr_chr_access_magfield_x, + .val_handle = &magfield_handle[MAGFIELD_CHAR_X], + .flags = BLE_GATT_CHR_F_READ, + .descriptors = (struct ble_gatt_dsc_def[]){ + { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_svr_chr_access_magfield_format, + }, + { 0 } + }, + }, + [MAGFIELD_CHAR_Y] = { + .uuid = BLE_UUID16_DECLARE(MAGFIELD_Y_UUID), + .access_cb = gatt_svr_chr_access_magfield_y, + .val_handle = &magfield_handle[MAGFIELD_CHAR_Y], + .flags = BLE_GATT_CHR_F_READ, + .descriptors = (struct ble_gatt_dsc_def[]){ + { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_svr_chr_access_magfield_format, + }, + { 0 } + }, + }, + [MAGFIELD_CHAR_Z] = { + .uuid = BLE_UUID16_DECLARE(MAGFIELD_Z_UUID), + .access_cb = gatt_svr_chr_access_magfield_z, + .val_handle = &magfield_handle[MAGFIELD_CHAR_Z], + .flags = BLE_GATT_CHR_F_READ, + .descriptors = (struct ble_gatt_dsc_def[]){ + { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .att_flags = BLE_ATT_F_READ, + .access_cb = gatt_svr_chr_access_magfield_format, + }, + { 0 } + }, + }, + { 0 }, + }, + }, { 0 }, }; @@ -277,6 +418,104 @@ static int gatt_svr_chr_access_batt_level(uint16_t conn_handle, uint16_t attr_ha return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; } +static int gatt_svr_chr_access_power_voltage(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ + int rc = 0; + + ESP_LOGI(NIMBLE_LOG_TAG, "batt voltage access event"); + + switch(ctxt->op){ + case BLE_GATT_ACCESS_OP_READ_CHR: + { + update_battery_level(main_app_conf->battery_conf); + uint16_t battery_voltage = main_app_conf->battery_conf->data->battery_mv; + rc = os_mbuf_append(ctxt->om, &battery_voltage, sizeof(battery_voltage)); + } + break; + case BLE_GATT_ACCESS_OP_READ_DSC: + { + uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid); + switch(uuid){ + case CHAR_PRES_FORMAT: + rc = os_mbuf_append(ctxt->om, &voltage_char_pres_format, 7); + break; + } + } + break; + } + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + + +static int gatt_svr_chr_access_power_pin(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ + int rc = 0; + + ESP_LOGI(NIMBLE_LOG_TAG, "batt power access event"); + + switch(ctxt->op){ + case BLE_GATT_ACCESS_OP_READ_CHR: + { + update_power_inout_readings(main_app_conf->power_conf->data); + uint16_t powerin = main_app_conf->power_conf->data->pin; + rc = os_mbuf_append(ctxt->om, &powerin, sizeof(powerin)); + } + break; + case BLE_GATT_ACCESS_OP_READ_DSC: + { + uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid); + switch(uuid){ + case CHAR_PRES_FORMAT: + rc = os_mbuf_append(ctxt->om, &voltage_char_pres_format, 7); + break; + } + } + break; + } + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} +static int gatt_svr_chr_access_power_pout(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ + int rc = 0; + + ESP_LOGI(NIMBLE_LOG_TAG, "batt power access event"); + + switch(ctxt->op){ + case BLE_GATT_ACCESS_OP_READ_CHR: + { + update_power_inout_readings(main_app_conf->power_conf->data); + uint16_t powerout = main_app_conf->power_conf->data->pout; + rc = os_mbuf_append(ctxt->om, &powerout, sizeof(powerout)); + } + break; + case BLE_GATT_ACCESS_OP_READ_DSC: + { + uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid); + switch(uuid){ + case CHAR_PRES_FORMAT: + rc = os_mbuf_append(ctxt->om, &voltage_char_pres_format, 7); + break; + } + } + break; + } + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} +static int gatt_svr_chr_access_power_format(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ + int rc = 0; + + if(ctxt->op != BLE_GATT_ACCESS_OP_READ_DSC){ + ESP_LOGE(NIMBLE_LOG_TAG, "wrong callback called"); + } + + ESP_LOGI(NIMBLE_LOG_TAG, "batt voltage pres access event"); + + uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid); + switch(uuid){ + case CHAR_PRES_FORMAT: + rc = os_mbuf_append(ctxt->om, &power_char_pres_format, 7); + break; + } + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + static void ble_advertise(void){ main_app_conf->sensor->enabled = false; ESP_ERROR_CHECK(ble_gap_adv_set_fields(&adv_fields)); diff --git a/components/BTlib/include/BTlib_nimble.h b/components/BTlib/include/BTlib_nimble.h index 829659e..0b22f60 100644 --- a/components/BTlib/include/BTlib_nimble.h +++ b/components/BTlib/include/BTlib_nimble.h @@ -18,12 +18,27 @@ #define PPM_UNIT_UUID 0x27C4 #define PERCENT_UNIT_UUID 0x27AD +#define VOLTAGE_UNIT_UUID 0x2728 +#define POWER_UNIT_UUID 0x2726 +#define TESLA_UNIT_UUID 0x272D #define NSP_DESC_MAIN 0x0106 +#define NSP_DESC_INSIDE 0x010B +#define NSP_DESC_OUTSIDE 0x010C #define BATT_SVC_UUID 0x180F #define BATT_LEVEL_UUID 0x2A19 +#define POWER_SVC_UUID 0x6BBE15A6 +#define BATT_VOLTAGE_UUID 0x5EF9 +#define ALIM_POWER_IN_UUID 0x71C5 +#define ALIM_POWER_OUT_UUID 0x71C6 + +#define MAGFIELD_SVC_UUID 0x92F2710C +#define MAGFIELD_X_UUID 0x3B91 +#define MAGFIELD_Y_UUID 0x23E8 +#define MAGFIELD_Z_UUID 0x7F84 + #define DEFAULT_NAME "CO2_ble" #define DEFAULT_NAME_LEN 7 @@ -111,6 +126,8 @@ enum { enum{ ES_SVC_IDX, BATT_SVC_IDX, + POWER_SVC_IDX, + MAGFIELD_SVC_IDX, }; enum{ @@ -148,6 +165,20 @@ struct batt_level_char_descr { }; +enum{ + POWER_CHAR_VOLTAGE, + POWER_CHAR_PIN, + POWER_CHAR_POUT, + POWER_CHAR_NB, +}; + +enum{ + MAGFIELD_CHAR_X, + MAGFIELD_CHAR_Y, + MAGFIELD_CHAR_Z, + MAGFIELD_CHAR_NB, +}; + void initBle(configuration_data_t* main_conf); void ble_sensor_notify(); #endif diff --git a/components/MMC56x3/MMC56x3.c b/components/MMC56x3/MMC56x3.c index 7dddec9..0d8e5ac 100644 --- a/components/MMC56x3/MMC56x3.c +++ b/components/MMC56x3/MMC56x3.c @@ -89,4 +89,10 @@ esp_err_t MMC56x3_to_mG(int32_t x, int32_t y, int32_t z, float* rx, float* ry, f *ry = y*0.0625; *rz = z*0.0625; return ESP_OK; +} +esp_err_t MMC56x3_to_pT(int32_t x, int32_t y, int32_t z, int32_t* rx, int32_t* ry, int32_t* rz){ + *rx = x*6250; + *ry = y*6250; + *rz = z*6250; + return ESP_OK; } \ No newline at end of file diff --git a/components/MMC56x3/include/MMC56x3.h b/components/MMC56x3/include/MMC56x3.h index f85dad3..47227ac 100644 --- a/components/MMC56x3/include/MMC56x3.h +++ b/components/MMC56x3/include/MMC56x3.h @@ -11,4 +11,5 @@ esp_err_t MMC56x3_reset(); esp_err_t MMC56x3_get_bridge_offset(int32_t* x, int32_t* y, int32_t* z); -esp_err_t MMC56x3_to_mG(int32_t x, int32_t y, int32_t z, float* rx, float* ry, float* rz); \ No newline at end of file +esp_err_t MMC56x3_to_mG(int32_t x, int32_t y, int32_t z, float* rx, float* ry, float* rz); +esp_err_t MMC56x3_to_pT(int32_t x, int32_t y, int32_t z, int32_t* rx, int32_t* ry, int32_t* rz); diff --git a/components/battery/battery.c b/components/battery/battery.c index 2c230f8..d971f77 100644 --- a/components/battery/battery.c +++ b/components/battery/battery.c @@ -49,6 +49,7 @@ void update_battery_level(battery_conf_t* batt_conf){ int val_mv=0; ESP_ERROR_CHECK(adc_cali_raw_to_voltage(batt_conf->cali_handle, val_raw, &val_mv)); val_mv *= 2; // external voltage divider + data->battery_mv = val_mv; if(val_mv < data->min_mv) data->battery_percent = 0; else data->battery_percent = (val_mv - data->min_mv) / data->scale; ESP_LOGI("BATTMS", "raw : %d, mv : %d, percent : %d, min %d, scale %d", val_raw, val_mv, data->battery_percent, data->min_mv, data->scale); diff --git a/components/battery/include/battery.h b/components/battery/include/battery.h index 486c357..550b725 100644 --- a/components/battery/include/battery.h +++ b/components/battery/include/battery.h @@ -7,6 +7,7 @@ struct battery_data { uint8_t battery_percent; + uint16_t battery_mv; uint16_t min_mv; // 0% in mV uint16_t scale; // scaling between mV reading and percent }; diff --git a/components/configuration/CMakeLists.txt b/components/configuration/CMakeLists.txt index fadc165..706335e 100644 --- a/components/configuration/CMakeLists.txt +++ b/components/configuration/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "configuration.c" INCLUDE_DIRS "include" - REQUIRES sensirion_i2c_scd4x APlib ledController battery nvs_flash) + REQUIRES sensirion_i2c_scd4x APlib ledController battery power_inout nvs_flash) diff --git a/components/configuration/configuration.c b/components/configuration/configuration.c index 4af7571..d55a636 100644 --- a/components/configuration/configuration.c +++ b/components/configuration/configuration.c @@ -3,6 +3,8 @@ #include #include #include "configuration.h" +#include "nvs.h" +#include "power_inout.h" #include "scd4x_data.h" #include "nvs_flash.h" #include "sdkconfig.h" @@ -12,6 +14,9 @@ void init_conf_from_nvs(configuration_data_t* conf){ nvs_handle_t nvs; ESP_ERROR_CHECK(nvs_open("battms", NVS_READONLY, &nvs)); conf->battery_conf = get_battery_configuration(nvs); + + ESP_ERROR_CHECK(nvs_open("power", NVS_READONLY, &nvs)); + conf->power_conf = get_power_configuration(nvs); ESP_ERROR_CHECK(nvs_open("leds", NVS_READONLY, &nvs)); conf->leds = get_led_configuration(nvs); diff --git a/components/configuration/include/configuration.h b/components/configuration/include/configuration.h index d24d629..9328da9 100644 --- a/components/configuration/include/configuration.h +++ b/components/configuration/include/configuration.h @@ -8,6 +8,7 @@ #include "APlib.h" #include "ledController.h" #include "battery.h" +#include "power_inout.h" struct configuration_data { scd4x_data_t* measure; @@ -18,6 +19,7 @@ struct configuration_data { wifi_config_t* wifi_config; char hostname[16]; battery_conf_t* battery_conf; + power_conf_t* power_conf; }; typedef struct configuration_data configuration_data_t; diff --git a/default_conf.csv b/default_conf.csv index f225f81..24f617b 100644 --- a/default_conf.csv +++ b/default_conf.csv @@ -15,5 +15,7 @@ led3_conf,data,hex2bin,06000000BB02640001FF # battms,namespace,, poll_delay,data,u16,10000 -min_mv,data,u16,3700 -max_mv,data,u16,4200 \ No newline at end of file +min_mv,data,u16,3300 +max_mv,data,u16,4200 +# +power,namespace,, \ No newline at end of file diff --git a/main/CO2_Sense.c b/main/CO2_Sense.c index 6b84c9d..036dec3 100644 --- a/main/CO2_Sense.c +++ b/main/CO2_Sense.c @@ -45,7 +45,7 @@ void app_main(void){ .min_freq_mhz = 40, .light_sleep_enable = true, // enable light sleep }; - ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); + //ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); configuration_data_t* conf = malloc(sizeof(configuration_data_t)); init_conf_from_nvs(conf); diff --git a/nvs.bin b/nvs.bin index fa9406103f54be0e34dc7b383ed2b4913bcd7462..265920ca5e5cd40287b922a97ef4f92f8d7762f4 100644 GIT binary patch delta 51 zcmZoTz}Rqrae^A-zsZiwk_#P}B^ekQ|DOoCZC{XIo?66!04$pwnV0h{5(oeQsZ|a+ delta 23 fcmZoTz}Rqrae~_9L}sal4$Pa?1rG2m5(oeQW{n5t