d
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 /* 00002 00003 Copyright (c) 2012-2014 RedBearLab 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 00009 subject to the following conditions: 00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 00018 */ 00019 00020 /* 00021 * The application works with the BLEController iOS/Android App. 00022 * Type something from the Terminal to send 00023 * to the BLEController App or vice verse. 00024 * Characteristics received from App will print on Terminal. 00025 */ 00026 00027 #include "mbed.h" 00028 #include "ble/BLE.h" 00029 00030 00031 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00032 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00033 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00034 00035 #define TXRX_BUF_LEN 20 00036 00037 BLE ble; 00038 00039 Serial pc(USBTX, USBRX); 00040 00041 00042 // The Nordic UART Service 00043 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00044 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00045 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00046 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00047 00048 00049 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00050 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00051 00052 static uint8_t rx_buf[TXRX_BUF_LEN]; 00053 static uint8_t rx_len=0; 00054 00055 00056 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00057 00058 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00059 00060 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00061 00062 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00063 00064 00065 00066 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00067 { 00068 pc.printf("Disconnected \r\n"); 00069 pc.printf("Restart advertising \r\n"); 00070 ble.startAdvertising(); 00071 } 00072 00073 void WrittenHandler(const GattWriteCallbackParams *Handler) 00074 { 00075 uint8_t buf[TXRX_BUF_LEN]; 00076 uint16_t bytesRead, index; 00077 00078 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 00079 { 00080 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00081 memset(txPayload, 0, TXRX_BUF_LEN); 00082 memcpy(txPayload, buf, TXRX_BUF_LEN); 00083 pc.printf("WriteHandler \r\n"); 00084 pc.printf("Length: "); 00085 pc.putc(bytesRead); 00086 pc.printf("\r\n"); 00087 pc.printf("Data: "); 00088 for(index=0; index<bytesRead; index++) 00089 { 00090 pc.putc(txPayload[index]); 00091 } 00092 pc.printf("\r\n"); 00093 } 00094 } 00095 00096 void uartCB(void) 00097 { 00098 while(pc.readable()) 00099 { 00100 rx_buf[rx_len++] = pc.getc(); 00101 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') 00102 { 00103 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 00104 pc.printf("RecHandler \r\n"); 00105 pc.printf("Length: "); 00106 pc.putc(rx_len); 00107 pc.printf("\r\n"); 00108 rx_len = 0; 00109 break; 00110 } 00111 } 00112 } 00113 00114 int main(void) 00115 { 00116 ble.init(); 00117 ble.onDisconnection(disconnectionCallback); 00118 ble.onDataWritten(WrittenHandler); 00119 00120 pc.baud(9600); 00121 pc.printf("SimpleChat Init \r\n"); 00122 00123 pc.attach( uartCB , pc.RxIrq); 00124 // setup advertising 00125 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00126 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00127 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00128 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); 00129 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00130 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00131 // 100ms; in multiples of 0.625ms. 00132 ble.setAdvertisingInterval(160); 00133 00134 ble.addService(uartService); 00135 00136 ble.startAdvertising(); 00137 pc.printf("Advertising Start \r\n"); 00138 00139 while(1) 00140 { 00141 ble.waitForEvent(); 00142 } 00143 } 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159
Generated on Wed Aug 10 2022 04:14:01 by 1.7.2