2021-10-15 07:25:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-11 20:24:12 +00:00
|
|
|
"context"
|
2021-11-25 20:41:36 +00:00
|
|
|
"sync"
|
|
|
|
|
2021-11-24 19:36:36 +00:00
|
|
|
"github.com/godbus/dbus/v5"
|
2021-10-15 07:25:34 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.arsenm.dev/infinitime"
|
2022-11-21 19:59:54 +00:00
|
|
|
"go.arsenm.dev/itd/internal/utils"
|
|
|
|
|
2021-10-15 07:25:34 +00:00
|
|
|
)
|
|
|
|
|
2022-05-11 20:24:12 +00:00
|
|
|
func initCallNotifs(ctx context.Context, dev *infinitime.Device) error {
|
2021-11-24 21:04:20 +00:00
|
|
|
// Connect to system bus. This connection is for method calls.
|
2022-11-21 19:59:54 +00:00
|
|
|
conn, err := utils.NewSystemBusConn(ctx)
|
2021-11-24 19:36:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-15 07:25:34 +00:00
|
|
|
|
2021-11-24 21:04:20 +00:00
|
|
|
// Check if modem manager interface exists
|
2022-05-11 20:24:12 +00:00
|
|
|
exists, err := modemManagerExists(ctx, conn)
|
2021-11-24 21:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it does not exist, stop function
|
|
|
|
if !exists {
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to system bus. This connection is for monitoring.
|
2022-11-21 19:59:54 +00:00
|
|
|
monitorConn, err := utils.NewSystemBusConn(ctx)
|
2021-10-15 07:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-24 19:36:36 +00:00
|
|
|
|
2021-11-24 20:00:44 +00:00
|
|
|
// Add match for new calls to monitor connection
|
2021-11-24 19:36:36 +00:00
|
|
|
err = monitorConn.AddMatchSignal(
|
|
|
|
dbus.WithMatchSender("org.freedesktop.ModemManager1"),
|
|
|
|
dbus.WithMatchInterface("org.freedesktop.ModemManager1.Modem.Voice"),
|
|
|
|
dbus.WithMatchMember("CallAdded"),
|
|
|
|
)
|
2021-10-15 07:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-24 20:00:44 +00:00
|
|
|
// Create channel to receive calls
|
|
|
|
callCh := make(chan *dbus.Message, 5)
|
|
|
|
// Notify channel upon received message
|
2021-11-24 19:54:16 +00:00
|
|
|
monitorConn.Eavesdrop(callCh)
|
2021-11-24 20:00:44 +00:00
|
|
|
|
2021-11-25 20:41:36 +00:00
|
|
|
var respHandlerOnce sync.Once
|
|
|
|
var callObj dbus.BusObject
|
|
|
|
|
2021-10-15 07:25:34 +00:00
|
|
|
go func() {
|
2021-11-24 20:00:44 +00:00
|
|
|
// For every message received
|
2021-11-24 19:54:16 +00:00
|
|
|
for event := range callCh {
|
2021-11-24 20:00:44 +00:00
|
|
|
// Get path to call object
|
2021-11-24 19:54:16 +00:00
|
|
|
callPath := event.Body[0].(dbus.ObjectPath)
|
2021-11-24 20:00:44 +00:00
|
|
|
// Get call object
|
2021-11-25 20:41:36 +00:00
|
|
|
callObj = conn.Object("org.freedesktop.ModemManager1", callPath)
|
2021-11-24 19:36:36 +00:00
|
|
|
|
2021-11-24 20:00:44 +00:00
|
|
|
// Get phone number from call object using method call connection
|
2021-11-24 19:36:36 +00:00
|
|
|
phoneNum, err := getPhoneNum(conn, callObj)
|
|
|
|
if err != nil {
|
2021-11-24 21:04:20 +00:00
|
|
|
log.Error().Err(err).Msg("Error getting phone number")
|
|
|
|
continue
|
2021-11-24 19:36:36 +00:00
|
|
|
}
|
2021-10-15 07:25:34 +00:00
|
|
|
|
2021-11-24 20:00:44 +00:00
|
|
|
// Send call notification to InfiniTime
|
2021-11-24 19:36:36 +00:00
|
|
|
resCh, err := dev.NotifyCall(phoneNum)
|
|
|
|
if err != nil {
|
2021-10-15 07:25:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-25 20:41:36 +00:00
|
|
|
go respHandlerOnce.Do(func() {
|
2021-11-24 19:36:36 +00:00
|
|
|
// Wait for PineTime response
|
2021-11-25 20:41:36 +00:00
|
|
|
for res := range resCh {
|
|
|
|
switch res {
|
|
|
|
case infinitime.CallStatusAccepted:
|
|
|
|
// Attempt to accept call
|
2022-05-11 20:24:12 +00:00
|
|
|
err = acceptCall(ctx, conn, callObj)
|
2021-11-25 20:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Error accepting call")
|
|
|
|
}
|
|
|
|
case infinitime.CallStatusDeclined:
|
|
|
|
// Attempt to decline call
|
2022-05-11 20:24:12 +00:00
|
|
|
err = declineCall(ctx, conn, callObj)
|
2021-11-25 20:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Error declining call")
|
|
|
|
}
|
|
|
|
case infinitime.CallStatusMuted:
|
|
|
|
// Warn about unimplemented muting
|
|
|
|
log.Warn().Msg("Muting calls is not implemented")
|
2021-11-24 19:36:36 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-25 20:41:36 +00:00
|
|
|
})
|
2021-10-15 07:25:34 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-11-24 21:04:20 +00:00
|
|
|
|
|
|
|
log.Info().Msg("Relaying calls to InfiniTime")
|
2021-10-15 07:25:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:24:12 +00:00
|
|
|
func modemManagerExists(ctx context.Context, conn *dbus.Conn) (bool, error) {
|
2021-11-24 21:04:20 +00:00
|
|
|
var names []string
|
2022-05-11 20:24:12 +00:00
|
|
|
err := conn.BusObject().CallWithContext(
|
|
|
|
ctx, "org.freedesktop.DBus.ListNames", 0,
|
|
|
|
).Store(&names)
|
2021-11-24 21:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strSlcContains(names, "org.freedesktop.ModemManager1"), nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 20:00:44 +00:00
|
|
|
// getPhoneNum gets a phone number from a call object using a DBus connection
|
2021-11-24 19:36:36 +00:00
|
|
|
func getPhoneNum(conn *dbus.Conn, callObj dbus.BusObject) (string, error) {
|
|
|
|
var out string
|
2021-11-24 20:00:44 +00:00
|
|
|
// Get number property on DBus object and store return value in out
|
2021-11-24 19:36:36 +00:00
|
|
|
err := callObj.StoreProperty("org.freedesktop.ModemManager1.Call.Number", &out)
|
2021-10-15 07:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-11-24 19:36:36 +00:00
|
|
|
return out, nil
|
2021-10-15 07:25:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 20:00:44 +00:00
|
|
|
// getPhoneNum accepts a call using a DBus connection
|
2022-05-11 20:24:12 +00:00
|
|
|
func acceptCall(ctx context.Context, conn *dbus.Conn, callObj dbus.BusObject) error {
|
2021-11-24 20:00:44 +00:00
|
|
|
// Call Accept() method on DBus object
|
2022-05-11 20:24:12 +00:00
|
|
|
call := callObj.CallWithContext(
|
|
|
|
ctx, "org.freedesktop.ModemManager1.Call.Accept", 0,
|
|
|
|
)
|
2021-11-24 19:36:36 +00:00
|
|
|
if call.Err != nil {
|
|
|
|
return call.Err
|
|
|
|
}
|
|
|
|
return nil
|
2021-10-15 07:25:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 20:00:44 +00:00
|
|
|
// getPhoneNum declines a call using a DBus connection
|
2022-05-11 20:24:12 +00:00
|
|
|
func declineCall(ctx context.Context, conn *dbus.Conn, callObj dbus.BusObject) error {
|
2021-11-24 20:00:44 +00:00
|
|
|
// Call Hangup() method on DBus object
|
2022-05-11 20:24:12 +00:00
|
|
|
call := callObj.CallWithContext(
|
|
|
|
ctx, "org.freedesktop.ModemManager1.Call.Hangup", 0,
|
|
|
|
)
|
2021-11-24 19:36:36 +00:00
|
|
|
if call.Err != nil {
|
|
|
|
return call.Err
|
|
|
|
}
|
|
|
|
return nil
|
2021-11-24 20:00:44 +00:00
|
|
|
}
|