From charlesreid1

Line 47: Line 47:


wireless - ???
wireless - ???
==Network Configuration==
After installing OpenWRT, the Widy will, by default, assign IP addresses in the range <code>192.168.1.X</code>. However, this will cause problems if you want to connect to the Widy and simultaneously connect to a wireless network that also assigns IP addresses in the range <code>192.168.1.X</code>.
To fix this, use the LUCI interface to change the IP address range that the Widy uses to assign new IP addresses. I changed it to <code>192.168.0.X</code> to keep it simple.
Start by logging in to the LUCI interface:
[[Image:OpenWRT_Login.png|300px]]
Go to Network > Interfaces:
[[Image:OpenWRT_NetworkInterfaces1.png|500px]]
Now you will see a list of enabled/active network interfaces. Click the "Edit" button for the network interface you want to change:
[[Image:OpenWRT_NetworkInterfaces2.png|500px]]
Now change the "IPv4 address" text field to be the IP address range that you want:
[[Image:OpenWRT_IPv4address.png|500px]]


==System Configuration==
==System Configuration==

Revision as of 17:49, 21 June 2016

UCI = Unified Configuration Interface

OpenWRT uses UCI to configure just about everything.

Overview of Widy Configuration

On the Widy, or any device running OpenWrt, the device's configuration is set using UCI, the Unified Configuration Interface. This is similar in spirit to the way that most routers have web interfaces that allow you to modify their configuration options. You can interact with the UCI system using two different UCI interfaces: one is the web interface, LUCI, and the other is the command line interface uci.

In each case, changes to the UCI options are written to a group of files located in /etc/config. The list of files looks like this:

# ls -l /etc/config
-rw-r--r--    1 root     root           724 Mar 16 22:33 dhcp
-rw-r--r--    1 root     root            62 Oct  1  2014 dropbear
-rw-r--r--    1 root     root          2024 Mar  3 06:25 firewall
-rw-r--r--    1 root     root           151 Oct  1  2014 fstab
-rw-r--r--    1 root     root           624 Oct  1  2014 luci
-rw-r--r--    1 root     root           449 Mar 16 22:31 network
-rw-r--r--    1 root     root           677 Oct  1  2014 system
-rw-r--r--    1 root     root             0 Oct  1  2014 ubootenv
-rw-r--r--    1 root     root           717 Sep 20  2014 ucitrack
-rw-------    1 root     root           635 Oct  1  2014 uhttpd
-rw-r--r--    1 root     root           374 Mar  3 12:08 wireless

Each config file covers one major aspect of the Widy's operation.

DHCP - configuration for the DHCP server, when the Widy is acting as a router

Dropbear - this is the lightweight SSH server that runs on the Widy (see [1])

Firewall -

Fstab - important for setting up the file system. This is an important step in expanding the Widy's filesystem to live on a USB jump drive

LUCI - the LUCI interface is the web interface to the Widy, which allows you to use a nice GUI webpage instead of a command-line interface

Network - configures the network. Duh.

System - ???

ubootenv - ???

ucitrack - ???

uhttpd - ???

wireless - ???

Network Configuration

After installing OpenWRT, the Widy will, by default, assign IP addresses in the range 192.168.1.X. However, this will cause problems if you want to connect to the Widy and simultaneously connect to a wireless network that also assigns IP addresses in the range 192.168.1.X.

To fix this, use the LUCI interface to change the IP address range that the Widy uses to assign new IP addresses. I changed it to 192.168.0.X to keep it simple.

Start by logging in to the LUCI interface:

OpenWRT Login.png

Go to Network > Interfaces:

OpenWRT NetworkInterfaces1.png

Now you will see a list of enabled/active network interfaces. Click the "Edit" button for the network interface you want to change:

OpenWRT NetworkInterfaces2.png

Now change the "IPv4 address" text field to be the IP address range that you want:

OpenWRT IPv4address.png

System Configuration

Link: https://wiki.openwrt.org/doc/uci/system

Q: What happens when you change /etc/config/file? Does it stay persistent?

Q: When and how does UCI override changes with defaults?

Q: How can I link UCI with the 3 slider buttons?

Slider Button Configuration

The Widy comes equipped with a slider button, with 3 positions:

  • 3G/4G
  • WISP
  • AP

WidySwitch.jpg

Before we can control these buttons, we need to know their names.

Start by making a script that will run whenever the buttons are activated:

$ mkdir -p /etc/hotplug.d/button

Now create the file /etc/hotplug.d/button/buttons and add a command to log some information about the button in the system log:

#!/bin/sh
logger the button was $BUTTON and the action was $ACTION

Now save the file and exit. This script will run whenever the buttons on the Widy are activated.

Slide the button from the 3G/4G position to the WISP position. If you are connected directly via ethernet, your connection should stay alive.

