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.

1726 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)
  418. {
  419. LOG_ERROR("TRYING TO CONNECT ON OPENED PORT!");
  420. return false;
  421. };
  422. // if( m_bInitComplete ) {
  423. // LOG_ERROR( "TRYING TO CREATE CREATED PORT!" );
  424. // return false;
  425. // };
  426. #ifdef __linux__
  427. int result;
  428. /* Open File Descriptor */
  429. // m_iFID = open( DevicePath, O_RDWR| O_NONBLOCK | O_NDELAY );
  430. #ifdef __arm__
  431. LOG("ARM Defined!");
  432. #endif
  433. #ifdef RASPI_HARDWARE
  434. LOG("RASPI_HARDWARE Defined!");
  435. #endif
  436. #ifdef __aarch64__
  437. LOG("AARCH64 Defined!");
  438. #endif
  439. #ifdef USE_WIRINGX
  440. LOG("USE_WIRINGX Defined!");
  441. LOG(WIRINGX_PLATFORM);
  442. #endif
  443. #ifdef USE_WIRINGPI
  444. LOG("USE_WIRINGPI Defined!");
  445. if (strstr(m_sUartData.path, "USB") == NULL)
  446. {
  447. #ifdef DEBUG
  448. LOG("MANUAL RTS CTL(R)");
  449. #endif
  450. m_bManualRTS = true;
  451. wiringPiSetup();
  452. // Set RTS Pin
  453. pinMode(RTS_PIN_NUMBER, OUTPUT);
  454. }
  455. else
  456. {
  457. #ifdef DEBUG
  458. LOG("NO MANUAL RTS CTL(R)");
  459. #endif
  460. m_bManualRTS = false;
  461. };
  462. #else
  463. #ifdef USE_WIRINGX
  464. if (strstr(m_sUartData.path, "USB") == NULL)
  465. {
  466. #ifdef DEBUG
  467. LOG("MANUAL RTS CTL(X)");
  468. #endif
  469. if (wiringXSetup(WIRINGX_PLATFORM, NULL) == -1)
  470. {
  471. LOG_ERROR("WiringX Platform Init Failure!");
  472. m_bManualRTS = false;
  473. }
  474. else
  475. {
  476. // Set RTS Pin
  477. // pinMode( 0, OUTPUT );
  478. pinMode(RTS_PIN_NUMBER, PINMODE_OUTPUT);
  479. m_bManualRTS = true;
  480. };
  481. }
  482. else
  483. {
  484. #ifdef DEBUG
  485. LOG("NO MANUAL RTS CTL(X)");
  486. #endif
  487. m_bManualRTS = false;
  488. };
  489. #else
  490. #ifdef DEBUG
  491. LOG("NO MANUAL RTS CTL(INTEL)");
  492. #endif
  493. m_bManualRTS = false;
  494. #endif
  495. #endif
  496. do
  497. {
  498. m_iInFID = open(m_sUartData.path, O_RDONLY | O_NONBLOCK | O_NDELAY);
  499. }
  500. while (m_iInFID == -1 && errno == EINTR);
  501. if (m_iInFID == -1)
  502. {
  503. LOG_ERROR("Can not open Input Stream");
  504. return false;
  505. };
  506. do
  507. {
  508. result = fcntl(m_iInFID, F_SETFL, O_NONBLOCK);
  509. }
  510. while (result == -1 && errno == EINTR);
  511. if (result == -1)
  512. {
  513. const int error = errno;
  514. do
  515. {
  516. result = close(m_iInFID);
  517. }
  518. while (result == -1 && errno == EINTR);
  519. errno = error;
  520. sprintf(MsgBuffer, "Error %d openning port: %s", errno, strerror(errno));
  521. LOG_ERROR(MsgBuffer);
  522. return false;
  523. };
  524. do
  525. {
  526. m_iOutFID = open(m_sUartData.path, O_WRONLY | O_NDELAY);
  527. }
  528. while (m_iOutFID == -1 && errno == EINTR);
  529. if (m_iOutFID == -1)
  530. {
  531. LOG_ERROR("Can not open Output Stream");
  532. return false;
  533. };
  534. /* *** Configure Port *** */
  535. memset(&m_sTTY, 0, sizeof(m_sTTY));
  536. /* Error Handling */
  537. do
  538. {
  539. result = tcgetattr(m_iInFID, &m_sTTY);
  540. }
  541. while (result == -1 && errno == EINTR);
  542. if (result == -1)
  543. {
  544. const int error = errno;
  545. do
  546. {
  547. result = close(m_iInFID);
  548. }
  549. while (result == -1 && errno == EINTR);
  550. errno = error;
  551. sprintf(MsgBuffer, "Error %d from tcgetattr: %s", errno, strerror(errno));
  552. LOG_ERROR(MsgBuffer);
  553. return false;
  554. };
  555. /* Set Baud Rate */
  556. int RetVal = 0;
  557. switch (m_sUartData.speed)
  558. {
  559. case 4000000: cfsetspeed(&m_sTTY, B4000000); break;
  560. case 3500000: cfsetspeed(&m_sTTY, B3500000); break;
  561. case 3000000: cfsetspeed(&m_sTTY, B3000000); break;
  562. case 2500000: cfsetspeed(&m_sTTY, B2500000); break;
  563. case 2000000: cfsetspeed(&m_sTTY, B2000000); break;
  564. case 1500000: cfsetspeed(&m_sTTY, B1500000); break;
  565. case 1152000: cfsetspeed(&m_sTTY, B1152000); break;
  566. case 1000000: cfsetspeed(&m_sTTY, B1000000); break;
  567. case 921600 : cfsetspeed(&m_sTTY, B921600); break;
  568. case 460800 : cfsetspeed(&m_sTTY, B460800); break;
  569. case 230400 : cfsetspeed(&m_sTTY, B230400); break;
  570. case 115200 : cfsetspeed(&m_sTTY, B115200); break;
  571. case 19200 : cfsetspeed(&m_sTTY, B19200); break;
  572. case 9600 : cfsetspeed(&m_sTTY, B9600); break;
  573. default:
  574. {
  575. close(m_iInFID);
  576. close(m_iOutFID);
  577. sprintf(MsgBuffer, "Error cannot set speed as [%d] bod", m_sUartData.speed);
  578. LOG_ERROR(MsgBuffer);
  579. return false;
  580. };
  581. };
  582. if (RetVal == 0)
  583. {
  584. #ifdef DEBUG
  585. sprintf(MsgBuffer, "speed set to [%d] bod", m_sUartData.speed);
  586. LOG(MsgBuffer);
  587. #endif
  588. }
  589. else
  590. {
  591. RetVal = cfgetispeed(&m_sTTY);
  592. sprintf(MsgBuffer, "Error cannot set speed as [%d] bod. Current is: %d bod. ", m_sUartData.speed, RetVal);
  593. LOG_ERROR(MsgBuffer);
  594. return false;
  595. };
  596. /* Setting other Port Stuff */
  597. m_sTTY.c_cflag &= ~PARENB; // Make 8n1
  598. m_sTTY.c_cflag &= ~CSTOPB;
  599. m_sTTY.c_cflag &= ~CSIZE;
  600. m_sTTY.c_cflag |= CS8;
  601. m_sTTY.c_cflag &= ~CRTSCTS; // no flow control
  602. //
  603. m_sTTY.c_iflag &= ~(INLCR | ICRNL); // fixing 0x0D->0x0A
  604. //
  605. m_sTTY.c_lflag = 0; // no signaling chars, no echo, no canonical processing
  606. m_sTTY.c_oflag = 0; // no remapping, no delays
  607. m_sTTY.c_cc[VMIN] = 0; // read doesn't block
  608. m_sTTY.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  609. m_sTTY.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  610. m_sTTY.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  611. m_sTTY.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  612. m_sTTY.c_oflag &= ~OPOST; // make raw
  613. /* Flush Port, then applies attributes */
  614. tcflush(m_iOutFID, TCIFLUSH);
  615. do
  616. {
  617. result = tcsetattr(m_iOutFID, TCSANOW, &m_sTTY);
  618. }
  619. while (result == -1 && errno == EINTR);
  620. if (result == -1)
  621. {
  622. const int error = errno;
  623. do
  624. {
  625. result = close(m_iOutFID);
  626. }
  627. while (result == -1 && errno == EINTR);
  628. errno = error;
  629. sprintf(MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror(errno));
  630. LOG_ERROR(MsgBuffer);
  631. return false;
  632. };
  633. do
  634. {
  635. result = tcsetattr(m_iInFID, TCSANOW, &m_sTTY);
  636. }
  637. while (result == -1 && errno == EINTR);
  638. if (result == -1)
  639. {
  640. const int error = errno;
  641. do
  642. {
  643. result = close(m_iInFID);
  644. }
  645. while (result == -1 && errno == EINTR);
  646. errno = error;
  647. sprintf(MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror(errno));
  648. LOG_ERROR(MsgBuffer);
  649. return false;
  650. };
  651. #else // Linux
  652. m_ioFID = CreateFile((LPCSTR) m_sDevicePath, GENERIC_READ | GENERIC_WRITE, 0UL, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING,
  653. // FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
  654. NULL);
  655. if (m_ioFID == INVALID_HANDLE_VALUE)
  656. {
  657. setConnectionStatus(CS_DISCONNECTED);
  658. sprintf(MsgBuffer, "Port [%s] Open Failed Err: %d", m_sDevicePath, GetLastError());
  659. LOG_ERROR(MsgBuffer);
  660. return false;
  661. };
  662. // Initialize the DCBlength member.
  663. PortDCB.DCBlength = sizeof(DCB);
  664. // Get the default port setting information.
  665. GetCommState(m_ioFID, &PortDCB);
  666. // Change the DCB structure settings
  667. /*
  668. PortDCB.fBinary = TRUE; // Binary mode; no EOF check
  669. PortDCB.fParity = NOPARITY; // Enable parity checking
  670. PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
  671. PortDCB.fErrorChar = FALSE; // Disable error replacement
  672. PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
  673. PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error
  674. PortDCB.fNull = FALSE; // Disable null stripping
  675. PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
  676. PortDCB.BaudRate = Speed;
  677. PortDCB.StopBits = ONESTOPBIT;
  678. */
  679. PortDCB.BaudRate = Speed;
  680. PortDCB.ByteSize = 8;
  681. PortDCB.Parity = NOPARITY;
  682. PortDCB.StopBits = ONESTOPBIT;
  683. PortDCB.EvtChar = 0;
  684. PortDCB.fParity = (PortDCB.Parity != NOPARITY);
  685. PortDCB.fOutxCtsFlow = FALSE;
  686. PortDCB.fOutxDsrFlow = FALSE;
  687. PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
  688. PortDCB.fDsrSensitivity = FALSE;
  689. PortDCB.fOutX = FALSE;
  690. PortDCB.fInX = FALSE;
  691. PortDCB.fErrorChar = FALSE;
  692. PortDCB.fNull = FALSE;
  693. // dcb.fRtsControl = RTS_CONTROL_DISABLE;
  694. PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
  695. // dcb.fRtsControl = RTS_CONTROL_TOGGLE; // specially for PROMAG USB->RS485 converter
  696. PortDCB.fAbortOnError = FALSE;
  697. PortDCB.fBinary = TRUE;
  698. PortDCB.fTXContinueOnXoff = TRUE;
  699. // Re-configure the port with the new DCB structure.
  700. if (! SetCommState(m_ioFID, &PortDCB))
  701. {
  702. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommState Failed)", m_sDevicePath);
  703. LOG_ERROR(MsgBuffer);
  704. CloseHandle(m_ioFID);
  705. return false;
  706. };
  707. COMMTIMEOUTS CommTimeouts;
  708. // Retrieve the time-out parameters for all read and write operations
  709. GetCommTimeouts(m_ioFID, &CommTimeouts);
  710. /*
  711. CommTimeouts.ReadIntervalTimeout = 20;
  712. CommTimeouts.ReadTotalTimeoutMultiplier = 10;
  713. CommTimeouts.ReadTotalTimeoutConstant = 100;
  714. CommTimeouts.WriteTotalTimeoutMultiplier = 10;
  715. CommTimeouts.WriteTotalTimeoutConstant = 100;
  716. */
  717. CommTimeouts.ReadIntervalTimeout = 20UL;
  718. CommTimeouts.ReadTotalTimeoutMultiplier = 1UL;
  719. CommTimeouts.ReadTotalTimeoutConstant = 5UL;
  720. CommTimeouts.WriteTotalTimeoutMultiplier = 1UL;
  721. CommTimeouts.WriteTotalTimeoutConstant = 1UL;
  722. // Set the time-out parameters for all read and write operations on the port.
  723. if (! SetCommTimeouts(m_ioFID, &CommTimeouts))
  724. {
  725. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommTimeouts Failed)", m_sDevicePath);
  726. LOG_ERROR(MsgBuffer);
  727. CloseHandle(m_ioFID);
  728. return false;
  729. };
  730. // Clear the port of any existing data.
  731. if (PurgeComm(m_ioFID, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT) == 0)
  732. {
  733. sprintf(MsgBuffer, "Port [%s] Clearing The Port Failed", m_sDevicePath);
  734. LOG_ERROR(MsgBuffer);
  735. CloseHandle(m_ioFID);
  736. return false;
  737. };
  738. EscapeCommFunction(m_ioFID, SETRTS);
  739. EscapeCommFunction(m_ioFID, SETDTR);
  740. // Set DataTerminalReady 1
  741. #endif
  742. setConnectionStatus(CS_CONNECTED);
  743. #ifdef FIO_SELECT_MODE
  744. FD_SET(m_iInFID, &m_sReaderSet);
  745. if (m_nReaderSetMax < m_iInFID)
  746. {
  747. m_nReaderSetMax = m_iInFID;
  748. }
  749. #endif
  750. if (init())
  751. {
  752. #ifdef DEBUG
  753. sprintf(MsgBuffer, "Init %s [%d] OK", m_sUartData.path, m_sUartData.speed);
  754. LOG(MsgBuffer);
  755. #endif
  756. return true;
  757. }
  758. else
  759. {
  760. LOG_ERROR("HW INIT Failure!");
  761. };
  762. return false;
  763. }
  764. bool hardwareResourceSerialProvider::closePort()
  765. {
  766. //char tBuff[128];
  767. setConnectionStatus(CS_DISCONNECTED);
  768. #ifdef FIO_SELECT_MODE
  769. if (m_iControlPipeFD[0] > -1)
  770. {
  771. write(m_iControlPipeFD[0], "E", 1);
  772. }
  773. if (m_iControlPipeFD[1] > -1)
  774. {
  775. write(m_iControlPipeFD[1], "E", 1);
  776. }
  777. #endif
  778. #ifndef HALF_DUPLEX
  779. if (m_bThreadAStatus != TH_PRE_INIT && m_bThreadBStatus != TH_PRE_INIT)
  780. {
  781. uninit();
  782. };
  783. #else
  784. if (m_bThreadStatus != TH_PRE_INIT)
  785. {
  786. uninit();
  787. };
  788. #endif
  789. #ifdef FIO_SELECT_MODE
  790. if (m_iControlPipeFD[0] > -1)
  791. {
  792. close(m_iControlPipeFD[0]);
  793. }
  794. if (m_iControlPipeFD[1] > -1)
  795. {
  796. close(m_iControlPipeFD[1]);
  797. }
  798. #endif
  799. switch (m_sUartData.type)
  800. {
  801. case HARDWARE_UART:
  802. {
  803. break;
  804. };
  805. case HARDWARE_IP_UART:
  806. {
  807. if (m_iDeviceFD != -1)
  808. {
  809. switch (m_sUartData.mode)
  810. {
  811. case SOL_UDP_CLIENT:
  812. case SOL_TCP_CLIENT:
  813. case SOL_UDP_SERVER:
  814. {
  815. #ifdef __linux__
  816. close(m_iDeviceFD);
  817. #else
  818. // When your application is finished receiving datagrams close the socket.
  819. LOG("Server: Finished receiving.Closing the listening socket...");
  820. if (closesocket(m_sSocket) != 0)
  821. {
  822. sprintf(tBuff, "closesocket() failed!Error code : %ld", WSAGetLastError());
  823. LOG_ERROR(tBuff);
  824. }
  825. else
  826. {
  827. LOG("closesocket() is OK...");
  828. };
  829. // When your application is finished call WSACleanup.
  830. LOG("Server: Cleaning up...");
  831. if (WSACleanup() != 0)
  832. {
  833. sprintf(tBuff, "WSACleanup() failed!Error code : %ld", WSAGetLastError());
  834. LOG_ERROR(tBuff);
  835. }
  836. else
  837. {
  838. LOG("WSACleanup() is OK");
  839. };
  840. #endif
  841. break;
  842. };
  843. case SOL_TCP_SERVER:
  844. {
  845. close(m_iDeviceFD);
  846. break;
  847. };
  848. };
  849. m_iDeviceFD = -1;
  850. };
  851. break;
  852. };
  853. };
  854. return true;
  855. }
  856. #ifndef HALF_DUPLEX
  857. void hardwareResourceSerialProvider::m_fThreadAProc(void)
  858. {
  859. int index;
  860. int RetVal;
  861. #ifdef FIO_SELECT_MODE
  862. int iActiveFDS;
  863. fd_set sWorkingSet;
  864. struct timeval sTimeOut;
  865. #endif
  866. m_bThreadAStatus = TH_WORKING;
  867. #ifdef DEBUG
  868. pthread_setname_np( pthread_self(), "UART Sender" );
  869. #endif
  870. while (m_bThreadAStatus < TH_ENDED)
  871. {
  872. #ifdef FIO_SELECT_MODE
  873. sTimeOut.tv_sec = 5;
  874. sTimeOut.tv_usec = 0;
  875. memcpy(&sWorkingSet, &m_sSenderSet, sizeof(m_sSenderSet));
  876. iActiveFDS = select(m_nSenderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut);
  877. if (iActiveFDS == 0)
  878. {
  879. }
  880. else if (iActiveFDS == -1)
  881. {
  882. // m_bThreadBStatus = TH_ENDED;
  883. m_bThreadAStatus = TH_ENDED;
  884. LOG_ERROR("Failure during Select[s]!");
  885. }
  886. else
  887. {
  888. for (int i = 0; i <= m_nSenderSetMax && iActiveFDS > 0; i++)
  889. {
  890. if (FD_ISSET(i, &sWorkingSet))
  891. {
  892. iActiveFDS--;
  893. // Successful
  894. if (i == m_iControlPipeFD[0])
  895. {
  896. char buffer;
  897. RetVal = read(i, &buffer, 1);
  898. if (buffer == 'A')
  899. {
  900. #endif
  901. m_mBufferProtectorMutex.lock();
  902. if (m_nOutputBufferSize > 0)
  903. {
  904. index = 0;
  905. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED))
  906. {
  907. #ifdef DEBUG_EXTRA
  908. char TT[64];
  909. sprintf(TT, "Have %d Bytes to send!", m_nOutputBufferSize);
  910. LOG(TT);
  911. #endif
  912. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  913. if (RetVal > 0)
  914. {
  915. index += RetVal;
  916. }
  917. else
  918. {
  919. // if 0 or -(error)
  920. if (RetVal == 0)
  921. {
  922. usleep(100);
  923. }
  924. else
  925. {
  926. setConnectionStatus(CS_DISCONNECTED);
  927. m_nOutputBufferSize = 0;
  928. // m_bThreadBStatus = TH_ENDED;
  929. m_bThreadAStatus = TH_ENDED;
  930. LOG_ERROR("Connection Droped on Sending Data");
  931. };
  932. };
  933. };
  934. m_nOutputBufferSize = 0;
  935. m_mBufferProtectorMutex.unlock();
  936. #ifndef FIO_SELECT_MODE
  937. }
  938. else
  939. {
  940. m_mBufferProtectorMutex.unlock();
  941. SLEEP(5);
  942. #endif
  943. };
  944. #ifdef FIO_SELECT_MODE
  945. };
  946. if (buffer == 'E')
  947. {
  948. // m_bThreadBStatus = TH_ENDED;
  949. m_bThreadAStatus = TH_ENDED;
  950. LOG("Requesting Destruction of Transport[s]!");
  951. };
  952. };
  953. };
  954. };
  955. };
  956. #endif
  957. };
  958. }
  959. void hardwareResourceSerialProvider::m_fThreadBProc(void)
  960. {
  961. int RetVal;
  962. #ifdef FIO_SELECT_MODE
  963. int iActiveFDS;
  964. fd_set sWorkingSet;
  965. struct timeval sTimeOut;
  966. #endif
  967. m_bThreadBStatus = TH_WORKING;
  968. #ifdef DEBUG
  969. pthread_setname_np(pthread_self(), "UART Receiver");
  970. #endif
  971. while (m_bThreadBStatus < TH_ENDED)
  972. {
  973. #ifdef FIO_SELECT_MODE
  974. sTimeOut.tv_sec = 5;
  975. sTimeOut.tv_usec = 0;
  976. memcpy(&sWorkingSet, &m_sReaderSet, sizeof(m_sReaderSet));
  977. iActiveFDS = select(m_nReaderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut);
  978. if (iActiveFDS == 0)
  979. {
  980. }
  981. else if (iActiveFDS == -1)
  982. {
  983. m_bThreadBStatus = TH_ENDED;
  984. // m_bThreadAStatus = TH_ENDED;
  985. LOG_ERROR("Failure during Select[r]!");
  986. }
  987. else
  988. {
  989. for (int i = 0; i <= m_nReaderSetMax && iActiveFDS > 0; i++)
  990. {
  991. if (FD_ISSET(i, &sWorkingSet))
  992. {
  993. if (i == m_iControlPipeFD[1])
  994. {
  995. char buffer;
  996. RetVal = read(i, &buffer, 1);
  997. if (buffer == 'E')
  998. {
  999. m_bThreadBStatus = TH_ENDED;
  1000. LOG("Requesting Destruction of Transport[r]!");
  1001. };
  1002. }
  1003. else
  1004. {
  1005. #endif
  1006. RetVal = recptBlock(m_pInputBuffer, &m_nInputBufferSize);
  1007. if (RetVal == -1)
  1008. {
  1009. setConnectionStatus(CS_DISCONNECTED);
  1010. m_nInputBufferSize = 0;
  1011. m_bThreadBStatus = TH_ENDED;
  1012. #ifdef DEBUG_EXTRA
  1013. LOG("Connection Droped on Reception Data");
  1014. #endif
  1015. }
  1016. else if (m_nInputBufferSize > 0)
  1017. {
  1018. do
  1019. {
  1020. m_mQueueProtectorMutex.lock();
  1021. if (m_vParsers.size())
  1022. {
  1023. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++)
  1024. {
  1025. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  1026. }
  1027. m_nInputBufferSize = 0;
  1028. m_mQueueProtectorMutex.unlock();
  1029. }
  1030. else
  1031. {
  1032. #ifdef DEBUG_EXTRA
  1033. LOG(m_sUartData.path);
  1034. LOG((char *) m_pInputBuffer);
  1035. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  1036. #endif
  1037. m_nInputBufferSize = 0;
  1038. m_mQueueProtectorMutex.unlock();
  1039. SLEEP(100);
  1040. };
  1041. }
  1042. while ((m_vParsers.size() == 0) /*&& ( m_bThreadAStatus < TH_ENDED )*/ && (m_bThreadBStatus < TH_ENDED));
  1043. #ifndef FIO_SELECT_MODE
  1044. }
  1045. else
  1046. {
  1047. SLEEP(5);
  1048. #endif
  1049. };
  1050. #ifdef FIO_SELECT_MODE
  1051. };
  1052. };
  1053. };
  1054. };
  1055. #endif
  1056. };
  1057. }
  1058. bool hardwareResourceSerialProvider::sendData(uint8_t * Data, int DataSize)
  1059. {
  1060. #ifndef HALF_DUPLEX
  1061. if (m_bThreadAStatus != TH_WORKING)
  1062. {
  1063. if (m_bThreadAStatus < TH_WORKING)
  1064. {
  1065. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  1066. }
  1067. else
  1068. {
  1069. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  1070. }
  1071. return false;
  1072. };
  1073. #else
  1074. if (m_bThreadStatus != TH_WORKING)
  1075. {
  1076. if (m_bThreadStatus < TH_WORKING)
  1077. {
  1078. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  1079. }
  1080. else
  1081. {
  1082. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  1083. }
  1084. return false;
  1085. };
  1086. #endif
  1087. m_mBufferProtectorMutex.lock();
  1088. if (m_nOutputBufferSize + DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH)
  1089. {
  1090. m_mBufferProtectorMutex.unlock();
  1091. LOG_ERROR("Data Too BIG");
  1092. return false;
  1093. };
  1094. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  1095. m_nOutputBufferSize += DataSize;
  1096. m_mBufferProtectorMutex.unlock();
  1097. #ifdef FIO_SELECT_MODE
  1098. if (m_iControlPipeFD[1] > -1)
  1099. {
  1100. write(m_iControlPipeFD[1], "A", 1);
  1101. }
  1102. #endif
  1103. return true;
  1104. }
  1105. #else // HALF_DUPLEX
  1106. void hardwareResourceSerialProvider::m_fThreadProc(void)
  1107. {
  1108. int index;
  1109. int RetVal;
  1110. m_bThreadStatus = TH_WORKING;
  1111. #ifdef DEBUG_EXTRA
  1112. pthread_setname_np(pthread_self(), "UART HalfDuplex");
  1113. #endif
  1114. while (m_bThreadStatus < TH_ENDED)
  1115. {
  1116. m_mBufferProtectorMutex.lock();
  1117. if (! m_qIO.empty())
  1118. {
  1119. SIO_TZ * tIOData;
  1120. tIOData = m_qIO.front();
  1121. m_mBufferProtectorMutex.unlock();
  1122. if (tIOData)
  1123. {
  1124. // Remove from Queue
  1125. m_mBufferProtectorMutex.lock();
  1126. m_qIO.pop();
  1127. m_mBufferProtectorMutex.unlock();
  1128. index = 0;
  1129. while ((index < tIOData->out_size) && (m_bThreadStatus < TH_ENDED))
  1130. {
  1131. #ifdef DEBUG_EXTRA
  1132. char TT[64];
  1133. sprintf(TT, "Have %d Bytes to send!", tIOData->out_size - index);
  1134. LOG(TT);
  1135. #endif
  1136. RetVal = sendBlock(tIOData->out_buffer + index, tIOData->out_size - index);
  1137. if (RetVal > 0)
  1138. {
  1139. index += RetVal;
  1140. }
  1141. else
  1142. {
  1143. // if 0 or -(error)
  1144. if (RetVal == 0)
  1145. {
  1146. usleep(100);
  1147. }
  1148. else
  1149. {
  1150. setConnectionStatus(CS_DISCONNECTED);
  1151. // m_nOutputBufferSize = 0;
  1152. tIOData->out_size = 0;
  1153. m_bThreadStatus = TH_ENDED;
  1154. LOG_ERROR("Connection Droped on Sending Data");
  1155. };
  1156. };
  1157. };
  1158. tIOData->out_size = 0;
  1159. bool tDataOnline = false;
  1160. uint8_t tTOCounter = 20;
  1161. do
  1162. {
  1163. RetVal = recptBlock(tIOData->inp_buffer, &tIOData->inp_size);
  1164. if (RetVal == -1)
  1165. {
  1166. setConnectionStatus(CS_DISCONNECTED);
  1167. tIOData->inp_size = 0;
  1168. m_bThreadStatus = TH_ENDED;
  1169. LOG_ERROR("Connection Droped on Reception Data");
  1170. }
  1171. else if (tIOData->inp_size)
  1172. {
  1173. tDataOnline = true;
  1174. do
  1175. {
  1176. m_mQueueProtectorMutex.lock();
  1177. if (m_vParsers.size())
  1178. {
  1179. for (parseQueue::iterator idx = m_vParsers.begin(); idx != m_vParsers.end(); idx++)
  1180. {
  1181. (*idx)->parse(tIOData->inp_buffer, tIOData->inp_size);
  1182. }
  1183. tIOData->inp_size = 0;
  1184. m_mQueueProtectorMutex.unlock();
  1185. }
  1186. else
  1187. {
  1188. #ifdef DEBUG_EXTRA
  1189. LOG(m_sUartData.path);
  1190. LOG((char *) tIOData->inp_buffer);
  1191. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  1192. #endif
  1193. tIOData->inp_size = 0;
  1194. m_mQueueProtectorMutex.unlock();
  1195. SLEEP(100);
  1196. };
  1197. }
  1198. while ((m_vParsers.size() == 0) && (m_bThreadStatus < TH_ENDED));
  1199. }
  1200. else
  1201. {
  1202. if (tTOCounter)
  1203. {
  1204. tTOCounter--;
  1205. SLEEP(5);
  1206. #ifdef DEBUG
  1207. }
  1208. else
  1209. {
  1210. LOG_ERROR("PACK TIMEOUT!");
  1211. #endif
  1212. };
  1213. };
  1214. }
  1215. while ((m_bThreadStatus < TH_ENDED) && tTOCounter && ! (tDataOnline && RetVal == 0)); // Pack1 conditions -> White Space after Data.
  1216. // Free Package
  1217. // LOG("Del Pack");
  1218. free(tIOData);
  1219. }
  1220. else
  1221. {
  1222. // Mutex already unlocked!
  1223. SLEEP(5);
  1224. };
  1225. }
  1226. else
  1227. {
  1228. m_mBufferProtectorMutex.unlock();
  1229. SLEEP(5);
  1230. };
  1231. };
  1232. }
  1233. bool hardwareResourceSerialProvider::sendData(uint8_t * Data, int DataSize)
  1234. {
  1235. if (m_bThreadStatus != TH_WORKING)
  1236. {
  1237. if (m_bThreadStatus < TH_WORKING)
  1238. {
  1239. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  1240. }
  1241. else
  1242. {
  1243. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  1244. }
  1245. return false;
  1246. };
  1247. if (DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH)
  1248. {
  1249. LOG_ERROR("Data Too BIG");
  1250. }
  1251. else
  1252. {
  1253. SIO_TZ * tData = (SIO_TZ *) malloc(sizeof(SIO_TZ));
  1254. if (tData)
  1255. {
  1256. memset(tData, 0, sizeof(SIO_TZ));
  1257. tData->out_size = DataSize;
  1258. memcpy(tData->out_buffer, Data, DataSize);
  1259. m_mBufferProtectorMutex.lock();
  1260. m_qIO.push(tData);
  1261. m_mBufferProtectorMutex.unlock();
  1262. return true;
  1263. }
  1264. else
  1265. {
  1266. LOG_ERROR("MemAlloc Error!");
  1267. };
  1268. };
  1269. return false;
  1270. }
  1271. #endif // Half Duplex
  1272. bool hardwareResourceSerialProvider::reconnect(void)
  1273. {
  1274. closePort();
  1275. switch (m_sUartData.type)
  1276. {
  1277. case HARDWARE_UART : m_bValid = create_HW(); break;
  1278. case HARDWARE_IP_UART: m_bValid = create_SOL(); break;
  1279. };
  1280. return m_bValid;
  1281. }
  1282. int hardwareResourceSerialProvider::recptBlock(unsigned char * Buffer, int * BufferLength)
  1283. {
  1284. #ifdef __linux__
  1285. int RetVal = 0;
  1286. #else
  1287. DWORD RetVal = 0;
  1288. #endif
  1289. if (m_eConnectionStatus == CS_CONNECTED)
  1290. {
  1291. switch (m_sUartData.type)
  1292. {
  1293. case HARDWARE_UART:
  1294. {
  1295. #ifdef __linux__
  1296. do
  1297. {
  1298. RetVal = read(m_iInFID, Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH);
  1299. #ifndef HALF_DUPLEX
  1300. if (RetVal == -1 && errno == EINTR && m_bThreadAStatus == TH_WORKING)
  1301. {
  1302. SLEEP(1);
  1303. }
  1304. }
  1305. while (RetVal == -1 && errno == EINTR && m_bThreadAStatus == TH_WORKING);
  1306. #else
  1307. if (RetVal == -1 && errno == EINTR && m_bThreadStatus == TH_WORKING)
  1308. {
  1309. SLEEP(1);
  1310. }
  1311. }
  1312. while (RetVal == -1 && errno == EINTR && m_bThreadStatus == TH_WORKING);
  1313. #endif
  1314. #else // Windows
  1315. OVERLAPPED osReader = {0};
  1316. if (! ReadFile(m_ioFID, Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH, &RetVal, &osReader))
  1317. {
  1318. if (GetLastError() != ERROR_IO_PENDING)
  1319. { // read not delayed?
  1320. char tBuffer[256];
  1321. sprintf(tBuffer, "Communication Error %d!", GetLastError());
  1322. LOG_ERROR(tBuffer);
  1323. // Error in communications; report it.
  1324. return -1;
  1325. #ifdef DEBUG_SERIAL_COMMUNICATION
  1326. }
  1327. else
  1328. {
  1329. // fWaitingOnRead = TRUE;
  1330. LOG("WREAD!");
  1331. #endif
  1332. };
  1333. #ifdef DEBUG_SERIAL_COMMUNICATION
  1334. }
  1335. else
  1336. {
  1337. // read completed immediately
  1338. LOG("IMREAD!");
  1339. #endif
  1340. };
  1341. #endif // Linux
  1342. break;
  1343. };
  1344. case HARDWARE_IP_UART:
  1345. { // SOL
  1346. #ifdef __linux__
  1347. if (m_iDeviceFD > -1)
  1348. {
  1349. struct sockaddr ClientAddress;
  1350. socklen_t addr_len = sizeof(ClientAddress); // len is value/resuslt
  1351. // RetVal = recvfrom( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_WAITALL, ( struct sockaddr *) &m_sClientAddr, &addr_len );
  1352. RetVal = recvfrom(m_iDeviceFD, (char *) Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH, MSG_DONTWAIT, (struct sockaddr *) &ClientAddress, &addr_len);
  1353. if (RetVal < 1)
  1354. {
  1355. if (errno != EAGAIN && errno != EINTR)
  1356. {
  1357. LOG_ERROR("Peer Closes Connection!");
  1358. shutdown(m_iDeviceFD, 2);
  1359. close(m_iDeviceFD);
  1360. *BufferLength = 0;
  1361. m_iDeviceFD = -1;
  1362. setConnectionStatus(CS_DISCONNECTED);
  1363. return -1;
  1364. };
  1365. };
  1366. }
  1367. else
  1368. {
  1369. LOG_ERROR("Not Connected!");
  1370. *BufferLength = 0;
  1371. setConnectionStatus(CS_DISCONNECTED);
  1372. return -1;
  1373. };
  1374. #else
  1375. switch (m_eType)
  1376. {
  1377. case SOL_UDP_CLIENT:
  1378. case SOL_TCP_CLIENT:
  1379. case SOL_UDP_SERVER:
  1380. {
  1381. SOCKADDR_IN tAddr;
  1382. int tLen = sizeof(SOCKADDR_IN);
  1383. RetVal = recvfrom(m_sSocket, (char *) Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH, 0, (struct sockaddr *) &tAddr, &tLen);
  1384. if (RetVal == SOCKET_ERROR)
  1385. {
  1386. char cBuffer[165];
  1387. sprintf(cBuffer, "recvfrom() failed with error code : %d", WSAGetLastError());
  1388. LOG_ERROR(cBuffer);
  1389. };
  1390. break;
  1391. };
  1392. };
  1393. #endif
  1394. break;
  1395. }; // End of SOL
  1396. };
  1397. }
  1398. else
  1399. {
  1400. if (m_sUartData.type == HARDWARE_IP_UART)
  1401. {
  1402. // make prep for connection SOL_TCP_SERVER
  1403. if (m_sUartData.mode == SOL_TCP_CLIENT)
  1404. {
  1405. // ACCEPT
  1406. struct sockaddr ClientAddress;
  1407. socklen_t size = (socklen_t) sizeof(struct sockaddr_in);
  1408. // int ClientSocket;
  1409. m_iDeviceFD = accept(m_iMasterFD, &ClientAddress, &size);
  1410. if (m_iDeviceFD < 0)
  1411. {
  1412. setConnectionStatus(CS_DISCONNECTED);
  1413. LOG_ERROR("Connection Accept Failure!");
  1414. return -1;
  1415. };
  1416. }
  1417. else
  1418. {
  1419. LOG_ERROR("RCPT ON not connected!");
  1420. shutdown(m_iDeviceFD, 2);
  1421. close(m_iDeviceFD);
  1422. *BufferLength = 0;
  1423. m_iDeviceFD = -1;
  1424. setConnectionStatus(CS_DISCONNECTED);
  1425. return -1;
  1426. };
  1427. #ifdef DEBUG_EXTRA
  1428. }
  1429. else
  1430. {
  1431. LOG("Port is not Ready to Receive");
  1432. #endif
  1433. };
  1434. return -1; // Exit after unsuccess reconnect
  1435. };
  1436. if (RetVal > 0)
  1437. {
  1438. #ifdef DEBUG_SERIAL_COMMUNICATION
  1439. char MsgBuffer[CLOG];
  1440. if (RetVal > 1 || RetVal == 0)
  1441. {
  1442. sprintf(MsgBuffer, "Received %d bytes", RetVal);
  1443. }
  1444. else
  1445. {
  1446. sprintf(MsgBuffer, "Received 1 byte");
  1447. };
  1448. LOG(MsgBuffer);
  1449. for (int i = 0; i < RetVal; i++)
  1450. {
  1451. sprintf(MsgBuffer, "<0x%02X", Buffer[i]);
  1452. LOG(MsgBuffer);
  1453. };
  1454. #endif
  1455. *BufferLength = RetVal;
  1456. return RetVal;
  1457. };
  1458. *BufferLength = 0;
  1459. if (RetVal == -1)
  1460. {
  1461. return 0;
  1462. }
  1463. if (RetVal == 0)
  1464. {
  1465. errno = 0;
  1466. return 0;
  1467. };
  1468. /* r < -1, should never happen. */
  1469. errno = EIO;
  1470. return 0;
  1471. }
  1472. void hardwareResourceSerialProvider::setRTS(uint8_t Val)
  1473. {
  1474. #if defined(USE_WIRINGPI) || defined(USE_WIRINGX)
  1475. if (Val)
  1476. {
  1477. // Use RTS GPIO
  1478. digitalWrite(RTS_PIN_NUMBER, HIGH);
  1479. }
  1480. else
  1481. {
  1482. digitalWrite(RTS_PIN_NUMBER, LOW);
  1483. };
  1484. #else
  1485. int flags;
  1486. if (Val)
  1487. {
  1488. ioctl(m_iInFID, TIOCMGET, &flags);
  1489. flags |= TIOCM_RTS;
  1490. ioctl(m_iInFID, TIOCMSET, &flags);
  1491. ioctl(m_iOutFID, TIOCMGET, &flags);
  1492. flags |= TIOCM_RTS;
  1493. ioctl(m_iOutFID, TIOCMSET, &flags);
  1494. }
  1495. else
  1496. {
  1497. ioctl(m_iInFID, TIOCMGET, &flags);
  1498. flags &= ~TIOCM_RTS;
  1499. ioctl(m_iInFID, TIOCMSET, &flags);
  1500. ioctl(m_iOutFID, TIOCMGET, &flags);
  1501. flags &= ~TIOCM_RTS;
  1502. ioctl(m_iOutFID, TIOCMSET, &flags);
  1503. };
  1504. #endif
  1505. }
  1506. #include <sys/ioctl.h>
  1507. int hardwareResourceSerialProvider::sendBlock(unsigned char * Data, int DataSize)
  1508. {
  1509. #ifdef __linux__
  1510. int RetVal = 0;
  1511. #else
  1512. DWORD RetVal = 0;
  1513. #endif
  1514. if (m_eConnectionStatus == CS_CONNECTED)
  1515. {
  1516. switch (m_sUartData.type)
  1517. {
  1518. case HARDWARE_UART:
  1519. {
  1520. #ifdef __linux__
  1521. if (m_bManualRTS)
  1522. {
  1523. setRTS(1);
  1524. };
  1525. do
  1526. {
  1527. if (m_bManualRTS)
  1528. {
  1529. // #if defined( USE_WIRINGPI ) || defined( USE_WIRINGX )
  1530. // digitalWrite( RTS_PIN_NUMBER, HIGH );
  1531. // #else
  1532. setRTS(1);
  1533. // #endif
  1534. };
  1535. RetVal = write(m_iOutFID, Data, DataSize);
  1536. if (m_bManualRTS)
  1537. {
  1538. uint8_t lsr;
  1539. do
  1540. {
  1541. //int r =
  1542. ioctl(m_iOutFID, TIOCSERGETLSR, &lsr);
  1543. }
  1544. while (! (lsr & TIOCSER_TEMT));
  1545. // #if defined( USE_WIRINGPI ) || defined( USE_WIRINGX )
  1546. // digitalWrite( RTS_PIN_NUMBER, LOW );
  1547. // #else
  1548. setRTS(0);
  1549. // #endif
  1550. };
  1551. if (RetVal == DataSize)
  1552. {
  1553. usleep(10);
  1554. };
  1555. #ifndef HALF_DUPLEX
  1556. }
  1557. while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK) && m_bThreadAStatus == TH_WORKING);
  1558. #else
  1559. }
  1560. while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK) && m_bThreadStatus == TH_WORKING);
  1561. #endif
  1562. // if( m_bManualRTS ) setRTS( 0 );
  1563. #else //!__linux__
  1564. WriteFile(m_ioFID, Data, DataSize, &RetVal, NULL);
  1565. #endif
  1566. #ifdef DEBUG_EXTRA
  1567. char MsgBuffer[CLOG];
  1568. sprintf(MsgBuffer, "Sent %d byte(s) of %d", RetVal, DataSize);
  1569. LOG(MsgBuffer);
  1570. #ifdef DEBUG_SERIAL_COMMUNICATION
  1571. for (int i = 0; i < DataSize; i++)
  1572. {
  1573. sprintf(MsgBuffer, ">0x%02X", Data[i]);
  1574. LOG(MsgBuffer);
  1575. };
  1576. #endif
  1577. #endif
  1578. break;
  1579. };
  1580. case HARDWARE_IP_UART:
  1581. {
  1582. #ifdef __linux__
  1583. if (m_iDeviceFD > -1)
  1584. {
  1585. #ifdef DEBUG_EXTRA
  1586. char cBuffer[256];
  1587. sprintf(cBuffer, "Write into Port %d bytes", DataSize);
  1588. LOG(cBuffer);
  1589. #endif
  1590. struct sockaddr ClientAddress;
  1591. socklen_t addr_len = sizeof(ClientAddress);
  1592. RetVal = sendto(m_iDeviceFD, (const char *) Data, DataSize, MSG_CONFIRM, (const struct sockaddr *) &ClientAddress, addr_len);
  1593. if ((RetVal == -1) && (errno != EINTR) && (errno != EWOULDBLOCK))
  1594. {
  1595. LOG_ERROR("Error acures while writing data into the Socket");
  1596. shutdown(m_iDeviceFD, 2);
  1597. close(m_iDeviceFD);
  1598. m_iDeviceFD = -1;
  1599. setConnectionStatus(CS_DISCONNECTED);
  1600. return -1;
  1601. };
  1602. if (RetVal < 0)
  1603. {
  1604. RetVal = 0;
  1605. }
  1606. #ifdef DEBUG_EXTRA
  1607. sprintf(cBuffer, "Bytes Sent: %ld", RetVal);
  1608. LOG(cBuffer);
  1609. #ifdef DEBUG_SERIAL_COMMUNICATION
  1610. for (int zz = 0; zz < RetVal; zz++)
  1611. {
  1612. sprintf(cBuffer, "SD: 0x%02X", (uint8_t) Data[zz]);
  1613. LOG(cBuffer);
  1614. };
  1615. #endif
  1616. #endif
  1617. }
  1618. else
  1619. {
  1620. LOG_ERROR("Not Connected!");
  1621. // m_bConnected = false;
  1622. setConnectionStatus(CS_DISCONNECTED);
  1623. return -1;
  1624. };
  1625. #else // Windows
  1626. if (sendto(m_sSocket, (char *) Data, DataSize, 0, (struct sockaddr *) &ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR)
  1627. {
  1628. char cBuffer[256];
  1629. sprintf(cBuffer, "sendto() failed with error code : %d", WSAGetLastError());
  1630. LOG_ERROR(cBuffer);
  1631. return -1;
  1632. };
  1633. #endif
  1634. break;
  1635. };
  1636. };
  1637. }
  1638. else
  1639. {
  1640. #ifdef DEBUG_EXTRA
  1641. LOG("Port is not Ready to Send");
  1642. #endif
  1643. return -1;
  1644. };
  1645. return RetVal;
  1646. }
  1647. #undef LOG
  1648. #undef LOG_ERROR
  1649. // #undef DEBUG_SERIAL_COMMUNICATION
  1650. // #undef DEBUG_EXTRA