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.

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