BLe Central Role. It can connect to a number of peripheral that use the same uart ble services
Dependencies: BLE_API mbed nRF51822
main.cpp@4:59628fe57da0, 2016-09-26 (annotated)
- Committer:
- tanasaro10
- Date:
- Mon Sep 26 20:02:00 2016 +0000
- Revision:
- 4:59628fe57da0
- Parent:
- 3:d6f80e11a7f4
Central Role sample, it can connect to a number of peripheral that use serial ble services.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 1 | #include "mbed.h" |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 2 | #include "ble/BLE.h" |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 3 | #include "ble/DiscoveredCharacteristic.h" |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 4 | #include "ble/DiscoveredService.h" |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 5 | #include "ble/GapScanningParams.h" |
mbed_tw_hoehoe | 2:4b53d13d9851 | 6 | #include "ble_radio_notification.h" |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 7 | #include "ble_gap.h" |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 8 | |
tanasaro10 | 4:59628fe57da0 | 9 | |
tanasaro10 | 4:59628fe57da0 | 10 | #define NUMBER_OF_PERIPHERALS 1 |
tanasaro10 | 4:59628fe57da0 | 11 | #define TXRX_BUF_LEN 20 /** For radio message transmission*/ |
tanasaro10 | 4:59628fe57da0 | 12 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 13 | BLE ble; |
mbed_tw_hoehoe | 1:2f1203d70643 | 14 | Serial pc(USBTX, USBRX); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 15 | |
tanasaro10 | 4:59628fe57da0 | 16 | // The Nordic UART Service |
tanasaro10 | 4:59628fe57da0 | 17 | static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
tanasaro10 | 4:59628fe57da0 | 18 | static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
tanasaro10 | 4:59628fe57da0 | 19 | static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
tanasaro10 | 4:59628fe57da0 | 20 | static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; |
tanasaro10 | 4:59628fe57da0 | 21 | static const uint8_t uart_tx_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 3, 0, 0x3D, 0x71}; |
tanasaro10 | 4:59628fe57da0 | 22 | static const uint8_t uart_rx_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 2, 0, 0x3D, 0x71}; |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 23 | |
tanasaro10 | 4:59628fe57da0 | 24 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
tanasaro10 | 4:59628fe57da0 | 25 | static uint8_t rx_len=0; |
tanasaro10 | 4:59628fe57da0 | 26 | bool bReadingReady=false, bReadNow=false, bScanUartBLE[NUMBER_OF_PERIPHERALS] = {false,},bScanRqtBLE[NUMBER_OF_PERIPHERALS] = {false,}; |
tanasaro10 | 4:59628fe57da0 | 27 | bool bScanPending[NUMBER_OF_PERIPHERALS] = {false,},bFirstIn[NUMBER_OF_PERIPHERALS] = {false,}; |
tanasaro10 | 4:59628fe57da0 | 28 | uint8_t gNumOfClients=1, gNumOfDevConnected=0; |
tanasaro10 | 4:59628fe57da0 | 29 | UUID serviceUUID(uart_base_uuid); |
tanasaro10 | 4:59628fe57da0 | 30 | UUID accelTxUUID(uart_tx_uuid); |
tanasaro10 | 4:59628fe57da0 | 31 | UUID accelRxUUID(uart_rx_uuid); |
tanasaro10 | 4:59628fe57da0 | 32 | uint16_t gCounter=0; |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 33 | |
tanasaro10 | 4:59628fe57da0 | 34 | Ticker periodicActions; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 35 | typedef struct { |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 36 | Gap::Handle_t handle; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 37 | Gap::Address_t address; |
tanasaro10 | 4:59628fe57da0 | 38 | DiscoveredCharacteristic TxChar; |
tanasaro10 | 4:59628fe57da0 | 39 | DiscoveredCharacteristic RxChar; |
tanasaro10 | 4:59628fe57da0 | 40 | bool connected; |
tanasaro10 | 4:59628fe57da0 | 41 | bool active; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 42 | uint8_t* deviceName; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 43 | } peripheral_t; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 44 | |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 45 | static peripheral_t gs_peripheral[NUMBER_OF_PERIPHERALS]; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 46 | |
tanasaro10 | 4:59628fe57da0 | 47 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
tanasaro10 | 4:59628fe57da0 | 48 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
tanasaro10 | 4:59628fe57da0 | 49 | |
tanasaro10 | 4:59628fe57da0 | 50 | /* function to read from a registered client*/ |
tanasaro10 | 4:59628fe57da0 | 51 | void readFromClient(uint8_t clientNr){ |
tanasaro10 | 4:59628fe57da0 | 52 | if (clientNr <gNumOfDevConnected){ |
tanasaro10 | 4:59628fe57da0 | 53 | if(gs_peripheral[clientNr].connected){ |
tanasaro10 | 4:59628fe57da0 | 54 | //ble.gattClient().read(gs_peripheral[clientNr].RxChar.getConnectionHandle(), gs_peripheral[clientNr].RxChar.getValueHandle(), 0); |
tanasaro10 | 4:59628fe57da0 | 55 | ble.gattClient().read(gs_peripheral[clientNr].handle, gs_peripheral[clientNr].RxChar.getValueHandle(), 0); |
tanasaro10 | 4:59628fe57da0 | 56 | } |
tanasaro10 | 4:59628fe57da0 | 57 | } |
tanasaro10 | 4:59628fe57da0 | 58 | } |
tanasaro10 | 4:59628fe57da0 | 59 | int8_t getClientIdFromHandle(Gap::Handle_t handle){ |
tanasaro10 | 4:59628fe57da0 | 60 | uint8_t i=0; |
tanasaro10 | 4:59628fe57da0 | 61 | while (i<gNumOfDevConnected){ |
tanasaro10 | 4:59628fe57da0 | 62 | if (memcmp(&handle,&gs_peripheral[i].handle,sizeof(Gap::Handle_t))==0){ |
tanasaro10 | 4:59628fe57da0 | 63 | return i; |
tanasaro10 | 4:59628fe57da0 | 64 | } |
tanasaro10 | 4:59628fe57da0 | 65 | i++; |
tanasaro10 | 4:59628fe57da0 | 66 | } |
tanasaro10 | 4:59628fe57da0 | 67 | return -1; |
tanasaro10 | 4:59628fe57da0 | 68 | } |
tanasaro10 | 4:59628fe57da0 | 69 | |
tanasaro10 | 4:59628fe57da0 | 70 | uint32_t ble_advdata_parser_all(uint8_t type, uint8_t advdata_len, uint8_t *p_advdata) |
tanasaro10 | 4:59628fe57da0 | 71 | { |
tanasaro10 | 4:59628fe57da0 | 72 | uint8_t index=0,i,len; |
tanasaro10 | 4:59628fe57da0 | 73 | char myString[32]; |
tanasaro10 | 4:59628fe57da0 | 74 | uint8_t my_field_data[32]; |
tanasaro10 | 4:59628fe57da0 | 75 | uint8_t field_type; |
tanasaro10 | 4:59628fe57da0 | 76 | |
tanasaro10 | 4:59628fe57da0 | 77 | while(index<advdata_len) |
tanasaro10 | 4:59628fe57da0 | 78 | { |
tanasaro10 | 4:59628fe57da0 | 79 | len = p_advdata[index]-1; |
tanasaro10 | 4:59628fe57da0 | 80 | field_type = p_advdata[index+1]; |
tanasaro10 | 4:59628fe57da0 | 81 | { |
tanasaro10 | 4:59628fe57da0 | 82 | memcpy(my_field_data, &p_advdata[index+2], len); |
tanasaro10 | 4:59628fe57da0 | 83 | |
tanasaro10 | 4:59628fe57da0 | 84 | pc.printf("T:%x ",field_type); |
tanasaro10 | 4:59628fe57da0 | 85 | for (i=0;i<len;i++) |
tanasaro10 | 4:59628fe57da0 | 86 | pc.printf("%x",my_field_data[i]); |
tanasaro10 | 4:59628fe57da0 | 87 | memcpy(myString,my_field_data, len); |
tanasaro10 | 4:59628fe57da0 | 88 | pc.printf(" %s\r\n",myString); |
tanasaro10 | 4:59628fe57da0 | 89 | memset(my_field_data,0,32); |
tanasaro10 | 4:59628fe57da0 | 90 | } |
tanasaro10 | 4:59628fe57da0 | 91 | index += len + 2; |
tanasaro10 | 4:59628fe57da0 | 92 | } |
tanasaro10 | 4:59628fe57da0 | 93 | return NRF_SUCCESS; |
tanasaro10 | 4:59628fe57da0 | 94 | } |
tanasaro10 | 4:59628fe57da0 | 95 | |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 96 | uint32_t ble_advdata_parser(uint8_t type, uint8_t advdata_len, uint8_t *p_advdata, uint8_t *len, uint8_t *p_field_data) |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 97 | { |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 98 | uint8_t index=0; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 99 | uint8_t field_length, field_type; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 100 | |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 101 | while(index<advdata_len) |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 102 | { |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 103 | field_length = p_advdata[index]; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 104 | field_type = p_advdata[index+1]; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 105 | if(field_type == type) |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 106 | { |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 107 | memcpy(p_field_data, &p_advdata[index+2], (field_length-1)); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 108 | *len = field_length - 1; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 109 | return NRF_SUCCESS; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 110 | } |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 111 | index += field_length + 1; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 112 | } |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 113 | return NRF_ERROR_NOT_FOUND; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 114 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 115 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 116 | void scanCallback(const Gap::AdvertisementCallbackParams_t *params) { |
tanasaro10 | 4:59628fe57da0 | 117 | uint8_t len;char myString[32]; |
tanasaro10 | 4:59628fe57da0 | 118 | uint8_t adv_name[31]; |
tanasaro10 | 4:59628fe57da0 | 119 | |
tanasaro10 | 4:59628fe57da0 | 120 | pc.printf("PeerAddr[%02x%02x%02x%02x%02x%02x], Rssi %d, isScanRsp %u, AdvType %u \r\n", |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 121 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 122 | params->rssi, params->isScanResponse, params->type); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 123 | |
tanasaro10 | 4:59628fe57da0 | 124 | ble_advdata_parser_all(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, params->advertisingDataLen, (uint8_t *)params->advertisingData); |
tanasaro10 | 4:59628fe57da0 | 125 | // if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, |
tanasaro10 | 4:59628fe57da0 | 126 | if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 127 | params->advertisingDataLen, |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 128 | (uint8_t *)params->advertisingData, &len, adv_name)){ |
tanasaro10 | 4:59628fe57da0 | 129 | for(uint8_t i=0; i<gNumOfClients; i++){ |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 130 | if(gs_peripheral[i].connected == false){ |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 131 | memcpy(gs_peripheral[i].address, params->peerAddr, sizeof(params->peerAddr)); |
tanasaro10 | 4:59628fe57da0 | 132 | memcpy(myString,adv_name,len); |
tanasaro10 | 4:59628fe57da0 | 133 | pc.printf("%s - %c\r\n",myString,gs_peripheral[i].address); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 134 | gs_peripheral[i].deviceName = adv_name; |
tanasaro10 | 4:59628fe57da0 | 135 | gs_peripheral[i].active=false; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 136 | ble.connect(params->peerAddr, BLEProtocol::AddressType::RANDOM_STATIC, NULL, NULL); |
tanasaro10 | 4:59628fe57da0 | 137 | // break; |
tanasaro10 | 4:59628fe57da0 | 138 | } |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 139 | } |
tanasaro10 | 4:59628fe57da0 | 140 | if (gNumOfDevConnected==gNumOfClients) |
tanasaro10 | 4:59628fe57da0 | 141 | ble.stopScan(); |
tanasaro10 | 4:59628fe57da0 | 142 | pc.printf("Stop Scan\r\n"); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 143 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 144 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 145 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 146 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
tanasaro10 | 4:59628fe57da0 | 147 | uint8_t buff[16];int16_t i=0; |
tanasaro10 | 4:59628fe57da0 | 148 | |
tanasaro10 | 4:59628fe57da0 | 149 | memcpy( buff,service->getUUID().getBaseUUID(),16); |
tanasaro10 | 4:59628fe57da0 | 150 | /* Check only for the UART service*/ |
tanasaro10 | 4:59628fe57da0 | 151 | if (memcmp(uart_base_uuid_rev,service->getUUID().getBaseUUID(),16)!=0) |
tanasaro10 | 4:59628fe57da0 | 152 | pc.printf("UART Serv Not Found\r\n",i); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 153 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 154 | |
tanasaro10 | 4:59628fe57da0 | 155 | void characteristicRxTxDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
tanasaro10 | 4:59628fe57da0 | 156 | //char buff[30],i=0; |
tanasaro10 | 4:59628fe57da0 | 157 | // Tx and Rx characteristic should be found ! |
tanasaro10 | 4:59628fe57da0 | 158 | uint8_t idx=0; |
tanasaro10 | 4:59628fe57da0 | 159 | idx=getClientIdFromHandle(characteristicP->getConnectionHandle()); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 160 | |
tanasaro10 | 4:59628fe57da0 | 161 | if (memcmp(uart_tx_uuid_rev,characteristicP->getUUID().getBaseUUID(),characteristicP->getUUID().getLen())==0){ |
tanasaro10 | 4:59628fe57da0 | 162 | gs_peripheral[idx].TxChar = *characteristicP; |
tanasaro10 | 4:59628fe57da0 | 163 | pc.printf("C%c: Tx Service !\r\n",idx); |
tanasaro10 | 4:59628fe57da0 | 164 | |
tanasaro10 | 4:59628fe57da0 | 165 | } else if (memcmp(uart_rx_uuid_rev,characteristicP->getUUID().getBaseUUID(),characteristicP->getUUID().getLen())==0){ |
tanasaro10 | 4:59628fe57da0 | 166 | gs_peripheral[idx].RxChar = *characteristicP; |
tanasaro10 | 4:59628fe57da0 | 167 | pc.printf("C%c: Rx Service !\r\n",idx); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 168 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 169 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 170 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 171 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
tanasaro10 | 4:59628fe57da0 | 172 | //pc.printf("GAP_EVT_CONNECTED\r\n"); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 173 | if (params->role == Gap::CENTRAL) { |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 174 | |
tanasaro10 | 4:59628fe57da0 | 175 | for(uint8_t i=0; i<gNumOfClients; i++){ |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 176 | if(gs_peripheral[i].connected == false){ |
tanasaro10 | 4:59628fe57da0 | 177 | gNumOfDevConnected++; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 178 | gs_peripheral[i].handle = params->handle; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 179 | gs_peripheral[i].connected = true; |
tanasaro10 | 4:59628fe57da0 | 180 | break; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 181 | } |
tanasaro10 | 4:59628fe57da0 | 182 | } |
tanasaro10 | 4:59628fe57da0 | 183 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicRxTxDiscoveryCallback, serviceUUID); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 184 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 185 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 186 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 187 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 188 | pc.printf("terminated SD for handle %u\r\n", connectionHandle); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 189 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 190 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 191 | void triggerRead(const GattReadCallbackParams *response) { |
tanasaro10 | 4:59628fe57da0 | 192 | int idx; char msg[25]={'C',0,':',0}; |
tanasaro10 | 4:59628fe57da0 | 193 | //uint16_t counter=0; |
tanasaro10 | 4:59628fe57da0 | 194 | static char pmsg[25]; |
tanasaro10 | 4:59628fe57da0 | 195 | //pc.printf("triggerRead.....\r\n"); |
tanasaro10 | 4:59628fe57da0 | 196 | //pc.printf("len: %d\r\n", response->len); |
tanasaro10 | 4:59628fe57da0 | 197 | idx = getClientIdFromHandle(response->connHandle); |
tanasaro10 | 4:59628fe57da0 | 198 | //pc.printf("trRead idx=%d",idx); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 199 | const uint8_t *data = response->data; |
tanasaro10 | 4:59628fe57da0 | 200 | if (response->len>0){ |
tanasaro10 | 4:59628fe57da0 | 201 | msg[1]='0'+idx; |
tanasaro10 | 4:59628fe57da0 | 202 | //pc.printf("C%c:", idx+'0'); |
tanasaro10 | 4:59628fe57da0 | 203 | memcpy(&msg[4],data,response->len); |
tanasaro10 | 4:59628fe57da0 | 204 | if (bScanUartBLE[idx]==true){ |
tanasaro10 | 4:59628fe57da0 | 205 | if(bScanRqtBLE[idx]==true){ |
tanasaro10 | 4:59628fe57da0 | 206 | bFirstIn[idx] = true; |
tanasaro10 | 4:59628fe57da0 | 207 | gCounter ++; |
tanasaro10 | 4:59628fe57da0 | 208 | // stop condition |
tanasaro10 | 4:59628fe57da0 | 209 | bScanPending[idx] = true; |
tanasaro10 | 4:59628fe57da0 | 210 | //pc.printf("C%c:%x %d=%s\r\n",msg[1],msg[4],msg[5],&msg[6]); |
tanasaro10 | 4:59628fe57da0 | 211 | } else { |
tanasaro10 | 4:59628fe57da0 | 212 | //if (memcmp(msg,pmsg,25)!=0){ |
tanasaro10 | 4:59628fe57da0 | 213 | pc.printf("C%d:%s\r\n",idx,&msg[4]); |
tanasaro10 | 4:59628fe57da0 | 214 | memcpy(pmsg,msg,25); |
tanasaro10 | 4:59628fe57da0 | 215 | //} |
tanasaro10 | 4:59628fe57da0 | 216 | } |
tanasaro10 | 4:59628fe57da0 | 217 | } else{ |
tanasaro10 | 4:59628fe57da0 | 218 | //if (memcmp(msg,pmsg,25)!=0){ |
tanasaro10 | 4:59628fe57da0 | 219 | pc.printf("C%d:%s\r\n",idx,&msg[4]); |
tanasaro10 | 4:59628fe57da0 | 220 | memcpy(pmsg,msg,25); |
tanasaro10 | 4:59628fe57da0 | 221 | // } |
tanasaro10 | 4:59628fe57da0 | 222 | gs_peripheral[idx].active = false; |
tanasaro10 | 4:59628fe57da0 | 223 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 224 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 225 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 226 | |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 227 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
tanasaro10 | 4:59628fe57da0 | 228 | uint8_t idx; |
mbed_tw_hoehoe | 1:2f1203d70643 | 229 | pc.printf("disconnected\r\n"); |
tanasaro10 | 4:59628fe57da0 | 230 | idx = getClientIdFromHandle(params->handle); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 231 | |
tanasaro10 | 4:59628fe57da0 | 232 | if(gs_peripheral[idx].handle == params->handle){ |
tanasaro10 | 4:59628fe57da0 | 233 | gs_peripheral[idx].connected = false; |
tanasaro10 | 4:59628fe57da0 | 234 | gs_peripheral[idx].handle = 0xFFFF; |
tanasaro10 | 4:59628fe57da0 | 235 | gs_peripheral[idx].active = false; |
tanasaro10 | 4:59628fe57da0 | 236 | gNumOfDevConnected --; |
tanasaro10 | 4:59628fe57da0 | 237 | } |
tanasaro10 | 4:59628fe57da0 | 238 | pc.printf("numOfDevConn %d",gNumOfDevConnected); |
tanasaro10 | 4:59628fe57da0 | 239 | wait(3.0); |
tanasaro10 | 4:59628fe57da0 | 240 | ble.gap().startScan(scanCallback); |
tanasaro10 | 4:59628fe57da0 | 241 | } |
tanasaro10 | 4:59628fe57da0 | 242 | |
tanasaro10 | 4:59628fe57da0 | 243 | void at_eachInstant(){ |
tanasaro10 | 4:59628fe57da0 | 244 | for(uint8_t i=0; i<gNumOfDevConnected; i++){ |
tanasaro10 | 4:59628fe57da0 | 245 | if (gs_peripheral[i].active == true){ |
tanasaro10 | 4:59628fe57da0 | 246 | if ((bScanRqtBLE[i]==true)&&(bFirstIn[i]==true)){ |
tanasaro10 | 4:59628fe57da0 | 247 | if (bScanPending[i] == true){ |
tanasaro10 | 4:59628fe57da0 | 248 | bScanPending[i] = false; |
tanasaro10 | 4:59628fe57da0 | 249 | } else { |
tanasaro10 | 4:59628fe57da0 | 250 | bScanRqtBLE[i]=false; |
tanasaro10 | 4:59628fe57da0 | 251 | pc.printf("gCounter %d",gCounter); |
tanasaro10 | 4:59628fe57da0 | 252 | } |
tanasaro10 | 4:59628fe57da0 | 253 | } |
tanasaro10 | 4:59628fe57da0 | 254 | readFromClient(i); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 255 | } |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 256 | } |
tanasaro10 | 4:59628fe57da0 | 257 | } |
tanasaro10 | 4:59628fe57da0 | 258 | void decodeLocalCmd(uint8_t length,uint8_t * buffer){ |
tanasaro10 | 4:59628fe57da0 | 259 | switch (buffer[0]){ |
tanasaro10 | 4:59628fe57da0 | 260 | case 's':{ |
tanasaro10 | 4:59628fe57da0 | 261 | /*scan options*/ |
tanasaro10 | 4:59628fe57da0 | 262 | if (buffer[1]=='0'){ |
tanasaro10 | 4:59628fe57da0 | 263 | /*stop reading scan */ |
tanasaro10 | 4:59628fe57da0 | 264 | bScanUartBLE[buffer[2]-'0'] = false; |
tanasaro10 | 4:59628fe57da0 | 265 | |
tanasaro10 | 4:59628fe57da0 | 266 | } else if (buffer[1]=='1'){ |
tanasaro10 | 4:59628fe57da0 | 267 | /*start reading scan*/ |
tanasaro10 | 4:59628fe57da0 | 268 | bScanUartBLE[buffer[2]-'0'] = true; |
tanasaro10 | 4:59628fe57da0 | 269 | } else if (buffer[1]=='2'){ |
tanasaro10 | 4:59628fe57da0 | 270 | uint8_t rxBuff[5]={'x','f','4','\r','\n'}; |
tanasaro10 | 4:59628fe57da0 | 271 | /*start reading scan*/ |
tanasaro10 | 4:59628fe57da0 | 272 | gCounter =0; |
tanasaro10 | 4:59628fe57da0 | 273 | bScanUartBLE[buffer[2]-'0'] = true; |
tanasaro10 | 4:59628fe57da0 | 274 | bScanRqtBLE[buffer[2]-'0'] = true; |
tanasaro10 | 4:59628fe57da0 | 275 | gs_peripheral[buffer[2]-'0'].TxChar.write(5,rxBuff); |
tanasaro10 | 4:59628fe57da0 | 276 | gs_peripheral[buffer[2]-'0'].active = true; |
tanasaro10 | 4:59628fe57da0 | 277 | } else { |
tanasaro10 | 4:59628fe57da0 | 278 | pc.printf("No existing cmd!\r\n"); |
tanasaro10 | 4:59628fe57da0 | 279 | } |
tanasaro10 | 4:59628fe57da0 | 280 | break; |
tanasaro10 | 4:59628fe57da0 | 281 | } |
tanasaro10 | 4:59628fe57da0 | 282 | case 'r':{ |
tanasaro10 | 4:59628fe57da0 | 283 | break; |
tanasaro10 | 4:59628fe57da0 | 284 | } |
tanasaro10 | 4:59628fe57da0 | 285 | case 'i':{ |
tanasaro10 | 4:59628fe57da0 | 286 | break; |
tanasaro10 | 4:59628fe57da0 | 287 | } |
tanasaro10 | 4:59628fe57da0 | 288 | default:{ |
tanasaro10 | 4:59628fe57da0 | 289 | pc.printf("No existing cmd!\r\n"); |
tanasaro10 | 4:59628fe57da0 | 290 | } |
tanasaro10 | 4:59628fe57da0 | 291 | } |
tanasaro10 | 4:59628fe57da0 | 292 | |
tanasaro10 | 4:59628fe57da0 | 293 | } |
tanasaro10 | 4:59628fe57da0 | 294 | void uartCB(void) |
tanasaro10 | 4:59628fe57da0 | 295 | { |
tanasaro10 | 4:59628fe57da0 | 296 | while(pc.readable()) { |
tanasaro10 | 4:59628fe57da0 | 297 | rx_buf[rx_len++] = pc.getc(); |
tanasaro10 | 4:59628fe57da0 | 298 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') { |
tanasaro10 | 4:59628fe57da0 | 299 | /* start with m then it is a command*/ |
tanasaro10 | 4:59628fe57da0 | 300 | if (rx_buf[0]=='m') |
tanasaro10 | 4:59628fe57da0 | 301 | { |
tanasaro10 | 4:59628fe57da0 | 302 | if (rx_buf[1]=='l'){ |
tanasaro10 | 4:59628fe57da0 | 303 | /* local BLE = central BLE, a local command*/ |
tanasaro10 | 4:59628fe57da0 | 304 | decodeLocalCmd(rx_len-2,&rx_buf[2]); |
tanasaro10 | 4:59628fe57da0 | 305 | } else if ((rx_buf[1]>='0')&&(rx_buf[1]<('0'+NUMBER_OF_PERIPHERALS))){ |
tanasaro10 | 4:59628fe57da0 | 306 | /* send command to indicated client*/ |
tanasaro10 | 4:59628fe57da0 | 307 | gs_peripheral[rx_buf[1]-'0'].TxChar.write(rx_len-2,&rx_buf[2]); |
tanasaro10 | 4:59628fe57da0 | 308 | gs_peripheral[rx_buf[1]-'0'].active = true; |
tanasaro10 | 4:59628fe57da0 | 309 | //bReadNow= true; |
tanasaro10 | 4:59628fe57da0 | 310 | } else { |
tanasaro10 | 4:59628fe57da0 | 311 | pc.printf("Invalid Cmd ! No existing client %c\r\n",rx_buf[1]); |
tanasaro10 | 4:59628fe57da0 | 312 | } |
tanasaro10 | 4:59628fe57da0 | 313 | } |
tanasaro10 | 4:59628fe57da0 | 314 | rx_len= 0; |
tanasaro10 | 4:59628fe57da0 | 315 | break; |
tanasaro10 | 4:59628fe57da0 | 316 | } |
tanasaro10 | 4:59628fe57da0 | 317 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 318 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 319 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 320 | int main(void) { |
tanasaro10 | 4:59628fe57da0 | 321 | pc.baud(19200); |
tanasaro10 | 4:59628fe57da0 | 322 | wait(5.0); |
tanasaro10 | 4:59628fe57da0 | 323 | |
tanasaro10 | 4:59628fe57da0 | 324 | pc.attach( uartCB , pc.RxIrq); |
tanasaro10 | 4:59628fe57da0 | 325 | ble.init(); |
mbed_tw_hoehoe | 1:2f1203d70643 | 326 | pc.printf("start\r\n"); |
tanasaro10 | 4:59628fe57da0 | 327 | //ble.addService(uartService); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 328 | ble.onConnection(connectionCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 329 | ble.onDisconnection(disconnectionCallback); |
tanasaro10 | 4:59628fe57da0 | 330 | //ble.onDataWritten(WrittenHandler); |
tanasaro10 | 4:59628fe57da0 | 331 | //ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 332 | ble.gattClient().onDataRead(triggerRead); |
tanasaro10 | 4:59628fe57da0 | 333 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 334 | |
tanasaro10 | 4:59628fe57da0 | 335 | ble.gap().setScanParams(5, 5); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 336 | ble.gap().startScan(scanCallback); |
tanasaro10 | 4:59628fe57da0 | 337 | // 100 msec |
tanasaro10 | 4:59628fe57da0 | 338 | periodicActions.attach_us(&at_eachInstant,1000u); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 339 | while (true) { |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 340 | ble.waitForEvent(); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 341 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 342 | } |