blob: 0fcaa4989210a71e423cae312035d0e61bca5632 [file] [log] [blame]
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001/*
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
17package android.hardware;
18
Artur Satayev26958002019-12-10 17:47:52 +000019import android.compat.annotation.UnsupportedAppUsage;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040020import android.os.ParcelFileDescriptor;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040021
22import java.io.FileDescriptor;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040023import java.io.IOException;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040024import java.nio.ByteBuffer;
25
26/**
27 * @hide
28 */
29public class SerialPort {
30
31 private static final String TAG = "SerialPort";
32
33 // used by the JNI code
Mathew Inwoodbcbe4402018-08-08 15:42:59 +010034 @UnsupportedAppUsage
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040035 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 */
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 */
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 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +010063 @UnsupportedAppUsage
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040064 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 */
77 public String getName() {
78 return mName;
79 }
80
81 /**
Glenn Kastencfde15d2012-11-08 09:04:43 -080082 * 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.
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040085 *
86 * @param buffer to read into
87 * @return number of bytes read
88 */
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 /**
Glenn Kastencfde15d2012-11-08 09:04:43 -0800100 * 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.
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400103 *
104 * @param buffer to write
105 * @param length number of bytes to write
106 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100107 @UnsupportedAppUsage
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400108 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
Mike Lockwood7dbc4b42011-09-23 11:43:42 -0400118 /**
119 * Sends a stream of zero valued bits for 0.25 to 0.5 seconds
120 */
121 public void sendBreak() {
122 native_send_break();
123 }
124
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400125 private native void native_open(FileDescriptor pfd, int speed) throws IOException;
126 private native void native_close();
127 private native int native_read_array(byte[] buffer, int length) throws IOException;
128 private native int native_read_direct(ByteBuffer buffer, int length) throws IOException;
129 private native void native_write_array(byte[] buffer, int length) throws IOException;
130 private native void native_write_direct(ByteBuffer buffer, int length) throws IOException;
Mike Lockwood7dbc4b42011-09-23 11:43:42 -0400131 private native void native_send_break();
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400132}