/services/java/com/android/server/SerialService.java

https://github.com/aizuzi/platform_frameworks_base · Java · 63 lines · 38 code · 10 blank · 15 comment · 4 complexity · 7b910dec2fcf1afbd875c686246aa848 MD5 · raw file

  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. package com.android.server;
  17. import android.content.Context;
  18. import android.hardware.ISerialManager;
  19. import android.os.ParcelFileDescriptor;
  20. import java.io.File;
  21. import java.util.ArrayList;
  22. public class SerialService extends ISerialManager.Stub {
  23. private final Context mContext;
  24. private final String[] mSerialPorts;
  25. public SerialService(Context context) {
  26. mContext = context;
  27. mSerialPorts = context.getResources().getStringArray(
  28. com.android.internal.R.array.config_serialPorts);
  29. }
  30. public String[] getSerialPorts() {
  31. mContext.enforceCallingOrSelfPermission(android.Manifest.permission.SERIAL_PORT, null);
  32. ArrayList<String> ports = new ArrayList<String>();
  33. for (int i = 0; i < mSerialPorts.length; i++) {
  34. String path = mSerialPorts[i];
  35. if (new File(path).exists()) {
  36. ports.add(path);
  37. }
  38. }
  39. String[] result = new String[ports.size()];
  40. ports.toArray(result);
  41. return result;
  42. }
  43. public ParcelFileDescriptor openSerialPort(String path) {
  44. mContext.enforceCallingOrSelfPermission(android.Manifest.permission.SERIAL_PORT, null);
  45. for (int i = 0; i < mSerialPorts.length; i++) {
  46. if (mSerialPorts[i].equals(path)) {
  47. return native_open(path);
  48. }
  49. }
  50. throw new IllegalArgumentException("Invalid serial port " + path);
  51. }
  52. private native ParcelFileDescriptor native_open(String path);
  53. }