IVGCVSW-3580: Extend the IProfilingConnection to connect to a Socket

Signed-off-by: Teresa Charlin <teresa.charlinreyes@arm.com>
Signed-off-by: Aron Virginas-Tar <aron.virginas-tar@arm.com>

Change-Id: I72e099ee00052f7a0907c055c7bff02c7d8fde86
diff --git a/src/profiling/SocketProfilingConnection.cpp b/src/profiling/SocketProfilingConnection.cpp
new file mode 100644
index 0000000..21a7a1d
--- /dev/null
+++ b/src/profiling/SocketProfilingConnection.cpp
@@ -0,0 +1,77 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "SocketProfilingConnection.hpp"
+
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <cerrno>
+#include <string>
+
+namespace armnn
+{
+namespace profiling
+{
+
+SocketProfilingConnection::SocketProfilingConnection()
+{
+    memset(m_Socket, 0, sizeof(m_Socket));
+    // Note: we're using Linux specific SOCK_CLOEXEC flag.
+    m_Socket[0].fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+    if (m_Socket[0].fd == -1)
+    {
+        throw armnn::Exception(std::string(": Socket construction failed: ") + strerror(errno));
+    }
+
+    // Connect to the named unix domain socket.
+    struct sockaddr_un server{};
+    memset(&server, 0, sizeof(sockaddr_un));
+    // As m_GatorNamespace begins with a null character we need to ignore that when getting its length.
+    memcpy(server.sun_path, m_GatorNamespace, strlen(m_GatorNamespace + 1) + 1);
+    server.sun_family = AF_UNIX;
+    if (0 != connect(m_Socket[0].fd, reinterpret_cast<const sockaddr*>(&server), sizeof(sockaddr_un)))
+    {
+        close(m_Socket[0].fd);
+        throw armnn::Exception(std::string(": Cannot connect to stream socket: ") + strerror(errno));
+    }
+
+    // Our socket will only be interested in polling reads.
+    m_Socket[0].events = POLLIN;
+
+    // Make the socket non blocking.
+    const int currentFlags = fcntl(m_Socket[0].fd, F_GETFL);
+    if (0 != fcntl(m_Socket[0].fd, F_SETFL, currentFlags | O_NONBLOCK))
+    {
+        close(m_Socket[0].fd);
+        throw armnn::Exception(std::string(": Failed to set socket as non blocking: ") + strerror(errno));
+    }
+}
+
+bool SocketProfilingConnection::IsOpen()
+{
+    // Dummy return value, function not implemented
+    return true;
+}
+
+void SocketProfilingConnection::Close()
+{
+    // Function not implemented
+}
+
+bool SocketProfilingConnection::WritePacket(const char* buffer, uint32_t length)
+{
+    // Dummy return value, function not implemented
+    return true;
+}
+
+Packet SocketProfilingConnection::ReadPacket(uint32_t timeout)
+{
+    // Dummy return value, function not implemented
+    return {472580096, 0, nullptr};
+}
+
+} // namespace profiling
+} // namespace armnn
diff --git a/src/profiling/SocketProfilingConnection.hpp b/src/profiling/SocketProfilingConnection.hpp
new file mode 100644
index 0000000..f58e1f4
--- /dev/null
+++ b/src/profiling/SocketProfilingConnection.hpp
@@ -0,0 +1,33 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "IProfilingConnection.hpp"
+
+#include <poll.h>
+#include <Runtime.hpp>
+
+#pragma once
+
+namespace armnn
+{
+namespace profiling
+{
+
+class SocketProfilingConnection : public IProfilingConnection
+{
+public:
+    SocketProfilingConnection();
+    bool IsOpen() final;
+    void Close() final;
+    bool WritePacket(const char* buffer, uint32_t length) final;
+    Packet ReadPacket(uint32_t timeout) final;
+private:
+    // To indicate we want to use an abstract UDS ensure the first character of the address is 0.
+    const char* m_GatorNamespace = "\0gatord_namespace";
+    struct pollfd m_Socket[1]{};
+};
+
+} // namespace profiling
+} // namespace armnn
diff --git a/src/profiling/test/ProfilingTests.cpp b/src/profiling/test/ProfilingTests.cpp
index fe94092..25fae6c 100644
--- a/src/profiling/test/ProfilingTests.cpp
+++ b/src/profiling/test/ProfilingTests.cpp
@@ -10,9 +10,12 @@
 #include "../Holder.hpp"
 #include "../Packet.hpp"
 #include "../PacketVersionResolver.hpp"
+#include "../ProfilingService.hpp"
 #include "../ProfilingStateMachine.hpp"
 #include "../ProfilingUtils.hpp"
-#include "../ProfilingService.hpp"
+#include "../SocketProfilingConnection.hpp"
+
+#include <Runtime.hpp>
 
 #include <boost/test/unit_test.hpp>
 
@@ -528,4 +531,10 @@
     BOOST_TEST(uid1 != uid2);
 }
 
+BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
+{
+    // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
+    BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
+}
+
 BOOST_AUTO_TEST_SUITE_END()