1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "km_compat.h"
18 #include <android/binder_manager.h>
19 
20 #include <mutex>
21 
22 extern "C" {
23 
24 // Create a KeyMintDevice and add it as a service.
addKeyMintDeviceService()25 int32_t addKeyMintDeviceService() {
26     static std::mutex mutex;
27     std::lock_guard<std::mutex> lock(mutex);
28     static std::shared_ptr<KeystoreCompatService> ti;
29     binder_status_t status = STATUS_OK;
30     if (!ti) {
31         ti = ndk::SharedRefBase::make<KeystoreCompatService>();
32         const auto instanceName = "android.security.compat";
33         status = AServiceManager_addService(ti->asBinder().get(), instanceName);
34         if (status != STATUS_OK) {
35             ti.reset();
36         }
37     }
38     return status;
39 }
40 }
41