ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Revision:
73:d36e0b817ebf
Parent:
69:9d6ecd584a0c
Child:
74:138d8c31f9f6
--- a/HIDController_BLE.cpp	Fri Sep 02 08:49:41 2016 +0900
+++ b/HIDController_BLE.cpp	Fri Sep 02 09:15:01 2016 +0900
@@ -41,8 +41,27 @@
 static void onConnect(const Gap::ConnectionCallbackParams_t *params) {
 	peerAddress.type = params->peerAddrType;
 	memcpy(peerAddress.address, params->peerAddr, Gap::ADDR_LEN);
-	// TODO whitelist に peerAddr が含まれていない場合 securitySetupCompletedCallback を待ってから CONNECTED にすべき
-	controllerStatus = CONNECTED;
+
+	BLEProtocol::Address_t peerAddresses[2];
+	Gap::Whitelist_t whitelist;
+	whitelist.size = 0;
+	whitelist.capacity = 2;
+	whitelist.addresses = peerAddresses;
+	BLE::Instance(BLE::DEFAULT_INSTANCE).gap().getWhitelist(whitelist);
+	DEBUG_PRINTF_BLE_INTERRUPT("getWhitelist %d\r\n", whitelist.size);
+	for (int i = 0; i < whitelist.size; i++) {
+		if (whitelist.addresses[i].type == params->peerAddrType &&
+				memcmp(whitelist.addresses[i].address, params->peerAddr, Gap::ADDR_LEN) == 0) {
+
+			BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_ALL_REQS);
+			controllerStatus = CONNECTED;
+			DEBUG_PRINTF_BLE_INTERRUPT("peer is found in whitelist\r\n");
+			return;
+		}
+	}
+
+	DEBUG_PRINTF_BLE_INTERRUPT("peer is not found in whitelist\r\n");
+	controllerStatus = CONNECTING;
 }
 
 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) {
@@ -88,6 +107,7 @@
 		// BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_SCAN_REQS);
 		// BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_CONN_REQS);
 		BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_ALL_REQS);
+		controllerStatus = CONNECTED;
 	} else {
 		DEBUG_PRINTF_BLE_INTERRUPT("Security failed %d\r\n", status);
 	}
@@ -206,9 +226,20 @@
 	/* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
 	ble.gap().setTxPower(0);
 
-	// TODO
-	// ちゃんと接続済みになったら ADV_POLICY_FILTER_ALL_REQS にしないといけないが
-	// ペアリングできてるかどうか知るよしがなくてどうしよもない
+	{
+		BLEProtocol::Address_t peerAddresses[2];
+		Gap::Whitelist_t whitelist;
+		whitelist.size = 0;
+		whitelist.capacity = 2;
+		whitelist.addresses = peerAddresses;
+		error = ble.securityManager().getAddressesFromBondTable(whitelist);
+		DEBUG_PRINTF_BLE("getAddressesFromBondTable %d\r\n", whitelist.size);
+		BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setWhitelist(whitelist);
+	//	for (int i = 0; i < whitelist.size; i++) {
+	//		whitelist.addresses[i]
+	//	}
+	}
+
 	ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST);
 
 	// DEBUG_PRINTF_BLE("advertising\r\n");