Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
added graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Binozo committed Jul 28, 2023
1 parent da8b8d4 commit 7e9f136
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
7 changes: 6 additions & 1 deletion internal/ble/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const airDropBit = 0x05
func SendAirDropBeacon(appleAccount account.AppleAccount) error {
manufacturerData := appleAccount.BuildManufacturerData()

adv := adapter.DefaultAdvertisement()
adv = adapter.DefaultAdvertisement()
adv.Configure(bluetooth.AdvertisementOptions{
LocalName: device,
ServiceUUIDs: nil,
Expand Down Expand Up @@ -49,3 +49,8 @@ func StartScan(onBeaconReceive chan AirDropBeacon) error {
}
})
}

func Shutdown() error {
adv.Stop()
return adapter.StopScan() // This doesn't work
}
1 change: 1 addition & 0 deletions internal/ble/ble.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ble
import "github.com/Binozo/tinygo-bluetooth"

var adapter = bluetooth.DefaultAdapter
var adv *bluetooth.Advertisement

const device = "hci0"

Expand Down
22 changes: 22 additions & 0 deletions pkg/airdrop/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"os/signal"
"syscall"
"time"
)

Expand Down Expand Up @@ -101,6 +103,7 @@ func Init() error {
if debug {
log.Info().Msg("GoDrop setup successful")
}
go listenForShutdown()
return nil
}

Expand Down Expand Up @@ -171,3 +174,22 @@ func OnAsk(callback func(request air.Request) bool) {
func OnFiles(callback func(request air.Request, files []*cpio.File)) {
interaction.AddFilesCallback(callback)
}

// Shutdown gracefully
func listenForShutdown() {
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM) // all OS signals
sig := <-ch
log.Info().Msgf("[GoDrop] Shutting down... (%v)", sig)
if err := owl.Kill(); err != nil {
log.Error().Err(err).Msg("Couldn't kill Owl")
}

if err := ble.Shutdown(); err != nil {
log.Error().Err(err).Msg("Couldn't BLE scan")
}

awdl.ShutdownService()

//os.Exit(-1)
}
17 changes: 12 additions & 5 deletions pkg/owl/owl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import (

const OwlInterface = "awdl0"

var owlCmd *exec.Cmd

// Setup the Wifi interface and start OWL
func Setup(wifiInterface string) error {
setupWifi(wifiInterface)
return startOwl(wifiInterface)
}

func startOwl(wifiInterface string) error {
cmd := exec.Command("owl", "-i", wifiInterface, "-c", "44") // -c 44 works best
owlCmd = exec.Command("owl", "-i", wifiInterface, "-c", "44") // -c 44 works best

// Owl is somehow only using stderr as main log output
//stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
//stdout, _ := owlCmd.StdoutPipe()
stderr, _ := owlCmd.StderrPipe()
//go listenOnOutput(stdout)
go listenOnOutput(stderr)

err := cmd.Start()
err := owlCmd.Start()
if err != nil {
return err
}
Expand All @@ -34,7 +36,7 @@ func startOwl(wifiInterface string) error {
//log.Info().Msgf("Owl is up and running at %s", GetOwlInterfaceAddr())

go func() {
err = cmd.Wait()
err = owlCmd.Wait()
if err != nil {
// Exited with error
owlTerminated(outLogs)
Expand Down Expand Up @@ -77,3 +79,8 @@ func GetOwlInterfaceAddr() string {
log.Fatal().Msg("Couldn't find Owl interface")
return ""
}

// Kill Owl
func Kill() error {
return owlCmd.Process.Kill()
}

0 comments on commit 7e9f136

Please sign in to comment.