using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Plane.Protocols
{
    /*
     * 
     * 通信模块协议
     * 
     */
    public class MavComm
    {
        /// <summary>
        /// 发送数据时的数据包头
        /// </summary>
        public const ushort COMM_SEND_PACKET_HEADER = 0x9551;
        /// <summary>
        /// 接受数据时的数据包头
        /// </summary>
        public const ushort COMM_RECEIVE_PACKET_HEADER = 0x7713;


        #region 命令类型 2字节
        //-----------------命令类型-----------------
        /// <summary>
        /// 查询状态
        /// </summary>
        public const short COMM_QUERY_STATUS = 0x00;

        /// <summary>
        /// 变更飞机总数及参与者
        /// </summary>
        public const short COMM_SET_MAV_COUNT = 0x10;

        /// <summary>
        /// 获取飞机详细信息
        /// </summary>
        public const short COMM_GET_COPTERS_COUNT = 0x20;

        /// <summary>
        /// 时间同步，不改变当前模式
        /// </summary>
        public const short COMM_ASYN_TIME = 0x30;

        /// <summary>
        /// 进入空闲模式
        /// </summary>
        public const short COMM_IDLE_MODE = 0x50;

        /// <summary>
        /// 进入搜索模式，命名编号targetID  memcpy(pdata,input,2);
        /// </summary>
        public const short COMM_SEARCH_MODE = 0x51;

        /// <summary>
        /// 进入航点下载模式 (写航点)
        /// </summary>
        public const short COMM_DOWNLOAD_MODE = 0x52;

        /// <summary>
        /// 下载模式通信
        /// </summary>
        public const short COMM_DOWNLOAD_COMM = 0x53;

        /// <summary>
        /// 进入飞行模式（无负载）
        /// </summary>
        public const short COMM_FLIGHT_MODE = 0x54;

        /// <summary>
        /// 进入飞行模式（有负载数据）
        /// </summary>
        public const short COMM_FLIGHT_LOAD_MODE = 0x55;

        /// <summary>
        /// 通信模块
        /// </summary>
        public const short COMM_NOTIFICATION = 0x1234;

        /// <summary>
        /// 空中升级(更新通信模块飞机端)
        /// </summary>
        public const short COMM_UPDATE_COPTER_MODULE = 0xFD;



        /// <summary>
        /// 测试模块
        /// </summary>
        public const short COMM_TEST_MODULE = 0x3C;



        #endregion 

        public enum CommMode
        {
            IDLE = 0,
            SEARCH = 1,
            DOWNLOAD = 3,
            FLIGHT = 4
        }

        public enum MessageType
        {
            STR = 0,
            SCAN2 = 2,
            SCAN3 = 3
        }

        //search 搜索模式相关
        public const ushort SEARCH_COMPLETED = 0x0502;                  //搜索模式 扫描成功
        public const ushort FREQUENCY_HOPPING_COMPLETED = 0x0503;       //搜索模式 跳频表更新成功
        public const ushort SEARCH_FINDCOPTER = 0x0401;                 //搜索模式 找到指定目标
        public const ushort FREQUENCY_HOPPING_ERROR = 0x0100;           //搜索模式 跳频表更新错误
        public const ushort SEARCH_OUTTIME = 0xFFFF;                    //搜索模式 没有人应答   超时时间20S


        public const ushort MISSION_SEND_COMPLETED = 0x0503;            //航点传输到飞控成功，可开始传输下个飞机航点信息, 
                                                                        //需要再后续等待一个成功消息，才算完成   

        public const ushort P2P_SEND_FAILED = 0x0600;                   //点对点通信发送失败 

        public const ushort ERROR_CODE_START = 0x0100;
        public const ushort ERROR_CODE_END = 0x03FF;


        [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 2)]
        public struct comm_set_mav_count
        {
            public Int16 mav_count;    //飞机总数
        };

        [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 4)]
        public struct comm_update_copter_module
        {
            public Int16 mav_count;    //飞机总数
            public Int16 update_code; 
        };


        [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 8)]
        public struct comm_asyn_time
        {
            public Int64 time_stamp;
        };

        [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 28)]
        public struct comm_copter_info
        {
            public Int32 control_mode;
            public UInt32 lat;
            public UInt32 lng;
            public float retain;
            public Int32 alt;

            public Int16 gps_status;
            
            public byte lock_status;
            public byte gps_num_sats;
            public Int16 battery_voltage;
            public UInt16 heading;

        };

    }
}
