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 all 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
6 changes: 6 additions & 0 deletions src/android/jni/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ Java_com_intel_realsense_librealsense_RsContext_nQueryDevices(JNIEnv *env, jclas
handle_error(env, e);
return (jlong) device_list_handle;
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_intel_realsense_librealsense_RsContext_nGetVersion(JNIEnv *env, jclass type) {
return env->NewStringUTF(RS2_API_VERSION_STR);
}
27 changes: 19 additions & 8 deletions src/android/usb_host/android_uvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include <atomic>
#include <zconf.h>

#define DEQUEUE_TIMEOUT 50
#define STREAMING_BULK_TRANSFER_TIMEOUT 1000

// Data structures for Backend-Frontend queue:
struct frame;
// We keep no more then 2 frames in between frontend and backend
Expand Down Expand Up @@ -932,25 +935,33 @@ void stream_thread(usbhost_uvc_stream_context *strctx) {
std::thread t([&]() {
while (keep_sending_callbacks) {
frame_ptr fp(nullptr, [](frame *) {});
if (queue.dequeue(&fp, 50)) {
if (queue.dequeue(&fp, DEQUEUE_TIMEOUT)) {
strctx->stream->user_cb(&fp->fo, strctx->stream->user_ptr);
}
}
});
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);
if(sts != librealsense::platform::RS2_USB_STATUS_SUCCESS)
auto sts = messenger->bulk_transfer(read_ep, strctx->stream->outbuf, LIBUVC_XFER_BUF_SIZE, transferred, STREAMING_BULK_TRANSFER_TIMEOUT);
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 EPROTO: return RS2_USB_STATUS_OVERFLOW; //protocol error seems to identify FW overflow
//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 @@ -11,6 +11,10 @@ public static void init(Context context){
mDeviceWatcher = new DeviceWatcher(context);
}

public static String getVersion(){
return nGetVersion();
}

/**
* @deprecated use {@link #queryDevices()} instead.
*/
Expand Down Expand Up @@ -45,6 +49,7 @@ public void close() {
}

private static native long nCreate();
private static native String nGetVersion();
private static native long nQueryDevices(long handle);
private static native void nDelete(long handle);
}
4 changes: 2 additions & 2 deletions wrappers/android/tools/camera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.intel.realsense.camera"
minSdkVersion 19
targetSdkVersion 26
versionCode 2
versionName "0.0.1"
versionCode 5
versionName "0.0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void onClick(View view) {
return;
}

String appVersion = BuildConfig.VERSION_NAME;
String lrsVersion = RsContext.getVersion();
TextView versions = findViewById(R.id.versionsText);
versions.setText("librealsense version: " + lrsVersion + "\ncamera app version: " + appVersion);
mPermissionsGrunted = true;
}

Expand Down Expand Up @@ -86,33 +90,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,36 @@ 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);
loadInfosList();
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 loadInfosList() {
final ListView listview = findViewById(R.id.info_list_view);
String appVersion = "Camera App Version: " + BuildConfig.VERSION_NAME;
String lrsVersion = "LibRealSense Version: " + RsContext.getVersion();

final String[] info = { lrsVersion, appVersion};
final ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.files_list_view, info);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}

private void loadSettingsList(final Device device){
final ListView listview = findViewById(R.id.settings_list_view);
Expand Down
Loading