pcremote-client-n8x0 -> client sources
[remotepc] / pcremote-client-n8x0 / debian / pcremote-client / usr / share / pcremote-client / connection / bluetoothconnectionmanager.py
1 # -*- coding: utf-8 -*-
2
3 #  ****************************************************************************
4 #  Copyright (c) 2008 INdT/Fucapi.
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU Lesser General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Lesser General Public License for more details.
14
15 #  You should have received a copy of the GNU Lesser General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #  ============================================================================
19 #  Project Name : PC Remote
20 #  Author       : Jônatas Isvi
21 #  Email        : jonatas.nona@gmail.com
22 #  Version      : 1.0
23 #  Description  : BluetoothConnectionManager
24 #  ============================================================================
25
26
27 import bluetooth
28 from exceptions import *
29 from   genericconnectionmanager import *
30
31 class BluetoothConnectionError(Exception):
32     pass
33
34 class BluetoothConnectionManager(GenericConnectionManager):
35
36         def __init__(self):     
37                 GenericConnectionManager.__init__(self)
38                 print "BluetoothConnectionManager iniciado."
39                 # globals data variables
40                 self.sock           =    None
41                 self.port           =    None
42                 self.address        =    None
43                 self.client_sock    =    None
44                 self.client_address =    None           
45
46         # fast way to create a simple server
47         def create_server(self, protocol, port):
48                 self.create_socket(protocol)
49                 self.set_port(port)
50                 self.bind()
51                 self.listen()
52                 self.accept()
53
54         # fast way to create a simple client    
55         def create_client(self, protocol, address, port):
56                 self.create_socket(protocol)
57                 self.set_address(address)
58                 self.set_port(port)
59                 self.connect()
60
61         # search for all devices
62         def find_devices(self, time=8):
63                 list_devices = bluetooth.discover_devices(lookup_names = True, duration=time)
64                 if list_devices:
65                         return list_devices
66                 else:
67                         raise BluetoothConnectionError, "Device were not found." 
68         
69         # search the device port
70         def find_port(self, addr):
71                 port = None
72                 aux = addr.split(":")
73                 if len(aux) == 6:
74                         services = bluetooth.find_service(address=addr)
75                         for i in range(len(services)):
76                                 port = services[i]['port']
77                 
78                         if port != None:        
79                                 return port
80                         else:
81                                 raise BluetoothConnectionError, "Port not found."
82                                 
83                 else:
84                         raise BluetoothConnectionError, "Invalid address."
85         
86         # search device services
87         def find_services(self, service=None, addr=None):
88                 if service == None and addr == None:
89                         list = bluetooth.find_service()
90                         # returns all services
91                         return list
92                 elif service != None and addr == None:
93                         list = bluetooth.find_service(name=service)
94                         # returns only the device services indicated by name
95                         if list != []:
96                                 return list
97                         else:
98                                 raise BluetoothConnectionError, "Name of the service does not exist."
99                 elif service == None and addr != None:
100                         number = addr.split(":")
101                         # returns only the device services indicated by address
102                         if(len(number) == 6):
103                                 list = bluetooth.find_service(address=addr)
104                                 if list != []:
105                                         return list
106                                 else:
107                                         raise BluetoothConnectionError, "Services not found."
108                         else:
109                                 raise BluetoothConnectionError, "Invalid address."
110                 elif service != None and addr != None:
111                         number = addr.split(":")
112                         # returns only the device service indicated by address
113                         if(len(number) == 6):
114                                 list = bluetooth.find_service(name=service, address=addr)
115                                 if list != []:
116                                         return list
117                                 else:
118                                         raise BluetoothConnectionError, "Services not found."
119                         else:
120                                 raise BluetoothConnectionError, "Invalid address."
121         
122
123         # search the device indicated by name
124         def find_device_address_by_name(self, device_name):     
125                 list = bluetooth.discover_devices()
126                 addr = None
127                 
128                 for address in list:
129                         if device_name == bluetooth.lookup_name(address):
130                                 addr = address
131                                 break
132                 if addr:
133                         return addr
134                 else:
135                         raise BluetoothConnectionError, "Device name not found."
136         
137
138         def find_devices_only_names(self):
139                 list = self.find_devices()
140                 list_names = []
141                 for address, names in list:
142                         list_names += [names]
143         
144                 if list_names:
145                         return list_names
146                 else:
147                         raise BluetoothConnectionError, "Devices were not found."
148
149         # get the client address
150         def get_client_address(self):
151                 return self.client_address      
152
153         # set the port to communicate
154         def set_port(self, port):
155                 self.port = port
156
157         # get the port to communicate
158         def get_port(self):
159                 return self.port
160
161         # set the device address
162         def set_address(self, address):
163                 aux = address.split(":")
164                 if len(aux) == 6:
165                         self.address = address
166                 else:
167                         raise BluetoothConnectionError, "Invalid address."
168
169         # get the device address
170         def get_address(self):
171                 return self.address
172
173         # create a socket with a determinated protocol
174         def create_socket(self, protocol=None):
175                 if protocol == 'rfcomm' or protocol == 'RFCOMM':
176                         self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
177                 elif protocol == 'l2cap' or protocol == 'L2CAP':
178                         self.sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
179                 else:   
180                         raise BluetoothConnectionError, "Undefined Protocol."
181
182         # bind the communication
183         def bind(self):
184                 self.sock.bind(("", self.get_port()))
185
186         # just listen the tube, only to server
187         def listen(self):
188                 self.sock.listen(1)
189
190         # accept the client communication
191         # returns an object 
192         def accept(self):
193                 self.client_sock, self.client_address = self.sock.accept()
194                 
195         # connect devices
196         def connect(self):
197                 self.sock.connect((self.get_address(), self.get_port()))
198
199         # send string message           
200         def send_message(self, msg=None):
201                 self.sock.send(msg)
202
203         # receive string message
204         def received_message(self):
205                 return self.client_sock.recv(1024)
206
207         # close connection
208         def close(self):
209                 if self.sock != None and self.client_sock != None:
210                         self.client_sock.close()
211                         self.sock.close()       
212                 elif self.sock != None and self.client_sock == None:
213                         self.sock.close()
214                 else:
215                         self.client_sock.close()
216