
    di;                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ  G d d	          Z G d
 dej                  ZdS )z) pySim: tell old 2G SIMs apart from UICC
    N)List)SwMatchError)SimCardCommands)CardApplicationinterpret_sw)all_subclassesc                      e Zd ZdZd Zd ZdefdZdefdZ	e
ded	efd
            Ze
dedededee         d	ef
d            Zeej        ded	dfd                        Zeded	efd            Ze
defd            ZddZdS )CardProfilezA Card Profile describes a card, it's filesystem hierarchy, an [initial] list of
    applications as well as profile-specific SW and shell commands.  Every card has
    one card profile, but there may be multiple applications within that profile.c                    || _         |                    dd          | _        |                    dg           | _        |                    di           | _        |                    dg           | _        |                    dg           | _        |                    dd          | _        |                    d	d
          | _        |                    dg           | _	        dS )aw  
        Args:
                desc (str) : Description
                files_in_mf : List of CardEF instances present in MF
                applications : List of CardApplications present on card
                sw : List of status word definitions
                shell_cmdsets : List of cmd2 shell command sets of profile-specific commands
                cla : class byte that should be used with cards of this profile
                sel_ctrl : selection control bytes class byte that should be used with cards of this profile
                addons: List of optional CardAddons that a card of this profile might have
        descNfiles_in_mfswapplicationsshell_cmdsetscla00sel_ctrl0004addons)
namegetr   r   r   r   r   r   r   r   selfr   kws      ;/home/jenkins/workspace/simtester-sanitize/pySim/profile.py__init__zCardProfile.__init__&   s     	FF64((	66-44&&r""FF>266VVOR8866%&&z622ffXr**    c                     | j         S Nr   r   s    r   __str__zCardProfile.__str__=   
    yr   appc                 :    | j                             |           dS )zAdd an application to a card profile.

        Args:
                app : CardApplication instance to be added to profile
        N)r   append)r   r$   s     r   add_applicationzCardProfile.add_application@   s!     	  %%%%%r   r   c                 ,    t          | j        |          S )zInterpret a given status word within the profile.

        Args:
                sw : Status word as string of 4 hex digits

        Returns:
                Tuple of two strings
        )r   r   )r   r   s     r   r   zCardProfile.interpret_swH   s     DGR(((r   data_hexreturnc                     | S )a  Decode the response to a SELECT command.

        This is the fall-back method which doesn't perform any decoding. It mostly
        exists so specific derived classes can overload it for actual decoding.
        This method is implemented in the profile and is only used when application
        specific decoding cannot be performed (no ADF is selected).

        Args:
                data_hex: Hex string of the select response
         )r)   s    r   decode_select_responsez"CardProfile.decode_select_responseS   s	     r   scccla_byter   fidsc                 ~    |                                   || _        || _        |D ]}|                     |           dS )z?Helper function used by some derived _try_match_card() methods.N)
reset_cardr/   r   select_file)r.   r/   r   r0   fids        r   _mf_select_testzCardProfile._mf_select_testa   sP    
 	 	! 	!COOC    	! 	!r   Nc                     dS )a4  Try to see if the specific profile matches the card. This method is a
        placeholder that is overloaded by specific derived classes. The method
        actively probes the card to make sure the profile class matches the
        physical card. This usually also means that the card is reset during
        the process, so this method must not be called at random times. It may
        only be called on startup.  If there is no exception raised, we assume
        the card matches the profile.

        Args:
                scc: SimCardCommands class
        Nr,   )clsr.   s     r   _try_match_cardzCardProfile._try_match_cardm   s	     	r   c                 H   |j         }|j        }	 |                     |           	 ||_         ||_        |                                 dS # t          $ r& Y ||_         ||_        |                                 dS w xY w# ||_         ||_        |                                 w xY w)a  Check if the specific profile matches the card. The method
        actively probes the card to make sure the profile class matches the
        physical card. This usually also means that the card is reset during
        the process, so this method must not be called at random times. It may
        only be called on startup.

        Args:
                scc: SimCardCommands class
        Returns:
                match = True, no match = False
        TF)r   r/   r8   r2   r   )r7   r.   
sel_backup
cla_backups       r   match_with_cardzCardProfile.match_with_card}   s     \
\
	$$$ &CL%CLNN  	 	 	%CL%CLNN	 &CL%CLNNs#   A
 

A:A= 9A::A= =$B!c                     t          t          t                              }|                    t	          j        d                     |D ]#}|                    |           r |            c S $d S )NORDER)key)listr   r
   sortoperator
attrgetterr<   )r.   profilesps      r   pickzCardProfile.pick   sv    {3344(-g66777 	 	A  %% qss


 tr   addonCardProfileAddonc                 P    || j         vsJ | j                             |           d S r   )r   r&   )r   rG   s     r   	add_addonzCardProfile.add_addon   s1    DK''''5!!!!!r   )rG   rH   )__name__
__module____qualname____doc__r   r"   r   r'   strr   staticmethodobjectr-   r   r   boolr5   classmethodabcabstractmethodr8   r<   rF   rJ   r,   r   r   r
   r
   !   s       U U+ + +.  &? & & & &	)s 	) 	) 	) 	)      \ 	!_ 	!"%	!14	!"3i	!,0	! 	! 	! \	! / d     [ / d    [0 /    \" " " " " "r   r
   c                   N    e Zd ZdZdefdZd Zej        ddde	fd            Z
d	S )
rH   zA Card Profile Add-on is something that is not a card application or a full stand-alone
    card profile, but an add-on to an existing profile.  Think of GSM-R specific files existing
    on what is otherwise a SIM or USIM+SIM card.r   c                     || _         |                    dd          | _        |                    dg           | _        |                    dg           | _        dS )z
        Args:
                desc (str) : Description
                files_in_mf : List of CardEF instances present in MF
                shell_cmdsets : List of cmd2 shell command sets of profile-specific commands
        r   Nr   r   )r   r   r   r   r   r   s      r   r   zCardProfileAddon.__init__   sR     	FF64((	66-44VVOR88r   c                     | j         S r   r    r!   s    r   r"   zCardProfileAddon.__str__   r#   r   cardCardBaser*   c                     dS )zPProbe a given card to determine whether or not this add-on is present/supported.Nr,   )r   rY   s     r   probezCardProfileAddon.probe   s      r   N)rK   rL   rM   rN   rO   r   r"   rT   rU   rR   r\   r,   r   r   rH   rH      s        4 4
9S 
9 
9 
9 
9   	_* _ _ _ _ _ _ _r   rH   )rN   rT   rB   typingr   pySim.exceptionsr   pySim.commandsr   pySim.filesystemr   r   pySim.utilsr   r
   ABCrH   r,   r   r   <module>rc      s    * 


        ) ) ) ) ) ) * * * * * * : : : : : : : : & & & & & &C" C" C" C" C" C" C" C"J_ _ _ _ _sw _ _ _ _ _r   