1 /*
2 * Copyright (c) 2023 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 "dms_client.h"
17
18 #include "ability_manager_errors.h"
19 #include "distributed_parcel_helper.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "string_ex.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace DistributedSchedule {
28 namespace {
29 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
30 }
GetDmsProxy()31 sptr<IRemoteObject> DistributedClient::GetDmsProxy()
32 {
33 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34 if (samgrProxy == nullptr) {
35 HILOG_ERROR("fail to get samgr.");
36 return nullptr;
37 }
38 return samgrProxy->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID);
39 }
40
RegisterDSchedEventListener(const std::string & type,const sptr<IDSchedEventListener> & obj)41 int32_t DistributedClient::RegisterDSchedEventListener(const std::string& type,
42 const sptr<IDSchedEventListener>& obj)
43 {
44 HILOG_INFO("RegisterDSchedEventListener called");
45 sptr<IRemoteObject> remote = GetDmsProxy();
46 if (remote == nullptr) {
47 HILOG_ERROR("remote system ablity is nullptr");
48 return AAFwk::INVALID_PARAMETERS_ERR;
49 }
50 MessageParcel data;
51 MessageParcel reply;
52 if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
53 return ERR_FLATTEN_OBJECT;
54 }
55 PARCEL_WRITE_HELPER(data, String, type);
56 PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
57 PARCEL_TRANSACT_SYNC_RET_INT(remote, REGISTER_DSCHED_EVENT_LISTENER, data, reply);
58 }
59
UnRegisterDSchedEventListener(const std::string & type,const sptr<IDSchedEventListener> & obj)60 int32_t DistributedClient::UnRegisterDSchedEventListener(const std::string& type,
61 const sptr<IDSchedEventListener>& obj)
62 {
63 HILOG_INFO("UnRegisterDSchedEventListener called");
64 sptr<IRemoteObject> remote = GetDmsProxy();
65 if (remote == nullptr) {
66 HILOG_ERROR("remote system ablity is null");
67 return AAFwk::INVALID_PARAMETERS_ERR;
68 }
69 MessageParcel data;
70 MessageParcel reply;
71 if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
72 HILOG_DEBUG("write interface token failed.");
73 return ERR_FLATTEN_OBJECT;
74 }
75 PARCEL_WRITE_HELPER(data, String, type);
76 PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
77 PARCEL_TRANSACT_SYNC_RET_INT(remote, UNREGISTER_DSCHED_EVENT_LISTENER, data, reply);
78 }
79 } // namespace DistributedSchedule
80 } // namespace OHOS