Openwrt builds Hello ipk

In our practical work, we will develop our own functional modules

For Openwrt, its convenience is that it can be installed and uninstalled at any time like ko file

For Openwrt, there are also a set of new steps of "standard":

1. Add module directory under package directory: hello

2. Add the corresponding file, like this

Files: store configuration files and startup scripts

Configuration file: the defined content is uci format

config globals 'globe'
	option agent 'openwrt'
	option url 'http://192.168.100.115:8080'
	option delay '10'

Source code by calling custom API

Save configuration to

int read_conf( struct Hello *hello)
{
    struct uci_context *ctx = uci_alloc_context(); 
    if (!ctx)
    {
        fprintf(stderr, "No memory\n");
        return 1;
    }

    getValue(ctx, "agent", hello->agent, sizeof(hello->agent)); 
    getValue(ctx, "url", hello->url, sizeof(hello->url)); 
    char delay[20];
    getValue(ctx, "delay", delay, sizeof(delay)); 
    hello->delay = atoi(delay); 

    uci_free_context(ctx); 
    return 0;
}
 

Later, you can operate through the structure

Startup script:

#!/bin/sh /etc/rc.common
 
USE_PROCD=1
START=15
STOP=85
PROG=/bin/hello
 
validate_hello_section()
{
	uci_validate_section hello globals globe \
	'delay:range(1,200)' 
}
 
start_service() {
	echo "start HelloRoute!"
	validate_hello_section || {
	echo "hello validattion failed!"
	return 1
	}
 
	procd_open_instance
	procd_set_param command "$PROG" –f -w bjbook.net
	procd_set_param respawn
	procd_close_instance
}
 
service_triggers()
{
	procd_add_reload_trigger "hello"
}
 
reload_service()
{
	stop
	start
}

Start and stop variables: determine the order of script execution

start(), stop(): decide how to start and stop the service. We directly call the Hello module here, so the boot will enable hello

custom(): the extension command is used. It is not used here for the time being

src: store the source code and the corresponding Makefile (only for compiling the source code)

all:
	$(CC) $(CFLAGS) -luci hello.c -o hello

clean:
	rm *.o hello

 

Top level Makefile: contains compilation and installation instructions to control the compilation and generation of installation package of code in openwrt environment

        1. mk file is introduced to define the name and version of the software package

include $(TOPDIR)/rules.mk 
 
PKG_NAME:=hello
PKG_RELEASE:=1.0

        2. Set the software package classification so that we can find it in the corresponding category when making menuconfig

Set the dependent variable libuci so that the source code can call the UCI interface

Syntax: use: + library name, which means that if a software package is selected, the library will be automatically selected

define Package/$(PKG_NAME)
	SECTION:=net
	CATEGORY:=Network
	TITLE:=Hello utility
	DEPENDS:=+libuci
	URL:=https://blog.csdn.net/mrbone9?spm=1008.2028.3001.5343
	MAINTAINER:=31642407@qq.com
endef

        3. Compilation part: create a compilation directory and copy the code to this directory

                build_dir/target-i386_pentium4_musl/hello

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

        5. The first parameter passed in is $(1)

define Package/$(PKG_NAME)/install
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/hello.conf $(1)/etc/config/hello
	
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/hello.init $(1)/etc/init.d/hello
	
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hello $(1)/usr/sbin/hello
endef

         6. pack

$(eval $(call BuildPackage,$(PKG_NAME)))

 

compile:

When the files are ready, you can start compiling. Remember that we set the category, and then you can select the module through the menu

$ make menuconfig

     

$ make package/hello/compile V=s

After completion, you can see the generated software package

Installation:

Copy ipk to OS running Openwrt

 

Uninstall:

The installed ipk can view the actual name through the following command

Call opkg to unload

opkg is a software package management tool. You can enter to view specific commands

Through the above steps, we can build our own software package. The operation part is simple

The following chapters will study the code framework, UCI, ubus, procd and other contents

 

Keywords: openwrt

Added by Sanoz0r on Thu, 10 Feb 2022 11:52:32 +0200