This file is indexed.

/usr/share/hplip/base/wifi.py is in hplip-data 3.14.3-0ubuntu3.4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# -*- coding: utf-8 -*-
#
# (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Author: Don Welch
#

# StdLib
import time
import cStringIO
from base.g import *
try:
   import xml.parsers.expat
except ImportError,e:
   log.info("\n")
   log.error("Failed to import xml.parsers.expat(%s).\nThis may be due to the incompatible version of python-xml package.\n"%(e))
   if "undefined symbol" in str(e):
       log.info(log.blue("Please re-install compatible version (other than 2.7.2-7.14.1) due to bug reported at 'https://bugzilla.novell.com/show_bug.cgi?id=766778'."))
       log.info(log.blue("\n        Run the following commands in root mode to change the python-xml package.(i.e Installing 2.7.2-7.1.2)"))
       log.info(log.blue("\n        Using zypper:\n        'zypper remove python-xml'\n        'zypper install python-xml-2.7.2-7.1.2'"))
       log.info(log.blue("\n        Using apt-get:\n        'apt-get remove python-xml'\n        'apt-get install python-xml-2.7.2-7.1.2'"))
       log.info(log.blue("\n        Using yum:\n        'yum remove python-xml'\n        'yum install python-xml-2.7.2-7.1.2'"))

   sys.exit(1)
   
# Local
from base.g import *
from base import device, utils

MAX_NETWORKS = 100
MAX_RETRIES = 20
NS = "http://www.hp.com/schemas/imaging/cnc/dcsl/2006/05/WifiConfig"
PREAMBLE = """<?xml version="1.0" encoding="utf-8"?>
<WiFiConfig xmlns="%s">
""" % NS

def _readWriteWifiConfig(dev, request):
    if not request:
        log.error("Invalid request")
        return 'executionfailed', {}

    log.debug("Sending request on wifi config channel...")
    log.log_data(request)
    #log.xml(request)

    bytes_written = dev.writeWifiConfig(request)
    log.debug("Wrote %d bytes." % bytes_written)

    data = cStringIO.StringIO()
    log.debug("Reading response on wifi config channel...")
    bytesread = dev.readWifiConfig(device.MAX_BUFFER, stream=data, timeout=30)
    i = 0
    # if response data is > 8192 bytes, make sure we have read it all...
    while True:
        i += 1
        bytesread = dev.readWifiConfig(device.MAX_BUFFER, stream=data, timeout=1)
        if not bytesread or i > MAX_RETRIES:
            break

    data = data.getvalue()

    #log.xml(repr(data))

    # Convert any char references
    data = utils.unescape(data)

    #log.xml(repr(data))
    data = unicode(data, 'utf-8')

    #log.xml(repr(data))

    # C4380 returns invalid XML for DeviceCapabilitiesResponse
    # Eliminate any invalid characters
    data = data.replace(u"Devicecapabilities", u"DeviceCapabilities").replace('\x00', '')

    log.log_data(data)
    log.debug("Read %d bytes." % len(data))

    if not data:
        log.error("No data")
        return 'executionfailed', {}

    #log.xml(data)

    try:
        params = utils.XMLToDictParser().parseXML(data)
    except xml.parsers.expat.ExpatError, e:
        log.error("XML parser failed: %s" % e)
        match = re.search(r"""line\s*(\d+).*?column\s*(\d+)""", str(e), re.I)
        if match is not None:
            log.error(data[int(match.group(2)):])
        return 'executionfailed', {}

    #log.pprint(params)

    errorreturn = 'executionfailed'
    for p in params:
        if p.lower().endswith('errorreturn'):
            errorreturn = params[p].lower()
            break

    params['errorreturn'] = errorreturn

    return errorreturn, params

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


def getDeviceCapabilities(dev):
    ret = {}
    request = PREAMBLE + """<GetDeviceCapabilitiesRequest>
</GetDeviceCapabilitiesRequest>
</WiFiConfig>"""

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return {}

    ret['errorreturn'] = errorreturn
    if errorreturn != 'ok':
        log.error("GetDeviceCapabilities returned an error: %s" % errorreturn)
        return ret

    param_keys = ['wificonfig-getdevicecapabilitiesresponse-devicecapabilities-numberofsupportedwifiaccessories',
                  'wificonfig-getdevicecapabilitiesresponse-interfaceversion-minorreleasenumber',
                  'wificonfig-getdevicecapabilitiesresponse-interfaceversion-majorreleasenumber',
                 ]

    for p in param_keys:
        try:
            ret[p.split('-')[-1]] = params[p]
        except KeyError:
            log.debug("Missing response key: %s" % p)
            continue

    return ret

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


