1 /*
2 * Copyright (c) 2025 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 "general_connection_server_proxy.h"
17
18 #include <mutex>
19 #include "conn_log.h"
20 #include "general_connection_server_proxy_standard.h"
21 #include "ipc_skeleton.h"
22 #include "iremote_broker.h"
23 #include "iremote_object.h"
24 #include "iremote_proxy.h"
25 #include "softbus_error_code.h"
26 #include "softbus_server_ipc_interface_code.h"
27
28 using namespace OHOS;
29 namespace {
30 sptr<ConnectionServerProxy> g_serverProxy = nullptr;
31 uint32_t g_getSystemAbilityId = 2;
32 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
33 std::mutex g_mutex;
34 } // namespace
35
GetSystemAbility()36 static sptr<IRemoteObject> GetSystemAbility()
37 {
38 MessageParcel data;
39
40 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
41 return nullptr;
42 }
43 if (!data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER)) {
44 CONN_LOGE(CONN_COMMON, "write SOFTBUS_SERVER_SA_ID_INNER failed");
45 return nullptr;
46 }
47 MessageParcel reply;
48 MessageOption option;
49 sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
50 if (samgr == nullptr) {
51 CONN_LOGE(CONN_COMMON, "get samgr failed");
52 return nullptr;
53 }
54 int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
55 if (err != 0) {
56 CONN_LOGE(CONN_COMMON, "get GetSystemAbility failed, err=%{public}d", err);
57 return nullptr;
58 }
59 return reply.ReadRemoteObject();
60 }
61
ConnectionServerProxyInit(void)62 int32_t ConnectionServerProxyInit(void)
63 {
64 std::lock_guard<std::mutex> lock(g_mutex);
65 if (g_serverProxy != nullptr) {
66 CONN_LOGI(CONN_INIT, "Init success");
67 return SOFTBUS_OK;
68 }
69 sptr<IRemoteObject> object = GetSystemAbility();
70 if (object == nullptr) {
71 CONN_LOGE(CONN_INIT, "Get remote softbus object failed");
72 return SOFTBUS_SERVER_NOT_INIT;
73 }
74 g_serverProxy = new (std::nothrow) ConnectionServerProxy(object);
75 if (g_serverProxy == nullptr) {
76 CONN_LOGE(CONN_INIT, "Create connection server proxy failed");
77 return SOFTBUS_SERVER_NOT_INIT;
78 }
79 return SOFTBUS_OK;
80 }
81
ConnectionServerProxyDeInit(void)82 void ConnectionServerProxyDeInit(void)
83 {
84 std::lock_guard<std::mutex> lock(g_mutex);
85 if (g_serverProxy == nullptr) {
86 CONN_LOGE(CONN_INIT, "g_serverProxy is nullptr");
87 return;
88 }
89 g_serverProxy.clear();
90 }
91
ServerIpcCreateServer(const char * pkgName,const char * name)92 int32_t ServerIpcCreateServer(const char *pkgName, const char *name)
93 {
94 CONN_CHECK_AND_RETURN_RET_LOGE(
95 g_serverProxy != nullptr, SOFTBUS_NO_INIT, CONN_COMMON, "softbus server g_serverProxy is nullptr");
96
97 return g_serverProxy->CreateServer(pkgName, name);
98 }
99
ServerIpcRemoveServer(const char * pkgName,const char * name)100 int32_t ServerIpcRemoveServer(const char *pkgName, const char *name)
101 {
102 CONN_CHECK_AND_RETURN_RET_LOGE(
103 g_serverProxy != nullptr, SOFTBUS_NO_INIT, CONN_COMMON, "softbus server g_serverProxy is nullptr");
104
105 return g_serverProxy->RemoveServer(pkgName, name);
106 }
107
ServerIpcConnect(const char * pkgName,const char * name,const Address * address)108 int32_t ServerIpcConnect(const char *pkgName, const char *name, const Address *address)
109 {
110 CONN_CHECK_AND_RETURN_RET_LOGE(
111 g_serverProxy != nullptr, SOFTBUS_NO_INIT, CONN_COMMON, "softbus server g_serverProxy is nullptr");
112
113 return g_serverProxy->Connect(pkgName, name, address);
114 }
115
ServerIpcDisconnect(uint32_t handle)116 int32_t ServerIpcDisconnect(uint32_t handle)
117 {
118 CONN_CHECK_AND_RETURN_RET_LOGE(
119 g_serverProxy != nullptr, SOFTBUS_NO_INIT, CONN_COMMON, "softbus server g_serverProxy is nullptr");
120
121 return g_serverProxy->Disconnect(handle);
122 }
123
ServerIpcSend(uint32_t handle,const uint8_t * data,uint32_t len)124 int32_t ServerIpcSend(uint32_t handle, const uint8_t *data, uint32_t len)
125 {
126 CONN_CHECK_AND_RETURN_RET_LOGE(
127 g_serverProxy != nullptr, SOFTBUS_NO_INIT, CONN_COMMON, "softbus server g_serverProxy is nullptr");
128
129 return g_serverProxy->Send(handle, data, len);
130 }
131
ServerIpcGetPeerDeviceId(uint32_t handle,char * deviceId,uint32_t len)132 int32_t ServerIpcGetPeerDeviceId(uint32_t handle, char *deviceId, uint32_t len)
133 {
134 CONN_CHECK_AND_RETURN_RET_LOGE(
135 g_serverProxy != nullptr, SOFTBUS_NO_INIT, CONN_COMMON, "softbus server g_serverProxy is nullptr");
136
137 return g_serverProxy->ConnGetPeerDeviceId(handle, deviceId, len);
138 }