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 an 14 * limitations under the License. 15 */ 16 17 package com.android.server; 18 19 import android.content.Context; 20 import android.hardware.ISerialManager; 21 import android.os.ParcelFileDescriptor; 22 23 import java.io.File; 24 import java.util.ArrayList; 25 26 public 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); 54 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); 60 } 61 62 private native ParcelFileDescriptor native_open(String path); 63 } 64