- Ensure 10 seconds timeout entryType if found or not-found. 83/3883/7
authorJoseph Morrow <joseph.l.morrow@intel.com>
Tue, 8 Dec 2015 20:14:50 +0000 (12:14 -0800)
committerJon A. Cruz <jonc@osg.samsung.com>
Thu, 10 Dec 2015 00:20:53 +0000 (00:20 +0000)
- Ignore “REPORTATTR:” prompts.
- Empty the buffer when the stack is initialized.

Change-Id: I4452622951ad07cfc5fe81f4f98e52f96b8d47d4
Signed-off-by: Thuyen Tran <thuyen.c.tran@intel.com>
Signed-off-by: Joseph Morrow <joseph.l.morrow@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3883
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
plugins/zigbee_wrapper/telegesis_wrapper/src/telegesis_socket.c

index d4c592d..6ca3442 100644 (file)
@@ -90,6 +90,7 @@ static TWEntryTypePair TWEntryTypePairArray[] =
     {"ENROLLED:",       1, TW_ENROLLED},
     {"ZONESTATUS:",     1, TW_ZONESTATUS},
     {"AddrResp:",       1, TW_ADDRESS_RESPONSE},
+    {"REPORTATTR:",     1, TW_NONE},
     {"Unknown:",        0, TW_NONE},
     {"Unknown:",        1, TW_MAX_ENTRY}
 };
@@ -98,17 +99,26 @@ TWEntry * TWEntryList = NULL;
 
 TWEntryTypePair getEntryTypePair(const char * bufferLine)
 {
+    size_t count = sizeof(TWEntryTypePairArray)/sizeof(TWEntryTypePairArray[0]);
+    if (!bufferLine)
+    {
+        return TWEntryTypePairArray[count-1];
+    }
     size_t bufferLength = strlen(bufferLine);
-    for(uint8_t i = 0; i < TW_MAX_ENTRY; i++)
+    for (size_t i = 0; i < count; i++)
     {
+        if (!TWEntryTypePairArray[i].resultTxt)
+        {
+            return TWEntryTypePairArray[count-1];
+        }
         size_t resultTxtLength = strlen(TWEntryTypePairArray[i].resultTxt);
-        if((bufferLength >= resultTxtLength) &&
+        if ((bufferLength >= resultTxtLength) &&
            strncmp(bufferLine, TWEntryTypePairArray[i].resultTxt, resultTxtLength) == 0)
         {
             return TWEntryTypePairArray[i];
         }
     }
-    return TWEntryTypePairArray[TW_MAX_ENTRY];
+    return TWEntryTypePairArray[count-1];
 }
 
 TWResultCode TWWait(pthread_cond_t * cond, pthread_mutex_t * mutex, uint8_t timeout)
@@ -391,6 +401,10 @@ TWEntry * readEntry(int fd)
                 goto exit;
             }
             entryTypePair = getEntryTypePair(bufferLine);
