Repository navigation
Expand file tree
/
Copy pathmethods.cpp
More file actions
4151 lines (4026 loc) · 180 KB
/
Copy pathmethods.cpp
File metadata and controls
4151 lines (4026 loc) · 180 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
#include "methods.h"
#include "exceptions.h"
#include "dll.h"
#include "defines.h"
#ifndef USING_STUDIO_8
#define USING_STUDIO_8 1
#endif
#include <icsnVC40.h>
#ifdef _USE_INTERNAL_HEADER_
#include <icsnVC40Internal.h>
#endif
#include <datetime.h>
#include "object_spy_message.h"
#include "object_neo_device.h"
#include "object_vcan3_settings.h"
#include "object_fire_settings.h"
#include "object_can_settings.h"
#include "object_iso9141_keyword2000_settings.h"
#include "object_iso9141_keyword2000_init_steps.h"
#include "object_lin_settings.h"
#include "object_swcan_settings.h"
#include "object_textapi_settings.h"
#include "object_uart_settings.h"
#include "object_vcanrf_settings.h"
#include "object_cyan_settings.h"
#include "object_api_firmware_info.h"
#include "object_op_eth_general_settings.h"
#include "object_op_eth_settings.h"
#include "object_rad_galaxy_settings.h"
#include "setup_module_auto_defines.h"
#include "object_cm_iso157652_tx_message.h"
#include "object_cm_iso157652_rx_message.h"
#include "object_vcan412_settings.h"
#include "object_vividcan_settings.h"
//#include "object_vcan4_settings.h" // Not implemented in 802
#include "object_device_settings.h"
extern PyTypeObject spy_message_object_type;
// __func__, __FUNCTION__ and __PRETTY_FUNCTION__ are not preprocessor macros.
// but MSVC doesn't follow c standard and treats __FUNCTION__ as a string literal macro...
#if (defined(_WIN32) || defined(__WIN32__))
#define arg_parse(a,f) a f
#else
const char* arg_parse(const char* args, const char* func)
{
static char buffer[128];
memset(buffer, '\0', sizeof(buffer)/sizeof(buffer[0]));
strcpy(buffer, args);
strcat(buffer, func);
return (const char*)buffer;
}
#endif
#ifndef _USE_INTERNAL_HEADER_
typedef struct
{
unsigned char sec; // --- Seconds (00-59)
unsigned char min; // --- (00-59)
unsigned char hour; // --- (00-23)
unsigned char day; // --- (01-31)
unsigned char month; // --- (01-12)
unsigned char year; // --- (00-99)
} icsSpyTime;
#endif
// Linux specific fixes
#if !(defined(_WIN32) || defined(__WIN32__))
// __int64 is a msvc specific type
#include <inttypes.h>
#define __int64 int64_t
// __stdcall is a windows calling convention
#ifndef __stdcall
#define __stdcall
// Sleep is a windows.h specific function.
#include <unistd.h>
#define Sleep(i) sleep(i)
#endif
#endif
// Internal function
char* neodevice_to_string(unsigned long type)
{
switch (type) {
case NEODEVICE_UNKNOWN: return "Unknown";
case NEODEVICE_BLUE: return "neoVI BLUE";
case NEODEVICE_ECU_AVB: return "neoECU/AVB TSN";
case NEODEVICE_RADSUPERMOON: return "RADSuperMoon";
case NEODEVICE_DW_VCAN: return "ValueCAN DW";
case NEODEVICE_RADMOON2: return "RADMoon2";
case NEODEVICE_RADGIGALOG: return "RADGigalog";
case NEODEVICE_VCAN41: return "ValueCAN4-1";
case NEODEVICE_FIRE: return "neoVI FIRE";
//case NEODEVICE_RADPLUTO: return "RAD-Pluto";
case NEODEVICE_VCAN42_EL: return "ValueCAN4-1";
//case NEODEVICE_RADIO_CANHUB: return "neoRAD-IO2 CANHUB";
//case NEODEVICE_NEOECU12: return "neoECU12";
case NEODEVICE_VCAN3: return "ValueCAN3";
case NEODEVICE_RED: return "neoVI RED";
case NEODEVICE_ECU: return "neoECU";
case NEODEVICE_IEVB: return "IEVB";
case NEODEVICE_PENDANT: return "Pendant";
case NEODEVICE_OBD2_PRO: return "neoOBD2 Pro";
case NEODEVICE_ECUCHIP_UART: return "ECUCHIP";
case NEODEVICE_PLASMA: return "neoVI PLASMA";
case NEODEVICE_NEOANALOG: return "neoAnalog";
case NEODEVICE_CT_OBD: return "neoOBD CT";
case NEODEVICE_ION: return "neoVI ION";
case NEODEVICE_RADSTAR: return "RADStar";
case NEODEVICE_VCAN44: return "ValueCAN4-4";
case NEODEVICE_VCAN42: return "ValueCAN4-2";
case NEODEVICE_CMPROBE: return "CM Probe";
case NEODEVICE_EEVB: return "EEVB";
case NEODEVICE_VCANRF: return "ValueCAN.rf";
case NEODEVICE_FIRE2: return "neoVI FIRE2";
case NEODEVICE_FLEX: return "neoVI FLEX";
case NEODEVICE_RADGALAXY: return "RADGalaxy";
case NEODEVICE_RADSTAR2: return "RADStar2";
case NEODEVICE_VIVIDCAN: return "VividCAN";
case NEODEVICE_OBD2_SIM: return "neoOBD2 Sim";
//case NEODEVICE_ANY_PLASMA: return "neoVI PLASMA";
//case NEODEVICE_ANY_ION: return "neoVI ION";
};
return "Unknown";
}
bool _convertListOrTupleToArray(PyObject* obj, std::vector<PyObject*>* results)
{
if (!obj || !results)
{
set_ics_exception(exception_runtime_error(), "_convertListOrTupleToArray() was passed invalid arguments");
return false;
}
results->clear();
if (PyList_CheckExact(obj))
{
Py_ssize_t length = PyList_Size(obj);
for (Py_ssize_t i=0; i < length; ++i)
results->push_back(PyList_GetItem(obj, i));
return true;
}
else if (PyTuple_CheckExact(obj))
{
Py_ssize_t length = PyTuple_Size(obj);
for (Py_ssize_t i=0; i < length; ++i)
results->push_back(PyTuple_GetItem(obj, i));
return true;
}
else
{
set_ics_exception(exception_runtime_error(), "_convertListOrTupleToArray() was passed neither a list or tuple");
return false;
}
set_ics_exception(exception_runtime_error(), "_convertListOrTupleToArray() bug!");
return false;
}
PyObject* meth_find_devices(PyObject* self, PyObject* args, PyObject* keywords)
{
PyObject* device_type = NULL;
int ex_options = -1;
char* kwords[] = { "type", "stOptionsOpenNeoEx", NULL };
if (!PyArg_ParseTupleAndKeywords(args, keywords, arg_parse("|Oi:", __FUNCTION__),
kwords, &device_type, &ex_options)) {
return NULL;
}
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
NeoDevice devices[255];
int count = 255;
unsigned long legacy_dev_type = 0xFFFFBFFF;
bool use_legacy_device_type = false;
#if PY_MAJOR_VERSION >= 3
if (device_type && PyLong_Check(device_type))
#else
if (device_type && PyInt_Check(device_type))
#endif PY_MAJOR_VERSION >= 3
{
legacy_dev_type = PyLong_AsLong(device_type);
use_legacy_device_type = true;
}
try
{
#ifndef USE_LEGACY_FIND_DEVICES
// Get the list of device types
unsigned int* device_type_list = NULL;
unsigned int device_type_list_size = 0;
if (!use_legacy_device_type && device_type && device_type != Py_None)
{
std::vector<PyObject*> device_type_vector;
if (!_convertListOrTupleToArray(device_type, &device_type_vector))
return NULL;
device_type_list_size = device_type_vector.size();
device_type_list = new unsigned int[device_type_list_size];
for (unsigned int i=0; i < device_type_list_size; ++i)
device_type_list[i] = PyLong_AsLong(device_type_vector[i]);
}
Py_BEGIN_ALLOW_THREADS
try
{
if (use_legacy_device_type)
throw ice::Exception("Use Legacy Function!");
if (ex_options != -1)
throw ice::Exception("Need to use old style find function");
//int _stdcall icsneoFindNeoDevicesNewStyle(unsigned int* deviceTypes, unsigned int numDeviceTypes, NeoDevice* pNeoDevice, int* pNumDevices)
ice::Function<int __stdcall (unsigned int*, unsigned int, NeoDevice*, int*)> icsneoFindNeoDevicesNewStyle(lib, "icsneoFindNeoDevicesNewStyle");
if (!icsneoFindNeoDevicesNewStyle(device_type_list, device_type_list_size, devices, &count))
{
Py_BLOCK_THREADS
if (device_type_list)
delete[] device_type_list;
return set_ics_exception(exception_runtime_error(), "icsneoFindNeoDevicesNewStyle() Failed");
}
}
catch (ice::Exception& ex)
{
// Use the old style, we are using an old DLL
if (device_type_list) {
delete[] device_type_list;
device_type_list = NULL;
}
ice::Function<int __stdcall (unsigned long, NeoDevice*, int*)> icsneoFindNeoDevices(lib, "icsneoFindNeoDevices");
if (ex_options == -1 && !icsneoFindNeoDevices(legacy_dev_type, devices, &count)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoFindNeoDevices() Failed");
}
if (ex_options != -1) {
ice::Function<int __stdcall (unsigned long, NeoDevice*, int*, const POptionsFindNeoEx)> icsneoFindNeoDevicesEx(lib, "icsneoFindNeoDevicesEx");
OptionsFindNeoEx opts = {0};
opts.CANOptions.iNetworkID = ex_options;
if (!icsneoFindNeoDevicesEx(legacy_dev_type, devices, &count, &opts)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoFindNeoDevicesEx() Failed");
}
}
}
if (device_type_list)
delete[] device_type_list;
Py_END_ALLOW_THREADS
#else
ice::Function<int __stdcall (unsigned long, NeoDevice*, int*)> icsneoFindNeoDevices(lib, "icsneoFindNeoDevices");
Py_BEGIN_ALLOW_THREADS
if (ex_options == -1 && !icsneoFindNeoDevices(legacy_dev_type, devices, &count)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoFindNeoDevices() Failed");
}
if (ex_options != -1) {
ice::Function<int __stdcall (unsigned long, NeoDevice*, int*, const POptionsFindNeoEx)> icsneoFindNeoDevicesEx(lib, "icsneoFindNeoDevicesEx");
OptionsFindNeoEx opts = {0};
opts.CANOptions.iNetworkID = ex_options;
if (!icsneoFindNeoDevicesEx(legacy_dev_type, devices, &count, &opts)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoFindNeoDevicesEx() Failed");
}
}
Py_END_ALLOW_THREADS
#endif
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
//PyObject* list_object = PyList_New(count);
PyObject* tuple = PyTuple_New(count);
if (!tuple) {
return NULL;
}
for (int i=0; i < count; ++i) {
PyObject* obj = PyObject_CallObject((PyObject*)&neo_device_object_type, NULL);
if (!obj) {
// This should only happen if we run out of memory (malloc failure)?
PyErr_Print();
return set_ics_exception(exception_runtime_error(), "Failed to allocate " NEO_DEVICE_OBJECT_NAME);
}
// Copy the NeoDevice struct into our python NeoDevice object
neo_device_object* neo_device = (neo_device_object*)obj;
neo_device->dev = devices[i];
neo_device->name = PyUnicode_FromString(neodevice_to_string(devices[i].DeviceType));
//PyList_Append(list_object, neo_device);
PyTuple_SetItem(tuple, i, obj);
}
return tuple;
}
PyObject* meth_open_device(PyObject* self, PyObject* args, PyObject* keywords)
{
/*
unsigned char * bNetworkIDs,
int bConfigRead,
int iOptions
*/
//int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...)
unsigned long serial_number = 0;
PyObject* obj = NULL;
PyObject* bNetworkIDs = NULL;
int bConfigRead = 0;
int iOptions = 0;
int ex_options = -1;
char* kwords[] = { "device", "bNetworkIDs", "bConfigRead", "iOptions", "stOptionsOpenNeoEx", NULL };
if (!PyArg_ParseTupleAndKeywords(args, keywords, arg_parse("|OOiii:", __FUNCTION__), kwords, &obj, &bNetworkIDs,
&bConfigRead, &iOptions, &ex_options)) {
return NULL;
}
// if (!PyArg_ParseTupleAndKeywords(args, keywords, arg_parse("|bbbbbbbiiii:", __FUNCTION__), kwords, &relay1, &relay2,
// &relay3, &relay4, &relay5, &led5, &led6, &msb, &lsb, &analog, &relay_timeout)) {
// Argument Parsing
bool use_user_neo_device = false;
if (obj && PyLong_CheckExact(obj)) {
serial_number = PyLong_AsLong(obj);
} else if (serial_number == 0 && obj && !PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument is not of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
} else if (obj && PyNeoDevice_CheckExact(obj)) {
// User supplied us with a ics.NeoDevice object, so lets use it.
use_user_neo_device = true;
}
unsigned char net_ids[NETID_MAX] = {0};
if (bNetworkIDs && PyTuple_CheckExact(bNetworkIDs)) {
for (int i=0; i < PyTuple_Size(bNetworkIDs); ++i) {
long id = PyLong_AsLong(PyTuple_GetItem(bNetworkIDs, i));
net_ids[i] = (unsigned char)id;
}
} else if (!bNetworkIDs) {
for (int i=0; i < NETID_MAX; ++i) {
net_ids[i] = i;
}
}
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
bool use_new_finder = false;
try
{
//int _stdcall icsneoFindNeoDevicesNewStyle(unsigned int* deviceTypes, unsigned int numDeviceTypes, NeoDevice* pNeoDevice, int* pNumDevices)
ice::Function<int __stdcall (unsigned int*, unsigned int, NeoDevice*, int*)> icsneoFindNeoDevicesNewStyle(lib, "icsneoFindNeoDevicesNewStyle");
use_new_finder = true;
} catch (ice::Exception& ex)
{
use_new_finder = false;
}
ice::Function<int __stdcall (unsigned long, NeoDevice*, int*)> icsneoFindNeoDevices(lib, "icsneoFindNeoDevices");
ice::Function<int __stdcall (NeoDevice*, ICS_HANDLE*, unsigned char*, int, int)> icsneoOpenNeoDevice(lib, "icsneoOpenNeoDevice");
if (use_user_neo_device) {
neo_device_object* neo_device = PyNeoDevice_GetNeoDevice(obj);
ICS_HANDLE handle = PyNeoDevice_GetHandle(neo_device);
Py_BEGIN_ALLOW_THREADS
if (ex_options == -1 && !icsneoOpenNeoDevice(&PyNeoDevice_GetNeoDevice(obj)->dev, &handle, (bNetworkIDs ? net_ids : NULL), bConfigRead, iOptions)) {
Py_BLOCK_THREADS
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to open device");
}
if (ex_options != -1) {
ice::Function<int __stdcall (NeoDevice*, ICS_HANDLE*, unsigned char*, int, int, POptionsOpenNeoEx)> icsneoOpenNeoDeviceEx(lib, "icsneoOpenNeoDeviceEx");
OptionsOpenNeoEx opts = {0};
opts.CANOptions.iNetworkID = ex_options;
if (!icsneoOpenNeoDeviceEx(&PyNeoDevice_GetNeoDevice(obj)->dev, &handle, (bNetworkIDs ? net_ids : NULL), bConfigRead, iOptions, &opts)) {
Py_BLOCK_THREADS
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to open device");
}
}
PyNeoDevice_SetHandle(obj, handle);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
} else {
// We need to find the device and create the ics.NeoDevice object our self
PyObject* device = PyObject_CallObject((PyObject*)&neo_device_object_type, NULL);
if (!device) {
// This should only happen if we run out of memory (malloc failure)?
PyErr_Print();
return set_ics_exception(exception_runtime_error(), "Failed to allocate " NEO_DEVICE_OBJECT_NAME);
}
NeoDevice devices[255];
int count = 255;
unsigned long device_type = -1;
Py_BEGIN_ALLOW_THREADS
// Get a list of devices to find serial number
if (use_new_finder && ex_options == -1) {
//int _stdcall icsneoFindNeoDevicesNewStyle(unsigned int* deviceTypes, unsigned int numDeviceTypes, NeoDevice* pNeoDevice, int* pNumDevices)
ice::Function<int __stdcall (unsigned int*, unsigned int, NeoDevice*, int*)> icsneoFindNeoDevicesNewStyle(lib, "icsneoFindNeoDevicesNewStyle");
if (!icsneoFindNeoDevicesNewStyle(NULL, 0, devices, &count)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoFindNeoDevicesNewStyle() Failed");
}
} else {
// Use old find method
if (ex_options == -1 && !icsneoFindNeoDevices(device_type, devices, &count)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoFindNeoDevices() failed");
}
if (ex_options != -1) {
ice::Function<int __stdcall (unsigned long, NeoDevice*, int*, const POptionsFindNeoEx)> icsneoFindNeoDevicesEx(lib, "icsneoFindNeoDevicesEx");
OptionsFindNeoEx opts = {0};
opts.CANOptions.iNetworkID = ex_options;
if (!icsneoFindNeoDevicesEx(device_type, devices, &count, &opts)) {
Py_BLOCK_THREADS
return set_ics_exception_dev(exception_runtime_error(), obj, "icsneoFindNeoDevicesEx() failed");
}
}
}
Py_END_ALLOW_THREADS
// Find the serial number
for (int i=0; i < count; ++i) {
// Grab the first device we find and return it
if (serial_number == 0 && devices[i].NumberOfClients == 0) {
PyNeoDevice_GetNeoDevice(device)->dev = devices[i];
PyNeoDevice_GetNeoDevice(device)->name = PyUnicode_FromString(neodevice_to_string(devices[i].DeviceType));
ICS_HANDLE handle = 0;
Py_BEGIN_ALLOW_THREADS
if (ex_options == -1 && !icsneoOpenNeoDevice(&PyNeoDevice_GetNeoDevice(device)->dev, &handle, (bNetworkIDs ? net_ids : NULL), bConfigRead, iOptions)) {
Py_BLOCK_THREADS
return set_ics_exception_dev(exception_runtime_error(), device, "Failed to open device");
}
if (ex_options != -1) {
ice::Function<int __stdcall (NeoDevice*, ICS_HANDLE*, unsigned char*, int, int, POptionsOpenNeoEx)> icsneoOpenNeoDeviceEx(lib, "icsneoOpenNeoDeviceEx");
OptionsOpenNeoEx opts = {0};
opts.CANOptions.iNetworkID = ex_options;
if (!icsneoOpenNeoDeviceEx(&PyNeoDevice_GetNeoDevice(obj)->dev, &handle, (bNetworkIDs ? net_ids : NULL), bConfigRead, iOptions, &opts)) {
Py_BLOCK_THREADS
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to open device");
}
}
PyNeoDevice_SetHandle(device, handle);
Py_END_ALLOW_THREADS
return device;
} else if (devices[i].SerialNumber == serial_number) {
PyNeoDevice_GetNeoDevice(device)->dev = devices[i];
PyNeoDevice_GetNeoDevice(device)->name = PyUnicode_FromString(neodevice_to_string(devices[i].DeviceType));
ICS_HANDLE handle = 0;
Py_BEGIN_ALLOW_THREADS
if (ex_options == -1 && !icsneoOpenNeoDevice(&PyNeoDevice_GetNeoDevice(device)->dev, &handle, (bNetworkIDs ? net_ids : NULL), bConfigRead, iOptions)) {
Py_BLOCK_THREADS
return set_ics_exception_dev(exception_runtime_error(), device, "Failed to open device");
}
if (ex_options != -1) {
ice::Function<int __stdcall (NeoDevice*, ICS_HANDLE*, unsigned char*, int, int, POptionsOpenNeoEx)> icsneoOpenNeoDeviceEx(lib, "icsneoOpenNeoDeviceEx");
OptionsOpenNeoEx opts = {0};
opts.CANOptions.iNetworkID = ex_options;
if (!icsneoOpenNeoDeviceEx(&PyNeoDevice_GetNeoDevice(obj)->dev, &handle, (bNetworkIDs ? net_ids : NULL), bConfigRead, iOptions, &opts)) {
Py_BLOCK_THREADS
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to open device");
}
}
PyNeoDevice_SetHandle(device, handle);
Py_END_ALLOW_THREADS
return device;
}
}
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to find a free device to open");
}
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_close_device(PyObject* self, PyObject* args)
{
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("O:", __FUNCTION__), &obj)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, int*)> icsneoClosePort(lib, "icsneoClosePort");
ice::Function<int __stdcall (ICS_HANDLE)> icsneoFreeObject(lib, "icsneoFreeObject");
int error_count = 0;
Py_BEGIN_ALLOW_THREADS
if (!icsneoClosePort(PyNeoDevice_GetNeoDevice(obj)->handle, &error_count)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoClosePort() Failed");
}
icsneoFreeObject(PyNeoDevice_GetNeoDevice(obj)->handle);
Py_END_ALLOW_THREADS
PyNeoDevice_GetNeoDevice(obj)->handle = 0;
return Py_BuildValue("i", error_count);
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
/*
* Converts tm structure to second resolution.
* Returns seconds elapsed since the beginning of the month.
*/
__int64 _tm_secs(tm* t)
{
__int64 result = -1;
if (!t)
return result;
result = t->tm_mday*86400;
result += t->tm_hour*3600;
result += t->tm_min*60;
result += t->tm_sec;
return result;
}
/*
* Calculates the offset (in seconds) between two tm structures.
* Returns -1 on NULL parameters, -2 if month and year are not
* the same. Order of t1 and t2 doesn't matter as the difference
* will always be positive.
* Returns the difference of the two in seconds.
*/
__int64 _tm_secs_offset(tm* t1, tm* t2)
{
__int64 result = -1;
__int64 r1 = _tm_secs(t1);
__int64 r2 = _tm_secs(t2);
if (r1 == -1 || r2 == -1)
return result;
if (t1->tm_year != t2->tm_year ||
t1->tm_mon != t1->tm_mon)
return -2;
if ((result = r1 - r2) < 0)
result *= -1;
return result;
}
PyObject* meth_get_rtc(PyObject* self, PyObject* args)
{
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("O:", __FUNCTION__), &obj)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetHandle(obj);
icsSpyTime ics_time;
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, icsSpyTime*)> icsneoGetRTC(lib, "icsneoGetRTC");
Py_BEGIN_ALLOW_THREADS
if (!icsneoGetRTC(handle, &ics_time)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoGetRTC() Failed");
}
Py_END_ALLOW_THREADS
time_t current_time = time(0);
// Bug #6600 - icsneoSetRTC is utc, icsneoGetRTC is local
//tm* current_utc_time = gmtime(¤t_time); // UTC
tm* current_utc_time = localtime(¤t_time); // Local
tm device_utc_time;
device_utc_time.tm_hour = ics_time.hour;
device_utc_time.tm_min = ics_time.min;
device_utc_time.tm_sec = ics_time.sec;
device_utc_time.tm_mon = ics_time.month-1;
device_utc_time.tm_mday = ics_time.day;
device_utc_time.tm_year = ics_time.year;
device_utc_time.tm_isdst = -1;
device_utc_time.tm_year += 100;
unsigned long offset = (unsigned long)_tm_secs_offset(&device_utc_time, current_utc_time);
PyDateTime_IMPORT;
if (!PyDateTimeAPI) {
return set_ics_exception(exception_runtime_error(), "Failed to initialize PyDateTime");
}
PyObject* datetime = PyDateTime_FromDateAndTime(device_utc_time.tm_year+1900,
device_utc_time.tm_mon,
device_utc_time.tm_mday,
device_utc_time.tm_hour,
device_utc_time.tm_min,
device_utc_time.tm_sec,
0);
return Py_BuildValue("(O,i)", datetime, offset);
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_set_rtc(PyObject* self, PyObject* args)
{
PyObject* datetime_object = NULL;
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("O|O:", __FUNCTION__), &obj, &datetime_object)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetNeoDevice(obj)->handle;
PyDateTime_IMPORT;
if (!PyDateTimeAPI) {
return set_ics_exception(exception_runtime_error(), "Failed to initialize PyDateTime");
}
if (!datetime_object || !PyDateTime_Check(datetime_object)) {
// We didn't get a valid Python DateTime Object, use current UTC Time
time_t current_time = time(0);
tm* current_utc_time = gmtime(¤t_time);
datetime_object = PyDateTime_FromDateAndTime(current_utc_time->tm_year+1900,
current_utc_time->tm_mon+1,
current_utc_time->tm_mday,
current_utc_time->tm_hour,
current_utc_time->tm_min,
current_utc_time->tm_sec,
0);
}
// Convert Python DateTime object to icsSpyTime
icsSpyTime ics_time;
ics_time.sec = PyDateTime_DATE_GET_SECOND(datetime_object);
ics_time.min = PyDateTime_DATE_GET_MINUTE(datetime_object);
ics_time.hour = PyDateTime_DATE_GET_HOUR(datetime_object);
ics_time.day = PyDateTime_GET_DAY(datetime_object);
ics_time.month = PyDateTime_GET_MONTH(datetime_object);
ics_time.year = PyDateTime_GET_YEAR(datetime_object) % 100;
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, icsSpyTime*)> icsneoSetRTC(lib, "icsneoSetRTC");
Py_BEGIN_ALLOW_THREADS
if (!icsneoSetRTC(handle, &ics_time)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoSetRTC() Failed");
}
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_coremini_load(PyObject* self, PyObject* args)
{
PyObject* arg_data = NULL;
int location;
PyObject* obj = NULL;
bool isFileData = false;
if (!PyArg_ParseTuple(args, arg_parse("OOi:", __FUNCTION__), &obj, &arg_data, &location)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetNeoDevice(obj)->handle;
long fsize;
unsigned char* data = NULL;
int data_size = 0;
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_CheckExact(arg_data)) {
char* file_name = PyUniStr_AsStrOrUTF8(arg_data);
#else
if (PyString_Check(arg_data)) {
char* file_name = PyUniStr_AsStrOrUTF8(arg_data);
#endif
if (!file_name) {
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to convert file path to string");
}
FILE* f;
f = fopen(file_name, "rb");
if (!f) {
std::stringstream ss;
ss << "Failed to open CoreMini script file: '" << file_name << "'. Please make sure path exists";
return set_ics_exception_dev(exception_runtime_error(), obj, (char*)ss.str().c_str());
}
fseek(f, 0, SEEK_END);
fsize = ftell(f);
rewind(f);
data = (unsigned char*) malloc(sizeof(char)*fsize);
data_size = (int)fread(data, 1, fsize, f);
fclose(f);
if (fsize != data_size) {
return set_ics_exception_dev(exception_runtime_error(), obj, "CoreMini binary file size mismatch");
}
} else if (PyTuple_CheckExact(arg_data)) {
Py_ssize_t tuple_size = PyTuple_Size(arg_data);
data = (unsigned char*) malloc(sizeof(char)*tuple_size);
// Move tuple data into array
for (int i=0; i < tuple_size; ++i) {
PyObject* value = PyTuple_GET_ITEM(arg_data, i);
if (!PyLong_CheckExact(value)) {
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to convert tuple data. Tuple data must be integer type");
}
data[i] = (unsigned char)PyLong_AsLong(PyTuple_GET_ITEM(arg_data, i));
}
fsize = tuple_size;
data_size = fsize;
} else {
return set_ics_exception_dev(exception_runtime_error(), obj, "Argument must be filepath or tuple");
}
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, const unsigned char*, unsigned long, int)> icsneoScriptLoad(lib, "icsneoScriptLoad");
Py_BEGIN_ALLOW_THREADS
if (!icsneoScriptLoad(handle, data, data_size, location)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoScriptLoad() Failed");
}
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_coremini_start(PyObject* self, PyObject* args)
{
int location;
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("Oi:", __FUNCTION__), &obj, &location)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetNeoDevice(obj)->handle;
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, int)> icsneoScriptStart(lib, "icsneoScriptStart");
Py_BEGIN_ALLOW_THREADS
if (!icsneoScriptStart(handle, location)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoScriptStart() Failed");
}
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_coremini_stop(PyObject* self, PyObject* args)
{
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("O:", __FUNCTION__), &obj)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetHandle(obj);
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE)> icsneoScriptStop(lib, "icsneoScriptStop");
Py_BEGIN_ALLOW_THREADS
if (! icsneoScriptStop(handle)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoScriptStop() Failed");
}
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_coremini_clear(PyObject* self, PyObject* args)
{
int location;
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("Oi:", __FUNCTION__), &obj, &location)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetHandle(obj);
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, int)> icsneoScriptClear(lib, "icsneoScriptClear");
Py_BEGIN_ALLOW_THREADS
if (!icsneoScriptClear(handle, location)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoScriptClear() Failed");
}
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_coremini_get_status(PyObject* self, PyObject* args)
{
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("O:", __FUNCTION__), &obj)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetNeoDevice(obj)->handle;
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, int*)> icsneoScriptGetScriptStatus(lib, "icsneoScriptGetScriptStatus");
int status = 0;
Py_BEGIN_ALLOW_THREADS
if (!icsneoScriptGetScriptStatus(handle, &status)) {
Py_BLOCK_THREADS
return set_ics_exception(exception_runtime_error(), "icsneoScriptClear() Failed");
}
Py_END_ALLOW_THREADS
return Py_BuildValue("b", status==1);
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_transmit_messages(PyObject* self, PyObject* args)
{
PyObject* temp;
bool created_tuple = false;
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("OO:", __FUNCTION__), &obj, &temp)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetHandle(obj);
PyObject* tuple = temp;
if (!PyTuple_CheckExact(temp)) {
tuple = Py_BuildValue("(O)", temp);
if (!tuple) {
return NULL;
}
created_tuple = true;
}
if (!PyTuple_CheckExact(tuple)) {
return set_ics_exception(exception_argument_error(), "Second argument must be of tuple type!");
}
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, icsSpyMessage*, int, int)> icsneoTxMessages(lib, "icsneoTxMessages");
const Py_ssize_t TUPLE_COUNT = PyTuple_Size(tuple);
icsSpyMessage** msgs = new icsSpyMessage*[TUPLE_COUNT]();
for (int i=0; i < TUPLE_COUNT; ++i) {
spy_message_object* obj = (spy_message_object*)PyTuple_GetItem(tuple, i);
if (!obj) {
if (created_tuple) {
Py_XDECREF(tuple);
}
delete[] msgs;
return set_ics_exception(exception_runtime_error(), "Tuple item must be of " MODULE_NAME "." SPY_MESSAGE_OBJECT_NAME);
}
msgs[i] = &(obj->msg);
}
Py_BEGIN_ALLOW_THREADS
for (int i=0; i < TUPLE_COUNT; ++i) {
if (!icsneoTxMessages(handle, msgs[i], msgs[i]->NetworkID, 1)) {
Py_BLOCK_THREADS
if (created_tuple) {
Py_XDECREF(tuple);
}
delete[] msgs;
return set_ics_exception(exception_runtime_error(), "icsneoTxMessages() Failed");
}
/*
if (msgs[i]->ExtraDataPtrEnabled && msgs[i]->ExtraDataPtr != NULL) {
delete msgs[i]->ExtraDataPtr;
msgs[i]->ExtraDataPtr = NULL;
msgs[i]->ExtraDataPtrEnabled = 0;
}
*/
}
Py_END_ALLOW_THREADS
if (created_tuple) {
Py_XDECREF(tuple);
}
delete[] msgs;
Py_RETURN_NONE;
}
catch (ice::Exception& ex)
{
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
}
return set_ics_exception(exception_runtime_error(), "This is a bug!");
}
PyObject* meth_get_messages(PyObject* self, PyObject* args)
{
//Py_RETURN_NONE;
double timeout = 0.1;
int use_j1850 = 0;
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, arg_parse("O|bd:", __FUNCTION__), &obj, &use_j1850, &timeout)) {
return NULL;
}
if (!PyNeoDevice_CheckExact(obj)) {
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
}
ICS_HANDLE handle = PyNeoDevice_GetHandle(obj);
// Convert timeout to ms
timeout *= 1000;
try
{
ice::Library* lib = dll_get_library();
if (!lib) {
char buffer[512];
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
}
ice::Function<int __stdcall (ICS_HANDLE, icsSpyMessage*, int*, int*)> icsneoGetMessages(lib, "icsneoGetMessages");
int count;
int errors = 0;
union SpyMessage {
icsSpyMessageJ1850 msg_j1850;
icsSpyMessage msg;
};
SpyMessage *msgs = PyMem_New(SpyMessage, 20000);
if (!msgs) {
// This should only happen if we run out of memory (malloc failure)?
PyErr_Print();
return set_ics_exception_dev(exception_runtime_error(), obj, "Failed to allocate " SPY_MESSAGE_OBJECT_NAME);
}
Py_BEGIN_ALLOW_THREADS
for (double i=timeout; i > 0; --i) {
count = 20000;
errors = 0;
if (!icsneoGetMessages(handle, (icsSpyMessage*)msgs, &count, &errors) && !errors) {
// We are going to try one more time just incase
count = 20000;
errors = 0;
if (!icsneoGetMessages(handle, (icsSpyMessage*)msgs, &count, &errors)) {