Skip to content

Commit

Permalink
Merge pull request IntelRealSense#4304 from matkatz/android-camera-fw…
Browse files Browse the repository at this point in the history
…-update-2

Android camera app - firmware update
  • Loading branch information
ev-mp authored Jun 27, 2019
2 parents 9a15641 + 160efb7 commit bb2d64e
Show file tree
Hide file tree
Showing 29 changed files with 621 additions and 183 deletions.
1 change: 1 addition & 0 deletions CMake/android_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ macro(os_set_flags)
set(FORCE_LIBUVC OFF)
set(BACKEND RS2_USE_ANDROID_BACKEND)
set(HWM_OVER_XU ON)
set(IMPORT_DEPTH_CAM_FW OFF)
else()
set(FORCE_LIBUVC ON)
set(BACKEND RS2_USE_V4L2_BACKEND)
Expand Down
3 changes: 0 additions & 3 deletions src/android/android-uvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ namespace librealsense {
int len) {

int status = uvc_set_ctrl(_device.get(), xu.unit, ctrl, (void *) data, len);
if (status < 0)
LOG_ERROR("SetXU result:" << status);

return status >= 0;
}

Expand Down
36 changes: 34 additions & 2 deletions src/android/jni/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "../../../include/librealsense2/rs.h"
#include "../../../include/librealsense2/hpp/rs_device.hpp"
#include "../../api.h"

