changing network functionality

pull/216/head
connor rigby 2016-12-14 15:14:44 -08:00
parent ce7b15a50a
commit 375b6b43ba
18 changed files with 157 additions and 30 deletions

View File

@ -4,9 +4,6 @@ import_config "#{Mix.env}.exs"
config :farmbot_auth,
callbacks: [Farmbot.RPC.Transport.GenMqtt.Handler]
config :farmbot_configurator,
callback: Farmbot.BotState.Monitor
config :json_rpc,
transport: Farmbot.RPC.Transport.GenMqtt.Handler,
handler: Farmbot.RPC.Handler

View File

@ -1,2 +0,0 @@
use Mix.Config
config :farmbot, state_path: "/tmp"

View File

@ -0,0 +1,4 @@
use Mix.Config
config :farmbot, state_path: "/tmp"
# it is expected that in development mode, something else is managing network.
config :network, false

View File

@ -0,0 +1,10 @@
use Mix.Config
config :farmbot, state_path: "/tmp"
# when booting on qemu, we only have one interface, and we expect it to be
# automatically configured.
config :network,
hostapd: false,
auto_dhcp: "eth0",
interfaces: [
{"eth0", :ethernet},
]

View File

@ -0,0 +1,11 @@
use Mix.Config
config :farmbot, state_path: "/state"
# on rpi3 we have wifi (with host mode) and ethernet available.
# on boot we should bring up hostapd to allow for configurations.
config :network,
hostapd: "wlan0",
auto_dhcp: false,
interfaces: [
{"eth0", :ethernet},
{"wlan0", :wifi}
]

View File

@ -3,12 +3,6 @@ config :nerves, :firmware,
rootfs_additions: "config/rootfs-additions-#{Mix.Project.config[:target]}",
hardware: "config/rootfs-additions-#{Mix.Project.config[:target]}"
config :farmbot,
state_path: "/state"
config :farmbot_networking,
dnsmasq_path: "/root/dnsmasq.lease"
config :logger, :console,
# format: "$metadata[$level] $levelpad$message\r\n",
colors: [enabled: true ]
@ -28,5 +22,6 @@ config :iex,
"%node",
">",
:reset ] |> IO.ANSI.format |> IO.chardata_to_string
# overwrite anything on if need be.
import_config "#{Mix.Project.config[:target]}.exs"
import_config "hardware_#{Mix.Project.config[:target]}.exs"

View File

@ -1,3 +0,0 @@
use Mix.Config
config :farmbot, state_path: "/tmp"
config :farmbot, config_file: "default_config_qemu.json"

View File

@ -1 +0,0 @@
use Mix.Config

View File

@ -110,7 +110,7 @@ defmodule Farmbot.BotState.Monitor do
do
BotState.update_config("timezone", timezone)
BotState.add_creds({email,password,server})
NetMan.connect(:ethernet, BotState.Network)
# NetMan.connect(:ethernet, BotState.Network)
dispatch(mgr,state)
end
@ -123,7 +123,7 @@ defmodule Farmbot.BotState.Monitor do
do
BotState.update_config("timezone", timezone)
BotState.add_creds({email,password,server})
NetMan.connect({ssid, psk}, BotState.Network)
# NetMan.connect({ssid, psk}, BotState.Network)
dispatch(mgr,state)
end

View File

@ -22,10 +22,10 @@ defmodule Farmbot.BotState.Network do
}
@spec load(args) :: {:ok, t}
def load(_) do
NetMan.put_pid(__MODULE__)
# NetMan.put_pid(__MODULE__)
case get_config(:connection) do
{:ok, connection} ->
:ok = start_connection(connection)
start_connection(connection)
f = %State{connected?: false, connection: connection}
{:ok, f}
_ ->
@ -37,8 +37,9 @@ defmodule Farmbot.BotState.Network do
end
@spec start_connection(connection) :: :ok | {:error, atom}
defp start_connection(connection), do: NetMan.connect(connection, __MODULE__)
defp start_connection(connection) do
# NetMan.connect(connection, __MODULE__)
end
def handle_call(event, _from, %State{} = state) do
Logger.warn ">> got an unhandled call in " <>
"Network State tracker: #{inspect event}"

View File

@ -0,0 +1,24 @@
defmodule Network.EventManager do
use GenEvent
require Logger
def handle_event({:udhcpc, _, :bound, %{ipv4_address: address}}, state) do
# NetMan.on_ip(address)
{:ok, state}
end
def handle_event({:nerves_wpa_supplicant, _, wpa_event}, state) when is_atom(wpa_event) do
#:"CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid=\"supersecretssid\" auth_failures=2 duration=20 reason=WRONG_KEY"
event_string = Atom.to_string(wpa_event)
if(String.contains?(event_string, "reason=WRONG_KEY")) do
# NetMan.bad_key
end
{:ok, state}
end
def handle_event(_event, state) do
#IO.inspect event
#IO.inspect state
{:ok, state}
end
end

