2020-01-22 21:46:41 -08:00
|
|
|
package com.geeksville.mesh
|
2020-01-22 21:25:31 -08:00
|
|
|
|
|
|
|
|
import android.app.Service
|
2020-01-24 17:05:55 -08:00
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
|
import android.content.Context
|
2020-01-22 21:25:31 -08:00
|
|
|
import android.content.Intent
|
2020-01-24 17:05:55 -08:00
|
|
|
import android.content.IntentFilter
|
2020-01-22 21:25:31 -08:00
|
|
|
import android.os.IBinder
|
2020-01-22 22:16:30 -08:00
|
|
|
import com.geeksville.android.Logging
|
2020-01-22 21:25:31 -08:00
|
|
|
|
2020-01-24 17:05:55 -08:00
|
|
|
|
2020-01-23 08:09:50 -08:00
|
|
|
/**
|
2020-01-24 17:05:55 -08:00
|
|
|
* Handles all the communication with android apps. Also keeps an internal model
|
|
|
|
|
* of the network state.
|
|
|
|
|
*
|
2020-01-23 08:09:50 -08:00
|
|
|
* Note: this service will go away once all clients are unbound from it.
|
|
|
|
|
*/
|
2020-01-22 22:16:30 -08:00
|
|
|
class MeshService : Service(), Logging {
|
|
|
|
|
|
|
|
|
|
/*
|
2020-01-23 06:34:15 -08:00
|
|
|
see com.geeksville.mesh broadcast intents
|
2020-01-22 22:16:30 -08:00
|
|
|
// RECEIVED_OPAQUE for data received from other nodes
|
|
|
|
|
// NODE_CHANGE for new IDs appearing or disappearing
|
|
|
|
|
// CONNECTION_CHANGED for losing/gaining connection to the packet radio
|
|
|
|
|
*/
|
2020-01-24 17:05:55 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The RECEIVED_OPAQUE:
|
|
|
|
|
* Payload will be the raw bytes which were contained within a MeshPacket.Opaque field
|
|
|
|
|
* Sender will be a user ID string
|
|
|
|
|
*/
|
2020-01-22 22:16:30 -08:00
|
|
|
fun broadcastReceivedOpaque(senderId: String, payload: ByteArray) {
|
|
|
|
|
val intent = Intent("$prefix.RECEIVED_OPAQUE")
|
2020-01-24 17:05:55 -08:00
|
|
|
intent.putExtra(EXTRA_SENDER, senderId)
|
|
|
|
|
intent.putExtra(EXTRA_PAYLOAD, payload)
|
2020-01-22 22:16:30 -08:00
|
|
|
sendBroadcast(intent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun broadcastNodeChange(nodeId: String, isOnline: Boolean) {
|
|
|
|
|
val intent = Intent("$prefix.NODE_CHANGE")
|
2020-01-24 17:05:55 -08:00
|
|
|
intent.putExtra(EXTRA_ID, nodeId)
|
|
|
|
|
intent.putExtra(EXTRA_ONLINE, isOnline)
|
2020-01-22 22:16:30 -08:00
|
|
|
sendBroadcast(intent)
|
|
|
|
|
}
|
2020-01-22 21:25:31 -08:00
|
|
|
|
|
|
|
|
override fun onBind(intent: Intent): IBinder {
|
|
|
|
|
// Return the interface
|
|
|
|
|
return binder
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 17:05:55 -08:00
|
|
|
override fun onCreate() {
|
|
|
|
|
super.onCreate()
|
|
|
|
|
|
|
|
|
|
val filter = IntentFilter(RadioInterfaceService.RECEIVE_FROMRADIO_ACTION)
|
|
|
|
|
registerReceiver(radioInterfaceReceiver, filter)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
|
unregisterReceiver(radioInterfaceReceiver)
|
|
|
|
|
super.onDestroy()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receives messages from our BT radio service and processes them to update our model
|
|
|
|
|
* and send to clients as needed.
|
|
|
|
|
*/
|
|
|
|
|
private val radioInterfaceReceiver = object : BroadcastReceiver() {
|
|
|
|
|
|
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
|
|
|
val proto = MeshProtos.FromRadio.parseFrom(intent.getByteArrayExtra(EXTRA_PAYLOAD)!!)
|
|
|
|
|
TODO("FIXME - update model and send messages as needed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 21:25:31 -08:00
|
|
|
private val binder = object : IMeshService.Stub() {
|
2020-01-22 22:16:30 -08:00
|
|
|
override fun setOwner(myId: String, longName: String, shortName: String) {
|
|
|
|
|
error("TODO setOwner $myId : $longName : $shortName")
|
2020-01-22 21:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-22 22:16:30 -08:00
|
|
|
override fun sendOpaque(destId: String, payload: ByteArray) {
|
|
|
|
|
error("TODO sendOpaque $destId <- ${payload.size}")
|
2020-01-22 21:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-22 22:16:30 -08:00
|
|
|
override fun getOnline(): Array<String> {
|
|
|
|
|
error("TODO getOnline")
|
|
|
|
|
return arrayOf("+16508675309")
|
2020-01-22 21:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun isConnected(): Boolean {
|
2020-01-22 22:16:30 -08:00
|
|
|
error("TODO isConnected")
|
|
|
|
|
return true
|
2020-01-22 21:25:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|