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

Commit 7e9f136

Browse files
committed
added graceful shutdown
1 parent da8b8d4 commit 7e9f136

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

internal/ble/beacon.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const airDropBit = 0x05
1414
func SendAirDropBeacon(appleAccount account.AppleAccount) error {
1515
manufacturerData := appleAccount.BuildManufacturerData()
1616

17-
adv := adapter.DefaultAdvertisement()
17+
adv = adapter.DefaultAdvertisement()
1818
adv.Configure(bluetooth.AdvertisementOptions{
1919
LocalName: device,
2020
ServiceUUIDs: nil,
@@ -49,3 +49,8 @@ func StartScan(onBeaconReceive chan AirDropBeacon) error {
4949
}
5050
})
5151
}
52+
53+
func Shutdown() error {
54+
adv.Stop()
55+
return adapter.StopScan() // This doesn't work
56+
}

internal/ble/ble.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ble
33
import "github.com/Binozo/tinygo-bluetooth"
44

55
var adapter = bluetooth.DefaultAdapter
6+
var adv *bluetooth.Advertisement
67

78
const device = "hci0"
89

pkg/airdrop/api.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/rs/zerolog"
1515
"github.com/rs/zerolog/log"
1616
"os"
17+
"os/signal"
18+
"syscall"
1719
"time"
1820
)
1921

@@ -101,6 +103,7 @@ func Init() error {
101103
if debug {
102104
log.Info().Msg("GoDrop setup successful")
103105
}
106+
go listenForShutdown()
104107
return nil
105108
}
106109

@@ -171,3 +174,22 @@ func OnAsk(callback func(request air.Request) bool) {
171174
func OnFiles(callback func(request air.Request, files []*cpio.File)) {
172175
interaction.AddFilesCallback(callback)
173176
}
177+
178+
// Shutdown gracefully
179+
func listenForShutdown() {
180+
ch := make(chan os.Signal)
181+
signal.Notify(ch, os.Interrupt, syscall.SIGTERM) // all OS signals
182+
sig := <-ch
183+
log.Info().Msgf("[GoDrop] Shutting down... (%v)", sig)
184+
if err := owl.Kill(); err != nil {
185+
log.Error().Err(err).Msg("Couldn't kill Owl")
186+
}
187+
188+
if err := ble.Shutdown(); err != nil {
189+
log.Error().Err(err).Msg("Couldn't BLE scan")
190+
}
191+
192+
awdl.ShutdownService()
193+
194+
//os.Exit(-1)
195+
}

pkg/owl/owl.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@ import (
99

1010
const OwlInterface = "awdl0"
1111

12+
var owlCmd *exec.Cmd
13+
1214
// Setup the Wifi interface and start OWL
1315
func Setup(wifiInterface string) error {
1416
setupWifi(wifiInterface)
1517
return startOwl(wifiInterface)
1618
}
1719

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

2123
// Owl is somehow only using stderr as main log output
22-
//stdout, _ := cmd.StdoutPipe()
23-
stderr, _ := cmd.StderrPipe()
24+
//stdout, _ := owlCmd.StdoutPipe()
25+
stderr, _ := owlCmd.StderrPipe()
2426
//go listenOnOutput(stdout)
2527
go listenOnOutput(stderr)
2628

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

3638
go func() {
37-
err = cmd.Wait()
39+
err = owlCmd.Wait()
3840
if err != nil {
3941
// Exited with error
4042
owlTerminated(outLogs)
@@ -77,3 +79,8 @@ func GetOwlInterfaceAddr() string {
7779
log.Fatal().Msg("Couldn't find Owl interface")
7880
return ""
7981
}
82+
83+
// Kill Owl
84+
func Kill() error {
85+
return owlCmd.Process.Kill()
86+
}

0 commit comments

Comments
 (0)