Add ability to specify device address for wifi p2p
This commit is contained in:
@@ -28,6 +28,7 @@ import be.mygod.vpnhotspot.util.*
|
||||
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||
import kotlinx.coroutines.*
|
||||
import timber.log.Timber
|
||||
import java.lang.IllegalArgumentException
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.net.NetworkInterface
|
||||
|
||||
@@ -44,6 +45,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
|
||||
private const val KEY_PASSPHRASE = "service.repeater.passphrase"
|
||||
private const val KEY_OPERATING_BAND = "service.repeater.band"
|
||||
private const val KEY_OPERATING_CHANNEL = "service.repeater.oc"
|
||||
private const val KEY_DEVICE_ADDRESS = "service.repeater.mac"
|
||||
/**
|
||||
* Placeholder for bypassing networkName check.
|
||||
*/
|
||||
@@ -90,6 +92,17 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
|
||||
return if (result in 1..165) result else 0
|
||||
}
|
||||
set(value) = app.pref.edit { putString(KEY_OPERATING_CHANNEL, value.toString()) }
|
||||
var deviceAddress: MacAddressCompat?
|
||||
get() = try {
|
||||
MacAddressCompat(app.pref.getLong(KEY_DEVICE_ADDRESS, MacAddressCompat.ANY_ADDRESS.addr)).run {
|
||||
validate()
|
||||
if (this == MacAddressCompat.ANY_ADDRESS) null else this
|
||||
}
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Timber.w(e)
|
||||
null
|
||||
}
|
||||
set(value) = app.pref.edit { putLong(KEY_DEVICE_ADDRESS, (value ?: MacAddressCompat.ANY_ADDRESS).addr) }
|
||||
}
|
||||
|
||||
enum class Status {
|
||||
@@ -317,6 +330,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
|
||||
if (oc == 0) setGroupOperatingBand(operatingBand)
|
||||
else setGroupOperatingFrequency(SoftApConfigurationCompat.channelToFrequency(oc))
|
||||
}
|
||||
setDeviceAddress(deviceAddress?.toPlatform())
|
||||
}.build().run {
|
||||
useParcel { p ->
|
||||
p.writeParcelable(this, 0)
|
||||
|
||||
@@ -192,13 +192,15 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
else -> throw IllegalArgumentException("Unknown operatingBand")
|
||||
}
|
||||
channel = RepeaterService.operatingChannel
|
||||
bssid = RepeaterService.deviceAddress
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val group = binder?.group
|
||||
if (group != null) try {
|
||||
val config = withContext(Dispatchers.Default) {
|
||||
P2pSupplicantConfiguration(group, binder?.thisDevice?.deviceAddress)
|
||||
P2pSupplicantConfiguration(group,
|
||||
binder?.thisDevice?.deviceAddress, RepeaterService.deviceAddress?.toString())
|
||||
}
|
||||
holder.config = config
|
||||
return SoftApConfigurationCompat.empty().apply {
|
||||
@@ -209,6 +211,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
band = SoftApConfigurationCompat.BAND_ANY
|
||||
channel = RepeaterService.operatingChannel
|
||||
}
|
||||
bssid = RepeaterService.deviceAddress
|
||||
}
|
||||
} catch (e: RuntimeException) {
|
||||
Timber.w(e)
|
||||
@@ -239,5 +242,6 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
holder.config = null
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= 23) RepeaterService.operatingChannel = config.channel
|
||||
RepeaterService.deviceAddress = config.bssid
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,8 @@ inline class MacAddressCompat(val addr: Long) : Parcelable {
|
||||
fun MacAddress.toCompat() = fromBytes(toByteArray())
|
||||
}
|
||||
|
||||
fun validate() = require(addr and ((1L shl 48) - 1).inv() == 0L)
|
||||
|
||||
fun toList() = ByteBuffer.allocate(8).run {
|
||||
order(ByteOrder.LITTLE_ENDIAN)
|
||||
putLong(addr)
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.io.File
|
||||
* https://android.googlesource.com/platform/external/wpa_supplicant_8/+/d2986c2/wpa_supplicant/config.c#488
|
||||
* https://android.googlesource.com/platform/external/wpa_supplicant_8/+/6fa46df/wpa_supplicant/config_file.c#182
|
||||
*/
|
||||
class P2pSupplicantConfiguration(private val group: WifiP2pGroup, ownerAddress: String?) {
|
||||
class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAddresses: String?) {
|
||||
companion object {
|
||||
private const val TAG = "P2pSupplicantConfiguration"
|
||||
private const val CONF_PATH_TREBLE = "/data/vendor/wifi/wpa/p2p_supplicant.conf"
|
||||
@@ -53,7 +53,8 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, ownerAddress:
|
||||
RootSession.checkOutput(command, shell, false, false)
|
||||
val parser = Parser(shell.out)
|
||||
try {
|
||||
val bssids = listOfNotNull(group.owner.deviceAddress, ownerAddress, RepeaterService.lastMac)
|
||||
val bssids = (listOf(group.owner.deviceAddress, RepeaterService.lastMac) + ownerAddresses)
|
||||
.filterNotNull()
|
||||
.distinct()
|
||||
.filter {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user