1- defmodule NervesHub.RPC.DeviceAuth do
2- @ moduledoc false
1+ defmodule NervesHub.DeviceLink.Connections do
2+ @ moduledoc """
3+ Functions for connecting devices, including the reporting of connection availability.
4+ """
35
46 alias NervesHub.Devices
5- alias NervesHub.Devices.Connections
7+ alias NervesHub.Devices.Connections , as: DeviceConnections
68 alias NervesHub.Devices.DeviceConnection
79 alias NervesHub.Devices.Device
810 alias NervesHub.Products
@@ -13,6 +15,12 @@ defmodule NervesHub.RPC.DeviceAuth do
1315 # Default 90 seconds max age for the signature
1416 @ default_max_hmac_age 90
1517
18+ @ type auth ( ) :: { :ssl_certs , any ( ) } | { :shared_secrets , list ( ) }
19+ @ type connection_id ( ) :: binary ( )
20+
21+ @ spec connect_device ( auth ( ) ) :: { :ok , { connection_id ( ) , Device . t ( ) } } | { :error , :invalid_auth }
22+ def connect_device ( auth )
23+
1624 def connect_device ( { :ssl_certs , ssl_cert } ) do
1725 X509.Certificate . from_der! ( ssl_cert )
1826 |> Devices . get_device_by_x509 ( )
@@ -52,6 +60,9 @@ defmodule NervesHub.RPC.DeviceAuth do
5260 end
5361 end
5462
63+ @ spec disconnect_device ( any ( ) , Device . t ( ) , connection_id ( ) ) :: :ok
64+ def disconnect_device ( reason , device , reference_id )
65+
5566 def disconnect_device ( { :error , { :shutdown , :disconnected } } , device , reference_id ) do
5667 :telemetry . execute ( [ :nerves_hub , :devices , :duplicate_connection ] , % { count: 1 } , % {
5768 ref_id: reference_id ,
@@ -67,13 +78,18 @@ defmodule NervesHub.RPC.DeviceAuth do
6778 identifier: device . identifier
6879 } )
6980
70- { :ok , _device_connection } = Connections . device_disconnected ( reference_id )
81+ { :ok , _device_connection } = DeviceConnections . device_disconnected ( reference_id )
7182
7283 Tracker . offline ( device )
7384
7485 :ok
7586 end
7687
88+ @ spec device_heartbeat ( connection_id ( ) ) :: :ok
89+ def device_heartbeat ( reference_id ) do
90+ DeviceConnections . device_heartbeat ( reference_id )
91+ end
92+
7793 defp on_connect ( % Device { status: :registered } = device ) do
7894 Devices . set_as_provisioned! ( device )
7995 |> on_connect ( )
@@ -89,7 +105,7 @@ defmodule NervesHub.RPC.DeviceAuth do
89105 % { }
90106 )
91107
92- { :ok , % DeviceConnection { id: connection_id } } = Connections . device_connected ( device . id )
108+ { :ok , % DeviceConnection { id: connection_id } } = DeviceConnections . device_connected ( device . id )
93109
94110 :telemetry . execute ( [ :nerves_hub , :devices , :connect ] , % { count: 1 } , % {
95111 ref_id: connection_id ,
0 commit comments