
    >qaE                         d Z ddlZddlZddlmZ  G d de          Z G d de          Zd	 Zd
 Z	d Z
d Zd Zd Zd Zd Z G d d          Z G d de          ZdS )a  
*******************************************************************
  Copyright (c) 2017, 2019 IBM Corp.

  All rights reserved. This program and the accompanying materials
  are made available under the terms of the Eclipse Public License v2.0
  and Eclipse Distribution License v1.0 which accompany this distribution.

  The Eclipse Public License is available at
     http://www.eclipse.org/legal/epl-v10.html
  and the Eclipse Distribution License is available at
    http://www.eclipse.org/org/documents/edl-v10.php.

  Contributors:
     Ian Craggs - initial implementation and/or documentation
*******************************************************************
    N   )PacketTypesc                       e Zd ZdS )MQTTExceptionN__name__
__module____qualname__     H/opt/windhager/venv/lib/python3.11/site-packages/paho/mqtt/properties.pyr   r              Dr   r   c                       e Zd ZdS )MalformedPacketNr   r   r   r   r   r      r   r   r   c                 F    t          t          j        d|                     S )N!H	bytearraystructpacklengths    r   
writeInt16r   !       V[v..///r   c                 H    t          j        d| d d                   d         S )Nr      r   r   unpackbufs    r   	readInt16r!   &   !    =s2A2w''**r   c                 F    t          t          j        d|                     S )N!Lr   r   s    r   
writeInt32r%   +   r   r   c                 H    t          j        d| d d                   d         S )Nr$      r   r   r   s    r   	readInt32r(   0   r"   r   c                     t           j        d         dk     rt          | d          } n2t          |           t          d          k    r| nt	          | d          } t          t          |                     | z   S )Nr      utf-8r   )sysversion_infor   typebytesr   len)datas    r   writeUTFr2   5   sh    
Qw''DzzT#YY..ttE$4H4Hc$ii  4''r   c                    |dk    rt          |           }nt          d          |dz  }||k    rt          d          | dd|z                                d          }|D ]V}t          |          }|dk    r|dk    rt          d          |dk    rt          d	          |d
k    rt          d          W||dz   fS )Nr   z%Not enough data to read string lengthz Length delimited string too longr+   i   i  z,[MQTT-1.5.4-1] D800-DFFF found in UTF-8 datar   z'[MQTT-1.5.4-2] Null found in UTF-8 datai  z#[MQTT-1.5.4-3] U+FEFF in UTF-8 data)r!   r   decodeord)buffermaxlenr   r    cord_cs         r   readUTFr:   >   s    {{6""EFFF
aKF@AAA
1V8

