From fbfdfb5b61750801a415e5a3a7ac372cf517d4c0 Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Tue, 24 Oct 2017 20:42:02 +0300 Subject: [PATCH] Initial commit --- .gitignore | 12 +++++ README.md | 29 ++++++++++++ config/config.exs | 32 ++++++++++++++ lib/duck_tag.ex | 3 ++ lib/duck_tag/application.ex | 20 +++++++++ lib/duck_tag/rfid_handler.ex | 6 +++ mix.exs | 86 ++++++++++++++++++++++++++++++++++++ mix.lock.host | 2 + mix.lock.rpi0 | 19 ++++++++ rel/config.exs | 40 +++++++++++++++++ rel/vm.args | 25 +++++++++++ ssh/id_nerves | 27 +++++++++++ ssh/id_nerves.pub | 1 + test/duck_tag_test.exs | 8 ++++ test/test_helper.exs | 1 + 15 files changed, 311 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config/config.exs create mode 100644 lib/duck_tag.ex create mode 100644 lib/duck_tag/application.ex create mode 100644 lib/duck_tag/rfid_handler.ex create mode 100644 mix.exs create mode 100644 mix.lock.host create mode 100644 mix.lock.rpi0 create mode 100644 rel/config.exs create mode 100644 rel/vm.args create mode 100644 ssh/id_nerves create mode 100644 ssh/id_nerves.pub create mode 100644 test/duck_tag_test.exs create mode 100644 test/test_helper.exs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86fa203 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# App artifacts +/_build +/db +/deps +/*.ez + +# Nerves artifacts +/_images +/.nerves + +# Generate on crash by the VM +erl_crash.dump diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee0ceaa --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# DuckTag + +## Targets + +Nerves applications produce images for hardware targets based on the +`MIX_TARGET` environment variable. If `MIX_TARGET` is unset, `mix` builds an +image that runs on the host (e.g., your laptop). This is useful for executing +logic tests, running utilities, and debugging. Other targets are represented by +a short name like `rpi3` that maps to a Nerves system image for that platform. +All of this logic is in the generated `mix.exs` and may be customized. For more +information about targets see: + +https://hexdocs.pm/nerves/targets.html#content + +## Getting Started + +To start your Nerves app: + * `export MIX_TARGET=my_target` or prefix every command with + `MIX_TARGET=my_target`. For example, `MIX_TARGET=rpi3` + * Install dependencies with `mix deps.get` + * Create firmware with `mix firmware` + * Burn to an SD card with `mix firmware.burn` + +## Learn more + + * Official docs: https://hexdocs.pm/nerves/getting-started.html + * Official website: http://www.nerves-project.org/ + * Discussion Slack elixir-lang #nerves ([Invite](https://elixir-slackin.herokuapp.com/)) + * Source: https://github.com/nerves-project/nerves diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..9555ab1 --- /dev/null +++ b/config/config.exs @@ -0,0 +1,32 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +# +# This configuration file is loaded before any dependency and +# is restricted to this project. +use Mix.Config + +# Customize the firmware. Uncomment all or parts of the following +# to add files to the root filesystem or modify the firmware +# archive. + +# config :nerves, :firmware, +# rootfs_overlay: "rootfs_overlay", +# fwup_conf: "config/fwup.conf" + +# Use bootloader to start the main application. See the bootloader +# docs for separating out critical OTP applications such as those +# involved with firmware updates. +config :bootloader, + init: [:nerves_runtime, :nerves_init_gadget], + app: :duck_tag + +config :nerves_firmware_ssh, + authorized_keys: [ + File.read!(Path.join(['ssh', 'id_nerves.pub'])) + ] + +# Import target specific config. This must remain at the bottom +# of this file so it overrides the configuration defined above. +# Uncomment to use target specific configurations + +# import_config "#{Mix.Project.config[:target]}.exs" diff --git a/lib/duck_tag.ex b/lib/duck_tag.ex new file mode 100644 index 0000000..2c00a7d --- /dev/null +++ b/lib/duck_tag.ex @@ -0,0 +1,3 @@ +defmodule DuckTag do + +end diff --git a/lib/duck_tag/application.ex b/lib/duck_tag/application.ex new file mode 100644 index 0000000..652ecbf --- /dev/null +++ b/lib/duck_tag/application.ex @@ -0,0 +1,20 @@ +defmodule DuckTag.Application do + use Application + + # See http://elixir-lang.org/docs/stable/elixir/Application.html + # for more information on OTP Applications + def start(_type, _args) do + import Supervisor.Spec, warn: false + + # Define workers and child supervisors to be supervised + children = [ + # worker(DuckTag.Worker, [arg1, arg2, arg3]), + worker(Nerves.IO.RC522, [{DuckTag.RFIDHandler, :tag_scanned}]) + ] + + # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: DuckTag.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/lib/duck_tag/rfid_handler.ex b/lib/duck_tag/rfid_handler.ex new file mode 100644 index 0000000..338cd43 --- /dev/null +++ b/lib/duck_tag/rfid_handler.ex @@ -0,0 +1,6 @@ +defmodule DuckTag.RFIDHandler do + def tag_scanned(uid) do + IO.puts("TAG SCANNED") + IO.inspect(uid) + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..13fe50a --- /dev/null +++ b/mix.exs @@ -0,0 +1,86 @@ +defmodule DuckTag.Mixfile do + use Mix.Project + + @target System.get_env("MIX_TARGET") || "host" + + Mix.shell.info([:green, """ + Mix environment + MIX_TARGET: #{@target} + MIX_ENV: #{Mix.env} + """, :reset]) + + def project do + [app: :duck_tag, + version: "0.1.0", + elixir: "~> 1.4", + target: @target, + archives: [nerves_bootstrap: "~> 0.6"], + deps_path: "deps/#{@target}", + build_path: "_build/#{@target}", + lockfile: "mix.lock.#{@target}", + build_embedded: Mix.env == :prod, + start_permanent: Mix.env == :prod, + aliases: aliases(@target), + deps: deps()] + end + + # Configuration for the OTP application. + # + # Type `mix help compile.app` for more information. + def application, do: application(@target) + + # Specify target specific application configurations + # It is common that the application start function will start and supervise + # applications which could cause the host to fail. Because of this, we only + # invoke DuckTag.start/2 when running on a target. + def application("host") do + [extra_applications: [:logger]] + end + def application(_target) do + [mod: {DuckTag.Application, []}, + extra_applications: [:logger]] + end + + # Dependencies can be Hex packages: + # + # {:my_dep, "~> 0.3.0"} + # + # Or git/path repositories: + # + # {:my_dep, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + # + # Type "mix help deps" for more examples and options + def deps do + [{:nerves, "~> 0.7", runtime: false}] ++ + deps(@target) + end + + # Specify target specific dependencies + def deps("host"), do: [] + def deps(target) do + [ + {:bootloader, "~> 0.1"}, + {:nerves_runtime, "~> 0.4"}, + {:nerves_init_gadget, "~> 0.2"}, + {:nerves_io_rc522, "~> 0.1.0"} + ] ++ system(target) + end + + def system("rpi"), do: [{:nerves_system_rpi, ">= 0.0.0", runtime: false}] + def system("rpi0"), do: [{:nerves_system_rpi0, ">= 0.0.0", runtime: false}] + def system("rpi2"), do: [{:nerves_system_rpi2, ">= 0.0.0", runtime: false}] + def system("rpi3"), do: [{:nerves_system_rpi3, ">= 0.0.0", runtime: false}] + def system("bbb"), do: [{:nerves_system_bbb, ">= 0.0.0", runtime: false}] + def system("linkit"), do: [{:nerves_system_linkit, ">= 0.0.0", runtime: false}] + def system("ev3"), do: [{:nerves_system_ev3, ">= 0.0.0", runtime: false}] + def system("qemu_arm"), do: [{:nerves_system_qemu_arm, ">= 0.0.0", runtime: false}] + def system(target), do: Mix.raise "Unknown MIX_TARGET: #{target}" + + # We do not invoke the Nerves Env when running on the Host + def aliases("host"), do: [] + def aliases(_target) do + ["deps.precompile": ["nerves.precompile", "deps.precompile"], + "deps.loadpaths": ["deps.loadpaths", "nerves.loadpaths"]] + end + +end diff --git a/mix.lock.host b/mix.lock.host new file mode 100644 index 0000000..ebb0af6 --- /dev/null +++ b/mix.lock.host @@ -0,0 +1,2 @@ +%{"distillery": {:hex, :distillery, "1.5.2", "eec18b2d37b55b0bcb670cf2bcf64228ed38ce8b046bb30a9b636a6f5a4c0080", [], [], "hexpm"}, + "nerves": {:hex, :nerves, "0.7.5", "3aa6a336b2ad6c1c9589cc2b577511b3c4c375c1ba6c533ab9f88adb8c21f0c3", [], [{:distillery, "~> 1.4", [hex: :distillery, repo: "hexpm", optional: false]}], "hexpm"}} diff --git a/mix.lock.rpi0 b/mix.lock.rpi0 new file mode 100644 index 0000000..6e86039 --- /dev/null +++ b/mix.lock.rpi0 @@ -0,0 +1,19 @@ +%{"bootloader": {:hex, :bootloader, "0.1.2", "835ddcf50b796714658f342061d5d48ebc34cbd0d81cdbd5a5a8ae00705d72b1", [], [{:distillery, "~> 1.0", [hex: :distillery, repo: "hexpm", optional: false]}], "hexpm"}, + "distillery": {:hex, :distillery, "1.5.2", "eec18b2d37b55b0bcb670cf2bcf64228ed38ce8b046bb30a9b636a6f5a4c0080", [], [], "hexpm"}, + "dns": {:hex, :dns, "1.0.1", "1d88187fdf564d937cee202949141090707fd0c9d7fcae903a6878ef24ef5d1e", [], [{:socket, "~> 0.3.12", [hex: :socket, repo: "hexpm", optional: false]}], "hexpm"}, + "elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [], [], "hexpm"}, + "mdns": {:hex, :mdns, "0.1.6", "b51b902b15b50e0e1522483c6a5fb073413e3d3d6ef52a44b93a541460b47d29", [], [{:dns, "~> 1.0", [hex: :dns, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves": {:hex, :nerves, "0.7.5", "3aa6a336b2ad6c1c9589cc2b577511b3c4c375c1ba6c533ab9f88adb8c21f0c3", [], [{:distillery, "~> 1.4", [hex: :distillery, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_firmware_ssh": {:hex, :nerves_firmware_ssh, "0.3.1", "e8b1967fa0aff255230be539c68ec868d33884193a385caff957ebad7d6aa8af", [], [{:nerves_runtime, "~> 0.4", [hex: :nerves_runtime, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_init_gadget": {:hex, :nerves_init_gadget, "0.2.1", "20f36dd062fb00e2be8817ddff1b9ced9762877cfe23f6ec1d5936a37e3fc2c8", [], [{:mdns, "~> 0.1", [hex: :mdns, repo: "hexpm", optional: false]}, {:nerves_firmware_ssh, "~> 0.2", [hex: :nerves_firmware_ssh, repo: "hexpm", optional: false]}, {:nerves_network, "~> 0.3", [hex: :nerves_network, repo: "hexpm", optional: false]}, {:nerves_runtime, "~> 0.3", [hex: :nerves_runtime, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_io_rc522": {:hex, :nerves_io_rc522, "0.1.0", "5a84a23ff2ac6e4c9cc0ea2931e43cf06d175b991bfd8b0ef664192cee25b7a4", [], [{:elixir_make, "~> 0.3", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_network": {:hex, :nerves_network, "0.3.4", "c50a36b8263cda2bee18f408287d0f4474f8367702d170864325abbd5d424e4d", [], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:nerves_network_interface, "~> 0.4.0", [hex: :nerves_network_interface, repo: "hexpm", optional: false]}, {:nerves_wpa_supplicant, "~> 0.3.0", [hex: :nerves_wpa_supplicant, repo: "hexpm", optional: false]}, {:system_registry, "~> 0.4", [hex: :system_registry, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_network_interface": {:hex, :nerves_network_interface, "0.4.2", "7a3663a07803f2f9f1e37146714d24ccec1e9349268586e4ed8c41f38641d837", [], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_runtime": {:hex, :nerves_runtime, "0.5.0", "5f4135fe3c94ca5c9e25d677350fbb136f279d14aac4871236961b6f5ccbcfa5", [], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:system_registry, "~> 0.5", [hex: :system_registry, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_system_br": {:hex, :nerves_system_br, "0.14.0", "bff3e310e93c7143525257289cc1ba957ebaeebbaf6e97428d0d01c13f440e49", [], [], "hexpm"}, + "nerves_system_rpi0": {:hex, :nerves_system_rpi0, "0.18.1", "7d24a99b2f456af9d1d99b04618336859344e55954262c920904bc5b9f081508", [], [{:nerves, "~> 0.7", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "~> 0.14.0", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv6_rpi_linux_gnueabi, "~> 0.11.0", [hex: :nerves_toolchain_armv6_rpi_linux_gnueabi, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_toolchain_armv6_rpi_linux_gnueabi": {:hex, :nerves_toolchain_armv6_rpi_linux_gnueabi, "0.11.0", "74ee72f15baffe773e41bae1baeef3942fc8f97fa47d9d1bf9db07c17eca90bd", [], [{:nerves, "~> 0.7", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.1", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm"}, + "nerves_toolchain_ctng": {:hex, :nerves_toolchain_ctng, "1.1.0", "0f03e4a3f3beef5fe271de0148b9f106c417e57f303f635c21c74b4bd6eb68ee", [], [], "hexpm"}, + "nerves_wpa_supplicant": {:hex, :nerves_wpa_supplicant, "0.3.2", "19dc7e1248336e7f542b11b2b857ceb5b088d3eb41a6ca75b7b76628dcf67aad", [], [{:elixir_make, "~> 0.3", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, + "socket": {:hex, :socket, "0.3.12", "4a6543815136503fee67eff0932da1742fad83f84c49130c854114153cc549a6", [], [], "hexpm"}, + "system_registry": {:hex, :system_registry, "0.6.0", "31642177e6002d3cff2ada3553ed4e9c0a6ca015797d62d7d17c0ab8696185fc", [], [], "hexpm"}} diff --git a/rel/config.exs b/rel/config.exs new file mode 100644 index 0000000..293e920 --- /dev/null +++ b/rel/config.exs @@ -0,0 +1,40 @@ +use Mix.Releases.Config, + # This sets the default release built by `mix release` + default_release: :default, + # This sets the default environment used by `mix release` + default_environment: :dev + +# For a full list of config options for both releases +# and environments, visit https://hexdocs.pm/distillery/configuration.html + + +# You may define one or more environments in this file, +# an environment's settings will override those of a release +# when building in that environment, this combination of release +# and environment configuration is called a profile + +environment :dev do + set cookie: :"JNP[nh_b9$D%]1P{_P2?()9*mv`Z1:E18T.w;kRLZX`_$E;es@1~3k7Upmg*IhH3" +end + +environment :prod do + set cookie: :"JNP[nh_b9$D%]1P{_P2?()9*mv`Z1:E18T.w;kRLZX`_$E;es@1~3k7Upmg*IhH3" +end + +# You may define one or more releases in this file. +# If you have not set a default release, or selected one +# when running `mix release`, the first release in the file +# will be used by default + +release :duck_tag do + set version: current_version(:duck_tag) + plugin Bootloader.Plugin + if System.get_env("NERVES_SYSTEM") do + set dev_mode: false + set include_src: false + set include_erts: System.get_env("ERL_LIB_DIR") + set include_system_libs: System.get_env("ERL_SYSTEM_LIB_DIR") + set vm_args: "rel/vm.args" + end +end + diff --git a/rel/vm.args b/rel/vm.args new file mode 100644 index 0000000..43ab938 --- /dev/null +++ b/rel/vm.args @@ -0,0 +1,25 @@ +## Add custom options here + +## Distributed Erlang Options +## The cookie needs to be configured prior to vm boot for +## for read only filesystem. + +# -name duck_tag@0.0.0.0 +-setcookie QzZpjmW58COOicHqloZZKRdTN688yjYr6XL8Klbcr6cCT0br3dlKjVeABjGp5nOr + +## Use Ctrl-C to interrupt the current shell rather than invoking the emulator's +## break handler and possibly exiting the VM. ++Bc + +## Save the shell history between reboots +## See http://erlang.org/doc/man/kernel_app.html for additional options +-kernel shell_history enabled + +## Start the Elixir shell + +-noshell +-user Elixir.IEx.CLI + +## Options added after -extra are interpreted as plain arguments and +## can be retrieved using :init.get_plain_arguments(). +-extra --no-halt diff --git a/ssh/id_nerves b/ssh/id_nerves new file mode 100644 index 0000000..0bf6b5c --- /dev/null +++ b/ssh/id_nerves @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpQIBAAKCAQEAusMwKRvBtC39nz5qKC3AeeF9iwYWGNZ/Oj8EfyWdcbji4wCv +3x3UvxcGAGY307yVNGHRdC8WRUXutpzr5ry/pgs6w8q9P/jUx+MZlRXDJ6/XOeqh +95yrV45AdlP6Ke6kP/Gthy9G8pgVjCkwxi8w30EC4FAtvUzaIVWvT9vrflgg93zB +9wfnLXM535G8UArz4B+t1+ciWqg/gxF5C9WSvg/hI7dONW0yFpc27oboVOnM+HaH +wx0eI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Scc/BKKCdHs3bGxOxdyVU ++nHGQzGhXqRcQudLMrtT28mNbQVKg0mscYMuBB2iM2ZlaP2avzxy8kZmx5H5028g +PUk9dvcCgYEAgsdiL/IjgaCitgypZfsP01IrMOvt3iVwk4syg7Yz58OiDhrcLt8/ +gxcdBAmBW705UyhNQPMsReb7kjOJ8rKq/+Ps/YGveExp8e1AcB1c4N1ub7P7hDcj +4cZXdvtqt+L5yKDXaZHwL5utMc6NWFqCjGYK8uH1sz+tDLR651LsVRQ= +-----END RSA PRIVATE KEY----- diff --git a/ssh/id_nerves.pub b/ssh/id_nerves.pub new file mode 100644 index 0000000..fae00e6 --- /dev/null +++ b/ssh/id_nerves.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6wzApG8G0Lf2fPmooLcB54X2LBhYY1n86PwR/JZ1xuOLjAK/fHdS/FwYAZjfTvJU0YdF0LxZFRe62nOvmvL+mCzrDyr0/+NTH4xmVFcMnr9c56qH3nKtXjkB2U/op7qQ/8a2HL0bymBWMKTDGLzDfQQLgUC29TNohVa9P2+t+WCD3fMH3B+ctcznfkbxQCvPgH63X5yJaqD+DEXkL1ZK+D+Ejt041bTIWlzbuhuhU6cz4dofDHR4jkhD8S0H3kQmdZvnDAkbLTcz2odUSP+54GRk/pZ0Kq4Mrzs5PuBpYhZwOj05tJWNHIsZcYTNfuG4QzGHxx7JKJKA7bPXW9cUJ diff --git a/test/duck_tag_test.exs b/test/duck_tag_test.exs new file mode 100644 index 0000000..c1a7ea6 --- /dev/null +++ b/test/duck_tag_test.exs @@ -0,0 +1,8 @@ +defmodule DuckTagTest do + use ExUnit.Case + doctest DuckTag + + test "the truth" do + assert 1 + 1 == 2 + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..4b8b246 --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start