My embedded system runs Linux OS. The system has two joysticks connected to it through USB.
My program should read any of these two devices when they are ready and pass the joystick key information to upper layer.
My program does the following.
- Open both the joystick files /dev/js0 and /dev/js1 if they are not already opened.
i.e. if fd0 or fd1 == -1 (default value is -1) open it. - select() // maxfd+1 and readset is passed to select
- which ever device is ready, program uses read() to read the data.
- The above all three steps are done periodically in a timer.
The above code works fine. No issues.
As part of negative test case, I removed one of the joystick from USB after it has been opened and read successfully.
I started seeing the following perror -> "read() failed: no such file or device.
I want to handle this error such that if the joystick is removed and inserted back the code should accordingly re-open it without any issues.
As soon as I encounter the above perror, I tried to close the device and set my fd to -1 so that as part of step 1 it will be reopend. But this is not working. I am currently debugging it. I think close() is not working after encountering no such file or device error.
If anybody has encountered this type of problem or know how to handle this type of scenario (USB device removed and inserted after opening it), kindly provide your inputs.
thanks much. Regards, Emerson