[Dissecting OpenWRT] #4: /lib/functions/network.sh

OpenWRT/LEDE has alread provided us some useful shell functions to find information from physical and logical interfaces. In this post I will show you some functions from this file.

  1. Inquire the first IP address of logical interface: network_get_ipaddr
    Create a shell script called test.sh and fill it with
#!/bin/sh

. /lib/functions/network.sh

if [ "$#" -ne 1 ];then
    echo "Syntax:$0 interface"
    exit 1
fi

network_get_ipaddr ip $1
echo $ip

you can use like this

root@OpenWrt:/# chmod a+x test.sh
root@OpenWrt:/# ./test.sh lan
  1. Inquire L3 network device: network_get_device
#!/bin/sh

. /lib/functions/network.sh

if [ "$#" -ne 1 ];then
    echo "Syntax:$0 interface"
    exit 1
fi

network_get_device ifname $1
echo $ifname

you can use like this

root@OpenWrt:/# ./test.sh lan
br-lan
  1. Inquire the first IPv4 subnet of logical interface: network_get_subnet
#!/bin/sh

. /lib/functions/network.sh

if [ "$#" -ne 1 ];then
    echo "Syntax:$0 interface"
    exit 1
fi

network_get_subnet subnet $1
echo $subnet

you can use like this

root@OpenWrt:/# ./test.sh lan
192.168.10.1/24
  1. Inquire IPv4 gateway of logical interface: network_get_gateway
#!/bin/sh

. /lib/functions/network.sh

if [ "$#" -ne 1 ];then
    echo "Syntax:$0 interface"
    exit 1
fi

network_get_gateway gateway $1
echo $gateway

you can use like this

root@OpenWrt:/# ./test.sh lan
192.168.10.1
  1. Inquire DNS server of logical interface: network_get_dnsserver
#!/bin/sh

. /lib/functions/network.sh

if [ "$#" -ne 1 ];then
    echo "Syntax:$0 interface"
    exit 1
fi

network_get_dnsserver dnsserver $1
echo $dnsserver

you can use like this

root@OpenWrt:/# ./test.sh lan
192.168.10.1
  1. Inquire protocol of logical interface: network_get_protocol
#!/bin/sh

. /lib/functions/network.sh

if [ "$#" -ne 1 ];then
    echo "Syntax:$0 interface"
    exit 1
fi

network_get_protocol proto $1
echo $proto

you can use like this

root@OpenWrt:/# ./test.sh lan
static
  1. Inquire current state of logical interface: network_is_up
#!/bin/sh

. /lib/functions/network.sh

if [ "$#" -ne 1 ];then
    echo "Syntax:$0 interface"
    exit 1
fi

network_get_protocol $1

if [ $? = 0 ];then
    echo "up"
else
    echo "down"
fi

you can use like this

root@OpenWrt:/# ./test.sh lan
up
root@OpenWrt:/# ifconfig eth0 down
root@OpenWrt:/# ./test.sh lan
down

enjoy network.sh. 🙂

 

Advertisements
Posted in LEDE, OpenWRT | Leave a comment

Build LoRaWAN Gateway with Ci40 and iC880a

In this post I will show you how to setup an OpenWRT based LoRaWAN gateway with Ci40 and iC880a. It has a user-friedly interface to config parameters.

#Hareware#

  1. IMST iC880a SPI concentrator board
  2. Pigtail for antenna
  3. Ci40
  4. Power Supply 2A with micro USB
  5. MicroSD card
  6. RPi to iC8880a interface

Ci40 has a 40-pin header which is though not 100% compatible with Raspberry Pi. The header make it possible to connect lots of Raspberry Pi HATs such as this backplane from Tindie.

Pins

iC880a Description RPi pin Ci40 pin
21 Supply 5V 2 5v to Raspberry Pi board
22 GND 6 GND
13 Reset 22 MFIO_21
14 SPI CLK 23 SPI_M0_MCLK
15 SPI MISO 21 SPI_M0_MISO
16 SPI MOSI 19 SPI_M0_MOSI
17 SPI Nss 24 SPI_M0_CS2 (MFIO_28)

Setting up the software

OpenWrt Preparation

At first, please following the instruction to prepare the OpenWrt on SD card. I prefer the SD card boot not normal because I want to save lifecycles of NAND FLASH on my board.

