[WiFi Widora] Hướng dẫn sử dụng OpenWrt trên Gateway Widora BIT3.0 MT7688AN

Bước 1: Tải SDK-Openwrt 

1/ Chọn version phù hợp với kernel-openwrt trên board ở trang https://downloads.openwrt.org/releases/

2/ Ở đây mình board mình chạy openwrt 18.06, nên mình sẽ tải SDK version 18.06

(https://downloads.openwrt.org/releases/18.06.0/targets/ramips/mt76x8/)

Bước 2: Cách tạo package để build một chương trình C

1/ Tạo cây thư mục như bên dưới :

SDK-openwrt/package

   +helloworld              # Tên package

        -Makefile             # Makefile mô tả cho package

        +src

            -Makefile         # Makefile để build chương trình C

            -helloworld.c   # C/C++ source code

2/ Thư mục ‘src’ chứa file cMakefile

  • /helloworld/src/helloworld.c mình viết một chương trình đơn giản như sau:

#include <stdio.h>

int main(void)

{

            printf("Hello World ! This is the first function !\n");

            return 0;

}

3/ ~/helloworld/src/Makefile chúng ta sẽ viết các lệnh để build chương trình C như sau:

#build helloworld

helloworld: helloworld.o

            $(CC) $(LDFLAGS) helloworld.o -o helloworld

helloworld.o: helloworld.c

            $(CC) $(CFLAGS) -c helloworld.c

#remove object files

clean:

            rm *.o helloworld

Với Makefile ở trên chúng ta sẽ build được file object cho chương trình helloworld.c của chúng ta chạy trên máy tính. Để build chương trình helloworld.c chạy được trên board Widora chúng ta cần thêm một chương trình Makefile dành cho openwrt-sdk biết được để build chương trình của chúng ta.

4/ ~/ SDK-openwrt /package/helloworld/Makefile:

 #######################################################################

include $(TOPDIR)/rules.mk

 

# Name and release number of this package

PKG_NAME:=helloworld

PKG_RELEASE:=1.0.0

PKG_VERSION:=1

# The root build directory, $(BUILD_DIR), is by default the build_mipsel

# directory in your OpenWrt SDK directory

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

# Specify package information for this program.

define Package/helloworld

            SECTION:=utils

            CATEGORY:=Utilities

endef

# Specify what needs to be done to prepare for building the package.

# In our case, we need to copy the source files to the build directory.

# This is NOT the default.  The default uses the PKG_SOURCE_URL and the

# PKG_SOURCE which is not defined here to download the source from the web.

define Build/Prepare

            mkdir -p $(PKG_BUILD_DIR)

            $(CP) ./src/* $(PKG_BUILD_DIR)/

endef

 

# Specify where and how to install the program. Since we only have one file,

# the helloworld executable, install it by copying it to the /bin directory on

# the router. The $(1) variable represents the root directory on the router running

# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install

# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the

# command to copy the binary file from its current location (in our case the build

# directory) to the install directory.

define Package/helloworld/install

            $(INSTALL_DIR) $(1)/bin

            $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/

endef

 

# This line executes the necessary commands to compile our program.

# The above define directives specify all the information needed, but this

# line calls BuildPackage which in turn actually uses this information to

# build a package.

$(eval $(call BuildPackage,helloworld))

########################################################################

Như vậy chúng ta đã thao tác xong những việc cần thiết để chuẩn bị cho việc biên dịch một chương trình C.

Bước 3: Biên dịch chương trình

1/ Ở thư mục SDK, chúng ta chạy lệnh sau:

-> Cú pháp : $ make /package/packagename/compile -j1

Ví dụ : $ make /package/helloworld/compile -j1

2/ Kết quả như trên là xong, chúng ta sẽ thu được một file có tên helloworld_1.0.0-1_mipsel_24kc.ipk trong đường dẫn: ~SDK-openwrt/bin/packages/mipsel_24kc/base/

Bước 4: Cách thực thi chương trình C trên board Gateway Wifi Widora

1/ Copy file .ipk đã được build tới board Widora.

-> Cú pháp: $ scp filename.ipk root@ip_board:/root

Ví dụ: $ scp helloworld_1.0.0-1_mipsel_24kc.ipk root@192.168.1.13

2/ Cài đặt chương trình lên board

-> Cú pháp: $ opkg install filename.ipk

Ví dụ: $ opkg install helloworld_1.0.0-1_mipsel_24kc.ipk

-> Để cài đặt lại chương trình chúng ta thêm ‘--force-reinstall’ vào sau cú pháp trên.

Ví dụ: $ opkg install helloworld_1.0.0-1_mipsel_24kc.ipk --force-reinstall

 

Như vậy ta có app C “helloworld” trong thư mục /bin/ . Chạy thử chương trình.

Xong ! Như vậy chúng ta đã biết cách để build và chạy một chương trình C trên board Gateway Wifi Widora.

Hy vọng bài viết này sẽ giúp ích cho các bạn. Nếu các bạn có những thắc mắc cần giải đáp, hãy nhắn tin cho mình qua Fanpage: EPCB hoặc qua Email: epcbtech@gmail.com.

Cảm ơn các bạn đã theo dõi bài viết và chúc các bạn thành công.

Bình luận