├── component.mk ├── include └── ota_server.h ├── Makefile.example ├── README.md ├── ota_server.c └── LICENSE /component.mk: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /include/ota_server.h: -------------------------------------------------------------------------------- 1 | #ifndef _OTA_SERVER_H_ 2 | #define _OTA_SERVER_H__ 3 | 4 | #define OTA_LISTEN_PORT 8032 5 | #define OTA_BUFF_SIZE 1024 6 | 7 | void ota_server_start(); 8 | 9 | #endif /* _OTA_SERVER_H_ */ 10 | -------------------------------------------------------------------------------- /Makefile.example: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := app-ota-template 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | ota: app 11 | curl ${ESP32_IP}:8032 --data-binary @- < build/$(PROJECT_NAME).bin 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP-IDF OTA server component 2 | 3 | A component for ESP-IDF that adds OTA capability for your project easily. 4 | 5 | See also: https://github.com/yanbe/esp-idf-ota-template 6 | 7 | ## Usage 8 | 9 | This repository is served as a "component" for ESP-IDF project so you can add OTA capability to your existing project easily. 10 | 11 | ```sh 12 | $ cd path/to/your/esp-idf-project 13 | $ git remote add esp32-ota-server https://github.com/yanbe/esp32-ota-server.git 14 | $ git subtree add --prefix components/ota_server esp32-ota-server master --squash 15 | $ make menuconfig 16 | ``` 17 | 18 | In `make menuconfig`, 19 | 20 | * In Partition Table: select "Factory app, two OTA definitions" 21 | * In Wifi Configuration section: set your Wifi access point's SSID / password 22 | 23 | ### Integrate OTA capability to your project 24 | 25 | You have to add small portion of code to integrate OTA capability to your project. 26 | 27 | OTA server have to started after ESP32 joined to WiFi network. So you have to 28 | implement event handler (if not yet) and then emit `CONNECTED_BIT` event 29 | which `ota_server_task` waits for. 30 | 31 | In your main/main.c, 32 | 33 | ```c 34 | #include "ota_server.h" 35 | 36 | static EventGroupHandle_t wifi_event_group; 37 | 38 | static const int CONNECTED_BIT = BIT0; 39 | 40 | static esp_err_t event_handler(void *ctx, system_event_t *event) 41 | { 42 | switch(event->event_id) { 43 | /* ... */ 44 | case SYSTEM_EVENT_STA_GOT_IP: 45 | xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); 46 | break; 47 | /* ... */ 48 | } 49 | return ESP_OK; 50 | } 51 | 52 | static void initialise_wifi(void) 53 | { 54 | /* snip. wifi initialization code... */ 55 | } 56 | 57 | 58 | static void ota_server_task(void * param) 59 | { 60 | xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); 61 | ota_server_start(); 62 | vTaskDelete(NULL); 63 | } 64 | 65 | 66 | void app_main() { 67 | initialise_wifi(); 68 | xTaskCreate(&ota_server_task, "ota_server_task", 4096, NULL, 5, NULL); 69 | } 70 | 71 | ``` 72 | 73 | ### Inital flashing and OTA 74 | 75 | To enable OTA capability, your have to configure partition table and flash your code via UART once. 76 | 77 | ```sh 78 | $ make erase_flash flash 79 | ``` 80 | 81 | Then, via serial output, confirm which IP address is assigned to a ESP32. It should be something like below. 82 | 83 | ```sh 84 | $ make monitor 85 | ... 86 | I (4086) event: ip: 192.168.10.10, mask: 255.255.255.0, gw: 192.168.10.1 87 | ``` 88 | 89 | After that, you can use flashing capability via special make target. 90 | 91 | ```sh 92 | $ cp components/ota-server/Makefile.example Makefile # For additional make target "ota" 93 | $ make ota ESP32_IP=192.168.10.10 94 | 200 OK 95 | 96 | Success. Next boot partition is ota_0 97 | ``` 98 | 99 | ### Modifing and Updating OTA server component 100 | 101 | As `components/ota_server` is introduced as via `git subtree` command, your can modify `components/ota_server` 102 | and commit it as your project's seamlessly, 103 | 104 | If you want to merge any changes have made to https://github.com/yanbe/esp32-ota-server , 105 | you can merge them into your project by following command. 106 | ``` 107 | $ git pull -s subtree --squash --allow-unrelated-histories esp32-ota-server master 108 | ``` 109 | -------------------------------------------------------------------------------- /ota_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "esp_system.h" 4 | #include "esp_log.h" 5 | #include "esp_ota_ops.h" 6 | 7 | #include "freertos/FreeRTOS.h" 8 | #include "freertos/task.h" 9 | #include "lwip/sockets.h" 10 | 11 | #include "ota_server.h" 12 | 13 | static const char * TAG = "OTA"; 14 | 15 | /*socket*/ 16 | static int connect_socket = 0; 17 | 18 | static int get_socket_error_code(int socket) 19 | { 20 | int result; 21 | u32_t optlen = sizeof(int); 22 | int err = getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen); 23 | if (err == -1) { 24 | ESP_LOGE(TAG, "getsockopt failed:%s", strerror(err)); 25 | return -1; 26 | } 27 | return result; 28 | } 29 | 30 | static int show_socket_error_reason(const char *str, int socket) 31 | { 32 | int err = get_socket_error_code(socket); 33 | 34 | if (err != 0) { 35 | ESP_LOGW(TAG, "%s socket error %d %s", str, err, strerror(err)); 36 | } 37 | 38 | return err; 39 | } 40 | 41 | static esp_err_t create_tcp_server() 42 | { 43 | ESP_LOGI(TAG, "server socket....port=%d", OTA_LISTEN_PORT); 44 | int server_socket = 0; 45 | struct sockaddr_in server_addr; 46 | server_socket = socket(AF_INET, SOCK_STREAM, 0); 47 | if (server_socket < 0) { 48 | show_socket_error_reason("create_server", server_socket); 49 | return ESP_FAIL; 50 | } 51 | 52 | server_addr.sin_family = AF_INET; 53 | server_addr.sin_port = htons(OTA_LISTEN_PORT); 54 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 55 | if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { 56 | show_socket_error_reason("bind_server", server_socket); 57 | close(server_socket); 58 | return ESP_FAIL; 59 | } 60 | if (listen(server_socket, 5) < 0) { 61 | show_socket_error_reason("listen_server", server_socket); 62 | close(server_socket); 63 | return ESP_FAIL; 64 | } 65 | struct sockaddr_in client_addr; 66 | unsigned int socklen = sizeof(client_addr); 67 | connect_socket = accept(server_socket, (struct sockaddr *)&client_addr, &socklen); 68 | if (connect_socket < 0) { 69 | show_socket_error_reason("accept_server", connect_socket); 70 | close(server_socket); 71 | return ESP_FAIL; 72 | } 73 | /*connection established,now can send/recv*/ 74 | ESP_LOGI(TAG, "tcp connection established!"); 75 | return ESP_OK; 76 | } 77 | 78 | void ota_server_start() 79 | { 80 | ESP_ERROR_CHECK( create_tcp_server() ); 81 | 82 | const esp_partition_t *update_partition = esp_ota_get_next_update_partition(NULL); 83 | 84 | ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", 85 | update_partition->subtype, update_partition->address); 86 | 87 | int recv_len; 88 | char ota_buff[OTA_BUFF_SIZE] = {0}; 89 | bool is_req_body_started = false; 90 | int content_length = -1; 91 | int content_received = 0; 92 | 93 | esp_ota_handle_t ota_handle; 94 | do { 95 | recv_len = recv(connect_socket, ota_buff, OTA_BUFF_SIZE, 0); 96 | if (recv_len > 0) { 97 | if (!is_req_body_started) { 98 | const char *content_length_start = "Content-Length: "; 99 | char *content_length_start_p = strstr(ota_buff, content_length_start) + strlen(content_length_start); 100 | sscanf(content_length_start_p, "%d", &content_length); 101 | ESP_LOGI(TAG, "Detected content length: %d", content_length); 102 | ESP_ERROR_CHECK( esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &ota_handle) ); 103 | const char *header_end = "\r\n\r\n"; 104 | char *body_start_p = strstr(ota_buff, header_end) + strlen(header_end); 105 | int body_part_len = recv_len - (body_start_p - ota_buff); 106 | esp_ota_write(ota_handle, body_start_p, body_part_len); 107 | content_received += body_part_len; 108 | is_req_body_started = true; 109 | } else { 110 | esp_ota_write(ota_handle, ota_buff, recv_len); 111 | content_received += recv_len; 112 | } 113 | } 114 | else if (recv_len < 0) { 115 | ESP_LOGE(TAG, "Error: recv data error! errno=%d", errno); 116 | } 117 | } while (recv_len > 0 && content_received < content_length); 118 | 119 | ESP_LOGI(TAG, "Binary transferred finished: %d bytes", content_received); 120 | 121 | ESP_ERROR_CHECK( esp_ota_end(ota_handle) ); 122 | esp_err_t err = esp_ota_set_boot_partition(update_partition); 123 | char res_buff[50]; 124 | int send_len; 125 | if (err == ESP_OK) { 126 | send_len = sprintf(res_buff, "200 OK\n\nSuccess. Next boot partition is %s\n", update_partition->label); 127 | } else { 128 | send_len = sprintf(res_buff, "400 Bad Request\n\nFailure. Error code: 0x%x\n", err); 129 | } 130 | send(connect_socket, res_buff, send_len, 0); 131 | close(connect_socket); 132 | 133 | const esp_partition_t *boot_partition = esp_ota_get_boot_partition(); 134 | ESP_LOGI(TAG, "Next boot partition subtype %d at offset 0x%x", 135 | boot_partition->subtype, boot_partition->address); 136 | ESP_LOGI(TAG, "Prepare to restart system!"); 137 | esp_restart(); 138 | } 139 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------