1 /*
2  * Copyright (C) 2011 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 "SerialServiceJNI"
18 #include "utils/Log.h"
19 
20 #include "jni.h"
21 #include <nativehelper/JNIPlatformHelp.h>
22 #include "android_runtime/AndroidRuntime.h"
23 
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 
28 namespace android
29 {
30 
31 static struct parcel_file_descriptor_offsets_t
32 {
33     jclass mClass;
34     jmethodID mConstructor;
35 } gParcelFileDescriptorOffsets;
36 
android_server_SerialService_open(JNIEnv * env,jobject,jstring path)37 static jobject android_server_SerialService_open(JNIEnv *env, jobject /* thiz */, jstring path)
38 {
39     const char *pathStr = env->GetStringUTFChars(path, NULL);
40 
41     int fd = open(pathStr, O_RDWR | O_NOCTTY);
42     if (fd < 0) {
43         ALOGE("could not open %s", pathStr);
44         env->ReleaseStringUTFChars(path, pathStr);
45         return NULL;
46     }
47     env->ReleaseStringUTFChars(path, pathStr);
48 
49     jobject fileDescriptor = jniCreateFileDescriptor(env, fd);
50     if (fileDescriptor == NULL) {
51         close(fd);
52         return NULL;
53     }
54     return env->NewObject(gParcelFileDescriptorOffsets.mClass,
55         gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
56 }
57 
58 
59 static const JNINativeMethod method_table[] = {
60     { "native_open",                "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
61                                     (void*)android_server_SerialService_open },
62 };
63 
register_android_server_SerialService(JNIEnv * env)64 int register_android_server_SerialService(JNIEnv *env)
65 {
66     jclass clazz = env->FindClass("com/android/server/SerialService");
67     if (clazz == NULL) {
68         ALOGE("Can't find com/android/server/SerialService");
69         return -1;
70     }
71 
72     clazz = env->FindClass("android/os/ParcelFileDescriptor");
73     LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
74     gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
75     gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
76     LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
77                  "Unable to find constructor for android.os.ParcelFileDescriptor");
78 
79     return jniRegisterNativeMethods(env, "com/android/server/SerialService",
80             method_table, NELEM(method_table));
81 }
82 
83 };
84