keystore: Run Wifi keystore HAL in keystore daemon

The wifi keystore hal will run in the context of the main keystore
daemon.

Also,
Use the new IKeystore::tryGetService() for retrieveing the HAL service.

Bug: 34603782
Test: Able to connect to wifi passpoint networks.

Change-Id: I1436ea83166e5ad17372d98b0fd699c0dd732a11
diff --git a/keystore-engine/keystore_backend_hidl.cpp b/keystore-engine/keystore_backend_hidl.cpp
index 37ad9a1..80a3601 100644
--- a/keystore-engine/keystore_backend_hidl.cpp
+++ b/keystore-engine/keystore_backend_hidl.cpp
@@ -37,7 +37,7 @@
         return -1;
     }
 
-    sp<IKeystore> service = IKeystore::getService();
+    sp<IKeystore> service = IKeystore::tryGetService();
     if (service == NULL) {
         ALOGE("could not contact keystore HAL");
         return -1;
@@ -64,7 +64,7 @@
         return -1;
     }
 
-    sp<IKeystore> service = IKeystore::getService();
+    sp<IKeystore> service = IKeystore::tryGetService();
     if (service == NULL) {
         ALOGE("could not contact keystore HAL");
         return -1;
diff --git a/keystore/Android.mk b/keystore/Android.mk
index b379151..5d84565 100644
--- a/keystore/Android.mk
+++ b/keystore/Android.mk
@@ -49,6 +49,7 @@
 	libcutils \
 	libcrypto \
 	libhardware \
+	libwifikeystorehal \
 	libkeystore_binder \
 	liblog \
 	libsoftkeymaster \
@@ -60,7 +61,8 @@
 	libhwbinder \
 	libhidlbase \
 	libhidltransport \
-	android.hardware.keymaster@3.0
+	android.hardware.keymaster@3.0 \
+	android.system.wifi.keystore@1.0
 LOCAL_MODULE := keystore
 LOCAL_MODULE_TAGS := optional
 LOCAL_INIT_RC := keystore.rc
diff --git a/keystore/keystore_get_wifi_hidl.cpp b/keystore/keystore_get_wifi_hidl.cpp
index 971b19d..7dbf20c 100644
--- a/keystore/keystore_get_wifi_hidl.cpp
+++ b/keystore/keystore_get_wifi_hidl.cpp
@@ -39,7 +39,7 @@
         return -1;
     }
 
-    sp<IKeystore> service = IKeystore::getService();
+    sp<IKeystore> service = IKeystore::tryGetService();
     if (service == NULL) {
         ALOGE("could not contact keystore HAL");
         return -1;
diff --git a/keystore/keystore_main.cpp b/keystore/keystore_main.cpp
index 1cdb398..a739c5e 100644
--- a/keystore/keystore_main.cpp
+++ b/keystore/keystore_main.cpp
@@ -21,6 +21,8 @@
 #include <binder/IServiceManager.h>
 
 #include <android/hardware/keymaster/3.0/IHwKeymasterDevice.h>
+#include <android/system/wifi/keystore/1.0/IKeystore.h>
+#include <wifikeystorehal/keystore.h>
 
 #include <cutils/log.h>
 
@@ -38,6 +40,10 @@
  * user-defined password. To keep things simple, buffers are always larger than
  * the maximum space we needed, so boundary checks on buffers are omitted. */
 
+using ::android::system::wifi::keystore::V1_0::IKeystore;
+using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
+using ::android::hardware::configureRpcThreadpool;
+
 /**
  * TODO implement keystore daemon using binderized keymaster HAL.
  */
@@ -97,9 +103,20 @@
         return -1;
     }
 
+    /**
+     * Register the wifi keystore HAL service to run in passthrough mode.
+     * This will spawn off a new thread which will service the HIDL
+     * transactions.
+     */
+    configureRpcThreadpool(1, false /* callerWillJoin */);
+    android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
+    android::status_t err = wifiKeystoreHalService->registerAsService();
+    if (ret != android::OK) {
+        ALOGE("Cannot register wifi keystore HAL service: %d", err);
+    }
+
     /*
-     * We're the only thread in existence, so we're just going to process
-     * Binder transaction as a single-threaded program.
+     * This thread is just going to process Binder transactions.
      */
     android::IPCThreadState::self()->joinThreadPool();
     return 1;