Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permission request + Wrong CSW status #163

Closed
ukmarn opened this issue Nov 22, 2018 · 18 comments
Closed

Permission request + Wrong CSW status #163

ukmarn opened this issue Nov 22, 2018 · 18 comments

Comments

@ukmarn
Copy link

ukmarn commented Nov 22, 2018

Experiencing similar problem as #124:
When not using intent-filter and only requesting permission inside MainActivity, the application works fine. The problem is that with usbManager.requirePermission() the user "default checkbox" selection is not saved and the permission dialog keeps opening each time USB device is attached.

Now I tried adding intent-filter with USB_DEVICE_ATTACHED action code which by documentation should save permission choice that user selected the first time.
The first time USB is attached the code works like it should - but each next time USB attaches, the library throws me:

E/UsbDocumentProvider: error setting up device
    java.io.IOException: Unsuccessful Csw status: 1
        at com.github.mjdev.libaums.driver.scsi.ScsiBlockDevice.transferCommand(ScsiBlockDevice.java:184)

Any ideas why this would happen?
I understand if library was throwing this in both cases (using intent-filter and just requesting for permission) but what bothers me is that when manually requesting for permission, library works as supposed but when using filter-intent not.

@magnusja
Copy link
Owner

Did you try the delay with sleeping proposed in issue #124 ?

@ukmarn
Copy link
Author

ukmarn commented Nov 27, 2018

Yes, also tried that with no success.
Tried from 1 second to 5 and also on both .init and when searching. No luck.
Any other idea?

@magnusja
Copy link
Owner

In some other thread, they suggested ignoring the CSW status overall. Apparently, everything still works fine. But this is obviously wrong specification-wise. Maybe there is also a bug related to parsing the CSW status. Did you try different pen drives?

@ukmarn
Copy link
Author

ukmarn commented Nov 27, 2018

Sorry didn't yet. Only tried with single one, also tried reformatting it few times FAT, FAT32 and still the same.
Will try it out with different one and report back here.

@wangb123
Copy link

wangb123 commented Dec 4, 2018

i also found this problem when I use 64G + storage. UsbDevice information is as follows: UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1507,mProductId=1864,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=Generic,mProductName=USB Storage,mVersion=2.16,mSerialNumber=000000001209,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=250,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=8,mSubclass=6,mProtocol=80,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=512,mInterval=0] UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=512,mInterval=0]]]] found usb interface: UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=8,mSubclass=6,mProtocol=80,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=512,mInterval=0] UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=512,mInterval=0]] found usb endpoint: UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=512,mInterval=0] found usb endpoint: UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=512,mInterval=0]
Then I try to open the file browser, and there are two disk characters, U disk and Generic. If this is the time to execute UsbMassStorageDevice.init (), the two disk characters will disappear, and it seems that UsbMassStorageDevice takes over the device.
http://h.yaolanimage.cn/ali/wbing/assets/islflidhslhfds.jpeg
Then I tried to get the content of the U disk through the file system. When I listened to the U disk mount, I succeeded in getting the file.

public class MediaReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            return;
        }
        switch (action) {
            case Intent.ACTION_MEDIA_CHECKING:
                break;
            case Intent.ACTION_MEDIA_MOUNTED:
                // 获取挂载路径, 读取U盘文件
                WLogTool.e("MOUNTED");
                Uri uri = intent.getData();
                if (uri != null) {
                    String filePath = uri.getPath();
                    File rootFile = new File(filePath);
                    WLogTool.e(rootFile.getAbsolutePath());
                    for (File file : rootFile.listFiles()) {
                        // 文件列表...
                        WLogTool.e(file.getAbsolutePath());
                    }
                }
                break;
            case Intent.ACTION_MEDIA_EJECT:
                break;
            case Intent.ACTION_MEDIA_UNMOUNTED:
                WLogTool.e("UNMOUNTED");
                break;
        }
    }
}

@lorenzos
Copy link

lorenzos commented Apr 27, 2019

