Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 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 | |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 21 | |
| 22 | import java.io.FileDescriptor; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 23 | import java.io.IOException; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 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 |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 34 | @UnsupportedAppUsage |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 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 | */ |
| 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 Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 63 | @UnsupportedAppUsage |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 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 | */ |
| 77 | public String getName() { |
| 78 | return mName; |
| 79 | } |
| 80 | |
| 81 | /** |
Glenn Kasten | cfde15d | 2012-11-08 09:04:43 -0800 | [diff] [blame] | 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. |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 85 | * |
| 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 Kasten | cfde15d | 2012-11-08 09:04:43 -0800 | [diff] [blame] | 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. |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 103 | * |
| 104 | * @param buffer to write |
| 105 | * @param length number of bytes to write |
| 106 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 107 | @UnsupportedAppUsage |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 7dbc4b4 | 2011-09-23 11:43:42 -0400 | [diff] [blame] | 118 | /** |
| 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 Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 125 | 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 Lockwood | 7dbc4b4 | 2011-09-23 11:43:42 -0400 | [diff] [blame] | 131 | private native void native_send_break(); |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 132 | } |