aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/devices/ble_hci/common-hal/_bleio/Characteristic.c
blob: e2f9cad0efd7eef321b77bfe133da8df13734348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) Dan Halbert for Adafruit Industries
 * Copyright (c) 2018 Artur Pacholec
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "py/runtime.h"

#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Characteristic.h"
#include "shared-bindings/_bleio/CharacteristicBuffer.h"
#include "shared-bindings/_bleio/Descriptor.h"
#include "shared-bindings/_bleio/PacketBuffer.h"
#include "shared-bindings/_bleio/Service.h"

#include "common-hal/_bleio/Adapter.h"
#include "common-hal/_bleio/att.h"

#define CCCD_NOTIFY   0x1
#define CCCD_INDICATE 0x2


void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_service_obj_t *service, uint16_t handle, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props, bleio_attribute_security_mode_t read_perm, bleio_attribute_security_mode_t write_perm, mp_int_t max_length, bool fixed_length, mp_buffer_info_t *initial_value_bufinfo, const char *user_description) {
    self->service = service;
    self->uuid = uuid;
    self->decl_handle = BLE_GATT_HANDLE_INVALID;
    self->handle = BLE_GATT_HANDLE_INVALID;
    self->props = props;
    self->read_perm = read_perm;
    self->write_perm = write_perm;
    self->descriptor_list = mp_obj_new_list(0, NULL);
    self->observer = mp_const_none;
    self->user_desc = NULL;
    self->cccd = NULL;
    self->sccd = NULL;
    self->value = mp_obj_new_bytes(initial_value_bufinfo->buf, initial_value_bufinfo->len);

    const mp_int_t max_length_max = 512;
    if (max_length < 0 || max_length > max_length_max) {
        mp_raise_ValueError(translate("max_length must be <= 512"));
    }
    self->max_length = max_length;
    self->fixed_length = fixed_length;

    if (service->is_remote) {
        self->handle = handle;
    } else {
        common_hal_bleio_service_add_characteristic(self->service, self, initial_value_bufinfo, user_description);
    }
}

mp_obj_tuple_t *common_hal_bleio_characteristic_get_descriptors(bleio_characteristic_obj_t *self) {
    return mp_obj_new_tuple(self->descriptor_list->len, self->descriptor_list->items);
}

bleio_service_obj_t *common_hal_bleio_characteristic_get_service(bleio_characteristic_obj_t *self) {
    return self->service;
}

size_t common_hal_bleio_characteristic_get_max_length(bleio_characteristic_obj_t *self) {
    return self->max_length;
}

size_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self, uint8_t *buf, size_t len) {
    // Do GATT operations only if this characteristic has been added to a registered service.
    if (self->handle != BLE_GATT_HANDLE_INVALID) {
        // FIX uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
        if (common_hal_bleio_service_get_is_remote(self->service)) {
            // FIX read remote chars
            // uint8_t rsp[MAX(len, 512)];
            // FIX improve att_read_req to write into our requested buffer.
            // return att_read_req(conn_handle, self->handle, rsp);
            return 0; // FIX
        } else {
            mp_buffer_info_t bufinfo;
            if (!mp_get_buffer(self->value, &bufinfo, MP_BUFFER_READ)) {
                return 0;
            }
            const size_t actual_length = MIN(len, bufinfo.len);
            memcpy(buf, bufinfo.buf, actual_length);
            return actual_length;
        }
    }

    return 0;
}

