feature(trans):add DfsBind

Signed-off-by: wuchengwen <wuchengwen4@huawei.com>
This commit is contained in:
wuchengwen 2024-04-26 15:19:57 +08:00 committed by 吴成文
parent b4920e99dd
commit 31f3832b74
9 changed files with 188 additions and 6 deletions

View File

@ -78,6 +78,18 @@
/** Callback for publish result */
void (*OnPublishResult)(int publishId, PublishResult reason);
} IPublishCb;
// 发布信息
typedef struct {
int publishId; // 发现消息Id
DiscoverMode mode; // 发现模式
ExchangeMedium medium; // 发现媒介
ExchangeFreq freq; // 发现频率
const char *capability; // 被发现设备需要具备的能力
unsigned char *capabilityData; // 业务发布的自定义数据
unsigned int dataLen; // 数据长度
bool ranging; // 是否测距
} PublishInfo;
// 发布服务
int32_t PublishLNN(const char *pkgName, const PublishInfo *info, const IPublishCb *cb);
@ -231,17 +243,18 @@
void (*OnStream)(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
void (*OnFile)(int32_t socket, FileEvent *event);
void (*OnQos)(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount);
void (*OnError)(int32_t socket, int32_t errCode);
} ISocketListener;
typedef enum {
QOS_TYPE_MIN_BW, // 最小带宽
QOS_TYPE_MAX_LATENCY, // 最大建链时延
QOS_TYPE_MAX_WAIT_TIMEOUT, // Bind超时时间
QOS_TYPE_MIN_LATENCY, // 最小建链时延
QOS_TYPE_MAX_WAIT_TIMEOUT, // 最大超时时间
QOS_TYPE_MAX_BUFFER, // 最大缓存
QOS_TYPE_FIRST_PACKAGE, // 首包大小
QOS_TYPE_RTT_LEVEL, // 往返时间级别
QOS_TYPE_MAX_BUFFER, // 最大缓存(预留)
QOS_TYPE_FIRST_PACKAGE, // 首包大小(预留)
QOS_TYPE_MAX_IDLE_TIMEOUT, // 最大空闲时间
QOS_TYPE_TRANS_RELIABILITY, // 传输可靠性
QOS_TYPE_TRANS_RELIABILITY, // 传输可靠性(预留)
QOS_TYPE_BUTT,
} QosType;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -36,6 +36,7 @@
#ifndef INNER_SOCKET_H
#define INNER_SOCKET_H
#include "socket.h"
#include "softbus_common.h"
#ifdef __cplusplus
@ -82,6 +83,20 @@ int32_t DBinderGrantPermission(int32_t uid, int32_t pid, const char *socketName)
* @version 1.0
*/
int32_t DBinderRemovePermission(const char *socketName);
/**
* @brief Bind for dfs.
*
* @param socket Indicates the the unique socket fd.
* @param listener Indicates the pointer to the socket callback.
* @return Returns <b>SOFTBUS_TRANS_INVALID_PARAM</b> if invalid parameters are detected.
* @return Returns <b>INVALID_SOCKET</b> if the operation fails.
* @return Returns <b>SOFTBUS_OK</b> if the socket is bind;
* returns an error code otherwise.
* @since 1.0
* @version 1.0
*/
int32_t DfsBind(int32_t socket, const ISocketListener *listener);
#ifdef __cplusplus
}
#endif

View File

@ -404,6 +404,7 @@
"GetMtuSize";
"DBinderGrantPermission";
"DBinderRemovePermission";
"DfsBind";
"ClientAddPolicyReportCallback";
"ClientDeletePolicyReportCallback";
"ServerEnableDiscoveryPolicy";

View File

@ -29,6 +29,7 @@ int32_t ClientListen(int32_t socket, const QosTV qos[], uint32_t qosCount, const
int32_t ClientBind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener, bool isAsync);
void ClientShutdown(int32_t socket);
int32_t GetSocketMtuSize(int32_t socket, uint32_t *mtuSize);
int32_t ClientDfsBind(int32_t socket, const ISocketListener *listener);
#ifdef __cplusplus
}
#endif

View File

@ -209,6 +209,7 @@ int32_t ClientTransSetChannelInfo(const char *sessionName, int32_t sessionId, in
int32_t GetSessionStateAndSessionNameBySessionId(int32_t sessionId, char *sessionName, SessionState *sessionState);
int32_t SetSessionStateBySessionId(int32_t sessionId, SessionState sessionState);
int32_t ClientDfsIpcOpenSession(int32_t sessionId, TransInfo *transInfo);
#ifdef __cplusplus
}

View File

@ -2534,4 +2534,74 @@ int32_t SetSessionStateBySessionId(int32_t sessionId, SessionState sessionState)
sessionNode->sessionState = sessionState;
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
return SOFTBUS_OK;
}
static void FillDsfSocketParam(
SessionParam *param, SessionAttribute *tmpAttr, ClientSessionServer *serverNode, SessionInfo *sessionNode)
{
tmpAttr->fastTransData = NULL;
tmpAttr->fastTransDataSize = 0;
tmpAttr->dataType = sessionNode->info.flag;
tmpAttr->attr.streamAttr.streamType = sessionNode->info.streamType;
tmpAttr->linkTypeNum = 2;
tmpAttr->linkType[0] = LINK_TYPE_WIFI_WLAN_5G;
tmpAttr->linkType[1] = LINK_TYPE_WIFI_WLAN_2G;
param->sessionName = serverNode->sessionName;
param->peerSessionName = sessionNode->info.peerSessionName;
param->peerDeviceId = sessionNode->info.peerDeviceId;
param->groupId = "reserved";
param->attr = tmpAttr;
param->isQosLane = false;
param->qosCount = 0;
(void)memset_s(param->qos, sizeof(param->qos), 0, sizeof(param->qos));
param->isAsync = false;
}
int32_t ClientDfsIpcOpenSession(int32_t sessionId, TransInfo *transInfo)
{
if (sessionId < 0 || transInfo == NULL) {
TRANS_LOGE(TRANS_SDK, "Invalid param");
return SOFTBUS_INVALID_PARAM;
}
if (g_clientSessionServerList == NULL) {
TRANS_LOGE(TRANS_SDK, "not init");
return SOFTBUS_TRANS_SESSION_SERVER_NOINIT;
}
if (SoftBusMutexLock(&(g_clientSessionServerList->lock)) != 0) {
TRANS_LOGE(TRANS_SDK, "lock failed");
return SOFTBUS_LOCK_ERR;
}
ClientSessionServer *serverNode = NULL;
SessionInfo *sessionNode = NULL;
if (GetSessionById(sessionId, &serverNode, &sessionNode) != SOFTBUS_OK) {
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
TRANS_LOGE(TRANS_SDK, "session not found. sessionId=%{public}d", sessionId);
return SOFTBUS_TRANS_SESSION_INFO_NOT_FOUND;
}
int32_t ret = CheckBindSocketInfo(sessionNode);
if (ret != SOFTBUS_OK) {
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
TRANS_LOGE(TRANS_SDK, "check socekt info failed, ret=%{public}d", ret);
return ret;
}
SessionAttribute tmpAttr;
(void)memset_s(&tmpAttr, sizeof(SessionAttribute), 0, sizeof(SessionAttribute));
SessionParam param;
FillDsfSocketParam(&param, &tmpAttr, serverNode, sessionNode);
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
param.sessionId = sessionId;
ret = SetSessionStateBySessionId(param.sessionId, SESSION_STATE_OPENING);
TRANS_CHECK_AND_RETURN_RET_LOGE(
ret == SOFTBUS_OK, ret, TRANS_SDK, "set session state failed, maybe cancel, ret=%{public}d", ret);
ret = ServerIpcOpenSession(&param, transInfo);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_SDK, "open session ipc err: ret=%{public}d", ret);
return ret;
}
return SOFTBUS_OK;
}

