Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
Orion Hodson | 2a822ea | 2021-02-22 11:57:16 +0000 | [diff] [blame] | 27 | #include "core_jni_helpers.h" |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 28 | #include "jni.h" |
Orion Hodson | 329c612 | 2020-06-02 13:22:06 +0100 | [diff] [blame] | 29 | #include <nativehelper/JNIPlatformHelp.h> |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 30 | #include "android_runtime/AndroidRuntime.h" |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 31 | #include "private/android_filesystem_config.h" |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 32 | |
| 33 | #include "MtpServer.h" |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 34 | #include "MtpStorage.h" |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 35 | |
Mike Lockwood | 81ea83d | 2010-06-30 17:49:41 -0400 | [diff] [blame] | 36 | using namespace android; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 37 | |
Orion Hodson | 2a822ea | 2021-02-22 11:57:16 +0000 | [diff] [blame] | 38 | static Mutex sMutex; |
| 39 | |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 40 | // MtpServer fields |
| 41 | static jfieldID field_MtpServer_nativeContext; |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 42 | |
| 43 | // MtpStorage fields |
| 44 | static jfieldID field_MtpStorage_storageId; |
| 45 | static jfieldID field_MtpStorage_path; |
| 46 | static jfieldID field_MtpStorage_description; |
Mike Lockwood | 5169054 | 2011-05-09 20:16:05 -0700 | [diff] [blame] | 47 | static jfieldID field_MtpStorage_removable; |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 48 | static jfieldID field_MtpStorage_maxFileSize; |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 49 | |
Orion Hodson | 2a822ea | 2021-02-22 11:57:16 +0000 | [diff] [blame] | 50 | // Initializer for the jfieldIDs above. This method must be invoked before accessing MtpServer and |
| 51 | // MtpStorage fields. |
| 52 | static void initializeJavaIDs(JNIEnv* env) { |
| 53 | static std::once_flag sJniInitialized; |
| 54 | |
| 55 | std::call_once(sJniInitialized, [](JNIEnv *env) { |
| 56 | const jclass storage_clazz = FindClassOrDie(env, "android/mtp/MtpStorage"); |
| 57 | field_MtpStorage_storageId = GetFieldIDOrDie(env, storage_clazz, "mStorageId", "I"); |
| 58 | field_MtpStorage_path = |
| 59 | GetFieldIDOrDie(env, storage_clazz, "mPath", "Ljava/lang/String;"); |
| 60 | field_MtpStorage_description = |
| 61 | GetFieldIDOrDie(env, storage_clazz, "mDescription", "Ljava/lang/String;"); |
| 62 | field_MtpStorage_removable = GetFieldIDOrDie(env, storage_clazz, "mRemovable", "Z"); |
| 63 | field_MtpStorage_maxFileSize = GetFieldIDOrDie(env, storage_clazz, "mMaxFileSize", "J"); |
| 64 | |
| 65 | const jclass server_clazz = FindClassOrDie(env, "android/mtp/MtpServer"); |
| 66 | field_MtpServer_nativeContext = GetFieldIDOrDie(env, server_clazz, "mNativeContext", "J"); |
| 67 | }, env); |
| 68 | } |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 69 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 70 | // ---------------------------------------------------------------------------- |
| 71 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 72 | // in android_mtp_MtpDatabase.cpp |
Jerry Zhang | f9c5c25 | 2017-08-16 18:07:51 -0700 | [diff] [blame] | 73 | extern IMtpDatabase* getMtpDatabase(JNIEnv *env, jobject database); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 74 | |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 75 | static inline MtpServer* getMtpServer(JNIEnv *env, jobject thiz) { |
Orion Hodson | 2a822ea | 2021-02-22 11:57:16 +0000 | [diff] [blame] | 76 | initializeJavaIDs(env); |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 77 | return (MtpServer*)env->GetLongField(thiz, field_MtpServer_nativeContext); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 80 | static void |
Jerry Zhang | 6d319b8 | 2017-12-06 16:03:57 -0800 | [diff] [blame] | 81 | android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jobject jControlFd, |
| 82 | jboolean usePtp, jstring deviceInfoManufacturer, jstring deviceInfoModel, |
| 83 | jstring deviceInfoDeviceVersion, jstring deviceInfoSerialNumber) |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 84 | { |
Orion Hodson | 2a822ea | 2021-02-22 11:57:16 +0000 | [diff] [blame] | 85 | initializeJavaIDs(env); |
| 86 | |
Alex Klyubin | abdc2b4 | 2016-12-21 11:19:52 -0800 | [diff] [blame] | 87 | const char *deviceInfoManufacturerStr = env->GetStringUTFChars(deviceInfoManufacturer, NULL); |
| 88 | const char *deviceInfoModelStr = env->GetStringUTFChars(deviceInfoModel, NULL); |
| 89 | const char *deviceInfoDeviceVersionStr = env->GetStringUTFChars(deviceInfoDeviceVersion, NULL); |
| 90 | const char *deviceInfoSerialNumberStr = env->GetStringUTFChars(deviceInfoSerialNumber, NULL); |
Jerry Zhang | 6d319b8 | 2017-12-06 16:03:57 -0800 | [diff] [blame] | 91 | int controlFd = dup(jniGetFDFromFileDescriptor(env, jControlFd)); |
| 92 | MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase), controlFd, |
Jerry Zhang | def7b193 | 2017-10-17 13:47:51 -0700 | [diff] [blame] | 93 | usePtp, |
Jerry Zhang | d9f3005 | 2018-03-27 15:29:09 -0700 | [diff] [blame] | 94 | (deviceInfoManufacturerStr != NULL) ? deviceInfoManufacturerStr : "", |
| 95 | (deviceInfoModelStr != NULL) ? deviceInfoModelStr : "", |
| 96 | (deviceInfoDeviceVersionStr != NULL) ? deviceInfoDeviceVersionStr : "", |
| 97 | (deviceInfoSerialNumberStr != NULL) ? deviceInfoSerialNumberStr : ""); |
Alex Klyubin | abdc2b4 | 2016-12-21 11:19:52 -0800 | [diff] [blame] | 98 | if (deviceInfoManufacturerStr != NULL) { |
| 99 | env->ReleaseStringUTFChars(deviceInfoManufacturer, deviceInfoManufacturerStr); |
| 100 | } |
| 101 | if (deviceInfoModelStr != NULL) { |
| 102 | env->ReleaseStringUTFChars(deviceInfoModel, deviceInfoModelStr); |
| 103 | } |
| 104 | if (deviceInfoDeviceVersionStr != NULL) { |
| 105 | env->ReleaseStringUTFChars(deviceInfoDeviceVersion, deviceInfoDeviceVersionStr); |
| 106 | } |
| 107 | if (deviceInfoSerialNumberStr != NULL) { |
| 108 | env->ReleaseStringUTFChars(deviceInfoSerialNumber, deviceInfoSerialNumberStr); |
| 109 | } |
Jerry Zhang | bb598ee | 2016-10-24 14:35:08 -0700 | [diff] [blame] | 110 | env->SetLongField(thiz, field_MtpServer_nativeContext, (jlong)server); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void |
| 114 | android_mtp_MtpServer_run(JNIEnv *env, jobject thiz) |
| 115 | { |
| 116 | MtpServer* server = getMtpServer(env, thiz); |
| 117 | if (server) |
| 118 | server->run(); |
| 119 | else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 120 | ALOGE("server is null in run"); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void |
| 124 | android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz) |
| 125 | { |
| 126 | Mutex::Autolock autoLock(sMutex); |
| 127 | |
| 128 | MtpServer* server = getMtpServer(env, thiz); |
| 129 | if (server) { |
| 130 | delete server; |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 131 | env->SetLongField(thiz, field_MtpServer_nativeContext, 0); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 132 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 133 | ALOGE("server is null in cleanup"); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 134 | } |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 137 | static void |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 138 | android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle) |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 139 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 140 | Mutex::Autolock autoLock(sMutex); |
| 141 | |
| 142 | MtpServer* server = getMtpServer(env, thiz); |
| 143 | if (server) |
| 144 | server->sendObjectAdded(handle); |
| 145 | else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 146 | ALOGE("server is null in send_object_added"); |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 150 | android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle) |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 151 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 152 | Mutex::Autolock autoLock(sMutex); |
| 153 | |
| 154 | MtpServer* server = getMtpServer(env, thiz); |
| 155 | if (server) |
| 156 | server->sendObjectRemoved(handle); |
| 157 | else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 158 | ALOGE("server is null in send_object_removed"); |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Mike Lockwood | eabe8bf | 2010-08-31 14:35:23 -0400 | [diff] [blame] | 161 | static void |
James | e4f680e | 2018-07-02 17:42:07 +0800 | [diff] [blame] | 162 | android_mtp_MtpServer_send_object_info_changed(JNIEnv *env, jobject thiz, jint handle) |
| 163 | { |
| 164 | Mutex::Autolock autoLock(sMutex); |
| 165 | |
| 166 | MtpServer* server = getMtpServer(env, thiz); |
| 167 | if (server) |
| 168 | server->sendObjectInfoChanged(handle); |
| 169 | else |
| 170 | ALOGE("server is null in send_object_info_changed"); |
| 171 | } |
| 172 | |
| 173 | static void |
Mike Lockwood | 56c8524 | 2014-03-07 13:29:08 -0800 | [diff] [blame] | 174 | android_mtp_MtpServer_send_device_property_changed(JNIEnv *env, jobject thiz, jint property) |
| 175 | { |
| 176 | Mutex::Autolock autoLock(sMutex); |
| 177 | |
| 178 | MtpServer* server = getMtpServer(env, thiz); |
| 179 | if (server) |
| 180 | server->sendDevicePropertyChanged(property); |
| 181 | else |
| 182 | ALOGE("server is null in send_object_removed"); |
| 183 | } |
| 184 | |
| 185 | static void |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 186 | android_mtp_MtpServer_add_storage(JNIEnv *env, jobject thiz, jobject jstorage) |
Mike Lockwood | 66e57f6 | 2011-02-18 13:24:01 -0500 | [diff] [blame] | 187 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 188 | Mutex::Autolock autoLock(sMutex); |
| 189 | |
| 190 | MtpServer* server = getMtpServer(env, thiz); |
| 191 | if (server) { |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 192 | jint storageID = env->GetIntField(jstorage, field_MtpStorage_storageId); |
| 193 | jstring path = (jstring)env->GetObjectField(jstorage, field_MtpStorage_path); |
| 194 | jstring description = (jstring)env->GetObjectField(jstorage, field_MtpStorage_description); |
Mike Lockwood | 5169054 | 2011-05-09 20:16:05 -0700 | [diff] [blame] | 195 | jboolean removable = env->GetBooleanField(jstorage, field_MtpStorage_removable); |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 196 | jlong maxFileSize = env->GetLongField(jstorage, field_MtpStorage_maxFileSize); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 197 | |
| 198 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 199 | if (pathStr != NULL) { |
| 200 | const char *descriptionStr = env->GetStringUTFChars(description, NULL); |
| 201 | if (descriptionStr != NULL) { |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 202 | MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr, |
Jerry Zhang | f9c5c25 | 2017-08-16 18:07:51 -0700 | [diff] [blame] | 203 | removable, maxFileSize); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 204 | server->addStorage(storage); |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 205 | env->ReleaseStringUTFChars(path, pathStr); |
| 206 | env->ReleaseStringUTFChars(description, descriptionStr); |
| 207 | } else { |
| 208 | env->ReleaseStringUTFChars(path, pathStr); |
| 209 | } |
| 210 | } |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 211 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 212 | ALOGE("server is null in add_storage"); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 213 | } |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void |
| 217 | android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId) |
| 218 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 219 | Mutex::Autolock autoLock(sMutex); |
| 220 | |
| 221 | MtpServer* server = getMtpServer(env, thiz); |
| 222 | if (server) { |
| 223 | MtpStorage* storage = server->getStorage(storageId); |
| 224 | if (storage) { |
| 225 | server->removeStorage(storage); |
| 226 | delete storage; |
| 227 | } |
| 228 | } else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 229 | ALOGE("server is null in remove_storage"); |
Mike Lockwood | 66e57f6 | 2011-02-18 13:24:01 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 232 | // ---------------------------------------------------------------------------- |
| 233 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 234 | static const JNINativeMethod gMethods[] = { |
Jerry Zhang | 6d319b8 | 2017-12-06 16:03:57 -0800 | [diff] [blame] | 235 | {"native_setup", "(Landroid/mtp/MtpDatabase;Ljava/io/FileDescriptor;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 236 | (void *)android_mtp_MtpServer_setup}, |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 237 | {"native_run", "()V", (void *)android_mtp_MtpServer_run}, |
| 238 | {"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup}, |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 239 | {"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added}, |
| 240 | {"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed}, |
James | e4f680e | 2018-07-02 17:42:07 +0800 | [diff] [blame] | 241 | {"native_send_object_info_changed", "(I)V", (void *)android_mtp_MtpServer_send_object_info_changed}, |
Mike Lockwood | 56c8524 | 2014-03-07 13:29:08 -0800 | [diff] [blame] | 242 | {"native_send_device_property_changed", "(I)V", |
| 243 | (void *)android_mtp_MtpServer_send_device_property_changed}, |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 244 | {"native_add_storage", "(Landroid/mtp/MtpStorage;)V", |
| 245 | (void *)android_mtp_MtpServer_add_storage}, |
| 246 | {"native_remove_storage", "(I)V", (void *)android_mtp_MtpServer_remove_storage}, |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 247 | }; |
| 248 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 249 | int register_android_mtp_MtpServer(JNIEnv *env) |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 250 | { |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 251 | return AndroidRuntime::registerNativeMethods(env, |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 252 | "android/mtp/MtpServer", gMethods, NELEM(gMethods)); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 253 | } |