/*********************************************************************************
 *
 *  lora_aprs_node_pico is a LoRa APRS KISS modem with additional PE1RXF telemetry
 *  capabilities. It runs on a Raspberry Pi Pico.
 *
 * (C)2023 M.T. Konstapel https://meezenest.nl/mees
 *
 * This file is part of lora_aprs_node_pico.
 *
 *    lora_aprs_node_pico 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.
 *
 *    lora_aprs_node_pico 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 lora_aprs_node_pico. If not, see <https://www.gnu.org/licenses/>.
 *
 **********************************************************************************/
#ifndef KISS_H
  #define KISS_H
  #include <stdio.h>
  #include <string.h>
  #include <time.h>
  #include <stdarg.h>

  #define FEND              0xC0
  #define FESC              0xDB
  #define TFEND             0xDC
  #define TFESC             0xDD

  #define CMD_UNKNOWN       0xFE
  #define CMD_DATA          0x00
  #define CMD_HARDWARE      0x06
  #define CMD_EXIT_KISS     0xFF

  #define HW_RSSI           0x21
  
  #define CMD_ERROR         0x90
  #define ERROR_INITRADIO   0x01
  #define ERROR_TXFAILED    0x02
  #define ERROR_QUEUE_FULL  0x04

    struct aprs_frame {
        uint8_t source_address[10];
        uint8_t digi_path[255];
        uint8_t data_field[255];
        uint8_t message[255];
        uint8_t digis[10][10];
        uint8_t acknowledge_number[255];
        bool acknowledge_request = false;
        uint16_t server_command = 0;
        uint16_t number_of_digipeaters = 0;
        
        uint8_t valid_apsr_data = false;
    };

    struct ax25_frame {
        uint8_t complete[512];
        uint16_t lenght = 0;
        uint8_t encoded_kiss_frame[512];
        uint8_t decoded_kiss_frame[512];
        uint16_t kiss_length = 0;
    };
    
    struct kiss_tx_frame {
        uint8_t source_address[10];
        uint8_t data_field[512];
        uint8_t digis[10][10];
        uint16_t number_of_digipeaters = 0;
        
        uint8_t lora_string[512];
        
        uint8_t valid_data = false;
    };

  class KissClass
  {
  public:
      uint16_t EncodeFrame(struct aprs_frame *frame, struct ax25_frame *ax25frame);
      uint16_t DecodeFrame(uint8_t string[], struct kiss_tx_frame *kisstxframe);
  private:
      uint8_t * EncodeCall(uint8_t string[]);
  };

#endif
