【T113】buildroot增加app
文章目录新增app源码monitor_usb.cMakefilebuildroot 配置Config.inmonitor_usb.mkplatform/config/buildroot/Config.inplatform/config/buildroot/platform.mkbuildroot/buildroot-201902/configs/sun8iw20p1_t113_defconfig配置程序为默认启动S92monitor_usb.sh原文链接https://lixiaogang03.github.io/2023/12/05/Buildroot-Add-App/新增app源码目录t113_linux/platform/apps/monitor_usb$ tree.├── Makefile └── monitor_usb.cmonitor_usb.c#includeunistd.h#includestdio.h#includestring.h#includelibudev.h#includestdlib.h#includesys/reboot.h/** * swupdate ota */voidswupdate_ota(){constchar*filename/mnt/usb/sda1/update.swu;intresultaccess(filename,F_OK);if(result0){intresponsesystem(swupdate -v -i /mnt/usb/sda1/update.swu -e stable,now_A_next_B);if(response-1){printf(Failed to execute swupdate command\n);}else{printf(Rebooting the device...\n);sync();// 同步磁盘上的数据if(reboot(RB_AUTOBOOT)-1){printf(Failed to reboot the system\n);}}}else{printf(/mnt/usb/sda1/update.swu does not exits\n);}}intmain(){structudev*udev;structudev_monitor*mon;structudev_device*dev;intfd;/* 初始化 udev */udevudev_new();if(!udev){fprintf(stderr,Cant create udev\n);return1;}/* 创建 udev 监听器 */monudev_monitor_new_from_netlink(udev,udev);if(!mon){fprintf(stderr,Cant create udev monitor\n);return1;}/* 设置要监听的事件类型 */if(udev_monitor_filter_add_match_subsystem_devtype(mon,block,disk)0||udev_monitor_enable_receiving(mon)0){fprintf(stderr,Cant filter block subsystem or enable receiving\n);return1;}// 获取文件描述符fdudev_monitor_get_fd(mon);/* 循环监听并处理事件 */while(1){// 使用 select 系统调用来等待设备事件或超时fd_set fds;FD_ZERO(fds);FD_SET(fd,fds);structtimevaltimeout;timeout.tv_sec5;// 设置超时时间为 5 秒timeout.tv_usec0;// 使用 select() 设置超时时间if(select(fd1,fds,NULL,NULL,timeout)0){printf(Error waiting for device event\n);continue;}if(FD_ISSET(fd,fds)){// 当设备事件发生时从监视器接收设备对象devudev_monitor_receive_device(mon);if(devNULL){printf(Invalid device received from udev monitor\n);continue;}constchar*actionudev_device_get_action(dev);constchar*devpathudev_device_get_devnode(dev);printf(Action: %s, Device Path: %s\n,action,devpath);if(strcmp(add,action)0){swupdate_ota();}udev_device_unref(dev);}else{// 没有设备事件发生// printf(No device events received\n);}}/* 清理资源 */udev_monitor_unref(mon);udev_unref(udev);return0;}MakefileOBJSmonitor_usb.o OUT_BINmonitor_usb all:$(OBJS)$(CC)$(LDFLAGS)-g-o$(OUT_BIN)$^ %.o: %.c$(CC)$(CFLAGS)-c-o$$.PHONY: clean clean:rm-rf*.orm-rf$(OUT_BIN)rm-rf$(shellfind-name*.d)buildroot 配置t113_linux/platform/config/buildroot/monitor_usb$ tree.├── Config.in └── monitor_usb.mkConfig.inconfig BR2_PACKAGE_MONITOR_USB boolmonitor_usbselectBR2_PACKAGE_EUDEVhelpmonitor usbmonitor_usb.mk################################################################################## monitor usb#################################################################################MONITOR_USB_SITE_METHODlocalMONITOR_USB_SITE$(PLATFORM_PATH)/../../apps/monitor_usb MONITOR_USB_LICENSEGPLv2, GPLv3 MONITOR_USB_LICENSE_FILESCopyright COPYING MONITOR_USB_DEPENDENCIESudev MONITOR_USB_LDFLAGS-L$(TARGET_DIR)/usr/lib/-ludevdefine MONITOR_USB_BUILD_CMDS$(MAKE)CC$(TARGET_CC)CFLAGS$(MONITOR_USB_CFLAGS)\LDFLAGS$(MONITOR_USB_LDFLAGS)-C$(D)all endef define MONITOR_USB_INSTALL_TARGET_CMDS$(INSTALL)-D-m0755$(D)/monitor_usb$(TARGET_DIR)/usr/bin endef$(eval$(generic-package))platform/config/buildroot/Config.insource../../platform/config/buildroot/monitor_usb/Config.inplatform/config/buildroot/platform.mkinclude${PLATFORM_PATH}/monitor_usb/monitor_usb.mkbuildroot/buildroot-201902/configs/sun8iw20p1_t113_defconfigBR2_PACKAGE_MONITOR_USBy配置程序为默认启动t113_linux/platform/framework/auto/rootfs/etc/init.d$ tree.├── rcK ├── rcS ├── S01syslogd ├── S02klogd ├── S05mount_userdata ├── S10udev ├── S20urandom ├── S30dbus ├── S40network ├── S50bluetooth ├── S50postgresql ├── S52_4G-Daemon.sh ├── S80dnsmasq ├── S90alsa.sh └── S92monitor_usb.shS92monitor_usb.sh#!/bin/shcase$1instart)echoStarting monitor_usb/usr/bin/monitor_usb;;stop)echoStopping monitor_usbkillallmonitor_usb;;restart)$0stop$0start;;*)echoUsage:$0{start|stop|restart}exit1;;esacexit0