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 package android.hardware;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.os.ParcelFileDescriptor;
21 
22 import java.io.FileDescriptor;
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 
26 /**
27  * @hide
28  */
29 public class SerialPort {
30 
31     private static final String TAG = "SerialPort";
32 
33     // used by the JNI code
34     @UnsupportedAppUsage
35     private int mNativeContext;
36     private final String mName;
37     private ParcelFileDescriptor mFileDescriptor;
38 
39     /**
40      * SerialPort should only be instantiated by SerialManager
41      * @hide
42      */
SerialPort(String name)43     public SerialPort(String name) {
44         mName = name;
45     }
46 
47     /**
48      * SerialPort should only be instantiated by SerialManager
49      * Speed must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600,
50      * 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000,
51      * 1500000, 2000000, 2500000, 3000000, 3500000, 4000000
52      *
53      * @hide
54      */
open(ParcelFileDescriptor pfd, int speed)55     public void open(ParcelFileDescriptor pfd, int speed) throws IOException {
56         native_open(pfd.getFileDescriptor(), speed);
57         mFileDescriptor = pfd;
58     }
59 
60     /**
61      * Closes the serial port
62      */
63     @UnsupportedAppUsage
close()64     public void close() throws IOException {
65         if (mFileDescriptor != null) {
66             mFileDescriptor.close();
67             mFileDescriptor = null;
68         }
69         native_close();
70     }
71 
72     /**
73      * Returns the name of the serial port
74      *
75      * @return the serial port's name
76      */
getName()77     public String getName() {
78         return mName;
79     }
80 
81     /**
82      * Reads data into the provided buffer.
83      * Note that the value returned by {@link java.nio.Buffer#position()} on this buffer is
84      * unchanged after a call to this method.
85      *
86      * @param buffer to read into
87      * @return number of bytes read
88      */
read(ByteBuffer buffer)89     public int read(ByteBuffer buffer) throws IOException {
90         if (buffer.isDirect()) {
91             return native_read_direct(buffer, buffer.remaining());
92         } else if (buffer.hasArray()) {
93             return native_read_array(buffer.array(), buffer.remaining());
94         } else {
95             throw new IllegalArgumentException("buffer is not direct and has no array");
96         }
97     }
98 
99     /**
100      * Writes data from provided buffer.
101      * Note that the value returned by {@link java.nio.Buffer#position()} on this buffer is
102      * unchanged after a call to this method.
103      *
104      * @param buffer to write
105      * @param length number of bytes to write
106      */
107     @UnsupportedAppUsage
write(ByteBuffer buffer, int length)108     public void write(ByteBuffer buffer, int length) throws IOException {
109         if (buffer.isDirect()) {
110             native_write_direct(buffer, length);
111         } else if (buffer.hasArray()) {
112             native_write_array(buffer.array(), length);
113         } else {
114             throw new IllegalArgumentException("buffer is not direct and has no array");
115         }
116     }
117 
118     /**
119      * Sends a stream of zero valued bits for 0.25 to 0.5 seconds
120      */
sendBreak()121     public void sendBreak() {
122         native_send_break();
123     }
124 
native_open(FileDescriptor pfd, int speed)125     private native void native_open(FileDescriptor pfd, int speed) throws IOException;
native_close()126     private native void native_close();
native_read_array(byte[] buffer, int length)127     private native int native_read_array(byte[] buffer, int length) throws IOException;
native_read_direct(ByteBuffer buffer, int length)128     private native int native_read_direct(ByteBuffer buffer, int length) throws IOException;
native_write_array(byte[] buffer, int length)129     private native void native_write_array(byte[] buffer, int length) throws IOException;
native_write_direct(ByteBuffer buffer, int length)130     private native void native_write_direct(ByteBuffer buffer, int length) throws IOException;
native_send_break()131     private native void native_send_break();
132 }
133