InterfaceAbstraction.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "InterfaceAbstraction.h"
  33. #define MAX_OPEN_RETRY 10000
  34. bool InterfaceAbstraction::Open(const char * toIPAPath, const char * fromIPAPath)
  35. {
  36. int tries_cnt = MAX_OPEN_RETRY;
  37. if (NULL == toIPAPath && NULL == fromIPAPath)
  38. {
  39. printf("InterfaceAbstraction constructor got 2 null arguments.\n");
  40. exit(0);
  41. }
  42. if (NULL != toIPAPath) {
  43. while (tries_cnt > 0) {
  44. printf("trying to open %s %d/%d\n", toIPAPath, MAX_OPEN_RETRY - tries_cnt, MAX_OPEN_RETRY);
  45. // Sleep for 5 msec
  46. usleep(5000);
  47. m_toIPADescriptor = open(toIPAPath, O_WRONLY);
  48. if (-1 != m_toIPADescriptor) {
  49. printf("Success!\n");
  50. break;
  51. }
  52. tries_cnt--;
  53. }
  54. printf("open retries_cnt=%d\n", MAX_OPEN_RETRY - tries_cnt);
  55. if (-1 == m_toIPADescriptor) {
  56. printf("InterfaceAbstraction failed while opening %s.\n", toIPAPath);
  57. exit(0);
  58. }
  59. m_toChannelName = toIPAPath;
  60. printf("%s device node opened, fd = %d.\n", toIPAPath, m_toIPADescriptor);
  61. }
  62. tries_cnt = MAX_OPEN_RETRY;
  63. if (NULL != fromIPAPath) {
  64. while (tries_cnt > 0) {
  65. printf("trying to open %s %d/%d\n", fromIPAPath, MAX_OPEN_RETRY - tries_cnt, MAX_OPEN_RETRY);
  66. // Sleep for 5 msec
  67. usleep(5000);
  68. m_fromIPADescriptor = open(fromIPAPath, O_RDONLY);
  69. if (-1 != m_fromIPADescriptor) {
  70. printf("Success!\n");
  71. break;
  72. }
  73. tries_cnt--;
  74. }
  75. printf("open retries_cnt=%d\n", MAX_OPEN_RETRY - tries_cnt);
  76. if (-1 == m_fromIPADescriptor)
  77. {
  78. printf("InterfaceAbstraction failed on opening %s.\n", fromIPAPath);
  79. exit(0);
  80. }
  81. m_fromChannelName = fromIPAPath;
  82. printf("%s device node opened, fd = %d.\n", fromIPAPath, m_fromIPADescriptor);
  83. }
  84. return true;
  85. }/*Ctor*/
  86. void InterfaceAbstraction::Close()
  87. {
  88. close(m_toIPADescriptor);
  89. close(m_fromIPADescriptor);
  90. }
  91. bool InterfaceAbstraction::SendData(unsigned char *buf, size_t size)
  92. {
  93. int bytesWritten = 0;
  94. printf("Trying to write %zu bytes to %d.\n", size, m_toIPADescriptor);
  95. bytesWritten = write(m_toIPADescriptor, buf, size);
  96. if (-1 == bytesWritten)
  97. {
  98. int err = errno;
  99. printf(
  100. "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
  101. printf(
  102. "Failed to execute command\n write(m_toIPADescriptor, buf, size);\n "
  103. "m_toIPADescriptor=%d, buf=0x%p, size=%zu",m_toIPADescriptor,
  104. buf,
  105. size);
  106. printf("Error on write execution, errno=%d, Quitting!\n", err);
  107. printf(
  108. "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
  109. exit(-1);
  110. }
  111. printf("bytesWritten = %d.\n", bytesWritten);
  112. return bytesWritten;
  113. }
  114. int InterfaceAbstraction::ReceiveData(unsigned char *buf, size_t size)
  115. {
  116. size_t bytesRead = 0;
  117. size_t totalBytesRead = 0;
  118. bool continueRead = false;
  119. do
  120. {
  121. printf("Trying to read %zu bytes from %d.\n", size, m_fromIPADescriptor);
  122. bytesRead = read(m_fromIPADescriptor, (void*)buf, size);
  123. printf("Read %zu bytes.\n", bytesRead);
  124. totalBytesRead += bytesRead;
  125. if (bytesRead == size)
  126. continueRead = true;
  127. else
  128. continueRead = false;
  129. } while (continueRead);
  130. return totalBytesRead;
  131. }
  132. InterfaceAbstraction::~InterfaceAbstraction()
  133. {
  134. close(m_fromIPADescriptor);
  135. m_fromChannelName = "";
  136. close(m_toIPADescriptor);
  137. m_toChannelName = "";
  138. }