1a34a8711Sopenharmony_ci/*
2a34a8711Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3a34a8711Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a34a8711Sopenharmony_ci * you may not use this file except in compliance with the License.
5a34a8711Sopenharmony_ci * You may obtain a copy of the License at
6a34a8711Sopenharmony_ci *
7a34a8711Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a34a8711Sopenharmony_ci *
9a34a8711Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a34a8711Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a34a8711Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a34a8711Sopenharmony_ci * See the License for the specific language governing permissions and
13a34a8711Sopenharmony_ci * limitations under the License.
14a34a8711Sopenharmony_ci */
15a34a8711Sopenharmony_ci
16a34a8711Sopenharmony_ci#include "rpc_trans.h"
17a34a8711Sopenharmony_ci
18a34a8711Sopenharmony_ci#include <stdbool.h>
19a34a8711Sopenharmony_ci
20a34a8711Sopenharmony_ci#include "dbinder_service_inner.h"
21a34a8711Sopenharmony_ci#include "rpc_errno.h"
22a34a8711Sopenharmony_ci#include "rpc_log.h"
23a34a8711Sopenharmony_ci#include "rpc_session_handle.h"
24a34a8711Sopenharmony_ci
25a34a8711Sopenharmony_ci
26a34a8711Sopenharmony_cistatic int32_t OnConnected(int32_t sessionId, int32_t result)
27a34a8711Sopenharmony_ci{
28a34a8711Sopenharmony_ci    if (sessionId <= 0 || result != 0) {
29a34a8711Sopenharmony_ci        RPC_LOG_INFO("dbinder OnConnected failed, receive sessionId=%d, result=%d", sessionId, result);
30a34a8711Sopenharmony_ci        return ERR_FAILED;
31a34a8711Sopenharmony_ci    }
32a34a8711Sopenharmony_ci    RPC_LOG_INFO("dbinder OnConnected callback, receive sessionId: %d", sessionId);
33a34a8711Sopenharmony_ci    return HandleNewConnection(GetSessionIdList(), sessionId);
34a34a8711Sopenharmony_ci}
35a34a8711Sopenharmony_ci
36a34a8711Sopenharmony_cistatic int32_t OnDisconnected(int32_t sessionId)
37a34a8711Sopenharmony_ci{
38a34a8711Sopenharmony_ci    RPC_LOG_INFO("dbinder OnDisconnected callback, receive sessionId: %d", sessionId);
39a34a8711Sopenharmony_ci    return ERR_NONE;
40a34a8711Sopenharmony_ci}
41a34a8711Sopenharmony_ci
42a34a8711Sopenharmony_cistatic int32_t OnRecieved(int32_t sessionId, const void *data, uint32_t len)
43a34a8711Sopenharmony_ci{
44a34a8711Sopenharmony_ci    RPC_LOG_INFO("dbinder OnRecieved callback, receive sessionId: %d", sessionId);
45a34a8711Sopenharmony_ci    if (data == NULL) {
46a34a8711Sopenharmony_ci        RPC_LOG_ERROR("OnRecieved failed, data is null");
47a34a8711Sopenharmony_ci        return ERR_FAILED;
48a34a8711Sopenharmony_ci    }
49a34a8711Sopenharmony_ci    if (len != sizeof(DHandleEntryTxRx)) {
50a34a8711Sopenharmony_ci        RPC_LOG_ERROR("OnRecieved received data length %d, excepted length %d", len, sizeof(DHandleEntryTxRx));
51a34a8711Sopenharmony_ci        return ERR_FAILED;
52a34a8711Sopenharmony_ci    }
53a34a8711Sopenharmony_ci
54a34a8711Sopenharmony_ci    DHandleEntryTxRx *handleEntry = (DHandleEntryTxRx *)data;
55a34a8711Sopenharmony_ci    if (OnRemoteMessageTask(handleEntry) != ERR_NONE) {
56a34a8711Sopenharmony_ci        RPC_LOG_ERROR("OnRemoteMessageTask failed");
57a34a8711Sopenharmony_ci        return ERR_FAILED;
58a34a8711Sopenharmony_ci    }
59a34a8711Sopenharmony_ci
60a34a8711Sopenharmony_ci    return ERR_NONE;
61a34a8711Sopenharmony_ci}
62a34a8711Sopenharmony_ci
63a34a8711Sopenharmony_cistatic TransCallback g_sessionListener = {
64a34a8711Sopenharmony_ci    .OnConnected = OnConnected,
65a34a8711Sopenharmony_ci    .OnDisconnected = OnDisconnected,
66a34a8711Sopenharmony_ci    .OnRecieved = OnRecieved
67a34a8711Sopenharmony_ci};
68a34a8711Sopenharmony_ci
69a34a8711Sopenharmony_ciTransCallback *GetDBinderTransCallback(void)
70a34a8711Sopenharmony_ci{
71a34a8711Sopenharmony_ci    RPC_LOG_INFO("GetTransCallback dbinder");
72a34a8711Sopenharmony_ci    return &g_sessionListener;
73a34a8711Sopenharmony_ci}