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