summaryrefslogtreecommitdiff
path: root/maximus/networkclock/src/NetworkClockProcessor.cpp
blob: 18d77bc937426e69a35753bbeaabe372546cf6fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/************************************************************************
                        NetworkClockProcessor.cpp - Copyright buret

Here you can write a license for your code, some comments or any other
information you want to have in your generated code. To to this simply
configure the "headings" directory in uml to point to a directory
where you have your heading files.

or you can just replace the contents of this file with your own.
If you want to do this, this file is located at

/usr/share/apps/umbrello/headings/heading.cpp

-->Code Generators searches for heading files based on the file extension
   i.e. it will look for a file name ending in ".h" to include in C++ header
   files, and for a file name ending in ".java" to include in all generated
   java code.
   If you name the file "heading.<extension>", Code Generator will always
   choose this file even if there are other files with the same extension in the
   directory. If you name the file something else, it must be the only one with that
   extension in the directory to guarantee that Code Generator will choose it.

you can use variables in your heading files which are replaced at generation
time. possible variables are : author, date, time, filename and filepath.
just write %variable_name%

This file was generated on %date% at %time%
The original location of this file is /home/buret/eclipse/maximus/networkclock/src/NetworkClockProcessor.cpp
**************************************************************************/

#include "NetworkClockProcessor.h"

#include "NetworkClockEvtList.h"
#include "NetworkClockEvt.h"
#include "ClockSciMsg.h"
#include "ISci.h"
#include "ISystem.h"
#include "IFunctionCall.h"
#include "IPhy.h"
#include "IEthernet.h"

#include "sci_types.h"

#include "Error.h"
#include "Logger.h"

#include <netinet/in.h> // for 'htonl()' and 'htons()'
#include <iomanip> // for 'setfill()' and 'setw()'
#include <iostream> // for 'cout', 'cerr' and 'clog'
using namespace std;

// For the 'processNextEvt' function
//
#define callMemberFunction(object, pMember) (object.*(pMember))
typedef bool (NetworkClockProcessor::*memberFunction)(NetworkClockEvt&);
memberFunction arrayProcess[NETWORK_CLOCK_TYPE_NB] = {  &NetworkClockProcessor::processEvtNone,
                                                        &NetworkClockProcessor::processEvtRemove,
                                                        &NetworkClockProcessor::processEvtStation,
                                                        &NetworkClockProcessor::processEvtFunctionCall,
                                                        &NetworkClockProcessor::processEvtPhy,
                                                        &NetworkClockProcessor::processEvtSystem,
                                                        &NetworkClockProcessor::processEvtEthernet  };


// Constructors/Destructors
//  


NetworkClockProcessor::NetworkClockProcessor ( ):
mCurrentTickValue(0),
mpNetworkClockEvtList(NULL),
mpSciServer(NULL),
mpSystemManager(NULL),
mpFunctionCallManager(NULL),
mpPhyProcessor(NULL),
mpEthernet(NULL)
{
  logFunction();
  
  initAttributes ();
}


NetworkClockProcessor::NetworkClockProcessor (  ISci * p_sci_server,
                                                ISystem * p_system_manager,
                                                IFunctionCall * p_function_call_manager,
                                                IPhy * p_phy_processor,
                                                IEthernet * p_ethernet   ):
mCurrentTickValue(0),
mpNetworkClockEvtList(NULL)
{
  logFunction();

  if (  (NULL != p_sci_server)
        && (NULL != p_system_manager)
        && (NULL != p_function_call_manager)
        && (NULL != p_phy_processor)
        && (NULL != p_ethernet)  )
  {
    mpSciServer = p_sci_server;
    mpSystemManager = p_system_manager;
    mpFunctionCallManager = p_function_call_manager;
    mpPhyProcessor = p_phy_processor;
    mpEthernet = p_ethernet;

    registerClockSciMsg ();
    mpPhyProcessor->setNetworkClock(this);
    mpSystemManager->setNetworkClock(this);
  } 
  else
  {
    errno = EINVAL;
    throw Error(__PRETTY_FUNCTION__, "A pointer is NULL", errno);
  }
  initAttributes ();
}


