Added function to config terminal for read/write.
authorThuyen Tran <thuyen.c.tran@intel.com>
Sat, 26 Sep 2015 18:45:19 +0000 (11:45 -0700)
committerJon A. Cruz <jonc@osg.samsung.com>
Sat, 26 Sep 2015 01:10:50 +0000 (01:10 +0000)
Change-Id: I816c23cfd68bd85ff32bf4dbc5cadba5a4c708c8
Signed-off-by: Thuyen Tran <thuyen.c.tran@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3065
Reviewed-by: Joseph Morrow <joseph.l.morrow@intel.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
plugins/zigbee_wrapper/telegesis_wrapper/src/telegesis_wrapper.c

index 55683f5..3cbcf43 100644 (file)
@@ -202,8 +202,7 @@ static TWResultCode AsciiHexToValue(char* hexString, int length, uint64_t* value
 static int AsciiToHex(char c);
 static int Tokenize(const char *input, const char* delimiters, char* output[]);
 
-static int SetInterfaceAttribs(int fd, int speed, int parity);
-static void SetBlocking(int fd, int should_block);
+static int SetTerminalInfo(int fd, int speed, int parity, int shouldBlock);
 
 static void CleanArray(char* responseArray[], char* promptArray[],
                        uint8_t responseCount, uint8_t promptCount);
@@ -2263,67 +2262,68 @@ TWResultCode AsciiHexToValue(char* hexString, int length, uint64_t* value)
  * Apply interface attribute values to terminal settings.
  *
  */
-int SetInterfaceAttribs(int fd, int speed, int parity)
+int SetTerminalInfo(int fd, int speed, int parity, int shouldBlock)
 {
-    struct termios tty;
-    memset (&tty, 0, sizeof tty);
-    if (tcgetattr (fd, &tty) != 0)
+    OC_LOG_V(INFO, TAG, "Enter SetTerminalInfo()");
+
+    int ret = 0;
+    struct termios terminalInfo = {0, 0, 0, 0, 0, 0, 0};
+    ret = tcgetattr(fd, &terminalInfo);
+    if (ret != 0)
     {
-        OC_LOG_V(ERROR, TAG, "SetInterfaceAttribs: Error %d from tcgetattr", errno);
-        return -1;
+        OC_LOG_V(ERROR, TAG, "tcgetattr() - error=%d", ret);
+        ret = -1;
+        goto exit;
     }
 
-    cfsetospeed (&tty, speed);
-    cfsetispeed (&tty, speed);
-
-    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
-    // disable IGNBRK for mismatched speed tests;
-    //otherwise receive break as \000 chars
-    tty.c_iflag &= ~IGNBRK;         // disable break processing
-    tty.c_lflag = 0;                // no signaling chars, no echo,
-                                    // no canonical processing
-    tty.c_oflag = 0;                // no re-mapping, no delays
-    tty.c_cc[VMIN]  = 0;            // read doesn't block
-    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
-
-    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
-
-    tty.c_cflag |= (CLOCAL | CREAD);    // ignore modem controls, enable reading
-    tty.c_cflag &= ~(PARENB | PARODD);  // shut off parity
-    tty.c_cflag |= parity;
-    tty.c_cflag &= ~CSTOPB;
-    tty.c_cflag &= ~CRTSCTS;
-
-    if (tcsetattr (fd, TCSANOW, &tty) != 0)
+    ret = cfsetispeed (&terminalInfo, speed);
+    if (ret != 0)
     {
-        OC_LOG_V(ERROR, TAG, "SetInterfaceAttribs: Error %d from tcsetattr", errno);
-        return -1;
+        OC_LOG_V(ERROR, TAG, "cfsetispeed() - error=%d", ret);
+        ret = -1;
+        goto exit;
     }
-    return 0;
-}
 
-/**
- *
- * Apply blocking value to terminal settings.
- *
- */
-void SetBlocking (int fd, int should_block)
-{
-    struct termios tty;
-    memset (&tty, 0, sizeof tty);
-    if (tcgetattr (fd, &tty) != 0)
+    ret = cfsetospeed (&terminalInfo, speed);
+    if (ret != 0)
     {
-        OC_LOG_V(ERROR, TAG, "SetBlocking: Error %d from tcgetattr", errno);
-        return;
+        OC_LOG_V(ERROR, TAG, "cfsetospeed() - error=%d", ret);
+        ret = -1;
+        goto exit;
     }
 
-    tty.c_cc[VMIN]  = should_block ? 1 : 0;
-    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
+    terminalInfo.c_cflag = (terminalInfo.c_cflag & ~CSIZE);     //byte size
+    terminalInfo.c_cflag |= CS8;        //byte size is 8
+
+    terminalInfo.c_cflag &= ~PARENB;    //no parity
+    terminalInfo.c_cflag |= parity;     //no parity
+
+    terminalInfo.c_cflag &= ~CSTOPB;    //1 stop bit
+
+    terminalInfo.c_cflag |= CREAD;      //enable the receiver
+
+    //Input Control Settings
+    terminalInfo.c_iflag &= ~IGNBRK;    //break condition
 
-    if (tcsetattr (fd, TCSANOW, &tty) != 0)
+    //Local Mode Settings
+    terminalInfo.c_lflag = 0;
+
+    // whether to block on read and read time-out
+    terminalInfo.c_cc[VMIN]  = (shouldBlock >= 1) ? 1 : 0;
+    terminalInfo.c_cc[VTIME] = 5;
+
+    //Input Control Settings
+    terminalInfo.c_oflag = 0;
+
+    if (tcsetattr (fd, TCSANOW, &terminalInfo) != 0)
     {
-        OC_LOG_V(ERROR, TAG, "SetBlocking: Error %d setting terminal attributes", errno);
+        OC_LOG_V(ERROR, TAG, "tcsetattr");
+        ret = -1;
     }
+
+exit:
+    OC_LOG_V(INFO, TAG, "Leave SetTerminalInfo() with ret=%d", ret);
+    return ret;
 }
 
 /**
@@ -2418,8 +2418,8 @@ TWResultCode OpenPort(int * fd)
         return TW_RESULT_ERROR;
     }
 
-    SetInterfaceAttribs(tw_fd, DEVICE_BAUDRATE, 0); // set speed to 19,200 bps, 8n1 (no parity)
-    SetBlocking(tw_fd, 0); // set no blocking
+    // set speed to 19,200 bps, 8n1 (no parity), no block
+    SetTerminalInfo(tw_fd, DEVICE_BAUDRATE, 0, 0);
 
     return TW_RESULT_OK;
 }
@@ -2845,6 +2845,7 @@ void DeallocateTWDeviceList()
     OICFree(g_FoundMatchedDeviceList->deviceList->endpointOfInterest->clusterList);
     g_FoundMatchedDeviceList->deviceList->endpointOfInterest->clusterList = NULL;
 
+
     OICFree(g_FoundMatchedDeviceList->deviceList->endpointOfInterest);
     g_FoundMatchedDeviceList->deviceList->endpointOfInterest = NULL;