1 /*
2 * Copyright (c) 2024 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 "client_trans_socket_option.h"
17
18 #include "client_trans_session_manager.h"
19 #include "softbus_error_code.h"
20 #include "trans_log.h"
21 #include "trans_type.h"
22
23 typedef struct {
24 OptType optType;
25 int32_t (*GetOpt)(int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t *optValueSize);
26 int32_t (*SetOpt)(int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t optValueSize);
27 } SocketOptMap;
28
TransGetSocketMaxBufferLen(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t * optValueSize)29 static int32_t TransGetSocketMaxBufferLen(
30 int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t *optValueSize)
31 {
32 (void)socket;
33 (void)level;
34 (void)optType;
35 (void)optValue;
36 (void)optValueSize;
37 return SOFTBUS_NOT_IMPLEMENT;
38 }
39
TransGetSocketFirstPackage(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t * optValueSize)40 static int32_t TransGetSocketFirstPackage(
41 int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t *optValueSize)
42 {
43 (void)socket;
44 (void)level;
45 (void)optType;
46 (void)optValue;
47 (void)optValueSize;
48 return SOFTBUS_NOT_IMPLEMENT;
49 }
50
TransGetSocketMaxIdleTime(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t * optValueSize)51 static int32_t TransGetSocketMaxIdleTime(
52 int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t *optValueSize)
53 {
54 if (socket < 0) {
55 TRANS_LOGE(TRANS_SDK, "invalid socket, socket=%{public}d", socket);
56 return SOFTBUS_INVALID_PARAM;
57 }
58 *optValueSize = sizeof(uint32_t);
59 return GetMaxIdleTimeBySocket(socket, (uint32_t *)optValue);
60 }
61
TransSetSocketMaxIdleTime(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t optValueSize)62 static int32_t TransSetSocketMaxIdleTime(
63 int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t optValueSize)
64 {
65 if (socket < 0 || optValueSize < (int32_t)sizeof(uint32_t)) {
66 TRANS_LOGE(TRANS_SDK, "invalid param, socket=%{public}d, optValueSize=%{public}d", socket, optValueSize);
67 return SOFTBUS_INVALID_PARAM;
68 }
69
70 uint32_t maxIdleTime = *(uint32_t *)optValue;
71 return SetMaxIdleTimeBySocket(socket, maxIdleTime);
72 }
73
TransGetSupportTlv(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t * optValueSize)74 static int32_t TransGetSupportTlv(
75 int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t *optValueSize)
76 {
77 if (socket <= 0) {
78 TRANS_LOGE(TRANS_SDK, "invalid socket, socket=%{public}d", socket);
79 return SOFTBUS_INVALID_PARAM;
80 }
81 return TransGetSupportTlvBySocket(socket, (bool *)optValue, optValueSize);
82 }
83
TransSetNeedAck(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t optValueSize)84 static int32_t TransSetNeedAck(
85 int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t optValueSize)
86 {
87 if (socket <= 0) {
88 TRANS_LOGE(TRANS_SDK, "invalid socket, socket=%{public}d, optValueSize=%{public}d", socket, optValueSize);
89 return SOFTBUS_INVALID_PARAM;
90 }
91 if (optValueSize != sizeof(bool)) {
92 TRANS_LOGE(TRANS_SDK, "optValueSize invalid param");
93 return SOFTBUS_INVALID_PARAM;
94 }
95 bool needAck = *(bool *)optValue;
96 return TransSetNeedAckBySocket(socket, needAck);
97 }
98
99 static SocketOptMap g_socketOptMap[] = {
100 { OPT_TYPE_MAX_BUFFER, TransGetSocketMaxBufferLen, NULL },
101 { OPT_TYPE_FIRST_PACKAGE, TransGetSocketFirstPackage, NULL },
102 { OPT_TYPE_MAX_IDLE_TIMEOUT, TransGetSocketMaxIdleTime, TransSetSocketMaxIdleTime },
103 { OPT_TYPE_SUPPORT_ACK, TransGetSupportTlv, NULL },
104 { OPT_TYPE_NEED_ACK, NULL, TransSetNeedAck },
105 };
106
GetCommonSocketOpt(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t * optValueSize)107 int32_t GetCommonSocketOpt(int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t *optValueSize)
108 {
109 if (optValue == NULL || optValueSize == NULL) {
110 TRANS_LOGE(TRANS_SDK, "invalid param.");
111 return SOFTBUS_INVALID_PARAM;
112 }
113
114 int32_t count = sizeof(g_socketOptMap) / sizeof(SocketOptMap);
115 for (int32_t i = 0; i < count; i++) {
116 if (optType == g_socketOptMap[i].optType) {
117 if (g_socketOptMap[i].GetOpt != NULL) {
118 int32_t ret = g_socketOptMap[i].GetOpt(socket, level, optType, optValue, optValueSize);
119 return ret;
120 }
121 }
122 }
123 return SOFTBUS_NOT_IMPLEMENT;
124 }
125
SetCommonSocketOpt(int32_t socket,OptLevel level,OptType optType,void * optValue,int32_t optValueSize)126 int32_t SetCommonSocketOpt(int32_t socket, OptLevel level, OptType optType, void *optValue, int32_t optValueSize)
127 {
128 if (optValue == NULL || optValueSize <= 0) {
129 TRANS_LOGE(TRANS_SDK, "invalid param.");
130 return SOFTBUS_INVALID_PARAM;
131 }
132
133 int32_t count = sizeof(g_socketOptMap) / sizeof(SocketOptMap);
134 for (int32_t i = 0; i < count; i++) {
135 if (optType == g_socketOptMap[i].optType) {
136 if (g_socketOptMap[i].SetOpt != NULL) {
137 int32_t ret = g_socketOptMap[i].SetOpt(socket, level, optType, optValue, optValueSize);
138 return ret;
139 }
140 }
141 }
142 return SOFTBUS_NOT_IMPLEMENT;
143 }