forked from EIPStackGroup/OpENer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipcommon.c
More file actions
1625 lines (1372 loc) · 66.9 KB
/
Copy pathcipcommon.c
File metadata and controls
1625 lines (1372 loc) · 66.9 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
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
*
******************************************************************************/
#include <string.h>
#include <stdio.h>
#include "cipcommon.h"
#include "opener_user_conf.h"
#include "opener_api.h"
#include "cipidentity.h"
#include "ciptcpipinterface.h"
#include "cipethernetlink.h"
#include "cipconnectionmanager.h"
#include "endianconv.h"
#include "encap.h"
#include "ciperror.h"
#include "cipassembly.h"
#include "cipmessagerouter.h"
#if defined(OPENER_IS_DLR_DEVICE) && 0 != OPENER_IS_DLR_DEVICE
#include "cipdlr.h"
#endif
#include "cipqos.h"
#include "cpf.h"
#include "trace.h"
#include "appcontype.h"
#include "cipepath.h"
#include "stdlib.h"
#include "ciptypes.h"
#include "cipstring.h"
#if defined(CIP_FILE_OBJECT) && 0 != CIP_FILE_OBJECT
#include "OpENerFileObject/cipfile.h"
#endif
#if defined(CIP_SECURITY_OBJECTS) && 0 != CIP_SECURITY_OBJECTS
#include "SecurityObjects/CipSecurityObject/cipsecurity.h"
#include "SecurityObjects/EtherNetIPSecurityObject/ethernetipsecurity.h"
#include "SecurityObjects/CertificateManagementObject/certificatemanagement.h"
#endif
/* private functions*/
EipStatus CipStackInit(const EipUint16 unique_connection_id) {
/* The message router is the first CIP object be initialized!!! */
EipStatus eip_status = CipMessageRouterInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
eip_status = CipIdentityInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
eip_status = CipTcpIpInterfaceInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
eip_status = CipEthernetLinkInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
eip_status = ConnectionManagerInit(unique_connection_id);
OPENER_ASSERT(kEipStatusOk == eip_status);
eip_status = CipAssemblyInitialize();
OPENER_ASSERT(kEipStatusOk == eip_status);
#if defined(OPENER_IS_DLR_DEVICE) && 0 != OPENER_IS_DLR_DEVICE
eip_status = CipDlrInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
#endif
eip_status = CipQoSInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
#if defined(CIP_FILE_OBJECT) && 0 != CIP_FILE_OBJECT
eip_status = CipFileInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
#endif
#if defined(CIP_SECURITY_OBJECTS) && 0 != CIP_SECURITY_OBJECTS
eip_status = CipSecurityInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
eip_status = EIPSecurityInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
eip_status = CertificateManagementObjectInit();
OPENER_ASSERT(kEipStatusOk == eip_status);
#endif
/* the application has to be initialized at last */
eip_status = ApplicationInitialization();
OPENER_ASSERT(kEipStatusOk == eip_status);
return eip_status;
}
void ShutdownCipStack(void) {
/* First close all connections */
CloseAllConnections();
/* Than free the sockets of currently active encapsulation sessions */
EncapsulationShutDown();
/*clean the data needed for the assembly object's attribute 3*/
ShutdownAssemblies();
ShutdownTcpIpInterface();
/*no clear all the instances and classes */
DeleteAllClasses();
}
EipStatus NotifyClass(const CipClass *RESTRICT const cip_class,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response,
const struct sockaddr *originator_address,
const CipSessionHandle encapsulation_session) {
/* find the instance: if instNr==0, the class is addressed, else find the instance */
CipInstanceNum instance_number =
message_router_request->request_path.instance_number; /* get the instance number */
CipInstance *instance = GetCipInstance(cip_class, instance_number); /* look up the instance (note that if inst==0 this will be the class itself) */
if(instance) /* if instance is found */
{
OPENER_TRACE_INFO("notify: found instance %d%s\n",
instance_number,
instance_number == 0 ? " (class object)" : "");
CipServiceStruct *service = instance->cip_class->services; /* get pointer to array of services */
if(NULL != service) /* if services are defined */
{
for(size_t i = 0; i < instance->cip_class->number_of_services; i++) /* seach the services list */
{
if(message_router_request->service == service->service_number) /* if match is found */
{
/* call the service, and return what it returns */
OPENER_TRACE_INFO("notify: calling %s service\n", service->name);
OPENER_ASSERT(NULL != service->service_function);
return service->service_function(instance,
message_router_request,
message_router_response,
originator_address,
encapsulation_session);
} else {
service++;
}
}
} OPENER_TRACE_WARN(
"notify: service 0x%x not supported\n", message_router_request->service);
message_router_response->general_status = kCipErrorServiceNotSupported; /* if no services or service not found, return an error reply*/
} else {
OPENER_TRACE_WARN("notify: instance number %d unknown\n", instance_number);
/* if instance not found, return an error reply */
message_router_response->general_status = kCipErrorPathDestinationUnknown;
/* according to the test tool this is the correct error flag instead of CIP_ERROR_OBJECT_DOES_NOT_EXIST */
}
/* handle error replies*/
message_router_response->size_of_additional_status = 0; /* fill in the rest of the reply with not much of anything*/
InitializeENIPMessage(&message_router_response->message);
message_router_response->reply_service =
(0x80 | message_router_request->service); /* except the reply code is an echo of the command + the reply flag */
return kEipStatusOkSend;
}
CipUint GetMaxInstanceNumber(CipClass *RESTRICT const cip_class) {
CipUint max_instance = 0;
CipInstance *instance = cip_class->instances;
while (NULL != instance) { /* loop trough all instances of class */
if(instance->instance_number > max_instance) {
max_instance = instance->instance_number;
}
instance = instance->next;
}
return max_instance;
}
CipInstance *AddCipInstances(CipClass *RESTRICT const cip_class,
const CipInstanceNum number_of_instances) {
CipInstance **next_instance = NULL;
CipInstance *first_instance = NULL; /* Initialize to error result */
CipInstanceNum instance_number = 1; /* the first instance is number 1 */
int new_instances = 0;
OPENER_TRACE_INFO("adding %d instances to class %s\n",
number_of_instances,
cip_class->class_name);
/* Allocate and initialize all needed instances one by one. */
for(new_instances = 0; new_instances < number_of_instances; new_instances++) {
/* Find next free instance number */
CipBool found_free_number = false;
while (!found_free_number) {
next_instance = &cip_class->instances; /* set pointer to head of existing instances chain */
found_free_number = true; /* anticipate instance_number is not in use*/
/* loop through existing instances */
while (*next_instance) /* as long as what next_instance points to is not zero */
{
/* check if instance number in use */
if(instance_number == (*next_instance)->instance_number) {
found_free_number = false; /* instance number exists already */
break;
}
next_instance = &(*next_instance)->next; /* get next instance in instances chain*/
}
if(!found_free_number) {
instance_number++; /* try next instance_number and loop again through existing instances */
}
}
CipInstance *current_instance =
(CipInstance *) CipCalloc( 1, sizeof(CipInstance) );
OPENER_ASSERT(NULL != current_instance); /* fail if run out of memory */
if(NULL == current_instance) {
break;
}
if(NULL == first_instance) {
first_instance = current_instance; /* remember the first allocated instance */
}
current_instance->instance_number = instance_number; /* assign the next sequential instance number */
current_instance->cip_class = cip_class; /* point each instance to its class */
if(cip_class->number_of_attributes) /* if the class calls for instance attributes */
{ /* then allocate storage for the attribute array */
current_instance->attributes = (CipAttributeStruct *) CipCalloc(
cip_class->number_of_attributes,
sizeof(CipAttributeStruct) );
OPENER_ASSERT(NULL != current_instance->attributes);/* fail if run out of memory */
if(NULL == current_instance->attributes) {
break;
}
}
*next_instance = current_instance; /* link the previous pointer to this new node */
next_instance = ¤t_instance->next; /* update pp to point to the next link of the current node */
cip_class->number_of_instances += 1; /* update the total number of instances recorded by the class */
instance_number++; /* update to the number of the next node*/
}
cip_class->max_instance = GetMaxInstanceNumber(cip_class); /* update largest instance number (class Attribute 2) */
if(new_instances != number_of_instances) {
/* TODO: Free again all attributes and instances allocated so far in this call. */
OPENER_TRACE_ERR(
"ERROR: Allocated only %d instances of requested %d for class %s\n",
new_instances,
number_of_instances,
cip_class->class_name);
first_instance = NULL; /* failed to allocate all instances / attributes */
}
return first_instance;
}
CipInstance *AddCipInstance(CipClass *RESTRICT const cip_class,
const CipInstanceNum instance_id) {
CipInstance *instance = GetCipInstance(cip_class, instance_id);
if(NULL == instance) { /*we have no instance with given id*/
instance = AddCipInstances(cip_class, 1);
instance->instance_number = instance_id;
}
cip_class->max_instance = GetMaxInstanceNumber(cip_class); /* update largest instance number (class Attribute 2) */
return instance;
}
CipClass *CreateCipClass(const CipUdint class_code,
const int number_of_class_attributes,
const EipUint32 highest_class_attribute_number,
const int number_of_class_services,
const int number_of_instance_attributes,
const EipUint32 highest_instance_attribute_number,
const int number_of_instance_services,
const CipInstanceNum number_of_instances,
const char *const name,
const EipUint16 revision,
InitializeCipClass initializer) {
OPENER_TRACE_INFO("creating class '%s' with code: 0x%" PRIX32 "\n", name,
class_code);
OPENER_ASSERT( NULL == GetCipClass(class_code) ); /* check if an class with the ClassID already exists */
/* should never try to redefine a class*/
/* a metaClass is a class that holds the class attributes and services
CIP can talk to an instance, therefore an instance has a pointer to its class
CIP can talk to a class, therefore a class struct is a subclass of the instance struct,
and contains a pointer to a metaclass
CIP never explicitly addresses a metaclass*/
CipClass *const cip_class = (CipClass *) CipCalloc( 1, sizeof(CipClass) ); /* create the class object*/
CipClass *const meta_class = (CipClass *) CipCalloc( 1, sizeof(CipClass) ); /* create the metaclass object*/
/* initialize the class-specific fields of the Class struct*/
cip_class->class_code = class_code; /* the class remembers the class ID */
cip_class->revision = revision; /* the class remembers the class ID */
cip_class->max_instance = 0; /* the largest instance number of a created object in this class */
cip_class->number_of_instances = 0; /* the number of instances initially zero (more created below) */
cip_class->instances = 0;
cip_class->number_of_attributes = number_of_instance_attributes; /* the class remembers the number of instances of that class */
cip_class->highest_attribute_number = highest_instance_attribute_number; /* indicate which attributes are included in instance getAttributeAll */
cip_class->number_of_services = number_of_instance_services; /* the class manages the behavior of the instances */
cip_class->services = 0;
/* Allocate and initialize the class name string. */
OPENER_ASSERT(NULL != name);
const size_t name_len = strlen(name); /* Length does not include termination byte. */
OPENER_ASSERT(0 < name_len); /* Cannot be an empty string. */
cip_class->class_name = CipCalloc(name_len + 1, 1); /* Allocate length plus termination byte. */
OPENER_ASSERT(NULL != cip_class->class_name);
/*
* memcpy is used here, instead of a strcpy variant, to avoid Visual Studio
* depreciation warnings arising from strcpy use, and the recommended
* alternatives, e.g. strcpy_s, are not available on all systems. In this
* case the size of the source string is already known, so memcpy() is
* suitable.
*/
memcpy(cip_class->class_name, name, name_len + 1);
/* initialize the class-specific fields of the metaClass struct */
meta_class->class_code = 0xffffffff; /* set metaclass ID (this should never be referenced) */
meta_class->number_of_instances = 1; /* the class object is the only instance of the metaclass */
meta_class->instances = (CipInstance *) cip_class;
meta_class->number_of_attributes = number_of_class_attributes + 7; /* the metaclass remembers how many class attributes exist*/
meta_class->highest_attribute_number = highest_class_attribute_number; /* indicate which attributes are included in class getAttributeAll*/
meta_class->number_of_services = number_of_class_services; /* the metaclass manages the behavior of the class itself */
meta_class->class_name = (char *) CipCalloc(1, strlen(name) + 6); /* fabricate the name "meta<classname>"*/
snprintf(meta_class->class_name, strlen(name) + 6, "meta-%s", name);
/* initialize the instance-specific fields of the Class struct*/
cip_class->class_instance.instance_number = 0; /* the class object is instance zero of the class it describes (weird, but that's the spec)*/
cip_class->class_instance.attributes = 0; /* this will later point to the class attibutes*/
cip_class->class_instance.cip_class = meta_class; /* the class's class is the metaclass (like SmallTalk)*/
cip_class->class_instance.next = 0; /* the next link will always be zero, since there is only one instance of any particular class object */
meta_class->class_instance.instance_number = 0xffff; /*the metaclass object does not really have a valid instance number*/
meta_class->class_instance.attributes = NULL;/* the metaclass has no attributes*/
meta_class->class_instance.cip_class = NULL; /* the metaclass has no class*/
meta_class->class_instance.next = NULL; /* the next link will always be zero, since there is only one instance of any particular metaclass object*/
/* further initialization of the class object*/
cip_class->class_instance.attributes = (CipAttributeStruct *) CipCalloc(
meta_class->number_of_attributes,
sizeof(CipAttributeStruct) );
/* TODO -- check that we didn't run out of memory?*/
meta_class->services = (CipServiceStruct *) CipCalloc(
meta_class->number_of_services,
sizeof(CipServiceStruct) );
cip_class->services = (CipServiceStruct *) CipCalloc(
cip_class->number_of_services,
sizeof(CipServiceStruct) );
if(number_of_instances > 0) {
AddCipInstances(cip_class, number_of_instances); /*TODO handle return value and clean up if necessary*/
}
if(RegisterCipClass(cip_class) == kEipStatusError) {/* no memory to register class in Message Router */
return 0; /*TODO handle return value and clean up if necessary*/
}
AllocateAttributeMasks(meta_class); /* Allocation of bitmasks for Class Attributes */
AllocateAttributeMasks(cip_class); /* Allocation of bitmasks for Instance Attributes */
if(NULL == initializer) {
InsertAttribute( (CipInstance *) cip_class, 1, kCipUint, EncodeCipUint,
NULL, (void *) &cip_class->revision,
kGetableSingleAndAll ); /* revision */
InsertAttribute( (CipInstance *) cip_class, 2, kCipUint, EncodeCipUint,
NULL, (void *) &cip_class->max_instance,
kGetableSingleAndAll ); /* #2 Max instance no. */
InsertAttribute( (CipInstance *) cip_class, 3, kCipUint, EncodeCipUint,
NULL, (void *) &cip_class->number_of_instances,
kGetableSingleAndAll ); /* number of instances currently existing*/
InsertAttribute( (CipInstance *) cip_class, 4, kCipUint, EncodeCipUint,
NULL, (void *) &kCipUintZero, kGetableAllDummy ); /* optional attribute list - default = 0 */
InsertAttribute( (CipInstance *) cip_class, 5, kCipUint, EncodeCipUint,
NULL, (void *) &kCipUintZero, kNotSetOrGetable ); /* optional service list - default = 0 */
InsertAttribute( (CipInstance *) cip_class, 6, kCipUint, EncodeCipUint,
NULL, (void *) &meta_class->highest_attribute_number,
kGetableSingle ); /* max class attribute number*/
InsertAttribute( (CipInstance *) cip_class, 7, kCipUint, EncodeCipUint,
NULL, (void *) &cip_class->highest_attribute_number,
kGetableSingle ); /* max instance attribute number*/
if(number_of_class_services > 0) {
if(number_of_class_services > 1) { /*only if the mask has values add the get_attribute_all service */
InsertService(meta_class,
kGetAttributeAll,
&GetAttributeAll,
"GetAttributeAll"); /* bind instance services to the metaclass*/
}
InsertService(meta_class,
kGetAttributeSingle,
&GetAttributeSingle,
"GetAttributeSingle");
}
} else {
initializer(cip_class);
}
/* create the standard class services*/
return cip_class;
}
void InsertAttribute(CipInstance *const instance,
const EipUint16 attribute_number,
const EipUint8 cip_type,
CipAttributeEncodeInMessage encode_function,
CipAttributeDecodeFromMessage decode_function,
void *const data,
const EipByte cip_flags) {
OPENER_ASSERT(NULL != data); /* Its not allowed to push a NULL pointer, as this marks an unused attribute struct */
CipAttributeStruct *attribute = instance->attributes;
CipClass *cip_class = instance->cip_class;
OPENER_ASSERT(NULL != attribute);
/* adding a attribute to a class that was not declared to have any attributes is not allowed */
for(int i = 0; i < instance->cip_class->number_of_attributes; i++) {
if(attribute->data == NULL) { /* found non set attribute */
attribute->attribute_number = attribute_number;
attribute->type = cip_type;
attribute->encode = encode_function;
attribute->decode = decode_function;
attribute->attribute_flags = cip_flags;
attribute->data = data;
OPENER_ASSERT(attribute_number <= cip_class->highest_attribute_number);
size_t index = CalculateIndex(attribute_number);
cip_class->get_single_bit_mask[index] |=
(cip_flags & kGetableSingle) ? 1 << (attribute_number) % 8 : 0;
cip_class->get_all_bit_mask[index] |=
( cip_flags & (kGetableAll | kGetableAllDummy) ) ? 1 <<
(attribute_number) % 8 : 0;
cip_class->set_bit_mask[index] |= ( (cip_flags & kSetable) ? 1 : 0 ) <<
( (attribute_number) % 8 );
return;
}
attribute++;
} OPENER_TRACE_ERR(
"Tried to insert too many attributes into class: %" PRIu32 " '%s', instance %" PRIu32 "\n",
cip_class->class_code,
cip_class->class_name,
instance->instance_number);
OPENER_ASSERT(false);
/* trying to insert too many attributes*/
}
void InsertService(const CipClass *const cip_class,
const EipUint8 service_number,
const CipServiceFunction service_function,
char *const service_name) {
CipServiceStruct *service = cip_class->services; /* get a pointer to the service array*/
OPENER_TRACE_INFO("%s, number of services:%d, service number:%d\n",
cip_class->class_name, cip_class->number_of_services,
service_number);
OPENER_ASSERT(service != NULL);
/* adding a service to a class that was not declared to have services is not allowed*/
for(int i = 0; i < cip_class->number_of_services; i++) /* Iterate over all service slots attached to the class */
{
if(service->service_number == service_number ||
service->service_function == NULL) /* found undefined service slot*/
{
service->service_number = service_number; /* fill in service number*/
service->service_function = service_function; /* fill in function address*/
service->name = service_name;
return;
}
++service;
}
OPENER_ASSERT(false);
/* adding more services than were declared is a no-no*/
}
void InsertGetSetCallback(CipClass *const cip_class,
CipGetSetCallback callback_function,
CIPAttributeFlag callbacks_to_install) {
if( 0 != (kPreGetFunc & callbacks_to_install) ) {
cip_class->PreGetCallback = callback_function;
}
if( 0 != (kPostGetFunc & callbacks_to_install) ) {
cip_class->PostGetCallback = callback_function;
}
if( 0 != (kPreSetFunc & callbacks_to_install) ) {
cip_class->PreSetCallback = callback_function;
}
/* The PostSetCallback is used for both, the after set action and the storage
* of non volatile data. Therefore check for both flags set. */
if( 0 != ( (kPostSetFunc | kNvDataFunc) & callbacks_to_install ) ) {
cip_class->PostSetCallback = callback_function;
}
}
CipAttributeStruct *GetCipAttribute(const CipInstance *const instance,
const EipUint16 attribute_number) {
CipAttributeStruct *attribute = instance->attributes; /* init pointer to array of attributes*/
for(int i = 0; i < instance->cip_class->number_of_attributes; i++) {
if(attribute_number == attribute->attribute_number) {
return attribute;
} else {
++attribute;
}
}
OPENER_TRACE_WARN("attribute %d not defined\n", attribute_number);
return NULL;
}
void GenerateGetAttributeSingleHeader(
const CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
InitializeENIPMessage(&message_router_response->message);
message_router_response->reply_service =
(0x80 | message_router_request->service);
message_router_response->general_status = kCipErrorAttributeNotSupported;
message_router_response->size_of_additional_status = 0;
}
/* TODO this needs to check for buffer overflow*/
EipStatus GetAttributeSingle(CipInstance *RESTRICT const instance,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response,
const struct sockaddr *originator_address,
const CipSessionHandle encapsulation_session) {
/* Suppress unused parameter compiler warning. */
(void) originator_address;
(void) encapsulation_session;
/* Mask for filtering get-ability */
CipAttributeStruct *attribute = GetCipAttribute(instance,
message_router_request->request_path.attribute_number);
GenerateGetAttributeSingleHeader(message_router_request,
message_router_response);
EipUint16 attribute_number =
message_router_request->request_path.attribute_number;
if( (NULL != attribute) && (NULL != attribute->data) ) {
uint8_t get_bit_mask =
(instance->cip_class->get_single_bit_mask[CalculateIndex(attribute_number)
]);
if( 0 != ( get_bit_mask & ( 1 << (attribute_number % 8) ) ) ) {
OPENER_TRACE_INFO("getAttribute %d\n",
message_router_request->request_path.attribute_number); /* create a reply message containing the data*/
/* Call the PreGetCallback if enabled for this attribute and the class provides one. */
if( (attribute->attribute_flags & kPreGetFunc) &&
NULL != instance->cip_class->PreGetCallback ) {
instance->cip_class->PreGetCallback(instance,
attribute,
message_router_request->service);
}
OPENER_ASSERT(NULL != attribute);
attribute->encode(attribute->data, &message_router_response->message);
message_router_response->general_status = kCipErrorSuccess;
/* Call the PostGetCallback if enabled for this attribute and the class provides one. */
if( (attribute->attribute_flags & kPostGetFunc) &&
NULL != instance->cip_class->PostGetCallback ) {
instance->cip_class->PostGetCallback(instance,
attribute,
message_router_request->service);
}
}
}
return kEipStatusOkSend;
}
void EncodeCipBool(const void *const data,
ENIPMessage *const outgoing_message) {
AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
}
void EncodeCipByte(const void *const data,
ENIPMessage *const outgoing_message) {
AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
}
void EncodeCipWord(const void *const data,
ENIPMessage *const outgoing_message) {
AddIntToMessage(*(EipUint16 *) (data), outgoing_message);
}
void EncodeCipDword(const void *const data,
ENIPMessage *const outgoing_message) {
AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
}
void EncodeCipLword(const void *const data,
ENIPMessage *const outgoing_message) {
AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
}
void EncodeCipUsint(const void *const data,
ENIPMessage *const outgoing_message) {
AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
}
void EncodeCipUint(const void *const data,
ENIPMessage *const outgoing_message) {
AddIntToMessage(*(EipUint16 *) (data), outgoing_message);
}
void EncodeCipUdint(const void *const data,
ENIPMessage *const outgoing_message) {
AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
}
void EncodeCipUlint(const void *const data,
ENIPMessage *const outgoing_message) {
AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
}
void EncodeCipSint(const void *const data,
ENIPMessage *const outgoing_message) {
AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
}
void EncodeCipInt(const void *const data,
ENIPMessage *const outgoing_message) {
AddIntToMessage(*(EipUint16 *) (data), outgoing_message);
}
void EncodeCipDint(const void *const data,
ENIPMessage *const outgoing_message) {
AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
}
void EncodeCipLint(const void *const data,
ENIPMessage *const outgoing_message) {
AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
}
void EncodeCipReal(const void *const data,
ENIPMessage *const outgoing_message) {
AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
}
void EncodeCipLreal(const void *const data,
ENIPMessage *const outgoing_message) {
AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
}
void EncodeCipShortString(const void *const data,
ENIPMessage *const outgoing_message) {
CipShortString *const short_string = (CipShortString *) data;
AddSintToMessage(short_string->length, outgoing_message);
memcpy(outgoing_message->current_message_position,
short_string->string,
short_string->length);
outgoing_message->current_message_position += short_string->length;
outgoing_message->used_message_length += short_string->length;
}
void EncodeCipString(const void *const data,
ENIPMessage *const outgoing_message) {
CipString *const string = (CipString *) data;
AddIntToMessage(*(EipUint16 *) &(string->length), outgoing_message);
if(0 != string->length) {
memcpy(outgoing_message->current_message_position,
string->string,
string->length);
outgoing_message->current_message_position += string->length;
outgoing_message->used_message_length += string->length;
if(outgoing_message->used_message_length & 0x01) {
/* we have an odd byte count */
AddSintToMessage(0, outgoing_message);
}
}
}
void EncodeCipString2(const void *const data,
ENIPMessage *const outgoing_message) {
/* Suppress unused parameter compiler warning. */
(void)data;
(void)outgoing_message;
OPENER_ASSERT(false); /* Not implemented yet */
}
void EncodeCipStringN(const void *const data,
ENIPMessage *const outgoing_message) {
/* Suppress unused parameter compiler warning. */
(void)data;
(void)outgoing_message;
OPENER_ASSERT(false); /* Not implemented yet */
}
static void CipStringIHeaderEncoding(const CipStringIStruct *const string,
ENIPMessage *const outgoing_message) {
EncodeCipUsint(&(string->language_char_1), outgoing_message);
EncodeCipUsint(&(string->language_char_2), outgoing_message);
EncodeCipUsint(&(string->language_char_3), outgoing_message);
EncodeCipUsint(&(string->char_string_struct), outgoing_message);
EncodeCipUint(&(string->character_set), outgoing_message);
}
void EncodeCipStringI(const void *const data,
ENIPMessage *const outgoing_message) {
const CipStringI *const string_i = data;
EncodeCipUsint(&(string_i->number_of_strings), outgoing_message);
for(size_t i = 0; i < string_i->number_of_strings; ++i) {
CipStringIHeaderEncoding( (string_i->array_of_string_i_structs) + i,
outgoing_message );
switch(string_i->array_of_string_i_structs[i].char_string_struct) {
case kCipString:
EncodeCipString(string_i->array_of_string_i_structs[i].string,
outgoing_message);
break;
case kCipString2:
EncodeCipString2(string_i->array_of_string_i_structs[i].string,
outgoing_message);
break;
case kCipStringN:
EncodeCipStringN(string_i->array_of_string_i_structs[i].string,
outgoing_message);
break;
case kCipShortString:
EncodeCipShortString(string_i->array_of_string_i_structs[i].string,
outgoing_message);
break;
default:
OPENER_ASSERT(false);
break;
}
}
}
void EncodeCipByteArray(const void *const data,
ENIPMessage *const outgoing_message) {
OPENER_TRACE_INFO(" -> get attribute byte array\r\n");
CipByteArray *cip_byte_array = (CipByteArray *) data;
memcpy(outgoing_message->current_message_position,
cip_byte_array->data,
cip_byte_array->length);
outgoing_message->current_message_position += cip_byte_array->length;
outgoing_message->used_message_length += cip_byte_array->length;
}
void EncodeCipEPath(const void *const data,
ENIPMessage *const outgoing_message) {
AddIntToMessage( ( (CipEpath *)data )->path_size, outgoing_message );
EncodeEPath( (CipEpath *) data, outgoing_message );
}
void EncodeCipEthernetLinkPhyisicalAddress(const void *const data,
ENIPMessage *const outgoing_message)
{
EipUint8 *p = (EipUint8 *) data;
memcpy(outgoing_message->current_message_position, p, 6);
outgoing_message->current_message_position += 6;
outgoing_message->used_message_length += 6;
}
void GenerateSetAttributeSingleHeader(
const CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
InitializeENIPMessage(&message_router_response->message);
message_router_response->reply_service =
(0x80 | message_router_request->service);
message_router_response->general_status = kCipErrorAttributeNotSupported;
message_router_response->size_of_additional_status = 0;
}
EipStatus SetAttributeSingle(CipInstance *RESTRICT const instance,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response,
const struct sockaddr *originator_address,
const CipSessionHandle encapsulation_session) {
/* Suppress unused parameter compiler warning. */
(void)originator_address;
(void)encapsulation_session;
CipAttributeStruct *attribute = GetCipAttribute(instance,
message_router_request->request_path.attribute_number);
GenerateSetAttributeSingleHeader(message_router_request,
message_router_response);
EipUint16 attribute_number =
message_router_request->request_path.attribute_number;
/* Mask for filtering set-ability */
if( (NULL != attribute) && (NULL != attribute->data) ) {
if( (attribute->attribute_flags == kGetableAllDummy) ||
(attribute->attribute_flags == kNotSetOrGetable) ||
(attribute->attribute_flags == kGetableAll) ) {
OPENER_TRACE_WARN("SetAttributeSingle: Attribute %d not supported!\n\r",
attribute_number);
} else {
uint8_t set_bit_mask =
(instance->cip_class->set_bit_mask[CalculateIndex(attribute_number)]);
if( 0 != ( set_bit_mask & ( 1 << (attribute_number % 8) ) ) ) {
OPENER_TRACE_INFO("setAttribute %d\n", attribute_number);
/* Call the PreSetCallback if enabled for this attribute and the class provides one. */
if( (attribute->attribute_flags & kPreSetFunc) &&
NULL != instance->cip_class->PreSetCallback ) {
instance->cip_class->PreSetCallback(instance,
attribute,
message_router_request->service);
}
OPENER_ASSERT(NULL != attribute);
attribute->decode(attribute->data,
message_router_request,
message_router_response); //writes data to attribute, sets resonse status
/* Call the PostSetCallback if enabled for this attribute and the class provides one. */
if( ( attribute->attribute_flags & (kPostSetFunc | kNvDataFunc) ) &&
NULL != instance->cip_class->PostSetCallback ) {
instance->cip_class->PostSetCallback(instance,
attribute,
message_router_request->service);
}
} else {
message_router_response->general_status = kCipErrorAttributeNotSetable;
OPENER_TRACE_WARN("SetAttributeSingle: Attribute %d not setable!\n\r",
attribute_number);
}
}
}
return kEipStatusOkSend;
}
int DecodeCipBool(CipBool *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetBoolFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 1;
}
int DecodeCipByte(CipByte *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetByteFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 1;
}
int DecodeCipByteArray(CipByteArray *const data,
const CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response)
{
int number_of_decoded_bytes = -1;
OPENER_TRACE_INFO(" -> set attribute byte array\r\n");
CipByteArray *cip_byte_array = data;
if(message_router_request->request_data_size < data->length) {
OPENER_TRACE_INFO(
"DecodeCipByteArray: not enough data received.\n");
message_router_response->general_status = kCipErrorNotEnoughData;
return number_of_decoded_bytes;
}
if(message_router_request->request_data_size > data->length) {
OPENER_TRACE_INFO(
"DecodeCipByteArray: too much data received.\n");
message_router_response->general_status = kCipErrorTooMuchData;
return number_of_decoded_bytes;
}
// data-length is correct
memcpy(cip_byte_array->data,
message_router_request->data,
cip_byte_array->length);
number_of_decoded_bytes = cip_byte_array->length;
message_router_response->general_status = kCipErrorSuccess;
return number_of_decoded_bytes;
}
int DecodeCipWord(CipWord *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetWordFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 2;
}
int DecodeCipDword(CipDword *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetDintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 4;
}
int DecodeCipLword(CipLword *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetLintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 4;
}
int DecodeCipUsint(CipUsint *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetUsintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 1;
}
int DecodeCipUint(CipUint *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetUintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 2;
}
int DecodeCipUdint(CipUdint *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetUdintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 4;
}
int DecodeCipUlint(CipUlint *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetLintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 8;
}
int DecodeCipSint(CipSint *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetSintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 1;
}
int DecodeCipInt(CipInt *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetIntFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 2;
}
int DecodeCipDint(CipDint *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetDintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 4;
}
int DecodeCipLint(CipLint *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetLintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 8;
}
int DecodeCipReal(CipReal *const data,
CipMessageRouterRequest *const message_router_request,
CipMessageRouterResponse *const message_router_response) {
*data = GetDintFromMessage(&message_router_request->data);
message_router_response->general_status = kCipErrorSuccess;
return 4;