Esempio n. 1
0
        public bool Connect()
        {
            bool isConnect = false;

            Console.WriteLine("usb:connect");

            if ((bt_vid == -1) || (bt_pid == -1))
            {
                throw new Exception("USBDevice identification data not set");
            }

            controlTransferDelegate = ControlTransferCB;

            sessionHandle = new MonoUsbSessionHandle();
            if (sessionHandle.IsInvalid)
            {
                throw new Exception(String.Format("failed init libusb contenxt {0}:{1}", MonoUsbSessionHandle.LastErrorCode,
                                                  MonoUsbSessionHandle.LastErrorString));
            }

            profileList       = new MonoUsbProfileList();
            MonoUsbDeviceList = MonoUsbDevice.MonoUsbDeviceList;

            MyUsbDeviceArray = new UsbDevice[MAX_USB_DEVICE_COUNT];
            UsbDeviceCount   = 0;

            DiscoveryUsbDevice(true);
            SetConfigUsbDevice();

            return(isConnect);
        }
Esempio n. 2
0
        static void config_op(MonoUsbDeviceHandle handle, byte endpoint, byte[] data, int readcnt)
        {
            int actual_length = 0;
            int res;

            byte[] recv_buf = new byte[1024];

            GCHandle data_gc     = GCHandle.Alloc(data, GCHandleType.Pinned);
            GCHandle recv_buf_gc = GCHandle.Alloc(recv_buf, GCHandleType.Pinned);

            MonoUsbTransferDelegate d = noop_usb_callback;

            MonoUsbTransfer transfer = new MonoUsbTransfer(0);

            wait = 0;

            Console.WriteLine("data_gc addr = {0}", data_gc.AddrOfPinnedObject());
            Console.WriteLine("recv_buf_gc addr = {0}", recv_buf_gc.AddrOfPinnedObject());

            // Execute the write operation (asynchronous).
            transfer.FillBulk(handle, endpoint, data_gc.AddrOfPinnedObject(), data.Length, d, recv_buf_gc.AddrOfPinnedObject(), 4000);
            transfer.Submit();

            Thread.Sleep(300);

            // Execute the specified number of read operations (synchronous).
            for (int x = 0; x < readcnt; ++x)
            {
                res = Usb.BulkTransfer(handle, (byte)(endpoint | 0x80), recv_buf_gc.AddrOfPinnedObject(), recv_buf.Length, out actual_length, 4000);
                if (res != 0)
                {
                    throw new Exception("config_op Usb.BulkTransfer failure");
                }
                // Should only be here once the above transfer completes.
            }

            // Wait for the first write asynchronous to return.
            // Do not poll forever. Abort if it takes too long.
            var st = DateTime.Now;

            if (readcnt > 0)
            {
                while (wait == 0 && (DateTime.Now - st).TotalSeconds < 30)
                {
                    Thread.Sleep(100);
                }
            }

            data_gc.Free();
            recv_buf_gc.Free();
        }
Esempio n. 3
0
        public void Initilise()
        {
            // Assign the control transfer delegate to the callback function.
            controlTransferDelegate = ControlTransferCB;

            // Initialize the context.
            sessionHandle = new MonoUsbSessionHandle();

            if (sessionHandle.IsInvalid)
            {
                throw new Exception(String.Format("Failed intializing libusb context.\n{0}:{1}",
                                                  MonoUsbSessionHandle.LastErrorCode,
                                                  MonoUsbSessionHandle.LastErrorString));
            }
        }
