ItsRSUsSimulator_Functions.ttcn 35.7 KB
Newer Older
garciay's avatar
garciay committed
1
2
3
4
5
6
7
8
9
10
11
12
13
module ItsRSUsSimulator_Functions {
    
    // LibCommon
    import from LibCommon_BasicTypesAndValues all;
    import from LibCommon_DataStrings all;
    import from LibCommon_VerdictControl all;
    import from LibCommon_Sync all;
    import from LibCommon_Time all;
    
    // LibIts
    import from ITS_Container language "ASN.1:1997" all;
    import from CAM_PDU_Descriptions language "ASN.1:1997" all;
    import from DENM_PDU_Descriptions language "ASN.1:1997" all;
14
15
16
17
18
    import from MAPEM_PDU_Descriptions language "ASN.1:1997" all;
    import from SPATEM_PDU_Descriptions language "ASN.1:1997" all;
    import from IVIM_PDU_Descriptions language "ASN.1:1997" all;
    import from SREM_PDU_Descriptions language "ASN.1:1997" all;
    import from SSEM_PDU_Descriptions language "ASN.1:1997" all;
garciay's avatar
garciay committed
19
    import from DSRC language "ASN.1:1997" all;
garciay's avatar
garciay committed
20
21
22
23
24
25
26
27
    
    // LibItsCommon
    import from LibItsCommon_Functions all;
    
    // LibItsBtp
    import from LibItsBtp_TypesAndValues all;
    import from LibItsBtp_Templates all;
    
garciay's avatar
garciay committed
28
29
30
    // LibItsCam
    import from LibItsCam_Templates all;
    
garciay's avatar
garciay committed
31
32
33
    // LibItsDenm
    import from LibItsDenm_Templates all;
    
garciay's avatar
garciay committed
34
35
36
    // LibItsMapemSpatemm
    import from LibItsMapemSpatem_Templates all;
    
37
38
39
40
41
42
    // LibItsMapemSpatemm
    import from LibItsIvim_Templates all;
    
