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

Android error handling fix #4032

Merged
merged 4 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/android/usb_host/android_uvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,19 +938,27 @@ void stream_thread(usbhost_uvc_stream_context *strctx) {
}
});
LOG_DEBUG("Transfer thread started for endpoint address: " << strctx->endpoint);
bool disconnect = false;
do {
auto i = strctx->stream->stream_if->interface;
uint32_t transferred = 0;
auto sts = messenger->bulk_transfer(read_ep, strctx->stream->outbuf, LIBUVC_XFER_BUF_SIZE, transferred, 1000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use hard-coded timeouts ? (1000, 50)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will create a define, it is not accessible to the app level

if(sts != librealsense::platform::RS2_USB_STATUS_SUCCESS)
switch(sts)
{
if(sts == librealsense::platform::RS2_USB_STATUS_NO_DEVICE)
case librealsense::platform::RS2_USB_STATUS_NO_DEVICE:
disconnect = true;
break;
case librealsense::platform::RS2_USB_STATUS_OVERFLOW:
messenger->reset_endpoint(read_ep, reset_ep_timeout);
break;
case librealsense::platform::RS2_USB_STATUS_SUCCESS:
strctx->stream->got_bytes = transferred;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gottransferred_bytes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the name in all of our UVC implementations, we can change it when moving to rsuvc

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using libuvc under Android, how can I handle the error?

usbhost_uvc_process_payload(strctx->stream, &archive, &queue);
break;
default:
break;
continue;
}
strctx->stream->got_bytes = transferred;
usbhost_uvc_process_payload(strctx->stream, &archive, &queue);
} while (strctx->stream->running);
} while (!disconnect && strctx->stream->running);

int ep = strctx->endpoint;
messenger->reset_endpoint(read_ep, reset_ep_timeout);
Expand Down
13 changes: 7 additions & 6 deletions src/usbhost/messenger-usbhost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ namespace librealsense
{
case EBADF:
case ENODEV: return RS2_USB_STATUS_NO_DEVICE;
//TODO:MK
case 71: return RS2_USB_STATUS_OVERFLOW; //protocol error seems to identify FW overflow
matkatz marked this conversation as resolved.
Show resolved Hide resolved
//TODO:MK
default: return RS2_USB_STATUS_OTHER;
}
}
Expand Down Expand Up @@ -116,9 +117,9 @@ namespace librealsense
int length = 0;
usb_status rv = control_transfer(requestType, request, value, ep, buffer, length, transferred, timeout_ms);
if(rv == RS2_USB_STATUS_SUCCESS)
LOG_DEBUG("USB pipe " << ep << " reset successfully");
LOG_INFO("USB pipe " << ep << " reset successfully");
else
LOG_DEBUG("Failed to reset the USB pipe " << ep << ", error: " << usb_status_to_string.at(rv).c_str());
LOG_WARNING("Failed to reset the USB pipe " << ep << ", error: " << usb_status_to_string.at(rv).c_str());
return rv;
}

Expand All @@ -129,7 +130,7 @@ namespace librealsense
{
std::string strerr = strerror(errno);
LOG_WARNING("control_transfer returned error, index: " << index << ", error: " << strerr);
return usbhost_status_to_rs(sts);
return usbhost_status_to_rs(errno);
}
transferred = sts;
return RS2_USB_STATUS_SUCCESS;
Expand All @@ -141,8 +142,8 @@ namespace librealsense
if(sts < 0)
{
std::string strerr = strerror(errno);
LOG_WARNING("bulk_transfer returned error, endpoint: " << endpoint->get_address() << ", error: " << strerr);
return usbhost_status_to_rs(sts);
LOG_WARNING("bulk_transfer returned error, endpoint: " << (int)endpoint->get_address() << ", error: " << strerr << ", number: " << (int)errno);
return usbhost_status_to_rs(errno);
}
transferred = sts;
return RS2_USB_STATUS_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import java.util.List;

