/*******************************************************************************
 * Copyright (c) 2018 Relayr GmbH
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Alexander Kaiser
 *   Sascha Hackel
 *   Axel Rennoch
 *******************************************************************************/
module MQTT_Functions_Network
{
  import from MQTT_TestSystem_Common all;

  import from MQTTasp_PortType all;
  import from MQTTasp_Types all;
  import from MQTTasp_CtrlFunct all;
  import from MQTT_v3_1_1_IPL4SizeFunction all;

  //import from IPL4asp_PortType all;
  import from IPL4asp_Types all;

  import from Random_Helper all;

  /*
   * This is a wrapper of IPL4 functionality of MQTT: TODO: move to MQTT_Function_Protocol ?
   */

  group Common
  {
    function f_ipl4_close(integer p_idx) runs on MQTT_MTC return Result
    {
      log("[IPL4](close connection)(", p_idx, ")")
      var Result vl_result := MQTTasp_CtrlFunct.f_IPL4_close(MQTT_PCO, p_idx);
      return vl_result
    }
  } // group Common

  group Broker
  {
    function f_ipl4_listen(integer p_port := 1883, charstring p_loc_name := "0.0.0.0") runs on MQTT_Broker_MTC return Result
    {
       log("[IPL4](listen on )(", p_loc_name, ":", p_port, ")")
       var Result vl_result := MQTTasp_CtrlFunct.f_IPL4_listen(MQTT_PCO, p_loc_name, p_port, {tcp := {}})
       return vl_result;
    }
  } // group Broker

  group Client
  {
    function f_ipl4_connect(integer p_local_port := -1) runs on MQTT_Client_MTC
    return boolean
    {
      if(p_local_port == -1)
      {
        connection.ipl4.local_port := f_random_port_number()
      } else {
        connection.ipl4.local_port := p_local_port
      }

      var charstring v_remote_host := connection.ipl4.remote_name
      var integer v_remote_port := connection.ipl4.remote_port
      var charstring v_local_host := connection.ipl4.local_name
      var integer v_local_port := connection.ipl4.local_port
      var boolean v_reuse_address := connection.ipl4.reuse_address;
      var integer v_connection_id := connection.ipl4.id

      var boolean v_connectState := false;

      var Result vl_result := MQTTasp_CtrlFunct.f_IPL4_connect(MQTT_PCO, v_remote_host, v_remote_port, v_local_host, v_local_port, v_connection_id, {tcp := {}}, {{reuseAddress := {enable := v_reuse_address}}});

      if (ispresent(vl_result.connId))
      {
        v_connectState := true;
        connection.ipl4.id := vl_result.connId
        connection.ipl4.tcp_state := Connected
        connection.client_state := Disconnected

        // set callback to determine the boundaries of MQTT messages.
        // IPL4 requires for assembling fragmented TCP packets
        var integer v_cid := vl_result.connId
        var f_IPL4_getMsgLen getMsg_Func := refers(f_GetMsgLengthMQTT);
        f_IPL4_setGetMsgLen(MQTT_PCO, v_cid, getMsg_Func, {});
      }

      return v_connectState;
    }

    function f_ipl4_disconnect() runs on MQTT_Client_MTC
    return boolean
    {
      var integer v_connection_id := connection.ipl4.id
      var Result vl_result := f_ipl4_close(v_connection_id)

      if (ispresent(vl_result.connId))
      {
        connection.ipl4.tcp_state := Disconnected
        connection.client_state := Disconnected
        return true
      }
      else
      {
        return false
      }
    }
  } // group Client

} // module
