esp_err_t esp_event_send(system_event_t *event) { if (s_event_queue == NULL) { ESP_LOGE(TAG, "Event loop not initialized via esp_event_loop_init, but esp_event_send called"); return ESP_ERR_INVALID_STATE; } if (event->event_id == SYSTEM_EVENT_STA_GOT_IP || event->event_id == SYSTEM_EVENT_STA_LOST_IP) { if (g_mesh_event_cb) { mesh_event_t mevent; if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { mevent.id = MESH_EVENT_ROOT_GOT_IP; memcpy(&mevent.info.got_ip, &event->event_info.got_ip, sizeof(system_event_sta_got_ip_t)); } else { mevent.id = MESH_EVENT_ROOT_LOST_IP; } g_mesh_event_cb(mevent); } } portBASE_TYPE ret = xQueueSendToBack(s_event_queue, event, 0); if (ret != pdPASS) { if (event) { ESP_LOGE(TAG, "e=%d f", event->event_id); } else { ESP_LOGE(TAG, "e null"); } return ESP_FAIL; } return ESP_OK; }
static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions) { esp_err_t err; /* Check for plaintext partition table */ err = bootloader_flash_read(ESP_PARTITION_TABLE_OFFSET, partition_table, ESP_PARTITION_TABLE_MAX_LEN, false); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to read partition table data"); return err; } if (esp_partition_table_verify(partition_table, false, num_partitions) == ESP_OK) { ESP_LOGD(TAG, "partition table is plaintext. Encrypting..."); esp_err_t err = esp_flash_encrypt_region(ESP_PARTITION_TABLE_OFFSET, FLASH_SECTOR_SIZE); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to encrypt partition table in place. %x", err); return err; } } else { ESP_LOGE(TAG, "Failed to read partition table data - not plaintext?"); return ESP_ERR_INVALID_STATE; } /* Valid partition table loded */ return ESP_OK; }
esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length) { esp_err_t err; uint32_t buf[FLASH_SECTOR_SIZE / sizeof(uint32_t)]; if (src_addr % FLASH_SECTOR_SIZE != 0) { ESP_LOGE(TAG, "esp_flash_encrypt_region bad src_addr 0x%x",src_addr); return ESP_FAIL; } for (size_t i = 0; i < data_length; i += FLASH_SECTOR_SIZE) { rtc_wdt_feed(); uint32_t sec_start = i + src_addr; err = bootloader_flash_read(sec_start, buf, FLASH_SECTOR_SIZE, false); if (err != ESP_OK) { goto flash_failed; } err = bootloader_flash_erase_sector(sec_start / FLASH_SECTOR_SIZE); if (err != ESP_OK) { goto flash_failed; } err = bootloader_flash_write(sec_start, buf, FLASH_SECTOR_SIZE, true); if (err != ESP_OK) { goto flash_failed; } } return ESP_OK; flash_failed: ESP_LOGE(TAG, "flash operation failed: 0x%x", err); return err; }
static esp_err_t encrypt_bootloader() { esp_err_t err; uint32_t image_length; /* Check for plaintext bootloader (verification will fail if it's already encrypted) */ if (esp_image_verify_bootloader(&image_length) == ESP_OK) { ESP_LOGD(TAG, "bootloader is plaintext. Encrypting..."); err = esp_flash_encrypt_region(ESP_BOOTLOADER_OFFSET, image_length); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to encrypt bootloader in place: 0x%x", err); return err; } if (esp_secure_boot_enabled()) { /* If secure boot is enabled and bootloader was plaintext, also need to encrypt secure boot IV+digest. */ ESP_LOGD(TAG, "Encrypting secure bootloader IV & digest..."); err = esp_flash_encrypt_region(FLASH_OFFS_SECURE_BOOT_IV_DIGEST, FLASH_SECTOR_SIZE); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to encrypt bootloader IV & digest in place: 0x%x", err); return err; } } } else { ESP_LOGW(TAG, "no valid bootloader was found"); } return ESP_OK; }
esp_err_t app_prov_is_provisioned(bool *provisioned) { *provisioned = false; #ifdef CONFIG_RESET_PROVISIONED nvs_flash_erase(); #endif if (nvs_flash_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to init NVS"); return ESP_FAIL; } wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); if (esp_wifi_init(&cfg) != ESP_OK) { ESP_LOGE(TAG, "Failed to init wifi"); return ESP_FAIL; } /* Get WiFi Station configuration */ wifi_config_t wifi_cfg; if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) { return ESP_FAIL; } if (strlen((const char*) wifi_cfg.sta.ssid)) { *provisioned = true; ESP_LOGI(TAG, "Found ssid %s", (const char*) wifi_cfg.sta.ssid); ESP_LOGI(TAG, "Found password %s", (const char*) wifi_cfg.sta.password); } return ESP_OK; }
scan_list_t *list_new_item(void) { scan_list_t *newItem = (scan_list_t *) malloc(sizeof(scan_list_t)); if (newItem == NULL) { ESP_LOGE(TAG, "malloc list item failed!"); return NULL; } memset(newItem, 0, sizeof(scan_list_t)); newItem->bda = (char *) malloc(BDA_SIZE); if (newItem->bda == NULL) { ESP_LOGE(TAG, "alloc for BDA failed!"); free(newItem); return NULL; } newItem->uuid = (char *)malloc(UUID_SIZE); if (newItem->uuid == NULL) { ESP_LOGE(TAG, "alloc for UUID failed!"); free(newItem->bda); free(newItem); return NULL; } return newItem; }
/** * @brief Initialize the I2C interface. * * @param [in] address The address of the slave device. * @param [in] sdaPin The pin to use for SDA data. * @param [in] sclPin The pin to use for SCL clock. * @return N/A. */ void I2C::init(uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin, uint32_t clockSpeed, i2c_port_t portNum) { ESP_LOGD(LOG_TAG, ">> I2c::init. address=%d, sda=%d, scl=%d, clockSpeed=%d, portNum=%d", address, sdaPin, sclPin, clockSpeed, portNum); assert(portNum < I2C_NUM_MAX); m_portNum = portNum; m_sdaPin = sdaPin; m_sclPin = sclPin; m_address = address; i2c_config_t conf; conf.mode = I2C_MODE_MASTER; conf.sda_io_num = sdaPin; conf.scl_io_num = sclPin; conf.sda_pullup_en = GPIO_PULLUP_ENABLE; conf.scl_pullup_en = GPIO_PULLUP_ENABLE; conf.master.clk_speed = 100000; esp_err_t errRc = ::i2c_param_config(m_portNum, &conf); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "i2c_param_config: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); } if (!driverInstalled) { errRc = ::i2c_driver_install(m_portNum, I2C_MODE_MASTER, 0, 0, 0); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "i2c_driver_install: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); } driverInstalled = true; } } // init
/** * @brief Read the value of the remote characteristic. * @return The value of the remote characteristic. */ std::string BLERemoteCharacteristic::readValue() { ESP_LOGD(LOG_TAG, ">> readValue(): uuid: %s, handle: %d 0x%.2x", getUUID().toString().c_str(), getHandle(), getHandle()); // Check to see that we are connected. if (!getRemoteService()->getClient()->isConnected()) { ESP_LOGE(LOG_TAG, "Disconnected"); throw BLEDisconnectedException(); } m_semaphoreReadCharEvt.take("readValue"); // Ask the BLE subsystem to retrieve the value for the remote hosted characteristic. // This is an asynchronous request which means that we must block waiting for the response // to become available. esp_err_t errRc = ::esp_ble_gattc_read_char( m_pRemoteService->getClient()->getGattcIf(), m_pRemoteService->getClient()->getConnId(), // The connection ID to the BLE server getHandle(), // The handle of this characteristic ESP_GATT_AUTH_REQ_NONE); // Security if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gattc_read_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return ""; } // Block waiting for the event that indicates that the read has completed. When it has, the std::string found // in m_value will contain our data. m_semaphoreReadCharEvt.wait("readValue"); ESP_LOGD(LOG_TAG, "<< readValue(): length: %d", m_value.length()); return m_value; } // readValue
/** * @brief Start scanning. * @param [in] duration The duration in seconds for which to scan. * @return N/A. */ BLEScanResults BLEScan::start(uint32_t duration) { ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration); m_semaphoreScanEnd.take(std::string("start")); m_scanResults.m_vectorAdvertisedDevices.clear(); esp_err_t errRc = ::esp_ble_gap_set_scan_params(&m_scan_params); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc)); m_semaphoreScanEnd.give(); return m_scanResults; } errRc = ::esp_ble_gap_start_scanning(duration); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc)); m_semaphoreScanEnd.give(); return m_scanResults; } m_stopped = false; m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release. ESP_LOGD(LOG_TAG, "<< start()"); return m_scanResults; } // start
/** * @brief Start the service. * Here we wish to start the service which means that we will respond to partner requests about it. * Starting a service also means that we can create the corresponding characteristics. * @return Start the service. */ void BLEService::start() { // We ask the BLE runtime to start the service and then create each of the characteristics. // We start the service through its local handle which was returned in the ESP_GATTS_CREATE_EVT event // obtained as a result of calling esp_ble_gatts_create_service(). // ESP_LOGD(LOG_TAG, ">> start(): Starting service (esp_ble_gatts_start_service): %s", toString().c_str()); if (m_handle == NULL_HANDLE) { ESP_LOGE(LOG_TAG, "<< !!! We attempted to start a service but don't know its handle!"); return; } BLECharacteristic *pCharacteristic = m_characteristicMap.getFirst(); while(pCharacteristic != nullptr) { m_lastCreatedCharacteristic = pCharacteristic; pCharacteristic->executeCreate(this); pCharacteristic = m_characteristicMap.getNext(); } // Start each of the characteristics ... these are found in the m_characteristicMap. m_semaphoreStartEvt.take("start"); esp_err_t errRc = ::esp_ble_gatts_start_service(m_handle); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_start_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return; } m_semaphoreStartEvt.wait("start"); ESP_LOGD(LOG_TAG, "<< start()"); } // start
/* * Selects a boot partition. * The conditions for switching to another firmware are checked. */ static int selected_boot_partition(const bootloader_state_t *bs) { int boot_index = bootloader_utility_get_selected_boot_partition(bs); if (boot_index == INVALID_INDEX) { return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents) } else { // Factory firmware. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) { ESP_LOGI(TAG, "Detect a condition of the factory reset"); bool ota_data_erase = false; #ifdef CONFIG_BOOTLOADER_OTA_DATA_ERASE ota_data_erase = true; #endif const char *list_erase = CONFIG_BOOTLOADER_DATA_FACTORY_RESET; ESP_LOGI(TAG, "Data partitions to erase: %s", list_erase); if (bootloader_common_erase_part_type_data(list_erase, ota_data_erase) == false) { ESP_LOGE(TAG, "Not all partitions were erased"); } return bootloader_utility_get_selected_boot_partition(bs); } #endif // TEST firmware. #ifdef CONFIG_BOOTLOADER_APP_TEST if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) { ESP_LOGI(TAG, "Detect a boot condition of the test firmware"); #ifdef CONFIG_BOOTLOADER_APP_TEST_IN_OTA_1 /* In this case, test bin will locate in ota_1 by default. This is the solution for small Flash. */ return 1; #else if (bs->test.offset != 0) { boot_index = TEST_APP_INDEX; return boot_index; } else { ESP_LOGE(TAG, "Test firmware is not found in partition table"); return INVALID_INDEX; } #endif } #endif #ifdef CONFIG_ESP8266_BOOT_COPY_APP if (boot_index == 1) { ESP_LOGI(TAG, "Copy application from OAT1 to OTA0, please wait ..."); int ret = esp_patition_copy_ota1_to_ota0(bs); if (ret) { ESP_LOGE(TAG, "Fail to initialize OTA0"); return INVALID_INDEX; } boot_index = 0; } #endif // Customer implementation. // if (gpio_pin_1 == true && ...){ // boot_index = required_boot_partition; // } ... } return boot_index; }
const esp_phy_init_data_t* esp_phy_get_init_data() { const esp_partition_t* partition = esp_partition_find_first( ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL); if (partition == NULL) { ESP_LOGE(TAG, "PHY data partition not found"); return NULL; } ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address); size_t init_data_store_length = sizeof(phy_init_magic_pre) + sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post); uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length); if (init_data_store == NULL) { ESP_LOGE(TAG, "failed to allocate memory for PHY init data"); return NULL; } esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length); if (err != ESP_OK) { ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err); return NULL; } if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 || memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post), PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) { ESP_LOGE(TAG, "failed to validate PHY data partition"); return NULL; } ESP_LOGD(TAG, "PHY data partition validated"); return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre)); }
static void example_task(void *p) { example_event_data_t *arg = (example_event_data_t *) p; timer_isr_handle_t inth; ESP_LOGI(TAG, "%p: run task", xTaskGetCurrentTaskHandle()); esp_err_t res = timer_isr_register(arg->group, arg->timer, example_timer_isr, arg, 0, &inth); if (res != ESP_OK) { ESP_LOGE(TAG, "%p: failed to register timer ISR", xTaskGetCurrentTaskHandle()); } else { res = timer_start(arg->group, arg->timer); if (res != ESP_OK) { ESP_LOGE(TAG, "%p: failed to start timer", xTaskGetCurrentTaskHandle()); } } while (1) { uint32_t event_val; SYSVIEW_EXAMPLE_WAIT_EVENT_START(); xTaskNotifyWait(0, 0, &event_val, portMAX_DELAY); SYSVIEW_EXAMPLE_WAIT_EVENT_END(event_val); ESP_LOGI(TAG, "Task[%p]: received event %d", xTaskGetCurrentTaskHandle(), event_val); } }
/** * @brief Write the new value for the characteristic. * @param [in] newValue The new value to write. * @param [in] response Do we expect a response? * @return N/A. */ void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) { ESP_LOGD(LOG_TAG, ">> writeValue(), length: %d", newValue.length()); // Check to see that we are connected. if (!getRemoteService()->getClient()->isConnected()) { ESP_LOGE(LOG_TAG, "Disconnected"); throw BLEDisconnectedException(); } m_semaphoreWriteCharEvt.take("writeValue"); // Invoke the ESP-IDF API to perform the write. esp_err_t errRc = ::esp_ble_gattc_write_char( m_pRemoteService->getClient()->getGattcIf(), m_pRemoteService->getClient()->getConnId(), getHandle(), newValue.length(), (uint8_t*)newValue.data(), response?ESP_GATT_WRITE_TYPE_RSP:ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE ); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gattc_write_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return; } m_semaphoreWriteCharEvt.wait("writeValue"); ESP_LOGD(LOG_TAG, "<< writeValue"); } // writeValue
/** * @brief Execute the creation of the descriptor with the BLE runtime in ESP. * @param [in] pCharacteristic The characteristic to which to register this descriptor. */ void BLEDescriptor::executeCreate(BLECharacteristic* pCharacteristic) { ESP_LOGD(LOG_TAG, ">> executeCreate(): %s", toString().c_str()); if (m_handle != NULL_HANDLE) { ESP_LOGE(LOG_TAG, "Descriptor already has a handle."); return; } m_pCharacteristic = pCharacteristic; // Save the characteristic associated with this service. esp_attr_control_t control; control.auto_rsp = ESP_GATT_RSP_BY_APP; m_semaphoreCreateEvt.take("executeCreate"); esp_err_t errRc = ::esp_ble_gatts_add_char_descr( pCharacteristic->getService()->getHandle(), getUUID().getNative(), (esp_gatt_perm_t)(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE), &m_value, &control); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_add_char_descr: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return; } m_semaphoreCreateEvt.wait("executeCreate"); ESP_LOGD(LOG_TAG, "<< executeCreate"); } // executeCreate
void app_main() { /* Initialize NVS — it is used to store PHY calibration data */ esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK( ret ); ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) { ESP_LOGE(GAP_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret)); return; } if ((ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) { ESP_LOGE(GAP_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret)); return; } if ((ret = esp_bluedroid_init()) != ESP_OK) { ESP_LOGE(GAP_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); return; } if ((ret = esp_bluedroid_enable()) != ESP_OK) { ESP_LOGE(GAP_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); return; } bt_app_gap_start_up(); }
static bool connect_to_http_server() { ESP_LOGI(TAG, "Server IP: %s Server Port:%s", EXAMPLE_SERVER_IP, EXAMPLE_SERVER_PORT); sprintf(http_request, "GET %s HTTP/1.1\r\nHost: %s:%s \r\n\r\n", EXAMPLE_FILENAME, EXAMPLE_SERVER_IP, EXAMPLE_SERVER_PORT); int http_connect_flag = -1; struct sockaddr_in sock_info; socket_id = socket(AF_INET, SOCK_STREAM, 0); if (socket_id == -1) { ESP_LOGE(TAG, "Create socket failed!"); return false; } // set connect info memset(&sock_info, 0, sizeof(struct sockaddr_in)); sock_info.sin_family = AF_INET; sock_info.sin_addr.s_addr = inet_addr(EXAMPLE_SERVER_IP); sock_info.sin_port = htons(atoi(EXAMPLE_SERVER_PORT)); // connect to http server http_connect_flag = connect(socket_id, (struct sockaddr *)&sock_info, sizeof(sock_info)); if (http_connect_flag == -1) { ESP_LOGE(TAG, "Connect to server failed! errno=%d", errno); close(socket_id); return false; } else { ESP_LOGI(TAG, "Connected to server"); return true; } return false; }
static esp_err_t i2c_cmd_link_append(i2c_cmd_handle_t cmd_handle, i2c_cmd_t *cmd) { i2c_cmd_desc_t *cmd_desc = (i2c_cmd_desc_t *) cmd_handle; if (cmd_desc->head == NULL) { cmd_desc->head = (i2c_cmd_link_t *) heap_caps_calloc(1, sizeof(i2c_cmd_link_t), MALLOC_CAP_8BIT); if (cmd_desc->head == NULL) { ESP_LOGE(I2C_TAG, I2C_CMD_MALLOC_ERR_STR); goto err; } cmd_desc->cur = cmd_desc->head; cmd_desc->free = cmd_desc->head; } else { cmd_desc->cur->next = (i2c_cmd_link_t *) heap_caps_calloc(1, sizeof(i2c_cmd_link_t), MALLOC_CAP_8BIT); if (cmd_desc->cur->next == NULL) { ESP_LOGE(I2C_TAG, I2C_CMD_MALLOC_ERR_STR); goto err; } cmd_desc->cur = cmd_desc->cur->next; } memcpy((uint8_t *) &cmd_desc->cur->cmd, (uint8_t *) cmd, sizeof(i2c_cmd_t)); cmd_desc->cur->next = NULL; return ESP_OK; err: return ESP_FAIL; }
void app_main() { vTaskDelay(1000 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(nvs_flash_init_partition("Mynvs")); nvs_handle handle; ESP_ERROR_CHECK(nvs_open_from_partition("Mynvs","store", NVS_READWRITE, &handle)); int32_t val = 0; esp_err_t result = nvs_get_i32(handle, "val", &val); switch (result) { case ESP_ERR_NOT_FOUND: ESP_LOGE(TAG, "Value not set yet"); break; case ESP_OK: ESP_LOGI(TAG, "Value is %d", val); break; default: ESP_LOGE(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(result)); break; } val++; ESP_ERROR_CHECK(nvs_set_i32(handle, "val", val)); ESP_ERROR_CHECK(nvs_commit(handle)); nvs_close(handle); }
static int esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd, const esp_tls_cfg_t *cfg) { int ret = -1; struct addrinfo *res = resolve_host_name(host, hostlen); if (!res) { return ret; } int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (fd < 0) { ESP_LOGE(TAG, "Failed to create socket (family %d socktype %d protocol %d)", res->ai_family, res->ai_socktype, res->ai_protocol); goto err_freeaddr; } *sockfd = fd; void *addr_ptr; if (res->ai_family == AF_INET) { struct sockaddr_in *p = (struct sockaddr_in *)res->ai_addr; p->sin_port = htons(port); addr_ptr = p; } else if (res->ai_family == AF_INET6) { struct sockaddr_in6 *p = (struct sockaddr_in6 *)res->ai_addr; p->sin6_port = htons(port); p->sin6_family = AF_INET6; addr_ptr = p; } else { ESP_LOGE(TAG, "Unsupported protocol family %d", res->ai_family); goto err_freesocket; } if (cfg) { if (cfg->timeout_ms >= 0) { struct timeval tv; ms_to_timeval(cfg->timeout_ms, &tv); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); } if (cfg->non_block) { int flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK); } } ret = connect(fd, addr_ptr, res->ai_addrlen); if (ret < 0 && !(errno == EINPROGRESS && cfg->non_block)) { ESP_LOGE(TAG, "Failed to connnect to host (errno %d)", errno); goto err_freesocket; } freeaddrinfo(res); return 0; err_freesocket: close(fd); err_freeaddr: freeaddrinfo(res); return ret; }
esp_err_t sdmmc_init_mmc_read_ext_csd(sdmmc_card_t* card) { int card_type; esp_err_t err = ESP_OK; uint8_t* ext_csd = heap_caps_malloc(EXT_CSD_MMC_SIZE, MALLOC_CAP_DMA); if (!ext_csd) { ESP_LOGE(TAG, "%s: could not allocate ext_csd", __func__); return ESP_ERR_NO_MEM; } uint32_t sectors = 0; ESP_LOGD(TAG, "MMC version: %d", card->csd.mmc_ver); if (card->csd.mmc_ver < MMC_CSD_MMCVER_4_0) { err = ESP_ERR_NOT_SUPPORTED; goto out; } /* read EXT_CSD */ err = sdmmc_mmc_send_ext_csd_data(card, ext_csd, EXT_CSD_MMC_SIZE); if (err != ESP_OK) { ESP_LOGE(TAG, "%s: send_ext_csd_data error 0x%x", __func__, err); goto out; } card_type = ext_csd[EXT_CSD_CARD_TYPE]; card->is_ddr = 0; if (card_type & EXT_CSD_CARD_TYPE_F_52M_1_8V) { card->max_freq_khz = SDMMC_FREQ_52M; if ((card->host.flags & SDMMC_HOST_FLAG_DDR) && card->host.max_freq_khz >= SDMMC_FREQ_26M && card->host.get_bus_width(card->host.slot) == 4) { ESP_LOGD(TAG, "card and host support DDR mode"); card->is_ddr = 1; } } else if (card_type & EXT_CSD_CARD_TYPE_F_52M) { card->max_freq_khz = SDMMC_FREQ_52M; } else if (card_type & EXT_CSD_CARD_TYPE_F_26M) { card->max_freq_khz = SDMMC_FREQ_26M; } else { ESP_LOGW(TAG, "%s: unknown CARD_TYPE 0x%x", __func__, card_type); } /* For MMC cards, use speed value from EXT_CSD */ card->csd.tr_speed = card->max_freq_khz * 1000; ESP_LOGD(TAG, "MMC card type %d, max_freq_khz=%d, is_ddr=%d", card_type, card->max_freq_khz, card->is_ddr); card->max_freq_khz = MIN(card->max_freq_khz, card->host.max_freq_khz); if (card->host.flags & SDMMC_HOST_FLAG_8BIT) { card->ext_csd.power_class = ext_csd[(card->max_freq_khz > SDMMC_FREQ_26M) ? EXT_CSD_PWR_CL_52_360 : EXT_CSD_PWR_CL_26_360] >> 4; card->log_bus_width = 3; } else if (card->host.flags & SDMMC_HOST_FLAG_4BIT) {
bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase) { const esp_partition_info_t *partitions; const char *marker; esp_err_t err; int num_partitions; bool ret = true; partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN); if (!partitions) { ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN); return false; } ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions); err = esp_partition_table_verify(partitions, true, &num_partitions); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to verify partition table"); ret = false; } else { ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned"); for (int i = 0; i < num_partitions; i++) { const esp_partition_info_t *partition = &partitions[i]; char label[sizeof(partition->label) + 1] = {0}; if (partition->type == PART_TYPE_DATA) { bool fl_ota_data_erase = false; if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) { fl_ota_data_erase = true; } // partition->label is not null-terminated string. strncpy(label, (char *)&partition->label, sizeof(label) - 1); if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) { err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size); if (err != ESP_OK) { ret = false; marker = "err"; } else { marker = "yes"; } } else { marker = "no"; } ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label, partition->pos.offset, partition->pos.size, marker); } } } bootloader_munmap(partitions); return ret; }
/********************************************************************* * * SEGGER_RTT_ESP32_FlushNoLock() * * Function description * Flushes buffered events. * * Parameters * min_sz Threshold for flushing data. If current filling level is above this value, data will be flushed. TRAX destinations only. * tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinetly. * * Return value * None. */ void SEGGER_RTT_ESP32_FlushNoLock(unsigned long min_sz, unsigned long tmo) { esp_err_t res = esp_apptrace_write(ESP_APPTRACE_DEST_TRAX, s_events_buf, s_events_buf_filled, tmo); if (res != ESP_OK) { ESP_LOGE(TAG, "Failed to flush buffered events (%d)!\n", res); } // flush even if we failed to write buffered events, because no new events will be sent after STOP res = esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, min_sz, tmo); if (res != ESP_OK) { ESP_LOGE(TAG, "Failed to flush apptrace data (%d)!\n", res); } s_events_buf_filled = 0; }
void bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_index) { int index = start_index; esp_partition_pos_t part; esp_image_metadata_t image_data; if(start_index == TEST_APP_INDEX) { if (try_load_partition(&bs->test, &image_data)) { load_image(&image_data); } else { ESP_LOGE(TAG, "No bootable test partition in the partition table"); bootloader_reset(); } } /* work backwards from start_index, down to the factory app */ for(index = start_index; index >= FACTORY_INDEX; index--) { part = index_to_partition(bs, index); if (part.size == 0) { continue; } ESP_LOGD(TAG, TRY_LOG_FORMAT, index, part.offset, part.size); if (try_load_partition(&part, &image_data)) { load_image(&image_data); } log_invalid_app_partition(index); } /* failing that work forwards from start_index, try valid OTA slots */ for(index = start_index + 1; index < bs->app_count; index++) { part = index_to_partition(bs, index); if (part.size == 0) { continue; } ESP_LOGD(TAG, TRY_LOG_FORMAT, index, part.offset, part.size); if (try_load_partition(&part, &image_data)) { load_image(&image_data); } log_invalid_app_partition(index); } if (try_load_partition(&bs->test, &image_data)) { ESP_LOGW(TAG, "Falling back to test app as only bootable partition"); load_image(&image_data); } ESP_LOGE(TAG, "No bootable app partitions in the partition table"); bzero(&image_data, sizeof(esp_image_metadata_t)); bootloader_reset(); }
esp_err_t sdcard_mount(const char* base_path) { sdmmc_host_t host = SDMMC_HOST_DEFAULT(); // To use 1-line SD mode, uncomment the following line: host.flags = SDMMC_HOST_FLAG_1BIT; sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); slot_config.gpio_cd = g_gpio; slot_config.width = 1; esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = false, .max_files = SD_CARD_OPEN_FILE_NUM_MAX }; sdmmc_card_t* card; ESP_LOGI(TAG, "Trying to mount with base path=%s", base_path); esp_err_t ret = esp_vfs_fat_sdmmc_mount(base_path, &host, &slot_config, &mount_config, &card); switch (ret) { case ESP_OK: // Card has been initialized, print its properties sdmmc_card_print_info(card); ESP_LOGI(TAG, "CID name %s!\n", card->cid.name); break; case ESP_ERR_INVALID_STATE: ESP_LOGE(TAG, "File system already mounted"); break; case ESP_FAIL: ESP_LOGE(TAG, "Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true."); break; default: ESP_LOGE(TAG, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret); break; } return ret; } esp_err_t sdcard_unmount(void) { esp_err_t ret = esp_vfs_fat_sdmmc_unmount(); if (ret == ESP_ERR_INVALID_STATE) { ESP_LOGE(TAG, "File system not mounted"); } return ret; }
/** * Retrieve the connection info. A rc==0 means ok. */ static int getConnectionInfo(connection_info_t *pConnectionInfo) { nvs_handle handle; size_t size; esp_err_t err; uint32_t version; err = nvs_open(BOOTWIFI_NAMESPACE, NVS_READWRITE, &handle); if (err != 0) { ESP_LOGE(tag, "nvs_open: %x", err); return -1; } // Get the version that the data was saved against. err = nvs_get_u32(handle, KEY_VERSION, &version); if (err != ESP_OK) { ESP_LOGD(tag, "No version record found (%d).", err); nvs_close(handle); return -1; } // Check the versions match if ((version & 0xff00) != (g_version & 0xff00)) { ESP_LOGD(tag, "Incompatible versions ... current is %x, found is %x", version, g_version); nvs_close(handle); return -1; } size = sizeof(connection_info_t); err = nvs_get_blob(handle, KEY_CONNECTION_INFO, pConnectionInfo, &size); if (err != ESP_OK) { ESP_LOGD(tag, "No connection record found (%d).", err); nvs_close(handle); return -1; } if (err != ESP_OK) { ESP_LOGE(tag, "nvs_open: %x", err); nvs_close(handle); return -1; } // Cleanup nvs_close(handle); // Do a sanity check on the SSID if (strlen(pConnectionInfo->ssid) == 0) { ESP_LOGD(tag, "NULL ssid detected"); return -1; } return 0; } // getConnectionInfo
/** * @brief Populate the descriptors (if any) for this characteristic. */ void BLERemoteCharacteristic::retrieveDescriptors() { ESP_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str()); removeDescriptors(); // Remove any existing descriptors. // Loop over each of the descriptors within the service associated with this characteristic. // For each descriptor we find, create a BLERemoteDescriptor instance. uint16_t offset = 0; esp_gattc_descr_elem_t result; while(1) { uint16_t count = 1; esp_gatt_status_t status = ::esp_ble_gattc_get_all_descr( getRemoteService()->getClient()->getGattcIf(), getRemoteService()->getClient()->getConnId(), getHandle(), &result, &count, offset ); if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries. break; } if (status != ESP_GATT_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_all_descr: %s", BLEUtils::gattStatusToString(status).c_str()); break; } if (count == 0) { break; } ESP_LOGE(LOG_TAG, ""); ESP_LOGD(LOG_TAG, "Found a descriptor: Handle: %d, UUID: %s", result.handle, BLEUUID(result.uuid).toString().c_str()); // We now have a new characteristic ... let us add that to our set of known characteristics BLERemoteDescriptor *pNewRemoteDescriptor = new BLERemoteDescriptor( result.handle, BLEUUID(result.uuid), this ); m_descriptorMap.insert(std::pair<std::string, BLERemoteDescriptor*>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor)); offset++; } // while true //m_haveCharacteristics = true; // Remember that we have received the characteristics. ESP_LOGD(LOG_TAG, "<< retrieveDescriptors(): Found %d descriptors.", offset); } // getDescriptors
static void log_invalid_app_partition(int index) { const char *not_bootable = " is not bootable"; /* save a few string literal bytes */ switch(index) { case FACTORY_INDEX: ESP_LOGE(TAG, "Factory app partition%s", not_bootable); break; case TEST_APP_INDEX: ESP_LOGE(TAG, "Factory test app partition%s", not_bootable); break; default: ESP_LOGE(TAG, "OTA app partition slot %d%s", index, not_bootable); break; } }
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle, esp_phy_calibration_data_t* out_cal_data) { esp_err_t err; uint32_t cal_data_version; err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version); if (err != ESP_OK) { ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err); return err; } uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16)); ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version); if (cal_data_version != cal_format_version) { ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d", __func__, cal_format_version, cal_data_version); return ESP_FAIL; } uint8_t cal_data_mac[6]; size_t length = sizeof(cal_data_mac); err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length); if (err != ESP_OK) { ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err); return err; } if (length != sizeof(cal_data_mac)) { ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length); return ESP_ERR_INVALID_SIZE; } uint8_t sta_mac[6]; esp_efuse_mac_get_default(sta_mac); if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) { ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \ MACSTR ", found " MACSTR, __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac)); return ESP_FAIL; } length = sizeof(*out_cal_data); err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length); if (err != ESP_OK) { ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err); return err; } if (length != sizeof(*out_cal_data)) { ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length); return ESP_ERR_INVALID_SIZE; } return ESP_OK; }
/** * Register the VFS at the specified mount point. * The callback functions are registered to handle the * different functions that may be requested against the * VFS. */ void registerTestVFS(char *mountPoint) { esp_vfs_t vfs; esp_err_t err; vfs.fd_offset = 0; vfs.flags = ESP_VFS_FLAG_DEFAULT; vfs.close = vfs_close; vfs.closedir = vfs_closedir; vfs.fstat = vfs_fstat; vfs.link = vfs_link; vfs.lseek = vfs_lseek; vfs.mkdir = vfs_mkdir; vfs.open = vfs_open; vfs.opendir = vfs_opendir; vfs.read = vfs_read; vfs.readdir = vfs_readdir; vfs.rename = vfs_rename; vfs.rmdir = vfs_rmdir; vfs.seekdir = vfs_seekdir; vfs.stat = vfs_stat; vfs.telldir = vfs_telldir; vfs.unlink = vfs_unlink; vfs.write = vfs_write; err = esp_vfs_register(mountPoint, &vfs, NULL); if (err != ESP_OK) { ESP_LOGE(tag, "esp_vfs_register: err=%d", err); } } // End of registerTestVFS