summaryrefslogtreecommitdiff
path: root/cesar/maximus/sci/src/SciMsg.cpp
blob: 1ce0caeb36d0b705b308b286a832f8b4d34c310b (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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/************************************************************************
                        SciMsg.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/sci/src/SciMsg.cpp
**************************************************************************/

#include "SciMsg.h"

#include "Station.h"

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

#include <sstream> // for 'ostringstream'
#include <iomanip> // for 'setfill()' and 'setw()'
#include <iostream> // for 'cout', 'cerr' and 'clog'
using namespace std;


// Constructors/Destructors
//  


Sci_Msg_Id SciMsg::mSciMsgId = 0; 


SciMsg::SciMsg ( ):
mSciMsgStatus(MAXIMUS_SCI_MSG_STATUS_NONE),
mSciMsgType(SCI_MSG_TYPE_NONE),
mSciMsgStationId(0),
mSciMsgFlag(SCI_MSG_FLAG_NONE),
mSciMsgDataLength(0),
mpSciMsgData(NULL),
mSpecializedSciMsgHeaderSize(0),
mSpecializedSciMsgDataLength(0),
mpSpecializedSciMsgData(NULL)
{
  logFunction();
  
  initAttributes();
}


void SciMsg::initAttributes ( )
{
  logFunction();

  mSciMsgHeader.magic_id = 0x00000000;
  mSciMsgHeader.version = 0x00;
  mSciMsgHeader.type = 0x00;
  mSciMsgHeader.length = 0x0000;
  mSciMsgHeader.station_id = 0x0000;
  mSciMsgHeader.msg_id = 0x0000;
  mSciMsgHeader.netclock_high = 0x00000000;
  mSciMsgHeader.netclock_low = 0x00000000;
  mSciMsgHeader.flags = 0x0000;
  mSciMsgHeader.reserved = 0x0000;
}


SciMsg::SciMsg ( const SciMsg & sci_msg )
{
  logFunction();

  initAttributes();
  
  mSciMsgStatus = sci_msg.getSciMsgStatus();
  
  mSciMsgType = sci_msg.getSciMsgType();
  mSciMsgStationId = sci_msg.getSciMsgStationId();
  mSciMsgFlag = sci_msg.getSciMsgFlag();
  
  setSciMsgHeader(*(sci_msg.getSciMsgHeader()));
  mSciMsgDataLength = sci_msg.getSciMsgDataLength();
  mpSciMsgData = sci_msg.getSciMsgData();
  
  mSpecializedSciMsgHeaderSize = sci_msg.getSpecializedSciMsgHeaderSize();
  mSpecializedSciMsgDataLength = sci_msg.getSpecializedSciMsgDataLength();
  mpSpecializedSciMsgData = sci_msg.getSpecializedSciMsgData();
}


SciMsg::SciMsg ( const SciMsg * p_sci_msg )
{
  logFunction();

  initAttributes();
  
  if (NULL != p_sci_msg)
  {
    mSciMsgStatus = p_sci_msg->getSciMsgStatus();
    
    mSciMsgType = p_sci_msg->getSciMsgType();
    mSciMsgStationId = p_sci_msg->getSciMsgStationId();
    mSciMsgFlag = p_sci_msg->getSciMsgFlag();

    setSciMsgHeader(*(p_sci_msg->getSciMsgHeader()));
    mSciMsgDataLength = p_sci_msg->getSciMsgDataLength();
    mpSciMsgData = p_sci_msg->getSciMsgData();
    
    mSpecializedSciMsgHeaderSize = p_sci_msg->getSpecializedSciMsgHeaderSize();
    mSpecializedSciMsgDataLength = p_sci_msg->getSpecializedSciMsgDataLength();
    mpSpecializedSciMsgData = p_sci_msg->getSpecializedSciMsgData();
  }
  else
  {
    errno = EINVAL;
    throw Error(__PRETTY_FUNCTION__, "SCI message pointer is NULL", errno);
  }
}


SciMsg::~SciMsg ( )
{
  logFunction();

  if (NULL != mpSciMsgData)
  {
    delete [] mpSciMsgData;
    mpSciMsgData = NULL;
  }
  if (NULL != mpSpecializedSciMsgData)
  {
    delete [] mpSpecializedSciMsgData;
    mpSpecializedSciMsgData = NULL;
  }
}


//  
// Methods
//  


// Other methods
// 


// public methods
//


// Check SCI msg compatibility (check SCI msg version)
//
bool SciMsg::checkCompatibility ( ) const
{
  logFunction();

  return (getDefinedSciMsgVersion() == getSciMsgHeader()->version);
}


// Check SCI msg validity (check SCI msg magic id and SCI msg id)
//
bool SciMsg::checkValidity ( ) const
{
  logFunction();
  bool bCheck = false;

  if ( (getDefinedSciMsgMagicId() == getSciMsgHeader()->magic_id)
        && (SCI_MSG_ID_STATION <= getSciMsgHeader()->msg_id) )
  {
    bCheck = true; 
  }

  return bCheck;
}


// Remove specialized SCI message header from specialized SCI msg data
// Then, set specialized SCI message data length, and specialized SCI msg data
//
bool SciMsg::identifySpecializedSciMsgData ( )
{
  logFunction();
  bool bIdentifyData = false;

  if (NULL != getSciMsgData())
  {
    // Check data lengths coherence
    //
    if (getSciMsgDataLength() == getSciMsgHeader()->length)
    {
      // Create temporary variables
      //
      unsigned long tempDataLength = getSciMsgDataLength();
      unsigned char * pTempData = new unsigned char [tempDataLength];

      if (NULL != pTempData)
      {
        for (unsigned long i=0; i<tempDataLength; i++)
        {
          *(pTempData+i) = *(getSciMsgData()+i);
        }

        bIdentifyData = removeData (getSpecializedSciMsgHeaderSize(), tempDataLength, &pTempData);

        if (0 != bIdentifyData)
        {
          bIdentifyData &= setSpecializedSciMsgDataLength (tempDataLength);
          bIdentifyData &= setSpecializedSciMsgData (pTempData);
        }

        // Free temporary allocated memory
        //
        delete [] pTempData;
        pTempData = NULL;
      }
      else
      {
        throw Error(__PRETTY_FUNCTION__, "Initialized data pointer is NULL");
      }
    }
    else
    {
      errno = EPROTO;
      throw Error(__PRETTY_FUNCTION__, "Incoherent data lengths between SCI message header and set data length", errno);
    }
  }
  else
  {
    throw Error(__PRETTY_FUNCTION__, "Received data pointer is NULL");
  }

  return bIdentifyData;
}


void SciMsg::displaySciMsgHeader ( ) const
{
#if CONFIG_LOG
  logFunction();

  clog << logger(LOG_INFO) << "SCI msg header = " << endl;
  clog << logger(LOG_INFO) << "\tmagic_id = 0x" << setfill('0') << setw(8) << uppercase << hex << getSciMsgHeader()->magic_id << endl;
  clog << logger(LOG_INFO) << "\tversion = 0x" << setfill('0') << setw(2) << uppercase << hex << static_cast<unsigned short int>(getSciMsgHeader()->version) << endl;
  displaySciMsgType();
  clog << logger(LOG_INFO) << "\tlength = " << dec << getSciMsgHeader()->length << " bytes (0x" << setfill('0') << setw(4) << uppercase << hex << getSciMsgHeader()->length << ")"  << endl;
  clog << logger(LOG_INFO) << "\tstation_id = " << dec <<  getSciMsgHeader()->station_id << " (0x" << setfill('0') << setw(4) << uppercase << hex << getSciMsgHeader()->station_id << ")" << endl;
  clog << logger(LOG_INFO) << "\tmsg_id = 0x" << setfill('0') << setw(4) << uppercase << hex << getSciMsgHeader()->msg_id << endl;
  clog << logger(LOG_INFO) << "\tnetclock = 0x" << setfill('0') << setw(8) << uppercase << hex << getSciMsgHeader()->netclock_high \
       << setfill('0') << setw(8) << uppercase << hex << getSciMsgHeader()->netclock_low << endl;
  displaySciMsgFlag();
  clog << logger(LOG_INFO) << "\treserved = 0x" << setfill('0') << setw(4) << uppercase << hex << getSciMsgHeader()->reserved << dec << endl;
#endif /* CONFIG_LOG */
}


void SciMsg::displaySciMsgData ( ) const
{
#if CONFIG_LOG
  logFunction();

  ostringstream oss;
  oss << "SCI msg data = ";
  if (NULL != getSciMsgData())
  {
    for (unsigned long i=0; i<getSciMsgDataLength(); i++)
    {
      if ( '\0' == *(getSciMsgData()+i) )
      {
        oss << "\\0"; 
      }
      else
      {
        oss << *(getSciMsgData()+i);
      }
    }
  }
  else
  {
    oss << "NULL!";
  }
  oss << endl;
  oss.flush();
  clog << logger(LOG_INFO) << oss.str();
#endif /* CONFIG_LOG */
}


void SciMsg::displaySpecializedSciMsgData ( ) const
{
#if CONFIG_LOG
  logFunction();

  ostringstream oss;
  oss << "specialized SCI msg data = ";
  if (NULL != getSpecializedSciMsgData())
  {
    for (unsigned long i=0; i<getSpecializedSciMsgDataLength(); i++)
    {
      if ( '\0' == *(getSpecializedSciMsgData()+i) )
      {
        oss << "\\0"; 
      }
      else
      {
        oss << *(getSpecializedSciMsgData()+i);
      }
    }
  }
  else
  {
    oss << "NULL!";
  }
  oss << endl;
  oss.flush();
  clog << logger(LOG_INFO) << oss.str();
#endif /* CONFIG_LOG */
}


uint32_t SciMsg::getDefinedSciMsgMagicId ( ) const
{
  logFunction();

  // Convert string to uint32_t
  //
  string stringMagicId(SCI_MSG_MAGIC);
  uint32_t magicId = (static_cast<uint8_t>(stringMagicId[0])<<24) + (static_cast<uint8_t>(stringMagicId[1])<<16) + (static_cast<uint8_t>(stringMagicId[2])<<8) + static_cast<uint8_t>(stringMagicId[3]);

  return magicId; 
}


uint8_t SciMsg::getDefinedSciMsgVersion ( ) const
{
  return SCI_MSG_VERSION;
}


void SciMsg::displaySciMsgType ( ) const
{
#if CONFIG_LOG
  logFunction();

  switch (getSciMsgHeader()->type)
  {
    case 0:
      clog << logger(LOG_INFO) << "\ttype = SCI_MSG_TYPE_NONE" << endl;
      break;
    case 1:
      clog << logger(LOG_INFO) << "\ttype = SCI_MSG_TYPE_NETWORK_CLOCK" << endl;
      break;
    case 2:
      clog << logger(LOG_INFO) << "\ttype = SCI_MSG_TYPE_FUNCTION_CALL" << endl;
      break;
    case 3:
      clog << logger(LOG_INFO) << "\ttype = SCI_MSG_TYPE_PHY" << endl;
      break;
    case 4:
      clog << logger(LOG_INFO) << "\ttype = SCI_MSG_TYPE_SYSTEM" << endl;
      break;
    case 5:
      clog << logger(LOG_INFO) << "\ttype = SCI_MSG_TYPE_ETHERNET" << endl;
      break;
    default:
      clog << logger(LOG_INFO) << "\ttype = unknown!" << endl;
      break;
  }
#endif /* CONFIG_LOG */
}


// private methods
//


void SciMsg::displaySciMsgFlag ( ) const
{
#if CONFIG_LOG
  logFunction();

  switch (getSciMsgHeader()->flags)
  {
    case 0:
      clog << logger(LOG_INFO) << "\tflags = SCI_MSG_FLAG_NONE" << endl;
      break;
    case 1:
      clog << logger(LOG_INFO) << "\tflags = SCI_MSG_FLAG_RESP" << endl;
      break;
    case 2:
      clog << logger(LOG_INFO) << "\tflags = SCI_MSG_FLAG_ACK" << endl;
      break;
    case 3:
      clog << logger(LOG_INFO) << "\tflags = SCI_MSG_FLAG_NEEDACK" << endl;
      break;
    case 4:
      clog << logger(LOG_INFO) << "\tflags = SCI_MSG_FLAG_SYNC" << endl;
      break;
    default:
      clog << logger(LOG_INFO) << "\tflags = unknown!" << endl;
      break;
  }
#endif /* CONFIG_LOG */
}


// protected methods
//


// Remove required data length from data, and update data length
//
bool SciMsg::removeData ( const unsigned long data_length_to_remove, unsigned long & data_length, unsigned char ** pp_data )
{
  logFunction();
  bool bRemoveData = false;

  if (  (data_length >= data_length_to_remove) // check that there are enough data to remove the required data length
        && (NULL != *pp_data) )
  {
    // Create temporary variable and copy data
    //
    unsigned char * remainingData = new unsigned char [data_length-data_length_to_remove];
    for (unsigned long i=data_length_to_remove; i<data_length; i++)
    {
      *(remainingData+i-data_length_to_remove) = *((*pp_data)+i);
    }

    // Update data length
    //
    data_length -= data_length_to_remove;

    // Fill data
    //
    for (unsigned long i=0; i<data_length; i++)
    {
      *((*pp_data)+i) = *(remainingData+i);
    }

    // Free temporary allocated memory
    //
    delete [] remainingData;
    remainingData = NULL;

    bRemoveData = true;
  }
  else
  {
    errno = ENODATA;
    throw Error(__PRETTY_FUNCTION__, "Not enough data", errno);
  }

  return bRemoveData; 
}


// Accessor methods
//  


// public attribute accessor methods
//  


// private attribute accessor methods
//  


Sci_Msg_Status SciMsg::getSciMsgStatus ( ) const
{
  return mSciMsgStatus;
}


bool SciMsg::setSciMsgStatus ( const Sci_Msg_Status status )
{
  mSciMsgStatus = status;

  return true;
}


Sci_Msg_Type SciMsg::getSciMsgType ( ) const
{
  return mSciMsgType;
}


bool SciMsg::setSciMsgType ( const Sci_Msg_Type type )
{
  mSciMsgType = type;
  
  return true;
}


Sci_Msg_Station_Id SciMsg::getSciMsgStationId ( ) const
{
  return mSciMsgStationId;
}


bool SciMsg::setSciMsgStationId ( const Sci_Msg_Station_Id station_id )
{
  mSciMsgStationId = station_id;
  
  return true;
}


Sci_Msg_Id SciMsg::getSciMsgId ( ) const
{
  return mSciMsgId;
}


bool SciMsg::incrementSciMsgId ( )
{
  logFunction();
  
  if ( SCI_MSG_ID_STATION > (mSciMsgId+1) )
  {
    mSciMsgId++;
  }
  else
  {
    mSciMsgId = 0x0001; 
  }
  
  return true;
}


Sci_Msg_Flag SciMsg::getSciMsgFlag ( ) const
{
  return mSciMsgFlag;
}


bool SciMsg::setSciMsgFlag ( const Sci_Msg_Flag flag )
{
  mSciMsgFlag = flag;
  
  return true;
}


Sci_Msg_Header * SciMsg::getSciMsgHeader ( ) const
{
  return (Sci_Msg_Header *)&mSciMsgHeader;
}


bool SciMsg::setSciMsgHeader ( const Sci_Msg_Header & sci_msg_header )
{
  logFunction();
  bool bSetHeader = false;

  mSciMsgHeader = sci_msg_header;

  // Set SCI msg attributes from SCI msg header
  //
  setSciMsgType (static_cast<Sci_Msg_Type>(getSciMsgHeader()->type));
  setSciMsgStationId (static_cast<Sci_Msg_Station_Id>(getSciMsgHeader()->station_id));
  setSciMsgFlag (static_cast<Sci_Msg_Flag>(getSciMsgHeader()->flags));
 
  displaySciMsgHeader();
  bSetHeader = true;

  return bSetHeader;
}


unsigned long SciMsg::getSciMsgDataLength ( ) const
{
  return mSciMsgDataLength;
}


bool SciMsg::setSciMsgDataLength ( const unsigned long data_length )
{
  logFunction();
  
  if ( SCI_MSG_MAX_SIZE >= data_length )
  {
    mSciMsgDataLength = data_length;
  }
  else
  {
    errno = EMSGSIZE;
    throw Error(__PRETTY_FUNCTION__, "Data length exceeds max size", errno);
  }
  
  return true;
}


unsigned char * SciMsg::getSciMsgData ( ) const
{
  return mpSciMsgData;
}


bool SciMsg::setSciMsgData ( const unsigned char * p_data )
{
  logFunction();
  bool bSetData = false;

  if (NULL != mpSciMsgData)
  {
    delete [] mpSciMsgData;
    mpSciMsgData = NULL;
  }

  if (NULL != p_data)
  {
    mpSciMsgData = new unsigned char [getSciMsgDataLength()];
    if (NULL != mpSciMsgData)
    {
      for (unsigned int i=0; i<getSciMsgDataLength(); i++)
      {
        *(mpSciMsgData+i) = *(p_data+i);
      }

      displaySciMsgData();
      bSetData = true;
    }
    else
    {
      throw Error(__PRETTY_FUNCTION__, "SCI message data pointer is NULL");
    }
  }
  else
  {
    errno = EINVAL;
    throw Error(__PRETTY_FUNCTION__, "Received data pointer is NULL", errno);
  }

  return bSetData;
}


// protected attribute accessor methods
//  


unsigned long SciMsg::getSpecializedSciMsgHeaderSize ( ) const
{
  return mSpecializedSciMsgHeaderSize;  
} 
  

bool SciMsg::setSpecializedSciMsgHeaderSize ( const unsigned long header_size )
{
  mSpecializedSciMsgHeaderSize = header_size;
  
  return true;
}


unsigned long SciMsg::getSpecializedSciMsgDataLength ( ) const
{
  return mSpecializedSciMsgDataLength;
}


bool SciMsg::setSpecializedSciMsgDataLength ( const unsigned long data_length )
{
  mSpecializedSciMsgDataLength = data_length;
  
  return true;
}


unsigned char * SciMsg::getSpecializedSciMsgData ( ) const
{
  return mpSpecializedSciMsgData;
}


bool SciMsg::setSpecializedSciMsgData ( const unsigned char * p_data )
{
  logFunction();

  if (NULL != mpSpecializedSciMsgData)
  {
    delete [] mpSpecializedSciMsgData;
    mpSpecializedSciMsgData = NULL;
  }

  if (NULL != p_data)
  {
    mpSpecializedSciMsgData = new unsigned char [getSpecializedSciMsgDataLength()];
    if (NULL != mpSpecializedSciMsgData)
    {
      for (unsigned long i=0; i<getSpecializedSciMsgDataLength(); i++)
      {
        *(mpSpecializedSciMsgData+i) = *(p_data+i);
      }
    }
  }

  return true;
}