Subversion Repositories group.electronics

Rev

Rev 145 | Rev 155 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.DeviceNotify;


namespace NITNavComm {

    public class Device {
        public int vid { get; set; }
        public int pid { get; set; }
        public string type { get; set; }

        public Device(string type, int vid, int pid) {
            this.type = type;
            this.vid = vid;
            this.pid = pid;
        }

    }

    public class NITDevice : Device {
        public int id { get; set; }
        public string hw { get; set; }
        public string sw { get; set; }
        public string status { get; set; }
        public string serial { get; set; }

        protected UsbDevice usbDevice;
        public UsbRegistry usbRegistry { get; set; }

        public NITDevice(UsbRegistry usbRegistry, string type, int vid, int pid) : base(type, vid, pid) {
            this.id = -1;
            this.hw = "";
            this.sw = "";
            this.serial = "";
            this.status = "";
            this.usbRegistry = usbRegistry;
            
        }

        public bool Open() {
            if (this.usbRegistry == null || !this.usbRegistry.IsAlive)
                return false;

            if (this.usbDevice == null)
                this.usbRegistry.Open(out this.usbDevice);
            else
                this.usbDevice.Open();
                
            return true;
        }

        public bool Close() {
            if (this.usbDevice == null)
                return false;

            if (this.usbDevice.IsOpen)
                this.usbDevice.Close();

            return true;
        }

        public bool isOpen() {
            if (this.usbDevice == null || ! usbDevice.IsOpen)
                return false;

            return true;
        }

        public bool SendCommand(byte command, short value, short index) {
            int transferred;
            byte[] buffer = new byte[8];
            return SendCommand(command, value, index, buffer, out transferred);
        }

        public bool SendCommand(byte command, short value, short index, object buffer, out int transferred) {
            if (!this.isOpen())
                throw new Exception("USB Device " + this.type + "(" + this.serial + ") not open/connected");

            UsbSetupPacket packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor 
                | (byte)UsbEndpointDirection.EndpointIn, command, value, index, 0);

            return this.usbDevice.ControlTransfer(ref packet, buffer, 8, out transferred);
        }

        public bool GetDeviceVersion() {
            byte[] data = new byte[8];
            int transferred;
            bool success = this.SendCommand(01, 0, 0, data, out transferred);
            this.hw = data[0].ToString();
            this.sw = data[1].ToString();
            return success;
        }

    }

    class NITPanels {

        public List<NITDevice> devices { get; set; }

        public NITPanels() {
            devices = new List<NITDevice>();
        }

        public int UsbScan() {

            List<Device> regDevices = new List<Device>();
            regDevices.Add(new Device("NITCommNav", 0x20a0, 0x4236));
            regDevices.Add(new Device("NITAudioSel", 0x20a0, 0x4237));
            regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));

            int idCount = 0;
            devices = new List<NITDevice>();

            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
            foreach (UsbRegistry usbReg in allDevices) {
                foreach (Device device in regDevices) {
                    if (usbReg.Vid == device.vid 
                        && usbReg.Pid == device.pid) {
                            NITDevice found = null;

                            switch (device.type) {
                                case ("NITCommNav") : {
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
                                    break;
                                }
                            }

                            found.id = idCount;
                            found.status = "Found";
                            
                            found.usbRegistry = usbReg;
                            UsbDevice dev; 
                            if (usbReg.Open(out dev)) {
                                found.status = "Available";
                                dev.Close();
                            }

                            if (found.Open()) {
                                found.status = "Connected";
                                found.GetDeviceVersion();
                            }  else
                                found.status = "Failed";
                            
                            idCount++;
                            devices.Add(found);
                    }
                }
            }

            return idCount;

        }

    }
}