public class Device extends LrsClass {
private List<Sensor> _sensors = new ArrayList<>();

public Device(long handle){
mHandle = handle;
}

public List<Sensor> querySensors(){
long[] sensorsHandles = nQuerySensors(mHandle);
List<Sensor> rv = new ArrayList<>();
for(long h : sensorsHandles){
rv.add(new Sensor(h));
_sensors.add(new Sensor(h));
}
return rv;
}

public List<Sensor> querySensors(){
return _sensors;
}

public boolean supportsInfo(CameraInfo info){
Expand Down Expand Up @@ -44,6 +44,8 @@ public byte[] serializePresetToJson(){

@Override
public void close() {
for (Sensor s : _sensors)
s.close();
nRelease(mHandle);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,35 @@ private void init() {
}

private boolean validated_device(){
DeviceList devices = mRsContext.queryDevices();
if(devices.getDeviceCount() == 0)
return false;
Device device = devices.createDevice(0);
if(!device.supportsInfo(CameraInfo.RECOMMENDED_FIRMWARE_VERSION))
return true;
final String recFw = device.getInfo(CameraInfo.RECOMMENDED_FIRMWARE_VERSION);
final String fw = device.getInfo(CameraInfo.FIRMWARE_VERSION);
String[] sFw = fw.split("\\.");
String[] sRecFw = recFw.split("\\.");
for(int i = 0; i < sRecFw.length; i++){
if(Integer.parseInt(sFw[i]) > Integer.parseInt(sRecFw[i]))
break;
if(Integer.parseInt(sFw[i]) < Integer.parseInt(sRecFw[i])){
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView textView = findViewById(R.id.connectCameraText);
textView.setText("The FW of the connected device is:\n " + fw +
"\n\nThe recommended FW for this device is:\n " + recFw +
"\n\nPlease update your device to the recommended FW or higher");
}
});
return false;
}
}
return true;
try(DeviceList devices = mRsContext.queryDevices()){
if(devices.getDeviceCount() == 0)
return false;
try(Device device = devices.createDevice(0)) {
if (!device.supportsInfo(CameraInfo.RECOMMENDED_FIRMWARE_VERSION))
return true;
final String recFw = device.getInfo(CameraInfo.RECOMMENDED_FIRMWARE_VERSION);
final String fw = device.getInfo(CameraInfo.FIRMWARE_VERSION);
String[] sFw = fw.split("\\.");
String[] sRecFw = recFw.split("\\.");
for(int i = 0; i < sRecFw.length; i++){
if(Integer.parseInt(sFw[i]) > Integer.parseInt(sRecFw[i]))
break;
if(Integer.parseInt(sFw[i]) < Integer.parseInt(sRecFw[i])){
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView textView = findViewById(R.id.connectCameraText);
textView.setText("The FW of the connected device is:\n " + fw +
"\n\nThe recommended FW for this device is:\n " + recFw +
"\n\nPlease update your device to the recommended FW or higher");
}
});
return false;
}
}
return true;
}
}
}

