1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci
16060ff233Sopenharmony_ci#include "comm_log.h"
17060ff233Sopenharmony_ci#include "ipc_skeleton.h"
18060ff233Sopenharmony_ci#include "iproxy_client.h"
19060ff233Sopenharmony_ci#include "samgr_lite.h"
20060ff233Sopenharmony_ci#include "softbus_adapter_file.h"
21060ff233Sopenharmony_ci#include "softbus_adapter_timer.h"
22060ff233Sopenharmony_ci#include "softbus_def.h"
23060ff233Sopenharmony_ci#include "softbus_errcode.h"
24060ff233Sopenharmony_ci#include "softbus_server_ipc_interface_code.h"
25060ff233Sopenharmony_ci#include "softbus_server_proxy.h"
26060ff233Sopenharmony_ci
27060ff233Sopenharmony_ci
28060ff233Sopenharmony_ci#define WAIT_SERVER_READY_INTERVAL_COUNT 50
29060ff233Sopenharmony_ci
30060ff233Sopenharmony_cistatic IClientProxy *g_serverProxy = NULL;
31060ff233Sopenharmony_cistatic IClientProxy *g_oldServerProxy = NULL;
32060ff233Sopenharmony_ci
33060ff233Sopenharmony_cistatic int ClientSimpleResultCb(IOwner owner, int code, IpcIo *reply)
34060ff233Sopenharmony_ci{
35060ff233Sopenharmony_ci    ReadInt32(reply, (int *)owner);
36060ff233Sopenharmony_ci    COMM_LOGI(COMM_SDK, "retvalue=%{public}d", *(int *)owner);
37060ff233Sopenharmony_ci    return EC_SUCCESS;
38060ff233Sopenharmony_ci}
39060ff233Sopenharmony_ci
40060ff233Sopenharmony_cistatic IClientProxy *GetServerProxy(void)
41060ff233Sopenharmony_ci{
42060ff233Sopenharmony_ci    IClientProxy *clientProxy = NULL;
43060ff233Sopenharmony_ci
44060ff233Sopenharmony_ci    COMM_LOGI(COMM_SDK, "start get client proxy");
45060ff233Sopenharmony_ci    int32_t proxyInitCount = 0;
46060ff233Sopenharmony_ci    while (clientProxy == NULL) {
47060ff233Sopenharmony_ci        proxyInitCount++;
48060ff233Sopenharmony_ci        if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) {
49060ff233Sopenharmony_ci            COMM_LOGE(COMM_SDK, "frame get server proxy error");
50060ff233Sopenharmony_ci            return NULL;
51060ff233Sopenharmony_ci        }
52060ff233Sopenharmony_ci        IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SOFTBUS_SERVICE);
53060ff233Sopenharmony_ci        if (iUnknown == NULL) {
54060ff233Sopenharmony_ci            SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
55060ff233Sopenharmony_ci            continue;
56060ff233Sopenharmony_ci        }
57060ff233Sopenharmony_ci
58060ff233Sopenharmony_ci        int32_t ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&clientProxy);
59060ff233Sopenharmony_ci        if (ret != EC_SUCCESS || clientProxy == NULL) {
60060ff233Sopenharmony_ci            COMM_LOGE(COMM_SDK, "QueryInterface failed. ret=%{public}d", ret);
61060ff233Sopenharmony_ci            SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
62060ff233Sopenharmony_ci            continue;
63060ff233Sopenharmony_ci        }
64060ff233Sopenharmony_ci    }
65060ff233Sopenharmony_ci
66060ff233Sopenharmony_ci    COMM_LOGI(COMM_SDK, "frame get client proxy ok");
67060ff233Sopenharmony_ci    return clientProxy;
68060ff233Sopenharmony_ci}
69060ff233Sopenharmony_ci
70060ff233Sopenharmony_ciint32_t RegisterService(const char *name, const struct CommonScvId *svcId)
71060ff233Sopenharmony_ci{
72060ff233Sopenharmony_ci    COMM_LOGI(COMM_SDK, "server register service client push.");
73060ff233Sopenharmony_ci    if ((svcId == NULL) || (name == NULL)) {
74060ff233Sopenharmony_ci        COMM_LOGE(COMM_SDK, "Invalid param");
75060ff233Sopenharmony_ci        return SOFTBUS_INVALID_PARAM;
76060ff233Sopenharmony_ci    }
77060ff233Sopenharmony_ci    uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
78060ff233Sopenharmony_ci
79060ff233Sopenharmony_ci    IpcIo request = {0};
80060ff233Sopenharmony_ci    IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 1);
81060ff233Sopenharmony_ci    WriteString(&request, name);
82060ff233Sopenharmony_ci
83060ff233Sopenharmony_ci    SvcIdentity svc = {0};
84060ff233Sopenharmony_ci    svc.handle = svcId->handle;
85060ff233Sopenharmony_ci    svc.token = svcId->token;
86060ff233Sopenharmony_ci    svc.cookie = svcId->cookie;
87060ff233Sopenharmony_ci    bool value = WriteRemoteObject(&request, &svc);
88060ff233Sopenharmony_ci    if (!value) {
89060ff233Sopenharmony_ci        return SOFTBUS_TRANS_PROXY_WRITEOBJECT_FAILED;
90060ff233Sopenharmony_ci    }
91060ff233Sopenharmony_ci
92060ff233Sopenharmony_ci    int ret = SOFTBUS_IPC_ERR;
93060ff233Sopenharmony_ci    if (g_serverProxy->Invoke(g_serverProxy, MANAGE_REGISTER_SERVICE, &request, &ret,
94060ff233Sopenharmony_ci        ClientSimpleResultCb) != EC_SUCCESS) {
95060ff233Sopenharmony_ci        COMM_LOGI(COMM_SDK, "Call back ret=%{public}d", ret);
96060ff233Sopenharmony_ci        return SOFTBUS_IPC_ERR;
97060ff233Sopenharmony_ci    }
98060ff233Sopenharmony_ci    return ret;
99060ff233Sopenharmony_ci}
100060ff233Sopenharmony_ci
101060ff233Sopenharmony_civoid __attribute__((weak)) HOS_SystemInit(void)
102060ff233Sopenharmony_ci{
103060ff233Sopenharmony_ci    SAMGR_Bootstrap();
104060ff233Sopenharmony_ci    return;
105060ff233Sopenharmony_ci}
106060ff233Sopenharmony_ci
107060ff233Sopenharmony_ciint32_t ServerProxyInit(void)
108060ff233Sopenharmony_ci{
109060ff233Sopenharmony_ci    HOS_SystemInit();
110060ff233Sopenharmony_ci    g_serverProxy = GetServerProxy();
111060ff233Sopenharmony_ci    if (g_serverProxy == NULL) {
112060ff233Sopenharmony_ci        COMM_LOGE(COMM_SDK, "get ipc client proxy failed");
113060ff233Sopenharmony_ci        return SOFTBUS_IPC_ERR;
114060ff233Sopenharmony_ci    }
115060ff233Sopenharmony_ci
116060ff233Sopenharmony_ci    if (g_serverProxy == g_oldServerProxy) {
117060ff233Sopenharmony_ci        g_serverProxy = NULL;
118060ff233Sopenharmony_ci        COMM_LOGE(COMM_SDK, "get ipc client proxy is the same as old");
119060ff233Sopenharmony_ci        return SOFTBUS_IPC_ERR;
120060ff233Sopenharmony_ci    }
121060ff233Sopenharmony_ci
122060ff233Sopenharmony_ci    COMM_LOGI(COMM_SDK, "ServerProvideInterfaceInit ok");
123060ff233Sopenharmony_ci    return SOFTBUS_OK;
124060ff233Sopenharmony_ci}
125060ff233Sopenharmony_ci
126060ff233Sopenharmony_ciint32_t ServerProxyDeInit(void)
127060ff233Sopenharmony_ci{
128060ff233Sopenharmony_ci    g_oldServerProxy = g_serverProxy;
129060ff233Sopenharmony_ci    if (g_serverProxy != NULL) {
130060ff233Sopenharmony_ci        (void)g_serverProxy->Release((IUnknown *)(g_serverProxy));
131060ff233Sopenharmony_ci        g_serverProxy = NULL;
132060ff233Sopenharmony_ci    }
133060ff233Sopenharmony_ci
134060ff233Sopenharmony_ci    COMM_LOGI(COMM_SDK, "ServerProxyDeInit ok");
135060ff233Sopenharmony_ci    return SOFTBUS_OK;
136060ff233Sopenharmony_ci}