BLE Chat with Serial
Dependencies: LinkNode_SimpleChat mbed nRF51822
Fork of nRF51822_SimpleChat by
main.cpp@3:17f2d639f37b, 2016-04-13 (annotated)
- Committer:
- helloqi
- Date:
- Wed Apr 13 12:45:39 2016 +0000
- Revision:
- 3:17f2d639f37b
- Parent:
- 2:4b66b69c7ecb
8? BLE Chat with Serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 1:1c058e553423 | 1 | /* |
RedBearLab | 0:cffe8ac1bdf0 | 2 | |
RedBearLab | 1:1c058e553423 | 3 | Copyright (c) 2012-2014 RedBearLab |
RedBearLab | 1:1c058e553423 | 4 | |
RedBearLab | 1:1c058e553423 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
RedBearLab | 1:1c058e553423 | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
RedBearLab | 1:1c058e553423 | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
RedBearLab | 1:1c058e553423 | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
RedBearLab | 1:1c058e553423 | 9 | subject to the following conditions: |
RedBearLab | 1:1c058e553423 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
RedBearLab | 1:1c058e553423 | 11 | |
RedBearLab | 1:1c058e553423 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
RedBearLab | 1:1c058e553423 | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
RedBearLab | 1:1c058e553423 | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
RedBearLab | 1:1c058e553423 | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
RedBearLab | 1:1c058e553423 | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
RedBearLab | 1:1c058e553423 | 17 | |
RedBearLab | 1:1c058e553423 | 18 | */ |
RedBearLab | 1:1c058e553423 | 19 | |
RedBearLab | 1:1c058e553423 | 20 | /* |
RedBearLab | 1:1c058e553423 | 21 | * The application works with the BLEController iOS/Android App. |
RedBearLab | 1:1c058e553423 | 22 | * Type something from the Terminal to send |
RedBearLab | 1:1c058e553423 | 23 | * to the BLEController App or vice verse. |
RedBearLab | 1:1c058e553423 | 24 | * Characteristics received from App will print on Terminal. |
RedBearLab | 1:1c058e553423 | 25 | */ |
RedBearLab | 1:1c058e553423 | 26 | |
RedBearLab | 0:cffe8ac1bdf0 | 27 | #include "mbed.h" |
RedBearLab | 2:4b66b69c7ecb | 28 | #include "ble/BLE.h" |
RedBearLab | 0:cffe8ac1bdf0 | 29 | |
RedBearLab | 0:cffe8ac1bdf0 | 30 | |
RedBearLab | 0:cffe8ac1bdf0 | 31 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:cffe8ac1bdf0 | 32 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 33 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 34 | |
RedBearLab | 0:cffe8ac1bdf0 | 35 | #define TXRX_BUF_LEN 20 |
RedBearLab | 0:cffe8ac1bdf0 | 36 | |
RedBearLab | 2:4b66b69c7ecb | 37 | BLE ble; |
RedBearLab | 0:cffe8ac1bdf0 | 38 | |
helloqi | 3:17f2d639f37b | 39 | Serial pc(P0_23,P0_25); |
helloqi | 3:17f2d639f37b | 40 | DigitalOut LED_G(P0_19); |
helloqi | 3:17f2d639f37b | 41 | DigitalOut LED_R(P0_20); |
RedBearLab | 0:cffe8ac1bdf0 | 42 | |
RedBearLab | 0:cffe8ac1bdf0 | 43 | // The Nordic UART Service |
RedBearLab | 0:cffe8ac1bdf0 | 44 | static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 45 | static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 46 | static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 47 | static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; |
RedBearLab | 0:cffe8ac1bdf0 | 48 | |
RedBearLab | 0:cffe8ac1bdf0 | 49 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 50 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 51 | |
RedBearLab | 0:cffe8ac1bdf0 | 52 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 53 | static uint8_t rx_len=0; |
RedBearLab | 0:cffe8ac1bdf0 | 54 | |
RedBearLab | 0:cffe8ac1bdf0 | 55 | |
RedBearLab | 0:cffe8ac1bdf0 | 56 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
RedBearLab | 0:cffe8ac1bdf0 | 57 | |
RedBearLab | 0:cffe8ac1bdf0 | 58 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
RedBearLab | 0:cffe8ac1bdf0 | 59 | |
RedBearLab | 0:cffe8ac1bdf0 | 60 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
RedBearLab | 0:cffe8ac1bdf0 | 61 | |
RedBearLab | 0:cffe8ac1bdf0 | 62 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:cffe8ac1bdf0 | 63 | |
RedBearLab | 0:cffe8ac1bdf0 | 64 | |
RedBearLab | 0:cffe8ac1bdf0 | 65 | |
RedBearLab | 0:cffe8ac1bdf0 | 66 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
RedBearLab | 0:cffe8ac1bdf0 | 67 | { |
RedBearLab | 0:cffe8ac1bdf0 | 68 | pc.printf("Disconnected \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 69 | pc.printf("Restart advertising \r\n"); |
helloqi | 3:17f2d639f37b | 70 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 71 | } |
RedBearLab | 0:cffe8ac1bdf0 | 72 | |
RedBearLab | 2:4b66b69c7ecb | 73 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:cffe8ac1bdf0 | 74 | { |
RedBearLab | 0:cffe8ac1bdf0 | 75 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 76 | uint16_t bytesRead, index; |
RedBearLab | 0:cffe8ac1bdf0 | 77 | |
RedBearLab | 2:4b66b69c7ecb | 78 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:cffe8ac1bdf0 | 79 | { |
RedBearLab | 0:cffe8ac1bdf0 | 80 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 81 | memset(txPayload, 0, TXRX_BUF_LEN); |
RedBearLab | 0:cffe8ac1bdf0 | 82 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
RedBearLab | 0:cffe8ac1bdf0 | 83 | for(index=0; index<bytesRead; index++) |
RedBearLab | 0:cffe8ac1bdf0 | 84 | { |
helloqi | 3:17f2d639f37b | 85 | pc.putc(txPayload[index]); |
RedBearLab | 0:cffe8ac1bdf0 | 86 | } |
RedBearLab | 0:cffe8ac1bdf0 | 87 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 88 | } |
RedBearLab | 0:cffe8ac1bdf0 | 89 | } |
RedBearLab | 0:cffe8ac1bdf0 | 90 | |
RedBearLab | 0:cffe8ac1bdf0 | 91 | void uartCB(void) |
RedBearLab | 0:cffe8ac1bdf0 | 92 | { |
RedBearLab | 0:cffe8ac1bdf0 | 93 | while(pc.readable()) |
RedBearLab | 0:cffe8ac1bdf0 | 94 | { |
RedBearLab | 0:cffe8ac1bdf0 | 95 | rx_buf[rx_len++] = pc.getc(); |
RedBearLab | 0:cffe8ac1bdf0 | 96 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') |
RedBearLab | 0:cffe8ac1bdf0 | 97 | { |
RedBearLab | 0:cffe8ac1bdf0 | 98 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
RedBearLab | 0:cffe8ac1bdf0 | 99 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 100 | rx_len = 0; |
RedBearLab | 0:cffe8ac1bdf0 | 101 | break; |
RedBearLab | 0:cffe8ac1bdf0 | 102 | } |
RedBearLab | 0:cffe8ac1bdf0 | 103 | } |
RedBearLab | 0:cffe8ac1bdf0 | 104 | } |
RedBearLab | 0:cffe8ac1bdf0 | 105 | |
RedBearLab | 0:cffe8ac1bdf0 | 106 | int main(void) |
RedBearLab | 0:cffe8ac1bdf0 | 107 | { |
RedBearLab | 0:cffe8ac1bdf0 | 108 | ble.init(); |
RedBearLab | 0:cffe8ac1bdf0 | 109 | ble.onDisconnection(disconnectionCallback); |
RedBearLab | 0:cffe8ac1bdf0 | 110 | ble.onDataWritten(WrittenHandler); |
RedBearLab | 0:cffe8ac1bdf0 | 111 | |
RedBearLab | 0:cffe8ac1bdf0 | 112 | pc.baud(9600); |
RedBearLab | 0:cffe8ac1bdf0 | 113 | pc.printf("SimpleChat Init \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 114 | |
RedBearLab | 0:cffe8ac1bdf0 | 115 | pc.attach( uartCB , pc.RxIrq); |
RedBearLab | 0:cffe8ac1bdf0 | 116 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:cffe8ac1bdf0 | 117 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:cffe8ac1bdf0 | 118 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
RedBearLab | 0:cffe8ac1bdf0 | 119 | (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); |
RedBearLab | 0:cffe8ac1bdf0 | 120 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
RedBearLab | 0:cffe8ac1bdf0 | 121 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
helloqi | 3:17f2d639f37b | 122 | // 100ms; in multiples of 0.625ms. |
RedBearLab | 0:cffe8ac1bdf0 | 123 | ble.setAdvertisingInterval(160); |
RedBearLab | 0:cffe8ac1bdf0 | 124 | |
RedBearLab | 0:cffe8ac1bdf0 | 125 | ble.addService(uartService); |
RedBearLab | 0:cffe8ac1bdf0 | 126 | |
RedBearLab | 0:cffe8ac1bdf0 | 127 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 128 | pc.printf("Advertising Start \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 129 | |
helloqi | 3:17f2d639f37b | 130 | LED_R=0; |
helloqi | 3:17f2d639f37b | 131 | LED_G=0; |
helloqi | 3:17f2d639f37b | 132 | |
RedBearLab | 0:cffe8ac1bdf0 | 133 | while(1) |
RedBearLab | 0:cffe8ac1bdf0 | 134 | { |
RedBearLab | 0:cffe8ac1bdf0 | 135 | ble.waitForEvent(); |
RedBearLab | 0:cffe8ac1bdf0 | 136 | } |
RedBearLab | 0:cffe8ac1bdf0 | 137 | } |
RedBearLab | 0:cffe8ac1bdf0 | 138 | |
RedBearLab | 0:cffe8ac1bdf0 | 139 | |
RedBearLab | 0:cffe8ac1bdf0 | 140 | |
RedBearLab | 0:cffe8ac1bdf0 | 141 | |
RedBearLab | 0:cffe8ac1bdf0 | 142 | |
RedBearLab | 0:cffe8ac1bdf0 | 143 | |
RedBearLab | 0:cffe8ac1bdf0 | 144 | |
RedBearLab | 0:cffe8ac1bdf0 | 145 | |
RedBearLab | 0:cffe8ac1bdf0 | 146 | |
RedBearLab | 0:cffe8ac1bdf0 | 147 | |
RedBearLab | 0:cffe8ac1bdf0 | 148 | |
RedBearLab | 0:cffe8ac1bdf0 | 149 | |
RedBearLab | 0:cffe8ac1bdf0 | 150 | |
RedBearLab | 0:cffe8ac1bdf0 | 151 | |
RedBearLab | 0:cffe8ac1bdf0 | 152 | |
RedBearLab | 0:cffe8ac1bdf0 | 153 |