def update(self, observable, actions):

        (addedcards, removedcards) = actions

        for card in addedcards:
            logger.info("+ Inserted: %s", toHexString(card.atr))

            connection = card.createConnection()
            connection.connect()

            # This will log raw card traffic to console
            connection.addObserver(ConsoleCardConnectionObserver())
      
            # connection object itself is CardConnectionDecorator wrapper
            # and we need to address the underlying connection object
            # directly
            logger.info("Opened connection %s", connection.component)
            desfire = DESFire(PCSCDevice(connection.component))
            key_setting=desfire.getKeySetting()
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,key_setting)
            info=desfire.getCardVersion()
            logger.info(info)
            logger.info('Format card')
            desfire.formatCard()
            logger.info('Create application with ID 00AE16')
            desfire.createApplication("00 AE 16",[DESFireKeySettings.KS_ALLOW_CHANGE_MK,DESFireKeySettings.KS_LISTING_WITHOUT_MK,DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE],14,DESFireKeyType.DF_KEY_AES)
            logger.info('Select application with ID 00AE16')
            desfire.selectApplication('00 AE 16')
            default_key=desfire.createKeySetting('00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00',0,DESFireKeyType.DF_KEY_AES,[])            
            app_key=desfire.createKeySetting('00 10 20 31 40 50 60 70 80 90 A0 B0 B0 A0 90 80',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,default_key)
            logger.info('Cange Key %d',0)
            desfire.changeKey(0,app_key,default_key)
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,app_key)
            desfire.changeKeySettings([ DESFireKeySettings.KS_ALLOW_CHANGE_MK, DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE, DESFireKeySettings.KS_CHANGE_KEY_WITH_KEY_1])
            app_key_1=desfire.createKeySetting('11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',1)
            desfire.changeKey(1,app_key_1,default_key)
            logger.info('Auth Key %d',1)
            desfire.authenticate(1,app_key_1)
            app_key_2=desfire.createKeySetting('22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00 11',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',2)
            desfire.changeKey(2,app_key_2,default_key)
            app_key_3=desfire.createKeySetting('33 44 55 66 77 88 99 AA BB CC DD EE FF 00 11 22',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',3)
            desfire.changeKey(3,app_key_3,default_key)
            app_key_4=desfire.createKeySetting('44 55 66 77 88 99 AA BB CC DD EE FF 00 11 22 33',0,DESFireKeyType.DF_KEY_AES,[])
            logger.info('Cange Key %d',4)
            desfire.changeKey(4,app_key_4,default_key)
            logger.info('Auth Key %d',0)
            desfire.authenticate(0,app_key)
            filePerm=DESFireFilePermissions()
            filePerm.setPerm(0x04,0x03,0x0F,0x02) # key 4 read, key3 write, no key read and write, key2 change permissions
            logger.info('Creat File with ID %d and %d byte',0,32)
            desfire.createStdDataFile(0,filePerm,32) # file Id 0, length 32 byte
            logger.info('Auth Key %d',3)
            desfire.authenticate(3,app_key_3)
            write='00 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20'
            logger.info('Data write %s',write)
            desfire.writeFileData(0,0,32,write)
            logger.info('Auth Key %d',4)
            desfire.authenticate(4,app_key_4)
            read=desfire.readFileData(0,0,32)
            logger.info('Data read %s',byte_array_to_human_readable_hex(read))
def Test_2k3DES():
    reader = DummyPCSCDevice()
    #Get key Settings
    reader.addResponse('45', ['00 0F 01'])
    #Select Application
    reader.addResponse('5A 16 DE 00', ['D5 41 00 00'])
    #Auth DES
    reader.addResponse('1A 00', [
        'AF DE 50 F9 23 10 CA F5 A5', 'AF B2 95 57 99 26 15 5A E3',
        'AF 94 14 81 9C C8 BB 62 C3', 'AF 53 A6 70 D7 8C 0D FF D6'
    ])
    reader.addResponse('AF E0 06 16 66 87 04 D5 54 9C 8D 6A 13 A0 F8 FC ED',
                       ['00 1D 9D 29 54 69 7D E7 60'])
    #Change Key
    reader.addResponse(
        'C4 00 BE DE 0F C6 ED 34 7D CF 0D 51 C7 17 DF 75 D9 7D 2C 5A 2B A6 CA C7 47 9D',
        ['00'])
    #Auth 2
    reader.addResponse('AF 70 F3 49 74 0C 94 5D AE 15 9B A9 FE DB CC 46 1A',
                       ['00 B8 FD 7F E5 6B 24 1F C4'])
    #Change Key 2
    reader.addResponse(
        'C4 00 94 E4 F7 09 DC 2A 2B 07 55 26 10 A1 96 6E 5C 49 EC 90 F6 16 ED EC A5 5B',
        ['00'])
    #Auth 3
    reader.addResponse('AF 93 7E 6B 18 54 A6 D9 2E 0F D9 75 D9 90 90 01 E8',
                       ['00 E0 55 D1 1D D9 53 50 60'])
    #Get key version
    reader.addResponse('64 00', ['00 10 33 45 AA 95 F2 D9 56 CF'])
    #Change Key 3
    reader.addResponse(
        'C4 00 FC 9E 20 FD 77 19 1E 2A AB 0C FD 53 D9 99 99 84 BC 59 E8 86 BF EB 42 D0',
        ['00'])
    #Auth4
    reader.addResponse('AF B3 08 40 8B 57 5A 20 25 25 3D 49 D6 93 CC C2 9C',
                       ['00 52 5B B0 1E 5B 70 B7 94'])
    #change Key other numeber(1)
    reader.addResponse(
        'C4 01 4E B6 69 E4 8D CA 58 47 49 54 2E 1B E8 9C B4 C7 84 5A 38 C5 7D 19 DE 59',
        ['00 2E AD 04 DC F1 21 E0 FE'])
    #change key 1 second
    reader.addResponse(
        'C4 01 FA 7B EF A6 78 2C 93 E8 D6 9C F7 35 2C FD 33 DF 5B C8 AC 4F BA 49 06 FC',
        ['00 CB 0A 50 64 05 51 28 93'])

    desfire = DESFire(reader)
    key_setting = desfire.getKeySetting()
    desfire.authenticate(0, key_setting, 'C9 6C E3 5E 4D 60 87 F2')
    new_key = desfire.createKeySetting(
        '00 10 20 31 40 50 60 70 80 90 A0 B0 B0 A0 90 80', 0,
        DESFireKeyType.DF_KEY_2K3DES, [])
    desfire.changeKey(0, new_key, key_setting)
    desfire.authenticate(0, new_key, '53 0E 3D 90 F7 A2 01 C4')
    new_key2 = desfire.createKeySetting(
        '10 18 20 29 30 38 40 48 50 58 60 68 70 78 80 88', 0,
        DESFireKeyType.DF_KEY_2K3DES, [])
    desfire.changeKey(0, new_key2, new_key)
    desfire.authenticate(0, new_key2, 'DD B0 97 C2 A1 E4 7B 96')
    desfire.getKeyVersion(0)
    new_key3 = desfire.createKeySetting(
        '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00', 0,
        DESFireKeyType.DF_KEY_2K3DES, [])
    desfire.changeKey(0, new_key3, new_key2)
    desfire.authenticate(0, key_setting, 'CB A6 75 E8 EF BA B9 9C')
    new_key4 = desfire.createKeySetting(
        '00 10 20 31 40 50 60 70 80 90 A0 B0 B0 A0 90 80', 0,
        DESFireKeyType.DF_KEY_2K3DES, [])
    desfire.changeKey(1, new_key4, key_setting)
    new_key5 = desfire.createKeySetting(
        '10 18 20 29 30 38 40 48 50 58 60 68 70 78 80 88', 0,
        DESFireKeyType.DF_KEY_2K3DES, [])
    desfire.changeKey(1, new_key5, new_key4)