#!/usr/bin/python

import sys
import dbus
import os
import portio


def getStatus (radio):
    # acquire permission for I/O
    portio.ioperm(0x68, 1, 1)
    portio.ioperm(0x6C, 1, 1)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x80) == 0):
            break

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xb0,0x6c)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xbb,0x68)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x01) != 0):
            break
    wstatus = portio.inb(0x68)
    print 'status = %x' % (wstatus)
        
    if (radio == 1):
        if (wstatus & 1 << 0):
            print 'Wifi on'
            x = 1
        else:
            print 'Wifi off'
            x = 0
        os.system ("echo " + str(x) + " > /tmp/wifi_status")

    if (radio == 2):
        if (wstatus & 1 << 1):
            print 'BT on'
            x = 1
        else:
            print 'BT off'
            x = 0
        os.system ("echo " + str(x) + " > /tmp/bt_status")

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xff,0x6c)


def powerControl (radio):
    # acquire permission for I/O
    portio.ioperm(0x68, 1, 1)
    portio.ioperm(0x6C, 1, 1)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x80) == 0):
            break

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xb0,0x6c)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xbb,0x68)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x01) != 0):
            break
    wstatus = portio.inb(0x68)
    print 'status = %x' % (wstatus)
        
    if (radio == 1):
        if (wstatus & 1 << 0):
            print 'Wifi on'
            x = 1
        else:
            print 'Wifi off'
            x = 3
    
    if (radio == 2):
        if (wstatus & 1 << 1):
            print 'BT on'
            x = 2
        else:
            print 'BT off'
            x = 4
    
    if (radio == 3):
        x = 1
    if (radio == 4):
        x = 2
    if (radio == 5):
        x = 3
    if (radio == 6):
        x = 4

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xff,0x6c)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x80) == 0):
            break

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xb1,0x6c)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xbb,0x68)

    new_wstatus = wstatus
    if (x == 1):
        new_wstatus = new_wstatus & 0xfe # turn off wireless
        os.system("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:false")
    if (x == 2):
        new_wstatus = new_wstatus & 0xfd # turn off bluetooth
    if (x == 3):
        new_wstatus = new_wstatus | 0x01 # turn on wireless
        os.system("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:true")
    if (x == 4):
        new_wstatus = new_wstatus | 0x02 # turn on bluetooth

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(new_wstatus,0x68)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xff,0x6c)

def ACControl (x):
    # acquire permission for I/O
    portio.ioperm(0x68, 1, 1)
    portio.ioperm(0x6C, 1, 1)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x80) == 0):
            break

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0x59,0x6c)

    if (x == 1):
        new_wstatus = 0xD7 # turn off AC charge
    if (x == 2):
        new_wstatus = 0xD8 # turn on AC charge

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(new_wstatus,0x68)

    while 1:
        status = portio.inb(0x6c)
        if ((status & 0x02) == 0):
            break
    portio.outb(0xff,0x6c)


#print str(sys.argv)
go = True

if len(sys.argv) < 3: 
    print "Too few arguments. Stopping."
    go = False

if go:
    objct = sys.argv[1]
    action = sys.argv[2]

    if objct == "BT": 
        if action == "on":
            print "\tTurning BT ON"
            powerControl(6)
        elif action == "off":
            print "\tTurning BT OFF"
            powerControl(4)
        elif action == "toggle":
            print "\tToggle BT"
            powerControl(2)
        elif action == "status":
            print "\tStatus BT"
            getStatus(2)
    elif objct == "WIFI": 
        if action == "on":
            print "\tTurning WIFI ON"
            powerControl(5)
        elif action == "off":
            print "\tTurning WIFI OFF"
            powerControl(3)
        elif action == "toggle":
            print "\tToggle WIFI"
            powerControl(1)
        elif action == "status":
            print "\tStatus WIFI"
            getStatus(1)
    elif objct == "AC_CHARGING": 
        if action == "on":
            print "\tTurning AC_CHARGING on"
            ACControl(2)
        if action == "off":
            print "\tTurning AC_CHARGING off"
            ACControl(1)
    elif objct == "RADIO": 
        if action == "toggle":
            powerControl(1)
            powerControl(2)


