-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ManufacturerData: handle different return values - Apple - fail on non-binary inputs - return binary input on error case - ServiceData: serialization functions
- Loading branch information
1 parent
3a4f861
commit 87b1f1d
Showing
12 changed files
with
169 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Harald.DataType do | ||
@moduledoc """ | ||
Reference: Core Specification Supplement, Part A | ||
""" | ||
|
||
alias Harald.DataType.{ManufacturerData, ServiceData} | ||
|
||
@doc """ | ||
Returns a list of data type implementation modules. | ||
""" | ||
def modules do | ||
[ | ||
ManufacturerData, | ||
ServiceData | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Harald.DataType.ServiceData do | ||
@moduledoc """ | ||
> The Service Data data type consists of a service UUID with the data associated with that | ||
> service. | ||
Reference: Core Specification Supplement, Part A, section 1.11.1 | ||
""" | ||
|
||
alias Harald.Serializable | ||
require Harald.AssignedNumbers.GenericAccessProfile, as: GenericAccessProfile | ||
|
||
@behaviour Serializable | ||
|
||
@description_32 "Service Data - 32-bit UUID" | ||
|
||
@doc """ | ||
Returns the three GAP descriptions encompassed by service data. | ||
""" | ||
def gap_descriptions, do: [@description_32] | ||
|
||
@doc """ | ||
iex> serialize({"Service Data - 32-bit UUID", %{data: <<5, 6>>, uuid: 67305985}}) | ||
{:ok, <<32, 1, 2, 3, 4, 5, 6>>} | ||
""" | ||
@impl Serializable | ||
def serialize({@description_32, %{data: data, uuid: uuid}}) do | ||
binary = << | ||
GenericAccessProfile.id(unquote(@description_32)), | ||
uuid::little-size(32), | ||
data::binary | ||
>> | ||
|
||
{:ok, binary} | ||
end | ||
|
||
def serialize(_), do: :error | ||
|
||
@doc """ | ||
Deserializes a service data binary. | ||
iex> deserialize(<<32, 1, 2, 3, 4, 5, 6>>) | ||
{:ok, {"Service Data - 32-bit UUID", %{data: <<5, 6>>, uuid: 67305985}}} | ||
""" | ||
@impl Serializable | ||
def deserialize(<<GenericAccessProfile.id(unquote(@description_32)), bin::binary>>) do | ||
{status, data} = | ||
case bin do | ||
<<uuid::little-size(32), data::binary>> -> | ||
service_data_32 = %{ | ||
uuid: uuid, | ||
data: data | ||
} | ||
|
||
{:ok, service_data_32} | ||
|
||
_ -> | ||
{:error, bin} | ||
end | ||
|
||
{status, {@description_32, data}} | ||
end | ||
|
||
def deserialize(bin), do: {:error, bin} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Harald.DataType.ManufacturerData.AppleTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
alias Harald.Generators.DataType.ManufacturerData.Apple, as: AppleGen | ||
alias Harald.DataType.ManufacturerData.Apple | ||
|
||
doctest Apple, import: true | ||
|
||
property "symmetric (de)serialization" do | ||
check all bin <- AppleGen.binary() do | ||
case Apple.deserialize(bin) do | ||
{:ok, data} -> assert {:ok, bin} == Apple.serialize(data) | ||
{:error, _} -> :ok | ||
end | ||
end | ||
|
||
check all bin <- StreamData.binary() do | ||
case Apple.deserialize(bin) do | ||
{:ok, data} -> assert {:ok, bin} == Apple.serialize(data) | ||
{:error, _} -> :ok | ||
end | ||
end | ||
end | ||
end |
6 changes: 3 additions & 3 deletions
6
test/harald/manufacturer_data_test.exs → ...rald/data_type/manufacturer_data_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Harald.DataType.ServiceDataTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
alias Harald.Generators.DataType.ServiceData, as: ServiceDataGen | ||
alias Harald.DataType.ServiceData | ||
|
||
doctest ServiceData, import: true | ||
|
||
property "symmetric (de)serialization" do | ||
check all bin <- ServiceDataGen.binary() do | ||
case ServiceData.deserialize(bin) do | ||
{:ok, data} -> assert {:ok, bin} == ServiceData.serialize(data) | ||
{:error, _} -> :ok | ||
end | ||
end | ||
|
||
check all bin <- StreamData.binary() do | ||
case ServiceData.deserialize(bin) do | ||
{:ok, data} -> assert {:ok, bin} == ServiceData.serialize(data) | ||
{:error, _} -> :ok | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
test/support/generators/manufacturer_data.ex → ...generators/data_type/manufacturer_data.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Harald.Generators.DataType.ServiceData do | ||
@moduledoc """ | ||
StreamData generators for service data. | ||
""" | ||
|
||
import ExUnitProperties, only: :macros | ||
alias Harald.DataType.ServiceData | ||
require Harald.AssignedNumbers.GenericAccessProfile, as: GenericAccessProfile | ||
|
||
@description_32 "Service Data - 32-bit UUID" | ||
|
||
def binary do | ||
gen all gap_description <- StreamData.member_of(ServiceData.gap_descriptions()), | ||
bin <- binary(gap_description) do | ||
bin | ||
end | ||
end | ||
|
||
def binary(@description_32) do | ||
gen all uuid <- StreamData.binary(length: 4), | ||
data <- StreamData.binary(length: 1) do | ||
<<GenericAccessProfile.id(unquote(@description_32)), uuid::binary-size(4), data::binary>> | ||
end | ||
end | ||
end |