/*

  Zerocat Chipflasher--- Flash free firmware, kick the Management Engine.

  Copyright (C) 2015, 2016  kai <kmx@posteo.net>
  Copyright (C) 2016, 2017, 2018, 2020, 2021, 2022, 2026  Kai Mertens <kmx@posteo.net>

  This file is part of Zerocat Chipflasher.

  Zerocat Chipflasher is free software: you can redistribute it and/or
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  Zerocat Chipflasher is distributed in the hope that it will be
  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Zerocat Chipflasher.
  If not, see <http://www.gnu.org/licenses/>.


***/


# ifndef __PROTOCOL_H__
#   define __PROTOCOL_H__


//  Serial Control Codes
//  ====================


// "\0"
#   define NUL              0x00

// Ascii 'start of header'
#   define SOH              0x01

// Ascii 'start of text'.
#   define STX              0x02

// Ascii 'end of text'.
#   define ETX              0x03

// Ascii 'end of transmission'
#   define EOT              0x04

// Ascii 'enquiry'
#   define ENQ              0x05

// Ascii 'acknowledge'
#   define ACK              0x06

// Ascii LF "\n" 'line feed'
#   define NEW_LINE         0x0a

// Ascii CR "\r" 'carriage return'
#   define CARR_RET         0x0d

// Ascii 'neg. acknowledge'
#   define NAK              0x15

// Ascii 'synchronous idle'
#   define SYN              0x16

// Ascii 'end of block transmission'
#   define ETB              0x17

// Ascii 'cancel'
#   define CAN              0x18

// Ascii 'end of medium'
#   define EM               0x19

// Ascii 'substitute'
#   define SUB              0x1a

// Ascii 'escape'
#   define ESC              0x1b

// Ascii 'file separator'
#   define FS               0x1c

// Ascii 'group separator'
#   define GS               0x1d

// Ascii 'record separator'
#   define RS               0x1e

// Ascii 'unit separator'
#   define US               0x1f


//  Line Ending Characters
//  ======================


// Used by outfifo() to send the CARR_RET character
// – or a different, printable character, to ease debugging!
#   define CLIM_CR          CARR_RET

// Used by outfifo() to send the NEW_LINE character
// – or a different, printable character, to ease debugging!
#   define CLIM_NL          NEW_LINE

// Function HEXD_rxline uses CLIM_HEXD for detection of line ending
// and returns that character in its feedback loop.
#   define CLIM_HEXD        CLIM_CR


//  Parallax Terminal Exit Sequence
//  ===============================
//
//  Exit sequence: EXIT_CHAR_A, EXIT_CHAR_B, status byte
//

// First char of Parallax’ exit sequence.
#   define EXIT_CHAR_A      0xff

// Second char of Parallax’ exit sequence.
#   define EXIT_CHAR_B      0x00


//  Obsolete Single Command Bytes for connect Utility
//  =================================================


//! @ingroup SERIAL_CODES
//! @brief Start of input, enable stdin on host.
#   define INPUT_START              RS

//! @ingroup SERIAL_CODES
//! @brief End of input, disable stdin on host.
#   define INPUT_STOP               US


//  Header Command Bytes for connect Utility
//  ========================================
//
//  Header command sequence: SOH, command byte, command argument byte (zero if not used)
//

// Enable stdin on host
#   define STDIN_ENABLE             0x10      // no argument byte

// Disable stdin on host
#   define STDIN_DISABLE            0x11      // no argument byte

// Send data to terminal and save on disk.
#   define CHIP_TO_FILE             0x20      // one argument byte: line type

// Send data to Propeller and write to chip.
#   define FILE_TO_CHIP             0x22      // one argument byte: line type

// OTP Array transmissions
#   define SECARR0_TO_FILE          0x24      // start Security Array 0 data transmission to host (followed by line type)
#   define FILE_TO_SECARR0          0x26
#   define SECARR1_TO_FILE          0x28      // start Security Array 1 data transmission to host (followed by line type)
#   define FILE_TO_SECARR1          0x2a
#   define SECARR2_TO_FILE          0x2c      // start Security Array 2 data transmission to host (followed by line type)
#   define FILE_TO_SECARR2          0x2e
#   define SECARR3_TO_FILE          0x30      // start Security Array 3 data transmission to host (followed by line type)
#   define FILE_TO_SECARR3          0x32

// Display a byte (debugging feature)
#   define DEBUG_BYTE0              0x40      // one argument byte

// Display a byte (follow-up, debugging feature)
#   define DEBUG_BYTEN              0x41      // one argument byte

// Set Line Visibility
#   define SET_LINE_VISIBILITY      0x50      // one argument byte: $00 = off, $01 = on

// Set byte timing
#   define SET_USEC_BYTE            0x60      // one argument byte

// Quit connect and say good bye
#   define GOOD_BYE                 0x70      // one argument byte: status

// Start handshake between kick-connect and connect
#   define THIS_IS_KICK_CONNECT     0x80      // no argument byte

// Start handshake between kick2-connect and connect
#   define THIS_IS_KICK2_CONNECT    0x90      // no argument byte

// Tell connect about current menu level (aka job level)
#   define REPORT_MENU_LEVEL        0xa0      // one argument byte

// Tell connect about current SPI power status
#   define REPORT_SPI_POWER         0xa1      // one argument byte

// Tell connect about job key id in progress, followed by key id byte
#   define REPORT_MENU_KEYID        0xa2      // one argument byte

// start SFDP Array data transmission to host (followed by line type)
#   define SFDP_TO_FILE             0xb0      // one argument byte


//  Timings
//  =======

// number of seconds for watchdog line timeout during file to chip action
#   define SEC_WATCHDOG             3


//  Error Types
//  ===========


enum ERRCODE_t {
  ERRC__SUCCESS = 0,
  ERRC__NO_SREC,                  //file buffer flush
  ERRC__NO_HEX_DIGIT,             //file buffer flush
  ERRC__LINE_ENDING_ERROR,        //file buffer flush
  ERRC__LINE_COUNT_MISMATCH,      //file buffer flush
  ERRC__LINE_TOO_LONG,            //file buffer flush
  ERRC__BUFFER_OVERRUN,           //file buffer flush
  ERRC__HEXD_PARSE_ERROR,         //file buffer flush
  ERRC__NO_SUCH_FILE,             //file buffer flush
  ERRC__JOB_CANCELLATION,         //file buffer flush, used for comparison
  ERRC__PORT_OPEN_FAILURE,
  ERRC__INVALID_ADDRESS,
  ERRC__DEBUG,
  ERRC__CHECKSUM_MISMATCH,        //warning and error, used for comparison
  ERRC__LINE_LENGTH_MISMATCH,     //warning and error
  ERRC__WRONG_CHARACTER,          //warning and error
  ERRC__LINE_TIMEOUT,             //warning and error
  ERRC__POWER_FAILURE,            //warning and error
  ERRC__FATAL                     //fatal error
};


# endif   // __PROTOCOL_H__

