├── .gitignore ├── LICENSE ├── README.md ├── adapter.go ├── agent.go ├── device.go ├── examples └── main.go ├── go.mod ├── go.sum ├── iwd.go ├── known_network.go ├── network.go └── station.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | iwd-menu 15 | 16 | # Dependency directories (remove the comment below to include it) 17 | # vendor/ 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Christian Rebischke 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # iwd 2 | 3 | Go dbus API bindings for the Internet Wireless Daemon "iwd". 4 | -------------------------------------------------------------------------------- /adapter.go: -------------------------------------------------------------------------------- 1 | package iwd 2 | 3 | import "github.com/godbus/dbus/v5" 4 | 5 | const ( 6 | objectAdapter = "net.connman.iwd.Adapter" 7 | ) 8 | 9 | // Adapter refers to the iwd network adapter like "phy/0" for example: /net/connman/iwd/0 10 | type Adapter struct { 11 | Path dbus.ObjectPath 12 | Model string 13 | Name string 14 | Powered bool 15 | SupportedModes []string 16 | Vendor string 17 | } 18 | -------------------------------------------------------------------------------- /agent.go: -------------------------------------------------------------------------------- 1 | package iwd 2 | 3 | import "github.com/godbus/dbus/v5" 4 | 5 | type Agent struct { 6 | Path dbus.ObjectPath 7 | } 8 | -------------------------------------------------------------------------------- /device.go: -------------------------------------------------------------------------------- 1 | package iwd 2 | 3 | import "github.com/godbus/dbus/v5" 4 | 5 | const ( 6 | objectDevice = "net.connman.iwd.Device" 7 | ) 8 | 9 | // Device refers to the iwd network device like "wlan0" for example: /net/connman/iwd/0/4 10 | type Device struct { 11 | Path dbus.ObjectPath 12 | Adapter dbus.ObjectPath 13 | Address string 14 | Mode string 15 | Name string 16 | Powered bool 17 | } 18 | -------------------------------------------------------------------------------- /examples/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/godbus/dbus/v5" 6 | "github.com/shibumi/iwd" 7 | ) 8 | 9 | // This little example shows the network name of the connected wifi network. 10 | func main() { 11 | conn, err := dbus.SystemBus() 12 | if err != nil { 13 | panic(err) 14 | } 15 | defer conn.Close() 16 | 17 | iwdClient := iwd.New(conn) 18 | // lookup connected network 19 | var networkPath dbus.ObjectPath 20 | for _, station := range iwdClient.Stations { 21 | if station.State == "connected" { 22 | networkPath = station.ConnectedNetwork 23 | break 24 | } 25 | } 26 | for _, network := range iwdClient.Networks { 27 | if network.Path == networkPath { 28 | fmt.Println(network.Name) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/shibumi/iwd 2 | 3 | go 1.18 4 | 5 | require github.com/godbus/dbus/v5 v5.1.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME= 2 | github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= 3 | github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= 4 | github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= 5 | -------------------------------------------------------------------------------- /iwd.go: -------------------------------------------------------------------------------- 1 | package iwd 2 | 3 | import ( 4 | "github.com/godbus/dbus/v5" 5 | ) 6 | 7 | const ( 8 | objectIwd = "net.connman.iwd" 9 | objectIwdPath = "/net/connman/iwd" 10 | iwdAgentManager = "net.connman.iwd.AgentManager" 11 | iwdAdapter = "net.connman.iwd.Adapter" 12 | iwdDevice = "net.connman.iwd.Device" 13 | iwdSimpleConfiguration = "net.connman.iwd.SimpleConfiguation" 14 | iwdNetwork = "net.connman.iwd.Network" 15 | ) 16 | 17 | // Iwd is a struct over all major iwd components 18 | type Iwd struct { 19 | Agents []Agent 20 | Adapters []Adapter 21 | KnownNetworks []KnownNetwork 22 | Networks []Network 23 | Stations []Station 24 | Devices []Device 25 | } 26 | 27 | // New parses the net.connman.iwd object index and initializes an iwd object 28 | func New(conn *dbus.Conn) Iwd { 29 | var objects map[dbus.ObjectPath]map[string]map[string]dbus.Variant 30 | objectManager := conn.Object(objectIwd, "/") 31 | objectManager.Call("org.freedesktop.DBus.ObjectManager.GetManagedObjects", 0).Store(&objects) 32 | i := Iwd{ 33 | make([]Agent, 0), 34 | make([]Adapter, 0), 35 | make([]KnownNetwork, 0), 36 | make([]Network, 0), 37 | make([]Station, 0), 38 | make([]Device, 0), 39 | } 40 | for k, v := range objects { 41 | for resource, obj := range v { 42 | switch resource { 43 | case objectAdapter: 44 | i.Adapters = append(i.Adapters, Adapter{ 45 | Path: k, 46 | Model: obj["Model"].Value().(string), Name: obj["Name"].Value().(string), 47 | Powered: obj["Powered"].Value().(bool), SupportedModes: obj["SupportedModes"].Value().([]string), 48 | Vendor: obj["Vendor"].Value().(string), 49 | }) 50 | case objectKnownNetwork: 51 | i.KnownNetworks = append(i.KnownNetworks, KnownNetwork{ 52 | Path: k, 53 | AutoConnect: obj["AutoConnect"].Value().(bool), Hidden: obj["Hidden"].Value().(bool), 54 | LastConnectedTime: obj["LastConnectedTime"].Value().(string), Name: obj["Name"].Value().(string), 55 | Type: obj["Type"].Value().(string), 56 | }) 57 | case objectNetwork: 58 | i.Networks = append(i.Networks, Network{ 59 | Path: k, 60 | Connected: obj["Connected"].Value().(bool), Device: obj["Device"].Value().(dbus.ObjectPath), 61 | Name: obj["Name"].Value().(string), Type: obj["Type"].Value().(string), 62 | }) 63 | case objectStation: 64 | i.Stations = append(i.Stations, Station{ 65 | Path: k, 66 | ConnectedNetwork: obj["ConnectedNetwork"].Value().(dbus.ObjectPath), Scanning: obj["Scanning"].Value().(bool), 67 | State: obj["State"].Value().(string), 68 | }) 69 | case objectDevice: 70 | i.Devices = append(i.Devices, Device{ 71 | Path: k, 72 | Adapter: obj["Adapter"].Value().(dbus.ObjectPath), Address: obj["Address"].Value().(string), 73 | Mode: obj["Mode"].Value().(string), Name: obj["Name"].Value().(string), Powered: obj["Powered"].Value().(bool), 74 | }) 75 | } 76 | } 77 | } 78 | return i 79 | } 80 | -------------------------------------------------------------------------------- /known_network.go: -------------------------------------------------------------------------------- 1 | package iwd 2 | 3 | import "github.com/godbus/dbus/v5" 4 | 5 | const ( 6 | objectKnownNetwork = "net.connman.iwd.KnownNetwork" 7 | ) 8 | 9 | // KnownNetwork refers to the net.connman.iwd.KnownNetwork object 10 | type KnownNetwork struct { 11 | Path dbus.ObjectPath 12 | AutoConnect bool 13 | Hidden bool 14 | LastConnectedTime string 15 | Name string 16 | Type string 17 | } 18 | -------------------------------------------------------------------------------- /network.go: -------------------------------------------------------------------------------- 1 | package iwd 2 | 3 | import ( 4 | dbus "github.com/godbus/dbus/v5" 5 | "strconv" 6 | ) 7 | 8 | const ( 9 | objectNetwork = "net.connman.iwd.Network" 10 | callNetworkConnect = "net.connman.iwd.Network.Connect" 11 | ) 12 | 13 | // Network refers to the iwd network for example: /net/connman/iwd/0/4/7a65696b7561697a65696b756169646577616e67_psk 14 | type Network struct { 15 | Path dbus.ObjectPath 16 | Connected bool 17 | Device dbus.ObjectPath 18 | KnownNetwork dbus.ObjectPath 19 | Name string 20 | Type string 21 | } 22 | 23 | // Connect establishes a connection with a network 24 | // Currently this only works for open networks 25 | func (n *Network) Connect(conn *dbus.Conn) error { 26 | // path = /net/connman/iwd///_ 27 | device := conn.Object(objectIwd, n.Path) 28 | call := device.Call(callNetworkConnect, 0) 29 | if call.Err != nil { 30 | return call.Err 31 | } 32 | return nil 33 | } 34 | 35 | func GetNetwork(conn *dbus.Conn, objectPath dbus.ObjectPath) (*Network, error) { 36 | obj := conn.Object(objectNetwork, objectPath) 37 | name, err := obj.GetProperty("Name") 38 | if err != nil { 39 | return nil, err 40 | } 41 | networkType, err := obj.GetProperty("Type") 42 | if err != nil { 43 | return nil, err 44 | } 45 | connected, err := obj.GetProperty("Connected") 46 | if err != nil { 47 | return nil, err 48 | } 49 | connectedBool, err := strconv.ParseBool(connected.String()) 50 | if err != nil { 51 | return nil, err 52 | } 53 | return &Network{ 54 | Path: objectPath, 55 | Connected: connectedBool, 56 | Device: "", 57 | KnownNetwork: "", 58 | Name: name.String(), 59 | Type: networkType.String(), 60 | }, nil 61 | } 62 | -------------------------------------------------------------------------------- /station.go: -------------------------------------------------------------------------------- 1 | package iwd 2 | 3 | import "github.com/godbus/dbus/v5" 4 | 5 | const ( 6 | objectStation = "net.connman.iwd.Station" 7 | callStationScan = "net.connman.iwd.Station.Scan" 8 | ) 9 | 10 | // Station refers to net.connman.iwd.Station 11 | type Station struct { 12 | Path dbus.ObjectPath 13 | ConnectedNetwork dbus.ObjectPath 14 | Scanning bool 15 | State string 16 | } 17 | 18 | // Scan scans for wireless networks 19 | func (s *Station) Scan(conn *dbus.Conn) error { 20 | obj := conn.Object(objectStation, "") 21 | call := obj.Call(callStationScan, 0) 22 | if call.Err != nil { 23 | return call.Err 24 | } 25 | return nil 26 | } 27 | --------------------------------------------------------------------------------