Astarte.Core.Device (astarte_core v1.0.6) View Source

Utility functions to deal with Astarte devices

Link to this section 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.

Link to this section Types

Specs

device_id() :: <<_::128>>

Specs

encoded_device_id() :: String.t()

Link to this section Functions

Link to this function

decode_device_id(encoded_device_id, opts \\ [])

View Source

Specs

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}.

Link to this function

decode_extended_device_id(encoded_device_id)

View Source

Specs

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}.

Link to this function

encode_device_id(device_id)

View Source

Specs

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.

Specs

random_device_id() :: device_id :: device_id()

Generate a random Astarte device id.

The generated device id is also a valid UUID v4.