• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "permission_util.h"
17 
18 #include <thread>
19 #include "sensor_agent_type.h"
20 #include "sensors_errors.h"
21 #include "sensors_log_domain.h"
22 
23 namespace OHOS {
24 namespace Sensors {
25 using namespace OHOS::HiviewDFX;
26 
27 namespace {
28 constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_UTILS, "PermissionUtil" };
29 const std::string ACCELEROMETER_PERMISSION = "ohos.permission.ACCELEROMETER";
30 const std::string GYROSCOPE_PERMISSION = "ohos.permission.GYROSCOPE";
31 const std::string ACTIVITY_MOTION_PERMISSION = "ohos.permission.ACTIVITY_MOTION";
32 const std::string READ_HEALTH_DATA_PERMISSION = "ohos.permission.READ_HEALTH_DATA";
33 }  // namespace
34 
35 std::unordered_map<uint32_t, std::string> PermissionUtil::sensorPermissions_ = {
36     { SENSOR_TYPE_ID_ACCELEROMETER, ACCELEROMETER_PERMISSION },
37     { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, ACCELEROMETER_PERMISSION },
38     { SENSOR_TYPE_ID_LINEAR_ACCELERATION, ACCELEROMETER_PERMISSION },
39     { SENSOR_TYPE_ID_GYROSCOPE, GYROSCOPE_PERMISSION },
40     { SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, GYROSCOPE_PERMISSION },
41     { SENSOR_TYPE_ID_PEDOMETER_DETECTION, ACTIVITY_MOTION_PERMISSION },
42     { SENSOR_TYPE_ID_PEDOMETER, ACTIVITY_MOTION_PERMISSION },
43     { SENSOR_TYPE_ID_HEART_RATE, READ_HEALTH_DATA_PERMISSION }
44 };
45 
CheckSensorPermission(AccessTokenID callerToken,int32_t sensorTypeId)46 bool PermissionUtil::CheckSensorPermission(AccessTokenID callerToken, int32_t sensorTypeId)
47 {
48     if (sensorPermissions_.find(sensorTypeId) == sensorPermissions_.end()) {
49         return true;
50     }
51     std::string permissionName = sensorPermissions_[sensorTypeId];
52     int32_t result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
53     if (result != PERMISSION_GRANTED) {
54         HiLog::Error(LABEL, "%{public}s sensorId: %{public}d grant failed, result: %{public}d",
55             __func__, sensorTypeId, result);
56         return false;
57     }
58     HiLog::Debug(LABEL, "%{public}s sensorId: %{public}d grant success", __func__, sensorTypeId);
59     return true;
60 }
61 }  // namespace Sensors
62 }  // namespace OHOS
63