File: igtl_bind.c

package info (click to toggle)
openigtlink 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 20,076; ansic: 6,704; sh: 227; perl: 74; makefile: 46
file content (712 lines) | stat: -rw-r--r-- 15,869 bytes parent folder | download | duplicates (5)
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
/*=========================================================================

  Program:   The OpenIGTLink Library
  Language:  C
  Web page:  http://openigtlink.org/

  Copyright (c) Insight Software Consortium. All rights reserved.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include <stdlib.h>
#include <string.h>

#include "igtl_bind.h"
#include "igtl_util.h"


void igtl_export igtl_bind_init_info(igtl_bind_info * bind_info)
{
  if (bind_info)
    {
    bind_info->ncmessages = 0;
    bind_info->child_info_array = NULL;
    bind_info->resol = 0;
    bind_info->request_all = 0;
    bind_info->status = 0;
    }
}


int igtl_export igtl_bind_alloc_info(igtl_bind_info * bind_info, igtl_uint16 ncmessages)
{
  size_t size;
  
  if (bind_info == NULL)
    {
    return 0;
    }

  size = (size_t) ncmessages * sizeof(igtl_bind_child_info);
  bind_info->child_info_array = malloc(size);

  if (bind_info->child_info_array != NULL)
    {
    bind_info->ncmessages = ncmessages;
    memset(bind_info->child_info_array, 0, size);
    return 1;
    }
  else
    {
    bind_info->ncmessages = 0;
    return 0;
    }
}


int igtl_export igtl_bind_free_info(igtl_bind_info * bind_info)
{

  if (bind_info == NULL)
    {
    return 0;
    }
  
  if (bind_info->ncmessages == 0 || bind_info->child_info_array == NULL)
    {
    bind_info->ncmessages = 0;
    return 1;
    }

  free(bind_info->child_info_array);
  return 1;

}


/*
 * Function to unpack BIND message
 */
int igtl_bind_unpack_normal(void * byte_array, igtl_bind_info * info)
{
  igtl_uint16 i;
  igtl_uint16 ncmessages;
  igtl_uint16 nametable_size;
  size_t      namelen;
  char * ptr;
  char * ptr2;
  igtl_uint64 tmp64;
  
  if (byte_array == NULL || info == NULL)
    {
    return 0;
    }
  
  /* Number of child messages */
  ptr = (char *) byte_array;
  if (igtl_is_little_endian())
    {
    ncmessages = BYTE_SWAP_INT16(*((igtl_uint16*)ptr));
    }
  else
    {
    ncmessages = *((igtl_uint16*)ptr);
    }
  
  /* Allocate an array of bind_info, if neccessary */
  if (ncmessages != info->ncmessages)
    {
    if (igtl_bind_alloc_info(info, ncmessages) == 0)
      {
      return 0;
      }
    }
  
  /* Pointer to the first element in the BIND header section */
  ptr += sizeof(igtl_uint16);
  
  /* Extract types and body sizes from the BIND header section */
  for (i = 0; i < ncmessages; i ++)
    {
    /* Type of child message */
    strncpy(info->child_info_array[i].type, (char*)ptr, IGTL_HEADER_TYPE_SIZE);
    info->child_info_array[i].type[IGTL_HEADER_TYPE_SIZE] = '\0';
    
    /* Body size of child message */
    ptr += IGTL_HEADER_TYPE_SIZE;
    if (igtl_is_little_endian())
      {
      memcpy(&tmp64, ptr, sizeof(igtl_uint64));
      info->child_info_array[i].size = BYTE_SWAP_INT64(tmp64);
      }
    else
      {
      memcpy(&(info->child_info_array[i].size), ptr, sizeof(igtl_uint64));
      }

    ptr += sizeof(igtl_uint64);
    }

  /* Name table size */
  if (igtl_is_little_endian())
    {
    nametable_size = BYTE_SWAP_INT16(*((igtl_uint16 *) ptr));
    }
  else
    {
    nametable_size = *((igtl_uint16 *) ptr);
    }

  /*
   * Check if the name table field is aligned to WORD (The length of
   * the field must be even. 
   */
  if (nametable_size % 2 != 0)
    {
    return 0;
    }
  
  ptr += sizeof(igtl_uint16);
  ptr2 = ptr;

  /* Extract device names from the name table section */
  if (nametable_size > 0)
    {
    for (i = 0; i < ncmessages; i ++)
      {
      strncpy(info->child_info_array[i].name, ptr, IGTL_HEADER_NAME_SIZE);
      info->child_info_array[i].name[IGTL_HEADER_NAME_SIZE] = '\0';
      namelen = strlen(info->child_info_array[i].name);
      ptr += namelen + 1;
      }
    }

  ptr = ptr2 + nametable_size;

  /* Set pointers to the child message bodies */
  for (i = 0; i < ncmessages; i ++)
    {
    info->child_info_array[i].ptr = ptr;
    /* Note: a padding byte is added, if the size of the child message body
       is odd. */
    ptr += info->child_info_array[i].size + (info->child_info_array[i].size % 2);
    }
    
  /** TODO: check the total size of the message? **/

  return 1;
}


