Connecting to Devices#
Before interacting with OCA objects, the controller must establish a network connection to the device. libaes70 provides integration points for different networking libraries, so you can choose the one that fits your project.
Controller connections are supported for libuv, boost::asio, and lwIP (for embedded platforms). Choose the integration that fits your project.
An example for establishing a connection can be found in examples/simple_controller:
static void connect()
{
std::cerr << "Trying to connect to device." << std::endl;
libuv::tcp::connect<aes70::controller::connection>("127.0.0.1", 65123,
[&](const char *error,
std::shared_ptr<aes70::controller::connection> con) {
if (!error)
{
std::cerr << "connected." << std::endl;
con->set_keepalive_interval(1000);
connected(con);
subs.add(con->onclose(
[](const aes70::controller::connection &) { closed(); }));
}
else
{
std::cerr << "connection failed: " << error << std::endl;
closed();
}
});
}
int main(int argc, const char **argv)
{
connect();
return uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}
Note
It is generally recommended to use set_keepalive_interval to enable keepalive
messages. Otherwise, the detection of connection loss can be unreliable.
Observing a connection (auto-reconnect)#
For long-running controllers that should stay connected to a device across temporary
disconnects, use aes70::controller::observe_connection.
It establishes a connection using a connect functor (e.g. from your networking
integration’s connector_factory) and, when the connection closes or the initial
connect fails, automatically retries. You supply a callback that runs each time
a connection is established; typically it returns a subscription from
observe_path or
other observers so that when the connection drops, those subscriptions are cleaned
up and reconnect is triggered.
- Header:
aes70/controller/observe_connection.hpp - Connect functor: Use
libuv::tcp::connector_factory,aes70::boost::tcp::connector_factory, orlwip::tcp::connector_factory(each parameterized withaes70::controller::connection). - Return value: A
subscription. Keep it alive for as long as you want the controller to maintain and re-establish the connection; dropping it stops reconnection.
The esp32_mute_controller example (examples/esp32_mute_controller) uses
observe_connection with the lwIP connector factory to connect to a device,
observe a mute at a path, and toggle it on a timer.
Detecting connection loss#
When not using observe_connection, long-running applications that must handle
the loss of connections should use the onclose() method to handle connection
close events and re-create a connection after some time interval.
Note
Note that the subscription returned from onclose() must be kept alive until the connection
is lost. See aes70::controller::subscription
for details.