def getAdaptorList(dev):
    ret = {}
    request = PREAMBLE + """<GetAdaptorListRequest>
</GetAdaptorListRequest>
</WiFiConfig>"""

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return {'adaptorlistlength': 0}

    ret['errorreturn'] = errorreturn
    if errorreturn != 'ok':
        log.error("GetAdaptorList returned an error: %s" % errorreturn)
        return ret

    try:
        adaptor_list_length = int(params['wificonfig-getadaptorlistresponse-adaptorlistlength'])
    except (ValueError, KeyError):
        adaptor_list_length = 0

    ret['adaptorlistlength'] = adaptor_list_length

    if adaptor_list_length == 0:
        log.error("GetAdaptorList returned 0 adaptors")

    elif adaptor_list_length == 1:
        try:
            ret['adaptorid-0'] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorid']
            ret['adaptorname-0'] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorname']
            ret['adaptorpresence-0'] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorpresence']
            ret['adaptorstate-0'] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorstate']
            ret['adaptortype-0'] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptortype']
        except KeyError, e:
            log.debug("Missing response key: %s" % e)
    else:
        for a in xrange(adaptor_list_length):
            try:
                ret['adaptorid-%d' % a] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorid-%d' % a]
                ret['adaptorname-%d' % a] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorname-%d' % a]
                ret['adaptorpresence-%d' % a] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorpresence-%d' % a]
                ret['adaptorstate-%d' % a] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptorstate-%d' % a]
                ret['adaptortype-%d' % a] = params['wificonfig-getadaptorlistresponse-adaptorlist-adaptorinfo-adaptortype-%d' % a]
            except KeyError, e:
                log.debug("Missing response key: %s" % e)

    return ret

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

def getWifiAdaptorID(dev):
    # rVal: [[adaptor_id, name, state, presence]]
    # ret: [adaptor_id, name, state, presence]
    rVal = []
    ret = getAdaptorList(dev)

    try:
        num_adaptors = ret['adaptorlistlength']
    except KeyError:
        num_adaptors = 0

    for n in xrange(num_adaptors):
        try:
            name = ret['adaptortype-%d' % n]
        except KeyError:
            name = ''

        if name.lower() in ('wifiembedded', 'wifiaccessory'):
            params = ['adaptorid', 'adaptorname', 'adaptorstate', 'adaptorpresence']

            r = []
            for p in params:
                try:
                    x = ret[''.join([p, '-', str(n)])]
                except KeyError:
                    if p == 'adaptorid':
                        x = -1
                    else:
                        x = 'Unknown'

                r.append(x)

            rVal.append(r)
            
    return rVal


# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                         
def setAdaptorPower(dev, adapterList, power_state='PowerOn'):
    adaptor_id=-1
    adaptorName =""
    for a in adapterList:
        adaptor_id = a[0]
        adaptorName = a[1]
        request = PREAMBLE + """<SetAdaptorPowerRequest>
<AdaptorID>%s</AdaptorID>
<PowerState>%s</PowerState>
</SetAdaptorPowerRequest>
</WiFiConfig>""" % (adaptor_id, power_state.encode('utf-8'))

        errorreturn, params = _readWriteWifiConfig(dev, request)
        if not params:
            return -1 ,"","",""

        if errorreturn != 'ok':
            log.error("SetAdaptorPower returned an error: %s" % errorreturn)
        else:
            log.debug("SetAdaptorPower returned Success.")
            return adaptor_id, adaptorName, a[2], a[3]

    return -1 ,"","",""

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


