# `Grizzly.ZWave.DSK`
[🔗](https://github.com/smartrent/grizzly/blob/v9.1.4/lib/grizzly/zwave/dsk.ex#L1)

Module for working with the SmartStart and S2 DSKs

# `dsk_binary`

```elixir
@type dsk_binary() :: &lt;&lt;_::128&gt;&gt;
```

The DSK binary is the elixir binary string form of the DSK

The format is `<<b1, b2, b3, ... b16>>`

That is 16 bytes.

An example of this would be:

```elixir
<<196, 109, 73, 131, 38, 196, 119, 227, 62, 101, 131, 175, 15, 165, 14, 39>>
```

# `dsk_string`

```elixir
@type dsk_string() :: &lt;&lt;_::376&gt;&gt;
```

The DSK string is the string version of the DSK

The general format is `XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXX`

That is 8 blocks of 16 bit integers separated by a dash.

An example of this would be `50285-18819-09924-30691-15973-33711-04005-03623`

# `t`

```elixir
@type t() :: %Grizzly.ZWave.DSK{raw: &lt;&lt;_::128&gt;&gt;}
```

# `binary_to_string`

```elixir
@spec binary_to_string(dsk_binary()) :: {:ok, dsk_string()}
```

Take a binary representation of the DSK and change it into the
string representation

# `new`

```elixir
@spec new(binary()) :: t()
```

Make a new DSK

If less than 16 bytes are passed in, the rest are initialized to zero.
Due to how DSKs are constructed, odd length binaries aren't allowed since
they should never be possible.

# `nwi_home_id`

```elixir
@spec nwi_home_id(t()) :: non_neg_integer()
```

Extracts the NWI Home ID (the Home ID used by a SmartStart device when it is
not yet included in a network) from the DSK.

The NWI Home ID is calculated by taking bytes 9-12 of the DSK. Given this value,
the two most significant bits are then set and the least significant bit is cleared.

# `parse`

```elixir
@spec parse(dsk_string()) :: {:ok, t()} | {:error, :invalid_dsk}
```

Parse a textual representation of a DSK

# `parse!`

```elixir
@spec parse!(dsk_string()) :: t() | no_return()
```

Same as `parse/1` but raises an ArgumentError if the DSK is invalid.

# `parse_pin`

```elixir
@spec parse_pin(String.t() | non_neg_integer()) :: {:ok, t()} | {:error, :invalid_dsk}
```

Parse a DSK PIN

PINs can also be parsed by `parse/1`. When working with PINs, though, it's
nice to be more forgiving and accept PINs as integers or strings without
leading zeros.

String examples:

```
iex> {:ok, dsk} = DSK.parse_pin("12345"); dsk
#DSK<12345-00000-00000-00000-00000-00000-00000-00000>

iex> {:ok, dsk} = DSK.parse_pin("123"); dsk
#DSK<00123-00000-00000-00000-00000-00000-00000-00000>
```

Integer examples:

```
iex> {:ok, dsk} = DSK.parse_pin(12345); dsk
#DSK<12345-00000-00000-00000-00000-00000-00000-00000>

iex> {:ok, dsk} = DSK.parse_pin(123); dsk
#DSK<00123-00000-00000-00000-00000-00000-00000-00000>
```

# `random`

```elixir
@spec random() :: t()
```

Generate a random DSK.

# `string_to_binary`

> This function is deprecated. Use DSK.parse/1 instead.

```elixir
@spec string_to_binary(dsk_string()) :: {:ok, dsk_binary()} | {:error, :invalid_dsk}
```

Take a string representation of the DSK and change it into the
binary representation

# `to_pin_string`

```elixir
@spec to_pin_string(t()) :: String.t()
```

Return the first five digits of a DSK for use as a PIN

```
iex> {:ok, dsk} = DSK.parse("50285-18819-09924-30691-15973-33711-04005-03623")
iex> DSK.to_pin_string(dsk)
"50285"

iex> {:ok, dsk} = DSK.parse("00001-18819-09924-30691-15973-33711-04005-03623")
iex> DSK.to_pin_string(dsk)
"00001"
```

# `to_string`

```elixir
@spec to_string(
  t(),
  keyword()
) :: String.t()
```

Convert the DSK to a string

```
iex> {:ok, dsk} = DSK.parse("50285-18819-09924-30691-15973-33711-04005-03623")
iex> DSK.to_string(dsk)
"50285-18819-09924-30691-15973-33711-04005-03623"

iex> {:ok, dsk} = DSK.parse("50285-18819-09924-30691-15973-33711-04005-03623")
iex> DSK.to_string(dsk, delimiter: "")
"5028518819099243069115973337110400503623"
```

Options:

  * `:delimiter` - character to join the 5 byte sections together (default `"-"`)

# `zeros`

```elixir
@spec zeros() :: t()
```

Generate a DSK that is all zeros

This is useful for placeholder/default DSKs.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
