From 7a772a54589309cb12a8d65afaf6c4d327956bd1 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Wed, 24 Nov 2021 12:00:44 -0800 Subject: [PATCH] Add comments --- calls.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/calls.go b/calls.go index 0467481..6f4018a 100644 --- a/calls.go +++ b/calls.go @@ -7,17 +7,19 @@ import ( ) func initCallNotifs(dev *infinitime.Device) error { - // Connect to dbus session monitorConn + // Connect to system bus. This connection is for monitoring. monitorConn, err := newSystemBusConn() if err != nil { return err } + // Connect to system bus. This connection is for method calls. conn, err := newSystemBusConn() if err != nil { return err } + // Add match for new calls to monitor connection err = monitorConn.AddMatchSignal( dbus.WithMatchSender("org.freedesktop.ModemManager1"), dbus.WithMatchInterface("org.freedesktop.ModemManager1.Modem.Voice"), @@ -27,18 +29,26 @@ func initCallNotifs(dev *infinitime.Device) error { return err } - callCh := make(chan *dbus.Message, 10) + // Create channel to receive calls + callCh := make(chan *dbus.Message, 5) + // Notify channel upon received message monitorConn.Eavesdrop(callCh) + go func() { + // For every message received for event := range callCh { + // Get path to call object callPath := event.Body[0].(dbus.ObjectPath) + // Get call object callObj := conn.Object("org.freedesktop.ModemManager1", callPath) + // Get phone number from call object using method call connection phoneNum, err := getPhoneNum(conn, callObj) if err != nil { log.Fatal().Err(err).Send() } + // Send call notification to InfiniTime resCh, err := dev.NotifyCall(phoneNum) if err != nil { continue @@ -70,8 +80,10 @@ func initCallNotifs(dev *infinitime.Device) error { return nil } +// getPhoneNum gets a phone number from a call object using a DBus connection func getPhoneNum(conn *dbus.Conn, callObj dbus.BusObject) (string, error) { var out string + // Get number property on DBus object and store return value in out err := callObj.StoreProperty("org.freedesktop.ModemManager1.Call.Number", &out) if err != nil { return "", err @@ -79,7 +91,9 @@ func getPhoneNum(conn *dbus.Conn, callObj dbus.BusObject) (string, error) { return out, nil } +// getPhoneNum accepts a call using a DBus connection func acceptCall(conn *dbus.Conn, callObj dbus.BusObject) error { + // Call Accept() method on DBus object call := callObj.Call("org.freedesktop.ModemManager1.Call.Accept", 0) if call.Err != nil { return call.Err @@ -87,10 +101,12 @@ func acceptCall(conn *dbus.Conn, callObj dbus.BusObject) error { return nil } +// getPhoneNum declines a call using a DBus connection func declineCall(conn *dbus.Conn, callObj dbus.BusObject) error { + // Call Hangup() method on DBus object call := callObj.Call("org.freedesktop.ModemManager1.Call.Hangup", 0) if call.Err != nil { return call.Err } return nil -} \ No newline at end of file +}