move qmi_get_error_str to into utils.c
[project/uqmi.git] / dev.c
diff --git a/dev.c b/dev.c
index 9bf7ab20f904b5ac631c3d2538c5f6384139f292..495a7be14794a3a7c0f5e59e8f4d3207fcbaa27f 100644 (file)
--- a/dev.c
+++ b/dev.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <strings.h>
 #include "uqmi.h"
 #include "qmi-errors.h"
 #include "qmi-errors.c"
@@ -37,14 +38,6 @@ static const uint8_t qmi_services[__QMI_SERVICE_LAST] = {
 };
 #undef __qmi_service
 
-static struct {
-       struct mbim_command_message mbim;
-       union {
-               char buf[512];
-               struct qmi_msg msg;
-       } u;
-} __packed msgbuf;
-
 #ifdef DEBUG_PACKET
 void dump_packet(const char *prefix, void *ptr, int len)
 {
@@ -70,6 +63,34 @@ qmi_get_service_idx(QmiService svc)
        return -1;
 }
 
+static bool qmi_message_is_response(struct qmi_msg *msg)
+{
+       if (msg->qmux.service == QMI_SERVICE_CTL) {
+               if (msg->flags & QMI_CTL_FLAG_RESPONSE)
+                       return true;
+       }
+       else {
+               if (msg->flags & QMI_SERVICE_FLAG_RESPONSE)
+                       return true;
+       }
+
+       return false;
+}
+
+static bool qmi_message_is_indication(struct qmi_msg *msg)
+{
+       if (msg->qmux.service == QMI_SERVICE_CTL) {
+               if (msg->flags & QMI_CTL_FLAG_INDICATION)
+                       return true;
+       }
+       else {
+               if (msg->flags & QMI_SERVICE_FLAG_INDICATION)
+                       return true;
+       }
+
+       return false;
+}
+
 static void __qmi_request_complete(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
 {
        void *tlv_buf;
@@ -104,6 +125,27 @@ static void qmi_process_msg(struct qmi_dev *qmi, struct qmi_msg *msg)
        struct qmi_request *req;
        uint16_t tid;
 
+       if (qmi_message_is_indication(msg)) {
+               if (msg->qmux.service == QMI_SERVICE_CTL) {
+                       struct qmi_msg sync_msg = {0};
+                       qmi_set_ctl_sync_request(&sync_msg);
+                       /* A SYNC indication might be sent on boot in order to indicate
+                        * that all Client IDs have been deallocated by the modem:
+                        * cancel all requests, as they will not be answered. */
+                       if (msg->ctl.message == sync_msg.ctl.message) {
+                               while (!list_empty(&qmi->req)) {
+                                       req = list_first_entry(&qmi->req, struct qmi_request, list);
+                                       qmi_request_cancel(qmi, req);
+                               }
+                       }
+               }
+
+               return;
+       }
+
+       if (!qmi_message_is_response(msg))
+               return;
+
        if (msg->qmux.service == QMI_SERVICE_CTL)
                tid = msg->ctl.transaction;
        else
@@ -162,11 +204,12 @@ static void qmi_notify_read(struct ustream *us, int bytes)
        }
 }
 
-int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, request_cb cb)
+int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, request_cb cb)
 {
+       struct qmi_msg *msg = qmi->buf;
        int len = qmi_complete_request_message(msg);
        uint16_t tid;
-       char *buf = (void *) msg;
+       void *buf = (void *) qmi->buf;
 
        memset(req, 0, sizeof(*req));
        req->ret = -1;
@@ -207,6 +250,12 @@ void qmi_request_cancel(struct qmi_dev *qmi, struct qmi_request *req)
        __qmi_request_complete(qmi, req, NULL);
 }
 
+/*! Run uloop until the request has been completed
+ *
+ * \param qmi the qmi device
+ * \param req the request to wait for
+ * \return req->ret (0 on success)
+ */
 int qmi_request_wait(struct qmi_dev *qmi, struct qmi_request *req)
 {
        bool complete = false;
@@ -260,7 +309,7 @@ int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id)
        };
        struct qmi_connect_request req;
        int idx = qmi_get_service_idx(svc);
-       struct qmi_msg *msg = &msgbuf.u.msg;
+       struct qmi_msg *msg = qmi->buf;
 
        if (idx < 0)
                return -1;
@@ -270,7 +319,7 @@ int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id)
 
        if (client_id < 0) {
                qmi_set_ctl_allocate_cid_request(msg, &creq);
-               qmi_request_start(qmi, &req.req, msg, qmi_connect_service_cb);
+               qmi_request_start(qmi, &req.req, qmi_connect_service_cb);
                qmi_request_wait(qmi, &req.req);
 
                if (req.req.ret)
@@ -299,14 +348,14 @@ static void __qmi_service_disconnect(struct qmi_dev *qmi, int idx)
                )
        };
        struct qmi_request req;
-       struct qmi_msg *msg = &msgbuf.u.msg;
+       struct qmi_msg *msg = qmi->buf;
 
        qmi->service_connected &= ~(1 << idx);
        qmi->service_data[idx].client_id = -1;
        qmi->service_data[idx].tid = 0;
 
        qmi_set_ctl_release_cid_request(msg, &creq);
-       qmi_request_start(qmi, &req, msg, NULL);
+       qmi_request_start(qmi, &req, NULL);
        qmi_request_wait(qmi, &req);
 }
 
@@ -347,6 +396,13 @@ int qmi_service_get_client_id(struct qmi_dev *qmi, QmiService svc)
 
 int qmi_device_open(struct qmi_dev *qmi, const char *path)
 {
+       static struct {
+               struct mbim_command_message mbim;
+               union {
+                       char buf[2048];
+                       struct qmi_msg msg;
+               } u;
+       } __packed msgbuf;
        struct ustream *us = &qmi->sf.stream;
        int fd;
 
@@ -360,6 +416,7 @@ int qmi_device_open(struct qmi_dev *qmi, const char *path)
        ustream_fd_init(&qmi->sf, fd);
        INIT_LIST_HEAD(&qmi->req);
        qmi->ctl_tid = 1;
+       qmi->buf = msgbuf.u.buf;
 
        return 0;
 }
@@ -401,15 +458,3 @@ QmiService qmi_service_get_by_name(const char *str)
 
        return -1;
 }
-
-const char *qmi_get_error_str(int code)
-{
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(qmi_errors); i++) {
-               if (qmi_errors[i].code == code)
-                       return qmi_errors[i].text;
-       }
-
-       return "Unknown error";
-}