IVGCVSW-4073 Send stream info in the ConnectionAcknowledgedCommandHandler

 * Added call to ISendTimelinePacket::SendStreamMetaDataPacket
 * Added call to ISendTimelinePacket::SendTimelineMessageDirectoryPackage
 * Added new StreamMetadataCommandHandler class to the mock Gatord service
 * Updated code and unit tests
 * Added include paths to the gatord mock target

Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com>
Change-Id: Ic6d200b513175884607b7c0563cbfa4942ff2fc6
diff --git a/Android.mk b/Android.mk
index 6daefef..177f6fc 100644
--- a/Android.mk
+++ b/Android.mk
@@ -197,6 +197,7 @@
         src/profiling/ProfilingUtils.cpp \
         src/profiling/RequestCounterDirectoryCommandHandler.cpp \
         src/profiling/SendCounterPacket.cpp \
+        src/profiling/SendTimelinePacket.cpp \
         src/profiling/SocketProfilingConnection.cpp \
         src/profiling/TimelineUtilityMethods.cpp
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fe3240d..f088a21 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -936,6 +936,8 @@
         tests/profiling/gatordmock/PeriodicCounterCaptureCommandHandler.hpp
         tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.cpp
         tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.hpp
+        tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp
+        tests/profiling/gatordmock/StreamMetadataCommandHandler.hpp
         tests/profiling/timelineDecoder/ITimelineDecoder.h
         tests/profiling/timelineDecoder/TimelineCaptureCommandHandler.cpp
         tests/profiling/timelineDecoder/TimelineCaptureCommandHandler.hpp
@@ -946,6 +948,9 @@
         )
 
     include_directories(${Boost_INCLUDE_DIRS} src/profiling)
+    include_directories(${Boost_INCLUDE_DIRS} tests/profiling)
+    include_directories(${Boost_INCLUDE_DIRS} tests/profiling/gatordmock)
+    include_directories(${Boost_INCLUDE_DIRS} tests/profiling/timelineDecoder)
 
     add_library_ex(gatordMockService STATIC ${gatord_mock_sources})
 
diff --git a/src/profiling/ConnectionAcknowledgedCommandHandler.cpp b/src/profiling/ConnectionAcknowledgedCommandHandler.cpp
index a0825c7..630b555 100644
--- a/src/profiling/ConnectionAcknowledgedCommandHandler.cpp
+++ b/src/profiling/ConnectionAcknowledgedCommandHandler.cpp
@@ -38,7 +38,10 @@
         // Once a Connection Acknowledged packet has been received, move to the Active state immediately
         m_StateMachine.TransitionToState(ProfilingState::Active);
 
+        // Send all the packet required for the handshake with the external profiling service
+        m_SendCounterPacket.SendStreamMetaDataPacket();
         m_SendCounterPacket.SendCounterDirectoryPacket(m_CounterDirectory);
+        m_SendTimelinePacket.SendTimelineMessageDirectoryPackage();
 
         // Notify the Send Thread that new data is available in the Counter Stream Buffer
         m_SendCounterPacket.SetReadyToRead();
diff --git a/src/profiling/ConnectionAcknowledgedCommandHandler.hpp b/src/profiling/ConnectionAcknowledgedCommandHandler.hpp
index 7e7904d..05559a3 100644
--- a/src/profiling/ConnectionAcknowledgedCommandHandler.hpp
+++ b/src/profiling/ConnectionAcknowledgedCommandHandler.hpp
@@ -7,6 +7,7 @@
 
 #include "CommandHandlerFunctor.hpp"
 #include "ISendCounterPacket.hpp"
+#include "ISendTimelinePacket.hpp"
 #include "Packet.hpp"
 #include "ProfilingStateMachine.hpp"
 
@@ -25,10 +26,12 @@
                                          uint32_t version,
                                          ICounterDirectory& counterDirectory,
                                          ISendCounterPacket& sendCounterPacket,
+                                         ISendTimelinePacket& sendTimelinePacket,
                                          ProfilingStateMachine& profilingStateMachine)
         : CommandHandlerFunctor(familyId, packetId, version)
         , m_CounterDirectory(counterDirectory)
         , m_SendCounterPacket(sendCounterPacket)
+        , m_SendTimelinePacket(sendTimelinePacket)
         , m_StateMachine(profilingStateMachine)
     {}
 
@@ -37,6 +40,7 @@
 private:
     const ICounterDirectory& m_CounterDirectory;
     ISendCounterPacket&      m_SendCounterPacket;
