kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
				
				
				
			change to use @mik3y /usb-serial-for-android (WIP)
							rodzic
							
								
									712e034228
								
							
						
					
					
						commit
						d1a3d98de4
					
				| 
						 | 
				
			
			@ -21,5 +21,10 @@
 | 
			
		|||
      <option name="name" value="Google" />
 | 
			
		||||
      <option name="url" value="https://dl.google.com/dl/android/maven2/" />
 | 
			
		||||
    </remote-repository>
 | 
			
		||||
    <remote-repository>
 | 
			
		||||
      <option name="id" value="maven" />
 | 
			
		||||
      <option name="name" value="maven" />
 | 
			
		||||
      <option name="url" value="https://jitpack.io" />
 | 
			
		||||
    </remote-repository>
 | 
			
		||||
  </component>
 | 
			
		||||
</project>
 | 
			
		||||
| 
						 | 
				
			
			@ -121,7 +121,8 @@ dependencies {
 | 
			
		|||
    implementation 'com.google.protobuf:protobuf-javalite:3.12.2'
 | 
			
		||||
 | 
			
		||||
    // For UART access
 | 
			
		||||
    implementation 'com.google.android.things:androidthings:1.0'
 | 
			
		||||
    // implementation 'com.google.android.things:androidthings:1.0'
 | 
			
		||||
    implementation 'com.github.mik3y:usb-serial-for-android:v2.2.2'
 | 
			
		||||
 | 
			
		||||
    // mapbox
 | 
			
		||||
    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.1.0'
 | 
			
		||||
| 
						 | 
				
			
			@ -136,7 +137,7 @@ dependencies {
 | 
			
		|||
    implementation 'com.google.android.gms:play-services-auth:18.0.0'
 | 
			
		||||
 | 
			
		||||
    // Add the Firebase SDK for Crashlytics.
 | 
			
		||||
    implementation 'com.google.firebase:firebase-crashlytics:17.0.0'
 | 
			
		||||
    implementation 'com.google.firebase:firebase-crashlytics:17.0.1'
 | 
			
		||||
 | 
			
		||||
    // alas implementation bug deep in the bowels when I tried it for my SyncBluetoothDevice class
 | 
			
		||||
    // implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -41,7 +41,7 @@
 | 
			
		|||
    <uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND" />
 | 
			
		||||
 | 
			
		||||
    <!-- Uart access -->
 | 
			
		||||
    <uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    <!-- the xing library will try to bring this permission in but we don't want it -->
 | 
			
		||||
    <uses-permission
 | 
			
		||||
| 
						 | 
				
			
			@ -69,8 +69,6 @@
 | 
			
		|||
        android:hardwareAccelerated="true"
 | 
			
		||||
        android:theme="@style/AppTheme">
 | 
			
		||||
 | 
			
		||||
        <uses-library android:name="com.google.android.things" />
 | 
			
		||||
 | 
			
		||||
        <meta-data
 | 
			
		||||
            android:name="com.mixpanel.android.MPConfig.DisableViewCrawler"
 | 
			
		||||
            android:value="true" />
 | 
			
		||||
| 
						 | 
				
			
			@ -110,7 +108,14 @@
 | 
			
		|||
        <service
 | 
			
		||||
            android:name="com.geeksville.mesh.service.SerialInterfaceService"
 | 
			
		||||
            android:enabled="true"
 | 
			
		||||
            android:exported="false" />
 | 
			
		||||
            android:exported="false">
 | 
			
		||||
            <intent-filter>
 | 
			
		||||
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
 | 
			
		||||
            </intent-filter>
 | 
			
		||||
            <meta-data
 | 
			
		||||
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
 | 
			
		||||
                android:resource="@xml/device_filter" />
 | 
			
		||||
        </service>
 | 
			
		||||
 | 
			
		||||
        <activity
 | 
			
		||||
            android:name="com.geeksville.mesh.MainActivity"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,36 +1,27 @@
 | 
			
		|||
package com.geeksville.mesh.service
 | 
			
		||||
 | 
			
		||||
import com.google.android.things.pio.PeripheralManager
 | 
			
		||||
import com.google.android.things.pio.UartDevice
 | 
			
		||||
import com.google.android.things.pio.UartDeviceCallback
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.hardware.usb.UsbManager
 | 
			
		||||
import com.hoho.android.usbserial.driver.UsbSerialDriver
 | 
			
		||||
import com.hoho.android.usbserial.driver.UsbSerialPort
 | 
			
		||||
import com.hoho.android.usbserial.driver.UsbSerialProber
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SerialInterfaceService : InterfaceService() {
 | 
			
		||||
    companion object {
 | 
			
		||||
        fun findPorts(): List<String> {
 | 
			
		||||
            val manager = PeripheralManager.getInstance()
 | 
			
		||||
            return manager.uartDeviceList
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private val START1 = 0x94.toByte()
 | 
			
		||||
        private val START2 = 0xc3.toByte()
 | 
			
		||||
        private val MAX_TO_FROM_RADIO_SIZE = 512
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private var uart: UartDevice? = null
 | 
			
		||||
    private var uart: UsbSerialPort? = null
 | 
			
		||||
 | 
			
		||||
    /** The index of the next byte we are hoping to receive */
 | 
			
		||||
    private var rxPtr = 0
 | 
			
		||||
 | 
			
		||||
    private val callback = object : UartDeviceCallback {
 | 
			
		||||
        override fun onUartDeviceDataAvailable(p0: UartDevice): Boolean {
 | 
			
		||||
 | 
			
		||||
            return uart != null // keep reading until our device goes away
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        override fun onUartDeviceError(uart: UartDevice, error: Int) {
 | 
			
		||||
            super.onUartDeviceError(uart, error)
 | 
			
		||||
        }
 | 
			
		||||
    private val manager: UsbManager by lazy {
 | 
			
		||||
        getSystemService(Context.USB_SERVICE) as UsbManager
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    override fun handleSendToRadio(p: ByteArray) {
 | 
			
		||||
        uart?.apply {
 | 
			
		||||
            val header = ByteArray(4)
 | 
			
		||||
| 
						 | 
				
			
			@ -38,36 +29,89 @@ class SerialInterfaceService : InterfaceService() {
 | 
			
		|||
            header[1] = START2
 | 
			
		||||
            header[2] = (p.size shr 8).toByte()
 | 
			
		||||
            header[3] = (p.size and 0xff).toByte()
 | 
			
		||||
            write(header, header.size)
 | 
			
		||||
            write(p, p.size)
 | 
			
		||||
            // flush(UartDevice.FLUSH_OUT) - I don't think we need to stall for htis
 | 
			
		||||
            write(header, 0)
 | 
			
		||||
            write(p, 0)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
    /** Print device serial debug output somewhere */
 | 
			
		||||
    private fun debugOut(c: Byte) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun readerLoop() {
 | 
			
		||||
        val scratch = ByteArray(1)
 | 
			
		||||
        var ptr = 0
 | 
			
		||||
        while (true) { // FIXME wait for someone to ask us to exit, and catch continuation exception
 | 
			
		||||
            uart?.apply {
 | 
			
		||||
                read(scratch, 1)
 | 
			
		||||
                when (ptr) {
 | 
			
		||||
                    0 ->
 | 
			
		||||
        try {
 | 
			
		||||
            val scratch = ByteArray(1)
 | 
			
		||||
 | 
			
		||||
            /** The index of the next byte we are hoping to receive */
 | 
			
		||||
            var ptr = 0
 | 
			
		||||
 | 
			
		||||
            /** The two halves of our length */
 | 
			
		||||
            var msb = 0
 | 
			
		||||
            var lsb = 0
 | 
			
		||||
 | 
			
		||||
            while (true) { // FIXME wait for someone to ask us to exit, and catch continuation exception
 | 
			
		||||
                uart?.apply {
 | 
			
		||||
                    read(scratch, 0)
 | 
			
		||||
                    val c = scratch[0]
 | 
			
		||||
 | 
			
		||||
                    // Assume we will be advancing our pointer
 | 
			
		||||
                    var nextPtr = ptr + 1
 | 
			
		||||
 | 
			
		||||
                    when (ptr) {
 | 
			
		||||
                        0 -> // looking for START1
 | 
			
		||||
                            if (c != START1) {
 | 
			
		||||
                                debugOut(c)
 | 
			
		||||
                                nextPtr = 0 // Restart from scratch
 | 
			
		||||
                            }
 | 
			
		||||
                        1 -> // Looking for START2
 | 
			
		||||
                            if (c != START2)
 | 
			
		||||
                                nextPtr = 0 // Restart from scratch
 | 
			
		||||
                        2 -> // Looking for MSB of our 16 bit length
 | 
			
		||||
                            msb = c.toInt() and 0xff
 | 
			
		||||
                        3 -> // Looking for LSB of our 16 bit length
 | 
			
		||||
                            lsb = c.toInt() and 0xff
 | 
			
		||||
                        else -> { // We've read our header, do one big read for the packet itself
 | 
			
		||||
                            val packetLen = (msb shl 8) or lsb
 | 
			
		||||
 | 
			
		||||
                            // If packet len is too long, the bytes must have been corrupted, start looking for START1 again
 | 
			
		||||
                            if (packetLen <= MAX_TO_FROM_RADIO_SIZE) {
 | 
			
		||||
                                val buf = ByteArray(packetLen)
 | 
			
		||||
                                read(buf, 0)
 | 
			
		||||
                                handleFromRadio(buf)
 | 
			
		||||
                            }
 | 
			
		||||
                            nextPtr = 0 // Start parsing the next packet
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    ptr = nextPtr
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } catch (ex: Exception) {
 | 
			
		||||
            errormsg("Terminating reader thread due to ${ex.message}", ex)
 | 
			
		||||
        }
 | 
			
		||||
    } */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    override fun onCreate() {
 | 
			
		||||
        super.onCreate()
 | 
			
		||||
 | 
			
		||||
        val port = findPorts()[0]
 | 
			
		||||
        val manager = PeripheralManager.getInstance()
 | 
			
		||||
        uart = manager.openUartDevice(port)
 | 
			
		||||
        uart?.apply {
 | 
			
		||||
            setBaudrate(921600)
 | 
			
		||||
            registerUartDeviceCallback(callback)
 | 
			
		||||
        val drivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager)
 | 
			
		||||
 | 
			
		||||
        // Open a connection to the first available driver.
 | 
			
		||||
        // Open a connection to the first available driver.
 | 
			
		||||
        val driver: UsbSerialDriver = drivers[0]
 | 
			
		||||
        val connection = manager.openDevice(driver.device)
 | 
			
		||||
        if (connection == null) {
 | 
			
		||||
            // FIXME add UsbManager.requestPermission(driver.getDevice(), ..) handling to activity
 | 
			
		||||
            TODO()
 | 
			
		||||
        } else {
 | 
			
		||||
            val port = driver.ports[0] // Most devices have just one port (port 0)
 | 
			
		||||
 | 
			
		||||
            port.open(connection)
 | 
			
		||||
            port.setParameters(921600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
 | 
			
		||||
            uart = port
 | 
			
		||||
 | 
			
		||||
            // FIXME, start reading thread
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,58 @@
 | 
			
		|||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<resources>
 | 
			
		||||
    <!-- 0x0403 FTDI -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="1027"
 | 
			
		||||
        product-id="24577" /> <!-- 0x6001: FT232R -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="1027"
 | 
			
		||||
        product-id="24592" /> <!-- 0x6010: FT2232H -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="1027"
 | 
			
		||||
        product-id="24593" /> <!-- 0x6011: FT4232H -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="1027"
 | 
			
		||||
        product-id="24596" /> <!-- 0x6014: FT232H -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="1027"
 | 
			
		||||
        product-id="24597" /> <!-- 0x6015: FT231X -->
 | 
			
		||||
 | 
			
		||||
    <!-- 0x10C4 / 0xEAxx: Silabs CP210x -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="4292"
 | 
			
		||||
        product-id="60000" /> <!-- 0xea60: CP2102 -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="4292"
 | 
			
		||||
        product-id="60016" /> <!-- 0xea70: CP2105 -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="4292"
 | 
			
		||||
        product-id="60017" /> <!-- 0xea71: CP2108 -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="4292"
 | 
			
		||||
        product-id="60032" /> <!-- 0xea80: CP2110 -->
 | 
			
		||||
 | 
			
		||||
    <!-- 0x067B / 0x2303: Prolific PL2303 -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="1659"
 | 
			
		||||
        product-id="8963" />
 | 
			
		||||
 | 
			
		||||
    <!-- 0x1a86 / 0x7523: Qinheng CH340 -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="6790"
 | 
			
		||||
        product-id="29987" />
 | 
			
		||||
 | 
			
		||||
    <!-- CDC driver -->
 | 
			
		||||
    <usb-device vendor-id="9025" />                   <!-- 0x2341 / ......: Arduino -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="5824"
 | 
			
		||||
        product-id="1155" /> <!-- 0x16C0 / 0x0483: Teensyduino  -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="1003"
 | 
			
		||||
        product-id="8260" /> <!-- 0x03EB / 0x2044: Atmel Lufa -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="7855"
 | 
			
		||||
        product-id="4" /> <!-- 0x1eaf / 0x0004: Leaflabs Maple -->
 | 
			
		||||
    <usb-device
 | 
			
		||||
        vendor-id="3368"
 | 
			
		||||
        product-id="516" /> <!-- 0x0d28 / 0x0204: ARM mbed -->
 | 
			
		||||
</resources>
 | 
			
		||||
| 
						 | 
				
			
			@ -32,7 +32,7 @@ allprojects {
 | 
			
		|||
    repositories {
 | 
			
		||||
        google()
 | 
			
		||||
        jcenter()
 | 
			
		||||
 | 
			
		||||
        maven { url 'https://jitpack.io' }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Ładowanie…
	
		Reference in New Issue