void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
    if (self->fixed_length && bufinfo->len != self->max_length) {
        mp_raise_ValueError(translate("Value length != required fixed length"));
    }
    if (bufinfo->len > self->max_length) {
        mp_raise_ValueError(translate("Value length > max_length"));
    }

    // Do GATT operations only if this characteristic has been added to a registered service.
    if (self->handle != BLE_GATT_HANDLE_INVALID) {
        if (common_hal_bleio_service_get_is_remote(self->service)) {
            // FIX uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
            if (self->props & CHAR_PROP_WRITE) {
                // FIX writing remote chars
                // uint8_t rsp[sizeof(bt_att_error_rsp)];
                // att_write_req(conn_handle, self->handle, bufinfo->buf, bufinfo->len, rsp);
            } else if (self->props & CHAR_PROP_WRITE_NO_RESPONSE) {
                // att_write_cmd(conn_handle, self->handle, bufinfo->buff, bufinfo->len);
            } else {
                mp_raise_bleio_BluetoothError(translate("Characteristic not writable"));
            }
        } else {
            // Always write the value locally even if no connections are active.
            bleio_characteristic_set_local_value(self, bufinfo);
            // Notify or indicate all active connections.

            uint16_t cccd_value = 0;
            mp_buffer_info_t cccd_bufinfo = {
                .buf = &cccd_value,
                .len = sizeof(cccd_value),
            };

            const bool notify = self->props & CHAR_PROP_NOTIFY;
            const bool indicate = self->props & CHAR_PROP_INDICATE;
            // Read the CCCD value, if there is one.
            if ((notify | indicate) && self->cccd != NULL) {
                common_hal_bleio_descriptor_get_value(self->cccd, cccd_bufinfo.buf, cccd_bufinfo.len);
            }

            // It's possible that both notify and indicate are set.
            if (notify && (cccd_value & CCCD_NOTIFY)) {
                att_notify(self->handle, bufinfo->buf, MIN(bufinfo->len, self->max_length));
            }
            if (indicate && (cccd_value & CCCD_INDICATE)) {
                att_indicate(self->handle, bufinfo->buf, MIN(bufinfo->len, self->max_length));

            }
        }
    }
}

bleio_uuid_obj_t *common_hal_bleio_characteristic_get_uuid(bleio_characteristic_obj_t *self) {
    return self->uuid;
}

bleio_characteristic_properties_t common_hal_bleio_characteristic_get_properties(bleio_characteristic_obj_t *self) {
    return self->props;
}

void common_hal_bleio_characteristic_add_descriptor(bleio_characteristic_obj_t *self, bleio_descriptor_obj_t *descriptor) {
    if (self->handle != common_hal_bleio_adapter_obj.last_added_characteristic_handle) {
        mp_raise_bleio_BluetoothError(
            translate("Descriptor can only be added to most recently added characteristic"));
    }

    descriptor->handle = bleio_adapter_add_attribute(&common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(descriptor));
    // Include this descriptor in the service handle's range.
    self->service->end_handle = descriptor->handle;

    mp_obj_list_append(MP_OBJ_FROM_PTR(self->descriptor_list),
        MP_OBJ_FROM_PTR(descriptor));
}

void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self, bool notify, bool indicate) {
    if (self->cccd == NULL) {
        mp_raise_bleio_BluetoothError(translate("No CCCD for this Characteristic"));
    }

    if (!common_hal_bleio_service_get_is_remote(self->service)) {
        mp_raise_bleio_RoleError(translate("Can't set CCCD on local Characteristic"));
    }

    const uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
    common_hal_bleio_check_connected(conn_handle);

    uint16_t cccd_value =
        (notify ? CCCD_NOTIFY : 0) |
        (indicate ? CCCD_INDICATE : 0);

    // FIX do remote
    (void)cccd_value;
    // uint8_t rsp[sizeof(bt_att_error_rsp)];
    // if (att_write_req(conn_handle, self->cccd->handle, &cccd_value, sizeof(cccd_value)) == 0) {
    //     mp_raise_bleio_BluetoothError(translate("Could not write CCCD"));
    // }
}

bool bleio_characteristic_set_local_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
    if (self->fixed_length && bufinfo->len != self->max_length) {
        return false;
    }
    if (bufinfo->len > self->max_length) {
        return false;
    }

    self->value = mp_obj_new_bytes(bufinfo->buf, bufinfo->len);

    if (mp_obj_is_type(self->observer, &bleio_characteristic_buffer_type)) {
        bleio_characteristic_buffer_update(MP_OBJ_FROM_PTR(self->observer), bufinfo);
    } else if (mp_obj_is_type(self->observer, &bleio_packet_buffer_type)) {
        bleio_packet_buffer_update(MP_OBJ_FROM_PTR(self->observer), bufinfo);
    } else {
        return false;
    }
    return true;
}

void bleio_characteristic_set_observer(bleio_characteristic_obj_t *self, mp_obj_t observer) {
    self->observer = observer;
}

void bleio_characteristic_clear_observer(bleio_characteristic_obj_t *self) {
    self->observer = mp_const_none;
}