/*
 * ReceiveSync.c
 * 
 * Gocator 2000 Sample
 * Copyright (C) 2011 by LMI Technologies Inc.
 * 
 * Licensed under The MIT License.
 * Redistributions of files must retain the above copyright notice.
 *
 * Purpose: Connect to Gocator system and receive data
 *
 */
#include <Go2.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#define NUM_PROFILES	10
#define RECEIVE_TIMEOUT	20000 

void main(int argc, char **argv)
{
	int status;
	Go2System system = GO2_NULL;
	Go2Data data = GO2_NULL;
	Go2IPAddress address;
	short int* memory = NULL;
	unsigned int i;
	unsigned int j;


	// initialize Go2 API
	if ((status = Go2Api_Initialize()) != GO2_OK)
	{
		printf("Error: Go2Api_Initialize:%d\n", status);
		return;
	}

	// construct Go2 system object
	if ((status = Go2System_Construct(&system)) != GO2_OK)
	{
		printf("Error: Go2System_Construct:%d\n", status);
		return;
	}

	// convert ip address string to Go2IPAddress object
	if ((status = Go2IPAddress_Parse("192.168.1.10", &address)) != GO2_OK)
	{
		printf("Error: Go2IPAddress_Parse:%d\n", status);
		return;
	}

	// connect to Gocator system at default address
	if ((status = Go2System_Connect(system,address)) != GO2_OK)
	{
		printf("Error: Go2System_Connect:%d\n", status);
		return;
	}

	// establish a Gocator data connection	
	if ((status = Go2System_ConnectData(system, GO2_NULL, GO2_NULL)) != GO2_OK)
	{
		printf("Error: Go2System_ConnectData:%d\n", status);
		return;
	}

	// Start Gocator system	
	if ((status = Go2System_Start(system)) != GO2_OK)
	{
		printf("Error: Go2System_Start:%d\n", status);
		return;
	}

	//loop until 10 results are received
	i = 0;
	while (i < NUM_PROFILES)
	{
		if (Go2System_ReceiveData(system, RECEIVE_TIMEOUT, &data) == GO2_OK)
		{
			printf("Data message received:\n"); 
			printf("  Timestamp: %llu\n", Go2Data_Timestamp(data)); 
			printf("  Encoder: %lld\n", Go2Data_Encoder(data));  
			printf("  Frame index: %llu\n", Go2Data_FrameIndex(data));  
			printf("  Item count: %u\n", Go2Data_ItemCount(data));  

			//each result can have multiple data items
			for (j = 0; j < Go2Data_ItemCount(data); ++j)
			{
				Go2Data dataItem = Go2Data_ItemAt(data, j); 
				if(Go2Object_Type(dataItem) == GO2_TYPE_PROFILE_DATA)
				{   
					unsigned int profilePointCount = Go2ProfileData_Width(dataItem); 
					unsigned int profileSizeBytes = profilePointCount * sizeof(short int);         
					printf("    Item[%u]: Profile data (%u points)\n", j, profilePointCount);
					//allocate memory
					if (memory == NULL)
					{	
						memory = malloc(NUM_PROFILES * profileSizeBytes);
					}
					//copy profiles to memory array
					memcpy(&memory[i * profilePointCount], Go2ProfileData_Ranges(dataItem), profileSizeBytes);
					i++;
				}       
			}
			if ((status = Go2Data_Destroy(data)) != GO2_OK)
			{
				printf("Error: Go2Data_Destroy:%d\n", status);
				return;
			}
		}
	}
	
	// stop Gocator system	
	if ((status = Go2System_Stop(system)) != GO2_OK)
	{
		printf ("Error: Go2System_Stop:%d\n", status);
		return;
	}

	// destroy system handle
	if ((status = Go2System_Destroy(system)) != GO2_OK)
	{
		printf ("Error: Go2System_Destroy:%d\n", status);
		return;
	}

	// terminate Go2 API
	if ((status = Go2Api_Terminate()) != GO2_OK)
	{
		printf ("Error: Go2Api_Terminate:%d\n", status);
		return;
	}

	//free memory array
	free(memory);

	printf("Press any key to continue...\n", status);
	getchar();

	return;
}