    // LibItsMapemSpatemm
    import from LibItsSremSsem_Templates all;
    
garciay's avatar
garciay committed
43
44
45
46
47
48
49
    // LibItsGeoNetworking
    import from LibItsGeoNetworking_TestSystem all;
    import from LibItsGeoNetworking_Functions all;
    import from LibItsGeoNetworking_Templates all;
    import from LibItsGeoNetworking_TypesAndValues all;
    import from LibItsGeoNetworking_Pixits all;
    
garciay's avatar
garciay committed
50
51
52
53
    // LibItsSecurity
    import from LibItsSecurity_TypesAndValues all;
    import from LibItsSecurity_Functions all;
    
garciay's avatar
garciay committed
54
55
56
57
58
59
60
    // AtsRSUsSimulator
    import from ItsRSUsSimulator_TypesAndValues all;
    import from ItsRSUsSimulator_TestSystem all;
    import from ItsRSUsSimulator_Templates all;
    import from ItsRSUsSimulator_Pics all;
    import from ItsRSUsSimulator_Pixits all;
    
garciay's avatar
garciay committed
61
62
    group externalFunctions {
        
garciay's avatar
garciay committed
63
64
65
66
67
68
69
70
71
        /**
         * @desc    External function to compute a position using a reference position, a distance and an orientation 
         * @param   p_refLongPosVector  Vehicle reference position
         * @param   p_cenLongPosVector  Collision point position
         * @param   p_rotation          Rotation angle in 1/10 of degrees (from North)
         * @param   p_latitude          Computed position's latitude
         * @param   p_longitude         Computed position's longitude
         * @remark See http://www.movable-type.co.uk/scripts/latlong.html
         */
garciay's avatar
garciay committed
72
73
74
75
76
        external function fx_computePositionFromRotation(
                                                         in Int32 p_refLatitude, 
                                                         in Int32 p_refLongitude, 
                                                         in Int32 p_cenLatitude, 
                                                         in Int32 p_cenLongitude, 
garciay's avatar
garciay committed
77
                                                         in Int32 p_rotation, 
garciay's avatar
garciay committed
78
79
80
81
82
83
                                                         out Int32 p_latitude,
                                                         out Int32 p_longitude
        );
        
    } // End of group externalFunctions
    
84
    group rsuConfigurationFunctions {
garciay's avatar
garciay committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        
        /**
         * @desc This configuration features:
         *       - one ITS node (IUT)
         *       - two ITS nodes (nodeA, nodeB)
         *       - Area1 which only includes NodeB and IUT
         *       - Area2 which only includes NodeB
         *         NodeB being close to the area center
         */
        function f_cf01Up() runs on ItsRSUsSimulator {
            
            // Local variables
            
            // Map
            map(self:acPort, system:acPort);
garciay's avatar
garciay committed
100
            //map(self:cfPort, system:cfPort);
garciay's avatar
garciay committed
101
102
103
104
105
106
107
108
109
            map(self:geoNetworkingPort, system:geoNetworkingPort);
            
            // Connect
            f_connect4SelfOrClientSync();
            activate(a_cf01Down());
            
            // Initialise secured mode
            f_initialiseSecuredMode();
            
110
            //Initialze the Config module
garciay's avatar
garciay committed
111
            //cfPort.send(CfInitialize:{});
garciay's avatar
garciay committed
112
113
            
            // Initialisations
114
115
            f_setup_rsu(vc_rsu_id);
             
garciay's avatar
garciay committed
116
117
118
119
        } // End of function f_cf01Up
        
        function f_cf01Down() runs on ItsRSUsSimulator {
            
120
121
            vc_rsuMessagesValueList := {};
            
garciay's avatar
garciay committed
122
123
124
125
            f_uninitialiseSecuredMode();
            
            // Unmap
            unmap(self:acPort, system:acPort);
garciay's avatar
garciay committed
126
            //unmap(self:cfPort, system:cfPort);
garciay's avatar
garciay committed
127
128
129
130
131
132
133
            unmap(self:geoNetworkingPort, system:geoNetworkingPort);
            
            // Disconnect
            f_disconnect4SelfOrClientSync();
            
        } // End of function f_cf01Down
        
garciay's avatar
garciay committed
134
135
136
        /**
         * @desc Default handling cf01 de-initialisation.
         */
garciay's avatar
garciay committed
137
        altstep a_cf01Down() runs on ItsRSUsSimulator {
garciay's avatar
garciay committed
138
139
140
141
142
143
144
145
            [] a_shutdown() {
                f_poDefault();
                f_cf01Down();
                log("*** a_cf01Down: INFO: TEST COMPONENT NOW STOPPING ITSELF! ***");
                stop;
            }
        } // End of altstep a_cf01Down
        
146
147
    } // End of group rsuConfigurationFunctions
    
garciay's avatar
garciay committed
148
149
150
151
    group usecase6 {
        
        function f_initialiseVehicleSimulatorComponent(
                                                       in ItsRSUsSimulator p_component,
garciay's avatar
garciay committed
152
                                                       in integer p_vehicleIndex,
garciay's avatar
garciay committed
153
154
155
156
                                                       out template (omit) CAM p_cam
        ) runs on ItsRSUsSimulator { 
            p_cam := 
                m_camMsg_vehicle(
garciay's avatar
garciay committed
157
                    f_getTsStationId() + p_vehicleIndex,
garciay's avatar
garciay committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
                    f_getCurrentTime() mod 65536,    // Shall be updated in function f_prepare_vehicle_cam
                    m_rsuPosition(
                        0,                           // Shall be computed
                        0                            // Shall be computed
                    )
            );
            map(p_component:geoNetworkingPort, system:geoNetworkingPort);
        } // End of function f_initialiseVehicleSimulatorComponent
        
        function f_uninitialiseVehicleSimulatorComponent(
                                                         in ItsRSUsSimulator p_component
        ) runs on ItsRSUsSimulator { 
            p_component.done;
            unmap(p_component:geoNetworkingPort, system:geoNetworkingPort);
        } // End of function f_uninitialiseVehicleSimulatorComponent
        
    } // End of group usecase6 
garciay's avatar
garciay committed
175
    
176
177
178
179
180
181
    function f_setup_rsu(
                         in integer p_rsu_id
    ) runs on ItsRSUsSimulator { 
        
        // Local variables
        var template (value) DenmParmContainers v_denmParmContainers;
182
        var template (omit) octetstring v_beacon := omit;
183
184
185
186
187
188
189
190
191
        var template (omit) DENMs v_denms := omit;
        var template (omit) CAM v_cam := omit;
        var template (omit) MAPEM v_mapem := omit;
        var template (omit) SPATEMs v_spatems := omit;
        var template (omit) IVIM v_ivim := omit;
        var template (omit) SSEM v_ssem := omit;
        var integer v_counter;
        
        vc_longPosVectorRsu := PICS_RSU_PARAMS[p_rsu_id].longPosVector;
192
193
        // MAPEM, only if PX_ETSI_USE_CASE_ID is set to 3
        if ((vc_mapem == true) and (PX_ETSI_USE_CASE_ID == 3) and ispresent(PICS_MAPEM_PARMS_RSUs[p_rsu_id].intersections)) {
194
195
196
            // Build the list of the MAPEM events
            v_mapem := 
                m_mapemParm(
197
                    PICS_RSU_PARAMS[vc_rsu_id].stationID,
198
199
200
201
202
203
204
205
                    m_mapem(
                        f_getMsgIssueRevision(),
                        PICS_MAPEM_PARMS_RSUs[p_rsu_id].intersections,
                        PICS_MAPEM_PARMS_RSUs[p_rsu_id].roadSegments
            ));
            // Update revision fields
            v_mapem.map_.intersections[0].revision := f_incMsgIssueRevision();
            // TODO Add more?
206
207
        } else {
            vc_mapem := false;
208
        }
209
210
        // SPATEM, only if PX_ETSI_USE_CASE_ID is set to 3
        if ((vc_spatem == true) and (PX_ETSI_USE_CASE_ID == 3) and (lengthof(PICS_SPATEM_PARMS_RSUs[p_rsu_id]) != 0)) {
211
212
213
214
            // Reset counter
            vc_currentPhaseStartTime := 36001;
            vc_endPhaseStartTime := 0;
            vc_spatemStatesId := 0;
garciay's avatar
garciay committed
215
            // Build the list of the SPATEM events
216
217
218
219
220
221
222
223
224
225
226
227
228
229
            for (v_counter := 0; v_counter < lengthof(PICS_SPATEM_PARMS_RSUs[p_rsu_id]); v_counter := v_counter + 1) {
                v_spatems[v_counter] := 
                    m_spatemParm(
                        PICS_RSU_PARAMS[p_rsu_id].stationID,
                        m_spatem(
                            PICS_SPATEM_PARMS_RSUs[p_rsu_id][v_counter].intersections,
                            "SignalGroupID #" & int2str(PICS_SPATEM_PARMS_RSUs[p_rsu_id][v_counter].signalGroupID)
                ));
                for (var integer v_intersection := 0; v_intersection < lengthof(v_spatems[v_counter].spat.intersections); v_intersection := v_intersection + 1) {
                    vc_states[v_counter][v_intersection] := v_spatems[v_counter].spat.intersections[v_intersection].states;
                } // End of 'for' statement
                // TODO Build SPATEM with dynamic values
            } // End of 'for' statement
            // TODO Build SPATEM with dynamic values
230
231
        } else {
            vc_spatem := false;
232
        }
233
234
        // IVIM, only if PX_ETSI_USE_CASE_ID is set to 5
        if ((vc_ivim == true) and (PX_ETSI_USE_CASE_ID == 5) and ispresent(PICS_IVIM_PARMS_RSUs[p_rsu_id].provider)) {
garciay's avatar
garciay committed
235
            // Build the list of the IVIM events
236
237
238
239
240
241
242
243
244
            v_ivim := 
                m_ivimParm(
                    PICS_RSU_PARAMS[p_rsu_id].stationID,
                    m_ivimStructure(
                        m_iviManagementContainer(
                            PICS_IVIM_PARMS_RSUs[p_rsu_id].provider,
                            PICS_IVIM_PARMS_RSUs[p_rsu_id].iviIdentificationNumber,
                            0//IviStatus_new_
                        ),
garciay's avatar
garciay committed
245
                        PICS_IVIM_PARMS_RSUs[p_rsu_id].iviContainers
246
247
248
            ));
            // Update ivi status
            v_ivim.ivi.mandatory.validFrom := f_getCurrentTime();
249
            v_ivim.ivi.mandatory.validTo := valueof(v_ivim.ivi.mandatory.validFrom) + 43200000; // 12hours
250
251
        } else {
            vc_ivim := false;
252
        }
253
        // DENM, only if PX_ETSI_USE_CASE_ID is set to 1
garciay's avatar
garciay committed
254
        if ((vc_denm == true) and (PX_ETSI_USE_CASE_ID == 1)) {
255
256
257
258
259
260
261
262
263
264
265
            // Build the list of the DENM events for the specified RSU (PX_RSU_ID) and the given zone (PX_ETSI_ZONE_ID)
            for (v_counter := 0; v_counter < lengthof(PICS_DENM_EVENTS_RSU[p_rsu_id][PX_ETSI_ZONE_ID - 1]); v_counter := v_counter + 1) {
                var DenmEventsParmsPerZone v_denmEventsParmsPerZone := PICS_DENM_EVENTS_RSU[p_rsu_id][PX_ETSI_ZONE_ID - 1];
                
                v_denmParmContainers := m_denmParmContainers(
                    PICS_RSU_PARAMS[p_rsu_id].stationID,
                    f_incDenmSequenceNumber(),
                    PICS_DENM_REPETITION_INTERVAL,
                    v_denmEventsParmsPerZone[v_counter].eventPosition,
                    v_denmEventsParmsPerZone[v_counter].causeCodeType,
                    v_denmEventsParmsPerZone[v_counter].eventHistory,
266
                    v_denmEventsParmsPerZone[v_counter].traces,
267
                    PICS_DENM_VALIDITY_DURATION,
268
269
                    PICS_DENM_RELEVANCE_DISTANCE,
                    PICS_DENM_RELEVANCE_TRAFFIC_DIRECTION
270
271
                );
                v_denms[v_counter] := valueof(
272
273
                    m_denmPdu_rsu(
                        PICS_RSU_PARAMS[p_rsu_id].stationID,
274
275
276
277
278
279
280
281
282
                        m_denm(
                               v_denmParmContainers.managementContainer, 
                               v_denmParmContainers.situationContainer,
                               v_denmParmContainers.locationContainer
                )));
                if (ispresent(v_denmEventsParmsPerZone[v_counter].roadWorksContainerExtended)) {
                    v_denms[v_counter].denm.alacarte := m_alacarte(v_denmEventsParmsPerZone[v_counter].roadWorksContainerExtended);
                }
            } // End of 'for' statement
283
            // Update referenceDenms field, all except the current one
garciay's avatar
garciay committed
284
285
286
287
288
289
290
291
292
293
294
295
            for (v_counter := 0; v_counter < lengthof(PICS_DENM_EVENTS_RSU[p_rsu_id][PX_ETSI_ZONE_ID - 1]); v_counter := v_counter + 1) {
                if (ispresent(v_denms[v_counter].denm.alacarte.roadWorks)) {
                    var integer v_referenceDenms := 0;
                    for (var integer v_counter1 := 0; v_counter1 < lengthof(PICS_DENM_EVENTS_RSU[p_rsu_id][PX_ETSI_ZONE_ID - 1]); v_counter1 := v_counter1 + 1) {
                        if (v_counter1 == v_counter) {
                            continue;
                        }
                        v_denms[v_counter].denm.alacarte.roadWorks.referenceDenms[v_referenceDenms] := v_denms[v_counter1].denm.management.actionID;
                        v_referenceDenms := v_referenceDenms + 1;
                    } // End of 'for' statement
                }
            } // End of 'for' statement
296
297
        } else {
            vc_denm := false;
298
299
        }
        // CAM
300
        if (vc_cam == true) {
301
            // Build the list of the CAM events
garciay's avatar
garciay committed
302
303
304
305
306
            if (PX_ETSI_USE_CASE_ID == 7) {
                vc_longPosVectorRsu := PICS_UC7_LPV.longPosVector;
                v_cam := 
                    m_camParm(
                        PICS_UC7_LPV.stationID,
garciay's avatar
garciay committed
307
308
                        PICS_UC7_LPV.stationType,
                        PICS_UC7_LPV.vehicleRole, 
garciay's avatar
garciay committed
309
310
311
312
313
314
315
316
317
318
319
320
                        m_rsuPosition(
                            PICS_UC7_LPV.longPosVector.latitude,
                            PICS_UC7_LPV.longPosVector.longitude
                        ), 
                        PICS_UC7_LPV.pathHistory,
                        {
                            rsuContainerHighFrequency := {
                                protectedCommunicationZonesRSU := omit
                            }
                        }
                );
            } else if (PX_ETSI_USE_CASE_ID == 9) {
321
322
323
                v_cam := 
                    m_camParm(
                        PICS_RSU_PARAMS[p_rsu_id].stationID,
garciay's avatar
garciay committed
324
325
                        -,
                        -,
326
327
328
329
330
331
332
                        m_rsuPosition(
                            vc_longPosVectorRsu.latitude,
                            vc_longPosVectorRsu.longitude
                        ), 
                        PICS_RSU_PARAMS[p_rsu_id].pathHistory,
                        m_highFrequencyContainer_rsuContainerHighFrequency(
                            m_rSUContainerHighFrequency(
garciay's avatar
garciay committed
333
                                PICS_UC9_PCZ
334
335
336
337
338
                )));
            } else {
                v_cam := 
                    m_camParm(
                        PICS_RSU_PARAMS[p_rsu_id].stationID,
garciay's avatar
garciay committed
339
340
                        -,
                        -,
341
342
343
344
                        m_rsuPosition(
                            vc_longPosVectorRsu.latitude,
                            vc_longPosVectorRsu.longitude
                        ), 
garciay's avatar
garciay committed
345
346
347
348
349
350
                        PICS_RSU_PARAMS[p_rsu_id].pathHistory,
                        {
                            rsuContainerHighFrequency := {
                                protectedCommunicationZonesRSU := omit
                            }
                        }
351
352
                );
            }
353
        } 
354
        if (vc_beacon == true) {
355
356
357
            // Build the list of the BeACON events
            v_beacon := 'AAAAAAAA'O; // TODO Use a PICS
        } 
358
359
360
        // Build the messages value list for this RSU
        vc_rsuMessagesValueList[p_rsu_id] := 
            m_rsuProfile(
361
                v_beacon,
362
363
364
365
366
367
368
369
                v_cam, 
                v_denms,
                v_mapem,
                v_spatems,
                v_ivim,
                v_ssem
        );
        if (PICS_RSU_PARAMS[p_rsu_id].geoShape == e_geoCircle) {
370
371
372
373
374
375
376
377
378
379
            vc_geoArea := {
                shape := e_geoCircle,
                area := {
                    geoAreaPosLatitude := vc_longPosVectorRsu.latitude,
                    geoAreaPosLongitude := vc_longPosVectorRsu.longitude,
                    distanceA := PICS_RSU_PARAMS[p_rsu_id].geoParms.radius,
                    distanceB := PICS_RSU_PARAMS[p_rsu_id].geoParms.radius,
                    angle := 0
                }
            }
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
        } else {
            log("*** " & testcasename() & ": INCONC: Wrong PICS_RSU_GEOAREA_FORM event initialisation ***");
            setverdict(inconc);
            stop;
        }
        
    } // End of function f_setup_rsu
    
