【T113】buildroot增加app
文章目录
- 新增app源码
- monitor_usb.c
- Makefile
- buildroot 配置
- Config.in
- monitor_usb.mk
- platform/config/buildroot/Config.in
- platform/config/buildroot/platform.mk
- buildroot/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
#include<unistd.h>#include<stdio.h>#include<string.h>#include<libudev.h>#include<stdlib.h>#include<sys/reboot.h>/** * swupdate ota */voidswupdate_ota(){constchar*filename="/mnt/usb/sda1/update.swu";intresult=access(filename,F_OK);if(result==0){intresponse=system("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 */udev=udev_new();if(!udev){fprintf(stderr,"Can't create udev\n");return1;}/* 创建 udev 监听器 */mon=udev_monitor_new_from_netlink(udev,"udev");if(!mon){fprintf(stderr,"Can't 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,"Can't filter block subsystem or enable receiving\n");return1;}// 获取文件描述符fd=udev_monitor_get_fd(mon);/* 循环监听并处理事件 */while(1){// 使用 select 系统调用来等待设备事件或超时fd_set fds;FD_ZERO(&fds);FD_SET(fd,&fds);structtimevaltimeout;timeout.tv_sec=5;// 设置超时时间为 5 秒timeout.tv_usec=0;// 使用 select() 设置超时时间if(select(fd+1,&fds,NULL,NULL,&timeout)<0){printf("Error waiting for device event\n");continue;}if(FD_ISSET(fd,&fds)){// 当设备事件发生时,从监视器接收设备对象dev=udev_monitor_receive_device(mon);if(dev==NULL){printf("Invalid device received from udev monitor\n");continue;}constchar*action=udev_device_get_action(dev);constchar*devpath=udev_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;}Makefile
OBJS=monitor_usb.o OUT_BIN=monitor_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.in
config BR2_PACKAGE_MONITOR_USB bool"monitor_usb"selectBR2_PACKAGE_EUDEVhelpmonitor usbmonitor_usb.mk
################################################################################## monitor usb#################################################################################MONITOR_USB_SITE_METHOD=localMONITOR_USB_SITE=$(PLATFORM_PATH)/../../apps/monitor_usb MONITOR_USB_LICENSE=GPLv2+, GPLv3+ MONITOR_USB_LICENSE_FILES=Copyright COPYING MONITOR_USB_DEPENDENCIES=udev 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.in
source"../../platform/config/buildroot/monitor_usb/Config.in"platform/config/buildroot/platform.mk
include${PLATFORM_PATH}/monitor_usb/monitor_usb.mkbuildroot/buildroot-201902/configs/sun8iw20p1_t113_defconfig
BR2_PACKAGE_MONITOR_USB=y配置程序为默认启动
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"$1"instart)echo"Starting monitor_usb"/usr/bin/monitor_usb&;;stop)echo"Stopping monitor_usb"killallmonitor_usb;;restart)$0stop$0start;;*)echo"Usage:$0{start|stop|restart}"exit1;;esacexit0