diff options
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/app/ContextImpl.java | 8 | ||||
| -rw-r--r-- | core/java/android/content/Context.java | 11 | ||||
| -rw-r--r-- | core/java/android/hardware/ISerialManager.aidl | 29 | ||||
| -rw-r--r-- | core/java/android/hardware/SerialManager.java | 88 | ||||
| -rw-r--r-- | core/java/android/hardware/SerialPort.java | 122 |
5 files changed, 258 insertions, 0 deletions
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index db5113e..37900b6 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -42,7 +42,9 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; +import android.hardware.ISerialManager; import android.hardware.SensorManager; +import android.hardware.SerialManager; import android.hardware.usb.IUsbManager; import android.hardware.usb.UsbManager; import android.location.CountryDetector; @@ -428,6 +430,12 @@ class ContextImpl extends Context { return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); }}); + registerService(SERIAL_SERVICE, new ServiceFetcher() { + public Object createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(SERIAL_SERVICE); + return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); + }}); + registerService(VIBRATOR_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { return new Vibrator(); diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index bfbd0ac..3d4e354 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -1789,6 +1789,17 @@ public abstract class Context { public static final String USB_SERVICE = "usb"; /** + * Use with {@link #getSystemService} to retrieve a {@link + * android.hardware.SerialManager} for access to serial ports. + * + * @see #getSystemService + * @see android.harware.SerialManager + * + * @hide + */ + public static final String SERIAL_SERVICE = "serial"; + + /** * Determine whether the given permission is allowed for a particular * process and user ID running in the system. * diff --git a/core/java/android/hardware/ISerialManager.aidl b/core/java/android/hardware/ISerialManager.aidl new file mode 100644 index 0000000..74d30f7 --- /dev/null +++ b/core/java/android/hardware/ISerialManager.aidl @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware; + +import android.os.ParcelFileDescriptor; + +/** @hide */ +interface ISerialManager +{ + /* Returns a list of all available serial ports */ + String[] getSerialPorts(); + + /* Returns a file descriptor for the serial port. */ + ParcelFileDescriptor openSerialPort(String name); +} diff --git a/core/java/android/hardware/SerialManager.java b/core/java/android/hardware/SerialManager.java new file mode 100644 index 0000000..c5e1c2b --- /dev/null +++ b/core/java/android/hardware/SerialManager.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package android.hardware; + +import android.app.PendingIntent; +import android.content.Context; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.os.SystemProperties; +import android.util.Log; + +import java.io.IOException; +import java.util.HashMap; + +/** + * @hide + */ +public class SerialManager { + private static final String TAG = "SerialManager"; + + private final Context mContext; + private final ISerialManager mService; + + /** + * {@hide} + */ + public SerialManager(Context context, ISerialManager service) { + mContext = context; + mService = service; + } + + /** + * Returns a string array containing the names of available serial ports + * + * @return names of available serial ports + */ + public String[] getSerialPorts() { + try { + return mService.getSerialPorts(); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in getSerialPorts", e); + return null; + } + } + + /** + * Opens and returns the {@link android.hardware.SerialPort} with the given name. + * The speed of the serial port must be one of: + * 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, + * 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, + * 1500000, 2000000, 2500000, 3000000, 3500000 or 4000000 + * + * @param name of the serial port + * @param speed at which to open the serial port + * @return the serial port + */ + public SerialPort openSerialPort(String name, int speed) throws IOException { + try { + ParcelFileDescriptor pfd = mService.openSerialPort(name); + if (pfd != null) { + SerialPort port = new SerialPort(name); + port.open(pfd, speed); + return port; + } else { + throw new IOException("Could not open serial port " + name); + } + } catch (RemoteException e) { + Log.e(TAG, "exception in UsbManager.openDevice", e); + } + return null; + } +} diff --git a/core/java/android/hardware/SerialPort.java b/core/java/android/hardware/SerialPort.java new file mode 100644 index 0000000..0889790 --- /dev/null +++ b/core/java/android/hardware/SerialPort.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware; + +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; + +import java.nio.ByteBuffer; + +/** + * @hide + */ +public class SerialPort { + + private static final String TAG = "SerialPort"; + + // used by the JNI code + private int mNativeContext; + private final String mName; + private ParcelFileDescriptor mFileDescriptor; + + /** + * SerialPort should only be instantiated by SerialManager + * @hide + */ + public SerialPort(String name) { + mName = name; + } + + /** + * SerialPort should only be instantiated by SerialManager + * Speed must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, + * 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, + * 1500000, 2000000, 2500000, 3000000, 3500000, 4000000 + * + * @hide + */ + public void open(ParcelFileDescriptor pfd, int speed) throws IOException { + native_open(pfd.getFileDescriptor(), speed); + mFileDescriptor = pfd; + } + + /** + * Closes the serial port + */ + public void close() throws IOException { + if (mFileDescriptor != null) { + mFileDescriptor.close(); + mFileDescriptor = null; + } + native_close(); + } + + /** + * Returns the name of the serial port + * + * @return the serial port's name + */ + public String getName() { + return mName; + } + + /** + * Reads data into the provided buffer + * + * @param buffer to read into + * @return number of bytes read + */ + public int read(ByteBuffer buffer) throws IOException { + if (buffer.isDirect()) { + return native_read_direct(buffer, buffer.remaining()); + } else if (buffer.hasArray()) { + return native_read_array(buffer.array(), buffer.remaining()); + } else { + throw new IllegalArgumentException("buffer is not direct and has no array"); + } + } + + /** + * Writes data from provided buffer + * + * @param buffer to write + * @param length number of bytes to write + */ + public void write(ByteBuffer buffer, int length) throws IOException { + if (buffer.isDirect()) { + native_write_direct(buffer, length); + } else if (buffer.hasArray()) { + native_write_array(buffer.array(), length); + } else { + throw new IllegalArgumentException("buffer is not direct and has no array"); + } + } + + private native void native_open(FileDescriptor pfd, int speed) throws IOException; + private native void native_close(); + private native int native_read_array(byte[] buffer, int length) throws IOException; + private native int native_read_direct(ByteBuffer buffer, int length) throws IOException; + private native void native_write_array(byte[] buffer, int length) throws IOException; + private native void native_write_direct(ByteBuffer buffer, int length) throws IOException; +} |
