2021-08-19 19:41:09 -05:00
|
|
|
package infinitime
|
|
|
|
|
|
|
|
import (
|
2021-12-16 23:30:29 -06:00
|
|
|
"github.com/godbus/dbus/v5"
|
2021-08-19 19:41:09 -05:00
|
|
|
bt "github.com/muka/go-bluetooth/api"
|
|
|
|
"github.com/muka/go-bluetooth/bluez/profile/adapter"
|
2021-12-16 23:30:29 -06:00
|
|
|
"github.com/muka/go-bluetooth/bluez/profile/agent"
|
|
|
|
"github.com/muka/go-bluetooth/hw/linux/btmgmt"
|
2021-08-19 19:41:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var defaultAdapter *adapter.Adapter1
|
2021-12-16 23:30:29 -06:00
|
|
|
var itdAgent *Agent
|
2021-08-19 19:41:09 -05:00
|
|
|
|
2022-05-02 22:14:46 -05:00
|
|
|
func Init(adapterID string) {
|
2021-12-16 23:30:29 -06:00
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ag := &Agent{}
|
|
|
|
err = agent.ExposeAgent(conn, ag, agent.CapKeyboardDisplay, true)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:14:46 -05:00
|
|
|
if adapterID == "" {
|
|
|
|
adapterID = bt.GetDefaultAdapterID()
|
|
|
|
}
|
|
|
|
|
2021-08-19 19:41:09 -05:00
|
|
|
// Get bluez default adapter
|
2022-05-02 22:14:46 -05:00
|
|
|
da, err := bt.GetAdapter(adapterID)
|
2021-08-19 19:41:09 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-09 10:34:56 -05:00
|
|
|
|
2024-03-24 06:31:04 -05:00
|
|
|
//daMgmt := btmgmt.NewBtMgmt(adapterID)
|
|
|
|
//daMgmt.SetPowered(true)
|
2021-09-09 10:34:56 -05:00
|
|
|
|
2021-08-19 19:41:09 -05:00
|
|
|
defaultAdapter = da
|
2021-12-16 23:30:29 -06:00
|
|
|
itdAgent = ag
|
2021-08-19 19:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func Exit() error {
|
2021-11-01 11:19:12 -05:00
|
|
|
if defaultAdapter != nil {
|
|
|
|
defaultAdapter.Close()
|
|
|
|
}
|
2021-12-16 23:30:29 -06:00
|
|
|
agent.RemoveAgent(itdAgent)
|
2021-08-19 19:41:09 -05:00
|
|
|
return bt.Exit()
|
2021-09-09 10:34:56 -05:00
|
|
|
}
|
2021-12-16 23:30:29 -06:00
|
|
|
|
|
|
|
var errAuthFailed = dbus.NewError("org.bluez.Error.AuthenticationFailed", nil)
|
|
|
|
|
|
|
|
// Agent implements the agent.Agent1Client interface.
|
|
|
|
// It only requires RequestPasskey as that is all InfiniTime
|
|
|
|
// will use.
|
|
|
|
type Agent struct {
|
|
|
|
ReqPasskey func() (uint32, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release returns nil
|
|
|
|
func (*Agent) Release() *dbus.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestPinCode returns an empty string and nil
|
|
|
|
func (*Agent) RequestPinCode(device dbus.ObjectPath) (pincode string, err *dbus.Error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisplayPinCode returns nil
|
|
|
|
func (*Agent) DisplayPinCode(device dbus.ObjectPath, pincode string) *dbus.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestPasskey runs Agent.ReqPasskey and returns the result
|
|
|
|
func (a *Agent) RequestPasskey(device dbus.ObjectPath) (uint32, *dbus.Error) {
|
|
|
|
if a.ReqPasskey == nil {
|
|
|
|
return 0, errAuthFailed
|
|
|
|
}
|
2023-01-04 17:00:15 -06:00
|
|
|
log.Debug("Passkey requested, calling onReqPasskey callback").Send()
|
2021-12-16 23:30:29 -06:00
|
|
|
passkey, err := a.ReqPasskey()
|
|
|
|
if err != nil {
|
|
|
|
return 0, errAuthFailed
|
|
|
|
}
|
|
|
|
return passkey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisplayPasskey returns nil
|
|
|
|
func (*Agent) DisplayPasskey(device dbus.ObjectPath, passkey uint32, entered uint16) *dbus.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestConfirmation returns nil
|
|
|
|
func (*Agent) RequestConfirmation(device dbus.ObjectPath, passkey uint32) *dbus.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestAuthorization returns nil
|
|
|
|
func (*Agent) RequestAuthorization(device dbus.ObjectPath) *dbus.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeService returns nil
|
|
|
|
func (*Agent) AuthorizeService(device dbus.ObjectPath, uuid string) *dbus.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel returns nil
|
|
|
|
func (*Agent) Cancel() *dbus.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns "/dev/arsenm/infinitime/Agent"
|
|
|
|
func (*Agent) Path() dbus.ObjectPath {
|
|
|
|
return "/dev/arsenm/infinitime/Agent"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface returns "org.bluez.Agent1"
|
|
|
|
func (*Agent) Interface() string {
|
|
|
|
return "org.bluez.Agent1"
|
|
|
|
}
|