You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1713 lines
39 KiB

2 years ago
2 years ago
​
2 years ago
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
  1. #include "hardwareResourceSerialProvider.h"
  2. #include "../data/protocol/protocol.h"
  3. #include <fcntl.h>
  4. #include <sys/ioctl.h>
  5. #include <sys/stat.h>
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. #include <termios.h>
  9. // #include "rgooseDBConnector.h"
  10. // #define DEBUG_EXTRA
  11. // #define DEBUG_SERIAL_COMMUNICATION
  12. #define LOG(X) hardwareResource::m_pLog->Log("HWR_SERIAL", this, X)
  13. #ifdef DEBUG
  14. #define LOG_ERROR(X) \
  15. { \
  16. char _tBfrErrMSG2w34[2048]; \
  17. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  18. hardwareResource::m_pLog->Log("HWR_SERIAL", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  19. }
  20. #else
  21. #define LOG_ERROR(X) hardwareResource::m_pLog->Log("HWR_SERIAL", this, _LOG_ERROR, X);
  22. #endif
  23. #ifndef HALF_DUPLEX
  24. hardwareResourceSerialProvider::hardwareResourceSerialProvider(uint32_t dbID) :
  25. hardwareResource(dbID, HARDWARE_PROVIDER_SERIAL),
  26. basicTransport(),
  27. basicDualThread()
  28. {
  29. m_nInputBufferSize = 0;
  30. m_nOutputBufferSize = 0;
  31. #else
  32. hardwareResourceSerialProvider::hardwareResourceSerialProvider(uint32_t dbID) :
  33. hardwareResource(dbID, HARDWARE_PROVIDER_SERIAL),
  34. basicTransport(),
  35. basicThread()
  36. {
  37. #endif
  38. memset(&m_sUartData, 0, sizeof(m_sUartData));
  39. memset(&m_sTTY, 0, sizeof(m_sTTY));
  40. m_iDeviceFD = -1;
  41. m_iMasterFD = -1;
  42. #ifdef FIO_SELECT_MODE
  43. m_iControlPipeFD[0] = m_iControlPipeFD[1] = -1;
  44. pipe(m_iControlPipeFD);
  45. FD_ZERO(&m_sSenderSet);
  46. FD_ZERO(&m_sReaderSet);
  47. FD_SET(m_iControlPipeFD[0], &m_sSenderSet);
  48. m_nSenderSetMax = m_iControlPipeFD[0];
  49. FD_SET(m_iControlPipeFD[1], &m_sReaderSet);
  50. m_nReaderSetMax = m_iControlPipeFD[1];
  51. #endif
  52. #ifdef __linux__
  53. m_iInFID = -1;
  54. m_iOutFID = -1;
  55. m_bManualRTS = false;
  56. #endif
  57. }
  58. hardwareResourceSerialProvider::~hardwareResourceSerialProvider()
  59. {
  60. closePort();
  61. }
  62. bool hardwareResourceSerialProvider::create(void)
  63. {
  64. if (m_pDBC != NULL)
  65. {
  66. DBC_UART_DATA * tDATA = NULL;
  67. tDATA = m_pDBC->getUartData(m_sResourceSettings.aux_id);
  68. if (tDATA != NULL)
  69. {
  70. memcpy(&m_sUartData, tDATA, sizeof(DBC_UART_DATA));
  71. #ifdef DEBUG
  72. LOG(m_sUartData.path);
  73. #endif
  74. free(tDATA);
  75. switch (m_sUartData.type)
  76. {
  77. case HARDWARE_UART : m_bValid = create_HW(); break;
  78. case HARDWARE_IP_UART: m_bValid = create_SOL(); break;
  79. };
  80. }
  81. else
  82. {
  83. LOG_ERROR("No Data!!");
  84. };
  85. };
  86. return m_bValid;
  87. }
  88. bool hardwareResourceSerialProvider::validate(void)
  89. {
  90. if (m_pDBC != NULL)
  91. {
  92. DBC_UART_DATA * tDATA = m_pDBC->getUartData(m_sResourceSettings.aux_id);
  93. switch (m_sUartData.type)
  94. {
  95. case HARDWARE_UART:
  96. {
  97. // HW port is valid
  98. #ifndef HALF_DUPLEX
  99. if ((m_bThreadBStatus == TH_ENDED) && (m_bThreadAStatus == TH_ENDED))
  100. {
  101. LOG("IO Theads are Finished!");
  102. #else
  103. if (m_bThreadStatus == TH_ENDED)
  104. {
  105. LOG("IO Thead is Finished!");
  106. #endif
  107. m_bValid = false;
  108. closePort();
  109. };
  110. break;
  111. };
  112. case HARDWARE_IP_UART:
  113. {
  114. // Ping E2U device
  115. if (m_bValid)
  116. {
  117. m_bValid = pingDevice();
  118. if (! m_bValid)
  119. {
  120. LOG("No Ping to Device!!");
  121. closePort();
  122. };
  123. };
  124. break;
  125. };
  126. };
  127. #ifdef DEBUG_EXTRA
  128. LOG("Check Settings For Changes");
  129. #endif
  130. if (tDATA != NULL)
  131. {
  132. bool bFlag = true;
  133. if (strcmp(tDATA->path, m_sUartData.path) != 0)
  134. {
  135. bFlag = false;
  136. }
  137. if (bFlag && (m_sUartData.speed != tDATA->speed))
  138. {
  139. bFlag = false;
  140. }
  141. if (bFlag && (m_sUartData.mode != tDATA->mode))
  142. {
  143. bFlag = false;
  144. }
  145. if (bFlag && (m_sUartData.port != tDATA->port))
  146. {
  147. bFlag = false;
  148. }
  149. if (! bFlag)
  150. {
  151. #ifdef DEBUG_EXTRA
  152. LOG("Settings Has been Changed!");
  153. #endif
  154. memcpy(&m_sUartData, tDATA, sizeof(DBC_UART_DATA));
  155. #ifdef DEBUG_EXTRA
  156. LOG("Reconect with new settings");
  157. #endif
  158. if (m_bValid)
  159. {
  160. closePort();
  161. }
  162. switch (m_sUartData.type)
  163. {
  164. case HARDWARE_UART : m_bValid = create_HW(); break;
  165. case HARDWARE_IP_UART: m_bValid = create_SOL(); break;
  166. };
  167. };
  168. // Check If Not Connected to Connect!
  169. if (! m_bValid)
  170. {
  171. #ifdef DEBUG_EXTRA
  172. LOG("Try to Conncet");
  173. #endif
  174. switch (m_sUartData.type)
  175. {
  176. case HARDWARE_UART : m_bValid = create_HW(); break;
  177. case HARDWARE_IP_UART: m_bValid = create_SOL(); break;
  178. };
  179. };
  180. free(tDATA);
  181. }
  182. else
  183. {
  184. LOG_ERROR("Error Fetch DB Data!");
  185. };
  186. }
  187. else
  188. {
  189. m_bValid = false;
  190. }
  191. return m_bValid;
  192. }
  193. bool hardwareResourceSerialProvider::pingDevice()
  194. {
  195. bool RetVal = false;
  196. if (m_bValid)
  197. {
  198. RetVal = pingIP4(m_sUartData.path);
  199. }
  200. return RetVal;
  201. }
  202. bool hardwareResourceSerialProvider::create_SOL()
  203. {
  204. /*
  205. if( m_sUartData.mode == SOL_NOT_DEFINED ) {
  206. LOG_ERROR("No Connection Type Defined!");
  207. return false;
  208. };
  209. */
  210. if (m_sUartData.port == 0)
  211. {
  212. LOG_ERROR("No Port Defined!");
  213. return false;
  214. };
  215. if (m_sUartData.speed == 0)
  216. {
  217. LOG_ERROR("No Speed Defined!");
  218. return false;
  219. };
  220. if (m_sUartData.path[0] == (char) 0)
  221. {
  222. LOG_ERROR("No Adress Defined!");
  223. return false;
  224. };
  225. switch (m_sUartData.mode)
  226. {
  227. case SOL_UDP_CLIENT:
  228. {
  229. #ifdef DEBUG
  230. LOG("PORT IS CLIENT - Create UDP Server");
  231. #endif
  232. #ifdef __linux__
  233. // Creating socket file descriptor
  234. if ((m_iDeviceFD = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  235. {
  236. LOG_ERROR("UDP Socket Creation Failed!");
  237. return false;
  238. };
  239. fcntl(m_iDeviceFD, F_SETFL, O_NONBLOCK);
  240. // Filling server information
  241. struct sockaddr_in tServerAddr;
  242. memset(&tServerAddr, 0, sizeof(tServerAddr));
  243. tServerAddr.sin_family = AF_INET; // IPv4
  244. tServerAddr.sin_addr.s_addr = INADDR_ANY;
  245. tServerAddr.sin_port = htons(m_sUartData.port);
  246. // Bind the socket with the server address
  247. if (bind(m_iDeviceFD, (const struct sockaddr *) &tServerAddr, sizeof(tServerAddr)) < 0)
  248. {
  249. LOG_ERROR("UDP Bind Failed");
  250. return false;
  251. };
  252. #else
  253. WSADATA wsaData;
  254. // Initialize Winsock version 2.2
  255. char tBuff[128];
  256. if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  257. {
  258. sprintf(tBuff, "WSAStartup failed with error %ld", WSAGetLastError());
  259. LOG_ERROR(tBuff);
  260. return false;
  261. }
  262. else
  263. {
  264. sprintf(tBuff, "The Winsock DLL status is %s.", wsaData.szSystemStatus);
  265. LOG(tBuff);
  266. };
  267. // Set up a SOCKADDR_IN structure that will tell bind that we
  268. // want to receive datagrams from all interfaces using port 5150.
  269. // The IPv4 family
  270. ReceiverAddr.sin_family = AF_INET;
  271. // Port no. 5150
  272. ReceiverAddr.sin_port = htons(m_nPort);
  273. // From all interface (0.0.0.0)
  274. ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  275. // Associate the address information with the socket using bind.
  276. // At this point you can receive datagrams on your bound socket.
  277. if (bind(m_sSocket, (SOCKADDR *) &ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR)
  278. {
  279. sprintf(tBuff, "Server: bind() failed!Error : %ld.", WSAGetLastError());
  280. // Close the socket
  281. closesocket(m_sSocket);
  282. // Do the clean up
  283. WSACleanup();
  284. // and exit with error
  285. LOG_ERROR(tBuff);
  286. return false;
  287. }
  288. else
  289. {
  290. LOG("UDP Server: bind() is OK!");
  291. };
  292. // Some info on the receiver side...
  293. SOCKADDR_IN tAddr;
  294. int tLen = sizeof(SOCKADDR_IN);
  295. getsockname(m_sSocket, (SOCKADDR *) &tAddr, &tLen);
  296. sprintf(tBuff, "Receiving IP(s) used : %s", inet_ntoa(tAddr.sin_addr));
  297. LOG(tBuff);
  298. sprintf(tBuff, "Receiving port used : %d", htons(tAddr.sin_port));
  299. LOG(tBuff);
  300. SelectTiming = recvfromTimeOutUDP(m_sSocket, 10, 0);
  301. #endif
  302. break;
  303. };
  304. case SOL_TCP_CLIENT:
  305. {
  306. #ifdef DEBUG
  307. LOG("PORT IS CLIENT - Create TCP Server");
  308. #endif
  309. break;
  310. };
  311. case SOL_UDP_SERVER:
  312. {
  313. #ifdef DEBUG
  314. LOG("PORT IS SERVER - Create UDP Client");
  315. #endif
  316. #ifdef __linux__
  317. // Creating socket file descriptor
  318. if ((m_iDeviceFD = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  319. {
  320. LOG_ERROR("Socket Creation Failed!");
  321. return false;
  322. };
  323. fcntl(m_iDeviceFD, F_SETFL, O_NONBLOCK);
  324. // Filling server information
  325. struct sockaddr_in tServerAddr;
  326. memset(&tServerAddr, 0, sizeof(tServerAddr));
  327. tServerAddr.sin_family = AF_INET;
  328. tServerAddr.sin_addr.s_addr = INADDR_ANY;
  329. tServerAddr.sin_port = htons(m_sUartData.port);
  330. #else
  331. #endif
  332. break;
  333. };
  334. case SOL_TCP_SERVER:
  335. {
  336. #ifdef DEBUG
  337. LOG("PORT IS SERVER - Create TCP Client");
  338. #endif
  339. // socket create and varification
  340. m_iDeviceFD = socket(AF_INET, SOCK_STREAM, 0);
  341. if (m_iDeviceFD == -1)
  342. {
  343. LOG_ERROR("Socket creation failed!");
  344. return false;
  345. }
  346. #ifdef DEBUG
  347. else
  348. {
  349. LOG("Socket successfully created");
  350. }
  351. #endif
  352. // assign IP, PORT
  353. struct sockaddr_in tServerAddr;
  354. memset(&tServerAddr, 0, sizeof(tServerAddr));
  355. tServerAddr.sin_family = AF_INET;
  356. tServerAddr.sin_addr.s_addr = inet_addr(m_sUartData.path);
  357. tServerAddr.sin_port = htons(m_sUartData.port);
  358. // connect the client socket to server socket
  359. if (connect(m_iDeviceFD, (struct sockaddr *) &tServerAddr, sizeof(tServerAddr)) != 0)
  360. {
  361. char cBuffer[256];
  362. sprintf(cBuffer, "ERROR connect to [%s:%d] (%s)", m_sUartData.path, m_sUartData.port, strerror(errno));
  363. LOG_ERROR(cBuffer);
  364. return false;
  365. }
  366. #ifdef DEBUG
  367. else
  368. {
  369. LOG("Connected to the server");
  370. }
  371. #endif
  372. // Set non-blocking
  373. int arg;
  374. if ((arg = fcntl(m_iDeviceFD, F_GETFL, NULL)) < 0)
  375. {
  376. char tBuf[128];
  377. sprintf(tBuf, "Error fcntl(..., F_GETFL) (%s)", strerror(errno));
  378. LOG_ERROR(tBuf);
  379. return false;
  380. };
  381. arg |= O_NONBLOCK;
  382. if (fcntl(m_iDeviceFD, F_SETFL, arg) < 0)
  383. {
  384. char tBuf[128];
  385. sprintf(tBuf, "Error fcntl(..., F_SETFL) (%s)", strerror(errno));
  386. LOG_ERROR(tBuf);
  387. return false;
  388. };
  389. setConnectionStatus(CS_CONNECTED);
  390. break;
  391. };
  392. };
  393. #ifdef FIO_SELECT_MODE
  394. FD_SET(m_iDeviceFD, &m_sReaderSet);
  395. if (m_nReaderSetMax < m_iDeviceFD)
  396. {
  397. m_nReaderSetMax = m_iDeviceFD;
  398. }
  399. #endif
  400. return init();
  401. }
  402. bool hardwareResourceSerialProvider::create_HW()
  403. {
  404. char MsgBuffer[CLOG];
  405. if (m_eConnectionStatus == CS_CONNECTED)
  406. {
  407. LOG_ERROR("TRYING TO CONNECT ON OPENED PORT!");
  408. return false;
  409. };
  410. // if( m_bInitComplete ) {
  411. // LOG_ERROR( "TRYING TO CREATE CREATED PORT!" );
  412. // return false;
  413. // };
  414. #ifdef __linux__
  415. int result;
  416. /* Open File Descriptor */
  417. // m_iFID = open( DevicePath, O_RDWR| O_NONBLOCK | O_NDELAY );
  418. #ifdef __arm__
  419. LOG("ARM Defined!");
  420. #endif
  421. #ifdef RASPI_HARDWARE
  422. LOG("RASPI_HARDWARE Defined!");
  423. #endif
  424. #ifdef __aarch64__
  425. LOG("AARCH64 Defined!");
  426. #endif
  427. #ifdef USE_WIRINGX
  428. LOG("USE_WIRINGX Defined!");
  429. LOG(WIRINGX_PLATFORM);
  430. #endif
  431. #ifdef USE_WIRINGPI
  432. LOG("USE_WIRINGPI Defined!");
  433. if (strstr(m_sUartData.path, "USB") == NULL)
  434. {
  435. #ifdef DEBUG
  436. LOG("MANUAL RTS CTL(R)");
  437. #endif
  438. m_bManualRTS = true;
  439. wiringPiSetup();
  440. // Set RTS Pin
  441. pinMode(RTS_PIN_NUMBER, OUTPUT);
  442. }
  443. else
  444. {
  445. #ifdef DEBUG
  446. LOG("NO MANUAL RTS CTL(R)");
  447. #endif
  448. m_bManualRTS = false;
  449. };
  450. #else
  451. #ifdef USE_WIRINGX
  452. if (strstr(m_sUartData.path, "USB") == NULL)
  453. {
  454. #ifdef DEBUG
  455. LOG("MANUAL RTS CTL(X)");
  456. #endif
  457. if (wiringXSetup(WIRINGX_PLATFORM, NULL) == -1)
  458. {
  459. LOG_ERROR("WiringX Platform Init Failure!");
  460. m_bManualRTS = false;
  461. }
  462. else
  463. {
  464. // Set RTS Pin
  465. // pinMode( 0, OUTPUT );
  466. pinMode(RTS_PIN_NUMBER, PINMODE_OUTPUT);
  467. m_bManualRTS = true;
  468. };
  469. }
  470. else
  471. {
  472. #ifdef DEBUG
  473. LOG("NO MANUAL RTS CTL(X)");
  474. #endif
  475. m_bManualRTS = false;
  476. };
  477. #else
  478. #ifdef DEBUG
  479. LOG("NO MANUAL RTS CTL(INTEL)");
  480. #endif
  481. m_bManualRTS = false;
  482. #endif
  483. #endif
  484. do
  485. {
  486. m_iInFID = open(m_sUartData.path, O_RDONLY | O_NONBLOCK | O_NDELAY);
  487. }
  488. while (m_iInFID == -1 && errno == EINTR);
  489. if (m_iInFID == -1)
  490. {
  491. LOG_ERROR("Can not open Input Stream");
  492. return false;
  493. };
  494. do
  495. {
  496. result = fcntl(m_iInFID, F_SETFL, O_NONBLOCK);
  497. }
  498. while (result == -1 && errno == EINTR);
  499. if (result == -1)
  500. {
  501. const int error = errno;
  502. do
  503. {
  504. result = close(m_iInFID);
  505. }
  506. while (result == -1 && errno == EINTR);
  507. errno = error;
  508. sprintf(MsgBuffer, "Error %d openning port: %s", errno, strerror(errno));
  509. LOG_ERROR(MsgBuffer);
  510. return false;
  511. };
  512. do
  513. {
  514. m_iOutFID = open(m_sUartData.path, O_WRONLY | O_NDELAY);
  515. }
  516. while (m_iOutFID == -1 && errno == EINTR);
  517. if (m_iOutFID == -1)
  518. {
  519. LOG_ERROR("Can not open Output Stream");
  520. return false;
  521. };
  522. /* *** Configure Port *** */
  523. memset(&m_sTTY, 0, sizeof(m_sTTY));
  524. /* Error Handling */
  525. do
  526. {
  527. result = tcgetattr(m_iInFID, &m_sTTY);
  528. }
  529. while (result == -1 && errno == EINTR);
  530. if (result == -1)
  531. {
  532. const int error = errno;
  533. do
  534. {
  535. result = close(m_iInFID);
  536. }
  537. while (result == -1 && errno == EINTR);
  538. errno = error;
  539. sprintf(MsgBuffer, "Error %d from tcgetattr: %s", errno, strerror(errno));
  540. LOG_ERROR(MsgBuffer);
  541. return false;
  542. };
  543. /* Set Baud Rate */
  544. int RetVal = 0;
  545. switch (m_sUartData.speed)
  546. {
  547. case 4000000: cfsetspeed(&m_sTTY, B4000000); break;
  548. case 3500000: cfsetspeed(&m_sTTY, B3500000); break;
  549. case 3000000: cfsetspeed(&m_sTTY, B3000000); break;
  550. case 2500000: cfsetspeed(&m_sTTY, B2500000); break;
  551. case 2000000: cfsetspeed(&m_sTTY, B2000000); break;
  552. case 1500000: cfsetspeed(&m_sTTY, B1500000); break;
  553. case 1152000: cfsetspeed(&m_sTTY, B1152000); break;
  554. case 1000000: cfsetspeed(&m_sTTY, B1000000); break;
  555. case 921600 : cfsetspeed(&m_sTTY, B921600); break;
  556. case 460800 : cfsetspeed(&m_sTTY, B460800); break;
  557. case 230400 : cfsetspeed(&m_sTTY, B230400); break;
  558. case 115200 : cfsetspeed(&m_sTTY, B115200); break;
  559. case 19200 : cfsetspeed(&m_sTTY, B19200); break;
  560. case 9600 : cfsetspeed(&m_sTTY, B9600); break;
  561. default:
  562. {
  563. close(m_iInFID);
  564. close(m_iOutFID);
  565. sprintf(MsgBuffer, "Error cannot set speed as [%d] bod", m_sUartData.speed);
  566. LOG_ERROR(MsgBuffer);
  567. return false;
  568. };
  569. };
  570. if (RetVal == 0)
  571. {
  572. #ifdef DEBUG
  573. sprintf(MsgBuffer, "speed set to [%d] bod", m_sUartData.speed);
  574. LOG(MsgBuffer);
  575. #endif
  576. }
  577. else
  578. {
  579. RetVal = cfgetispeed(&m_sTTY);
  580. sprintf(MsgBuffer, "Error cannot set speed as [%d] bod. Current is: %d bod. ", m_sUartData.speed, RetVal);
  581. LOG_ERROR(MsgBuffer);
  582. return false;
  583. };
  584. /* Setting other Port Stuff */
  585. m_sTTY.c_cflag &= ~PARENB; // Make 8n1
  586. m_sTTY.c_cflag &= ~CSTOPB;
  587. m_sTTY.c_cflag &= ~CSIZE;
  588. m_sTTY.c_cflag |= CS8;
  589. m_sTTY.c_cflag &= ~CRTSCTS; // no flow control
  590. //
  591. m_sTTY.c_iflag &= ~(INLCR | ICRNL); // fixing 0x0D->0x0A
  592. //
  593. m_sTTY.c_lflag = 0; // no signaling chars, no echo, no canonical processing
  594. m_sTTY.c_oflag = 0; // no remapping, no delays
  595. m_sTTY.c_cc[VMIN] = 0; // read doesn't block
  596. m_sTTY.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  597. m_sTTY.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  598. m_sTTY.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  599. m_sTTY.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  600. m_sTTY.c_oflag &= ~OPOST; // make raw
  601. /* Flush Port, then applies attributes */
  602. tcflush(m_iOutFID, TCIFLUSH);
  603. do
  604. {
  605. result = tcsetattr(m_iOutFID, TCSANOW, &m_sTTY);
  606. }
  607. while (result == -1 && errno == EINTR);
  608. if (result == -1)
  609. {
  610. const int error = errno;
  611. do
  612. {
  613. result = close(m_iOutFID);
  614. }
  615. while (result == -1 && errno == EINTR);
  616. errno = error;
  617. sprintf(MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror(errno));
  618. LOG_ERROR(MsgBuffer);
  619. return false;
  620. };
  621. do
  622. {
  623. result = tcsetattr(m_iInFID, TCSANOW, &m_sTTY);
  624. }
  625. while (result == -1 && errno == EINTR);
  626. if (result == -1)
  627. {
  628. const int error = errno;
  629. do
  630. {
  631. result = close(m_iInFID);
  632. }
  633. while (result == -1 && errno == EINTR);
  634. errno = error;
  635. sprintf(MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror(errno));
  636. LOG_ERROR(MsgBuffer);
  637. return false;
  638. };
  639. #else // Linux
  640. m_ioFID = CreateFile((LPCSTR) m_sDevicePath, GENERIC_READ | GENERIC_WRITE, 0UL, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING,
  641. // FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
  642. NULL);
  643. if (m_ioFID == INVALID_HANDLE_VALUE)
  644. {
  645. setConnectionStatus(CS_DISCONNECTED);
  646. sprintf(MsgBuffer, "Port [%s] Open Failed Err: %d", m_sDevicePath, GetLastError());
  647. LOG_ERROR(MsgBuffer);
  648. return false;
  649. };
  650. // Initialize the DCBlength member.
  651. PortDCB.DCBlength = sizeof(DCB);
  652. // Get the default port setting information.
  653. GetCommState(m_ioFID, &PortDCB);
  654. // Change the DCB structure settings
  655. /*
  656. PortDCB.fBinary = TRUE; // Binary mode; no EOF check
  657. PortDCB.fParity = NOPARITY; // Enable parity checking
  658. PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
  659. PortDCB.fErrorChar = FALSE; // Disable error replacement
  660. PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
  661. PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error
  662. PortDCB.fNull = FALSE; // Disable null stripping
  663. PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
  664. PortDCB.BaudRate = Speed;
  665. PortDCB.StopBits = ONESTOPBIT;
  666. */
  667. PortDCB.BaudRate = Speed;
  668. PortDCB.ByteSize = 8;
  669. PortDCB.Parity = NOPARITY;
  670. PortDCB.StopBits = ONESTOPBIT;
  671. PortDCB.EvtChar = 0;
  672. PortDCB.fParity = (PortDCB.Parity != NOPARITY);
  673. PortDCB.fOutxCtsFlow = FALSE;
  674. PortDCB.fOutxDsrFlow = FALSE;
  675. PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
  676. PortDCB.fDsrSensitivity = FALSE;
  677. PortDCB.fOutX = FALSE;
  678. PortDCB.fInX = FALSE;
  679. PortDCB.fErrorChar = FALSE;
  680. PortDCB.fNull = FALSE;
  681. // dcb.fRtsControl = RTS_CONTROL_DISABLE;
  682. PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
  683. // dcb.fRtsControl = RTS_CONTROL_TOGGLE; // specially for PROMAG USB->RS485 converter
  684. PortDCB.fAbortOnError = FALSE;
  685. PortDCB.fBinary = TRUE;
  686. PortDCB.fTXContinueOnXoff = TRUE;
  687. // Re-configure the port with the new DCB structure.
  688. if (! SetCommState(m_ioFID, &PortDCB))
  689. {
  690. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommState Failed)", m_sDevicePath);
  691. LOG_ERROR(MsgBuffer);
  692. CloseHandle(m_ioFID);
  693. return false;
  694. };
  695. COMMTIMEOUTS CommTimeouts;
  696. // Retrieve the time-out parameters for all read and write operations
  697. GetCommTimeouts(m_ioFID, &CommTimeouts);
  698. /*
  699. CommTimeouts.ReadIntervalTimeout = 20;
  700. CommTimeouts.ReadTotalTimeoutMultiplier = 10;
  701. CommTimeouts.ReadTotalTimeoutConstant = 100;
  702. CommTimeouts.WriteTotalTimeoutMultiplier = 10;
  703. CommTimeouts.WriteTotalTimeoutConstant = 100;
  704. */
  705. CommTimeouts.ReadIntervalTimeout = 20UL;
  706. CommTimeouts.ReadTotalTimeoutMultiplier = 1UL;
  707. CommTimeouts.ReadTotalTimeoutConstant = 5UL;
  708. CommTimeouts.WriteTotalTimeoutMultiplier = 1UL;
  709. CommTimeouts.WriteTotalTimeoutConstant = 1UL;
  710. // Set the time-out parameters for all read and write operations on the port.
  711. if (! SetCommTimeouts(m_ioFID, &CommTimeouts))
  712. {
  713. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommTimeouts Failed)", m_sDevicePath);
  714. LOG_ERROR(MsgBuffer);
  715. CloseHandle(m_ioFID);
  716. return false;
  717. };
  718. // Clear the port of any existing data.
  719. if (PurgeComm(m_ioFID, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT) == 0)
  720. {
  721. sprintf(MsgBuffer, "Port [%s] Clearing The Port Failed", m_sDevicePath);
  722. LOG_ERROR(MsgBuffer);
  723. CloseHandle(m_ioFID);
  724. return false;
  725. };
  726. EscapeCommFunction(m_ioFID, SETRTS);
  727. EscapeCommFunction(m_ioFID, SETDTR);
  728. // Set DataTerminalReady 1
  729. #endif
  730. setConnectionStatus(CS_CONNECTED);
  731. #ifdef FIO_SELECT_MODE
  732. FD_SET(m_iInFID, &m_sReaderSet);
  733. if (m_nReaderSetMax < m_iInFID)
  734. {
  735. m_nReaderSetMax = m_iInFID;
  736. }
  737. #endif
  738. if (init())
  739. {
  740. #ifdef DEBUG
  741. sprintf(MsgBuffer, "Init %s [%d] OK", m_sUartData.path, m_sUartData.speed);
  742. LOG(MsgBuffer);
  743. #endif
  744. return true;
  745. }
  746. else
  747. {
  748. LOG_ERROR("HW INIT Failure!");
  749. };
  750. return false;
  751. }
  752. bool hardwareResourceSerialProvider::closePort()
  753. {
  754. //char tBuff[128];
  755. setConnectionStatus(CS_DISCONNECTED);
  756. #ifdef FIO_SELECT_MODE
  757. if (m_iControlPipeFD[0] > -1)
  758. {
  759. write(m_iControlPipeFD[0], "E", 1);
  760. }
  761. if (m_iControlPipeFD[1] > -1)
  762. {
  763. write(m_iControlPipeFD[1], "E", 1);
  764. }
  765. #endif
  766. #ifndef HALF_DUPLEX
  767. if (m_bThreadAStatus != TH_PRE_INIT && m_bThreadBStatus != TH_PRE_INIT)
  768. {
  769. uninit();
  770. };
  771. #else
  772. if (m_bThreadStatus != TH_PRE_INIT)
  773. {
  774. uninit();
  775. };
  776. #endif
  777. #ifdef FIO_SELECT_MODE
  778. if (m_iControlPipeFD[0] > -1)
  779. {
  780. close(m_iControlPipeFD[0]);
  781. }
  782. if (m_iControlPipeFD[1] > -1)
  783. {
  784. close(m_iControlPipeFD[1]);
  785. }
  786. #endif
  787. switch (m_sUartData.type)
  788. {
  789. case HARDWARE_UART:
  790. {
  791. break;
  792. };
  793. case HARDWARE_IP_UART:
  794. {
  795. if (m_iDeviceFD != -1)
  796. {
  797. switch (m_sUartData.mode)
  798. {
  799. case SOL_UDP_CLIENT:
  800. case SOL_TCP_CLIENT:
  801. case SOL_UDP_SERVER:
  802. {
  803. #ifdef __linux__
  804. close(m_iDeviceFD);
  805. #else
  806. // When your application is finished receiving datagrams close the socket.
  807. LOG("Server: Finished receiving.Closing the listening socket...");
  808. if (closesocket(m_sSocket) != 0)
  809. {
  810. sprintf(tBuff, "closesocket() failed!Error code : %ld", WSAGetLastError());
  811. LOG_ERROR(tBuff);
  812. }
  813. else
  814. {
  815. LOG("closesocket() is OK...");
  816. };
  817. // When your application is finished call WSACleanup.
  818. LOG("Server: Cleaning up...");
  819. if (WSACleanup() != 0)
  820. {
  821. sprintf(tBuff, "WSACleanup() failed!Error code : %ld", WSAGetLastError());
  822. LOG_ERROR(tBuff);
  823. }
  824. else
  825. {
  826. LOG("WSACleanup() is OK");
  827. };
  828. #endif
  829. break;
  830. };
  831. case SOL_TCP_SERVER:
  832. {
  833. close(m_iDeviceFD);
  834. break;
  835. };
  836. };
  837. m_iDeviceFD = -1;
  838. };
  839. break;
  840. };
  841. };
  842. return true;
  843. }
  844. #ifndef HALF_DUPLEX
  845. void hardwareResourceSerialProvider::m_fThreadAProc(void)
  846. {
  847. int index;
  848. int RetVal;
  849. #ifdef FIO_SELECT_MODE
  850. int iActiveFDS;
  851. fd_set sWorkingSet;
  852. struct timeval sTimeOut;
  853. #endif
  854. m_bThreadAStatus = TH_WORKING;
  855. #ifdef DEBUG
  856. pthread_setname_np( pthread_self(), "UART Sender" );
  857. #endif
  858. while (m_bThreadAStatus < TH_ENDED)
  859. {
  860. #ifdef FIO_SELECT_MODE
  861. sTimeOut.tv_sec = 5;
  862. sTimeOut.tv_usec = 0;
  863. memcpy(&sWorkingSet, &m_sSenderSet, sizeof(m_sSenderSet));
  864. iActiveFDS = select(m_nSenderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut);
  865. if (iActiveFDS == 0)
  866. {
  867. }
  868. else if (iActiveFDS == -1)
  869. {
  870. // m_bThreadBStatus = TH_ENDED;
  871. m_bThreadAStatus = TH_ENDED;
  872. LOG_ERROR("Failure during Select[s]!");
  873. }
  874. else
  875. {
  876. for (int i = 0; i <= m_nSenderSetMax && iActiveFDS > 0; i++)
  877. {
  878. if (FD_ISSET(i, &sWorkingSet))
  879. {
  880. iActiveFDS--;
  881. // Successful
  882. if (i == m_iControlPipeFD[0])
  883. {
  884. char buffer;
  885. RetVal = read(i, &buffer, 1);
  886. if (buffer == 'A')
  887. {
  888. #endif
  889. m_mBufferProtectorMutex.lock();
  890. if (m_nOutputBufferSize > 0)
  891. {
  892. index = 0;
  893. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED))
  894. {
  895. #ifdef DEBUG_EXTRA
  896. char TT[64];
  897. sprintf(TT, "Have %d Bytes to send!", m_nOutputBufferSize);
  898. LOG(TT);
  899. #endif
  900. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  901. if (RetVal > 0)
  902. {
  903. index += RetVal;
  904. }
  905. else
  906. {
  907. // if 0 or -(error)
  908. if (RetVal == 0)
  909. {
  910. usleep(100);
  911. }
  912. else
  913. {
  914. setConnectionStatus(CS_DISCONNECTED);
  915. m_nOutputBufferSize = 0;
  916. // m_bThreadBStatus = TH_ENDED;
  917. m_bThreadAStatus = TH_ENDED;
  918. LOG_ERROR("Connection Droped on Sending Data");
  919. };
  920. };
  921. };
  922. m_nOutputBufferSize = 0;
  923. m_mBufferProtectorMutex.unlock();
  924. #ifndef FIO_SELECT_MODE
  925. }
  926. else
  927. {
  928. m_mBufferProtectorMutex.unlock();
  929. SLEEP(5);
  930. #endif
  931. };
  932. #ifdef FIO_SELECT_MODE
  933. };
  934. if (buffer == 'E')
  935. {
  936. // m_bThreadBStatus = TH_ENDED;
  937. m_bThreadAStatus = TH_ENDED;
  938. LOG("Requesting Destruction of Transport[s]!");
  939. };
  940. };
  941. };
  942. };
  943. };
  944. #endif
  945. };
  946. }
  947. void hardwareResourceSerialProvider::m_fThreadBProc(void)
  948. {
  949. int RetVal;
  950. #ifdef FIO_SELECT_MODE
  951. int iActiveFDS;
  952. fd_set sWorkingSet;
  953. struct timeval sTimeOut;
  954. #endif
  955. m_bThreadBStatus = TH_WORKING;
  956. #ifdef DEBUG
  957. pthread_setname_np(pthread_self(), "UART Receiver");
  958. #endif
  959. while (m_bThreadBStatus < TH_ENDED)
  960. {
  961. #ifdef FIO_SELECT_MODE
  962. sTimeOut.tv_sec = 5;
  963. sTimeOut.tv_usec = 0;
  964. memcpy(&sWorkingSet, &m_sReaderSet, sizeof(m_sReaderSet));
  965. iActiveFDS = select(m_nReaderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut);
  966. if (iActiveFDS == 0)
  967. {
  968. }
  969. else if (iActiveFDS == -1)
  970. {
  971. m_bThreadBStatus = TH_ENDED;
  972. // m_bThreadAStatus = TH_ENDED;
  973. LOG_ERROR("Failure during Select[r]!");
  974. }
  975. else
  976. {
  977. for (int i = 0; i <= m_nReaderSetMax && iActiveFDS > 0; i++)
  978. {
  979. if (FD_ISSET(i, &sWorkingSet))
  980. {
  981. if (i == m_iControlPipeFD[1])
  982. {
  983. char buffer;
  984. RetVal = read(i, &buffer, 1);
  985. if (buffer == 'E')
  986. {
  987. m_bThreadBStatus = TH_ENDED;
  988. LOG("Requesting Destruction of Transport[r]!");
  989. };
  990. }
  991. else
  992. {
  993. #endif
  994. RetVal = recptBlock(m_pInputBuffer, &m_nInputBufferSize);
  995. if (RetVal == -1)
  996. {
  997. setConnectionStatus(CS_DISCONNECTED);
  998. m_nInputBufferSize = 0;
  999. m_bThreadBStatus = TH_ENDED;
  1000. #ifdef DEBUG_EXTRA
  1001. LOG("Connection Droped on Reception Data");
  1002. #endif
  1003. }
  1004. else if (m_nInputBufferSize > 0)
  1005. {
  1006. do
  1007. {
  1008. m_mQueueProtectorMutex.lock();
  1009. if (m_vParsers.size())
  1010. {
  1011. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++)
  1012. {
  1013. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  1014. }
  1015. m_nInputBufferSize = 0;
  1016. m_mQueueProtectorMutex.unlock();
  1017. }
  1018. else
  1019. {
  1020. #ifdef DEBUG_EXTRA
  1021. LOG(m_sUartData.path);
  1022. LOG((char *) m_pInputBuffer);
  1023. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  1024. #endif
  1025. m_nInputBufferSize = 0;
  1026. m_mQueueProtectorMutex.unlock();
  1027. SLEEP(100);
  1028. };
  1029. }
  1030. while ((m_vParsers.size() == 0) /*&& ( m_bThreadAStatus < TH_ENDED )*/ && (m_bThreadBStatus < TH_ENDED));
  1031. #ifndef FIO_SELECT_MODE
  1032. }
  1033. else
  1034. {
  1035. SLEEP(5);
  1036. #endif
  1037. };
  1038. #ifdef FIO_SELECT_MODE
  1039. };
  1040. };
  1041. };
  1042. };
  1043. #endif
  1044. };
  1045. }
  1046. bool hardwareResourceSerialProvider::sendData(uint8_t * Data, int DataSize)
  1047. {
  1048. #ifndef HALF_DUPLEX
  1049. if (m_bThreadAStatus != TH_WORKING)
  1050. {
  1051. if (m_bThreadAStatus < TH_WORKING)
  1052. {
  1053. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  1054. }
  1055. else
  1056. {
  1057. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  1058. }
  1059. return false;
  1060. };
  1061. #else
  1062. if (m_bThreadStatus != TH_WORKING)
  1063. {
  1064. if (m_bThreadStatus < TH_WORKING)
  1065. {
  1066. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  1067. }
  1068. else
  1069. {
  1070. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  1071. }
  1072. return false;
  1073. };
  1074. #endif
  1075. m_mBufferProtectorMutex.lock();
  1076. if (m_nOutputBufferSize + DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH)
  1077. {
  1078. m_mBufferProtectorMutex.unlock();
  1079. LOG_ERROR("Data Too BIG");
  1080. return false;
  1081. };
  1082. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  1083. m_nOutputBufferSize += DataSize;
  1084. m_mBufferProtectorMutex.unlock();
  1085. #ifdef FIO_SELECT_MODE
  1086. if (m_iControlPipeFD[1] > -1)
  1087. {
  1088. write(m_iControlPipeFD[1], "A", 1);
  1089. }
  1090. #endif
  1091. return true;
  1092. }
  1093. #else // HALF_DUPLEX
  1094. void hardwareResourceSerialProvider::m_fThreadProc(void)
  1095. {
  1096. int index;
  1097. int RetVal;
  1098. m_bThreadStatus = TH_WORKING;
  1099. #ifdef DEBUG_EXTRA
  1100. pthread_setname_np(pthread_self(), "UART HalfDuplex");
  1101. #endif
  1102. while (m_bThreadStatus < TH_ENDED)
  1103. {
  1104. m_mBufferProtectorMutex.lock();
  1105. if (! m_qIO.empty())
  1106. {
  1107. SIO_TZ * tIOData;
  1108. tIOData = m_qIO.front();
  1109. m_mBufferProtectorMutex.unlock();
  1110. if (tIOData)
  1111. {
  1112. // Remove from Queue
  1113. m_mBufferProtectorMutex.lock();
  1114. m_qIO.pop();
  1115. m_mBufferProtectorMutex.unlock();
  1116. index = 0;
  1117. while ((index < tIOData->out_size) && (m_bThreadStatus < TH_ENDED))
  1118. {
  1119. #ifdef DEBUG_EXTRA
  1120. char TT[64];
  1121. sprintf(TT, "Have %d Bytes to send!", tIOData->out_size - index);
  1122. LOG(TT);
  1123. #endif
  1124. RetVal = sendBlock(tIOData->out_buffer + index, tIOData->out_size - index);
  1125. if (RetVal > 0)
  1126. {
  1127. index += RetVal;
  1128. }
  1129. else
  1130. {
  1131. // if 0 or -(error)
  1132. if (RetVal == 0)
  1133. {
  1134. usleep(100);
  1135. }
  1136. else
  1137. {
  1138. setConnectionStatus(CS_DISCONNECTED);
  1139. // m_nOutputBufferSize = 0;
  1140. tIOData->out_size = 0;
  1141. m_bThreadStatus = TH_ENDED;
  1142. LOG_ERROR("Connection Droped on Sending Data");
  1143. };
  1144. };
  1145. };
  1146. tIOData->out_size = 0;
  1147. bool tDataOnline = false;
  1148. uint8_t tTOCounter = 20;
  1149. do
  1150. {
  1151. RetVal = recptBlock(tIOData->inp_buffer, &tIOData->inp_size);
  1152. if (RetVal == -1)
  1153. {
  1154. setConnectionStatus(CS_DISCONNECTED);
  1155. tIOData->inp_size = 0;
  1156. m_bThreadStatus = TH_ENDED;
  1157. LOG_ERROR("Connection Droped on Reception Data");
  1158. }
  1159. else if (tIOData->inp_size)
  1160. {
  1161. tDataOnline = true;
  1162. do
  1163. {
  1164. m_mQueueProtectorMutex.lock();
  1165. if (m_vParsers.size())
  1166. {
  1167. for (parseQueue::iterator idx = m_vParsers.begin(); idx != m_vParsers.end(); idx++)
  1168. {
  1169. (*idx)->parse(tIOData->inp_buffer, tIOData->inp_size);
  1170. }
  1171. tIOData->inp_size = 0;
  1172. m_mQueueProtectorMutex.unlock();
  1173. }
  1174. else
  1175. {
  1176. #ifdef DEBUG_EXTRA
  1177. LOG(m_sUartData.path);
  1178. LOG((char *) tIOData->inp_buffer);
  1179. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  1180. #endif
  1181. tIOData->inp_size = 0;
  1182. m_mQueueProtectorMutex.unlock();
  1183. SLEEP(100);
  1184. };
  1185. }
  1186. while ((m_vParsers.size() == 0) && (m_bThreadStatus < TH_ENDED));
  1187. }
  1188. else
  1189. {
  1190. if (tTOCounter)
  1191. {
  1192. tTOCounter--;
  1193. SLEEP(5);
  1194. #ifdef DEBUG
  1195. }
  1196. else
  1197. {
  1198. LOG_ERROR("PACK TIMEOUT!");
  1199. #endif
  1200. };
  1201. };
  1202. }
  1203. while ((m_bThreadStatus < TH_ENDED) && tTOCounter && ! (tDataOnline && RetVal == 0)); // Pack1 conditions -> White Space after Data.
  1204. // Free Package
  1205. // LOG("Del Pack");
  1206. free(tIOData);
  1207. }
  1208. else
  1209. {
  1210. // Mutex already unlocked!
  1211. SLEEP(5);
  1212. };
  1213. }
  1214. else
  1215. {
  1216. m_mBufferProtectorMutex.unlock();
  1217. SLEEP(5);
  1218. };
  1219. };
  1220. }
  1221. bool hardwareResourceSerialProvider::sendData(uint8_t * Data, int DataSize)
  1222. {
  1223. if (m_bThreadStatus != TH_WORKING)
  1224. {
  1225. if (m_bThreadStatus < TH_WORKING)
  1226. {
  1227. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  1228. }
  1229. else
  1230. {
  1231. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  1232. }
  1233. return false;
  1234. };
  1235. if (DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH)
  1236. {
  1237. LOG_ERROR("Data Too BIG");
  1238. }
  1239. else
  1240. {
  1241. SIO_TZ * tData = (SIO_TZ *) malloc(sizeof(SIO_TZ));
  1242. if (tData)
  1243. {
  1244. memset(tData, 0, sizeof(SIO_TZ));
  1245. tData->out_size = DataSize;
  1246. memcpy(tData->out_buffer, Data, DataSize);
  1247. m_mBufferProtectorMutex.lock();
  1248. m_qIO.push(tData);
  1249. m_mBufferProtectorMutex.unlock();
  1250. return true;
  1251. }
  1252. else
  1253. {
  1254. LOG_ERROR("MemAlloc Error!");
  1255. };
  1256. };
  1257. return false;
  1258. }
  1259. #endif // Half Duplex
  1260. bool hardwareResourceSerialProvider::reconnect(void)
  1261. {
  1262. closePort();
  1263. switch (m_sUartData.type)
  1264. {
  1265. case HARDWARE_UART : m_bValid = create_HW(); break;
  1266. case HARDWARE_IP_UART: m_bValid = create_SOL(); break;
  1267. };
  1268. return m_bValid;
  1269. }
  1270. int hardwareResourceSerialProvider::recptBlock(unsigned char * Buffer, int * BufferLength)
  1271. {
  1272. #ifdef __linux__
  1273. int RetVal = 0;
  1274. #else
  1275. DWORD RetVal = 0;
  1276. #endif
  1277. if (m_eConnectionStatus == CS_CONNECTED)
  1278. {
  1279. switch (m_sUartData.type)
  1280. {
  1281. case HARDWARE_UART:
  1282. {
  1283. #ifdef __linux__
  1284. do
  1285. {
  1286. RetVal = read(m_iInFID, Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH);
  1287. #ifndef HALF_DUPLEX
  1288. if (RetVal == -1 && errno == EINTR && m_bThreadAStatus == TH_WORKING)
  1289. {
  1290. SLEEP(1);
  1291. }
  1292. }
  1293. while (RetVal == -1 && errno == EINTR && m_bThreadAStatus == TH_WORKING);
  1294. #else
  1295. if (RetVal == -1 && errno == EINTR && m_bThreadStatus == TH_WORKING)
  1296. {
  1297. SLEEP(1);
  1298. }
  1299. }
  1300. while (RetVal == -1 && errno == EINTR && m_bThreadStatus == TH_WORKING);
  1301. #endif
  1302. #else // Windows
  1303. OVERLAPPED osReader = {0};
  1304. if (! ReadFile(m_ioFID, Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH, &RetVal, &osReader))
  1305. {
  1306. if (GetLastError() != ERROR_IO_PENDING)
  1307. { // read not delayed?
  1308. char tBuffer[256];
  1309. sprintf(tBuffer, "Communication Error %d!", GetLastError());
  1310. LOG_ERROR(tBuffer);
  1311. // Error in communications; report it.
  1312. return -1;
  1313. #ifdef DEBUG_SERIAL_COMMUNICATION
  1314. }
  1315. else
  1316. {
  1317. // fWaitingOnRead = TRUE;
  1318. LOG("WREAD!");
  1319. #endif
  1320. };
  1321. #ifdef DEBUG_SERIAL_COMMUNICATION
  1322. }
  1323. else
  1324. {
  1325. // read completed immediately
  1326. LOG("IMREAD!");
  1327. #endif
  1328. };
  1329. #endif // Linux
  1330. break;
  1331. };
  1332. case HARDWARE_IP_UART:
  1333. { // SOL
  1334. #ifdef __linux__
  1335. if (m_iDeviceFD > -1)
  1336. {
  1337. struct sockaddr ClientAddress;
  1338. socklen_t addr_len = sizeof(ClientAddress); // len is value/resuslt
  1339. // RetVal = recvfrom( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_WAITALL, ( struct sockaddr *) &m_sClientAddr, &addr_len );
  1340. RetVal = recvfrom(m_iDeviceFD, (char *) Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH, MSG_DONTWAIT, (struct sockaddr *) &ClientAddress, &addr_len);
  1341. if (RetVal < 1)
  1342. {
  1343. if (errno != EAGAIN && errno != EINTR)
  1344. {
  1345. LOG_ERROR("Peer Closes Connection!");
  1346. shutdown(m_iDeviceFD, 2);
  1347. close(m_iDeviceFD);
  1348. *BufferLength = 0;
  1349. m_iDeviceFD = -1;
  1350. setConnectionStatus(CS_DISCONNECTED);
  1351. return -1;
  1352. };
  1353. };
  1354. }
  1355. else
  1356. {
  1357. LOG_ERROR("Not Connected!");
  1358. *BufferLength = 0;
  1359. setConnectionStatus(CS_DISCONNECTED);
  1360. return -1;
  1361. };
  1362. #else
  1363. switch (m_eType)
  1364. {
  1365. case SOL_UDP_CLIENT:
  1366. case SOL_TCP_CLIENT:
  1367. case SOL_UDP_SERVER:
  1368. {
  1369. SOCKADDR_IN tAddr;
  1370. int tLen = sizeof(SOCKADDR_IN);
  1371. RetVal = recvfrom(m_sSocket, (char *) Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH, 0, (struct sockaddr *) &tAddr, &tLen);
  1372. if (RetVal == SOCKET_ERROR)
  1373. {
  1374. char cBuffer[165];
  1375. sprintf(cBuffer, "recvfrom() failed with error code : %d", WSAGetLastError());
  1376. LOG_ERROR(cBuffer);
  1377. };
  1378. break;
  1379. };
  1380. };
  1381. #endif
  1382. break;
  1383. }; // End of SOL
  1384. };
  1385. }
  1386. else
  1387. {
  1388. if (m_sUartData.type == HARDWARE_IP_UART)
  1389. {
  1390. // make prep for connection SOL_TCP_SERVER
  1391. if (m_sUartData.mode == SOL_TCP_CLIENT)
  1392. {
  1393. // ACCEPT
  1394. struct sockaddr ClientAddress;
  1395. socklen_t size = (socklen_t) sizeof(struct sockaddr_in);
  1396. // int ClientSocket;
  1397. m_iDeviceFD = accept(m_iMasterFD, &ClientAddress, &size);
  1398. if (m_iDeviceFD < 0)
  1399. {
  1400. setConnectionStatus(CS_DISCONNECTED);
  1401. LOG_ERROR("Connection Accept Failure!");
  1402. return -1;
  1403. };
  1404. }
  1405. else
  1406. {
  1407. LOG_ERROR("RCPT ON not connected!");
  1408. shutdown(m_iDeviceFD, 2);
  1409. close(m_iDeviceFD);
  1410. *BufferLength = 0;
  1411. m_iDeviceFD = -1;
  1412. setConnectionStatus(CS_DISCONNECTED);
  1413. return -1;
  1414. };
  1415. #ifdef DEBUG_EXTRA
  1416. }
  1417. else
  1418. {
  1419. LOG("Port is not Ready to Receive");
  1420. #endif
  1421. };
  1422. return -1; // Exit after unsuccess reconnect
  1423. };
  1424. if (RetVal > 0)
  1425. {
  1426. #ifdef DEBUG_SERIAL_COMMUNICATION
  1427. char MsgBuffer[CLOG];
  1428. if (RetVal > 1 || RetVal == 0)
  1429. {
  1430. sprintf(MsgBuffer, "Received %d bytes", RetVal);
  1431. }
  1432. else
  1433. {
  1434. sprintf(MsgBuffer, "Received 1 byte");
  1435. };
  1436. LOG(MsgBuffer);
  1437. for (int i = 0; i < RetVal; i++)
  1438. {
  1439. sprintf(MsgBuffer, "<0x%02X", Buffer[i]);
  1440. LOG(MsgBuffer);
  1441. };
  1442. #endif
  1443. *BufferLength = RetVal;
  1444. return RetVal;
  1445. };
  1446. *BufferLength = 0;
  1447. if (RetVal == -1)
  1448. {
  1449. return 0;
  1450. }
  1451. if (RetVal == 0)
  1452. {
  1453. errno = 0;
  1454. return 0;
  1455. };
  1456. /* r < -1, should never happen. */
  1457. errno = EIO;
  1458. return 0;
  1459. }
  1460. void hardwareResourceSerialProvider::setRTS(uint8_t Val)
  1461. {
  1462. #if defined(USE_WIRINGPI) || defined(USE_WIRINGX)
  1463. if (Val)
  1464. {
  1465. // Use RTS GPIO
  1466. digitalWrite(RTS_PIN_NUMBER, HIGH);
  1467. }
  1468. else
  1469. {
  1470. digitalWrite(RTS_PIN_NUMBER, LOW);
  1471. };
  1472. #else
  1473. int flags;
  1474. if (Val)
  1475. {
  1476. ioctl(m_iInFID, TIOCMGET, &flags);
  1477. flags |= TIOCM_RTS;
  1478. ioctl(m_iInFID, TIOCMSET, &flags);
  1479. ioctl(m_iOutFID, TIOCMGET, &flags);
  1480. flags |= TIOCM_RTS;
  1481. ioctl(m_iOutFID, TIOCMSET, &flags);
  1482. }
  1483. else
  1484. {
  1485. ioctl(m_iInFID, TIOCMGET, &flags);
  1486. flags &= ~TIOCM_RTS;
  1487. ioctl(m_iInFID, TIOCMSET, &flags);
  1488. ioctl(m_iOutFID, TIOCMGET, &flags);
  1489. flags &= ~TIOCM_RTS;
  1490. ioctl(m_iOutFID, TIOCMSET, &flags);
  1491. };
  1492. #endif
  1493. }
  1494. #include <sys/ioctl.h>
  1495. int hardwareResourceSerialProvider::sendBlock(unsigned char * Data, int DataSize)
  1496. {
  1497. #ifdef __linux__
  1498. int RetVal = 0;
  1499. #else
  1500. DWORD RetVal = 0;
  1501. #endif
  1502. if (m_eConnectionStatus == CS_CONNECTED)
  1503. {
  1504. switch (m_sUartData.type)
  1505. {
  1506. case HARDWARE_UART:
  1507. {
  1508. #ifdef __linux__
  1509. if (m_bManualRTS)
  1510. {
  1511. setRTS(1);
  1512. };
  1513. do
  1514. {
  1515. if (m_bManualRTS)
  1516. {
  1517. // #if defined( USE_WIRINGPI ) || defined( USE_WIRINGX )
  1518. // digitalWrite( RTS_PIN_NUMBER, HIGH );
  1519. // #else
  1520. setRTS(1);
  1521. // #endif
  1522. };
  1523. RetVal = write(m_iOutFID, Data, DataSize);
  1524. if (m_bManualRTS)
  1525. {
  1526. uint8_t lsr;
  1527. do
  1528. {
  1529. //int r =
  1530. ioctl(m_iOutFID, TIOCSERGETLSR, &lsr);
  1531. }
  1532. while (! (lsr & TIOCSER_TEMT));
  1533. // #if defined( USE_WIRINGPI ) || defined( USE_WIRINGX )
  1534. // digitalWrite( RTS_PIN_NUMBER, LOW );
  1535. // #else
  1536. setRTS(0);
  1537. // #endif
  1538. };
  1539. if (RetVal == DataSize)
  1540. {
  1541. usleep(10);
  1542. };
  1543. #ifndef HALF_DUPLEX
  1544. }
  1545. while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK) && m_bThreadAStatus == TH_WORKING);
  1546. #else
  1547. }
  1548. while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK) && m_bThreadStatus == TH_WORKING);
  1549. #endif
  1550. // if( m_bManualRTS ) setRTS( 0 );
  1551. #else //!__linux__
  1552. WriteFile(m_ioFID, Data, DataSize, &RetVal, NULL);
  1553. #endif
  1554. #ifdef DEBUG_EXTRA
  1555. char MsgBuffer[CLOG];
  1556. sprintf(MsgBuffer, "Sent %d byte(s) of %d", RetVal, DataSize);
  1557. LOG(MsgBuffer);
  1558. #ifdef DEBUG_SERIAL_COMMUNICATION
  1559. for (int i = 0; i < DataSize; i++)
  1560. {
  1561. sprintf(MsgBuffer, ">0x%02X", Data[i]);
  1562. LOG(MsgBuffer);
  1563. };
  1564. #endif
  1565. #endif
  1566. break;
  1567. };
  1568. case HARDWARE_IP_UART:
  1569. {
  1570. #ifdef __linux__
  1571. if (m_iDeviceFD > -1)
  1572. {
  1573. #ifdef DEBUG_EXTRA
  1574. char cBuffer[256];
  1575. sprintf(cBuffer, "Write into Port %d bytes", DataSize);
  1576. LOG(cBuffer);
  1577. #endif
  1578. struct sockaddr ClientAddress;
  1579. socklen_t addr_len = sizeof(ClientAddress);
  1580. RetVal = sendto(m_iDeviceFD, (const char *) Data, DataSize, MSG_CONFIRM, (const struct sockaddr *) &ClientAddress, addr_len);
  1581. if ((RetVal == -1) && (errno != EINTR) && (errno != EWOULDBLOCK))
  1582. {
  1583. LOG_ERROR("Error acures while writing data into the Socket");
  1584. shutdown(m_iDeviceFD, 2);
  1585. close(m_iDeviceFD);
  1586. m_iDeviceFD = -1;
  1587. setConnectionStatus(CS_DISCONNECTED);
  1588. return -1;
  1589. };
  1590. if (RetVal < 0)
  1591. {
  1592. RetVal = 0;
  1593. }
  1594. #ifdef DEBUG_EXTRA
  1595. sprintf(cBuffer, "Bytes Sent: %ld", RetVal);
  1596. LOG(cBuffer);
  1597. #ifdef DEBUG_SERIAL_COMMUNICATION
  1598. for (int zz = 0; zz < RetVal; zz++)
  1599. {
  1600. sprintf(cBuffer, "SD: 0x%02X", (uint8_t) Data[zz]);
  1601. LOG(cBuffer);
  1602. };
  1603. #endif
  1604. #endif
  1605. }
  1606. else
  1607. {
  1608. LOG_ERROR("Not Connected!");
  1609. // m_bConnected = false;
  1610. setConnectionStatus(CS_DISCONNECTED);
  1611. return -1;
  1612. };
  1613. #else // Windows
  1614. if (sendto(m_sSocket, (char *) Data, DataSize, 0, (struct sockaddr *) &ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR)
  1615. {
  1616. char cBuffer[256];
  1617. sprintf(cBuffer, "sendto() failed with error code : %d", WSAGetLastError());
  1618. LOG_ERROR(cBuffer);
  1619. return -1;
  1620. };
  1621. #endif
  1622. break;
  1623. };
  1624. };
  1625. }
  1626. else
  1627. {
  1628. #ifdef DEBUG_EXTRA
  1629. LOG("Port is not Ready to Send");
  1630. #endif
  1631. return -1;
  1632. };
  1633. return RetVal;
  1634. }
  1635. #undef LOG
  1636. #undef LOG_ERROR
  1637. // #undef DEBUG_SERIAL_COMMUNICATION
  1638. // #undef DEBUG_EXTRA