2023-06-26 17:34:13 +02:00

138 lines
4.1 KiB
C

#include <stdint.h>
#include "esp_log.h"
#include "host/ble_att.h"
#include "host/ble_gatt.h"
#include "host/ble_uuid.h"
#include "sdkconfig.h"
#include "BLE_UUID.h"
#include "BLEh.h"
#include "simple_gatt_value_server.h"
#include "BLE.h"
#define TAG "BLE"
struct char_pres_format frequency_char_pres_format = {
.format = FORMAT_UINT32,
.unit = HERTZ_UNIT_UUID,
.exponent = 0,
.namespc = 1,
.descrH = NSP_DESC_MAIN & 0xff,
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
};
struct dsc_content switching_frequency_dsc = {
.data = &frequency_char_pres_format,
.data_size = CHAR_PRESENTATION_FORMAT_SIZE,
};
struct char_pres_format duty_cycle_char_pres_format = {
.format = FORMAT_UINT32,
.unit = UNITLESS_UNIT_UUID,
.exponent = -6,
.namespc = 1,
.descrH = NSP_DESC_MAIN & 0xff,
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
};
struct dsc_content duty_cycle_dsc = {
.data = &duty_cycle_char_pres_format,
.data_size = CHAR_PRESENTATION_FORMAT_SIZE,
};
struct ble_gatt_svc_def gatt_svcs[] = {
[RAW_VALUES_SVC_ID] = {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(METROLOGY_SERVICE),
},
[CALC_VALUES_SVC_ID] = {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(METROLOGY_SERVICE),
},
[CONFIGURATION_SVC_ID] = {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(CONFIGURATION_SERVICE),
.characteristics = (struct ble_gatt_chr_def[]){
[SWITCHING_FREQUENCY_CHR_ID] = {
.uuid = BLE_UUID16_DECLARE(SWITCHING_FREQUENCY_CHAR),
.access_cb = NULL,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
.min_key_size = 0,
.arg = NULL,
.descriptors = (struct ble_gatt_dsc_def[]){
[0] = {
.uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT),
.att_flags = BLE_ATT_F_READ,
.access_cb = NULL,
.arg = &switching_frequency_dsc,
},
{ 0 },
},
},
[DUTY_CYCLE_CHR_ID] = {
.uuid = BLE_UUID16_DECLARE(DUTY_CYCLE_FREQUENCY_CHAR),
.access_cb = NULL,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
.min_key_size = 0,
.arg = NULL,
.descriptors = (struct ble_gatt_dsc_def[]){
[0] = {
.uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT),
.att_flags = BLE_ATT_F_READ,
.access_cb = NULL,
.arg = &duty_cycle_dsc,
},
{ 0 },
},
},
{ 0 },
},
},
{ 0 },
};
static struct char_pres_format mv_char_pres_format = {
.format = FORMAT_UINT32,
.unit = VOLTS_UNIT_UUID,
.exponent = -3,
.namespc = 1,
.descrH = NSP_DESC_MAIN & 0xff,
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
};
static struct char_pres_format mA_char_pres_format = {
.format = FORMAT_UINT32,
.unit = AMPERE_UNIT_UUID,
.exponent = -3,
.namespc = 1,
.descrH = NSP_DESC_MAIN & 0xff,
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
};
ble_uuid_t* voltage_char_uuid = BLE_UUID16_DECLARE(VOLTAGE_CHAR);
ble_uuid_t* current_char_uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_CHAR);
void init_ble_app_services(uint32_t raw_mv_meas[], uint32_t calc_meas[], uint8_t adc_pins[], gatt_value_server_handle_t* raw_mv_serve_handle, gatt_value_server_handle_t* calc_meas_serve_handle){
*raw_mv_serve_handle = simple_gatt_identical_values_server(raw_mv_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, &mv_char_pres_format, &gatt_svcs[RAW_VALUES_SVC_ID], RAW_VALUES_SVC_ID, voltage_char_uuid);
struct char_pres_format* calc_meas_formats[CONFIG_ADC_IN_PIN_NUM];
ble_uuid_t* calc_meas_char_uuid[CONFIG_ADC_IN_PIN_NUM];
for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){
switch(adc_pins[i]){
case CONFIG_OUT_VOLT_PIN:
calc_meas_formats[i] = &mv_char_pres_format;
calc_meas_char_uuid[i] = voltage_char_uuid;
break;
default:
calc_meas_formats[i] = &mA_char_pres_format;
calc_meas_char_uuid[i] = current_char_uuid;
}
}
*calc_meas_serve_handle = simple_gatt_value_server(calc_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, calc_meas_formats, &gatt_svcs[CALC_VALUES_SVC_ID], CALC_VALUES_SVC_ID, calc_meas_char_uuid);
}