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