OpenTTCN/Working documents/Mappings/C to TTCN-3 mapping/main.ttcn
From OpenTTCN
module main
{
import from C_types all;
import from ApplicationTypes all;
type port PortType message
{
inout Message;
}
type record address
{
charstring host,
integer portField
}
modulepar
{
// IP address and UDP port number of the SUT.
charstring PX_SUT_IP_ADDR := "127.0.0.1";
integer PX_SUT_PORT := 7431;
}
type component ComponentType
{
port PortType p;
timer T_GUARD := 10.0;
var address sut_addr :=
{
host := PX_SUT_IP_ADDR,
portField := PX_SUT_PORT
};
}
type component TSI_Type
{
port PortType tsiPort;
}
/*****************************************************************************
Frame format:
Octet Frame
+-------------------------------------+
1 | Type code (most significant byte) |
+-------------------------------------+
2 | Type code (least significant byte) |
+-------------------------------------+
3 | Message content |
| (C data) |
N | |
+-------------------------------------+
*****************************************************************************/
type union Payload
{
T1 t1,
T2 t2,
T2b t2b,
T3 t3,
T4 t4,
T4b t4b,
ST1 st1,
ST2 st2,
ST2b st2b,
ST3 st3,
ST3b st3b,
ST4 st4,
ST4b st4b,
UT1 ut1,
UT2 ut2,
UT2b ut2b,
UT3 ut3,
UT3b ut3b,
UT4 ut4,
UT4b ut4b,
FT1 ft1,
FT2 ft2,
FT3 ft3,
BS0 bs0,
BS0b bs0b,
BS0c bs0c,
BS0d bs0d,
BS1 bs1,
BS2 bs2,
BS2Tag bs2tag,
BS3 bs3,
WORDREGS wordregs,
BYTEREGS byteregs,
REGS regs,
DAY day,
ALT_REGS alt_regs,
STRUCT_A struct_a,
STRUCT_B struct_b,
STRUCT_C struct_c
}
/////////////////////////////////////////////////////////////////////////////
// Values of typeCode in the Message type. Message content is interpreted
// based on the type code.
/////////////////////////////////////////////////////////////////////////////
const integer PAYLOAD_TYPE_T1 := 1;
const integer PAYLOAD_TYPE_T2 := 10;
const integer PAYLOAD_TYPE_T2b := 20;
const integer PAYLOAD_TYPE_T3 := 30;
const integer PAYLOAD_TYPE_T4 := 40;
const integer PAYLOAD_TYPE_T4b := 50;
const integer PAYLOAD_TYPE_ST1 := 60;
const integer PAYLOAD_TYPE_ST2 := 70;
const integer PAYLOAD_TYPE_ST2b := 80;
const integer PAYLOAD_TYPE_ST3 := 90;
const integer PAYLOAD_TYPE_ST3b := 100;
const integer PAYLOAD_TYPE_ST4 := 110;
const integer PAYLOAD_TYPE_ST4b := 120;
const integer PAYLOAD_TYPE_UT1 := 130;
const integer PAYLOAD_TYPE_UT2 := 140;
const integer PAYLOAD_TYPE_UT2b := 150;
const integer PAYLOAD_TYPE_UT3 := 160;
const integer PAYLOAD_TYPE_UT3b := 170;
const integer PAYLOAD_TYPE_UT4 := 180;
const integer PAYLOAD_TYPE_UT4b := 190;
const integer PAYLOAD_TYPE_FT1 := 200;
const integer PAYLOAD_TYPE_FT2 := 210;
const integer PAYLOAD_TYPE_FT3 := 220;
const integer PAYLOAD_TYPE_BS0 := 222;
const integer PAYLOAD_TYPE_BS0b := 224;
const integer PAYLOAD_TYPE_BS0c := 226;
const integer PAYLOAD_TYPE_BS0d := 228;
const integer PAYLOAD_TYPE_BS1 := 230;
const integer PAYLOAD_TYPE_BS2 := 240;
const integer PAYLOAD_TYPE_BS2Tag := 250;
const integer PAYLOAD_TYPE_BS3 := 260;
const integer PAYLOAD_TYPE_WORDREGS := 270;
const integer PAYLOAD_TYPE_BYTEREGS := 280;
const integer PAYLOAD_TYPE_REGS := 290;
const integer PAYLOAD_TYPE_DAY := 300;
const integer PAYLOAD_TYPE_ALT_REGS := 310;
const integer PAYLOAD_TYPE_STRUCT_A := 320;
const integer PAYLOAD_TYPE_STRUCT_B := 330;
const integer PAYLOAD_TYPE_STRUCT_C := 340;
/////////////////////////////////////////////////////////////////////////////
type record Message
{
integer typeCode, // two octets, big-endian
Payload payload // plain C data defined by the union field type
}
altstep DefaultAltstep() runs on ComponentType
{
[] any port.check
{
setverdict(fail);
stop;
}
[] T_GUARD.timeout
{
setverdict(fail);
stop;
}
}
type record of Message Messages;
testcase TC_1()
runs on ComponentType
system TSI_Type
{
activate(DefaultAltstep());
T_GUARD.start;
map(mtc:p, system:tsiPort);
log("--------------- Group 1 ---------------");
var BS0 bs0var :=
{
f1 := "B",
f2 := 32764,
f3 := 1,
f4 := "N"
}
var BS0b bs0bvar :=
{
fld0 := 32765,
fld1 := bs0var,
fld2 := 129,
fld3 := -32767,
fld4 := bs0var,
fld5 := 254
}
var Messages m :=
{
/////////////////////////////////////////////////////////////////////
{
typeCode := PAYLOAD_TYPE_T1,
payload :=
{
t1 := "A"
}
},
{
typeCode := PAYLOAD_TYPE_T2,
payload :=
{
t2 := -16385
}
},
{
typeCode := PAYLOAD_TYPE_T2b,
payload :=
{
t2b := 30000
}
},
{
typeCode := PAYLOAD_TYPE_T3,
payload :=
{
t3 := -2000000000
}
},
{
typeCode := PAYLOAD_TYPE_T4,
payload :=
{
t4 := 2000000000
}
},
{
typeCode := PAYLOAD_TYPE_T4b,
payload :=
{
t4b := -2147483648
}
},
{
typeCode := PAYLOAD_TYPE_T4b,
payload :=
{
t4b := 2147483647
}
},
/////////////////////////////////////////////////////////////////////
{
typeCode := PAYLOAD_TYPE_ST1,
payload :=
{
st1 := 120
}
},
{
typeCode := PAYLOAD_TYPE_ST1,
payload :=
{
st1 := -120
}
},
{
typeCode := PAYLOAD_TYPE_ST2,
payload :=
{
st2 := -16385
}
},
{
typeCode := PAYLOAD_TYPE_ST2b,
payload :=
{
st2b := 32767
}
},
{
typeCode := PAYLOAD_TYPE_ST2b,
payload :=
{
st2b := -32768
}
},
{
typeCode := PAYLOAD_TYPE_ST3,
payload :=
{
st3 := -2000000000
}
},
{
typeCode := PAYLOAD_TYPE_ST3b,
payload :=
{
st3b := -2147483648
}
},
{
typeCode := PAYLOAD_TYPE_ST4,
payload :=
{
st4 := 2000000000
}
},
{
typeCode := PAYLOAD_TYPE_ST4b,
payload :=
{
st4b := -2147483648
}
},
{
typeCode := PAYLOAD_TYPE_ST4b,
payload :=
{
st4b := 2147483647
}
},
/////////////////////////////////////////////////////////////////////
{
typeCode := PAYLOAD_TYPE_UT1,
payload :=
{
ut1 := 120
}
},
{
typeCode := PAYLOAD_TYPE_UT1,
payload :=
{
ut1 := 240
}
},
{
typeCode := PAYLOAD_TYPE_UT1,
payload :=
{
ut1 := 255
}
},
{
typeCode := PAYLOAD_TYPE_UT2,
payload :=
{
ut2 := 16385
}
},
{
typeCode := PAYLOAD_TYPE_UT2b,
payload :=
{
ut2b := 32767
}
},
{
typeCode := PAYLOAD_TYPE_UT2b,
payload :=
{
ut2b := 65535
}
},
{
typeCode := PAYLOAD_TYPE_UT3,
payload :=
{
ut3 := 4000000000
}
},
{
typeCode := PAYLOAD_TYPE_UT3b,
payload :=
{
ut3b := 2147483648
}
},
{
typeCode := PAYLOAD_TYPE_UT4,
payload :=
{
ut4 := 2000000000
}
},
{
typeCode := PAYLOAD_TYPE_UT4,
payload :=
{
ut4 := 4294967295
}
},
{
typeCode := PAYLOAD_TYPE_UT4b,
payload :=
{
ut4b := 2147483648
}
},
{
typeCode := PAYLOAD_TYPE_UT4b,
payload :=
{
ut4b := 2147483647
}
},
{
typeCode := PAYLOAD_TYPE_UT4b,
payload :=
{
ut4b := 4294967295
}
},
/////////////////////////////////////////////////////////////////////
{
typeCode := PAYLOAD_TYPE_FT1,
payload :=
{
ft1 := 2.5E3
}
},
{
typeCode := PAYLOAD_TYPE_FT1,
payload :=
{
ft1 := -2.5E3
}
},
{
typeCode := PAYLOAD_TYPE_FT1,
payload :=
{
ft1 := -0.125
}
},
{
typeCode := PAYLOAD_TYPE_FT2,
payload :=
{
ft2 := 2.5E3
}
},
{
typeCode := PAYLOAD_TYPE_FT2,
payload :=
{
ft2 := -2.5E3
}
},
{
typeCode := PAYLOAD_TYPE_FT2,
payload :=
{
ft2 := -0.125E-3
}
},
{
typeCode := PAYLOAD_TYPE_FT3,
payload :=
{
ft3 := 2.5E3
}
},
{
typeCode := PAYLOAD_TYPE_FT3,
payload :=
{
ft3 := -2.5E3
}
},
{
typeCode := PAYLOAD_TYPE_FT3,
payload :=
{
ft3 := -0.125E-3
}
},
{
typeCode := PAYLOAD_TYPE_BS0,
payload :=
{
bs0 := bs0var
}
},
{
typeCode := PAYLOAD_TYPE_BS0b,
payload :=
{
bs0b := bs0bvar
}
},
{
typeCode := PAYLOAD_TYPE_BS0c,
payload :=
{
bs0c :=
{
ff1 := "Z",
ff2 := bs0bvar,
ff3 := "Y",
ff4 := bs0var,
ff5 := -20
}
}
},
{
typeCode := PAYLOAD_TYPE_BS0d,
payload :=
{
bs0d :=
{
f1 := "M",
f2 := -128,
f3 := 250,
f4 := -32000,
f5 := 251,
f6 := 64500,
f7 := 252,
f8 := -2000000001,
f9 := 253,
f10 := 2000000003,
f11 := 254,
f12 := 0.125,
f13 := 255,
f14 := 0.125E-3,
f15 := 192,
f16 := 0.25E-3,
f17 := 193
}
}
},
{
typeCode := PAYLOAD_TYPE_BS1,
payload :=
{
bs1 :=
{
data := 192,
bits := 2147483647
}
}
},
{
typeCode := PAYLOAD_TYPE_BS2,
payload :=
{
bs2 :=
{
data := "U",
bits := 2147483647 / 2
}
}
},
{
typeCode := PAYLOAD_TYPE_BS2Tag,
payload :=
{
bs2tag :=
{
data := "U",
bits := 2147483647 / 2
}
}
},
{
typeCode := PAYLOAD_TYPE_BS3,
payload :=
{
bs3 :=
{
data := -64,
bits := 32767
}
}
},
{
typeCode := PAYLOAD_TYPE_STRUCT_A,
payload :=
{
struct_a :=
{
x := 2147483647 / 2,
x2 := "A",
z :=
{
y := -2147483647 / 2,
y2 := "B"
},
z2 := "C"
}
}
},
{
typeCode := PAYLOAD_TYPE_DAY,
payload :=
{
day := DAY_tuesday
}
}
}
var integer TEST_MSGS_NUM := sizeof(m);
for (var integer i := 0; i < TEST_MSGS_NUM; i := i + 1)
{
p.send(m[i]) to sut_addr;
p.receive(m[i]) from sut_addr;
}
log("--------------- Group 2 ---------------");
var REGS regs_var1 :=
{
x := { ax := 256, bx := 770 },
h := omit
},
regs_var2 :=
{
x := omit,
h := { al := 0, ah := 1, bl := 2, bh := 3 }
},
regs_var3 :=
{
x := { ax := 256, bx := 770 },
h := { al := 0, ah := 1, bl := 2, bh := 3 }
}
var Messages out_m :=
{
/////////////////////////////////////////////////////////////////////
{
typeCode := PAYLOAD_TYPE_REGS,
payload :=
{
regs := regs_var1
}
},
{
typeCode := PAYLOAD_TYPE_REGS,
payload :=
{
regs := regs_var2
}
},
{
typeCode := PAYLOAD_TYPE_ALT_REGS,
payload :=
{
alt_regs :=
{
x := omit,
h := { al := 1, ah := 2, bl := 3, bh := 4 }
}
}
},
{
typeCode := PAYLOAD_TYPE_STRUCT_B,
payload :=
{
struct_b :=
{
f1 := 64,
f2 := regs_var2,
f3 := 65
}
}
},
{
typeCode := PAYLOAD_TYPE_STRUCT_C,
payload :=
{
struct_c :=
{
f1 := 64,
f2 :=
{
u1 := omit,
u2 := 32001
},
f3 := 65
}
}
},
{
typeCode := PAYLOAD_TYPE_STRUCT_C,
payload :=
{
struct_c :=
{
f1 := 130,
f2 :=
{
u1 :=
{
uf1 := omit,
uf2 :=
{
b1 := 1234567890,
b2 := 2145678901,
b3 :=
{
f1 := 64,
f2 :=
{
x := omit,
h := { al := 15, ah := 25, bl := 35, bh := 45 }
},
f3 := 65
},
b4 := 1324567890
},
uf3 := omit,
uf4 := omit,
uf5 := omit,
uf6 := omit,
uf7 := omit
},
u2 := omit
},
f3 := 131
}
}
}
}
var template Messages in_m :=
{
/////////////////////////////////////////////////////////////////////
{
typeCode := PAYLOAD_TYPE_REGS,
payload :=
{
regs := regs_var3
}
},
{
typeCode := PAYLOAD_TYPE_REGS,
payload :=
{
regs := regs_var3
}
},
{
typeCode := PAYLOAD_TYPE_ALT_REGS,
payload :=
{
alt_regs :=
{
x := { ax := 513, bx := 1027 },
h := { al := 1, ah := 2, bl := 3, bh := 4 }
}
}
},
{
typeCode := PAYLOAD_TYPE_STRUCT_B,
payload :=
{
struct_b :=
{
f1 := 64,
f2 := regs_var3,
f3 := 65
}
}
},
{
typeCode := PAYLOAD_TYPE_STRUCT_C,
payload :=
{
struct_c :=
{
f1 := 64,
f2 :=
{
u1 := ?,
u2 := 32001
},
f3 := 65
}
}
},
{
typeCode := PAYLOAD_TYPE_STRUCT_C,
payload :=
{
struct_c :=
{
f1 := 130,
f2 :=
{
u1 :=
{
uf1 :=
{
a1 := 210,
a2 := 2145678901,
a3 := 64
},
uf2 :=
{
b1 := 1234567890,
b2 := 2145678901,
b3 :=
{
f1 := 64,
f2 :=
{
x := { ax := 6415, bx := 11555 },
h := { al := 15, ah := 25, bl := 35, bh := 45 }
},
f3 := 65
},
b4 := 1324567890
},
uf3 :=
{
c1 := 1234567890
},
uf4 :=
{
d1 := 210,
d2 := 722
},
uf5 :=
{
e1 := 1234567890,
e2 := 53,
e3 := 420413504
},
uf6 :=
{
f1 := 210,
f2 :=
{
x := { ax := 18838, bx := 30261 },
h := { al := 150, ah := 73, bl := 53, bh := 118 }
},
f3 := 228
},
uf7 := 210
},
u2 := 722
},
f3 := 131
}
}
}
}
TEST_MSGS_NUM := sizeof(out_m);
for (var integer i := 0; i < TEST_MSGS_NUM; i := i + 1)
{
p.send(out_m[i]) to sut_addr;
p.receive(in_m[i]) from sut_addr;
}
setverdict(pass);
}
testcase TC_mismatch_demo()
runs on ComponentType
system TSI_Type
{
activate(DefaultAltstep());
T_GUARD.start;
map(mtc:p, system:tsiPort);
p.send(Message :
{
typeCode := PAYLOAD_TYPE_STRUCT_C,
payload :=
{
struct_c :=
{
f1 := 130,
f2 :=
{
u1 :=
{
uf1 := omit,
uf2 :=
{
b1 := 1234567890,
b2 := 2145678901,
b3 :=
{
f1 := 64,
f2 :=
{
x := omit,
h := { al := 15, ah := 25, bl := 35, bh := 45 }
},
f3 := 65
},
b4 := 1324567890
},
uf3 := omit,
uf4 := omit,
uf5 := omit,
uf6 := omit,
uf7 := omit
},
u2 := omit
},
f3 := 131
}
}
}) to sut_addr;
alt
{
[] p.receive(Message :
{
typeCode := PAYLOAD_TYPE_STRUCT_C,
payload :=
{
struct_c :=
{
f1 := 130,
f2 :=
{
u1 :=
{
uf1 := ?,
uf2 :=
{
b1 := 1234567890,
b2 := 2145678901,
b3 :=
{
f1 := 64,
f2 :=
{
x := ?,
h := { al := 15, ah := 25000 /* causes mismatch */, bl := 35, bh := 45 }
},
f3 := 65
},
b4 := 1324567890
},
uf3 := ?,
uf4 := ?,
uf5 := ?,
uf6 := ?,
uf7 := ?
},
u2 := ?
},
f3 := 131
}
}
}) from sut_addr
{
setverdict(fail);
stop;
}
[] p.receive
{
setverdict(pass);
}
}
}
}
