• 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 "device_info_stub.h"
17 
18 #include "beget_ext.h"
19 #include "idevice_info.h"
20 #include "ipc_skeleton.h"
21 #include "accesstoken_kit.h"
22 #include "parcel.h"
23 #include "string_ex.h"
24 #include "system_ability_definition.h"
25 #include "param_comm.h"
26 #include "sysparam_errno.h"
27 
28 using std::u16string;
29 namespace OHOS {
30 using namespace Security;
31 using namespace Security::AccessToken;
32 
33 namespace device_info {
REGISTER_SYSTEM_ABILITY_BY_ID(DeviceInfoService,SYSPARAM_DEVICE_SERVICE_ID,true)34 REGISTER_SYSTEM_ABILITY_BY_ID(DeviceInfoService, SYSPARAM_DEVICE_SERVICE_ID, true)
35 
36 int32_t DeviceInfoStub::OnRemoteRequest(uint32_t code,
37     MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39     std::u16string myDescriptor = IDeviceInfo::GetDescriptor();
40     std::u16string remoteDescriptor = data.ReadInterfaceToken();
41     DINFO_CHECK(myDescriptor == remoteDescriptor, return ERR_FAIL, "Invalid remoteDescriptor");
42 
43     int ret = ERR_FAIL;
44     switch (code) {
45         case COMMAND_GET_UDID: {
46             if (!CheckPermission(data, PERMISSION_UDID)) {
47                 return SYSPARAM_PERMISSION_DENIED;
48             }
49             char localDeviceInfo[UDID_LEN] = {0};
50             ret = GetDevUdid_(localDeviceInfo, UDID_LEN);
51             DINFO_CHECK(ret == 0, break, "Failed to get dev udid");
52             reply.WriteString16(Str8ToStr16(localDeviceInfo));
53             break;
54         }
55         case COMMAND_GET_SERIAL_ID: {
56             if (!CheckPermission(data, PERMISSION_UDID)) {
57                 return SYSPARAM_PERMISSION_DENIED;
58             }
59             const char *serialNumber = GetSerial_();
60             DINFO_CHECK(serialNumber != nullptr, break, "Failed to get serialNumber");
61             reply.WriteString16(Str8ToStr16(serialNumber));
62             break;
63         }
64         default: {
65             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
66         }
67     }
68     return ret;
69 }
70 
CheckPermission(MessageParcel & data,const std::string & permission)71 bool DeviceInfoStub::CheckPermission(MessageParcel &data, const std::string &permission)
72 {
73     AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
74     int32_t result = TypePermissionState::PERMISSION_GRANTED;
75     int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
76     if (tokenType == TOKEN_INVALID) {
77         DINFO_LOGE("AccessToken type:%d, permission:%d denied!", tokenType, callerToken);
78         return false;
79     } else {
80         result = AccessTokenKit::VerifyAccessToken(callerToken, permission);
81     }
82     if (result == TypePermissionState::PERMISSION_DENIED) {
83         DINFO_LOGE("AccessTokenID:%d, permission:%s denied!", callerToken, permission.c_str());
84         return false;
85     }
86     DINFO_LOGI("tokenType %d dAccessTokenID:%d, permission:%s matched!", tokenType, callerToken, permission.c_str());
87     return true;
88 }
89 
GetUdid(std::string & result)90 int32_t DeviceInfoService::GetUdid(std::string& result)
91 {
92     return 0;
93 }
GetSerialID(std::string & result)94 int32_t DeviceInfoService::GetSerialID(std::string& result)
95 {
96     return 0;
97 }
98 
OnStart(void)99 void DeviceInfoService::OnStart(void)
100 {
101     DINFO_LOGI("WatcherManager OnStart");
102     bool res = Publish(this);
103     if (!res) {
104         DINFO_LOGE("WatcherManager Publish failed");
105     }
106     return;
107 }
108 
OnStop(void)109 void DeviceInfoService::OnStop(void)
110 {
111 }
112 
Dump(int fd,const std::vector<std::u16string> & args)113 int DeviceInfoService::Dump(int fd, const std::vector<std::u16string>& args)
114 {
115     (void)args;
116     DINFO_LOGI("DeviceInfoService Dump");
117     DINFO_CHECK(fd >= 0, return -1, "Invalid fd for dump %d", fd);
118     return dprintf(fd, "%s\n", "No information to dump for this service");
119 }
120 } // namespace device_info
121 } // namespace OHOS
122