3

I am trying to communicate with a Nokia Lumia phone(RM-917), over USB using LIBUSING and C#. LIBUSB is able to see the device's information(pid,vid,etc). However, I am not able to successfully write to ANY endpoint, even sending the exact command as the Windows Device Recovery Tool.

According to WinUSB, the write endpoint is EP07, however, this endpoint just times out. I have tried every other endpoint, and all of these fail.

`
public void initDevice()
{


    if(this.lumiaDevice == null)
    {
        throw new Exception("LumiaPhoneManager does not have a selected device");
    }

    UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x0421, 0x0661);
    MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);


    IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
    if (!ReferenceEquals(wholeUsbDevice, null))
    {
        // This is a "whole" USB device. Before it can be used, 
        // the desired configuration and interface must be selected.

        // Select config #1
        wholeUsbDevice.SetConfiguration(1);

        // Claim interface #0.
        wholeUsbDevice.ClaimInterface(1);
    }

    if (this.writer == null)
    {
        writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep07);
    }



}


    public void readPCode()

{
    currentID++;
    var _x = new jsonPkt();
    ErrorCode ec = ErrorCode.None;
    int bytesWritten;
    _x.id = this.currentID + 1;
    _x.method = "ReadProductCode";

    string value = @"{""jsonrpc"":""<JSONRPC>"",""id"":<ID>,""method"":""<METHOD>"",""params"":null}";
    value = value.Replace("<JSONRPC>", "2.0");
    value = value.Replace("<ID>", currentID.ToString());
    value = value.Replace("<METHOD>", _x.method.ToString());


ec = writer.Write(Encoding.Default.GetBytes(value), 8000, out bytesWritten);
    currentID++;

    if (ec != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);

    byte[] readBuffer = new byte[1024];
    while (ec == ErrorCode.None)
    {
        int bytesRead;

        // If the device hasn't sent data in the last 100 milliseconds,
        // a timeout error (ec = IoTimedOut) will occur. 
        ec = reader.Read(readBuffer, 100, out bytesRead);

     //   if (bytesRead == 0) throw new Exception("No more bytes!");

        // Write that output to the console.
        this.rtb.Text += Encoding.Default.GetString(readBuffer, 0, bytesRead).ToString() + "\n";

    }
}
user1698144
  • 754
  • 4
  • 13
  • 36
  • Could you please share the descriptors? I guess you can read those as you mentioned you can see VID and DID. – Shaibal Mar 28 '17 at 05:00
  • https://pastebin.com/wwEXURKi – user1698144 Mar 28 '17 at 05:29
  • Ahh! I may have figured it out. The device has 3 interfaces, one of which is nokia care suit, the actual WinUSB device is not showing up at all. My program is sending data to the wrong (driver?) which has the same VID and product ID – user1698144 Mar 28 '17 at 05:36
  • I have not seen the descriptors yet bit thats what I was going to check too. Anyway looks like you have your answer. :) – Shaibal Mar 28 '17 at 06:11
  • The write operation doesn't time out. However, the usb device is simply unresponsive to any commands. Also, any breakpoints after the write command are not being hit. – user1698144 Mar 28 '17 at 17:59
  • Most probably the device is sending NAKs. If thats the case, the host will keep sending OUT token. It looks like hang situation but its actually not. – Shaibal Mar 28 '17 at 18:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139290/discussion-between-shaibal-and-user1698144). – Shaibal Mar 28 '17 at 18:03

2 Answers2

1

Found the solution

Debugged the OEM software and found the program was using a different path to the USB device. After that I was getting access denied errors, which was solved by moving the project to a different drive. For reasons unknown, when the program runs on c drive, the CreateFile function fails with access denied

user1698144
  • 754
  • 4
  • 13
  • 36
-1

Its possible that to activate write, you need to send some class specific control request first. You mentioned that windows device recovery tool is able to write. You can install USB packet sniffer software in your windows PC and then use the device manager to write some data to the device. Packet sniffer tool will be able to capture all the packets sent to the device. This way you can see the exact device requests which are required to enable write operation.

Analyzer software - http://www.usblyzer.com/

Please try this way to resolve your problem.

PS- I am assuming you do not have a hardware USB packet analyzer like Lecroy advisor or Beagle. Software packet sniffer should be fine since the host is a PC.

Shaibal
  • 907
  • 8
  • 18