you don’t need to compile your own OpenWrt with lora-feed. I have prepared compiled ipk packages for this tutorial. Downloading these packages and copy to the SD card.

Install LoRa packages

From computer log in your Ci40 from serial or ssh.

$ opkg install libc_1.1.15-1_pistachio.ipk
$ opkg install libloragw_5.0.1-1_pistachio.ipk
$ opkg install lora-gateway-tests_5.0.1-1_pistachio.ipk
$ opkg install lora-gateway-utils_5.0.1-1_pistachio.ipk
$ opkg install packet-forwarder-utils_4.0.1-1_pistachio.ipk
$ opkg packet-forwarder_4.0.1-1_pistachio.ipk
$ opkg luci-app-pkt-fwd_1.0-1_all.ipk

You have all the necessary programs to start your LoRa gateway.
In my configuration, I use global_conf.json only and it will be automatically generated by Lua script. So please do not edit this file. The default configuration is for basic EU686 without GPS and beacon supports. You can change the configuration from LuCI interface as you wish.

Start LoRa gateway

Run the packet-forwarder by

$ /etc/init.d/lora_pkg_fwd start

If you want to automatically start after boot, you can do it by

$ /etc/init.d/lora_pkg_fwd enable

Posted in Ci40, LEDE, LoRa, OpenWRT | Leave a comment

Meet /etc/preinit again

*/etc/preinit is the init program after Linux kernel booted. It is briefly introduced in OpenWRT/LEDE: System Boot Sequence. This time I will meet it again and dive into it to search some internals.

As a reference, the code of preinit is shown below. And it is assumed that $PREINIT is already initialized.

#!/bin/sh
# Copyright (C) 2006-2016 OpenWrt.org
# Copyright (C) 2010 Vertical Communications

[ -z "$PREINIT" ] && exec /sbin/init

export PATH="%PATH%"

pi_ifname=
pi_ip=192.168.1.1
pi_broadcast=192.168.1.255
pi_netmask=255.255.255.0

fs_failsafe_ifname=
fs_failsafe_ip=192.168.1.1
fs_failsafe_broadcast=192.168.1.255
fs_failsafe_netmask=255.255.255.0

fs_failsafe_wait_timeout=2

pi_suppress_stderr="y"
pi_init_suppress_stderr="y"
pi_init_path="%PATH%"
pi_init_cmd="/sbin/init"

. /lib/functions.sh
. /lib/functions/preinit.sh
. /lib/functions/system.sh

boot_hook_init preinit_essential
boot_hook_init preinit_main
boot_hook_init failsafe
boot_hook_init initramfs
boot_hook_init preinit_mount_root

