AT: Expect to func return codes and added responses queue mutex
authorPaul Kocialkowski <contact@paulk.fr>
Wed, 25 Jul 2012 21:01:35 +0000 (23:01 +0200)
committerPaul Kocialkowski <contact@paulk.fr>
Wed, 25 Jul 2012 21:01:35 +0000 (23:01 +0200)
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
at.c
at.h

diff --git a/at.c b/at.c
index 3e0b6b3..f189474 100644 (file)
--- a/at.c
+++ b/at.c
@@ -470,7 +470,7 @@ void at_responses_handling_init(void)
 
        // First lock
        AT_SYNC_LOCK();
-       AT_RESPONSES_LOCK();
+       AT_RESPONSES_QUEUE_LOCK();
 }
 
 void at_responses_handling_sync_clean(void)
@@ -500,6 +500,8 @@ int at_response_queue(struct at_response *response)
                return -1;
        }
 
+       AT_RESPONSES_LOCK();
+
        // Index is the request index in the responses array
        index = at_responses_handling.responses_queue.responses_count;
        // Count is the total count of responses in the array
@@ -526,6 +528,8 @@ int at_response_queue(struct at_response *response)
 
        AT_RESPONSES_UNLOCK();
 
+       AT_RESPONSES_QUEUE_UNLOCK();
+
        return 0;
 }
 
@@ -537,12 +541,17 @@ struct at_response *at_response_dequeue(void)
        int pos;
        int i;
 
+       AT_RESPONSES_LOCK();
+
        response = NULL;
        responses = at_responses_handling.responses_queue.responses;
        responses_count = at_responses_handling.responses_queue.responses_count;
 
        if(responses_count <= 0 && responses == NULL) {
                LOGE("No response queued, blocking!");
+
+               AT_RESPONSES_UNLOCK();
+               AT_RESPONSES_QUEUE_LOCK();
                AT_RESPONSES_LOCK();
 
                responses = at_responses_handling.responses_queue.responses;
@@ -559,6 +568,7 @@ struct at_response *at_response_dequeue(void)
 
        if(response == NULL) {
                LOGD("Found no valid response, aborting!");
+               AT_RESPONSES_UNLOCK();
                return NULL;
        }
 
@@ -581,6 +591,8 @@ struct at_response *at_response_dequeue(void)
                at_responses_handling.responses_queue.responses = NULL;
        }
 
+       AT_RESPONSES_UNLOCK();
+
        return response;
 }
 
@@ -711,7 +723,6 @@ struct at_async_request *at_request_expect_to_func_find(char *command)
                        if(requests[i]->command != NULL) {
                                rc = at_commands_compare(command, requests[i]->command);
                                if(rc) {
-                                       LOGD("Found a matching request!");
                                        request = requests[i];
                                        break;
                                }
@@ -735,16 +746,20 @@ int at_response_expect_to_func(struct at_response *response)
        request = at_request_expect_to_func_find(response->command);
        if(request == NULL || request->func == NULL) {
                return -1;
+       } else {
+               LOGD("Found a matching request!");
        }
 
        // This must run out of any mutex
        rc = request->func(response, request->data, request->token);
 
-       AT_ASYNC_LOCK();
-       at_request_expect_to_func_release(request);
-       AT_ASYNC_UNLOCK();
+       if(rc != AT_RESPONSE_UNHANDELD) {
+               AT_ASYNC_LOCK();
+               at_request_expect_to_func_release(request);
+               AT_ASYNC_UNLOCK();
+       }
 
-       return rc < 0 ? rc : 0;
+       return rc;
 }
 
 /*
diff --git a/at.h b/at.h
index 97c545a..abd4d90 100644 (file)
--- a/at.h
+++ b/at.h
 #ifndef _HAYES_RIL_AT_H_
 #define _HAYES_RIL_AT_H_
 
+/*
+ * Defines
+ */
+
 #define AT_SYNC_LOCK() pthread_mutex_lock(&(at_responses_handling.sync_mutex));
 #define AT_SYNC_UNLOCK() pthread_mutex_unlock(&(at_responses_handling.sync_mutex));
 #define AT_ASYNC_LOCK() pthread_mutex_lock(&(at_responses_handling.async_mutex));
 #define AT_ASYNC_UNLOCK() pthread_mutex_unlock(&(at_responses_handling.async_mutex));
-#define AT_RESPONSES_LOCK() pthread_mutex_lock(&(at_responses_handling.responses_queue_mutex));
-#define AT_RESPONSES_UNLOCK() pthread_mutex_unlock(&(at_responses_handling.responses_queue_mutex));
+#define AT_RESPONSES_LOCK() pthread_mutex_lock(&(at_responses_handling.responses_mutex));
+#define AT_RESPONSES_UNLOCK() pthread_mutex_unlock(&(at_responses_handling.responses_mutex));
+#define AT_RESPONSES_QUEUE_LOCK() pthread_mutex_lock(&(at_responses_handling.responses_queue_mutex));
+#define AT_RESPONSES_QUEUE_UNLOCK() pthread_mutex_unlock(&(at_responses_handling.responses_queue_mutex));
 
 #define AT_PARSE_SINGLE_QUOTE  (1 << 0)
 #define AT_PARSE_DOUBLE_QUOTE  (1 << 1)
 
+/*
+ * Variables structures
+ */
+
 struct at_response {
        char *command;
 
@@ -58,6 +68,12 @@ enum {
        AT_STATUS_INTERNAL_ERROR,
 };
 
+enum {
+       AT_RESPONSE_UNHANDELD,
+       AT_RESPONSE_HANDLED_OK,
+       AT_RESPONSE_HANDLED_ERROR,
+};
+
 struct at_responses_queue {
        struct at_response **responses;
        int responses_count;
@@ -79,8 +95,9 @@ struct at_async_queue {
 };
 
 struct at_responses_handling {
-       struct at_responses_queue responses_queue;
        pthread_mutex_t responses_queue_mutex;
+       struct at_responses_queue responses_queue;
+       pthread_mutex_t responses_mutex;
 
        struct at_async_queue async_queue;
        pthread_mutex_t async_mutex;
@@ -90,6 +107,10 @@ struct at_responses_handling {
        pthread_mutex_t sync_mutex;
 };
 
+/*
+ * Functions
+ */
+
 // Utilities
 int at_strings_compare(char *major, char *minor);
 int at_commands_compare(char *major, char *minor);