clang-format adcread.cpp, antenna.cpp, blescan.cpp

This commit is contained in:
Klaus K Wilting 2018-06-12 19:44:11 +02:00
parent a47cf1517a
commit 0cbfb1c354
3 changed files with 288 additions and 237 deletions

View File

@ -5,39 +5,42 @@
#include <driver/adc.h>
#include <esp_adc_cal.h>
#define DEFAULT_VREF 1100 // to be done: use adc2_vref_to_gpio() to obtain a better estimate
#define DEFAULT_VREF 1100 // tbd: use adc2_vref_to_gpio() for better estimate
#define NO_OF_SAMPLES 64 // we do multisampling
// Local logging tag
static const char TAG[] = "main";
static void print_char_val_type(esp_adc_cal_value_t val_type)
{
static void print_char_val_type(esp_adc_cal_value_t val_type) {
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
ESP_LOGI(TAG,"ADC characterization based on Two Point values stored in eFuse");
ESP_LOGI(TAG,
"ADC characterization based on Two Point values stored in eFuse");
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
ESP_LOGI(TAG,"ADC characterization based on reference voltage stored in eFuse");
ESP_LOGI(TAG,
"ADC characterization based on reference voltage stored in eFuse");
} else {
ESP_LOGI(TAG,"ADC characterization based on default reference voltage");
ESP_LOGI(TAG, "ADC characterization based on default reference voltage");
}
}
uint16_t read_voltage(void)
{
uint16_t read_voltage(void) {
static const adc1_channel_t channel = HAS_BATTERY_PROBE;
static const adc_atten_t atten = ADC_ATTEN_DB_11;
static const adc_unit_t unit = ADC_UNIT_1;
//configure ADC1
// configure ADC1
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12));
ESP_ERROR_CHECK(adc1_config_channel_atten(channel, atten));
//calibrate ADC1
esp_adc_cal_characteristics_t *adc_chars = (esp_adc_cal_characteristics_t *) calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
// calibrate ADC1
esp_adc_cal_characteristics_t *adc_chars =
(esp_adc_cal_characteristics_t *)calloc(
1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(
unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
print_char_val_type(val_type);
//multisample ADC1
// multisample ADC1
uint32_t adc_reading = 0;
for (int i = 0; i < NO_OF_SAMPLES; i++) {
adc_reading += adc1_get_raw(channel);
@ -45,12 +48,13 @@ uint16_t read_voltage(void)
adc_reading /= NO_OF_SAMPLES;
//Convert adc_reading to voltage in mV
uint16_t voltage = (uint16_t) esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
#ifdef BATT_FACTOR
// Convert adc_reading to voltage in mV
uint16_t voltage =
(uint16_t)esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
#ifdef BATT_FACTOR
voltage *= BATT_FACTOR;
#endif
ESP_LOGI(TAG,"Raw: %d / Voltage: %dmV", adc_reading, voltage);
#endif
ESP_LOGI(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage);
return voltage;
}
#endif // HAS_BATTERY_PROBE

View File

@ -1,4 +1,4 @@
/* switches wifi antenna, if board has switch to select internal and external antenna */
/* switches wifi antenna, if board has switch internal / external antenna */
#ifdef HAS_ANTENNA_SWITCH
@ -7,10 +7,7 @@
// Local logging tag
static const char TAG[] = "wifi";
typedef enum {
ANTENNA_INT = 0,
ANTENNA_EXT
} antenna_type_t;
typedef enum { ANTENNA_INT = 0, ANTENNA_EXT } antenna_type_t;
void antenna_init(void) {
gpio_config_t gpioconf = {.pin_bit_mask = 1ull << HAS_ANTENNA_SWITCH,
@ -21,7 +18,7 @@ void antenna_init(void) {
gpio_config(&gpioconf);
}
void antenna_select (const uint8_t _ant) {
void antenna_select(const uint8_t _ant) {
if (HAS_ANTENNA_SWITCH < 32) {
if (_ant == ANTENNA_EXT) {
GPIO_REG_WRITE(GPIO_OUT_W1TS_REG, 1 << HAS_ANTENNA_SWITCH);

View File

@ -14,7 +14,8 @@ https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner
#include <esp_blufi_api.h> // needed for BLE_ADDR types, do not remove
#include <bt_types.h>
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
#define BT_BD_ADDR_HEX(addr) \
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
// local Tag for logging
static const char TAG[] = "bluetooth";
@ -23,7 +24,7 @@ static const char TAG[] = "bluetooth";
bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type);
const char *bt_addr_t_to_string(esp_ble_addr_type_t type) {
switch(type) {
switch (type) {
case BLE_ADDR_TYPE_PUBLIC:
return "BLE_ADDR_TYPE_PUBLIC";
case BLE_ADDR_TYPE_RANDOM:
@ -38,45 +39,81 @@ const char *bt_addr_t_to_string(esp_ble_addr_type_t type) {
} // bt_addr_t_to_string
const char *btsig_gap_type(uint32_t gap_type) {
switch (gap_type)
{
case 0x01: return "Flags";
case 0x02: return "Incomplete List of 16-bit Service Class UUIDs";
case 0x03: return "Complete List of 16-bit Service Class UUIDs";
case 0x04: return "Incomplete List of 32-bit Service Class UUIDs";
case 0x05: return "Complete List of 32-bit Service Class UUIDs";
case 0x06: return "Incomplete List of 128-bit Service Class UUIDs";
case 0x07: return "Complete List of 128-bit Service Class UUIDs";
case 0x08: return "Shortened Local Name";
case 0x09: return "Complete Local Name";
case 0x0A: return "Tx Power Level";
case 0x0D: return "Class of Device";
case 0x0E: return "Simple Pairing Hash C/C-192";
case 0x0F: return "Simple Pairing Randomizer R/R-192";
case 0x10: return "Device ID/Security Manager TK Value";
case 0x11: return "Security Manager Out of Band Flags";
case 0x12: return "Slave Connection Interval Range";
case 0x14: return "List of 16-bit Service Solicitation UUIDs";
case 0x1F: return "List of 32-bit Service Solicitation UUIDs";
case 0x15: return "List of 128-bit Service Solicitation UUIDs";
case 0x16: return "Service Data - 16-bit UUID";
case 0x20: return "Service Data - 32-bit UUID";
case 0x21: return "Service Data - 128-bit UUID";
case 0x22: return "LE Secure Connections Confirmation Value";
case 0x23: return "LE Secure Connections Random Value";
case 0x24: return "URI";
case 0x25: return "Indoor Positioning";
case 0x26: return "Transport Discovery Data";
case 0x17: return "Public Target Address";
case 0x18: return "Random Target Address";
case 0x19: return "Appearance";
case 0x1A: return "Advertising Interval";
case 0x1B: return "LE Bluetooth Device Address";
case 0x1C: return "LE Role";
case 0x1D: return "Simple Pairing Hash C-256";
case 0x1E: return "Simple Pairing Randomizer R-256";
case 0x3D: return "3D Information Data";
case 0xFF: return "Manufacturer Specific Data";
switch (gap_type) {
case 0x01:
return "Flags";
case 0x02:
return "Incomplete List of 16-bit Service Class UUIDs";
case 0x03:
return "Complete List of 16-bit Service Class UUIDs";
case 0x04:
return "Incomplete List of 32-bit Service Class UUIDs";
case 0x05:
return "Complete List of 32-bit Service Class UUIDs";
case 0x06:
return "Incomplete List of 128-bit Service Class UUIDs";
case 0x07:
return "Complete List of 128-bit Service Class UUIDs";
case 0x08:
return "Shortened Local Name";
case 0x09:
return "Complete Local Name";
case 0x0A:
return "Tx Power Level";
case 0x0D:
return "Class of Device";
case 0x0E:
return "Simple Pairing Hash C/C-192";
case 0x0F:
return "Simple Pairing Randomizer R/R-192";
case 0x10:
return "Device ID/Security Manager TK Value";
case 0x11:
return "Security Manager Out of Band Flags";
case 0x12:
return "Slave Connection Interval Range";
case 0x14:
return "List of 16-bit Service Solicitation UUIDs";
case 0x1F:
return "List of 32-bit Service Solicitation UUIDs";
case 0x15:
return "List of 128-bit Service Solicitation UUIDs";
case 0x16:
return "Service Data - 16-bit UUID";
case 0x20:
return "Service Data - 32-bit UUID";
case 0x21:
return "Service Data - 128-bit UUID";
case 0x22:
return "LE Secure Connections Confirmation Value";
case 0x23:
return "LE Secure Connections Random Value";
case 0x24:
return "URI";
case 0x25:
return "Indoor Positioning";
case 0x26:
return "Transport Discovery Data";
case 0x17:
return "Public Target Address";
case 0x18:
return "Random Target Address";
case 0x19:
return "Appearance";
case 0x1A:
return "Advertising Interval";
case 0x1B:
return "LE Bluetooth Device Address";
case 0x1C:
return "LE Role";
case 0x1D:
return "Simple Pairing Hash C-256";
case 0x1E:
return "Simple Pairing Randomizer R-256";
case 0x3D:
return "3D Information Data";
case 0xFF:
return "Manufacturer Specific Data";
default:
return "Unknown type";
@ -84,14 +121,14 @@ const char *btsig_gap_type(uint32_t gap_type) {
} // btsig_gap_type
// using IRAM_:ATTR here to speed up callback function
IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t *param) {
esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param;
ESP_LOGD(TAG, "BT payload rcvd -> type: 0x%.2x -> %s", *p->scan_rst.ble_adv, btsig_gap_type(*p->scan_rst.ble_adv));
ESP_LOGD(TAG, "BT payload rcvd -> type: 0x%.2x -> %s", *p->scan_rst.ble_adv,
btsig_gap_type(*p->scan_rst.ble_adv));
switch (event)
{
switch (event) {
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
// restart scan
ESP_ERROR_CHECK(esp_ble_gap_start_scanning(BLESCANTIME));
@ -99,49 +136,59 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb
case ESP_GAP_BLE_SCAN_RESULT_EVT:
// evaluate scan results
if ( p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) // Inquiry complete, scan is done
if (p->scan_rst.search_evt ==
ESP_GAP_SEARCH_INQ_CMPL_EVT) // Inquiry complete, scan is done
{ // restart scan
ESP_ERROR_CHECK(esp_ble_gap_start_scanning(BLESCANTIME));
return;
}
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) // Inquiry result for a peer device
if (p->scan_rst.search_evt ==
ESP_GAP_SEARCH_INQ_RES_EVT) // Inquiry result for a peer device
{ // evaluate sniffed packet
ESP_LOGD(TAG, "Device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", BT_BD_ADDR_HEX(p->scan_rst.bda));
ESP_LOGD(TAG, "Addr_type : %s", bt_addr_t_to_string(p->scan_rst.ble_addr_type));
ESP_LOGD(TAG, "Device address (bda): %02x:%02x:%02x:%02x:%02x:%02x",
BT_BD_ADDR_HEX(p->scan_rst.bda));
ESP_LOGD(TAG, "Addr_type : %s",
bt_addr_t_to_string(p->scan_rst.ble_addr_type));
ESP_LOGD(TAG, "RSSI : %d", p->scan_rst.rssi);
if ((cfg.rssilimit) && (p->scan_rst.rssi < cfg.rssilimit )) { // rssi is negative value
ESP_LOGI(TAG, "BLTH RSSI %d -> ignoring (limit: %d)", p->scan_rst.rssi, cfg.rssilimit);
if ((cfg.rssilimit) &&
(p->scan_rst.rssi < cfg.rssilimit)) { // rssi is negative value
ESP_LOGI(TAG, "BLTH RSSI %d -> ignoring (limit: %d)", p->scan_rst.rssi,
cfg.rssilimit);
break;
}
#ifdef VENDORFILTER
#ifdef VENDORFILTER
if ((p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RANDOM) || (p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RPA_RANDOM)) {
if ((p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RANDOM) ||
(p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RPA_RANDOM)) {
ESP_LOGD(TAG, "BT device filtered");
break;
}
#endif
#endif
// add this device and show new count total if it was not previously added
mac_add((uint8_t *) p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE);
mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE);
/* to be improved in vendorfilter if:
// you can search for elements in the payload using the
// function esp_ble_resolve_adv_data()
//
// Like this, that scans for the "Complete name" (looking inside the payload buffer)
// Like this, that scans for the "Complete name" (looking inside the
payload buffer)
// uint8_t len;
// uint8_t *data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, ESP_BLE_AD_TYPE_NAME_CMPL, &len);
// uint8_t *data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv,
ESP_BLE_AD_TYPE_NAME_CMPL, &len);
filter BLE devices using their advertisements to get filter alternative to vendor OUI
if vendorfiltering is on, we ...
filter BLE devices using their advertisements to get filter alternative to
vendor OUI if vendorfiltering is on, we ...
- want to count: mobile phones and tablets
- don't want to count: beacons, peripherals (earphones, headsets, printers), cars and machines
see
- don't want to count: beacons, peripherals (earphones, headsets,
printers), cars and machines see
https://github.com/nkolban/ESP32_BLE_Arduino/blob/master/src/BLEAdvertisedDevice.cpp
http://www.libelium.com/products/meshlium/smartphone-detection/
@ -150,9 +197,10 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb
https://www.bluetooth.com/specifications/assigned-numbers/baseband
"The Class of Device (CoD) in case of Bluetooth which allows us to differentiate the type of
device (smartphone, handsfree, computer, LAN/network AP). With this parameter we can
differentiate among pedestrians and vehicles."
"The Class of Device (CoD) in case of Bluetooth which allows us to
differentiate the type of device (smartphone, handsfree, computer,
LAN/network AP). With this parameter we can differentiate among
pedestrians and vehicles."
*/
@ -164,29 +212,29 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb
}
} // gap_callback_handler
esp_err_t register_ble_callback(void) {
ESP_LOGI(TAG, "Register GAP callback");
// This function is called to occur gap event, such as scan result.
//register the scan callback function to the gap module
// register the scan callback function to the gap module
ESP_ERROR_CHECK(esp_ble_gap_register_callback(&gap_callback_handler));
static esp_ble_scan_params_t ble_scan_params =
{
static esp_ble_scan_params_t ble_scan_params = {
.scan_type = BLE_SCAN_TYPE_PASSIVE,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
#ifdef VENDORFILTER
#ifdef VENDORFILTER
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_WLIST_PRA_DIR,
// ADV_IND, ADV_NONCONN_IND, ADV_SCAN_IND packets are used for broadcasting
// data in broadcast applications (e.g., Beacons), so we don't want them in vendorfilter mode
#else
// data in broadcast applications (e.g., Beacons), so we don't want them in
// vendorfilter mode
#else
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
#endif
#endif
.scan_interval = (uint16_t) (cfg.blescantime * 10 / 0.625), // Time = N * 0.625 msec
.scan_window = (uint16_t) (BLESCANWINDOW / 0.625) // Time = N * 0.625 msec
.scan_interval =
(uint16_t)(cfg.blescantime * 10 / 0.625), // Time = N * 0.625 msec
.scan_window = (uint16_t)(BLESCANWINDOW / 0.625) // Time = N * 0.625 msec
};
ESP_LOGI(TAG, "Set GAP scan parameters");
@ -198,16 +246,18 @@ esp_err_t register_ble_callback(void) {
} // register_ble_callback
void start_BLEscan(void){
void start_BLEscan(void) {
ESP_LOGI(TAG, "Initializing bluetooth scanner ...");
// Initialize BT controller to allocate task and other resource.
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
bt_cfg.controller_task_stack_size = BLESTACKSIZE; // set BT stack size to value configured in paxcounter.conf
bt_cfg.controller_task_stack_size =
BLESTACKSIZE; // set BT stack size to value configured in paxcounter.conf
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BTDM));
// Init and alloc the resource for bluetooth stack, must be done prior to every bluetooth stuff
// Init and alloc the resource for bluetooth stack, must be done prior to
// every bluetooth stuff
ESP_ERROR_CHECK(esp_bluedroid_init());
ESP_ERROR_CHECK(esp_bluedroid_enable());
@ -217,7 +267,7 @@ void start_BLEscan(void){
ESP_LOGI(TAG, "Bluetooth scanner started");
} // start_BLEscan
void stop_BLEscan(void){
void stop_BLEscan(void) {
ESP_LOGI(TAG, "Shutting down bluetooth scanner ...");
ESP_ERROR_CHECK(esp_ble_gap_register_callback(NULL));
ESP_ERROR_CHECK(esp_bluedroid_disable());