├── example ├── test │ ├── test_helper.exs │ └── example_test.exs ├── config │ ├── host.exs │ ├── config.exs │ └── target.exs ├── assets │ └── cdev_example_rpi0.png ├── .formatter.exs ├── .gitignore ├── rootfs_overlay │ └── etc │ │ └── iex.exs ├── lib │ ├── example.ex │ └── example │ │ └── application.ex ├── rel │ └── vm.args.eex ├── README.md ├── mix.exs └── mix.lock ├── test ├── circuits_cdev_test.exs └── test_helper.exs ├── CHANGELOG.md ├── .circleci ├── md-style.rb └── config.yml ├── .formatter.exs ├── .github └── dependabot.yml ├── lib ├── cdev │ ├── line_handle.ex │ ├── application.ex │ ├── events │ │ └── event.ex │ ├── line_info.ex │ ├── events.ex │ └── nif.ex └── cdev.ex ├── src ├── enif_gpio_common.h ├── cdev_nif.h ├── enif_gpio_common.c ├── gpio_chip.h ├── gpio_chip.c └── cdev_nif.c ├── .gitignore ├── mix.exs ├── mix.lock ├── Makefile ├── README.md └── LICENSE /example/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /test/circuits_cdev_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.Test do 2 | end 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v0.1.0 4 | 5 | Initial release to hex. 6 | -------------------------------------------------------------------------------- /.circleci/md-style.rb: -------------------------------------------------------------------------------- 1 | all 2 | exclude_rule 'MD026' 3 | rule 'MD029', :style => 'ordered' 4 | -------------------------------------------------------------------------------- /example/config/host.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | # Add configuration that is only needed when running on the host here. 4 | -------------------------------------------------------------------------------- /.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /example/assets/cdev_example_rpi0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elixir-circuits/circuits_cdev/HEAD/example/assets/cdev_example_rpi0.png -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: mix 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 -------------------------------------------------------------------------------- /example/.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: [ 4 | "{mix,.formatter}.exs", 5 | "{config,lib,test}/**/*.{ex,exs}", 6 | "rootfs_overlay/etc/iex.exs" 7 | ] 8 | ] 9 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | # Always warning as errors 2 | if Version.match?(System.version(), "~> 1.10") do 3 | Code.put_compiler_option(:warnings_as_errors, true) 4 | end 5 | 6 | ExUnit.start() 7 | -------------------------------------------------------------------------------- /example/test/example_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExampleTest do 2 | use ExUnit.Case 3 | doctest Example 4 | 5 | test "greets the world" do 6 | assert Example.hello() == :world 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /lib/cdev/line_handle.ex: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.LineHandle do 2 | @moduledoc """ 3 | A handle to read and set values to GPIO line(s) 4 | """ 5 | 6 | alias Circuits.Cdev 7 | 8 | @type t() :: %__MODULE__{ 9 | chip: Cdev.chip(), 10 | handle: reference() 11 | } 12 | 13 | defstruct chip: nil, handle: nil 14 | end 15 | -------------------------------------------------------------------------------- /lib/cdev/application.ex: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.Application do 2 | @moduledoc false 3 | 4 | use Application 5 | 6 | def start(_type, _args) do 7 | children = [ 8 | Circuits.Cdev.Events 9 | ] 10 | 11 | opts = [strategy: :rest_for_one, name: Circuits.Cdev.Supervisor] 12 | Supervisor.start_link(children, opts) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /src/enif_gpio_common.h: -------------------------------------------------------------------------------- 1 | #ifndef ENIF_GPIO_COMMON_H 2 | #define ENIF_GPIO_COMMON_H 3 | 4 | #include "erl_nif.h" 5 | 6 | int common_make_int_array_from_list(ErlNifEnv *env, ERL_NIF_TERM list, int length, int buff[]); 7 | 8 | ERL_NIF_TERM common_make_and_release_resource(ErlNifEnv *env, void *obj); 9 | 10 | ERL_NIF_TERM common_make_error(ErlNifEnv *env, ERL_NIF_TERM term); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/cdev_nif.h: -------------------------------------------------------------------------------- 1 | #ifndef CDEV_NIF_H 2 | #define CDEV_NIF_H 3 | 4 | #include "erl_nif.h" 5 | 6 | typedef struct { 7 | ErlNifResourceType *gpio_chip_rt; 8 | ErlNifResourceType *gpio_chip_line_handle_rt; 9 | ErlNifResourceType *gpio_chip_event_handle_rt; 10 | ErlNifResourceType *gpio_chip_event_data_rt; 11 | 12 | ERL_NIF_TERM atom_ok; 13 | ERL_NIF_TERM atom_error; 14 | } chip_priv_t; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /lib/cdev/events/event.ex: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.Events.Event do 2 | @moduledoc false 3 | 4 | alias Circuits.Cdev 5 | 6 | @type t() :: %__MODULE__{ 7 | handle: reference(), 8 | listener: pid(), 9 | offset: Cdev.offset(), 10 | chip: Cdev.chip(), 11 | last_event: non_neg_integer() 12 | } 13 | 14 | defstruct handle: nil, listener: nil, offset: nil, chip: nil, last_event: 0 15 | end 16 | -------------------------------------------------------------------------------- /lib/cdev/line_info.ex: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.LineInfo do 2 | @moduledoc """ 3 | Line information 4 | """ 5 | 6 | alias Circuits.Cdev 7 | 8 | @type t() :: %__MODULE__{ 9 | offset: Cdev.offset(), 10 | name: String.t(), 11 | consumer: String.t(), 12 | active_low: boolean(), 13 | direction: Cdev.line_direction() 14 | } 15 | 16 | defstruct offset: nil, name: nil, consumer: nil, active_low: nil, direction: nil 17 | end 18 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where third-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | /config/dev.exs -------------------------------------------------------------------------------- /example/rootfs_overlay/etc/iex.exs: -------------------------------------------------------------------------------- 1 | IO.puts(""" 2 | \e[34m████▄▖ \e[36m▐███ 3 | \e[34m█▌ ▀▜█▙▄▖ \e[36m▐█ 4 | \e[34m█▌ \e[36m▐█▄▖\e[34m▝▀█▌ \e[36m▐█ \e[39mN E R V E S 5 | \e[34m█▌ \e[36m▝▀█▙▄▖ ▐█ 6 | \e[34m███▌ \e[36m▀▜████\e[0m 7 | """) 8 | 9 | # Add Toolshed helpers to the IEx session 10 | use Toolshed 11 | 12 | if RingLogger in Application.get_env(:logger, :backends, []) do 13 | IO.puts(""" 14 | RingLogger is collecting log messages from Elixir and Linux. To see the 15 | messages, either attach the current IEx session to the logger: 16 | 17 | RingLogger.attach 18 | 19 | or print the next messages in the log: 20 | 21 | RingLogger.next 22 | """) 23 | end 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where third-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | circuits_cdev-*.tar 24 | 25 | /priv/ 26 | -------------------------------------------------------------------------------- /src/enif_gpio_common.c: -------------------------------------------------------------------------------- 1 | #include "enif_gpio_common.h" 2 | #include "cdev_nif.h" 3 | 4 | int common_make_int_array_from_list(ErlNifEnv *env, ERL_NIF_TERM list, int length, int buff[]) 5 | { 6 | ERL_NIF_TERM head; 7 | ERL_NIF_TERM tail; 8 | 9 | for (int i = 0; i < length; i++) { 10 | int value; 11 | 12 | enif_get_list_cell(env, list, &head, &tail); 13 | 14 | if (!enif_get_int(env, head, &value)) 15 | return -1; 16 | 17 | buff[i] = value; 18 | 19 | list = tail; 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | ERL_NIF_TERM common_make_and_release_resource(ErlNifEnv *env, void *obj) 26 | { 27 | ERL_NIF_TERM resource = enif_make_resource(env, obj); 28 | enif_release_resource(obj); 29 | 30 | return resource; 31 | } 32 | 33 | ERL_NIF_TERM common_make_error(ErlNifEnv *env, ERL_NIF_TERM error) 34 | { 35 | chip_priv_t *priv = enif_priv_data(env); 36 | 37 | return enif_make_tuple2(env, priv->atom_error, error); 38 | } 39 | -------------------------------------------------------------------------------- /example/lib/example.ex: -------------------------------------------------------------------------------- 1 | defmodule Example do 2 | @moduledoc false 3 | 4 | use GenServer 5 | 6 | alias Circuits.Cdev 7 | 8 | def start_link(args) do 9 | GenServer.start_link(__MODULE__, args) 10 | end 11 | 12 | @impl GenServer 13 | def init(args) do 14 | chip = Keyword.get(args, :chip, "gpiochip0") 15 | led1 = Keyword.get(args, :led1, 17) 16 | led2 = Keyword.get(args, :led2, 22) 17 | button = Keyword.get(args, :button, 27) 18 | 19 | {:ok, line} = Cdev.request_lines(chip, [led1, led2], :output) 20 | 21 | # Set initial values led1 will be on and led2 will be off 22 | :ok = Cdev.set_values(line, [1, 0]) 23 | 24 | :ok = Cdev.listen_event("gpiochip0", button) 25 | 26 | {:ok, line} 27 | end 28 | 29 | @impl GenServer 30 | def handle_info({:circuits_cdev, 27, _timestamp, 1}, line) do 31 | Cdev.set_values(line, [0, 1]) 32 | 33 | {:noreply, line} 34 | end 35 | 36 | def handle_info({:circuits_cdev, 27, _timestamp, 0}, line) do 37 | Cdev.set_values(line, [1, 0]) 38 | 39 | {:noreply, line} 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /example/lib/example/application.ex: -------------------------------------------------------------------------------- 1 | defmodule Example.Application do 2 | # See https://hexdocs.pm/elixir/Application.html 3 | # for more information on OTP Applications 4 | @moduledoc false 5 | 6 | use Application 7 | 8 | def start(_type, _args) do 9 | # See https://hexdocs.pm/elixir/Supervisor.html 10 | # for other strategies and supported options 11 | opts = [strategy: :one_for_one, name: Example.Supervisor] 12 | 13 | children = 14 | [ 15 | # Children for all targets 16 | # Starts a worker by calling: Example.Worker.start_link(arg) 17 | # {Example.Worker, arg}, 18 | ] ++ children(target()) 19 | 20 | Supervisor.start_link(children, opts) 21 | end 22 | 23 | # List all child processes to be supervised 24 | def children(:host) do 25 | [ 26 | # Children that only run on the host 27 | # Starts a worker by calling: Example.Worker.start_link(arg) 28 | # {Example.Worker, arg}, 29 | ] 30 | end 31 | 32 | def children(_target) do 33 | example_args = Application.get_env(:example, :config, []) 34 | 35 | [ 36 | {Example, example_args} 37 | ] 38 | end 39 | 40 | def target() do 41 | Application.get_env(:example, :target) 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /example/config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | # 4 | # This configuration file is loaded before any dependency and 5 | # is restricted to this project. 6 | import Config 7 | 8 | # Enable the Nerves integration with Mix 9 | Application.start(:nerves_bootstrap) 10 | 11 | config :example, target: Mix.target() 12 | 13 | # Customize non-Elixir parts of the firmware. See 14 | # https://hexdocs.pm/nerves/advanced-configuration.html for details. 15 | 16 | config :nerves, :firmware, rootfs_overlay: "rootfs_overlay" 17 | 18 | # Set the SOURCE_DATE_EPOCH date for reproducible builds. 19 | # See https://reproducible-builds.org/docs/source-date-epoch/ for more information 20 | 21 | config :nerves, source_date_epoch: "1615393459" 22 | 23 | # Use Ringlogger as the logger backend and remove :console. 24 | # See https://hexdocs.pm/ring_logger/readme.html for more information on 25 | # configuring ring_logger. 26 | 27 | config :logger, backends: [RingLogger] 28 | 29 | if Mix.target() == :host or Mix.target() == :"" do 30 | import_config "host.exs" 31 | else 32 | import_config "target.exs" 33 | end 34 | 35 | # Import a dev config if available 36 | if Mix.env() == :dev and File.exists?("config/dev.exs") do 37 | import_config "dev.exs" 38 | end 39 | -------------------------------------------------------------------------------- /example/rel/vm.args.eex: -------------------------------------------------------------------------------- 1 | ## Add custom options here 2 | 3 | ## Distributed Erlang Options 4 | ## The cookie needs to be configured prior to vm boot for 5 | ## for read only filesystem. 6 | 7 | -setcookie <%= @release.options[:cookie] %> 8 | 9 | ## Use Ctrl-C to interrupt the current shell rather than invoking the emulator's 10 | ## break handler and possibly exiting the VM. 11 | +Bc 12 | 13 | # Allow time warps so that the Erlang system time can more closely match the 14 | # OS system time. 15 | +C multi_time_warp 16 | 17 | ## Load code at system startup 18 | ## See http://erlang.org/doc/system_principles/system_principles.html#code-loading-strategy 19 | -mode embedded 20 | 21 | ## Disable scheduler busy wait to reduce idle CPU usage and avoid delaying 22 | ## other OS processes. See http://erlang.org/doc/man/erl.html#+sbwt 23 | +sbwt none 24 | +sbwtdcpu none 25 | +sbwtdio none 26 | 27 | ## Save the shell history between reboots 28 | ## See http://erlang.org/doc/man/kernel_app.html for additional options 29 | -kernel shell_history enabled 30 | 31 | ## Enable heartbeat monitoring of the Erlang runtime system 32 | -heart -env HEART_BEAT_TIMEOUT 30 33 | 34 | ## Start the Elixir shell 35 | 36 | -noshell 37 | -user Elixir.IEx.CLI 38 | 39 | ## Enable colors in the shell 40 | -elixir ansi_enabled true 41 | 42 | ## Options added after -extra are interpreted as plain arguments and can be 43 | ## retrieved using :init.get_plain_arguments(). Options before the "--" are 44 | ## interpreted by Elixir and anything afterwards is left around for other IEx 45 | ## and user applications. 46 | -extra --no-halt 47 | -- 48 | --dot-iex /etc/iex.exs 49 | 50 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | Example of driving GPIOs via the character device driver. 4 | 5 | This example provides switching between two LEDs via a button. When the button 6 | is not press `:led1` will be active and when the button is pressed `:led2` will 7 | be active. 8 | 9 | By default this will try to use `gpiochip0` for the GPIO device. `:led1` will be 10 | set to line `17` and `:led2` will be set to line `22`. The button events will be 11 | sent to line `27`. This defaults should work for Raspberry PI based systems. 12 | However, if you need to configure the system you can in the `target.exs` file 13 | like: 14 | 15 | ```elixir 16 | config :example, 17 | config: [ 18 | chip: , 19 | led1: 20 | led2: , 21 | button: 22 | ] 23 | ``` 24 | 25 | ![Circuit Example rpi0](assets/cdev_example_rpi0.png) 26 | 27 | ## Targets 28 | 29 | Nerves applications produce images for hardware targets based on the 30 | `MIX_TARGET` environment variable. If `MIX_TARGET` is unset, `mix` builds an 31 | image that runs on the host (e.g., your laptop). This is useful for executing 32 | logic tests, running utilities, and debugging. Other targets are represented by 33 | a short name like `rpi3` that maps to a Nerves system image for that platform. 34 | All of this logic is in the generated `mix.exs` and may be customized. For more 35 | information about targets see: 36 | 37 | https://hexdocs.pm/nerves/targets.html#content 38 | 39 | ## Getting Started 40 | 41 | To start your Nerves app: 42 | * `export MIX_TARGET=my_target` or prefix every command with 43 | `MIX_TARGET=my_target`. For example, `MIX_TARGET=rpi3` 44 | * Install dependencies with `mix deps.get` 45 | * Create firmware with `mix firmware` 46 | * Burn to an SD card with `mix firmware.burn` 47 | 48 | ## Learn more 49 | 50 | * Official docs: https://hexdocs.pm/nerves/getting-started.html 51 | * Official website: https://nerves-project.org/ 52 | * Forum: https://elixirforum.com/c/nerves-forum 53 | * Discussion Slack elixir-lang #nerves ([Invite](https://elixir-slackin.herokuapp.com/)) 54 | * Source: https://github.com/nerves-project/nerves 55 | -------------------------------------------------------------------------------- /lib/cdev/events.ex: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.Events do 2 | @moduledoc false 3 | 4 | use GenServer 5 | 6 | alias Circuits.Cdev 7 | alias Circuits.Cdev.Nif 8 | alias Circuits.Cdev.Events.Event 9 | 10 | def start_link(args) do 11 | GenServer.start_link(__MODULE__, args, name: __MODULE__) 12 | end 13 | 14 | @doc """ 15 | Listen to line events on the offset 16 | """ 17 | @spec listen_event(Cdev.chip(), Cdev.offset()) :: :ok 18 | def listen_event(chip, offset) do 19 | GenServer.call(__MODULE__, {:listen_event, chip, offset, self()}) 20 | end 21 | 22 | @impl GenServer 23 | def init(_args) do 24 | {:ok, %{}} 25 | end 26 | 27 | @impl GenServer 28 | def handle_call({:listen_event, chip, offset, listener}, _from, state) do 29 | {:ok, event_handle} = Nif.request_event_nif(chip.reference, offset) 30 | {:ok, event_data} = Nif.make_event_data_nif(event_handle) 31 | 32 | event_ref = make_ref() 33 | 34 | :ok = Nif.listen_event_nif(event_handle, event_data, event_ref) 35 | 36 | event = %Event{ 37 | handle: event_handle, 38 | listener: listener, 39 | offset: offset, 40 | chip: chip 41 | } 42 | 43 | state = Map.put(state, event_ref, event) 44 | 45 | {:reply, :ok, Map.put(state, event_ref, event)} 46 | end 47 | 48 | @impl GenServer 49 | def handle_info({:select, event_data_ref, event_ref, :ready_input}, state) do 50 | %Event{ 51 | handle: event_handle, 52 | listener: listener, 53 | last_event: last_event, 54 | offset: offset 55 | } = event = Map.fetch!(state, event_ref) 56 | 57 | {:ok, event_value, timestamp} = Nif.read_event_data_nif(event_handle, event_data_ref) 58 | 59 | if event_value != last_event do 60 | send( 61 | listener, 62 | {:circuits_cdev, offset, timestamp, event_value_to_offset_value(event_value)} 63 | ) 64 | end 65 | 66 | :ok = Nif.listen_event_nif(event_handle, event_data_ref, event_ref) 67 | 68 | event = %Event{event | last_event: event_value} 69 | 70 | {:noreply, Map.update!(state, event_ref, fn _ -> event end)} 71 | end 72 | 73 | defp event_value_to_offset_value(1), do: 1 74 | defp event_value_to_offset_value(2), do: 0 75 | end 76 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.MixProject do 2 | use Mix.Project 3 | 4 | @version "0.1.0" 5 | @source_url "https://github.com/elixir-circuits/circuits_cdev" 6 | 7 | def project do 8 | [ 9 | app: :circuits_cdev, 10 | version: @version, 11 | elixir: "~> 1.9", 12 | description: description(), 13 | package: package(), 14 | source_url: @source_url, 15 | compilers: [:elixir_make | Mix.compilers()], 16 | make_targets: ["all"], 17 | make_clean: ["clean"], 18 | docs: docs(), 19 | aliases: [format: [&format_c/1, "format"]], 20 | start_permanent: Mix.env() == :prod, 21 | build_embedded: true, 22 | dialyzer: [ 23 | flags: [:unmatched_returns, :error_handling, :race_conditions, :underspecs] 24 | ], 25 | deps: deps(), 26 | preferred_cli_env: %{docs: :docs, "hex.publish": :docs, "hex.build": :docs} 27 | ] 28 | end 29 | 30 | def application do 31 | [ 32 | extra_applications: [:logger], 33 | mod: {Circuits.Cdev.Application, []} 34 | ] 35 | end 36 | 37 | defp description do 38 | "Drive GPIOs using the Linux cdev interface" 39 | end 40 | 41 | defp package do 42 | %{ 43 | files: [ 44 | "lib", 45 | "src/*.[ch]", 46 | "mix.exs", 47 | "README.md", 48 | "LICENSE", 49 | "CHANGELOG.md", 50 | "Makefile" 51 | ], 52 | licenses: ["Apache-2.0"], 53 | links: %{"GitHub" => @source_url} 54 | } 55 | end 56 | 57 | defp deps do 58 | [ 59 | {:elixir_make, "~> 0.6", runtime: false}, 60 | {:ex_doc, "~> 0.28.0", only: :docs, runtime: false}, 61 | {:dialyxir, "~> 1.2.0", only: :dev, runtime: false} 62 | ] 63 | end 64 | 65 | defp docs do 66 | [ 67 | extras: ["README.md"], 68 | main: "readme", 69 | source_ref: "v#{@version}", 70 | source_url: @source_url 71 | ] 72 | end 73 | 74 | defp format_c([]) do 75 | case System.find_executable("astyle") do 76 | nil -> 77 | Mix.Shell.IO.info("Install astyle to format C code.") 78 | 79 | astyle -> 80 | System.cmd(astyle, ["-n", "src/*.c"], into: IO.stream(:stdio, :line)) 81 | end 82 | end 83 | 84 | defp format_c(_args), do: true 85 | end 86 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | defaults: &defaults 4 | working_directory: ~/repo 5 | environment: 6 | LC_ALL: C.UTF-8 7 | 8 | install_hex_rebar: &install_hex_rebar 9 | run: 10 | name: Install hex and rebar 11 | command: | 12 | mix local.hex --force 13 | mix local.rebar --force 14 | install_system_deps: &install_system_deps 15 | run: 16 | name: Install system dependencies 17 | command: | 18 | apk add build-base linux-headers 19 | jobs: 20 | build_elixir_1_12_otp_24: 21 | docker: 22 | - image: hexpm/elixir:1.12.0-rc.1-erlang-24.0-alpine-3.13.3 23 | <<: *defaults 24 | steps: 25 | - checkout 26 | - <<: *install_hex_rebar 27 | - <<: *install_system_deps 28 | - restore_cache: 29 | keys: 30 | - v1-mix-cache-{{ checksum "mix.lock" }} 31 | - run: mix deps.get 32 | - run: mix test 33 | - run: mix format --check-formatted 34 | - run: mix deps.unlock --check-unused 35 | - run: mix docs 36 | - run: mix hex.build 37 | - run: mix dialyzer 38 | - save_cache: 39 | key: v1-mix-cache-{{ checksum "mix.lock" }} 40 | paths: 41 | - _build 42 | - deps 43 | 44 | build_elixir_1_11_otp_23: 45 | docker: 46 | - image: hexpm/elixir:1.11.3-erlang-23.2.2-alpine-3.12.1 47 | <<: *defaults 48 | steps: 49 | - checkout 50 | - <<: *install_hex_rebar 51 | - <<: *install_system_deps 52 | - run: mix deps.get 53 | - run: mix test 54 | 55 | build_elixir_1_10_otp_23: 56 | docker: 57 | - image: hexpm/elixir:1.10.4-erlang-23.1.2-alpine-3.12.1 58 | <<: *defaults 59 | steps: 60 | - checkout 61 | - <<: *install_hex_rebar 62 | - <<: *install_system_deps 63 | - run: mix deps.get 64 | - run: mix test 65 | 66 | build_elixir_1_9_otp_22: 67 | docker: 68 | - image: hexpm/elixir:1.9.4-erlang-22.3.4.9-alpine-3.12.0 69 | <<: *defaults 70 | steps: 71 | - checkout 72 | - <<: *install_hex_rebar 73 | - <<: *install_system_deps 74 | - run: mix deps.get 75 | - run: mix test 76 | 77 | workflows: 78 | version: 2 79 | build_test: 80 | jobs: 81 | - build_elixir_1_12_otp_24 82 | - build_elixir_1_11_otp_23 83 | - build_elixir_1_10_otp_23 84 | - build_elixir_1_9_otp_22 85 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"}, 3 | "earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"}, 4 | "elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"}, 5 | "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, 6 | "ex_doc": {:hex, :ex_doc, "0.28.5", "3e52a6d2130ce74d096859e477b97080c156d0926701c13870a4e1f752363279", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d2c4b07133113e9aa3e9ba27efb9088ba900e9e51caa383919676afdf09ab181"}, 7 | "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"}, 8 | "makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"}, 9 | "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, 10 | "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, 11 | } 12 | -------------------------------------------------------------------------------- /example/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Example.MixProject do 2 | use Mix.Project 3 | 4 | @app :example 5 | @version "0.1.0" 6 | @all_targets [:rpi, :rpi0, :rpi2, :rpi3, :rpi3a, :rpi4, :bbb, :osd32mp1, :x86_64] 7 | 8 | def project do 9 | [ 10 | app: @app, 11 | version: @version, 12 | elixir: "~> 1.9", 13 | archives: [nerves_bootstrap: "~> 1.10"], 14 | start_permanent: Mix.env() == :prod, 15 | build_embedded: true, 16 | deps: deps(), 17 | releases: [{@app, release()}], 18 | preferred_cli_target: [run: :host, test: :host] 19 | ] 20 | end 21 | 22 | # Run "mix help compile.app" to learn about applications. 23 | def application do 24 | [ 25 | mod: {Example.Application, []}, 26 | extra_applications: [:logger, :runtime_tools] 27 | ] 28 | end 29 | 30 | # Run "mix help deps" to learn about dependencies. 31 | defp deps do 32 | [ 33 | # Dependencies for all targets 34 | {:nerves, "~> 1.7.0", runtime: false}, 35 | {:shoehorn, "~> 0.7.0"}, 36 | {:ring_logger, "~> 0.8.1"}, 37 | {:toolshed, "~> 0.2.13"}, 38 | 39 | # Dependencies for all targets except :host 40 | {:nerves_runtime, "~> 0.11.3", targets: @all_targets}, 41 | {:nerves_pack, "~> 0.4.0", targets: @all_targets}, 42 | {:circuits_cdev, path: "../", targets: @all_targets}, 43 | 44 | # Dependencies for specific targets 45 | {:nerves_system_rpi, "~> 1.16", runtime: false, targets: :rpi}, 46 | {:nerves_system_rpi0, "~> 1.16", runtime: false, targets: :rpi0}, 47 | {:nerves_system_rpi2, "~> 1.16", runtime: false, targets: :rpi2}, 48 | {:nerves_system_rpi3, "~> 1.16", runtime: false, targets: :rpi3}, 49 | {:nerves_system_rpi3a, "~> 1.16", runtime: false, targets: :rpi3a}, 50 | {:nerves_system_rpi4, "~> 1.16", runtime: false, targets: :rpi4}, 51 | {:nerves_system_bbb, "~> 2.11", runtime: false, targets: :bbb}, 52 | {:nerves_system_osd32mp1, "~> 0.7", runtime: false, targets: :osd32mp1}, 53 | {:nerves_system_x86_64, "~> 1.16.0", runtime: false, targets: :x86_64} 54 | ] 55 | end 56 | 57 | def release do 58 | [ 59 | overwrite: true, 60 | # Erlang distribution is not started automatically. 61 | # See https://hexdocs.pm/nerves_pack/readme.html#erlang-distribution 62 | cookie: "#{@app}_cookie", 63 | include_erts: &Nerves.Release.erts/0, 64 | steps: [&Nerves.Release.init/1, :assemble], 65 | strip_beams: Mix.env() == :prod or [keep: ["Docs"]] 66 | ] 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | # Makefile for building the NIF 3 | # 4 | # Makefile targets: 5 | # 6 | # all/install build and install the NIF 7 | # clean clean build products and intermediates 8 | # 9 | # Variables to override: 10 | # 11 | # MIX_APP_PATH path to the build directory 12 | # 13 | # CC C compiler 14 | # CROSSCOMPILE crosscompiler prefix, if any 15 | # CFLAGS compiler flags for compiling all C files 16 | # ERL_CFLAGS additional compiler flags for files using Erlang header files 17 | # ERL_EI_INCLUDE_DIR include path to ei.h (Required for crosscompile) 18 | # ERL_EI_LIBDIR path to libei.a (Required for crosscompile) 19 | # LDFLAGS linker flags for linking all binaries 20 | # ERL_LDFLAGS additional linker flags for projects referencing Erlang libraries 21 | 22 | PREFIX = $(MIX_APP_PATH)/priv 23 | BUILD = $(MIX_APP_PATH)/obj 24 | 25 | NIF = $(PREFIX)/cdev_nif.so 26 | 27 | CFLAGS ?= -O2 -Wall -Wextra -Wno-unused-parameter -pedantic 28 | CFLAGS += $(TARGET_CFLAGS) 29 | 30 | $(info "**** MIX_ENV set to [$(MIX_ENV)] ****") 31 | $(info "**** CIRCUITS_MIX_ENV set to [$(CIRCUITS_MIX_ENV)] ****") 32 | 33 | # Check that we're on a supported build platform 34 | ifeq ($(CROSSCOMPILE),) 35 | # Not crosscompiling. 36 | ifeq ($(shell uname -s),Darwin) 37 | $(warning Elixir Circuits only works on Nerves and Linux.) 38 | # $(warning Compiling a stub NIF for testing.) 39 | # HAL_SRC = src/hal_stub.c 40 | # LDFLAGS += -undefined dynamic_lookup -dynamiclib 41 | else 42 | ifneq ($(filter $(CIRCUITS_MIX_ENV) $(MIX_ENV),test),) 43 | $(warning Compiling stub NIF to support 'mix test') 44 | # HAL_SRC = src/hal_stub.c 45 | endif 46 | LDFLAGS += -fPIC -shared 47 | CFLAGS += -fPIC 48 | endif 49 | else 50 | # Crosscompiled build 51 | LDFLAGS += -fPIC -shared 52 | CFLAGS += -fPIC 53 | endif 54 | 55 | # Set Erlang-specific compile and linker flags 56 | ERL_CFLAGS ?= -I$(ERL_EI_INCLUDE_DIR) 57 | ERL_LDFLAGS ?= -L$(ERL_EI_LIBDIR) -lei 58 | 59 | SRC = src/gpio_chip.c src/enif_gpio_common.c src/cdev_nif.c 60 | HEADERS =$(wildcard src/*.h) 61 | OBJ = $(SRC:src/%.c=$(BUILD)/%.o) 62 | 63 | calling_from_make: 64 | mix compile 65 | 66 | all: install 67 | 68 | install: $(PREFIX) $(BUILD) $(NIF) 69 | 70 | $(OBJ): $(HEADERS) Makefile 71 | 72 | $(BUILD)/%.o: src/%.c 73 | $(CC) -c $(ERL_CFLAGS) $(CFLAGS) -o $@ $< 74 | 75 | $(NIF): $(OBJ) 76 | $(CC) -o $@ $(ERL_LDFLAGS) $(LDFLAGS) $^ 77 | 78 | $(PREFIX) $(BUILD): 79 | mkdir -p $@ 80 | 81 | clean: 82 | $(RM) $(NIF) $(OBJ) 83 | 84 | .PHONY: all clean calling_from_make install 85 | 86 | # NIF = ./priv/cdev_nif.so 87 | 88 | # all: 89 | # mkdir -p priv 90 | # gcc -o $(NIF) -I$(ERL_EI_INCLUDE_DIR) -fpic -shared src/cdev_nif.c 91 | 92 | # clean: 93 | # rm -r priv 94 | 95 | -------------------------------------------------------------------------------- /src/gpio_chip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Erlang Nif and GPIO character device driver functions 3 | */ 4 | #ifndef GPIO_CHIP_H 5 | #define GPIO_CHIP_H 6 | 7 | #include 8 | #include 9 | #include "erl_nif.h" 10 | 11 | #define GPIO_CHIP_LINE_INPUT 0x00 12 | #define GPIO_CHIP_LINE_OUTPUT 0x01 13 | 14 | #define GPIO_CHIP_ACTIVE_HIGH 0x00 15 | #define GPIO_CHIP_ACTIVE_LOW 0x01 16 | 17 | /* 18 | * Struct to encapsulate details about a GPIO chip 19 | */ 20 | struct gpio_chip { 21 | unsigned int num_lines; 22 | int fd; 23 | char name[32]; 24 | char label[32]; 25 | 26 | int has_info; 27 | }; 28 | 29 | /* 30 | * Struct for a line handle 31 | */ 32 | struct gpio_chip_line_handle { 33 | int num_lines; 34 | int handle_fd; 35 | }; 36 | 37 | /* 38 | * Struct for a line event handle 39 | */ 40 | struct gpio_chip_event_handle { 41 | int fd; 42 | }; 43 | 44 | /* 45 | * Struct for event data 46 | */ 47 | struct gpio_chip_event_data { 48 | uint64_t timestamp; 49 | int id; 50 | }; 51 | 52 | /* 53 | * Struct for line information 54 | */ 55 | struct gpio_chip_line_info { 56 | int direction; 57 | int active_low; 58 | 59 | char name[32]; 60 | char consumer[32]; 61 | }; 62 | 63 | /* 64 | * Close the GPIO chip 65 | */ 66 | void chip_close(struct gpio_chip *chip); 67 | 68 | /* 69 | * Close the event handle 70 | */ 71 | void chip_event_handle_close(struct gpio_chip_event_handle *handle); 72 | 73 | /* 74 | * Get the chip info 75 | */ 76 | int chip_get_info(struct gpio_chip *chip); 77 | 78 | /* 79 | * Get information about a line 80 | */ 81 | int chip_get_line_info(struct gpio_chip *chip, int offset, struct gpio_chip_line_info *line_info); 82 | 83 | /* 84 | * Close a line handle 85 | */ 86 | void chip_line_handle_close(struct gpio_chip_line_handle *handle); 87 | 88 | /* 89 | * Open the chip 90 | */ 91 | int chip_open(struct gpio_chip *chip, const char *path); 92 | 93 | /* 94 | * Read event data into the data struct from the event handle 95 | */ 96 | int chip_read_event_data(struct gpio_chip_event_handle *handle, struct gpio_chip_event_data *data); 97 | 98 | /* 99 | * Read the values from a line handle 100 | */ 101 | int chip_read_values(struct gpio_chip_line_handle *handle, int *buff); 102 | 103 | /* 104 | * Request an event handle 105 | */ 106 | int chip_request_event(struct gpio_chip *chip, int offset, struct gpio_chip_event_handle *handle); 107 | 108 | /* 109 | * Request a line handle 110 | */ 111 | int chip_request_lines(struct gpio_chip *chip, int offsets[], int direction, struct gpio_chip_line_handle *handle); 112 | 113 | /* 114 | * Set the values of the GPIO lines 115 | */ 116 | int chip_set_values(struct gpio_chip_line_handle *handle, int values[]); 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /example/config/target.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | # Use shoehorn to start the main application. See the shoehorn 4 | # docs for separating out critical OTP applications such as those 5 | # involved with firmware updates. 6 | 7 | config :shoehorn, 8 | init: [:nerves_runtime, :nerves_pack], 9 | app: Mix.Project.config()[:app] 10 | 11 | # Nerves Runtime can enumerate hardware devices and send notifications via 12 | # SystemRegistry. This slows down startup and not many programs make use of 13 | # this feature. 14 | 15 | config :nerves_runtime, :kernel, use_system_registry: false 16 | 17 | # Erlinit can be configured without a rootfs_overlay. See 18 | # https://github.com/nerves-project/erlinit/ for more information on 19 | # configuring erlinit. 20 | 21 | config :nerves, 22 | erlinit: [ 23 | hostname_pattern: "nerves-%s" 24 | ] 25 | 26 | # Configure the device for SSH IEx prompt access and firmware updates 27 | # 28 | # * See https://hexdocs.pm/nerves_ssh/readme.html for general SSH configuration 29 | # * See https://hexdocs.pm/ssh_subsystem_fwup/readme.html for firmware updates 30 | 31 | keys = 32 | [ 33 | Path.join([System.user_home!(), ".ssh", "id_rsa.pub"]), 34 | Path.join([System.user_home!(), ".ssh", "id_ecdsa.pub"]), 35 | Path.join([System.user_home!(), ".ssh", "id_ed25519.pub"]) 36 | ] 37 | |> Enum.filter(&File.exists?/1) 38 | 39 | if keys == [], 40 | do: 41 | Mix.raise(""" 42 | No SSH public keys found in ~/.ssh. An ssh authorized key is needed to 43 | log into the Nerves device and update firmware on it using ssh. 44 | See your project's config.exs for this error message. 45 | """) 46 | 47 | config :nerves_ssh, 48 | authorized_keys: Enum.map(keys, &File.read!/1) 49 | 50 | # Configure the network using vintage_net 51 | # See https://github.com/nerves-networking/vintage_net for more information 52 | config :vintage_net, 53 | regulatory_domain: "US", 54 | config: [ 55 | {"usb0", %{type: VintageNetDirect}}, 56 | {"eth0", 57 | %{ 58 | type: VintageNetEthernet, 59 | ipv4: %{method: :dhcp} 60 | }}, 61 | {"wlan0", %{type: VintageNetWiFi}} 62 | ] 63 | 64 | config :mdns_lite, 65 | # The `host` key specifies what hostnames mdns_lite advertises. `:hostname` 66 | # advertises the device's hostname.local. For the official Nerves systems, this 67 | # is "nerves-<4 digit serial#>.local". mdns_lite also advertises 68 | # "nerves.local" for convenience. If more than one Nerves device is on the 69 | # network, delete "nerves" from the list. 70 | 71 | host: [:hostname, "nerves"], 72 | ttl: 120, 73 | 74 | # Advertise the following services over mDNS. 75 | services: [ 76 | %{ 77 | name: "SSH Remote Login Protocol", 78 | protocol: "ssh", 79 | transport: "tcp", 80 | port: 22 81 | }, 82 | %{ 83 | name: "Secure File Transfer Protocol over SSH", 84 | protocol: "sftp-ssh", 85 | transport: "tcp", 86 | port: 22 87 | }, 88 | %{ 89 | name: "Erlang Port Mapper Daemon", 90 | protocol: "epmd", 91 | transport: "tcp", 92 | port: 4369 93 | } 94 | ] 95 | 96 | # Import target specific config. This must remain at the bottom 97 | # of this file so it overrides the configuration defined above. 98 | # Uncomment to use target specific configurations 99 | 100 | # import_config "#{Mix.target()}.exs" 101 | -------------------------------------------------------------------------------- /lib/cdev/nif.ex: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev.Nif do 2 | @moduledoc false 3 | 4 | # Lower level nif bindings 5 | 6 | @on_load {:load_nif, 0} 7 | @compile {:autoload, false} 8 | 9 | alias Circuits.Cdev 10 | 11 | def load_nif() do 12 | nif_binary = Application.app_dir(:circuits_cdev, "priv/cdev_nif") 13 | 14 | :erlang.load_nif(to_charlist(nif_binary), 0) 15 | end 16 | 17 | @doc """ 18 | Open a GPIO chip 19 | """ 20 | @spec chip_open_nif(charlist()) :: {:ok, reference()} | {:error, atom} 21 | def chip_open_nif(_path) do 22 | :erlang.nif_error(:nif_not_loaded) 23 | end 24 | 25 | @doc """ 26 | Get the information about a chip 27 | """ 28 | @spec get_chip_info_nif(reference()) :: 29 | {:ok, name :: charlist(), label :: charlist(), num_lines :: non_neg_integer()} 30 | | {:error, atom()} 31 | def get_chip_info_nif(_chip_ref) do 32 | :erlang.nif_error(:nif_not_loaded) 33 | end 34 | 35 | @doc """ 36 | Get the information about a line 37 | """ 38 | @spec get_line_info_nif(reference(), Cdev.offset()) :: 39 | {:ok, name :: charlist(), consumer :: charlist(), active_low :: non_neg_integer(), 40 | open_drain :: non_neg_integer()} 41 | | {:error, atom()} 42 | def get_line_info_nif(_chip_ref, _offset) do 43 | :erlang.nif_error(:nif_not_loaded) 44 | end 45 | 46 | @doc """ 47 | Listen for an event 48 | 49 | This function requires an event handle and a event data resource. 50 | """ 51 | @spec listen_event_nif(reference(), reference(), reference()) :: :ok | {:error, atom()} 52 | def listen_event_nif(_event_handle, _event_data, _ref) do 53 | :erlang.nif_error(:nif_not_loaded) 54 | end 55 | 56 | @doc """ 57 | Get an event data resource to be used with for listening for events. 58 | """ 59 | @spec make_event_data_nif(reference()) :: {:ok, reference()} | {:error, atom()} 60 | def make_event_data_nif(_event_handle) do 61 | :erlang.nif_error(:nif_not_loaded) 62 | end 63 | 64 | @doc """ 65 | Read the event data from the event handle 66 | """ 67 | @spec read_event_data_nif(reference(), reference()) :: 68 | {:ok, value :: non_neg_integer(), timestamp :: non_neg_integer()} | {:error, atom()} 69 | def read_event_data_nif(_event_handle, _event_data) do 70 | :erlang.nif_error(:nif_not_loaded) 71 | end 72 | 73 | @doc """ 74 | Read a list of values from the line handle 75 | """ 76 | @spec read_values_nif(reference()) :: {:ok, [Cdev.offset_value()]} | {:error, atom()} 77 | def read_values_nif(_line_handle_ref) do 78 | :erlang.nif_error(:nif_not_loaded) 79 | end 80 | 81 | @doc """ 82 | Request an event handle to be used for listening for events 83 | """ 84 | @spec request_event_nif(reference(), Cdev.offset()) :: {:ok, reference()} | {:error, atom()} 85 | def request_event_nif(_chip_ref, _offset) do 86 | :erlang.nif_error(:nif_not_loaded) 87 | end 88 | 89 | @doc """ 90 | Request lines to control 91 | """ 92 | @spec request_lines_nif(reference(), [Cdev.offset()], 0 | 1) :: 93 | {:ok, reference()} | {:error, atom()} 94 | def request_lines_nif(_chip_ref, _offset, _direction) do 95 | :erlang.nif_error(:nif_not_loaded) 96 | end 97 | 98 | @doc """ 99 | Set the values of the GPIO line(s) 100 | """ 101 | @spec set_values_nif(reference(), [Cdev.offset_value()]) :: :ok | {:error, atom()} 102 | def set_values_nif(_handle_ref, _values) do 103 | :erlang.nif_error(:nif_not_loaded) 104 | end 105 | end 106 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # circuits_cdev 2 | 3 | [![CircleCI](https://circleci.com/gh/elixir-circuits/circuits_cdev.svg?style=svg)](https://circleci.com/gh/elixir-circuits/circuits_cdev) 4 | 5 | Character device GPIO library for Elixir. 6 | 7 | *This library is no longer maintained. Please see [circuits_gpio](https://github.com/elixir-circuits/circuits_gpio) for a GPIO library that uses the character device GPIO API* 8 | 9 | Since Linux 4.8 the `sysfs` interface is deprecated in preference of the 10 | character device GPIO API. 11 | 12 | The advantages of the character device API over the `sysfs` interface are: 13 | 14 | * The allocation of the GPIO is tied to the process that is using it, which 15 | prevents many process from trying to control a GPIO at once. 16 | * Since the GPIO is tied to the process, if the process ends for any reason the 17 | GPIO will be cleaned up automatically. 18 | * It is possible to read or write many GPIOs at once. 19 | * It is possible to configure the state of the GPIO (open-source, open-drain, etc). 20 | * The polling process to catch event is reliable. 21 | 22 | The way to drive a GPIO line (a pin) is by requesting a line handle from a GPIO 23 | chip. A system may have many GPIO chips and these can be named or they default 24 | to using the name `gpiochipN` where `N` is the chip number starting a `0`. 25 | 26 | For example the main GPIO chip on the Raspberry PI systems is `gpiochip0` 27 | located at `"/dev/gpiochip0"`. 28 | 29 | ## Install 30 | 31 | ```elixir 32 | def deps do 33 | [{:circuits_cdev, "~> 0.1.0"}] 34 | end 35 | ``` 36 | 37 | ## Controlling the output of a line 38 | 39 | First request a line handle from the GPIO chip: 40 | 41 | ```elixir 42 | {:ok, line_handle} = Circuits.Cdev.request_line("gpiochip0", 17, :output) 43 | ``` 44 | 45 | After getting a line handle can now set the value of the line: 46 | 47 | ```elixir 48 | Circuits.Cdev.read_value(line_handle) 49 | 0 50 | ``` 51 | 52 | To set the value of the line: 53 | 54 | ```elixir 55 | Circuits.Cdev.set_value(line_handle, 1) 56 | :ok 57 | ``` 58 | 59 | ## Controlling the output of many lines 60 | 61 | First request a line handle from the GPIO chip: 62 | 63 | ```elixir 64 | {:ok, line_handle} = Circuits.Cdev.request_line("gpiochip0", [17, 27 20], :output) 65 | ``` 66 | 67 | After getting the line handle you can set the values of the lines. Notice that 68 | you to provide all the values in the same order as you requested the lines: 69 | 70 | ```elixir 71 | Circuits.Cdev.set_values(line_handle, [0, 1, 1]) 72 | ``` 73 | 74 | When reading the values of a line handle that controls more than one line you 75 | will receive a list of values in the order of that you requested the lines: 76 | 77 | ```elixir 78 | Circuits.Cdev.read_values(line_handle) 79 | {:ok, [0, 1, 1]} 80 | ``` 81 | 82 | ## Listen for events on a line 83 | 84 | You can listen for events on an GPIO line by calling the `listen_event/2` function: 85 | 86 | ```elixir 87 | Circuits.Cdev.listen_event("gpiochip0", 27) 88 | :ok 89 | ``` 90 | 91 | When an event is received from the line it will be in the form of: 92 | 93 | ```elixir 94 | {:circuits_cdev, pin_number, timestamp, new_value} 95 | ``` 96 | 97 | The `timestamp` is the unix time in nanoseconds. In order to convert it into a 98 | `DateTime` you will need to pass the `:nanoseconds` as the unit to 99 | `DateTime.from_unix/3` function: 100 | 101 | ```elixir 102 | DateTime.from_unix(timestamp, :nanoseconds) 103 | {:ok, ~U[1990-07-24 07:30:03.123456Z]} 104 | ``` 105 | 106 | Also when calculating the time delta between two events keep in mind the 107 | difference will be nanoseconds. 108 | -------------------------------------------------------------------------------- /src/gpio_chip.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "gpio_chip.h" 8 | 9 | void chip_close(struct gpio_chip *chip) 10 | { 11 | close(chip->fd); 12 | } 13 | 14 | void chip_event_handle_close(struct gpio_chip_event_handle *handle) 15 | { 16 | close(handle->fd); 17 | } 18 | 19 | int chip_open(struct gpio_chip *chip, const char *path) 20 | { 21 | int fd = open(path, O_RDWR); 22 | 23 | if (fd < 0) 24 | return fd; 25 | 26 | chip->fd = fd; 27 | 28 | return 0; 29 | } 30 | 31 | int chip_get_info(struct gpio_chip *chip) { 32 | if (chip->has_info == 1) 33 | return 0; 34 | 35 | struct gpiochip_info info; 36 | 37 | memset(&info, 0, sizeof(info)); 38 | 39 | int rv = ioctl(chip->fd, GPIO_GET_CHIPINFO_IOCTL, &info); 40 | 41 | if (rv < 0) 42 | return rv; 43 | 44 | chip->num_lines = info.lines; 45 | 46 | strncpy(chip->name, info.name, sizeof(chip->name)); 47 | 48 | if (info.label[0] == '\0') 49 | strncpy(chip->label, "unknown", sizeof(chip->label)); 50 | else 51 | strncpy(chip->label, info.label, sizeof(chip->label)); 52 | 53 | chip->has_info = 1; 54 | 55 | return 0; 56 | } 57 | 58 | 59 | void chip_line_handle_close(struct gpio_chip_line_handle *handle) { 60 | close(handle->handle_fd); 61 | } 62 | 63 | int chip_get_line_info(struct gpio_chip *chip, int offset, struct gpio_chip_line_info *line_info) 64 | { 65 | struct gpioline_info info; 66 | 67 | memset(&info, 0, sizeof(info)); 68 | 69 | info.line_offset = offset; 70 | 71 | int rv = ioctl(chip->fd, GPIO_GET_LINEINFO_IOCTL, &info); 72 | 73 | if (rv < 0) 74 | return -1; 75 | 76 | line_info->direction = info.flags & GPIOLINE_FLAG_IS_OUT 77 | ? GPIO_CHIP_LINE_OUTPUT 78 | : GPIO_CHIP_LINE_INPUT; 79 | 80 | line_info->active_low = info.flags & GPIOLINE_FLAG_ACTIVE_LOW 81 | ? GPIO_CHIP_ACTIVE_LOW 82 | : GPIO_CHIP_ACTIVE_HIGH; 83 | 84 | strncpy(line_info->name, info.name, sizeof(line_info->name)); 85 | strncpy(line_info->consumer, info.consumer, sizeof(line_info->consumer)); 86 | 87 | return 0; 88 | } 89 | 90 | int chip_read_event_data(struct gpio_chip_event_handle *handle, struct gpio_chip_event_data *data) 91 | { 92 | struct gpioevent_data event_data; 93 | 94 | int bytes_read = read(handle->fd, &event_data, sizeof(event_data)); 95 | 96 | if (bytes_read == -1) 97 | return -1; 98 | 99 | data->timestamp = event_data.timestamp; 100 | data->id = event_data.id; 101 | 102 | return 0; 103 | } 104 | 105 | int chip_read_values(struct gpio_chip_line_handle *handle, int *buff) 106 | { 107 | struct gpiohandle_data data; 108 | 109 | memset(&data, 0, sizeof(data)); 110 | 111 | int rv = ioctl(handle->handle_fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); 112 | 113 | if (rv < 0) 114 | return -1; 115 | 116 | for (int i = 0; i < handle->num_lines; i++) { 117 | buff[i] = (int)data.values[i]; 118 | } 119 | 120 | return 0; 121 | } 122 | 123 | int chip_request_event(struct gpio_chip *chip, int offset, struct gpio_chip_event_handle *handle) 124 | { 125 | struct gpioevent_request req; 126 | 127 | memset(&req, 0, sizeof(req)); 128 | 129 | req.lineoffset = offset; 130 | req.handleflags = GPIOHANDLE_REQUEST_INPUT; 131 | req.eventflags = GPIOEVENT_REQUEST_BOTH_EDGES; 132 | 133 | strncpy(req.consumer_label, "circuits_cdev", sizeof(req.consumer_label - 1)); 134 | 135 | int rv = ioctl(chip->fd, GPIO_GET_LINEEVENT_IOCTL, &req); 136 | 137 | if (rv < 0) 138 | return -1; 139 | 140 | handle->fd = req.fd; 141 | 142 | return 0; 143 | } 144 | 145 | int chip_request_lines(struct gpio_chip *chip, int offsets[], int direction, struct gpio_chip_line_handle *handle) 146 | { 147 | if (handle->num_lines == 0) 148 | return 0; 149 | 150 | if (handle->num_lines >= GPIOHANDLES_MAX) 151 | return -2; 152 | 153 | int gpio_direction = -1; 154 | 155 | if (direction == 0) { 156 | gpio_direction = GPIOHANDLE_REQUEST_INPUT; 157 | } else if (direction == 1) { 158 | gpio_direction = GPIOHANDLE_REQUEST_OUTPUT; 159 | } else { 160 | return -1; 161 | } 162 | 163 | struct gpiohandle_request req; 164 | 165 | memset(&req, 0, sizeof(req)); 166 | 167 | req.flags |= gpio_direction; 168 | req.lines = handle->num_lines; 169 | 170 | for (int i = 0; i < handle->num_lines; i++) { 171 | req.lineoffsets[i] = offsets[i]; 172 | } 173 | 174 | for (int i = 0; i < handle->num_lines; i++) { 175 | req.default_values[i] = 0; 176 | } 177 | 178 | strcpy(req.consumer_label, "circuits_gpio_chip"); 179 | 180 | int rv = ioctl(chip->fd, GPIO_GET_LINEHANDLE_IOCTL, &req); 181 | 182 | if (rv > 0) 183 | return -3; 184 | 185 | handle->handle_fd = req.fd; 186 | 187 | return 0; 188 | } 189 | 190 | int chip_set_values(struct gpio_chip_line_handle *handle, int values[]) 191 | { 192 | struct gpiohandle_data data; 193 | 194 | for (int i = 0; i < handle->num_lines; i++) { 195 | data.values[i] = values[i]; 196 | } 197 | 198 | int rv = ioctl(handle->handle_fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data); 199 | 200 | if (rv < 0) 201 | return -1; 202 | 203 | return 0; 204 | } 205 | -------------------------------------------------------------------------------- /lib/cdev.ex: -------------------------------------------------------------------------------- 1 | defmodule Circuits.Cdev do 2 | @moduledoc """ 3 | Control GPIOs using Linux GPIO character device interface 4 | 5 | With the character device driver for GPIOs there three concepts to learn. 6 | 7 | First, the API is made up of chips and lines that are grouped together for 8 | that chip. A chip is more of a grouping identifier than anything physical 9 | property about the board. 10 | 11 | Secondly, the API requires us to request lines from a GPIO chip. The reason 12 | for this is the kernel can provide control over who "owns" that line and 13 | prevent multiple programs from trying to control the same GPIO pin. 14 | 15 | Lastly, you can listen for events on a line. These events report if the line 16 | is high or low. 17 | 18 | Generally speaking the character device driver allows more fine grain control 19 | and more reliability than the `sysfs` API. 20 | """ 21 | 22 | alias Circuits.Cdev.{Events, LineHandle, LineInfo, Nif} 23 | 24 | @type chip() :: %{ 25 | name: String.t(), 26 | label: String.t(), 27 | number_of_lines: non_neg_integer(), 28 | reference: reference() 29 | } 30 | 31 | @typedoc """ 32 | The offset of the pin 33 | 34 | An offset is the pin number provided. Normally these are labeled `GPIO N` or 35 | `GPIO_N` where `N` is the pin number. For example, if you wanted to use to 36 | use `GPIO 17` on a Raspberry PI the offset value would be `17`. 37 | 38 | More resources: 39 | 40 | Raspberry PI: https://pinout.xyz/ 41 | Beaglebone: https://beagleboard.org/Support/bone101 42 | """ 43 | @type offset() :: non_neg_integer() 44 | 45 | @typedoc """ 46 | The value of the offset 47 | 48 | This is either 0 for low or off, or 1 for high or on. 49 | """ 50 | @type offset_value() :: 0 | 1 51 | 52 | @typedoc """ 53 | The direction of the line 54 | 55 | With the character device you drive a line with configured offsets. These 56 | offsets all share a direction, either `:output` or `:input`, which is called 57 | the line direction. 58 | 59 | The `:output` direction means you control the GPIOs by setting the value of 60 | the GPIOs to 1 or 0. See `Circuits.Cdev.set_value/2` for more 61 | information. 62 | 63 | The `:input` direction means you can only read the current value of the GPIOs 64 | on the line. See `Circuits.Cdev.read_value/1` for more information. 65 | """ 66 | @type line_direction() :: :input | :output 67 | 68 | @doc """ 69 | Getting information about a line 70 | """ 71 | @spec get_line_info(chip(), offset()) :: {:ok, LineInfo.t()} | {:error, atom()} 72 | def get_line_info(chip, offset) do 73 | case Nif.get_line_info_nif(chip.reference, offset) do 74 | {:ok, name, consumer, direction, active_low} -> 75 | {:ok, 76 | %LineInfo{ 77 | offset: offset, 78 | name: to_string(name), 79 | consumer: to_string(consumer), 80 | direction: direction_to_atom(direction), 81 | active_low: active_low_int_to_bool(active_low) 82 | }} 83 | 84 | error -> 85 | error 86 | end 87 | end 88 | 89 | @doc """ 90 | Listen to line events on the line offset 91 | 92 | ```elixir 93 | Circuits.Cdev.listen_event(mygpio_chip, 24) 94 | # cause the offset to change value 95 | flush 96 | {:circuits_cdev, 24, timestamp, new_value} 97 | ``` 98 | 99 | The timestamp will be in nanoseconds so as you do time calculations and 100 | conversions be sure to take that into account. 101 | 102 | The `new_value` will be the value the offset value changed to either `1` or 103 | `0`. 104 | """ 105 | @spec listen_event(chip() | String.t(), offset()) :: :ok 106 | def listen_event(chip_name, offset) when is_binary(chip_name) do 107 | case open(chip_name) do 108 | {:ok, chip} -> listen_event(chip, offset) 109 | end 110 | end 111 | 112 | def listen_event(chip, offset) do 113 | Events.listen_event(chip, offset) 114 | end 115 | 116 | @doc """ 117 | Open a GPIO Chip 118 | 119 | ```elixir 120 | {:ok, chip} = Circuits.Cdev.open(gpiochip_device) 121 | ``` 122 | """ 123 | @spec open(String.t()) :: {:ok, chip()} 124 | def open(chip_name) do 125 | chip_name = Path.join("/dev", chip_name) 126 | {:ok, ref} = Nif.chip_open_nif(to_charlist(chip_name)) 127 | {:ok, name, label, number_of_lines} = Nif.get_chip_info_nif(ref) 128 | 129 | {:ok, 130 | %{ 131 | name: to_string(name), 132 | label: to_string(label), 133 | number_of_lines: number_of_lines, 134 | reference: ref 135 | }} 136 | end 137 | 138 | @doc """ 139 | Read value from a line handle 140 | 141 | This is useful when you have a line handle that contains only one GPIO 142 | offset. 143 | 144 | If you want to read multiple GPIOs at once see 145 | `Circuits.Cdev.read_values/1`. 146 | 147 | ```elixir 148 | {:ok, line_handle} = Circuits.Cdev.request_line("gpiochip0", 17) 149 | {:ok, 0} = Circuits.Cdev.read_value(line_handle) 150 | ``` 151 | """ 152 | @spec read_value(LineHandle.t()) :: {:ok, offset_value()} | {:error, atom()} 153 | def read_value(line_handle) do 154 | case read_values(line_handle) do 155 | {:ok, [value]} -> 156 | {:ok, value} 157 | 158 | error -> 159 | error 160 | end 161 | end 162 | 163 | @doc """ 164 | Read values for a line handle 165 | 166 | This is useful when you a line handle that contains multiple GPIO offsets. 167 | 168 | ```elixir 169 | {:ok, line_handle} = Circuits.Cdev.request_lines("gpiochip0", [17, 22, 23, 24]) 170 | {:ok, [0, 0, 0, 0]} = Circuits.Cdev.read_values(line_handle) 171 | ``` 172 | 173 | Note that the values in the list match the index order of how the offsets were 174 | requested. 175 | 176 | Note that the order of the values returned return the order that the offsets 177 | were requested. 178 | """ 179 | @spec read_values(LineHandle.t()) :: {:ok, [offset_value()]} | {:error, atom()} 180 | def read_values(line_handle) do 181 | %LineHandle{handle: handle} = line_handle 182 | 183 | Nif.read_values_nif(handle) 184 | end 185 | 186 | @doc """ 187 | Request a line handle for a single GPIO offset 188 | 189 | 190 | ```elixir 191 | {:ok, line_handle} = Circuits.Cdev.request_line(my_gpio_chip, 17, :output) 192 | ``` 193 | 194 | See `Circuits.Cdev.request_lines/3` and `Circuits.Cdev.LineHandle` for 195 | more details about line handles. 196 | """ 197 | @spec request_line(chip() | String.t(), offset(), line_direction()) :: {:ok, LineHandle.t()} 198 | def request_line(chip_name, offset, direction) when is_binary(chip_name) do 199 | case open(chip_name) do 200 | {:ok, chip} -> 201 | request_lines(chip, [offset], direction) 202 | end 203 | end 204 | 205 | def request_line(chip, offset, direction) do 206 | request_lines(chip, [offset], direction) 207 | end 208 | 209 | @doc """ 210 | Request a line handle for multiple GPIO offsets 211 | 212 | ```elixir 213 | {:ok, line_handle} = Circuits.Cdev.request_lines(my_gpio_chip, [17, 24], :output) 214 | ``` 215 | 216 | For the GPIO character device driver you drive GPIOs by requesting for a line 217 | handle what contains one or more GPIO offsets. The line handle is mechanism 218 | by which you can read and set the values of the GPIO(s). The line handle is 219 | attached to the calling process and kernel will not allow others to control 220 | the GPIO(s) that are part of that the line handle. Moreover, one the process 221 | that requested the line handle goes away the kernel will be able to 222 | automatically free the system resources that were tied to that line handle. 223 | """ 224 | @spec request_lines(chip() | String.t(), [offset()], line_direction()) :: {:ok, LineHandle.t()} 225 | def request_lines(chip_name, offsets, direction) when is_binary(chip_name) do 226 | case open(chip_name) do 227 | {:ok, chip} -> 228 | request_lines(chip, offsets, direction) 229 | end 230 | end 231 | 232 | def request_lines(chip, offsets, direction) do 233 | {:ok, handle} = Nif.request_lines_nif(chip.reference, offsets, direction_from_atom(direction)) 234 | 235 | {:ok, %LineHandle{chip: chip, handle: handle}} 236 | end 237 | 238 | @doc """ 239 | Set the value of the GPIO 240 | 241 | ```elixir 242 | {:ok, line_handle} = Circuits.Cdev.request_lines(my_gpio_chip, 17) 243 | {:ok, 0} = Circuits.Cdev.read_value(line_handle) 244 | :ok = Circuits.Cdev.set_value(line_handle, 1) 245 | {:ok, 1} = Circuits.Cdev.read_value(line_handle) 246 | ``` 247 | """ 248 | @spec set_value(LineHandle.t(), offset_value()) :: :ok | {:error, atom()} 249 | def set_value(handle, value) do 250 | set_values(handle, [value]) 251 | end 252 | 253 | @doc """ 254 | Set values of the GPIOs 255 | 256 | ```elixir 257 | {:ok, line_handle} = Circuits.Cdev.request_lines(my_gpio_chip, [17, 24, 22]) 258 | {:ok, [0, 0, 0]} = Circuits.Cdev.read_value(line_handle) 259 | :ok = Circuits.Cdev.set_value(line_handle, [1, 0, 1]) 260 | {:ok, [1, 0, 1]} = Circuits.Cdev.read_value(line_handle) 261 | ``` 262 | 263 | Note that the order of the values that were sent matches the order by which 264 | the GPIO offsets where requested. In the example above offset 17 was set to 265 | 1, offset 24 was stayed at 0, offset 22 was set to 1. 266 | """ 267 | @spec set_values(LineHandle.t(), [offset_value()]) :: :ok | {:error, atom()} 268 | def set_values(line_handle, values) do 269 | %LineHandle{handle: handle} = line_handle 270 | Nif.set_values_nif(handle, values) 271 | end 272 | 273 | defp direction_from_atom(:input), do: 0 274 | defp direction_from_atom(:output), do: 1 275 | 276 | defp direction_to_atom(0), do: :input 277 | defp direction_to_atom(1), do: :output 278 | 279 | defp active_low_int_to_bool(0), do: false 280 | defp active_low_int_to_bool(1), do: true 281 | end 282 | -------------------------------------------------------------------------------- /src/cdev_nif.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "cdev_nif.h" 8 | #include "enif_gpio_common.h" 9 | #include "gpio_chip.h" 10 | 11 | static void chip_res_destructor(ErlNifEnv *env, void *res) 12 | { 13 | chip_close((struct gpio_chip *) res); 14 | } 15 | 16 | static void line_res_destructor(ErlNifEnv *env, void *res) 17 | { 18 | chip_line_handle_close((struct gpio_chip_line_handle *) res); 19 | } 20 | 21 | static void event_res_destructor(ErlNifEnv *env, void *res) 22 | { 23 | chip_event_handle_close((struct gpio_chip_event_handle *) res); 24 | } 25 | 26 | static void event_data_res_destructor(ErlNifEnv *env, void *res) {} 27 | 28 | static int load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM info) 29 | { 30 | chip_priv_t *priv = enif_alloc(sizeof(chip_priv_t)); 31 | 32 | if (!priv) { 33 | return 1; 34 | } 35 | 36 | priv->atom_error = enif_make_atom(env, "error"); 37 | priv->atom_ok = enif_make_atom(env, "ok"); 38 | 39 | priv->gpio_chip_rt = enif_open_resource_type(env, NULL, "gpio_chip", chip_res_destructor, ERL_NIF_RT_CREATE, NULL); 40 | priv->gpio_chip_line_handle_rt = enif_open_resource_type(env, NULL, "gpio_line_handle", line_res_destructor, ERL_NIF_RT_CREATE, NULL); 41 | priv->gpio_chip_event_handle_rt= enif_open_resource_type(env, NULL, "gpio_event_handle", event_res_destructor, ERL_NIF_RT_CREATE, NULL); 42 | priv->gpio_chip_event_data_rt = enif_open_resource_type(env, NULL, "gpio_event_data", event_data_res_destructor, ERL_NIF_RT_CREATE, NULL); 43 | 44 | *priv_data = (void *) priv; 45 | 46 | return 0; 47 | } 48 | 49 | static ERL_NIF_TERM chip_open_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 50 | { 51 | chip_priv_t *priv = enif_priv_data(env); 52 | char chip_path[16]; 53 | memset(&chip_path, '\0', sizeof(chip_path)); 54 | 55 | if (!enif_get_string(env, argv[0], chip_path, sizeof(chip_path), ERL_NIF_LATIN1)) 56 | return enif_make_badarg(env); 57 | 58 | struct gpio_chip *chip = enif_alloc_resource(priv->gpio_chip_rt, sizeof(struct gpio_chip)); 59 | 60 | int rv = chip_open(chip, chip_path); 61 | 62 | if (rv < 0) 63 | return common_make_error(env, enif_make_atom(env, "open_failed")); 64 | 65 | ERL_NIF_TERM chip_res = common_make_and_release_resource(env, chip); 66 | 67 | return enif_make_tuple2(env, priv->atom_ok, chip_res); 68 | } 69 | 70 | static ERL_NIF_TERM get_chip_info_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 71 | { 72 | chip_priv_t *priv = enif_priv_data(env); 73 | struct gpio_chip *chip; 74 | 75 | if (argc != 1 || !enif_get_resource(env, argv[0], priv->gpio_chip_rt, (void **)&chip)) 76 | return enif_make_badarg(env); 77 | 78 | int rv = chip_get_info(chip); 79 | 80 | if (rv < 0) 81 | return common_make_error(env, enif_make_atom(env, "info_get_failed")); 82 | 83 | ERL_NIF_TERM name = enif_make_string(env, chip->name, ERL_NIF_LATIN1); 84 | ERL_NIF_TERM label = enif_make_string(env, chip->label, ERL_NIF_LATIN1); 85 | ERL_NIF_TERM number_lines = enif_make_int(env, (int)chip->num_lines); 86 | 87 | return enif_make_tuple4(env, priv->atom_ok, name, label, number_lines); 88 | } 89 | 90 | static ERL_NIF_TERM get_line_info_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 91 | { 92 | chip_priv_t *priv = enif_priv_data(env); 93 | struct gpio_chip *chip; 94 | int offset; 95 | 96 | if (argc != 2 97 | || !enif_get_resource(env, argv[0], priv->gpio_chip_rt, (void **)&chip) 98 | || !enif_get_int(env, argv[1], &offset)) 99 | return enif_make_badarg(env); 100 | 101 | struct gpio_chip_line_info line_info; 102 | 103 | int rv = chip_get_line_info(chip, offset, &line_info); 104 | 105 | if (rv == -1) 106 | return common_make_error(env, enif_make_atom(env, "line_info_get_failed")); 107 | 108 | ERL_NIF_TERM direction = enif_make_int(env, line_info.direction); 109 | ERL_NIF_TERM active_low = enif_make_int(env, line_info.active_low); 110 | ERL_NIF_TERM name = enif_make_string(env, line_info.name, ERL_NIF_LATIN1); 111 | ERL_NIF_TERM consumer = enif_make_string(env, line_info.consumer, ERL_NIF_LATIN1); 112 | 113 | return enif_make_tuple5(env, priv->atom_ok, name, consumer, direction, active_low); 114 | } 115 | 116 | static ERL_NIF_TERM listen_event_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 117 | { 118 | chip_priv_t *priv = enif_priv_data(env); 119 | struct gpio_chip_event_handle *handle; 120 | struct gpio_chip_event_data *data; 121 | 122 | if (argc != 3 123 | || !enif_get_resource(env, argv[0], priv->gpio_chip_event_handle_rt, (void **)&handle) 124 | || !enif_get_resource(env, argv[1], priv->gpio_chip_event_data_rt, (void **)&data)) 125 | return enif_make_badarg(env); 126 | 127 | int select_rv = enif_select(env, handle->fd, ERL_NIF_SELECT_READ, data, NULL, argv[2]); 128 | 129 | if (select_rv < 0) 130 | return common_make_error(env, enif_make_atom(env, "failed_to_listen_for_events")); 131 | 132 | return priv->atom_ok; 133 | } 134 | 135 | static ERL_NIF_TERM make_event_data_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 136 | { 137 | chip_priv_t *priv = enif_priv_data(env); 138 | struct gpio_chip_event_handle *handle; 139 | 140 | if (argc != 1 141 | || !enif_get_resource(env, argv[0], priv->gpio_chip_event_handle_rt, (void **)&handle)) 142 | return enif_make_badarg(env); 143 | 144 | struct enif_event_data *data = enif_alloc_resource(priv->gpio_chip_event_data_rt, sizeof(struct gpio_chip_event_data)); 145 | memset(data, 0, sizeof(struct gpio_chip_event_data)); 146 | 147 | ERL_NIF_TERM data_res = common_make_and_release_resource(env, data); 148 | 149 | return enif_make_tuple2(env, priv->atom_ok, data_res); 150 | } 151 | 152 | static ERL_NIF_TERM read_event_data_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 153 | { 154 | chip_priv_t *priv = enif_priv_data(env); 155 | struct gpio_chip_event_handle *handle; 156 | struct gpio_chip_event_data *event_data; 157 | 158 | if (argc != 2 159 | || !enif_get_resource(env, argv[0], priv->gpio_chip_event_handle_rt, (void **)&handle) 160 | || !enif_get_resource(env, argv[1], priv->gpio_chip_event_data_rt, (void **)&event_data)) 161 | return enif_make_badarg(env); 162 | 163 | int rv = chip_read_event_data(handle, event_data); 164 | 165 | if (rv != 0) 166 | return common_make_error(env, enif_make_atom(env, "read_event_data_failed")); 167 | 168 | ERL_NIF_TERM value = enif_make_int(env, event_data->id); 169 | ERL_NIF_TERM timestamp = enif_make_uint64(env, event_data->timestamp); 170 | 171 | return enif_make_tuple3(env, priv->atom_ok, value, timestamp); 172 | 173 | return priv->atom_ok; 174 | } 175 | 176 | static ERL_NIF_TERM read_values_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 177 | { 178 | chip_priv_t *priv = enif_priv_data(env); 179 | struct gpio_chip_line_handle *handle; 180 | 181 | if (argc != 1 || !enif_get_resource(env, argv[0], priv->gpio_chip_line_handle_rt, (void **)&handle)) 182 | return enif_make_badarg(env); 183 | 184 | int buff[handle->num_lines]; 185 | 186 | int rv = chip_read_values(handle, buff); 187 | 188 | if (rv != 0) 189 | return common_make_error(env, enif_make_atom(env, "read_values_failed")); 190 | 191 | ERL_NIF_TERM term_array[handle->num_lines]; 192 | 193 | for (int i = 0; i < handle->num_lines; i++) { 194 | ERL_NIF_TERM value = enif_make_int(env, buff[i]); 195 | term_array[i] = value; 196 | } 197 | 198 | ERL_NIF_TERM values_list = enif_make_list_from_array(env, term_array, handle->num_lines); 199 | 200 | return enif_make_tuple2(env, priv->atom_ok, values_list); 201 | } 202 | 203 | static ERL_NIF_TERM request_event_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 204 | { 205 | chip_priv_t *priv = enif_priv_data(env); 206 | struct gpio_chip *chip; 207 | int offset; 208 | 209 | if (argc != 2 210 | || !enif_get_resource(env, argv[0], priv->gpio_chip_rt, (void **)&chip) 211 | || !enif_get_int(env, argv[1], &offset)) 212 | return enif_make_badarg(env); 213 | 214 | struct gpio_chip_event_handle *handle = enif_alloc_resource(priv->gpio_chip_event_handle_rt, sizeof(struct gpio_chip_event_handle)); 215 | 216 | int rv = chip_request_event(chip, offset, handle); 217 | 218 | if (rv != 0) 219 | return common_make_error(env, enif_make_atom(env, "event_request_failed")); 220 | 221 | ERL_NIF_TERM handle_res = common_make_and_release_resource(env, handle); 222 | 223 | return enif_make_tuple2(env, priv->atom_ok, handle_res); 224 | } 225 | 226 | static ERL_NIF_TERM request_lines_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 227 | { 228 | chip_priv_t *priv = enif_priv_data(env); 229 | struct gpio_chip *chip; 230 | int direction; 231 | int num_offsets; 232 | 233 | if (argc != 3 234 | || !enif_get_resource(env, argv[0], priv->gpio_chip_rt, (void **)&chip) 235 | || !enif_get_int(env, argv[2], &direction) 236 | || !enif_get_list_length(env, argv[1], &num_offsets)) 237 | return enif_make_badarg(env); 238 | 239 | int offsets_array[num_offsets]; 240 | 241 | int pv = common_make_int_array_from_list(env, argv[1], num_offsets, offsets_array); 242 | 243 | if (pv != 0) 244 | return enif_make_badarg(env); 245 | 246 | struct gpio_chip_line_handle *line_handle = enif_alloc_resource(priv->gpio_chip_line_handle_rt, sizeof(struct gpio_chip_line_handle)); 247 | 248 | line_handle->num_lines = num_offsets; 249 | 250 | int rv = chip_request_lines(chip, offsets_array, direction, line_handle); 251 | 252 | if (rv != 0) 253 | return common_make_error(env, enif_make_atom(env, "lines_request_failed")); 254 | 255 | ERL_NIF_TERM line_handle_res = common_make_and_release_resource(env, line_handle); 256 | 257 | return enif_make_tuple2(env, priv->atom_ok, line_handle_res); 258 | } 259 | 260 | static ERL_NIF_TERM set_values_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) 261 | { 262 | chip_priv_t *priv = enif_priv_data(env); 263 | struct gpio_chip_line_handle *handle; 264 | int num_values; 265 | 266 | if (argc != 2 267 | || !enif_get_resource(env, argv[0], priv->gpio_chip_line_handle_rt, (void **)&handle) 268 | || !enif_get_list_length(env, argv[1], &num_values)) 269 | return enif_make_badarg(env); 270 | 271 | if (handle->num_lines != num_values) { 272 | ERL_NIF_TERM msg = enif_make_atom(env, "different_number_of_lines"); 273 | ERL_NIF_TERM err = enif_make_tuple3(env, msg, handle->num_lines, num_values); 274 | return common_make_error(env, err); 275 | } 276 | 277 | int values_array[num_values]; 278 | 279 | int ar = common_make_int_array_from_list(env, argv[1], num_values, values_array); 280 | 281 | if (ar != 0) 282 | return common_make_error(env, enif_make_atom(env, "failed_to_parse_values_list")); 283 | 284 | int rv = chip_set_values(handle, values_array); 285 | 286 | if (rv != 0) 287 | return common_make_error(env, enif_make_atom(env, "failed_to_set_values")); 288 | 289 | return priv->atom_ok; 290 | } 291 | 292 | static ErlNifFunc nif_funcs[] = { 293 | {"chip_open_nif", 1, chip_open_nif, 0}, 294 | {"get_chip_info_nif", 1, get_chip_info_nif, 0}, 295 | {"get_line_info_nif", 2, get_line_info_nif, 0}, 296 | {"listen_event_nif", 3, listen_event_nif, 0}, 297 | {"make_event_data_nif", 1, make_event_data_nif, 0}, 298 | {"read_event_data_nif", 2, read_event_data_nif, 0}, 299 | {"read_values_nif", 1, read_values_nif, 0}, 300 | {"request_event_nif", 2, request_event_nif, 0}, 301 | {"request_lines_nif", 3, request_lines_nif, 0}, 302 | {"set_values_nif", 2, set_values_nif, 0} 303 | }; 304 | 305 | ERL_NIF_INIT(Elixir.Circuits.Cdev.Nif, nif_funcs, load, NULL, NULL, NULL) 306 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /example/mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "beam_notify": {:hex, :beam_notify, "0.2.1", "64f94f5b486b4cc96ef10a633188560cdd6ae2fccaf6fd746851cdc12b85d5f1", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "d1b3a8d280867c43c2c53e61556ca5ad87e0907ad25797d2c753bac5d8b3dc44"}, 3 | "circular_buffer": {:hex, :circular_buffer, "0.4.0", "a51ea76bb03c4a38207934264bcc600018ead966728ca80da731458c5f940f8b", [:mix], [], "hexpm", "c604b19f2101982b63264e2ed90c6fb0fe502540b6af83ce95135ac9b6f2d847"}, 4 | "dns": {:hex, :dns, "2.2.0", "4721a79c2bccc25481930dffbfd06f40851321c3d679986af307111214bf124c", [:mix], [{:socket, "~> 0.3.13", [hex: :socket, repo: "hexpm", optional: false]}], "hexpm", "13ed1ef36ce896211ec6ce5e02709dbfb12aa61d6255bda8d531577a0a5a56e0"}, 5 | "elixir_make": {:hex, :elixir_make, "0.6.2", "7dffacd77dec4c37b39af867cedaabb0b59f6a871f89722c25b28fcd4bd70530", [:mix], [], "hexpm", "03e49eadda22526a7e5279d53321d1cced6552f344ba4e03e619063de75348d9"}, 6 | "gen_state_machine": {:hex, :gen_state_machine, "3.0.0", "1e57f86a494e5c6b14137ebef26a7eb342b3b0070c7135f2d6768ed3f6b6cdff", [:mix], [], "hexpm", "0a59652574bebceb7309f6b749d2a41b45fdeda8dbb4da0791e355dd19f0ed15"}, 7 | "mdns_lite": {:hex, :mdns_lite, "0.7.0", "91da487424d5ba70e13eb44bba67469541cdfe423dfad65be54bca28d8b4217f", [:mix], [{:vintage_net, "~> 0.7", [hex: :vintage_net, repo: "hexpm", optional: false]}], "hexpm", "b93421ec3919b844793468547a160589a3aad1cdfd4692a1977bc85acd44ab0b"}, 8 | "muontrap": {:hex, :muontrap, "0.6.1", "fa11dc9152470c4d0ce5a5fcb6524d8c1edc9bf6d63b3f6a89096f1e751ae271", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "86d1ef2fa0a30435a1d595e96f631ad4a24a931d8d855688e012fadd7147bd1d"}, 9 | "nerves": {:hex, :nerves, "1.7.10", "6a28a69bd671c708615e0b3caded2efaf7159679e3568d0a4eb3acb73ab2c6aa", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "2137d3eaaf8ce0db9189eec26e7ee8d24183a680bc270c812d14d4e8c1a305a7"}, 10 | "nerves_pack": {:hex, :nerves_pack, "0.4.2", "458c7c9f5f7f67e9568f6b0ce16200eef3b93a1686a61773d488294b827dc922", [:mix], [{:mdns_lite, "~> 0.6", [hex: :mdns_lite, repo: "hexpm", optional: false]}, {:nerves_runtime, "~> 0.6", [hex: :nerves_runtime, repo: "hexpm", optional: false]}, {:nerves_ssh, "~> 0.2", [hex: :nerves_ssh, repo: "hexpm", optional: false]}, {:nerves_time, "~> 0.3", [hex: :nerves_time, repo: "hexpm", optional: false]}, {:ring_logger, "~> 0.8", [hex: :ring_logger, repo: "hexpm", optional: false]}, {:vintage_net, "~> 0.7.0 or ~> 0.8.0 or ~> 0.9.0 or ~> 0.10.0", [hex: :vintage_net, repo: "hexpm", optional: false]}, {:vintage_net_direct, "~> 0.7", [hex: :vintage_net_direct, repo: "hexpm", optional: false]}, {:vintage_net_ethernet, "~> 0.7", [hex: :vintage_net_ethernet, repo: "hexpm", optional: false]}, {:vintage_net_wifi, "~> 0.7", [hex: :vintage_net_wifi, repo: "hexpm", optional: false]}], "hexpm", "7a8f51074ca4c230f4b5d436f949a9eaf053ec5cbbc1309ae8b1eea720151ccb"}, 11 | "nerves_runtime": {:hex, :nerves_runtime, "0.11.5", "6dc92ed2ca2921742844193593d6363da70f088fb490307d029cff1d818fc705", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:system_registry, "~> 0.8.0", [hex: :system_registry, repo: "hexpm", optional: false]}, {:uboot_env, "~> 0.1.1 or ~> 0.2.0 or ~> 0.3.0", [hex: :uboot_env, repo: "hexpm", optional: false]}], "hexpm", "07fd8846583c45227484ab49a8567f011a0d92df05ab1a567c6aec53137bcedf"}, 12 | "nerves_ssh": {:hex, :nerves_ssh, "0.2.2", "7154bbb75825f5176317b94dfdc65aa353aab8f49ef72b66c77342be5fa92f47", [:mix], [{:nerves_runtime, "~> 0.11", [hex: :nerves_runtime, repo: "hexpm", optional: false]}, {:ssh_subsystem_fwup, "~> 0.5", [hex: :ssh_subsystem_fwup, repo: "hexpm", optional: false]}], "hexpm", "8b073aabefb4ef050eb00922f67fe0e870d104a94e7ba7748d335451f4f90479"}, 13 | "nerves_system_bbb": {:hex, :nerves_system_bbb, "2.11.1", "2c64c60f086c278bfa61e7ff11b6ae7d0c717bfe1680290d54d80573574e4119", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.4", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv7_nerves_linux_gnueabihf, "~> 1.4.3", [hex: :nerves_toolchain_armv7_nerves_linux_gnueabihf, repo: "hexpm", optional: false]}], "hexpm", "4909815b6e12acbc2bb8ceca56a1a1bc82401cfac3fc3399d03048a15f53d0af"}, 14 | "nerves_system_br": {:hex, :nerves_system_br, "1.16.1", "2e86dd7511d6fc974bf425cb335eaa9cb59292ecd42e63805f58560cccb04375", [:mix], [], "hexpm", "ca7147d2cae703aea26c4d399dde9a6373b9eac925dcd091440d10345267457a"}, 15 | "nerves_system_osd32mp1": {:hex, :nerves_system_osd32mp1, "0.7.1", "785042924345ab92c162adaf461f9bd5d5ff19ae340e8dc753e7b104bcb0f3bc", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.4", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv7_nerves_linux_gnueabihf, "~> 1.4.3", [hex: :nerves_toolchain_armv7_nerves_linux_gnueabihf, repo: "hexpm", optional: false]}], "hexpm", "318c491ca13174b37f6b9397f69dc17c686ffb93ab44db11de09fd02609fdf82"}, 16 | "nerves_system_rpi": {:hex, :nerves_system_rpi, "1.16.1", "5937064b5f7a1c7a29666537664f8920ff77a417a8a20e0b73d9648de6c0d433", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.3", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv6_nerves_linux_gnueabihf, "~> 1.4.3", [hex: :nerves_toolchain_armv6_nerves_linux_gnueabihf, repo: "hexpm", optional: false]}], "hexpm", "4a82f0e62a9ea77cc53872db518572d464c17fd78b8ab7b6cebcd3c62f4bd322"}, 17 | "nerves_system_rpi0": {:hex, :nerves_system_rpi0, "1.16.1", "072e6ed7f26a96902c3cc7d1654ec211ba6d885ad8acaeb000ea3a82adbd78a0", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.3", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv6_nerves_linux_gnueabihf, "~> 1.4.3", [hex: :nerves_toolchain_armv6_nerves_linux_gnueabihf, repo: "hexpm", optional: false]}], "hexpm", "7498752ec247b31a5858b9de1ec7a3eda8c365ecd74448b9ce9235b9f27431f7"}, 18 | "nerves_system_rpi2": {:hex, :nerves_system_rpi2, "1.16.1", "971dd502628a15b41d8710f0aaaa044fad93b44c124e0f850f6bd604f0255d7d", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.4", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv7_nerves_linux_gnueabihf, "~> 1.4.3", [hex: :nerves_toolchain_armv7_nerves_linux_gnueabihf, repo: "hexpm", optional: false]}], "hexpm", "daee96b3f407986de584e2e83e94621e8e13c1b7d3bcffb535269977aa4f1360"}, 19 | "nerves_system_rpi3": {:hex, :nerves_system_rpi3, "1.16.1", "fa6f3296a2c92a736f346a9775e700292ae0e0c09f553465c56b3791ea002e03", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.4", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv7_nerves_linux_gnueabihf, "~> 1.4.3", [hex: :nerves_toolchain_armv7_nerves_linux_gnueabihf, repo: "hexpm", optional: false]}], "hexpm", "152da2ee1c70ac9e282575428bf53372d406de72bf6a3e834661a524fdfa83c5"}, 20 | "nerves_system_rpi3a": {:hex, :nerves_system_rpi3a, "1.16.1", "10394ff84db46a1bc461d125f1e1829740c978e286b6ebd0d9758a8e563e5312", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.3", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_armv7_nerves_linux_gnueabihf, "~> 1.4.3", [hex: :nerves_toolchain_armv7_nerves_linux_gnueabihf, repo: "hexpm", optional: false]}], "hexpm", "c40ecf08d4c0b6b3f07df8fc0254e69de101f7a07f9a6f3327d2634701b83aba"}, 21 | "nerves_system_rpi4": {:hex, :nerves_system_rpi4, "1.16.1", "1ad1b6ffc92f1eb7fca3f0a4c2ec38960b51d5b4a745e63b532492d690c55254", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.3", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_aarch64_nerves_linux_gnu, "~> 1.4.3", [hex: :nerves_toolchain_aarch64_nerves_linux_gnu, repo: "hexpm", optional: false]}], "hexpm", "2ac3a42266a2cfe887b6162b5e7a3b31e2204a3fa07c3814601d4cdc76140dd9"}, 22 | "nerves_system_x86_64": {:hex, :nerves_system_x86_64, "1.16.1", "bc6f113d4bab2f14e743ed90f4d9c4a12430a9d67c571efdef3fb5071baa6907", [:mix], [{:nerves, "~> 1.5.4 or ~> 1.6.0 or ~> 1.7.4", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_system_br, "1.16.1", [hex: :nerves_system_br, repo: "hexpm", optional: false]}, {:nerves_toolchain_x86_64_nerves_linux_musl, "~> 1.4.3", [hex: :nerves_toolchain_x86_64_nerves_linux_musl, repo: "hexpm", optional: false]}], "hexpm", "82582c25a4d4273ef56d3067aa6ce23b428b57272518c2625cdec44a16ff1d83"}, 23 | "nerves_time": {:hex, :nerves_time, "0.4.3", "c936ddf7f1f9eb890c8dfcf116f5ac665780351704066282bed15fe6d2cf9b1b", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:muontrap, "~> 0.5", [hex: :muontrap, repo: "hexpm", optional: false]}], "hexpm", "a8a7d6e129e90ce35296d0e3522580335da8a0413790ad27c33bcea0cb63c4e5"}, 24 | "nerves_toolchain_aarch64_nerves_linux_gnu": {:hex, :nerves_toolchain_aarch64_nerves_linux_gnu, "1.4.3", "6aa784fd3779251a4438e9874ed646492c3a4289f10581d01c4d61acda7d0b2d", [:mix], [{:nerves, "~> 1.4", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.8.4", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "75ab06af12118b423b4bba870a4f10d81f83678fd7fc57278927ce9d52516c5e"}, 25 | "nerves_toolchain_aarch64_unknown_linux_gnu": {:hex, :nerves_toolchain_aarch64_unknown_linux_gnu, "1.3.2", "0948d4af5fd45a9ba01a3087737ac837c3ad36da31f6445ad0f3ab7ea3c61634", [:mix], [{:nerves, "~> 1.4", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.7.2", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "33f487fe40be4cce4aed72ba5eb1f1911b2b0c19972b1ff5ca51ea4f14abdab7"}, 26 | "nerves_toolchain_arm_unknown_linux_gnueabihf": {:hex, :nerves_toolchain_arm_unknown_linux_gnueabihf, "1.3.2", "c1adf1c067f46810a678946506f379b8d1694b154fcbc77b1de5fda97494fbfc", [:mix], [{:nerves, "~> 1.0", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.7.2", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "0200515efb0ddd6a2318234c7084f7cdb8691fbb83c47def527558b4921f3a3a"}, 27 | "nerves_toolchain_armv6_nerves_linux_gnueabihf": {:hex, :nerves_toolchain_armv6_nerves_linux_gnueabihf, "1.4.3", "c75a3975388c3378deb72ed50178f34f8cc47b575b32546d22e522a577f6b8c0", [:mix], [{:nerves, "~> 1.0", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.8.4", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "33e3a174ff51f81bca3181560d33c065694828e8c8c72aada0b92a46ba7cfbe5"}, 28 | "nerves_toolchain_armv6_rpi_linux_gnueabi": {:hex, :nerves_toolchain_armv6_rpi_linux_gnueabi, "1.3.2", "beb0f97d6f432406af6c4fdcee02d2ab750298845834f869a0b98239362783ae", [:mix], [{:nerves, "~> 1.0", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.7.2", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "ef93b7d678241be2d4520dceecf6a2116bb87640576211dad3b618ce79b85e39"}, 29 | "nerves_toolchain_armv7_nerves_linux_gnueabihf": {:hex, :nerves_toolchain_armv7_nerves_linux_gnueabihf, "1.4.3", "ff5b8fed2a71daea7ac07a5a0a6ecec7a4985d2a617a89ab073591d441d9cda4", [:mix], [{:nerves, "~> 1.0", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.8.4", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "ffefca61b7282a5a10032e61b6dab6758d24eee732c5d8c16fe6aada52dd099a"}, 30 | "nerves_toolchain_ctng": {:hex, :nerves_toolchain_ctng, "1.8.4", "2f6b4153e3904502d117f9d957c12eaafd490e1d2bdf20a85328ada46a1350da", [:mix], [{:nerves, "~> 1.0", [hex: :nerves, repo: "hexpm", optional: false]}], "hexpm", "6194be9b1364fdc1db6b2a0e98fa8dcb94fe1af373dcf8149298d62ce9b1b369"}, 31 | "nerves_toolchain_x86_64_nerves_linux_musl": {:hex, :nerves_toolchain_x86_64_nerves_linux_musl, "1.4.3", "2fb6fd7e618afb0bc2f3114bd316381229b288d7a2f392ea3538359717cd5263", [:mix], [{:nerves, "~> 1.0", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.8.4", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "bdb044a2be8fe99d7b2606e478c64051b4a4cac6a4b69a617f0438a20e480a7c"}, 32 | "nerves_toolchain_x86_64_unknown_linux_musl": {:hex, :nerves_toolchain_x86_64_unknown_linux_musl, "1.3.2", "cc16ab1dda16d11a8942ba00b91214ce19915a0e1b76888bd432335847add9cf", [:mix], [{:nerves, "~> 1.0", [hex: :nerves, repo: "hexpm", optional: false]}, {:nerves_toolchain_ctng, "~> 1.7.2", [hex: :nerves_toolchain_ctng, repo: "hexpm", optional: false]}], "hexpm", "9117021e9942e35e87604629120c90278f238841dcd627bf085b694647dec396"}, 33 | "one_dhcpd": {:hex, :one_dhcpd, "0.2.5", "ecec86e567839bde69717abb24c1ae5c74fcdc71beccfce541a0c3149aa980a4", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "4d59693a9998c7dfd6d9f06c556ff23c73c817832f812205a6f09d38250b6dfa"}, 34 | "ring_logger": {:hex, :ring_logger, "0.8.2", "78e0ffbf89b3f5b3c58f8660cb6b6fe2ca7ed5f56dc64e993119189d629c278c", [:mix], [{:circular_buffer, "~> 0.4.0", [hex: :circular_buffer, repo: "hexpm", optional: false]}], "hexpm", "864c8982bfcdc17b7501d00cc2bcede6727ebf95cbeba30e70c795d03f9138c1"}, 35 | "shoehorn": {:hex, :shoehorn, "0.7.0", "fc23870fb6b470f5c520fee692637b120a36e163842ab497bbec7e8a1aa6cfe3", [:mix], [], "hexpm", "eeb317ac677b228906039ccf532a582ad9f6d31d7958c98f5c853fe0459e0243"}, 36 | "socket": {:hex, :socket, "0.3.13", "98a2ab20ce17f95fb512c5cadddba32b57273e0d2dba2d2e5f976c5969d0c632", [:mix], [], "hexpm", "f82ea9833ef49dde272e6568ab8aac657a636acb4cf44a7de8a935acb8957c2e"}, 37 | "ssh_subsystem_fwup": {:hex, :ssh_subsystem_fwup, "0.6.0", "821b9cdf52b8dcc286183bce7641e62589373642a10180cd4cadd8e322bc4e8e", [:mix], [], "hexpm", "e5bcc923af4ae49368177437d20383730d096281beb18e9e9a36806aff1ca750"}, 38 | "system_registry": {:hex, :system_registry, "0.8.2", "df791dc276652fcfb53be4dab823e05f8269b96ac57c26f86a67838dbc0eefe7", [:mix], [], "hexpm", "f7acdede22c73ab0b3735eead7f2095efb2a7a6198366564205274db2ca2a8f8"}, 39 | "toolshed": {:hex, :toolshed, "0.2.22", "f2f7bef8ae4f3dd8f43c7bc9d8ac2d8263d74cb3db593250aaf047ec9345e72f", [:mix], [{:nerves_runtime, "~> 0.8", [hex: :nerves_runtime, repo: "hexpm", optional: true]}], "hexpm", "59d1dad19235ec9eed50e717733be5329cab7be9b78d8d0e83058b4fe8faec4f"}, 40 | "uboot_env": {:hex, :uboot_env, "0.3.0", "8afbcc8e5b65e5d0d5660ded2f5835a959d2326fa8683183f380cd6464e75174", [:mix], [], "hexpm", "d8fe5d2b4d52a14398ace02bd604ff7a0fa8960550bb7254f75dcbd438ddc6a1"}, 41 | "vintage_net": {:hex, :vintage_net, "0.10.5", "0441e5c76338ca071c97503390fcfc484c2f352b7573453e8da1255e65345c96", [:make, :mix], [{:beam_notify, "~> 0.2.0", [hex: :beam_notify, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:gen_state_machine, "~> 2.0.0 or ~> 2.1.0 or ~> 3.0.0", [hex: :gen_state_machine, repo: "hexpm", optional: false]}, {:muontrap, "~> 0.5.1 or ~> 0.6.0", [hex: :muontrap, repo: "hexpm", optional: false]}], "hexpm", "69b599dff8e018d562a7dbdb1c192b663aad6ae7ced5690b3c0a1337f9f1f65c"}, 42 | "vintage_net_direct": {:hex, :vintage_net_direct, "0.10.1", "3daf4bc86595119f144b4956cbcef0916a2fd8b1238164040f82fe6fa6c1bcd9", [:mix], [{:one_dhcpd, "~> 0.2.3", [hex: :one_dhcpd, repo: "hexpm", optional: false]}, {:vintage_net, "~> 0.9.1 or ~> 0.10.0", [hex: :vintage_net, repo: "hexpm", optional: false]}], "hexpm", "bf0643a1f6533eddcac88ca94d16c52285d0252fe1675b140886c96f0e8ae39d"}, 43 | "vintage_net_ethernet": {:hex, :vintage_net_ethernet, "0.10.2", "7af44e5db1a536238061358bb3111f65e6b80157100016b34012ce68d2b34031", [:mix], [{:vintage_net, "~> 0.10.0", [hex: :vintage_net, repo: "hexpm", optional: false]}], "hexpm", "f80d3f8a80cebcf2f2091f46fb8f3f0dee1aea7f4e94890c19cfc512e26e496f"}, 44 | "vintage_net_wifi": {:hex, :vintage_net_wifi, "0.10.4", "9405a063f3602802cb7179d90377d58c88e8b1bcb92a7844c8662a937b3427bf", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:vintage_net, "~> 0.10.0", [hex: :vintage_net, repo: "hexpm", optional: false]}], "hexpm", "8b0373f1a0c1e3f93530c0bb4c8c88afb1a34c750f2482c9d0339a3b19d18b4e"}, 45 | } 46 | --------------------------------------------------------------------------------