Now look at the system log:

$ logread

You'll see a notice about a button being pressed, and a button being released. The "pressed" button is the starting position of the button.

Sliding it from the 3G/4G position (top) to the WISP position (middle) shows:

Jan 1 00:01:15 OpenWrt user.notice root: The button was BTN_0 and the action was pressed
Jan 1 00:01:15 OpenWrt user.notice root: The button was BTN_1 and the action was released

Then sliding it from the WISP position (middle) back to the 3G/4G position (top) shows:

Jan 1 00:01:18 OpenWrt user.notice root: The button was BTN_1 and the action was pressed
Jan 1 00:01:18 OpenWrt user.notice root: The button was BTN_0 and the action was released

How The Switch Works

By observing the output in the syslog, I deduced that there are only TWO buttons to set the state of a THREE-position switch. This is done by means of a truth table: with both switches off, it represents the null state (the switch is always set to something). If BTN_0 is on and BTN_1 is off, it represents one state; if BTN_0 is off and BTN_1 is on, it represents another state; and if BTN_0 and BTN_1 are both on, it represents a third state.

After puzzling over this system and putting in the effort to reverse-engineer it, I discovered this page that describes the whole setup in detail: https://wiki.openwrt.org/toh/tp-link/tl-mr3040

BTN_0 (Pin 19) BTN_1 (Pin 20) Switch State
0 1 3G/4G
1 0 WISP
1 1 AP

To make a switch that will listen for the state of the switch and cross-check it with the above truth table, create a script in /etc/hotplug.d/button:

$ mkdir -p /etc/hotplug.d/button
$ vi /etc/hotplug.d/button/10-slider

and the 10-slider script looks like:

#!/bin/sh
sleep 1
logger "log: $BUTTON -- $ACTION"

if [ $ACTION == "released" ] ; then
	if [ $BUTTON == "BTN_1" ] ; then
		logger "Slider WISP"
		#some actions here
	elif [ $BUTTON == "BTN_0" ] && grep -qe "sw1.*in  lo" /sys/kernel/debug/gpio ; then
		logger "Slider 3G 4G"
		#some action here
	fi
elif [ $BUTTON == "BTN_0" ] || [ $BUTTON == "BTN_1" ] ; then
	if grep -qe "sw1.*in  hi" /sys/kernel/debug/gpio ; then
		if grep -qe "sw2.*in  hi" /sys/kernel/debug/gpio ; then
			logger "Slider AP"
			#some action here
		fi                  
	fi
fi

Badda-bing


Connecting Configurations with Switch

Different switch states can be set with this init script.

we can add actions where those "some action here" comments are, specifically by using the uci command: https://wiki.openwrt.org/doc/uci#command_line_utility

Let's illustrate with an example:

When the router is set to 3G/4G, the IP address will be set to 192.168.100.1

When the router is set to WISP, the IP address will be set to 192.168.150.1

When the router is set to AP, the IP address will be set to 192.168.200.1

We will accomplish this by setting the IP address with uci.

UCI Command

To figure out what UCI command would be required to change a configuration file to set the IP address, I started by looking for which configuration file contained the IP address. Obviously, it's the /etc/config/network file.

To see what syntax was needed to change options in this file, I ran:

$ uci show network
network.loopback.proto=static
network.loopback.ipaddr=127.0.0.1
...
network.lan.proto=static
network.lan.type=bridge
network.lan.ipaddr=192.168.1.1

To change this configuration file with the uci command line interface, we can use a command like:

$ uci set network.lan.ipaddr=192.168.2.1

then restart the router:

/etc/init.d/network restart

If we add some code like this to the hotplug button init script, we can link configurations to the button positions.

#!/bin/sh
sleep 1
logger "log: $BUTTON -- $ACTION"

if [ $ACTION == "released" ] ; then
	if [ $BUTTON == "BTN_1" ] ; then
		logger "Slider WISP"
		uci set network.lan.ipaddr=192.168.100.1


	elif [ $BUTTON == "BTN_0" ] && grep -qe "sw1.*in  lo" /sys/kernel/debug/gpio ; then
		logger "Slider 3G 4G"
		uci set network.lan.ipaddr=192.168.150.1


	fi
elif [ $BUTTON == "BTN_0" ] || [ $BUTTON == "BTN_1" ] ; then
	if grep -qe "sw1.*in  hi" /sys/kernel/debug/gpio ; then
		if grep -qe "sw2.*in  hi" /sys/kernel/debug/gpio ; then
			logger "Slider AP"
        		uci set network.lan.ipaddr=192.168.200.1


		fi                  
	fi
fi

Links

Linkz:

Openwrt documentation on UCI: https://wiki.openwrt.org/doc/uci

http://mattventura.net/openwrt-stuff/

Flags