serial_io_tcp.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright 2018-2023 Espressif Systems (Shanghai) CO LTD
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "esp_loader_io.h"
  16. #include "serial_io_mock.h"
  17. #include <sys/socket.h>
  18. #include <arpa/inet.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <chrono>
  22. #include <thread>
  23. #include <iostream>
  24. #include <fstream>
  25. using namespace std;
  26. const uint32_t PORT = 5555;
  27. static int sock = 0;
  28. ofstream file;
  29. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  30. static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
  31. {
  32. static bool write_prev = false;
  33. if (write_prev != write) {
  34. write_prev = write;
  35. cout << endl << "--- " << (write ? "WRITE" : "READ") << " ---" << endl;
  36. }
  37. for (uint32_t i = 0; i < size; i++) {
  38. cout << hex << static_cast<unsigned int>(data[i]) << dec << ' ';
  39. }
  40. }
  41. #endif
  42. esp_loader_error_t loader_port_mock_init(const loader_serial_config_t *config)
  43. {
  44. struct sockaddr_in serv_addr;
  45. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  46. cout << "Socket creation error \n";
  47. return ESP_LOADER_ERROR_FAIL;
  48. }
  49. serv_addr.sin_family = AF_INET;
  50. serv_addr.sin_port = htons(PORT);
  51. // Convert IPv4 and IPv6 addresses from text to binary form
  52. if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
  53. cout << "Invalid address/ Address not supported \n";
  54. return ESP_LOADER_ERROR_FAIL;
  55. }
  56. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  57. cout << "Connection Failed \n";
  58. return ESP_LOADER_ERROR_FAIL;
  59. }
  60. file.open ("received_data.bin", ios::binary | ios::out);
  61. if (!file.is_open()) {
  62. cout << "Cannot open file\n";
  63. return ESP_LOADER_ERROR_FAIL;
  64. }
  65. return ESP_LOADER_SUCCESS;
  66. }
  67. void loader_port_mock_deinit()
  68. {
  69. if (sock != 0) {
  70. shutdown(sock, 0);
  71. close(sock);
  72. }
  73. if (file.is_open()) {
  74. file.close();
  75. }
  76. }
  77. esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  78. {
  79. uint32_t written = 0;
  80. do {
  81. int bytes_written = send(sock, &data[written], size - written, 0);
  82. if (bytes_written == -1) {
  83. cout << "Socket send failed\n";
  84. return ESP_LOADER_ERROR_FAIL;
  85. }
  86. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  87. transfer_debug_print(data, bytes_written, true);
  88. #endif
  89. written += bytes_written;
  90. } while (written != size);
  91. return ESP_LOADER_SUCCESS;
  92. }
  93. esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
  94. {
  95. uint32_t written = 0;
  96. int bytes_read = 0;
  97. do {
  98. bytes_read = read(sock, &data[written], size - written);
  99. if (bytes_read == 0) {
  100. cout << "Socket connection lost\n";
  101. return ESP_LOADER_ERROR_FAIL;
  102. }
  103. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  104. transfer_debug_print(data, bytes_read, false);
  105. #endif
  106. file.write((const char*)&data[written], bytes_read);
  107. file.flush();
  108. written += bytes_read;
  109. } while (written != size);
  110. return ESP_LOADER_SUCCESS;
  111. }
  112. void loader_port_enter_bootloader()
  113. {
  114. // GPIO0 and GPIO2 must be LOW
  115. // Then Reset
  116. }
  117. void loader_port_reset_target()
  118. {
  119. // Toggle reset pin
  120. }
  121. void loader_port_delay_ms(uint32_t ms)
  122. {
  123. this_thread::sleep_for(chrono::milliseconds(ms));
  124. }
  125. void loader_port_start_timer(uint32_t ms)
  126. {
  127. (void)ms;
  128. }
  129. uint32_t loader_port_remaining_time(void)
  130. {
  131. return 1;
  132. }