blob: 3e19c25b6c3a428f54f1af5ab690184b682b9f11 [file] [log] [blame]
Colm Donelana21620d2019-10-11 13:09:49 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
Matteo Martincigh65984272019-10-17 13:26:21 +01005
Colm Donelana21620d2019-10-11 13:09:49 +01006#include "GatordMockService.hpp"
7
Matteo Martincigh65984272019-10-17 13:26:21 +01008#include <CommandHandlerRegistry.hpp>
Keith Davis3201eea2019-10-24 17:30:41 +01009#include <PacketVersionResolver.hpp>
10#include <ProfilingUtils.hpp>
Rob Hughes25b74362020-01-13 11:14:59 +000011#include <NetworkSockets.hpp>
Colm Donelana21620d2019-10-11 13:09:49 +010012
13#include <cerrno>
14#include <fcntl.h>
Colm Donelanb682d842019-10-16 12:24:20 +010015#include <iomanip>
Colm Donelana21620d2019-10-11 13:09:49 +010016#include <iostream>
Colm Donelana21620d2019-10-11 13:09:49 +010017#include <string>
Rob Hughes25b74362020-01-13 11:14:59 +000018
19using namespace armnnUtils;
Colm Donelana21620d2019-10-11 13:09:49 +010020
Colm Donelana21620d2019-10-11 13:09:49 +010021namespace armnn
22{
23
24namespace gatordmock
25{
26
Finn Williamse6a2ccd2020-02-27 16:21:41 +000027bool GatordMockService::OpenListeningSocket(armnnUtils::Sockets::Socket listeningSocket,
28 const std::string udsNamespace,
29 const int numOfConnections)
Colm Donelana21620d2019-10-11 13:09:49 +010030{
Finn Williamse6a2ccd2020-02-27 16:21:41 +000031 if (-1 == listeningSocket)
Colm Donelana21620d2019-10-11 13:09:49 +010032 {
33 std::cerr << ": Socket construction failed: " << strerror(errno) << std::endl;
34 return false;
35 }
36
37 sockaddr_un udsAddress;
38 memset(&udsAddress, 0, sizeof(sockaddr_un));
39 // We've set the first element of sun_path to be 0, skip over it and copy the namespace after it.
40 memcpy(udsAddress.sun_path + 1, udsNamespace.c_str(), strlen(udsNamespace.c_str()));
41 udsAddress.sun_family = AF_UNIX;
42
43 // Bind the socket to the UDS namespace.
Finn Williamse6a2ccd2020-02-27 16:21:41 +000044 if (-1 == bind(listeningSocket, reinterpret_cast<const sockaddr*>(&udsAddress), sizeof(sockaddr_un)))
Colm Donelana21620d2019-10-11 13:09:49 +010045 {
46 std::cerr << ": Binding on socket failed: " << strerror(errno) << std::endl;
47 return false;
48 }
Finn Williamse6a2ccd2020-02-27 16:21:41 +000049 // Listen for 10 connections.
50 if (-1 == listen(listeningSocket, numOfConnections))
Colm Donelana21620d2019-10-11 13:09:49 +010051 {
52 std::cerr << ": Listen call on socket failed: " << strerror(errno) << std::endl;
53 return false;
54 }
Colm Donelana21620d2019-10-11 13:09:49 +010055 return true;
56}
57
Colm Donelana21620d2019-10-11 13:09:49 +010058bool GatordMockService::WaitForStreamMetaData()
59{
60 if (m_EchoPackets)
61 {
62 std::cout << "Waiting for stream meta data..." << std::endl;
63 }
64 // The start of the stream metadata is 2x32bit words, 0 and packet length.
Rob Hughes270233f2019-11-13 11:53:48 +000065 uint8_t header[8];
Colm Donelana21620d2019-10-11 13:09:49 +010066 if (!ReadFromSocket(header, 8))
67 {
68 return false;
69 }
Colm Donelanb682d842019-10-16 12:24:20 +010070 EchoPacket(PacketDirection::ReceivedHeader, header, 8);
Colm Donelana21620d2019-10-11 13:09:49 +010071 // The first word, stream_metadata_identifer, should always be 0.
72 if (ToUint32(&header[0], TargetEndianness::BeWire) != 0)
73 {
74 std::cerr << ": Protocol error. The stream_metadata_identifer was not 0." << std::endl;
75 return false;
76 }
77
Rob Hughes270233f2019-11-13 11:53:48 +000078 uint8_t pipeMagic[4];
Colm Donelana21620d2019-10-11 13:09:49 +010079 if (!ReadFromSocket(pipeMagic, 4))
80 {
81 return false;
82 }
Colm Donelanb682d842019-10-16 12:24:20 +010083 EchoPacket(PacketDirection::ReceivedData, pipeMagic, 4);
Colm Donelana21620d2019-10-11 13:09:49 +010084
85 // Before we interpret the length we need to read the pipe_magic word to determine endianness.
86 if (ToUint32(&pipeMagic[0], TargetEndianness::BeWire) == PIPE_MAGIC)
87 {
88 m_Endianness = TargetEndianness::BeWire;
89 }
90 else if (ToUint32(&pipeMagic[0], TargetEndianness::LeWire) == PIPE_MAGIC)
91 {
92 m_Endianness = TargetEndianness::LeWire;
93 }
94 else
95 {
96 std::cerr << ": Protocol read error. Unable to read PIPE_MAGIC value." << std::endl;
97 return false;
98 }
99 // Now we know the endianness we can get the length from the header.
100 // Remember we already read the pipe magic 4 bytes.
101 uint32_t metaDataLength = ToUint32(&header[4], m_Endianness) - 4;
102 // Read the entire packet.
Rob Hughes25b74362020-01-13 11:14:59 +0000103 std::vector<uint8_t> packetData(metaDataLength);
104 if (metaDataLength !=
105 boost::numeric_cast<uint32_t>(Sockets::Read(m_ClientConnection, packetData.data(), metaDataLength)))
Colm Donelana21620d2019-10-11 13:09:49 +0100106 {
107 std::cerr << ": Protocol read error. Data length mismatch." << std::endl;
108 return false;
109 }
Rob Hughes25b74362020-01-13 11:14:59 +0000110 EchoPacket(PacketDirection::ReceivedData, packetData.data(), metaDataLength);
Colm Donelanb682d842019-10-16 12:24:20 +0100111 m_StreamMetaDataVersion = ToUint32(&packetData[0], m_Endianness);
Colm Donelana21620d2019-10-11 13:09:49 +0100112 m_StreamMetaDataMaxDataLen = ToUint32(&packetData[4], m_Endianness);
Colm Donelanb682d842019-10-16 12:24:20 +0100113 m_StreamMetaDataPid = ToUint32(&packetData[8], m_Endianness);
Colm Donelana21620d2019-10-11 13:09:49 +0100114
115 return true;
116}
117
118void GatordMockService::SendConnectionAck()
119{
120 if (m_EchoPackets)
121 {
122 std::cout << "Sending connection acknowledgement." << std::endl;
123 }
124 // The connection ack packet is an empty data packet with packetId == 1.
125 SendPacket(0, 1, nullptr, 0);
126}
127
Finn Williams15db7452019-10-15 14:22:13 +0100128void GatordMockService::SendRequestCounterDir()
129{
130 if (m_EchoPackets)
131 {
132 std::cout << "Sending connection acknowledgement." << std::endl;
133 }
Keith Davis33ed2212020-03-30 10:43:41 +0100134 // The request counter directory packet is an empty data packet with packetId == 3.
Finn Williams15db7452019-10-15 14:22:13 +0100135 SendPacket(0, 3, nullptr, 0);
136}
137
Keith Davis33ed2212020-03-30 10:43:41 +0100138void GatordMockService::SendActivateTimelinePacket()
139{
140 if (m_EchoPackets)
141 {
142 std::cout << "Sending activate timeline packet." << std::endl;
143 }
144 // The activate timeline packet is an empty data packet with packetId == 6.
145 SendPacket(0, 6, nullptr, 0);
146}
147
148void GatordMockService::SendDeactivateTimelinePacket()
149{
150 if (m_EchoPackets)
151 {
152 std::cout << "Sending deactivate timeline packet." << std::endl;
153 }
154 // The deactivate timeline packet is an empty data packet with packetId == 7.
155 SendPacket(0, 7, nullptr, 0);
156}
157
Colm Donelana21620d2019-10-11 13:09:49 +0100158bool GatordMockService::LaunchReceivingThread()
159{
160 if (m_EchoPackets)
161 {
162 std::cout << "Launching receiving thread." << std::endl;
163 }
164 // At this point we want to make the socket non blocking.
Rob Hughes25b74362020-01-13 11:14:59 +0000165 if (!Sockets::SetNonBlocking(m_ClientConnection))
Colm Donelana21620d2019-10-11 13:09:49 +0100166 {
Rob Hughes25b74362020-01-13 11:14:59 +0000167 Sockets::Close(m_ClientConnection);
Colm Donelana21620d2019-10-11 13:09:49 +0100168 std::cerr << "Failed to set socket as non blocking: " << strerror(errno) << std::endl;
169 return false;
170 }
171 m_ListeningThread = std::thread(&GatordMockService::ReceiveLoop, this, std::ref(*this));
172 return true;
173}
174
175void GatordMockService::WaitForReceivingThread()
176{
Colm Donelan02705242019-11-14 14:19:07 +0000177 // The receiving thread may already have died.
178 if (m_CloseReceivingThread != true)
179 {
180 m_CloseReceivingThread.store(true);
181 }
Colm Donelana21620d2019-10-11 13:09:49 +0100182 // Check that the receiving thread is running
183 if (m_ListeningThread.joinable())
184 {
185 // Wait for the receiving thread to complete operations
186 m_ListeningThread.join();
187 }
Keith Davis33ed2212020-03-30 10:43:41 +0100188
189 if(m_EchoPackets)
190 {
191 m_TimelineDecoder.print();
192 }
Colm Donelana21620d2019-10-11 13:09:49 +0100193}
194
Colm Donelanb682d842019-10-16 12:24:20 +0100195void GatordMockService::SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters)
Colm Donelana21620d2019-10-11 13:09:49 +0100196{
Colm Donelanb682d842019-10-16 12:24:20 +0100197 // The packet body consists of a UINT32 representing the period following by zero or more
198 // UINT16's representing counter UID's. If the list is empty it implies all counters are to
199 // be disabled.
Colm Donelana21620d2019-10-11 13:09:49 +0100200
Colm Donelanb682d842019-10-16 12:24:20 +0100201 if (m_EchoPackets)
Colm Donelana21620d2019-10-11 13:09:49 +0100202 {
Colm Donelanb682d842019-10-16 12:24:20 +0100203 std::cout << "SendPeriodicCounterSelectionList: Period=" << std::dec << period << "uSec" << std::endl;
204 std::cout << "List length=" << counters.size() << std::endl;
205 ;
206 }
207 // Start by calculating the length of the packet body in bytes. This will be at least 4.
208 uint32_t dataLength = static_cast<uint32_t>(4 + (counters.size() * 2));
209
210 std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength);
211 unsigned char* data = reinterpret_cast<unsigned char*>(uniqueData.get());
212
213 uint32_t offset = 0;
214 profiling::WriteUint32(data, offset, period);
215 offset += 4;
216 for (std::vector<uint16_t>::iterator it = counters.begin(); it != counters.end(); ++it)
217 {
218 profiling::WriteUint16(data, offset, *it);
219 offset += 2;
Colm Donelana21620d2019-10-11 13:09:49 +0100220 }
221
Colm Donelanb682d842019-10-16 12:24:20 +0100222 // Send the packet.
223 SendPacket(0, 4, data, dataLength);
224 // There will be an echo response packet sitting in the receive thread. PeriodicCounterSelectionResponseHandler
225 // should deal with it.
Colm Donelana21620d2019-10-11 13:09:49 +0100226}
227
Rob Hughes25b74362020-01-13 11:14:59 +0000228void GatordMockService::WaitCommand(uint32_t timeout)
Colm Donelana21620d2019-10-11 13:09:49 +0100229{
Colm Donelan02705242019-11-14 14:19:07 +0000230 // Wait for a maximum of timeout microseconds or if the receive thread has closed.
231 // There is a certain level of rounding involved in this timing.
Rob Hughes25b74362020-01-13 11:14:59 +0000232 uint32_t iterations = timeout / 1000;
Colm Donelan02705242019-11-14 14:19:07 +0000233 std::cout << std::dec << "Wait command with timeout of " << timeout << " iterations = " << iterations << std::endl;
Rob Hughes25b74362020-01-13 11:14:59 +0000234 uint32_t count = 0;
Colm Donelan02705242019-11-14 14:19:07 +0000235 while ((this->ReceiveThreadRunning() && (count < iterations)))
236 {
237 std::this_thread::sleep_for(std::chrono::microseconds(1000));
238 ++count;
239 }
Colm Donelana21620d2019-10-11 13:09:49 +0100240 if (m_EchoPackets)
241 {
242 std::cout << std::dec << "Wait command with timeout of " << timeout << " microseconds completed. " << std::endl;
243 }
244}
245
246void GatordMockService::ReceiveLoop(GatordMockService& mockService)
247{
248 m_CloseReceivingThread.store(false);
249 while (!m_CloseReceivingThread.load())
250 {
251 try
252 {
253 armnn::profiling::Packet packet = mockService.WaitForPacket(500);
254 }
Jammy Zhoud80cc0c2019-10-21 16:44:40 +0800255 catch (const armnn::TimeoutException&)
Colm Donelana21620d2019-10-11 13:09:49 +0100256 {
257 // In this case we ignore timeouts and and keep trying to receive.
258 }
Keith Davis3201eea2019-10-24 17:30:41 +0100259 catch (const armnn::InvalidArgumentException& e)
Colm Donelanb682d842019-10-16 12:24:20 +0100260 {
261 // We couldn't find a functor to handle the packet?
262 std::cerr << "Packet received that could not be processed: " << e.what() << std::endl;
263 }
Keith Davis3201eea2019-10-24 17:30:41 +0100264 catch (const armnn::RuntimeException& e)
Colm Donelanb682d842019-10-16 12:24:20 +0100265 {
266 // A runtime exception occurred which means we must exit the loop.
267 std::cerr << "Receive thread closing: " << e.what() << std::endl;
268 m_CloseReceivingThread.store(true);
269 }
Colm Donelana21620d2019-10-11 13:09:49 +0100270 }
271}
272
273armnn::profiling::Packet GatordMockService::WaitForPacket(uint32_t timeoutMs)
274{
275 // Is there currently more than a headers worth of data waiting to be read?
276 int bytes_available;
Rob Hughes25b74362020-01-13 11:14:59 +0000277 Sockets::Ioctl(m_ClientConnection, FIONREAD, &bytes_available);
Colm Donelana21620d2019-10-11 13:09:49 +0100278 if (bytes_available > 8)
279 {
280 // Yes there is. Read it:
281 return ReceivePacket();
282 }
283 else
284 {
285 // No there's not. Poll for more data.
286 struct pollfd pollingFd[1]{};
287 pollingFd[0].fd = m_ClientConnection;
Rob Hughes25b74362020-01-13 11:14:59 +0000288 int pollResult = Sockets::Poll(pollingFd, 1, static_cast<int>(timeoutMs));
Colm Donelana21620d2019-10-11 13:09:49 +0100289
290 switch (pollResult)
291 {
292 // Error
293 case -1:
294 throw armnn::RuntimeException(std::string("File descriptor reported an error during polling: ") +
295 strerror(errno));
296
297 // Timeout
298 case 0:
299 throw armnn::TimeoutException("Timeout while waiting to receive packet.");
300
301 // Normal poll return. It could still contain an error signal
302 default:
Colm Donelana21620d2019-10-11 13:09:49 +0100303 // Check if the socket reported an error
304 if (pollingFd[0].revents & (POLLNVAL | POLLERR | POLLHUP))
305 {
Colm Donelanb682d842019-10-16 12:24:20 +0100306 if (pollingFd[0].revents == POLLNVAL)
307 {
308 throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLNVAL"));
309 }
310 if (pollingFd[0].revents == POLLERR)
311 {
312 throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLERR: ") +
313 strerror(errno));
314 }
315 if (pollingFd[0].revents == POLLHUP)
316 {
317 throw armnn::RuntimeException(std::string("Connection closed by remote client: POLLHUP"));
318 }
Colm Donelana21620d2019-10-11 13:09:49 +0100319 }
320
321 // Check if there is data to read
322 if (!(pollingFd[0].revents & (POLLIN)))
323 {
324 // This is a corner case. The socket as been woken up but not with any data.
325 // We'll throw a timeout exception to loop around again.
326 throw armnn::TimeoutException("File descriptor was polled but no data was available to receive.");
327 }
328 return ReceivePacket();
329 }
330 }
331}
332
Colm Donelana21620d2019-10-11 13:09:49 +0100333armnn::profiling::Packet GatordMockService::ReceivePacket()
334{
335 uint32_t header[2];
336 if (!ReadHeader(header))
337 {
338 return armnn::profiling::Packet();
339 }
340 // Read data_length bytes from the socket.
341 std::unique_ptr<unsigned char[]> uniquePacketData = std::make_unique<unsigned char[]>(header[1]);
Colm Donelanb682d842019-10-16 12:24:20 +0100342 unsigned char* packetData = reinterpret_cast<unsigned char*>(uniquePacketData.get());
Colm Donelana21620d2019-10-11 13:09:49 +0100343
344 if (!ReadFromSocket(packetData, header[1]))
345 {
346 return armnn::profiling::Packet();
347 }
348
Colm Donelanb682d842019-10-16 12:24:20 +0100349 EchoPacket(PacketDirection::ReceivedData, packetData, header[1]);
350
Colm Donelana21620d2019-10-11 13:09:49 +0100351 // Construct received packet
Colm Donelanb682d842019-10-16 12:24:20 +0100352 armnn::profiling::PacketVersionResolver packetVersionResolver;
Colm Donelana21620d2019-10-11 13:09:49 +0100353 armnn::profiling::Packet packetRx = armnn::profiling::Packet(header[0], header[1], uniquePacketData);
Colm Donelanb682d842019-10-16 12:24:20 +0100354 if (m_EchoPackets)
Colm Donelana21620d2019-10-11 13:09:49 +0100355 {
Colm Donelanb682d842019-10-16 12:24:20 +0100356 std::cout << "Processing packet ID= " << packetRx.GetPacketId() << " Length=" << packetRx.GetLength()
357 << std::endl;
Colm Donelana21620d2019-10-11 13:09:49 +0100358 }
359
Keith Davis3201eea2019-10-24 17:30:41 +0100360 profiling::Version version =
361 packetVersionResolver.ResolvePacketVersion(packetRx.GetPacketFamily(), packetRx.GetPacketId());
362
363 profiling::CommandHandlerFunctor* commandHandlerFunctor =
364 m_HandlerRegistry.GetFunctor(packetRx.GetPacketFamily(), packetRx.GetPacketId(), version.GetEncodedValue());
365 BOOST_ASSERT(commandHandlerFunctor);
366 commandHandlerFunctor->operator()(packetRx);
Colm Donelana21620d2019-10-11 13:09:49 +0100367 return packetRx;
368}
369
Rob Hughes270233f2019-11-13 11:53:48 +0000370bool GatordMockService::SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength)
Colm Donelana21620d2019-10-11 13:09:49 +0100371{
372 // Construct a packet from the id and data given and send it to the client.
373 // Encode the header.
374 uint32_t header[2];
375 header[0] = packetFamily << 26 | packetId << 16;
376 header[1] = dataLength;
377 // Add the header to the packet.
Rob Hughes25b74362020-01-13 11:14:59 +0000378 std::vector<uint8_t> packet(8 + dataLength);
379 InsertU32(header[0], packet.data(), m_Endianness);
380 InsertU32(header[1], packet.data() + 4, m_Endianness);
Colm Donelana21620d2019-10-11 13:09:49 +0100381 // And the rest of the data if there is any.
382 if (dataLength > 0)
383 {
Rob Hughes25b74362020-01-13 11:14:59 +0000384 memcpy((packet.data() + 8), data, dataLength);
Colm Donelana21620d2019-10-11 13:09:49 +0100385 }
Rob Hughes25b74362020-01-13 11:14:59 +0000386 EchoPacket(PacketDirection::Sending, packet.data(), packet.size());
387 if (-1 == Sockets::Write(m_ClientConnection, packet.data(), packet.size()))
Colm Donelana21620d2019-10-11 13:09:49 +0100388 {
389 std::cerr << ": Failure when writing to client socket: " << strerror(errno) << std::endl;
390 return false;
391 }
392 return true;
393}
394
395bool GatordMockService::ReadHeader(uint32_t headerAsWords[2])
396{
Colm Donelanb682d842019-10-16 12:24:20 +0100397 // The header will always be 2x32bit words.
Rob Hughes270233f2019-11-13 11:53:48 +0000398 uint8_t header[8];
Colm Donelana21620d2019-10-11 13:09:49 +0100399 if (!ReadFromSocket(header, 8))
400 {
401 return false;
402 }
Colm Donelanb682d842019-10-16 12:24:20 +0100403 EchoPacket(PacketDirection::ReceivedHeader, header, 8);
Colm Donelana21620d2019-10-11 13:09:49 +0100404 headerAsWords[0] = ToUint32(&header[0], m_Endianness);
405 headerAsWords[1] = ToUint32(&header[4], m_Endianness);
406 return true;
407}
408
Rob Hughes270233f2019-11-13 11:53:48 +0000409bool GatordMockService::ReadFromSocket(uint8_t* packetData, uint32_t expectedLength)
Colm Donelana21620d2019-10-11 13:09:49 +0100410{
411 // This is a blocking read until either expectedLength has been received or an error is detected.
Rob Hughes25b74362020-01-13 11:14:59 +0000412 long totalBytesRead = 0;
Jammy Zhoud80cc0c2019-10-21 16:44:40 +0800413 while (boost::numeric_cast<uint32_t>(totalBytesRead) < expectedLength)
Colm Donelana21620d2019-10-11 13:09:49 +0100414 {
Rob Hughes25b74362020-01-13 11:14:59 +0000415 long bytesRead = Sockets::Read(m_ClientConnection, packetData, expectedLength);
Colm Donelana21620d2019-10-11 13:09:49 +0100416 if (bytesRead < 0)
417 {
418 std::cerr << ": Failure when reading from client socket: " << strerror(errno) << std::endl;
419 return false;
420 }
421 if (bytesRead == 0)
422 {
423 std::cerr << ": EOF while reading from client socket." << std::endl;
424 return false;
425 }
426 totalBytesRead += bytesRead;
427 }
428 return true;
429};
430
Rob Hughes270233f2019-11-13 11:53:48 +0000431void GatordMockService::EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes)
Colm Donelana21620d2019-10-11 13:09:49 +0100432{
433 // If enabled print the contents of the data packet to the console.
434 if (m_EchoPackets)
435 {
436 if (direction == PacketDirection::Sending)
437 {
Colm Donelanb682d842019-10-16 12:24:20 +0100438 std::cout << "TX " << std::dec << lengthInBytes << " bytes : ";
439 }
440 else if (direction == PacketDirection::ReceivedHeader)
Colm Donelana21620d2019-10-11 13:09:49 +0100441 {
Colm Donelanb682d842019-10-16 12:24:20 +0100442 std::cout << "RX Header " << std::dec << lengthInBytes << " bytes : ";
443 }
444 else
445 {
446 std::cout << "RX Data " << std::dec << lengthInBytes << " bytes : ";
Colm Donelana21620d2019-10-11 13:09:49 +0100447 }
448 for (unsigned int i = 0; i < lengthInBytes; i++)
449 {
450 if ((i % 10) == 0)
451 {
452 std::cout << std::endl;
453 }
Colm Donelanb682d842019-10-16 12:24:20 +0100454 std::cout << "0x" << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(packet[i])
455 << " ";
Colm Donelana21620d2019-10-11 13:09:49 +0100456 }
457 std::cout << std::endl;
458 }
459}
460
Rob Hughes270233f2019-11-13 11:53:48 +0000461uint32_t GatordMockService::ToUint32(uint8_t* data, TargetEndianness endianness)
Colm Donelana21620d2019-10-11 13:09:49 +0100462{
463 // Extract the first 4 bytes starting at data and push them into a 32bit integer based on the
464 // specified endianness.
465 if (endianness == TargetEndianness::BeWire)
466 {
467 return static_cast<uint32_t>(data[0]) << 24 | static_cast<uint32_t>(data[1]) << 16 |
468 static_cast<uint32_t>(data[2]) << 8 | static_cast<uint32_t>(data[3]);
469 }
470 else
471 {
472 return static_cast<uint32_t>(data[3]) << 24 | static_cast<uint32_t>(data[2]) << 16 |
473 static_cast<uint32_t>(data[1]) << 8 | static_cast<uint32_t>(data[0]);
474 }
475}
476
Rob Hughes270233f2019-11-13 11:53:48 +0000477void GatordMockService::InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness)
Colm Donelana21620d2019-10-11 13:09:49 +0100478{
479 // Take the bytes of a 32bit integer and copy them into char array starting at data considering
480 // the endianness value.
481 if (endianness == TargetEndianness::BeWire)
482 {
Rob Hughes270233f2019-11-13 11:53:48 +0000483 *data = static_cast<uint8_t>((value >> 24) & 0xFF);
484 *(data + 1) = static_cast<uint8_t>((value >> 16) & 0xFF);
485 *(data + 2) = static_cast<uint8_t>((value >> 8) & 0xFF);
486 *(data + 3) = static_cast<uint8_t>(value & 0xFF);
Colm Donelana21620d2019-10-11 13:09:49 +0100487 }
488 else
489 {
Rob Hughes270233f2019-11-13 11:53:48 +0000490 *(data + 3) = static_cast<uint8_t>((value >> 24) & 0xFF);
491 *(data + 2) = static_cast<uint8_t>((value >> 16) & 0xFF);
492 *(data + 1) = static_cast<uint8_t>((value >> 8) & 0xFF);
493 *data = static_cast<uint8_t>(value & 0xFF);
Colm Donelana21620d2019-10-11 13:09:49 +0100494 }
495}
496
Colm Donelanb682d842019-10-16 12:24:20 +0100497} // namespace gatordmock
Colm Donelana21620d2019-10-11 13:09:49 +0100498
Colm Donelanb682d842019-10-16 12:24:20 +0100499} // namespace armnn