View Source Astarte.Core.Device (astarte_core v1.2.0)
Utility functions to deal with Astarte devices
Summary
Functions
Decodes a Base64 url encoded device id and returns it as a 128-bit binary (usable as uuid).
Decodes an extended Base64 url encoded device id.
Encodes a device id with the standard encoding (Base64 url encoding, no padding). The device id must be exactly 16 bytes (128 bits) long.
Generate a random Astarte device id.
Types
@type device_id() :: <<_::128>>
@type encoded_device_id() :: String.t()
Functions
@spec decode_device_id(encoded_device_id :: encoded_device_id(), opts :: options) :: {:ok, device_id :: device_id()} | {:error, atom()} when options: [option], option: {:allow_extended_id, boolean()}
Decodes a Base64 url encoded device id and returns it as a 128-bit binary (usable as uuid).
By default, it will fail with {:error, :extended_id_not_allowed}
if the size of the encoded device_id is > 128 bit.
You can pass allow_extended_id: true
as second argument to allow longer device ids (the returned binary will still be 128 bit long, but the function will not return an error and will instead drop the extended id).
Returns {:ok, device_id}
or {:error, reason}
.
@spec decode_extended_device_id(encoded_device_id :: encoded_device_id()) :: {:ok, device_id :: device_id(), extended_id :: binary()} | {:error, atom()}
Decodes an extended Base64 url encoded device id.
Returns {:ok, device_id, extended_id}
(where device_id
is a binary with the first 128 bits of the decoded id and extended_id
the rest of the decoded binary) or {:error, reason}
.
@spec encode_device_id(device_id :: device_id()) :: encoded_device_id :: encoded_device_id()
Encodes a device id with the standard encoding (Base64 url encoding, no padding). The device id must be exactly 16 bytes (128 bits) long.
Returns the encoded device id.
@spec random_device_id() :: device_id :: device_id()
Generate a random Astarte device id.
The generated device id is also a valid UUID v4.