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 #ifndef LNN_LANE_LINK_H
17 #define LNN_LANE_LINK_H
18 
19 #include <stdint.h>
20 #include "lnn_lane_def.h"
21 #include "lnn_lane_link_conflict.h"
22 #include "bus_center_info_key.h"
23 #include "softbus_common.h"
24 #include "softbus_def.h"
25 #include "softbus_protocol_def.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define COC_DIRECT_LATENCY      1200
32 #define BR_LATENCY              2500
33 #define WLAN_LATENCY            800
34 #define P2P_LATENCY             1600
35 #define BLE_LATENCY             1500
36 #define HML_LATENCY             1500
37 #define BR_REUSE_LATENCY        1000
38 
39 typedef struct {
40     uint32_t activeHml;
41     int32_t passiveHml;
42     int32_t rawHml;
43     bool isDisableLowPower;
44 } PowerControlInfo;
45 
46 typedef struct {
47     char peerNetworkId[NETWORK_ID_BUF_LEN];
48     bool networkDelegate;
49     bool p2pOnly;
50     LaneLinkType linkType;
51     ProtocolType acceptableProtocols;
52     int32_t pid;
53     uint32_t bandWidth;
54     uint64_t triggerLinkTime;
55     uint64_t availableLinkTime;
56     //OldInfo
57     LaneTransType transType;
58     char peerBleMac[MAX_MAC_LEN];
59     int32_t psm;
60     bool isSupportIpv6;
61     uint32_t actionAddr;
62 } LinkRequest;
63 
64 typedef struct {
65     int32_t channel;
66     LaneBandwidth bw;
67     WlanConnInfo connInfo;
68 } WlanLinkInfo;
69 
70 typedef struct {
71     int32_t channel;
72     LaneBandwidth bw;
73     P2pConnInfo connInfo;
74 } P2pLinkInfo;
75 
76 typedef struct {
77     char brMac[BT_MAC_LEN];
78 } BrLinkInfo;
79 
80 // 'GATT' and 'CoC' protocols under BLE use the same definitions
81 typedef struct {
82     BleProtocolType protoType;
83     char bleMac[BT_MAC_LEN];
84     char deviceIdHash[UDID_HASH_LEN];
85     int32_t psm;
86 } BleLinkInfo;
87 
88 // 'GATT' and 'CoC' protocols under BLE use the same definitions
89 typedef struct {
90     BleProtocolType protoType;
91     char networkId[NETWORK_ID_BUF_LEN];
92 } BleDirectInfo;
93 
94 typedef struct {
95     char peerUdid[UDID_BUF_LEN];
96     char netifName[NET_IF_NAME_LEN];
97     LaneLinkType type;
98     union {
99         WlanLinkInfo wlan;
100         P2pLinkInfo p2p;
101         BrLinkInfo br;
102         BleLinkInfo ble;
103         BleDirectInfo bleDirect;
104         RawWifiDirectConnInfo rawWifiDirect;
105     } linkInfo;
106 } LaneLinkInfo;
107 
108 typedef struct {
109     ListNode node;
110     uint64_t laneId;
111     bool isServerSide;
112     LaneLinkInfo link;
113     uint32_t laneScore;
114     uint32_t laneFload;
115     uint32_t clientRef;
116 } LaneResource;
117 
118 typedef struct {
119     void (*onLaneLinkSuccess)(uint32_t reqId, LaneLinkType linkType, const LaneLinkInfo *linkInfo);
120     void (*onLaneLinkFail)(uint32_t reqId, int32_t reason, LaneLinkType linkType);
121 } LaneLinkCb;
122 
123 int32_t InitLaneLink(void);
124 void DeinitLaneLink(void);
125 int32_t BuildLink(const LinkRequest *reqInfo, uint32_t reqId, const LaneLinkCb *cb);
126 int32_t DestroyLink(const char *networkId, uint32_t laneReqId, LaneLinkType type);
127 
128 void LaneDeleteP2pAddress(const char *networkId, bool isDestroy);
129 void LaneAddP2pAddress(const char *networkId, const char *ipAddr, uint16_t port);
130 
131 void LaneAddP2pAddressByIp(const char *ipAddr, uint16_t port);
132 void LaneUpdateP2pAddressByIp(const char *ipAddr, const char *networkId);
133 void DetectDisableWifiDirectApply(void);
134 
135 int32_t FindLaneResourceByLinkAddr(const LaneLinkInfo *info, LaneResource *resource);
136 int32_t FindLaneResourceByLinkType(const char *peerUdid, LaneLinkType type, LaneResource *resource);
137 int32_t CheckLaneResourceNumByLinkType(const char *peerUdid, LaneLinkType type, int32_t *laneNum);
138 int32_t AddLaneResourceToPool(const LaneLinkInfo *linkInfo, uint64_t laneId, bool isServerSide);
139 int32_t DelLaneResourceByLaneId(uint64_t laneId, bool isServerSide);
140 int32_t FindLaneResourceByLaneId(uint64_t laneId, LaneResource *resource);
141 int32_t UpdateLaneResourceLaneId(uint64_t oldLaneId, uint64_t newLaneId, const char *peerUdid);
142 uint64_t GenerateLaneId(const char *localUdid, const char *remoteUdid, LaneLinkType linkType);
143 int32_t ClearLaneResourceByLaneId(uint64_t laneId);
144 int32_t GetAllDevIdWithLinkType(LaneLinkType type, char **devIdList, uint8_t *devIdCnt);
145 int32_t QueryOtherLaneResource(const DevIdentifyInfo *inputInfo, LaneLinkType type);
146 bool FindLaneResourceByDevInfo(const DevIdentifyInfo *inputInfo, LaneLinkType type);
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 #endif // LNN_LANE_LINK_H