This is the description of the Go API bindings for the IP Connection. The IP Connection manages the communication between the API bindings and the Brick Daemon or a WIFI/Ethernet Extension. Before Bricks and Bricklets can be controlled using their API an IP Connection has to be created and its TCP/IP connection has to be established.
An installation guide for the Go API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (example_enumerate.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 | package main
import (
"fmt"
"github.com/Tinkerforge/go-api-bindings/ipconnection"
)
const ADDR string = "localhost:4223"
func main() {
ipcon := ipconnection.New()
defer ipcon.Close()
ipcon.Connect(ADDR) // Connect to brickd.
defer ipcon.Disconnect()
// Don't use device before ipcon is connected.
ipcon.RegisterEnumerateCallback(func(uid string, connectedUid string, position rune, hardwareVersion [3]uint8,
firmwareVersion [3]uint8, deviceIdentifier uint16, enumerationType ipconnection.EnumerationType) {
fmt.Printf("UID: %s\n", uid)
switch enumerationType {
case ipconnection.EnumerationTypeAvailable:
fmt.Printf("Enumeration Type: Available\n")
case ipconnection.EnumerationTypeConnected:
fmt.Printf("Enumeration Type: Connected\n")
case ipconnection.EnumerationTypeDisconnected:
fmt.Printf("Enumeration Type: Disconnected\n")
return
}
fmt.Printf("Connected UID: %s\n", connectedUid)
fmt.Printf("Position: %c\n", position)
fmt.Printf("Hardware Version: %d.%d.%d\n", hardwareVersion[0], hardwareVersion[1], hardwareVersion[2])
fmt.Printf("Firmware Version: %d.%d.%d\n", firmwareVersion[0], firmwareVersion[1], firmwareVersion[2])
fmt.Printf("Device Identifier: %d\n", deviceIdentifier)
fmt.Println("")
})
ipcon.Enumerate()
fmt.Println("Press enter to exit.")
fmt.Scanln()
}
|
Download (example_authenticate.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 58 59 60 61 62 63 | package main
import (
"fmt"
"github.com/Tinkerforge/go-api-bindings/ipconnection"
)
const ADDR string = "localhost:4223"
const SECRET string = "My Authentication Secret!"
func main() {
ipcon := ipconnection.New()
defer ipcon.Close()
// Disable auto reconnect mechanism, in case we have the wrong secret. If the authentication is successful, reenable it.
ipcon.SetAutoReconnect(false)
ipcon.RegisterConnectCallback(func(reason ipconnection.ConnectReason) {
if reason == ipconnection.ConnectReasonRequest {
fmt.Println("Connected by request")
} else if reason == ipconnection.ConnectReasonAutoReconnect {
fmt.Println("Auto-Reconnected")
}
// Authenticate first...
err := ipcon.Authenticate(SECRET)
if err != nil {
fmt.Println("Could not authenticate:", err)
return
}
fmt.Println("Authentication succeded")
ipcon.SetAutoReconnect(true) // ...reenable auto reconnect mechanism, as described above,
ipcon.Enumerate() // then trigger enumerate.
})
ipcon.RegisterEnumerateCallback(func(uid string, connectedUid string, position rune, hardwareVersion [3]uint8,
firmwareVersion [3]uint8, deviceIdentifier uint16, enumerationType ipconnection.EnumerationType) {
fmt.Printf("UID: %s\n", uid)
switch enumerationType {
case ipconnection.EnumerationTypeAvailable:
fmt.Printf("Enumeration Type: Available\n")
case ipconnection.EnumerationTypeConnected:
fmt.Printf("Enumeration Type: Connected\n")
case ipconnection.EnumerationTypeDisconnected:
fmt.Printf("Enumeration Type: Disconnected\n")
return
}
fmt.Printf("Connected UID: %s\n", connectedUid)
fmt.Printf("Position: %c\n", position)
fmt.Printf("Hardware Version: %d.%d.%d\n", hardwareVersion[0], hardwareVersion[1], hardwareVersion[2])
fmt.Printf("Firmware Version: %d.%d.%d\n", firmwareVersion[0], firmwareVersion[1], firmwareVersion[2])
fmt.Printf("Device Identifier: %d\n", deviceIdentifier)
fmt.Println("")
})
ipcon.Connect(ADDR) // Connect to brickd.
defer ipcon.Disconnect()
fmt.Println("Press enter to exit.")
fmt.Scanln()
}
|
The IPConnection is defined in the package github.com/Tinkerforge/go-api-bindings/ipconnection.
Creates an IP Connection instance that can be used to enumerate the available devices. It is also required for the constructor of Bricks and Bricklets.
Destroys this IP Connection and stops it's internal goroutines.
Creates a TCP/IP connection to the given addr with the form "host:port", as described here . The host and port can refer to a Brick Daemon or to a WIFI/Ethernet Extension.
Devices can only be controlled when the connection was established successfully.
Disconnects the TCP/IP connection from the Brick Daemon or the WIFI/Ethernet Extension.
Performs an authentication handshake with the connected Brick Daemon or WIFI/Ethernet Extension. If the handshake succeeds the connection switches from non-authenticated to authenticated state and communication can continue as normal. If the handshake fails then the connection gets closed. Authentication can fail if the wrong secret was used or if authentication is not enabled at all on the Brick Daemon or the WIFI/Ethernet Extension.
See the authentication tutorial for more information.
Can return the following states:
Enables or disables auto-reconnect. If auto-reconnect is enabled, the IP Connection will try to reconnect to the previously given address, if the currently existing connection is lost. Therefore, auto-reconnect only does something after a successful Connect() call.
Default value is true.
Returns true if auto-reconnect is enabled, false otherwise.
Sets the timeout for getters and for setters for which the response expected flag is activated.
Default timeout is 2500 ms.
Returns the timeout as set by SetTimeout().
Broadcasts an enumerate request. All devices will respond with an enumerate callback.
Callbacks can be registered to be notified about events. The registration is done with Register*Callback() functions of the IPConnection object. For example:
registrationId := ipcon.RegisterExampleCallback(func(param type) {
fmt.Println(param)
});
The available events are described below. It is possible to add several callbacks and to remove them with the corresponding Deregister*Callback() function, which expects a registration ID returned by Register*Callback().
The callback receives seven parameters:
Possible enumeration types are:
It should be possible to implement plug-and-play functionality with this (as is done in Brick Viewer).
The device identifier numbers can be found here. There are also constants for these numbers named following this pattern:
<device-package>.DeviceIdentifier
For example: master_brick.DeviceIdentifier or ambient_light_bricklet.DeviceIdentifier.
This event is triggered whenever the IP Connection got connected to a Brick Daemon or to a WIFI/Ethernet Extension, possible reasons are:
This event is triggered whenever the IP Connection got disconnected from a Brick Daemon or to a WIFI/Ethernet Extension, possible reasons are: