Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d14beb99 authored by Kacper Kapłon's avatar Kacper Kapłon Committed by Android (Google) Code Review
Browse files

Merge changes from topic "serial_enumerate_devices" into main

* changes:
  Add short test description and lint minor fixes
  Read serial drivers for /proc/tty/drivers file
parents b35740e7 4820c0f0
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
team {
    name: "trendy_team_webapps_cros_android",

    // go/trendy/manage/engineers/5643017580281856
    trendy_team_id: "5643017580281856",
}
+2 −0
Original line number Diff line number Diff line
@@ -135,6 +135,7 @@ filegroup {
        ":services.restrictions-sources",
        ":services.searchui-sources",
        ":services.selectiontoolbar-sources",
        ":services.serial-sources",
        ":services.smartspace-sources",
        ":services.soundtrigger-sources",
        ":services.supervision-sources",
@@ -313,6 +314,7 @@ java_library {
        "services.restrictions",
        "services.searchui",
        "services.selectiontoolbar",
        "services.serial",
        "services.smartspace",
        "services.soundtrigger",
        "services.supervision",
+24 −0
Original line number Diff line number Diff line
package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
}

filegroup {
    name: "services.serial-sources",
    srcs: ["java/**/*.java"],
    path: "java",
    visibility: ["//frameworks/base/services"],
}

java_library_static {
    name: "services.serial",
    defaults: ["platform_service_defaults"],
    srcs: [":services.serial-sources"],
    libs: [
        "services.core",
    ],
}
+92 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.serial;

import android.os.FileUtils;
import android.text.TextUtils;
import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

final class SerialDriversDiscovery {

    private static final String TAG = SerialDriversDiscovery.class.getSimpleName();
    private static final String PROC_TTY_DRIVERS_FILE = "/proc/tty/drivers";
    private static final String SERIAL_DRIVER_TYPE = "serial";

    private final String mDriversFile;

    SerialDriversDiscovery() {
        this(PROC_TTY_DRIVERS_FILE);
    }

    @VisibleForTesting
    SerialDriversDiscovery(String driversFile) {
        mDriversFile = driversFile;
    }

    List<SerialTtyDriverInfo> discover() throws IOException {
        String[] entries = readDriverFile();
        List<SerialTtyDriverInfo> driversList = new ArrayList<>();
        for (int i = 0; i < entries.length; i++) {
            String entry = entries[i];
            Optional<SerialTtyDriverInfo> driver = parseDriverInfoIfSerial(entry);
            if (driver.isPresent()) {
                driversList.add(driver.get());
            }
        }
        return driversList;
    }

    private String[] readDriverFile() throws IOException {
        File f = new File(mDriversFile);
        String fileContent = FileUtils.readTextFile(f, 0, null);
        return fileContent.split("\n");
    }

    private static Optional<SerialTtyDriverInfo> parseDriverInfoIfSerial(String entry) {
        // e.g.: entry="usbserial    /dev/ttyUSB   188    0-254    serial"
        String[] parts = entry.split("\\s+");
        if (parts.length != 5) {
            Slog.e(TAG, TextUtils.formatSimple("Failed parsing line in '%s'", entry));
            return Optional.empty();
        }

        if (!SERIAL_DRIVER_TYPE.equals(parts[4])) {
            // not a serial driver
            return Optional.empty();
        }

        try {
            String path = parts[1];
            int majorNumber = Integer.parseInt(parts[2]);
            String[] minorSplit = parts[3].split("-");
            int minorFrom = Integer.parseInt(minorSplit[0]);
            int minorTo = minorSplit.length > 1 ? Integer.parseInt(minorSplit[1]) : minorFrom;
            return Optional.of(new SerialTtyDriverInfo(path, majorNumber, minorFrom, minorTo));
        } catch (NumberFormatException e) {
            Slog.e(TAG, TextUtils.formatSimple("Failed paring number in '%s'", entry));
            return Optional.empty();
        }
    }
}
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.serial;

final class SerialTtyDriverInfo {

    final String mDevicePath;
    final int mMajorNumber;
    final int mMinorNumberFrom;
    final int mMinorNumberTo;

    SerialTtyDriverInfo(
            String devicePath, int majorNumber, int minorNumberFrom, int minorNumberTo) {
        mDevicePath = devicePath;
        mMajorNumber = majorNumber;
        mMinorNumberFrom = minorNumberFrom;
        mMinorNumberTo = minorNumberTo;
    }
}
Loading