APUE course source code
guowenxue
20 hours ago ff970152123acde6084e9de1b3bad134922fc9c2
updte iotd to support deb package
4 files modified
2 files added
171 ■■■■■ changed files
.gitignore 4 ●●●● patch | view | raw | blame | history
project/4.mqttd/deb/build.sh 128 ●●●●● patch | view | raw | blame | history
project/4.mqttd/deb/makefile 16 ●●●●● patch | view | raw | blame | history
project/4.mqttd/etc/iotd.conf 2 ●●● patch | view | raw | blame | history
project/4.mqttd/iotd.c 8 ●●●● patch | view | raw | blame | history
project/4.mqttd/makefile 13 ●●●● patch | view | raw | blame | history
.gitignore
@@ -14,6 +14,10 @@
cscope.*
tags
# deb package
project/4.mqttd/deb/*d
*.deb
# running files
*.log
*.db
project/4.mqttd/deb/build.sh
New file
@@ -0,0 +1,128 @@
#!/bin/bash
set -e
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
APP_NAME=$(basename "${PRJ_PATH}")
VERSION=1.0
ARCH=armhf
do_export() {
    echo $PRJ_PATH
    OUTPUT_DEB="${APP_NAME}.deb"
    BUILD_DIR="${APP_NAME}"
    BINARY_PATH="${PRJ_PATH}/${APP_NAME}"
    CONF_PATH="${PRJ_PATH}/etc/${APP_NAME}.conf"
    SERVICE_PATH="${PRJ_PATH}/etc/${APP_NAME}.service"
    DEBIAN_DIR="${BUILD_DIR}/DEBIAN"
}
# Clean all build artifacts
do_clean() {
    rm -rf ${BUILD_DIR} ${OUTPUT_DEB}
}
do_fetch() {
    # Validate required input files
    if [ ! -f ${BINARY_PATH} ]; then
        echo "ERROR: Binary not found at ${BINARY_PATH}" >&2
        exit 1
    fi
    if [ ! -f ${CONF_PATH} ]; then
        echo "ERROR: Config file not found at ${CONF_PATH}" >&2
        exit 1
    fi
    if [ ! -f ${SERVICE_PATH} ]; then
        echo "ERROR: Service file not found at ${SERVICE_PATH}" >&2
        exit 1
    fi
    # ensure clean state before fetch
    do_clean
    mkdir -p ${DEBIAN_DIR}
    install -D -m 755 ${BINARY_PATH} ${BUILD_DIR}/usr/bin/${APP_NAME}
    install -D -m 644 ${CONF_PATH} ${BUILD_DIR}/etc/${APP_NAME}.conf
    install -D -m 644 ${SERVICE_PATH} ${BUILD_DIR}/lib/systemd/system/${APP_NAME}.service
}
do_conf() {
    cat > ${DEBIAN_DIR}/control <<EOF
Package: ${APP_NAME}
Version: ${VERSION}
Section: utils
Priority: optional
Architecture: ${ARCH}
Maintainer: Guowenxue <guowenxue@gmail.com>
Description: Custom ${APP_NAME} daemon for Raspberry Pi
 This package provides the ${APP_NAME} service.
Depends: systemd
EOF
    cat > ${DEBIAN_DIR}/postinst <<EOF
#!/bin/sh
set -e
if [ "\$1" = "configure" ]; then
    systemctl daemon-reload
    systemctl enable --now ${APP_NAME}.service
fi
EOF
    chmod +x ${DEBIAN_DIR}/postinst
    cat > ${DEBIAN_DIR}/prerm <<EOF
#!/bin/sh
set -e
if [ "\$1" = "remove" ]; then
    systemctl stop ${APP_NAME}.service || true
    systemctl disable ${APP_NAME}.service || true
fi
EOF
    chmod +x ${DEBIAN_DIR}/prerm
    echo /etc/${APP_NAME}.conf > ${DEBIAN_DIR}/conffiles
}
do_build() {
    dpkg-deb --build ${BUILD_DIR} ${OUTPUT_DEB}
}
# Parse command-line options
do_args() {
    local clean_only=0
    while getopts "c" opt; do
        case $opt in
            c)
                clean_only=1
                ;;
            \?)
                echo "Usage: $0 [-c] [APP_NAME]" >&2
                exit 1
                ;;
        esac
    done
    shift $((OPTIND - 1))
    if [ $# -gt 0 ]; then
        APP_NAME="$1"
        PRJ_APP=$PRJ_PATH/${APP_NAME}
    fi
    do_export
    if [ "$clean_only" = "1" ]; then
        do_clean
        exit
    fi;
}
main() {
    do_args "$@"
    do_fetch
    do_conf
    do_build
}
main "$@"
project/4.mqttd/deb/makefile
New file
@@ -0,0 +1,16 @@
APP_NAME?=iotd
PRJ_PATH=$(shell pwd)
DEB_FILES := $(wildcard *.deb)
DEB_DIRS  := $(patsubst %.deb,%,$(DEB_FILES))
all:
    @./build.sh ${APP_NAME}
clean:
    @./build.sh -c ${APP_NAME}
distclean: clean
    @rm -rf ${DEB_FILES} ${DEB_DIRS}
project/4.mqttd/etc/iotd.conf
@@ -47,7 +47,7 @@
[publisher]
# mosquitto_sub -h weike-iot.com -p 8013 -u lingyun -P lingyun -t \$Sys/Studio/Uplink/rpi001
# mosquitto_pub -h weike-iot.com -p 8013 -u lingyun -P lingyun -t \$Sys/Studio/Uplink/rpi001 -m '{"temperature":66.66, "humidity":88.8}'
pubTopic="$Sys/Studio/Uplink"
pubQos=0
project/4.mqttd/iotd.c
@@ -230,9 +230,9 @@
            if( !item )
                continue;
            if( strcasecmp(item->valuestring, "on") )
            if( !strcasecmp(item->valuestring, "on") )
                turn_relay(which, ON);
            else if( strcasecmp(item->valuestring, "off") )
            else if( !strcasecmp(item->valuestring, "off") )
                turn_relay(which, OFF);
        }
    }
@@ -246,9 +246,9 @@
            if( !item )
                continue;
            if( strcasecmp(item->valuestring, "on") )
            if( !strcasecmp(item->valuestring, "on") )
                turn_led(which, ON);
            else if( strcasecmp(item->valuestring, "off") )
            else if( !strcasecmp(item->valuestring, "off") )
                turn_led(which, OFF);
        }
    }
project/4.mqttd/makefile
@@ -12,7 +12,9 @@
#*******************************************************************************
PRJ_PATH=$(shell pwd)
APP_NAME = iotd
APP_NAME=iotd
DEB_NAME=${APP_NAME}.deb
DEB_PATH=${PRJ_PATH}/deb
BUILD_ARCH=$(shell uname -m)
ifneq ($(findstring $(BUILD_ARCH), "x86_64" "i386"),)
@@ -39,12 +41,13 @@
# libraries
libs=openlibs ${SRCS}
LDFLAGS+=-lmosquitto -lcjson -lssl -lcrypto -lgpiod
LDFLAGS+=-lmosquitto -lcjson -lssl -lcrypto -lgpiod -lm
LDFLAGS+=-lpthread
all: entry subdir
    ${CROSS_COMPILE}gcc ${CFLAGS} ${SRCFILES} -o ${APP_NAME} ${LDFLAGS}
    @make gendeb
entry:
    @echo "Building ${APP_NAME} on ${BUILD_ARCH}"
@@ -52,11 +55,15 @@
subdir:
    @for dir in ${libs} ;  do CFLAGS="${CFLAGS}" make -C $${dir} ; done
gendeb:
    @make -C ${DEB_PATH} APP_NAME=${APP_NAME}
install:
    cp ${APP_NAME} /tftp
    cp ${APP_NAME} ${DEB_PATH}/${DEB_NAME} /tftp
clean:
    @for dir in ${SRCS} ; do if [ -e $${dir} ] ; then make clean -C $${dir}; fi; done
    @make clean -C ${DEB_PATH} APP_NAME=${APP_NAME}
    @rm -f ${APP_NAME}
distclean: