-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cpp
1054 lines (917 loc) · 32.9 KB
/
MainForm.cpp
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 <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <values.h>
#include <Registry.hpp>
#include "mbn.h"
#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//#pragma link "CSPIN"
#pragma link "cspin"
#pragma resource "*.dfm"
#define LF_APP 1
#define LF_CB 2
#define LF_OBJ 3
#define LF_ADR 4
#define NUMTYPE(type, dat) (\
(type) == MBN_DATATYPE_FLOAT ? (dat).Float :\
(type) == MBN_DATATYPE_SINT ? (float)(dat).SInt :\
(type) == MBN_DATATYPE_UINT || (type) == MBN_DATATYPE_STATE ? (float)(dat).UInt : MINFLOAT)
#define MMTYPE(t) (t == MBN_DATATYPE_OCTETS || t == MBN_DATATYPE_BITS ? MBN_DATATYPE_UINT : t)
TMain *Main;
TCriticalSection *lck;
struct mbn_node_info thisnode = {
0, 0,
"MambaNet Node Tester", "D&R Tester",
1, 24, 0,
};
//---------------------------------------------------------------------------
void addlog(int sec, char *fmt, ...) {
static char log[500], tmp[500];
va_list va;
va_start(va, fmt);
//lck->Enter();
sprintf(tmp, "[%s] %s", sec == LF_APP ? "app" : sec == LF_CB ? "cb " :
sec == LF_OBJ ? "obj" : "adr", fmt);
vsprintf(log, tmp, va);
if( sec == LF_APP && Main->chkLogApp->Checked
|| sec == LF_CB && Main->chkLogCb->Checked
|| sec == LF_OBJ && Main->chkLogObj->Checked
|| sec == LF_ADR && Main->chkLogAdr->Checked)
Main->memLog->Lines->Add(log);
Main->StatusBar->Panels->Items[0]->Text = log;
//lck->Leave();
va_end(va);
}
unsigned long hex2int(const char *hex, int len) {
int i;
unsigned long r = 0;
for(i=0; i<len; i++) {
r <<= 4;
if(hex[i] >= '0' && hex[i] <= '9')
r += hex[i]-'0';
else if(hex[i] >= 'a' && hex[i] <= 'f')
r += hex[i]-'a'+10;
else if(hex[i] >= 'A' && hex[i] <= 'F')
r += hex[i]-'A'+10;
}
return r;
}
char data2strdat[256];
char *data2str(unsigned char type, union mbn_data dat) {
char *d = data2strdat;
switch(type) {
case MBN_DATATYPE_NODATA: sprintf(d, "[no data]"); break;
case MBN_DATATYPE_UINT: sprintf(d, "%lu", dat.UInt); break;
case MBN_DATATYPE_SINT: sprintf(d, "%ld", dat.SInt); break;
case MBN_DATATYPE_STATE: sprintf(d, "0x%08lX", dat.State); break;
case MBN_DATATYPE_OCTETS: sprintf(d, "\"%s\"", dat.Octets); break;
case MBN_DATATYPE_FLOAT: sprintf(d, "%f", dat.Float); break;
case MBN_DATATYPE_BITS:
sprintf(d, "0x%02X%02X%02X%02X%02X%02X%02X%02X",
dat.Bits[0], dat.Bits[1], dat.Bits[2], dat.Bits[3],
dat.Bits[4], dat.Bits[5], dat.Bits[6], dat.Bits[7]);
break;
case MBN_DATATYPE_OBJINFO: sprintf(d, "[object info]"); break;
case MBN_DATATYPE_ERROR: sprintf(d, "ERROR:\"%s\"", dat.Error); break;
default: sprintf(d, "[unknown datatype]");
}
return d;
}
char *type2str(unsigned char type) {
switch(type) {
case MBN_DATATYPE_NODATA: return "-"; break;
case MBN_DATATYPE_UINT: return "UInt"; break;
case MBN_DATATYPE_SINT: return "SInt"; break;
case MBN_DATATYPE_STATE: return "State"; break;
case MBN_DATATYPE_OCTETS: return "Octets"; break;
case MBN_DATATYPE_FLOAT: return "Float"; break;
case MBN_DATATYPE_BITS: return "Bits"; break;
case MBN_DATATYPE_OBJINFO: return "Info"; break;
case MBN_DATATYPE_ERROR: return "Error"; break;
}
return "Unknown";
}
void mError(struct mbn_handler *mbn, int code, char *msg) {
addlog(LF_CB, "Error(%d): \"%s\")", code, msg);
}
void mOnlineStatus(struct mbn_handler *mbn, unsigned long addr, char valid) {
char tmp[9];
sprintf(tmp, "%08lX", addr);
lck->Enter();
Main->txtMyAddress->Text = tmp;
Main->validaddr = valid;
addlog(LF_CB, "Address status changed: %08lX (%d)", addr, valid);
lck->Leave();
}
void mAddressTableChange(struct mbn_handler *mbn, struct mbn_address_node *o, struct mbn_address_node *n) {
char nm[50];
struct mbn_address_node *cur;
TListItem *it;
lck->Enter();
cur = o == NULL ? n : o;
sprintf(nm, "%08lX (%04X:%04X:%04X)", cur->MambaNetAddr,
cur->ManufacturerID, cur->ProductID, cur->UniqueIDPerProduct);
if(!o && n) {
addlog(LF_CB, "New node: %s", nm);
Main->lvNodeList->Items->BeginUpdate();
it = Main->lvNodeList->Items->Add();
sprintf(nm, "%08lX", cur->MambaNetAddr);
it->Data = (void *)cur->MambaNetAddr;
it->Caption = nm;
sprintf(nm, "%04X:%04X:%04X", cur->ManufacturerID, cur->ProductID, cur->UniqueIDPerProduct);
it->SubItems->Add(nm);
sprintf(nm, "%08lX", cur->EngineAddr);
it->SubItems->Add(nm);
sprintf(nm, "%02X", cur->Services);
it->SubItems->Add(nm);
it->SubItems->Add("-");
it->SubItems->Add("-");
Main->lvNodeList->Items->EndUpdate();
if(Main->cbListFetch->Checked) {
mbnGetActuatorData(mbn, cur->MambaNetAddr, MBN_NODEOBJ_NAME, 1);
mbnGetSensorData(mbn, cur->MambaNetAddr, MBN_NODEOBJ_HWPARENT, 1);
}
} else if(o && !n) {
addlog(LF_CB, "Node timeout: %s", nm);
it = Main->lvNodeList->FindData(0, (void *)cur->MambaNetAddr, true, false);
if(it != NULL)
it->Delete();
}
lck->Leave();
}
int mActuatorDataResponse(struct mbn_handler *mbn, struct mbn_message *msg, unsigned short object, unsigned char type, union mbn_data dat) {
int r = 0;
TListItem *it;
bool t;
addlog(LF_CB, "Actuator Data Response from %08lX object %d (type %d), data %s",
msg->AddressFrom, object, type, data2str(type, dat));
if(object == MBN_NODEOBJ_NAME) {
lck->Enter();
it = Main->lvNodeList->FindData(0, (void *)msg->AddressFrom, true, false);
if(it != NULL)
it->SubItems->Strings[4] = (char *)dat.Octets;
lck->Leave();
}
if(msg->AddressFrom != Main->fromaddr)
return 1;
lck->Enter();
switch(object) {
case MBN_NODEOBJ_NAME:
Main->lblNodeName->Caption = (char *)dat.Octets;
break;
default:
if(object == Main->objnr) {
Main->lblObjActuator->Caption = data2str(type, dat);
Main->txtObjStrAct->Text = data2str(type, dat);
} else
r = 1;
}
lck->Leave();
return r;
}
int mSensorDataResponse(struct mbn_handler *mbn, struct mbn_message *msg, unsigned short object, unsigned char type, union mbn_data dat) {
char tmp[100];
int i, r = 0;
TListItem *it;
addlog(LF_CB, "Sensor Data Response from %08lX object %d (type %d), data %s",
msg->AddressFrom, object, type, data2str(type, dat));
if(object == MBN_NODEOBJ_HWPARENT) {
lck->Enter();
sprintf(tmp, "%02X%02X:%02X%02X:%02X%02X", dat.Octets[0], dat.Octets[1],
dat.Octets[2], dat.Octets[3], dat.Octets[4], dat.Octets[5], dat.Octets[6]);
it = Main->lvNodeList->FindData(0, (void *)msg->AddressFrom, true, false);
if(it != NULL)
it->SubItems->Strings[3] = tmp;
lck->Leave();
}
if(msg->AddressFrom != Main->fromaddr)
return 1;
lck->Enter();
switch(object) {
case MBN_NODEOBJ_DESCRIPTION:
Main->lblNodeDesc->Caption = (char *)dat.Octets;
break;
case MBN_NODEOBJ_HWMAJOR:
Main->lblNodeHWMa->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_HWMINOR:
Main->lblNodeHWMi->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_FWMAJOR:
Main->lblNodeFWMa->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_FWMINOR:
Main->lblNodeFWMi->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_FPGAMAJOR:
Main->lblNodeFPMa->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_FPGAMINOR:
Main->lblNodeFPMi->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_PROTOMAJOR:
Main->lblNodePRMa->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_PROTOMINOR:
Main->lblNodePRMi->Caption = data2str(type, dat);
break;
case MBN_NODEOBJ_HWPARENT:
Main->lblNodeHWPar->Caption = tmp;
break;
case MBN_NODEOBJ_SERVICEREQUEST:
Main->lblNodeSR->Caption = dat.State ? "Service requested" : "No service required";
break;
case MBN_NODEOBJ_NUMBEROFOBJECTS:
Main->lblObjCount->Caption = data2str(type, dat);
if(dat.UInt != 0) {
Main->objcount = dat.UInt;
sprintf(tmp, "0/%d", Main->objcount);
Main->lblObjStatus->Caption = tmp;
Main->btnObjRetry->Enabled = true;
for(i=0; i<dat.UInt; i++)
mbnGetObjectInformation(mbn, Main->fromaddr, 1024+i, 1);
}
break;
default:
if(object == Main->objnr)
Main->lblObjSensor->Caption = data2str(type, dat);
else
r = 1;
}
lck->Leave();
return r;
}
void mAcknowledgeTimeout(struct mbn_handler *mbn, struct mbn_message *a) {
addlog(LF_CB, "Acknowledge timeout for message #%d to %08lX", a->MessageID, a->AddressTo);
}
int mObjectInformationResponse(struct mbn_handler *mbn, struct mbn_message *msg, unsigned short obj, struct mbn_object *nfo) {
char tmp[30];
TListItem *it;
struct mbn_object *o;
int i;
if(nfo == NULL) {
addlog(LF_CB, "WTF!? nfo = NULL!?");
return 1;
}
addlog(LF_CB, "Object information response from %08lX object #%d", msg->AddressFrom, obj);
if(msg->AddressFrom != Main->fromaddr)
return 1;
if(obj < 1024) {
addlog(LF_APP, "Object %d out of bound!", obj);
return 1;
}
lck->Enter();
Main->lvObjects->Items->BeginUpdate();
it = NULL;
for(i=0; i<Main->lvObjects->Items->Count; i++) {
o = (struct mbn_object *)Main->lvObjects->Items->Item[i]->Data;
if(o->timeout == obj)
it = Main->lvObjects->Items->Item[i];
}
if(it == NULL) {
o = (struct mbn_object *)malloc(sizeof(struct mbn_object));
memcpy(o, nfo, sizeof(mbn_object));
o->timeout = obj;
it = Main->lvObjects->Items->Add();
it->Data = (void *)o;
sprintf(tmp, "%d/%d", ++Main->objget, Main->objcount);
Main->lblObjStatus->Caption = tmp;
if(Main->objget == Main->objcount)
Main->btnObjRetry->Enabled = false;
}
sprintf(tmp, "%6d", obj);
it->Caption = tmp;
sprintf(tmp, "%02X", nfo->Services);
it->SubItems->Add(tmp);
it->SubItems->Add((char *)nfo->Description);
if(nfo->SensorType != MBN_DATATYPE_NODATA) {
sprintf(tmp, "%d %s", nfo->SensorSize, type2str(nfo->SensorType));
it->SubItems->Add(tmp);
sprintf(tmp, "%.0f %.0f",
NUMTYPE(MMTYPE(nfo->SensorType), nfo->SensorMin),
NUMTYPE(MMTYPE(nfo->SensorType), nfo->SensorMax));
it->SubItems->Add(tmp);
} else {
it->SubItems->Add("-");
it->SubItems->Add("");
}
if(nfo->ActuatorType != MBN_DATATYPE_NODATA) {
sprintf(tmp, "%d %s", nfo->ActuatorSize, type2str(nfo->ActuatorType));
it->SubItems->Add(tmp);
sprintf(tmp, "%.0f %.0f %.0f",
NUMTYPE(MMTYPE(nfo->ActuatorType), nfo->ActuatorMin),
NUMTYPE(MMTYPE(nfo->ActuatorType), nfo->ActuatorMax),
NUMTYPE(MMTYPE(nfo->ActuatorType), nfo->ActuatorDefault));
it->SubItems->Add(tmp);
} else {
it->SubItems->Add("-");
it->SubItems->Add("");
}
Main->lvObjects->Items->EndUpdate();
lck->Leave();
return 0;
}
void mObjectError(struct mbn_handler *mbn, struct mbn_message *msg, unsigned short obj, char *err) {
addlog(LF_CB, "Object error from %08lX object #%d: %s", msg->AddressFrom, obj, err);
}
int mObjectFrequencyResponse(struct mbn_handler *mbn, struct mbn_message *msg, unsigned short obj, unsigned char freq) {
addlog(LF_CB, "Object frequency response from %08lX object #%d: %02X", msg->AddressFrom, obj, freq);
if(msg->AddressFrom != Main->fromaddr)
return 1;
lck->Enter();
if(obj == Main->objnr)
Main->cbObjFreq->ItemIndex = freq+1;
lck->Leave();
}
int mSensorDataChanged(struct mbn_handler *mbn, struct mbn_message *msg, unsigned short obj, unsigned char type, union mbn_data dat) {
addlog(LF_CB, "Sensor data changed from %08lX object %d: %s", msg->AddressFrom, obj, data2str(type, dat));
}
//---------------------------------------------------------------------------
__fastcall TMain::TMain(TComponent* Owner)
: TForm(Owner)
{
validaddr = 0;
mbn = NULL;
iflist = NULL;
lck = new TCriticalSection();
}
__fastcall TMain::~TMain()
{
if(iflist)
mbnEthernetIFFree(iflist);
if(mbn)
mbnFree(mbn);
mbn = NULL;
//delete lck;
}
//---------------------------------------------------------------------------
void __fastcall TMain::FormCreate(TObject *Sender)
{
char err[MBN_ERRSIZE], ifdesc[512];
struct mbn_if_ethernet *n;
if((iflist = mbnEthernetIFList(err)) == NULL)
addlog(LF_APP, "Error getting interface list: %s", err);
else {
lstInterfaces->Items->Clear();
for(n=iflist; n!=NULL; n=n->next) {
sprintf(ifdesc, "%02X:%02X:%02X:%02X:%02X:%02X",
n->addr[0], n->addr[1], n->addr[2], n->addr[3], n->addr[4], n->addr[5]);
lstInterfaces->Items->AddObject(ifdesc, (TObject *)n->name);
}
lstInterfaces->ItemIndex = 0;
}
TRegistry *reg = new TRegistry;
if(reg->OpenKey("\\Software\\MambaNet Tester", false) && reg->ValueExists("UniqueID"))
cseUniqueID->Value = reg->ReadInteger("UniqueID");
delete reg;
lvObjects->Columns->Items[0]->Width = 40;
lvObjects->Columns->Items[1]->Width = 25;
lvObjects->Columns->Items[2]->Width = 130;
lvObjects->Columns->Items[3]->Width = 60;
lvObjects->Columns->Items[4]->Width = 65;
lvObjects->Columns->Items[5]->Width = 60;
lvObjects->Columns->Items[6]->Width = 90;
lvObjects->Selected = NULL;
lvNodeList->Columns->Items[0]->Width = 60;
lvNodeList->Columns->Items[1]->Width = 95;
lvNodeList->Columns->Items[2]->Width = 60;
lvNodeList->Columns->Items[3]->Width = 25;
lvNodeList->Columns->Items[4]->Width = 95;
lvNodeList->Columns->Items[5]->Width = 130;
lvNodeList->Selected = NULL;
cbObjFreq->ItemIndex = -1;
btnObjStrAct->Visible = false;
txtObjStrAct->Visible = false;
StatusBar->Panels->Items[0]->Text = "Not connected";
}
//---------------------------------------------------------------------------
void __fastcall TMain::btnOpenClick(TObject *Sender) {
struct mbn_interface *itf;
char error[MBN_ERRSIZE];
switch (ConnectionPageControl->ActivePageIndex)
{
case 0:
{
if(lstInterfaces->ItemIndex < 0)
return addlog(LF_APP, "No device selected!");
if((itf = mbnEthernetOpen((char *)lstInterfaces->Items->Objects[lstInterfaces->ItemIndex], error)) == NULL)
return addlog(LF_APP, "EthernetOpen failed: %s", error);
}
break;
case 1:
{
char Temp[256];
char *url_found;
char *port_found;
char url[256];
char port[16];
strcpy(Temp, UDPEdit->Text.c_str());
url_found = strtok(Temp, ":");
port_found = strtok(NULL, ":");
if (url_found == NULL)
return addlog(LF_APP, "No URL given!");
strcpy(url, url_found);
sprintf(port, "34848");
if (port_found != NULL)
{
strcpy(port, port_found);
}
if((itf = mbnUDPOpen(url, port, NULL, error)) == NULL)
return addlog(LF_APP, "EthernetOpen failed: %s", error);
}
break;
case 2:
{
char Temp[256];
char *url_found;
char *port_found;
char url[256];
char port[16];
strcpy(Temp, TCPEdit->Text.c_str());
url_found = strtok(Temp, ":");
port_found = strtok(NULL, ":");
if (url_found == NULL)
return addlog(LF_APP, "No URL given!");
strcpy(url, url_found);
sprintf(port, "34848");
if (port_found != NULL)
{
strcpy(port, port_found);
}
if((itf = mbnTCPOpen(url, port, NULL, NULL, error)) == NULL)
return addlog(LF_APP, "EthernetOpen failed: %s", error);
}
break;
}
thisnode.UniqueIDPerProduct = cseUniqueID->Value;
if((mbn = mbnInit(&thisnode, NULL, itf, error)) == NULL)
return addlog(LF_APP, "Init failed: %s", error);
mbnSetErrorCallback(mbn, mError);
mbnSetOnlineStatusCallback(mbn, mOnlineStatus);
mbnSetAddressTableChangeCallback(mbn, mAddressTableChange);
mbnSetActuatorDataResponseCallback(mbn, mActuatorDataResponse);
mbnSetSensorDataResponseCallback(mbn, mSensorDataResponse);
mbnSetAcknowledgeTimeoutCallback(mbn, mAcknowledgeTimeout);
mbnSetObjectInformationResponseCallback(mbn, mObjectInformationResponse);
mbnSetObjectErrorCallback(mbn, mObjectError);
mbnSetObjectFrequencyResponseCallback(mbn, mObjectFrequencyResponse);
mbnSetSensorDataChangedCallback(mbn, mSensorDataChanged);
addlog(LF_APP, "Created MambaNet node, starting interface");
mbnStartInterface(itf, error);
lstInterfaces->Enabled = false;
UDPEdit->Enabled = false;
TCPEdit->Enabled = false;
cseUniqueID->Enabled = false;
btnOpen->Enabled = false;
btnClose->Enabled = true;
mbnSendPingRequest(mbn, MBN_BROADCAST_ADDRESS);
}
//---------------------------------------------------------------------------
void __fastcall TMain::btnCloseClick(TObject *Sender) {
lck->Enter();
lvNodeList->Items->Clear();
lstInterfaces->Enabled = true;
UDPEdit->Enabled = true;
TCPEdit->Enabled = true;
cseUniqueID->Enabled = true;
btnOpen->Enabled = true;
btnClose->Enabled = false;
if(mbn)
mbnFree(mbn);
mbn = NULL;
validaddr = 0;
lck->Leave();
addlog(LF_APP, "Closed MambaNet node");
}
//---------------------------------------------------------------------------
void __fastcall TMain::mbnPingAllClick(TObject *Sender) {
if(!mbn)
return addlog(LF_APP, "Not connected!");
mbnSendPingRequest(mbn, MBN_BROADCAST_ADDRESS);
}
//---------------------------------------------------------------------------
void __fastcall TMain::btnLogClearClick(TObject *Sender) {
lck->Enter();
memLog->Lines->Clear();
lck->Leave();
}
//---------------------------------------------------------------------------
void __fastcall TMain::cbListFetchClick(TObject *Sender)
{
int i;
TListItem *it;
lck->Enter();
if(!cbListFetch->Checked) {
lck->Leave();
return;
}
for(i=0; i<lvNodeList->Items->Count; i++) {
it = lvNodeList->Items->Item[i];
if(it->SubItems->Strings[3].AnsiCompare("-") == 0)
mbnGetSensorData(mbn, (unsigned long)it->Data, MBN_NODEOBJ_HWPARENT, 1);
if(it->SubItems->Strings[4].AnsiCompare("-") == 0)
mbnGetActuatorData(mbn, (unsigned long)it->Data, MBN_NODEOBJ_NAME, 1);
}
lck->Leave();
}
//---------------------------------------------------------------------------
void __fastcall TMain::lvNodeListDblClick(TObject *Sender)
{
char tmp[100], ack;
int n;
lck->Enter();
if(lvNodeList->Selected == NULL) {
lck->Leave();
return;
}
pcTabs->ActivePageIndex = 1;
if(!validaddr) {
lck->Leave();
return addlog(LF_APP, "We don't have a valid address yet!");
}
fromaddr = (unsigned long)lvNodeList->Selected->Data;
lblNodeName->Caption = "-";
lblNodeDesc->Caption = "-";
lblNodeAddr->Caption = lvNodeList->Selected->Caption;
lblNodeID->Caption = lvNodeList->Selected->SubItems->Strings[0];
lblNodeHWMa->Caption = "-";
lblNodeHWMi->Caption = "-";
lblNodeFWMa->Caption = "-";
lblNodeFWMi->Caption = "-";
lblNodeFPMa->Caption = "-";
lblNodeFPMi->Caption = "-";
lblNodePRMa->Caption = "-";
lblNodePRMi->Caption = "-";
lblNodeHWPar->Caption = "-";
lblNodeSR->Caption = "-";
lblObjCount->Caption = "-";
lblNodeEngine->Caption = lvNodeList->Selected->SubItems->Strings[1];
lvObjects->Items->BeginUpdate();
lvObjects->Items->Clear();
lvObjects->Items->EndUpdate();
lblObjStatus->Caption = "0/0";
btnObjRetry->Enabled = false;
objnr = objcount = objget = 0;
lck->Leave();
ack = 1;
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_DESCRIPTION, ack);
mbnGetActuatorData(mbn, fromaddr, MBN_NODEOBJ_NAME, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_HWMAJOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_HWMINOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_FWMAJOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_FWMINOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_FPGAMAJOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_FPGAMINOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_PROTOMAJOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_PROTOMINOR, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_HWPARENT, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_SERVICEREQUEST, ack);
mbnGetSensorData(mbn, fromaddr, MBN_NODEOBJ_NUMBEROFOBJECTS, ack);
}
//---------------------------------------------------------------------------
void __fastcall TMain::lvObjectsSelectItem(TObject *Sender,
TListItem *Item, bool Selected)
{
TListItem *l;
struct mbn_object *nfo, *o;
TItemStates sel = TItemStates() << isSelected;
double min, max;
char tmp[50];
int same = 1;
lck->Enter();
lblObjSensor->Caption = "-";
lblObjActuator->Caption = "-";
cbObjFreq->ItemIndex = 0;
btnObjStrAct->Visible = false;
txtObjStrAct->Visible = false;
btnObjRetry->Visible = true;
lblObjStatus->Visible = true;
objnr = 0;
if(lvObjects->SelCount < 1 || Item == NULL) {
lck->Leave();
return;
}
nfo = (struct mbn_object *)Item->Data;
if(nfo == NULL) {
lck->Leave();
return;
}
if(lvObjects->SelCount == 1 && Selected) {
objnr = nfo->timeout;
if(nfo->SensorType != MBN_DATATYPE_NODATA)
mbnGetSensorData(mbn, fromaddr, objnr, 1);
if(nfo->ActuatorType != MBN_DATATYPE_NODATA)
mbnGetActuatorData(mbn, fromaddr, objnr, 1);
mbnGetObjectFrequency(mbn, fromaddr, objnr, 1);
}
/* make sure all selected actuators are of the same type */
min = NUMTYPE(MMTYPE(nfo->ActuatorType), nfo->ActuatorMin);
max = NUMTYPE(MMTYPE(nfo->ActuatorType), nfo->ActuatorMax);
if(nfo->ActuatorType != MBN_DATATYPE_NODATA) {
for(l=lvObjects->Selected; l!=NULL; l=lvObjects->GetNextItem(l, sdAll, sel)) {
o = (struct mbn_object *)l->Data;
if(o->ActuatorType != nfo->ActuatorType
|| (__int64)(NUMTYPE(MMTYPE(o->ActuatorType), o->ActuatorMax)*100) != (__int64)(max*100)
|| (__int64)(NUMTYPE(MMTYPE(o->ActuatorType), o->ActuatorMin)*100) != (__int64)(min*100)) {
same = 0;
break;
}
}
if(same) {
btnObjRetry->Visible = false;
lblObjStatus->Visible = false;
btnObjStrAct->Visible = true;
txtObjStrAct->Text = "";
txtObjStrAct->Visible = true;
}
}
lck->Leave();
}
//---------------------------------------------------------------------------
void __fastcall TMain::cbObjFreqChange(TObject *Sender)
{
TListItem *l;
struct mbn_object *nfo;
TItemStates sel = TItemStates() << isSelected;
if(cbObjFreq->ItemIndex < 1)
return;
if(lvObjects->SelCount < 1)
return addlog(LF_APP, "Nothing selected!");
lck->Enter();
for(l=lvObjects->Selected; l!=NULL; l=lvObjects->GetNextItem(l, sdAll, sel)) {
nfo = (struct mbn_object *)l->Data;
addlog(LF_APP, "Setting object frequency of %08lX object #%d to %02X", fromaddr, nfo->timeout, cbObjFreq->ItemIndex-1);
mbnSetObjectFrequency(mbn, fromaddr, nfo->timeout, cbObjFreq->ItemIndex-1, 1);
}
lck->Leave();
}
//---------------------------------------------------------------------------
void __fastcall TMain::lvObjectsDeletion(TObject *Sender, TListItem *Item)
{
if(Item->Data != NULL) {
free(Item->Data);
Item->Data = NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TMain::btnObjStrActClick(TObject *Sender)
{
TItemStates sel = TItemStates() << isSelected;
TListItem *l;
union mbn_data dat;
struct mbn_object *nfo;
unsigned char *str[100];
unsigned char ObjectData[64];
char *tmp;
char cnt;
lck->Enter();
if(txtObjStrAct->Visible == false) {
lck->Leave();
return;
}
if(lvObjects->SelCount < 1) {
addlog(LF_APP, "Nothing selected!");
lck->Leave();
return;
}
for(l=lvObjects->Selected; l!=NULL; l=lvObjects->GetNextItem(l, sdAll, sel)) {
nfo = (struct mbn_object *)l->Data;
switch(nfo->ActuatorType) {
case MBN_DATATYPE_UINT:
case MBN_DATATYPE_STATE:
if(txtObjStrAct->Text.Pos("0x") == 1) {
tmp = txtObjStrAct->Text.c_str();
dat.UInt = hex2int(tmp+2, strlen(tmp)-2);
} else
dat.UInt = (unsigned long) txtObjStrAct->Text.ToDouble();
break;
case MBN_DATATYPE_FLOAT: dat.Float = (float) txtObjStrAct->Text.ToDouble(); break;
case MBN_DATATYPE_SINT: dat.SInt = txtObjStrAct->Text.ToInt(); break;
case MBN_DATATYPE_OCTETS:
memset((void *)str, 0, 100);
strncpy((char *)str, txtObjStrAct->Text.c_str(), 100);
dat.Octets = (unsigned char *)str;
bool ArrayOfIntegersFound = false;
int cntSeperator=0;
for (cnt=0; cnt<strlen((char *)str); cnt++) {
if (((char *)str)[cnt] == '|') {
cntSeperator++;
}
}
if (cntSeperator == nfo->ActuatorSize-1) { //Try to decode array of integers.
int FirstChar = 0;
int cntObjectData = 0;
int TextSize = strlen((char *)str);
for (cnt=0; cnt<TextSize; cnt++) {
if (((char *)str)[cnt] == '|') {
((char *)str)[cnt] = 0x00;
try {
AnsiString Value = &((char *)str)[FirstChar];
int IntegerData = Value.ToInt();
ObjectData[cntObjectData++] = IntegerData&0xFF;
FirstChar = cnt+1;
} catch (Exception &exception) {
}
}
if (cnt==(TextSize-1)) { //Last value
AnsiString Value = &((char *)str)[FirstChar];
int IntegerData = Value.ToInt();
ObjectData[cntObjectData++] = IntegerData&0xFF;
}
}
if (cntObjectData == nfo->ActuatorSize)
{
ArrayOfIntegersFound = true;
}
}
if (ArrayOfIntegersFound)
{
dat.Octets = (unsigned char *)ObjectData;
}
break;
}
addlog(LF_APP, "Setting actuator data of %08lX object #%d to %s", fromaddr, nfo->timeout, data2str(nfo->ActuatorType, dat));
mbnSetActuatorData(mbn, fromaddr, nfo->timeout, nfo->ActuatorType, nfo->ActuatorSize, dat, 1);
}
lblObjActuator->Caption = data2str(nfo->ActuatorType, dat);
lck->Leave();
}
//---------------------------------------------------------------------------
void __fastcall TMain::btnObjRetryClick(TObject *Sender)
{
TListItem *it;
struct mbn_object *nfo;
int i, j, last;
lck->Enter();
last = 1023;
for(i=0; i<lvObjects->Items->Count; i++) {
nfo = (struct mbn_object *)lvObjects->Items->Item[i]->Data;
for(j=last+1;j<nfo->timeout;j++)
mbnGetObjectInformation(mbn, fromaddr, j, 0);
last = nfo->timeout;
}
lck->Leave();
}
//---------------------------------------------------------------------------
void __fastcall TMain::cseUniqueIDChange(TObject *Sender)
{
TRegistry *reg = new TRegistry;
if(reg->OpenKey("\\Software\\MambaNet Tester", true))
reg->WriteInteger("UniqueID", cseUniqueID->Value);
delete reg;
}
//---------------------------------------------------------------------------
void __fastcall TMain::RefreshTimerTimer(TObject *Sender)
{
char err[MBN_ERRSIZE];
if(mbn && cbObjRefresh->Checked && lvObjects->SelCount >= 0 && pcTabs->ActivePageIndex == 1)
lvObjectsSelectItem(Sender, lvObjects->Selected, true);
if (mbn != NULL)
{
// if (mbnEthernetMIILinkStatus(mbn->itf, err))
// StatusBar->Panels->Items[0]->Text = "Link up";
// else
// StatusBar->Panels->Items[0]->Text = "Link down";
}
}
//---------------------------------------------------------------------------
void __fastcall TMain::lvNodeListCompare(TObject *Sender, TListItem *Item1,
TListItem *Item2, int Data, int &Compare)
{
AnsiString a, b;
int column = nodelistsort & ~0x80;
if(nodelistsort & 0x80) { /* reversed */
a = column ? Item2->SubItems->Strings[column-1] : Item2->Caption;
b = column ? Item1->SubItems->Strings[column-1] : Item1->Caption;
} else {
a = column ? Item1->SubItems->Strings[column-1] : Item1->Caption;
b = column ? Item2->SubItems->Strings[column-1] : Item2->Caption;
}
Compare = a.AnsiCompare(b);
}
void __fastcall TMain::lvNodeListColumnClick(TObject *Sender,
TListColumn *Column)
{
if((nodelistsort & ~0x80) == Column->Index) {
if(nodelistsort & 0x80)
nodelistsort -= 0x80;
else
nodelistsort |= 0x80;
} else if(nodelistsort & 0x80)
nodelistsort -= 0x80;
nodelistsort = Column->Index | (nodelistsort & 0x80);
lvNodeList->CustomSort(NULL, 0);
}
//---------------------------------------------------------------------------
//make a 32 float representation.
//8 bit = seee mmmm
//16 bit = seee eemm mmmm mmmm
//32 bit = seee eeee emmm mmmm mmm mmmmm mmmm mmmm
unsigned char Float2VariableFloat(float InputFloat, unsigned char VariableFloatBufferSize, unsigned char *FloatBuffer)
{
unsigned long TemporyCastedFloat;
int exponent;
unsigned long mantessa;
char signbit;
unsigned char ReturnStatus;
ReturnStatus = 1;
TemporyCastedFloat = *((unsigned long *)&InputFloat);
mantessa = TemporyCastedFloat&0x007FFFFF;
exponent = (TemporyCastedFloat>>23)&0xFF;
exponent -= 127;
signbit = (TemporyCastedFloat>>(23+8))&0x01;
if (TemporyCastedFloat == 0x00000000)
{
unsigned char cntSize;
for (cntSize=0; cntSize<VariableFloatBufferSize; cntSize++)
{
FloatBuffer[cntSize] = 0x00;
}
ReturnStatus = 0;
}
else
{
switch (VariableFloatBufferSize)
{
case 1:
{
exponent += 3;
if (exponent<7)
{
if (exponent<0)
{
exponent = 0;
}
FloatBuffer[0] = signbit;
FloatBuffer[0] <<= 3;
FloatBuffer[0] |= (exponent&0x7);
FloatBuffer[0] <<= 4;
FloatBuffer[0] |= (mantessa>>19)&0x0F;
ReturnStatus = 0;
}
else if (exponent > 6)
{ //+/-INF (or NaN)
exponent = 7;
mantessa = 0;
FloatBuffer[0] = signbit;
FloatBuffer[0] <<= 3;
FloatBuffer[0] |= (exponent&0x7);
FloatBuffer[0] <<= 4;
FloatBuffer[0] |= (mantessa>>19)&0x0F;
ReturnStatus = 0;
}
}
break;
case 2: