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