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 | |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 17 | package android.hardware; |
| 18 | |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 19 | import android.annotation.SystemService; |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 21 | import android.content.Context; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 22 | import android.os.ParcelFileDescriptor; |
| 23 | import android.os.RemoteException; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 24 | |
| 25 | import java.io.IOException; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * @hide |
| 29 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 30 | @SystemService(Context.SERIAL_SERVICE) |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 31 | public class SerialManager { |
| 32 | private static final String TAG = "SerialManager"; |
| 33 | |
| 34 | private final Context mContext; |
| 35 | private final ISerialManager mService; |
| 36 | |
| 37 | /** |
| 38 | * {@hide} |
| 39 | */ |
| 40 | public SerialManager(Context context, ISerialManager service) { |
| 41 | mContext = context; |
| 42 | mService = service; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns a string array containing the names of available serial ports |
| 47 | * |
| 48 | * @return names of available serial ports |
| 49 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 50 | @UnsupportedAppUsage |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 51 | public String[] getSerialPorts() { |
| 52 | try { |
| 53 | return mService.getSerialPorts(); |
| 54 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 55 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Opens and returns the {@link android.hardware.SerialPort} with the given name. |
| 61 | * The speed of the serial port must be one of: |
| 62 | * 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, |
| 63 | * 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, |
| 64 | * 1500000, 2000000, 2500000, 3000000, 3500000 or 4000000 |
| 65 | * |
| 66 | * @param name of the serial port |
| 67 | * @param speed at which to open the serial port |
| 68 | * @return the serial port |
| 69 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 70 | @UnsupportedAppUsage |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 71 | public SerialPort openSerialPort(String name, int speed) throws IOException { |
| 72 | try { |
| 73 | ParcelFileDescriptor pfd = mService.openSerialPort(name); |
| 74 | if (pfd != null) { |
| 75 | SerialPort port = new SerialPort(name); |
| 76 | port.open(pfd, speed); |
| 77 | return port; |
| 78 | } else { |
| 79 | throw new IOException("Could not open serial port " + name); |
| 80 | } |
| 81 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 82 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 83 | } |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 84 | } |
| 85 | } |