    function f_process_cf_event(
                                in CfEvent p_cfEvent
    ) runs on ItsRSUsSimulator return boolean {
        var template (value) CfResult v_result := true;
        
        // Terminate simulation
        if (p_cfEvent == "stop") {
395
396
397
            if (ispresent(vc_rsuMessagesValueList[vc_rsu_id].beacon)) {
                tc_beacon.stop;
            }
398
            if (ispresent(vc_rsuMessagesValueList[vc_rsu_id].cam)) {
399
400
                tc_cam.stop;
            }
401
            if (ispresent(vc_rsuMessagesValueList[vc_rsu_id].denms)) {
402
403
                tc_denm.stop;
            }
404
            if (ispresent(vc_rsuMessagesValueList[vc_rsu_id].mapem)) {
405
406
                tc_mapem.stop;
            }
407
            if (ispresent(vc_rsuMessagesValueList[vc_rsu_id].spatems)) {
408
409
                tc_spatem.stop;
            }
410
            if (ispresent(vc_rsuMessagesValueList[vc_rsu_id].ivim)) {
411
412
413
414
415
416
417
418
419
420
                tc_ivim.stop;
            }
        } else {
            v_result := false;
        }
        
        cfPort.send(v_result);
        
        return false;
    } // End of function f_process_cf_event
garciay's avatar
garciay committed
421
    
422
423
424
425
426
427
428
429
430
431
432
433
    function f_prepare_beacon(
                              out template (value) Payload p_payload
    ) runs on ItsRSUsSimulator {
        
        p_payload := valueof(
            f_adaptPayload(
                vc_rsuMessagesValueList[vc_rsu_id].beacon,
                0,
                -,
                e_any
            )
        );
garciay's avatar
garciay committed
434
    } // End of function f_prepare_beacon
435
    
garciay's avatar
garciay committed
436
437
438
439
    function f_prepare_cam(
                           out template (value) Payload p_payload
    ) runs on ItsRSUsSimulator {
        
440
        vc_rsuMessagesValueList[vc_rsu_id].cam.cam.generationDeltaTime := f_getCurrentTime() mod 65536; // See ETSI EN 302 637-2 V1.3.0 - Clause B.3 generationDelatTime
garciay's avatar
garciay committed
441
442
443
444
445
        p_payload := valueof(
            f_adaptPayload(
                bit2oct(
                    encvalue(
                        valueof(
446
                            vc_rsuMessagesValueList[vc_rsu_id].cam
garciay's avatar
garciay committed
447
448
449
450
451
                ))),
                PICS_CAM_BTP_DESTINATION_PORT,
                PICS_CAM_BTP_SOURCE_PORT
            )
        );
452
    } // End of function f_prepare_cam
garciay's avatar
garciay committed
453
454
    
    function f_prepare_denm(
455
456
                            out template (value) Payload p_payload,
                            in boolean p_cancellation := false
garciay's avatar
garciay committed
457
458
    ) runs on ItsRSUsSimulator {
        
459
        // Update dynamic parms
460
        vc_rsuMessagesValueList[vc_rsu_id].denms[vc_denmEventCounter].denm.management.referenceTime  := f_getCurrentTime();
461
462
463
        if (p_cancellation == true) {
            vc_rsuMessagesValueList[vc_rsu_id].denms[vc_denmEventCounter].denm.management.termination := isCancellation;
        }
garciay's avatar
garciay committed
464
465
466
467
        p_payload := valueof(
            f_adaptPayload(
                bit2oct(
                    encvalue(
468
                        valueof(vc_rsuMessagesValueList[vc_rsu_id].denms[vc_denmEventCounter]
garciay's avatar
garciay committed
469
470
471
472
473
                ))),
                PICS_DENM_BTP_DESTINATION_PORT,
                PICS_DENM_BTP_SOURCE_PORT
            )
        ); 
474
        vc_denmEventCounter := (vc_denmEventCounter + 1) mod lengthof(vc_rsuMessagesValueList[vc_rsu_id].denms);
475
    } // End of function f_prepare_denm
garciay's avatar
garciay committed
476
477
478
479
480
    
