ShimClient.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "ShimClient.h"
  2. #include "trace.h"
  3. #include <iostream>
  4. #include <Arduino.h>
  5. #include <ctime>
  6. extern "C" {
  7. uint32_t millis(void) {
  8. return time(0)*1000;
  9. }
  10. }
  11. ShimClient::ShimClient() {
  12. this->responseBuffer = new Buffer();
  13. this->expectBuffer = new Buffer();
  14. this->_allowConnect = true;
  15. this->_connected = false;
  16. this->_error = false;
  17. this->expectAnything = true;
  18. this->_received = 0;
  19. this->_expectedPort = 0;
  20. }
  21. int ShimClient::connect(IPAddress ip, uint16_t port) {
  22. if (this->_allowConnect) {
  23. this->_connected = true;
  24. }
  25. if (this->_expectedPort !=0) {
  26. // if (memcmp(ip,this->_expectedIP,4) != 0) {
  27. // TRACE( "ip mismatch\n");
  28. // this->_error = true;
  29. // }
  30. if (port != this->_expectedPort) {
  31. TRACE( "port mismatch\n");
  32. this->_error = true;
  33. }
  34. }
  35. return this->_connected;
  36. }
  37. int ShimClient::connect(const char *host, uint16_t port) {
  38. if (this->_allowConnect) {
  39. this->_connected = true;
  40. }
  41. if (this->_expectedPort !=0) {
  42. if (strcmp(host,this->_expectedHost) != 0) {
  43. TRACE( "host mismatch\n");
  44. this->_error = true;
  45. }
  46. if (port != this->_expectedPort) {
  47. TRACE( "port mismatch\n");
  48. this->_error = true;
  49. }
  50. }
  51. return this->_connected;
  52. }
  53. size_t ShimClient::write(uint8_t b) {
  54. this->_received += 1;
  55. TRACE(std::hex << (unsigned int)b);
  56. if (!this->expectAnything) {
  57. if (this->expectBuffer->available()) {
  58. uint8_t expected = this->expectBuffer->next();
  59. if (expected != b) {
  60. this->_error = true;
  61. TRACE("!=" << (unsigned int)expected);
  62. }
  63. } else {
  64. this->_error = true;
  65. }
  66. }
  67. TRACE("\n"<< std::dec);
  68. return 1;
  69. }
  70. size_t ShimClient::write(const uint8_t *buf, size_t size) {
  71. this->_received += size;
  72. TRACE( "[" << std::dec << (unsigned int)(size) << "] ");
  73. uint16_t i=0;
  74. for (;i<size;i++) {
  75. if (i>0) {
  76. TRACE(":");
  77. }
  78. TRACE(std::hex << (unsigned int)(buf[i]));
  79. if (!this->expectAnything) {
  80. if (this->expectBuffer->available()) {
  81. uint8_t expected = this->expectBuffer->next();
  82. if (expected != buf[i]) {
  83. this->_error = true;
  84. TRACE("!=" << (unsigned int)expected);
  85. }
  86. } else {
  87. this->_error = true;
  88. }
  89. }
  90. }
  91. TRACE("\n"<<std::dec);
  92. return size;
  93. }
  94. int ShimClient::available() {
  95. return this->responseBuffer->available();
  96. }
  97. int ShimClient::read() { return this->responseBuffer->next(); }
  98. int ShimClient::read(uint8_t *buf, size_t size) {
  99. uint16_t i = 0;
  100. for (;i<size;i++) {
  101. buf[i] = this->read();
  102. }
  103. return size;
  104. }
  105. int ShimClient::peek() { return 0; }
  106. void ShimClient::flush() {}
  107. void ShimClient::stop() {
  108. this->setConnected(false);
  109. }
  110. uint8_t ShimClient::connected() { return this->_connected; }
  111. ShimClient::operator bool() { return true; }
  112. ShimClient* ShimClient::respond(uint8_t *buf, size_t size) {
  113. this->responseBuffer->add(buf,size);
  114. return this;
  115. }
  116. ShimClient* ShimClient::expect(uint8_t *buf, size_t size) {
  117. this->expectAnything = false;
  118. this->expectBuffer->add(buf,size);
  119. return this;
  120. }
  121. void ShimClient::setConnected(bool b) {
  122. this->_connected = b;
  123. }
  124. void ShimClient::setAllowConnect(bool b) {
  125. this->_allowConnect = b;
  126. }
  127. bool ShimClient::error() {
  128. return this->_error;
  129. }
  130. uint16_t ShimClient::received() {
  131. return this->_received;
  132. }
  133. void ShimClient::expectConnect(IPAddress ip, uint16_t port) {
  134. this->_expectedIP = ip;
  135. this->_expectedPort = port;
  136. }
  137. void ShimClient::expectConnect(const char *host, uint16_t port) {
  138. this->_expectedHost = host;
  139. this->_expectedPort = port;
  140. }