+    ISendTimelinePacket&     m_SendTimelinePacket;
     ProfilingStateMachine&   m_StateMachine;
 
 };
diff --git a/src/profiling/ProfilingService.hpp b/src/profiling/ProfilingService.hpp
index 427cdbd..b68b652 100644
--- a/src/profiling/ProfilingService.hpp
+++ b/src/profiling/ProfilingService.hpp
@@ -18,6 +18,7 @@
 #include "ProfilingStateMachine.hpp"
 #include "RequestCounterDirectoryCommandHandler.hpp"
 #include "SendCounterPacket.hpp"
+#include "SendTimelinePacket.hpp"
 #include "TimelinePacketWriterFactory.hpp"
 
 namespace armnn
@@ -105,6 +106,7 @@
     CommandHandler m_CommandHandler;
     BufferManager m_BufferManager;
     SendCounterPacket m_SendCounterPacket;
+    SendTimelinePacket m_SendTimelinePacket;
     Holder m_Holder;
     PeriodicCounterCapture m_PeriodicCounterCapture;
     ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler;
@@ -132,12 +134,14 @@
                            m_PacketVersionResolver)
         , m_BufferManager()
         , m_SendCounterPacket(m_StateMachine, m_BufferManager)
+        , m_SendTimelinePacket(m_BufferManager)
         , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this)
         , m_ConnectionAcknowledgedCommandHandler(0,
                                                  1,
                                                  m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(),
                                                  m_CounterDirectory,
                                                  m_SendCounterPacket,
+                                                 m_SendTimelinePacket,
                                                  m_StateMachine)
         , m_RequestCounterDirectoryCommandHandler(0,
                                                   3,
diff --git a/src/profiling/test/ProfilingTests.cpp b/src/profiling/test/ProfilingTests.cpp
index 79a241b..b197273 100644
--- a/src/profiling/test/ProfilingTests.cpp
+++ b/src/profiling/test/ProfilingTests.cpp
@@ -22,6 +22,8 @@
 #include <RequestCounterDirectoryCommandHandler.hpp>
 #include <Runtime.hpp>
 #include <SocketProfilingConnection.hpp>
+#include <SendCounterPacket.hpp>
+#include <SendTimelinePacket.hpp>
 
 #include <armnn/Conversion.hpp>
 
@@ -128,8 +130,11 @@
     CounterDirectory counterDirectory;
     MockBufferManager mockBuffer(1024);
     SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
+    SendTimelinePacket sendTimelinePacket(mockBuffer);
+
     ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
-                                                                              sendCounterPacket, profilingStateMachine);
+                                                                              sendCounterPacket, sendTimelinePacket,
+                                                                              profilingStateMachine);
     CommandHandlerRegistry commandHandlerRegistry;
 
     commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
@@ -1731,9 +1736,10 @@
     CounterDirectory counterDirectory;
     MockBufferManager mockBuffer(1024);
     SendCounterPacket sendCounterPacket(profilingState, mockBuffer);
+    SendTimelinePacket sendTimelinePacket(mockBuffer);
 
     ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId, connectionPacketId, version, counterDirectory,
-                                                        sendCounterPacket, profilingState);
+                                                        sendCounterPacket, sendTimelinePacket, profilingState);
 
     // command handler received packet on ProfilingState::Uninitialised
     BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
@@ -1759,7 +1765,8 @@
     profilingState.TransitionToState(ProfilingState::NotConnected);
     profilingState.TransitionToState(ProfilingState::WaitingForAck);
     ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId, differentPacketId, version,
-                                                                 counterDirectory, sendCounterPacket, profilingState);
+                                                                 counterDirectory, sendCounterPacket,
+                                                                 sendTimelinePacket, profilingState);
     BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
 }
 
