| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2019-2022, Jacques Gagnon | ||
| 3 | * SPDX-License-Identifier: Apache-2.0 | ||
| 4 | */ | ||
| 5 | |||
| 6 | #include <stdio.h> | ||
| 7 | #include "host.h" | ||
| 8 | #include "hci.h" | ||
| 9 | #include "att.h" | ||
| 10 | #include "zephyr/uuid.h" | ||
| 11 | #include "zephyr/att.h" | ||
| 12 | #include "zephyr/gatt.h" | ||
| 13 | |||
| 14 | static uint16_t max_mtu = 23; | ||
| 15 | |||
| 16 | ✗ | void bt_att_cmd(uint16_t handle, uint8_t code, uint16_t len) { | |
| 17 | ✗ | uint16_t packet_len = (BT_HCI_H4_HDR_SIZE + BT_HCI_ACL_HDR_SIZE | |
| 18 | + sizeof(struct bt_l2cap_hdr) + sizeof(struct bt_att_hdr) + len); | ||
| 19 | |||
| 20 | ✗ | bt_hci_pkt_tmp.h4_hdr.type = BT_HCI_H4_TYPE_ACL; | |
| 21 | |||
| 22 | ✗ | bt_hci_pkt_tmp.acl_hdr.handle = bt_acl_handle_pack(handle, 0x2); | |
| 23 | ✗ | bt_hci_pkt_tmp.acl_hdr.len = packet_len - BT_HCI_H4_HDR_SIZE - BT_HCI_ACL_HDR_SIZE; | |
| 24 | |||
| 25 | ✗ | bt_hci_pkt_tmp.l2cap_hdr.len = bt_hci_pkt_tmp.acl_hdr.len - sizeof(bt_hci_pkt_tmp.l2cap_hdr); | |
| 26 | ✗ | bt_hci_pkt_tmp.l2cap_hdr.cid = BT_L2CAP_CID_ATT; | |
| 27 | |||
| 28 | ✗ | bt_hci_pkt_tmp.att_hdr.code = code; | |
| 29 | |||
| 30 | ✗ | bt_host_txq_add((uint8_t *)&bt_hci_pkt_tmp, packet_len); | |
| 31 | ✗ | } | |
| 32 | |||
| 33 | ✗ | void bt_att_cmd_error_rsp(uint16_t handle, uint8_t req_opcode, uint16_t err_handle, uint8_t err) { | |
| 34 | ✗ | struct bt_att_error_rsp *error_rsp = (struct bt_att_error_rsp *)bt_hci_pkt_tmp.att_data; | |
| 35 | |||
| 36 | ✗ | error_rsp->request = req_opcode; | |
| 37 | ✗ | error_rsp->handle = err_handle; | |
| 38 | ✗ | error_rsp->error = err; | |
| 39 | |||
| 40 | ✗ | bt_att_cmd(handle, BT_ATT_OP_ERROR_RSP, sizeof(*error_rsp)); | |
| 41 | ✗ | } | |
| 42 | |||
| 43 | ✗ | void bt_att_cmd_mtu_req(uint16_t handle, uint16_t mtu) { | |
| 44 | ✗ | struct bt_att_exchange_mtu_req *mtu_req = (struct bt_att_exchange_mtu_req *)bt_hci_pkt_tmp.att_data; | |
| 45 | |||
| 46 | ✗ | mtu_req->mtu = mtu; | |
| 47 | |||
| 48 | ✗ | bt_att_cmd(handle, BT_ATT_OP_MTU_REQ, sizeof(*mtu_req)); | |
| 49 | ✗ | } | |
| 50 | |||
| 51 | ✗ | void bt_att_cmd_mtu_rsp(uint16_t handle, uint16_t mtu) { | |
| 52 | ✗ | struct bt_att_exchange_mtu_rsp *mtu_rsp = (struct bt_att_exchange_mtu_rsp *)bt_hci_pkt_tmp.att_data; | |
| 53 | |||
| 54 | ✗ | mtu_rsp->mtu = mtu; | |
| 55 | |||
| 56 | ✗ | bt_att_cmd(handle, BT_ATT_OP_MTU_RSP, sizeof(*mtu_rsp)); | |
| 57 | ✗ | } | |
| 58 | |||
| 59 | ✗ | void bt_att_cmd_find_info_req(uint16_t handle, uint16_t start, uint16_t end) { | |
| 60 | ✗ | struct bt_att_find_info_req *find_info_req = (struct bt_att_find_info_req *)bt_hci_pkt_tmp.att_data; | |
| 61 | |||
| 62 | ✗ | find_info_req->start_handle = start; | |
| 63 | ✗ | find_info_req->end_handle = end; | |
| 64 | |||
| 65 | ✗ | bt_att_cmd(handle, BT_ATT_OP_FIND_INFO_REQ, sizeof(*find_info_req)); | |
| 66 | ✗ | } | |
| 67 | |||
| 68 | ✗ | void bt_att_cmd_read_type_req_uuid16(uint16_t handle, uint16_t start, uint16_t end, uint16_t uuid) { | |
| 69 | ✗ | struct bt_att_read_type_req *read_type_req = (struct bt_att_read_type_req *)bt_hci_pkt_tmp.att_data; | |
| 70 | ✗ | printf("# %s\n", __FUNCTION__); | |
| 71 | |||
| 72 | ✗ | read_type_req->start_handle = start; | |
| 73 | ✗ | read_type_req->end_handle = end; | |
| 74 | ✗ | *(uint16_t *)read_type_req->uuid = uuid; | |
| 75 | |||
| 76 | ✗ | bt_att_cmd(handle, BT_ATT_OP_READ_TYPE_REQ, sizeof(read_type_req->start_handle) | |
| 77 | + sizeof(read_type_req->start_handle) + sizeof(uuid)); | ||
| 78 | ✗ | } | |
| 79 | |||
| 80 | ✗ | void bt_att_cmd_read_req(uint16_t handle, uint16_t att_handle) { | |
| 81 | ✗ | struct bt_att_read_req *read_req = (struct bt_att_read_req *)bt_hci_pkt_tmp.att_data; | |
| 82 | |||
| 83 | ✗ | read_req->handle = att_handle; | |
| 84 | |||
| 85 | ✗ | bt_att_cmd(handle, BT_ATT_OP_READ_REQ, sizeof(*read_req)); | |
| 86 | ✗ | } | |
| 87 | |||
| 88 | ✗ | void bt_att_cmd_read_blob_req(uint16_t handle, uint16_t att_handle, uint16_t offset) { | |
| 89 | ✗ | struct bt_att_read_blob_req *read_blob_req = (struct bt_att_read_blob_req *)bt_hci_pkt_tmp.att_data; | |
| 90 | ✗ | printf("# %s\n", __FUNCTION__); | |
| 91 | |||
| 92 | ✗ | read_blob_req->handle = att_handle; | |
| 93 | ✗ | read_blob_req->offset = offset; | |
| 94 | |||
| 95 | ✗ | bt_att_cmd(handle, BT_ATT_OP_READ_BLOB_REQ, sizeof(*read_blob_req)); | |
| 96 | ✗ | } | |
| 97 | |||
| 98 | ✗ | void bt_att_cmd_read_group_req_uuid16(uint16_t handle, uint16_t start, uint16_t uuid) { | |
| 99 | ✗ | struct bt_att_read_group_req *read_group_req = (struct bt_att_read_group_req *)bt_hci_pkt_tmp.att_data; | |
| 100 | |||
| 101 | ✗ | read_group_req->start_handle = start; | |
| 102 | ✗ | read_group_req->end_handle = 0xFFFF; | |
| 103 | ✗ | *(uint16_t *)read_group_req->uuid = uuid; | |
| 104 | |||
| 105 | ✗ | bt_att_cmd(handle, BT_ATT_OP_READ_GROUP_REQ, sizeof(read_group_req->start_handle) | |
| 106 | + sizeof(read_group_req->start_handle) + sizeof(uuid)); | ||
| 107 | ✗ | } | |
| 108 | |||
| 109 | ✗ | void bt_att_cmd_write_req(uint16_t handle, uint16_t att_handle, uint8_t *data, uint32_t len) { | |
| 110 | ✗ | struct bt_att_write_req *write_req = (struct bt_att_write_req *)bt_hci_pkt_tmp.att_data; | |
| 111 | |||
| 112 | ✗ | write_req->handle = att_handle; | |
| 113 | ✗ | memcpy(write_req->value, data, len); | |
| 114 | |||
| 115 | ✗ | bt_att_cmd(handle, BT_ATT_OP_WRITE_REQ, sizeof(write_req->handle) + len); | |
| 116 | ✗ | } | |
| 117 | |||
| 118 | ✗ | void bt_att_cmd_write_cmd(uint16_t handle, uint16_t att_handle, uint8_t *data, uint32_t len) { | |
| 119 | ✗ | struct bt_att_write_cmd *write_cmd = (struct bt_att_write_cmd *)bt_hci_pkt_tmp.att_data; | |
| 120 | |||
| 121 | ✗ | write_cmd->handle = att_handle; | |
| 122 | ✗ | memcpy(write_cmd->value, data, len); | |
| 123 | |||
| 124 | ✗ | bt_att_cmd(handle, BT_ATT_OP_WRITE_CMD, sizeof(write_cmd->handle) + len); | |
| 125 | ✗ | } | |
| 126 | |||
| 127 | ✗ | void bt_att_cmd_wr_rsp(uint16_t handle) { | |
| 128 | |||
| 129 | ✗ | bt_att_cmd(handle, BT_ATT_OP_WRITE_RSP, 0); | |
| 130 | ✗ | } | |
| 131 | |||
| 132 | ✗ | void bt_att_cmd_prep_wr_rsp(uint16_t handle, uint8_t *data, uint32_t data_len) { | |
| 133 | |||
| 134 | ✗ | memcpy(bt_hci_pkt_tmp.att_data, data, data_len); | |
| 135 | |||
| 136 | ✗ | bt_att_cmd(handle, BT_ATT_OP_PREPARE_WRITE_RSP, data_len); | |
| 137 | ✗ | } | |
| 138 | |||
| 139 | ✗ | void bt_att_cmd_exec_wr_rsp(uint16_t handle) { | |
| 140 | |||
| 141 | ✗ | bt_att_cmd(handle, BT_ATT_OP_EXEC_WRITE_RSP, 0); | |
| 142 | ✗ | } | |
| 143 | |||
| 144 | ✗ | void bt_att_set_le_max_mtu(uint16_t le_max_mtu) { | |
| 145 | ✗ | max_mtu = le_max_mtu; | |
| 146 | ✗ | printf("# %s %d\n", __FUNCTION__, max_mtu); | |
| 147 | ✗ | } | |
| 148 | |||
| 149 | ✗ | uint16_t bt_att_get_le_max_mtu(void) { | |
| 150 | ✗ | return max_mtu; | |
| 151 | } | ||
| 152 |