+            if(entryTypePair.entryType == TW_NONE)
+            {
+                goto exit;
+            }
         }
         else
         {
@@ -421,11 +435,31 @@ exit:
 
 TWResultCode TWRetrieveEUI(PIPlugin * plugin, TWSock * twSock)
 {
+    if(!plugin || !twSock)
+    {
+        OC_LOG(ERROR, TAG, "Invalid param.");
+        return TW_RESULT_ERROR_INVALID_PARAMS;
+    }
     if(twSock->isActive == false)
     {
+        OC_LOG(ERROR, TAG, "Tried to retrieve Zigbee EUI on an uninitialized socket.");
         return TW_RESULT_ERROR;
     }
 
+    //Empty buffer
+    char hideBuffer[1] = "";
+    int p = 1;
+    while(p != 0)
+    {
+        errno = 0;
+        p = read(twSock->fd, hideBuffer, 1);
+        if(p < 0)
+        {
+            OC_LOG_V(ERROR, TAG, "\tCould not read from port. Errno is: %d\n", errno);
+            return TW_RESULT_ERROR;
+        }
+    }
+
     TWEntry * entry = NULL;
     TWResultCode deleteResult = TW_RESULT_OK;
     TWResultCode result = TWIssueATCommand(plugin, AT_CMD_GET_LOCAL_EUI);
@@ -450,12 +484,22 @@ TWResultCode TWRetrieveEUI(PIPlugin * plugin, TWSock * twSock)
     twSock->eui = (char *) OICMalloc(strlen(entry->lines[0].line)+1);
     if(!twSock->eui)
     {
+        result = TWReleaseMutex(&twSock->mutex);
+        if(result != TW_RESULT_OK)
+        {
+            goto exit;
+        }
         result = TW_RESULT_ERROR_NO_MEMORY;
         goto exit;
     }
 
     if(SIZE_EUI != (strlen(entry->lines[0].line)+1))
     {
+        result = TWReleaseMutex(&twSock->mutex);
+        if(result != TW_RESULT_OK)
+        {
+            goto exit;
+        }
         OICFree(twSock->eui);
         result = TW_RESULT_ERROR;
         goto exit;
@@ -579,6 +623,8 @@ void * readForever(/*PIPlugin*/ void * plugin)
                     continue;
                     // This EINTR signal is not for us. Do not handle it.
                 }
+                // Notify other threads waiting for a response that the stack is going down.
+                pthread_cond_signal(&twSock->queueCV);
                 OC_LOG(DEBUG, TAG, "Thread has been joined. Exiting thread.");
                 pthread_exit(PTHREAD_CANCELED);
                 return NULL;
@@ -762,28 +808,51 @@ TWResultCode TWDequeueEntry(PIPlugin * plugin, TWEntry ** entry, TWEntryType typ
     {
         return ret;
     }
-
+    *entry = NULL;
     if(type != TW_NONE)
     {
-        // Wait for up to 10 seconds for the entry to put into the queue.
-        ret = TWWait(&twSock->queueCV, &twSock->mutex, TIME_OUT_10_SECONDS);
-        if(ret != TW_RESULT_OK)
-        {
-            return ret;
+        struct timespec abs_time;
+        clock_gettime(CLOCK_REALTIME , &abs_time);
+        abs_time.tv_sec += TIME_OUT_10_SECONDS;
+        while(!*entry)
+         {
+            // Wait for up to 10 seconds for the entry to put into the queue.
+            ret = TWWait(&twSock->queueCV, &twSock->mutex, TIME_OUT_10_SECONDS);
+            if(ret != TW_RESULT_OK)
+            {
+                return ret;
+            }
+            if(twSock->isActive == false)
+            {
+                break;
+            }
+            TWEntry * out = NULL;
+            TWEntry * temp = NULL;
+            LL_FOREACH_SAFE(twSock->queue, out, temp)
+             {
+                if(out->type == type)
+                {
+                    *entry = out;
+                    break;
+                }
+            }
+            struct timespec cur_time;
+            clock_gettime(CLOCK_REALTIME, &cur_time);
+            if(cur_time.tv_sec >= abs_time.tv_sec)
+            {
+                break;
+            }
         }
     }
-
-    *entry = twSock->queue;
-    if(*entry)
+    else
     {
-        LL_DELETE(twSock->queue, *entry);
+        *entry = twSock->queue;
     }
-    ret = TWReleaseMutex(&twSock->mutex);
-    if(ret != TW_RESULT_OK)
+    if(*entry)
     {
-        return ret;
+        LL_DELETE(twSock->queue, *entry);
     }
-    return ret;
+    return TWReleaseMutex(&twSock->mutex);
 }
 
 TWResultCode TWFreeQueue(PIPlugin * plugin)
@@ -941,3 +1010,4 @@ TWResultCode TWStopSock(PIPlugin * plugin)
 
     return ret;
 }
+