• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/JNIHelp.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         return NULL;
52     }
53     return env->NewObject(gParcelFileDescriptorOffsets.mClass,
54         gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
55 }
56 
57 
58 static const JNINativeMethod method_table[] = {
59     { "native_open",                "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
60                                     (void*)android_server_SerialService_open },
61 };
62 
register_android_server_SerialService(JNIEnv * env)63 int register_android_server_SerialService(JNIEnv *env)
64 {
65     jclass clazz = env->FindClass("com/android/server/SerialService");
66     if (clazz == NULL) {
67         ALOGE("Can't find com/android/server/SerialService");
68         return -1;
69     }
70 
71     clazz = env->FindClass("android/os/ParcelFileDescriptor");
72     LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
73     gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
74     gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
75     LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
76                  "Unable to find constructor for android.os.ParcelFileDescriptor");
77 
78     return jniRegisterNativeMethods(env, "com/android/server/SerialService",
79             method_table, NELEM(method_table));
80 }
81 
82 };
83