private DeviceListener mListener = new DeviceListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ protected void onResume() {

TextView message = findViewById(R.id.list_view_title);

RsContext ctx = new RsContext();
DeviceList devices = ctx.queryDevices();
if(devices.getDeviceCount() == 0){
finish();
}

message.setText("Device info:");

Map<CameraInfo,String> infoMap = new TreeMap<>();

final Device device = ctx.queryDevices().createDevice(0);
for(CameraInfo ci : CameraInfo.values()){
if(device.supportsInfo(ci))
infoMap.put(ci, device.getInfo(ci));
RsContext ctx = new RsContext();
try(DeviceList devices = ctx.queryDevices()){
if(devices.getDeviceCount() == 0){
finish();
}
message.setText("Device info:");

try(final Device device = devices.createDevice(0)){
for(CameraInfo ci : CameraInfo.values()){
if(device.supportsInfo(ci))
infoMap.put(ci, device.getInfo(ci));
}
}
}

final String[] info = new String[infoMap.size()];
Expand All @@ -51,10 +52,9 @@ protected void onResume() {
info[i++] = e.getKey().toString() + ": " + e.getValue();
}


final ListView listview = findViewById(R.id.list_view);

final ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.files_list_view, info);
listview.setAdapter(adapter);;
listview.setAdapter(adapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,35 @@ protected void onResume() {
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
RsContext ctx = new RsContext();
DeviceList devices = ctx.queryDevices();
if(devices.getDeviceCount() == 0){
Log.e(TAG, "failed to set preset, no device found");
finish();
}
Device device = ctx.queryDevices().createDevice(0);
if(!device.isInAdvancedMode()){
Log.e(TAG, "failed to set preset, device not in advanced mode");
finish();
}
final String item = finalPresets[position];
try {
InputStream is = resources.getAssets().open("presets/" + item);
byte[] buffer = new byte[is.available()];
is.read(buffer);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(buffer);
baos.close();
is.close();
device.loadPresetFromJson(buffer);
} catch (IOException e) {
Log.e(TAG, "failed to set preset, failed to open preset file, error: " + e.getMessage());
}finally {
finish();
try(DeviceList devices = ctx.queryDevices()) {
if(devices.getDeviceCount() == 0){
Log.e(TAG, "failed to set preset, no device found");
finish();
}
try(Device device = devices.createDevice(0)){
if(!device.isInAdvancedMode()){
Log.e(TAG, "failed to set preset, device not in advanced mode");
finish();
}
final String item = finalPresets[position];
try {
InputStream is = resources.getAssets().open("presets/" + item);
byte[] buffer = new byte[is.available()];
is.read(buffer);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(buffer);
baos.close();
is.close();
device.loadPresetFromJson(buffer);
} catch (IOException e) {
Log.e(TAG, "failed to set preset, failed to open preset file, error: " + e.getMessage());
}
catch (Exception e) {
Log.e(TAG, "failed to set preset, error: " + e.getMessage());
}finally {
finish();
}
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class SettingsActivity extends AppCompatActivity {
private static final int INDEX_ADVANCE_MODE = 1;
private static final int INDEX_PRESETS = 2;

private Device _device;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -48,18 +50,24 @@ protected void onResume() {
RsContext ctx = new RsContext();
try(DeviceList devices = ctx.queryDevices()) {
if (devices.getDeviceCount() == 0) {
return;
throw new Exception("Failed to detect a connected device");
}
Device device = ctx.queryDevices().createDevice(0);
loadSettingsList(device);
StreamProfileSelector[] profilesList = createSettingList(device);
loadStreamList(device, profilesList);
_device = devices.createDevice(0);
loadSettingsList(_device);
StreamProfileSelector[] profilesList = createSettingList(_device);
loadStreamList(_device, profilesList);
} catch(Exception e){
Log.e(TAG, "failed to load settings, error: " + e.getMessage());
Toast.makeText(this, "Failed to load settings", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
protected void onPause() {
super.onPause();
if (_device != null)
_device.close();
}

private void loadSettingsList(final Device device){
final ListView listview = findViewById(R.id.settings_list_view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ public void run() {
private void configStream(Config config){
config.disableAllStreams();
RsContext ctx = new RsContext();
String pid;
Map<Integer, List<VideoStreamProfile>> profilesMap;
try(DeviceList devices = ctx.queryDevices()) {
if (devices.getDeviceCount() == 0) {
return;
}
}
String pid;
Map<Integer, List<VideoStreamProfile>> profilesMap;
try(Device device = ctx.queryDevices().createDevice(0)){
pid = device.getInfo(CameraInfo.PRODUCT_ID);
profilesMap = SettingsActivity.createProfilesMap(device);
try (Device device = devices.createDevice(0)) {
pid = device.getInfo(CameraInfo.PRODUCT_ID);
profilesMap = SettingsActivity.createProfilesMap(device);
}
}

SharedPreferences sharedPref = mContext.getSharedPreferences(mContext.getString(R.string.app_settings), Context.MODE_PRIVATE);
Expand Down