blob: 1abe4588261a1df529feab6a5d5fccaa18189b98 [file] [log] [blame]
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001/*
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 an
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.content.Context;
20import android.hardware.ISerialManager;
21import android.os.ParcelFileDescriptor;
22
23import java.io.File;
24import java.util.ArrayList;
25
26public class SerialService extends ISerialManager.Stub {
27
28 private final Context mContext;
29 private final String[] mSerialPorts;
30
31 public SerialService(Context context) {
32 mContext = context;
33 mSerialPorts = context.getResources().getStringArray(
34 com.android.internal.R.array.config_serialPorts);
35 }
36
37 public String[] getSerialPorts() {
38 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.SERIAL_PORT, null);
39
40 ArrayList<String> ports = new ArrayList<String>();
41 for (int i = 0; i < mSerialPorts.length; i++) {
42 String path = mSerialPorts[i];
43 if (new File(path).exists()) {
44 ports.add(path);
45 }
46 }
47 String[] result = new String[ports.size()];
48 ports.toArray(result);
49 return result;
50 }
51
52 public ParcelFileDescriptor openSerialPort(String path) {
53 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.SERIAL_PORT, null);
Mike Lockwood80248d42012-10-31 12:41:14 -070054 for (int i = 0; i < mSerialPorts.length; i++) {
55 if (mSerialPorts[i].equals(path)) {
56 return native_open(path);
57 }
58 }
59 throw new IllegalArgumentException("Invalid serial port " + path);
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040060 }
61
62 private native ParcelFileDescriptor native_open(String path);
63}