#pragma once

enum class MESSAGES_ID : unsigned char
{
  SysInfo = 1,
  GyroInfo = 2,
  AccelInfo = 3,
  GpsInfo = 4,
  InertialInfo = 5,
  BatteryInfo = 6,
  Ack = 7,
  StatusCommand = 8,
  StatusAllCommands = 9,
  Command = 10,
  RequestReg = 11,
  ResponseReg = 12,
  Auth = 13,
  OpenKey = 14,
  MissionCount = 15,
  MissionItem = 16,
  MissionItemAck = 17,
  MissionRequestItem = 18,
  RequestLastMessage = 19,
  ConnectionTest = 20,

  CountMenuCategories = 32,
  CategoriesMenu = 33,
  CategoriesParameters = 34,

	//Ping = 200, // :)
	//Pong = 201, // :]
};
enum class COMMANDS_NAME : unsigned char
{
  ChangeNav = 1,
  ChangeSpeed = 2,
  Land = 3,
  GoHome = 4,
  StopEngine = 5,
  StartEngine = 6,
  Pause = 7,
  Continue = 8,
  GoToGlobal = 9,
  GoToLocal = 10,
  
  SetParameter = 15,
};
enum class ERROR_CODE_COMMAND : unsigned char
{
  No_error = 0,
  Speed_has_already_been_set = 1,
  Engines_are_already_running = 2,
  Engines_have_already_been_stopped = 3,
  UAV_is_already_on_the_ground = 4,
  UAV_is_already_landing = 5,
  UAV_is_already_moving = 6,
  Mission_has_not_been_launched = 7,
  Mission_is_already_on_pause = 8,
  Mission_was_not_on_pause = 9,
  Mission_was_not_launched = 10,
  This_NavSys_has_already_been_applied = 11,
  Lost_connection_with_controller = 12,
  The_command_is_canceled_when_a_new_one_is_received = 13,
  Error_reading_the_mission_file = 14,
  Error_validating_the_mission_file = 15
};
enum class ENGINE_STATUS : unsigned char
{
  disable = 0,
  enable = 1
};

enum class STATUS_MODE : unsigned char
{
  on_ground = 1,
  taking_off = 2,
  fly = 3,
  landing = 4
};

enum class SYS_MODE : unsigned char
{
  take_off = 1,
  land = 2,
  go_home = 3,
  auto_mode = 4,
  hold = 5
};

enum class NAV_SYS_MODE : unsigned char
{
  inertial = 1,
  gps = 2,
  Optical_flow = 3
};

enum class EXEC_CODE_COMMAND : unsigned char
{
  error = 0,
  in_progress = 1,
  completed = 2
};

enum class FEASIBILITY_CODE_COMMAND : unsigned char
{
  cannot_be_performed = 0,
  accepted = 1
};

enum class DATA_TYPES : unsigned char
{
  dt_char = 0,
  dt_unsigned_char = 1,
  dt_string = 2,
  dt_short = 3,
  dt_bool = 4,
  dt_unsigned_short = 5,
  dt_int = 6,
  dt_unsigned_int = 7,
  dt_long = 8,
  dt_unsigned_long = 9,
  dt_long_long = 10,
  dt_unsigned_long_long = 11,
  dt_float_32 = 12,
  dt_float_64 = 13,
  dt_unknown = 255
};

#pragma pack(push,1)

