ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
การตรวจหาอุปกรณ์ใน Tradefed
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การเชื่อมต่ออุปกรณ์ใหม่จะทริกเกอร์ชุดเหตุการณ์ที่เกิดขึ้นไม่พร้อมกันซึ่งไม่ชัดเจนแต่ควรทำความเข้าใจ
เชื่อมต่อทางกายภาพ
Tradefed ใช้ไลบรารี ddmlib
(ไลบรารี adb
ของ Java) เพื่อให้บริการการโต้ตอบพื้นฐานกับ adb
และอุปกรณ์ ส่วนหนึ่งคืออินเทอร์เฟซ IDeviceChangeListener ที่ช่วยให้รับเหตุการณ์ใหม่ของอุปกรณ์ได้ เช่น
deviceConnected
: เมื่อ adb
พบอุปกรณ์ใหม่
deviceDisconnected
: เมื่ออุปกรณ์ไม่ได้รายงานไปยัง adb
อีกต่อไป
deviceChanged
: เมื่อสถานะอุปกรณ์ที่สำคัญเกิดขึ้น (เช่น อุปกรณ์ออฟไลน์หรืออุปกรณ์ออนไลน์)
เหตุการณ์เหล่านี้เพียงพอในระดับ adb
ในการตัดสินใจว่าอุปกรณ์เชื่อมต่ออยู่ ออนไลน์ หรือออฟไลน์ แต่สำหรับชุดทดสอบ เราต้องการสถานะที่ดีกว่านี้เพื่อให้มั่นใจว่าอุปกรณ์พร้อมที่จะเริ่มการทดสอบจริงๆ และไม่ควรได้รับผลกระทบจากสถานะที่อาจไม่เสถียรซึ่งอาจเกิดขึ้นกับอุปกรณ์ที่เพิ่งเชื่อมต่อ
ลำดับเหตุการณ์ใน Tradefed มีดังนี้
- อุปกรณ์ได้รับการจดจำเป็น
deviceConnected
และเปิดรับเหตุการณ์ปกติจาก
adb
ระบบจะสร้างกิจกรรม Tradefed ภายในที่จะทําสิ่งต่อไปนี้
- ตรวจสอบว่าระบบรู้จักอุปกรณ์แล้วหรือยัง โดย Tradefed จะเก็บข้อมูลอ้างอิงภายในสำหรับอุปกรณ์บางเครื่อง (โดยเฉพาะอุปกรณ์ที่กําลังจัดสรรและทำการทดสอบอยู่) เพื่อไม่ให้ TF สูญเสียการติดตามอุปกรณ์แบบสุ่ม
- ตรวจสอบว่าอุปกรณ์เป็น
ONLINE
หรือ OFFLINE
ในกรณีที่อุปกรณ์เป็น
OFFLINE
: ระบบจะเปลี่ยนสถานะของอุปกรณ์เป็น CONNECTED_OFFLINE
Tradefed ซึ่งยังไม่อนุญาตให้อุปกรณ์ทำการทดสอบ หากอุปกรณ์ออนไลน์ในภายหลัง อุปกรณ์จะเข้าสู่วงจร ONLINE
หากเราได้รับเหตุการณ์ deviceDisconnect
ระบบจะนำอุปกรณ์ออกจากรายการ
ONLINE
(ตามที่ adb เห็น): ระบบจะตั้งค่าอุปกรณ์เป็นสถานะ
CONNECTED_ONLINE
และจะตรวจสอบความพร้อมใช้งานของอุปกรณ์สำหรับการทดสอบการกําหนดค่า: checking_availability
หากการตรวจสอบ availability
สำเร็จ ระบบจะทําเครื่องหมายอุปกรณ์ว่าพร้อมสําหรับการจัดสรรการทดสอบ และอุปกรณ์จะทําการทดสอบได้ หากไม่ ระบบจะทำเครื่องหมายอุปกรณ์เป็น unavailable
สำหรับการแบ่งสรรและอุปกรณ์จะไม่ได้รับการจัดทดสอบ
สถานะทั้งหมดเหล่านี้จะแสดงในคอนโซล Tradefed เมื่อแสดงข้อมูลอุปกรณ์ผ่าน tf> list devices
โปรดทราบว่าเมื่ออุปกรณ์ได้รับการจัดสรรสำหรับการทดสอบแล้ว เหตุการณ์ข้างต้นส่วนใหญ่จะไม่เกิดขึ้นและ Tradefed จะกำหนดสถานะของอุปกรณ์เป็นการภายใน ดังนั้นอุปกรณ์จึงอาจหายไปจาก adb devices
ขณะยังแสดงอยู่ใน Tradefed กรณีนี้อาจเกิดขึ้นเมื่อการทดสอบกำลังรีบูตอุปกรณ์
อุปกรณ์เสมือนที่เชื่อมต่อด้วย adb connect
เมื่อสร้างอุปกรณ์เสมือนระยะไกลแล้ว Tradefed จะเชื่อมต่อกับอุปกรณ์ดังกล่าวโดยใช้ adb
connect
ซึ่งโดยปกติแล้วจะทริกเกอร์อุปกรณ์ที่แสดงใน adb devices
เป็น <some ip>:<port number>
และจะเป็นไปตามลำดับเดียวกับอุปกรณ์ที่เชื่อมต่ออยู่จริง
การติดตามอุปกรณ์เมื่อมีเหตุการณ์ deviceConnected เกิดขึ้น
เมื่อ deviceConnected
เกิดขึ้น ddmlib
จะสร้างข้อมูลอ้างอิงใหม่ IDevice เพื่อติดตามอุปกรณ์ที่เชื่อมต่อใหม่
Tradefed ใช้การอ้างอิงนั้นและรวมไว้ในการใช้งานอินเตอร์เฟซอุปกรณ์ของตัวเอง ITestDevice เพื่อให้บริการขั้นสูงมากขึ้น แต่ IDevice พื้นฐานคืออุปกรณ์ที่มาจาก ddmlib
เสมอ
ซึ่งหมายความว่าหากมีการเชื่อมต่ออุปกรณ์ใหม่ ระบบจะสร้าง ITestDevice ใหม่และเชื่อมโยงกับ IDevice เมื่อเกิดกรณีนี้ขึ้นระหว่างการเรียกใช้และมีการใช้ ITestDevice ระบบจะยังคงแทนที่ IDevice ที่เกี่ยวข้องเพื่อให้การทดสอบดำเนินการต่อได้ตามข้อมูลอ้างอิงที่เหมาะสม ซึ่งจะดำเนินการได้อย่างราบรื่นทุกครั้งที่อุปกรณ์ตัดการเชื่อมต่อ/เชื่อมต่อใหม่ (เช่น ในระหว่างการรีบูต)
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Device detection in Tradefed\n\nA new device connection triggers a series of asynchronous events that are not\nobvious yet worth understanding.\n\nPhysically connected\n--------------------\n\nTradefed uses the `ddmlib` library (a Java `adb` library) to provide the basic\ninteraction with `adb` and devices. Part of this solution is the\n[IDeviceChangeListener interface](https://android.googlesource.com/platform/tools/base/+/refs/heads/android16-release/ddmlib/src/main/java/com/android/ddmlib/AndroidDebugBridge.java)\nthat allows reception of new device events, such as:\n\n- `deviceConnected`: When a new device is seen by `adb`\n- `deviceDisconnected`: When a device is not reporting to `adb` anymore\n- `deviceChanged`: When a major device state occurs (such as device offline or device online)\n\nThese events are enough at the `adb` level to decide whether or not a device is\nconnected, online, or offline. But for the test harness, we need a stronger\nstate than this to ensure a device is truly ready to start running tests; it\nshould not be impacted by potential state flakiness that can come with a newly\nconnected device.\n\nThis is the sequence of events in Tradefed:\n\n1. Device is recognized as `deviceConnected` and open to regular events from `adb`\n2. An internal Tradefed event is created that will:\n\n - Check if the device is known already; Tradefed keeps internal reference to some devices (especially the one current allocated and running tests) to avoid TF losing track of them randomly.\n - Check if the device is `ONLINE` or `OFFLINE`.\n3. If device is:\n\n - `OFFLINE`: Device will be switched to Tradefed `CONNECTED_OFFLINE`\n state, which doesn't allow the device to run tests yet. If the device is\n online later, it will go through the `ONLINE` cycle. If we receive a\n `deviceDisconnect` event, the device will simply be removed from the list.\n\n - `ONLINE` (as seen by adb): Device will be put in the state\n `CONNECTED_ONLINE` and will have its availability checked for test\n allocation: `checking_availability`.\n\n4. If `availability` check is successful, the device will be marked as\n available for test allocation; it will be able to run tests. If not, the\n device will be marked as `unavailable` for allocation and cannot receive any\n tests.\n\nAll of these states are reflected in the Tradefed console when listing the\ndevices via: `tf\u003e list devices`\n\nIt's important to note that when the device is currently allocated for a test,\nmost of the above will not occur and Tradefed will determine the device state\ninternally. So it's possible for a device to disappear from `adb devices` while\nstill being listed by Tradefed. That can happen when a test is rebooting the\ndevice for example.\n\nVirtual device connected with adb connect\n-----------------------------------------\n\nWhen a remote virtual device is created, Tradefed connects to it using `adb\nconnect`. This will usually trigger the device showing in `adb devices` as\n`\u003csome ip\u003e:\u003cport number\u003e` and will follow the same sequence as physically\nconnected devices.\n\nDevice tracking when a deviceConnected event occurs\n---------------------------------------------------\n\nWhen `deviceConnected` occurs, `ddmlib` creates a new reference\n[IDevice](https://android.googlesource.com/platform/tools/base/+/refs/heads/android16-release/ddmlib/src/main/java/com/android/ddmlib/IDevice.java)\nto track the newly connected device.\n\nTradefed uses that reference and wraps it in its own implementation of device\ninterface\n[ITestDevice](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/device_build_interfaces/com/android/tradefed/device/ITestDevice.java)\nto provide more advanced service. But the underlying IDevice is always the one\ncoming from `ddmlib`.\n\nThis means if a new device is connected, a new ITestDevice is created and\nassociated with the IDevice. When this happens during an invocation and the\nITestDevice is being used, the underlying IDevice is still replaced so\ntesting can proceed on the proper reference. This is done seamlessly each time a\ndevice is disconnected/reconnected (for example, during a reboot)."]]