def performScan(dev, adapterName, ssid=None):
    ret, i, scan_state = {}, 0, "NewScan"

    while True:
        if ssid is None: # Undirected
            request = PREAMBLE + """<UndirectedScanRequest>
<ScanState>%s</ScanState>
</UndirectedScanRequest>
</WiFiConfig>""" % scan_state

            typ = 'UndirectedScan'
            rsp = 'undirectedscanresponse'

        else: # Directed
            request = PREAMBLE + """<DirectedScanRequest>
<SSID>%s</SSID>
<ScanState>%s</ScanState>
</DirectedScanRequest>
</WiFiConfig>""" % (ssid.encode('utf-8'), scan_state)

            typ = 'Directed'
            rsp = 'directedscanresponse'

        errorreturn, params = _readWriteWifiConfig(dev, request)
        if not params:
            return {'numberofscanentries': 0}

        ret['errorreturn'] = errorreturn
        if errorreturn != 'ok':
            log.error("%s returned an error: %s" % (typ, errorreturn))
            return ret

        try:
            number_of_scan_entries = int(params['wificonfig-%s-numberofscanentries' % rsp])
        except (ValueError, KeyError):
            number_of_scan_entries = 0

        ret['numberofscanentries'] = number_of_scan_entries

        if number_of_scan_entries == 0:
            if scan_state.lower() == 'scancomplete':
                log.debug("%s returned 0 entries. Scan complete." % typ)
            else:
                log.debug("%s returned 0 entries. Resuming scan..." % typ)

        elif number_of_scan_entries == 1:
            try:
                ssid = params['wificonfig-%s-scanlist-scanentry-ssid' % rsp]
                if not ssid:
                    ret['ssid-0'] = u'(unknown)'
                else:
                    ret['ssid-0'] = ssid
                ret['bssid-0'] = params['wificonfig-%s-scanlist-scanentry-bssid' % rsp]
                ret['channel-0'] = params['wificonfig-%s-scanlist-scanentry-channel' % rsp]
                ret['communicationmode-0'] = params['wificonfig-%s-scanlist-scanentry-communicationmode' % rsp]
                ret['dbm-0'] = params['wificonfig-%s-scanlist-scanentry-dbm' % rsp]
                ret['encryptiontype-0'] = params['wificonfig-%s-scanlist-scanentry-encryptiontype' % rsp]
                ret['rank-0'] = params['wificonfig-%s-scanlist-scanentry-rank' % rsp]
                ret['signalstrength-0'] = params['wificonfig-%s-scanlist-scanentry-signalstrength' % rsp]
            except KeyError, e:
                log.debug("Missing response key: %s" % e)

        else:
            for a in xrange(number_of_scan_entries):
                j = a+i
                try:
                    ssid = params['wificonfig-%s-scanlist-scanentry-ssid-%d' % (rsp, j)]
                    if not ssid:
                        ret['ssid-%d' % j] = u'(unknown)'
                    else:
                        ret['ssid-%d' % j] = ssid
                    ret['bssid-%d' % j] = params['wificonfig-%s-scanlist-scanentry-bssid-%d' % (rsp, j)]
                    ret['channel-%d' % j] = params['wificonfig-%s-scanlist-scanentry-channel-%d' % (rsp, j)]
                    ret['communicationmode-%d' % j] = params['wificonfig-%s-scanlist-scanentry-communicationmode-%d' % (rsp, j)]
                    ret['dbm-%d' % j] = params['wificonfig-%s-scanlist-scanentry-dbm-%d' % (rsp, j)]
                    ret['encryptiontype-%d' % j] = params['wificonfig-%s-scanlist-scanentry-encryptiontype-%d' % (rsp, j)]
                    ret['rank-%d' % j] = params['wificonfig-%s-scanlist-scanentry-rank-%d' % (rsp, j)]
                    ret['signalstrength-%d' % j] = params['wificonfig-%s-scanlist-scanentry-signalstrength-%d' % (rsp, j)]
                except KeyError, e:
                    log.debug("Missing response key: %s" % e)

        try:
            scan_state = ret['scanstate'] = params['wificonfig-%s-scanstate' % rsp] # MoreEntriesAvailable, ScanComplete
            ret['signalstrengthmax'] = params['wificonfig-%s-scansettings-signalstrengthmax' % rsp]
            ret['signalstrengthmin'] = params['wificonfig-%s-scansettings-signalstrengthmin' % rsp]
        except KeyError, e:
            log.debug("Missing response key: %s" % e)

        if scan_state.lower() == 'scancomplete':
            break

        scan_state = "ResumeScan"
        i += number_of_scan_entries

        if i > MAX_NETWORKS:
            break

        time.sleep(2)

    return ret

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


def associate(dev, adapterName,ssid, communication_mode, encryption_type, key):
    ret = {}
    request = PREAMBLE + """<AssociateRequest>
<SSID>%s</SSID>
<CommunicationMode>%s</CommunicationMode>
<EncryptionType>%s</EncryptionType>
<EncryptedParameters>%s</EncryptedParameters>
<Key>%s</Key>
</AssociateRequest>
</WiFiConfig>""" % (ssid.encode('utf-8'), communication_mode.encode('utf-8'),
                    encryption_type.encode('utf-8'), u"False".encode('utf-8'),
                    key.encode('utf-8'))

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return {}

    ret['errorreturn'] = errorreturn
    if errorreturn != 'ok':
        log.error("Associate returned an error: %s" % errorreturn)
        return ret

    return ret

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


