summaryrefslogtreecommitdiff
path: root/maximus/networkclock/src/ClockSciMsg.cpp
blob: af79e32ca6659fac905208b2faeaa3b7e4da1492 (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
/************************************************************************
                        ClockSciMsg.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/ClockSciMsg.cpp
**************************************************************************/

#include "ClockSciMsg.h"
#include "NetworkClockProcessor.h"
#include "Error.h"

#include <iomanip> // for 'std::setfill' and 'std::setw'
#include <iostream> // for 'cout', 'clog' and 'cerr'
#include <netinet/in.h> // for 'ntohl' and 'ntohs' functions
using namespace std;


// Constructors/Destructors
//  


ClockSciMsg::ClockSciMsg ( ):
mSpecializedSciMsgType(NETWORK_CLOCK_TYPE_NONE),
mSpecializedSciMsgId(0),
mSpecializedSciMsgTick(0),
mpSpecializedSciMsgHeader(NULL),
mpNetworkClockProcessor(NULL)
{
  //clog << "ClockSciMsg()" << endl;
  
  initAttributes();
}


ClockSciMsg::ClockSciMsg ( NetworkClockProcessor * p_network_clock_processor ):
mSpecializedSciMsgType(NETWORK_CLOCK_TYPE_NONE),
mSpecializedSciMsgId(0),
mSpecializedSciMsgTick(0),
mpSpecializedSciMsgHeader(NULL),
mpNetworkClockProcessor(NULL)
{
  //clog << "ClockSciMsg(NetworkClockProcessor*)" << endl;
  
  initAttributes();
  if (NULL != p_network_clock_processor)
  {
    mpNetworkClockProcessor = p_network_clock_processor;
  }
  else
  {
    throw Error("ClockSciMsg(NetworkClockProcessor*)", "Received NetworkClockProcessor pointer is NULL");
  }
}


void ClockSciMsg::initAttributes ( )
{
  //clog << "ClockSciMsg::initAttributes" << endl;
  
  mpSpecializedSciMsgHeader = new Network_Clock_Header;
  if (NULL != mpSpecializedSciMsgHeader)
  {
    mpSpecializedSciMsgHeader->version = NETWORK_CLOCK_VERSION;
    mpSpecializedSciMsgHeader->type = 0x00;
    mpSpecializedSciMsgHeader->id = 0x0000;
    mpSpecializedSciMsgHeader->flags = 0x0000;
    mpSpecializedSciMsgHeader->reserved = 0x0000;
    mpSpecializedSciMsgHeader->tick_high = 0x00000000;
    mpSpecializedSciMsgHeader->tick_low = 0x00000000;
  }
}


ClockSciMsg::~ClockSciMsg ( )
{
  //clog << "~ClockSciMsg" << endl;
  
  if (NULL != mpSpecializedSciMsgHeader)
  {
    delete (mpSpecializedSciMsgHeader);
    mpSpecializedSciMsgHeader = NULL;
  }
  
  if (NULL != mpNetworkClockProcessor)
  {
    mpNetworkClockProcessor = NULL; 
  }
}


//  
// Methods
//  


// Other methods
//  


// public methods
//


SciMsg * ClockSciMsg::create ( ) const
{
  //clog << "ClockSciMsg::create" << endl;
  
  return new ClockSciMsg (mpNetworkClockProcessor);
}


bool ClockSciMsg::dispatchMsg ( )
{
  clog << "ClockSciMsg::dispatchMsg" << endl;
  bool bDispatch = false;
  
  if (NULL != mpNetworkClockProcessor)
  {
    // Create event to insert or remove
    //
    NetworkClockEvt evtToDispatch ( SciMsg::getSciMsgStationId(),
                                    getSpecializedSciMsgType(),
                                    getSpecializedSciMsgId()  );
      
    // According to the clock SCI msg type, dispatch clock SCI msg
    //
    if ( NETWORK_CLOCK_TYPE_REMOVE == getSpecializedSciMsgType() )
    {
      bDispatch = mpNetworkClockProcessor->removeEvt (getSpecializedSciMsgTick(), evtToDispatch);
    }
    else
    {
      if ( (0 < getSpecializedSciMsgType()) && (NETWORK_CLOCK_TYPE_NB > getSpecializedSciMsgType()) )
      {
        bDispatch = mpNetworkClockProcessor->insertEvt (getSpecializedSciMsgTick(), evtToDispatch);
      }
      else
      {
        cerr << "\tdo nothing" << endl; 
      }
    }
  }
  else
  {
    throw Error("ClockSciMsg::dispatchMsg", "Initialized network clock processor pointer is NULL");
  }
  
  return bDispatch;
}


// When receiving a SCI msg, specialized SCI msg header has to be extracted from received SCI msg data
// 
bool ClockSciMsg::identifySpecializedSciMsgHeader ( )
{
  //clog << "ClockSciMsg::identifySpecializedSciMsgHeader" << endl;
  bool bIdentifyHeader = false;

  if (NULL != SciMsg::getSciMsgData())
  {
    // Set specialized SCI msg header size
    //
    SciMsg::setSpecializedSciMsgHeaderSize((unsigned long)sizeof(struct Network_Clock_Header));
    
    // Get specialized SCI message header contents
    //
    if (SciMsg::getSciMsgDataLength() >= SciMsg::getSpecializedSciMsgHeaderSize()) // check that there are enough data to get the specialized SCI message header
    {
      if (NULL != mpSpecializedSciMsgHeader)
      {
        *mpSpecializedSciMsgHeader = *((Network_Clock_Header*)SciMsg::getSciMsgData());
        getSpecializedSciMsgHeader()->id = ntohs(getSpecializedSciMsgHeader()->id);
        getSpecializedSciMsgHeader()->flags = ntohs(getSpecializedSciMsgHeader()->flags);
        getSpecializedSciMsgHeader()->reserved = ntohs(getSpecializedSciMsgHeader()->reserved);
        getSpecializedSciMsgHeader()->tick_high = ntohl(getSpecializedSciMsgHeader()->tick_high);
        getSpecializedSciMsgHeader()->tick_low = ntohl(getSpecializedSciMsgHeader()->tick_low);    
        
        // Recompose tick value
        //
        uint64_t tempTickValue = (static_cast<uint64_t>(getSpecializedSciMsgHeader()->tick_high)<<32) +  static_cast<uint64_t>(getSpecializedSciMsgHeader()->tick_low);
                
        // Set specialized SCI msg attributes from specialized SCI msg header
        //
        bIdentifyHeader = setSpecializedSciMsgType (static_cast<Network_Clock_Type>(getSpecializedSciMsgHeader()->type));
        bIdentifyHeader &= setSpecializedSciMsgId (static_cast<Network_Clock_Id>(getSpecializedSciMsgHeader()->id));
        bIdentifyHeader &= setSpecializedSciMsgTick (static_cast<Network_Clock_Tick>(tempTickValue));
                         
        displaySpecializedSciMsgHeader();
      }
    }
    else
    {
      throw Error("ClockSciMsg::identifySpecializedSciMsgHeader", "Not enough data to get the specialized SCI message header");
    }
  }
  else
  {
     throw Error("ClockSciMsg::identifySpecializedSciMsgHeader", "SCI msg data pointer is NULL");
  } 
    
  return bIdentifyHeader;
}


// Check specialized SCI msg compatibility (check specialized SCI msg version)
//
bool ClockSciMsg::checkCompatibility ( ) const
{
  //clog << "ClockSciMsg::checkCompatibility" << endl;
  bool bCheck = false;
  
  if ( (NULL != getSpecializedSciMsgHeader()) && (NETWORK_CLOCK_VERSION == getSpecializedSciMsgHeader()->version) )
  {
    bCheck = SciMsg::checkCompatibility();
  }
  
  return bCheck;
}


void ClockSciMsg::displaySpecializedSciMsgHeader( ) const
{
  clog << "\tclock SCI msg header = " << endl;
  clog << "\t\tversion = 0x" << setfill('0') << setw(2) << uppercase << hex << (unsigned short int)getSpecializedSciMsgHeader()->version << endl;
  clog << "\t\ttype = ";
  displaySpecializedSciMsgType();
  clog << endl;
  clog << "\t\tid = 0x" << setfill('0') << setw(4) << uppercase << hex << getSpecializedSciMsgId() << endl;
  clog << "\t\tflags = 0x" << setfill('0') << setw(4) << uppercase << hex << getSpecializedSciMsgHeader()->flags << endl;
  clog << "\t\treserved = 0x" << setfill('0') << setw(4) << uppercase << hex << getSpecializedSciMsgHeader()->reserved << endl;
  clog << "\t\ttick = 0x" << setfill('0') << setw(8) << uppercase << hex << getSpecializedSciMsgHeader()->tick_high;
  clog << setfill('0') << setw(8) << uppercase << hex << getSpecializedSciMsgHeader()->tick_low << endl;
}


// private methods
//


void ClockSciMsg::displaySpecializedSciMsgType ( ) const
{
  switch (getSpecializedSciMsgType())
  {
    case 0:
      clog << "NETWORK_CLOCK_TYPE_NONE";
      break;
    case 1:
      clog << "NETWORK_CLOCK_TYPE_REMOVE";
      break;
    case 2:
      clog << "NETWORK_CLOCK_TYPE_STATION";
      break;
    case 3:
      clog << "NETWORK_CLOCK_TYPE_FUNCTION_CALL";
      break;
    case 4:
      clog << "NETWORK_CLOCK_TYPE_PHY";
      break;
    case 5:
      clog << "NETWORK_CLOCK_TYPE_SYSTEM";
      break;      
    default:      
      clog << "unknown";
      break;
  }
}


// protected methods
//


// Accessor methods
//  


// public attribute accessor methods
//  


// private attribute accessor methods
//  


Network_Clock_Type ClockSciMsg::getSpecializedSciMsgType ( ) const
{
  return mSpecializedSciMsgType;
}


bool ClockSciMsg::setSpecializedSciMsgType ( const Network_Clock_Type type )
{
  clog << "ClockSciMsg::setSpecializedSciMsgType" << endl;
  bool bSetType = true;
  
  mSpecializedSciMsgType = type;
  //clog << "\tspecialized SCI msg type = ";
  //displaySpecializedSciMsgType();
  //clog << endl;
  
  return bSetType;
}


Network_Clock_Id ClockSciMsg::getSpecializedSciMsgId ( ) const
{
  return mSpecializedSciMsgId;
}


bool ClockSciMsg::setSpecializedSciMsgId ( const Network_Clock_Id id )
{
  //clog << "ClockSciMsg::setSpecializedSciMsgId" << endl;
  bool bSetId = true;
  
  mSpecializedSciMsgId = id;
  //clog << "\tspecialized SCI msg id = " << dec << getSpecializedSciMsgId() << endl;
  
  return bSetId;
}


Network_Clock_Tick ClockSciMsg::getSpecializedSciMsgTick ( ) const
{
  return mSpecializedSciMsgTick;
}


bool ClockSciMsg::setSpecializedSciMsgTick ( const Network_Clock_Tick tick )
{
  //clog << "ClockSciMsg::setSpecializedSciMsgTick" << endl;
  bool bSetTick = true;
  
  mSpecializedSciMsgTick = tick;
  //clog << "\tspecialized SCI msg tick = " << getSpecializedSciMsgTick() << endl;
  
  return bSetTick;
}


void * ClockSciMsg::returnSpecializedSciMsgHeader ( ) const
{
  return (void*)mpSpecializedSciMsgHeader;
}


Network_Clock_Header * ClockSciMsg::getSpecializedSciMsgHeader ( ) const
{
  return mpSpecializedSciMsgHeader;
}


bool ClockSciMsg::setSpecializedSciMsgHeader ( const Network_Clock_Header * p_specialized_sci_msg_header )
{
  //clog << "ClockSciMsg::setSpecializedSciMsgHeader" << endl;
  bool bSetHeader = false;
  
  if ( (NULL != p_specialized_sci_msg_header) && (NULL != mpSpecializedSciMsgHeader) )
  {
    *mpSpecializedSciMsgHeader = *p_specialized_sci_msg_header;
    
    // Recompose tick value
    //
    uint64_t tempTickValue = (static_cast<uint64_t>(getSpecializedSciMsgHeader()->tick_high)<<32) +  static_cast<uint64_t>(getSpecializedSciMsgHeader()->tick_low);
                
    // Set specialized SCI msg attributes from specialized SCI msg header
    //
    bSetHeader = setSpecializedSciMsgType (static_cast<Network_Clock_Type>(getSpecializedSciMsgHeader()->type));
    bSetHeader &= setSpecializedSciMsgId (static_cast<Network_Clock_Id>(getSpecializedSciMsgHeader()->id));
    bSetHeader &= setSpecializedSciMsgTick (static_cast<Network_Clock_Tick>(tempTickValue));

    displaySpecializedSciMsgHeader();
  }
  else
  {
    throw Error("ClockSciMsg::setSpecializedSciMsgHeader", "Received and/or initialized network clock header pointer is NULL");
  }
   
  return bSetHeader;
}

// protected attribute accessor methods
//