blob: 6e71a52fa100d0629f7b1438f12100485a6b4112 [file] [log] [blame]
Pawin Vongmasafbffe922017-03-01 02:25:36 -08001/*
2 * Copyright (C) 2005 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#define LOG_TAG "HalToken"
18
19#include <utils/Log.h>
20#include <binder/HalToken.h>
21
22#include <android/hidl/token/1.0/ITokenManager.h>
23
24namespace android {
25
26using ::android::hidl::token::V1_0::ITokenManager;
27
28sp<ITokenManager> gTokenManager = nullptr;
29
30ITokenManager* getTokenManager() {
31 if (gTokenManager != nullptr) {
32 return gTokenManager.get();
33 }
34 gTokenManager = ITokenManager::getService();
35 if (gTokenManager == nullptr) {
36 ALOGE("Cannot retrieve TokenManager.");
37 }
38 return gTokenManager.get();
39}
40
41sp<HInterface> retrieveHalInterface(const HalToken& token) {
42 auto transaction = getTokenManager()->get(token);
43 if (!transaction.isOk()) {
44 ALOGE("getHalInterface: Cannot obtain interface from token.");
45 return nullptr;
46 }
47 return static_cast<sp<HInterface> >(transaction);
48}
49
50bool createHalToken(const sp<HInterface>& interface, HalToken* token) {
51 auto transaction = getTokenManager()->createToken(interface);
52 if (!transaction.isOk()) {
53 ALOGE("createHalToken: Cannot create token from interface.");
54 return false;
55 }
56 *token = static_cast<HalToken>(transaction);
57 return true;
58}
59
60bool deleteHalToken(const HalToken& token) {
61 auto transaction = getTokenManager()->unregister(token);
62 if (!transaction.isOk()) {
63 ALOGE("deleteHalToken: Cannot unregister hal token.");
64 return false;
65 }
66 return true;
67}
68
69}; // namespace android
70