OSDN Git Service

android/gatt: Handle get descriptor client command
authorJakub Tyszkowski <jakub.tyszkowski@tieto.com>
Tue, 1 Apr 2014 15:19:24 +0000 (17:19 +0200)
committerSzymon Janc <szymon.janc@tieto.com>
Thu, 3 Apr 2014 14:25:08 +0000 (16:25 +0200)
This adds basic get characteristic descriptor command handling. In case
of no descriptor, proper notification with bad status is being sent.

android/gatt.c

index cb6065a..51e7c07 100644 (file)
@@ -1420,12 +1420,94 @@ done:
                                HAL_OP_GATT_CLIENT_GET_CHARACTERISTIC, status);
 }
 
+static void send_client_descr_notify(int32_t status, int32_t conn_id,
+                                       bool primary,
+                                       const struct element_id *srvc,
+                                       const struct element_id *ch,
+                                       const struct element_id *opt_descr)
+{
+       struct hal_ev_gatt_client_get_descriptor ev;
+
+       memset(&ev, 0, sizeof(ev));
+
+       ev.status = status;
+       ev.conn_id = conn_id;
+
+       element_id_to_hal_srvc_id(srvc, primary, &ev.srvc_id);
+       element_id_to_hal_gatt_id(ch, &ev.char_id);
+
+       if (opt_descr)
+               element_id_to_hal_gatt_id(opt_descr, &ev.descr_id);
+
+       ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
+                       HAL_EV_GATT_CLIENT_GET_DESCRIPTOR, sizeof(ev), &ev);
+}
+
 static void handle_client_get_descriptor(const void *buf, uint16_t len)
 {
+       const struct hal_cmd_gatt_client_get_descriptor *cmd = buf;
+       struct descriptor *descr = NULL;
+       struct characteristic *ch;
+       struct service *srvc;
+       struct element_id srvc_id;
+       struct element_id char_id;
+       struct gatt_device *dev;
+       int32_t conn_id;
+       uint8_t primary;
+       uint8_t status;
+
        DBG("");
 
+       if ((len != sizeof(*cmd) + cmd->number * sizeof(cmd->gatt_id[0])) ||
+                               (cmd->number != 1 && cmd->number != 2)) {
+               error("gatt: Invalid get descr command (%u bytes), terminating",
+                                                                       len);
+
+               raise(SIGTERM);
+               return;
+       }
+
+       conn_id = cmd->conn_id;
+       primary = cmd->srvc_id.is_primary;
+
+       hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
+       hal_gatt_id_to_element_id(&cmd->gatt_id[0], &char_id);
+
+       if (!find_service(conn_id, &srvc_id, &dev, &srvc)) {
+               error("gatt: Get descr. could not find service");
+
+               status = HAL_STATUS_FAILED;
+               goto failed;
+       }
+
+       ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
+       if (!ch) {
+               error("gatt: Get descr. could not find characteristic");
+
+               status = HAL_STATUS_FAILED;
+               goto failed;
+       }
+
+       if (queue_isempty(ch->descriptors)) {
+               /* TODO: Build the cache */
+
+               ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
+                                       HAL_OP_GATT_CLIENT_GET_DESCRIPTOR,
+                                       HAL_STATUS_FAILED);
+               return;
+       }
+
+       status = HAL_STATUS_FAILED;
+
+       /* TODO: Send from cache */
+
+failed:
+       send_client_descr_notify(descr ? GATT_SUCCESS : GATT_FAILURE, conn_id,
+                                               primary, &srvc_id, &char_id,
+                                               &descr->id);
+
        ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
-                       HAL_OP_GATT_CLIENT_GET_DESCRIPTOR, HAL_STATUS_FAILED);
+                               HAL_OP_GATT_CLIENT_GET_DESCRIPTOR, status);
 }
 
 struct read_char_data {