blob: 5d83d9c832c3c8a00fcc24d7a8182931159d126c [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
19import android.os.ParcelFileDescriptor;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040020
21import java.io.FileDescriptor;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040022import java.io.IOException;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040023
24import java.nio.ByteBuffer;
25
26/**
27 * @hide
28 */
29public 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 Kastencfde15d2012-11-08 09:04:43 -080080 * 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 Lockwoodb01e8bf2011-08-29 20:11:07 -040083 *
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 Kastencfde15d2012-11-08 09:04:43 -080098 * 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 Lockwoodb01e8bf2011-08-29 20:11:07 -0400101 *
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 Lockwood7dbc4b42011-09-23 11:43:42 -0400115 /**
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 Lockwoodb01e8bf2011-08-29 20:11:07 -0400122 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 Lockwood7dbc4b42011-09-23 11:43:42 -0400128 private native void native_send_break();
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400129}