{"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}
};
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)
goto exit;
}
entryTypePair = getEntryTypePair(bufferLine);
+ if(entryTypePair.entryType == TW_NONE)
+ {
+ goto exit;
+ }
}
else
{
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);
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;
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;
{
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)
return ret;
}
+