1 # -*- coding: utf-8 -*-
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.
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.
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/>.
18 # ============================================================================
19 # Project Name : PC Remote
20 # Author : André Portela
21 # Email : andre_portela_@hotmail.com
22 # Reviewer : Jônatas Isvi
23 # Email : jonatas.nona@gmail.com
25 # Description : Interface Class, connection manager
26 # ============================================================================
28 from wirelessconnectionmanager import *
29 from bluetoothconnectionmanager import *
30 from exceptions import *
34 _btconst = ['bluetooth', 'BLUETOOTH', 'blue']
35 _wificonst = ['wireless', 'WIRELESS', 'wifi']
38 def __init__(self, string):
40 if(self.string in _btconst):
41 self.obj = BluetoothConnectionManager()
42 elif(self.string in _wificonst):
43 self.obj = WirelessConnectionManager()
45 raise IconnectionError, "Undefined type."
48 # ********************************************************************************
49 # Generic methods -> Wireless and Bluetooth *
50 # ********************************************************************************
52 # create a socket with defined protocol
53 def create_socket(self, protocol=None):
54 self.obj.create_socket(protocol)
60 # accept the connection
62 return self.obj.accept()
64 # send a message to device
65 def send_message(self, msg=None):
66 self.obj.send_message(msg)
69 def received_message(self):
70 return self.obj.received_message()
76 # listen the connection
84 # set the port to communicate
85 def set_port(self, port):
86 self.obj.set_port(port)
88 # get the port to communicate
90 return self.obj.get_port()
92 # set the device address
93 def set_address(self, address):
94 self.obj.set_address(address)
96 # get the device address
97 def get_address(self):
98 return self.obj.get_address()
100 # get the client address
101 def get_client_address(self):
102 return self.obj.get_client_address()
104 # ************************************************************************************
105 # Bluetooth methods - All methods for bluetooth services *
106 # ************************************************************************************
108 # fast way to create a simple server
109 def bt_create_server(self, protocol, port):
110 if self.string in _btconst:
111 return self.obj.create_server(protocol, port)
113 raise IconnectionError, "Only method used by Bluetooth connections."
115 # fast way to create a simple client
116 def bt_create_client(self, protocol, address, port):
117 if self.string in _btconst:
118 return self.obj.create_client(protocol, address, port)
120 raise IconnectionError, "Only method used by Bluetooth connections."
122 # search for all devices
123 def bt_find_devices(self, time=8):
124 if self.string in _btconst:
125 return self.obj.find_devices(time)
127 raise IconnectionError, "Only method used by Bluetooth connections."
129 # search only devices names
130 def bt_find_devices_only_names(self):
131 if self.string in _btconst:
132 return self.obj.find_devices_only_names()
134 raise IconnectionError, "Only method used by Bluetooth connections."
137 # search the device port
138 def bt_find_port(self, addr):
139 if self.string in _btconst:
140 return self.obj.find_port(addr)
142 raise IconnectionError, "Only method used by Bluetooth connections."
145 # search device services
146 def bt_find_services(self, service=None, addr=None):
147 if self.string in _btconst:
148 return self.obj.find_services(service, addr)
150 raise IconnectionError, "Only method used by Bluetooth connections."
153 # search the device indicated by name
154 def bt_find_device_address_by_name(self, device_name=None):
155 if self.string in _btconst:
156 return self.obj.find_device_address_by_name(device_name)
158 raise IconnectionError, "Only method used by Bluetooth connections."
162 # ***********************************************************************************
163 # Wireless method - All methods for wireless services *
164 # ***********************************************************************************