1 /*
2  * Copyright (C) 2022 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 "rpc_trans.h"
17 
18 #include <stdbool.h>
19 
20 #include "dbinder_service_inner.h"
21 #include "rpc_errno.h"
22 #include "rpc_log.h"
23 #include "rpc_session_handle.h"
24 
25 
OnConnected(int32_t sessionId,int32_t result)26 static int32_t OnConnected(int32_t sessionId, int32_t result)
27 {
28     if (sessionId <= 0 || result != 0) {
29         RPC_LOG_INFO("dbinder OnConnected failed, receive sessionId=%d, result=%d", sessionId, result);
30         return ERR_FAILED;
31     }
32     RPC_LOG_INFO("dbinder OnConnected callback, receive sessionId: %d", sessionId);
33     return HandleNewConnection(GetSessionIdList(), sessionId);
34 }
35 
OnDisconnected(int32_t sessionId)36 static int32_t OnDisconnected(int32_t sessionId)
37 {
38     RPC_LOG_INFO("dbinder OnDisconnected callback, receive sessionId: %d", sessionId);
39     return ERR_NONE;
40 }
41 
OnRecieved(int32_t sessionId,const void * data,uint32_t len)42 static int32_t OnRecieved(int32_t sessionId, const void *data, uint32_t len)
43 {
44     RPC_LOG_INFO("dbinder OnRecieved callback, receive sessionId: %d", sessionId);
45     if (data == NULL) {
46         RPC_LOG_ERROR("OnRecieved failed, data is null");
47         return ERR_FAILED;
48     }
49     if (len != sizeof(DHandleEntryTxRx)) {
50         RPC_LOG_ERROR("OnRecieved received data length %d, excepted length %d", len, sizeof(DHandleEntryTxRx));
51         return ERR_FAILED;
52     }
53 
54     DHandleEntryTxRx *handleEntry = (DHandleEntryTxRx *)data;
55     if (OnRemoteMessageTask(handleEntry) != ERR_NONE) {
56         RPC_LOG_ERROR("OnRemoteMessageTask failed");
57         return ERR_FAILED;
58     }
59 
60     return ERR_NONE;
61 }
62 
63 static TransCallback g_sessionListener = {
64     .OnConnected = OnConnected,
65     .OnDisconnected = OnDisconnected,
66     .OnRecieved = OnRecieved
67 };
68 
GetDBinderTransCallback(void)69 TransCallback *GetDBinderTransCallback(void)
70 {
71     RPC_LOG_INFO("GetTransCallback dbinder");
72     return &g_sessionListener;
73 }