#
#G
,
,C I IAF??u!"PQQQD==!"KLLLF??!"GHHH q=r   c                 @    t          t          |                     | z   S N)r   r0   )r6   s    r   
writeBytesr=   S   s    c&kk""V++r   c                 D    t          |           }| dd|z            |dz   fS )Nr   )r!   )r6   r   s     r   	readBytesr?   W   s+    vF!AfH*vax''r   c                   >    e Zd ZdZed             Zed             ZdS )VariableByteIntegerszh
    MQTT variable byte integer helper class.  Used
    in several places in MQTT v5.0 properties.

    c                     d| cxk    rdk    sn J d}	 | dz  }| dz  } | dk    r|dz  }t           j        d         dk    r|t          |g          z  }n|t          t          |                    z  }| dk    rnf|S )z
          Convert an integer 0 <= x <= 268435455 into multi-byte format.
          Returns the buffer convered from the integer.
        r   r   r      r*   )r,   r-   r/   chr)xr6   digits      r   encodezVariableByteIntegers.encodec   s     A""""""""""
	GE#IA1uu"a''%..(%E

+++Avv
	 r   c                 |    d}d}d}	 |dz  }| d         }| dd         } ||dz  |z  z  }|dz  dk    rn|dz  }2||fS )z
          Get the value of a multi-byte integer from a buffer
          Return the value, and the number of bytes used.

          [MQTT-1.5.5-1] the encoded value MUST use the minimum number of bytes necessary to represent the value
        r   r   N   rD   r   )r6   
multipliervaluer/   rG   s        r   r4   zVariableByteIntegers.decodex   sw     
	QJE1IEABBZFeckZ//Es{a#J	 u~r   N)r   r	   r
   __doc__staticmethodrH   r4   r   r   r   rA   rA   \   sT            \(   \  r   rA   c                   `    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd ZdS )
Propertiesab  MQTT v5.0 properties class.

    See Properties.names for a list of accepted property names along with their numeric values.

    See Properties.properties for the data type of each property.

    Example of use:

        publish_properties = Properties(PacketTypes.PUBLISH)
        publish_properties.UserProperty = ("a", "2")
        publish_properties.UserProperty = ("c", "3")

    First the object is created with packet type as argument, no properties will be present at
    this point.  Then properties are added as attributes, the name of which is the string property
    name without the spaces.

    c                 >   || _         g d| _        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&d'd(d)d*d+d,d-d.
| _        i d| j                            d/          t          j        t          j        gfd| j                            d0          t          j        t          j        gfd| j                            d1          t          j        t          j        gfd	| j                            d1          t          j        t          j        gfd| j                            d2          t          j        t          j        gfd| j                            d3          t          j        t          j        gfd| j                            d0          t          j        t          j	        t          j
        gfd| j                            d1          t          j	        gfd| j                            d4          t          j	        gfd| j                            d1          t          j        t          j	        t          j        gfd| j                            d2          t          j        t          j	        t          j        gfd| j                            d/          t          j        gfd| j                            d0          t          j        gfd| j                            d/          t          j        gfd| j                            d1          t          j	        gfd!| j                            d1          t          j	        t          j
        gfd#| j                            d1          t          j	        t          j        t          j        t          j        t          j        t          j        t          j        t          j
        t          j        g	f| j                            d4          t          j        t          j	        gf| j                            d4          t          j        t          j	        gf| j                            d4          t          j        gf| j                            d/          t          j	        gf| j                            d/          t          j	        gf| j                            d5          t          j        t          j	        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j
        t          j        t          j        gf| j                            d0          t          j        t          j	        gf| j                            d/          t          j	        gf| j                            d/          t          j	        gf| j                            d/          t          j	        gfd6
| _        d S )7N)ByteTwo Byte IntegerFour Byte IntegerVariable Byte IntegerBinary DataUTF-8 Encoded StringUTF-8 String PairzPayload Format Indicatorr   zMessage Expiry Intervalr   zContent Typer*   zResponse Topic   zCorrelation Data	   zSubscription Identifier   zSession Expiry Interval   zAssigned Client Identifier   zServer Keep Alive   zAuthentication Method   zAuthentication Data   zRequest Problem Information   zWill Delay Interval   zRequest Response Information   zResponse Information   zServer Reference   zReason String   !   "   #   $   %   &   '   (   )   *   )
zReceive MaximumzTopic Alias MaximumzTopic AliaszMaximum QoSzRetain AvailablezUser PropertyzMaximum Packet SizezWildcard Subscription Availablez!Subscription Identifier AvailablezShared Subscription AvailablerR   rT   rW   rV   rU   rS   rX   )
rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   )
packetTypetypesnamesindexr   PUBLISHWILLMESSAGE	SUBSCRIBECONNECTCONNACK
DISCONNECTAUTHPUBACKPUBRECPUBRELPUBCOMPSUBACKUNSUBACKUNSUBSCRIBE
properties)selfrq   s     r   __init__zProperties.__init__   s   $R R R

&
%q
 A
 a	

 
 &r
 &r
 )"
  
 $R
 "2
 *2
 "2
 +B
 #B
  !
" R#
$  "#% "#%/113-/7
 
 

<0
 
  ((;+>@W*XY0
 
  !4558K[Md7ef	0

 
  !788;;NP[Pg:hi0
 
  !788;;NP[Pg:hi0
 
  //+2E{G^1_`0
 !!"9::%{'<=?0
 !!"566%{':K<RSU0
 !!"899K<O;PQ0
 !!"4558K7LM0
 !!"899%{':K<LMO0
  !!-00%{':K<LMO!0
$ !!&))%&(%0
( !!"5669P8QR)0
* !!&))K,?+@A+0
, !!"899K<O;PQ-0
. !!"899%{'=>@/0
2 !!"899%{'9;;M$k&9;;M&(>@PRS30
: !!"455%{':;=!!"455%{':;=!!"4558K7LM!!&))K,?+@A!!&))K,?+@A!!"566%{':%{'9$k&8+:M'););+?(+*:K<SUV !!"566%{':;=!!&))K,?+@A!!&))K,?+@A!!&))K,?+@A_0
 0
 0
r   c                 0    |                      |          dv S )N)r[   rl   )getIdentFromName)r   compressedNames     r   allowsMultiplezProperties.allowsMultiple   s    $$^44@@r   c                     d}| j                                         D ]+}||                    dd          k    r| j         |         } n,|S )N  )rs   keysreplace)r   r   resultnames       r   r   zProperties.getIdentFromName   sX    JOO%% 	 	Dc2!6!666D) 7 r   c                 6   |                     dd          }g d}||v rt                              | ||           d S |d | j                                        D             vr6t          dt          | j                                                  z             | j        | j        | 	                    |                   d         vr*t          d|dt          j        | j                           t          |          t          g           k    r|d	v r|dk     s|d
k    rt          d|z            |dv r|dk     s|d
k    rt          d|z            |dv r|dk     s|dk    rt          d|z            |dv r|dk    r|dk    rt          d|z            |                     |          rQt          |          t          g           k    r|g}t          | |          rt                              | |          |z   }t                              | ||           d S )Nr   r   )rq   rr   rs   r   c                 :    g | ]}|                     d d          S )r   r   )r   ).0anames     r   
<listcomp>z*Properties.__setattr__.<locals>.<listcomp>	  s&    RRRuEMM#r22RRRr   zProperty name must be one of r   z	Property z does not apply to packet type )ReceiveMaximum
TopicAliasi  z.%s property value must be in the range 1-65535)TopicAliasMaximumr   z.%s property value must be in the range 0-65535)MaximumPacketSizeSubscriptionIdentifierrC   z2%s property value must be in the range 1-268435455)RequestResponseInformationRequestProblemInformationPayloadFormatIndicatorz %s property value must be 0 or 1)r   object__setattr__rs   r   r   strrq   r   r   r   Namesr.   r   hasattr__getattribute__)r   r   rL   privateVarss       r   r   zProperties.__setattr__  s   ||C$$DDD;tT511111 RR
@Q@QRRRRR#3C
8I8I4J4JJL L L dod6K6KD6Q6Q&RST&UUU#m'+tt[->t-O-O%Q R R R E{{d2hh&&;;;"QYY%%--'HDQS S S222"QYY%%--'HDQS S SLLL"QYY%)*;*;'LPTUW W Wrrr"aZZEQJJ':dCE E E ""4(( H;;$r((**"GE4&& H"33D$??%GEtT511111r   c           	          d}d}| j                                         D ]W}|                    dd          }t          | |          r/|s|dz  }||dz   t	          t          | |                    z   z  }d}X|dz  }|S )	N[Tr   r   z, z : F])rs   r   r   r   r   getattr)r   r6   firstr   r   s        r   __str__zProperties.__str__/  s    JOO%% 	 	D!\\#r22Nt^,,  #dNF.50n55667 7#r   c                    i }| j                                         D ]p}|                    dd          }t          | |          rHt	          | |          }|dk    r-t          |t                    r|                                ||<   k|||<   q|S )Nr   r   CorrelationData)rs   r   r   r   r   
isinstancer/   hex)r   r1   r   r   vals        r   jsonzProperties.json=  s    JOO%% 	/ 	/D!\\#r22Nt^,, /dN33!%666:c5;Q;Q6+.7799D((+.D(r   c                     d}| j                                         D ],}|                    dd          }t          | |          rd} n-|S )NTr   r   F)rs   r   r   r   )r   rcr   r   s       r   isEmptyzProperties.isEmptyI  s]    JOO%% 	 	D!\\#r22Nt^,,  	r   c                     | j                                         D ]8}|                    dd          }t          | |          rt	          | |           9d S )Nr   r   )rs   r   r   r   delattr)r   r   r   s      r   clearzProperties.clearR  s]    JOO%% 	. 	.D!\\#r22Nt^,, .n---	. 	.r   c                    d}|t                               |          z  }|| j                            d          k    r?t          j        d         dk     r|t          |          z  }nb|t          |g          z  }nM|| j                            d          k    r|t          |          z  }n|| j                            d          k    r|t          |          z  }n|| j                            d          k    r|t                               |          z  }n|| j                            d          k    r|t          |          z  }n}|| j                            d	          k    r|t          |          z  }nL|| j                            d
          k    r.|t          |d                   t          |d                   z   z  }|S )Nr   rR   r   r*   rS   rT   rU   rV   rW   rX   r   )rA   rH   rr   rt   r,   r-   rE   r/   r   r%   r=   r2   )r   
identifierr.   rL   r6   s        r   writePropertyzProperties.writePropertyX  s   &--j9994:##F++++"Q&&#e**$%..(TZ%%&89999j'''FFTZ%%&9::::j'''FFTZ%%&=>>>>*11%888FFTZ%%m4444j'''FFTZ%%&<====huoo%FFTZ%%&9::::huQx((8E!H+=+===Fr   c           
         d}| j                                         D ]}|                    dd          }t          | |          r|                     |          }| j        |         d         }|                     |          r.t          | |          D ]}||                     |||          z  }||                     ||t          | |                    z  }t          
                    t          |                    |z   S )Nr   r   r   r   )rs   r   r   r   r   r   r   r   r   rA   rH   r0   )r   r6   r   r   r   	attr_typeprops          r   r   zProperties.packn  s    JOO%% 	P 	PD!\\#r22Nt^,, 	P!22>BB
 OJ7:	&&~66 P 'n = = F F$"4"4Z5>#F #F FF d00Y18~1N1NP P PF#**3v;;77&@@r   c                    || j                             d          k    r|d         }d}nZ|| j                             d          k    rt          |          }d}n)|| j                             d          k    rt          |          }d}n|| j                             d          k    rt                              |          \  }}n|| j                             d	          k    rt          |          \  }}n|| j                             d
          k    rt          ||          \  }}nZ|| j                             d          k    r<t          ||          \  }}||d          }t          |||z
            \  }}||f}||z  }||fS )NrR   r   r   rS   r   rT   r'   rU   rV   rW   rX   )rr   rt   r!   r(   rA   r4   r?   r:   )r   r6   r.   propslenrL   valuelenvalue1	valuelen1s           r   readPropertyzProperties.readProperty  s   4:##F++++1IEHHTZ%%&89999f%%EHHTZ%%&9::::f%%EHHTZ%%&=>>>>299&AAOE88TZ%%m4444'//OE88TZ%%&<====%fh77OE88TZ%%&9::::%fh77OE8HII&F '80C D DFIFOE	!Hhr   c                 D    d }| j         D ]}| j         |         |k    r|}|S r<   )rs   )r   r   r   r   s       r   getNameFromIdentzProperties.getNameFromIdent  s5    J 	 	Dz$:--	r   c                    t           j        d         dk     rt          |          }|                                  t                              |          \  }}||d          }|}|dk    rt                              |          \  }}||d          }||z  }| j        |         d         }|                     |||          \  }}	||	d          }||	z  }|                     |          }
|
	                    dd          }| 
                    |          s't          | |          rt          dt          z            t          | |
|           |dk    | ||z   fS )Nr   r*   r   r   z+Property '%s' must not exist more than once)r,   r-   r   r   rA   r4   r   r   r   r   r   r   r   propertysetattr)r   r6   r   VBIlenpropslenleftr   VBIlen2r   rL   r   propnamer   s               r   r   zProperties.unpack  s   A""v&&F

/66v>>&Q"6"="=# #JGHH%FG#L
3A6I"//	<1 1OE8HII&FH$L,,Z88H%--c266N&&~66 N74;X;X N#AHLN N ND(E*** Q  X&&&r   N)r   r	   r
   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rP   rP      s         $S
 S
 S
jA A A  +2 +2 +2Z  
 
 
  . . .  ,A A A"  0  ' ' ' ' 'r   rP   )rM   r   r,   packettypesr   	Exceptionr   r   r   r!   r%   r(   r2   r:   r=   r?   rA   r   rP   r   r   r   <module>r      sh   $  



 $ $ $ $ $ $	 	 	 	 	I 	 	 		 	 	 	 	m 	 	 	0 0 0
+ + +
0 0 0
+ + +
( ( (  *, , ,( ( (
/ / / / / / / /dh' h' h' h' h' h' h' h' h' h'r   