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 
ClientSimpleResultCb(IOwner owner,int code,IpcIo * reply)32 static int ClientSimpleResultCb(IOwner owner, int code, IpcIo *reply)
33 {
34     ReadInt32(reply, (int *)owner);
35     COMM_LOGI(COMM_SDK, "retvalue=%{public}d", *(int *)owner);
36     return EC_SUCCESS;
37 }
38 
GetServerProxy(void)39 static IClientProxy *GetServerProxy(void)
40 {
41     IClientProxy *clientProxy = NULL;
42 
43     COMM_LOGI(COMM_SDK, "start get client proxy");
44     int32_t proxyInitCount = 0;
45     while (clientProxy == NULL) {
46         proxyInitCount++;
47         if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) {
48             COMM_LOGE(COMM_SDK, "frame get server proxy error");
49             return NULL;
50         }
51         IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SOFTBUS_SERVICE);
52         if (iUnknown == NULL) {
53             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
54             continue;
55         }
56 
57         int32_t ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&clientProxy);
58         if (ret != EC_SUCCESS || clientProxy == NULL) {
59             COMM_LOGE(COMM_SDK, "QueryInterface failed. ret=%{public}d", ret);
60             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
61             continue;
62         }
63     }
64 
65     COMM_LOGI(COMM_SDK, "frame get client proxy ok");
66     return clientProxy;
67 }
68 
RegisterService(const char * name,const struct CommonScvId * svcId)69 int32_t RegisterService(const char *name, const struct CommonScvId *svcId)
70 {
71     COMM_LOGI(COMM_SDK, "server register service client push.");
72     if ((svcId == NULL) || (name == NULL)) {
73         COMM_LOGE(COMM_SDK, "Invalid param");
74         return SOFTBUS_INVALID_PARAM;
75     }
76     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
77 
78     IpcIo request = {0};
79     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 1);
80     WriteString(&request, name);
81 
82     SvcIdentity svc = {0};
83     svc.handle = svcId->handle;
84     svc.token = svcId->token;
85     svc.cookie = svcId->cookie;
86     bool value = WriteRemoteObject(&request, &svc);
87     if (!value) {
88         return SOFTBUS_ERR;
89     }
90 
91     int ret = SOFTBUS_ERR;
92     if (g_serverProxy->Invoke(g_serverProxy, MANAGE_REGISTER_SERVICE, &request, &ret,
93         ClientSimpleResultCb) != EC_SUCCESS) {
94         COMM_LOGI(COMM_SDK, "Call back ret=%{public}d", ret);
95         return SOFTBUS_ERR;
96     }
97     return ret;
98 }
99 
HOS_SystemInit(void)100 void __attribute__((weak)) HOS_SystemInit(void)
101 {
102     SAMGR_Bootstrap();
103     return;
104 }
105 
ServerProxyInit(void)106 int32_t ServerProxyInit(void)
107 {
108     HOS_SystemInit();
109     g_serverProxy = GetServerProxy();
110     if (g_serverProxy == NULL) {
111         COMM_LOGE(COMM_SDK, "get ipc client proxy failed");
112         return SOFTBUS_ERR;
113     }
114     COMM_LOGI(COMM_SDK, "ServerProvideInterfaceInit ok");
115     return SOFTBUS_OK;
116 }
117