-
Notifications
You must be signed in to change notification settings - Fork 0
/
cphidget.c
2056 lines (1721 loc) · 56.7 KB
/
cphidget.c
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
/*
* This file is part of libphidget21
*
* Copyright 2006-2015 Phidgets Inc <patrick@phidgets.com>
*
* This library 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 library 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 library; if not, see
* <http://www.gnu.org/licenses/>
*/
#include "stdafx.h"
#include "cphidget.h"
#include "cusb.h"
#include "csocket.h"
#include "cthread.h"
#include "cphidgetlist.h"
#include "utils.h"
static int CPhidgetGPP_dataInput(CPhidgetHandle phid, unsigned char *buffer, int length);
int print_debug_messages = FALSE;
void(CCONV *fptrJavaDetachCurrentThread)(void) = NULL;
CPhidgetListHandle ActiveDevices = 0;
CPhidgetListHandle AttachedDevices = 0;
/* Protects ActiveDevices and AttachedDevices */
int phidgetLocksInitialized = PFALSE;
CThread_mutex_t activeDevicesLock;
CThread_mutex_t attachedDevicesLock;
char
translate_bool_to_ascii(char value)
{
switch (value) {
case PTRUE:
return '1';
case PFALSE:
return '0';
}
return '?';
}
int CCONV phidget_type_to_id(const char *name)
{
int i;
for(i=0;i<PHIDGET_DEVICE_CLASS_COUNT;i++)
{
if(Phid_DeviceName[i] != NULL)
{
if(!strcmp(Phid_DeviceName[i],name))
return i;
}
}
return -1;
}
//returns ms
double timestampdiff(CPhidget_Timestamp time1, CPhidget_Timestamp time2)
{
return ((time1.seconds * 1000) + (double)(time1.microseconds / 1000.0)) - ((time2.seconds * 1000) + (double)(time2.microseconds / 1000.0));
}
double timeSince(TIME *start)
{
#ifdef _WINDOWS
TIME now;
FILETIME nowft, oldft, resft;
ULARGE_INTEGER nowul, oldul, resul;
double duration;
GetSystemTime(&now);
SystemTimeToFileTime(&now, &nowft);
SystemTimeToFileTime(start, &oldft);
nowul.HighPart = nowft.dwHighDateTime;
nowul.LowPart = nowft.dwLowDateTime;
oldul.HighPart = oldft.dwHighDateTime;
oldul.LowPart = oldft.dwLowDateTime;
resul.HighPart = nowul.HighPart - oldul.HighPart;
resul.LowPart = nowul.LowPart - oldul.LowPart;
resft.dwHighDateTime = resul.HighPart;
resft.dwLowDateTime = resul.LowPart;
duration = (double)(resft.dwLowDateTime/10000000.0);
#else
struct timeval now;
gettimeofday(&now, NULL);
double duration = (now.tv_sec - start->tv_sec) + (double)((now.tv_usec-start->tv_usec)/1000000.0);
#endif
return duration;
}
void setTimeNow(TIME *now)
{
#ifdef _WINDOWS
GetSystemTime(now);
#else
gettimeofday(now,NULL);
#endif
}
/* The is a void pointer that gets malloced only of some platforms */
void CPhidgetFHandle_free(void *arg)
{
#if defined(_MACOSX) || defined(WINCE)
return;
#else
free(arg); arg = NULL;
return;
#endif
}
//Two different Phidget Handles that refer to the same Physical Phidget
int CCONV CPhidget_areEqual(void *arg1, void *arg2)
{
CPhidgetHandle phid1 = (CPhidgetHandle)arg1;
CPhidgetHandle phid2 = (CPhidgetHandle)arg2;
if(!phid1 || !phid2)
return PFALSE;
// If they are not the same device class
if(phid2->deviceID != phid1->deviceID)
return PFALSE;
// If they both have a device id, but they are different
if(phid1->deviceIDSpec && phid2->deviceIDSpec && (phid2->deviceIDSpec != phid1->deviceIDSpec))
return PFALSE;
// Neither device is marked as PHIDGETOPEN_ANY
if(phid1->specificDevice && phid2->specificDevice)
{
// If one is open serial and the other is open label
if((phid1->specificDevice == PHIDGETOPEN_SERIAL && phid2->specificDevice == PHIDGETOPEN_LABEL) ||
(phid1->specificDevice == PHIDGETOPEN_LABEL && phid2->specificDevice == PHIDGETOPEN_SERIAL))
return PFALSE;
// If one is open serial but they have different serials
if((phid1->specificDevice == PHIDGETOPEN_SERIAL || phid2->specificDevice == PHIDGETOPEN_SERIAL) &&
phid2->serialNumber != phid1->serialNumber)
return PFALSE;
// If one is open label but they have different labels
if((phid1->specificDevice == PHIDGETOPEN_LABEL || phid2->specificDevice == PHIDGETOPEN_LABEL) &&
strncmp(phid1->label, phid2->label, MAX_LABEL_STORAGE-1))
return PFALSE;
}
return PTRUE;
//OLD
/*if(
(
(phid1->specificDevice == 0 || phid2->specificDevice == 0)?1:(phid2->serialNumber == phid1->serialNumber))
&&
(
(phid1->deviceIDSpec == 0 || phid2->deviceIDSpec == 0)?1:(phid2->deviceIDSpec == phid1->deviceIDSpec)
) &&
(phid2->deviceID == phid1->deviceID))
return 1;
return 0;*/
}
//Two different Phidget Handles that refer to the same Physical Phidget - more stringent
// Same serial, device class and device id
int CCONV CPhidget_areExtraEqual(void *arg1, void *arg2)
{
CPhidgetHandle phid1 = (CPhidgetHandle)arg1;
CPhidgetHandle phid2 = (CPhidgetHandle)arg2;
if(!phid1 || !phid2)
return PFALSE;
if(
(phid2->serialNumber == phid1->serialNumber) &&
(phid2->deviceIDSpec == phid1->deviceIDSpec) &&
(phid2->deviceID == phid1->deviceID)
)
return PTRUE;
return PFALSE;
}
//Two identical Phidget Handles (same pointer)
int CCONV CPhidgetHandle_areEqual(void *arg1, void *arg2)
{
if(arg1 == arg2) return 1;
return 0;
}
void CPhidgetErrorEvent_free(void *arg)
{
free(((void **)arg)[0]);
free(arg);
}
void CCONV CPhidget_free(void *arg)
{
CPhidgetHandle phid = (CPhidgetHandle)arg;
if (!phid)
return;
//Call device specific free function if it exists
if(phid->fptrFree)
phid->fptrFree(phid);
//this is only malloc-ed on windows and linux, not wince or mac
#if (defined(_WINDOWS) && !defined(WINCE)) || defined(_LINUX) || defined(_ANDROID)
if (phid->CPhidgetFHandle) {
CPhidgetFHandle_free(phid->CPhidgetFHandle); phid->CPhidgetFHandle = NULL;
}
#endif
CThread_mutex_destroy(&phid->lock);
CThread_mutex_destroy(&phid->openCloseLock);
CThread_mutex_destroy(&phid->writelock);
CThread_mutex_destroy(&phid->outputLock);
CThread_destroy_event(&phid->writeAvailableEvent);
CThread_destroy_event(&phid->writtenEvent);
CList_emptyList((CListHandle *)&phid->errEventList, PTRUE, CPhidgetErrorEvent_free);
free(phid); phid = NULL;
return;
}
int CCONV CPhidget_create(CPhidgetHandle *phid)
{
CPhidgetHandle temp_phid;
TESTPTR(phid)
if(!(temp_phid = malloc(sizeof(CPhidget))))
return EPHIDGET_NOMEMORY;
ZEROMEM(temp_phid, sizeof(CPhidget));
CThread_mutex_init(&temp_phid->lock);
CThread_mutex_init(&temp_phid->openCloseLock);
CThread_mutex_init(&temp_phid->writelock);
CThread_mutex_init(&temp_phid->outputLock);
CThread_create_event(&temp_phid->writeAvailableEvent);
CThread_create_event(&temp_phid->writtenEvent);
CPhidget_clearStatusFlag(&temp_phid->status, PHIDGET_ATTACHED_FLAG, &temp_phid->lock);
*phid = temp_phid;
return EPHIDGET_OK;
}
const char *CPhidget_strerror(int error)
{
if ((error < 0) || (error >= PHIDGET_ERROR_CODE_COUNT))
return Phid_UnknownErrorDescription;
return Phid_ErrorDescriptions[error];
}
int CPhidget_statusFlagIsSet(int status, int flag)
{
if(status & flag) return PTRUE;
return PFALSE;
}
//status is a flags variable
int CPhidget_setStatusFlag(int *status, int flag, CThread_mutex_t *lock)
{
TESTPTR(status)
if(lock != NULL) CThread_mutex_lock(lock);
*status |= flag;
if(lock != NULL) CThread_mutex_unlock(lock);
return EPHIDGET_OK;
}
int CPhidget_clearStatusFlag(int *status, int flag, CThread_mutex_t *lock)
{
TESTPTR(status)
if(lock != NULL) CThread_mutex_lock(lock);
*status &= (~flag);
if(lock != NULL) CThread_mutex_unlock(lock);
return EPHIDGET_OK;
}
int CPhidget_read(CPhidgetHandle phid)
{
int result = EPHIDGET_OK;
TESTPTR(phid)
if (CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG) || CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHING_FLAG))
{
if((result = CUSBReadPacket((CPhidgetHandle)phid, phid->lastReadPacket)) != EPHIDGET_OK)
return result;
if((phid->lastReadPacket[0] & PHID_USB_GENERAL_PACKET_FLAG) && deviceSupportsGeneralUSBProtocol(phid))
result = CPhidgetGPP_dataInput(phid, phid->lastReadPacket, phid->inputReportByteLength);
else if (phid->fptrData)
result= phid->fptrData(phid, phid->lastReadPacket, phid->inputReportByteLength);
return result;
}
return EPHIDGET_NOTATTACHED;
}
int CPhidget_write(CPhidgetHandle phid)
{
unsigned char buffer[MAX_OUT_PACKET_SIZE];
unsigned int len;
int result = EPHIDGET_OK;
TESTPTR(phid)
ZEROMEM(buffer, sizeof(buffer));
CThread_reset_event(&phid->writeAvailableEvent);
len = MAX_OUT_PACKET_SIZE;
if ((result = phid->fptrGetPacket((CPhidgetHandle)phid, buffer, &len))
!= EPHIDGET_OK)
goto fail;
// XXX len ignored
if ((result = CUSBSendPacket((CPhidgetHandle)phid, buffer)) !=
EPHIDGET_OK)
goto fail;
fail:
/*
* under all circumstances (especially failure), signal
* waiting writers to loop around and check device status.
*/
CThread_set_event(&phid->writtenEvent);
return result;
}
//Begin exported functions
int CCONV
CPhidget_open(CPhidgetHandle phid, int serialNumber)
{
int result = 0;
TESTPTR(phid)
if (serialNumber < -1)
return EPHIDGET_INVALIDARG;
#if defined(_IPHONE)
return EPHIDGET_UNSUPPORTED;
#endif
#if defined(_ANDROID)
if(!ANDROID_USB_GOOD)
return EPHIDGET_UNSUPPORTED;
#endif
CThread_mutex_lock(&phid->openCloseLock);
if (CPhidget_statusFlagIsSet(phid->status, PHIDGET_OPENED_FLAG))
{
LOG(PHIDGET_LOG_WARNING, "Open was called on an already opened Phidget handle.");
CThread_mutex_unlock(&phid->openCloseLock);
return EPHIDGET_OK;
}
if (serialNumber == -1)
phid->specificDevice = PHIDGETOPEN_ANY;
else
phid->specificDevice = PHIDGETOPEN_SERIAL;
phid->serialNumber = serialNumber;
result = RegisterLocalDevice(phid);
CPhidget_setStatusFlag(&phid->status, PHIDGET_OPENED_FLAG, &phid->lock);
CThread_mutex_unlock(&phid->openCloseLock);
return result;
}
int CCONV
CPhidget_openLabel(CPhidgetHandle phid, const char *label)
{
int result = 0;
TESTPTR(phid)
#if defined(_IPHONE)
return EPHIDGET_UNSUPPORTED;
#endif
#if defined(_ANDROID)
if(!ANDROID_USB_GOOD)
return EPHIDGET_UNSUPPORTED;
#endif
if(label != NULL && ((result = encodeLabelString(label, NULL, NULL)) != EPHIDGET_OK))
return result;
CThread_mutex_lock(&phid->openCloseLock);
if (CPhidget_statusFlagIsSet(phid->status, PHIDGET_OPENED_FLAG))
{
LOG(PHIDGET_LOG_WARNING, "Open was called on an already opened Phidget handle.");
CThread_mutex_unlock(&phid->openCloseLock);
return EPHIDGET_OK;
}
if (label == NULL)
phid->specificDevice = PHIDGETOPEN_ANY;
else
{
phid->specificDevice = PHIDGETOPEN_LABEL;
memcpy(phid->label, label, strlen(label)+1);
}
result = RegisterLocalDevice(phid);
CPhidget_setStatusFlag(&phid->status, PHIDGET_OPENED_FLAG, &phid->lock);
CThread_mutex_unlock(&phid->openCloseLock);
return result;
}
int CCONV
CPhidget_close(CPhidgetHandle phid)
{
int result = EPHIDGET_OK;
char key[1024], val[6];
TESTPTR(phid)
CThread_mutex_lock(&phid->openCloseLock);
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_OPENED_FLAG))
{
//Could be .NET finalizer - always calls close even if already called.
LOG(PHIDGET_LOG_INFO, "Close was called on an already closed Phidget handle.");
CThread_mutex_unlock(&phid->openCloseLock);
return EPHIDGET_OK;
}
//Call device specific close function if it exists
if(phid->fptrClose)
phid->fptrClose(phid);
if(CPhidget_statusFlagIsSet(phid->status, PHIDGET_REMOTE_FLAG))
{
CThread_mutex_lock(&phid->lock);
if(CPhidget_statusFlagIsSet(phid->status, PHIDGET_SERVER_CONNECTED_FLAG))
{
struct sockaddr_storage name;
char addr[200], *a;
socklen_t namelen = sizeof(name);
int port, e;
if(getsockname(phid->networkInfo->server->socket, (struct sockaddr *)&name, &namelen) != 0)
{
#ifndef _WINDOWS
LOG(PHIDGET_LOG_WARNING,"getsockname: %s", strerror(errno));
#else
LOG(PHIDGET_LOG_WARNING,"getsockname: WSA Error %d", WSAGetLastError());
#endif
goto netdone;
}
if((e = getnameinfo((struct sockaddr *)&name, namelen, addr, sizeof(addr), NULL, 0, NI_NUMERICHOST)) != 0)
{
LOG(PHIDGET_LOG_WARNING,"getnameinfo: %s", gai_strerror(e));
goto netdone;
}
port = (int)((struct sockaddr_in *)&name)->sin_port;
escape(addr, strlen(addr), &a);
if(phid->specificDevice == PHIDGETOPEN_SERIAL)
snprintf(key, sizeof(key), "/PCK/Client/%s/%d%05d/%s/%d", a, phid->networkInfo->uniqueConnectionID, port, Phid_DeviceName[phid->deviceID], phid->serialNumber);
else if(phid->specificDevice == PHIDGETOPEN_LABEL)
{
char *l;
escape(phid->label, strlen(phid->label), &l);
snprintf(key, sizeof(key), "/PCK/Client/%s/%d%05d/%s/-1/%s", a, phid->networkInfo->uniqueConnectionID, port, Phid_DeviceName[phid->deviceID], l);
free(l);
}
else
snprintf(key, sizeof(key), "/PCK/Client/%s/%d%05d/%s", a, phid->networkInfo->uniqueConnectionID, port, Phid_DeviceName[phid->deviceID]);
snprintf(val, sizeof(val), "Close");
free(a);
//don't care about errors on this, so we don't add the error handler - just close already!
pdc_async_set(phid->networkInfo->server->pdcs, key, val, (int)strlen(val), PTRUE, NULL, NULL);
}
netdone:
CThread_mutex_unlock(&phid->lock);
result = unregisterRemotePhidget(phid);
phid->keyCount = 0;
}
else
{
if(!phidgetLocksInitialized)
{
CThread_mutex_init(&activeDevicesLock);
CThread_mutex_init(&attachedDevicesLock);
phidgetLocksInitialized = PTRUE;
}
CThread_mutex_lock(&activeDevicesLock);
CList_removeFromList((CListHandle *)&ActiveDevices, phid, CPhidget_areEqual, FALSE, NULL);
CThread_mutex_unlock(&activeDevicesLock);
if (CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)) {
phid->writeStopFlag = PTRUE;
CThread_join(&phid->writeThread); //join before closing because we want to wait for outstanding writes to complete
result = CUSBCloseHandle(phid);
CThread_join(&phid->readThread);
}
if(phid->specificDevice == PHIDGETOPEN_ANY_ATTACHED)
{
phid->specificDevice = PHIDGETOPEN_ANY;
phid->serialNumber = -1;
}
//if there are no more active phidgets or managers, wait for the central thread to exit
if(!ActiveDevices && !ActivePhidgetManagers)
{
JoinCentralThread();
//Shut down USB
#if defined(_LINUX) && !defined(_ANDROID)
CUSBUninit();
#endif
}
}
CPhidget_clearStatusFlag(&phid->status, PHIDGET_OPENED_FLAG, &phid->lock);
CThread_mutex_unlock(&phid->openCloseLock);
return result;
}
//TODO: maybe this should take (CPhidgetHandle *) so that we can nulify the pointer
// (and handle multiple calls to CPhidget_delete)
int CCONV
CPhidget_delete(CPhidgetHandle phid)
{
CPhidget_free(phid);
return EPHIDGET_OK;
}
int CCONV
CPhidget_set_OnDetach_Handler(CPhidgetHandle phid,
int(CCONV *fptr)(CPhidgetHandle, void *), void *userPtr)
{
TESTPTR(phid)
phid->fptrDetach = fptr;
phid->fptrDetachptr = userPtr;
return EPHIDGET_OK;
}
int CCONV
CPhidget_set_OnAttach_Handler(CPhidgetHandle phid,
int(CCONV *fptr)(CPhidgetHandle, void *), void *userPtr)
{
TESTPTR(phid)
phid->fptrAttach = fptr;
phid->fptrAttachptr = userPtr;
return EPHIDGET_OK;
}
int CCONV
CPhidget_set_OnError_Handler(CPhidgetHandle phid,
int (CCONV *fptr) (CPhidgetHandle, void *, int, const char *), void *userPtr)
{
TESTPTR(phid)
phid->fptrError = fptr;
phid->fptrErrorptr = userPtr;
return EPHIDGET_OK;
}
int CCONV CPhidget_set_OnServerConnect_Handler(CPhidgetHandle phid, int (CCONV *fptr)(CPhidgetHandle phid, void *userPtr), void *userPtr)
{
TESTPTR(phid)
phid->fptrServerConnect = fptr;
phid->fptrServerConnectptr = userPtr;
return EPHIDGET_OK;
}
int CCONV CPhidget_set_OnServerDisconnect_Handler(CPhidgetHandle phid, int (CCONV *fptr)(CPhidgetHandle phid, void *userPtr), void *userPtr)
{
TESTPTR(phid)
phid->fptrServerDisconnect = fptr;
phid->fptrServerDisconnectptr = userPtr;
return EPHIDGET_OK;
}
int CCONV
CPhidget_getDeviceName(CPhidgetHandle phid, const char **buffer)
{
TESTPTRS(phid, buffer)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)
&& !CPhidget_statusFlagIsSet(phid->status, PHIDGET_DETACHING_FLAG))
return EPHIDGET_NOTATTACHED;
if(phid->deviceIDSpec == PHIDID_FIRMWARE_UPGRADE)
{
if(!phid->firmwareUpgradeName[0])
snprintf(phid->firmwareUpgradeName, 30, "%s %s", phid->usbProduct, phid->deviceDef->pdd_name);
*buffer = phid->firmwareUpgradeName;
}
else
*buffer = (char *)phid->deviceDef->pdd_name;
return EPHIDGET_OK;
}
int CCONV
CPhidget_getSerialNumber(CPhidgetHandle phid, int *serialNumber)
{
TESTPTRS(phid, serialNumber)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)
&& !CPhidget_statusFlagIsSet(phid->status, PHIDGET_DETACHING_FLAG))
return EPHIDGET_NOTATTACHED;
*serialNumber = phid->serialNumber;
return EPHIDGET_OK;
}
int CCONV
CPhidget_getDeviceVersion(CPhidgetHandle phid, int *devVer)
{
TESTPTRS(phid, devVer)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)
&& !CPhidget_statusFlagIsSet(phid->status, PHIDGET_DETACHING_FLAG))
return EPHIDGET_NOTATTACHED;
*devVer = phid->deviceVersion;
return EPHIDGET_OK;
}
/* for now this just returns the attached bit of the status variable - this function should probably be renamed*/
// This CAN be called on closed devices, this should NOT be called internally as it is confusing
int CCONV
CPhidget_getDeviceStatus(CPhidgetHandle phid, int *status)
{
TESTPTRS(phid, status)
*status = CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG);
return EPHIDGET_OK;
}
/* for now this just returns the attached bit of the status variable */
int CCONV
CPhidget_getServerStatus(CPhidgetHandle phid, int *status)
{
TESTPTRS(phid, status)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_REMOTE_FLAG))
return EPHIDGET_UNSUPPORTED;
CThread_mutex_lock(&phid->lock);
if(CPhidget_statusFlagIsSet(phid->status, PHIDGET_SERVER_CONNECTED_FLAG))
if(phid->networkInfo->server)
*status = CPhidget_statusFlagIsSet(phid->networkInfo->server->status, PHIDGETSOCKET_CONNECTED_FLAG);
else
*status = PFALSE;
else
*status = PFALSE;
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_OK;
}
int CCONV
CPhidget_getLibraryVersion(const char **buffer)
{
TESTPTR(buffer)
*buffer = LibraryVersion;
return EPHIDGET_OK;
}
int CCONV
CPhidget_getDeviceType(CPhidgetHandle phid, const char **buffer)
{
TESTPTRS(phid, buffer)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)
&& !CPhidget_statusFlagIsSet(phid->status, PHIDGET_DETACHING_FLAG))
return EPHIDGET_NOTATTACHED;
*buffer = (char *)Phid_DeviceName[phid->deviceID];
return EPHIDGET_OK;
}
int CCONV
CPhidget_getDeviceID(CPhidgetHandle phid, CPhidget_DeviceID *deviceID)
{
TESTPTRS(phid, deviceID)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)
&& !CPhidget_statusFlagIsSet(phid->status, PHIDGET_DETACHING_FLAG))
return EPHIDGET_NOTATTACHED;
*deviceID = phid->deviceIDSpec;
return EPHIDGET_OK;
}
int CCONV
CPhidget_getDeviceClass(CPhidgetHandle phid, CPhidget_DeviceClass *deviceClass)
{
TESTPTRS(phid, deviceClass)
*deviceClass = phid->deviceID;
return EPHIDGET_OK;
}
int CCONV
CPhidget_getUSBProductString(CPhidgetHandle phid, const char **buffer)
{
TESTPTRS(phid, buffer)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)
&& !CPhidget_statusFlagIsSet(phid->status, PHIDGET_DETACHING_FLAG))
return EPHIDGET_NOTATTACHED;
*buffer = (char *)phid->usbProduct;
return EPHIDGET_OK;
}
int CCONV
CPhidget_getDeviceLabel(CPhidgetHandle phid, const char **buffer)
{
TESTPTRS(phid, buffer)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG)
&& !CPhidget_statusFlagIsSet(phid->status, PHIDGET_DETACHING_FLAG))
return EPHIDGET_NOTATTACHED;
*buffer = (char *)phid->label;
return EPHIDGET_OK;
}
int CCONV
CPhidget_setDeviceLabel(CPhidgetHandle phid, const char *buffer)
{
int ret = EPHIDGET_OK;
TESTPTRS(phid, buffer)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG))
return EPHIDGET_NOTATTACHED;
if(CPhidget_statusFlagIsSet(phid->status, PHIDGET_REMOTE_FLAG))
{
char key[1024];
if((ret = encodeLabelString(buffer, NULL, NULL)) != EPHIDGET_OK)
return ret;
snprintf(key, sizeof(key), "/PCK/%s/%d/Label", phid->deviceType, phid->serialNumber);
CThread_mutex_lock(&phid->lock);
if(!CPhidget_statusFlagIsSet(phid->status, PHIDGET_SERVER_CONNECTED_FLAG))
{
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_NETWORK_NOTCONNECTED;
}
pdc_async_set(phid->networkInfo->server->pdcs, key, buffer, (int)strlen(buffer), PFALSE, internal_async_network_error_handler, phid);
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_OK;
}
else
{
int len;
char buffer2[(MAX_LABEL_SIZE * 2) + 2];
ZEROMEM(buffer2, (MAX_LABEL_SIZE * 2) + 2);
len = (MAX_LABEL_SIZE * 2);
if((ret = encodeLabelString(buffer, &buffer2[2], &len)) == EPHIDGET_OK)
{
//length of descriptor
buffer2[0] = len+2;
//type of descriptor (string)
buffer2[1] = 3;
//make sure we're not trying to set a label that will match the wrap-around bug when read back
if(labelHasWrapError(phid->serialNumber, buffer2) == PTRUE)
{
LOG(PHIDGET_LOG_WARNING, "Can't set a label that would match the wraparound bug.");
return EPHIDGET_INVALIDARG;
}
if ((ret = CUSBSetLabel(phid, buffer2)) == EPHIDGET_OK)
{
int triedUTF8 = PFALSE;
refresh:
//read back the label and compare it
if ((ret = CUSBRefreshLabelString(phid)) == EPHIDGET_OK)
{
CPhidgetHandle foundPhidget;
//label read back didn't match
if(strcmp(buffer, phid->label))
{
//label descriptor is longer then 16 bytes and the first 7 bytes back match;
// almost certainly this is a problem with the wrap around bug.
if(buffer2[0] > 16 && !strncmp(buffer, phid->label, 7) && triedUTF8 == PFALSE)
{
//try setting the label as UTF-8 with 0xFFFF header - we can encode up to 12 bytes
if(strlen(buffer) <= 12)
{
LOG(PHIDGET_LOG_INFO, "Trying to setLabel as UTF-8 because of wrap around bug.");
//only try this once
triedUTF8 = PTRUE;
strcpy(&buffer2[4], buffer);
buffer2[0] = strlen(buffer) + 4;
buffer2[2] = 0xFF;
buffer2[3] = 0xFF;
if ((ret = CUSBSetLabel(phid, buffer2)) == EPHIDGET_OK)
{
//go check it
goto refresh;
}
else //setLabel failed
{
LOG(PHIDGET_LOG_ERROR, "Something unexpected happened trying to set the label (UTF-8). Try again.");
goto clearlabel;
}
}
else //label is too long to be stored, but we have tried to write the label, so we need to clear out anything stored
{
ret = EPHIDGET_INVALIDARG;
LOG(PHIDGET_LOG_ERROR, "This device supports 12-bytes UTF-8 labels. Try again with a shorter string, or pure ASCII.");
goto clearlabel;
}
}
else //label doesn't match and it doesn't look like the wrap around error
{
ret = EPHIDGET_UNEXPECTED;
LOG(PHIDGET_LOG_ERROR, "set label doesn't match read back label: \"%s\" vs. \"%s\"", buffer, phid->label);
goto clearlabel;
}
}
else //label matches, we're good
{
//update label in PhidgetManager
CThread_mutex_lock(&attachedDevicesLock);
if(CList_findInList((CListHandle)AttachedDevices, phid, CPhidget_areEqual, (void **)&foundPhidget) == EPHIDGET_OK)
{
strcpy(foundPhidget->label, buffer);
}
CThread_mutex_unlock(&attachedDevicesLock);
}
}
else
{
LOG(PHIDGET_LOG_ERROR, "Was unable to read back the label after setting.");
goto clearlabel;
}
}
else
{
LOG(PHIDGET_LOG_ERROR, "Something unexpected happened trying to set the label. Try again.");
return ret;
}
}
else
{
LOG(PHIDGET_LOG_ERROR, "Error encoding label string, not setting label.");
return ret;
}
//Success!
return EPHIDGET_OK;
//if a setLabel succeeded, but then we got an error verifying, then we should just clear the label so there's nothing funky in there.
clearlabel:
LOG(PHIDGET_LOG_INFO, "Clearing label because of an error during set.");
ZEROMEM(buffer2, (MAX_LABEL_SIZE * 2) + 2);
buffer2[0] = 2;
buffer2[1] = 3;
CUSBSetLabel(phid, buffer2);
return ret;
}
}
int CCONV
CPhidget_getServerID(CPhidgetHandle phid, const char **buffer)
{
TESTPTRS(phid, buffer)
#ifdef USE_ZEROCONF
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_REMOTE_FLAG))
return EPHIDGET_UNSUPPORTED;
CThread_mutex_lock(&phid->lock);
if(!phid->networkInfo->mdns) //not mDNS - not yet supported
{
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_UNSUPPORTED;
}
//refresh ONLY if connected - otherwise it might block
if(CPhidget_statusFlagIsSet(phid->status, PHIDGET_SERVER_CONNECTED_FLAG))
{
if(refreshZeroconfPhidget(phid))
{
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_NETWORK;
}
}
if(phid->networkInfo->zeroconf_server_id)
{
*buffer = (char *)phid->networkInfo->zeroconf_server_id;
}
else if(phid->networkInfo->requested_serverID)
{
*buffer = (char *)phid->networkInfo->requested_serverID;
}
else
{
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_UNEXPECTED;
}
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_OK;
#else
return EPHIDGET_UNSUPPORTED;
#endif
}
int CCONV
CPhidget_getServerAddress(CPhidgetHandle phid, const char **ipAddr, int *port)
{
TESTPTRS(phid, ipAddr)
TESTPTR(port)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_REMOTE_FLAG))
return EPHIDGET_UNSUPPORTED;
CThread_mutex_lock(&phid->lock);
#ifdef USE_ZEROCONF
if(phid->networkInfo->mdns)
{
//Look it up again new EVERY time!
if(getZeroconfHostPort(phid->networkInfo))
{
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_NETWORK;
}
if(!phid->networkInfo->zeroconf_host || !phid->networkInfo->zeroconf_port)
{
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_UNEXPECTED;
}
*ipAddr = (char *)phid->networkInfo->zeroconf_host;
*port = strtol(phid->networkInfo->zeroconf_port, NULL, 10);
}
else
#endif
{
if(CPhidget_statusFlagIsSet(phid->status, PHIDGET_SERVER_CONNECTED_FLAG))
{
if(!phid->networkInfo->server->address || !phid->networkInfo->server->port)
{
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_UNEXPECTED;
}
*ipAddr = (char *)phid->networkInfo->server->address;
*port = strtol(phid->networkInfo->server->port, NULL, 10);
}
else
{
*ipAddr = (char *)phid->networkInfo->requested_address;
*port = strtol(phid->networkInfo->requested_port, NULL, 10);
}
}
CThread_mutex_unlock(&phid->lock);
return EPHIDGET_OK;
}
int CCONV CPhidget_getErrorDescription(int ErrorCode, const char **buf)
{
TESTPTR(buf)
if ((ErrorCode < 0) || (ErrorCode >= PHIDGET_ERROR_CODE_COUNT)) {
*buf = CPhidget_strerror(EPHIDGET_INVALID);
return EPHIDGET_INVALID;
}
*buf = CPhidget_strerror(ErrorCode);
return EPHIDGET_OK;
}
//expect 6 bytes of data
int CCONV CPhidget_calibrate(CPhidgetHandle phid, unsigned char Offset, unsigned char *data)
{
int result;
unsigned char buffer[8];
TESTPTR(phid)
if (!CPhidget_statusFlagIsSet(phid->status, PHIDGET_ATTACHED_FLAG))
return EPHIDGET_NOTATTACHED;
ZEROMEM(buffer, sizeof(buffer));
buffer[0] = 0x74;