
51822 gatt gamepad no test https://github.com/ozanaltinkaya/BLE_Gamepad_NRF51822
Dependencies: mbed BLE_API nRF51822
examples_common.cpp@0:706cf6936c11, 2021-07-20 (annotated)
- Committer:
- weisimin
- Date:
- Tue Jul 20 09:23:54 2021 +0000
- Revision:
- 0:706cf6936c11
BLE GATT GAMEPAD NO TEST
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
weisimin | 0:706cf6936c11 | 1 | /* mbed Microcontroller Library |
weisimin | 0:706cf6936c11 | 2 | * Copyright (c) 2015 ARM Limited |
weisimin | 0:706cf6936c11 | 3 | * |
weisimin | 0:706cf6936c11 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
weisimin | 0:706cf6936c11 | 5 | * you may not use this file except in compliance with the License. |
weisimin | 0:706cf6936c11 | 6 | * You may obtain a copy of the License at |
weisimin | 0:706cf6936c11 | 7 | * |
weisimin | 0:706cf6936c11 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
weisimin | 0:706cf6936c11 | 9 | * |
weisimin | 0:706cf6936c11 | 10 | * Unless required by applicable law or agreed to in writing, software |
weisimin | 0:706cf6936c11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
weisimin | 0:706cf6936c11 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
weisimin | 0:706cf6936c11 | 13 | * See the License for the specific language governing permissions and |
weisimin | 0:706cf6936c11 | 14 | * limitations under the License. |
weisimin | 0:706cf6936c11 | 15 | */ |
weisimin | 0:706cf6936c11 | 16 | |
weisimin | 0:706cf6936c11 | 17 | #include "ble/services/BatteryService.h" |
weisimin | 0:706cf6936c11 | 18 | #include "ble/services/DeviceInformationService.h" |
weisimin | 0:706cf6936c11 | 19 | |
weisimin | 0:706cf6936c11 | 20 | #include "examples_common.h" |
weisimin | 0:706cf6936c11 | 21 | |
weisimin | 0:706cf6936c11 | 22 | static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) |
weisimin | 0:706cf6936c11 | 23 | { |
weisimin | 0:706cf6936c11 | 24 | printf("Input passKey: "); |
weisimin | 0:706cf6936c11 | 25 | for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { |
weisimin | 0:706cf6936c11 | 26 | printf("%c", passkey[i]); |
weisimin | 0:706cf6936c11 | 27 | } |
weisimin | 0:706cf6936c11 | 28 | printf("\r\n"); |
weisimin | 0:706cf6936c11 | 29 | } |
weisimin | 0:706cf6936c11 | 30 | |
weisimin | 0:706cf6936c11 | 31 | static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) |
weisimin | 0:706cf6936c11 | 32 | { |
weisimin | 0:706cf6936c11 | 33 | if (status == SecurityManager::SEC_STATUS_SUCCESS) { |
weisimin | 0:706cf6936c11 | 34 | printf("Security success %d\r\n", status); |
weisimin | 0:706cf6936c11 | 35 | } else { |
weisimin | 0:706cf6936c11 | 36 | printf("Security failed %d\r\n", status); |
weisimin | 0:706cf6936c11 | 37 | } |
weisimin | 0:706cf6936c11 | 38 | } |
weisimin | 0:706cf6936c11 | 39 | |
weisimin | 0:706cf6936c11 | 40 | static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) |
weisimin | 0:706cf6936c11 | 41 | { |
weisimin | 0:706cf6936c11 | 42 | printf("Security setup initiated\r\n"); |
weisimin | 0:706cf6936c11 | 43 | } |
weisimin | 0:706cf6936c11 | 44 | |
weisimin | 0:706cf6936c11 | 45 | void initializeSecurity(BLE &ble) |
weisimin | 0:706cf6936c11 | 46 | { |
weisimin | 0:706cf6936c11 | 47 | bool enableBonding = true; |
weisimin | 0:706cf6936c11 | 48 | bool requireMITM = HID_SECURITY_REQUIRE_MITM; |
weisimin | 0:706cf6936c11 | 49 | |
weisimin | 0:706cf6936c11 | 50 | ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback); |
weisimin | 0:706cf6936c11 | 51 | ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); |
weisimin | 0:706cf6936c11 | 52 | ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); |
weisimin | 0:706cf6936c11 | 53 | |
weisimin | 0:706cf6936c11 | 54 | ble.securityManager().init(enableBonding, requireMITM, HID_SECURITY_IOCAPS); |
weisimin | 0:706cf6936c11 | 55 | } |
weisimin | 0:706cf6936c11 | 56 | |
weisimin | 0:706cf6936c11 | 57 | void initializeHOGP(BLE &ble) |
weisimin | 0:706cf6936c11 | 58 | { |
weisimin | 0:706cf6936c11 | 59 | static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, |
weisimin | 0:706cf6936c11 | 60 | GattService::UUID_DEVICE_INFORMATION_SERVICE, |
weisimin | 0:706cf6936c11 | 61 | GattService::UUID_BATTERY_SERVICE}; |
weisimin | 0:706cf6936c11 | 62 | |
weisimin | 0:706cf6936c11 | 63 | DeviceInformationService deviceInfo(ble, "ARM", "m1", "abc", "def", "ghi", "jkl"); |
weisimin | 0:706cf6936c11 | 64 | |
weisimin | 0:706cf6936c11 | 65 | BatteryService batteryInfo(ble, 80); |
weisimin | 0:706cf6936c11 | 66 | |
weisimin | 0:706cf6936c11 | 67 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | |
weisimin | 0:706cf6936c11 | 68 | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
weisimin | 0:706cf6936c11 | 69 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
weisimin | 0:706cf6936c11 | 70 | (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
weisimin | 0:706cf6936c11 | 71 | |
weisimin | 0:706cf6936c11 | 72 | // see 5.1.2: HID over GATT Specification (pg. 25) |
weisimin | 0:706cf6936c11 | 73 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
weisimin | 0:706cf6936c11 | 74 | // 30ms to 50ms is recommended (5.1.2) |
weisimin | 0:706cf6936c11 | 75 | ble.gap().setAdvertisingInterval(50); |
weisimin | 0:706cf6936c11 | 76 | } |