def getVSACodes(dev,adapterName):
    ret = []
    request = PREAMBLE + """<GetVSACodesRequest>
</GetVSACodesRequest>
</WiFiConfig>"""

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return []

    if errorreturn != 'ok':
        log.error("GetVSACodes returned an error: %s" % errorreturn)
        return ret

    try:
        rule = params['wificonfig-getvsacodesresponse-vsacodelist-vsacode-rulenumber']
        severity = params['wificonfig-getvsacodesresponse-vsacodelist-vsacode-severity']
    except KeyError:
        n = 0
        while True:
            try:
                rule = params['wificonfig-getvsacodesresponse-vsacodelist-vsacode-rulenumber-%d' % n]
            except KeyError:
                break

            severity = params['wificonfig-getvsacodesresponse-vsacodelist-vsacode-severity-%d' % n]

            ret.append((rule, severity))
            n += 1
    else:
        ret.append((rule, severity))

    return ret

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

def __getIPConfiguration(dev, adaptor_id=0):
    ret = {}
    request = PREAMBLE + """<GetIPConfigurationRequest>
<AdaptorID>%d</AdaptorID>
</GetIPConfigurationRequest>
</WiFiConfig>""" % adaptor_id

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return {}

    ret['errorreturn'] = errorreturn
    if errorreturn != 'ok':
        log.error("GetIPConfiguration returned an error: %s" % errorreturn)
        return ret

    param_keys = ['wificonfig-getipconfigurationresponse-ipconfiguration-addressmode',
                  'wificonfig-getipconfigurationresponse-ipconfiguration-alternatednsaddress',
                  'wificonfig-getipconfigurationresponse-ipconfiguration-gatewayaddress',
                  'wificonfig-getipconfigurationresponse-ipconfiguration-ipaddress',
                  'wificonfig-getipconfigurationresponse-ipconfiguration-primarydnsaddress',
                  'wificonfig-getipconfigurationresponse-ipconfiguration-subnetmask',
                  'wificonfig-getipconfigurationresponse-networkconfiguration-hostname',
                  ]

    for p in param_keys:
        try:
            ret[p.split('-')[-1]] = params[p]
        except KeyError:
            log.debug("Missing response key: %s" % p)
            continue

    return ret


def getIPConfiguration(dev, adapterName, adaptor_id=0):
    ip, hostname, addressmode, subnetmask, gateway, pridns, sec_dns = \
        '0.0.0.0', 'Unknown', 'Unknown', '0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0'
    ret = __getIPConfiguration(dev, adaptor_id)

    if ret and ret['errorreturn'].lower() == 'ok':
        try:
            ip = ret['ipaddress']
            hostname = ret['hostname']
            addressmode = ret['addressmode']
            subnetmask = ret['subnetmask']
            gateway = ret['gatewayaddress']
            pridns = ret['primarydnsaddress']
            sec_dns = ret['alternatednsaddress']
        except KeyError, e:
            log.debug("Missing response key: %s" % str(e))

    return ip, hostname, addressmode, subnetmask, gateway, pridns, sec_dns

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

def __getSignalStrength(dev, adaptor_id=0):
    ret = {}
    request = PREAMBLE + """<GetSignalStrengthRequest>
<AdaptorID>%d</AdaptorID>
</GetSignalStrengthRequest>
</WiFiConfig>""" % adaptor_id

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return {}

    ret['errorreturn'] = errorreturn
    if errorreturn != 'ok':
        log.error("GetSignalStrength returned an error: %s" % errorreturn)
        return ret

    param_keys = ['wificonfig-getsignalstrengthresponse-signalstrength-dbm',
                  'wificonfig-getsignalstrengthresponse-signalstrength-signalstrengthmax',
                  'wificonfig-getsignalstrengthresponse-signalstrength-signalstrengthmin',
                  'wificonfig-getsignalstrengthresponse-signalstrength-signalstrengthvalue',
                  ]

    for p in param_keys:
        try:
            ret[p.split('-')[-1]] = params[p]
        except KeyError:
            log.debug("Missing response key: %s" % p)
            continue

    return ret


