1 /*
2  * Copyright (c) 2021-2022 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 "system_ability_status_change_stub.h"
17 
18 #include <cinttypes>
19 
20 #include "errors.h"
21 #include "ipc_object_stub.h"
22 #include "ipc_types.h"
23 #include "message_option.h"
24 #include "message_parcel.h"
25 #include "refbase.h"
26 #include "sam_log.h"
27 #include "datetime_ex.h"
28 
29 namespace OHOS {
30 namespace {
31 constexpr int32_t FIRST_SYS_ABILITY_ID = 0x00000000;
32 constexpr int32_t LAST_SYS_ABILITY_ID = 0x00ffffff;
33 }
SystemAbilityStatusChangeStub()34 SystemAbilityStatusChangeStub::SystemAbilityStatusChangeStub()
35 {
36     memberFuncMap_[ON_ADD_SYSTEM_ABILITY] =
37         SystemAbilityStatusChangeStub::LocalOnAddSystemAbility;
38     memberFuncMap_[ON_REMOVE_SYSTEM_ABILITY] =
39         SystemAbilityStatusChangeStub::LocalOnRemoveSystemAbility;
40 }
41 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int32_t SystemAbilityStatusChangeStub::OnRemoteRequest(uint32_t code,
43     MessageParcel& data, MessageParcel& reply, MessageOption& option)
44 {
45     HILOGD("SystemAbilityStatusChangeStub::code:%{public}u, flags:%{public}d", code, option.GetFlags());
46     if (!EnforceInterceToken(data)) {
47         HILOGW("check interface token failed!");
48         return ERR_PERMISSION_DENIED;
49     }
50     auto iter = memberFuncMap_.find(code);
51     if (iter != memberFuncMap_.end()) {
52         return iter->second(this, data, reply);
53     }
54     HILOGW("unknown request code!");
55     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
56 }
57 
OnAddSystemAbilityInner(MessageParcel & data,MessageParcel & reply)58 int32_t SystemAbilityStatusChangeStub::OnAddSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
59 {
60     int32_t systemAbilityId = -1;
61     bool ret = data.ReadInt32(systemAbilityId);
62     if (!ret) {
63         return ERR_NULL_OBJECT;
64     }
65     if (!CheckInputSysAbilityId(systemAbilityId)) {
66         HILOGW("read systemAbilityId failed!");
67         return ERR_NULL_OBJECT;
68     }
69     std::string deviceId = data.ReadString();
70     int64_t begin = GetTickCount();
71     OnAddSystemAbility(systemAbilityId, deviceId);
72     HILOGD("OnAddSA:%{public}d, spend:%{public}" PRId64 " ms", systemAbilityId, GetTickCount() - begin);
73     return ERR_NONE;
74 }
75 
OnRemoveSystemAbilityInner(MessageParcel & data,MessageParcel & reply)76 int32_t SystemAbilityStatusChangeStub::OnRemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
77 {
78     int32_t systemAbilityId = -1;
79     bool ret = data.ReadInt32(systemAbilityId);
80     if (!ret) {
81         return ERR_NULL_OBJECT;
82     }
83     if (!CheckInputSysAbilityId(systemAbilityId)) {
84         HILOGW("read systemAbilityId failed!");
85         return ERR_NULL_OBJECT;
86     }
87     std::string deviceId = data.ReadString();
88     int64_t begin = GetTickCount();
89     OnRemoveSystemAbility(systemAbilityId, deviceId);
90     HILOGD("OnRemoveSA:%{public}d, spend:%{public}" PRId64 " ms", systemAbilityId, GetTickCount() - begin);
91     return ERR_NONE;
92 }
93 
CheckInputSysAbilityId(int32_t systemAbilityId)94 bool SystemAbilityStatusChangeStub::CheckInputSysAbilityId(int32_t systemAbilityId)
95 {
96     return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
97 }
98 
EnforceInterceToken(MessageParcel & data)99 bool SystemAbilityStatusChangeStub::EnforceInterceToken(MessageParcel& data)
100 {
101     std::u16string interfaceToken = data.ReadInterfaceToken();
102     return interfaceToken == GetDescriptor();
103 }
104 }
105