PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/usbpro/MockEndpoint.h

https://code.google.com/
C Header | 145 lines | 80 code | 23 blank | 42 comment | 0 complexity | 91f7052f726b5c4d77bc461c4e533b5a MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * MockEndpoint.h
  17. * This allows unittest of data received on a ConnectedDescriptor. The general
  18. * use case is:
  19. *
  20. * ola::io::PipeSocket pipe;
  21. * pipe.Init();
  22. * ola::io::PipeSocket *other_end = pip.OppositeEnd();
  23. * MockEndpoint endpoint(other_end)
  24. * ola::io::SelectServer ss;
  25. * ss.AddReadDescriptor(&pipe);
  26. * ss.AddReadDescriptor(other_end);
  27. * // Do the test here
  28. * ss.Run();
  29. * endpoint.Verify(); // make sure there are no calls remaining
  30. * Copyright (C) 2011 Simon Newton
  31. */
  32. #ifndef PLUGINS_USBPRO_MOCKENDPOINT_H_
  33. #define PLUGINS_USBPRO_MOCKENDPOINT_H_
  34. #include <cppunit/extensions/HelperMacros.h>
  35. #include <queue>
  36. #include "ola/Callback.h"
  37. #include "ola/io/Descriptor.h"
  38. /**
  39. * The MockEndpoint, this is used for the unittests.
  40. */
  41. class MockEndpoint {
  42. public:
  43. explicit MockEndpoint(ola::io::ConnectedDescriptor *descriptor);
  44. ~MockEndpoint();
  45. typedef ola::SingleUseCallback0<void> NotificationCallback;
  46. void AddExpectedData(const uint8_t *request_data,
  47. unsigned int request_size,
  48. NotificationCallback *callback = NULL);
  49. // This does the same as above, but puts the data inside a Usb Pro style
  50. // frame.
  51. void AddExpectedUsbProMessage(uint8_t label,
  52. const uint8_t *request_payload_data,
  53. unsigned int request_payload_size,
  54. NotificationCallback *callback = NULL);
  55. // This does the same as above, but puts the data inside a Robe style
  56. // frame.
  57. void AddExpectedRobeMessage(uint8_t label,
  58. const uint8_t *request_payload_data,
  59. unsigned int request_payload_size,
  60. NotificationCallback *callback = NULL);
  61. void AddExpectedDataAndReturn(const uint8_t *request_data,
  62. unsigned int request_size,
  63. const uint8_t *response_data,
  64. unsigned int response_size);
  65. // This does the same as above, but puts the data inside a Usb Pro style
  66. // frame.
  67. void AddExpectedUsbProDataAndReturn(uint8_t request_label,
  68. const uint8_t *request_payload_data,
  69. unsigned int request_payload_size,
  70. uint8_t response_label,
  71. const uint8_t *response_payload_data,
  72. unsigned int response_payload_size);
  73. // This does the same as above, but puts the data inside a Robe style
  74. // frame.
  75. void AddExpectedRobeDataAndReturn(uint8_t request_label,
  76. const uint8_t *request_payload_data,
  77. unsigned int request_payload_size,
  78. uint8_t response_label,
  79. const uint8_t *response_payload_data,
  80. unsigned int response_payload_size);
  81. void SendUnsolicited(const uint8_t *data,
  82. unsigned int length);
  83. void SendUnsolicitedUsbProData(uint8_t label,
  84. const uint8_t *response_payload_data,
  85. unsigned int response_payload_size);
  86. void SendUnsolicitedRobeData(uint8_t label,
  87. const uint8_t *response_payload_data,
  88. unsigned int response_payload_size);
  89. void Verify() {
  90. CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), m_expected_data.size());
  91. }
  92. private:
  93. ola::io::ConnectedDescriptor *m_descriptor;
  94. typedef struct {
  95. unsigned int length;
  96. const uint8_t *data;
  97. } data_frame;
  98. typedef struct {
  99. bool send_response;
  100. bool free_request;
  101. bool free_response;
  102. data_frame expected_data_frame;
  103. data_frame return_data_frame;
  104. NotificationCallback *callback;
  105. } expected_data;
  106. std::queue<expected_data> m_expected_data;
  107. void DescriptorReady();
  108. uint8_t *BuildUsbProMessage(uint8_t label,
  109. const uint8_t *data,
  110. unsigned int data_size,
  111. unsigned int *total_size);
  112. uint8_t *BuildRobeMessage(uint8_t label,
  113. const uint8_t *data,
  114. unsigned int data_size,
  115. unsigned int *total_size);
  116. enum {MAX_DATA_SIZE = 600};
  117. static const unsigned int FOOTER_SIZE = 1;
  118. static const unsigned int HEADER_SIZE = 4;
  119. static const unsigned int ROBE_FOOTER_SIZE = 1;
  120. static const unsigned int ROBE_HEADER_SIZE = 5;
  121. };
  122. #endif // PLUGINS_USBPRO_MOCKENDPOINT_H_