Download this file

PDU.cc    123 lines (99 with data), 3.5 kB

  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
//
// Copyright Š 2014 PRISTINE Consortium (http://ict-pristine.eu)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
/**
* @file PDU.cc
* @author Marcel Marek (imarek@fit.vutbr.cz)
* @date Apr 30, 2014
* @brief
* @detail
*/
#include <PDU.h>
Register_Class(PDU);
void PDU::addUserData(const unsigned char* userData, unsigned int size, bool *fragment){
if(userData_arraysize == 0){
userData_var= new unsigned char[size];
memcpy(userData_var, userData, size);
*fragment = false;
}else{
if(*fragment){
// if(pduLen_var - PDU_HEADER_LEN == userData_arraysize){
//allocate new space
unsigned char * tmp = new unsigned char[userData_arraysize + SDU_FRAG_LEN];
//put size of the exising segment/SDU in the PDU
tmp[0] = userData_arraysize & 0xFF;
tmp[1] = (userData_arraysize << 8) & 0xFF;
//update size of the userData array
userData_arraysize += SDU_FRAG_LEN;
//copy existing data
memcpy(&tmp[SDU_FRAG_LEN], userData_var, userData_arraysize);
delete [] userData_var;
//assign pointer
userData_var = tmp;
*fragment = true;
}
//allocate new space
unsigned char * tmp = new unsigned char [size + this->userData_arraysize + SDU_FRAG_LEN];
//put size of the exising segment/SDU in the PDU
tmp[this->userData_arraysize] = size & 0xFF;
tmp[this->userData_arraysize +1 ] = (size << 8) & 0xFF;
this->userData_arraysize += SDU_FRAG_LEN;
//copy existing data
memcpy(tmp, this->userData_var, this->userData_arraysize);
//copy new data
memcpy(&tmp[this->userData_arraysize], userData, size);
delete [] this->userData_var;
this->userData_var = tmp;
}
this->userData_arraysize += size;
pduLen_var = PDU_HEADER_LEN + userData_arraysize;
}
/*
* @param sduSeqNum is unused for the moment because its usage is not clear to me (yet)
*/
void PDU::putDelimitFlags(int delimitFlags, bool fragment, int sduSeqNum){
//TODO B2 Incorporate size of the delimiter flag field into process of delimiting SDUs/PDUs
if(sduSeqNum != -1){
//SDU Sequence Number present
//TODO B2 add support for SDU Seq Number
}
flags_var |= 0x08 & (sduSeqNum == -1) ? 0x00: 0xFF;
flags_var |= delimitFlags;
unsigned char * tmp = new unsigned char[userData_arraysize + SDU_DELIMITER_FLAG_LEN];
//put size of the exising segment/SDU in the PDU
tmp[0] = 0x08 & (sduSeqNum == -1) ? 0x00: 0xFF;
tmp[0] |= delimitFlags;
//update size of the userData array
userData_arraysize += SDU_DELIMITER_FLAG_LEN;
//copy existing data
memcpy(&tmp[SDU_DELIMITER_FLAG_LEN], userData_var, userData_arraysize);
delete [] userData_var;
//assign pointer
userData_var = tmp;
}
unsigned int getHeaderSize(){
return PDU_HEADER_LEN;
}
//PDU::PDU()
//{
// // TODO Auto-generated constructor stub
//
//}
//
//PDU::~PDU()
//{
// // TODO Auto-generated destructor stub
//}