InterfaceAbstraction.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #include <iostream>
  33. #include "InterfaceAbstraction.h"
  34. #define MAX_OPEN_RETRY 10000
  35. using std::cout;
  36. using std::endl;
  37. bool InterfaceAbstraction::Open(const char * toIPAPath, const char * fromIPAPath)
  38. {
  39. int tries_cnt = MAX_OPEN_RETRY;
  40. if (NULL == toIPAPath && NULL == fromIPAPath)
  41. {
  42. printf("InterfaceAbstraction constructor got 2 null arguments.\n");
  43. exit(0);
  44. }
  45. if (NULL != toIPAPath) {
  46. while (tries_cnt > 0) {
  47. printf("trying to open %s %d/%d\n", toIPAPath, MAX_OPEN_RETRY - tries_cnt, MAX_OPEN_RETRY);
  48. // Sleep for 5 msec
  49. usleep(5000);
  50. m_toIPADescriptor = open(toIPAPath, O_WRONLY);
  51. if (-1 != m_toIPADescriptor) {
  52. printf("Success!\n");
  53. break;
  54. }
  55. tries_cnt--;
  56. }
  57. printf("open retries_cnt=%d\n", MAX_OPEN_RETRY - tries_cnt);
  58. if (-1 == m_toIPADescriptor) {
  59. printf("InterfaceAbstraction failed while opening %s.\n", toIPAPath);
  60. exit(0);
  61. }
  62. m_toChannelName = toIPAPath;
  63. printf("%s device node opened, fd = %d.\n", toIPAPath, m_toIPADescriptor);
  64. }
  65. tries_cnt = MAX_OPEN_RETRY;
  66. if (NULL != fromIPAPath) {
  67. while (tries_cnt > 0) {
  68. printf("trying to open %s %d/%d\n", fromIPAPath, MAX_OPEN_RETRY - tries_cnt, MAX_OPEN_RETRY);
  69. // Sleep for 5 msec
  70. usleep(5000);
  71. m_fromIPADescriptor = open(fromIPAPath, O_RDONLY);
  72. if (-1 != m_fromIPADescriptor) {
  73. printf("Success!\n");
  74. break;
  75. }
  76. tries_cnt--;
  77. }
  78. printf("open retries_cnt=%d\n", MAX_OPEN_RETRY - tries_cnt);
  79. if (-1 == m_fromIPADescriptor)
  80. {
  81. printf("InterfaceAbstraction failed on opening %s.\n", fromIPAPath);
  82. exit(0);
  83. }
  84. m_fromChannelName = fromIPAPath;
  85. printf("%s device node opened, fd = %d.\n", fromIPAPath, m_fromIPADescriptor);
  86. }
  87. return true;
  88. }/*Ctor*/
  89. void InterfaceAbstraction::Close()
  90. {
  91. close(m_toIPADescriptor);
  92. close(m_fromIPADescriptor);
  93. }
  94. long InterfaceAbstraction::SendData(unsigned char *buf, size_t size)
  95. {
  96. long bytesWritten = 0;
  97. printf("Trying to write %zu bytes to %d.\n", size, m_toIPADescriptor);
  98. bytesWritten = write(m_toIPADescriptor, buf, size);
  99. if (-1 == bytesWritten)
  100. {
  101. int err = errno;
  102. printf(
  103. "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
  104. printf(
  105. "Failed to execute command\n write(m_toIPADescriptor, buf, size);\n "
  106. "m_toIPADescriptor=%d, buf=0x%p, size=%zu",m_toIPADescriptor,
  107. buf,
  108. size);
  109. printf("Error on write execution, errno=%d, Quitting!\n", err);
  110. printf(
  111. "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
  112. exit(-1);
  113. }
  114. cout << "bytesWritten = " << bytesWritten << endl;
  115. return bytesWritten;
  116. }
  117. int InterfaceAbstraction::ReceiveData(unsigned char *buf, size_t size)
  118. {
  119. size_t bytesRead = 0;
  120. size_t totalBytesRead = 0;
  121. bool continueRead = false;
  122. do
  123. {
  124. printf("Trying to read %zu bytes from %d.\n", size, m_fromIPADescriptor);
  125. bytesRead = read(m_fromIPADescriptor, (void*)buf, size);
  126. printf("Read %zu bytes.\n", bytesRead);
  127. totalBytesRead += bytesRead;
  128. if (bytesRead == size)
  129. continueRead = true;
  130. else
  131. continueRead = false;
  132. } while (continueRead);
  133. return totalBytesRead;
  134. }
  135. int InterfaceAbstraction::ReceiveSingleDataChunk(unsigned char *buf, size_t size){
  136. size_t bytesRead = 0;
  137. printf("Trying to read %zu bytes from %d.\n", size, m_fromIPADescriptor);
  138. bytesRead = read(m_fromIPADescriptor, (void*)buf, size);
  139. printf("Read %zu bytes.\n", bytesRead);
  140. return bytesRead;
  141. }
  142. int InterfaceAbstraction::setReadNoBlock(){
  143. int flags = fcntl(m_fromIPADescriptor, F_GETFL, 0);
  144. if(flags == -1){
  145. return -1;
  146. }
  147. return fcntl(m_fromIPADescriptor, F_SETFL, flags | O_NONBLOCK);
  148. }
  149. int InterfaceAbstraction::clearReadNoBlock(){
  150. int flags = fcntl(m_fromIPADescriptor, F_GETFL, 0);
  151. if(flags == -1){
  152. return -1;
  153. }
  154. return fcntl(m_fromIPADescriptor, F_SETFL, flags & ~O_NONBLOCK);
  155. }
  156. InterfaceAbstraction::~InterfaceAbstraction()
  157. {
  158. close(m_fromIPADescriptor);
  159. m_fromChannelName = "";
  160. close(m_toIPADescriptor);
  161. m_toChannelName = "";
  162. }