
    h(                        d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZ ddlmZ ddlmZmZ dd	d
Zi dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;Zd<d=d>d?d@dAdBdCdDZ G dE dFe          Z G dG dHe          Z G dI dJe          Z G dK dLe          Z G dM dNe          Z G dO dPe          Z G dQ dRe          Z G dS dTe          Z  G dU dVe
          Z! G dW dXe           Z" G dY dZe           Z# eeed[\            eeed[]            eeed^_            eee"d`_            eeed^_            eee#d`_           dS )az
PCOM

PCOM is a protocol to communicate with Unitronics PLCs either by serial
or TCP. Two modes are available, ASCII and Binary.

https://unitronicsplc.com/Download/SoftwareUtilities/Unitronics%20PCOM%20Protocol.pdf
    N)Packetbind_layers)TCP)	XShortFieldByteEnumField
XByteFieldStrFixedLenFieldStrLenFieldLEShortFieldLEFieldLenFieldXLE3BytesFieldXLEShortField)	RandShort)bytes_encodeorbasciibinary)e   f   IDzSend Identification CommandCCRzSend Start CommandCCSzSend Stop CommandCCEzSend Reset CommandCCIzSend Init CommandCCzReply of Admin Commands (CC*)UGz
Get UnitIDUSz
Set UnitIDRCzGet RTCSCzSet RTCREzRead InputsRAzRead OutputsGSzRead System BitsGFzRead System IntegersRNHzRead System LongsRNJzRead System Double WordsRBzRead Memory BitszRead Memory IntegerszRead Memory LongszRead Memory Double WordszRead Longs / Double WordszWrite OutputszWrite System BitszWrite System IntegerszWrite System LongszWrite System Double WordszWrite Memory BitszWrite Memory IntegerszWrite Memory LongszWrite Memory Double WordszWrite Longs / Double Words)RWRNLRNDRNSASSSFSNHSNJSBSWSNLSNDSNzGet PLC Name RequestzGet PLC Name ReplyzRead Operands RequestzRead Operands ReplyzRead Data Table RequestzRead Data Table ReplyzWrite Data Table RequestzWrite Data Table Reply)      M            D      c                       e Zd Z ed e                       edde           edd           edd          gZ	d Z
dS )	PCOMtransIdmoder   reservedr   lenNc                 x    | j         /|r-|d d         t          j        dt          |                    z   }||z   S )Nr9   z<H)rB   structpack)selfpktpays      \/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/contrib/scada/pcom.py
post_buildzPCOM.post_buildT   s=    8bqb'FKc#hh777CSy    )__name__
__module____qualname__r   r   r   _protocol_modesr   r   fields_descrJ    rK   rI   r>   r>   L   sn        Iyy{{++fdO44
:t$$UD!!	K    rK   r>   c                       e Zd ZdZdS )PCOMRequestzPCOM/TCP RequestNrL   rM   rN   namerQ   rK   rI   rS   rS   Z   s        DDDrK   rS   c                       e Zd ZdZdS )PCOMResponsezPCOM/TCP ResponseNrT   rQ   rK   rI   rW   rW   ^   s        DDDrK   rW   c                   $    e Zd Zed             ZdS )	PCOMAsciic           	      0   d}t          |           } t          |           D ]\  }}|t          |          z  }t          t	          t
          t          |dz            dd                              d                                                              S )Nr         )	r   	enumerater   listmapordhexzfillupper)commandn_cs       rI   pcom_ascii_checksumzPCOMAscii.pcom_ascii_checksumc   s    w''g&& 	 	DAqQKAACSS\\!""-33A66<<>>??@@@rK   N)rL   rM   rN   staticmethodrh   rQ   rK   rI   rY   rY   b   s2        A A \A A ArK   rY   c                        e Zd Z fdZ xZS )PCOMAsciiCommandFieldc                     t          t          |                               ||          }|dd         }|t          v rt          |         dz   |z   S |dd         }|t          v rt          |         dz   |z   S |S )N   r9       )superrk   i2repr_ascii_command_codes)rF   rG   xscode	__class__s        rI   rq   zPCOMAsciiCommandField.i2reprm   s    '..55c1==1v''''-3a771v''''-3a77rK   rL   rM   rN   rq   __classcell__rv   s   @rI   rk   rk   l   s8                rK   rk   c                       e Zd ZdZ eddd           eddd           edd	d
            edd           edd          gZd Z	dS )PCOMAsciiRequestzPCOM/ASCII Requeststx/rm   unitId00r\   rd    c                      | j         j        dz
  S )N   
underlayerrB   rG   s    rI   <lambda>zPCOMAsciiRequest.<lambda>~       3>3E3I rK   length_fromchksumNetx   c                     | j         Zt                              |dd                   }|d d         t          j        d|d         |d                   z   |dd          z   }||z   S )Nrm   2Br   r   rY   rh   rD   rE   rF   rG   rH   r   s       rI   rJ   zPCOMAsciiRequest.post_build   d    ;223qt9==Fcrc(V[vay&)DDDs233xOCSyrK   
rL   rM   rN   rU   r	   rk   r   r   rP   rJ   rQ   rK   rI   r{   r{   x   s        DQ''4++r'I'I	K 	K 	KHd##
5$K    rK   r{   c                       e Zd ZdZ eddd           eddd           eddd	 
           edd           edd          gZd Z	dS )PCOMAsciiResponsezPCOM/ASCII Responser|   z/Ar\   r~   r   rd   r   c                      | j         j        dz
  S )N   r   r   s    rI   r   zPCOMAsciiResponse.<lambda>   r   rK   r   r   Nr   r   c                     | j         Zt                              |dd                   }|d d         t          j        d|d         |d                   z   |dd          z   }||z   S )Nr\   r   r   r   rm   r   r   r   s       rI   rJ   zPCOMAsciiResponse.post_build   r   rK   r   rQ   rK   rI   r   r      s         Da((4++r'I'I	K 	K 	KHd##
5$K    rK   r   c                   *    e Zd Zed             Zd ZdS )
PCOMBinaryc                 x   d}t          |           } t          |           D ]0\  }}t          |t                    r|nt	          |          }||z  }1|dk    rddgS t          d|dz  z
            dd                              d          }t          |d d         d          t          |dd          d          gS )Nr   i   r\   r9      )r   r]   
isinstanceintr`   ra   rb   )rd   re   rf   rg   two_complements        rI   pcom_binary_checksumzPCOMBinary.pcom_binary_checksum   s    w''g&& 	 	DAq3''3SVVAFAA66$< AK!899!""=CCAFFNrr*B//^ABB5G1L1LMMrK   c                    | j         Zt                              |d d                   }|d d         t          j        d|d         |d                   z   |dd          z   }| j        Zt                              |dd                   }|d d         t          j        d|d         |d                   z   |dd          z   }||z   S )	N      r   rm   r      r   r   )headerChksumPCOMBinaryRequestr   rD   rE   footerChksumr   s       rI   rJ   zPCOMBinary.post_build   s    $&;;CHEEFcrc(V[vay&)DDDs233xOC$&;;C2JGGFcrc(V[vay&)DDDs233xOCSyrK   N)rL   rM   rN   ri   r   rJ   rQ   rK   rI   r   r      s?        
N 
N \
N    rK   r   c                        e Zd Z fdZ xZS )PCOMBinaryCommandFieldc                     t          t          |                               ||          }|t          v rt          |         dz   |z   S |S )Nz - )rp   r   rq   _binary_command_codes)rF   rG   rs   rt   rv   s       rI   rq   zPCOMBinaryCommandField.i2repr   sI    ($//66sA>>%%%(+e3a77HrK   rw   ry   s   @rI   r   r      s8                rK   r   c                   B   e Zd ZdZ eddd           edd           edd           ed	d
           edd           edd           edd           eddd           eddd           e	dd           e
ddd            e	dd           edd          gZdS )r   zPCOM/Binary Requestr|   /_OPLCr   idr   	reserved1   	reserved2rm   	reserved3rd   N	reserved4commandSpecificr   rB   data	length_ofr   c                     | j         S NrB   r   s    rI   r   zPCOMBinaryRequest.<lambda>        rK   r   r   r   \   rL   rM   rN   rU   r	   r   r   r   r   r   r
   rP   rQ   rK   rI   r   r      s         D!,,
4
;%%
;$${C((y$//
;$$*B22qF333nd++FB,?,?@@@nd++
5$KKKrK   r   c                   B   e Zd ZdZ eddd           edd           edd           ed	d
           edd           edd           edd           eddd           eddd           e	dd           e
ddd            e	dd           edd          gZdS )PCOMBinaryResponsezPCOM/Binary Responser|   r   r   r   r   r   r   r   rm   r   rd   Nr   r   r   rB   r   r   r   c                     | j         S r   r   r   s    rI   r   zPCOMBinaryResponse.<lambda>   r   rK   r   r   r   r   r   rQ   rK   rI   r   r      s        !D!,,
;%%
4
;$${C((y$//
;$$*B22qF333nd++FB,?,?@@@nd++
5$KKKrK   r   i O  )dport)sportr   )r@   r   )$__doc__rD   scapy.packetr   r   scapy.layers.inetr   scapy.fieldsr   r   r   r	   r
   r   r   r   r   scapy.volatiler   scapy.compatr   r   rO   rr   r   r>   rS   rW   rY   rk   r{   r   r   r   r   r   rQ   rK   rI   <module>r      s     , , , , , , , , ! ! ! ! ! !3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 % $ $ $ $ $ * * * * * * * * 11 
' 	  
  
	 
 
  	
)  	,  	,  	)  	)  	-  	.  	
  	
   
   
%! " 	
# $ !%
%


!&

!&
&?      F !

!

#
!
$
"	 	     6       $       4   A A A A A A A A	 	 	 	 	K 	 	 	    y   $    	   $       0    Z       
   &       & CE * * * * CU + + + + K) 5 5 5 5 K* 6 6 6 6 L+$ 7 7 7 7 L,4 8 8 8 8 8 8rK   