2010-06-29 16:42:13 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "MtpServerJNI"
|
|
|
|
#include "utils/Log.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <utils/threads.h>
|
|
|
|
|
|
|
|
#include "jni.h"
|
2017-07-19 09:50:45 -07:00
|
|
|
#include <nativehelper/JNIHelp.h>
|
2010-06-29 16:42:13 -04:00
|
|
|
#include "android_runtime/AndroidRuntime.h"
|
2010-07-02 15:15:07 -04:00
|
|
|
#include "private/android_filesystem_config.h"
|
2010-06-29 16:42:13 -04:00
|
|
|
|
|
|
|
#include "MtpServer.h"
|
2011-02-18 09:07:14 -05:00
|
|
|
#include "MtpStorage.h"
|
2010-06-29 16:42:13 -04:00
|
|
|
|
2010-06-30 17:49:41 -04:00
|
|
|
using namespace android;
|
2010-06-29 16:42:13 -04:00
|
|
|
|
2011-07-11 15:04:38 -04:00
|
|
|
// MtpServer fields
|
|
|
|
static jfieldID field_MtpServer_nativeContext;
|
2011-04-05 10:21:27 -04:00
|
|
|
|
|
|
|
// MtpStorage fields
|
|
|
|
static jfieldID field_MtpStorage_storageId;
|
|
|
|
static jfieldID field_MtpStorage_path;
|
|
|
|
static jfieldID field_MtpStorage_description;
|
2011-05-09 20:16:05 -07:00
|
|
|
static jfieldID field_MtpStorage_removable;
|
2011-07-11 09:18:03 -04:00
|
|
|
static jfieldID field_MtpStorage_maxFileSize;
|
2011-04-05 10:21:27 -04:00
|
|
|
|
|
|
|
static Mutex sMutex;
|
|
|
|
|
2010-06-29 16:42:13 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-12-30 11:54:33 -05:00
|
|
|
// in android_mtp_MtpDatabase.cpp
|
2017-08-16 18:07:51 -07:00
|
|
|
extern IMtpDatabase* getMtpDatabase(JNIEnv *env, jobject database);
|
2010-06-29 16:42:13 -04:00
|
|
|
|
2011-07-11 15:04:38 -04:00
|
|
|
static inline MtpServer* getMtpServer(JNIEnv *env, jobject thiz) {
|
2013-12-17 19:04:19 +00:00
|
|
|
return (MtpServer*)env->GetLongField(thiz, field_MtpServer_nativeContext);
|
2010-06-29 16:42:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-12-06 16:03:57 -08:00
|
|
|
android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jobject jControlFd,
|
|
|
|
jboolean usePtp, jstring deviceInfoManufacturer, jstring deviceInfoModel,
|
|
|
|
jstring deviceInfoDeviceVersion, jstring deviceInfoSerialNumber)
|
2010-06-29 16:42:13 -04:00
|
|
|
{
|
2016-12-21 11:19:52 -08:00
|
|
|
const char *deviceInfoManufacturerStr = env->GetStringUTFChars(deviceInfoManufacturer, NULL);
|
|
|
|
const char *deviceInfoModelStr = env->GetStringUTFChars(deviceInfoModel, NULL);
|
|
|
|
const char *deviceInfoDeviceVersionStr = env->GetStringUTFChars(deviceInfoDeviceVersion, NULL);
|
|
|
|
const char *deviceInfoSerialNumberStr = env->GetStringUTFChars(deviceInfoSerialNumber, NULL);
|
2017-12-06 16:03:57 -08:00
|
|
|
int controlFd = dup(jniGetFDFromFileDescriptor(env, jControlFd));
|
|
|
|
MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase), controlFd,
|
2017-10-17 13:47:51 -07:00
|
|
|
usePtp,
|
2018-03-27 15:29:09 -07:00
|
|
|
(deviceInfoManufacturerStr != NULL) ? deviceInfoManufacturerStr : "",
|
|
|
|
(deviceInfoModelStr != NULL) ? deviceInfoModelStr : "",
|
|
|
|
(deviceInfoDeviceVersionStr != NULL) ? deviceInfoDeviceVersionStr : "",
|
|
|
|
(deviceInfoSerialNumberStr != NULL) ? deviceInfoSerialNumberStr : "");
|
2016-12-21 11:19:52 -08:00
|
|
|
if (deviceInfoManufacturerStr != NULL) {
|
|
|
|
env->ReleaseStringUTFChars(deviceInfoManufacturer, deviceInfoManufacturerStr);
|
|
|
|
}
|
|
|
|
if (deviceInfoModelStr != NULL) {
|
|
|
|
env->ReleaseStringUTFChars(deviceInfoModel, deviceInfoModelStr);
|
|
|
|
}
|
|
|
|
if (deviceInfoDeviceVersionStr != NULL) {
|
|
|
|
env->ReleaseStringUTFChars(deviceInfoDeviceVersion, deviceInfoDeviceVersionStr);
|
|
|
|
}
|
|
|
|
if (deviceInfoSerialNumberStr != NULL) {
|
|
|
|
env->ReleaseStringUTFChars(deviceInfoSerialNumber, deviceInfoSerialNumberStr);
|
|
|
|
}
|
2016-10-24 14:35:08 -07:00
|
|
|
env->SetLongField(thiz, field_MtpServer_nativeContext, (jlong)server);
|
2010-06-29 16:42:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-07-11 15:04:38 -04:00
|
|
|
android_mtp_MtpServer_run(JNIEnv *env, jobject thiz)
|
2010-06-29 16:42:13 -04:00
|
|
|
{
|
2011-07-11 15:04:38 -04:00
|
|
|
MtpServer* server = getMtpServer(env, thiz);
|
|
|
|
if (server)
|
|
|
|
server->run();
|
|
|
|
else
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("server is null in run");
|
2010-06-29 16:42:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-07-11 15:04:38 -04:00
|
|
|
android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz)
|
2010-06-29 16:42:13 -04:00
|
|
|
{
|
2011-07-11 15:04:38 -04:00
|
|
|
Mutex::Autolock autoLock(sMutex);
|
|
|
|
|
|
|
|
MtpServer* server = getMtpServer(env, thiz);
|
|
|
|
if (server) {
|
|
|
|
delete server;
|
2013-12-17 19:04:19 +00:00
|
|
|
env->SetLongField(thiz, field_MtpServer_nativeContext, 0);
|
2011-07-11 15:04:38 -04:00
|
|
|
} else {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("server is null in cleanup");
|
2011-01-25 09:29:27 -08:00
|
|
|
}
|
2010-06-29 16:42:13 -04:00
|
|
|
}
|
|
|
|
|
2010-07-12 18:54:16 -04:00
|
|
|
static void
|
2010-12-30 11:54:33 -05:00
|
|
|
android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
|
2010-07-12 18:54:16 -04:00
|
|
|
{
|
2011-07-11 15:04:38 -04:00
|
|
|
Mutex::Autolock autoLock(sMutex);
|
|
|
|
|
|
|
|
MtpServer* server = getMtpServer(env, thiz);
|
|
|
|
if (server)
|
|
|
|
server->sendObjectAdded(handle);
|
|
|
|
else
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("server is null in send_object_added");
|
2010-07-12 18:54:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-12-30 11:54:33 -05:00
|
|
|
android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle)
|
2010-07-12 18:54:16 -04:00
|
|
|
{
|
2011-07-11 15:04:38 -04:00
|
|
|
Mutex::Autolock autoLock(sMutex);
|
|
|
|
|
|
|
|
MtpServer* server = getMtpServer(env, thiz);
|
|
|
|
if (server)
|
|
|
|
server->sendObjectRemoved(handle);
|
|
|
|
else
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("server is null in send_object_removed");
|
2010-07-12 18:54:16 -04:00
|
|
|
}
|
|
|
|
|
2014-03-07 13:29:08 -08:00
|
|
|
static void
|
|
|
|
android_mtp_MtpServer_send_device_property_changed(JNIEnv *env, jobject thiz, jint property)
|
|
|
|
{
|
|
|
|
Mutex::Autolock autoLock(sMutex);
|
|
|
|
|
|
|
|
MtpServer* server = getMtpServer(env, thiz);
|
|
|
|
if (server)
|
|
|
|
server->sendDevicePropertyChanged(property);
|
|
|
|
else
|
|
|
|
ALOGE("server is null in send_object_removed");
|
|
|
|
}
|
|
|
|
|
2011-02-18 13:24:01 -05:00
|
|
|
static void
|
2011-04-05 10:21:27 -04:00
|
|
|
android_mtp_MtpServer_add_storage(JNIEnv *env, jobject thiz, jobject jstorage)
|
2011-02-18 13:24:01 -05:00
|
|
|
{
|
2011-07-11 15:04:38 -04:00
|
|
|
Mutex::Autolock autoLock(sMutex);
|
|
|
|
|
|
|
|
MtpServer* server = getMtpServer(env, thiz);
|
|
|
|
if (server) {
|
2011-04-05 10:21:27 -04:00
|
|
|
jint storageID = env->GetIntField(jstorage, field_MtpStorage_storageId);
|
|
|
|
jstring path = (jstring)env->GetObjectField(jstorage, field_MtpStorage_path);
|
|
|
|
jstring description = (jstring)env->GetObjectField(jstorage, field_MtpStorage_description);
|
2011-05-09 20:16:05 -07:00
|
|
|
jboolean removable = env->GetBooleanField(jstorage, field_MtpStorage_removable);
|
2011-07-11 09:18:03 -04:00
|
|
|
jlong maxFileSize = env->GetLongField(jstorage, field_MtpStorage_maxFileSize);
|
2011-04-05 10:21:27 -04:00
|
|
|
|
|
|
|
const char *pathStr = env->GetStringUTFChars(path, NULL);
|
2011-04-06 11:57:48 -07:00
|
|
|
if (pathStr != NULL) {
|
|
|
|
const char *descriptionStr = env->GetStringUTFChars(description, NULL);
|
|
|
|
if (descriptionStr != NULL) {
|
2011-07-11 09:18:03 -04:00
|
|
|
MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr,
|
2017-08-16 18:07:51 -07:00
|
|
|
removable, maxFileSize);
|
2011-07-11 15:04:38 -04:00
|
|
|
server->addStorage(storage);
|
2011-04-06 11:57:48 -07:00
|
|
|
env->ReleaseStringUTFChars(path, pathStr);
|
|
|
|
env->ReleaseStringUTFChars(description, descriptionStr);
|
|
|
|
} else {
|
|
|
|
env->ReleaseStringUTFChars(path, pathStr);
|
|
|
|
}
|
|
|
|
}
|
2011-04-05 10:21:27 -04:00
|
|
|
} else {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("server is null in add_storage");
|
2011-04-05 10:21:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId)
|
|
|
|
{
|
2011-07-11 15:04:38 -04:00
|
|
|
Mutex::Autolock autoLock(sMutex);
|
|
|
|
|
|
|
|
MtpServer* server = getMtpServer(env, thiz);
|
|
|
|
if (server) {
|
|
|
|
MtpStorage* storage = server->getStorage(storageId);
|
|
|
|
if (storage) {
|
|
|
|
server->removeStorage(storage);
|
|
|
|
delete storage;
|
|
|
|
}
|
|
|
|
} else
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("server is null in remove_storage");
|
2011-02-18 13:24:01 -05:00
|
|
|
}
|
|
|
|
|
2010-06-29 16:42:13 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-09-19 17:31:01 -04:00
|
|
|
static const JNINativeMethod gMethods[] = {
|
2017-12-06 16:03:57 -08:00
|
|
|
{"native_setup", "(Landroid/mtp/MtpDatabase;Ljava/io/FileDescriptor;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
|
2010-12-30 11:54:33 -05:00
|
|
|
(void *)android_mtp_MtpServer_setup},
|
2011-07-11 15:04:38 -04:00
|
|
|
{"native_run", "()V", (void *)android_mtp_MtpServer_run},
|
|
|
|
{"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup},
|
2010-12-30 11:54:33 -05:00
|
|
|
{"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added},
|
|
|
|
{"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
|
2014-03-07 13:29:08 -08:00
|
|
|
{"native_send_device_property_changed", "(I)V",
|
|
|
|
(void *)android_mtp_MtpServer_send_device_property_changed},
|
2011-04-05 10:21:27 -04:00
|
|
|
{"native_add_storage", "(Landroid/mtp/MtpStorage;)V",
|
|
|
|
(void *)android_mtp_MtpServer_add_storage},
|
|
|
|
{"native_remove_storage", "(I)V", (void *)android_mtp_MtpServer_remove_storage},
|
2010-06-29 16:42:13 -04:00
|
|
|
};
|
|
|
|
|
2010-12-30 11:54:33 -05:00
|
|
|
int register_android_mtp_MtpServer(JNIEnv *env)
|
2010-06-29 16:42:13 -04:00
|
|
|
{
|
|
|
|
jclass clazz;
|
|
|
|
|
2011-04-05 10:21:27 -04:00
|
|
|
clazz = env->FindClass("android/mtp/MtpStorage");
|
|
|
|
if (clazz == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find android/mtp/MtpStorage");
|
2011-04-05 10:21:27 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
field_MtpStorage_storageId = env->GetFieldID(clazz, "mStorageId", "I");
|
|
|
|
if (field_MtpStorage_storageId == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find MtpStorage.mStorageId");
|
2011-04-05 10:21:27 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
field_MtpStorage_path = env->GetFieldID(clazz, "mPath", "Ljava/lang/String;");
|
|
|
|
if (field_MtpStorage_path == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find MtpStorage.mPath");
|
2011-04-05 10:21:27 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
field_MtpStorage_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;");
|
|
|
|
if (field_MtpStorage_description == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find MtpStorage.mDescription");
|
2011-04-05 10:21:27 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-09 20:16:05 -07:00
|
|
|
field_MtpStorage_removable = env->GetFieldID(clazz, "mRemovable", "Z");
|
|
|
|
if (field_MtpStorage_removable == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find MtpStorage.mRemovable");
|
2011-04-05 10:21:27 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-07-11 09:18:03 -04:00
|
|
|
field_MtpStorage_maxFileSize = env->GetFieldID(clazz, "mMaxFileSize", "J");
|
|
|
|
if (field_MtpStorage_maxFileSize == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find MtpStorage.mMaxFileSize");
|
2011-07-11 09:18:03 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-04-05 10:21:27 -04:00
|
|
|
|
2010-12-30 11:54:33 -05:00
|
|
|
clazz = env->FindClass("android/mtp/MtpServer");
|
2010-06-29 16:42:13 -04:00
|
|
|
if (clazz == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find android/mtp/MtpServer");
|
2010-06-29 16:42:13 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-12-17 19:04:19 +00:00
|
|
|
field_MtpServer_nativeContext = env->GetFieldID(clazz, "mNativeContext", "J");
|
2011-07-11 15:04:38 -04:00
|
|
|
if (field_MtpServer_nativeContext == NULL) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Can't find MtpServer.mNativeContext");
|
2011-07-11 15:04:38 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-06-29 16:42:13 -04:00
|
|
|
|
|
|
|
return AndroidRuntime::registerNativeMethods(env,
|
2010-12-30 11:54:33 -05:00
|
|
|
"android/mtp/MtpServer", gMethods, NELEM(gMethods));
|
2010-06-29 16:42:13 -04:00
|
|
|
}
|