From charlesreid1

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 check whether this is an issue, go to 192.168.1.1 in your browser. If you see the LUCI web interface, it's no biggie. If you see some other configuration page for a different wireless router, then you have an IP address conflict.

To fix this, disconnect from any networks you are connected to, then connect to the Widy using an ethernet cable. You can use the LUCI interface to change the IP address range that the Widy uses to assign new IP addresses. This example will change it to 192.168.0.X

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?

A: Yes, when you edit /etc/config/file it will be a permanent change. If you reboot the router the configuration will remain the same. If you open the LUCI web interface, it will load all of the configuration details from the files in /etc/config/. Everything will be reflected correctly, as it should.

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

A: If you don't set options in the files in /etc/config/, then default options will be used. If you set it in a config file, the option you set will be used. If you remove the setting from the config file, the default value will be used again.

Q: How can I link UCI with the 3-setting slider button on the side of the Widy?

A: You can use a shell script to check on the state of the slider button.

Opkg Package Manager Configuration

the opkg setup on openwrt underwent some changes a few years back, so there is now a split into "packages" and "old packages".

Up to date packages (these have maintainers and no security issues): https://archive.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/packages/

Old packages (these have no maintainers and potentially have issues): https://archive.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/oldpackages/

Best approach is probably to only add in the old packages when you specifically need them.

To modify the list of packages, edit /etc/opkg.cfg:

root@widy:~# cat /etc/opkg.conf

dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
option overlay_root /overlay
src/gz barrier_breaker_packages http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/packages
src/gz old_barrier_breaker_packages http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/oldpackages
src/gz barrier_breaker_base http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/base
src/gz barrier_breaker_luci http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/luci
src/gz barrier_breaker_management http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/management
src/gz barrier_breaker_routing http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/routing
src/gz barrier_breaker_telephony http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/telephony
option check_signature 1

Slider Button Configuration

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

  • 3G/4G
  • WISP
  • AP

WidySwitch.jpg

To link the slider button with scripts, you can create a script that will run whenever the buttons are activated. Start by creating the following directory:

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

Now, anytime you change the state of the slider button, the Widy will run any and all scripts contained in this folder.

You can create a new file /etc/hotplug.d/button/buttons, to run when the slider button is changed, and make a script that will record the names of each of the buttons to 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 # give it a second, in case the user has butterfingers
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

Bada-bing!

Checking Output from Switch Script

$ # okay, go ahead and flick the button

$ logread
Fri Mar  3 20:48:46 2017 authpriv.notice dropbear[1247]: Password auth succeeded for 'root' from 192.168.1.189:52934
Fri Mar  3 20:50:43 2017 user.notice root: [hotplug] loging button BTN_0 -- action pressed
Fri Mar  3 20:50:43 2017 user.notice root: [hotplug] Slider AP
Fri Mar  3 20:50:43 2017 user.notice root: [hotplug] Killed python and airodump
Fri Mar  3 20:50:44 2017 user.notice root: [hotplug] loging button BTN_1 -- action released
Fri Mar  3 20:50:44 2017 user.notice root: [hotplug] Slider WISP
Fri Mar  3 20:50:45 2017 user.notice root: [hotplug] loging button BTN_1 -- action pressed
Fri Mar  3 20:50:45 2017 user.notice root: [hotplug] Slider AP
Fri Mar  3 20:50:45 2017 user.notice root: [hotplug] Killed python and airodump

$

Connecting Configurations with Switch

Different switch states can be set with this init script. By adding actions in the above script, where it says "some action here," you can make the Widy do things when you change the slider switch.

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