//----------------------------------------------------------------------------------
// <copyright file="user_app_init.h" company="LiquidRobotics">
//	Copyright (c) Liquid Robotics Corporation.  All rights reserved.
// </copyright>
//
// <summary>
// 	Prototypes and definitions for app_init.c
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------

#ifndef __USER_APP_INIT_H__
#define __USER_APP_INIT_H__

//Use this decoration on the declaration of methods that are used as targets of
//function pointers (entry points for the tasks)
#define CALLBACK __attribute__ ((section(".callbacks")))

//--------------------
// Version Information
//--------------------
#define MAJOR	2
#define MINOR	1
#define BUILD	395


//EEPROM Data Locations
//Boot Loader Specific -- 0x00 to 0xFF

//System Parameters
#define EEPROM_SYSTEM_PARAMS_START			0x100
#define EEPROM_SYSTEM_PARAMS_SIZE			0x200
#define EEPROM_BOARD_ID_OFFSET				0x01		// One byte
#define EEPROM_POWER_DOWN_DELAY_OFFSET		0x02		// Two bytes
#define EEPROM_TASK1_OFFSET					0x10
#define	EEPROM_TASK2_OFFSET					0x20
#define EEPROM_TASK3_OFFSET					0x30

#define EEPROM_TASK_SIZE					0x10

#define EMPTY		0
#define PYLDCOMM	1
#define SMC			2
#define CTD37SI		3
#define GPCTD		4
#define C3			5
#define POWER		6
#define ATM			7
#define CO2			8


//Defines a task stack; use with LAUNCH_TASK with the same "taskName" parameter
//Note that the "taskName" parameter should not be quoted.
#define DEFINE_TASK_STACK(taskName,stackSize) OS_STK taskStack_##taskName[stackSize]

//Launches and sets the name of a task when a stack has already been defined using
//the same "taskName" parameter.  Note that the "taskName" parameter should not be
//quoted
#define LAUNCH_TASK(taskName, entryPoint, startParam, priority, id, pext, options, errorVar) 					\
			do {																								\
				OSTaskCreateExt(entryPoint, startParam, &taskStack_##taskName[sizeof(taskStack_##taskName)-1], \
					priority, id, taskStack_##taskName, sizeof(taskStack_##taskName), pext, options); 			\
				OSTaskNameSet(priority, (INT8U*) #taskName , &errorVar);										\
			} while(0)

//Creates a static local stack of the given size and then launches the task in one
//step.  The "taskName" parameter is used to create the local stack variable (with
//a prefix) and is also used as the task name: quotes are automatically created, so
//do not quote this parameter
#define DEFINE_TASK_STACK_AND_LAUNCH(taskName, stackSize, entryPoint, startParam, priority, id, pext, options, errorVar) \
			do {																								\
				static DEFINE_TASK_STACK(taskName,stackSize);													\
				LAUNCH_TASK(taskName, entryPoint, startParam, priority, id, pext, options, errorVar);			\
			} while(0)

//Personality Modules
#define EEPROM_PM1_START			0x300
#define EEPROM_PM1_SIZE				EEPROM_PM_SIZE


#define EEPROM_PM2_START			0x500
#define EEPROM_PM2_SIZE				EEPROM_PM_SIZE


#define EEPROM_PM3_START			0x700
#define EEPROM_PM3_SIZE				EEPROM_PM_SIZE

#define EEPROM_PM_SIZE				0x200


#define TASK_BUF_SIZE		4000
#define NUM_TASK_BUFS	3
#define NUM_TASK_FILES	4

extern OS_EVENT* pMutexFloatQueue;


// set by AppInit to TRUE if startup occurred due to watchdog timer overflow or FALSE if not
extern BOOLEAN restartCause_Watchdog;

// pointer to the function to continue as the admin task after AppStartTasks
typedef void (*AppStartContinuationTask_t)(void*);

// Function Prototypes
extern void AppInit(void);
extern AppStartContinuationTask_t AppStartTasks(void);

typedef struct _TASK_INIT_STRUCT_
{
	uint8_t		taskID;
	uint8_t 	portNum;
	uint16_t	eepromBaseAddr;
	char* 	pBuf;
}TASK_INIT_STRUCT, *PTASK_INIT_STRUCT;

#define CONSOLE_RESPONSE_OK							1
#define CONSOLE_RESPONSE_OK_TRUNCATED				2

#define CONSOLE_RESPONSE_UNKNOWN_CMD				10
#define CONSOLE_RESPONSE_DEVICE_BUSY				11
#define CONSOLE_RESPONSE_UNSUPPORTED_CMD			12
#define CONSOLE_RESPONSE_EMPTY_CMD					13
#define CONSOLE_RESPONSE_WRONG_DEVICE				14
#define CONSOLE_RESPONSE_CMD_FAILED					15
#define CONSOLE_RESPONSE_DEVICE_NOT_RESPONDING		16

#define CONSOLE_RESPONSE_DEVICE_CMD_NOT_SUPPORTED	20

#define CONSOLE_RESPONSE_INVALID_CMD_LENGTH			30
#define CONSOLE_RESPONSE_UNKNOWN_DESTINATION		31

typedef struct _CONSOLE_RESPONSE_INFO_
{
	uint16_t	source;
	uint8_t		status;
	uint8_t		format;
	uint8_t		length;
	uint32_t	id;
}CONSOLE_RESPONSE_INFO, *PCONSOLE_RESPONSE_INFO;

extern uint8_t mainBoardID;

#endif