/*
 * Function to unpack GET_BIND and STT_BIND messages
 */
int igtl_bind_unpack_request(void * byte_array, igtl_bind_info * info, igtl_uint64 size)
{
  igtl_uint16 i;
  igtl_uint16 ncmessages;
  igtl_uint16 nametable_size;
  size_t      namelen;
  char * ptr;
  char * ptr2;
  
  if (size == 0)
    {
    info->request_all = 1;
    return 1;
    }

  info->request_all = 0;

  if (byte_array == NULL || info == NULL)
    {
    return 0;
    }
  
  /* Number of child messages */
  ptr = (char *) byte_array;
  if (igtl_is_little_endian())
    {
    ncmessages = BYTE_SWAP_INT16(*((igtl_uint16*)ptr));
    }
  else
    {
    ncmessages = *((igtl_uint16*)ptr);
    }
  
  /* Allocate an array of bind_info, if neccessary */
  if (ncmessages != info->ncmessages)
    {
    if (igtl_bind_alloc_info(info, ncmessages) == 0)
      {
      return 0;
      }
    }
  
  /* Pointer to the first element in the BIND header section */
  ptr += sizeof(igtl_uint16);
  
  /* Extract types and body sizes from the BIND header section */
  for (i = 0; i < ncmessages; i ++)
    {
    /* Type of child message */
    strncpy(info->child_info_array[i].type, (char*)ptr, IGTL_HEADER_TYPE_SIZE);
    info->child_info_array[i].type[IGTL_HEADER_TYPE_SIZE] = '\0';
    ptr += IGTL_HEADER_TYPE_SIZE;
    }

  /* Name table size */
  if (igtl_is_little_endian())
    {
    nametable_size = BYTE_SWAP_INT16(*((igtl_uint16 *) ptr));
    }
  else
    {
    nametable_size = *((igtl_uint16 *) ptr);
    }

  /*
   * Check if the name table field is aligned to WORD (The length of
   * the field must be even. 
   */
  if (nametable_size % 2 != 0)
    {
    return 0;
    }
  
  ptr += sizeof(igtl_uint16);
  ptr2 = ptr;

  /* Extract device names from the name table section */
  if (nametable_size > 0)
    {
    for (i = 0; i < ncmessages; i ++)
      {
      strncpy(info->child_info_array[i].name, ptr, IGTL_HEADER_NAME_SIZE);
      info->child_info_array[i].name[IGTL_HEADER_NAME_SIZE] = '\0';
      namelen = strlen(info->child_info_array[i].name);
      ptr += namelen + 1;
      }
    }

  /** TODO: check the total size of the message? **/

  return 1;
}