void NetworkClockProcessor::initAttributes ( )
{
  logFunction();
  
  mpNetworkClockEvtList = new  NetworkClockEvtList ();
  if (NULL == mpNetworkClockEvtList)
  {
    throw Error(__PRETTY_FUNCTION__, "Network clock event list pointer is NULL");  
  }
}


NetworkClockProcessor::~NetworkClockProcessor ( )
{
  logFunction();

  if (NULL != mpNetworkClockEvtList)
  {
    delete (mpNetworkClockEvtList);
    mpNetworkClockEvtList = NULL;
  }
  if (NULL != mpSciServer)
  {
    mpSciServer = NULL;
  }
  if (NULL != mpSystemManager)
  {
    mpSystemManager = NULL;
  }
  if (NULL != mpFunctionCallManager)
  {
    mpFunctionCallManager = NULL;
  }
  if (NULL != mpPhyProcessor)
  {
    mpPhyProcessor = NULL;
  }
  if (NULL != mpEthernet)
  {
    mpEthernet = NULL;
  }
}


//  
// Methods
//  


// Other methods
//  


// public methods
//  


void NetworkClockProcessor::createEvt ( const Network_Clock_Tick tick_value, const NetworkClockEvt & evt_to_send )
{
  logFunction();
  
  if ( !insertEvt (tick_value, evt_to_send) )
  {
    clog << logger(LOG_ERROR) << "cannot insert event!" << endl;
  }
}


bool NetworkClockProcessor::insertEvt ( const Network_Clock_Tick tick_value, const NetworkClockEvt & evt_to_insert )
{
  logFunction();
  bool bInsert = false;

  if (NULL != mpNetworkClockEvtList)
  {
    if ( mpNetworkClockEvtList->insertElement (tick_value, evt_to_insert) )
    {
      bInsert = true;
    }
  }
  else
  {
    throw Error(__PRETTY_FUNCTION__, "Network clock event list pointer is NULL");
  }

  return bInsert;
}


bool NetworkClockProcessor::removeEvt ( const Network_Clock_Tick tick_value, const NetworkClockEvt & evt_to_remove )
{
  logFunction();
  bool bRemove = false;
  
  if (NULL != mpNetworkClockEvtList)
  {
    if ( mpNetworkClockEvtList->removeElement (tick_value, evt_to_remove) )
    {
      bRemove = true;
    }
  }
  else
  {
    throw Error(__PRETTY_FUNCTION__, "Network clock event list pointer is NULL");
  }
  
  return bRemove;
}


bool NetworkClockProcessor::removeEvts ( const Sci_Msg_Station_Id station_id )
{
  logFunction();

  if (NULL == mpNetworkClockEvtList)
  {
    throw Error(__PRETTY_FUNCTION__, "Network clock event list pointer is NULL");
  }

  return mpNetworkClockEvtList->removeElement(station_id);
}


bool NetworkClockProcessor::processNextEvt ( const Network_Clock_Tick max_tick_value )
{
  logFunction();
  bool bProcess = true;

  /* If all stations are IDLE,
   * get and process events at the current tick in the NetworkClockEvtList.
   * If max tick value is reached, return false. */

  if ( (NULL != mpNetworkClockEvtList) && (NULL != mpSystemManager) )
  {
    if (mpSystemManager->areAllActiveStationsIdle())
    {
      NetworkClockEvt * pEvtToProcess = NULL;
      Network_Clock_Tick currentTickValue = getCurrentTickValue();

      // If an event has been get, update tick and process evt
      //
      if (mpNetworkClockEvtList->getNextElement(currentTickValue, pEvtToProcess))
      {
        if (NULL == pEvtToProcess)
        {
          throw Error(__PRETTY_FUNCTION__, "get event pointer is NULL");
        }
        if ( (0 == max_tick_value) || (max_tick_value > currentTickValue) )
        {
          bProcess = setCurrentTickValue(currentTickValue);
          clog << logger(LOG_INFO) << "processing event at tick " << dec << getCurrentTickValue() << "..." << endl;
          bProcess &= callMemberFunction (*this, arrayProcess[pEvtToProcess->getNetworkClockType()]) (*pEvtToProcess);
          if (bProcess)
          {
            // clear event that has been processed
            mpNetworkClockEvtList->removeElement(currentTickValue, *pEvtToProcess);
          }
          else
          {
            clog << logger(LOG_ERROR) << "event at tick " << dec << getCurrentTickValue() << " has not been correctly processed!" << endl;
          }
        }
        else
        {
          setCurrentTickValue(max_tick_value);
          clog << logger(LOG_INFO) << "tick " << dec << getCurrentTickValue() << " is reached" << endl;
          bProcess = false;
        }
      }
    }
  }
  else
  {
    throw Error(__PRETTY_FUNCTION__, "A pointer is NULL");
  }

  return bProcess;
}


bool NetworkClockProcessor::processEvtNone ( NetworkClockEvt & evt_to_process )
{
  logFunction();

  errno = ENOMSG;
  throw Error(__PRETTY_FUNCTION__, "event cannot be sent because type is not set", errno);

  return false;
}


bool NetworkClockProcessor::processEvtRemove ( NetworkClockEvt & evt_to_process )
{
  logFunction();

  errno = ENOMSG;
  throw Error(__PRETTY_FUNCTION__, "event cannot be sent because type is set to REMOVE", errno);

  return false;
}


// Goal of this method is to fill SCI msg data that have to be sent on an output pipe
//
bool NetworkClockProcessor::processEvtStation ( NetworkClockEvt & evt_to_process )
{
  logFunction();
  bool bProcessStation = false;

  clog << logger(LOG_COM) << "sending network clock SCI message..." << endl;

  ClockSciMsg clockSciMsgToSend(this);

  // Decompose current tick value into two uint32_t values
  //
  uint32_t netclockHigh=0, netclockLow=0;
  netclockLow = (uint32_t)(getCurrentTickValue());
  netclockHigh = (uint32_t)(((uint64_t)(getCurrentTickValue()) >> 32));

  // Fill specialized SCI msg header
  //
  struct Network_Clock_Header clockSciMsgHeader = { NETWORK_CLOCK_VERSION,
                                                    static_cast<uint8_t>(evt_to_process.getNetworkClockType()),
                                                    evt_to_process.getNetworkClockId(),
                                                    0x0000, // flags
                                                    0x0000, // reserved
                                                    netclockHigh,
                                                    netclockLow };

  // Set specialized SCI msg header
  //
  bProcessStation = clockSciMsgToSend.setSpecializedSciMsgHeader(clockSciMsgHeader);

  // As specialized SCI msg header will be sent on an output pipe, 'hton' functions have to be called
  //
  clockSciMsgToSend.setId(htons(clockSciMsgToSend.getSpecializedSciMsgHeader().id));
  clockSciMsgToSend.setTickHigh(htonl(clockSciMsgToSend.getSpecializedSciMsgHeader().tick_high));
  clockSciMsgToSend.setTickLow(htonl(clockSciMsgToSend.getSpecializedSciMsgHeader().tick_low));

  // Fill specialized SCI msg attributes:
  // - header size
  //
  bProcessStation &= clockSciMsgToSend.setSpecializedSciMsgHeaderSize(static_cast<unsigned long>(sizeof(Network_Clock_Header)));

  // Fill SCI msg attributes:
  // - type
  //
  bProcessStation &= clockSciMsgToSend.setSciMsgType(SCI_MSG_TYPE_NETWORK_CLOCK);

  // Fill specialized SCI msg data length and specialized SCI msg data
  // When sending a clock SCI msg, there is no clock SCI msg data
  //
  bProcessStation &= clockSciMsgToSend.setSpecializedSciMsgDataLength(0);
  bProcessStation &= clockSciMsgToSend.setSpecializedSciMsgData(NULL);

  // Fill SCI msg attributes:
  // - station id
  //
  bProcessStation &= clockSciMsgToSend.setSciMsgStationId(evt_to_process.getStationId());

  if (0 != bProcessStation)
  {
    // SCI msg is sent on an output pipe by the SCI server
    //
    bProcessStation = sendEvtMsg (clockSciMsgToSend);
  }
  else
  {
    clog << logger(LOG_ERROR) << "event cannot be sent because it is not correctly filled in!" << endl;
  }

  return bProcessStation;
}