View File

@ -1216,4 +1216,43 @@ int32_t GetSocketMtuSize(int32_t socket, uint32_t *mtuSize)
*mtuSize = dataConfig;
TRANS_LOGI(TRANS_SDK, "get mtuSize success, socket=%{public}d, mtu=%{public}" PRIu32, socket, *mtuSize);
return SOFTBUS_OK;
}
int32_t ClientDfsBind(int32_t socket, const ISocketListener *listener)
{
if (!IsValidSessionId(socket) || !IsValidSocketListener(listener, false)) {
TRANS_LOGE(TRANS_SDK, "invalid param");
return SOFTBUS_INVALID_PARAM;
}
int32_t ret = ClientSetListenerBySessionId(socket, listener, false);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_SDK, "set listener by socket=%{public}d failed, ret=%{public}d", socket, ret);
return ret;
}
ret = SetSessionIsAsyncById(socket, false);
TRANS_CHECK_AND_RETURN_RET_LOGE(
ret == SOFTBUS_OK, ret, TRANS_SDK, "set session is async failed, ret=%{public}d", ret);
TransInfo transInfo;
ret = ClientDfsIpcOpenSession(socket, &transInfo);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_SDK, "open session failed, ret=%{public}d", ret);
return ret;
}
ret = ClientSetChannelBySessionId(socket, &transInfo);
TRANS_CHECK_AND_RETURN_RET_LOGE(
ret == SOFTBUS_OK, ret, TRANS_SDK, "set channel by socket=%{public}d failed, ret=%{public}d", socket, ret);
ret = SetSessionStateBySessionId(socket, SESSION_STATE_OPENED);
TRANS_CHECK_AND_RETURN_RET_LOGE(
ret == SOFTBUS_OK, ret, TRANS_SDK, "set session state failed socket=%{public}d, ret=%{public}d", socket, ret);
ret = CheckSessionIsOpened(socket);
TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_SDK, "CheckSessionIsOpened err, ret=%{public}d", ret);
ret = ClientSetSocketState(socket, 0, SESSION_ROLE_CLIENT);
TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_SDK, "set session role failed, ret=%{public}d", ret);
TRANS_LOGI(TRANS_SDK, "DfsBind ok: socket=%{public}d, channelId=%{public}d, channelType=%{public}d", socket,
transInfo.channelId, transInfo.channelType);
return SOFTBUS_OK;
}

View File

@ -153,4 +153,9 @@ int32_t DBinderGrantPermission(int32_t uid, int32_t pid, const char *socketName)
int32_t DBinderRemovePermission(const char *socketName)
{
return ClientRemovePermission(socketName);
}
int32_t DfsBind(int32_t socket, const ISocketListener *listener)
{
return ClientDfsBind(socket, listener);
}

View File

@ -18,6 +18,7 @@
#include <securec.h>
#include "socket.h"
#include "inner_socket.h"
#include "softbus_error_code.h"
#define SOCKET_NAME_MAX_LEN 255
@ -192,4 +193,40 @@ HWTEST_F(TransClientSocketServiceTest, DataType001, TestSize.Level1)
EXPECT_EQ(socketId, SOFTBUS_ERR);
}
}
/**
* @tc.name: DfsBind001
* @tc.desc: call DfsBind function with invalid socket or listener.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TransClientSocketServiceTest, DfsBind001, TestSize.Level1)
{
ISocketListener listener;
int32_t ret = DfsBind(-1, &listener);
EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = DfsBind(1, nullptr);
EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = DfsBind(1, &listener);
EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
}
static void OnShutdown(int32_t socket, ShutdownReason reason)
{
return;
}
/**
* @tc.name: DfsBind002
* @tc.desc: call DfsBind function with offline socket.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TransClientSocketServiceTest, DfsBind002, TestSize.Level1)
{
ISocketListener listener = { .OnShutdown = OnShutdown };
int32_t ret = DfsBind(1, &listener);
EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_INFO_NOT_FOUND);
}
} // namespace OHOS