Skip to content
Snippets Groups Projects
Commit 64b3ee99 authored by Tomas Hlavacek's avatar Tomas Hlavacek
Browse files

Omnia: sfpswitch support LEDs

Add LED indication to the sfpswitch daemon for Omnia.
The WAN LED has to be driven by software when SFP is inserted.
LED lights when the LOS and FLT signals are inactive.
The DIS signal has to be set to inactive.
parent 30830bc8
No related branches found
No related tags found
No related merge requests found
......@@ -7,12 +7,15 @@ import time
sfpdet_pin = 508
sfpdis_pin = 505
sfplos_pin = 507
sfpflt_pin = 504
gpio_export = '/sys/class/gpio/export'
sfp_select = '/sys/devices/platform/soc/soc:internal-regs/f1034000.ethernet/net/eth1/phy_select'
map = { 1: 'phy-def', 0: 'phy-sfp' }
cmd_net_res = 'ip link set down dev eth1; /etc/init.d/network restart'
cmd_safety_sleep = 2
wan_led = '/sys/devices/platform/soc/soc:internal-regs/f1011000.i2c/i2c-0/i2c-1/1-002b/leds/omnia-led:wan'
def write_once(path, value):
with open(path, 'w') as f:
......@@ -35,19 +38,36 @@ def init_gpio(pin):
def init():
init_gpio(sfpdet_pin)
init_gpio(sfpdis_pin)
write_once(os.path.join(gpio_dir(sfpdet_pin), 'direction'), 'in')
write_once(os.path.join(gpio_dir(sfpdet_pin), 'edge'), 'both')
init_gpio(sfpdis_pin)
write_once(os.path.join(gpio_dir(sfpdis_pin), 'direction'), 'out')
write_once(os.path.join(gpio_dir(sfpdis_pin), 'value'), '0')
init_gpio(sfplos_pin)
write_once(os.path.join(gpio_dir(sfplos_pin), 'direction'), 'in')
write_once(os.path.join(gpio_dir(sfplos_pin), 'edge'), 'both')
init_gpio(sfpflt_pin)
write_once(os.path.join(gpio_dir(sfpflt_pin), 'direction'), 'in')
write_once(os.path.join(gpio_dir(sfpflt_pin), 'edge'), 'both')
def set_led_mode(state):
if state == 1: # phy-def, autonomous blink
write_once(os.path.join(wan_led, 'autonomous'), '1')
elif state == 0: # phy-sfp, user blink
write_once(os.path.join(wan_led, 'autonomous'), '0')
else:
raise Exception("Unknown state %d. Can not happen." % state)
def set_led_brightness(light=False):
write_once(os.path.join(wan_led, 'brightness'), '1' if light else '0')
def do_switch(state, restart_net=True):
print 'Switching state to %s' % map[state]
write_once(sfp_select, map[state])
set_led_mode(state)
if restart_net:
time.sleep(cmd_safety_sleep)
os.system(cmd_net_res)
......@@ -61,23 +81,67 @@ def oneshot():
def run():
global state_last
def fdet_changed():
global state_last
fdet.seek(0)
state = int(fdet.read().strip())
if state != state_last:
state_last = state
do_switch(state)
def set_led():
global state_last
flos.seek(0)
fflt.seek(0)
los = int(flos.read().strip())
flt = int(fflt.read().strip())
set_led_mode(state_last)
if los or flt:
set_led_brightness(False)
else:
set_led_brightness(True)
def flos_changed():
set_led()
def fflt_changed():
set_led()
init()
f = open(os.path.join(gpio_dir(sfpdet_pin), 'value'), 'r')
po = select.poll()
po.register(f, select.POLLPRI)
fdet = open(os.path.join(gpio_dir(sfpdet_pin), 'value'), 'r')
flos = open(os.path.join(gpio_dir(sfplos_pin), 'value'), 'r')
fflt = open(os.path.join(gpio_dir(sfpflt_pin), 'value'), 'r')
state_last = int(f.read().strip())
po = select.epoll()
po.register(fdet, select.EPOLLPRI)
po.register(flos, select.EPOLLPRI)
po.register(fflt, select.EPOLLPRI)
state_last = int(fdet.read().strip())
do_switch(state_last)
set_led()
# main loop
while 1:
events = po.poll(60000)
if events:
f.seek(0)
state = int(f.read().strip())
if state != state_last:
state_last = state
do_switch(state)
for e in events:
ef = e[0] # event file descriptor
time.sleep(cmd_safety_sleep)
if ef == fdet.fileno():
fdet_changed()
elif ef == flos.fileno():
flos_changed()
elif ef == fflt.fileno():
fflt_changed()
else:
raise Exception("Unknown FD. Can not happen.")
def create_daemon():
try:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment