Tizen 2.1 base
[platform/upstream/hplip.git] / base / slp.py
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 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 General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # Author: Don Welch
20 #
21
22 # Std Lib
23 import sys
24 import time
25 import socket
26 import select
27 import struct
28 import random
29 import re
30
31 # Local
32 from g import *
33 import utils
34
35 prod_pat = re.compile(r"""\(\s*x-hp-prod_id\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
36 mac_pat  = re.compile(r"""\(\s*x-hp-mac\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
37 num_port_pat = re.compile(r"""\(\s*x-hp-num_port\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
38 ip_pat =   re.compile(r"""\(\s*x-hp-ip\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
39 p1_pat =   re.compile(r"""\(\s*x-hp-p1\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
40 p2_pat =   re.compile(r"""\(\s*x-hp-p2\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
41 p3_pat =   re.compile(r"""\(\s*x-hp-p3\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
42 hn_pat =   re.compile(r"""\(\s*x-hp-hn\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
43
44
45 def detectNetworkDevices(ttl=4, timeout=10): #, xid=None, qappobj = None):
46     mcast_addr, mcast_port ='224.0.1.60', 427
47     found_devices = {}
48
49     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
50
51     x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
52     
53     try:
54         x.connect(('1.2.3.4', 56))
55     except socket.error:
56         log.error("Network is unreachable. Please check your network connection and try again.")
57         return {}
58         
59     intf = x.getsockname()[0]
60     x.close()
61
62     s.setblocking(0)
63     ttl = struct.pack('B', ttl) 
64
65     try:
66         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
67         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
68     except (AttributeError, socket.error):
69         pass
70
71     try:
72         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
73         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
74         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP ,1)
75     except Exception, e:
76         log.error("Unable to setup multicast socket for SLP: %s" % e)
77         return {}
78
79     packet = ''.join(['\x01\x06\x00\x2c\x00\x00\x65\x6e\x00\x03', 
80         struct.pack('!H', random.randint(1, 65535)), '\x00\x00\x00\x18service:x-hpnp-discover:\x00\x00\x00\x00'])
81
82     try:
83         s.sendto(packet, 0, (mcast_addr, mcast_port))
84     except socket.error, e:
85         log.error("Unable to send broadcast SLP packet: %s" % e)
86
87     time_left = timeout
88     while time_left > 0:
89         start_time = time.time()
90         r, w, e = select.select([s], [], [s], time_left)
91         time_left -= (time.time() - start_time)
92         if not r: continue
93
94         data, addr = s.recvfrom(2048)
95         update_spinner() 
96
97         log.log_data(data, width=32)
98
99         try:
100             ver, func, length, flags, dialect, lang_code, char_encode, recv_xid, status_code, attr_length = \
101                 struct.unpack("!BBHBBHHHHH", data[:16])
102
103             x = struct.unpack("!%ds" % attr_length, data[16:])[0].strip()
104         except struct.error:
105             continue
106
107         try:
108             num_ports = int(num_port_pat.search(x).group(1))
109         except (AttributeError, ValueError):
110             num_ports = 1
111
112         if num_ports == 0: # Embedded devices
113             num_ports = 1
114
115         y = {'num_devices' : 0, 'num_ports': num_ports, 'product_id' : '', 
116              'status_code': 0, 'device2': '0', 'device3': '0', 'note': '', 'device1': '0'}
117
118         # Check port 1
119         try:
120             y['device1'] = p1_pat.search(x).group(1)
121         except AttributeError:
122             y['device1'] = '0'
123         else:
124             y['num_devices'] += 1
125
126
127         if num_ports > 1: # Check port 2
128             try:
129                 y['device2'] = p2_pat.search(x).group(1)
130             except AttributeError:
131                 y['device2'] = '0'
132             else:
133                 y['num_devices'] += 1
134
135
136             if num_ports > 2: # Check port 3
137                 try:
138                     y['device3'] = p3_pat.search(x).group(1)
139                 except AttributeError:
140                     y['device3'] = '0'
141                 else:
142                     y['num_devices'] += 1
143
144         if y['device1'] is None:
145             y['device1'] = '0'
146
147         if y['device2'] is None:
148             y['device2'] = '0'
149
150         if y['device3'] is None:
151             y['device3'] = '0'
152
153         try:
154             y['product_id'] = prod_pat.search(x).group(1)
155         except AttributeError:
156             y['product_id'] = ''
157         try:
158             y['mac'] = mac_pat.search(x).group(1)
159         except AttributeError:
160             y['mac'] = ''
161         try:
162             y['ip'] = ip_pat.search(x).group(1)
163         except AttributeError:
164             y['ip'] = ''
165         try:
166             y['hn'] = hn_pat.search(x).group(1)
167         except AttributeError:
168             y['hn'] = ''
169
170         y['status_code'] = status_code
171         found_devices[addr[0]] = y
172
173         log.debug("Found device: %s" % y)
174
175
176     return found_devices
177
178