qmictl: Port to new request API

Port the two calls in CTL to the new request API.

BUG=chromium-os:27301
TEST=connect_disconnect_test and get_cid_stress_test pass

Change-Id: I3417590c08264580c334296b7e7a739b6aac6b39
diff --git a/src/qmictl.c b/src/qmictl.c
index 7d36079..cd760ad 100644
--- a/src/qmictl.c
+++ b/src/qmictl.c
@@ -48,33 +48,15 @@
   return qmimsg_tlv_get(message, QMI_TLV_ID, sizeof(*id_out), id_out);
 }
 
-static struct qmimsg *ctl_msg_new(uint16_t transaction, uint16_t message)
-{
-  struct qmimsg *msg;
-
-  if (qmimsg_new(0, QMI_SVC_CTL, QMI_CTL_CLIENT,
-                 0, transaction, message,
-                 &msg))
-    return NULL;
-
-  return msg;
-}
-
-static void ctl_criteria(uint16_t transaction,
-                         struct listen_criteria *criteria)
-{
-  criteria->service = QMI_SVC_CTL;
-  criteria->client = QMI_CTL_CLIENT;
-  criteria->transaction = transaction;
-}
-
 struct get_cid_context {
   qmictl_get_cid_response_fn callback;
   void *context;
   uint8_t service;
 };
 
-static int got_cid(struct qmidev *qmidev, void *data,
+static void got_cid(struct qmidev *qmidev,
+                   void *data,
+                   int status,
                    struct qmimsg *message)
 {
   assert(qmidev);
@@ -83,14 +65,10 @@
 
   struct get_cid_context *context = data;
   struct qmi_cid id;
-  uint16_t status; uint16_t error;
   int result;
 
   id.client = 0;
 
-  result = qmi_result(message, &status, &error);
-  if (result)
-    goto out;
   if (status) {
     result = status;
     goto out;
@@ -99,6 +77,7 @@
   result = ctl_get_id(message, &id);
   if (result)
     goto out;
+
   if (id.service != context->service) {
     result = EINVAL;
     goto out;
@@ -109,30 +88,38 @@
 out:
   context->callback(qmidev, context->context, result, id.client);
   g_slice_free(struct get_cid_context, context);
-  return 1;
 }
 
 int qmictl_get_cid(struct qmidev *qmidev, uint8_t service,
                    qmictl_get_cid_response_fn caller_callback,
                    void *caller_context)
 {
+  int result;
+
   struct get_cid_context *context = g_slice_new(struct get_cid_context);
   context->callback = caller_callback;
   context->context  = caller_context;
   context->service  = service;
 
-  uint16_t tid = 0x0001; /* TODO: ACtually allocate a tid. */
-
   struct qmimsg *message;
-  message = ctl_msg_new(tid, QMICTL_MSG_GET_CID);
-  qmimsg_tlv_add(message, QMI_TLV_ID, sizeof(service), &service);
-  qmidev_send(qmidev, message);
+  result = qmidev_make_request(qmidev, QMI_SVC_CTL, QMICTL_MSG_GET_CID,
+                               &message);
+  if (result)
+    goto fail;
 
-  struct listen_criteria criteria;
-  ctl_criteria(tid, &criteria);
-  qmidev_listen(qmidev, &criteria, &got_cid, context);
+  qmimsg_tlv_add(message, QMI_TLV_ID, sizeof(service), &service);
+
+  result = qmidev_send_request(qmidev, message,
+                               QMICTL_MSG_GET_CID,
+                               &got_cid, context);
+  if (result)
+    goto fail;
 
   return 0;
+
+fail:
+  g_slice_free(struct get_cid_context, context);
+  return result;
 }
 
 struct release_cid_context {
@@ -141,8 +128,10 @@
   struct qmi_cid id;
 };
 
-static int released_cid(struct qmidev *qmidev, void *data,
-                        struct qmimsg *message)
+static void released_cid(struct qmidev *qmidev,
+                         void *data,
+                         int status,
+                         struct qmimsg *message)
 {
   assert(qmidev);
   assert(data);
@@ -150,20 +139,17 @@
 
   struct release_cid_context *context = data;
   struct qmi_cid id;
-  uint16_t status; uint16_t error;
   int result;
 
-  result = qmi_result(message, &status, &error);
-  if (result)
-    goto out;
   if (status) {
-    result = error;
+    result = status;
     goto out;
   }
 
   result = ctl_get_id(message, &id);
   if (result)
     goto out;
+
   if (id.service != context->id.service ||
       id.client != context->id.client) {
     result = EINVAL;
@@ -175,13 +161,14 @@
 out:
   context->callback(qmidev, context->context, result);
   g_slice_free(struct release_cid_context, context);
-  return 1;
 }
 
 int qmictl_release_cid(struct qmidev *qmidev, uint8_t service, uint8_t client,
                        qmidev_response_fn caller_callback,
                        void *caller_context)
 {
+  int result;
+
   struct release_cid_context *context =
       g_slice_new(struct release_cid_context);
   context->callback   = caller_callback;
@@ -189,14 +176,23 @@
   context->id.service = service;
   context->id.client  = client;
 
-  uint16_t tid = 0x0001; /* TODO: Allocate a transaction ID. */
-  struct qmimsg *message = ctl_msg_new(tid, QMICTL_MSG_RELEASE_CID);
-  qmimsg_tlv_add(message, QMI_TLV_ID, sizeof(context->id), &context->id);
-  qmidev_send(qmidev, message);
+  struct qmimsg *message;
+  result = qmidev_make_request(qmidev, QMI_SVC_CTL, QMICTL_MSG_RELEASE_CID,
+                               &message);
+  if (result)
+    goto fail;
 
-  struct listen_criteria criteria;
-  ctl_criteria(tid, &criteria);
-  qmidev_listen(qmidev, &criteria, &released_cid, context);
+  qmimsg_tlv_add(message, QMI_TLV_ID, sizeof(context->id), &context->id);
+
+  result = qmidev_send_request(qmidev, message,
+                               QMICTL_MSG_RELEASE_CID,
+                               &released_cid, context);
+  if (result)
+    goto fail;
 
   return 0;
+
+fail:
+  g_slice_free(struct release_cid_context, context);
+  return result;
 }