    function f_prepare_mapem(
                             out template (value) Payload p_payload
    ) runs on ItsRSUsSimulator {
        
garciay's avatar
garciay committed
481
482
483
484
485
        p_payload := valueof(
            f_adaptPayload(
                bit2oct(
                    encvalue(
                        valueof(
486
                            vc_rsuMessagesValueList[vc_rsu_id].mapem
garciay's avatar
garciay committed
487
488
489
490
491
                ))),
                PICS_MAPEM_BTP_DESTINATION_PORT,
                PICS_MAPEM_BTP_SOURCE_PORT
            )
        );
492
    } // End of function f_prepare_mapem
garciay's avatar
garciay committed
493
    
494
495
496
497
498
499
500
501
    function f_computeEndPhaseStartTime(
                               in TimeMark p_timeMark,
                               in UInt32 p_inc
    ) return TimeMark {
        var UInt32 v_endPhaseStartTime := p_timeMark + p_inc;
        return v_endPhaseStartTime mod 36000;
    }
    
garciay's avatar
garciay committed
502
    function f_prepare_spatem(
503
                              in template (value) SPATEM p_spatem,
garciay's avatar
garciay committed
504
505
                              out template (value) Payload p_payload
    ) runs on ItsRSUsSimulator {
506
        var template (omit) SPATEM v_spatem := p_spatem; // Make a copy
507
        var TimeMark v_currentTimeMark := f_getCurrentTimeMark();
garciay's avatar
garciay committed
508
        
garciay's avatar
garciay committed
509
        if (vc_currentPhaseStartTime == 36001) { // First call
garciay's avatar
garciay committed
510
            vc_currentPhaseStartTime := v_currentTimeMark;
garciay's avatar
garciay committed
511
512
513
514
515
516
517
518
519
520
521
            select (vc_spatemStatesId) {
                case (0) {
                    vc_endPhaseStartTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_30);
                }
                case (1) {
                    vc_endPhaseStartTime := f_computeEndPhaseStartTime(v_currentTimeMark, PICS_SPATEM_REPITITION_DURATION_STATE_30);
                }
                case (2) {
                    vc_endPhaseStartTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_30);
                }
            } // End of 'select' statement
522
        }
