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