#!/usr/bin/python # ######################################################################## # # Bluetooth Manager # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # ######################################################################## import time import inspect import subprocess import os import bluetooth import PyOBEX.responses from PyOBEX.client import * version = "0.0.1 beta" debug = True def print_dbg(dbg_str): """Metodo che stampa una linea di debug verbosa, inserendo la linea di codice che l'ha chiamata e l'eventuale modulo/classe chiamante.""" if debug == True: dbg_str = dbg_str.strip('\n') frame = inspect.currentframe().f_back try: line = frame.f_lineno fname = inspect.currentframe().f_code.co_filename print fname + ":" + str(line) + ": \t" + dbg_str finally: del frame class bluetooth_manager: """Questa classe si occupa della gestione dell'inizializzazione del demone bluetooth, invia file, fa` spam e mette l'antenna in grado di ricevere. FIXME: sistemare l'interfaccia della classe TODO: scrivere una documentazione decente con esempi """ # linee di comando per accensione/spegnimento del device __status = "off" __start = ["echo 1 > /sys/bus/platform/devices/neo1973-pm-bt.0/power_on", "echo 0 > /sys/bus/platform/devices/neo1973-pm-bt.0/reset", "/etc/init.d/bluetooth start"] __stop = ["echo 0 > /sys/bus/platform/devices/neo1973-pm-bt.0/power_on", "echo 1 > /sys/bus/platform/devices/neo1973-pm-bt.0/reset", "/etc/init.d/bluetooth stop"] # variabile che contiene le coppie nome-mac della scansione devices = [] def initialize_device(self): """Cerca per dispositivi bluetooth sulla macchina e quando ne trova uno lo inizializza.""" print_dbg("inizializzazione periferica bluetooth") for cmd in self.__start: # lancia un processo e fa' il pipe dello stdout al lanciatore # TODO: verificare se serve anche il pipe di stderr proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) for str in proc.stdout: print_dbg(str) proc.wait() def stop_device(self): """Stoppa il demone e spegne il device bluetooth.""" print_dbg("spegnimento della periferica in corso") for cmd in self.__stop: # lancia un processo e fa' il pipe dello stdout al lanciatore # TODO: verificare se serve anche il pipe di stderr proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) for str in proc.stdout: print_dbg(str) proc.wait() def scan(self): """Fa` una scansione alla ricerca di dispositivi nelle vicinanze""" try: print_dbg("scansione dei dispositivi in corso... ") self.devices = bluetooth.discover_devices(lookup_names = True) except bluetooth.btcommon.BluetoothError,msg: print_dbg("errore di connessione al dispositivo: il programma verra' terminato...") print_dbg(" * %s " % msg) if len(self.devices) > 0: for addr, name in self.devices: print_dbg(" found remote device: ") print_dbg(" * name: %s " % (name)) print_dbg(" * mac address: %s " % (addr)) def browse(self): """ metodo inutilizzato - DEBUG ONLY - """ def send_file(self, dev, file_path): """Invia uno specifico file ad uno specifico device.""" if os.path.exists(file_path) and os.path.isfile(file_path): services_push = bluetooth.find_service(name = "OBEX Object Push", address = dev[0]) if len(services_push) < 0: print_dbg("sul dispositivo remoto non e' installato OBEX PUSH") raise bluetooth.btcommon.BluetoothError("OBEX PUSH service missing") bt_client = Client(address=services_push[0]["host"], port=services_push[0]["port"]) if isinstance(bt_client.connect(), responses.Success): f_send = open(file_path, "rb") f_bin = f_send.read() bt_client.put(f_send.name, f_bin) bt_client.disconnect() else: raise IOException() def set_recieve(self): """Mette il dispositivo in ascolto sulla macchina locale in modo che possa ricevere dei file che gli vengono inviati.""" #TODO: classe da scrivere, messa qui come segnaposto e basta def pairing(self, dev): """Fa il pairing tra due dispositivi.""" ######################################################################## # # # INIZIO PARTE PER IL TEST DEI COMPONENTI # # # ######################################################################## bt = bluetooth_manager() try: bt.initialize_device() time.sleep(2) bt.scan() time.sleep(5) bt.send_file(bt.devices[0], "ss1.png") finally: bt.stop_device()