
    i!              
       0   U 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	 d dl
mZ d dlmZ  e j                  e      ZdZd	Zd
ZddddZdZdZdZdZdZddgeegdZeed<   dedefdZdefdZdeeef   defdZ de!dedefdZ"d e!de	d!ed"e#ddf
d#Z$y)$    N)Union)subp)Cloud)Config)
MetaSchema)PER_INSTANCErpi
interfacesenable_usb_gadgetdo_spido_i2c
do_onewire)spii2conewireserialdo_serial_consdo_serial_hwz/usr/bin/rpi-usb-gadgetz(Rebooting to apply config.txt changes...cc_raspberry_pizraspberry-pi-os)iddistros	frequencyactivate_by_schema_keysmetaenablereturnc                 `   t         j                  d|        t        j                  j	                  t
              st         j                  dt
               y	 t        j                  t
        | rdnddgdd       y	# t        j                  $ r }t         j                  d
|       Y d}~yd}~ww xY w)zEnable / Disable Raspberry Pi USB gadget mode when
    the script is present.

    Returns True when a reboot is required.
    zEnable rpi-usb-gadget mode: %sz#rpi-usb-gadget script not found: %sFonoffz-f   )capturetimeoutTz&Failed to configure rpi-usb-gadget: %sN)	LOGdebugospathexistsRPI_USB_GADGET_SCRIPTerrorr   ProcessExecutionError)r   es     B/usr/lib/python3/dist-packages/cloudinit/config/cc_raspberry_pi.pyconfigure_usb_gadgetr-   (   s     II.777>>/0		79NO		%E 
 	
  %% 		:A>s   #A: :B-B((B-c                  f    	 t        j                   g d       y# t         j                  $ r Y yw xY w)N)/usr/bin/raspi-confignonint	is_pifiveTF)r   r*        r,   r1   r1   K   s/    		BC%% s    00cfgc                 8   ddt         dt        fd}d}d}t        | t               r || d      } || d      }nt        | t              r| }t	               s|rd}	 t        j
                  dd	t        t        |rd
nd      g       	 t        j
                  dd	t        t        |rd
nd      g       y# t
        j                  $ r }t        j                  d|       Y d}~yd}~ww xY w# t
        j                  $ r }t        j                  d|       Y d}~yd}~ww xY w)au  Configure the serial (UART) interface on Raspberry Pi.

    Depending on the configuration, this enables or disables
    the serial console and the hardware UART by invoking
    `raspi-config` non-interactively.

    Args:
        cfg: Either a boolean or a dictionary:
            - If a bool, enables or disables the serial
              console and hardware UART together.
            - If a dict, may contain the keys `console`
              and `hardware` with boolean values controlling
              each individually.

    Returns:
        True if the configuration was applied successfully,
        False otherwise.
    Fcfg_dictnamec                     | j                  ||      }t        |t              st        j	                  dt
        ||       |S |S )Nz"Invalid value for %s.serial.%s: %s)get
isinstanceboolr#   warningRPI_INTERFACES_KEY)r6   r7   defaultvals       r,   get_bool_fieldz2configure_serial_interface.<locals>.get_bool_fieldg   s@    ll4)#t$KK4"	 N
r3   consolehardwareTr/   r0   r      z&Failed to configure serial console: %sNz'Failed to configure serial hardware: %s)F)dictstrr:   r;   r1   r   RASPI_CONFIG_SERIAL_CONS_FNr*   r#   r)   RASPI_CONFIG_SERIAL_HW_FN)r4   r@   enable_console	enable_hwr+   s        r,   configure_serial_interfacerJ   S   s   (
 
S 
 NI#t'Y7"3
3		C	 ;> 			'+AQ/		
		')A*		
  %% 		:A> %% 		;Q?s0   *B0 *C& 0C#CC#&D9DDifacec                    | t         j                         v s
J d|         	 t        j                  ddt         |    t        |rdnd      g       y# t        j                  $ r!}t
        j                  d| |       Y d}~y	d}~ww xY w)
a  Enable or disable a supported hardware interface.

    Supported interfaces are defined in `SUPPORTED_INTERFACES` and
    currently include SPI, I2C, and 1-Wire. The function applies
    the change using `raspi-config` in non-interactive mode.

    Args:
        iface: The interface name (e.g. `"spi"` or `"i2c"`).
        enable: True to enable the interface, False to disable it.

    Returns:
        True if the interface configuration succeeded, False otherwise.
    zUnsupported interface: r/   r0   r   rC   TzFailed to configure %s: %sNF)SUPPORTED_INTERFACESkeysr   rE   r*   r#   r)   )rK   r   r+   s      r,   configure_interfacerO      s     	%**,,)	 (),		'$U+AQ'		
 %% 		.q9s   -A B#A??Br7   cloudargsc                     t         |vry t        |t            t              s#t        j	                  dt         |t                   y |t            st        j                  dt                y d}|t            D ]  }|t        k(  r>|t            |   }t        |t              r|t        |      z  }nt        dt               K|t        k(  rbt        |t            |   t              s't        j	                  dt         |t            |           y |t            |   st        j                  d|        y t        |t            |   j                               }|D ]  }|t        j                         vr%|t        k7  rt        j	                  dt        |       >|t            |   |   }|t        k(  rBt        |t        t        f      st        j	                  dt        ||       n|t        |      z  }t        |t              r|t!        ||      z  }t        j	                  dt        ||        t        j	                  d|        |du r8|j"                  j%                  d	d
t&              }	t)        j(                  |	       y y )NzInvalid value for %s: %szEmpty value for %s. Skipping...FzInvalid value for zInvalid key for %s: %szInvalid value for %s.%s: %szUnsupported key: %sTrebootnow)modedelaymessage)RPI_BASE_KEYr:   rD   r#   r<   r$   ENABLE_USB_GADGET_KEYr;   r-   
ValueErrorr=   listrN   rM   SERIAL_INTERFACErJ   rO   distroshutdown_command
REBOOT_MSGr   )
r7   r4   rP   rQ   want_rebootkeyr   subkeyssubkeycmds
             r,   handlere      s[   3L)40&	

 			3\BK<  ;''&s+F&$'3F;; #56K5L!MNN&&c,/4d;. %c*
 &s+		;SA3|,S16689G "  "6";";"=="22KK02Df \*3/7--%ftTl;9.""	 $'A&'IIfd+#6vv#FFKKK5*	7 D KK-s3w;z dll++ , 

 			# r3   )%loggingr%   typingr   	cloudinitr   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.settingsr   	getLogger__name__r#   rX   r=   rY   rM   r\   rF   rG   r(   r_   r   __annotations__r;   r-   r1   rD   rJ   rE   rO   r[   re   r2   r3   r,   <module>rp      s    	   ! # . +g!! +  
  . * 1 7
 !" ,~	j    $  F4 KE$*$5 K$ K\s D T DT T6 T% Tt T Tr3   