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 PAN_NETWORK_H
17 #define PAN_NETWORK_H
18 
19 #include <fcntl.h>
20 #include <poll.h>
21 #include <pthread.h>
22 #include <unistd.h>
23 #include <condition_variable>
24 #include <mutex>
25 #include <string>
26 #include "base_def.h"
27 #include "pan_defines.h"
28 #include "raw_address.h"
29 
30 namespace OHOS {
31 namespace bluetooth {
32 static constexpr int POLL_TIMEOUT = 50;
33 
34 /**
35  * @brief Class for l2cap connection.
36  */
37 class PanNetwork {
38 public:
39     PanNetwork();
40     ~PanNetwork();
41 
42     int Close();
43     int Open();
44     int WriteData(EthernetHeader head, uint8_t *data, int len);
45     int ReceiveRemoteBusy(bool isBusy);
46 
47 private:
48     int TunSetIff();
49     int SetMacAddress(int inetSocket);
50     int SetIpAddress(int inetSocket);
51     int SetIffUp(int inetSocket);
52     int SetIffdown(int inetSocket);
53 
54     static void* PollEventThread(void* arg);
55     void PollEventThread_();
56     pthread_t CreateThread(void* (*startRoutine)(void*), void* arg);
57     void SetPanNetworkNonBlocking(int fd);
58     int ReadPanNetworkEvent();
59 
60     pthread_t pollThreadId_;
61     int fd_ = -1;
62     bool keepPolling_ = false;
63     bool isRemoteDeviceBusy_ = false;
64     std::mutex mutexBusyChanged_;
65     std::condition_variable cvWaitBusyChanged_;
66 
67     BT_DISALLOW_COPY_AND_ASSIGN(PanNetwork);
68 };
69 }  // namespace bluetooth
70 }  // namespace OHOS
71 #endif  // PAN_NETWORK_H