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