/// <summary>
        /// Refreshes the <see cref="MonoUsbProfile"/> list.
        /// </summary>
        /// <remarks>
        /// <para>This is your entry point into finding a USB device to operate.</para>
        /// <para>This return value of this function indicates the number of devices in the resultant list.</para>
        /// <para>The <see cref="MonoUsbProfileList"/> has a crude form of built-in device notification that works on all platforms. By adding an event handler to the <see cref="AddRemoveEvent"/> changes in the device profile list are reported when <see cref="Refresh"/> is called.</para>
        /// </remarks>
        /// <param name="sessionHandle">A valid <see cref="MonoUsbSessionHandle"/>.</param>
        /// <returns>The number of devices in the outputted list, or <see cref="MonoUsbError.ErrorNoMem"/> on memory allocation failure.</returns>
        /// <example>
        /// <code source="..\MonoLibUsb\MonoUsb.ShowInfo\ShowInfo.cs" lang="cs"/>
        /// </example>
        public int Refresh(MonoUsbSessionHandle sessionHandle)
        {
            lock (LockProfileList)
            {
                MonoUsbProfileList       newList = new MonoUsbProfileList();
                MonoUsbProfileListHandle monoUSBProfileListHandle;

                int ret = MonoUsbApi.GetDeviceList(sessionHandle, out monoUSBProfileListHandle);
                if (ret < 0 || monoUSBProfileListHandle.IsInvalid)
                {
#if LIBUSBDOTNET
                    UsbError.Error(ErrorCode.MonoApiError, ret, "Refresh:GetDeviceList Failed", this);
#else
                    System.Diagnostics.Debug.Print("libusb_get_device_list failed:{0} {1}",
                                                   (MonoUsbError)ret,
                                                   MonoUsbApi.StrError((MonoUsbError)ret));
#endif
                    return(ret);
                }
                int stopCount = ret;
                foreach (MonoUsbProfileHandle deviceProfileHandle in monoUSBProfileListHandle)
                {
                    newList.mList.Add(new MonoUsbProfile(deviceProfileHandle));
                    stopCount--;
                    if (stopCount <= 0)
                    {
                        break;
                    }
                }
                syncWith(newList);
                monoUSBProfileListHandle.Close();

                return(ret);
            }
        }
        /// <summary>
        /// Refreshes the <see cref="MonoUsbProfile"/> list.
        /// </summary>
        /// <remarks>
        /// <para>This is your entry point into finding a USB device to operate.</para>
        /// <para>This return value of this function indicates the number of devices in the resultant list.</para>
        /// <para>The <see cref="MonoUsbProfileList"/> has a crude form of built-in device notification that works on all platforms. By adding an event handler to the <see cref="AddRemoveEvent"/> changes in the device profile list are reported when <see cref="Refresh"/> is called.</para>
        /// </remarks>
        /// <param name="sessionHandle">A valid <see cref="MonoUsbSessionHandle"/>.</param>
        /// <returns>The number of devices in the outputted list, or <see cref="MonoUsbError.ErrorNoMem"/> on memory allocation failure.</returns>
        /// <example>
        /// <code source="..\MonoLibUsb\MonoUsb.ShowInfo\ShowInfo.cs" lang="cs"/>
        /// </example>
        public int Refresh(MonoUsbSessionHandle sessionHandle)
        {
            lock (LockProfileList)
            {
                MonoUsbProfileList       newList = new MonoUsbProfileList();
                MonoUsbProfileListHandle monoUSBProfileListHandle;

                int ret = MonoUsbApi.GetDeviceList(sessionHandle, out monoUSBProfileListHandle);
                if (ret < 0 || monoUSBProfileListHandle.IsInvalid)
                {
                    MonoUsbErrorMessage.Error(ErrorCode.MonoApiError, ret, "Refresh:GetDeviceList Failed", this);
                    return(ret);
                }
                int stopCount = ret;
                foreach (MonoUsbProfileHandle deviceProfileHandle in monoUSBProfileListHandle)
                {
                    newList.mList.Add(new MonoUsbProfile(deviceProfileHandle));
                    stopCount--;
                    if (stopCount <= 0)
                    {
                        break;
                    }
                }
                syncWith(newList);
                monoUSBProfileListHandle.Dispose();

                return(ret);
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            MonoUsbSessionHandle sessionHandle = null;
            MonoUsbDeviceHandle  rapidradio    = null;

            try
            {
                if (args.Length == 1 && args[0] == "--help")
                {
                    Usage();
                    return;
                }

                var cmdParams = ParseCmdParams(args);

                // set up USB lib
                sessionHandle = new MonoUsbSessionHandle();
                if (sessionHandle.IsInvalid)
                {
                    throw new Exception("Invalid session handle.");
                }

                MonoUsbProfileListHandle list;
                MonoUsbApi.GetDeviceList(sessionHandle, out list);

                var profileList = new MonoUsbProfileList();
                profileList.Refresh(sessionHandle);
                var rapidradios = profileList.Where(d => d.DeviceDescriptor != null && d.DeviceDescriptor.VendorID == Vid && d.DeviceDescriptor.ProductID == Pid).ToArray();

                if (rapidradios.Length == 0)
                {
                    Error("rapidradio USB device not found");
                }

                if (rapidradios.Length > 1)
                {
                    // more than 1 rapidradio USB attached
                    Error(string.Format("Detected {0} rapidradio USB modules - currently only one device is supported.", rapidradios.Length), false);
                }

                rapidradio = rapidradios[0].OpenDeviceHandle();
                if (rapidradio == null || rapidradio.IsInvalid)
                {
                    throw new Exception("Invalid session handle.");
                }
                var r = MonoUsbApi.SetConfiguration(rapidradio, 1);
                if (r != 0)
                {
                    Error("SetConfiguration error: " + GetErrorMessage(r));
                }
                r = MonoUsbApi.ClaimInterface(rapidradio, 0);
                if (r != 0)
                {
                    Error("ClaimInterface error: " + GetErrorMessage(r));
                }

                _unmanagedReadBuff  = Marshal.AllocHGlobal(32);
                _unmanagedWriteBuff = Marshal.AllocHGlobal(32);

                // send configuration to endpoint 4
                var settings = cmdParams.Serialize();
                SendData(rapidradio, 4, settings, settings.Length, 200);

                // send custom register settings (if any)
                foreach (var regPacket in cmdParams.SerializeRfmRegisters())
                {
                    if (cmdParams.Verbose)
                    {
                        Info(string.Format("Setting register R{0}={1}", regPacket[1], regPacket[2]));
                    }

                    SendData(rapidradio, 4, regPacket, regPacket.Length);
                }

                var stopWatch = new Stopwatch();
                if (_stats)
                {
                    stopWatch.Start();
                }

                if (cmdParams.Verbose)
                {
                    Info(string.Format("Address = 0x{0}", cmdParams.Address.ToString("X8")));
                    Info(string.Format("Channel = {0}", cmdParams.Channel));
                    Info(string.Format("Mode = {0}", cmdParams.SinglePacket == null ? cmdParams.Mode.ToString() : "Single Packet Mode"));
                    Info(string.Format("ACK {0}", cmdParams.Ack ? "enabled" : "disabled"));
                    Info(string.Format("Retries = {0}", cmdParams.Retries));
                    if (cmdParams.SinglePacket == null)
                    {
                        Info(string.Format("Packet Numbering {0}", cmdParams.PacketNumbering ? "enabled" : "disabled"));
                    }
                }

                if (cmdParams.SinglePacket != null)
                {
                    // send exactly one packet
                    StartReadingThread(rapidradio);
                    SendData(rapidradio, 2, cmdParams.SinglePacket.ToArray(), cmdParams.SinglePacket.Count);
                    StopReadingThread();
                }
                else
                {
                    switch (cmdParams.Mode)
                    {
                    case RadioMode.Listening:
                        Listening(rapidradio, cmdParams);
                        break;

                    case RadioMode.Sending:
                        Sending(rapidradio, cmdParams);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("Unknown mode");
                    }
                }

                if (_stats)
                {
                    Info("Transmission took " + stopWatch.Elapsed);
                }

                // switch off the radio
                TurnOff(rapidradio);
            }
            catch (Exception e)
            {
                Error("An error occured: " + GetRecursiveMessage(e, 10));
            }
            finally
            {
                {
                    // Free and close resources
                    if (rapidradio != null)
                    {
                        if (!rapidradio.IsInvalid)
                        {
                            MonoUsbApi.ReleaseInterface(rapidradio, 0);
                            rapidradio.Close();
                        }
                    }

                    if (sessionHandle != null)
                    {
                        sessionHandle.Close();
                    }

                    Marshal.FreeHGlobal(_unmanagedReadBuff);
                    Marshal.FreeHGlobal(_unmanagedWriteBuff);
                }
            }
        }