1 /*
2  * Copyright (C) 2024 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 "IpcProxy.h"
17 #include <IPCKit/ipc_error_code.h>
18 #include "Ipchelper.h"
19 
IpcProxy(OHIPCRemoteProxy *ipcProxy)20 IpcProxy::IpcProxy(OHIPCRemoteProxy *ipcProxy)
21     : ipcProxy_(ipcProxy)
22 {
23 }
24 
~IpcProxy()25 IpcProxy::~IpcProxy()
26 {
27     if (ipcProxy_ != nullptr) {
28         OH_IPCRemoteProxy_Destroy(ipcProxy_);
29     }
30 }
31 
RequestExitChildProcess()32 bool IpcProxy::RequestExitChildProcess()
33 {
34     if (ipcProxy_ == nullptr) {
35         return false;
36     }
37 
38     StdUniPtrIpcParcel data(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
39     StdUniPtrIpcParcel reply(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
40     if (data == nullptr || reply == nullptr) {
41         return false;
42     }
43 
44     if (!WriteInterfaceToken(data.get())) {
45         return false;
46     }
47 
48     OH_IPC_MessageOption ipcOpt;
49     ipcOpt.mode = OH_IPC_REQUEST_MODE_SYNC;
50     ipcOpt.timeout = 0;
51     ipcOpt.reserved = nullptr;
52     int ret = OH_IPCRemoteProxy_SendRequest(ipcProxy_, IPC_ID_REQUEST_EXIT_PROCESS, data.get(), reply.get(), &ipcOpt);
53     if (ret != OH_IPC_SUCCESS) {
54         return false;
55     }
56 
57     return true;
58 }
59 
Add(int32_t a, int32_t b)60 int32_t IpcProxy::Add(int32_t a, int32_t b)
61 {
62     if (ipcProxy_ == nullptr) {
63         return INT32_MIN;
64     }
65 
66     int32_t result = INT32_MIN;
67     StdUniPtrIpcParcel data(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
68     StdUniPtrIpcParcel reply(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
69     if (data == nullptr || reply == nullptr) {
70         return result;
71     }
72 
73     if (!WriteInterfaceToken(data.get()) ||
74         OH_IPCParcel_WriteInt32(data.get(), a) != OH_IPC_SUCCESS ||
75         OH_IPCParcel_WriteInt32(data.get(), b) != OH_IPC_SUCCESS) {
76         return result;
77     }
78 
79     OH_IPC_MessageOption ipcOpt;
80     ipcOpt.mode = OH_IPC_REQUEST_MODE_SYNC;
81     ipcOpt.timeout = 0;
82     ipcOpt.reserved = nullptr;
83     int ret = OH_IPCRemoteProxy_SendRequest(ipcProxy_, IPC_ID_ADD, data.get(), reply.get(), &ipcOpt);
84     if (ret != OH_IPC_SUCCESS) {
85         return result;
86     }
87 
88     OH_IPCParcel_ReadInt32(reply.get(), &result);
89     return result;
90 }
91 
StartNativeChildProcess()92 int32_t IpcProxy::StartNativeChildProcess()
93 {
94     if (ipcProxy_ == nullptr) {
95         return INT32_MIN;
96     }
97 
98     int32_t result = INT32_MIN;
99     StdUniPtrIpcParcel data(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
100     StdUniPtrIpcParcel reply(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
101     if (data == nullptr || reply == nullptr) {
102         return result;
103     }
104 
105     if (!WriteInterfaceToken(data.get())) {
106         return result;
107     }
108 
109     OH_IPC_MessageOption ipcOpt;
110     ipcOpt.mode = OH_IPC_REQUEST_MODE_SYNC;
111     ipcOpt.timeout = 0;
112     ipcOpt.reserved = nullptr;
113     int ret = OH_IPCRemoteProxy_SendRequest(
114         ipcProxy_, IPC_ID_START_NATIVE_CHILD_PROCESS, data.get(), reply.get(), &ipcOpt);
115     if (ret != OH_IPC_SUCCESS) {
116         return result;
117     }
118 
119     OH_IPCParcel_ReadInt32(reply.get(), &result);
120     return result;
121 }
122 
WriteInterfaceToken(OHIPCParcel* data)123 bool IpcProxy::WriteInterfaceToken(OHIPCParcel* data)
124 {
125     return OH_IPCParcel_WriteInterfaceToken(data, interfaceToken_) == OH_IPC_SUCCESS;
126 }
127