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 */ 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 */ 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 */ 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 */ 80 public String getName() { 81 return mName; 82 } 83 84 /** 85 * Reads data into the provided buffer. 86 * Note that the value returned by {@link java.nio.Buffer#position()} on this buffer is 87 * unchanged after a call to this method. 88 * 89 * @param buffer to read into 90 * @return number of bytes read 91 */ 92 public int read(ByteBuffer buffer) throws IOException { 93 if (buffer.isDirect()) { 94 return native_read_direct(buffer, buffer.remaining()); 95 } else if (buffer.hasArray()) { 96 return native_read_array(buffer.array(), buffer.remaining()); 97 } else { 98 throw new IllegalArgumentException("buffer is not direct and has no array"); 99 } 100 } 101 102 /** 103 * Writes data from provided buffer. 104 * Note that the value returned by {@link java.nio.Buffer#position()} on this buffer is 105 * unchanged after a call to this method. 106 * 107 * @param buffer to write 108 * @param length number of bytes to write 109 */ 110 public void write(ByteBuffer buffer, int length) throws IOException { 111 if (buffer.isDirect()) { 112 native_write_direct(buffer, length); 113 } else if (buffer.hasArray()) { 114 native_write_array(buffer.array(), length); 115 } else { 116 throw new IllegalArgumentException("buffer is not direct and has no array"); 117 } 118 } 119 120 /** 121 * Sends a stream of zero valued bits for 0.25 to 0.5 seconds 122 */ 123 public void sendBreak() { 124 native_send_break(); 125 } 126 127 private native void native_open(FileDescriptor pfd, int speed) throws IOException; 128 private native void native_close(); 129 private native int native_read_array(byte[] buffer, int length) throws IOException; 130 private native int native_read_direct(ByteBuffer buffer, int length) throws IOException; 131 private native void native_write_array(byte[] buffer, int length) throws IOException; 132 private native void native_write_direct(ByteBuffer buffer, int length) throws IOException; 133 private native void native_send_break(); 134 } 135