serial_io_tcp.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright 2018 Espressif Systems (Shanghai) PTE 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 "serial_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. esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config)
  30. {
  31. struct sockaddr_in serv_addr;
  32. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  33. cout << "Socket creation error \n";
  34. return ESP_LOADER_ERROR_FAIL;
  35. }
  36. serv_addr.sin_family = AF_INET;
  37. serv_addr.sin_port = htons(PORT);
  38. // Convert IPv4 and IPv6 addresses from text to binary form
  39. if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
  40. cout << "Invalid address/ Address not supported \n";
  41. return ESP_LOADER_ERROR_FAIL;
  42. }
  43. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  44. cout << "Connection Failed \n";
  45. return ESP_LOADER_ERROR_FAIL;
  46. }
  47. file.open ("received_data.bin", ios::binary | ios::out);
  48. if (!file.is_open()) {
  49. cout << "Cannot open file\n";
  50. return ESP_LOADER_ERROR_FAIL;
  51. }
  52. return ESP_LOADER_SUCCESS;
  53. }
  54. void loader_port_serial_deinit()
  55. {
  56. if (sock != 0) {
  57. shutdown(sock, 0);
  58. close(sock);
  59. }
  60. if (file.is_open()) {
  61. file.close();
  62. }
  63. }
  64. esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  65. {
  66. uint32_t written = 0;
  67. do {
  68. int bytes_written = send(sock, &data[written], size - written, 0);
  69. if (bytes_written == -1) {
  70. cout << "Socket send failed\n";
  71. return ESP_LOADER_ERROR_FAIL;
  72. }
  73. written += bytes_written;
  74. } while (written != size);
  75. return ESP_LOADER_SUCCESS;
  76. }
  77. esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
  78. {
  79. uint32_t written = 0;
  80. int bytes_read = 0;
  81. do {
  82. bytes_read = read(sock, &data[written], size - written);
  83. if (bytes_read == 0) {
  84. cout << "Socket connection lost\n";
  85. return ESP_LOADER_ERROR_FAIL;
  86. }
  87. file.write((const char*)&data[written], bytes_read);
  88. file.flush();
  89. written += bytes_read;
  90. } while (written != size);
  91. return ESP_LOADER_SUCCESS;
  92. }
  93. void loader_port_enter_bootloader()
  94. {
  95. // GPIO0 and GPIO2 must be LOW
  96. // Then Reset
  97. }
  98. void loader_port_reset_target()
  99. {
  100. // Toggle reset pin
  101. }
  102. void loader_port_delay_ms(uint32_t ms)
  103. {
  104. this_thread::sleep_for(chrono::milliseconds(ms));
  105. }
  106. void loader_port_start_timer(uint32_t ms)
  107. {
  108. (void)ms;
  109. }
  110. uint32_t loader_port_remaining_time(void)
  111. {
  112. return 1;
  113. }