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