cmd_ota.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Console example — various system commands
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "cmd_ota.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include "esp_log.h"
  12. #include "esp_console.h"
  13. #include "esp_system.h"
  14. #include "esp_sleep.h"
  15. #include "esp_spi_flash.h"
  16. #include "driver/rtc_io.h"
  17. #include "driver/uart.h"
  18. #include "argtable3/argtable3.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "soc/rtc_cntl_reg.h"
  22. #include "esp32/rom/uart.h"
  23. #include "sdkconfig.h"
  24. #include "platform_console.h"
  25. #include "messaging.h"
  26. static const char * TAG = "ota";
  27. extern esp_err_t start_ota(const char * bin_url);
  28. static struct {
  29. struct arg_str *url;
  30. struct arg_end *end;
  31. } ota_args;
  32. /* 'heap' command prints minumum heap size */
  33. static int perform_ota_update(int argc, char **argv)
  34. {
  35. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&ota_args);
  36. if (nerrors != 0) {
  37. return 1;
  38. }
  39. const char *url = ota_args.url->sval[0];
  40. esp_err_t err=ESP_OK;
  41. ESP_LOGI(TAG, "Starting ota: %s", url);
  42. start_ota(url);
  43. if (err != ESP_OK) {
  44. ESP_LOGE(TAG, "%s", esp_err_to_name(err));
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. void register_ota_cmd()
  50. {
  51. ota_args.url= arg_str1(NULL, NULL, "<url>", "url of the binary app file");
  52. ota_args.end = arg_end(2);
  53. const esp_console_cmd_t cmd = {
  54. .command = "update",
  55. .help = "Updates the application binary from the provided URL",
  56. .hint = NULL,
  57. .func = &perform_ota_update,
  58. .argtable = &ota_args
  59. };
  60. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  61. }