Same here, Unsuccessful Csw status: 1. I tried with a 5 seconds delay from permission request to .init(), but it doesn't change. I'm using an USB-C "hub", where you can simultaneously attach USB pen drives or SD cards. AUMS lists both, but the USB pen drive works, and the SD card reader throws that error. The following are the proerties of that USB devices:

immagine immagine

@magnusja
Copy link
Owner

Hey @lorenzos I havent tried sd card readers in while, but I remember that lots of them caused troubles :/

Did you try removing this check completely?

@lorenzos
Copy link

lorenzos commented Apr 29, 2019

@magnusja I did not. If I clone the repo locally and load "libaums" folder as a local gradle module, it should work as it is? Doesn’t it require additional building steps, etc? Thanks.

@magnusja
Copy link
Owner

Yes that should work

@lorenzos
Copy link

lorenzos commented Apr 30, 2019

@magnusja Just tried, unfortunately removing the check doesn't bring me far: I immediately get a Could not read from device, result == -1 errno 88 Socket operation on non-socket 😭

Edit: if I try repeating the access to the device, I get similar errors, but with different errno codes and descriptions.

@magnusja
Copy link
Owner

magnusja commented May 1, 2019

Yes I tried to get some more error information because Android API only throws an exception with no information at all but it seems that the errno from C is not really related to the USB error.

@lukasniedoba
Copy link

Hi @magnusja

I had been testing your library and got into the similar problems:
Unsuccessful Csw status: 1
durning the transfer of the TEST_UNIT_READY command.

It occurred only when using card reader device. When using the USB pen drive there was no problem. I discovered there is by default LUN set to the 0. In the case of the card reader device every physical slot has its logical unit assigned. So if I change the LUN then corresponding card reader port starts working.

Right now there is no possibility to choose the LUN in the code. If you agree with that I would like to help you with implementation of this functionality. But I have no time for waste so I want to consult it before I apply for the pull request so we prevent some useless steps.

I suggest to add some methods into ScsiBlockDevice for setting LUN and getting information about it. Or adding another layer of abstraction and adding the container of some class representing its ports into the ScsiBlockDevice. Also durning the initialization there should be the process of recognition of those ports. Any other solution? What do you think?

@timcrid
Copy link
Contributor

timcrid commented Jul 30, 2019

I wish I had seen your post earlier @lukasniedoba.
I also debugged for a while today and came to the same result as you.
I made a pretty easy fix for it. It works, but it's ugly.
There is certainly a better solution than this fix → Removed hardcoded LUN

@magnusja
Copy link
Owner

Hey @lukasniedoba , @timcrid ,

thanks for the support! I am always happy to take PRs. The LUN thing has been on my list for a while now.

@timcrid Your solution works but is probably a little hacky :D

@lukasniedoba The best solution imho would be if the user of the library does not really need to change anything. He or she just sees an additional device for each inserted SD card. Maybe additional partitions of a particular device would also be fine. In terms of implementation I am not a 100% sure. If on device level we would need some kind of wrapper around the ScsiBlockDevice which creates a new device for each LUN and returns it to the user. This probably needs some refactoring on device handling. The other option would be to just create a new partition for each LUN. Maybe it would also be possible to implement the notion of "subdevices" for ScsiBlockDevice when it turns out it is an SD card reader. What do you think?

@magnusja
Copy link
Owner

Looking at the code, maybe it makes sense to hook in here: https://github.com/magnusja/libaums/blob/develop/libaums/src/main/java/com/github/mjdev/libaums/UsbMassStorageDevice.java#L230

And just loop over the max LUNs and create a new mass storage device for each LUN.

@magnusja
Copy link
Owner

magnusja commented Aug 3, 2019

@timcrid @lukasniedoba can you please try this: #201

@timcrid
Copy link
Contributor

timcrid commented Aug 3, 2019

Hey @magnusja
Nice work!
I tried it with a normal USB stick and with a USB card-reader and it works fine.

@magnusja
Copy link
Owner

magnusja commented Aug 3, 2019

awesome :) will merge, will be in the next release

@magnusja magnusja closed this as completed Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants