This file is indexed.

/usr/share/hplip/base/LedmWifi.py is in hplip-data 3.16.11+repack0-3.

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
# -*- coding: utf-8 -*-
#
# (c) Copyright 2003-2015 HP 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: Shunmugaraj.K
#

# StdLib
import time
import io
import binascii
import xml.parsers.expat
from string import *

# Local
from .g import *
from . import device, utils
from .sixext import to_bytes_utf8

http_result_pat = re.compile("""HTTP/\d.\d\s(\d+)""", re.I)
HTTP_OK = 200
HTTP_ACCEPTED = 202
HTTP_NOCONTENT = 204
HTTP_ERROR = 500

MAX_RETRIES = 2

LEDM_WIFI_BASE_URI = "/IoMgmt/Adapters/"

# This payload is working for LaserJet Devices
adapterPowerXml_payload2 ="""<?xml version="1.0" encoding="UTF-8" ?><io:Adapter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:io="http://www.hp.com/schemas/imaging/con/ledm/iomgmt/2008/11/30" xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0/" xmlns:wifi="http://www.hp.com/schemas/imaging/con/wifi/2009/06/26">  <io:HardwareConfig> <dd:Power>%s</dd:Power> </io:HardwareConfig> </io:Adapter>"""

# This payload is working for OfficeJet and Photosmart Devices
adapterPowerXml_payload1 = """<?xml version="1.0" encoding="UTF-8"?><io:Adapters xmlns:io="http://www.hp.com/schemas/imaging/con/ledm/iomgmt/2008/11/30" xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0/"><io:Adapter><io:HardwareConfig><dd:Power>%s</dd:Power></io:HardwareConfig></io:Adapter></io:Adapters>"""

passPhraseXml="""<io:Profile xmlns:io="http://www.hp.com/schemas/imaging/con/ledm/iomgmt/2008/11/30" xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0/" xmlns:wifi="http://www.hp.com/schemas/imaging/con/wifi/2009/06/26" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hp.com/schemas/imaging/con/ledm/iomgmt/2008/11/30 ../../schemas/IoMgmt.xsd http://www.hp.com/schemas/imaging/con/dictionaries/1.0/ ../../schemas/dd/DataDictionaryMasterLEDM.xsd"><io:AdapterProfile><io:WifiProfile><wifi:SSID>%s</wifi:SSID><wifi:CommunicationMode>%s</wifi:CommunicationMode><wifi:EncryptionType>%s</wifi:EncryptionType><wifi:AuthenticationMode>%s</wifi:AuthenticationMode></io:WifiProfile></io:AdapterProfile></io:Profile>"""

keyInfoXml = """<io:KeyInfo><io:WpaPassPhraseInfo><wifi:RsnEncryption>AESOrTKIP</wifi:RsnEncryption><wifi:RsnAuthorization>autoWPA</wifi:RsnAuthorization><wifi:PassPhrase>%s</wifi:PassPhrase></io:WpaPassPhraseInfo></io:KeyInfo>"""

def getAdaptorList(dev):
    ret,params,elementCount,code ={},{},0,HTTP_ERROR         
    max_tries = 0
    while max_tries < MAX_RETRIES:
        max_tries +=1
        URI = LEDM_WIFI_BASE_URI[0:len(LEDM_WIFI_BASE_URI)-1]# to remove "\" from the string
        paramsList,code = readXmlTagDataFromURI(dev,URI,'<io:Adapters', '<io:Adapter>')
        if code == HTTP_OK:
            break

    if code != HTTP_OK:
        log.error("Request Failed With Response Code %d"%code)
        return ret

    ret['adaptorlistlength'] = len(paramsList)
    if len(paramsList) != 0:        
            a = 0
            for params in paramsList:
                ret['adaptorpresence-%d' % a] = ''
                ret['adaptorstate-%d' % a] = ''
                try:
                    ret['adaptorid-%d' % a] = params['io:adapter-map:resourcenode-map:resourcelink-dd:resourceuri']
                except KeyError as e:
                    log.debug("Missing response key: %s" % e)    #changed from error to debug
                    ret['adaptorid-%d' % a]=""
                try:
                    ret['adaptorname-%d' % a] = params['io:adapter-io:hardwareconfig-dd:name']
                except KeyError as e:
                    log.debug("Missing response key: %s" % e)    #changed from error to debug
                    ret['adaptorname-%d' % a] = ""
                try:
                    ret['adaptortype-%d' % a] = params['io:adapter-io:hardwareconfig-dd:deviceconnectivityporttype']
                except KeyError as e:
                    log.debug("Missing response key: %s" % e)    #changed from error to debug
                    ret['adaptortype-%d' % a] = ""
                    
                a = a+1
    return ret   


def getWifiAdaptorID(dev):
    rVal = []

    ret = getAdaptorList(dev)
    try:
        num_adaptors = ret['adaptorlistlength']
    except KeyError:
        num_adaptors = 0

    for n in range(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
                         
def setAdaptorPower(dev, adapterList, power_state='on'):
    adaptor_id=-1
    adaptorName =""
    for a in adapterList:
       adaptor_id = a[0]
       adaptorName = a[1]
       ret,powerXml,URI,code = {},'','',HTTP_ERROR
       URI = LEDM_WIFI_BASE_URI + adaptorName
       powerXml = adapterPowerXml_payload1 %(power_state)  
  
       ret['errorreturn'] = writeXmlDataToURI(dev,URI,powerXml,10)    
       if not(ret['errorreturn'] == HTTP_OK or ret['errorreturn'] == HTTP_NOCONTENT):
          log.debug("Wifi Adapter turn ON request Failed. ResponseCode=%s AdaptorId=%s AdaptorName=%s. Trying another interface" %(ret['errorreturn'],adaptor_id,adaptorName))
          powerXml = adapterPowerXml_payload2 %(power_state)
          ret['errorreturn'] = writeXmlDataToURI(dev,URI,powerXml,10)

       if not(ret['errorreturn'] == HTTP_OK or ret['errorreturn'] == HTTP_NOCONTENT):
          log.error("Wifi Adapter turn ON request Failed. ResponseCode=%s AdaptorId=%s AdaptorName=%s" %(ret['errorreturn'],adaptor_id,adaptorName))
       else:
          log.debug("Wifi Adapter turn ON request is Success. AdaptorId=%s AdaptorName=%s" %(adaptor_id,adaptorName))
#          adapaterState = a[2], adapterPresence= a[3] 
          return adaptor_id, adaptorName, a[2], a[3] 

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

def performScan(dev, adapterName, ssid=None):
    ret ={}

    if ssid is None:
        URI = LEDM_WIFI_BASE_URI + adapterName + "/WifiNetworks"
    else:
        URI = LEDM_WIFI_BASE_URI + adapterName + "/WifiNetworks/SSID="+ssid 

    while True:            
        params,code,elementCount = readXmlDataFromURI(dev,URI,'<io:WifiNetworks', '<io:WifiNetwork>',10)        
        if code == HTTP_ACCEPTED:
            continue
        else:
            break  

    ret['numberofscanentries'] = elementCount 
    if code != HTTP_OK:
        log.error("Request Failed With Response Code %d"%code)
        return ret
       
    if params is not None:              
        if elementCount == 1:
            try:
                ssid = binascii.unhexlify(str(params['io:wifinetworks-io:wifinetwork-wifi:ssid']).encode('utf-8')).decode("utf-8")
                if not ssid:
                    ret['ssid-0'] = to_unicode('(unknown)')
                else:
                    ret['ssid-0'] = ssid
                try:
                    ret['bssid-0'] = binascii.unhexlify(str(params['io:wifinetworks-io:wifinetwork-wifi:bssid']).encode('utf-8')).decode("utf-8")
                except:
                    ret['bssid-0'] = params['io:wifinetworks-io:wifinetwork-wifi:bssid']
                                   
                ret['channel-0'] = params['io:wifinetworks-io:wifinetwork-wifi:channel']
                ret['communicationmode-0'] = params['io:wifinetworks-io:wifinetwork-wifi:communicationmode']
                ret['dbm-0'] = params['io:wifinetworks-io:wifinetwork-io:signalinfo-wifi:dbm']
                ret['encryptiontype-0'] = params['io:wifinetworks-io:wifinetwork-wifi:encryptiontype']
                ret['signalstrength-0'] = params['io:wifinetworks-io:wifinetwork-io:signalinfo-wifi:signalstrength']                
            except KeyError as e:
                log.debug("Missing response key: %s" % e)  
        else:
            for a in range(elementCount):
                try:
                    try:
                        ssid = binascii.unhexlify(str(params['io:wifinetworks-io:wifinetwork-wifi:ssid-%d' % a]).encode('utf-8')).decode('utf-8')
                    except TypeError: 
                        # Some devices returns one invalid SSID (i.e. 0) along with valid SSIDs. e.g. Epic.
                        ssid = params['io:wifinetworks-io:wifinetwork-wifi:ssid-%d' % a]

                    if not ssid:
                        ret['ssid-%d' % a] = to_unicode('(unknown)')
                    else:
                        ret['ssid-%d' % a] = ssid
                    try:
                        ret['bssid-%d' % a] = binascii.unhexlify(str(params['io:wifinetworks-io:wifinetwork-wifi:bssid-%d' % a]).encode('utf-8')).decode("utf-8")
                    except:
                        ret['bssid-%d' % a] = params['io:wifinetworks-io:wifinetwork-wifi:bssid-%d' % a]
                    ret['channel-%d' % a] = params['io:wifinetworks-io:wifinetwork-wifi:channel-%d' % a]
                    ret['communicationmode-%d' % a] = params['io:wifinetworks-io:wifinetwork-wifi:communicationmode-%d' % a]
                    ret['dbm-%d' % a] = params['io:wifinetworks-io:wifinetwork-io:signalinfo-wifi:dbm-%d' % a]
                    ret['encryptiontype-%d' % a] = params['io:wifinetworks-io:wifinetwork-wifi:encryptiontype-%d' % a]
                    ret['signalstrength-%d' % a] = params['io:wifinetworks-io:wifinetwork-io:signalinfo-wifi:signalstrength-%d' % a]                        
                
                except KeyError as e:
                    log.debug("Missing response key: %s" % e)  
                try:                    
                    ret['signalstrengthmax'] = 5
                    ret['signalstrengthmin'] = 0
                except KeyError as e:
                    log.debug("Missing response key: %s" % e)       
    return ret    

def getIPConfiguration(dev, adapterName):
    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'
    protocol = 'old'
    URI = LEDM_WIFI_BASE_URI + adapterName + "/Protocols"
    #URI = "/DevMgmt/IOConfigDyn.xml"
    params,code,elementCount = {},HTTP_ERROR,0  
    max_tries = 0

    while max_tries < MAX_RETRIES:
        max_tries +=1
        params,code,elementCount = readXmlDataFromURI(dev,URI,'<io:Protocol', '<io:Protocol')
        if code == HTTP_OK:
            break 
     
    if code != HTTP_OK:
        max_tries = 0
        URI = "/DevMgmt/IOConfigDyn.xml"
        while max_tries < MAX_RETRIES:
            max_tries +=1
            params,code,elementCount = readXmlDataFromURI(dev,URI,'<iocfgdyn2:IOConfigDyn', '<dd3:IOAdaptorConfig')
            if code == HTTP_OK:
                protocol = 'new'
                break 
    if code != HTTP_OK:
        log.error("Request Failed With Response Code %d" %code)
        return ip, hostname, addressmode, subnetmask, gateway, pridns, sec_dns

    if protocol == 'old':
        if params is not None and code == HTTP_OK:
            try:
                ip = params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:ipv4address']            
                subnetmask = params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:subnetmask']
                gateway = params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:defaultgateway']
            
                if 'DHCP' in params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:configmethod']:
                    addressmode = 'dhcp'
                else:
                    addressmode = 'autoip'    
                    if elementCount ==1:
                        pridns = params['io:protocols-io:protocol-dd:dnsserveripaddress']
                        sec_dns = params['io:protocols-io:protocol-dd:secondarydnsserveripaddress']          
                        for a in range(elementCount):
                            if params['io:protocols-io:protocol-dd:dnsserveripaddress-%d' %a] !="::":
                                pridns = params['io:protocols-io:protocol-dd:dnsserveripaddress-%d' %a]
                                sec_dns = params['io:protocols-io:protocol-dd:secondarydnsserveripaddress-%d' %a]
                                break
            except KeyError as e:
                log.error("Missing response key: %s" % str(e))
    else:
        if params is not None and code == HTTP_OK:
            try:
            #ip = params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:ipv4address']
                try:
                    ip = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:ipaddress']
                except:
                    ip = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:ipaddress-0']

                #subnetmask = params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:subnetmask']
                try:
                    subnetmask = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:subnetmask']
                except:
                    subnetmask = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:subnetmask-0']

                #gateway = params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:defaultgateway']
                try:
                    gateway = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:defaultgateway']
                except:
                    gateway = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:defaultgateway-0']

                #if 'DHCP' in params['io:protocols-io:protocol-io:addresses-io:ipv4addresses-io:ipv4address-dd:configmethod']:
                try:
                    addressmode = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:ipconfigmethod'] 
                except:
                    addressmode = params['iocfgdyn2:ioconfigdyn-dd3:ioadaptorconfig-dd3:networkadaptorconfig-dd3:ipversionconfig-dd3:ipconfig-dd:ipconfigmethod-0'] 

                if 'dhcp' in addressmode.lower():
                    addressmode = 'dhcp'
                else:
                    addressmode = 'autoip'

            #if elementCount ==1:
            #    pridns = params['io:protocols-io:protocol-dd:dnsserveripaddress']
            #    sec_dns = params['io:protocols-io:protocol-dd:secondarydnsserveripaddress']          
            #for a in xrange(elementCount):
            #    if params['io:protocols-io:protocol-dd:dnsserveripaddress-%d' %a] !="::":
            #        pridns = params['io:protocols-io:protocol-dd:dnsserveripaddress-%d' %a]
            #        sec_dns = params['io:protocols-io:protocol-dd:secondarydnsserveripaddress-%d' %a]
            #        break
            except KeyError as e:
                log.error("Missing response key: %s" % str(e))        

    log.debug("ip=%s, hostname=%s, addressmode=%s, subnetmask=%s, gateway=%s, pridns=%s, sec_dns=%s"%(ip, hostname, addressmode, subnetmask, gateway, pridns, sec_dns))
    return ip, hostname, addressmode, subnetmask, gateway, pridns, sec_dns  




# TODO: Temporary Function. To be removed after refactoring.
def getwifiotherdetails(dev,adapterName):
    ip, subnet, gateway, pri_dns, sec_dns, mode = '', '', '', '', '', ''
    params1, params2, code1, code2, elementCount ={}, {}, HTTP_ERROR, HTTP_ERROR,0
    URI1 = LEDM_WIFI_BASE_URI + adapterName + "/Profiles/Active"
    URI2 = "/IoMgmt/IoConfig.xml" 
    max_tries = 0

    while max_tries < MAX_RETRIES:
        max_tries +=1
        params1, code1, elementCount = readXmlDataFromURI(dev,URI1,'<io:Profile', '<io:Profile')
        params2, code2, elementCount = readXmlDataFromURI(dev,URI2,'<io:IoConfig', '<io:IoConfig')
        if code1 == HTTP_OK and code2 == HTTP_OK:
            break 

    if code1 !=HTTP_OK and code2 != HTTP_OK:
        log.error("Request Failed With Response Code %d" %code)
        return ip, subnet, gateway, pri_dns, sec_dns 
	
    if params1 is not None and params2 is not None:
        try:
            ip = params1['io:profile-io:networkprofile-io:ipv4network-dd:ipaddress']
            subnet = params1['io:profile-io:networkprofile-io:ipv4network-dd:subnetmask']
            gateway = params1['io:profile-io:networkprofile-io:ipv4network-dd:defaultgateway']
            pri_dns  = params1['io:profile-io:networkprofile-io:ipv4network-dd:dnsserveripaddress']
            sec_dns = params1['io:profile-io:networkprofile-io:ipv4network-dd:secondarydnsserveripaddress']
            mode = params2['io:ioconfig-io:iodeviceprotocolconfig-io:ipv4domainname-dd:domainnameconfig-dd:configmethod']

        except KeyError as e:
            log.debug("Missing response key: %s" % str(e))
    return ip, subnet, gateway, pri_dns, sec_dns, mode
	
def getCryptoSuite(dev, adapterName):
    alg, mode, secretid = '', '', ''
    parms,code,elementCount ={},HTTP_ERROR,0
    URI = LEDM_WIFI_BASE_URI + adapterName + "/Profiles/Active"
    max_tries = 0
    
    while max_tries < MAX_RETRIES:
        max_tries +=1
        parms,code,elementCount = readXmlDataFromURI(dev,URI,'<io:Profile', '<io:Profile')
        if code == HTTP_OK:
            break 
    
    if code !=HTTP_OK:
        log.error("Request Failed With Response Code %d" %code)
        return  alg, mode, secretid

    if parms is not None:        
        try:
            mode = parms['io:profile-io:adapterprofile-io:wifiprofile-wifi:communicationmode']
            alg = parms['io:profile-io:adapterprofile-io:wifiprofile-wifi:encryptiontype']
            secretid = parms['io:profile-io:adapterprofile-io:wifiprofile-wifi:bssid']    
        except KeyError as e:
            log.debug("Missing response key: %s" % str(e))
    
    return  alg, mode, secretid


def associate(dev, adapterName, ssid, communication_mode, encryption_type, key):
    ret,code = {},HTTP_ERROR    
    URI = LEDM_WIFI_BASE_URI + adapterName + "/Profiles/Active"

    if encryption_type == 'none':
        authMode = 'open'
        ppXml = passPhraseXml%(binascii.hexlify(to_bytes_utf8(ssid)).decode('utf-8'), communication_mode,encryption_type,authMode)
    else:
        authMode = encryption_type
        pos = passPhraseXml.find("</io:WifiProfile>",0,len(passPhraseXml))
        ppXml = (passPhraseXml[:pos] + keyInfoXml + passPhraseXml[pos:])%(binascii.hexlify(to_bytes_utf8(ssid)).decode('utf-8'),communication_mode,encryption_type,\
        authMode,binascii.hexlify(to_bytes_utf8(key)).decode('utf-8'))        

    code = writeXmlDataToURI(dev,URI,ppXml,10)    
    ret['errorreturn'] = code
    if not(code == HTTP_OK or HTTP_NOCONTENT):
        log.error("Request Failed With Response Code %d" % ret['errorreturn'])
    
    return ret


def getVSACodes(dev, adapterName):
    ret,params,code,elementCount = [],{},HTTP_ERROR,0
    severity,rule ='',''
    URI = LEDM_WIFI_BASE_URI + adapterName + "/VsaCodes.xml"
    max_tries = 0
    
    while max_tries < MAX_RETRIES:
        max_tries +=1
        params,code,elementCount = readXmlDataFromURI(dev,URI,"<io:VsaCodes","<io:VsaCodes",10)
        if code == HTTP_OK:
            break
    
    if code != HTTP_OK:
        log.warn("Request Failed With Response Code %d"%code)
        return ret
 
    if params is not None:
        try:
            severity= params['io:vsacodes-wifi:vsacode-dd:severity']
        except:
            severity = ""
        try:
            rule = params['io:vsacodes-wifi:vsacode-wifi:rulenumber']            
       # except KeyError, e:
           # log.error("Missing response key: %s" % str(e))
        except:
            rule = ""
        ret.append((rule, severity))       
    return ret  


def getHostname(dev):
    hostName = ''
    URI = "/IoMgmt/IoConfig.xml"    
    max_tries = 0
    
    while max_tries < MAX_RETRIES:
        max_tries +=1
        params,code,elementCount = readXmlDataFromURI(dev,URI,'<io:IoConfig', '<io:IoConfig')        
        if code == HTTP_OK:
            break    
    
    if code != HTTP_OK:
        log.warn("Request failed with Response code %d. HostName not found."%code)
        return  hostName
       
    if params is not None:        
        try:               
            hostName = params['io:ioconfig-io:iodeviceconfig-dd3:hostname']            
        except KeyError as e:
            log.debug("Missing response key: %s" % e)

    return  hostName

def getSignalStrength(dev, adapterName, ssid, adaptor_id=0):
    ss_max, ss_min, ss_val, ss_dbm = 5, 0, 0, -200
    params,code,elementCount = {},HTTP_ERROR,0

    if ssid is not None:      
        URI = LEDM_WIFI_BASE_URI + adapterName + "/WifiNetworks/SSID="+ssid 
    else:
        return ss_max, ss_min, ss_val, ss_dbm

    while True:            
        params,code,elementCount = readXmlDataFromURI(dev,URI,'<io:WifiNetworks', '<io:WifiNetwork>',10)        
        if code == HTTP_ACCEPTED:
            log.info("Got Response as HTTP_ACCEPTED, so retrying to get the actual result")
            continue
        else:
            break  

    if code != HTTP_OK:
        log.error("Request Failed With Response Code %d"%code)
        return ss_max, ss_min, ss_val, ss_dbm
       
    if params is not None:        
        if elementCount == 1:
            try:                
                ss_dbm = params['io:wifinetworks-io:wifinetwork-io:signalinfo-wifi:dbm']                
                ss_val = params['io:wifinetworks-io:wifinetwork-io:signalinfo-wifi:signalstrength']                
            except KeyError as e:
                log.error("Missing response key: %s" % e)

    return  ss_max, ss_min, ss_val, ss_dbm


def readXmlTagDataFromURI(dev,URI,xmlRootNode,xmlReqDataNode,timeout=5):
    paramsList,code =[],HTTP_ERROR
    
    data = format_http_get(URI,0,"")
    log.info(data)
    response = io.BytesIO()
    if dev.openLEDM() == -1:
        dev.closeLEDM()
        if dev.openEWS_LEDM() == -1:
            
            dev.openMarvell_EWS()
            dev.writeMarvell_EWS(data)
            try:
                while dev.readMarvell_EWS(1024, response, timeout):
                    pass
            except Error:
                dev.closeMarvell_EWS()
                log.error("Unable to read Marvell_EWS Channel")
        else:
            dev.writeEWS_LEDM(data)
            try:
                dev.readLEDMData(dev.readEWS_LEDM, response, timeout)
            except Error:
                dev.closeEWS_LEDM()
                log.error("Unable to read EWS_LEDM Channel")
    else:
        dev.writeLEDM(data)
        try:
            dev.readLEDMData(dev.readLEDM, response, timeout)
        except Error:
            dev.closeLEDM()
            log.error("Unable to read LEDM Channel")
        
    strResp = response.getvalue().decode('utf-8')
    if strResp is not None:                             
        code = get_error_code(strResp)
        if code == HTTP_OK:
            strResp = utils.unchunck_xml_data(strResp)
            pos = strResp.find(xmlRootNode,0,len(strResp))    
            repstr = strResp[pos:].strip()
            repstr = repstr.replace('\r',' ').replace('\t',' ').replace('\n',' ') # To remove formating characters from the received xml
            repstr = repstr.rstrip('0')   # To remove trailing zero from the received xml
            try:
                parser_object = utils.extendedExpat()
                root_element = parser_object.Parse(repstr)
                xmlReqDataNode = ''.join(l for l in filter(lambda x: x not in '<>', xmlReqDataNode)) # [c for c in xmlReqDataNode if c not in "<>"] # To remove '<' and '>' characters
                reqDataElementList = root_element.getElementsByTagName(xmlReqDataNode)
                for node in reqDataElementList:
                    repstr = node.toString()
                    repstr = repstr.replace('\r',' ').replace('\t',' ').replace('\n',' ') # To remove formating characters from the received xml
                    params = utils.XMLToDictParser().parseXML(to_bytes_utf8(repstr))
                    paramsList.append(params)
            except xml.parsers.expat.ExpatError as e:
                log.debug("XML parser failed: %s" % e)  #changed from error to debug 
        else:
            log.debug("HTTP Responce failed with %s code"%code)
    return paramsList,code



def readXmlDataFromURI(dev,URI,xmlRootNode,xmlChildNode,timeout=5):
    params,code,elementCount ={},HTTP_ERROR,0 
    
    data = format_http_get(URI,0,"")
    log.info(data)
    response = io.BytesIO()
    if dev.openLEDM() == -1:
        dev.closeLEDM()
        if dev.openEWS_LEDM() == -1:
            
            dev.openMarvell_EWS()
            dev.writeMarvell_EWS(data)
            try:
                while dev.readMarvell_EWS(1024, response, timeout):
                    pass
            except Error:
                dev.closeMarvell_EWS()
                log.error("Unable to read Marvell_EWS Channel")
        else:
            dev.writeEWS_LEDM(data)
            try:
                dev.readLEDMData(dev.readEWS_LEDM, response,timeout)
            except Error:
                dev.closeEWS_LEDM()
                log.error("Unable to read EWS_LEDM Channel")
    else:
        dev.writeLEDM(data)
        try:
            dev.readLEDMData(dev.readLEDM, response,timeout)
        except Error:
            dev.closeLEDM()
            log.error("Unable to read LEDM Channel") 
    #dev.closeEWS_LEDM()    
    strResp = response.getvalue().decode('utf-8')
    if strResp is not None:                             
        code = get_error_code(strResp)
        if code == HTTP_OK:
            #strResp = utils.unchunck_xml_data(strResp)
            strResp = utils.extract_xml_chunk(strResp)
            pos = strResp.find(xmlRootNode,0,len(strResp))    
            repstr = strResp[pos:].strip()
            repstr = repstr.replace('\r',' ').replace('\t',' ').replace('\n',' ') # To remove formating characters from the received xml
            repstr = repstr.rstrip('0')   # To remove trailing zero from the received xml
            elementCount = repstr.count(xmlChildNode)
            try:
                params = utils.XMLToDictParser().parseXML(repstr)            
            except xml.parsers.expat.ExpatError as e:
                log.debug("XML parser failed: %s" % e)  #changed from error to debug 
        else:
            log.debug(" HTTP Responce failed with %s code"%code)

    return params,code,elementCount


def writeXmlDataToURI(dev,URI,xml,timeout=5):
    code = HTTP_ERROR

    data = format_http_put(URI,len(xml),xml)
    response = io.BytesIO()

    if dev.openLEDM() == -1:
        if dev.openEWS_LEDM() == -1:
            dev.openMarvell_EWS()
            dev.writeMarvell_EWS(data)
            try:
               while dev.readMarvell_EWS(1000, response, timeout):
                   pass
            except Error:
                dev.closeMarvell_EWS()
                log.error("Unable to read Marvell_EWS Channel")
        else:
            dev.writeEWS_LEDM(data)
            try:
                dev.readLEDMData(dev.readEWS_LEDM, response, timeout)
            except Error:
                dev.closeEWS_LEDM()
                log.error("Unable to read EWS_LEDM Channel")
            
    else:
        dev.writeLEDM(data)
        try:
            dev.readLEDMData(dev.readLEDM, response,timeout )
        except Error:
            dev.closeLEDM()
            log.error("Unable to read LEDM Channel") 
        

    strResp = response.getvalue().decode('utf-8')
    if strResp is not None:
        code = get_error_code(strResp)           
    return code


def get_error_code(ret):
    if not ret: return HTTP_ERROR
    match = http_result_pat.match(ret)
    if match is None: return HTTP_ERROR
    try:
        code = int(match.group(1))
    except (ValueError, TypeError):
        code = HTTP_ERROR
    return code


def format_http_get(requst, ledmlen, xmldata, content_type="text/xml; charset=utf-8"):
    host = 'localhost'
    return  utils.cat(
"""GET $requst HTTP/1.1\r
Host: $host\r
User-Agent: hplip/3.0\r
Content-Type: $content_type\r
Content-Length: $ledmlen\r
\r
$xmldata""")


def format_http_put(requst, ledmlen, xmldata, content_type="text/xml; charset=utf-8"):
    host = 'localhost'
    return  utils.cat(
"""PUT $requst HTTP/1.1\r
Host: $host\r
User-Agent: hplip/3.0\r
Content-Type: $content_type\r
Content-Length: $ledmlen\r
\r
$xmldata""")