int igtl_export igtl_bind_unpack(int type, void * byte_array, igtl_bind_info * info, igtl_uint64 size)
{
  char * ptr;

  switch (type)
    {

    case IGTL_TYPE_PREFIX_NONE:
      return igtl_bind_unpack_normal(byte_array, info);
      break;

    case IGTL_TYPE_PREFIX_GET:
      return igtl_bind_unpack_request(byte_array, info, size);
      break;

    case IGTL_TYPE_PREFIX_STT:
      /*
       * STT_BIND message has the same format as GET_BIND, except that it
       * has the time resolution field.
       */
      /* Obtain time resolution */
      ptr = byte_array;
      if (igtl_is_little_endian())
        {
        memcpy(&(info->resol), ptr, sizeof(igtl_uint64));
        info->resol = BYTE_SWAP_INT64(info->resol);
        }
      else
        {
        memcpy(&(info->resol), ptr, sizeof(igtl_uint64));
        }
      /* Unpack rest of the message */
      ptr += sizeof(igtl_uint64);
      return igtl_bind_unpack_request(ptr, info, size-sizeof(igtl_uint64));
      break;

    case IGTL_TYPE_PREFIX_STP:
      return 1;
      break;

    case IGTL_TYPE_PREFIX_RTS:
      info->status = * (igtl_uint8 *) byte_array;
      return 1;
      break;

    default:
      return 0;
      break;
    }
}


/*
 * Function to unpack BIND message
 */
int igtl_bind_pack_normal(igtl_bind_info * info, void * byte_array)
{
  char * ptr;
  igtl_uint32 i;
  igtl_uint32 nc;
  igtl_uint16 * nts;
  size_t wb;
  size_t len;
  igtl_uint16 tmp16;
  igtl_uint64 tmp64;

  ptr = (char *) byte_array;
  nc = info->ncmessages;

  /* Validate info */
  if (info->ncmessages == 0 || info->child_info_array == NULL)
    {
    return 0;
    }
  
  /* Number of child messages */
  if (igtl_is_little_endian())
    {
    /* *((igtl_uint16*) ptr) = BYTE_SWAP_INT16(info->ncmessages); */
    tmp16 = BYTE_SWAP_INT16(info->ncmessages);
    memcpy(ptr, &tmp16, sizeof(igtl_uint16));
    }
  else
    {
    /* *((igtl_uint16*) ptr) = info->ncmessages; */
    memcpy(ptr, &(info->ncmessages), sizeof(igtl_uint16));
    }
  ptr += sizeof(igtl_uint16);


  /* BIND header section */
  for (i = 0; i < nc; i ++)
    {
    /* Fill with 0 */
    memset(ptr, 0, IGTL_HEADER_TYPE_SIZE);
    strncpy((char*)ptr, info->child_info_array[i].type, IGTL_HEADER_TYPE_SIZE);
    ptr += IGTL_HEADER_TYPE_SIZE;

    /* 2011-04-29
     * Since ptr is not alined to double-word order at this point, 
     * "* ((igtl_uint64 *) ptr) = <value>" may fail in some archtecture.
     * In the following section, memcpy() is called instaed of using * ((igtl_uint64 *) ptr)
     * expression.
     */

    if (igtl_is_little_endian())
      {
      tmp64 = BYTE_SWAP_INT64(info->child_info_array[i].size);
      memcpy(ptr, &tmp64, sizeof(igtl_uint64));
      }
    else
      {
      memcpy(ptr, &(info->child_info_array[i].size), sizeof(igtl_uint64));
      }
    ptr += sizeof(igtl_uint64);
    }


  /* Name table section */
  nts = (igtl_uint16 *) ptr; /* save address for name table size field */
  ptr += sizeof(igtl_uint16);

  wb = 0;
  for (i = 0; i < nc; i ++)
    {
    len = strlen(info->child_info_array[i].name);
    if (len > IGTL_HEADER_NAME_SIZE)
      {
      return 0;
      }
    /* copy string + NULL-terminator */
    strncpy(ptr, info->child_info_array[i].name, len+1);
    ptr += (len + 1);
    wb += len + 1;
    }

  /* Add padding if the size of the name table section is not even */
  if (wb % 2 > 0)
    {
    *((igtl_uint8*)ptr) = 0;
    ptr ++;
    wb ++;
    }

  /* Substitute name table size */
  *nts = (igtl_uint16) wb;
  if (igtl_is_little_endian())
    {
    *nts = BYTE_SWAP_INT16(*nts);
    }

  return 1;

}


