summaryrefslogtreecommitdiff
path: root/maximus/sci/src/SciMsg.cpp
blob: 7da94a0f7ffd353b1319ac17d752e62098b6c8f1 (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
/************************************************************************
                        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 <iomanip> // for 'std::setfill' and 'std::setw'
#include <iostream> // for 'cout', 'cerr' and 'clog'
using namespace std;


// Constructors/Destructors
//  


SciMsg::SciMsg ( ):
mSciMsgStatus(MAXIMUS_SCI_MSG_STATUS_NONE),
mSciMsgFlag(SCI_MSG_FLAG_RESP),
mSciMsgType(SCI_MSG_TYPE_NONE),
mpSciMsgHeader(NULL),
mSciMsgDataLength(0),
mpSciMsgData(NULL),
mSpecializedSciMsgHeaderSize(0),
mSpecializedSciMsgDataLength(0),
mpSpecializedSciMsgData(NULL)
{
  clog << "SciMsg()" << endl;
  
  initAttributes();
}


void SciMsg::initAttributes ( )
{
  clog << "SciMsg::initAttributes" << endl;
  
  mpSciMsgHeader = new Sci_Msg_Header;
  if (NULL != mpSciMsgHeader)
  {
    mpSciMsgHeader->magic_id = 0;
    mpSciMsgHeader->version = SCI_MSG_VERSION;
    mpSciMsgHeader->type = 0;
    mpSciMsgHeader->length = 0;
    mpSciMsgHeader->station_id = 0;
    mpSciMsgHeader->msg_id = 0;
    mpSciMsgHeader->netclock_high = 0;
    mpSciMsgHeader->netclock_low = 0;
    mpSciMsgHeader->flags = 0;
    mpSciMsgHeader->reserved = 0;    
  }
  else
  {
    throw Error("SciMsg::initAttributes", "Initialized Sci_Msg_Header pointer is NULL");
  }
}


SciMsg::SciMsg ( const SciMsg & sci_msg )
{
  clog << "SciMsg(SciMsg&)" << endl;

  initAttributes();
  
  mSciMsgType = sci_msg.getSciMsgType();
  mSciMsgFlag = sci_msg.getSciMsgFlag();
  mSciMsgStatus = sci_msg.getSciMsgStatus();
  mpSciMsgHeader = 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 * sci_msg )
{
  clog << "SciMsg(SciMsg*)" << endl;

  initAttributes();
  
  if (NULL != sci_msg)
  {
    mSciMsgType = sci_msg->getSciMsgType();
    mSciMsgFlag = sci_msg->getSciMsgFlag();
    mSciMsgStatus = sci_msg->getSciMsgStatus();
    mpSciMsgHeader = sci_msg->getSciMsgHeader();
    mSciMsgDataLength = sci_msg->getSciMsgDataLength();
    mpSciMsgData = sci_msg->getSciMsgData();
    mSpecializedSciMsgHeaderSize = sci_msg->getSpecializedSciMsgHeaderSize();
    mSpecializedSciMsgDataLength = sci_msg->getSpecializedSciMsgDataLength();
    mpSpecializedSciMsgData = sci_msg->getSpecializedSciMsgData();
  }
  else
  {
    throw Error("SciMsg(SciMsg*)", "Received SciMsg pointer is NULL");
  }
}


SciMsg::~SciMsg ( )
{
  clog << "~SciMsg" << endl;
  
  if (NULL != mpSciMsgHeader)
  {
    delete (mpSciMsgHeader);
    mpSciMsgHeader = NULL;
  }
  if (NULL != mpSciMsgData)
  {
    delete [] mpSciMsgData;
    mpSciMsgData = NULL;
  }
  if (NULL != mpSpecializedSciMsgData)
  {
    delete [] mpSpecializedSciMsgData;
    mpSpecializedSciMsgData = NULL;
  }
}


//  
// Methods
//  


// Other methods
// 


// public methods
//


bool SciMsg::identifySpecializedSciMsgData ( const unsigned long data_length, const unsigned char * p_data )
{
  clog << "SciMsg::identifySpecializedSciMsgData" << endl;
  bool bIdentifyData = true;
  
  if (NULL != p_data)
  {
    // Check data lengths coherence
    //
    if (data_length == (SciMsg::getSciMsgHeader()->length))
    {
      // Create temporary variables because input variables are defined as constant
      //
      unsigned long tempDataLength = data_length;
      unsigned char * pTempData = new unsigned char [tempDataLength];
      if (NULL != pTempData)
      {
        for (unsigned long i=0; i<tempDataLength; i++)
        {
          *(pTempData+i) = *(p_data+i);
        }
        bIdentifyData = removeData (getSpecializedSciMsgHeaderSize(), tempDataLength, &pTempData);
        if (0 != bIdentifyData)
        {
          bIdentifyData &= setSpecializedSciMsgDataLength (tempDataLength);
          bIdentifyData &= setSpecializedSciMsgData (pTempData);
        }
        
        delete [] pTempData;
        pTempData = NULL;
      }
      else
      {
        throw Error("SciMsg::identifySpecializedSciMsgData", "Initialized data pointer is NULL");
      }
    }
    else
    {
      throw Error("SciMsg::identifySpecializedSciMsgData", "Incoherent data lengths between SCI message header and input data length");
    }
  }
  else
  {
    throw Error("SciMsg::identifySpecializedSciMsgData", "Received data pointer is NULL");
  }
      
  return bIdentifyData;
}


// private methods
//


void SciMsg::displaySciMsgHeader ( ) const
{
  clog << "SciMsg::displaySciMsgHeader" << endl;
  
  clog << "\tSCI msg header = " << endl;
  clog << hex;
  clog << "\t\tmagic_id = 0x" << setfill('0') << setw(8) << uppercase << getSciMsgHeader()->magic_id << endl;
  clog << "\t\tlength = 0x" << setfill('0') << setw(2) << uppercase << getSciMsgHeader()->length << endl;
  clog << "\t\ttype = 0x" << setfill('0') << setw(2) << uppercase << getSciMsgHeader()->type << endl;
  clog << "\t\tversion = 0x" << setfill('0') << setw(4) << uppercase << getSciMsgHeader()->version << endl;
  clog << "\t\tmsg_id = 0x" << setfill('0') << setw(4) << uppercase << getSciMsgHeader()->msg_id << endl;
  clog << "\t\tstation_id = 0x" << setfill('0') << setw(4) << uppercase << getSciMsgHeader()->station_id << endl;
  clog << "\t\tnetclock_high = 0x" << setfill('0') << setw(8) << uppercase << getSciMsgHeader()->netclock_high << endl;
  clog << "\t\tnetclock_low = 0x" << setfill('0') << setw(8) << uppercase << getSciMsgHeader()->netclock_low << endl;
  clog << "\t\treserved = 0x" << setfill('0') << setw(4) << uppercase << getSciMsgHeader()->reserved << endl;
  clog << "\t\tflags = 0x" << setfill('0') << setw(4) << uppercase << getSciMsgHeader()->flags << endl;
  clog << dec;
}


void SciMsg::displaySciMsgData ( ) const
{
  clog << "SciMsg::displaySciMsgData" << endl;
  
  clog << "\tSCI msg data = ";
  if (NULL != getSciMsgData())
  {
    for (unsigned long i=0; i<getSciMsgDataLength(); i++)
    {
      clog << *(getSciMsgData()+i);
    }
  }
  else
  {
    clog << "NULL";
  }
  clog << endl;
}


void SciMsg::displaySpecializedSciMsgData ( ) const
{
  clog << "SciMsg::displaySpecializedSciMsgData" << endl;
  
  clog << "\tspecialized SCI msg data = ";
  if (NULL != getSpecializedSciMsgData())
  {
    for (unsigned long i=0; i<getSpecializedSciMsgDataLength(); i++)
    {
      clog << *(getSpecializedSciMsgData()+i);
    }
  }
  else
  {
    clog << "NULL";
  }
  clog << endl;
}


// protected methods
//


bool SciMsg::removeData ( const unsigned long data_length_to_remove, unsigned long & data_length, unsigned char ** pp_data )
{
  clog << "SciMsg::removeData" << endl;
  bool bRemoveData = false;
  
  // Keep data without header
  //
  if (  (data_length >= data_length_to_remove) // check that there are enough data to remove the message header
        && (NULL != *pp_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);
    }
    for (unsigned long i=0; i<(data_length-data_length_to_remove); i++)
    {
      *((*pp_data)+i) = *(remainingData+i);
    }
    
    data_length -= data_length_to_remove; // update data length
    
    delete [] remainingData;
    remainingData = NULL;
    bRemoveData = true;
  }
  
  return bRemoveData; 
}


// Accessor methods
//  


// public attribute accessor methods
//  


// private attribute accessor methods
//  


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


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


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


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


bool SciMsg::setSciMsgHeader ( const Sci_Msg_Header * p_sci_msg_header )
{
  clog << "SciMsg::setSciMsgHeader" << endl;
  bool bSetHeader = false;
  
  if (NULL != p_sci_msg_header)
  {
    mpSciMsgHeader = new Sci_Msg_Header;
    if (NULL != mpSciMsgHeader)
    {
     * mpSciMsgHeader = * p_sci_msg_header;
     displaySciMsgHeader ();
     bSetHeader = true;
    }
    else
    {
      throw Error("SciMsg::setSciMsgHeader", "Initialized Sci_Msg_Header pointer is NULL");
    }
  }
  else
  {
    throw Error("SciMsg::setSciMsgHeader", "Received Sci_Msg_Header pointer is NULL");
  }
  
  return bSetHeader;
}


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


bool SciMsg::setSciMsgData ( const unsigned char * p_data )
{
  clog << "SciMsg::setSciMsgData" << endl;
  bool bSetData = false;

  if (NULL != p_data)
  {
    if (getSciMsgDataLength() == (unsigned long)getSciMsgHeader()->length) // check that data length of SCI message header is coherent with set data length
    {
      mpSciMsgData = new unsigned char [getSciMsgDataLength()];
      if (NULL != mpSciMsgData)
      {
        for (unsigned long i=0; i<getSciMsgDataLength(); i++)
        {
          *(mpSciMsgData+i) = *(p_data+i);
        }
        displaySciMsgData ();
        bSetData = true;
      }
      else
      {
        throw Error("SciMsg::setSciMsgData", "Initialized SCI msg data pointer is NULL");
      }
    }   
    else
    {
      throw Error("SciMsg::setSciMsgData", "Incoherent data lengths between SCI message header and set data length");
    }
  }
  else
  {
    throw Error("SciMsg::setSciMsgData", "Received data pointer is NULL");
  }
    
  return bSetData;
}


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


bool SciMsg::setSciMsgDataLength ( const unsigned long data_length )
{
  clog << "SciMsg::setSciMsgDataLength" << endl;
    
  mSciMsgDataLength = data_length;
  clog << "\tSCI msg data length = " << mSciMsgDataLength << endl;
  
  return true;
}


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


// protected attribute accessor methods
//  


bool SciMsg::setSpecializedSciMsgHeaderSize ( const unsigned long header_size )
{
  clog << "SciMsg::setSpecializedSciMsgHeaderSize" << endl;
  
  mSpecializedSciMsgHeaderSize = header_size;
  clog << dec << "\tspecialized SCI msg header size = " << mSpecializedSciMsgHeaderSize << endl;
  
  return true;
}


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

bool SciMsg::setSpecializedSciMsgDataLength ( const unsigned long data_length )
{
  clog << "SciMsg::setSpecializedSciMsgDataLength" << endl;
  
  mSpecializedSciMsgDataLength = data_length;
  clog << dec << "\tspecialized SCI msg data length = " << mSpecializedSciMsgDataLength << endl;
  
  return true;
}


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


bool SciMsg::setSpecializedSciMsgData ( const unsigned char * p_data )
{
  clog << "SciMsg::setSpecializedSciMsgData" << endl;
  bool bSetData = false;
   
  mpSpecializedSciMsgData = new unsigned char [getSpecializedSciMsgDataLength()];
  if (NULL != mpSpecializedSciMsgData)
  {
    for (unsigned long i=0; i<getSpecializedSciMsgDataLength(); i++)
    {
      *(mpSpecializedSciMsgData+i) = *(p_data+i);
    }
    displaySpecializedSciMsgData ();
    bSetData = true;
  }
  else
  {
    throw Error("SciMsg::setSciMsgData", "Received data pointer is NULL");
  }
  
  return bSetData;
}


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