aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Native/Tango.Emulations.Native/EmbeddedEmulator.cpp
blob: 431b641cd55c5cd6962971f5397b2aa01f1af965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "EmbeddedEmulator.h"
#include "MessageContainer.pb-c.h"
#include "MessageType.pb-c.h"
#include "UploadHardwareConfigurationRequest.pb-c.h";

using namespace std;

namespace Tango
{
	namespace Emulations
	{
		namespace Native
		{
			EmbeddedEmulator::EmbeddedEmulator()
			{
			}


			EmbeddedEmulator::~EmbeddedEmulator()
			{

			}

			MessageContainer createContainer(MessageType type, char* token, protobuf_c_boolean completed, void* response, size_t(*packPtr)(void*, uint8_t*), size_t(*sizePtr)(void*))
			{
				MessageContainer container = MESSAGE_CONTAINER__INIT;
				container.completed = completed;
				container.token = token;
				container.has_completed = true;
				container.has_data = true;
				container.has_type = true;
				container.type = type;



				uint8_t* response_buffer = (uint8_t*)malloc((*sizePtr)(response));
				size_t response_size = (*packPtr)(response, response_buffer);
				container.data.data = response_buffer;
				container.data.len = response_size;

				//free(response);
				return container;
			}

			size_t EmbeddedEmulator::ProcessMessage(uint8_t * input_buffer, size_t input_buffer_size, uint8_t *& output_buffer)
			{
				MessageContainer * container = message_container__unpack(NULL, input_buffer_size, input_buffer);

				switch (container->type)
				{
				case MESSAGE_TYPE__UploadHardwareConfigurationRequest:

					UploadHardwareConfigurationRequest* hardwareRequest = upload_hardware_configuration_request__unpack(NULL, container->data.len, container->data.data);
					MessageContainer responseContainer = createContainer(MESSAGE_TYPE__UploadHardwareConfigurationRequest, container->token, true, hardwareRequest, (size_t(*)(void*, uint8_t*))&upload_hardware_configuration_request__pack, (size_t(*)(void*))&upload_hardware_configuration_request__get_packed_size);

					output_buffer = (uint8_t*)malloc(message_container__get_packed_size(&responseContainer));
					size_t container_size = message_container__pack(&responseContainer, output_buffer);
					return container_size;
				}

				return 0;
			}
		}
	}
}