My fork
Dependencies: BLE_API nRF51822-bluetooth-mdw
Fork of microbit-dal by
Diff: source/bluetooth/MicroBitUARTService.cpp
- Revision:
- 65:f7ebabf23e15
- Parent:
- 58:2ac8d45f1b08
- Child:
- 66:2fc7d7c2fffc
--- a/source/bluetooth/MicroBitUARTService.cpp Wed Jul 13 12:18:43 2016 +0100 +++ b/source/bluetooth/MicroBitUARTService.cpp Wed Jul 13 12:18:45 2016 +0100 @@ -40,14 +40,17 @@ static uint8_t txBufferHead = 0; static uint8_t txBufferTail = 0; -static GattCharacteristic* txCharacteristic = NULL; +static GattCharacteristic* rxCharacteristic = NULL; /** - * A callback function for whenever a Bluetooth device consumes our TX Buffer + * A callback function for whenever a Bluetooth device consumes our RX Buffer */ -void on_confirmation(uint16_t handle) +void on_confirmation_received_callback(uint16_t handle) { - if(handle == txCharacteristic->getValueAttribute().getHandle()) +#if CONFIG_ENABLED(MICROBIT_DBG) + SERIAL_DEBUG->printf("RECEIVED!! %d \r\n",handle); +#endif + if(handle == rxCharacteristic->getValueAttribute().getHandle()) { txBufferTail = txBufferHead; MicroBitEvent(MICROBIT_ID_NOTIFY, MICROBIT_UART_S_EVT_TX_EMPTY); @@ -78,27 +81,27 @@ txBufferTail = 0; this->txBufferSize = txBufferSize; - GattCharacteristic rxCharacteristic(UARTServiceRXCharacteristicUUID, rxBuffer, 1, rxBufferSize, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); + GattCharacteristic txCharacteristic(UARTServiceTXCharacteristicUUID, rxBuffer, 1, rxBufferSize, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); - txCharacteristic = new GattCharacteristic(UARTServiceTXCharacteristicUUID, txBuffer, 1, txBufferSize, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); + rxCharacteristic = new GattCharacteristic(UARTServiceRXCharacteristicUUID, txBuffer, 1, txBufferSize, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); - GattCharacteristic *charTable[] = {txCharacteristic, &rxCharacteristic}; + GattCharacteristic *charTable[] = {&txCharacteristic, rxCharacteristic}; GattService uartService(UARTServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); _ble.addService(uartService); - this->rxCharacteristicHandle = rxCharacteristic.getValueAttribute().getHandle(); + this->txCharacteristicHandle = txCharacteristic.getValueAttribute().getHandle(); _ble.gattServer().onDataWritten(this, &MicroBitUARTService::onDataWritten); - _ble.gattServer().onConfirmationReceived(on_confirmation); + _ble.gattServer().onConfirmationReceived(on_confirmation_received_callback); } /** - * A callback function for whenever a Bluetooth device writes to our RX characteristic. + * A callback function for whenever a Bluetooth device writes to our TX characteristic. */ void MicroBitUARTService::onDataWritten(const GattWriteCallbackParams *params) { - if (params->handle == this->rxCharacteristicHandle) + if (params->handle == this->txCharacteristicHandle) { uint16_t bytesWritten = params->len; @@ -207,24 +210,11 @@ * * @param c the character to transmit * - * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode - * gives a different behaviour: - * - * ASYNC - Will copy as many characters as it can into the buffer for transmission, - * and return control to the user. - * - * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER - * - * SYNC_SLEEP - Will perform a cooperative blocking wait until all - * given characters have been received by the connected - * device. - * - * @return the number of characters written, or MICROBIT_NOT_SUPPORTED if there is - * no connected device, or the connected device has not enabled indications. + * @return the number of characters written (0, or 1). */ -int MicroBitUARTService::putc(char c, MicroBitSerialMode mode) +int MicroBitUARTService::putc(char c) { - return (send((uint8_t *)&c, 1, mode) == 1) ? 1 : EOF; + return (send((uint8_t *)&c, 1) == 1) ? 1 : EOF; } /** @@ -232,38 +222,21 @@ * * @param buf a buffer containing length number of bytes. * @param length the size of the buffer. - * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode - * gives a different behaviour: * - * ASYNC - Will copy as many characters as it can into the buffer for transmission, - * and return control to the user. - * - * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER - * - * SYNC_SLEEP - Will perform a cooperative blocking wait until all - * given characters have been received by the connected - * device. + * @return the number of characters copied into the buffer * - * @return the number of characters written, or MICROBIT_NOT_SUPPORTED if there is - * no connected device, or the connected device has not enabled indications. + * @note no modes for sending are available at the moment, due to interrupt overhead. */ -int MicroBitUARTService::send(const uint8_t *buf, int length, MicroBitSerialMode mode) +int MicroBitUARTService::send(const uint8_t *buf, int length) { - if(length < 1 || mode == SYNC_SPINWAIT) + if(length < 1) return MICROBIT_INVALID_PARAMETER; - bool updatesEnabled = false; - - ble.gattServer().areUpdatesEnabled(*txCharacteristic, &updatesEnabled); - - if(!ble.getGapState().connected && !updatesEnabled) - return MICROBIT_NOT_SUPPORTED; - int bytesWritten = 0; - while(bytesWritten < length && ble.getGapState().connected && updatesEnabled) - { - for(int bufferIterator = bytesWritten; bufferIterator < length; bufferIterator++) + if (ble.getGapState().connected) { + + for(int bufferIterator = 0; bufferIterator < length; bufferIterator++) { int nextHead = (txBufferHead + 1) % txBufferSize; @@ -279,25 +252,27 @@ int size = txBufferedSize(); +#if CONFIG_ENABLED(MICROBIT_DBG) + SERIAL_DEBUG->printf("tx size: %d", size); +#endif + uint8_t temp[size]; memclr(&temp, size); circularCopy(txBuffer, txBufferSize, temp, txBufferTail, txBufferHead); - - if(mode == SYNC_SLEEP) - fiber_wake_on_event(MICROBIT_ID_NOTIFY, MICROBIT_UART_S_EVT_TX_EMPTY); - - ble.gattServer().write(txCharacteristic->getValueAttribute().getHandle(), temp, size); +#if CONFIG_ENABLED(MICROBIT_DBG) + for(int i = 0; i < size; i++) + SERIAL_DEBUG->printf("%c",temp[i]); +#endif - if(mode == SYNC_SLEEP) - schedule(); - else - break; + ble.gattServer().write(rxCharacteristic->getValueAttribute().getHandle(), temp, size); + } - ble.gattServer().areUpdatesEnabled(*txCharacteristic, &updatesEnabled); - } +#if CONFIG_ENABLED(MICROBIT_DBG) + SERIAL_DEBUG->printf("written: %d \r\n",bytesWritten); +#endif return bytesWritten; } @@ -306,24 +281,14 @@ * Copies characters into the buffer used for Transmitting to the central device. * * @param s the string to transmit - * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode - * gives a different behaviour: * - * ASYNC - Will copy as many characters as it can into the buffer for transmission, - * and return control to the user. - * - * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER + * @return the number of characters copied into the buffer * - * SYNC_SLEEP - Will perform a cooperative blocking wait until all - * given characters have been received by the connected - * device. - * - * @return the number of characters written, or MICROBIT_NOT_SUPPORTED if there is - * no connected device, or the connected device has not enabled indications. + * @note no modes for sending are available at the moment, due to interrupt overhead. */ -int MicroBitUARTService::send(ManagedString s, MicroBitSerialMode mode) +int MicroBitUARTService::send(ManagedString s) { - return send((uint8_t *)s.toCharArray(), s.length(), mode); + return send((uint8_t *)s.toCharArray(), s.length()); } /**