/**********************************************************************
Project:    Automatic cat feeder
Date:       march 19 2006
Author:     Jean-Fran�ois Martel
Target:     PIC 18F252
Compiler:   Microchip mcc18
Filename:	Protocol.h

File description:	Communication protocol implementation.


jean-francois.martel@polymtl.ca
**********************************************************************/


#ifndef BOOTLOADERPROTOCOL_H
#define	BOOTLOADERPROTOCOL_H

//Protocol buffer specific definitions

#define MAX_BOOTLOADER_PAYLOAD_SIZE 150
#define BOOTLOADER_FRAME_HEADER 0xDEADBEEF
#define BOOTLOADER_FRAME_HEADER_1 0xDE
#define BOOTLOADER_FRAME_HEADER_2 0xAD
#define BOOTLOADER_FRAME_HEADER_3 0xBE
#define BOOTLOADER_FRAME_HEADER_4 0xEF
#define PROTOCOL_INFO_DATA_SIZE 13          //Header + Cmd + Size + CRC = 13 bytes
//State Machine states
enum States
{
	Initialization,
	RxHeader1,
    RxHeader2,
    RxHeader3,
    RxHeader4,
    RxCmd,
	RxPayloadSize1,
    RxPayloadSize2,
    RxPayloadSize3,
    RxPayloadSize4,
	RxPayload,
	RxCRC1,
	RxCRC2,
	RxCRC3,
	RxCRC4		
};

enum eBootloaderProtocolDataTransferError
{
    BOOTLOADER_CHUNK_TRANSFER_SUCCESS = 1,
    BOOTLOADER_CHUNK_TRANSFER_ERROR_RESEND = 2,
    BOOTLOADER_CHUNK_TRANSFER_ERROR_FLASH_FAILURE = 3,
    BOOTLOADER_CHUNK_TRANSFER_ERROR_INVALID_CHUNK_INDEX = 4,
    BOOTLOADER_CHUNK_TRANSFER_ERROR_FLASH_ERROR = 5,
    
    BOOTLOADER_CHUNK_TRANSFER_MAX_ERROR
};

enum eBootloaderProtocolInitTransferError
{
    BOOTLOADEDR_INIT_TRANSFER_ERROR = 0,
    BOOTLOADEDR_INIT_TRANSFER_OK = 1,
    BOOTLOADEDR_INIT_TRANSFER_ERROR_FLASH_NOT_ERASED,
    
    BOOTLOADEDR_INIT_TRANSFER_MAX_ERROR
};

enum eBootloaderUploadResult
{
    BOOTLOADER_UPLOAD_FAILED_UNKNOWN_ERROR = 0,
    BOOTLOADER_UPLOAD_SUCCESS = 1,
    BOOTLOADER_UPLOAD_FAILED_FLASH_VERIFICATION_ERROR = 2,
    
    BOOTLOADER_UPLOAD_MAX_ERROR
};

enum eFlashCheckResult
{
    FLASH_CHECK_FAILED = 0,
    FLASH_CHECK_SUCCESS = 1,
        
    FLASH_CHECK_MAX_RESULT
};

//enum DEVICES_IDS
//{
//	ID_MASTER,		//Master Controller
//	ID_CONSOLE,		//LCD Console
//	ID_PC,			//PC
//	ID_AV_MUX,		//Audio Video Multiplexer
//	ID_IR_REMOTE,
//	ID_DEADBOLT
//};

//enum MESSAGE_IDS
//{
//	TX_NETWORK_ACK = 1,
//	RX_GET_STATUS,
//	TX_DEADBOLT_STATUS,
//
//	MAX_NETWORK_CMD
//};

//State machine states definition

void BootloaderProtocolInit(void);
void BootloaderProtocolStateMachine(unsigned char STATE);
void BootloaderProtocolResetStateMachine(void);
void BootloaderProtocolProtocolAnalyzeNewData(unsigned char *DataBuf, int size);
void BootloaderProtocolSendFrame(unsigned char Cmd, int size);
unsigned char *BootloaderProtocolGetDataBufferPtr();

void BootloaderProtocolSendHeartbeat();
void BootloaderProtocolSendACK(unsigned char Cmd);
void BootloaderProtocolSendNACK(unsigned char Cmd);
void BootloaderProtocolSendInitUploadResponse(char result);
void BootloaderProtocolSendDataChunkResult(char ErrorCode, int ChunkValue);
void BootloaderProtocolSendBootloaderState(char State);
void BootloaderProtocolSendFirmwareUploadResult(char Result);
void BootloaderProtocolSendFlashCheckResult(char Result);
void BootloaderProtocolSendStoredFirmwareInfoResponse(unsigned char* FirmwareData, int size);

#endif