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::OnAddSystemAbilityInner;
38 memberFuncMap_[ON_REMOVE_SYSTEM_ABILITY] =
39 &SystemAbilityStatusChangeStub::OnRemoveSystemAbilityInner;
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 auto memberFunc = iter->second;
53 if (memberFunc != nullptr) {
54 return (this->*memberFunc)(data, reply);
55 }
56 }
57 HILOGW("unknown request code!");
58 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59 }
60
OnAddSystemAbilityInner(MessageParcel & data,MessageParcel & reply)61 int32_t SystemAbilityStatusChangeStub::OnAddSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
62 {
63 int32_t systemAbilityId = -1;
64 bool ret = data.ReadInt32(systemAbilityId);
65 if (!ret) {
66 return ERR_NULL_OBJECT;
67 }
68 if (!CheckInputSysAbilityId(systemAbilityId)) {
69 HILOGW("read systemAbilityId failed!");
70 return ERR_NULL_OBJECT;
71 }
72 std::string deviceId = data.ReadString();
73 int64_t begin = GetTickCount();
74 OnAddSystemAbility(systemAbilityId, deviceId);
75 HILOGD("OnAddSA:%{public}d, spend:%{public}" PRId64 " ms", systemAbilityId, GetTickCount() - begin);
76 return ERR_NONE;
77 }
78
OnRemoveSystemAbilityInner(MessageParcel & data,MessageParcel & reply)79 int32_t SystemAbilityStatusChangeStub::OnRemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
80 {
81 int32_t systemAbilityId = -1;
82 bool ret = data.ReadInt32(systemAbilityId);
83 if (!ret) {
84 return ERR_NULL_OBJECT;
85 }
86 if (!CheckInputSysAbilityId(systemAbilityId)) {
87 HILOGW("read systemAbilityId failed!");
88 return ERR_NULL_OBJECT;
89 }
90 std::string deviceId = data.ReadString();
91 int64_t begin = GetTickCount();
92 OnRemoveSystemAbility(systemAbilityId, deviceId);
93 HILOGD("OnRemoveSA:%{public}d, spend:%{public}" PRId64 " ms", systemAbilityId, GetTickCount() - begin);
94 return ERR_NONE;
95 }
96
CheckInputSysAbilityId(int32_t systemAbilityId)97 bool SystemAbilityStatusChangeStub::CheckInputSysAbilityId(int32_t systemAbilityId)
98 {
99 return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
100 }
101
EnforceInterceToken(MessageParcel & data)102 bool SystemAbilityStatusChangeStub::EnforceInterceToken(MessageParcel& data)
103 {
104 std::u16string interfaceToken = data.ReadInterfaceToken();
105 return interfaceToken == GetDescriptor();
106 }
107 }
108