Esempio n. 4
0
        ////////////////////////////////////////////////////////////////////////
        // Methods
        ////////////////////////////////////////////////////////////////////////

        bool initialize()
        {
            if (initialized)
            {
                return(initialized);
            }

            monoUsbSession = new MonoUsbSessionHandle();
            if (monoUsbSession.IsInvalid)
            {
                logger.error("init: Failed to initialize context");
                return(false);
            }
            MonoUsbApi.SetDebug(monoUsbSession, 0);

            controlTransferDelegate = ControlTransferCallback;

            findWasatchProfiles();

            initialized = true;
            return(initialized);
        }
        public static void ShowConfig(RichTextBox rtb)
        {
            // Assign the control transfer delegate to the callback function.
            controlTransferDelegate = ControlTransferCB;

            // Initialize the context.
            sessionHandle = new MonoUsbSessionHandle();
            if (sessionHandle.IsInvalid)
            {
                throw new Exception(String.Format("Failed intializing libusb context.\n{0}:{1}",
                                                  MonoUsbSessionHandle.LastErrorCode,
                                                  MonoUsbSessionHandle.LastErrorString));
            }

            MonoUsbProfileList  profileList    = new MonoUsbProfileList();
            MonoUsbDeviceHandle myDeviceHandle = null;

            try
            {
                // The list is initially empty.
                // Each time refresh is called the list contents are updated.
                profileList.Refresh(sessionHandle);

                // Use the GetList() method to get a generic List of MonoUsbProfiles
                // Find the first profile that matches in MyVidPidPredicate.
                MonoUsbProfile myProfile = profileList.GetList().Find(MyVidPidPredicate);
                if (myProfile == null)
                {
                    rtb.AppendText("Device not connected.");
                    return;
                }

                // Open the device handle to perform I/O
                myDeviceHandle = myProfile.OpenDeviceHandle();
                if (myDeviceHandle.IsInvalid)
                {
                    throw new Exception(String.Format("Failed opening device handle.\n{0}:{1}",
                                                      MonoUsbDeviceHandle.LastErrorCode,
                                                      MonoUsbDeviceHandle.LastErrorString));
                }
                int          ret;
                MonoUsbError e;

                // Set Configuration
                e = (MonoUsbError)(ret = MonoUsbApi.SetConfiguration(myDeviceHandle, 1));
                if (ret < 0)
                {
                    throw new Exception(String.Format("Failed SetConfiguration.\n{0}:{1}", e, MonoUsbApi.StrError(e)));
                }

                // Claim Interface
                e = (MonoUsbError)(ret = MonoUsbApi.ClaimInterface(myDeviceHandle, 0));
                if (ret < 0)
                {
                    throw new Exception(String.Format("Failed ClaimInterface.\n{0}:{1}", e, MonoUsbApi.StrError(e)));
                }

                // Create a vendor specific control setup, allocate 1 byte for return control data.
                byte requestType = (byte)(UsbCtrlFlags.Direction_In | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.RequestType_Vendor);
                byte request     = 0x0F;
                MonoUsbControlSetupHandle controlSetupHandle = new MonoUsbControlSetupHandle(requestType, request, 0, 0, 1);

                // Transfer the control setup packet
                ret = libusb_control_transfer(myDeviceHandle, controlSetupHandle, 1000);
                if (ret > 0)
                {
                    rtb.AppendText("\nSuccess!\n");
                    byte[] ctrlDataBytes  = controlSetupHandle.ControlSetup.GetData(ret);
                    string ctrlDataString = Helper.HexString(ctrlDataBytes, String.Empty, "h ");
                    rtb.AppendText(string.Format("Return Length: {0}", ret));
                    rtb.AppendText(string.Format("DATA (hex)   : [ {0} ]\n", ctrlDataString.Trim()));
                }
                MonoUsbApi.ReleaseInterface(myDeviceHandle, 0);
            }
            finally
            {
                profileList.Close();
                if (myDeviceHandle != null)
                {
                    myDeviceHandle.Close();
                }
                sessionHandle.Close();
            }
        }
