#!/usr/bin/python

from __future__ import absolute_import, print_function, unicode_literals

from gi.repository import GObject

import sys
import dbus
import dbus.mainloop.glib
import re
from optparse import OptionParser, make_option

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
mainloop = GObject.MainLoop()

manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")

option_list = [
		make_option("-i", "--device", action="store",
				type="string", dest="dev_id"),
		]
parser = OptionParser(option_list=option_list)

(options, args) = parser.parse_args()

if options.dev_id:
	adapter_path = manager.FindAdapter(options.dev_id)
else:
	adapter_path = manager.DefaultAdapter()

adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
							"org.bluez.Adapter")

if (len(args) < 1):
	print("Usage: %s <command>" % (sys.argv[0]))
	print("")
	print("  list")
	print("  services <address>")
	print("  create <address>")
	print("  remove <address|path>")
	print("  connect <address> [profile]")
	print("  disconnect <address> [profile]")
	print("  discover <address> [pattern]")
	print("  class <address>")
	print("  name <address>")
	print("  alias <address> [alias]")
	print("  trusted <address> [yes/no]")
	print("  blocked <address> [yes/no]")
	sys.exit(1)

if (args[0] == "list"):
	om = dbus.Interface(bus.get_object("org.bluez", "/"),
					"org.freedesktop.DBus.ObjectManager")
	objects = om.GetManagedObjects()

	for path, interfaces in objects.iteritems():
		if "org.bluez.Device" not in interfaces:
			continue
		properties = interfaces["org.bluez.Device"]
		if properties["Adapter"] != adapter_path:
			continue;
		print("%s %s" % (properties["Address"], properties["Alias"]))

	sys.exit(0)

def create_device_reply(device):
	print("New device (%s)" % device)
	mainloop.quit()
	sys.exit(0)

def create_device_error(error):
	print("Creating device failed: %s" % error)
	mainloop.quit()
	sys.exit(1)

if (args[0] == "create"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		adapter.CreateDevice(args[1],
				reply_handler=create_device_reply,
				error_handler=create_device_error)
	mainloop.run()

if (args[0] == "remove"):
	if (len(args) < 2):
		print("Need address or object path parameter")
	else:
		try:
			path = adapter.FindDevice(args[1])
		except:
			path = args[1]
		adapter.RemoveDevice(path)
	sys.exit(0)

if (args[0] == "connect"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) > 2):
			device.ConnectProfile(args[2])
		else:
			device.Connect()
	sys.exit(0)

if (args[0] == "disconnect"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) > 2):
			device.DisconnectProfile(args[2])
		else:
			device.Disconnect()
	sys.exit(0)

if (args[0] == "discover"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			pattern = ""
		else:
			pattern = args[2]
		services = device.DiscoverServices(pattern);
		for key in services.keys():
			p = re.compile(">.*?<")
			xml = p.sub("><", services[key].replace("\n", ""))
			print("[ 0x%5x ]" % (key))
			print(xml)
			print()
	sys.exit(0)

if (args[0] == "class"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
					"org.freedesktop.DBus.Properties")
		cls = device.Get("org.bluez.Device", "Class")
		print("0x%06x" % cls)
	sys.exit(0)

if (args[0] == "name"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
					"org.freedesktop.DBus.Properties")
		name = device.Get("org.bluez.Device", "Name")
		print(name)
	sys.exit(0)

if (args[0] == "alias"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
					"org.freedesktop.DBus.Properties")
		if (len(args) < 3):
			alias = device.Get("org.bluez.Device", "Alias")
			print(alias)
		else:
			device.Set("org.bluez.Device", "Alias", args[2])
	sys.exit(0)

if (args[0] == "trusted"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
					"org.freedesktop.DBus.Properties")
		if (len(args) < 3):
			trusted = device.Get("org.bluez.Device", "Trusted")
			print(trusted)
		else:
			if (args[2] == "yes"):
				value = dbus.Boolean(1)
			elif (args[2] == "no"):
				value = dbus.Boolean(0)
			else:
				value = dbus.Boolean(args[2])
			device.Set("org.bluez.Device", "Trusted", value)
	sys.exit(0)

if (args[0] == "blocked"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
					"org.freedesktop.DBus.Properties")
		if (len(args) < 3):
			blocked = device.Get("org.bluez.Device", "Blocked")
			print(blocked)
		else:
			if (args[2] == "yes"):
				value = dbus.Boolean(1)
			elif (args[2] == "no"):
				value = dbus.Boolean(0)
			else:
				value = dbus.Boolean(args[2])
			device.Set("org.bluez.Device", "Blocked", value)
	sys.exit(0)

if (args[0] == "services"):
	if (len(args) < 2):
		print("Need address parameter")
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
					"org.freedesktop.DBus.Properties")
		services = device.Get("org.bluez.Device", "Services")
		for path in services:
			print(path)
	sys.exit(0)

print("Unknown command")
sys.exit(1)
