Tidied some trace logs to assure all have a category (bit before a tab character...
[opal/cbnco.git] / src / h224 / q922.cxx
blob0b0bf7218a436db57f2340851c79dabdfb0bbb9e
1 /*
2 * q922.h
4 * Q.922 PDU implementation for the OpenH323 Project.
6 * Copyright (c) 2006 Network for Educational Technology, ETH Zurich.
7 * Written by Hannes Friederich.
9 * The contents of this file are subject to the Mozilla Public License
10 * Version 1.0 (the "License"); you may not use this file except in
11 * compliance with the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS"
15 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
16 * the License for the specific language governing rights and limitations
17 * under the License.
19 * Contributor(s): ______________________________________.
21 * $Log$
22 * Revision 1.6 2007/04/02 05:51:33 rjongbloed
23 * Tidied some trace logs to assure all have a category (bit before a tab character) set.
25 * Revision 1.5 2007/03/12 23:19:01 csoutheren
26 * Add ability to remove H.224
28 * Revision 1.4 2006/05/03 17:45:51 hfriederich
29 * reverting incorrect byte orderings in fcs from previous patch
31 * Revision 1.3 2006/05/01 10:29:50 csoutheren
32 * Added pragams for gcc < 4
34 * Revision 1.2 2006/04/24 12:53:50 rjongbloed
35 * Port of H.224 Far End Camera Control to DevStudio/Windows
37 * Revision 1.1 2006/04/20 16:48:17 hfriederich
38 * Initial version of H.224/H.281 implementation.
42 #include <ptlib.h>
44 #ifdef __GNUC__
45 #pragma implementation "q922.h"
46 #endif
48 #include <opal/buildopts.h>
50 #if OPAL_H224
52 #include <h224/h323h224.h>
54 #include <h224/q922.h>
56 #define Q922_FCS_SIZE 2
58 #define Q922_OK 0x00
59 #define Q922_FLAG 0x7e
60 #define Q922_ERROR 0x7f
63 * FCS lookup table.
64 * Code based on implementation in RFC1549
66 static WORD fcstable[256] = {
67 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
68 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
69 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
70 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
71 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
72 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
73 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
74 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
75 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
76 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
77 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
78 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
79 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
80 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
81 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
82 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
83 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
84 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
85 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
86 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
87 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
88 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
89 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
90 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
91 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
92 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
93 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
94 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
95 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
96 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
97 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
98 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
99 };
101 Q922_Frame::Q922_Frame(PINDEX size)
102 : PBYTEArray(Q922_HEADER_SIZE + size)
104 informationFieldSize = size;
108 Q922_Frame::~Q922_Frame()
113 void Q922_Frame::SetInformationFieldSize(PINDEX size)
115 informationFieldSize = size;
116 SetMinSize(Q922_HEADER_SIZE+informationFieldSize);
120 BOOL Q922_Frame::Decode(const BYTE *data, PINDEX size)
122 // a valid frame must contain at least 2xFLAG, 3 octets Q922 header,
123 // 2 octets FCS and at least 1 octet information
124 if(size < 2+3+2+1)
125 return FALSE;
127 PINDEX octetIndex = 0;
128 BYTE bitIndex = 7;
129 BYTE onesCounter = 0;
131 if(!FindFlagEnd(data, size, octetIndex, bitIndex))
132 return FALSE;
134 BYTE firstOctet;
135 BYTE secondOctet;
137 // read the two first octets
138 if(octetIndex >= size || DecodeByte(data, &firstOctet, octetIndex, bitIndex, onesCounter) != Q922_OK)
139 return FALSE;
141 if(octetIndex >= size || DecodeByte(data, &secondOctet, octetIndex, bitIndex, onesCounter) != Q922_OK)
142 return FALSE;
144 PINDEX arrayIndex = 0;
145 while(octetIndex < size) {
147 BYTE decodedByte;
148 BYTE result = DecodeByte(data, &decodedByte, octetIndex, bitIndex, onesCounter);
150 if(result == Q922_ERROR)
151 return FALSE;
153 if(result == Q922_FLAG) {
155 // Found end flag
156 // FCS is contained in firstOctet and secondOctet.
157 WORD fcs = (secondOctet << 8) | firstOctet;
159 // Calculate FCS from data to check
160 WORD calculatedFCS = CalculateFCS((const BYTE *)theArray, arrayIndex);
162 if (fcs != calculatedFCS) {
163 PTRACE(2, "Q.922\tFrame has incorrect checksum");
164 return FALSE;
167 if(arrayIndex > Q922_HEADER_SIZE) {
168 SetInformationFieldSize(arrayIndex - Q922_HEADER_SIZE);
169 return TRUE;
172 return FALSE;
175 theArray[arrayIndex] = firstOctet;
176 arrayIndex++;
178 firstOctet = secondOctet;
179 secondOctet = decodedByte;
181 // Q922-frames must not exceed an information field size of 260 octets
182 if(arrayIndex >= 260+Q922_HEADER_SIZE)
183 return FALSE;
186 return FALSE;
189 PINDEX Q922_Frame::GetEncodedSize() const
191 if(informationFieldSize == 0) {
192 return 0; // cannot encode
195 // dataSize is High Order Address, Low Order Address, Control, Information and
196 // Frame Check Sequence (FCS, 2 Bytes)
197 PINDEX dataSize = Q922_HEADER_SIZE + informationFieldSize + Q922_FCS_SIZE;
199 // Due to possible need for bit insertion, the size may grow.
200 // For simplicity, we assume twice the data size, which will certainly be enough.
201 // So, encoded size is 3*FLAG + 2*dataSize + 3*FLAG;
202 return 3+2*dataSize+3;
205 BOOL Q922_Frame::Encode(BYTE *buffer, PINDEX & size) const
207 BYTE bitIndex = 7;
208 return Encode(buffer, size, bitIndex);
211 BOOL Q922_Frame::Encode(BYTE *buffer, PINDEX & size, BYTE & theBitIndex) const
213 if(informationFieldSize == 0) {
214 return FALSE;
217 PINDEX octetIndex = 0;
218 BYTE bitIndex = theBitIndex;
219 BYTE onesCounter = 0;
221 // storing three FLAG sequencs.
222 // since the FLAG sequences may be not byte-aligned, the first FLAG sequence is encoded
223 // into a dummy buffer and extracted from there
224 buffer[0] = 0;
225 BYTE dummy[3];
226 EncodeOctetNoEscape(Q922_FLAG, dummy, octetIndex, bitIndex);
227 EncodeOctetNoEscape(Q922_FLAG, dummy, octetIndex, bitIndex);
228 buffer[0] = dummy[1];
229 buffer[1] = dummy[1];
230 octetIndex = 1;
231 EncodeOctetNoEscape(Q922_FLAG, buffer, octetIndex, bitIndex);
232 EncodeOctetNoEscape(Q922_FLAG, buffer, octetIndex, bitIndex);
234 // calculating the FCS
235 PINDEX dataSize = GetInformationFieldSize() + Q922_HEADER_SIZE;
236 WORD fcs = CalculateFCS((const BYTE *)theArray, dataSize);
238 // Encoding the data byte-by-byte
239 PINDEX i;
240 PINDEX count = Q922_HEADER_SIZE + informationFieldSize;
241 for(i = 0; i < count; i++) {
242 EncodeOctet(theArray[i], buffer, octetIndex, bitIndex, onesCounter);
245 // Encoding the FCS
246 EncodeOctet((BYTE)fcs, buffer, octetIndex, bitIndex, onesCounter);
247 EncodeOctet((BYTE)(fcs >> 8), buffer, octetIndex, bitIndex, onesCounter);
249 // Appending three FLAG sequences to the buffer
250 // the buffer is not necessary byte aligned!
251 EncodeOctetNoEscape(Q922_FLAG, buffer, octetIndex, bitIndex);
252 EncodeOctetNoEscape(Q922_FLAG, buffer, octetIndex, bitIndex);
253 EncodeOctetNoEscape(Q922_FLAG, buffer, octetIndex, bitIndex);
255 // determining correct number of octets
256 if(bitIndex == 7) {
257 octetIndex--;
260 size = octetIndex;
261 theBitIndex = bitIndex;
263 return TRUE;
267 BOOL Q922_Frame::FindFlagEnd(const BYTE *buffer,
268 PINDEX bufferSize,
269 PINDEX & octetIndex, BYTE & bitIndex)
271 BYTE positionsCorrect = 0;
273 while(octetIndex < bufferSize) {
275 BYTE bit = DecodeBit(buffer, octetIndex, bitIndex);
277 switch(positionsCorrect) {
278 case 0:
279 if(bit == 0) {
280 positionsCorrect = 1;
282 break;
283 case 1:
284 case 2:
285 case 3:
286 case 4:
287 case 5:
288 case 6:
289 if(bit == 1) {
290 positionsCorrect++;
291 } else {
292 positionsCorrect = 1;
294 break;
295 case 7:
296 if(bit == 0) {
297 positionsCorrect = 0xff;
298 } else {
299 // got 0x7f, ABORT sequence
300 return FALSE;
302 break;
303 default:
304 return FALSE;
307 if(positionsCorrect == 0xff) {
308 break;
312 if(positionsCorrect != 0xff) {
313 return FALSE;
316 // First FLAG sequence found, bit index determined.
317 // now check for additinal FLAG sequences
318 BYTE octet = Q922_FLAG;
320 while(octet == Q922_FLAG && octetIndex < bufferSize) {
322 PINDEX startOctetIndex = octetIndex;
323 BYTE startBitIndex = bitIndex;
325 BYTE positionsCorrect = 0;
327 unsigned i;
328 for(i = 0; i < 8; i++) {
330 BYTE bit = DecodeBit(buffer, octetIndex, bitIndex);
332 switch(positionsCorrect) {
333 case 0:
334 if(bit == 1) {
335 positionsCorrect = 0xf0;
336 } else {
337 positionsCorrect++;
339 break;
340 case 1:
341 case 2:
342 case 3:
343 case 4:
344 case 5:
345 case 6:
346 if(bit == 0) {
347 positionsCorrect = 0xf0;
348 } else {
349 positionsCorrect++;
351 break;
352 case 7:
353 if(bit == 1) {
354 // 0x7f read
355 return FALSE;
357 break;
358 default:
359 return FALSE;
362 if(positionsCorrect == 0xf0) {
363 octetIndex = startOctetIndex;
364 bitIndex = startBitIndex;
365 return TRUE;
370 return FALSE;
373 BYTE Q922_Frame::DecodeByte(const BYTE *buffer,
374 BYTE *destination,
375 PINDEX & octetIndex,
376 BYTE & bitIndex,
377 BYTE & onesCounter)
379 // decoded byte is copied to destination.
380 // returns Q922_OK if decoding succesful,
381 // returns Q922_FLAG if ending FLAG detected,
382 // returns Q922_ERROR if there was an error
384 BYTE decodedByte = 0x00;
386 PINDEX i;
387 for(i = 0; i < 8; i++) {
389 BYTE bit = DecodeBit(buffer, octetIndex, bitIndex);
391 if(bit) {
392 onesCounter++;
394 if(onesCounter == 6) {
396 // Either FLAG or ERROR
397 bit = DecodeBit(buffer, octetIndex, bitIndex);
399 if(i == 6 && !bit) { // FLAG is byte aligned AND has only 6 consecutive ones
400 return Q922_FLAG;
401 } else {
402 return Q922_ERROR;
406 } else {
407 if(onesCounter == 5) {
408 // discard bit, read again
409 bit = DecodeBit(buffer, octetIndex, bitIndex);
412 onesCounter = 0;
415 decodedByte |= (bit << i);
418 *destination = decodedByte;
419 return Q922_OK;
422 BYTE Q922_Frame::DecodeBit(const BYTE *buffer,
423 PINDEX & octetIndex,
424 BYTE & bitIndex)
426 BYTE bit = (buffer[octetIndex] >> bitIndex) & 0x01;
428 if(bitIndex == 0) {
429 octetIndex++;
430 bitIndex = 8;
432 bitIndex--;
434 return bit;
437 void Q922_Frame::EncodeOctet(BYTE octet, BYTE *buffer,
438 PINDEX & octetIndex,
439 BYTE & bitIndex,
440 BYTE & onesCounter) const
442 // data is sent out with LSB first, so we need
443 // to reverse the bit direction.
444 // In addition, it is required to insert a zero
445 // bit after 5 consecutive ones to avoid FLAG emulation
447 PINDEX i;
448 for(i = 0; i < 8; i++) {
449 // reading one bit from the octet and write it to the buffer
450 BYTE bit = (BYTE)((octet >> i) & 0x01);
452 EncodeBit(bit, buffer, octetIndex, bitIndex);
454 if(bit) {
455 onesCounter++;
457 if(onesCounter == 5) {
458 // insert a zero bit
459 EncodeBit(0, buffer, octetIndex, bitIndex);
460 onesCounter = 0;
462 } else {
463 onesCounter = 0;
468 void Q922_Frame::EncodeOctetNoEscape(BYTE octet,
469 BYTE *buffer,
470 PINDEX & octetIndex,
471 BYTE & bitIndex) const
473 // data is sent out with LSB first, so we need
474 // to reverse the bit direction.
476 PINDEX i;
477 for(i = 0; i < 8; i++) {
478 // reating one bit from the octet and write it to the buffer
479 BYTE bit = (BYTE)((octet >> i) & 0x01);
481 EncodeBit(bit, buffer, octetIndex, bitIndex);
485 void Q922_Frame::EncodeBit(BYTE bit,
486 BYTE *buffer,
487 PINDEX & octetIndex,
488 BYTE & bitIndex) const
490 if(bitIndex == 7) {
491 buffer[octetIndex] = 0;
494 buffer[octetIndex] |= ((bit & 0x01) << bitIndex);
496 // adjusting bit/byte index
497 if(bitIndex == 0) {
498 octetIndex++;
499 bitIndex = 8;
502 bitIndex--;
505 WORD Q922_Frame::CalculateFCS(const BYTE *data, PINDEX length) const
507 // initial value of FCS is all ones.
508 WORD fcs = 0xffff;
510 while(length--) {
511 fcs = (fcs >> 8) ^ fcstable[(fcs ^ *data++) & 0xff];
514 // take one's complement
515 fcs = ~fcs;
517 return fcs;
520 #endif // OPAL_H224