Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: USB Host get used hub port?

  1. #1
    Junior Member ohoomens's Avatar
    Join Date
    Feb 2019
    Location
    Hamburg
    Posts
    13

    USB Host get used hub port?

    Hi everyone!
    From what I know it is possible to retrieve port numbers of devices which are connected to a USB hub. I couldnt find any information regarding the libraries capability of doing so - is this possible with the current lib version?

    Thanks in advance!

  2. #2
    Senior Member wwatson's Avatar
    Join Date
    Aug 2017
    Posts
    870
    Quote Originally Posted by ohoomens View Post
    Hi everyone!
    From what I know it is possible to retrieve port numbers of devices which are connected to a USB hub. I couldnt find any information regarding the libraries capability of doing so - is this possible with the current lib version?

    Thanks in advance!
    This information can be found starting at line #83 in "USBHost_t36.h" in the USBHost_t36 library. The device_t struct is used as part of each claimed USB device with the information you are looking for. Paul explains it's usage there. An example of accessing this information can be found in "MassStorageDriver.cpp", "utility/msc.h" and an example of it's usage in "examples/storage/DriveInfo.ino".

    Code:
    // Show USB drive information for the selected USB drive.
    void printDriveInfo(USBDrive &drive) {
      // Print USB drive information.
      Serial.printf(F("       connected: %d\n"), drive.msDriveInfo.connected);
      Serial.printf(F("     initialized: %d\n"), drive.msDriveInfo.initialized);
      Serial.printf(F("   USB Vendor ID: %4.4x\n"), drive.msDriveInfo.idVendor);
      Serial.printf(F("  USB Product ID: %4.4x\n"), drive.msDriveInfo.idProduct);
      Serial.printf(F("      HUB Number: %d\n"), drive.msDriveInfo.hubNumber);
      Serial.printf(F("        HUB Port: %d\n"), drive.msDriveInfo.hubPort);
      Serial.printf(F("  Device Address: %d\n"), drive.msDriveInfo.deviceAddress);
      Serial.printf(F("Removable Device: "));
      if(drive.msDriveInfo.inquiry.Removable == 1) {
        Serial.printf(F("YES\n"));
      } else {
        Serial.printf(F("NO\n"));
      }
      Serial.printf(F("        VendorID: %8.8s\n"), drive.msDriveInfo.inquiry.VendorID);
      Serial.printf(F("       ProductID: %16.16s\n"), drive.msDriveInfo.inquiry.ProductID);
      Serial.printf(F("      RevisionID: %4.4s\n"), drive.msDriveInfo.inquiry.RevisionID);
      Serial.printf(F("         Version: %d\n"), drive.msDriveInfo.inquiry.Version);
      Serial.printf(F("    Sector Count: %ld\n"), drive.msDriveInfo.capacity.Blocks);
      Serial.printf(F("     Sector size: %ld\n"), drive.msDriveInfo.capacity.BlockSize);
      uint64_t drivesize = drive.msDriveInfo.capacity.Blocks;
      drivesize *= drive.msDriveInfo.capacity.BlockSize;
      Serial.print(F("   Disk Capacity: "));
      Serial.print(drivesize);
      Serial.println(" Bytes");
      drive.printPartionTable(Serial);
      Serial.println();
    }
    In "msc.h" is the struct:

    Code:
    // MSC Drive status/info struct
    typedef struct {
    	bool connected;    // Device is connected
    	bool initialized;  // Device is initialized
    	bool mounted;      // Device is mounted
    	const char * drvName;
    	uint32_t bufferSize;
    	uint8_t hubNumber;
    	uint8_t hubPort;
    	uint8_t deviceAddress;
    	uint16_t idVendor;
    	uint16_t idProduct;
    	msSCSICapacity_t capacity;
    	msInquiryResponse_t inquiry;	
    } __attribute__((packed)) msDriveInfo_t;
    And in "MassStorageDriver.cpp" in the claim function:
    Code:
    	idVendor = dev->idVendor;
    	idProduct = dev->idProduct;
    	hubNumber = dev->hub_address;
    	deviceAddress = dev->address;
    	hubPort = dev->hub_port; // Used for device ID with multiple drives.
    and in the mscInit function:
    Code:
    	// Retrieve drive information.
    	msDriveInfo.initialized = true;
    	msDriveInfo.hubNumber = getHubNumber();			// Which HUB.
    	msDriveInfo.hubPort = getHubPort();				// Which HUB port.
    	msDriveInfo.deviceAddress = getDeviceAddress();	// Device addreess.

    Maybe this will help you get started with getting this information for your usage.

  3. #3
    Junior Member ohoomens's Avatar
    Join Date
    Feb 2019
    Location
    Hamburg
    Posts
    13
    Thank you! I hadnít seen this. Iíve been looking for the same implementation in other devices (such as serial devices) but it seems that just the Mass Storage supports this?

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    11,655
    Only USBDrive has the two methods:
    Code:
        uint8_t getHubNumber() { return hubNumber; }
        uint8_t getHubPort() { return hubPort; }
    Any device has that information available to it:
    That is any device could also save away that information and expose methods to return it.

    They could probably expose them as simple as just adding the methods something like:
    Code:
        uint8_t getHubNumber() { return device? device->hub_address : 0; }
        uint8_t getHubPort() { return device? device->hub_port: 0;}
    That is if the claim method returns true the pointer to the device is stored in device method, so you can simply grab the data from it

  5. #5
    Junior Member ohoomens's Avatar
    Join Date
    Feb 2019
    Location
    Hamburg
    Posts
    13
    This works perfectly, thank you! I didnĎt know every device gets the port number so I assumed the solution would be more work ^^

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •