| /////////////////////////////////////////////////////////////////////////////// |
| // |
| // Copyright (c) 2000-2017 Ericsson Telecom AB |
| // |
| // 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 |
| /////////////////////////////////////////////////////////////////////////////// |
| // File: EPTF_COAP_LocalTransport_Functions.ttcn |
| // Description: |
| // Rev: R1A |
| // Prodnr: CNL 113 858 |
| // Updated: 2017-09-01 |
| // Contact: http://ttcn.ericsson.se |
| /////////////////////////////////////////////////////////////////////////////// |
| module EPTF_COAP_LocalTransport_Functions |
| { |
| import from IPL4asp_Types all; |
| |
| import from EPTF_CLL_Base_Functions all; |
| import from EPTF_CLL_Common_Definitions all; |
| import from EPTF_CLL_Logging_Definitions all; |
| import from EPTF_CLL_Logging_Functions all; |
| import from EPTF_CLL_HashMapStr2Int_Functions all; |
| import from EPTF_CLL_FBQ_Functions all; |
| import from EPTF_CLL_TransportCommPortIPL4_Functions all; |
| import from EPTF_CLL_RBTScheduler_Functions all; |
| |
| import from EPTF_COAP_LocalTransport_Definitions all; |
| import from EPTF_COAP_Transport_Definitions all; |
| |
| import from CoAP_Types all; |
| |
| function f_EPTF_COAP_LocalTransport_init() |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| if (v_EPTF_COAP_LocalTransport_initialized) {return;} |
| |
| f_EPTF_COAP_LocalTransport_initLogging(); |
| |
| f_EPTF_CommPort_IPL4_init(); |
| |
| v_EPTF_COAP_Transport_stats := c_EPTF_COAP_Transport_Statistics_empty; |
| |
| f_EPTF_COAP_LocalTransport_socketDB_init(); |
| |
| var f_IPL4_getMsgLen fcb_msglen := refers(f_EPTF_COAP_IPL4asp_getMsgLen); |
| f_EPTF_CommPort_IPL4_setMsgLen4LGenType(fcb_msglen, {}, c_COAP_Transport_LGenType); |
| |
| f_EPTF_CommPort_IPL4_setReceive({asp_Event := ?},refers(f_EPTF_COAP_IPL4asp_handleEvent), c_COAP_Transport_LGenType); |
| f_EPTF_CommPort_IPL4_setReceive({asp_RecvFrom := ?},refers(f_EPTF_COAP_IPL4asp_handleMessage), c_COAP_Transport_LGenType); |
| |
| f_EPTF_Base_registerCleanup(refers(f_EPTF_COAP_LocalTransport_cleanup)); |
| |
| v_EPTF_COAP_LocalTransport_initialized := true; |
| } |
| |
| function f_EPTF_COAP_LocalTransport_initLogging() |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| f_EPTF_Logging_init_CT("COAP_Transport"); |
| v_EPTF_COAP_LocalTransport_loggingMaskId := |
| f_EPTF_Logging_registerComponentMasks( |
| "COAP_LocalTransport_Logging", |
| {"WARNING", "DEBUG", "DEBUGV", "ERROR"}, |
| EPTF_Logging_CLL |
| ); |
| |
| if(tsp_EPTF_COAP_LocalTransport_debug){ |
| f_EPTF_Logging_enableLocalMask(v_EPTF_COAP_LocalTransport_loggingMaskId, c_COAP_LocalTransport_Logging_DEBUG); |
| } |
| else { |
| f_EPTF_Logging_disableLocalMask(v_EPTF_COAP_LocalTransport_loggingMaskId, c_COAP_LocalTransport_Logging_DEBUG); |
| } |
| |
| if(tsp_EPTF_COAP_LocalTransport_debugVerbose) { |
| f_EPTF_Logging_enableLocalMask(v_EPTF_COAP_LocalTransport_loggingMaskId, c_COAP_LocalTransport_Logging_DEBUGV); |
| } |
| else { |
| f_EPTF_Logging_disableLocalMask(v_EPTF_COAP_LocalTransport_loggingMaskId, c_COAP_LocalTransport_Logging_DEBUGV); |
| } |
| } |
| |
| function f_EPTF_COAP_LocalTransport_cleanup() |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| // Reset DBs, close connections |
| |
| f_EPTF_COAP_LocalTransport_socketDB_cleanUp(); |
| |
| vf_EPTF_COAP_Transport_receiveMessage := null; |
| vf_EPTF_COAP_Transport_receiveEvent := null; |
| |
| v_EPTF_COAP_LocalTransport_initialized := false; |
| } |
| |
| function f_EPTF_COAP_IPL4asp_getMsgLen |
| ( |
| in octetstring stream, |
| inout ro_integer args |
| ) |
| return integer |
| { |
| return lengthof(stream); |
| } |
| |
| function f_EPTF_COAP_IPL4asp_handleEvent() runs on EPTF_COAP_LocalTransport_CT |
| { |
| f_COAP_Transport_Logging_DEBUG(log2str(": incoming event: ", v_EPTF_CommPort_IPL4_incomingMessage)); |
| |
| if (vf_EPTF_COAP_Transport_receiveEvent != null) { |
| vf_EPTF_COAP_Transport_receiveEvent.apply(v_EPTF_CommPort_IPL4_incomingMessage.asp_Event); |
| } |
| } |
| |
| function f_EPTF_COAP_IPL4asp_handleMessage() runs on EPTF_COAP_LocalTransport_CT |
| { |
| var CoAP_Message vl_COAP_MSG; |
| var EPTF_COAP_PDU vl_EPTF_COAP_PDU; |
| |
| f_EPTF_SchedulerComp_refreshSnapshotTime(); |
| |
| f_CoAP_dec(v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.msg, vl_COAP_MSG); |
| |
| if (tsp_EPTF_COAP_LocalTransport_debug) { action("COAP transport receiving: \n", vl_COAP_MSG); } |
| |
| if (ischosen(vl_COAP_MSG.msg)) |
| { |
| vl_EPTF_COAP_PDU.pdu := vl_COAP_MSG.msg; |
| vl_EPTF_COAP_PDU.transportParams.localAddress.hostName := v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.locName; |
| vl_EPTF_COAP_PDU.transportParams.localAddress.portNumber := v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.locPort; |
| vl_EPTF_COAP_PDU.transportParams.remoteAddress.hostName := v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.remName; |
| vl_EPTF_COAP_PDU.transportParams.remoteAddress.portNumber := v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.remPort; |
| vl_EPTF_COAP_PDU.transportParams.proto := v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.proto; |
| |
| var SocketEntry vl_entry := c_SocketEntry_init; |
| var integer vl_sockIdx := f_EPTF_COAP_LocalTransport_socketDB_lookUp({ |
| v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.locName, |
| v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.locPort |
| }); |
| f_EPTF_COAP_LocalTransport_socketDB_get(vl_sockIdx, vl_entry); |
| vl_EPTF_COAP_PDU.eIdx := vl_entry.eIdx; |
| vl_EPTF_COAP_PDU.fsmIdx := -1; |
| if (tsp_EPTF_COAP_LocalTransport_debug) { action("COAP transport receiving: eIdx: ", vl_EPTF_COAP_PDU.eIdx); } |
| |
| v_EPTF_COAP_Transport_stats.nofReceivedMessages := |
| v_EPTF_COAP_Transport_stats.nofReceivedMessages + 1.0; |
| v_EPTF_COAP_Transport_stats.nofReceivedBytes := |
| v_EPTF_COAP_Transport_stats.nofReceivedBytes + int2float(lengthof(v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.msg)); |
| |
| if (vf_EPTF_COAP_Transport_receiveMessage != null) { |
| vf_EPTF_COAP_Transport_receiveMessage.apply(vl_EPTF_COAP_PDU); |
| } |
| } |
| else |
| { |
| f_COAP_Transport_Logging_WARNING( |
| log2str(%definitionId, " Couldn't decode msg: ",v_EPTF_CommPort_IPL4_incomingMessage.asp_RecvFrom.msg, " decoded: ", vl_COAP_MSG) |
| ); |
| } |
| } |
| |
| function f_EPTF_COAP_LocalTransport_send(in EPTF_COAP_PDU pl_msg) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| f_COAP_Transport_Logging_VERBOSE(log2str(%definitionId, " ", pl_msg)); |
| |
| var integer vl_sockIdx := f_EPTF_COAP_LocalTransport_socketDB_lookUp(pl_msg.transportParams.localAddress); |
| |
| if (vl_sockIdx < 0) |
| { |
| if (not f_EPTF_COAP_LocalTransport_startSocket(pl_msg.transportParams.localAddress, pl_msg.eIdx, vl_sockIdx)) |
| { |
| f_COAP_Transport_Logging_WARNING(log2str(": couldn't create socket: ", pl_msg.transportParams.localAddress)); |
| return; |
| } |
| } |
| f_EPTF_COAP_LocalTransport_socketDB_get(vl_sockIdx, v_COAP_LocalTransport_currentSocket); |
| |
| if (tsp_EPTF_COAP_LocalTransport_debug) { action("COAP transport sending: \n", pl_msg.pdu); } |
| |
| var octetstring v_encoded; |
| f_CoAP_enc(CoAP_Message: {msg := pl_msg.pdu}, v_encoded); |
| |
| f_EPTF_COAP_LocalTransport_IPL4_send |
| ( |
| v_COAP_LocalTransport_currentSocket.connId, |
| pl_msg.transportParams.remoteAddress.hostName, |
| pl_msg.transportParams.remoteAddress.portNumber, |
| pl_msg.transportParams.proto, |
| v_encoded |
| ); |
| } |
| |
| function f_EPTF_COAP_LocalTransport_transportApiRequest(EPTF_COAP_Transport_Request pl_req) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| if (ischosen(pl_req.params.startListening)) |
| { |
| var EPTF_COAP_Transport_Response vl_rsp := c_EPTF_COAP_Transport_Response_init; |
| vl_rsp.eIdx := pl_req.eIdx; |
| vl_rsp.fsmIdx := pl_req.fsmIdx; |
| |
| var integer vl_sockIdx := f_EPTF_COAP_LocalTransport_socketDB_lookUp(pl_req.params.startListening.localAddress); |
| if (vl_sockIdx < 0) |
| { |
| if (not f_EPTF_COAP_LocalTransport_startSocket(pl_req.params.startListening.localAddress, pl_req.eIdx, vl_sockIdx)) |
| { |
| f_COAP_Transport_Logging_WARNING(log2str(": couldn't create socket: ", pl_req.params.startListening.localAddress)); |
| } |
| else { vl_rsp.succ := true; } |
| } |
| else { vl_rsp.succ := false; } |
| vf_EPTF_COAP_Transport_apiResponse.apply(vl_rsp); |
| } |
| } |
| |
| function f_EPTF_COAP_LocalTransport_startSocket(in Socket p_sock, in integer p_eIdx, inout integer p_idx) |
| runs on EPTF_COAP_LocalTransport_CT |
| return boolean |
| { |
| var Result vl_result; |
| var SocketEntry vl_socketEntry := c_SocketEntry_init; |
| |
| vl_result := |
| f_EPTF_CommPort_IPL4_send( |
| {asp_Listen := |
| { |
| locName := p_sock.hostName, |
| locPort := p_sock.portNumber, |
| proto := { udp := {} } |
| } |
| }, |
| null, |
| false, |
| false, |
| c_COAP_Transport_LGenType); |
| |
| if (f_EPTF_COAP_IPL4asp_handleResult(vl_result)) // Everything went OK |
| { |
| vl_socketEntry.addr := p_sock; |
| vl_socketEntry.connId := vl_result.connId; |
| vl_socketEntry.eIdx := p_eIdx; |
| vl_socketEntry.state := OPENED; |
| } |
| else |
| { return false;} |
| |
| p_idx := f_EPTF_COAP_LocalTransport_socketDB_add(vl_socketEntry); |
| |
| return true; |
| } |
| |
| function f_EPTF_COAP_LocalTransport_IPL4_send( |
| in integer pl_connId, |
| in charstring pl_remName, |
| in integer pl_remPort, |
| in ProtoTuple pl_proto, |
| in octetstring pl_msg) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| var Result v_res; |
| |
| v_res := f_EPTF_CommPort_IPL4_send( |
| { |
| asp_SendTo := |
| { |
| connId := pl_connId, |
| remName := pl_remName, |
| remPort := pl_remPort, |
| proto := pl_proto, |
| msg := pl_msg |
| } |
| }, |
| null, |
| false, |
| false, |
| c_COAP_Transport_LGenType |
| ); |
| |
| if (ispresent(v_res.errorCode)) |
| { |
| f_COAP_Transport_Logging_WARNING(log2str(": message could not be sent.")); |
| } |
| |
| v_EPTF_COAP_Transport_stats.nofSentMessages := v_EPTF_COAP_Transport_stats.nofSentMessages + 1.0; |
| v_EPTF_COAP_Transport_stats.nofSentBytes := v_EPTF_COAP_Transport_stats.nofSentBytes + int2float(lengthof(pl_msg)); |
| } |
| |
| function f_EPTF_COAP_LocalTransport_socketDB_init() |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| f_EPTF_FBQ_initFreeBusyQueue(v_COAP_LocalTransport_localSocketDB.queue); |
| v_COAP_LocalTransport_localSocketDB.data := {}; |
| v_COAP_LocalTransport_localSocketDB.hashRef := f_EPTF_str2int_HashMap_New(c_EPTF_COAP_LocalTransport_SocketDB); |
| } |
| |
| function f_EPTF_COAP_LocalTransport_socketDB_cleanUp() |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| f_EPTF_FBQ_initFreeBusyQueue(v_COAP_LocalTransport_localSocketDB.queue); |
| v_COAP_LocalTransport_localSocketDB.data := {}; |
| f_EPTF_str2int_HashMap_Delete(c_EPTF_COAP_LocalTransport_SocketDB); |
| } |
| |
| function f_EPTF_COAP_LocalTransport_socketDB_add(in SocketEntry p_sock) |
| runs on EPTF_COAP_LocalTransport_CT |
| return integer |
| { |
| var integer v_idx := f_EPTF_COAP_LocalTransport_socketDB_lookUp(p_sock.addr); |
| |
| if (v_idx == -1) |
| { |
| v_idx := f_EPTF_FBQ_getOrCreateFreeSlot(v_COAP_LocalTransport_localSocketDB.queue); |
| f_EPTF_FBQ_moveFromFreeHeadToBusyTail(v_COAP_LocalTransport_localSocketDB.queue); |
| |
| f_COAP_Transport_Logging_DEBUG(log2str(": "," adding socket ", v_idx, " ", p_sock)); |
| |
| f_EPTF_str2int_HashMap_Insert(v_COAP_LocalTransport_localSocketDB.hashRef, f_EPTF_COAP_Socket2String(p_sock.addr), v_idx); |
| |
| v_COAP_LocalTransport_localSocketDB.data[v_idx] := p_sock; |
| } |
| |
| return v_idx; |
| } |
| |
| function f_EPTF_COAP_LocalTransport_socketDB_lookUp(in Socket p_sock) |
| runs on EPTF_COAP_LocalTransport_CT |
| return integer |
| { |
| var integer vl_idx := -1; |
| f_EPTF_str2int_HashMap_Find(v_COAP_LocalTransport_localSocketDB.hashRef, f_EPTF_COAP_Socket2String(p_sock), vl_idx); |
| return vl_idx; |
| } |
| |
| function f_EPTF_COAP_LocalTransport_socketDB_get(in integer p_idx, inout SocketEntry p_sock) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| if (p_idx < sizeof(v_COAP_LocalTransport_localSocketDB.data)) |
| { |
| p_sock := v_COAP_LocalTransport_localSocketDB.data[p_idx]; |
| } |
| } |
| |
| function f_EPTF_COAP_LocalTransport_socketDB_remove(in integer p_idx) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| v_COAP_LocalTransport_localSocketDB.data[p_idx] := c_SocketEntry_init; |
| f_EPTF_FBQ_moveFromBusyToFreeTail(p_idx, v_COAP_LocalTransport_localSocketDB.queue); |
| } |
| |
| function f_EPTF_COAP_Socket2String(Socket p_sock) |
| return charstring |
| { |
| return p_sock.hostName&":"&int2str(p_sock.portNumber); |
| } |
| |
| function f_COAP_Transport_Logging_VERBOSE(in @lazy charstring pl_message) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| if (c_EPTF_Common_debugSwitch) { |
| f_EPTF_Logging_debugV2(pl_message, v_EPTF_COAP_LocalTransport_loggingMaskId, {c_COAP_LocalTransport_Logging_DEBUGV}); |
| } |
| } |
| |
| function f_COAP_Transport_Logging_DEBUG(in @lazy charstring pl_message) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| if (c_EPTF_Common_debugSwitch) { |
| f_EPTF_Logging_debugV2(pl_message, v_EPTF_COAP_LocalTransport_loggingMaskId, {c_COAP_LocalTransport_Logging_DEBUG}); |
| } |
| } |
| |
| function f_COAP_Transport_Logging_WARNING(in @lazy charstring pl_message) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| if (c_EPTF_Common_debugSwitch) { |
| f_EPTF_Logging_debugV2(pl_message, v_EPTF_COAP_LocalTransport_loggingMaskId, {c_COAP_LocalTransport_Logging_WARNING}); |
| } |
| } |
| |
| function f_COAP_Transport_Logging_ERROR(in @lazy charstring pl_message) |
| runs on EPTF_COAP_LocalTransport_CT |
| { |
| if (c_EPTF_Common_debugSwitch) { |
| f_EPTF_Logging_debugV2(pl_message, v_EPTF_COAP_LocalTransport_loggingMaskId, {c_COAP_LocalTransport_Logging_ERROR}); |
| } |
| } |
| |
| function f_EPTF_COAP_IPL4asp_handleResult(inout Result p_res) |
| runs on EPTF_COAP_LocalTransport_CT |
| return boolean |
| { |
| if (ispresent(p_res.errorCode) and (p_res.errorCode != IPL4_ERROR_TEMPORARILY_UNAVAILABLE)) |
| { |
| f_COAP_Transport_Logging_WARNING(log2str("Warning: f_EPTF_COAP_IPL4asp_handleResult: IPL4 error: ", p_res)); |
| v_EPTF_COAP_Transport_stats.nofTransportWarnings := v_EPTF_COAP_Transport_stats.nofTransportWarnings + 1; |
| return false; |
| } |
| return true; |
| } |
| |
| } |