2020-04-10 12:15:00 -07:00
|
|
|
#include "BluetoothUtil.h"
|
|
|
|
|
#include "MeshBluetoothService.h"
|
|
|
|
|
#include "PowerFSM.h"
|
|
|
|
|
#include "configuration.h"
|
2020-06-28 11:12:12 -07:00
|
|
|
#include "esp_task_wdt.h"
|
2020-04-10 12:15:00 -07:00
|
|
|
#include "main.h"
|
2020-06-15 07:04:03 -07:00
|
|
|
#include "sleep.h"
|
2020-04-10 12:15:00 -07:00
|
|
|
#include "target_specific.h"
|
2020-06-28 11:12:12 -07:00
|
|
|
#include "utils.h"
|
2020-07-18 08:49:42 -07:00
|
|
|
#include <nvs.h>
|
|
|
|
|
#include <nvs_flash.h>
|
2020-04-10 12:15:00 -07:00
|
|
|
|
|
|
|
|
bool bluetoothOn;
|
|
|
|
|
|
|
|
|
|
// Enable/disable bluetooth.
|
|
|
|
|
void setBluetoothEnable(bool on)
|
|
|
|
|
{
|
|
|
|
|
if (on != bluetoothOn) {
|
|
|
|
|
DEBUG_MSG("Setting bluetooth enable=%d\n", on);
|
|
|
|
|
|
|
|
|
|
bluetoothOn = on;
|
|
|
|
|
if (on) {
|
|
|
|
|
Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap());
|
|
|
|
|
// ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
|
|
|
|
|
reinitBluetooth();
|
|
|
|
|
} else {
|
|
|
|
|
// We have to totally teardown our bluetooth objects to prevent leaks
|
|
|
|
|
stopMeshBluetoothService(); // Must do before shutting down bluetooth
|
|
|
|
|
deinitBLE();
|
|
|
|
|
destroyMeshBluetoothService(); // must do after deinit, because it frees our service
|
|
|
|
|
Serial.printf("Shutdown BT: %u heap size\n", ESP.getFreeHeap());
|
|
|
|
|
// ESP_ERROR_CHECK( heap_trace_stop() );
|
|
|
|
|
// heap_trace_dump();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 16:55:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getMacAddr(uint8_t *dmac)
|
|
|
|
|
{
|
|
|
|
|
assert(esp_efuse_mac_get_default(dmac) == ESP_OK);
|
2020-04-24 08:52:49 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-14 15:30:42 -07:00
|
|
|
/*
|
|
|
|
|
static void printBLEinfo() {
|
|
|
|
|
int dev_num = esp_ble_get_bond_device_num();
|
|
|
|
|
|
|
|
|
|
esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num);
|
|
|
|
|
esp_ble_get_bond_device_list(&dev_num, dev_list);
|
|
|
|
|
for (int i = 0; i < dev_num; i++) {
|
|
|
|
|
// esp_ble_remove_bond_device(dev_list[i].bd_addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} */
|
|
|
|
|
|
2020-04-24 08:52:49 -07:00
|
|
|
void esp32Setup()
|
|
|
|
|
{
|
2020-06-03 14:24:34 -07:00
|
|
|
uint32_t seed = esp_random();
|
|
|
|
|
DEBUG_MSG("Setting random seed %u\n", seed);
|
|
|
|
|
randomSeed(seed); // ESP docs say this is fairly random
|
2020-05-01 08:51:53 -07:00
|
|
|
|
2020-06-13 08:28:01 -07:00
|
|
|
DEBUG_MSG("Total heap: %d\n", ESP.getHeapSize());
|
|
|
|
|
DEBUG_MSG("Free heap: %d\n", ESP.getFreeHeap());
|
|
|
|
|
DEBUG_MSG("Total PSRAM: %d\n", ESP.getPsramSize());
|
|
|
|
|
DEBUG_MSG("Free PSRAM: %d\n", ESP.getFreePsram());
|
|
|
|
|
|
2020-07-18 08:49:42 -07:00
|
|
|
nvs_stats_t nvs_stats;
|
|
|
|
|
auto res = nvs_get_stats(NULL, &nvs_stats);
|
|
|
|
|
assert(res == ESP_OK);
|
|
|
|
|
DEBUG_MSG("NVS: UsedEntries %d, FreeEntries %d, AllEntries %d\n", nvs_stats.used_entries, nvs_stats.free_entries,
|
|
|
|
|
nvs_stats.total_entries);
|
|
|
|
|
|
2020-06-15 07:04:03 -07:00
|
|
|
// enableModemSleep();
|
|
|
|
|
|
2020-06-28 11:12:12 -07:00
|
|
|
// Since we are turning on watchdogs rather late in the release schedule, we really don't want to catch any
|
|
|
|
|
// false positives. The wait-to-sleep timeout for shutting down radios is 30 secs, so pick 45 for now.
|
|
|
|
|
#define APP_WATCHDOG_SECS 45
|
|
|
|
|
|
2020-07-18 08:49:42 -07:00
|
|
|
res = esp_task_wdt_init(APP_WATCHDOG_SECS, true);
|
2020-06-28 11:12:12 -07:00
|
|
|
assert(res == ESP_OK);
|
|
|
|
|
|
|
|
|
|
res = esp_task_wdt_add(NULL);
|
|
|
|
|
assert(res == ESP_OK);
|
2020-04-24 08:52:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
// Turn off for now
|
|
|
|
|
|
|
|
|
|
uint32_t axpDebugRead()
|
|
|
|
|
{
|
|
|
|
|
axp.debugCharging();
|
|
|
|
|
DEBUG_MSG("vbus current %f\n", axp.getVbusCurrent());
|
|
|
|
|
DEBUG_MSG("charge current %f\n", axp.getBattChargeCurrent());
|
|
|
|
|
DEBUG_MSG("bat voltage %f\n", axp.getBattVoltage());
|
|
|
|
|
DEBUG_MSG("batt pct %d\n", axp.getBattPercentage());
|
|
|
|
|
DEBUG_MSG("is battery connected %d\n", axp.isBatteryConnect());
|
|
|
|
|
DEBUG_MSG("is USB connected %d\n", axp.isVBUSPlug());
|
|
|
|
|
DEBUG_MSG("is charging %d\n", axp.isChargeing());
|
|
|
|
|
|
|
|
|
|
return 30 * 1000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Periodic axpDebugOutput(axpDebugRead);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// loop code specific to ESP32 targets
|
|
|
|
|
void esp32Loop()
|
|
|
|
|
{
|
2020-06-28 11:12:12 -07:00
|
|
|
esp_task_wdt_reset(); // service our app level watchdog
|
2020-04-24 08:52:49 -07:00
|
|
|
loopBLE();
|
|
|
|
|
|
|
|
|
|
// for debug printing
|
|
|
|
|
// radio.radioIf.canSleep();
|
2020-04-10 12:15:00 -07:00
|
|
|
}
|