moar services

This commit is contained in:
leo 2022-12-20 17:24:27 +01:00
parent 34b34e64d7
commit a3b8ed0a54
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
12 changed files with 297 additions and 9 deletions

View File

@ -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 <stdint.h>
#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));

View File

@ -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

View File

@ -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;
}

View File

@ -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);
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);

View File

@ -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);

View File

@ -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
};

View File

@ -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)

View File

@ -3,6 +3,8 @@
#include <stdlib.h>
#include <string.h>
#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);

View File

@ -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;

View File

@ -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
min_mv,data,u16,3300
max_mv,data,u16,4200
#
power,namespace,,
1 # Default nvs configuration
15 #
16 battms,namespace,,
17 poll_delay,data,u16,10000
18 min_mv,data,u16,3700 min_mv,data,u16,3300
19 max_mv,data,u16,4200
20 #
21 power,namespace,,

View File

@ -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);

BIN
nvs.bin

Binary file not shown.