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
27 #include "core_jni_helpers.h"
28 #include "jni.h"
29 #include <nativehelper/JNIPlatformHelp.h>
30 #include "android_runtime/AndroidRuntime.h"
31 #include "private/android_filesystem_config.h"
32
33 #include "MtpServer.h"
34 #include "MtpStorage.h"
35
36 using namespace android;
37
38 static Mutex sMutex;
39
40 // MtpServer fields
41 static jfieldID field_MtpServer_nativeContext;
42
43 // MtpStorage fields
44 static jfieldID field_MtpStorage_storageId;
45 static jfieldID field_MtpStorage_path;
46 static jfieldID field_MtpStorage_description;
47 static jfieldID field_MtpStorage_removable;
48 static jfieldID field_MtpStorage_maxFileSize;
49
50 // Initializer for the jfieldIDs above. This method must be invoked before accessing MtpServer and
51 // MtpStorage fields.
initializeJavaIDs(JNIEnv * env)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 }
69
70 // ----------------------------------------------------------------------------
71
72 // in android_mtp_MtpDatabase.cpp
73 extern IMtpDatabase* getMtpDatabase(JNIEnv *env, jobject database);
74
getMtpServer(JNIEnv * env,jobject thiz)75 static inline MtpServer* getMtpServer(JNIEnv *env, jobject thiz) {
76 initializeJavaIDs(env);
77 return (MtpServer*)env->GetLongField(thiz, field_MtpServer_nativeContext);
78 }
79
80 static void
android_mtp_MtpServer_setup(JNIEnv * env,jobject thiz,jobject javaDatabase,jobject jControlFd,jboolean usePtp,jstring deviceInfoManufacturer,jstring deviceInfoModel,jstring deviceInfoDeviceVersion,jstring deviceInfoSerialNumber)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)
84 {
85 initializeJavaIDs(env);
86
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);
91 int controlFd = dup(jniGetFDFromFileDescriptor(env, jControlFd));
92 MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase), controlFd,
93 usePtp,
94 (deviceInfoManufacturerStr != NULL) ? deviceInfoManufacturerStr : "",
95 (deviceInfoModelStr != NULL) ? deviceInfoModelStr : "",
96 (deviceInfoDeviceVersionStr != NULL) ? deviceInfoDeviceVersionStr : "",
97 (deviceInfoSerialNumberStr != NULL) ? deviceInfoSerialNumberStr : "");
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 }
110 env->SetLongField(thiz, field_MtpServer_nativeContext, (jlong)server);
111 }
112
113 static void
android_mtp_MtpServer_run(JNIEnv * env,jobject thiz)114 android_mtp_MtpServer_run(JNIEnv *env, jobject thiz)
115 {
116 MtpServer* server = getMtpServer(env, thiz);
117 if (server)
118 server->run();
119 else
120 ALOGE("server is null in run");
121 }
122
123 static void
android_mtp_MtpServer_cleanup(JNIEnv * env,jobject thiz)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;
131 env->SetLongField(thiz, field_MtpServer_nativeContext, 0);
132 } else {
133 ALOGE("server is null in cleanup");
134 }
135 }
136
137 static void
android_mtp_MtpServer_send_object_added(JNIEnv * env,jobject thiz,jint handle)138 android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
139 {
140 Mutex::Autolock autoLock(sMutex);
141
142 MtpServer* server = getMtpServer(env, thiz);
143 if (server)
144 server->sendObjectAdded(handle);
145 else
146 ALOGE("server is null in send_object_added");
147 }
148
149 static void
android_mtp_MtpServer_send_object_removed(JNIEnv * env,jobject thiz,jint handle)150 android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle)
151 {
152 Mutex::Autolock autoLock(sMutex);
153
154 MtpServer* server = getMtpServer(env, thiz);
155 if (server)
156 server->sendObjectRemoved(handle);
157 else
158 ALOGE("server is null in send_object_removed");
159 }
160
161 static void
android_mtp_MtpServer_send_object_info_changed(JNIEnv * env,jobject thiz,jint handle)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
android_mtp_MtpServer_send_device_property_changed(JNIEnv * env,jobject thiz,jint property)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
android_mtp_MtpServer_add_storage(JNIEnv * env,jobject thiz,jobject jstorage)186 android_mtp_MtpServer_add_storage(JNIEnv *env, jobject thiz, jobject jstorage)
187 {
188 Mutex::Autolock autoLock(sMutex);
189
190 MtpServer* server = getMtpServer(env, thiz);
191 if (server) {
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);
195 jboolean removable = env->GetBooleanField(jstorage, field_MtpStorage_removable);
196 jlong maxFileSize = env->GetLongField(jstorage, field_MtpStorage_maxFileSize);
197
198 const char *pathStr = env->GetStringUTFChars(path, NULL);
199 if (pathStr != NULL) {
200 const char *descriptionStr = env->GetStringUTFChars(description, NULL);
201 if (descriptionStr != NULL) {
202 MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr,
203 removable, maxFileSize);
204 server->addStorage(storage);
205 env->ReleaseStringUTFChars(path, pathStr);
206 env->ReleaseStringUTFChars(description, descriptionStr);
207 } else {
208 env->ReleaseStringUTFChars(path, pathStr);
209 }
210 }
211 } else {
212 ALOGE("server is null in add_storage");
213 }
214 }
215
216 static void
android_mtp_MtpServer_remove_storage(JNIEnv * env,jobject thiz,jint storageId)217 android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId)
218 {
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
229 ALOGE("server is null in remove_storage");
230 }
231
232 // ----------------------------------------------------------------------------
233
234 static const JNINativeMethod gMethods[] = {
235 {"native_setup", "(Landroid/mtp/MtpDatabase;Ljava/io/FileDescriptor;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
236 (void *)android_mtp_MtpServer_setup},
237 {"native_run", "()V", (void *)android_mtp_MtpServer_run},
238 {"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup},
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},
241 {"native_send_object_info_changed", "(I)V", (void *)android_mtp_MtpServer_send_object_info_changed},
242 {"native_send_device_property_changed", "(I)V",
243 (void *)android_mtp_MtpServer_send_device_property_changed},
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},
247 };
248
register_android_mtp_MtpServer(JNIEnv * env)249 int register_android_mtp_MtpServer(JNIEnv *env)
250 {
251 return AndroidRuntime::registerNativeMethods(env,
252 "android/mtp/MtpServer", gMethods, NELEM(gMethods));
253 }
254