-
Notifications
You must be signed in to change notification settings - Fork 32
/
ndicapi.cxx
4498 lines (3962 loc) · 121 KB
/
ndicapi.cxx
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=======================================================================
Copyright (c) 2000-2005 Atamai, Inc.
Use, modification and redistribution of the software, in source or
binary forms, are permitted provided that the following terms and
conditions are met:
1) Redistribution of the source code, in verbatim or modified
form, must retain the above copyright notice, this license,
the following disclaimer, and any notices that refer to this
license and/or the following disclaimer.
2) Redistribution in binary form must include the above copyright
notice, a copy of this license and the following disclaimer
in the documentation or with other materials provided with the
distribution.
3) Modified copies of the source code must be clearly marked as such,
and must not be misrepresented as verbatim copies of the source code.
THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
=======================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4996 )
#endif
#include "ndicapi.h"
#include "ndicapi_socket.h"
#include "ndicapi_thread.h"
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
#if defined(__APPLE__)
#include <dirent.h>
#endif
#ifdef __cplusplus
#include <assert.h>
#include <sstream>
#endif
//----------------------------------------------------------------------------
// Prototype for the error helper function, the definition is at the
// end of this file. A call to this function will both set ndicapi
// error indicator, and will also call the error callback function if
// there is one. The return value is equal to errnum.
namespace
{
int ndiSetError(ndicapi* pol, int errnum)
{
pol->ErrorCode = errnum;
// call the user-supplied callback function
if (pol->ErrorCallback)
{
pol->ErrorCallback(errnum, (char*)ndiErrorString(errnum), pol->ErrorCallbackData);
}
return errnum;
}
void parseComponents(ndicapi* api, const char* data);
void parseFrameComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parse6DComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parse3DComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parse1DComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parse2DComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parseLineSepComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parse3DErrorComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parse1DImageComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parseUVComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
void parseSystemAlertComponent(ndicapi* api, const char* data, unsigned short itemOption, unsigned int itemCount);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiSetErrorCallback(ndicapi* pol, NDIErrorCallback callback, void* userdata)
{
pol->ErrorCallback = callback;
pol->ErrorCallbackData = userdata;
}
//----------------------------------------------------------------------------
ndicapiExport void ndiLogState(ndicapi* pol, char outInformation[USHRT_MAX])
{
#ifdef __cplusplus
std::stringstream ss;
for (int i = 0; i < 3; ++i)
{
ss << "GxTransforms[" << i << "][52]: " << pol->GxTransforms[i] << std::endl;
}
ss << "GxStatus[8]: " << pol->GxStatus;
for (int i = 0; i < 3; ++i)
{
ss << "GxInformation[" << i << "][12]: " << pol->GxInformation[i] << std::endl;
}
for (int i = 0; i < 3; ++i)
{
ss << "GxSingleStray[" << i << "][24]: " << pol->GxSingleStray[i] << std::endl;
}
for (int i = 0; i < 3; ++i)
{
ss << "GxFrame[" << i << "][8]: " << pol->GxFrame[i] << std::endl;
}
for (int i = 0; i < 9; ++i)
{
ss << "GxPassiveTransforms[" << i << "][52]: " << pol->GxPassiveTransforms[i] << std::endl;
}
ss << "GxPassiveStatus[24]: " << pol->GxPassiveStatus;
for (int i = 0; i < 9; ++i)
{
ss << "GxPassiveInformation[" << i << "][12]: " << pol->GxPassiveInformation[i] << std::endl;
}
for (int i = 0; i < 9; ++i)
{
ss << "GxPassiveFrame[" << i << "][8]: " << pol->GxPassiveFrame[i] << std::endl;
}
ss << "GxPassiveStray[424]: " << pol->GxPassiveStray;
for (int i = 0; i < 3; ++i)
{
ss << "PstatBasic[" << i << "][32]: " << pol->PstatBasic[i] << std::endl;
}
for (int i = 0; i < 3; ++i)
{
ss << "PstatTesting[" << i << "][8]: " << pol->PstatTesting[i] << std::endl;
}
for (int i = 0; i < 3; ++i)
{
ss << "PstatPartNumber[" << i << "][20]: " << pol->PstatPartNumber[i] << std::endl;
}
for (int i = 0; i < 3; ++i)
{
ss << "PstatAccessories[" << i << "][2]: " << pol->PstatAccessories[i] << std::endl;
}
for (int i = 0; i < 3; ++i)
{
ss << "PstatMarkerType[" << i << "][2]: " << pol->PstatMarkerType[i] << std::endl;
}
for (int i = 0; i < 9; ++i)
{
ss << "PstatPassiveBasic[" << i << "][32]: " << pol->PstatPassiveBasic[i] << std::endl;
}
for (int i = 0; i < 9; ++i)
{
ss << "PstatPassiveTesting[" << i << "][8]: " << pol->PstatPassiveTesting[i] << std::endl;
}
for (int i = 0; i < 9; ++i)
{
ss << "PstatPassivePartNumber[" << i << "][20]: " << pol->PstatPassivePartNumber[i] << std::endl;
}
for (int i = 0; i < 9; ++i)
{
ss << "PstatPassiveAccessories[" << i << "][2]: " << pol->PstatPassiveAccessories[i] << std::endl;
}
for (int i = 0; i < 9; ++i)
{
ss << "PstatPassiveMarkerType[" << i << "][2]: " << pol->PstatPassiveMarkerType[i] << std::endl;
}
ss << "SstatControl[2]: " << pol->SstatControl << std::endl;
ss << "SstatSensor[2]: " << pol->SstatSensor << std::endl;
ss << "SstatTiu[2]: " << pol->SstatTiu << std::endl;
ss << "IrchkDetected: " << pol->IrchkDetected << std::endl;
ss << "IrchkSources[128]: " << pol->IrchkSources << std::endl;
ss << "PhrqReply[2]: " << pol->PhrqReply << std::endl;
ss << "PhsrReply[1284]: " << pol->PhsrReply << std::endl;
ss << "PhinfUnoccupied: " << pol->PhinfUnoccupied << std::endl;
ss << "PhinfBasic[34]: " << pol->PhinfBasic << std::endl;
ss << "PhinfTesting[8]: " << pol->PhinfTesting << std::endl;
ss << "PhinfPartNumber[20]: " << pol->PhinfPartNumber << std::endl;
ss << "PhinfAccessories[2]: " << pol->PhinfAccessories << std::endl;
ss << "PhinfMarkerType[2]: " << pol->PhinfMarkerType << std::endl;
ss << "PhinfPortLocation[14]: " << pol->PhinfPortLocation << std::endl;
ss << "PhinfGpioStatus[2]: " << pol->PhinfGpioStatus << std::endl;
ss << "TxHandleCount: " << pol->TxHandleCount << std::endl;
ss << "TxHandles[NDI_MAX_HANDLES]: " << pol->TxHandles << std::endl;
for (int i = 0; i < pol->TxHandleCount; ++i)
{
ss << "TxTransforms[" << i << "][52]: " << pol->TxTransforms[i] << std::endl;
}
for (int i = 0; i < pol->TxHandleCount; ++i)
{
ss << "TxStatus[" << i << "][8]: " << pol->TxStatus[i] << std::endl;
}
for (int i = 0; i < pol->TxHandleCount; ++i)
{
ss << "TxFrame[" << i << "][8]: " << pol->TxFrame[i] << std::endl;
}
for (int i = 0; i < pol->TxHandleCount; ++i)
{
ss << "TxInformation[" << i << "][12]: " << pol->TxInformation[i] << std::endl;
}
for (int i = 0; i < pol->TxHandleCount; ++i)
{
ss << "TxSingleStray[" << i << "][24]: " << pol->TxSingleStray[i] << std::endl;
}
ss << "TxSystemStatus[4]: " << pol->TxSystemStatus << std::endl;
ss << "TxPassiveStrayCount: " << pol->TxPassiveStrayCount << std::endl;
ss << "TxPassiveStrayOov[14]: " << pol->TxPassiveStrayOov << std::endl;
ss << "TxPassiveStray[1052]: " << pol->TxPassiveStray << std::endl;
ss << "BxHandleCount: " << pol->BxHandleCount << std::endl;
ss << "BxHandles[NDI_MAX_HANDLES]: " << pol->BxHandles << std::endl;
ss << "BxHandlesStatus[NDI_MAX_HANDLES]: " << pol->BxHandlesStatus << std::endl;
ss << "BxFrameNumber[NDI_MAX_HANDLES]: " << pol->BxFrameNumber << std::endl;
for (int i = 0; i < pol->BxHandleCount; ++i)
{
ss << "BxTransforms[" << i << "][8]: " << pol->BxTransforms[i] << std::endl;
}
ss << "BxPortStatus[NDI_MAX_HANDLES]: " << pol->BxPortStatus << std::endl;
for (int i = 0; i < pol->BxHandleCount; ++i)
{
ss << "BxToolMarkerInformation[" << i << "][11]: " << pol->BxToolMarkerInformation[i] << std::endl;
}
ss << "BxActiveSingleStrayMarkerStatus[NDI_MAX_HANDLES]: " << pol->BxActiveSingleStrayMarkerStatus << std::endl;
for (int i = 0; i < pol->BxHandleCount; ++i)
{
ss << "BxActiveSingleStrayMarkerPosition[" << i << "][3]: " << pol->BxActiveSingleStrayMarkerPosition[i] << std::endl;
}
ss << "Bx3DMarkerCount[NDI_MAX_HANDLES]: " << pol->Bx3DMarkerCount << std::endl;
for (int i = 0; i < pol->BxHandleCount; ++i)
{
ss << "Bx3DMarkerOutOfVolume[" << i << "][3]: " << pol->Bx3DMarkerOutOfVolume[i] << std::endl;
}
for (int i = 0; i < pol->BxHandleCount; ++i)
{
for (int j = 0; j < pol->Bx3DMarkerCount[i]; j++)
{
ss << "Bx3DMarkerPosition[" << i << "][" << j << "][3]: " << pol->Bx3DMarkerPosition[i][j] << std::endl;
}
}
ss << "BxPassiveStrayCount: " << pol->BxPassiveStrayCount << std::endl;
ss << "BxPassiveStrayOutOfVolume[30]: " << pol->BxPassiveStrayOutOfVolume << std::endl;
for (int i = 0; i < pol->BxPassiveStrayCount; ++i)
{
ss << "BxPassiveStrayPosition[" << i << "][3]: " << pol->BxPassiveStrayPosition[i] << std::endl;
}
assert(ss.str().size() < USHRT_MAX);
strncpy(&outInformation[0], ss.str().c_str(), ss.str().size());
#else
// Someone else can implement c style string building
#endif
}
//----------------------------------------------------------------------------
ndicapiExport void ndiTimeoutSocket(ndicapi* pol, int timeoutMsec)
{
ndiSocketTimeout(pol->Socket, timeoutMsec);
}
//----------------------------------------------------------------------------
ndicapiExport NDIErrorCallback ndiGetErrorCallback(ndicapi* pol)
{
return pol->ErrorCallback;
}
//----------------------------------------------------------------------------
ndicapiExport void* ndiGetErrorCallbackData(ndicapi* pol)
{
return pol->ErrorCallbackData;
}
//----------------------------------------------------------------------------
ndicapiExport unsigned long ndiHexToUnsignedLong(const char* string, int n)
{
int i;
unsigned long result = 0;
int c;
for (i = 0; i < n; i++)
{
c = string[i];
if (c >= 'a' && c <= 'f')
{
result = (result << 4) | (c + (10 - 'a'));
}
else if (c >= 'A' && c <= 'F')
{
result = (result << 4) | (c + (10 - 'A'));
}
else if (c >= '0' && c <= '9')
{
result = (result << 4) | (c - '0');
}
else
{
break;
}
}
return result;
}
//----------------------------------------------------------------------------
ndicapiExport unsigned int ndiHexToUnsignedInt(const char* string, int n)
{
int i;
unsigned int result = 0;
int c;
for (i = 0; i < n; i++)
{
c = string[i];
if (c >= 'a' && c <= 'f')
{
result = (result << 4) | (c + (10 - 'a'));
}
else if (c >= 'A' && c <= 'F')
{
result = (result << 4) | (c + (10 - 'A'));
}
else if (c >= '0' && c <= '9')
{
result = (result << 4) | (c - '0');
}
else
{
break;
}
}
return result;
}
//----------------------------------------------------------------------------
ndicapiExport long ndiSignedToLong(const char* cp, int n)
{
int i;
int c;
long result = 0;
int sign = 1;
c = cp[0];
if (c == '+')
{
sign = 1;
}
else if (c == '-')
{
sign = -1;
}
else
{
return 0;
}
for (i = 1; i < n; i++)
{
c = cp[i];
if (c >= '0' && c <= '9')
{
result = (result * 10) + (c - '0');
}
else
{
break;
}
}
return sign * result;
}
//----------------------------------------------------------------------------
ndicapiExport char* ndiHexEncode(char* cp, const void* data, int n)
{
const unsigned char* bdata;
int i, c1, c2;
unsigned int d;
char* tcp;
bdata = (const unsigned char*)data;
tcp = cp;
for (i = 0; i < n; i++)
{
d = bdata[i];
c1 = (d & 0xf0) >> 4;
c2 = (d & 0x0f);
c1 += '0';
c2 += '0';
if (c1 > '9')
{
c1 += ('A' - '0' - 10);
}
if (c2 > '9')
{
c2 += ('A' - '0' - 10);
}
*tcp++ = c1;
*tcp++ = c2;
}
return cp;
}
//----------------------------------------------------------------------------
ndicapiExport void* ndiHexDecode(void* data, const char* cp, int n)
{
unsigned char* bdata;
int i, c1, c2;
unsigned int d;
bdata = (unsigned char*)data;
for (i = 0; i < n; i++)
{
d = 0;
c1 = *cp++;
if (c1 >= 'a' && c1 <= 'f')
{
d = (c1 + (10 - 'a'));
}
else if (c1 >= 'A' && c1 <= 'F')
{
d = (c1 + (10 - 'A'));
}
else if (c1 >= '0' && c1 <= '9')
{
d = (c1 - '0');
}
c2 = *cp++;
d <<= 4;
if (c2 >= 'a' && c2 <= 'f')
{
d |= (c2 + (10 - 'a'));
}
else if (c2 >= 'A' && c2 <= 'F')
{
d |= (c2 + (10 - 'A'));
}
else if (c2 >= '0' && c2 <= '9')
{
d |= (c2 - '0');
}
bdata[i] = d;
}
return data;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiPVWRFromFile(ndicapi* pol, int port, char* filename)
{
unsigned char buffer[1024];
char hexdata[128];
FILE* file;
int addr;
pol->ErrorCode = 0;
file = fopen(filename, "rb");
if (file == NULL)
{
return -1;
}
memset(buffer, 0, 1024); // clear buffer to zero
fread(buffer, 1, 1024, file); // read at most 1k from file
if (ferror(file))
{
fclose(file);
return -1;
}
fclose(file);
for (addr = 0; addr < 1024; addr += 64) // write in chunks of 64 bytes
{
ndiPVWR(pol, port, addr, ndiHexEncode(hexdata, &buffer[addr], 64));
if (ndiGetError(pol) != NDI_OKAY)
{
return -1;
}
}
return 0;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiGetError(ndicapi* pol)
{
return pol->ErrorCode;
}
//----------------------------------------------------------------------------
ndicapiExport int ndiGetSocketError(ndicapi* pol)
{
return pol->SocketErrorCode;
}
//----------------------------------------------------------------------------
ndicapiExport const char* ndiErrorString(int errnum)
{
static const char* textarray_low[] = // values from 0x01 to 0x43
{
/* 0x01 */ "No error",
/* 0x02 */ "Invalid command",
/* 0x03 */ "Command too long",
/* 0x04 */ "Command too short",
/* 0x05 */ "Invalid CRC calculated for command",
/* 0x06 */ "Time-out on command execution",
/* 0x07 */ "Unable to set up new communication parameters",
/* 0x08 */ "Incorrect number of command parameters",
/* 0x09 */ "Invalid port handle selected",
/* 0x0A */ "Invalid tracking priority selected (must be S, D or B)",
/* 0x0B */ "Invalid LED selected",
/* 0x0C */ "Invalid LED state selected (must be B, F or S)",
/* 0x0D */ "Command is invalid while in the current mode",
/* 0x0E */ "No tool assigned to the selected port handle",
/* 0x0F */ "Selected port handle not initialized",
/* 0x10 */ "Selected port handle not enabled",
/* 0x11 */ "System not initialized",
/* 0x12 */ "Unable to stop tracking",
/* 0x13 */ "Unable to start tracking",
/* 0x14 */ "Unable to initialize Tool-in-Port",
/* 0x15 */ "Invalid Position Sensor or Field Generator characterization parameters",
/* 0x16 */ "Unable to initialize the Measurement System",
/* 0x17 */ "Unable to start diagnostic mode",
/* 0x18 */ "Unable to stop diagnostic mode",
/* 0x19 */ "Unable to determine environmental infrared or magnetic interference",
/* 0x1A */ "Unable to read device's firmware version information",
/* 0x1B */ "Internal Measurement System error",
/* 0x1C */ "Unable to initialize for environmental infrared diagnostics",
/* 0x1D */ "Unable to set marker firing signature",
/* 0x1E */ "Unable to search for SROM IDs",
/* 0x1F */ "Unable to read SROM data",
/* 0x20 */ "Unable to write SROM data",
/* 0x21 */ "Unable to select SROM",
/* 0x22 */ "Unable to perform tool current test",
/* 0x23 */ "Unable to find camera parameters from the selected volume for the wavelength of a tool enabled for tracking",
/* 0x24 */ "Command parameter out of range",
/* 0x25 */ "Unable to select parameters by volume",
/* 0x26 */ "Unable to determine Measurement System supported features list",
/* 0x27 */ "Reserved - Unrecognized Error 0x26",
/* 0x28 */ "Reserved - Unrecognized Error 0x27",
/* 0x29 */ "Too many tools are enabled, the configuration of tools loaded requires too many frames, or this tool type is not supported",
/* 0x2A */ "Main processor firmware corrupt",
/* 0x2B */ "No memory available for dynamic allocation (heap is full)",
/* 0x2C */ "Requested handle has not been allocated",
/* 0x2D */ "Requested handle has become unoccupied",
/* 0x2E */ "All handles have been allocated",
/* 0x2F */ "Incompatible firmware versions",
/* 0x30 */ "Invalid port description",
/* 0x31 */ "Requested port already assigned to a port handle",
/* 0x32 */ "Reserved - Unrecognized Error 0x31",
/* 0x33 */ "Invalid operation for the device associated with the specified port handle",
/* 0x34 */ "Feature not available",
/* 0x35 */ "User parameter does not exist",
/* 0x36 */ "Invalid value type(e.g.string instead of integer)",
/* 0x37 */ "User parameter value set is out of valid range",
/* 0x38 */ "User parameter array index is out of valid range",
/* 0x39 */ "User parameter size is incorrect",
/* 0x3A */ "Permission denied; file or user parameter is read - only, or a command which requires master mode is attempted from a monitor mode connection",
/* 0x3B */ "Reserved - Unrecognized Error 0x3A",
/* 0x3C */ "File not found",
/* 0x3D */ "Error writing to file",
/* 0x3E */ "Error reading from file",
/* 0x3F */ "Reserved - Unrecognized Error 0x3E",
/* 0x40 */ "Reserved - Unrecognized Error 0x3F",
/* 0x41 */ "Tool Definition File Error. This occurs if: the CRC failed or the file format is invalid",
/* 0x42 */ "Tool characteristics not supported",
/* 0x43 */ "Device not present",
};
static const char* textarray_high[] = // values from 0xf6 to 0xf4
{
"Too much environmental infrared",
"Unrecognized error code",
"Unrecognized error code",
"Unable to erase Flash EPROM"
"Unable to write Flash EPROM",
"Unable to read Flash EPROM",
};
static const char* textarray_api[] = // values specific to the API
{
"Bad CRC on reply from Measurement System",
"Error opening serial connection",
"Host not capable of given communications parameters",
"Device->host communication timeout",
"Serial port write error",
"Serial port read error",
"Measurement System failed to reset on break",
"Measurement System not found on specified port"
};
static const char* textarray_serial[] = // values specific to serial errors
{
"Serial DSR query failure",
"Bad reply from measurement system",
"System does not support Features.Firmware",
"Command VER failed"
};
if (errnum >= 0x00 && errnum <= 0x43)
{
return textarray_low[errnum];
}
else if (errnum >= 0xf1 && errnum <= 0xf6)
{
return textarray_high[errnum - 0xf1];
}
else if (errnum >= 0x0100 && errnum <= 0x0107)
{
return textarray_api[errnum - 0x0100];
}
else if (errnum >= 0x0200 && errnum <= 0x0203)
{
return textarray_serial[errnum - 0x200];
}
return "Unrecognized error code";
}
//----------------------------------------------------------------------------
ndicapiExport const char* ndiSerialDeviceName(int i)
{
#if defined(_WIN32)
// Windows
const size_t MAX_BUF_LEN = 100;
static char deviceName[100 + 1];
char deviceNumber[100 + 1];
int comPortNumber;
comPortNumber = i + 1;
deviceName[MAX_BUF_LEN] = 0;
deviceNumber[MAX_BUF_LEN] = 0;
if (comPortNumber < 1)
{
return NULL;
}
// COM port name format is different for port number under/over 10 (see Microsoft KB115831)
if (comPortNumber < 10)
{
strcpy_s(deviceName, MAX_BUF_LEN, "COM");
}
else
{
strcpy_s(deviceName, MAX_BUF_LEN, "\\\\.\\COM");
}
_itoa_s(comPortNumber, deviceNumber, MAX_BUF_LEN, 10);
strcat_s(deviceName, MAX_BUF_LEN, deviceNumber);
return deviceName;
#elif defined (__APPLE__)
// Apple
static char devicename[255 + 6];
DIR* dirp;
struct dirent* ep;
int j = 0;
dirp = opendir("/dev/");
if (dirp == NULL)
{
return NULL;
}
while ((ep = readdir(dirp)) != NULL && j <= i)
{
if (ep->d_name[0] == 'c' && ep->d_name[1] == 'u' &&
ep->d_name[2] == '.')
{
if (j == i)
{
strncpy(devicename, "/dev/", 5);
strncpy(devicename + 5, ep->d_name, 255);
devicename[255 + 5] = '\0';
closedir(dirp);
return devicename;
}
j++;
}
}
closedir(dirp);
return NULL;
#else
// Linux/Unix variants
#ifdef NDI_DEVICE0
if (i == 0)
{
return NDI_DEVICE0;
}
#endif
#ifdef NDI_DEVICE1
if (i == 1)
{
return NDI_DEVICE1;
}
#endif
#ifdef NDI_DEVICE2
if (i == 2)
{
return NDI_DEVICE2;
}
#endif
#ifdef NDI_DEVICE3
if (i == 3)
{
return NDI_DEVICE3;
}
#endif
#ifdef NDI_DEVICE4
if (i == 4)
{
return NDI_DEVICE4;
}
#endif
#ifdef NDI_DEVICE5
if (i == 5)
{
return NDI_DEVICE5;
}
#endif
#ifdef NDI_DEVICE6
if (i == 6)
{
return NDI_DEVICE6;
}
#endif
#ifdef NDI_DEVICE7
if (i == 7)
{
return NDI_DEVICE7;
}
#endif
return NULL;
#endif
}
//----------------------------------------------------------------------------
ndicapiExport int ndiSerialProbe(const char* device, bool checkDSR)
{
char reply[1024];
char init_reply[16];
NDIFileHandle serial_port;
int n;
serial_port = ndiSerialOpen(device);
if (serial_port == NDI_INVALID_HANDLE)
{
return NDI_OPEN_ERROR;
}
// check DSR line to see whether any device is connected
if (checkDSR && !ndiSerialCheckDSR(serial_port))
{
ndiSerialClose(serial_port);
return NDI_DSR_FAILURE;
}
// set comm parameters to default, but decrease timeout to 0.1s
if (ndiSerialComm(serial_port, 9600, "8N1", 0) < 0 || ndiSerialTimeout(serial_port, 100) < 0)
{
ndiSerialClose(serial_port);
return NDI_BAD_COMM;
}
// flush the buffers (which are unlikely to contain anything)
ndiSerialFlush(serial_port, NDI_IOFLUSH);
// try to initialize ndicapi
int errorCode;
if (ndiSerialWrite(serial_port, "INIT:E3A5\r", 10) < 10 || ndiSerialSleep(serial_port, 100) < 0 ||
ndiSerialRead(serial_port, init_reply, 16, false, &errorCode) <= 0 || strncmp(init_reply, "OKAYA896\r", 9) != 0)
{
// increase timeout to 7 seconds for reset
ndiSerialTimeout(serial_port, 7000);
// init failed: flush, reset, and try again
ndiSerialFlush(serial_port, NDI_IOFLUSH);
if (ndiSerialFlush(serial_port, NDI_IOFLUSH) < 0 ||
ndiSerialBreak(serial_port))
{
ndiSerialClose(serial_port);
return NDI_BAD_COMM;
}
n = ndiSerialRead(serial_port, init_reply, 16, false, &errorCode);
if (n < 0)
{
ndiSerialClose(serial_port);
return errorCode;
}
else if (n == 0)
{
ndiSerialClose(serial_port);
return NDI_TIMEOUT;
}
// check reply from reset
if (strncmp(init_reply, "RESETBE6F\r", 10) != 0)
{
ndiSerialClose(serial_port);
return NDI_BAD_REPLY;
}
// try to initialize a second time
ndiSerialSleep(serial_port, 100);
n = ndiSerialWrite(serial_port, "INIT:E3A5\r", 10);
if (n < 0)
{
ndiSerialClose(serial_port);
return NDI_WRITE_ERROR;
}
else if (n < 10)
{
ndiSerialClose(serial_port);
return NDI_TIMEOUT;
}
ndiSerialSleep(serial_port, 100);
n = ndiSerialRead(serial_port, init_reply, 16, false, &errorCode);
if (n < 0)
{
ndiSerialClose(serial_port);
return NDI_READ_ERROR;
}
else if (n == 0)
{
ndiSerialClose(serial_port);
return NDI_TIMEOUT;
}
if (strncmp(init_reply, "OKAYA896\r", 9) != 0)
{
ndiSerialClose(serial_port);
return NDI_PROBE_FAIL;
}
}
ndiSerialSleep(serial_port, 100);
if (ndiSerialWrite(serial_port, "GETINFO:Features.Firmware.Version0492\r", strlen("GETINFO:Features.Firmware.Version0492\r")) != strlen("GETINFO:Features.Firmware.Version0492\r"))
{
ndiSerialClose(serial_port);
return NDI_NO_FEATURES_FIRMWARE;
}
n = ndiSerialRead(serial_port, reply, 1023, false, &errorCode);
if (n == 0)
{
ndiSerialClose(serial_port);
return NDI_TIMEOUT;
}
else if (n < 0)
{
ndiSerialClose(serial_port);
return errorCode;
}
else
{
if (strncmp(reply, "ERROR", 5) == 0)
{
if (ndiSerialWrite(serial_port, "VER:065EE\r", 10) < 10 ||
(n = ndiSerialRead(serial_port, reply, 1023, false, &errorCode)) < 7)
{
ndiSerialClose(serial_port);
return NDI_COMMAND_VER_FAILED;
}
}
else if (strncmp(reply, "Features", strlen("Features")) != 0)
{
ndiSerialClose(serial_port);
return NDI_BAD_REPLY;
}
}
// restore things back to the way they were
ndiSerialClose(serial_port);
return NDI_OKAY;
}
//----------------------------------------------------------------------------
ndicapiExport ndicapi* ndiOpenSerial(const char* device)
{
NDIFileHandle serial_port;
ndicapi* pol;
serial_port = ndiSerialOpen(device);
if (serial_port == NDI_INVALID_HANDLE)
{
return NULL;
}
if (ndiSerialComm(serial_port, 9600, "8N1", 0) < 0)
{
ndiSerialClose(serial_port);
return NULL;
}
if (ndiSerialFlush(serial_port, NDI_IOFLUSH) < 0)
{
ndiSerialClose(serial_port);
return NULL;
}
pol = (ndicapi*)malloc(sizeof(ndicapi));
if (pol == 0)
{
ndiSerialClose(serial_port);
return NULL;
}
memset(pol, 0, sizeof(ndicapi));
pol->SerialDevice = serial_port;
// allocate the buffers
pol->SerialDeviceName = (char*)malloc(strlen(device) + 1);
pol->Command = (char*)malloc(2048);
pol->Reply = (char*)malloc(2048);
pol->ReplyNoCRC = (char*)malloc(2048);
pol->Hostname = NULL;
pol->Port = -1;
pol->Socket = -1;
if (pol->SerialDeviceName == 0 || pol->Command == 0 || pol->Reply == 0 || pol->ReplyNoCRC == 0)
{
if (pol->SerialDeviceName)
{
free(pol->SerialDeviceName);
}
if (pol->Command)
{
free(pol->Command);
}
if (pol->Reply)
{
free(pol->Reply);
}
if (pol->ReplyNoCRC)
{
free(pol->ReplyNoCRC);
}
ndiSerialClose(serial_port);
return NULL;
}
// initialize the allocated memory
strcpy(pol->SerialDeviceName, device);
memset(pol->Command, 0, 2048);
memset(pol->Reply, 0, 2048);
memset(pol->ReplyNoCRC, 0, 2048);
return pol;
}
//----------------------------------------------------------------------------
ndicapiExport ndicapi* ndiOpenNetwork(const char* hostname, int port)
{
NDISocketHandle socket;
ndicapi* device;
int connected = ndiSocketOpen(hostname, port, socket);
if (socket == -1 || connected == -1)
{
return NULL;
}
device = (ndicapi*)malloc(sizeof(ndicapi));
if (device == 0)
{
ndiSocketClose(socket);
return NULL;
}
memset(device, 0, sizeof(ndicapi));
device->Hostname = new char[strlen(hostname) + 1];
device->Hostname = strncpy(device->Hostname, hostname, strlen(hostname));
device->Port = port;
device->Socket = socket;
device->SerialDevice = NDI_INVALID_HANDLE;
device->SerialDeviceName = NULL;
// allocate the buffers