/*
 * Function to unpack STT_BIND and GET_BIND messages
 */
int igtl_bind_pack_request(igtl_bind_info * info, void * byte_array)
{
  char * ptr;
  igtl_uint32 i;
  igtl_uint32 nc;
  igtl_uint16 * nts;
  size_t wb;
  size_t len;
  igtl_uint16 tmp16;

  ptr = (char *) byte_array;
  nc = info->ncmessages;

  /* If requesting all available, no body is generated. */
  if (info->request_all == 1)
    {
    return 1;
    }

  /* Validate info */
  if (info->ncmessages == 0 || info->child_info_array == NULL)
    {
    return 0;
    }

  /* Number of child messages */
  if (igtl_is_little_endian())
    {
    /* *((igtl_uint16*) ptr) = BYTE_SWAP_INT16(info->ncmessages); */
    tmp16 = BYTE_SWAP_INT16(info->ncmessages);
    memcpy(ptr, &tmp16, sizeof(igtl_uint16));
    }
  else
    {
    /* *((igtl_uint16*) ptr) = info->ncmessages; */
    memcpy(ptr, &(info->ncmessages), sizeof(igtl_uint16));
    }
  ptr += sizeof(igtl_uint16);


  /* BIND header section */
  for (i = 0; i < nc; i ++)
    {
    /*memset(ptr, 0, IGTL_HEADER_TYPE_SIZE); */ /* Fill with 0 */
    strncpy((char*)ptr, info->child_info_array[i].type, IGTL_HEADER_TYPE_SIZE);
    ptr += IGTL_HEADER_TYPE_SIZE;
    }

  /* Name table section */
  nts = (igtl_uint16 *) ptr; /* save address for name table size field */
  ptr += sizeof(igtl_uint16);

  wb = 0;
  for (i = 0; i < nc; i ++)
    {
    len = strlen(info->child_info_array[i].name);
    if (len > IGTL_HEADER_NAME_SIZE)
      {
      return 0;
      }
    /* copy string + NULL-terminator */
    strncpy(ptr, info->child_info_array[i].name, len+1);
    ptr += (len + 1);
    wb += len + 1;
    }

  /* Substitute name table size */
  *nts = (igtl_uint16) wb;
  if (igtl_is_little_endian())
    {
    *nts = BYTE_SWAP_INT16(*nts);
    }

  return 1;
  
}


int igtl_export igtl_bind_pack(igtl_bind_info * info, void * byte_array, int type)
{
  char * ptr;
  igtl_uint64 tmp64;

  switch (type)
    {
    case IGTL_TYPE_PREFIX_NONE:
      return igtl_bind_pack_normal(info, byte_array);
      break;

    case IGTL_TYPE_PREFIX_GET:
      return igtl_bind_pack_request(info, byte_array);
      break;

    case IGTL_TYPE_PREFIX_STT:
      /*
       * STT_BIND message has the same format as GET_BIND, except that it
       * has the time resolution field.
       */
      ptr = (char *) byte_array;

      /* Get time resolution */ 
      if (igtl_is_little_endian())
        {
        memcpy(&tmp64, ptr, sizeof(igtl_uint64));
        info->resol = BYTE_SWAP_INT64(tmp64);
        }
      else
        {
        memcpy(&(info->resol), ptr, sizeof(igtl_uint64));
        }
      ptr += sizeof(igtl_uint64);

      /* Generate rest of the message */
      return igtl_bind_pack_request(info, ptr);
      break;
      
    case IGTL_TYPE_PREFIX_STP:
      return 1;
      break;

    case IGTL_TYPE_PREFIX_RTS:
      * (igtl_uint8 * ) byte_array = info->status;
      return 1;
      break;

    default:
      return 0;
      break;
    }
}


