|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Artur Pacholec |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <string.h> |
| 28 | +#include <stdio.h> |
| 29 | + |
| 30 | +#include "ble_drv.h" |
| 31 | +#include "ble_gatts.h" |
| 32 | +#include "nrf_soc.h" |
| 33 | + |
| 34 | +#include "py/runtime.h" |
| 35 | + |
| 36 | +#include "common-hal/bleio/__init__.h" |
| 37 | +#include "common-hal/bleio/CharacteristicBuffer.h" |
| 38 | + |
| 39 | +STATIC void characteristic_buffer_on_ble_evt(ble_evt_t *ble_evt, void *param) { |
| 40 | + bleio_characteristic_buffer_obj_t *self = (bleio_characteristic_buffer_obj_t *) param; |
| 41 | + switch (ble_evt->header.evt_id) { |
| 42 | + case BLE_GATTS_EVT_WRITE: { |
| 43 | + ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write; |
| 44 | + // Event handle must match the handle for my characteristic. |
| 45 | + if (evt_write->handle == self->characteristic->handle) { |
| 46 | + // Push all the data onto the ring buffer. |
| 47 | + for (size_t i = 0; i < evt_write->len; i++) { |
| 48 | + ringbuf_put(&self->ringbuf, evt_write->data[i]); |
| 49 | + } |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +// Assumes that buffer_size has been validated before call. |
| 58 | +void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self, bleio_characteristic_obj_t *characteristic, size_t buffer_size) { |
| 59 | + |
| 60 | + self->characteristic = characteristic; |
| 61 | + // This is a macro. |
| 62 | + ringbuf_alloc(&self->ringbuf, buffer_size); |
| 63 | + |
| 64 | + ble_drv_add_event_handler(characteristic_buffer_on_ble_evt, self); |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +// Returns a uint8_t byte value, or -1 if no data is available. |
| 69 | +int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self) { |
| 70 | + return ringbuf_get(&self->ringbuf); |
| 71 | +} |
| 72 | + |
| 73 | +void common_hal_bleio_characteristic_buffer_deinit(bleio_characteristic_buffer_obj_t *self) { |
| 74 | + ble_drv_remove_event_handler(characteristic_buffer_on_ble_evt, self); |
| 75 | +} |
0 commit comments