1 /*
2 * Copyright (C) 2021 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 "permissions_util.h"
18
19 #include <chre_host/log.h>
20
21 #include "chre/util/macros.h"
22 #include "chre/util/system/napp_permissions.h"
23
24 namespace android::hardware::contexthub::common::implementation {
25
chreToAndroidPermissions(uint32_t chrePermissions)26 std::vector<std::string> chreToAndroidPermissions(uint32_t chrePermissions) {
27 std::vector<std::string> androidPermissions;
28 if (BITMASK_HAS_VALUE(chrePermissions,
29 ::chre::NanoappPermissions::CHRE_PERMS_AUDIO)) {
30 androidPermissions.push_back(kRecordAudioPerm);
31 }
32
33 if (BITMASK_HAS_VALUE(chrePermissions,
34 ::chre::NanoappPermissions::CHRE_PERMS_GNSS) ||
35 BITMASK_HAS_VALUE(chrePermissions,
36 ::chre::NanoappPermissions::CHRE_PERMS_WIFI) ||
37 BITMASK_HAS_VALUE(chrePermissions,
38 ::chre::NanoappPermissions::CHRE_PERMS_WWAN)) {
39 androidPermissions.push_back(kFineLocationPerm);
40 androidPermissions.push_back(kBackgroundLocationPerm);
41 }
42
43 if (BITMASK_HAS_VALUE(chrePermissions,
44 ::chre::NanoappPermissions::CHRE_PERMS_BLE)) {
45 androidPermissions.push_back(kBluetoothScanPerm);
46 }
47
48 return androidPermissions;
49 }
50
androidToChrePermissions(const std::vector<std::string> & androidPermissions)51 uint32_t androidToChrePermissions(
52 const std::vector<std::string> &androidPermissions) {
53 uint32_t chrePermissions = 0;
54 for (const auto &permission : androidPermissions) {
55 if (permission == kRecordAudioPerm) {
56 chrePermissions |= ::chre::NanoappPermissions::CHRE_PERMS_AUDIO;
57 } else if (permission == kFineLocationPerm ||
58 permission == kBackgroundLocationPerm) {
59 chrePermissions |= ::chre::NanoappPermissions::CHRE_PERMS_GNSS |
60 ::chre::NanoappPermissions::CHRE_PERMS_WIFI |
61 ::chre::NanoappPermissions::CHRE_PERMS_WWAN;
62 } else if (permission == kBluetoothScanPerm) {
63 chrePermissions |= ::chre::NanoappPermissions::CHRE_PERMS_BLE;
64 } else {
65 LOGW("Unknown Android permission %s", permission.c_str());
66 }
67 }
68 return chrePermissions;
69 }
70
71 } // namespace android::hardware::contexthub::common::implementation
72