1 /*
2  * Copyright (c) 2022-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 #ifndef CLIENT_TRANS_PROXY_FILE_MANAGER_H
17 #define CLIENT_TRANS_PROXY_FILE_MANAGER_H
18 
19 #include <stdint.h>
20 
21 #include "client_trans_file_listener.h"
22 #include "client_trans_proxy_file_common.h"
23 
24 #ifdef __linux__
25 #define MAX_SEND_FILE_NUM 10
26 #else
27 #define MAX_SEND_FILE_NUM 1
28 #endif
29 #define MAX_FILE_SIZE (0x500000) /* 5M */
30 
31 #define DEFAULT_NEW_PATH_AUTHORITY (0750)
32 
33 #define INVALID_NODE_INDEX (-1)
34 
35 #define FILE_MAGIC_NUMBER 0xBABEFACE
36 
37 #define PROXY_BR_MAX_PACKET_SIZE (4096 - 48)
38 #define PROXY_BLE_MAX_PACKET_SIZE (1024 - 48)
39 
40 #define FRAME_MAGIC_OFFSET 4
41 #define FRAME_LEN_OFFSET 8
42 #define FRAME_HEAD_LEN (FRAME_MAGIC_OFFSET + FRAME_LEN_OFFSET)
43 #define FRAME_DATA_SEQ_OFFSET 4
44 #define FRAME_CRC_LEN 2
45 #define FRAME_CRC_CHECK_NUM_LEN 8
46 
47 #define IS_SEND_RESULT 1
48 #define IS_RECV_RESULT 0
49 
50 #define FILE_SEND_ACK_RESULT_SUCCESS 0xFFFFFFFF
51 #define FILE_SEND_ACK_INTERVAL 32
52 #define WAIT_START_ACK_TIME 20000
53 #define WAIT_ACK_TIME 200
54 #define WAIT_ACK_LAST_TIME 5000
55 #define WAIT_FRAME_ACK_TIMEOUT_COUNT 24
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 typedef enum {
62     TRANS_FILE_RECV_IDLE_STATE = 0,
63     TRANS_FILE_RECV_START_STATE,
64     TRANS_FILE_RECV_PROCESS_STATE,
65     TRANS_FILE_RECV_ERR_STATE,
66 } FileRecvState;
67 
68 typedef enum {
69     NODE_IDLE,
70     NODE_BUSY,
71     NODE_ERR,
72 } RecvFileNodeStatus;
73 
74 typedef struct {
75     uint32_t magic;
76     int32_t frameType;
77     uint32_t frameLength;
78     uint32_t seq;
79     uint16_t crc;
80     uint8_t *data;
81     uint8_t *fileData;
82 } FileFrame;
83 
84 typedef struct {
85     SoftBusMutex lock;
86     _Atomic uint32_t lockInitFlag;
87 } TransFileInfoLock;
88 
89 typedef struct {
90     ListNode node;
91     int32_t channelId;
92     uint32_t count;
93     SoftBusMutex sendLock;
94 } ProxyFileMutexLock;
95 
96 typedef struct {
97     uint32_t startSeq;
98     uint32_t seqResult;
99 } AckResponseData;
100 
101 typedef struct {
102     const char **files;
103     uint32_t fileCnt;
104     uint64_t bytesProcessed;
105     uint64_t bytesTotal;
106 } FilesInfo;
107 
108 typedef struct {
109     uint32_t seq;
110     int32_t fileFd;
111     int32_t fileStatus; /* 0: idle 1:busy */
112     uint64_t fileOffset;
113     uint64_t oneFrameLen;
114     uint32_t startSeq;
115     uint64_t seqResult;
116     uint32_t preStartSeq;
117     uint32_t preSeqResult;
118     uint64_t fileSize;
119     int32_t timeOut;
120     uint64_t checkSumCRC;
121     char filePath[MAX_FILE_PATH_NAME_LEN];
122 } SingleFileInfo;
123 
124 typedef struct {
125     ListNode node;
126     int32_t sessionId;
127     int32_t channelId;
128     int32_t osType;
129     int32_t fileEncrypt;
130     int32_t algorithm;
131     int32_t crc;
132     int32_t result;
133     FileListener fileListener;
134     int32_t objRefCount;
135     int32_t recvState;
136     SingleFileInfo recvFileInfo;
137 } FileRecipientInfo;
138 
139 typedef struct {
140     ListNode node;
141     int32_t channelId;
142     int32_t sessionId;
143     int32_t fd;
144     uint64_t fileSize;
145     uint64_t frameNum;
146     int32_t fileEncrypt;
147     int32_t algorithm;
148     int32_t crc;
149     uint32_t seq;
150     int32_t waitSeq;
151     int32_t waitTimeoutCount;
152     int32_t result;
153     uint64_t checkSumCRC;
154     FileListener fileListener;
155     FilesInfo totalInfo;
156     uint32_t packetSize;
157     int32_t osType;
158 } SendListenerInfo;
159 
160 int32_t ClinetTransProxyFileManagerInit(void);
161 void ClinetTransProxyFileManagerDeinit(void);
162 void ClientDeleteRecvFileList(int32_t sessionId);
163 
164 int32_t ProxyChannelSendFile(int32_t channelId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
165 int32_t ProcessRecvFileFrameData(int32_t sessionId, int32_t channelId, const FileFrame *oneFrame);
166 #ifdef __cplusplus
167 }
168 #endif
169 #endif // CLIENT_TRANS_PROXY_FILE_MANAGER_H
170