blob: fd09f5d8a94472f7554e17da6bf3effa8fee2782 [file] [log] [blame]
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000-2020 Ericsson Telecom AB
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v2.0
// which accompanies this distribution, and is available at
// https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
///////////////////////////////////////////////////////////////////////////////
// File: IFW_MQTT_Client_Functions.ttcn
// Description:
// Rev: <RnXnn>
// Prodnr: CNL 113 910
// Updated: 2020-02-06
// Contact: http://ttcn.ericsson.se
///////////////////////////////////////////////////////////////////////////////
module IFW_MQTT_Client_Functions
{
import from IFW_MQTT_Client_Definitions all;
import from MQTT_v3_1_1_Types all;
import from IPL4asp_Types all;
import from IPL4asp_PortType all;
import from IFW_Common all;
import from MQTT_v3_1_1_IPL4SizeFunction all;
///////////////////////////////////////////////////////////
// Module parameter: tsp_MQTT_Client_maxResponseTime
//
// Purpose:
// Time the client is waiting for a response message (0.0->not set)
//
// Type:
// *float*
//
// Default value:
// *0.0*
///////////////////////////////////////////////////////////
modulepar float tsp_MQTT_Client_maxResponseTime := 0.0;
function f_MQTT_Client_init() runs on IFW_MQTT_Client_CT
{
log(%definitionId, " started");
var Result vl_result;
log("Mapping started");
map(self:IPL4_PCO,system:IPL4_PCO);
var f_IPL4_getMsgLen vl_f := refers(f_GetMsgLengthMQTT);
f_IPL4_setGetMsgLen(IPL4_PCO, -1, vl_f, {});
log("Connecting the socket to the remote");
vl_result := f_IPL4_connect(IPL4_PCO, ctx.remoteHost, ctx.remotePort, ctx.localHost, ctx.localPort, -1, {tcp := {}}, {{reuseAddress := {enable := true}}});
f_checkResult(vl_result);
ctx.connId := vl_result.connId;
log(%definitionId, " finished");
}
function f_MQTT_Client_cleanUp() runs on IFW_MQTT_Client_CT
{
log(%definitionId, " started");
var Result vl_result;
log("Closing connection");
if (ctx.connId >= 0)
{
vl_result := f_IPL4_close(IPL4_PCO, ctx.connId, {tcp := {}});
//f_checkResult(vl_result);
}
log(%definitionId, " finished");
}
function f_MQTT_Client_getContext() runs on IFW_MQTT_Client_CT
return MqttClientContext
{
return ctx;
}
function f_MQTT_Client_setContext(in MqttClientContext p_ctx) runs on IFW_MQTT_Client_CT
{
ctx := p_ctx;
}
function f_MQTT_Client_setMessageToSend(in MQTT_v3_1_1_ReqResp p_msg) runs on IFW_MQTT_Client_CT
{
msgToSend := p_msg;
}
function f_MQTT_Client_send() runs on IFW_MQTT_Client_CT
{
var octetstring v_encoded;
f_MQTT_v3_1_1_enc({ msg := msgToSend}, v_encoded);
var ASP_SendTo vl_send;
vl_send.connId := ctx.connId;
vl_send.remName := ctx.remoteHost;
vl_send.remPort := ctx.remotePort;
vl_send.proto := {tcp := {}}
vl_send.msg := v_encoded;
log("MQTT PDU: ", msgToSend);
IPL4_PCO.send(vl_send);
}
function f_MQTT_Client_receive() runs on IFW_MQTT_Client_CT
{
// Activate default
timer t_Timeout := tsp_MQTT_Client_maxResponseTime;
if (tsp_MQTT_Client_maxResponseTime > 0.0) { t_Timeout.start; }
var ASP_RecvFrom v_ipl4Recv;
var ASP_Event v_ipl4Event;
alt
{
[] IPL4_PCO.receive(ASP_RecvFrom:?) -> value v_ipl4Recv
{
log("Received: ", v_ipl4Recv);
var MQTT_v3_1_1_Message v_mqttMsg;
f_MQTT_v3_1_1_dec(v_ipl4Recv.msg, v_mqttMsg);
if (ischosen(v_mqttMsg.msg)) {
lastReceived := v_mqttMsg.msg;
log("MQTT PDU: ", lastReceived);
}
else {
lastReceived := c_MQTTMessage_empty;
log("MQTT PDU [raw]:",v_mqttMsg);
}
}
[] IPL4_PCO.receive(ASP_Event:?) -> value v_ipl4Event
{
log("Received: ", v_ipl4Event);
repeat;
}
[] t_Timeout.timeout
{
lastReceived := c_MQTTMessage_empty;
}
// Deactivate default
}
}
function f_MQTT_Client_check(in template MQTT_v3_1_1_ReqResp p_expected) runs on IFW_MQTT_Client_CT
return ReturnBoolean
{
if (not match(lastReceived, p_expected))
{
log("CHECK: last received is not matching with expected template");
return false;
}
log("CHECK: return true");
return true;
}
}