garciay's avatar
garciay committed
523
524
        if (v_currentTimeMark >= vc_endPhaseStartTime) { // Change state
            vc_spatemStatesId := (vc_spatemStatesId + 1) mod vc_spatemStatesNum;
garciay's avatar
garciay committed
525
            vc_currentPhaseStartTime := v_currentTimeMark;
garciay's avatar
garciay committed
526
            vc_endPhaseStartTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_30);
527
528
529
        }
        log("vc_currentPhaseStartTime = ", vc_currentPhaseStartTime);
        log("v_currentTimeMark = ", v_currentTimeMark);
garciay's avatar
garciay committed
530
        log("vc_endPhaseStartTime = ", vc_endPhaseStartTime);
531
        log("vc_spatemStatesId = ", vc_spatemStatesId);
532
533
        // Rebuild SPATEM message
        for (var integer v_intersection := 0; v_intersection < lengthof(v_spatem.spat.intersections); v_intersection := v_intersection + 1) {
534
            var template (omit) MovementList v_states := vc_states[vc_signalGroupParmId][v_intersection];
535
536
537
            v_spatem.spat.intersections[v_intersection].states := { v_states[vc_spatemStatesId] };
            v_spatem.spat.intersections[v_intersection].moy := f_getMinuteOfTheYear();
            v_spatem.spat.intersections[v_intersection].timeStamp := f_getDSecond();
538
539
540
            select (vc_spatemStatesId) {
                case (0) {
                    v_spatem.spat.intersections[v_intersection].states[1] := v_states[1];
garciay's avatar
garciay committed
541
542
543
544
545
                    //v_spatem.spat.intersections[v_intersection].states[2] := v_states[2];
                    // Update startTime & minEndTime
                    v_spatem.spat.intersections[v_intersection].states[0].state_time_speed[0].timing.minEndTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_30);
                    v_spatem.spat.intersections[v_intersection].states[1].state_time_speed[0].timing.startTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_30);
                    v_spatem.spat.intersections[v_intersection].states[1].state_time_speed[0].timing.minEndTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_40);
546
547
548
                }
                case (1) {
                    v_spatem.spat.intersections[v_intersection].states[1] := v_states[2];
garciay's avatar
garciay committed
549
550
551
552
553
                    //v_spatem.spat.intersections[v_intersection].states[2] := v_states[0];
                    // Update startTime & minEndTime
                    v_spatem.spat.intersections[v_intersection].states[0].state_time_speed[0].timing.minEndTime := f_computeEndPhaseStartTime(v_currentTimeMark, PICS_SPATEM_REPITITION_DURATION_STATE_10);
                    v_spatem.spat.intersections[v_intersection].states[1].state_time_speed[0].timing.startTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_10);
                    v_spatem.spat.intersections[v_intersection].states[1].state_time_speed[0].timing.minEndTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_30);
554
555
556
                }
                case (2) {
                    v_spatem.spat.intersections[v_intersection].states[1] := v_states[0];
garciay's avatar
garciay committed
557
558
559
560
561
                    //v_spatem.spat.intersections[v_intersection].states[2] := v_states[1];
                    // Update startTime & minEndTime
                    v_spatem.spat.intersections[v_intersection].states[0].state_time_speed[0].timing.minEndTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_20);
                    v_spatem.spat.intersections[v_intersection].states[1].state_time_speed[0].timing.startTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_20);
                    v_spatem.spat.intersections[v_intersection].states[1].state_time_speed[0].timing.minEndTime := f_computeEndPhaseStartTime(vc_currentPhaseStartTime, PICS_SPATEM_REPITITION_DURATION_STATE_50);
562
563
564
565
566
                }
                case else {
                }
            } // End of 'select' statement
        } // End of 'for' statement
garciay's avatar
garciay committed
567
        log("v_spatem.spat.intersections = ", v_spatem.spat.intersections);
568
569
570
571
572
573
574
575
        
        p_payload := valueof(
            f_adaptPayload(
                bit2oct(
                    encvalue(
                        valueof(
                            v_spatem
                ))),
576
577
                PICS_SPATEM_BTP_DESTINATION_PORT,
                PICS_SPATEM_BTP_SOURCE_PORT
578
579
            )
        );
garciay's avatar
garciay committed
580
        
581
    } // End of function f_prepare_spatem
garciay's avatar
garciay committed
582
583
    
    function f_prepare_ivim(
584
                            out template (value) Payload p_payload
garciay's avatar
garciay committed
585
586
    ) runs on ItsRSUsSimulator {
        
587
588
589
590
591
        p_payload := valueof(
            f_adaptPayload(
                bit2oct(
                    encvalue(
                        valueof(
592
                            vc_rsuMessagesValueList[vc_rsu_id].ivim
593
594
595
596
597
                ))),
                PICS_IVIM_BTP_DESTINATION_PORT,
                PICS_IVIM_BTP_SOURCE_PORT
            )
        );
598
    } // End of function f_prepare_ivim
garciay's avatar
garciay committed
599
600
601
602
    
    function f_adaptPayload(
                            in template (value) octetstring p_finalPayload,
                            in template (value) BtpPortId  p_destPort,
603
604
                            in template (value) BtpPortId  p_srcPort := 0,
                            in NextHeader p_nextHeader := PX_GN_UPPER_LAYER
garciay's avatar
garciay committed
605
606
607
    ) return template (value) Payload {
        var template (value) Payload v_payload;
        
608
        if(p_nextHeader == e_any) {
garciay's avatar
garciay committed
609
610
611
612
            v_payload := { decodedPayload := omit, rawPayload := p_finalPayload};
            return v_payload;
        }
        
613
        if(p_nextHeader == e_ipv6) {
garciay's avatar
garciay committed
614
615
616
617
618
            log("*** " & testcasename() & ": INCONC: Layer IPv6 not supported ***");
            setverdict(inconc);
            stop;
        }
        
619
        if(p_nextHeader == e_btpA) {
garciay's avatar
garciay committed
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
            v_payload := { 
                decodedPayload := { 
                    btpPacket := m_btpAWithPorts(
                        p_destPort,
                        p_srcPort,
                        { 
                            decodedPayload := omit, 
                            rawPayload := p_finalPayload 
                        }
                    )
                },
                rawPayload := ''O
            };
            return v_payload;
        }
        
636
        if(p_nextHeader == e_btpB) {
garciay's avatar
garciay committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
            v_payload := { 
                decodedPayload := { 
                    btpPacket := m_btpBWithPorts(
                        p_destPort,
                        p_srcPort,
                        { 
                            decodedPayload := omit, 
                            rawPayload := p_finalPayload 
                        }
                    )
                }, 
                rawPayload := ''O
            };
            return v_payload;
        }
        
        return v_payload;
654
    } // End of function f_adaptPayload
garciay's avatar
garciay committed
655
    
garciay's avatar
garciay committed
656
657
658
659
    function f_mirror_and_send_vehicle_cam(
                                           in template (value) CAM p_camSimu,
                                           in CAM p_camVehicle,
                                           in ThreeDLocation p_location
garciay's avatar
garciay committed
660
661
    ) runs on ItsRSUsSimulator {
        // Local variables
garciay's avatar
garciay committed
662
        var ReferencePosition v_newPosition := p_camVehicle.cam.camParameters.basicContainer.referencePosition;
garciay's avatar
garciay committed
663
664
        var template (value) Payload v_payload;
        
garciay's avatar
garciay committed
665
        // Apply 90° rotation
garciay's avatar
garciay committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
        if (PICS_USE_SPV == true) {
            fx_computePositionFromRotation(
                p_location.latitude,
                p_location.longitude,
                PICS_UC6_COLLISION_POINT.latitude, 
                PICS_UC6_COLLISION_POINT.longitude, 
                900, 
                v_newPosition.latitude, 
                v_newPosition.longitude
            );
            log("Rotation: (", p_location.latitude, ", ", p_location.longitude, ") --> (", v_newPosition.latitude, ", ", v_newPosition.longitude, ")");
        } else {
            fx_computePositionFromRotation(
                p_camVehicle.cam.camParameters.basicContainer.referencePosition.latitude, 
                p_camVehicle.cam.camParameters.basicContainer.referencePosition.longitude, 
                PICS_UC6_COLLISION_POINT.latitude, 
                PICS_UC6_COLLISION_POINT.longitude, 
                900, 
                v_newPosition.latitude, 
                v_newPosition.longitude
            );
            log("Rotation: (", p_camVehicle.cam.camParameters.basicContainer.referencePosition.latitude, ", ", p_camVehicle.cam.camParameters.basicContainer.referencePosition.longitude, ") --> (", v_newPosition.latitude, ", ", v_newPosition.longitude, ")");
        }
garciay's avatar
garciay committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
        /*v_newPosition.latitude := ;
        v_newPosition.longitude := ;*/
        // Update CAM
        p_camSimu.cam.generationDeltaTime := f_getCurrentTime() mod 65536; // See ETSI EN 302 637-2 V1.3.0 - Clause B.3 generationDelatTime
        p_camSimu.cam.camParameters.basicContainer.referencePosition := v_newPosition;
        p_camSimu.cam.camParameters.highFrequencyContainer := p_camVehicle.cam.camParameters.highFrequencyContainer;
        v_payload := valueof(
            f_adaptPayload(
                bit2oct(
                    encvalue(
                        valueof(
                            p_camSimu
                ))),
                PICS_CAM_BTP_DESTINATION_PORT,
                PICS_CAM_BTP_SOURCE_PORT
            )
garciay's avatar
garciay committed
705
        );
garciay's avatar
garciay committed
706
707
708
709
710
711
712
713
714
        // Update GN
        vc_longPosVectorRsu := PICS_UC6_VEHICLE_TEMPLATE_POSITION;
        vc_longPosVectorRsu.latitude := valueof(p_camSimu.cam.camParameters.basicContainer.referencePosition.latitude);
        vc_longPosVectorRsu.longitude := valueof(p_camSimu.cam.camParameters.basicContainer.referencePosition.longitude);
        vc_geoArea := PICS_UC6_VEHICLE_TEMPLATE_GEOAREA;
        vc_geoArea.area.geoAreaPosLatitude := valueof(p_camSimu.cam.camParameters.basicContainer.referencePosition.latitude);
        vc_geoArea.area.geoAreaPosLongitude := valueof(p_camSimu.cam.camParameters.basicContainer.referencePosition.longitude);
        // And send it
        f_send(v_payload, PICS_CAM_ITS_AID);
garciay's avatar
garciay committed
715
    } // End of function f_mirror_and_send_vehicle_cam