bool NetworkClockProcessor::processEvtFunctionCall ( NetworkClockEvt & evt_to_process )
{
  logFunction();

  errno = ENOMSG;
  throw Error(__PRETTY_FUNCTION__, "event cannot be sent because type is set to FUNCTION_CALL", errno);

  return false;
}


bool NetworkClockProcessor::processEvtPhy ( NetworkClockEvt & evt_to_process )
{
  logFunction();

  return getPhy()->receiveEvt(evt_to_process);
}


bool NetworkClockProcessor::processEvtSystem ( NetworkClockEvt & evt_to_process )
{
  logFunction();
  
  return mpSystemManager->updateStationStatus(evt_to_process.getStationId(), MAXIMUS_STATION_STATUS_IDLE);
}


bool NetworkClockProcessor::processEvtEthernet ( NetworkClockEvt & evt_to_process )
{
  logFunction();

  errno = ENOMSG;
  throw Error(__PRETTY_FUNCTION__, "event cannot be processed because type is set to ETHERNET", errno);

  return false;
}


// private methods
//  


bool NetworkClockProcessor::sendEvtMsg ( ClockSciMsg & clock_sci_msg_to_send )
{
  logFunction();
  bool bSend = false;
  
  if (NULL != mpSciServer)
  {
    bSend = mpSciServer->fillSciMsg (clock_sci_msg_to_send);
    bSend &= mpSciServer->sendSciMsg (clock_sci_msg_to_send);
  }
  else
  {
    throw Error(__PRETTY_FUNCTION__, "SCI server pointer is NULL");
  }
    
  return bSend;
}


void NetworkClockProcessor::registerClockSciMsg ( )
{
  logFunction();
  
  if (NULL != mpSciServer)
  {
    mpSciServer->registerSpecializedSciMsg(SCI_MSG_TYPE_NETWORK_CLOCK, new ClockSciMsg(this));
  }
  else
  {
    throw Error(__PRETTY_FUNCTION__, "SCI server pointer is NULL");
  }
}


// protected methods
//  


// Accessor methods
//  


// public attribute accessor methods
//  


// private attribute accessor methods
//  


Network_Clock_Tick NetworkClockProcessor::getCurrentTickValue ( ) const
{
  return mCurrentTickValue;
}


bool NetworkClockProcessor::setCurrentTickValue ( const Network_Clock_Tick tick_value )
{
  bool bTick = false;
  
  mCurrentTickValue = tick_value;
  bTick = mpSciServer->updateTickValue(mCurrentTickValue);
  bTick &= logger.updateTickValue(mCurrentTickValue);
  
  return bTick;
}


NetworkClockEvtList * NetworkClockProcessor::getNetworkClockEvtList ( )
{
  return mpNetworkClockEvtList;
}


IPhy * NetworkClockProcessor::getPhy ( ) const
{
  if (NULL == mpPhyProcessor)
  {
    throw Error(__PRETTY_FUNCTION__, "PHY processor pointer is NULL");
  }

  return mpPhyProcessor;
}


// protected attribute accessor methods
//