Skip to content

Instantly share code, notes, and snippets.

@jribal
Last active March 19, 2019 09:13
Show Gist options
  • Save jribal/140ccbde7c54f6a434fa6b52cacace2f to your computer and use it in GitHub Desktop.
Save jribal/140ccbde7c54f6a434fa6b52cacace2f to your computer and use it in GitHub Desktop.
libsubsurface-bridge.c
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
int import_thread_cancelled = 0;
typedef struct dc_user_device_t
{
dc_descriptor_t *descriptor;
const char *vendor, *product, *devname;
const char *model, *btname;
unsigned char *fingerprint;
unsigned int fsize, fdiveid;
uint32_t libdc_firmware, libdc_serial;
uint32_t deviceid, diveid;
dc_device_t *device;
dc_context_t *context;
dc_iostream_t *iostream;
struct dive_trip *trip;
int preexisting;
bool force_download;
bool create_new_trip;
bool libdc_log;
bool libdc_dump;
bool bluetooth_mode;
FILE *libdc_logfile;
struct dive_table *download_table;
} device_data_t;
static int cancel_cb(void *userdata)
{
LOGD("%s", "cancel cb");
return import_thread_cancelled;
}
static void event_cb(dc_device_t *device, dc_event_type_t event, const void *data, void *userdata, char *test)
{
LOGD("%s", "event cb");
switch (event) {
case DC_EVENT_WAITING:
LOGD("%s", "event cb waiting");
break;
case DC_EVENT_PROGRESS:
LOGD("%s %g %g", "event cb progress", (double)progress->maximum, (double)progress->current / (double)progress->maximum);
if (!progress->maximum)
break;
progress_bar_fraction = (double)progress->current / (double)progress->maximum;
break;
case DC_EVENT_DEVINFO:
LOGD("%s", "event cb devinfo");
break;
case DC_EVENT_CLOCK:
LOGD("%s", "event cb clock");
break;
case DC_EVENT_VENDOR:
LOGD("%s", "event cb vendor");
break;
default:
break;
}
}
static dc_status_t get_available(void *io, size_t *available)
{
LOGD("%s", "serial get available");
return DC_STATUS_SUCCESS;
}
static dc_status_t set_timeout(void *io, int timeout)
{
LOGD("%s %d", "serial set timeout", timeout);
return DC_STATUS_SUCCESS;
}
static dc_status_t custom_sleep(void *io, unsigned int timeout)
{
LOGD("%s %d", "serial sleep", timeout);
return DC_STATUS_SUCCESS;
}
static dc_status_t serial_write(void *io, const void *data, size_t size, size_t *actual)
{
LOGD("%s %d", "serial write", size);
JNIEnv *env = envShared;
jobject obj = objShared;
jbyteArray jb;
jb=(*env)->NewByteArray(env, size);
(*env)->SetByteArrayRegion(env, jb, 0, size, data);
(*env)->CallVoidMethod(env, obj, writeToSocket, jb);
(*env)->DeleteLocalRef(env, jb);
return DC_STATUS_SUCCESS;
}
static dc_status_t serial_read(void *io, void *data, size_t size, size_t *actual)
{
LOGD("%s", "serial read");
return DC_STATUS_SUCCESS;
}
static dc_status_t serial_close(void *io)
{
LOGD("%s", "serial close");
return DC_STATUS_SUCCESS;
}
static dc_status_t serial_purge(void *userdata, dc_direction_t direction){
LOGD("%s", "serial purge");
return DC_STATUS_SUCCESS;
}
int dive_cb(const unsigned char *data, unsigned int size,
const unsigned char *fingerprint, unsigned int fsize,
void *userdata)
{
LOGD("%s", "dive cb");
}
jstring
Java_com_test_test_Test_libsubsurfaceExtract( JNIEnv* env,
jobject obj, jlong bluetoothsocket )
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif
envShared = env;
objShared = obj;
jclass parentClass = (*env)->GetObjectClass(env, obj);
if (parentClass == NULL)
LOGD("%s", "class is null");
writeToSocket = (*env)->GetMethodID(env, parentClass, "writeToSocket", "([B)V");
device_data_t data;
dc_iterator_t *iterator;
memset(&data, 0, sizeof(data));
data.trip = NULL;
data.download_table = NULL;
data.diveid = 0;
data.deviceid = 0;
data.bluetooth_mode = true;
data.btname = "OSTC+ 15184";
data.devname = "OSTC+ 15184";
data.libdc_dump = false;
data.libdc_log = true;
data.force_download = true;
if (dc_context_new(&data.context) == 0) {
if (dc_descriptor_iterator(&iterator) == 0){
while (dc_iterator_next(iterator, &data.descriptor) == 0){
const char *deviceName =dc_descriptor_get_product(data.descriptor);
if (strcmp(deviceName,"OSTC Sport") == 0 && dc_descriptor_get_model(data.descriptor) == 19) {
static const dc_custom_cbs_t callbacks = {
set_timeout, /* set_timeout */
NULL, /* set_latency */
NULL, /* set_break */
NULL, /* set_dtr */
NULL, /* set_rts */
NULL, /* get_lines */
get_available, /* get_received */
NULL, /* configure */
serial_read, /* read */
serial_write, /* write */
NULL, /* flush */
serial_purge, /* purge */
custom_sleep, /* sleep */
serial_close, /* close */
};
if (dc_custom_open(&data.iostream, data.context, DC_TRANSPORT_BLUETOOTH, &callbacks, (void *)bluetoothsocket) == 0 ) {
if (dc_device_open(&data.device, data.context, data.descriptor, data.iostream) == 0) {
dc_device_t *device = data.device;
LOGD("%s %s %d %d %d", "Device open success", dc_descriptor_get_product(data.descriptor),dc_descriptor_get_transports(data.descriptor), dc_descriptor_get_model(data.descriptor), dc_descriptor_get_type(data.descriptor) );
switch(dc_descriptor_get_type (data.descriptor)){
case DC_FAMILY_HW_OSTC3:
LOGD("%s", "ostc3 device open");
break;
case DC_FAMILY_HW_OSTC:
LOGD("%s", "ostc device open");
break;
default:
LOGD("%s %d", "unknown family", dc_descriptor_get_type (data.descriptor));
}
unsigned int events = DC_EVENT_WAITING | DC_EVENT_PROGRESS | DC_EVENT_DEVINFO | DC_EVENT_CLOCK | DC_EVENT_VENDOR;
if (dc_device_set_events(device, events , event_cb, &data) == 0){
LOGD("%s", "event success");
if (dc_device_set_cancel(device, cancel_cb, &data) == 0){
LOGD("%s", "cancel success");
if (dc_device_foreach(device, dive_cb, &data) == 0){
LOGD("%s", "foreach success");
}
else
LOGD("%s", "foreach failure");
}
else
LOGD("%s", "cancel failure");
}
else
LOGD("%s", "event failure");
}
else
LOGD("%s", "device open failure");
}
}
dc_descriptor_free(data.descriptor);
}
}
else{
return (*env)->NewStringUTF(env, "iterator failure");
}
}
else{
return (*env)->NewStringUTF(env, "context new failure");
}
return (*env)->NewStringUTF(env, "Hello from JNI test ! Compiled with ABI " ABI "., status is :");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment