blob: 39ff04ab2a0a53fbae670a3723e99a6729542252 [file] [log] [blame]
Mike Lockwood98ef64e2010-06-29 16:42:13 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "MtpServerJNI"
18#include "utils/Log.h"
19
20#include <stdio.h>
21#include <assert.h>
22#include <limits.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <utils/threads.h>
26
27#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070028#include <nativehelper/JNIHelp.h>
Mike Lockwood98ef64e2010-06-29 16:42:13 -040029#include "android_runtime/AndroidRuntime.h"
Mike Lockwooddad69272010-07-02 15:15:07 -040030#include "private/android_filesystem_config.h"
Mike Lockwood98ef64e2010-06-29 16:42:13 -040031
32#include "MtpServer.h"
Mike Lockwood467ca0d2011-02-18 09:07:14 -050033#include "MtpStorage.h"
Mike Lockwood98ef64e2010-06-29 16:42:13 -040034
Mike Lockwood81ea83d2010-06-30 17:49:41 -040035using namespace android;
Mike Lockwood98ef64e2010-06-29 16:42:13 -040036
Mike Lockwooddcc31942011-07-11 15:04:38 -040037// MtpServer fields
38static jfieldID field_MtpServer_nativeContext;
Mike Lockwoodb239b6832011-04-05 10:21:27 -040039
40// MtpStorage fields
41static jfieldID field_MtpStorage_storageId;
42static jfieldID field_MtpStorage_path;
43static jfieldID field_MtpStorage_description;
Mike Lockwood51690542011-05-09 20:16:05 -070044static jfieldID field_MtpStorage_removable;
Mike Lockwood7a59dd22011-07-11 09:18:03 -040045static jfieldID field_MtpStorage_maxFileSize;
Mike Lockwoodb239b6832011-04-05 10:21:27 -040046
47static Mutex sMutex;
48
Mike Lockwood98ef64e2010-06-29 16:42:13 -040049// ----------------------------------------------------------------------------
50
Mike Lockwood0cd01362010-12-30 11:54:33 -050051// in android_mtp_MtpDatabase.cpp
Jerry Zhangf9c5c252017-08-16 18:07:51 -070052extern IMtpDatabase* getMtpDatabase(JNIEnv *env, jobject database);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040053
Mike Lockwooddcc31942011-07-11 15:04:38 -040054static inline MtpServer* getMtpServer(JNIEnv *env, jobject thiz) {
Ashok Bhate2e59322013-12-17 19:04:19 +000055 return (MtpServer*)env->GetLongField(thiz, field_MtpServer_nativeContext);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040056}
57
Mike Lockwood98ef64e2010-06-29 16:42:13 -040058static void
Jerry Zhang6d319b82017-12-06 16:03:57 -080059android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jobject jControlFd,
60 jboolean usePtp, jstring deviceInfoManufacturer, jstring deviceInfoModel,
61 jstring deviceInfoDeviceVersion, jstring deviceInfoSerialNumber)
Mike Lockwood98ef64e2010-06-29 16:42:13 -040062{
Alex Klyubinabdc2b42016-12-21 11:19:52 -080063 const char *deviceInfoManufacturerStr = env->GetStringUTFChars(deviceInfoManufacturer, NULL);
64 const char *deviceInfoModelStr = env->GetStringUTFChars(deviceInfoModel, NULL);
65 const char *deviceInfoDeviceVersionStr = env->GetStringUTFChars(deviceInfoDeviceVersion, NULL);
66 const char *deviceInfoSerialNumberStr = env->GetStringUTFChars(deviceInfoSerialNumber, NULL);
Jerry Zhang6d319b82017-12-06 16:03:57 -080067 int controlFd = dup(jniGetFDFromFileDescriptor(env, jControlFd));
68 MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase), controlFd,
Jerry Zhangdef7b1932017-10-17 13:47:51 -070069 usePtp,
Jerry Zhangd9f30052018-03-27 15:29:09 -070070 (deviceInfoManufacturerStr != NULL) ? deviceInfoManufacturerStr : "",
71 (deviceInfoModelStr != NULL) ? deviceInfoModelStr : "",
72 (deviceInfoDeviceVersionStr != NULL) ? deviceInfoDeviceVersionStr : "",
73 (deviceInfoSerialNumberStr != NULL) ? deviceInfoSerialNumberStr : "");
Alex Klyubinabdc2b42016-12-21 11:19:52 -080074 if (deviceInfoManufacturerStr != NULL) {
75 env->ReleaseStringUTFChars(deviceInfoManufacturer, deviceInfoManufacturerStr);
76 }
77 if (deviceInfoModelStr != NULL) {
78 env->ReleaseStringUTFChars(deviceInfoModel, deviceInfoModelStr);
79 }
80 if (deviceInfoDeviceVersionStr != NULL) {
81 env->ReleaseStringUTFChars(deviceInfoDeviceVersion, deviceInfoDeviceVersionStr);
82 }
83 if (deviceInfoSerialNumberStr != NULL) {
84 env->ReleaseStringUTFChars(deviceInfoSerialNumber, deviceInfoSerialNumberStr);
85 }
Jerry Zhangbb598ee2016-10-24 14:35:08 -070086 env->SetLongField(thiz, field_MtpServer_nativeContext, (jlong)server);
Mike Lockwooddcc31942011-07-11 15:04:38 -040087}
88
89static void
90android_mtp_MtpServer_run(JNIEnv *env, jobject thiz)
91{
92 MtpServer* server = getMtpServer(env, thiz);
93 if (server)
94 server->run();
95 else
Steve Block3762c312012-01-06 19:20:56 +000096 ALOGE("server is null in run");
Mike Lockwooddcc31942011-07-11 15:04:38 -040097}
98
99static void
100android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz)
101{
102 Mutex::Autolock autoLock(sMutex);
103
104 MtpServer* server = getMtpServer(env, thiz);
105 if (server) {
106 delete server;
Ashok Bhate2e59322013-12-17 19:04:19 +0000107 env->SetLongField(thiz, field_MtpServer_nativeContext, 0);
Mike Lockwooddcc31942011-07-11 15:04:38 -0400108 } else {
Steve Block3762c312012-01-06 19:20:56 +0000109 ALOGE("server is null in cleanup");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400110 }
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400111}
112
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400113static void
Mike Lockwood0cd01362010-12-30 11:54:33 -0500114android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400115{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400116 Mutex::Autolock autoLock(sMutex);
117
118 MtpServer* server = getMtpServer(env, thiz);
119 if (server)
120 server->sendObjectAdded(handle);
121 else
Steve Block3762c312012-01-06 19:20:56 +0000122 ALOGE("server is null in send_object_added");
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400123}
124
125static void
Mike Lockwood0cd01362010-12-30 11:54:33 -0500126android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle)
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400127{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400128 Mutex::Autolock autoLock(sMutex);
129
130 MtpServer* server = getMtpServer(env, thiz);
131 if (server)
132 server->sendObjectRemoved(handle);
133 else
Steve Block3762c312012-01-06 19:20:56 +0000134 ALOGE("server is null in send_object_removed");
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400135}
136
Mike Lockwoodeabe8bf2010-08-31 14:35:23 -0400137static void
Jamese4f680e2018-07-02 17:42:07 +0800138android_mtp_MtpServer_send_object_info_changed(JNIEnv *env, jobject thiz, jint handle)
139{
140 Mutex::Autolock autoLock(sMutex);
141
142 MtpServer* server = getMtpServer(env, thiz);
143 if (server)
144 server->sendObjectInfoChanged(handle);
145 else
146 ALOGE("server is null in send_object_info_changed");
147}
148
149static void
Mike Lockwood56c85242014-03-07 13:29:08 -0800150android_mtp_MtpServer_send_device_property_changed(JNIEnv *env, jobject thiz, jint property)
151{
152 Mutex::Autolock autoLock(sMutex);
153
154 MtpServer* server = getMtpServer(env, thiz);
155 if (server)
156 server->sendDevicePropertyChanged(property);
157 else
158 ALOGE("server is null in send_object_removed");
159}
160
161static void
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400162android_mtp_MtpServer_add_storage(JNIEnv *env, jobject thiz, jobject jstorage)
Mike Lockwood66e57f62011-02-18 13:24:01 -0500163{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400164 Mutex::Autolock autoLock(sMutex);
165
166 MtpServer* server = getMtpServer(env, thiz);
167 if (server) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400168 jint storageID = env->GetIntField(jstorage, field_MtpStorage_storageId);
169 jstring path = (jstring)env->GetObjectField(jstorage, field_MtpStorage_path);
170 jstring description = (jstring)env->GetObjectField(jstorage, field_MtpStorage_description);
Mike Lockwood51690542011-05-09 20:16:05 -0700171 jboolean removable = env->GetBooleanField(jstorage, field_MtpStorage_removable);
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400172 jlong maxFileSize = env->GetLongField(jstorage, field_MtpStorage_maxFileSize);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400173
174 const char *pathStr = env->GetStringUTFChars(path, NULL);
James Dong39774722011-04-06 11:57:48 -0700175 if (pathStr != NULL) {
176 const char *descriptionStr = env->GetStringUTFChars(description, NULL);
177 if (descriptionStr != NULL) {
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400178 MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr,
Jerry Zhangf9c5c252017-08-16 18:07:51 -0700179 removable, maxFileSize);
Mike Lockwooddcc31942011-07-11 15:04:38 -0400180 server->addStorage(storage);
James Dong39774722011-04-06 11:57:48 -0700181 env->ReleaseStringUTFChars(path, pathStr);
182 env->ReleaseStringUTFChars(description, descriptionStr);
183 } else {
184 env->ReleaseStringUTFChars(path, pathStr);
185 }
186 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400187 } else {
Steve Block3762c312012-01-06 19:20:56 +0000188 ALOGE("server is null in add_storage");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400189 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400190}
191
192static void
193android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId)
194{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400195 Mutex::Autolock autoLock(sMutex);
196
197 MtpServer* server = getMtpServer(env, thiz);
198 if (server) {
199 MtpStorage* storage = server->getStorage(storageId);
200 if (storage) {
201 server->removeStorage(storage);
202 delete storage;
203 }
204 } else
Steve Block3762c312012-01-06 19:20:56 +0000205 ALOGE("server is null in remove_storage");
Mike Lockwood66e57f62011-02-18 13:24:01 -0500206}
207
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400208// ----------------------------------------------------------------------------
209
Daniel Micay76f6a862015-09-19 17:31:01 -0400210static const JNINativeMethod gMethods[] = {
Jerry Zhang6d319b82017-12-06 16:03:57 -0800211 {"native_setup", "(Landroid/mtp/MtpDatabase;Ljava/io/FileDescriptor;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
Mike Lockwood0cd01362010-12-30 11:54:33 -0500212 (void *)android_mtp_MtpServer_setup},
Mike Lockwooddcc31942011-07-11 15:04:38 -0400213 {"native_run", "()V", (void *)android_mtp_MtpServer_run},
214 {"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup},
Mike Lockwood0cd01362010-12-30 11:54:33 -0500215 {"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added},
216 {"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
Jamese4f680e2018-07-02 17:42:07 +0800217 {"native_send_object_info_changed", "(I)V", (void *)android_mtp_MtpServer_send_object_info_changed},
Mike Lockwood56c85242014-03-07 13:29:08 -0800218 {"native_send_device_property_changed", "(I)V",
219 (void *)android_mtp_MtpServer_send_device_property_changed},
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400220 {"native_add_storage", "(Landroid/mtp/MtpStorage;)V",
221 (void *)android_mtp_MtpServer_add_storage},
222 {"native_remove_storage", "(I)V", (void *)android_mtp_MtpServer_remove_storage},
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400223};
224
Mike Lockwood0cd01362010-12-30 11:54:33 -0500225int register_android_mtp_MtpServer(JNIEnv *env)
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400226{
227 jclass clazz;
228
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400229 clazz = env->FindClass("android/mtp/MtpStorage");
230 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000231 ALOGE("Can't find android/mtp/MtpStorage");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400232 return -1;
233 }
234 field_MtpStorage_storageId = env->GetFieldID(clazz, "mStorageId", "I");
235 if (field_MtpStorage_storageId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000236 ALOGE("Can't find MtpStorage.mStorageId");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400237 return -1;
238 }
239 field_MtpStorage_path = env->GetFieldID(clazz, "mPath", "Ljava/lang/String;");
240 if (field_MtpStorage_path == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000241 ALOGE("Can't find MtpStorage.mPath");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400242 return -1;
243 }
244 field_MtpStorage_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;");
245 if (field_MtpStorage_description == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000246 ALOGE("Can't find MtpStorage.mDescription");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400247 return -1;
248 }
Mike Lockwood51690542011-05-09 20:16:05 -0700249 field_MtpStorage_removable = env->GetFieldID(clazz, "mRemovable", "Z");
250 if (field_MtpStorage_removable == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000251 ALOGE("Can't find MtpStorage.mRemovable");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400252 return -1;
253 }
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400254 field_MtpStorage_maxFileSize = env->GetFieldID(clazz, "mMaxFileSize", "J");
255 if (field_MtpStorage_maxFileSize == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000256 ALOGE("Can't find MtpStorage.mMaxFileSize");
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400257 return -1;
258 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400259
Mike Lockwood0cd01362010-12-30 11:54:33 -0500260 clazz = env->FindClass("android/mtp/MtpServer");
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400261 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000262 ALOGE("Can't find android/mtp/MtpServer");
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400263 return -1;
264 }
Ashok Bhate2e59322013-12-17 19:04:19 +0000265 field_MtpServer_nativeContext = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400266 if (field_MtpServer_nativeContext == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000267 ALOGE("Can't find MtpServer.mNativeContext");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400268 return -1;
269 }
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400270
271 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -0500272 "android/mtp/MtpServer", gMethods, NELEM(gMethods));
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400273}