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.

522 lines
15 KiB

7 years ago
​
7 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
  1. #include "serialPort.h"
  2. #define LOG( X ) m_pLog->Log( "UART", this, X )
  3. #ifdef DEBUG
  4. #define LOG_ERROR( X ) {\
  5. char _tBfrErrMSG2w34[256];\
  6. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  7. m_pLog->Log( "UART", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  8. }
  9. #else
  10. #define LOG_ERROR( X ) m_pLog->Log( "UART", this, _LOG_ERROR, X );
  11. #endif
  12. serialPort::serialPort():transportSerial(){
  13. #ifdef __linux__
  14. memset( &m_sTTY, 0, sizeof( m_sTTY ) );
  15. m_iInFID = -1;
  16. m_iOutFID = -1;
  17. #else
  18. memset(&PortDCB, 0, sizeof(PortDCB));
  19. m_ioFID = INVALID_HANDLE_VALUE;
  20. #endif
  21. m_pLog = basicLogCollection::getInstance();
  22. m_bConnected = false;
  23. m_bInitComplete = false;
  24. m_sDevicePath = NULL;
  25. m_iLineSpeed = 9600;
  26. };
  27. serialPort::serialPort( CSTR DevicePath, int Speed ): transportSerial() {
  28. #ifdef __linux__
  29. memset(&m_sTTY, 0, sizeof(m_sTTY));
  30. m_iInFID = -1;
  31. m_iOutFID = -1;
  32. #else
  33. memset(&PortDCB, 0, sizeof(PortDCB));
  34. m_ioFID = INVALID_HANDLE_VALUE;
  35. #endif
  36. m_pLog = basicLogCollection::getInstance();
  37. m_bConnected = false;
  38. m_iLineSpeed = Speed;
  39. m_sDevicePath = (CSTR) malloc( strlen(DevicePath)+1);
  40. strcpy( m_sDevicePath, DevicePath );
  41. m_bInitComplete = HW_init( m_sDevicePath, m_iLineSpeed );
  42. if( m_bInitComplete ) {
  43. #ifdef DEBUG
  44. char MsgBuffer[CLOG];
  45. sprintf( MsgBuffer,"Init %s [%d] OK", m_sDevicePath, m_iLineSpeed );
  46. LOG( MsgBuffer );
  47. #endif
  48. m_bInitComplete = init();
  49. };
  50. };
  51. bool serialPort::create( CSTR DevicePath, int Speed ){
  52. if( m_bConnected ){
  53. LOG_ERROR( "TRYING TO CONNECT ON OPENED PORT!" );
  54. return false;
  55. };
  56. if( m_bInitComplete ) {
  57. LOG_ERROR( "TRYING TO CREATE CREATED PORT!" );
  58. return false;
  59. };
  60. m_iLineSpeed = Speed;
  61. m_sDevicePath = (CSTR) malloc( strlen(DevicePath)+1);
  62. strcpy( m_sDevicePath, DevicePath );
  63. m_bInitComplete = HW_init( m_sDevicePath, m_iLineSpeed );
  64. if( m_bInitComplete ) {
  65. #ifdef DEBUG
  66. char MsgBuffer[CLOG];
  67. sprintf( MsgBuffer,"Init %s [%d] OK", m_sDevicePath, m_iLineSpeed );
  68. LOG( MsgBuffer );
  69. #endif
  70. return m_bInitComplete = init();
  71. };
  72. return false;
  73. };
  74. // Destructor implementation
  75. serialPort::~serialPort(){
  76. int result;
  77. #ifdef __linux__
  78. if( m_iInFID != -1 ) {
  79. do{
  80. result = close(m_iInFID);
  81. } while (result == -1 && errno == EINTR);
  82. if (result == -1) {
  83. #else
  84. if (m_ioFID != INVALID_HANDLE_VALUE) {
  85. result = CloseHandle(m_ioFID);
  86. if (result == 0) {
  87. #endif
  88. char MsgBuffer[CLOG];
  89. sprintf( MsgBuffer, "Error %d closing input [%s]: %s", errno, m_sDevicePath, strerror (errno) );
  90. LOG_ERROR( MsgBuffer );
  91. };
  92. };
  93. #ifdef __linux__
  94. if (m_iOutFID != -1) {
  95. do{
  96. result = close( m_iOutFID );
  97. }while (result == -1 && errno == EINTR);
  98. if (result == -1) {
  99. char MsgBuffer[CLOG];
  100. sprintf( MsgBuffer, "Error %d closing output [%s]: %s", errno, m_sDevicePath, strerror (errno) );
  101. LOG_ERROR( MsgBuffer );
  102. };
  103. };
  104. #endif
  105. waitThreadAEnd( true );
  106. waitThreadBEnd( true );
  107. };
  108. bool serialPort::HW_init( CSTR DevicePath, int Speed ){
  109. char MsgBuffer[CLOG];
  110. m_bInitComplete = false;
  111. sprintf(m_sDevicePath, "%s", DevicePath);
  112. #ifdef __linux__
  113. int result;
  114. /* Open File Descriptor */
  115. //m_iFID = open( DevicePath, O_RDWR| O_NONBLOCK | O_NDELAY );
  116. do{
  117. m_iInFID = open( m_sDevicePath, O_RDONLY | O_NONBLOCK | O_NDELAY );
  118. }while( m_iInFID == -1 && errno == EINTR);
  119. if( m_iInFID == -1 ) return false;
  120. do{
  121. result = fcntl( m_iInFID, F_SETFL, O_NONBLOCK );
  122. }while( result == -1 && errno == EINTR );
  123. if( result == -1 ){
  124. int const error = errno;
  125. do{
  126. result = close( m_iInFID );
  127. }while( result == -1 && errno == EINTR );
  128. errno = error;
  129. sprintf( MsgBuffer, "Error %d openning port: %s", errno, strerror (errno) );
  130. LOG_ERROR( MsgBuffer );
  131. return false;
  132. };
  133. do{
  134. m_iOutFID = open( DevicePath, O_WRONLY | O_NDELAY );
  135. }while( m_iOutFID == -1 && errno == EINTR);
  136. if( m_iOutFID == -1 ) return false;
  137. /* *** Configure Port *** */
  138. memset( &m_sTTY, 0, sizeof( m_sTTY ) );
  139. /* Error Handling */
  140. do{
  141. result = tcgetattr( m_iInFID, &m_sTTY );
  142. }while( result == -1 && errno == EINTR );
  143. if( result == -1 ){
  144. int const error = errno;
  145. do{
  146. result = close( m_iInFID );
  147. }while( result == -1 && errno == EINTR );
  148. errno = error;
  149. sprintf( MsgBuffer, "Error %d from tcgetattr: %s", errno, strerror (errno) );
  150. LOG_ERROR( MsgBuffer );
  151. return false;
  152. };
  153. /* Set Baud Rate */
  154. m_iLineSpeed = Speed;
  155. int RetVal = 0;
  156. switch( m_iLineSpeed ){
  157. case 4000000: cfsetspeed (&m_sTTY, B4000000 ); break;
  158. case 3500000: cfsetspeed (&m_sTTY, B3500000 ); break;
  159. case 3000000: cfsetspeed (&m_sTTY, B3000000 ); break;
  160. case 2500000: cfsetspeed (&m_sTTY, B2500000 ); break;
  161. case 2000000: cfsetspeed (&m_sTTY, B2000000 ); break;
  162. case 1500000: cfsetspeed (&m_sTTY, B1500000 ); break;
  163. case 1152000: cfsetspeed (&m_sTTY, B1152000 ); break;
  164. case 1000000: cfsetspeed (&m_sTTY, B1000000 ); break;
  165. case 921600: cfsetspeed (&m_sTTY, B921600 ); break;
  166. case 460800: cfsetspeed (&m_sTTY, B460800 ); break;
  167. case 230400: cfsetspeed (&m_sTTY, B230400 ); break;
  168. case 115200: cfsetspeed (&m_sTTY, B115200 ); break;
  169. case 19200: cfsetspeed (&m_sTTY, B19200 ); break;
  170. case 9600: cfsetspeed (&m_sTTY, B9600 ); break;
  171. default:{
  172. close( m_iInFID );
  173. close( m_iOutFID );
  174. sprintf( MsgBuffer, "Error cannot set speed as [%d] bod", m_iLineSpeed );
  175. LOG_ERROR( MsgBuffer );
  176. return false;
  177. };
  178. };
  179. if( RetVal == 0 ) {
  180. #ifdef DEBUG
  181. sprintf( MsgBuffer, "speed set to [%d] bod", m_iLineSpeed );
  182. LOG( MsgBuffer );
  183. #endif
  184. } else {
  185. RetVal = cfgetispeed( &m_sTTY );
  186. sprintf( MsgBuffer, "Error cannot set speed as [%d] bod. Current is: %d bod. ", m_iLineSpeed, RetVal );
  187. LOG_ERROR( MsgBuffer );
  188. return false;
  189. };
  190. /* Setting other Port Stuff */
  191. m_sTTY.c_cflag &= ~PARENB; // Make 8n1
  192. m_sTTY.c_cflag &= ~CSTOPB;
  193. m_sTTY.c_cflag &= ~CSIZE;
  194. m_sTTY.c_cflag |= CS8;
  195. m_sTTY.c_cflag &= ~CRTSCTS; // no flow control
  196. m_sTTY.c_lflag = 0; // no signaling chars, no echo, no canonical processing
  197. m_sTTY.c_oflag = 0; // no remapping, no delays
  198. m_sTTY.c_cc[VMIN] = 0; // read doesn't block
  199. m_sTTY.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  200. m_sTTY.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  201. m_sTTY.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  202. m_sTTY.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  203. m_sTTY.c_oflag &= ~OPOST; // make raw
  204. /* Flush Port, then applies attributes */
  205. tcflush( m_iOutFID, TCIFLUSH );
  206. do{
  207. result = tcsetattr( m_iOutFID, TCSANOW, &m_sTTY );
  208. }while( result == -1 && errno == EINTR );
  209. if( result == -1 ){
  210. int const error = errno;
  211. do{
  212. result = close( m_iOutFID );
  213. }while( result == -1 && errno == EINTR );
  214. errno = error;
  215. sprintf( MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror (errno) );
  216. LOG_ERROR( MsgBuffer );
  217. return false;
  218. };
  219. do{
  220. result = tcsetattr( m_iInFID, TCSANOW, &m_sTTY );
  221. }while( result == -1 && errno == EINTR );
  222. if( result == -1 ){
  223. int const error = errno;
  224. do{
  225. result = close( m_iInFID );
  226. }while( result == -1 && errno == EINTR );
  227. errno = error;
  228. sprintf( MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror (errno) );
  229. LOG_ERROR( MsgBuffer );
  230. return false;
  231. };
  232. #else
  233. m_ioFID = CreateFile( (LPCSTR)m_sDevicePath,
  234. GENERIC_READ | GENERIC_WRITE,
  235. 0UL, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING,
  236. //FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
  237. NULL );
  238. if (m_ioFID == INVALID_HANDLE_VALUE){
  239. setConnectionStatus(CS_DISCONNECTED);
  240. sprintf(MsgBuffer, "Port [%s] Open Failed Err: %d", m_sDevicePath, GetLastError());
  241. LOG_ERROR(MsgBuffer);
  242. return false;
  243. };
  244. // Initialize the DCBlength member.
  245. PortDCB.DCBlength = sizeof(DCB);
  246. // Get the default port setting information.
  247. GetCommState(m_ioFID, &PortDCB);
  248. // Change the DCB structure settings
  249. /*
  250. PortDCB.fBinary = TRUE; // Binary mode; no EOF check
  251. PortDCB.fParity = NOPARITY; // Enable parity checking
  252. PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
  253. PortDCB.fErrorChar = FALSE; // Disable error replacement
  254. PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
  255. PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error
  256. PortDCB.fNull = FALSE; // Disable null stripping
  257. PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
  258. PortDCB.BaudRate = Speed;
  259. PortDCB.StopBits = ONESTOPBIT;
  260. */
  261. PortDCB.BaudRate = Speed;
  262. PortDCB.ByteSize = 8;
  263. PortDCB.Parity = NOPARITY;
  264. PortDCB.StopBits = ONESTOPBIT;
  265. PortDCB.EvtChar = 0;
  266. PortDCB.fParity = (PortDCB.Parity != NOPARITY);
  267. PortDCB.fOutxCtsFlow = FALSE;
  268. PortDCB.fOutxDsrFlow = FALSE;
  269. PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
  270. PortDCB.fDsrSensitivity = FALSE;
  271. PortDCB.fOutX = FALSE;
  272. PortDCB.fInX = FALSE;
  273. PortDCB.fErrorChar = FALSE;
  274. PortDCB.fNull = FALSE;
  275. // dcb.fRtsControl = RTS_CONTROL_DISABLE;
  276. PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
  277. // dcb.fRtsControl = RTS_CONTROL_TOGGLE; // specially for PROMAG USB->RS485 converter
  278. PortDCB.fAbortOnError = FALSE;
  279. PortDCB.fBinary = TRUE;
  280. PortDCB.fTXContinueOnXoff = TRUE;
  281. //Re-configure the port with the new DCB structure.
  282. if (!SetCommState(m_ioFID, &PortDCB)) {
  283. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommState Failed)", m_sDevicePath);
  284. LOG_ERROR(MsgBuffer);
  285. CloseHandle(m_ioFID);
  286. return false;
  287. };
  288. COMMTIMEOUTS CommTimeouts;
  289. // Retrieve the time-out parameters for all read and write operations
  290. GetCommTimeouts(m_ioFID, &CommTimeouts);
  291. /*
  292. CommTimeouts.ReadIntervalTimeout = 20;
  293. CommTimeouts.ReadTotalTimeoutMultiplier = 10;
  294. CommTimeouts.ReadTotalTimeoutConstant = 100;
  295. CommTimeouts.WriteTotalTimeoutMultiplier = 10;
  296. CommTimeouts.WriteTotalTimeoutConstant = 100;
  297. */
  298. CommTimeouts.ReadIntervalTimeout = 20UL;
  299. CommTimeouts.ReadTotalTimeoutMultiplier = 1UL;
  300. CommTimeouts.ReadTotalTimeoutConstant = 5UL;
  301. CommTimeouts.WriteTotalTimeoutMultiplier = 1UL;
  302. CommTimeouts.WriteTotalTimeoutConstant = 1UL;
  303. // Set the time-out parameters for all read and write operations on the port.
  304. if (!SetCommTimeouts(m_ioFID, &CommTimeouts)){
  305. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommTimeouts Failed)", m_sDevicePath);
  306. LOG_ERROR(MsgBuffer);
  307. CloseHandle(m_ioFID);
  308. return false;
  309. };
  310. // Clear the port of any existing data.
  311. if (PurgeComm(m_ioFID, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT ) == 0){
  312. sprintf(MsgBuffer, "Port [%s] Clearing The Port Failed", m_sDevicePath);
  313. LOG_ERROR(MsgBuffer);
  314. CloseHandle(m_ioFID);
  315. return false;
  316. };
  317. EscapeCommFunction(m_ioFID, SETRTS);
  318. EscapeCommFunction(m_ioFID, SETDTR);
  319. //Set DataTerminalReady 1
  320. #endif
  321. setConnectionStatus(CS_CONNECTED);
  322. return true;
  323. };
  324. int serialPort::sendBlock( unsigned char *Data, int DataSize ){
  325. if(!m_bInitComplete) {
  326. #ifdef DEBUG
  327. LOG( "Port is not Ready to Send" );
  328. #endif
  329. return -1;
  330. };
  331. #ifdef __linux__
  332. int RetVal;
  333. do{
  334. RetVal = write( m_iOutFID, Data, DataSize );
  335. usleep(100);
  336. } while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK));
  337. #else
  338. DWORD RetVal;
  339. //OVERLAPPED ov;
  340. //if (!
  341. WriteFile(m_ioFID, Data, DataSize, &RetVal, NULL);//){
  342. // RetVal = 0;
  343. // };
  344. #endif
  345. #ifdef DEBUG_COMMUNICATION
  346. char MsgBuffer[CLOG];
  347. sprintf( MsgBuffer, "Sent %d byte(s) of %d", RetVal, DataSize );
  348. LOG( MsgBuffer );
  349. #ifdef DEBUG_SERIAL_COMMUNICATION
  350. for( int i = 0; i < DataSize; i++ ){
  351. sprintf( MsgBuffer, "0x%02X", Data[i] );
  352. LOG( MsgBuffer );
  353. };
  354. #endif
  355. #endif
  356. return RetVal;
  357. //*/
  358. /*
  359. char const *p = (char const *)Data;
  360. char const *const q = (char const *)Data + DataSize;
  361. ssize_t n;
  362. char MsgBuffer[CLOG];
  363. sprintf( MsgBuffer, "Prepare to Send %d Bytes", DataSize );
  364. Log( MsgBuffer );
  365. while (p < q) {
  366. do {
  367. n = write( m_iOutFID, p, (size_t)(q - p));
  368. } while (n == (ssize_t)-1 && errno == EINTR);
  369. if (n == (ssize_t)-1 && errno == EWOULDBLOCK) {
  370. // Sleep for a millisecond, then retry.
  371. usleep(1000);
  372. continue;
  373. }
  374. if (n == (ssize_t)-1)
  375. return errno;
  376. else
  377. if (n < (ssize_t)1)
  378. return EIO;
  379. p += (size_t)n;
  380. }
  381. sprintf( MsgBuffer, "Sent %d Bytes", DataSize );
  382. Log( MsgBuffer );
  383. if (p != q)
  384. return EIO;
  385. return DataSize;
  386. */
  387. };
  388. int serialPort::recptBlock( unsigned char *Buffer, int *BufferLength ){
  389. if (!m_bInitComplete) {
  390. #ifdef DEBUG
  391. LOG("Port is Not Ready to Read");
  392. #endif
  393. return -1;
  394. };
  395. /*
  396. int n_read = read( m_iInFID, Buffer, sizeof(Buffer) );
  397. (*BufferLength) = n_read;
  398. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  399. char MsgBuffer[CLOG];
  400. sprintf( MsgBuffer, "Received %d bytes", n_read );
  401. Log( MsgBuffer );
  402. #endif
  403. // Error Handling
  404. if( n_read < 0 ){
  405. #ifndef _DEBUG_SERIAL_COMMUNICATION_
  406. char MsgBuffer[CLOG];
  407. #endif
  408. sprintf( MsgBuffer, "Error %d reading: %s", errno, strerror (errno) );
  409. Log( _ERROR, MsgBuffer );
  410. };
  411. return n_read;
  412. */
  413. #ifdef __linux__
  414. int RetVal;
  415. do {
  416. RetVal = read( m_iInFID, Buffer, MAXIMAL_SERIAL_PACKET_LENGTH );
  417. if( RetVal == -1 && errno == EINTR ) SLEEP(1);
  418. } while (RetVal == -1 && errno == EINTR);
  419. #else
  420. DWORD RetVal = 0;
  421. OVERLAPPED osReader = {0};
  422. if (!ReadFile(m_ioFID, Buffer, MAXIMAL_SERIAL_PACKET_LENGTH, &RetVal, &osReader)) {
  423. if (GetLastError() != ERROR_IO_PENDING){ // read not delayed?
  424. char tBuffer[256];
  425. sprintf(tBuffer, "Communication Error %d!", GetLastError());
  426. LOG_ERROR(tBuffer);
  427. // Error in communications; report it.
  428. return -1;// RetVal;
  429. } else {
  430. //fWaitingOnRead = TRUE;
  431. #ifdef DEBUG_SERIAL_COMMUNICATION
  432. LOG("WREAD!");
  433. #endif
  434. };
  435. } else {
  436. // read completed immediately
  437. #ifdef DEBUG_SERIAL_COMMUNICATION
  438. LOG("IMREAD!");
  439. #endif
  440. };
  441. #endif // Linux
  442. #ifdef DEBUG_SERIAL_COMMUNICATION
  443. if( RetVal > 0 ){
  444. char MsgBuffer[CLOG];
  445. if( RetVal > 1 || RetVal == 0 ) {
  446. sprintf( MsgBuffer, "Received %d bytes", RetVal );
  447. } else {
  448. sprintf( MsgBuffer, "Received 1 byte");
  449. };
  450. LOG( MsgBuffer );
  451. for (int i = 0; i < RetVal; i++) {
  452. sprintf(MsgBuffer, "0x%02X", Buffer[i]);
  453. LOG(MsgBuffer);
  454. };
  455. };
  456. #endif
  457. if( RetVal > 0) {
  458. *BufferLength = RetVal;
  459. return RetVal;
  460. };
  461. *BufferLength = 0;
  462. if( RetVal == -1) return 0;
  463. if( RetVal == 0 ){
  464. errno = 0;
  465. return 0;
  466. };
  467. /* r < -1, should never happen. */
  468. errno = EIO;
  469. return 0;
  470. };
  471. #undef LOG
  472. #undef LOG_ERROR