You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
7.4 KiB

  1. #!/bin/sh /etc/rc.common
  2. USE_PROCD=1
  3. START=99
  4. STOP=01
  5. CONFIGURATION=dcwapd
  6. VERBOSE=1
  7. # NOTE: all functions write the result to the $result variable
  8. result=
  9. get_channelsets()
  10. {
  11. # default to empty
  12. result=
  13. channelsets=$(uci show $CONFIGURATION | grep "=channel-set$")
  14. for channelset in $channelsets; do
  15. channelset=$(echo "$channelset" | sed -rn "s/$CONFIGURATION\.(.*)=.*/\1/p")
  16. result="$result $channelset"
  17. done
  18. if [ $VERBOSE -eq 1 ]; then
  19. echo "Channel Sets: $result" 2>&1 | logger
  20. fi
  21. }
  22. # $1 : the channel set name
  23. get_channelset_enabled()
  24. {
  25. # default to disabled
  26. result=0
  27. if [ -n "$1" ]; then
  28. result=$(uci get $CONFIGURATION."$1".enabled)
  29. fi
  30. if [ $VERBOSE -eq 1 ]; then
  31. echo "Channel Set \"$1\" Enabled: $result" 2>&1 | logger
  32. fi
  33. }
  34. # $1 : the channel set name
  35. get_primary_bridge()
  36. {
  37. result=
  38. if [ -n "$1" ]; then
  39. result=$(uci get $CONFIGURATION."$1".bridge)
  40. fi
  41. if [ $VERBOSE -eq 1 ]; then
  42. echo "Channel Set \"$1\" Primary Bridge: $result" 2>&1 | logger
  43. fi
  44. }
  45. # $1 : the channel set name
  46. get_datachannels()
  47. {
  48. # default to empty
  49. result=
  50. if [ -n "$1" ]; then
  51. result=$(uci get $CONFIGURATION."$1".data_channels)
  52. fi
  53. if [ $VERBOSE -eq 1 ]; then
  54. echo "Channel Set \"$1\" Data Channels: $result" 2>&1 | logger
  55. fi
  56. }
  57. # $1 : the wlan interface name
  58. get_wifi_iface_num()
  59. {
  60. result=
  61. if [ -n "$1" ];then
  62. #result=$(echo "$1" | sed -n "s/wlan//p")
  63. result=$(echo "$1" | sed -rn "s/wlan([0-9]*).*/\1/p")
  64. fi
  65. }
  66. # $1 : the bridge name
  67. get_bridge_network_name()
  68. {
  69. result=
  70. if [ -n "$1" ];then
  71. result=$(echo "$1" | sed -n "s/br-//p")
  72. fi
  73. }
  74. # $1 : the wlan interface name
  75. set_iface_init_state()
  76. {
  77. result=
  78. if [ -n "$1" ]; then
  79. iface=$1
  80. # need to extract the "X" from wlanX
  81. get_wifi_iface_num "$iface"
  82. iface_num=$result
  83. if [ -n "$iface_num" ]; then
  84. # get the iface network
  85. init_net=$(uci get wireless.@wifi-iface[$iface_num].network)
  86. if [ -n "$init_net" ]; then
  87. # if the iface network is a bridge, but doesn't start with "br-"
  88. # I think we need to prepend it?
  89. net_type=$(uci get network."$init_net".type)
  90. if [ -n "$net_type" ] && [ "$net_type" = "bridge" ]; then
  91. prefix_ok=$(echo "$init_net" | grep "^br-")
  92. if [ -z "$prefix_ok" ]; then
  93. init_net="br-$init_net"
  94. fi
  95. fi
  96. fi
  97. # make sure that the init_net section exists
  98. init_net_section=$(uci get dcwapd.init_net)
  99. if [ "$init_net_section" != "init_net" ]; then
  100. # the section did not exist
  101. uci set dcwapd.init_net=init_net
  102. fi
  103. # save the initial network
  104. if [ $VERBOSE -eq 1 ]; then
  105. echo "Saving '$iface' initial network '$init_net'" 2>&1 | logger
  106. fi
  107. uci set $CONFIGURATION.init_net."$iface"="$init_net"
  108. uci commit
  109. # save the initial network in the result variable
  110. result=$init_net
  111. fi
  112. fi
  113. }
  114. # $1 : the wlan interface name
  115. get_iface_init_state()
  116. {
  117. result=
  118. if [ -n "$1" ];then
  119. init_net=$(uci get $CONFIGURATION.init_net."$iface")
  120. # if the response starts with "uci: ", it was an error not the real result
  121. err=$(echo "$init_net" | grep "^uci: ")
  122. if [ -z "$err" ]; then
  123. # no error, set the result
  124. result=$init_net
  125. if [ $VERBOSE -eq 1 ]; then
  126. echo "Got '$iface' initial network '$init_net'" 2>&1 | logger
  127. fi
  128. fi
  129. fi
  130. }
  131. # $1 : the name of the data channel name to bring up
  132. datachannel_up()
  133. {
  134. if [ -n "$1" ]; then
  135. bridge=$(uci get $CONFIGURATION."$1".bridge)
  136. interfaces=$(uci get $CONFIGURATION."$1".interfaces)
  137. if [ $VERBOSE -eq 1 ]; then
  138. echo "Creating Data Channel Bridge: $bridge" 2>&1 | logger
  139. fi
  140. get_bridge_network_name "$bridge"
  141. netname=$result
  142. if [ -n "$netname" ]; then
  143. uci set network."$netname"=interface
  144. uci set network."$netname".type=bridge
  145. uci set network."$netname".proto=static
  146. uci set network."$netname".bridge_empty='1'
  147. fi
  148. # create the bridge
  149. uci commit
  150. /etc/init.d/network reload
  151. for iface in $interfaces; do
  152. # if iface is in a bridge, the bridge name should be stored in result
  153. set_iface_init_state "$iface"
  154. init_bridge=$result
  155. # update uci with the new bridge info
  156. get_wifi_iface_num "$iface"
  157. iface_num=$result
  158. if [ -n "$iface_num" ]; then
  159. uci set wireless.@wifi-iface[$iface_num].network="$netname"
  160. fi
  161. # manually put the interface into the data bridge
  162. # if iface is in a bridge, remove it before adding it to the data bridge
  163. if [ -n "$init_bridge" ]; then
  164. brctl delif "$init_bridge" "$iface" 2>&1 | logger
  165. fi
  166. brctl addif "$bridge" "$iface" 2>&1 | logger
  167. done
  168. # commit uci changes and reload the network
  169. uci commit
  170. /etc/init.d/network reload
  171. #/etc/init.d/network restart
  172. # while [ 1 ]; do
  173. # ifconfig "$bridge" > /dev/null 2>&1
  174. # if [ $? == 0 ]; then
  175. # break;
  176. # fi
  177. # sleep 1
  178. # done
  179. fi
  180. }
  181. # $1 : the name of the data channel to bring down
  182. datachannel_down()
  183. {
  184. if [ -n "$1" ]; then
  185. bridge=$(uci get $CONFIGURATION."$1".bridge)
  186. interfaces=$(uci get $CONFIGURATION."$1".interfaces)
  187. for iface in $interfaces; do
  188. if [ $VERBOSE -eq 1 ]; then
  189. echo "Deconfiguring Data Channel Interface: $iface" 2>&1 | logger
  190. fi
  191. # manually remove the interface from the data bridge
  192. brctl delif "$bridge" "$iface" 2>&1 | logger
  193. get_iface_init_state "$iface"
  194. init_bridge=$result
  195. if [ -n "$init_bridge" ]; then
  196. # manually move the interface back to the original bridge
  197. brctl addif "$init_bridge" "$iface" 2>&1 | logger
  198. # update uci with the new bridge and interface configuration
  199. get_wifi_iface_num "$iface"
  200. iface_num=$result
  201. get_bridge_network_name "$init_bridge"
  202. netname=$result
  203. if [ -n "$iface_num" ] && [ -n "$netname" ]; then
  204. uci set wireless.@wifi-iface[$iface_num].network="$netname"
  205. fi
  206. fi
  207. done
  208. if [ $VERBOSE -eq 1 ]; then
  209. echo "Deconfiguring Data Channel Bridge: $bridge" 2>&1 | logger
  210. fi
  211. # delete the bridge from uci
  212. get_bridge_network_name "$bridge"
  213. netname=$result
  214. if [ -n "$netname" ]; then
  215. uci delete network."$netname"
  216. fi
  217. # commit uci changes and reload the network
  218. uci commit
  219. /etc/init.d/network reload
  220. #`/etc/init.d/network restart`
  221. fi
  222. }
  223. start_service() {
  224. config_load "$CONFIGURATION"
  225. local enabled
  226. config_get enabled general enabled
  227. if [ "$enabled" != "1" ]; then
  228. echo "dcwapd is disabled in UCI"
  229. return 1
  230. fi
  231. get_channelsets
  232. # get the list of channel sets
  233. channelsets=$result
  234. for channelset in $channelsets; do
  235. if [ -n "$channelset" ]; then
  236. get_channelset_enabled "$channelset"
  237. enabled=$result
  238. if [ "$enabled" = "1" ]; then
  239. # the channel set is enabled
  240. # get the list of data channels used by the channel set
  241. get_datachannels "$channelset"
  242. datachannels=$result
  243. for datachannel in $datachannels; do
  244. datachannel_up "$datachannel"
  245. done
  246. fi
  247. fi
  248. done
  249. procd_open_instance
  250. procd_set_param file /etc/config/dcwapd
  251. procd_set_param command dcwapd
  252. procd_set_param stdout 1
  253. procd_set_param stderr 1
  254. procd_close_instance
  255. }
  256. stop_service() {
  257. get_channelsets
  258. # get the list of channel sets
  259. channelsets=$result
  260. for channelset in $channelsets; do
  261. if [ -n "$channelset" ]; then
  262. # we don't care if it is enabled, tear it down
  263. # get_channelset_enabled $channelset
  264. # enabled=$result
  265. # if [ $enabled = "1" ]; then
  266. # # the channel set is enabled
  267. # get the list of data channels used by the channel set
  268. get_datachannels "$channelset"
  269. datachannels=$result
  270. for datachannel in $datachannels; do
  271. datachannel_down "$datachannel"
  272. done
  273. # fi
  274. fi
  275. done
  276. sleep 1
  277. }
  278. service_triggers()
  279. {
  280. procd_add_reload_trigger dcwapd
  281. }
  282. reload_service() {
  283. stop
  284. start
  285. }