• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "ipc_skeleton.h"
21 #include "iremote_broker.h"
22 #include "iremote_object.h"
23 #include "iremote_proxy.h"
24 #include "softbus_errcode.h"
25 #include "softbus_ipc_def.h"
26 #include "softbus_log.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         return nullptr;
43     }
44 
45     data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
46     MessageParcel reply;
47     MessageOption option;
48     sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
49     int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
50     if (err != 0) {
51         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Get GetSystemAbility failed!\n");
52         return nullptr;
53     }
54     return reply.ReadRemoteObject();
55 }
56 
DiscServerProxyInit(void)57 int32_t DiscServerProxyInit(void)
58 {
59     std::lock_guard<std::mutex> lock(g_mutex);
60     sptr<IRemoteObject> object = GetSystemAbility();
61     if (object == nullptr) {
62         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Get remote softbus object failed!\n");
63         return SOFTBUS_ERR;
64     }
65     g_serverProxy = new (std::nothrow) DiscServerProxy(object);
66     if (g_serverProxy == nullptr) {
67         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Create disc server proxy failed!\n");
68         return SOFTBUS_ERR;
69     }
70     return SOFTBUS_OK;
71 }
72 
DiscServerProxyDeInit(void)73 void DiscServerProxyDeInit(void)
74 {
75     delete g_serverProxy;
76     g_serverProxy = nullptr;
77 }
78 
ServerIpcPublishService(const char * pkgName,const PublishInfo * info)79 int32_t ServerIpcPublishService(const char *pkgName, const PublishInfo *info)
80 {
81     if (g_serverProxy == nullptr) {
82         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
83         return SOFTBUS_ERR;
84     }
85     return g_serverProxy->PublishService(pkgName, info);
86 }
87 
ServerIpcUnPublishService(const char * pkgName,int32_t publishId)88 int32_t ServerIpcUnPublishService(const char *pkgName, int32_t publishId)
89 {
90     if (g_serverProxy == nullptr) {
91         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
92         return SOFTBUS_ERR;
93     }
94     int ret = g_serverProxy->UnPublishService(pkgName, publishId);
95     return ret;
96 }
97 
ServerIpcStartDiscovery(const char * pkgName,const SubscribeInfo * info)98 int32_t ServerIpcStartDiscovery(const char *pkgName, const SubscribeInfo *info)
99 {
100     if (g_serverProxy == nullptr) {
101         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
102         return SOFTBUS_ERR;
103     }
104     return g_serverProxy->StartDiscovery(pkgName, info);
105 }
106 
ServerIpcStopDiscovery(const char * pkgName,int32_t subscribeId)107 int32_t ServerIpcStopDiscovery(const char *pkgName, int32_t subscribeId)
108 {
109     if (g_serverProxy == nullptr) {
110         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
111         return SOFTBUS_ERR;
112     }
113     return g_serverProxy->StopDiscovery(pkgName, subscribeId);
114 }
115