diff --git a/tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp b/tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp
new file mode 100644
index 0000000..09255a5
--- /dev/null
+++ b/tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp
@@ -0,0 +1,127 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "StreamMetadataCommandHandler.hpp"
+
+#include <ProfilingUtils.hpp>
+
+#include <boost/cast.hpp>
+
+#include <sstream>
+#include <iostream>
+
+using namespace armnn::profiling;
+
+namespace armnn
+{
+
+namespace gatordmock
+{
+
+void StreamMetadataCommandHandler::operator()(const Packet& packet)
+{
+    ParseData(packet);
+
+    if (m_QuietOperation)
+    {
+        return;
+    }
+
+    std::stringstream ss;
+
+    ss << "Stream metadata packet received" << std::endl << std::endl;
+
+    ss << "Pipe magic: "              << m_PipeMagic                 << std::endl;
+    ss << "Stream metadata version: " << m_StreamMetadataVersion     << std::endl;
+    ss << "Max data len: "            << m_MaxDataLen                << std::endl;
+    ss << "Pid: "                     << m_Pid                       << std::endl;
+    ss << "Software info: "           << m_SoftwareInfo              << std::endl;
+    ss << "Hardware version: "        << m_HardwareVersion           << std::endl;
+    ss << "Software version: "        << m_SoftwareVersion           << std::endl;
+    ss << "Process name: "            << m_ProcessName               << std::endl;
+    ss << "Packet versions: "         << m_PacketVersionTable.size() << std::endl;
+
+    for (const auto& packetVersion : m_PacketVersionTable)
+    {
+        ss << "-----------------------" << std::endl;
+        ss << "Packet family: "  << packetVersion.m_PacketFamily  << std::endl;
+        ss << "Packet id: "      << packetVersion.m_PacketId      << std::endl;
+        ss << "Packet version: " << packetVersion.m_PacketVersion << std::endl;
+    }
+
+    std::cout << ss.str() << std::endl;
+}
+
+std::string ReadString(const unsigned char* buffer, unsigned int &offset)
+{
+    const char* stringPtr = reinterpret_cast<const char*>(&buffer[offset]);
+    return stringPtr != nullptr ? std::string(stringPtr) : "";
+}
+
+void StreamMetadataCommandHandler::ParseData(const Packet &packet)
+{
+    // Check that at least the packet contains the fixed-length fields
+    if (packet.GetLength() < 80)
+    {
+        return;
+    }
+
+    // Utils
+    unsigned int uint16_t_size = sizeof(uint16_t);
+    unsigned int uint32_t_size = sizeof(uint32_t);
+
+    const unsigned char* buffer = packet.GetData();
+    unsigned int offset = 0;
+
+    // Get the fixed-length fields
+    m_PipeMagic = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_StreamMetadataVersion = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_MaxDataLen = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_Pid = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_OffsetInfo = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_OffsetHwVersion = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_OffsetSwVersion = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_OffsetProcessName = ReadUint32(buffer, offset);
+    offset += uint32_t_size;
+    m_OffsetPacketVersionTable = ReadUint32(buffer, offset);
+    offset += uint32_t_size * 2; // Also skipping the reserved word (all zeros)
+
+    // Get the string fields
+    m_SoftwareInfo    = m_OffsetInfo        > 0 ? ReadString(buffer, m_OffsetInfo)        : "";
+    m_HardwareVersion = m_OffsetHwVersion   > 0 ? ReadString(buffer, m_OffsetHwVersion)   : "";
+    m_SoftwareVersion = m_OffsetSwVersion   > 0 ? ReadString(buffer, m_OffsetSwVersion)   : "";
+    m_ProcessName     = m_OffsetProcessName > 0 ? ReadString(buffer, m_OffsetProcessName) : "";
+
+    // Get the packet versions
+    m_PacketVersionTable.clear();
+    if (m_OffsetPacketVersionTable > 0)
+    {
+        offset = m_OffsetPacketVersionTable;
+        uint16_t packetEntries = ReadUint16(buffer, offset + uint16_t_size);
+        offset += uint32_t_size; // Also skipping the reserved bytes (all zeros)
+        for (uint16_t i = 0; i < packetEntries; i++)
+        {
+            uint16_t packetFamilyAndId = ReadUint16(buffer, offset + uint16_t_size);
+            uint16_t packetFamily = (packetFamilyAndId >> 10) & 0x003F;
+            uint16_t packetId     = (packetFamilyAndId >>  0) & 0x03FF;
+            offset += uint32_t_size; // Also skipping the reserved bytes (all zeros)
+            uint32_t packetVersion = ReadUint32(buffer, offset);
+            offset += uint32_t_size;
+
+            m_PacketVersionTable.push_back({ packetFamily, packetId, packetVersion });
+        }
+    }
+}
+
+} // namespace gatordmock
+
+} // namespace armnn
diff --git a/tests/profiling/gatordmock/StreamMetadataCommandHandler.hpp b/tests/profiling/gatordmock/StreamMetadataCommandHandler.hpp
new file mode 100644
index 0000000..4558345
--- /dev/null
+++ b/tests/profiling/gatordmock/StreamMetadataCommandHandler.hpp
@@ -0,0 +1,71 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include <Packet.hpp>
+#include <CommandHandlerFunctor.hpp>
+
+#include <vector>
+
+namespace armnn
+{
+
+namespace gatordmock
+{
+
+struct PacketVersion
+{
+    uint16_t m_PacketFamily;
+    uint16_t m_PacketId;
+    uint32_t m_PacketVersion;
+};
+
+class StreamMetadataCommandHandler : public profiling::CommandHandlerFunctor
+{
+
+public:
+    /**
+     * @param familyId The family of the packets this handler will service
+     * @param packetId The id of packets this handler will process
+     * @param version The version of that id
+     * @param quietOperation Optional parameter to turn off printouts. This is useful for unit tests
+     */
+    StreamMetadataCommandHandler(uint32_t familyId,
+                                 uint32_t packetId,
+                                 uint32_t version,
+                                 bool quietOperation = false)
+        : CommandHandlerFunctor(familyId, packetId, version)
+        , m_QuietOperation(quietOperation)
+    {}
+
+    void operator()(const armnn::profiling::Packet& packet) override;
+
+private:
+    void ParseData(const armnn::profiling::Packet& packet);
+
+    uint32_t m_PipeMagic;
+    uint32_t m_StreamMetadataVersion;
+    uint32_t m_MaxDataLen;
+    uint32_t m_Pid;
+    uint32_t m_OffsetInfo;
+    uint32_t m_OffsetHwVersion;
+    uint32_t m_OffsetSwVersion;
+    uint32_t m_OffsetProcessName;
+    uint32_t m_OffsetPacketVersionTable;
+
+    std::string m_SoftwareInfo;
+    std::string m_HardwareVersion;
+    std::string m_SoftwareVersion;
+    std::string m_ProcessName;
+
+    std::vector<PacketVersion> m_PacketVersionTable;
+
+    bool m_QuietOperation;
+};
+
+} // namespace gatordmock
+
+} // namespace armnn
diff --git a/tests/profiling/gatordmock/tests/GatordMockTests.cpp b/tests/profiling/gatordmock/tests/GatordMockTests.cpp
index 53f580d..26c0361 100644
--- a/tests/profiling/gatordmock/tests/GatordMockTests.cpp
+++ b/tests/profiling/gatordmock/tests/GatordMockTests.cpp
@@ -3,12 +3,13 @@
 // SPDX-License-Identifier: MIT
 //
 
