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 AUTH_CONNECTION_H
17 #define AUTH_CONNECTION_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 
22 #include "auth_common.h"
23 #include "auth_interface.h"
24 #include "softbus_conn_interface.h"
25 
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif
30 #endif
31 
32 typedef struct {
33     uint64_t connId;
34     AuthConnInfo connInfo;
35     bool fromServer;
36     AuthDataHead head;
37     uint32_t len;
38     uint8_t data[0];
39 } RepeatDeviceIdData;
40 
41 typedef struct {
42     void (*onConnectResult)(uint32_t requestId, uint64_t connId, int32_t result, const AuthConnInfo *connInfo);
43     void (*onDisconnected)(uint64_t connId, const AuthConnInfo *connInfo);
44     void (*onDataReceived)(
45         uint64_t connId, const AuthConnInfo *connInfo, bool fromServer, const AuthDataHead *head, const uint8_t *data);
46 } AuthConnListener;
47 
48 typedef struct {
49     int32_t fd;
50     int32_t ret;
51 } AuthConnectResult;
52 
53 int32_t AuthConnInit(const AuthConnListener *listener);
54 void AuthConnDeinit(void);
55 
56 int32_t ConnectAuthDevice(uint32_t requestId, const AuthConnInfo *connInfo, ConnSideType sideType);
57 void UpdateAuthDevicePriority(uint64_t connId);
58 void DisconnectAuthDevice(uint64_t *connId);
59 int32_t PostAuthData(uint64_t connId, bool toServer, const AuthDataHead *head, const uint8_t *data);
60 
61 ConnSideType GetConnSideType(uint64_t connId);
62 bool CheckActiveAuthConnection(const AuthConnInfo *connInfo);
63 
64 const char *GetConnTypeStr(uint64_t connId);
65 uint32_t GetConnId(uint64_t connId);
66 int32_t GetConnType(uint64_t connId);
67 int32_t GetFd(uint64_t connId);
68 uint64_t GenConnId(int32_t connType, int32_t id);
69 void UpdateFd(uint64_t *connId, int32_t id);
70 
71 uint32_t GetAuthDataSize(uint32_t len);
72 int32_t PackAuthData(const AuthDataHead *head, const uint8_t *data, uint8_t *buf, uint32_t size);
73 const uint8_t *UnpackAuthData(const uint8_t *data, uint32_t len, AuthDataHead *head);
74 int32_t GetConnInfoByConnectionId(uint32_t connectionId, AuthConnInfo *connInfo);
75 
76 void HandleRepeatDeviceIdDataDelay(uint64_t connId, const AuthConnInfo *connInfo, bool fromServer,
77     const AuthDataHead *head, const uint8_t *data);
78 
79 #define CONN_INFO         "conn=%{public}s:%{public}u"
80 #define CONN_DATA(connId) GetConnTypeStr(connId), GetConnId(connId)
81 
82 #ifdef __cplusplus
83 #if __cplusplus
84 }
85 #endif
86 #endif
87 #endif /* AUTH_CONNECTION_H */
88