-
Notifications
You must be signed in to change notification settings - Fork 0
/
obex.go
57 lines (51 loc) · 1.66 KB
/
obex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pitooth
import (
"fmt"
"os/exec"
)
/*
OBEX is a protocol for transferring files between devices over Bluetooth.
To use OBEX, we need to start the obexd service on the Raspberry Pi.
Then we can use [UUID: OBEX Object Push] or [UUID: OBEX File Transfer] to communicate.
*/
// Raspberry Pi as OBEX Server: Configure obexd to accept incoming connections and receive files.
// Sending the files must be handled by the client. Any files received will be saved in the specified directory.
func (btm *bluetoothManager) ControlOBEXServer(start bool, outputPath string) error {
// Check the current status of obexd
pgrepCmd := exec.Command("pgrep", "obexd")
pidBytes, err := pgrepCmd.Output()
isActive := err == nil
// Decide whether to start or stop based on the desired state and current status
if start && isActive {
btm.l.Debugln("obexd is already running.")
return nil
} else if !start && !isActive {
btm.l.Debugln("obexd is already stopped.")
return nil
}
var cmd *exec.Cmd
if start {
// Command to start the obexd service
btm.l.Debugln("Starting obexd service... ")
cmd = exec.Command("obexd", "-a", "-r", outputPath)
} else {
// Command to stop the obexd service
btm.l.Debugln("Stopping obexd service...")
cmd = exec.Command("killall", "obexd")
}
// Execute the command
if err := cmd.Start(); err != nil {
if start {
return fmt.Errorf("failed to start obexd: %v", err)
} else {
return fmt.Errorf("failed to stop obexd: %v", err)
}
} else {
if start {
btm.l.Infof("obexd [PID: %d] started successfully\n", cmd.Process.Pid)
} else {
btm.l.Infof("obexd [PID: %s] stopped successfully\n", string(pidBytes))
}
}
return nil
}