-#include "../GatordMockService.hpp"
-#include "../PeriodicCounterCaptureCommandHandler.hpp"
-
 #include <CommandHandlerRegistry.hpp>
 #include <DirectoryCaptureCommandHandler.hpp>
 #include <ProfilingService.hpp>
+#include <GatordMockService.hpp>
+#include <PeriodicCounterCaptureCommandHandler.hpp>
+#include <StreamMetadataCommandHandler.hpp>
+#include <TimelineDirectoryCaptureCommandHandler.hpp>
 
 #include <test/SendCounterPacketTests.hpp>
 
@@ -117,15 +118,23 @@
     profiling::CommandHandlerRegistry registry;
 
     // Update with derived functors
+    gatordmock::StreamMetadataCommandHandler streamMetadataCommandHandler(
+        0, 0, packetVersionResolver.ResolvePacketVersion(0, 0).GetEncodedValue(), true);
+
     gatordmock::PeriodicCounterCaptureCommandHandler counterCaptureCommandHandler(
         0, 4, packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), true);
 
     profiling::DirectoryCaptureCommandHandler directoryCaptureCommandHandler(
         0, 2, packetVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), true);
 
+    gatordmock::TimelineDirectoryCaptureCommandHandler timelineDirectoryCaptureCommandHandler(
+        1, 0, packetVersionResolver.ResolvePacketVersion(1, 0).GetEncodedValue(), true);
+
     // Register different derived functors
+    registry.RegisterFunctor(&streamMetadataCommandHandler);
     registry.RegisterFunctor(&counterCaptureCommandHandler);
     registry.RegisterFunctor(&directoryCaptureCommandHandler);
+    registry.RegisterFunctor(&timelineDirectoryCaptureCommandHandler);
     // Setup the mock service to bind to the UDS.
     std::string udsNamespace = "gatord_namespace";
     gatordmock::GatordMockService mockService(registry, false);