From 09a4632f7ae58f11d73dcbbbb7e69b397a40b9f6 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Tue, 12 Jan 2010 02:58:08 +0000
Subject: mode leds lua client works now updated openwrt package


diff --git a/luaclient.c b/luaclient.c
index 58aaa7e..3861977 100644
--- a/luaclient.c
+++ b/luaclient.c
@@ -49,6 +49,7 @@ static const luaL_Reg anylike_lualibs[] = {
   {LUA_TABLIBNAME, luaopen_table},
   {LUA_STRLIBNAME, luaopen_string},
   {LUA_MATHLIBNAME, luaopen_math},
+  {LUA_OSLIBNAME, luaopen_os},
   {LUA_LOGLIBNAME, luaopen_log},
   {LUA_SIGNALLIBNAME, luaopen_signal},
   {LUA_CMDLIBNAME, luaopen_cmd},
diff --git a/mode-leds.lua b/mode-leds.lua
index 1d49e8b..2c7e61c 100644
--- a/mode-leds.lua
+++ b/mode-leds.lua
@@ -21,9 +21,50 @@
 
 socket = require("socket")
 
+function set_master_led()
+   os.execute("/sbin/led.sh set master")                             
+   os.execute("/sbin/led.sh clear standby") 
+end
+
+function set_standby_led() 
+   os.execute("/sbin/led.sh clear master")                         
+   os.execute("/sbin/led.sh set standby")                 
+end
+
+function clear_leds()
+   os.execute("/sbin/led.sh clear master")                      
+   os.execute("/sbin/led.sh clear standby")    
+end
+
 function process_cmd(message)
    log.printf(log.DEBUG, "received message: '%s'", message)
-
+   
+   local capture = string.match(message, "Current Mode: (%a+)")
+   if(capture) then
+      if(capture == "Master") then
+         set_master_led()
+         return 0
+      else
+         if(capture == "Standby") then
+            set_standby_led()
+            return 0
+         end
+      end
+   end
+   
+   local capture = string.match(message, "new Mode: (%a+)")
+   if(capture) then
+      if(capture == "master") then
+         set_master_led()
+         return 0
+      else
+         if(capture == "standby") then  
+            set_standby_led()
+            return 0
+         end
+      end
+   end
+   
    return 0
 end
 
@@ -31,10 +72,12 @@ function main_loop(opt)
    log.printf(log.NOTICE, "main_loop started")
    local sig = signal.init()
    local cmdfd = cmd.init()
-
+   
    cmd.send_string("listen mode");
    cmd.send_string("status");
-
+   
+   clear_leds()
+   
    local return_value = 0
    while return_value == 0 do
       local readable, _, err = socket.select({ sig , cmdfd }, nil)
@@ -63,7 +106,9 @@ function main_loop(opt)
          end
       end
    end
-
+   
+   clear_leds() 
+   
    signal.stop()
    return return_value
 end
diff --git a/openwrt/rhctl/Makefile b/openwrt/rhctl/Makefile
index 82fb58e..22bbbd6 100644
--- a/openwrt/rhctl/Makefile
+++ b/openwrt/rhctl/Makefile
@@ -12,13 +12,13 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=rhctl
-PKG_VERSION:=1.0_RC1
+PKG_VERSION:=1.0
 PKG_RELEASE:=1
 
 PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
 PKG_SOURCE_URL:=http://localhost/
-PKG_MD5SUM:=5ef6f94450062e0daf7eb2b974ace3cf
+PKG_MD5SUM:=b746704c89869f23537d0d8e7d976357
 
 
 PKG_INSTALL_DIR:=$(PKG_BUILD_DIR)/ipkg-install
@@ -31,7 +31,7 @@ define Package/rhctl
   TITLE:=Radio Helsinki control tools
   URL:=http://www.helsinki.at
   MAINTAINER:=Christian Pointner <equinox@helsinki.at>
-  DEPENDS:=+udev udevtrigger
+  DEPENDS:=+liblua +libuci-lua +udev +udevtrigger
 endef
 
 define Package/rhctl/conffiles
@@ -40,7 +40,7 @@ endef
 
 define Package/rhctl/description
 rhctl is a collection of daemons and tools intended to control the 
-	main audio switch of Radio Helsinki.
+  main audio switch of Radio Helsinki.
 endef
 
 define Build/Configure
@@ -56,12 +56,14 @@ define Build/Compile
 	$(MAKE) -C $(PKG_BUILD_DIR) \
     $(TARGET_CONFIGURE_OPTS) \
     TARGET=Linux \
-    CFLAGS="$(TARGET_CFLAGS)" \
-    LDFLAGS="$(TARGET_LDFLAGS) -ldl" 
+    CFLAGS="$(TARGET_CFLAGS) -I$(STAGING_DIR)/usr/include" \
+    LDFLAGS="$(TARGET_LDFLAGS) -ldl -lm -lcrypt" \
+    LUA_LDFLAGS="-llua"
 	$(STRIP) $(PKG_BUILD_DIR)/switchctl
 	$(STRIP) $(PKG_BUILD_DIR)/serialclient
 	$(STRIP) $(PKG_BUILD_DIR)/stdioclient
 	$(STRIP) $(PKG_BUILD_DIR)/heartbeatclient
+	$(STRIP) $(PKG_BUILD_DIR)/luaclient
 endef
 
 define Package/rhctl/install
@@ -74,12 +76,16 @@ define Package/rhctl/install
 	$(INSTALL_BIN) $(PKG_BUILD_DIR)/serialclient $(1)/usr/bin/
 	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stdioclient $(1)/usr/bin/
 	$(INSTALL_BIN) $(PKG_BUILD_DIR)/heartbeatclient $(1)/usr/bin/
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/luaclient $(1)/usr/bin/
 	$(INSTALL_BIN) ./files/rhctlsh $(1)/usr/bin/
+	$(INSTALL_DIR) $(1)/usr/share/rhctl
+	$(INSTALL_DATA) $(PKG_BUILD_DIR)/*.lua $(1)/usr/share/rhctl/
 	$(INSTALL_DIR) $(1)/etc/init.d
 	$(INSTALL_BIN) ./files/udev.init $(1)/etc/init.d/udev
 	$(INSTALL_BIN) ./files/switchctl.init $(1)/etc/init.d/switchctl
 	$(INSTALL_BIN) ./files/serialclient.init $(1)/etc/init.d/serialclient
 	$(INSTALL_BIN) ./files/heartbeatclient.init $(1)/etc/init.d/heartbeatclient
+	$(INSTALL_BIN) ./files/luaclient.init $(1)/etc/init.d/luaclient
 	$(INSTALL_DIR) $(1)/etc/udev/rules.d
 	$(INSTALL_DATA) ./files/udev.rules $(1)/etc/udev/rules.d/50-rhctl.rules
 endef
diff --git a/openwrt/rhctl/files/luaclient.init b/openwrt/rhctl/files/luaclient.init
new file mode 100755
index 0000000..bd0a796
--- /dev/null
+++ b/openwrt/rhctl/files/luaclient.init
@@ -0,0 +1,112 @@
+#!/bin/sh /etc/rc.common
+START=73
+
+BIN=luaclient
+DAEMON=/usr/bin/$BIN
+DESC=$BIN
+RUN_D=/var/run/rhctl
+
+
+option_cb() {
+  local varname="$1"
+  local value="$2"
+
+  if ! echo "$CONFIG_OPTIONS" | grep " $varname " > /dev/null; then
+    CONFIG_OPTIONS="$CONFIG_OPTIONS $varname "
+  fi
+}
+
+foreach_config_forced() {
+  foreach_config $1 "$2" "forced"
+}
+
+foreach_config() {
+  local cfg="$1"
+  local name
+  local option
+  local value
+  local args=""
+  local forced=0
+
+  config_get name "$cfg" "name"
+  if [ -n "$2" ] && [ "x$2" != "x$name" ]; then
+    return
+  fi
+
+  if [ -n "$3" ] && [ "x$3" == "xforced" ]; then
+    forced=1
+  fi
+
+  for option in $CONFIG_OPTIONS
+  do
+    config_get value "$cfg" "$option"
+    if [ "x$option" == "xdisabled" ]; then
+      if [ $forced -eq 0 ] && [ -n "$value" ] && [ $value -eq 1 ]; then
+        echo -n " $name(disabled)"
+        return
+      fi
+      continue
+    fi
+
+    if [ "x$option" == "xname" ]; then
+      continue
+    fi
+
+    option=`echo $option | tr '_' '-'`
+    if [ -n "$value" ]; then
+      args="$args --$option $value"
+    fi
+  done
+  echo -n " $name"
+  local status="OK"
+  $DAEMON --write-pid "$RUN_D/$BIN.$name.pid" $args || status="failed"
+  echo -n "($status)"
+}
+
+stop_client() {
+  local name=$1
+  local pidfile=$RUN_D/$BIN.$name.pid
+  echo -n " $name"
+  local status="OK"
+  if [ ! -f "$pidfile" ]; then
+    status="tunnel not active"    
+  else
+    kill `cat $pidfile` > /dev/null 2>&1 || status="failed"
+    rm -f $pidfile
+  fi
+  echo -n "($status)"
+}
+
+start() {
+  echo -n "Starting $DESC:"
+  config_load "rhctl"
+  if [ $# -gt 0 ]; then
+    while [ $# -gt 0 ]; do
+      config_foreach foreach_config_forced "luaclient" "$1"
+      shift
+    done
+  else
+    config_foreach foreach_config "luaclient"
+  fi
+  echo "."
+}
+
+stop() {
+  echo -n "Stopping $DESC:"
+  local name
+  local pidfile
+
+  if [ $# -gt 0 ]; then
+    while [ $# -gt 0 ]; do
+      stop_client $1
+      shift
+    done
+  else
+    for pidfile in `ls $RUN_D/$BIN.*.pid 2> /dev/null`; do
+      name=${pidfile%%.pid}
+      name=${name##$RUN_D/$BIN.}
+      stop_client $name
+    done
+  fi
+  echo "."
+}
diff --git a/openwrt/rhctl/files/rhctl.config b/openwrt/rhctl/files/rhctl.config
index 2e4f176..7d3f06f 100644
--- a/openwrt/rhctl/files/rhctl.config
+++ b/openwrt/rhctl/files/rhctl.config
@@ -31,4 +31,11 @@ config 'heartbeatclient'
   option baudrate '38400'
   option device '/dev/ttyHeartbeat'
   option timeout '15'
-  option led 'heartbeat'
\ No newline at end of file
+  option led 'heartbeat'
+
+config 'luaclient'
+#  option disabled 1
+  option name 'mode-leds'
+  option log 'syslog:3,luaclient-mode-leds,daemon'
+  option command_sock '/var/run/rhctl/switchctl.sock'
+  option lua_file '/usr/share/rhctl/mode-leds.lua'
-- 
cgit v0.10.2