for pi_source_file in /lib/preinit/*; do
    . $pi_source_file
done

boot_run_hook preinit_essential

pi_mount_skip_next=false
pi_jffs2_mount_success=false
pi_failsafe_net_message=false

boot_run_hook preinit_main

The first part includes three group of parameters. They are initilized for the functions which are located in hook list.

Then we come to five hook lists which are initilized by boot_hook_init which is located in /lib/function/boot.sh. The code of boot_hook_init is show below.

boot_hook_init() {          
    local hook="${1}_hook" # add suffix _hook to every hook lister name
    export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook"
    export -n "$hook="
}

The point of this function is the command export -n. The option -n means remove the export property from each NAME. It marks each NAME not export to the environment and child-env, but other functions in this shell can still use this variable.

for pi_source_file in /lib/preinit/*; do
    . $pi_source_file
done
}

preinit is then looking for and excute every files located /lib/preinit/. The name of such file is starting with a number and then the name of function. Each file has similar structure as

function_name1() {
    #STATEMENTS
}
...
function_name2() {
    #STATEMENTS
}
boot_hook_add hook_name function_name1
boot_hook_add hook_name function_name2

There can be multiple functions and later using boot_hook_add to add into a certain hook linker. So what happens in boot_hook_add is shown below

boot_hook_add() {
    local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}"
    local func="${2}"

    [ -n "$func" ] && {
        local v; eval "v=\$$hook"
        export -n "$hook=${v:+$v }$func" # add function to responding hook list
    }
}

After adding functions, preinit excutes the functions located in preinit_essential by

boot_run_hook preinit_essential

here the definition of boot_run_hook is shown below

boot_run_hook() {
    local hook="$1"
    local func
    while boot_hook_shift "$hook" func; do
        local ran; eval "ran=\$PI_RAN_$func"
        [ -n "$ran" ] || {
            export -n "PI_RAN_$func=1"
            $func "$1" "$2"  # excute the function
        }
    done
}

Here boot_hook_shift loops every functions in “$hook” until the last one. It returns 0 as true when there is still functions in “$hook” and returns 1 as false when reaching the end. The code of boot_hook_shift is shown below:

boot_hook_shift() {
    local hook="${1}_hook"
    local rvar="${2}"

    local v; eval "v=\$$hook" # $$hook includes all the registered functions
    [ -n "$v" ] && {
        local first="${v%% *}"  # take the first function every time

        [ "$v" != "${v#* }" ] && \
            export -n "$hook=${v#* }" || \
            export -n "$hook="

        export -n "$rvar=$first" # export the first function for other ones
        return 0
    }

    return 1
}
Posted in Ci40, LEDE, OpenWRT | Leave a comment

iC880A LoRaWAN Gateway with Ci40

2017-06-27 15.13.19

with my lora-feed https://github.com/xueliu/lora-feed.

I am working on LuCi web interface to config the gateway. I will show it later.

Posted in Ci40, LEDE, LoRa, OpenWRT, 未分类 | Leave a comment

[Dissecting OpenWRT]#2: netifd – main()

netifd is an RPC-capable daemon written in C for better access to kernel APIs with the ability to listen on netlink events.

In this series serveral articles will be written introduce the internal of netifd which is a core componet of OpenWRT/LEDE system for network management and its support for 802.11. As a normal Linux program, standing at main() function as a start point is a good choice.

The main() function of netidf is rather short and simple. It hides all the details in functions

int main(int argc, char **argv)
{
    const char *socket = NULL;
    int ch;

    global_argv = argv;

    while ((ch = getopt(argc, argv, "d:s:p:c:h:r:l:S")) != -1) {
        switch(ch) {
        case 'd':
            debug_mask = strtoul(optarg, NULL, 0);
            break;
        case 's':
            socket = optarg;
            break;
        case 'p':
            main_path = optarg;
            break;
        case 'c':
            config_path = optarg;
            break;
        case 'h':
            hotplug_cmd_path = optarg;
            break;
        case 'r':
            resolv_conf = optarg;
            break;
        case 'l':
            log_level = atoi(optarg);
            if (log_level >= ARRAY_SIZE(log_class))
                log_level = ARRAY_SIZE(log_class) - 1;
            break;
#ifndef DUMMY_MODE
        case 'S':
            use_syslog = false;
            break;
#endif
        default:
            return usage(argv[0]);
        }
    }

    if (use_syslog)
        openlog("netifd", 0, LOG_DAEMON);

    netifd_setup_signals();
    if (netifd_ubus_init(socket) < 0) {
        fprintf(stderr, "Failed to connect to ubus\n");
        return 1;
    }

    proto_shell_init();
    wireless_init();

    if (system_init()) {
        fprintf(stderr, "Failed to initialize system control\n");
        return 1;
    }

    config_init_all();

    uloop_run();
    netifd_kill_processes();

    netifd_ubus_done();

    if (use_syslog)
        closelog();

    return 0;
}

A brief flow chart is shown below

netifd main function

where

Posted in 未分类 | Leave a comment

Semtech LoRa packages for OpenWRT/LEDE

This article is still under construction

Recently I made a repo for latest Semtech Lora packages.

link

A donation of SX1301 gateway board is appreciate. With this I could do further development of UCI and Luci for Lora.

References:
2. https://github.com/JiapengLi/OpenWrt-lora.git

Posted in LEDE, LoRa, OpenWRT | Leave a comment

[Dissecting OpenWRT]#1: Preface

From today I would like to start a new series (挖坑) called “Dissecting OpenWRT” (深入浅出 OpenWRT). Unlike other tutorials about OpenWRT, this new series will try to dissect OpenWRT from a developer’s point of view. It will focus on the implementation of OpenWRT core components, such as ubus, procd, netifd and luci. I hope that with the help of this new series, people will not only understand the insider but also know how to extend OpenWRT.

System:
1. https://clockworkbird9.wordpress.com/2016/09/29/openwrtlede-system-boot-sequence/

WLAN:
1. https://clockworkbird9.wordpress.com/2017/02/05/openwrtlede-wifi-statup-process/

Posted in OpenWRT | Leave a comment