/*
 * Function to calculate size of BIND message header
 */
igtl_uint64 igtl_bind_get_size_normal(igtl_bind_info * info)
{
  igtl_uint64 size;
  igtl_uint32 ntable_size;
  igtl_uint16 i;
  igtl_uint16 nc;
  
  nc = info->ncmessages;

  /* Size of NCMESSAGES section */
  size = sizeof(igtl_uint16);

  /* Add size of BIND header section */
  size += (IGTL_HEADER_TYPE_SIZE+sizeof(igtl_uint64)) * nc;

  /* Add size of table size field*/
  size += sizeof(igtl_uint16);

  /* Calculate size of name table */
  ntable_size = 0;
  for (i = 0; i < nc; i ++)
    {
    /* string length + NULL separator */
    ntable_size += strlen(info->child_info_array[i].name) + 1;
    }
  if (ntable_size %2 > 0)
    {
    ntable_size += 1;
    }
  size += ntable_size;

  return size;
}


/*
 * Function to calculate size of GET_BIND/STT_BIND messages
 */
igtl_uint64 igtl_bind_get_size_request(igtl_bind_info * info)
{
  igtl_uint64 size;
  igtl_uint32 ntable_size;
  igtl_uint16 i;
  igtl_uint16 nc;
  
  nc = info->ncmessages;

  /* Size of NCMESSAGES section */
  size = sizeof(igtl_uint16);

  /* Add size of BIND header section */
  size += (IGTL_HEADER_TYPE_SIZE+sizeof(igtl_uint64)) * nc;

  /* Add size of table size field*/
  size += sizeof(igtl_uint16);

  /* Calculate size of name table */
  ntable_size = 0;
  for (i = 0; i < nc; i ++)
    {
    /* string length + NULL separator */
    ntable_size += strlen(info->child_info_array[i].name) + 1;
    }

  return size;

}


igtl_uint64 igtl_export igtl_bind_get_size(igtl_bind_info * info, int type)
{
  switch (type)
    {
    case IGTL_TYPE_PREFIX_NONE:
      return igtl_bind_get_size_normal(info);
      break;

    case IGTL_TYPE_PREFIX_GET:
      return igtl_bind_get_size_request(info);
      break;

    case IGTL_TYPE_PREFIX_STT:
      /*
       * STT_BIND message has the same format as GET_BIND, except that it
       * has the time resolution field.
       */
      return igtl_bind_get_size_request(info) + sizeof(igtl_uint64);
      break;

    case IGTL_TYPE_PREFIX_STP:
      return 0;
      break;

    case IGTL_TYPE_PREFIX_RTS:
      return sizeof(igtl_uint8);
      break;

    default:
      return 0;
      break;
    }
}


igtl_uint64 igtl_export igtl_bind_get_crc(igtl_bind_info * info, int type, void* bind_message)
{
  igtl_uint64   crc;
  igtl_uint64   bind_length;
  igtl_uint16   i;
  igtl_uint16   nc;

  bind_length = (igtl_uint64)igtl_bind_get_size(info, type);
  crc = crc64(0, 0, 0);
  crc = crc64((unsigned char*) bind_message, (int)bind_length, crc);

  if (type == IGTL_TYPE_PREFIX_NONE)
    {
    nc = info->ncmessages;
    for (i = 0; i < nc; i ++)
      {
      crc = crc64((unsigned char*) info->child_info_array[i].ptr, (int)info->child_info_array[i].size, crc);
      }
    }

  return crc;
}