---
# System prepended metadata

title: task_1

---

# Task1: To underdtand OTA firmware updater codes

- The **OTA update mechanism** allows a device to update itself based on data received while the normal firmware is running (for example, over Wi-Fi or Bluetooth.)
- OTA requires configuring the Partition Tables with two OTA app slot partitions(ota_0 and ota_1) and an OTA Data patition.
- The OTA operation functions write a new app firmware image to whichever OTA app slot that is currently not selected for booting. After image verification, the OTA data partition is updated to specify that this image should be used for the next boot.



## [OTA_UPDATE_FPGA_USING_ESP32](https://github.com/Jamm02/OTA_UPDATE_FPGA_USING_ESP32/tree/master)

### main.c
This code performs following steps:
1. **Initialize NVS Flash:** This initiazes Non-Volatile Storage(NVS) flash, which is used for storing the configuration data.
2. **Initialize ESP-NETIF:** This initializes the ESP-IDF's networking abstration layer. It is use for configuring and managing network interfaces.
3. **Initialize Event Loop:** This creates the defauilt event loop for handling events in the ESP-IDF frameeork. This event loop is use for managing asychronous events in the system.
4. **Connect to WIfi:**
5. **Initialize File Storage (SPIFFS):**
6. **starts the file server:**

####  Imp terms/functions in main.c code


- **SPIFFS(SPI Flash File System- is a file system designed for flash memory):** SPIFFS provides a file system that can be used to store firmware images and related files. When performing an OTA update, the new firmware image can be uploaded to SPIFFS before initiating the update process.

#### static esp_err_t init_spiffs(void)

- This function **initialised the SPIFFS**. 
- Configuartion: It configures with the base path, maximum number of files, and the option to format the partition if mounting fails.
- Registration: Use the esp_vfs_spiffs_register function to register SPIFFS with the provided configuration. This function initializes SPIFFS based on the specified settings. esp_err_t ret result of the registration process saved in this. 
- SPIFFS Partition info: after registration it retrives the spiffs partition information,  such as the total size and the amount of space used.


### fileserver.c
main function of this code:
#### esp_err_t start_file_server(const char *base_path)

Following steps are performed in this function:

1. Static Variable Declaration: Declare static variable to store information related to file server. (Initially null)
2. Validates the specified file storage base path.
3. Allocate memory for server data using calloc.
4. Copy base path to server data.
5. HTTP server configuartion. 
6. URI matching configuration
7. Start HTTP server.
8. URI handler registartion: to download file, upload file, flashing files, to delete files.
9. Return success.

This function initializes and starts an HTTP file server. It performs base path validation, checks server is already started. allocated memory for server data, configures the http server. And registers URI handlers for file download, upload, flash, and delete operations.

### flasher.c
main function of this code:

#### esp_err_t flash(const char *file_name)
This function takes file_name parameter(file to be falsed), and opens this file in read mode, constructs its full path, and prints each chunk to the console, and then closes the file. And it also returns the error msg(ESP FAIL or ESP OK).
 
- Reading in Chunks: If the file opening is successful (flash_file is not NULL), the function enters a loop using fgets. Within each iteration of the loop, it reads up to 128 bytes (or until a newline character is encountered) from the file which is stored into the chunk array. This array or buffer has a size of 128 bytes, allowing it to hoild the data read from each portion of the file.


## [esp32-softap-ota](https://github.com/Jeija/esp32-softap-ota/tree/master)

working code for an **over-the-air (OTA) firmware updater** where the ESP32 receives the update while acting as an HTTP server in SoftAP mode.

### main.c

code working flow:
1. Declare HTML Content as Binary Data as external variable. The content of this varible is generated and include during the build process.
2. Define HTTP handlers for root path and update path.
3. HTTP sever URI configuration
4. HTTP server initialization
5. Wifi configuration
6. Main function: 
                  - Initializes NVS flash storage.
                  - Initializes the soft access point(Wifi configuration).
                  - Initializes yhe HTTP server.
                  - Marks the current app partition as valid and cancels any pending block
                  - Enterns infinite loopto keep application running.

Imp function of this code:
#### esp_err_t update_post_handler(httpd_req_t *req)--(flashing the firmware into the ESP32)

1. **Initializations**
2. **OTA Partition:** Obtains the next available OTA partition. Begins the OTA update process on that partition.
3. **Receive and Write Firmware Data in Chunks:** Enterns in the loop until all firmware data is received. Uses `httpd_req_recv` to receive data from the HTTP request in chunks.
4. **Write Received Firmware Chunk to the OTA Partition.**
5. **Validate and Switch to New OTA image:** Ends the OTA update process. Validates the new OTA image and sets it as the boot partition.
6. **Reboot.**