const unsigned short MaxLength = 256;
const unsigned char Global_stx = 0xAA;
//?????? ?????????
struct HeaderBegin
{
  unsigned char stx;
  unsigned short len1; //Len header + payload (message) 
  unsigned short len2; //Len header + payload (message)
  unsigned char crc; // CRC MESSAGE
  unsigned char data[0]; // Message
  //---
  bool CheckCRC();
  void SetCRC();
};
// ????????? ?????????
struct HeaderMessages
{
  MESSAGES_ID msgId;
  unsigned char srcId;
  unsigned char dstId;
  unsigned char len;
  unsigned long long timeusec;
  unsigned char data[0];
};
struct CommandObj
{
	COMMANDS_NAME commandId;
	unsigned char data[0];
};
//???????? ???????? ????????? ? ??????? ?? ???
struct PayloadBatteryInfo
{
  unsigned char perCharge;
  float voltage;
  float amperage;
  float temp;
  float totalPower;
  unsigned long long timeRemaining;
};
// ????????? ? ??????? ?? ??????? ??????
struct EnginePowerInfo
{
	unsigned char power; //??????? ???????? ?? ????? ?? 0 ?? 100%
	unsigned int engineSpeed;//??????? ?????? ? ??????? ??????
	float current; // ??? ???????????? ???????
	float voltage; // ?????????? ?? ??????
	float temp; // ??????????? ??????

};
struct PayloadSysInfo // ???????? ???????? ? ??????? ? ???????? ????
{
	SYS_MODE mode; //??????? ????? ?????? (????? - 1, ??????? - 2, ????? ??????????????? ???????? ????? - 3, ????? ??????????? ?????? - 4, ???????? - 5)
	STATUS_MODE statusMode; //??????? ?????? ????? (?? ????? (on ground) - 1, ????? ?????? (TakingOff) - 2, ????? (In air) - 3, ??????????? (Landing) - 4)
	NAV_SYS_MODE navSys; // ??????? ???????????? ??????? ????????? (GPS\???\Optical Flow) ??? - 1, GPS - 2, Optical Flow - 3
	ENGINE_STATUS engineStatus; // ??????? ????????? ???????. ???? - 0, ??? - 1.
	unsigned char engineCount; // ?????????? ?????????? ?? ????
	float pressureBaro; //??????????? ????????
	float tempBaro; // ??????????? ???. ?????
	EnginePowerInfo enginePower[0];
};
struct PayloadGyroInfo // ???????? ???????? ? ??????? ?? ?????????
{
	float yawGyroVel; // ??????? ???????? ????????? ???? ???????? (? ??? ????? ??????????? ???\??)
	float pitchGyroVel; // ??????? ???????? ????????? ???? ??????? (? ??? ????? ??????????? ???\??)
	float rollGyroVel; // ??????? ???????? ????????? ???? ?????  (? ??? ????? ??????????? ???\??)
};
struct PayloadAccelInfo // ???????? ???????? ????????? ? ??????? ?? ?????????????
{
	float yawAccelVel; // ??????? ????????? ????????? ???? ???????? (Z)
	float pitchAccelVel; // ??????? ????????? ????????? ???? ??????? (Y)
	float rollAccelVel; // ??????? ????????? ????????? ???? ????? (X)
	float aX; // ????????? ?? X (?\?^2)
	float aY; // ????????? ?? ? (?\?^2)
	float aZ; // ????????? ?? Z (?\?^2)
	float tempAccel; //??????????? ?????????????
};
struct PayloadGpsInfo // ???????? ???????? ????????? ? ??????? ?? GPS ???????
{
	float lat; //
	float lon; //
	float absAlt; //
	float realAlt; //
	float hdop; //
	float vdop; //
	float pdop; //
	float noise; //
	float jamming; //
	unsigned char satVisible;
	unsigned char satUsed; //
	float speed; // 
	unsigned char fixType; // NO_GPS - 0, NO_FIX - 1, 2D_FIX - 2, 3D_FIX - 3, DGPS - 4, RTK_FLOAT - 5, RTK_FIXED - 6, STATIC - 7, PPP - 8
	unsigned long long timeUTC; // UTC - GPS
};
struct PayloadInertialInfo // ???????? ???????? ? ??????? ?? ???????????? ??????? ?????????
{
	float x; // ?????????? X (?? ????????? ??????? ?????????)
	float y; // ?????????? Y (?? ????????? ??????? ?????????)
	float z; // ?????????? ?????? (?? ????????? ??????? ?????????)
	float headingDeg; // ??????? ??????????? ???????? ???? ???????????? ??????
	float speed; // ???????? ???? (?????????? ?? ?? GPS)
	float roll; // ??????? ???? ????? ? ????????
	float pitch; // ??????? ???? ??????? ? ????????
	float yaw; // ??????? ???? ???????? ? ????????
	float rollVel; // ???????? ?? ??????????? ????? ?\? (? ??????????? ??? Y)
	float pitchVel; // ???????? ?? ??????????? ??????? ?\? (? ??????????? ??? ?)
	float yawVel; // ???????? ?? ??????????? ???????? ?\? (? ??????????? ??? Z)
	float baroAlt; // ?????? ? ?????? ?? ?????????
};
//???????? ???????? ????????? ? ??????? ? ????????? ???????
struct PayloadCommandAck
{
	COMMANDS_NAME commandId;
	FEASIBILITY_CODE_COMMAND status; // 0 - ?? ????? ???? ????????? (??????????? ??? ??????), 1 - ???????
	ERROR_CODE_COMMAND errorCode;
};
struct PayloadStatusCommand
{
	COMMANDS_NAME commandId;
	EXEC_CODE_COMMAND executionCode; // 0 - ?????? ??? ?????????? ,1 - ??????????? ,2 - ????????? ???????.
	ERROR_CODE_COMMAND errorCode;
};
// ???????? ???????? ??????? ??? ????? ???????? ??????
struct PayloadCommandChangeSpeed
{
	float speed;
};
// ???????? ???????? ??????? ??? ????? ??????? ???. ???. ???????.
struct PayloadCommandChangeNav
{
	unsigned char nav_id; // Inertial - 1, GPS = 2, Oprical_Flow = 3
};
// ???????? ???????? ??????? ??? ??????????? ? ????? ?? ?????????? ???????????
struct PayloadCommandGoToGlobal
{
	float lat;
	float lon;
	float abs_alt;
	float speed;
};
// ???????? ???????? ??????? ??? ??????????? ? ????? ?? ????????? ???????????
struct PayloadCommandGoToLocal
{
	float x;
	float y;
	float z;
	float speed;
};

struct PayloadCountMenuCategories
{
	unsigned char countCategories;
	unsigned char data[0];
};

struct MenuCategori
{
	unsigned char id;
	unsigned char len_name;
	unsigned char count_param;
	char name[0];
};

struct PayloadMenuCategories
{
	unsigned char countCategories;
	unsigned char categories[0];
};

struct PayloadCategoriesParameters
{
	unsigned char cat_id;
	unsigned char param_id;
	unsigned char param_name_len;
	unsigned char param_len;
	DATA_TYPES param_type_code;
	char param_data[0];
};

struct PayloadCommandSetParameter
{
	unsigned char cat_id;
	unsigned char param_id;
	DATA_TYPES param_type_code;
	unsigned char param_len;
	char param_data[0];
};

#pragma pack(pop)