View File

@ -0,0 +1,29 @@
defmodule Farmbot.Network.Manager do
@moduledoc """
A behaviour for managing networking.
"""
@type ret_val :: :ok | {:error, atom}
@callback start_link(pid) :: {:ok, pid}
end
# I've identified 3 distinct modes of network operation.
# I've dubbed them 'virtual', 'lan', and 'wan'.
# wan being full on cloud-enabled, by virtue of wifi-client ethernet-client or 4g-lte-client.
# lan being local network only by virtue of wifi-client, wifi-host, ethernet-client, ethernet-host, or any to be determined client or host.
# Requisite • 1 min
# oh man that sentence just like clicked so much in my head
# 1 min
# Requisite Zero (requisite.zero@gmail.com)
# and virtual being host-only.
# Requisite • 1 min
# SHIT SON
# i know how to fix this now
# Now
# Requisite Zero (requisite.zero@gmail.com)
# You're welcome.
# Requisite • Now
# i was trying to see it all to specific
# Now

View File

@ -0,0 +1,9 @@
defmodule Module.concat([Farmbot, Network, Handler, "development"]) do
@behaviour Farmbot.Network.Handler
require Logger
def manager, do: GenEvent.start_link
def init(parent) do
Logger.debug ">> development network handler init."
Process.send_after(parent, :connected, 1_000)
end
end

View File

@ -0,0 +1,3 @@
defmodule Module.concat([Farmbot, Network, Manager, "qemu"]) do
end

View File

@ -0,0 +1,3 @@
defmodule Module.concat([Farmbot, Network, Manager, "rpi3"]) do
end

View File

@ -0,0 +1,30 @@
defmodule Farmbot.Network do
@moduledoc """
uh
"""
defmodule State do
defstruct [connected?: false]
@type t :: %__MODULE__{connected?: boolean}
end
require Logger
def start_link(hardware) do
GenServer.start_link(__MODULE__, hardware, name: __MODULE__)
end
def init(hardware) do
handler = Module.concat([Farmbot,Network,Handler,hardware])
Logger.debug ">> is initializing networking on: #{inspect hardware}"
{:ok, manager} = handler.manager
GenEvent.add_handler(manager, handler, self())
{:ok, %State{}}
end
def handle_info(:connected, state) do
Logger.debug ">> is connected!"
{:noreply, state}
end
end

10
mix.exs
View File

@ -42,7 +42,7 @@ defmodule Farmbot.Mixfile do
:mustache,
:timex,
:farmbot_auth,
:farmbot_configurator,
# :farmbot_configurator,
:vmq_commons,
:amnesia,
:quantum]
@ -50,11 +50,7 @@ defmodule Farmbot.Mixfile do
# on device
def apps(:prod) do
apps ++ platform_apps(target(:prod)) ++
[
:nerves,
:nerves_firmware_http
]
apps ++ platform_apps(target(:prod)) ++ [:nerves, :nerves_firmware_http]
end
# dev
@ -86,7 +82,7 @@ defmodule Farmbot.Mixfile do
{:quantum, ">= 1.8.1"},
{:farmbot_auth, github: "Farmbot/farmbot_auth"},
# {:farmbot_auth, path: "../farmbot_auth"},
{:farmbot_configurator, github: "Farmbot/farmbot_configurator"}
# {:farmbot_configurator, github: "Farmbot/farmbot_configurator"}
# {:farmbot_configurator, path: "../farmbot_configurator"}
]
end

View File

@ -38,4 +38,25 @@ and is "underkill" for large amounts of state that arent as easily "stringified"
* could also save to the /tmp partition, then another module copies that file to the main persistent ext4 fs.
* The Database uses amnesia, and could probably use built in :mnesia commands to copy it to persistent storage
* or :mnesia could copy to /tmp similarly to configuration, and the yet to be created module could copy that /tmp to persisteent fs.
* scheduler stil tbd
* scheduler stil tbd
# Network
## Problem
* network information needs to persist reboots.
* networking needs to support quite a few differente modes
* ethernet
* static
* dhcp
* wifi
* static
* dhcp
* host mode
* already connected (no managemnt from farmbot.)
but it also can't be too tightly coupled to authorization.
for example: i cant just have network get an ip address, and then tell authorization to do its thing.
network should be taken care of before the farmbot application starts, but be configurable at runtime.
configurator needs to have some knowledge of networking because it gets configured there, but it really only needs to know what
different interfaces are available and how to configure them. Configurator should have no knowledge of linux. it should just pass information along to something else.