blob: c76cebebc730523707376482c3e73422b513f49a [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
Jerry Zhangbb598ee2016-10-24 14:35:08 -070058static void android_mtp_configure(JNIEnv *, jobject, jboolean usePtp) {
59 MtpServer::configure(usePtp);
60}
61
Mike Lockwood98ef64e2010-06-29 16:42:13 -040062static void
Alex Klyubinabdc2b42016-12-21 11:19:52 -080063android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jboolean usePtp,
64 jstring deviceInfoManufacturer,
65 jstring deviceInfoModel,
66 jstring deviceInfoDeviceVersion,
67 jstring deviceInfoSerialNumber)
Mike Lockwood98ef64e2010-06-29 16:42:13 -040068{
Alex Klyubinabdc2b42016-12-21 11:19:52 -080069 const char *deviceInfoManufacturerStr = env->GetStringUTFChars(deviceInfoManufacturer, NULL);
70 const char *deviceInfoModelStr = env->GetStringUTFChars(deviceInfoModel, NULL);
71 const char *deviceInfoDeviceVersionStr = env->GetStringUTFChars(deviceInfoDeviceVersion, NULL);
72 const char *deviceInfoSerialNumberStr = env->GetStringUTFChars(deviceInfoSerialNumber, NULL);
Jerry Zhangbb598ee2016-10-24 14:35:08 -070073 MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase),
Jerry Zhangdef7b1932017-10-17 13:47:51 -070074 usePtp,
Alex Klyubinabdc2b42016-12-21 11:19:52 -080075 MtpString((deviceInfoManufacturerStr != NULL) ? deviceInfoManufacturerStr : ""),
76 MtpString((deviceInfoModelStr != NULL) ? deviceInfoModelStr : ""),
77 MtpString((deviceInfoDeviceVersionStr != NULL) ? deviceInfoDeviceVersionStr : ""),
78 MtpString((deviceInfoSerialNumberStr != NULL) ? deviceInfoSerialNumberStr : ""));
79 if (deviceInfoManufacturerStr != NULL) {
80 env->ReleaseStringUTFChars(deviceInfoManufacturer, deviceInfoManufacturerStr);
81 }
82 if (deviceInfoModelStr != NULL) {
83 env->ReleaseStringUTFChars(deviceInfoModel, deviceInfoModelStr);
84 }
85 if (deviceInfoDeviceVersionStr != NULL) {
86 env->ReleaseStringUTFChars(deviceInfoDeviceVersion, deviceInfoDeviceVersionStr);
87 }
88 if (deviceInfoSerialNumberStr != NULL) {
89 env->ReleaseStringUTFChars(deviceInfoSerialNumber, deviceInfoSerialNumberStr);
90 }
Jerry Zhangbb598ee2016-10-24 14:35:08 -070091 env->SetLongField(thiz, field_MtpServer_nativeContext, (jlong)server);
Mike Lockwooddcc31942011-07-11 15:04:38 -040092}
93
94static void
95android_mtp_MtpServer_run(JNIEnv *env, jobject thiz)
96{
97 MtpServer* server = getMtpServer(env, thiz);
98 if (server)
99 server->run();
100 else
Steve Block3762c312012-01-06 19:20:56 +0000101 ALOGE("server is null in run");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400102}
103
104static void
105android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz)
106{
107 Mutex::Autolock autoLock(sMutex);
108
109 MtpServer* server = getMtpServer(env, thiz);
110 if (server) {
111 delete server;
Ashok Bhate2e59322013-12-17 19:04:19 +0000112 env->SetLongField(thiz, field_MtpServer_nativeContext, 0);
Mike Lockwooddcc31942011-07-11 15:04:38 -0400113 } else {
Steve Block3762c312012-01-06 19:20:56 +0000114 ALOGE("server is null in cleanup");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400115 }
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400116}
117
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400118static void
Mike Lockwood0cd01362010-12-30 11:54:33 -0500119android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400120{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400121 Mutex::Autolock autoLock(sMutex);
122
123 MtpServer* server = getMtpServer(env, thiz);
124 if (server)
125 server->sendObjectAdded(handle);
126 else
Steve Block3762c312012-01-06 19:20:56 +0000127 ALOGE("server is null in send_object_added");
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400128}
129
130static void
Mike Lockwood0cd01362010-12-30 11:54:33 -0500131android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle)
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400132{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400133 Mutex::Autolock autoLock(sMutex);
134
135 MtpServer* server = getMtpServer(env, thiz);
136 if (server)
137 server->sendObjectRemoved(handle);
138 else
Steve Block3762c312012-01-06 19:20:56 +0000139 ALOGE("server is null in send_object_removed");
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400140}
141
Mike Lockwoodeabe8bf2010-08-31 14:35:23 -0400142static void
Mike Lockwood56c85242014-03-07 13:29:08 -0800143android_mtp_MtpServer_send_device_property_changed(JNIEnv *env, jobject thiz, jint property)
144{
145 Mutex::Autolock autoLock(sMutex);
146
147 MtpServer* server = getMtpServer(env, thiz);
148 if (server)
149 server->sendDevicePropertyChanged(property);
150 else
151 ALOGE("server is null in send_object_removed");
152}
153
154static void
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400155android_mtp_MtpServer_add_storage(JNIEnv *env, jobject thiz, jobject jstorage)
Mike Lockwood66e57f62011-02-18 13:24:01 -0500156{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400157 Mutex::Autolock autoLock(sMutex);
158
159 MtpServer* server = getMtpServer(env, thiz);
160 if (server) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400161 jint storageID = env->GetIntField(jstorage, field_MtpStorage_storageId);
162 jstring path = (jstring)env->GetObjectField(jstorage, field_MtpStorage_path);
163 jstring description = (jstring)env->GetObjectField(jstorage, field_MtpStorage_description);
Mike Lockwood51690542011-05-09 20:16:05 -0700164 jboolean removable = env->GetBooleanField(jstorage, field_MtpStorage_removable);
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400165 jlong maxFileSize = env->GetLongField(jstorage, field_MtpStorage_maxFileSize);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400166
167 const char *pathStr = env->GetStringUTFChars(path, NULL);
James Dong39774722011-04-06 11:57:48 -0700168 if (pathStr != NULL) {
169 const char *descriptionStr = env->GetStringUTFChars(description, NULL);
170 if (descriptionStr != NULL) {
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400171 MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr,
Jerry Zhangf9c5c252017-08-16 18:07:51 -0700172 removable, maxFileSize);
Mike Lockwooddcc31942011-07-11 15:04:38 -0400173 server->addStorage(storage);
James Dong39774722011-04-06 11:57:48 -0700174 env->ReleaseStringUTFChars(path, pathStr);
175 env->ReleaseStringUTFChars(description, descriptionStr);
176 } else {
177 env->ReleaseStringUTFChars(path, pathStr);
178 }
179 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400180 } else {
Steve Block3762c312012-01-06 19:20:56 +0000181 ALOGE("server is null in add_storage");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400182 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400183}
184
185static void
186android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId)
187{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400188 Mutex::Autolock autoLock(sMutex);
189
190 MtpServer* server = getMtpServer(env, thiz);
191 if (server) {
192 MtpStorage* storage = server->getStorage(storageId);
193 if (storage) {
194 server->removeStorage(storage);
195 delete storage;
196 }
197 } else
Steve Block3762c312012-01-06 19:20:56 +0000198 ALOGE("server is null in remove_storage");
Mike Lockwood66e57f62011-02-18 13:24:01 -0500199}
200
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400201// ----------------------------------------------------------------------------
202
Daniel Micay76f6a862015-09-19 17:31:01 -0400203static const JNINativeMethod gMethods[] = {
Jerry Zhangbb598ee2016-10-24 14:35:08 -0700204 {"native_configure", "(Z)V", (void *)android_mtp_configure},
Alex Klyubinabdc2b42016-12-21 11:19:52 -0800205 {"native_setup", "(Landroid/mtp/MtpDatabase;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
Mike Lockwood0cd01362010-12-30 11:54:33 -0500206 (void *)android_mtp_MtpServer_setup},
Mike Lockwooddcc31942011-07-11 15:04:38 -0400207 {"native_run", "()V", (void *)android_mtp_MtpServer_run},
208 {"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup},
Mike Lockwood0cd01362010-12-30 11:54:33 -0500209 {"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added},
210 {"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
Mike Lockwood56c85242014-03-07 13:29:08 -0800211 {"native_send_device_property_changed", "(I)V",
212 (void *)android_mtp_MtpServer_send_device_property_changed},
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400213 {"native_add_storage", "(Landroid/mtp/MtpStorage;)V",
214 (void *)android_mtp_MtpServer_add_storage},
215 {"native_remove_storage", "(I)V", (void *)android_mtp_MtpServer_remove_storage},
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400216};
217
Mike Lockwood0cd01362010-12-30 11:54:33 -0500218int register_android_mtp_MtpServer(JNIEnv *env)
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400219{
220 jclass clazz;
221
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400222 clazz = env->FindClass("android/mtp/MtpStorage");
223 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000224 ALOGE("Can't find android/mtp/MtpStorage");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400225 return -1;
226 }
227 field_MtpStorage_storageId = env->GetFieldID(clazz, "mStorageId", "I");
228 if (field_MtpStorage_storageId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000229 ALOGE("Can't find MtpStorage.mStorageId");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400230 return -1;
231 }
232 field_MtpStorage_path = env->GetFieldID(clazz, "mPath", "Ljava/lang/String;");
233 if (field_MtpStorage_path == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000234 ALOGE("Can't find MtpStorage.mPath");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400235 return -1;
236 }
237 field_MtpStorage_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;");
238 if (field_MtpStorage_description == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000239 ALOGE("Can't find MtpStorage.mDescription");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400240 return -1;
241 }
Mike Lockwood51690542011-05-09 20:16:05 -0700242 field_MtpStorage_removable = env->GetFieldID(clazz, "mRemovable", "Z");
243 if (field_MtpStorage_removable == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000244 ALOGE("Can't find MtpStorage.mRemovable");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400245 return -1;
246 }
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400247 field_MtpStorage_maxFileSize = env->GetFieldID(clazz, "mMaxFileSize", "J");
248 if (field_MtpStorage_maxFileSize == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000249 ALOGE("Can't find MtpStorage.mMaxFileSize");
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400250 return -1;
251 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400252
Mike Lockwood0cd01362010-12-30 11:54:33 -0500253 clazz = env->FindClass("android/mtp/MtpServer");
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400254 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000255 ALOGE("Can't find android/mtp/MtpServer");
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400256 return -1;
257 }
Ashok Bhate2e59322013-12-17 19:04:19 +0000258 field_MtpServer_nativeContext = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400259 if (field_MtpServer_nativeContext == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000260 ALOGE("Can't find MtpServer.mNativeContext");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400261 return -1;
262 }
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400263
264 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -0500265 "android/mtp/MtpServer", gMethods, NELEM(gMethods));
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400266}