def getSignalStrength(dev, adapterName, ssid, adaptor_id=0):
    ss_max, ss_min, ss_val, ss_dbm = 5, 0, 0, -200
    ret = __getSignalStrength(dev, adaptor_id)

    if ret and ret['errorreturn'].lower() == 'ok':
        try:
            ss_max = ret['signalstrengthmax']
            ss_min = ret['signalstrengthmin']
            ss_val = ret['signalstrengthvalue']
            ss_dbm = ret['dbm']
        except KeyError, e:
            log.debug("Missing response key: %s" % str(e))

    return ss_max, ss_min, ss_val, ss_dbm


# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

def __getCryptoSuite(dev):
    ret = {}
    request = PREAMBLE + """<GetCryptoSuiteRequest>
</GetCryptoSuiteRequest>
</WiFiConfig>"""

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return {}

    ret['errorreturn'] = errorreturn
    if errorreturn != 'ok':
        log.error("GetSignalStrength returned an error: %s" % errorreturn)
        return ret

    #log.pprint(params)

    param_keys = ['wificonfig-getcryptosuiteresponse-cryposuite-crypoalgorithm',
                  'wificonfig-getcryptosuiteresponse-cryposuite-crypomode',
                  'wificonfig-getcryptosuiteresponse-cryposuite-secretid',]

    for p in param_keys:
        try:
            ret[p.split('-')[-1]] = params[p]
        except KeyError:
            log.debug("Missing response key: %s" % p)
            continue

    return ret


def getCryptoSuite(dev, adapterName):
    alg, mode, secretid = '', '', ''
    ret = __getCryptoSuite(dev)

    if ret and ret['errorreturn'].lower() == 'ok':
        try:
            alg = ret['crypoalgorithm']
            mode = ret['crypomode']
            secretid = ret['secretid']
        except KeyError, e:
            log.debug("Missing response key: %s" % str(e))

    return  alg, mode, secretid

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

def getHostname(dev):
    ret = ''
    request = PREAMBLE + """<GetHostnameRequest>
</GetHostnameRequest>
</WiFiConfig>"""

    errorreturn, params = _readWriteWifiConfig(dev, request)
    if not params:
        return ret

    if errorreturn != 'ok':
       # log.error("GetHostname returned an error: %s" % errorreturn)
        return ret

    try:
        ret = params['wificonfig-gethostnameresponse-hostname']
    except KeyError:
        log.debug("Missing response key: hostname")

    return ret

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


def getLocation(bssid, ss):
    log.debug("Getting location for wifi AP: %s" % bssid)
    request = """<?xml version='1.0'?>
<LocationRQ xmlns='http://skyhookwireless.com/wps/2005' version='2.6' street-address-lookup='full'>
<authentication version='2.0'>
<simple>
<username>beta</username>
<realm>js.loki.com</realm>
</simple>
</authentication>
<access-point>
<mac>%s</mac>
<signal-strength>%d</signal-strength>
</access-point>
</LocationRQ>""" % (bssid.encode("utf-8"), ss)

    import httplib, socket
    ret = {}
    request_len = len(request)

    log.log_data(request)

    try:
        conn = httplib.HTTPSConnection("api.skyhookwireless.com")
        conn.putrequest("POST", "/wps2/location")
        conn.putheader("Content-type", "text/xml")
        conn.putheader("Content-Length", str(request_len))
        conn.endheaders()
        conn.send(request)
    except (socket.gaierror, socket.error):
        log.debug("Host connection error")
        return {}

    response = conn.getresponse()
    if response.status != 200:
        log.debug("Connection to location server failed")
        return {}

    xml = response.read()
    log.log_data(xml)

    try:
        params = utils.XMLToDictParser().parseXML(xml)
    except xml.parsers.expat.ExpatError:
        return {}

    if 'locationrs-error' in params:
        log.debug("Location server returned failure")
        return {}

    ret['latitude'] = params.get('locationrs-location-latitude', 0)
    ret['longitude'] = params.get('locationrs-location-longitude', 0)
    street_number = params.get('locationrs-location-street-address-street-number', '')
    street_name = params.get('locationrs-location-street-address-address-line', '')
    city = params.get('locationrs-location-street-address-city', '')
    country = params.get('locationrs-location-street-address-country-code', '')

    address = "%s %s, %s, %s" % (street_number, street_name, city, country)
    ret['address'] = address.strip()

    return ret