blob: 1b6dec54ff716ef0e0d90df477cdfd0b908a30e9 [file] [log] [blame]
Finn Williams2ed809c2020-04-20 21:21:07 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Finn Williams2ed809c2020-04-20 21:21:07 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Jim Flynnbbfe6032020-07-20 16:57:44 +01008#include <common/include/NetworkSockets.hpp>
9#include <common/include/Packet.hpp>
10#include <common/include/SocketConnectionException.hpp>
Finn Williams2ed809c2020-04-20 21:21:07 +010011
Finn Williams9937f932020-04-29 12:00:24 +010012#include <string>
13#include <atomic>
14
Jim Flynnbbfe6032020-07-20 16:57:44 +010015namespace arm
16{
17
18namespace pipe
Finn Williams2ed809c2020-04-20 21:21:07 +010019{
20
21enum class TargetEndianness
22{
23 BeWire,
24 LeWire
25};
26
27enum class PacketDirection
28{
29 Sending,
30 ReceivedHeader,
31 ReceivedData
32};
33class ConnectionHandler;
34
35class BasePipeServer
36{
37
38public:
39
Jim Flynnbbfe6032020-07-20 16:57:44 +010040 BasePipeServer(arm::pipe::Socket clientConnection, bool echoPackets)
Finn Williams2ed809c2020-04-20 21:21:07 +010041 : m_ClientConnection(clientConnection)
42 , m_EchoPackets(echoPackets)
43 {}
44
45 ~BasePipeServer()
46 {
47 // We have set SOCK_CLOEXEC on this socket but we'll close it to be good citizens.
Jim Flynnbbfe6032020-07-20 16:57:44 +010048 arm::pipe::Close(m_ClientConnection);
Finn Williams2ed809c2020-04-20 21:21:07 +010049 }
50
51 BasePipeServer(const BasePipeServer&) = delete;
52 BasePipeServer& operator=(const BasePipeServer&) = delete;
53
54 BasePipeServer(BasePipeServer&&) = delete;
55 BasePipeServer& operator=(BasePipeServer&&) = delete;
56
57 /// Close the client connection
58 /// @return 0 if successful
59 int Close()
60 {
Jim Flynnbbfe6032020-07-20 16:57:44 +010061 return arm::pipe::Close(m_ClientConnection);
Finn Williams2ed809c2020-04-20 21:21:07 +010062 }
63
64 /// Send a packet to the client
65 /// @return true if a valid packet has been sent.
66 bool SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength);
67
68 /// Set the client socket to nonblocking
69 /// @return true if successful.
70 bool SetNonBlocking()
71 {
Jim Flynnbbfe6032020-07-20 16:57:44 +010072 return arm::pipe::SetNonBlocking(m_ClientConnection);
Finn Williams2ed809c2020-04-20 21:21:07 +010073 }
74
75 /// Block on the client connection until a complete packet has been received.
76 /// @return true if a valid packet has been received.
Jim Flynnbbfe6032020-07-20 16:57:44 +010077 arm::pipe::Packet WaitForPacket(uint32_t timeoutMs);
Finn Williams2ed809c2020-04-20 21:21:07 +010078
79 /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this
80 /// packet differs from others as we need to determine endianness.
81 /// @return true only if a valid stream meta data packet has been received.
82 bool WaitForStreamMetaData();
83
84 uint32_t GetStreamMetadataVersion()
85 {
86 return m_StreamMetaDataVersion;
87 }
88
89 uint32_t GetStreamMetadataMaxDataLen()
90 {
91 return m_StreamMetaDataMaxDataLen;
92 }
93
94 uint32_t GetStreamMetadataPid()
95 {
96 return m_StreamMetaDataPid;
97 }
98
99private:
100
101 void EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes);
102 bool ReadFromSocket(uint8_t* packetData, uint32_t expectedLength);
103 bool ReadHeader(uint32_t headerAsWords[2]);
104
Jim Flynnbbfe6032020-07-20 16:57:44 +0100105 arm::pipe::Packet ReceivePacket();
Finn Williams2ed809c2020-04-20 21:21:07 +0100106
107 uint32_t ToUint32(uint8_t* data, TargetEndianness endianness);
108 void InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness);
109
Jim Flynnbbfe6032020-07-20 16:57:44 +0100110 arm::pipe::Socket m_ClientConnection;
Finn Williams2ed809c2020-04-20 21:21:07 +0100111 bool m_EchoPackets;
112 TargetEndianness m_Endianness;
Finn Williams2ed809c2020-04-20 21:21:07 +0100113
114 uint32_t m_StreamMetaDataVersion;
115 uint32_t m_StreamMetaDataMaxDataLen;
116 uint32_t m_StreamMetaDataPid;
117};
118
Jim Flynnbbfe6032020-07-20 16:57:44 +0100119} // namespace pipe
120} // namespace arm