2 # Converts a uci config file into an appropriate mosquitto.conf snippet
3 # expected to be used in an init file to generate a config file to run from
4 # Karl Palsson <karlp@remake.is> 2012.
5 # Considered to be released into the public domain
7 [ -f $IPKG_INSTROOT/lib
/functions.sh
] && .
$IPKG_INSTROOT/lib
/functions.sh
9 TCONF
=/tmp
/mosquitto.generated.$$.conf
10 while getopts "f:" o
; do
18 if [ -e $TCONF ]; then
19 echo "Odd, same temporary generated config file already existed: $TCONF"
23 echo "Generating mosquitto config file in $TCONF"
25 echo "# mosquitto.conf file generated from UCI config." >>$TCONF
26 echo "# Config snippet generated by $0 on $NOW" >>$TCONF
29 # Usage: append_if cfg uci_name output_name
30 # add a config line of the form "output_name <value>"
31 # if the "uci_name" was found.
32 # output_name defaults to uci_name if not specified.
37 if [ -z "$out_name" ]; then
40 config_get val
$cfg $uci_name
41 if [ -n "$val" ]; then
42 echo "$out_name $val" >> $TCONF
46 # mosquitto uses true/false, uci uses 1/0
47 # note that this is not shell truthy, but equality with 1!
50 echo "$1 true" >> $TCONF
52 echo "$1 false" >> $TCONF
56 # as per append_if, but gets the value as a uci bool, not raw
57 append_optional_bool
() {
61 config_get val
$cfg $uci_name
62 if [ -n "$val" ]; then
63 config_get_bool real
$cfg $uci_name
64 append_bool
$out_name $real
69 config_get destinations
"$1" log_dest
70 for dest
in $destinations; do
71 echo "log_dest $dest" >> $TCONF
74 config_get_bool no_remote
"$1" no_remote_access
0
75 if [ "$no_remote" -eq 1 ]; then
76 echo "bind_address 127.0.0.1" >> $TCONF
79 config_get port
"$1" port
1883
80 echo "port $port" >> $TCONF
81 append_if
"$1" protocol
82 append_if
"$1" max_inflight_messages
83 append_if
"$1" max_queued_messages
89 config_get port
"$1" port
90 if [ -z "$port" ]; then
91 echo "Ignoring listener section without port"
94 config_get_bool no_remote
"$1" no_remote_access
0
95 if [ "$no_remote" -eq 1 ]; then
96 echo "listener $port 127.0.0.1" >> $TCONF
98 echo "listener $port" >> $TCONF
101 append_if
"$1" protocol
105 echo "topic $1" >> $TCONF
109 config_get conn
"$1" connection
110 config_get addr
"$1" address
111 if [ -z "$conn" -o -z "$addr" ]; then
112 echo "Ignoring bridge section, misisng connection/address"
116 echo "# Bridge connection from UCI section" >> $TCONF
117 append_if
"$1" connection
118 append_if
"$1" address
120 config_list_foreach
"$1" topic add_topic
121 append_optional_bool
"$1" cleansession cleansession
122 append_optional_bool
"$1" try_private try_private
124 append_if
"$1" clientid
125 append_if
"$1" identity bridge_identity
126 append_if
"$1" psk bridge_psk
127 append_if
"$1" tls_version bridge_tls_version
131 config_load
"mosquitto"
132 config_foreach mosq_general
"mosquitto"
133 config_foreach add_listener
"listener"
134 config_foreach add_bridge
"bridge"