• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(int32_t exitCode)32 bool IpcProxy::RequestExitChildProcess(int32_t exitCode)
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         OH_IPCParcel_WriteInt32(data.get(), exitCode) != OH_IPC_SUCCESS) {
46         return false;
47     }
48 
49     OH_IPC_MessageOption ipcOpt;
50     ipcOpt.mode = OH_IPC_REQUEST_MODE_SYNC;
51     ipcOpt.timeout = 0;
52     ipcOpt.reserved = nullptr;
53     int ret = OH_IPCRemoteProxy_SendRequest(ipcProxy_, IPC_ID_REQUEST_EXIT_PROCESS, data.get(), reply.get(), &ipcOpt);
54     if (ret != OH_IPC_SUCCESS) {
55         return false;
56     }
57 
58     return true;
59 }
60 
Add(int32_t a,int32_t b)61 int32_t IpcProxy::Add(int32_t a, int32_t b)
62 {
63     if (ipcProxy_ == nullptr) {
64         return INT32_MIN;
65     }
66 
67     int32_t result = INT32_MIN;
68     StdUniPtrIpcParcel data(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
69     StdUniPtrIpcParcel reply(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
70     if (data == nullptr || reply == nullptr) {
71         return result;
72     }
73 
74     if (!WriteInterfaceToken(data.get()) ||
75         OH_IPCParcel_WriteInt32(data.get(), a) != OH_IPC_SUCCESS ||
76         OH_IPCParcel_WriteInt32(data.get(), b) != OH_IPC_SUCCESS) {
77         return result;
78     }
79 
80     OH_IPC_MessageOption ipcOpt;
81     ipcOpt.mode = OH_IPC_REQUEST_MODE_SYNC;
82     ipcOpt.timeout = 0;
83     ipcOpt.reserved = nullptr;
84     int ret = OH_IPCRemoteProxy_SendRequest(ipcProxy_, IPC_ID_ADD, data.get(), reply.get(), &ipcOpt);
85     if (ret != OH_IPC_SUCCESS) {
86         return result;
87     }
88 
89     OH_IPCParcel_ReadInt32(reply.get(), &result);
90     return result;
91 }
92 
StartNativeChildProcess()93 int32_t IpcProxy::StartNativeChildProcess()
94 {
95     if (ipcProxy_ == nullptr) {
96         return INT32_MIN;
97     }
98 
99     int32_t result = INT32_MIN;
100     StdUniPtrIpcParcel data(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
101     StdUniPtrIpcParcel reply(OH_IPCParcel_Create(), OH_IPCParcel_Destroy);
102     if (data == nullptr || reply == nullptr) {
103         return result;
104     }
105 
106     if (!WriteInterfaceToken(data.get())) {
107         return result;
108     }
109 
110     OH_IPC_MessageOption ipcOpt;
111     ipcOpt.mode = OH_IPC_REQUEST_MODE_SYNC;
112     ipcOpt.timeout = 0;
113     ipcOpt.reserved = nullptr;
114     int ret = OH_IPCRemoteProxy_SendRequest(
115         ipcProxy_, IPC_ID_START_NATIVE_CHILD_PROCESS, data.get(), reply.get(), &ipcOpt);
116     if (ret != OH_IPC_SUCCESS) {
117         return result;
118     }
119 
120     OH_IPCParcel_ReadInt32(reply.get(), &result);
121     return result;
122 }
123 
WriteInterfaceToken(OHIPCParcel * data)124 bool IpcProxy::WriteInterfaceToken(OHIPCParcel* data)
125 {
126     return OH_IPCParcel_WriteInterfaceToken(data, interfaceToken_) == OH_IPC_SUCCESS;
127 }
128