<!DOCTYPE module SYSTEM "module.dtd">

<module name="extra_dl" dir="datalink">
  <doc>
    <description>Extra datalink (PPRZ transport)</description>
    <configure name="EXTRA_DL_PORT" value="UARTX|UDPX|usb_serial" description="Select port for extra datalink"/>
    <configure name="EXTRA_DL_BAUD" value="B57600" description="Baudrate for extra datalink if link device is UART"/>
  </doc>
  <header>
    <file name="extra_pprz_dl.h"/>
  </header>
  <init fun="extra_pprz_dl_init()"/>
  <periodic fun="extra_pprz_dl_periodic()" freq="TELEMETRY_FREQUENCY" autorun="TRUE"/>

  <!--
  The event function listens on the extra telemetry port (from the payload computer for example)
  and parses the received "datalink" messages, such as MOVE_WP.
  We need this function to be able to listen to the extra telemetry port  

  As a result all messages normally handled by the autopilot (such as MOVE_WP) are processed.
  -->
  <event fun="extra_pprz_dl_event()"/>

  <makefile target="ap">
    <configure name="EXTRA_DL_PORT" default="uart1" case="upper|lower"/>
    <define name="EXTRA_DOWNLINK_DEVICE" value="$(EXTRA_DL_PORT_LOWER)"/>
    <define name="USE_$(EXTRA_DL_PORT_UPPER)"/>
    <raw>
      # Check for UDP port
      ifneq (,$(findstring udp,$(EXTRA_DL_PORT_LOWER)))
      include $(CFG_SHARED)/udp.makefile
      else
      ifneq (,$(findstring usb_serial,$(EXTRA_DL_PORT_LOWER)))
        # usb_serial telemetry chosen, add files based on architecture
        ifeq ($(ARCH), lpc21)
        $(TARGET).srcs += $(SRC_ARCH)/usb_ser_hw.c $(SRC_ARCH)/lpcusb/usbhw_lpc.c $(SRC_ARCH)/lpcusb/usbcontrol.c
        $(TARGET).srcs += $(SRC_ARCH)/lpcusb/usbstdreq.c $(SRC_ARCH)/lpcusb/usbinit.c
        else
        ifeq ($(ARCH), stm32)
        $(TARGET).srcs += $(SRC_ARCH)/usb_ser_hw.c
        else
        ifneq ($(ARCH), sim)
        $(error telemetry_transparent_usb currently only implemented for the lpc21 and stm32)
        endif
        endif
        endif
      else
      EXTRA_DL_BAUD ?= B57600
      $(TARGET).CFLAGS += -D$(EXTRA_DL_PORT_UPPER)_BAUD=$(EXTRA_DL_BAUD)
      endif # USB serial
      endif # UDP
    </raw>
    <file name="extra_pprz_dl.c"/>
    <file name="pprz_transport.c" dir="pprzlink/src"/>
  </makefile>
</module>
