mirror of
https://github.com/RoganDawes/P4wnP1_aloa.git
synced 2025-11-15 00:17:08 +01:00
Service: Rework bluetooth to configurable sub system
This commit is contained in:
@@ -325,13 +325,13 @@ func SetBridgeForwardDelay(name string, fd uint) (err error) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
func CheckInterfaceExistence(name string) (res bool, err error) {
|
||||
_, err = net.InterfaceByName(name)
|
||||
// ToDo: remove error part
|
||||
func CheckInterfaceExistence(name string) (res bool) {
|
||||
_, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false
|
||||
}
|
||||
return true, err
|
||||
return true
|
||||
}
|
||||
|
||||
func NetworkLinkUp(name string) (err error) {
|
||||
|
||||
@@ -283,10 +283,10 @@ Polls for presence of "usb0" / "usb1" till one of both is active or timeout is r
|
||||
|
||||
func pollForUSBEthernet(timeout time.Duration) error {
|
||||
for startTime := time.Now(); time.Since(startTime) < timeout; {
|
||||
if present, _ := CheckInterfaceExistence("usb0"); present {
|
||||
if present := CheckInterfaceExistence("usb0"); present {
|
||||
return nil
|
||||
}
|
||||
if present, _ := CheckInterfaceExistence("usb1"); present {
|
||||
if present := CheckInterfaceExistence("usb1"); present {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
pb "github.com/mame82/P4wnP1_go/proto"
|
||||
"github.com/mame82/P4wnP1_go/service/bluetooth"
|
||||
"github.com/mame82/mblue-toolz/toolz"
|
||||
"log"
|
||||
@@ -27,7 +28,7 @@ type BtService struct {
|
||||
serviceAvailable bool
|
||||
Controller *bluetooth.Controller
|
||||
BrName string
|
||||
bridgeIfDeployed bool
|
||||
//bridgeIfDeployed bool
|
||||
|
||||
Agent *bluetooth.DefaultAgent
|
||||
|
||||
@@ -92,8 +93,25 @@ func NewBtService(rootService *Service, retryTimeout time.Duration) (res *BtServ
|
||||
}
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
|
||||
if !res.serviceAvailable {
|
||||
log.Printf("No bluetooth adapter found after %v\n", retryTimeout)
|
||||
} else {
|
||||
//register the agent
|
||||
res.Agent.Start(toolz.AGENT_CAP_NO_INPUT_NO_OUTPUT)
|
||||
|
||||
// Deploy default settings
|
||||
defaultSettings := GetDefaultBluetoothSettings()
|
||||
_,err := res.DeployBluetoothControllerInformation(defaultSettings.Ci)
|
||||
if err != nil {
|
||||
log.Println("Not able to deploy default bluetooth settings: ", err.Error())
|
||||
} else {
|
||||
_,err = res.DeployBluetoothAgentSettings(defaultSettings.As)
|
||||
if err != nil {
|
||||
log.Println("Not able to deploy default bluetooth agent settings: ", err.Error())
|
||||
}
|
||||
}
|
||||
log.Println("Finished setting up bluetooth")
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -101,6 +119,22 @@ func NewBtService(rootService *Service, retryTimeout time.Duration) (res *BtServ
|
||||
return
|
||||
}
|
||||
|
||||
func (bt *BtService) Stop() {
|
||||
bt.Agent.Stop() // unregister the agent again
|
||||
if ci,err := bt.Controller.ReadControllerInformation(); err == nil {
|
||||
if ci.ServiceNetworkServerNap {
|
||||
bt.UnregisterNetworkServer(toolz.UUID_NETWORK_SERVER_NAP)
|
||||
}
|
||||
if ci.ServiceNetworkServerGn {
|
||||
bt.UnregisterNetworkServer(toolz.UUID_NETWORK_SERVER_GN)
|
||||
}
|
||||
if ci.ServiceNetworkServerPanu {
|
||||
bt.UnregisterNetworkServer(toolz.UUID_NETWORK_SERVER_PANU)
|
||||
}
|
||||
}
|
||||
bt.DisableBridge()
|
||||
}
|
||||
|
||||
func (bt *BtService) setServiceAvailable(val bool) {
|
||||
bt.serviceAvailableLock.Lock()
|
||||
defer bt.serviceAvailableLock.Unlock()
|
||||
@@ -113,9 +147,101 @@ func (bt *BtService) IsServiceAvailable() bool {
|
||||
return bt.serviceAvailable
|
||||
}
|
||||
|
||||
func (bt *BtService) DeployBluetoothNetworkService(btNwSvc *pb.BluetoothNetworkService) (err error) {
|
||||
uuid := toolz.UUID_NETWORK_SERVER_NAP
|
||||
switch btNwSvc.Type {
|
||||
case pb.BluetoothNetworkServiceType_NAP:
|
||||
uuid = toolz.UUID_NETWORK_SERVER_NAP
|
||||
case pb.BluetoothNetworkServiceType_PANU:
|
||||
uuid = toolz.UUID_NETWORK_SERVER_PANU
|
||||
case pb.BluetoothNetworkServiceType_GN:
|
||||
uuid = toolz.UUID_NETWORK_SERVER_GN
|
||||
}
|
||||
if btNwSvc.ServerOrConnect {
|
||||
// start server for given network service
|
||||
if btNwSvc.RegisterOrUnregister {
|
||||
return bt.RegisterNetworkServer(uuid)
|
||||
} else {
|
||||
return bt.UnregisterNetworkServer(uuid)
|
||||
}
|
||||
} else {
|
||||
//(dis)connect from/to given network network service of given remote device
|
||||
|
||||
if btNwSvc.RegisterOrUnregister {
|
||||
// register == connect
|
||||
return bt.ConnectNetwork(btNwSvc.MacOrName, uuid)
|
||||
} else {
|
||||
// unregister == disconnect
|
||||
return bt.DisconnectNetwork(btNwSvc.MacOrName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (bt *BtService) GetBluetoothAgentSettings() (as *pb.BluetoothAgentSettings, err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return &pb.BluetoothAgentSettings{},bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
as = &pb.BluetoothAgentSettings{}
|
||||
|
||||
pin,err := bt.GetPIN()
|
||||
if err != nil { return as,err }
|
||||
as.Pin = pin
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
func (bt *BtService) DeployBluetoothAgentSettings(src *pb.BluetoothAgentSettings) (res *pb.BluetoothAgentSettings, err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return &pb.BluetoothAgentSettings{},bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
res = &pb.BluetoothAgentSettings{}
|
||||
err = bt.SetPIN(src.Pin)
|
||||
if err != nil { return }
|
||||
return bt.GetBluetoothAgentSettings()
|
||||
}
|
||||
|
||||
|
||||
func (bt *BtService) DeployBluetoothControllerInformation(newBtCiRpc *pb.BluetoothControllerInformation) (updateBtCiRpc *pb.BluetoothControllerInformation, err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return &pb.BluetoothControllerInformation{},bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
|
||||
btCi := bluetooth.BluetoothControllerInformationFromRpc(newBtCiRpc)
|
||||
bridgeNameNap := BT_ETHERNET_BRIDGE_NAME
|
||||
bridgeNamePanu := BT_ETHERNET_BRIDGE_NAME
|
||||
bridgeNameGn := BT_ETHERNET_BRIDGE_NAME
|
||||
|
||||
// Update provided network services if needed
|
||||
if btCi.ServiceNetworkServerNap || btCi.ServiceNetworkServerGn || btCi.ServiceNetworkServerPanu {
|
||||
err = bt.EnableBridge()
|
||||
if err != nil { return &pb.BluetoothControllerInformation{},err }
|
||||
} else {
|
||||
bt.DisableBridge()
|
||||
}
|
||||
|
||||
log.Println("Updating settings from controller information...")
|
||||
updatedCi,err := bt.Controller.UpdateSettingsFromChangedControllerInformation(btCi, bridgeNameNap, bridgeNamePanu, bridgeNameGn)
|
||||
log.Printf("Deployed bluetooth settings\n%+v\n%v\n", updatedCi, err)
|
||||
if err != nil { return &pb.BluetoothControllerInformation{},err }
|
||||
updateBtCiRpc = bluetooth.BluetoothControllerInformationToRpc(updatedCi)
|
||||
return updateBtCiRpc, nil
|
||||
}
|
||||
|
||||
|
||||
func (bt *BtService) GetControllerInformation() (ctlInfo *pb.BluetoothControllerInformation ,err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return &pb.BluetoothControllerInformation{},bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
btCi,err := bt.Controller.ReadControllerInformation()
|
||||
if err != nil { return &pb.BluetoothControllerInformation{},err}
|
||||
btCiRpc := bluetooth.BluetoothControllerInformationToRpc(btCi)
|
||||
btCiRpc.IsAvailable = bt.IsServiceAvailable()
|
||||
return btCiRpc,nil
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// Notes: On Bluetooth settings
|
||||
// P4wnP1 is meant to run headless, which has influence on Pairing mode. There's legacy pairing (outdated and insecure)
|
||||
// which allows requesting a PIN from a remote device which wants to connect. The new Pairing mode is Secure Simple Pairing
|
||||
@@ -197,17 +323,6 @@ func (bt *BtService) StartNAP() (err error) {
|
||||
time.Sleep(time.Second) //give some time before registering NAP to SDP
|
||||
|
||||
// Enable PAN networking for bridge
|
||||
/*
|
||||
nw, err := toolz.NetworkServer(bt.Controller.DBusPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
//defer nw.Close()
|
||||
err = nw.Register(toolz.UUID_NETWORK_SERVER_NAP, BT_ETHERNET_BRIDGE_NAME)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
*/
|
||||
bt.RegisterNetworkServer(toolz.UUID_NETWORK_SERVER_NAP)
|
||||
|
||||
if mi, err := bt.RootSvc.SubSysNetwork.GetManagedInterface(BT_ETHERNET_BRIDGE_NAME); err == nil {
|
||||
@@ -216,6 +331,7 @@ func (bt *BtService) StartNAP() (err error) {
|
||||
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
func (bt *BtService) SetPIN(pin string) (err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
@@ -234,38 +350,65 @@ func (bt *BtService) GetPIN() (pin string, err error) {
|
||||
|
||||
|
||||
func (bt *BtService) RegisterNetworkServer(uuid toolz.NetworkServerUUID) (err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
|
||||
return bt.Controller.RegisterNetworkServer(uuid, BT_ETHERNET_BRIDGE_NAME)
|
||||
}
|
||||
|
||||
func (bt *BtService) UnregisterNetworkServer(uuid toolz.NetworkServerUUID) (err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
return bt.Controller.UnregisterNetworkServer(uuid)
|
||||
}
|
||||
|
||||
|
||||
func (bt *BtService) ConnectNetwork(deviceMac string, uuid toolz.NetworkServerUUID) (err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
return bt.Controller.ConnectNetwork(deviceMac, uuid)
|
||||
}
|
||||
|
||||
func (bt *BtService) DisconnectNetwork(deviceMac string) (err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
return bt.Controller.DisconnectNetwork(deviceMac)
|
||||
}
|
||||
|
||||
func (bt *BtService) IsServerNAPEnabled() (res bool, err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return false,bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
return bt.Controller.IsServerNAPEnabled()
|
||||
}
|
||||
|
||||
func (bt *BtService) IsServerPANUEnabled() (res bool, err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return false,bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
return bt.Controller.IsServerPANUEnabled()
|
||||
}
|
||||
|
||||
func (bt *BtService) IsServerGNEnabled() (res bool, err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return false,bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
return bt.Controller.IsServerGNEnabled()
|
||||
}
|
||||
|
||||
func (bt *BtService) CheckUUIDEnabled(uuids []string) (enabled []bool, err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return []bool{},bluetooth.ErrBtSvcNotAvailable
|
||||
}
|
||||
|
||||
return bt.Controller.CheckUUIDList(uuids)
|
||||
}
|
||||
|
||||
/*
|
||||
func (bt *BtService) StopNAP() (err error) {
|
||||
if !bt.IsServiceAvailable() {
|
||||
return bluetooth.ErrBtSvcNotAvailable
|
||||
@@ -292,45 +435,70 @@ func (bt *BtService) StopNAP() (err error) {
|
||||
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
// ToDo: Lock bridge creation
|
||||
func (bt *BtService) EnableBridge() (err error) {
|
||||
log.Println("Creating bluetooth bridge interface", BT_ETHERNET_BRIDGE_NAME)
|
||||
//Create the bridge
|
||||
err = CreateBridge(bt.BrName)
|
||||
log.Println("Creating bluetooth bridge interface", bt.BrName)
|
||||
exists := CheckInterfaceExistence(bt.BrName)
|
||||
if exists {
|
||||
log.Printf("... interface %s exists alread\n", bt.BrName)
|
||||
} else {
|
||||
log.Printf("... interface %s doesn't exist call bridge create\n", bt.BrName)
|
||||
//Create the bridge
|
||||
err = CreateBridge(bt.BrName)
|
||||
if err != nil {
|
||||
log.Printf("...error in CreateBridge %v\n", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("... set interface MAC %v\n", BT_ETHERNET_BRIDGE_MAC)
|
||||
err = setInterfaceMac(bt.BrName, BT_ETHERNET_BRIDGE_MAC)
|
||||
if err != nil {
|
||||
log.Printf("...error in setInterfaceMac %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = setInterfaceMac(BT_ETHERNET_BRIDGE_NAME, BT_ETHERNET_BRIDGE_MAC)
|
||||
log.Println("... set forward delay to 0")
|
||||
err = SetBridgeForwardDelay(bt.BrName, 0)
|
||||
if err != nil {
|
||||
log.Printf("...error in SetBridgeForwardDelay %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = SetBridgeForwardDelay(BT_ETHERNET_BRIDGE_NAME, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = SetBridgeSTP(BT_ETHERNET_BRIDGE_NAME, false)
|
||||
log.Println("... set spanning tree to off")
|
||||
err = SetBridgeSTP(bt.BrName, false)
|
||||
if err != nil {
|
||||
log.Printf("...error in BridgeSetSTP %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
//enable the bridge
|
||||
err = NetworkLinkUp(BT_ETHERNET_BRIDGE_NAME)
|
||||
log.Println("... bring bridge up")
|
||||
err = NetworkLinkUp(bt.BrName)
|
||||
if err != nil {
|
||||
log.Printf("...error in NetworkLinkUp %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
bt.bridgeIfDeployed = true
|
||||
// Reconfigure network
|
||||
log.Println("... reconfigure ethernet settings for interface", bt.BrName)
|
||||
if mi, err := bt.RootSvc.SubSysNetwork.GetManagedInterface(bt.BrName); err == nil {
|
||||
mi.ReDeploy()
|
||||
}
|
||||
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (bt *BtService) DisableBridge() {
|
||||
log.Println("Deleting bluetooth bridge interface", BT_ETHERNET_BRIDGE_NAME)
|
||||
log.Println("Deleting bluetooth bridge interface", bt.BrName)
|
||||
//we ignore error results and assume bridge is disable after this call (error could be created if bridge if wasn't existent, too)
|
||||
DeleteBridge(BT_ETHERNET_BRIDGE_NAME)
|
||||
bt.bridgeIfDeployed = false
|
||||
exists := CheckInterfaceExistence(bt.BrName)
|
||||
if exists {
|
||||
DeleteBridge(bt.BrName)
|
||||
}
|
||||
}
|
||||
|
||||
// assures bnep kernel module is loaded
|
||||
|
||||
@@ -228,6 +228,14 @@ func (c *Controller) UpdateSettingsFromChangedControllerInformation(newCi *btmgm
|
||||
}
|
||||
|
||||
// Update changeable toggles
|
||||
if currentCi.CurrentSettings.SecureSimplePairing != newCi.CurrentSettings.SecureSimplePairing {
|
||||
err := c.SetSSP(newCi.CurrentSettings.SecureSimplePairing)
|
||||
if err != nil {
|
||||
fmt.Println("Error setting bluetooth SSP")
|
||||
currentCi,_ = c.ReadControllerInformation()
|
||||
return currentCi,err
|
||||
}
|
||||
}
|
||||
|
||||
if currentCi.CurrentSettings.Connectable != newCi.CurrentSettings.Connectable {
|
||||
err := c.SetConnectable(newCi.CurrentSettings.Connectable)
|
||||
@@ -261,14 +269,6 @@ func (c *Controller) UpdateSettingsFromChangedControllerInformation(newCi *btmgm
|
||||
return currentCi,err
|
||||
}
|
||||
}
|
||||
if currentCi.CurrentSettings.SecureSimplePairing != newCi.CurrentSettings.SecureSimplePairing {
|
||||
err := c.SetSSP(newCi.CurrentSettings.SecureSimplePairing)
|
||||
if err != nil {
|
||||
fmt.Println("Error setting bluetooth SSP")
|
||||
currentCi,_ = c.ReadControllerInformation()
|
||||
return currentCi,err
|
||||
}
|
||||
}
|
||||
if currentCi.CurrentSettings.LinkLevelSecurity != newCi.CurrentSettings.LinkLevelSecurity {
|
||||
err := c.SetLinkLevelSecurity(newCi.CurrentSettings.LinkLevelSecurity)
|
||||
if err != nil {
|
||||
@@ -302,8 +302,6 @@ func (c *Controller) UpdateSettingsFromChangedControllerInformation(newCi *btmgm
|
||||
}
|
||||
}
|
||||
|
||||
// Update provided network services if needed
|
||||
|
||||
currentServices,err := c.CheckUUIDList([]string{bt_uuid.NAP_UUID, bt_uuid.PANU_UUID, bt_uuid.GN_UUID})
|
||||
if err != nil { return currentCi, err }
|
||||
if newCi.ServiceNetworkServerNap != currentServices[0] {
|
||||
|
||||
@@ -16,6 +16,29 @@ const (
|
||||
WIFI_ETHERNET_IFACE_NAME = "wlan0"
|
||||
)
|
||||
|
||||
func GetDefaultBluetoothSettings() (*pb.BluetoothSettings) {
|
||||
return &pb.BluetoothSettings{
|
||||
Ci: &pb.BluetoothControllerInformation{
|
||||
ServiceNetworkServerPanu: false,
|
||||
ServiceNetworkServerGn: false,
|
||||
ServiceNetworkServerNap: true,
|
||||
Name: "P4wnP1",
|
||||
CurrentSettings: &pb.BluetoothControllerSettings{
|
||||
Powered: true,
|
||||
Connectable: true,
|
||||
Bondable: true,
|
||||
Discoverable: true,
|
||||
LowEnergy: false,
|
||||
SecureSimplePairing: false, //we start with PIN based auth
|
||||
HighSpeed: false, // not possible with SSP off
|
||||
},
|
||||
},
|
||||
As: &pb.BluetoothAgentSettings{
|
||||
Pin: "1337",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func GetDefaultNetworkSettingsBluetooth() (*pb.EthernetInterfaceSettings) {
|
||||
ifSettings := &pb.EthernetInterfaceSettings{
|
||||
Name: BT_ETHERNET_BRIDGE_NAME,
|
||||
|
||||
@@ -50,7 +50,7 @@ func (nim *NetworkInterfaceManager) StartDHCPClient() (err error) {
|
||||
log.Printf("Starting DHCP client for interface '%s'...\n", nameIface)
|
||||
|
||||
//check if interface is valid
|
||||
if_exists,_ := CheckInterfaceExistence(nameIface)
|
||||
if_exists := CheckInterfaceExistence(nameIface)
|
||||
if !if_exists {
|
||||
return errors.New(fmt.Sprintf("The given interface '%s' doesn't exist", nameIface))
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func (nim *NetworkInterfaceManager) StartDHCPClient() (err error) {
|
||||
|
||||
func (nim *NetworkInterfaceManager) IsDHCPClientRunning() (running bool, pid int, err error) {
|
||||
nameIface := nim.InterfaceName
|
||||
if_exists,_ := CheckInterfaceExistence(nameIface)
|
||||
if_exists := CheckInterfaceExistence(nameIface)
|
||||
if !if_exists {
|
||||
return false, 0, errors.New(fmt.Sprintf("The given interface '%s' doesn't exist", nameIface))
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func (nim *NetworkInterfaceManager) StopDHCPClient() (err error) {
|
||||
log.Printf("Stopping DHCP client for interface '%s'...\n", nameIface)
|
||||
|
||||
//check if interface is valid
|
||||
if_exists,_ := CheckInterfaceExistence(nameIface)
|
||||
if_exists := CheckInterfaceExistence(nameIface)
|
||||
if !if_exists {
|
||||
return errors.New(fmt.Sprintf("The given interface '%s' doesn't exist", nameIface))
|
||||
}
|
||||
@@ -147,7 +147,7 @@ func (nim *NetworkInterfaceManager) StartDHCPServer(configPath string) (err erro
|
||||
log.Printf("Starting dnsmasq for interface '%s' with config '%s'...\n", nameIface, configPath)
|
||||
|
||||
//check if interface is valid
|
||||
if_exists,_ := CheckInterfaceExistence(nameIface)
|
||||
if_exists := CheckInterfaceExistence(nameIface)
|
||||
if !if_exists {
|
||||
return errors.New(fmt.Sprintf("The given interface '%s' doesn't exist", nameIface))
|
||||
}
|
||||
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
"github.com/mame82/P4wnP1_go/common"
|
||||
"github.com/mame82/P4wnP1_go/common_web"
|
||||
pb "github.com/mame82/P4wnP1_go/proto"
|
||||
"github.com/mame82/P4wnP1_go/service/bluetooth"
|
||||
"github.com/mame82/mblue-toolz/toolz"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"io"
|
||||
@@ -233,71 +231,27 @@ func (s *server) ListStoredDBBackups(ctx context.Context, e *pb.Empty) (ma *pb.S
|
||||
|
||||
|
||||
func (s *server) GetBluetoothAgentSettings(ctx context.Context, e *pb.Empty) (as *pb.BluetoothAgentSettings, err error) {
|
||||
as = &pb.BluetoothAgentSettings{}
|
||||
|
||||
pin,err := s.rootSvc.SubSysBluetooth.GetPIN()
|
||||
if err != nil { return as,err }
|
||||
as.Pin = pin
|
||||
return
|
||||
return s.rootSvc.SubSysBluetooth.GetBluetoothAgentSettings()
|
||||
}
|
||||
|
||||
func (s *server) DeployBluetoothAgentSettings(ctx context.Context, src *pb.BluetoothAgentSettings) (res *pb.BluetoothAgentSettings, err error) {
|
||||
err = s.rootSvc.SubSysBluetooth.SetPIN(src.Pin)
|
||||
return s.GetBluetoothAgentSettings(ctx, nil)
|
||||
return s.rootSvc.SubSysBluetooth.DeployBluetoothAgentSettings(src)
|
||||
}
|
||||
|
||||
// Unused, Server services are deployed via BluetoothControllerInformation
|
||||
func (s *server) SetBluetoothNetworkService(ctx context.Context, btNwSvc *pb.BluetoothNetworkService) (e *pb.Empty, err error) {
|
||||
e = &pb.Empty{}
|
||||
uuid := toolz.UUID_NETWORK_SERVER_NAP
|
||||
switch btNwSvc.Type {
|
||||
case pb.BluetoothNetworkServiceType_NAP:
|
||||
uuid = toolz.UUID_NETWORK_SERVER_NAP
|
||||
case pb.BluetoothNetworkServiceType_PANU:
|
||||
uuid = toolz.UUID_NETWORK_SERVER_PANU
|
||||
case pb.BluetoothNetworkServiceType_GN:
|
||||
uuid = toolz.UUID_NETWORK_SERVER_GN
|
||||
}
|
||||
if btNwSvc.ServerOrConnect {
|
||||
// start server for given network service
|
||||
if btNwSvc.RegisterOrUnregister {
|
||||
return e,s.rootSvc.SubSysBluetooth.RegisterNetworkServer(uuid)
|
||||
} else {
|
||||
return e,s.rootSvc.SubSysBluetooth.UnregisterNetworkServer(uuid)
|
||||
}
|
||||
} else {
|
||||
//(dis)connect from/to given network network service of given remote device
|
||||
|
||||
if btNwSvc.RegisterOrUnregister {
|
||||
// register == connect
|
||||
return e,s.rootSvc.SubSysBluetooth.ConnectNetwork(btNwSvc.MacOrName, uuid)
|
||||
} else {
|
||||
// unregister == disconnect
|
||||
return e,s.rootSvc.SubSysBluetooth.DisconnectNetwork(btNwSvc.MacOrName)
|
||||
}
|
||||
}
|
||||
err = s.rootSvc.SubSysBluetooth.DeployBluetoothNetworkService(btNwSvc)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *server) DeployBluetoothControllerInformation(ctx context.Context, newBtCiRpc *pb.BluetoothControllerInformation) (updateBtCiRpc *pb.BluetoothControllerInformation, err error) {
|
||||
btCi := bluetooth.BluetoothControllerInformationFromRpc(newBtCiRpc)
|
||||
bridgeNameNap := BT_ETHERNET_BRIDGE_NAME
|
||||
bridgeNamePanu := BT_ETHERNET_BRIDGE_NAME
|
||||
bridgeNameGn := BT_ETHERNET_BRIDGE_NAME
|
||||
updatedCi,err := s.rootSvc.SubSysBluetooth.Controller.UpdateSettingsFromChangedControllerInformation(btCi, bridgeNameNap, bridgeNamePanu, bridgeNameGn)
|
||||
fmt.Printf("Deployed bluetooth settings\n%+v\n%v\n", updatedCi, err)
|
||||
if err != nil { return &pb.BluetoothControllerInformation{},err }
|
||||
updateBtCiRpc = bluetooth.BluetoothControllerInformationToRpc(updatedCi)
|
||||
return updateBtCiRpc, nil
|
||||
return s.rootSvc.SubSysBluetooth.DeployBluetoothControllerInformation(newBtCiRpc)
|
||||
}
|
||||
|
||||
func (s *server) GetBluetoothControllerInformation(ctx context.Context, e *pb.Empty) (res *pb.BluetoothControllerInformation, err error) {
|
||||
res = &pb.BluetoothControllerInformation{}
|
||||
btCi,err := s.rootSvc.SubSysBluetooth.Controller.ReadControllerInformation()
|
||||
if err != nil { return res,err }
|
||||
btCiRpc := bluetooth.BluetoothControllerInformationToRpc(btCi)
|
||||
btCiRpc.IsAvailable = s.rootSvc.SubSysBluetooth.IsServiceAvailable()
|
||||
|
||||
return btCiRpc, nil
|
||||
return s.rootSvc.SubSysBluetooth.GetControllerInformation()
|
||||
}
|
||||
|
||||
func (s *server) StoreUSBSettings(ctx context.Context, r *pb.USBRequestSettingsStorage) (e *pb.Empty, err error) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/mame82/P4wnP1_go/common_web"
|
||||
pb "github.com/mame82/P4wnP1_go/proto"
|
||||
"github.com/mame82/P4wnP1_go/service/datastore"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -147,26 +148,31 @@ func NewService() (svc *Service, err error) {
|
||||
|
||||
svc.SubSysWifi = NewWifiService(svc) //Depends on NetworkSubSys
|
||||
|
||||
svc.SubSysBluetooth = NewBtService(svc, time.Second * 120) //Depends on NetworkSubSys (try to bring up bluetooth for up to 120s in background)
|
||||
|
||||
svc.SubSysTriggerActions = NewTriggerActionManager(svc) //Depends on EventManager, UsbGadgetManager (to trigger HID scripts)
|
||||
|
||||
svc.SubSysDwc2ConnectWatcher = NewDwc2ConnectWatcher(svc) // Depends on EventManager, should be started before USB gadget settings are deployed (to avoid missing initial state change)
|
||||
|
||||
svc.SubSysBluetooth = NewBtService(svc, time.Second * 120) //Depends on NetworkSubSys (try to bring up bluetooth for up to 120s in background)
|
||||
|
||||
svc.SubSysRPC = NewRpcServerService(svc) //Depends on all other
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Service) Start() {
|
||||
log.Println("Starting service ...")
|
||||
|
||||
s.SubSysEvent.Start()
|
||||
s.SubSysDwc2ConnectWatcher.Start()
|
||||
s.SubSysLed.Start()
|
||||
s.SubSysRPC.StartRpcServerAndWeb("0.0.0.0", "50051", "8000", PATH_WEBROOT) //start gRPC service
|
||||
log.Println("Starting TriggerAction event listener ...")
|
||||
s.SubSysTriggerActions.Start()
|
||||
|
||||
// Register TriggerActions
|
||||
log.Println("Register default TriggerActions ...")
|
||||
RegisterDefaultTriggerActions(s.SubSysTriggerActions)
|
||||
|
||||
/*
|
||||
// ToDo: 1) Manual start of BT NAP, has to be replaced by settings based approach (same as other subsystems)
|
||||
// ToDo: 2) create a signal based method s.SubSysBluetooth.WaitTillServiceUp(timeout duration)
|
||||
go func() {
|
||||
@@ -176,9 +182,10 @@ func (s *Service) Start() {
|
||||
}
|
||||
s.SubSysBluetooth.StartNAP()
|
||||
}()
|
||||
|
||||
*/
|
||||
|
||||
// fire service started Event
|
||||
log.Println("Fire service started event ...")
|
||||
s.SubSysEvent.Emit(ConstructEventTrigger(common_web.TRIGGER_EVT_TYPE_SERVICE_STARTED))
|
||||
}
|
||||
|
||||
@@ -186,7 +193,7 @@ func (s *Service) Stop() {
|
||||
s.SubSysTriggerActions.Stop()
|
||||
s.SubSysLed.Stop()
|
||||
|
||||
s.SubSysBluetooth.StopNAP()
|
||||
s.SubSysBluetooth.Stop()
|
||||
s.SubSysDwc2ConnectWatcher.Stop()
|
||||
s.SubSysEvent.Stop()
|
||||
}
|
||||
|
||||
@@ -449,7 +449,7 @@ func NewWifiService(rootSvc *Service) (res *WiFiService) {
|
||||
}
|
||||
|
||||
//Check interface existence
|
||||
if exists, _ := CheckInterfaceExistence(ifName); !exists {
|
||||
if exists := CheckInterfaceExistence(ifName); !exists {
|
||||
panic(errors.New(fmt.Sprintf("WiFi interface '%s' not present")))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user