Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_HeartRate_DELTA by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ble/BLE.h" 00019 #include "ble/services/BatteryService.h" 00020 #include "ble/services/DeviceInformationService.h" 00021 #include "HIDService.h" 00022 00023 //DigitalOut led1(LED1); 00024 Serial uart(USBTX, USBRX, 115200); 00025 00026 BLE deltaBLE; 00027 HIDService *hidService; 00028 unsigned char keyData; 00029 const static char DEVICE_NAME[] = "HID_Keyboard"; 00030 static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, 00031 GattService::UUID_BATTERY_SERVICE, 00032 GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00033 static volatile bool triggerSensorPolling = false; 00034 bool isConnectionSecured = false; 00035 bool isSecuritySetup = false; 00036 static uint8_t key_press_scan_buff[50]; 00037 static uint8_t modifyKey[50]; 00038 char msg[25] = ""; 00039 int index_b = 0; 00040 int index_w = 0; 00041 unsigned char uart_buf[64]; 00042 unsigned int i = 0; 00043 00044 DeviceInformationService *deviceInfo; 00045 BatteryService *batteryService; 00046 00047 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) 00048 { 00049 uart.printf("Input passKey: "); 00050 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { 00051 uart.printf("%c ", passkey[i]); 00052 } 00053 uart.printf("\r\n"); 00054 } 00055 00056 void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) 00057 { 00058 uart.printf("securitySetupInitiatedCallback\r\n"); 00059 isSecuritySetup = true; 00060 } 00061 00062 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) 00063 { 00064 if (status == SecurityManager::SEC_STATUS_SUCCESS) { 00065 uart.printf("Security success\r\n", status); 00066 //isConnectionSecured = true; 00067 } else { 00068 uart.printf("Security failed\r\n", status); 00069 } 00070 } 00071 00072 void linkSecuredCallback(Gap::Handle_t handle, SecurityManager::SecurityMode_t securityMode) 00073 { 00074 uart.printf("linkSecuredCallback\r\n"); 00075 if (!isSecuritySetup) { 00076 isConnectionSecured = true; 00077 } 00078 } 00079 00080 void securityContextStoredCallback(Gap::Handle_t handle) { 00081 uart.printf("securityContextStoredCallback\r\n"); 00082 isConnectionSecured = true; 00083 isSecuritySetup = false; 00084 } 00085 00086 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) 00087 { 00088 uart.printf("Connected\r\n"); 00089 } 00090 00091 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00092 { 00093 uart.printf("Disconnected\r\n"); 00094 isConnectionSecured = false; 00095 deltaBLE.gap().startAdvertising(); // restart advertising 00096 } 00097 00098 void onTimeoutCallback(Gap::TimeoutSource_t source) 00099 { 00100 switch (source) { 00101 case Gap::TIMEOUT_SRC_ADVERTISING: 00102 uart.printf("Advertising timeout\r\n"); 00103 break; 00104 case Gap::TIMEOUT_SRC_SECURITY_REQUEST: 00105 uart.printf("Security request timeout\r\n"); 00106 break; 00107 case Gap::TIMEOUT_SRC_SCAN: 00108 uart.printf("Scanning timeout\r\n"); 00109 break; 00110 case Gap::TIMEOUT_SRC_CONN: 00111 uart.printf("Connection timeout\r\n"); 00112 break; 00113 } 00114 } 00115 00116 void periodicCallback(void) 00117 { 00118 //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00119 00120 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00121 * heavy-weight sensor polling from the main thread. */ 00122 //triggerSensorPolling = true; 00123 } 00124 00125 void CLI_execute() { 00126 // if (i>=2) 00127 // if (uart_buf[i-2] == 0x0D && uart_buf[i-1] == 0x0A){//detecting CR LF 00128 if (uart.readable()) return;//retrun if it is not the end of packet 00129 00130 if (isConnectionSecured) { 00131 for (int j=0; j<i; j++) { 00132 keyData = uart_buf[j]; 00133 00134 if(keyData <= 0x39 && keyData >= 0x30){ //number 00135 if(keyData == 0x30){ 00136 modifyKey[index_b] = 0x00; 00137 key_press_scan_buff[index_b] = 0x27; 00138 index_b++; 00139 key_press_scan_buff[index_b] = 0x73; 00140 } else { 00141 modifyKey[index_b] = 0x00; 00142 key_press_scan_buff[index_b] = keyData-0x13; 00143 index_b++; 00144 key_press_scan_buff[index_b] = 0x73; 00145 } 00146 } else if(keyData <= 0x7a && keyData >= 0x61 ){ //lowercase letters 00147 modifyKey[index_b] = 0x00; 00148 key_press_scan_buff[index_b] = keyData-0x5d; 00149 index_b++; 00150 key_press_scan_buff[index_b] = 0x73; 00151 } else if(keyData <= 0x5a && keyData >= 0x41){ //uppercase letters 00152 modifyKey[index_b] = 0x02; 00153 key_press_scan_buff[index_b] = keyData-0x3d; 00154 index_b++; 00155 key_press_scan_buff[index_b] = 0x73; 00156 } else if (keyData == 0x20) { //space 00157 modifyKey[index_b] = 0x00; 00158 key_press_scan_buff[index_b] = 0x2c; 00159 index_b++; 00160 key_press_scan_buff[index_b] = 0x73; 00161 } else if (keyData == 0x08) { //backspace 00162 modifyKey[index_b] = 0x00; 00163 key_press_scan_buff[index_b] = 0x2a; 00164 index_b++; 00165 key_press_scan_buff[index_b] = 0x73; 00166 } else if (keyData == 0x0d) { //return 00167 modifyKey[index_b] = 0x00; 00168 key_press_scan_buff[index_b] = 0x28; 00169 index_b++; 00170 key_press_scan_buff[index_b] = 0x73; 00171 } else { 00172 modifyKey[index_b] = 0x00; 00173 //key_press_scan_buff[index_b] = 0x73; //this is dummy data. 00174 //msg[index_w+1] = '\0'; 00175 } 00176 index_b++; 00177 } 00178 00179 i=0; 00180 00181 for(int i = 0; i < index_b ; i++){ 00182 uart.printf("m[%x] k[%x] ", modifyKey[i], key_press_scan_buff[i]); 00183 hidService->updateReport(modifyKey[i], key_press_scan_buff[i]); 00184 //BLE::Instance(BLE::DEFAULT_INSTANCE).waitForEvent(); 00185 //wait(0.03); 00186 } 00187 00188 index_b = 0; 00189 index_w = 0; 00190 memset(modifyKey, 0, 50); 00191 memset(msg, 0, 25); 00192 memset(key_press_scan_buff, 0, 50); 00193 00194 } 00195 00196 // } 00197 } 00198 00199 void uart_interrupt() { 00200 uart.printf("uart_interrupt\r\n"); 00201 uart_buf[i++] = uart.getc(); 00202 CLI_execute(); 00203 } 00204 00205 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00206 { 00207 BLE &ble = params->ble; 00208 ble_error_t error = params->error; 00209 00210 if (error != BLE_ERROR_NONE) { 00211 return; 00212 } 00213 00214 bool enableBonding = true; 00215 bool requireMITM = false; 00216 00217 //uint8_t pKey[6] = {'1', '1', '1', '1', '1', '1'}; //set the passkey 00218 ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_NONE); 00219 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); 00220 ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback); 00221 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); 00222 ble.securityManager().onLinkSecured(linkSecuredCallback); 00223 ble.securityManager().onSecurityContextStored(securityContextStoredCallback); 00224 ble.gap().onDisconnection(disconnectionCallback); 00225 ble.gap().onConnection(onConnectionCallback); 00226 ble.gap().onTimeout(onTimeoutCallback); 00227 //ble.gattServer().onDataRead(onDataReadCallback); 00228 00229 /* Setup primary service. */ 00230 hidService = new HIDService(ble); 00231 00232 /* Setup auxiliary service. */ 00233 batteryService = new BatteryService(ble, 100); 00234 deviceInfo = new DeviceInformationService(ble, "DELTA", "NQ620", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00235 00236 /* Setup advertising. */ 00237 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00238 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00239 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); 00240 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00241 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00242 ble.gap().setAdvertisingInterval(50); /* 50ms */ 00243 ble.gap().startAdvertising(); 00244 uart.printf("Start advertising\r\n"); 00245 } 00246 00247 int main(void) 00248 { 00249 //led1 = 1; 00250 //Ticker ticker; 00251 //ticker.attach(periodicCallback, 1); // blink LED every second 00252 00253 uart.attach(&uart_interrupt); 00254 uart.printf("Srarting HID Service\r\n"); 00255 00256 BLE &deltaBLE = BLE::Instance(BLE::DEFAULT_INSTANCE); 00257 deltaBLE.init(bleInitComplete); 00258 00259 /* SpinWait for initialization to complete. This is necessary because the 00260 * BLE object is used in the main loop below. */ 00261 while (deltaBLE.hasInitialized() == false) { /* spin loop */ } 00262 00263 // infinite loop 00264 while (1) { 00265 deltaBLE.waitForEvent(); 00266 } 00267 }
Generated on Wed Jul 13 2022 17:19:29 by
