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