• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "disc_server_proxy.h"
17 #include "disc_server_proxy_standard.h"
18 
19 #include <mutex>
20 #include "disc_log.h"
21 #include "ipc_skeleton.h"
22 #include "iremote_broker.h"
23 #include "iremote_object.h"
24 #include "iremote_proxy.h"
25 #include "softbus_errcode.h"
26 #include "softbus_server_ipc_interface_code.h"
27 
28 using namespace OHOS;
29 
30 namespace {
31 sptr<DiscServerProxy> g_serverProxy = nullptr;
32 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
33 uint32_t g_getSystemAbilityId = 2;
34 std::mutex g_mutex;
35 }
36 
GetSystemAbility()37 static sptr<IRemoteObject> GetSystemAbility()
38 {
39     MessageParcel data;
40 
41     if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
42         DISC_LOGE(DISC_SDK, "Write INTERFACE_TOKEN failed!");
43         return nullptr;
44     }
45 
46     if (!data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER)) {
47         DISC_LOGE(DISC_SDK, "Write SERVER_SA_ID_INNER failed!");
48         return nullptr;
49     }
50     MessageParcel reply;
51     MessageOption option;
52     sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
53     if (samgr == nullptr) {
54         DISC_LOGE(DISC_SDK, "Get samgr failed!");
55         return nullptr;
56     }
57     int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
58     if (err != 0) {
59         DISC_LOGE(DISC_SDK, "Get GetSystemAbility failed!");
60         return nullptr;
61     }
62     return reply.ReadRemoteObject();
63 }
64 
DiscServerProxyInit(void)65 int32_t DiscServerProxyInit(void)
66 {
67     std::lock_guard<std::mutex> lock(g_mutex);
68     if (g_serverProxy != nullptr) {
69         DISC_LOGI(DISC_INIT, "Init success");
70         return SOFTBUS_OK;
71     }
72     sptr<IRemoteObject> object = GetSystemAbility();
73     if (object == nullptr) {
74         DISC_LOGE(DISC_INIT, "Get remote softbus object failed!");
75         return SOFTBUS_ERR;
76     }
77     g_serverProxy = new (std::nothrow) DiscServerProxy(object);
78     if (g_serverProxy == nullptr) {
79         DISC_LOGE(DISC_INIT, "Create disc server proxy failed!");
80         return SOFTBUS_ERR;
81     }
82     return SOFTBUS_OK;
83 }
84 
DiscServerProxyDeInit(void)85 void DiscServerProxyDeInit(void)
86 {
87     g_serverProxy.clear();
88 }
89 
ServerIpcPublishService(const char * pkgName,const PublishInfo * info)90 int32_t ServerIpcPublishService(const char *pkgName, const PublishInfo *info)
91 {
92     if (g_serverProxy == nullptr) {
93         DISC_LOGE(DISC_SDK, "softbus server g_serverProxy is nullptr!");
94         return SOFTBUS_ERR;
95     }
96     return g_serverProxy->PublishService(pkgName, info);
97 }
98 
ServerIpcUnPublishService(const char * pkgName,int32_t publishId)99 int32_t ServerIpcUnPublishService(const char *pkgName, int32_t publishId)
100 {
101     if (g_serverProxy == nullptr) {
102         DISC_LOGE(DISC_SDK, "softbus server g_serverProxy is nullptr!");
103         return SOFTBUS_ERR;
104     }
105     int ret = g_serverProxy->UnPublishService(pkgName, publishId);
106     return ret;
107 }
108 
ServerIpcStartDiscovery(const char * pkgName,const SubscribeInfo * info)109 int32_t ServerIpcStartDiscovery(const char *pkgName, const SubscribeInfo *info)
110 {
111     if (g_serverProxy == nullptr) {
112         DISC_LOGE(DISC_SDK, "softbus server g_serverProxy is nullptr!");
113         return SOFTBUS_ERR;
114     }
115     return g_serverProxy->StartDiscovery(pkgName, info);
116 }
117 
ServerIpcStopDiscovery(const char * pkgName,int32_t subscribeId)118 int32_t ServerIpcStopDiscovery(const char *pkgName, int32_t subscribeId)
119 {
120     if (g_serverProxy == nullptr) {
121         DISC_LOGE(DISC_SDK, "softbus server g_serverProxy is nullptr!");
122         return SOFTBUS_ERR;
123     }
124     return g_serverProxy->StopDiscovery(pkgName, subscribeId);
125 }