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 "comm_log.h"
17 #include "ipc_skeleton.h"
18 #include "iproxy_client.h"
19 #include "samgr_lite.h"
20 #include "softbus_adapter_file.h"
21 #include "softbus_adapter_timer.h"
22 #include "softbus_def.h"
23 #include "softbus_errcode.h"
24 #include "softbus_server_ipc_interface_code.h"
25 #include "softbus_server_proxy.h"
26 
27 
28 #define WAIT_SERVER_READY_INTERVAL_COUNT 50
29 
30 static IClientProxy *g_serverProxy = NULL;
31 static IClientProxy *g_oldServerProxy = NULL;
32 
ClientSimpleResultCb(IOwner owner, int code, IpcIo *reply)33 static int ClientSimpleResultCb(IOwner owner, int code, IpcIo *reply)
34 {
35     ReadInt32(reply, (int *)owner);
36     COMM_LOGI(COMM_SDK, "retvalue=%{public}d", *(int *)owner);
37     return EC_SUCCESS;
38 }
39 
GetServerProxy(void)40 static IClientProxy *GetServerProxy(void)
41 {
42     IClientProxy *clientProxy = NULL;
43 
44     COMM_LOGI(COMM_SDK, "start get client proxy");
45     int32_t proxyInitCount = 0;
46     while (clientProxy == NULL) {
47         proxyInitCount++;
48         if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) {
49             COMM_LOGE(COMM_SDK, "frame get server proxy error");
50             return NULL;
51         }
52         IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SOFTBUS_SERVICE);
53         if (iUnknown == NULL) {
54             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
55             continue;
56         }
57 
58         int32_t ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&clientProxy);
59         if (ret != EC_SUCCESS || clientProxy == NULL) {
60             COMM_LOGE(COMM_SDK, "QueryInterface failed. ret=%{public}d", ret);
61             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
62             continue;
63         }
64     }
65 
66     COMM_LOGI(COMM_SDK, "frame get client proxy ok");
67     return clientProxy;
68 }
69 
RegisterService(const char *name, const struct CommonScvId *svcId)70 int32_t RegisterService(const char *name, const struct CommonScvId *svcId)
71 {
72     COMM_LOGI(COMM_SDK, "server register service client push.");
73     if ((svcId == NULL) || (name == NULL)) {
74         COMM_LOGE(COMM_SDK, "Invalid param");
75         return SOFTBUS_INVALID_PARAM;
76     }
77     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
78 
79     IpcIo request = {0};
80     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 1);
81     WriteString(&request, name);
82 
83     SvcIdentity svc = {0};
84     svc.handle = svcId->handle;
85     svc.token = svcId->token;
86     svc.cookie = svcId->cookie;
87     bool value = WriteRemoteObject(&request, &svc);
88     if (!value) {
89         return SOFTBUS_TRANS_PROXY_WRITEOBJECT_FAILED;
90     }
91 
92     int ret = SOFTBUS_IPC_ERR;
93     if (g_serverProxy->Invoke(g_serverProxy, MANAGE_REGISTER_SERVICE, &request, &ret,
94         ClientSimpleResultCb) != EC_SUCCESS) {
95         COMM_LOGI(COMM_SDK, "Call back ret=%{public}d", ret);
96         return SOFTBUS_IPC_ERR;
97     }
98     return ret;
99 }
100 
HOS_SystemInit(void)101 void __attribute__((weak)) HOS_SystemInit(void)
102 {
103     SAMGR_Bootstrap();
104     return;
105 }
106 
ServerProxyInit(void)107 int32_t ServerProxyInit(void)
108 {
109     HOS_SystemInit();
110     g_serverProxy = GetServerProxy();
111     if (g_serverProxy == NULL) {
112         COMM_LOGE(COMM_SDK, "get ipc client proxy failed");
113         return SOFTBUS_IPC_ERR;
114     }
115 
116     if (g_serverProxy == g_oldServerProxy) {
117         g_serverProxy = NULL;
118         COMM_LOGE(COMM_SDK, "get ipc client proxy is the same as old");
119         return SOFTBUS_IPC_ERR;
120     }
121 
122     COMM_LOGI(COMM_SDK, "ServerProvideInterfaceInit ok");
123     return SOFTBUS_OK;
124 }
125 
ServerProxyDeInit(void)126 int32_t ServerProxyDeInit(void)
127 {
128     g_oldServerProxy = g_serverProxy;
129     if (g_serverProxy != NULL) {
130         (void)g_serverProxy->Release((IUnknown *)(g_serverProxy));
131         g_serverProxy = NULL;
132     }
133 
134     COMM_LOGI(COMM_SDK, "ServerProxyDeInit ok");
135     return SOFTBUS_OK;
136 }