garciay's avatar
garciay committed
716
    
garciay's avatar
garciay committed
717
    function f_send(
718
719
                    in template (value) Payload p_payload,
                    in UInt32 p_its_aid
garciay's avatar
garciay committed
720
721
722
    ) runs on ItsRSUsSimulator {
        var GeoNetworkingPdu v_geoNetworkingPdu;
        
garciay's avatar
garciay committed
723
        vc_longPosVectorRsu.timestamp_ := f_computeGnTimestamp();
garciay's avatar
garciay committed
724
        
725
726
        if (p_its_aid == 36) { // CAM 
            v_geoNetworkingPdu := valueof(m_geoNwPdu( // FIXME Use PIXIT parameter to get a fully configurable template
garciay's avatar
garciay committed
727
                m_geoNwShbPacket_payload(
728
729
730
731
                    vc_longPosVectorRsu,
                    valueof(p_payload)
                ),
                m_defaultLifetime,
garciay's avatar
garciay committed
732
                c_hopLimit1
733
734
735
736
737
738
739
740
741
742
743
744
            ));
        } else {
            v_geoNetworkingPdu := valueof(m_geoNwPdu( // FIXME Use PIXIT parameter to get a fully configurable template
                m_geoNwBroadcastPacket_payload( // TODO modifes 'template (value) GnNonSecuredPacket m_geoNwBroadcastPacket' to set the field
                    vc_longPosVectorRsu,
                    f_incLocalSeqNumber(),
                    f_geoArea2GeoBroadcastArea(vc_geoArea),
                    -,
                    -,
                    valueof(p_payload)
                ),
                m_defaultLifetime,
745
                c_hopLimit1
746
747
            ));
        }
748
        f_sendGeoNetMessage(valueof(m_geoNwReq_linkLayerBroadcast(v_geoNetworkingPdu, p_its_aid)));
749
    } // End of function f_send
garciay's avatar
garciay committed
750
751
752
753
    
    function f_processSrem(
                           in GeoNetworkingPdu p_geoNetworkingPdu
    ) runs on ItsRSUsSimulator {
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
        log("SREM=", p_geoNetworkingPdu);
        if (ispresent(p_geoNetworkingPdu.gnPacket.packet.payload.decodedPayload)) {
            if (ispresent(p_geoNetworkingPdu.gnPacket.packet.payload.decodedPayload.btpPacket.payload.decodedPayload)) {
                if (ischosen(p_geoNetworkingPdu.gnPacket.packet.payload.decodedPayload.btpPacket.payload.decodedPayload.sremPacket)) {
                    var SignalRequestMessage v_signalRequestMessage := p_geoNetworkingPdu.gnPacket.packet.payload.decodedPayload.btpPacket.payload.decodedPayload.sremPacket.srm;
                    var template (value) Payload v_payload;
                    
                    log(v_signalRequestMessage);
                    // Build response
                    // TODO v_ssem.ssm.status
                    v_payload := valueof(
                        f_adaptPayload(
                            bit2oct(
                                encvalue(
                                    valueof(
garciay's avatar
garciay committed
769
770
771
                                        m_ssem(
                                            v_signalRequestMessage
                            )))),
772
773
774
775
                            PICS_SSEM_BTP_DESTINATION_PORT,
                            PICS_SSEM_BTP_SOURCE_PORT
                        )
                    );
776
777
                    // Send SSEM 
                    f_send(v_payload, PICS_SSEM_ITS_AID);
778
779
780
                } // else, ignore message
            } // else, ignore message
        } // else, ignore message
781
    } // End of function f_processSrem
garciay's avatar
garciay committed
782
783
784
785
786
787
    
    function f_incLocalSeqNumber() runs on ItsRSUsSimulator return UInt16 {
        vc_localSeqNumber := (vc_localSeqNumber + 1) mod 65536;
        return vc_localSeqNumber;
    }
    
garciay's avatar
garciay committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
    function f_incMsgIssueRevision() runs on ItsRSUsSimulator return MsgCount {
        vc_msgIssueRevision := (vc_msgIssueRevision + 1) mod 128; // See MsgCount declaration
        return vc_msgIssueRevision;
    }
    
    function f_getMsgIssueRevision() runs on ItsRSUsSimulator return MsgCount {
        return vc_msgIssueRevision;
    }
    
    function f_incDenmSequenceNumber() runs on ItsRSUsSimulator return SequenceNumber {
        vc_sequenceNumber := (vc_sequenceNumber + 1) mod 65536; // See SequenceNumber declaration
        return vc_sequenceNumber;
    }
    
garciay's avatar
garciay committed
802
    function f_getDenmSequenceNumber() runs on ItsRSUsSimulator return SequenceNumber {
garciay's avatar
garciay committed
803
        return vc_sequenceNumber;
garciay's avatar
garciay committed
804
805
806
    }
    
} // End of module ItsRSUsSimulator_Functions