• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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 
20 #include "accesstoken_kit.h"
21 #include "privacy_kit.h"
22 #include "medical_errors.h"
23 #include "medical_log_domain.h"
24 #include "medical_native_type.h"
25 
26 namespace OHOS {
27 namespace Sensors {
28 using namespace OHOS::HiviewDFX;
29 
30 namespace {
31 constexpr HiLogLabel LABEL = { LOG_CORE, MedicalSensorLogDomain::MEDICAL_SENSOR_UTILS, "PermissionUtil" };
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     { TYPE_ID_PHOTOPLETHYSMOGRAPH, READ_HEALTH_DATA_PERMISSION },
37     { TYPE_ID_HEART_RATE, READ_HEALTH_DATA_PERMISSION }
38 };
39 
CheckSensorPermission(AccessTokenID callerToken,int32_t sensorTypeId)40 int32_t PermissionUtil::CheckSensorPermission(AccessTokenID callerToken, int32_t sensorTypeId)
41 {
42     if (sensorPermissions_.find(sensorTypeId) == sensorPermissions_.end()) {
43         return PERMISSION_GRANTED;
44     }
45     std::string permissionName = sensorPermissions_[sensorTypeId];
46     int32_t ret = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
47     AddPermissionRecord(callerToken, permissionName, (ret == PERMISSION_GRANTED));
48     return ret;
49 }
50 
AddPermissionRecord(AccessTokenID tokenID,const std::string & permissionName,bool status)51 void PermissionUtil::AddPermissionRecord(AccessTokenID tokenID, const std::string& permissionName, bool status)
52 {
53     int32_t successCount = status ? 1 : 0;
54     int32_t failCount = status ? 0 : 1;
55     int32_t ret = PrivacyKit::AddPermissionUsedRecord(tokenID, permissionName, successCount, failCount);
56     if (ret != 0) {
57         HiLog::Error(LABEL,
58             "AddPermissionUsedRecord fail,permissionName:%{public}s,successCount:%{public}d,failCount:%{public}d",
59             permissionName.c_str(), successCount, failCount);
60     }
61 }
62 }  // namespace Sensors
63 }  // namespace OHOS
64