• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_softbus_trans.h"
17 
18 #include <stddef.h>
19 #include <unistd.h>
20 
21 #include "dbinder_types.h"
22 #include "rpc_errno.h"
23 #include "rpc_log.h"
24 #include "securec.h"
25 #include "session.h"
26 #include "softbus_bus_center.h"
27 
28 static SessionAttribute g_sessionAttr = {.dataType = TYPE_BYTES};
29 
StartListen(const char * saSessionName,void * cb)30 static int32_t StartListen(const char *saSessionName, void *cb)
31 {
32     if (saSessionName == NULL) {
33         RPC_LOG_ERROR("StartListen saSessionName is null");
34         return ERR_FAILED;
35     }
36     if (cb == NULL) {
37         RPC_LOG_ERROR("StartListen callback is null");
38         return ERR_FAILED;
39     }
40 
41     int ret = CreateSessionServer(saSessionName, saSessionName, (ISessionListener *)cb);
42     if (ret != 0) {
43         RPC_LOG_ERROR("CreateSessionServer failed, error=%d", ret);
44         return ERR_FAILED;
45     }
46     return ERR_NONE;
47 }
48 
StopListen(const char * saSessionName)49 static int32_t StopListen(const char *saSessionName)
50 {
51     if (saSessionName == NULL) {
52         RPC_LOG_ERROR("StopListen saSessionName is null");
53         return ERR_FAILED;
54     }
55 
56     int ret = RemoveSessionServer(saSessionName, saSessionName);
57     if (ret != 0) {
58         RPC_LOG_ERROR("RemoveSessionServer failed, error=%d", ret);
59         return ERR_FAILED;
60     }
61     return ERR_NONE;
62 }
63 
Connect(const char * saSessionName,const char * peerDeviceId,void * args)64 static int32_t Connect(const char *saSessionName, const char *peerDeviceId, void *args)
65 {
66     if (saSessionName == NULL) {
67         RPC_LOG_ERROR("Connect SaSessionName is null");
68         return ERR_FAILED;
69     }
70     if (peerDeviceId == NULL) {
71         RPC_LOG_ERROR("Connect peerDeviceId is null");
72         return ERR_FAILED;
73     }
74 
75     int ret = OpenSession(saSessionName, saSessionName, peerDeviceId, "", &g_sessionAttr);
76     printf("SOFTBUS Connect deviceid %s\n", peerDeviceId);
77     if (ret < 0) {
78         RPC_LOG_ERROR("Connect OpenSession failed, error=%d", ret);
79         return ERR_FAILED;
80     }
81     return (int32_t)ret;
82 }
83 
Disconnect(int32_t sessionId)84 static int32_t Disconnect(int32_t sessionId)
85 {
86     if (sessionId < 0) {
87         RPC_LOG_ERROR("Disconnect invalid sessionId=%d", sessionId);
88         return ERR_FAILED;
89     }
90 
91     CloseSession(sessionId);
92     return ERR_NONE;
93 }
94 
Send(int32_t sessionId,const void * data,uint32_t len)95 static int32_t Send(int32_t sessionId, const void *data, uint32_t len)
96 {
97     if (sessionId < 0) {
98         RPC_LOG_ERROR("Send invalid sessionId=%d", sessionId);
99         return ERR_FAILED;
100     }
101     if (data == NULL) {
102         RPC_LOG_ERROR("Send data is null");
103         return ERR_FAILED;
104     }
105 
106     int ret = SendBytes(sessionId, data, len);
107     if (ret != 0) {
108         RPC_LOG_ERROR("Send failed, error=%d", ret);
109         return ERR_FAILED;
110     }
111     return ERR_NONE;
112 }
113 
GetLocalDeviceID(const char * saSessionName,char * deviceId)114 static int32_t GetLocalDeviceID(const char *saSessionName, char *deviceId)
115 {
116     if (saSessionName == NULL) {
117         RPC_LOG_ERROR("GetLocalDeviceID SaSessionName is null");
118         return NULL;
119     }
120     NodeBasicInfo nodeInfo;
121     int32_t ret = GetLocalNodeDeviceInfo(saSessionName, &nodeInfo);
122     if (ret != 0) {
123         RPC_LOG_ERROR("GetLocalDeviceID failed, error=%d", ret);
124         return NULL;
125     }
126     if (memcpy_s(deviceId, DEVICEID_LENGTH + 1, nodeInfo.networkId, DEVICEID_LENGTH + 1) != EOK) {
127         RPC_LOG_ERROR("GetLocalDeviceID memcpy failed");
128         return ERR_FAILED;
129     }
130     return ERR_NONE;
131 }
132 
133 static TransInterface g_softbusTrans = {
134     .StartListen = StartListen,
135     .StopListen = StopListen,
136     .Connect = Connect,
137     .Disconnect = Disconnect,
138     .Send = Send,
139     .GetLocalDeviceID = GetLocalDeviceID,
140 };
141 
GetSoftbusTrans(void)142 TransInterface *GetSoftbusTrans(void)
143 {
144     return &g_softbusTrans;
145 }