AT: Better failure handling and removed wrong unlock master
authorPaul Kocialkowski <contact@paulk.fr>
Sun, 2 Sep 2012 21:15:09 +0000 (23:15 +0200)
committerPaul Kocialkowski <contact@paulk.fr>
Sun, 2 Sep 2012 21:15:09 +0000 (23:15 +0200)
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
at.c

diff --git a/at.c b/at.c
index 7de2a08..0f365ba 100644 (file)
--- a/at.c
+++ b/at.c
@@ -890,10 +890,9 @@ int at_request_queue(struct at_request *request)
        return 0;
 }
 
-// Unqueue the oldest request in the queue
-struct at_request *at_request_dequeue(void)
+// Unqueue a request in the queue
+struct at_request *at_request_dequeue(struct at_request *request)
 {
-       struct at_request *request = NULL;
        struct at_request **requests = NULL;
        int requests_count = 0;
        int pos = -1;
@@ -912,11 +911,20 @@ struct at_request *at_request_dequeue(void)
                return NULL;
        }
 
-       for(i=0 ; i < requests_count ; i++) {
-               if(requests[i] != NULL) {
-                       request = requests[i];
-                       pos = i;
-                       break;
+       if(request == NULL) {
+               for(i=0 ; i < requests_count ; i++) {
+                       if(requests[i] != NULL) {
+                               request = requests[i];
+                               pos = i;
+                               break;
+                       }
+               }
+       } else {
+               for(i=0 ; i < requests_count ; i++) {
+                       if(requests[i] == request) {
+                               pos = i;
+                               break;
+                       }
                }
        }
 
@@ -1543,7 +1551,6 @@ int at_sync_response_dequeue(struct at_response *response)
                                // We don't need to check on the async queue as requests are sent one at a time
 
                                LOGE("Got a response for another request, aborting!");
-                               AT_SYNC_QUEUE_UNLOCK();
                                return -1;
                        }
                } else {
@@ -1986,6 +1993,9 @@ request_send:
        if(sync_request->response == NULL) {
                LOGE("Sync queue was unlocked but there is no response, aborting");
 
+               // Remove the request
+               at_request_dequeue(request);
+
                // Better trying to dequeue too
                at_sync_request_dequeue(sync_request);
                at_sync_request_free(sync_request);
@@ -2051,7 +2061,7 @@ int at_send_next_request(void)
                return -1;
        }
 
-       request = at_request_dequeue();
+       request = at_request_dequeue(NULL);
        if(request == NULL) {
                LOGD("Nothing left to send!");
                return 0;