1 /*
2  * Copyright 2019, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "frame.h"
20 #include "pollable.h"
21 #include "result.h"
22 
23 #include <bitset>
24 #include <functional>
25 #include <memory>
26 #include <vector>
27 
28 struct MacAddress;
29 
30 class RemoteConnection {
31 public:
32     using OnFrameCallback = std::function<void (std::unique_ptr<Frame>)>;
33     using OnAckCallback = std::function<void (FrameInfo&)>;
34     using OnErrorCallback = OnAckCallback;
35 
36     RemoteConnection(OnFrameCallback onFrameCallback,
37                      OnAckCallback onAckCallback,
38                      OnErrorCallback onErrorCallback);
39     ~RemoteConnection();
40 
41     Result init();
42 
getFd()43     int getFd() const { return mPipeFd; }
44     Pollable::Timestamp getTimeout() const;
45     void receive();
46     bool sendFrame(std::unique_ptr<Frame> frame);
47     bool ackFrame(FrameInfo& info, bool success);
48 
49 private:
50     RemoteConnection(const RemoteConnection&) = delete;
51     RemoteConnection& operator=(const RemoteConnection&) = delete;
52 
53     OnFrameCallback mOnFrameCallback;
54     OnAckCallback mOnAckCallback;
55     OnErrorCallback mOnErrorCallback;
56 
57     Pollable::Timestamp mDeadline = Pollable::Timestamp::max();
58     std::vector<unsigned char> mBuffer;
59     int mPipeFd = -1;
60     std::bitset<256> mReportedVersionMismatches;
61 };
62 
63