serial_io_mock.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <limits>
  16. #include <vector>
  17. #include <iterator>
  18. #include <algorithm>
  19. #include <iostream>
  20. #include <stdio.h>
  21. #include "serial_io.h"
  22. #include "serial_io_mock.h"
  23. using namespace std;
  24. vector<int8_t> write_buffer;
  25. vector<int8_t> read_buffer;
  26. uint32_t receive_delay = 0;
  27. esp_error_t serial_init(uint32_t baud_rate)
  28. {
  29. return ESP_SUCCESS;
  30. }
  31. void serial_deinit()
  32. {
  33. }
  34. esp_error_t serial_write(const int8_t *data, uint16_t size, uint32_t timeout)
  35. {
  36. copy(&data[0], &data[size], back_inserter(write_buffer));
  37. return ESP_SUCCESS;
  38. }
  39. esp_error_t serial_read(int8_t *data, uint16_t size, uint32_t timeout)
  40. {
  41. if (read_buffer.size() < size) {
  42. return ESP_ERROR_TIMEOUT;
  43. }
  44. if (receive_delay != 0 && timeout != 0) {
  45. if (receive_delay > timeout) {
  46. receive_delay -= timeout;
  47. return ESP_ERROR_TIMEOUT;
  48. }
  49. receive_delay = 0;
  50. }
  51. copy_n(read_buffer.begin(), size, data);
  52. read_buffer.erase(read_buffer.begin(), read_buffer.begin() + size);
  53. return ESP_SUCCESS;
  54. }
  55. void enter_bootloader()
  56. {
  57. // GPIO0 and GPIO2 must be LOW
  58. // Then Reset
  59. }
  60. void reset_target()
  61. {
  62. }
  63. void delay_miliseconds(uint32_t ms)
  64. {
  65. }
  66. // ---------- For testing purposes only ----------
  67. static void SLIP_encode(const int8_t *in_buff, size_t size, vector<int8_t> &encoded_buff)
  68. {
  69. encoded_buff.push_back('\xc0');
  70. for (uint32_t i = 0; i < size; i++) {
  71. if (in_buff[i] == '\xc0') {
  72. encoded_buff.push_back('\xdb');
  73. encoded_buff.push_back('\xdc');
  74. } else if (in_buff[i] == '\xdb') {
  75. encoded_buff.push_back('\xdb');
  76. encoded_buff.push_back('\xdd');
  77. } else {
  78. encoded_buff.push_back(in_buff[i]);
  79. }
  80. }
  81. encoded_buff.push_back('\xc0');
  82. }
  83. void clear_buffers()
  84. {
  85. write_buffer.clear();
  86. read_buffer.clear();
  87. }
  88. int8_t *write_buffer_data()
  89. {
  90. return write_buffer.data();
  91. }
  92. void set_read_buffer(const void *data, size_t size)
  93. {
  94. SLIP_encode((const int8_t *)data, size, read_buffer);
  95. }
  96. void print_array(int8_t *data, uint32_t size)
  97. {
  98. for (uint32_t i = 0; i < size; i++) {
  99. printf("%0x, ", (uint8_t)data[i]);
  100. }
  101. printf("\n");
  102. }
  103. void write_buffer_print()
  104. {
  105. print_array(write_buffer.data(), write_buffer.size());
  106. }
  107. void serial_set_time_delay(uint32_t miliseconds)
  108. {
  109. receive_delay = miliseconds;
  110. }