Esempio n. 6
0
        private static void Main(string[] args)
        {
            // Assign the control transfer delegate to the callback function.
            controlTransferDelegate = ControlTransferCB;

            // Initialize the context.
            sessionHandle = new MonoUsbSessionHandle();
            if (sessionHandle.IsInvalid)
                throw new Exception(String.Format("Failed intializing libusb context.\n{0}:{1}",
                                                  MonoUsbSessionHandle.LastErrorCode,
                                                  MonoUsbSessionHandle.LastErrorString));

            MonoUsbProfileList profileList = new MonoUsbProfileList();
            MonoUsbDeviceHandle myDeviceHandle = null;

            try
            {
                // The list is initially empty.
                // Each time refresh is called the list contents are updated.
                profileList.Refresh(sessionHandle);

                // Use the GetList() method to get a generic List of MonoUsbProfiles
                // Find the first profile that matches in MyVidPidPredicate.
                MonoUsbProfile myProfile = profileList.GetList().Find(MyVidPidPredicate);
                if (myProfile == null)
                {
                    Console.WriteLine("Device not connected.");
                    return;
                }

                // Open the device handle to perform I/O
                myDeviceHandle = myProfile.OpenDeviceHandle();
                if (myDeviceHandle.IsInvalid)
                    throw new Exception(String.Format("Failed opening device handle.\n{0}:{1}",
                                                      MonoUsbDeviceHandle.LastErrorCode,
                                                      MonoUsbDeviceHandle.LastErrorString));
                int ret;
                MonoUsbError e;

                // Set Configuration
                e = (MonoUsbError) (ret = MonoUsbApi.SetConfiguration(myDeviceHandle, 1));
                if (ret < 0) throw new Exception(String.Format("Failed SetConfiguration.\n{0}:{1}", e, MonoUsbApi.StrError(e)));

                // Claim Interface
                e = (MonoUsbError) (ret = MonoUsbApi.ClaimInterface(myDeviceHandle, 0));
                if (ret < 0) throw new Exception(String.Format("Failed ClaimInterface.\n{0}:{1}", e, MonoUsbApi.StrError(e)));

                // Create a vendor specific control setup, allocate 1 byte for return control data.
                byte requestType = (byte)(UsbCtrlFlags.Direction_In | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.RequestType_Vendor);
                byte request = 0x0F;
                MonoUsbControlSetupHandle controlSetupHandle = new MonoUsbControlSetupHandle(requestType, request, 0, 0, 1);

                // Transfer the control setup packet
                ret = libusb_control_transfer(myDeviceHandle, controlSetupHandle, 1000);
                if (ret > 0)
                {
                    Console.WriteLine("\nSuccess!\n");
                    byte[] ctrlDataBytes = controlSetupHandle.ControlSetup.GetData(ret);
                    string ctrlDataString = Helper.HexString(ctrlDataBytes, String.Empty, "h ");
                    Console.WriteLine("Return Length: {0}", ret);
                    Console.WriteLine("DATA (hex)   : [ {0} ]\n", ctrlDataString.Trim());
                }
                MonoUsbApi.ReleaseInterface(myDeviceHandle, 0);
            }
            finally
            {
                profileList.Close();
                if (myDeviceHandle != null) myDeviceHandle.Close();
                sessionHandle.Close();
            }
        }
Esempio n. 7
0
        // This function originated from do_sync_bulk_transfer()
        // in sync.c of the Libusb-1.0 source code.
        private static MonoUsbError doBulkAsyncTransfer(MonoUsbDeviceHandle dev_handle,
                                                        byte endpoint,
                                                        byte[] buffer,
                                                        int length,
                                                        out int transferred,
                                                        int timeout)
        {
            transferred = 0;
            MonoUsbTransfer transfer = new MonoUsbTransfer(0);

            if (transfer.IsInvalid)
            {
                return(MonoUsbError.ErrorNoMem);
            }

            MonoUsbTransferDelegate monoUsbTransferCallbackDelegate = bulkTransferCB;

            int[]    userCompleted   = new int[] { 0 };
            GCHandle gcUserCompleted = GCHandle.Alloc(userCompleted, GCHandleType.Pinned);

            MonoUsbError e;
            GCHandle     gcBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            transfer.FillBulk(
                dev_handle,
                endpoint,
                gcBuffer.AddrOfPinnedObject(),
                length,
                monoUsbTransferCallbackDelegate,
                gcUserCompleted.AddrOfPinnedObject(),
                timeout);

            e = transfer.Submit();
            if ((int)e < 0)
            {
                transfer.Free();
                gcUserCompleted.Free();
                return(e);
            }
            int r;

            Console.WriteLine("Transfer Submitted..");
            while (userCompleted[0] == 0)
            {
                e = (MonoUsbError)(r = Usb.HandleEvents(sessionHandle));
                if (r < 0)
                {
                    if (e == MonoUsbError.ErrorInterrupted)
                    {
                        continue;
                    }
                    transfer.Cancel();
                    while (userCompleted[0] == 0)
                    {
                        if (Usb.HandleEvents(sessionHandle) < 0)
                        {
                            break;
                        }
                    }
                    transfer.Free();
                    gcUserCompleted.Free();
                    return(e);
                }
            }

            transferred = transfer.ActualLength;
            e           = MonoUsbApi.MonoLibUsbErrorFromTransferStatus(transfer.Status);
            transfer.Free();
            gcUserCompleted.Free();
            return(e);
        }