Harald Welte | cd451ef | 2019-05-06 17:20:44 +0200 | [diff] [blame] | 1 | module CBSP_Adapter { |
| 2 | |
| 3 | /* CBSP Adapter layer, sitting on top of CBSP_CodecPort. |
| 4 | * test suites can 'inherit' in order to have a CBSP connection to the IUT which they're testing |
| 5 | * |
| 6 | * (C) 2018-2019 by Harald Welte <laforge@gnumonks.org> |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Released under the terms of GNU General Public License, Version 2 or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | import from Osmocom_Types all; |
| 15 | import from General_Types all; |
| 16 | import from CBSP_Types all; |
| 17 | import from CBSP_Templates all; |
| 18 | import from CBSP_CodecPort all; |
| 19 | import from CBSP_CodecPort_CtrlFunct all; |
| 20 | import from IPL4asp_Types all; |
| 21 | import from IPL4asp_PortType all; |
| 22 | import from Socket_API_Definitions all; |
| 23 | |
| 24 | const integer NUM_CBSP := 3; |
| 25 | |
| 26 | type component CBSP_Adapter_CT { |
| 27 | /* down-facing port to CBSP Codec port */ |
| 28 | port CBSP_CODEC_PT CBSP[NUM_CBSP]; |
| 29 | var IPL4asp_Types.ConnectionId g_cbsp_conn_id[NUM_CBSP] := { -1, -1, -1 }; |
| 30 | } |
| 31 | |
| 32 | private function f_set_tcp_segmentation(integer idx) runs on CBSP_Adapter_CT { |
| 33 | /* Set function for dissecting the binary stream into packets */ |
| 34 | var f_IPL4_getMsgLen vl_f := refers(f_IPL4_fixedMsgLen); |
| 35 | /* Offset: 1, size of length: 3, delta: 4, multiplier: 1, big-endian */ |
| 36 | CBSP_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(CBSP[idx], g_cbsp_conn_id[idx], vl_f, {1, 3, 4, 1, 0}); |
| 37 | } |
| 38 | |
| 39 | function f_connect(charstring remote_host, IPL4asp_Types.PortNumber remote_port, |
| 40 | charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0) |
| 41 | runs on CBSP_Adapter_CT { |
| 42 | var IPL4asp_Types.Result res; |
| 43 | map(self:CBSP[idx], system:CBSP); |
Pau Espin Pedrol | 28aa466 | 2020-09-15 18:32:59 +0200 | [diff] [blame] | 44 | if (g_cbsp_conn_id[idx] != -1) { |
| 45 | CBSP_CodecPort_CtrlFunct.f_IPL4_close(CBSP[idx], g_cbsp_conn_id[idx], {tcp := {}}); |
| 46 | g_cbsp_conn_id[idx] := -1; |
| 47 | } |
Harald Welte | cd451ef | 2019-05-06 17:20:44 +0200 | [diff] [blame] | 48 | res := CBSP_CodecPort_CtrlFunct.f_IPL4_connect(CBSP[idx], remote_host, remote_port, |
| 49 | local_host, local_port, 0, { tcp :={} }); |
| 50 | if (not ispresent(res.connId)) { |
Neels Hofmeyr | 7944004 | 2020-07-29 00:49:27 +0200 | [diff] [blame] | 51 | setverdict(fail, "Could not connect to CBSP port, check your configuration ", |
| 52 | "{remote ", remote_host, ":", remote_port, " local ", local_host, ":", local_port, "}"); |
Harald Welte | cd451ef | 2019-05-06 17:20:44 +0200 | [diff] [blame] | 53 | mtc.stop; |
| 54 | } |
| 55 | g_cbsp_conn_id[idx] := res.connId; |
| 56 | |
| 57 | f_set_tcp_segmentation(idx); |
| 58 | } |
| 59 | |
| 60 | /* Function to use to bind to a local port as IPA server, accepting remote clients */ |
| 61 | function f_bind(charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0) |
| 62 | runs on CBSP_Adapter_CT { |
| 63 | var IPL4asp_Types.Result res; |
| 64 | map(self:CBSP[idx], system:CBSP); |
Pau Espin Pedrol | 28aa466 | 2020-09-15 18:32:59 +0200 | [diff] [blame] | 65 | if (g_cbsp_conn_id[idx] != -1) { |
| 66 | CBSP_CodecPort_CtrlFunct.f_IPL4_close(CBSP[idx], g_cbsp_conn_id[idx], {tcp := {}}); |
| 67 | g_cbsp_conn_id[idx] := -1; |
| 68 | } |
Harald Welte | cd451ef | 2019-05-06 17:20:44 +0200 | [diff] [blame] | 69 | res := CBSP_CodecPort_CtrlFunct.f_IPL4_listen(CBSP[idx], local_host, local_port, { tcp:={} }); |
Pau Espin Pedrol | 28aa466 | 2020-09-15 18:32:59 +0200 | [diff] [blame] | 70 | if (not ispresent(res.connId)) { |
| 71 | setverdict(fail, "Could not bind to CBSP port, check your configuration ", |
| 72 | "{local ", local_host, ":", local_port, "}"); |
| 73 | mtc.stop; |
| 74 | } |
Harald Welte | cd451ef | 2019-05-06 17:20:44 +0200 | [diff] [blame] | 75 | g_cbsp_conn_id[idx] := res.connId; |
| 76 | |
| 77 | f_set_tcp_segmentation(idx); |
| 78 | } |
| 79 | |
| 80 | function f_cbsp_send(template (value) CBSP_PDU pdu, integer idx := 0) runs on CBSP_Adapter_CT { |
| 81 | CBSP[idx].send(ts_CBSP_Send(g_cbsp_conn_id[idx], pdu)); |
| 82 | } |
| 83 | |
| 84 | function f_cbsp_exp(template CBSP_PDU exp, integer idx := 0) runs on CBSP_Adapter_CT return CBSP_PDU { |
| 85 | var CBSP_RecvFrom rf; |
| 86 | CBSP[idx].receive(tr_CBSP_Recv(g_cbsp_conn_id[idx], exp)) -> value rf; |
| 87 | return rf.msg; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | } |