• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "abilityms_client.h"
17 
18 #include "ability_errors.h"
19 #include "ability_service_interface.h"
20 #include "liteipc_adapter.h"
21 #include "log.h"
22 #include "samgr_lite.h"
23 #include "serializer.h"
24 #include "want_utils.h"
25 
26 namespace OHOS {
27 const unsigned int ERROR_SLEEP_TIMES = 300000;
28 const unsigned int RETRY_TIMES = 20;
29 
Callback(void * owner,int code,IpcIo * reply)30 static int32_t Callback(void *owner, int code, IpcIo *reply)
31 {
32     return 0;
33 }
34 
Initialize() const35 bool AbilityMsClient::Initialize() const
36 {
37     if (amsProxy_ != nullptr) {
38         return true;
39     }
40     int retry = RETRY_TIMES;
41     while (retry--) {
42         IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_FEATURE);
43         if (iUnknown == nullptr) {
44             HILOG_ERROR(HILOG_MODULE_APP, "iUnknown is null");
45             usleep(ERROR_SLEEP_TIMES); // sleep 300ms
46             continue;
47         }
48 
49         (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&amsProxy_);
50         if (amsProxy_ == nullptr) {
51             HILOG_ERROR(HILOG_MODULE_APP, "ams proxy is null");
52             usleep(ERROR_SLEEP_TIMES); // sleep 300ms
53             continue;
54         }
55 
56         return true;
57     }
58 
59     return false;
60 }
61 
SchedulerLifecycleDone(uint64_t token,int state) const62 int AbilityMsClient::SchedulerLifecycleDone(uint64_t token, int state) const
63 {
64     if (amsProxy_ == nullptr) {
65         return PARAM_NULL_ERROR;
66     }
67     IpcIo req;
68     char data[IPC_IO_DATA_MAX];
69     IpcIoInit(&req, data, IPC_IO_DATA_MAX, 0);
70     IpcIoPushUint64(&req, token);
71     IpcIoPushInt32(&req, state);
72     return amsProxy_->Invoke(amsProxy_, ABILITY_TRANSACTION_DONE, &req, nullptr, Callback);
73 }
74 
ScheduleAms(const Want * want,uint64_t token,const SvcIdentity * sid,int commandType) const75 int AbilityMsClient::ScheduleAms(const Want *want, uint64_t token, const SvcIdentity *sid, int commandType) const
76 {
77     if (amsProxy_ == nullptr) {
78         return PARAM_NULL_ERROR;
79     }
80     IpcIo req;
81     char data[IPC_IO_DATA_MAX];
82     IpcIoInit(&req, data, IPC_IO_DATA_MAX, 3);
83     if (token != 0) {
84         IpcIoPushUint64(&req, token);
85     }
86     if (sid != nullptr) {
87         IpcIoPushSvc(&req, sid);
88 #ifdef __LINUX__
89         if (commandType == ATTACH_BUNDLE) {
90             pid_t pid = getpid();
91             IpcIoPushUint64(&req, pid);
92         }
93 #endif
94     }
95     if (want != nullptr && !SerializeWant(&req, want)) {
96         return SERIALIZE_ERROR;
97     }
98     return amsProxy_->Invoke(amsProxy_, commandType, &req, nullptr, Callback);
99 }
100 } //  namespace OHOS
101