extern "C" JNIEXPORT jboolean JNICALL
Java_com_intel_realsense_librealsense_Device_nSupportsInfo(JNIEnv *env, jclass type, jlong handle,
Expand Down Expand Up @@ -70,6 +71,26 @@ Java_com_intel_realsense_librealsense_Updatable_nEnterUpdateState(JNIEnv *env, j
handle_error(env, e);
}

extern "C"
JNIEXPORT jbyteArray JNICALL
Java_com_intel_realsense_librealsense_Updatable_nCreateFlashBackup(JNIEnv *env, jobject instance,
jlong handle) {
rs2_error* e = NULL;
jclass cls = env->GetObjectClass(instance);
jmethodID id = env->GetMethodID(cls, "onProgress", "(F)V");
auto cb = [&](float progress){ env->CallVoidMethod(instance, id, progress); };

std::shared_ptr<const rs2_raw_data_buffer> raw_data_buffer(
rs2_create_flash_backup_cpp(reinterpret_cast<rs2_device *>(handle), new rs2::update_progress_callback<decltype(cb)>(cb), &e),
[](const rs2_raw_data_buffer* buff){ if(buff) delete buff;});
handle_error(env, e);

jbyteArray rv = env->NewByteArray(raw_data_buffer->buffer.size());
env->SetByteArrayRegion(rv, 0, raw_data_buffer->buffer.size(),
reinterpret_cast<const jbyte *>(raw_data_buffer->buffer.data()));
return rv;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_UpdateDevice_nUpdate(JNIEnv *env, jobject instance,
Expand All @@ -80,8 +101,8 @@ Java_com_intel_realsense_librealsense_UpdateDevice_nUpdate(JNIEnv *env, jobject
jclass cls = env->GetObjectClass(instance);
jmethodID id = env->GetMethodID(cls, "onProgress", "(F)V");
auto cb = [&](float progress){ env->CallVoidMethod(instance, id, progress); };
rs2_update_cpp(reinterpret_cast<const rs2_device *>(handle), image, length,
new rs2::fw_update_progress_callback<decltype(cb)>(cb), &e);
rs2_update_firmware_cpp(reinterpret_cast<const rs2_device *>(handle), image, length,
new rs2::update_progress_callback<decltype(cb)>(cb), &e);
handle_error(env, e);
env->ReleaseByteArrayElements(image_, image, 0);
}
Expand All @@ -94,3 +115,14 @@ Java_com_intel_realsense_librealsense_Device_nHardwareReset(JNIEnv *env, jclass
rs2_hardware_reset(reinterpret_cast<const rs2_device *>(handle), &e);
handle_error(env, e);
}

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_intel_realsense_librealsense_Device_nIsDeviceExtendableTo(JNIEnv *env, jclass type,
jlong handle, jint extension) {
rs2_error *e = NULL;
int rv = rs2_is_device_extendable_to(reinterpret_cast<const rs2_device *>(handle),
static_cast<rs2_extension>(extension), &e);
handle_error(env, e);
return rv > 0;
}
12 changes: 0 additions & 12 deletions src/android/jni/device_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,3 @@ extern "C" JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_DeviceList_nRelease(JNIEnv *env, jclass type, jlong handle) {
rs2_delete_device_list((rs2_device_list *) handle);
}

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_intel_realsense_librealsense_DeviceList_nIsDeviceExtendableTo(JNIEnv *env, jclass type,
jlong handle,
jint extension) {
rs2_error *e = NULL;
int rv = rs2_is_device_extendable_to(reinterpret_cast<const rs2_device *>(handle),
static_cast<rs2_extension>(extension), &e);
handle_error(env, e);
return rv > 0;
}
22 changes: 6 additions & 16 deletions src/android/jni/stream_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,23 @@ Java_com_intel_realsense_librealsense_StreamProfile_nGetProfile(JNIEnv *env, jcl
}

extern "C" JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_VideoStreamProfile_nGetIntrinsics(JNIEnv *env, jclass type,
Java_com_intel_realsense_librealsense_VideoStreamProfile_nGetResolution(JNIEnv *env, jclass type,
jlong handle,
jobject params) {
rs2_intrinsics intr;
intr={0};
int width = -1;
int height = -1;
rs2_error *e = NULL;

rs2_get_video_stream_intrinsics((const rs2_stream_profile *) handle,&intr,&e);
rs2_get_video_stream_resolution((const rs2_stream_profile *) handle, &width, &height, &e);
handle_error(env, e);

jclass clazz = env->GetObjectClass(params);

jfieldID widthField = env->GetFieldID(clazz, "width", "I");
jfieldID heightField = env->GetFieldID(clazz, "height", "I");
jfieldID fxField = env->GetFieldID(clazz, "fx", "F");
jfieldID fyField = env->GetFieldID(clazz, "fy", "F");
jfieldID ppxField = env->GetFieldID(clazz, "ppx", "F");
jfieldID ppyField = env->GetFieldID(clazz, "ppy", "F");


env->SetIntField(params, widthField, intr.width);
env->SetIntField(params, heightField, intr.height);
env->SetFloatField(params,fxField,intr.fx);
env->SetFloatField(params,fyField,intr.fy);
env->SetFloatField(params,ppxField,intr.ppx);
env->SetFloatField(params,ppyField,intr.ppy);

env->SetIntField(params, widthField, width);
env->SetIntField(params, heightField, height);
}

extern "C" JNIEXPORT void JNICALL
Expand Down
4 changes: 2 additions & 2 deletions tools/terminal/auto-complete.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.

#include "auto-complete.h"
#include <iostream>
#include <thread>
#include <algorithm>
#include <string>
#include <cmath>
#include "auto-complete.h"

using namespace std;


Expand Down
1 change: 0 additions & 1 deletion wrappers/android/librealsense/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.intel.realsense.librealsense">
<uses-permission android:name="android.permission.CAMERA"/>
<application android:allowBackup="true" android:supportsRtl="true">
<activity android:name=".DeviceWatcherActivity"
android:theme="@android:style/Theme.NoDisplay"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class Device extends LrsClass {
private List<Sensor> _sensors = new ArrayList<>();

public Device(long handle){
Device(long handle){
mHandle = handle;
long[] sensorsHandles = nQuerySensors(mHandle);
for(long h : sensorsHandles){
Expand Down Expand Up @@ -42,8 +42,16 @@ public byte[] serializePresetToJson(){
return nSerializePresetToJson(mHandle);
}

public <T extends Device> T as(Class<T> type) {
return (T) this;
public boolean is(Extension extension) {
return nIsDeviceExtendableTo(mHandle, extension.value());
}

public <T extends Device> T as(Extension extension) {
switch (extension){
case UPDATABLE: return (T) new Updatable(mHandle);
case UPDATE_DEVICE: return (T) new UpdateDevice(mHandle);
}
throw new RuntimeException("this device is not extendable to " + extension.name());
}

public void hardwareReset(){
Expand All @@ -54,7 +62,8 @@ public void hardwareReset(){
public void close() {
for (Sensor s : _sensors)
s.close();
nRelease(mHandle);
if(mOwner)
nRelease(mHandle);
}

private static native boolean nSupportsInfo(long handle, int info);
Expand All @@ -65,5 +74,6 @@ public void close() {
private static native byte[] nSerializePresetToJson(long handle);
private static native long[] nQuerySensors(long handle);
private static native void nHardwareReset(long handle);
private static native boolean nIsDeviceExtendableTo(long handle, int extension);
private static native void nRelease(long handle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ public int getDeviceCount(){

public Device createDevice(int index){
long deviceHandle = nCreateDevice(mHandle, index);
if (nIsDeviceExtendableTo(deviceHandle, Extension.UPDATABLE.value()))
return new Updatable(deviceHandle);
if (nIsDeviceExtendableTo(deviceHandle, Extension.UPDATE_DEVICE.value()))
return new UpdateDevice(deviceHandle);
return new Device(deviceHandle);
}

Expand All @@ -37,7 +33,6 @@ public void close() {
nRelease(mHandle);
}

private static native boolean nIsDeviceExtendableTo(long handle, int extension);
private static native int nGetDeviceCount(long handle);
private static native long nCreateDevice(long handle, int index);
private static native boolean nContainsDevice(long handle, long deviceHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class DeviceWatcher extends LrsClass {
private final List<DeviceListener> mAppDeviceListener;

public synchronized void addListener(DeviceListener deviceListener){
if(!mAppDeviceListener.contains(deviceListener))
if(!mAppDeviceListener.contains(deviceListener)) {
mAppDeviceListener.add(deviceListener);
if(getDeviceCount() > 0)
deviceListener.onDeviceAttach();
}
}

public synchronized void removeListener(DeviceListener deviceListener){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract class LrsClass implements AutoCloseable {
}

protected long mHandle = 0;
protected boolean mOwner = true;

public long getHandle() { return mHandle; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.intel.realsense.librealsense;

public enum ProductClass {
public enum ProductLine {
ANY(0xff),
ANY_INTEL(0xfe),
NON_INTEL(0x01),
Expand All @@ -13,6 +13,6 @@ public enum ProductClass {

private final int mValue;

private ProductClass(int value) { mValue = value; }
private ProductLine(int value) { mValue = value; }
public int value() { return mValue; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public int getDeviceCount() {
}

public DeviceList queryDevices() {
return queryDevices(ProductClass.ANY_INTEL);
return queryDevices(ProductLine.ANY_INTEL);
}

public DeviceList queryDevices(ProductClass productClass) {
return new DeviceList(nQueryDevices(mHandle, productClass.value()));
public DeviceList queryDevices(ProductLine productLine) {
return new DeviceList(nQueryDevices(mHandle, productLine.value()));
}

public synchronized void setDevicesChangedCallback(DeviceListener listener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package com.intel.realsense.librealsense;

public class Updatable extends Device {
private ProgressListener mListener;

public void enterUpdateState() {
nEnterUpdateState(mHandle);
}

public synchronized byte[] createFlashBackup(ProgressListener listener){
mListener = listener;
return nCreateFlashBackup(mHandle);
}

Updatable(long handle){
super(handle);
mOwner = false;
}

void onProgress(float progress){
mListener.onProgress(progress);
}

private static native void nEnterUpdateState(long handle);
private native byte[] nCreateFlashBackup(long handle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public synchronized void update(byte[] image, ProgressListener listener){

UpdateDevice(long handle){
super(handle);
mOwner = false;
}

void onProgress(float progress){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,26 @@
package com.intel.realsense.librealsense;

public class VideoStreamProfile extends StreamProfile {
IntrinsicsParams mIntrinsicsParams;
ResolutionParams mResolutionParams;

private class IntrinsicsParams {
private class ResolutionParams {
public int width;
public int height;
public float fx;
public float fy;
public float ppx;
public float ppy;
}

VideoStreamProfile(long handle) {
super(handle);
mIntrinsicsParams = new IntrinsicsParams();
nGetIntrinsics(mHandle, mIntrinsicsParams);
mResolutionParams = new ResolutionParams();
nGetResolution(mHandle, mResolutionParams);
}

public int getWidth() {
return mIntrinsicsParams.width;
return mResolutionParams.width;
}

public int getHeight() {
return mIntrinsicsParams.height;
return mResolutionParams.height;
}

public float getFx() {
return mIntrinsicsParams.fx;
}

public float getFy() {
return mIntrinsicsParams.fy;
}

public float getPpx() {
return mIntrinsicsParams.ppx;
}

public float getPpy() {return mIntrinsicsParams.ppy; }


private static native void nGetIntrinsics(long handle, IntrinsicsParams params);
private static native void nGetResolution(long handle, ResolutionParams params);
}
30 changes: 30 additions & 0 deletions wrappers/android/tools/camera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ android {
}
}

def getFwVersion(String productLine) {
try {
String versionsFile = new File("$projectDir/../../../../common/fw/firmware-version.h").text
def regex = productLine + " \"(.*)\""
def matcher = ( versionsFile =~ regex )
return matcher[0][1]
}catch(e){
println(e)
return "Unknown FW version"
}
}

task downloadFirmware() {
mkdir "$projectDir/src/main/res/raw"

def d4xxFwVersion = getFwVersion('D4XX_RECOMMENDED_FIRMWARE_VERSION')
def d4xxFile = new File("$projectDir/src/main/res/raw/fw_d4xx.bin")
if (!d4xxFile.exists()) {
println("downloading D4xx fw: " + d4xxFwVersion)
new URL('http://realsense-hw-public.s3-eu-west-1.amazonaws.com/Releases/RS4xx/FW/D4XX_FW_Image-' + d4xxFwVersion + '.bin').withInputStream{ i -> d4xxFile.withOutputStream{ it << i }}
}

def sr3xxFwVersion = getFwVersion('SR3XX_RECOMMENDED_FIRMWARE_VERSION')
def sr3xxFile = new File("$projectDir/src/main/res/raw/fw_sr3xx.bin")
if (!sr3xxFile.exists()) {
println("downloading SR3xx fw: " + sr3xxFwVersion)
new URL('http://realsense-hw-public.s3-eu-west-1.amazonaws.com/Releases/SR300/FW/SR3XX_FW_Image-' + sr3xxFwVersion + '.bin').withInputStream{ i -> sr3xxFile.withOutputStream{ it << i }}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.0.0'
Expand Down
Loading

0 comments on commit bb2d64e

Please sign in to comment.