-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathpf_dcp.c
1988 lines (1857 loc) · 60.5 KB
/
pf_dcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* _ _ _
* _ __ | |_ _ | | __ _ | |__ ___
* | '__|| __|(_)| | / _` || '_ \ / __|
* | | | |_ _ | || (_| || |_) |\__ \
* |_| \__|(_)|_| \__,_||_.__/ |___/
*
* www.rt-labs.com
* Copyright 2018 rt-labs AB, Sweden.
*
* This software is dual-licensed under GPLv3 and a commercial
* license. See the file LICENSE.md distributed with this software for
* full license information.
********************************************************************/
/**
* @file
* @brief Implements the Discovery and basic Configuration Protocol (DCP).
*
* DCP runs over Ethernet layer 2 (not IP or UDP).
*
* The main DCP messages are:
* - GET
* - SET
* - IDENTIFY (asking for a particular device)
* - HELLO (broadcast to indicate the presence of a device)
*
* Registers the different frame IDs for these messages with the frame handler.
*
* Responses to IDENTIFY are delayed (by an amount calculated from the
* MAC address), as many devices might respond to the same request.
*
* The SAM (Source Address ?) makes sure that that just a single remote
* MAC address is used for the communication.
* A timer of 3 seconds is used for the SAM.
*/
#ifdef UNIT_TEST
#endif
#include <string.h>
#include <inttypes.h>
#include "pf_includes.h"
#include "pf_block_writer.h"
/*
* Various constants from the standard document.
*/
#define PF_DCP_SERVICE_GET 0x03
#define PF_DCP_SERVICE_SET 0x04
#define PF_DCP_SERVICE_IDENTIFY 0x05
#define PF_DCP_SERVICE_HELLO 0x06
#define PF_DCP_BLOCK_PADDING 0x00
#define PF_DCP_SERVICE_TYPE_REQUEST 0x00
#define PF_DCP_SERVICE_TYPE_SUCCESS 0x01
#define PF_DCP_SERVICE_TYPE_NOT_SUPPORTED 0x05
#define PF_DCP_HEADER_SIZE 10
#define PF_DCP_BLOCK_HDR_SIZE 4
#define PF_DCP_HELLO_FRAME_ID 0xfefc
#define PF_DCP_GET_SET_FRAME_ID 0xfefd
#define PF_DCP_ID_REQ_FRAME_ID 0xfefe
#define PF_DCP_ID_RES_FRAME_ID 0xfeff
#define PF_DCP_SIGNAL_LED_HALF_INTERVAL 500000 /* 0.5 seconds */
#define PF_DCP_SIGNAL_LED_NUMBER_OF_FLASHES 3
/* Client hold time, 3 seconds.
See Profinet 2.4 Services, section 6.3.11.2.2 */
#define PF_DCP_SAM_TIMEOUT 3000000 /* microseconds */
CC_PACKED_BEGIN
typedef struct CC_PACKED pf_ethhdr
{
pnet_ethaddr_t dest;
pnet_ethaddr_t src;
uint16_t type; /* Note: network endianness */
} pf_ethhdr_t;
CC_PACKED_END
CC_PACKED_BEGIN
typedef struct CC_PACKED pf_dcp_header
{
uint8_t service_id;
uint8_t service_type;
uint32_t xid; /* Note: network endianness */
uint16_t response_delay_factor; /* Note: network endianness */
uint16_t data_length; /* Note: network endianness */
} pf_dcp_header_t;
CC_PACKED_END
CC_STATIC_ASSERT (PF_DCP_HEADER_SIZE == sizeof (pf_dcp_header_t));
CC_PACKED_BEGIN
typedef struct CC_PACKED pf_dcp_block_hdr
{
uint8_t option;
uint8_t sub_option;
uint16_t block_length; /* Note: network endianness */
} pf_dcp_block_hdr_t;
CC_PACKED_END
CC_STATIC_ASSERT (PF_DCP_BLOCK_HDR_SIZE == sizeof (pf_dcp_block_hdr_t));
/*
* This is the standard DCP HELLO broadcast address (MAC).
*/
static const pnet_ethaddr_t dcp_mc_addr_hello = {
{0x01, 0x0e, 0xcf, 0x00, 0x00, 0x01}};
#define PF_MAC_NIL \
{ \
{ \
0, 0, 0, 0, 0, 0 \
} \
}
static const pnet_ethaddr_t mac_nil = PF_MAC_NIL;
/*
* A list of options that we can get/set/filter.
* Use in identify filter and identify response.
*/
typedef struct pf_dcp_opt_sub
{
uint8_t opt;
uint8_t sub;
} pf_dcp_opt_sub_t;
/* Device options to be included for example in DCP IDENTIFY responses */
static const pf_dcp_opt_sub_t device_options[] = {
{PF_DCP_OPT_IP, PF_DCP_SUB_IP_PAR},
{PF_DCP_OPT_IP, PF_DCP_SUB_IP_MAC},
{PF_DCP_OPT_DEVICE_PROPERTIES, PF_DCP_SUB_DEV_PROP_VENDOR},
{PF_DCP_OPT_DEVICE_PROPERTIES, PF_DCP_SUB_DEV_PROP_NAME},
{PF_DCP_OPT_DEVICE_PROPERTIES, PF_DCP_SUB_DEV_PROP_ID},
{PF_DCP_OPT_DEVICE_PROPERTIES, PF_DCP_SUB_DEV_PROP_ROLE},
{PF_DCP_OPT_DEVICE_PROPERTIES, PF_DCP_SUB_DEV_PROP_OPTIONS},
{PF_DCP_OPT_DEVICE_PROPERTIES, PF_DCP_SUB_DEV_PROP_ALIAS},
{PF_DCP_OPT_CONTROL, PF_DCP_SUB_CONTROL_START},
{PF_DCP_OPT_CONTROL, PF_DCP_SUB_CONTROL_STOP},
{PF_DCP_OPT_CONTROL, PF_DCP_SUB_CONTROL_SIGNAL},
{PF_DCP_OPT_CONTROL, PF_DCP_SUB_CONTROL_FACTORY_RESET},
{PF_DCP_OPT_CONTROL, PF_DCP_SUB_CONTROL_RESET_TO_FACTORY},
#if 0
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_HOSTNAME},
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_VENDOR_SPEC},
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_SERVER_ID},
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_PAR_REQ_LIST},
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_CLASS_ID},
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_CLIENT_ID},
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_FQDN},
{PF_DCP_OPT_DHCP, PF_DCP_SUB_DHCP_UUID_CLIENT_ID},
#endif
// {PF_DCP_OPT_DEVICE_INITIATIVE, PF_DCP_SUB_DEV_INITIATIVE_SUPPORT},
{PF_DCP_OPT_ALL, PF_DCP_SUB_ALL},
};
/**
* @internal
* Send a delayed DCP response to an IDENTIFY request.
*
* The delay is important as many devices might respond to the same request.
*
* This is a callback for the scheduler. Arguments should fulfill
* pf_scheduler_timeout_ftn_t
*
* @param net InOut: The p-net stack instance
* @param arg In: DCP responder data. Should be pnal_buf_t.
* @param current_time In: The current system time, in microseconds,
* when the scheduler is started to execute
* stored tasks. Not used here.
*/
static void pf_dcp_responder (pnet_t * net, void * arg, uint32_t current_time)
{
pnal_buf_t * p_buf = (pnal_buf_t *)arg;
pf_scheduler_reset_handle (&net->dcp_identresp_timeout);
if (p_buf != NULL)
{
if (net->dcp_delayed_response_waiting == true)
{
if (pf_eth_send (net, net->pf_interface.main_port.handle, p_buf) > 0)
{
LOG_DEBUG (
PNET_LOG,
"DCP(%d): Sent a DCP identify response.\n",
__LINE__);
}
pnal_buf_free (p_buf);
net->dcp_delayed_response_waiting = false;
}
}
}
/**
* @internal
* Clear SAM
* SAM limits the communication to a single remote MAC address
* for 3 seconds. This operation implements the timeout callback.
*
* This is a callback for the scheduler. Arguments should fulfill
* pf_scheduler_timeout_ftn_t
*
* @param net InOut: The p-net stack instance
* @param arg In: Not used.
* @param current_time In: Not used.
*/
static void pf_dcp_clear_sam (pnet_t * net, void * arg, uint32_t current_time)
{
LOG_DEBUG (
PF_DCP_LOG,
"DCP(%d): SAM timeout. Clear stored remote MAC address.\n",
__LINE__);
net->dcp_sam = mac_nil;
pf_scheduler_reset_handle (&net->dcp_sam_timeout);
}
/**
* @internal
* Restart SAM timeout
* SAM limits the communication to a single remote MAC address
* for 3 seconds.
*
* @param net InOut: The p-net stack instance
* @param mac In: Remote MAC address
*/
static void pf_dcp_restart_sam_timeout (pnet_t * net, const pnet_ethaddr_t * mac)
{
/* Make sure no other MAC address is used in the DCP communication
* for 3 seconds */
LOG_DEBUG (
PF_DCP_LOG,
"DCP(%d): Update SAM remote MAC address, and restart timeout.\n",
__LINE__);
memcpy (&net->dcp_sam, mac, sizeof (net->dcp_sam));
(void)pf_scheduler_restart (
net,
PF_DCP_SAM_TIMEOUT,
pf_dcp_clear_sam,
NULL,
&net->dcp_sam_timeout);
}
/**
* @internal
* Check SAM
* SAM limits the communication to a single remote MAC address
* for 3 seconds.
*
* @param net InOut: The p-net stack instance
* @param mac In: Remote MAC address
* @return true if DDP communication is allowed with respect to SAM.
* false if not.
*/
static bool pf_dcp_check_sam (pnet_t * net, const pnet_ethaddr_t * mac)
{
return ((memcmp (&net->dcp_sam, &mac_nil, sizeof (net->dcp_sam))) == 0) ||
(memcmp (&net->dcp_sam, mac, sizeof (net->dcp_sam)) == 0);
}
/**
* @internal
* Check that address is not a multi-cast address
* and also that it matches the local MAC address.
*
* This check was added to handle situation where network
* interface is in promiscuous mode and the need to filter out
* DCP requests intended for other devices.
*
* @param local_mac In: Local MAC address. Should be the
* MAC address of main (DAP) interface.
* @param destination_mac In: Destination MAC address
* @return true if destination address is ok and shall packet shall
* be accepted. false if not.
*/
bool pf_dcp_check_destination_address (
const pnet_ethaddr_t * local_mac,
const pnet_ethaddr_t * destination_mac)
{
return (
((destination_mac->addr[0] & 1) == 0) && /* Not multi-cast */
(memcmp (destination_mac, local_mac, sizeof (*destination_mac)) == 0));
}
/**
* @internal
* Add a block to a buffer, possibly including a block_info.
*
* @param p_dst Out: The destination buffer.
* @param p_dst_pos InOut: Position in the destination buffer.
* @param dst_max In: Size of destination buffer.
* @param opt In: Option key.
* @param sub In: Sub-option key.
* @param with_block_info In: If true then add block_info argument.
* @param block_info In: The block info argument.
* @param value_length In: The length in bytes of the p_value data.
* @param p_value In: The source data.
* @return
*/
static int pf_dcp_put_block (
uint8_t * p_dst,
uint16_t * p_dst_pos,
uint16_t dst_max,
uint8_t opt,
uint8_t sub,
bool with_block_info,
uint16_t block_info,
uint16_t value_length, /* Not including length of block_info */
const void * p_value)
{
int ret = -1;
uint16_t b_len;
if ((*p_dst_pos + value_length) < dst_max)
{
/* Adjust written block length if block_info is included */
b_len = value_length;
if (with_block_info)
{
b_len += sizeof (b_len);
}
pf_put_byte (opt, dst_max, p_dst, p_dst_pos);
pf_put_byte (sub, dst_max, p_dst, p_dst_pos);
pf_put_uint16 (true, b_len, dst_max, p_dst, p_dst_pos);
if (with_block_info == true)
{
pf_put_uint16 (true, block_info, dst_max, p_dst, p_dst_pos);
}
if ((p_value != NULL) && (value_length > 0))
{
pf_put_mem (p_value, value_length, dst_max, p_dst, p_dst_pos);
}
/* Add padding to align on uint16_t */
while ((*p_dst_pos) & 1)
{
pf_put_byte (0, dst_max, p_dst, p_dst_pos);
}
}
ret = 0; /* Skip excess data silently !! */
return ret;
}
/**
* @internal
* Handle one block in a DCP get request, by inserting it into the response.
*
* @param net InOut: The p-net stack instance
* @param p_dst Out: The destination buffer.
* @param p_dst_pos InOut: Position in the destination buffer.
* @param dst_max In: Size of destination buffer.
* @param opt In: Option key.
* @param sub In: Sub-option key.
* @param request_is_identify In: Usage in response to Identify request
* (skips some blocks)
* @param alias_name In: Alias name appended if != NULL
* @return
*/
static int pf_dcp_get_req (
pnet_t * net,
uint8_t * p_dst,
uint16_t * p_dst_pos,
uint16_t dst_max,
uint8_t opt,
uint8_t sub,
bool request_is_identify,
const char * alias_name)
{
int ret = 0; /* Assume all OK */
uint8_t block_error = PF_DCP_BLOCK_ERROR_NO_ERROR;
uint8_t negative_response_data[3]; /* For negative get */
uint16_t block_info = 0;
uint16_t value_length = 0;
uint8_t * p_value = NULL;
bool skip = false; /* When true: Do not insert block in response */
pf_full_ip_suite_t full_ip_suite;
uint16_t temp16;
uint16_t ix;
/* Get the data */
ret = pf_cmina_dcp_get_req (
net,
opt,
sub,
&value_length,
&p_value,
&block_error);
/* Some very specific options require special treatment */
switch (opt)
{
case PF_DCP_OPT_IP:
switch (sub)
{
case PF_DCP_SUB_IP_PAR:
memcpy (
&full_ip_suite.ip_suite,
p_value,
sizeof (full_ip_suite.ip_suite));
full_ip_suite.ip_suite.ip_addr =
htonl (full_ip_suite.ip_suite.ip_addr);
full_ip_suite.ip_suite.ip_mask =
htonl (full_ip_suite.ip_suite.ip_mask);
full_ip_suite.ip_suite.ip_gateway =
htonl (full_ip_suite.ip_suite.ip_gateway);
p_value = (uint8_t *)&full_ip_suite.ip_suite;
block_info = ((full_ip_suite.ip_suite.ip_addr == 0) &&
(full_ip_suite.ip_suite.ip_mask == 0) &&
(full_ip_suite.ip_suite.ip_gateway == 0))
? 0
: BIT (0);
/* ToDo: We do not yet support DHCP (block_info, BIT(1)) */
/* ToDo: We do not yet report on "IP address conflict" (block_info,
* BIT(7)) */
break;
case PF_DCP_SUB_IP_SUITE:
memcpy (&full_ip_suite, p_value, sizeof (full_ip_suite));
full_ip_suite.ip_suite.ip_addr =
htonl (full_ip_suite.ip_suite.ip_addr);
full_ip_suite.ip_suite.ip_mask =
htonl (full_ip_suite.ip_suite.ip_mask);
full_ip_suite.ip_suite.ip_gateway =
htonl (full_ip_suite.ip_suite.ip_gateway);
for (ix = 0; ix < NELEMENTS (full_ip_suite.dns_addr); ix++)
{
full_ip_suite.dns_addr[ix] = htonl (full_ip_suite.dns_addr[ix]);
}
p_value = (uint8_t *)&full_ip_suite;
block_info = ((full_ip_suite.ip_suite.ip_addr == 0) &&
(full_ip_suite.ip_suite.ip_mask == 0) &&
(full_ip_suite.ip_suite.ip_gateway == 0))
? 0
: BIT (0);
/* ToDo: We do not yet support DHCP (block_info, BIT(1)) */
/* ToDo: We do not yet report on "IP address conflict" (block_info,
* BIT(7)) */
break;
case PF_DCP_SUB_IP_MAC:
skip = request_is_identify;
break;
default:
break;
}
break;
case PF_DCP_OPT_DEVICE_PROPERTIES:
switch (sub)
{
case PF_DCP_SUB_DEV_PROP_OPTIONS:
if (sizeof (device_options) > 0)
{
p_value = (uint8_t *)device_options;
value_length = sizeof (device_options);
ret = 0;
}
else
{
skip = true;
ret = 0; /* This is OK - just omit the entire block. */
}
break;
case PF_DCP_SUB_DEV_PROP_GATEWAY:
if (value_length == sizeof (temp16))
{
memcpy (&temp16, p_value, sizeof (temp16));
temp16 = htons (temp16);
p_value = (uint8_t *)&temp16;
}
else
{
ret = -1;
}
break;
case PF_DCP_SUB_DEV_PROP_NAME:
value_length = (uint16_t)strlen ((char *)p_value);
break;
case PF_DCP_SUB_DEV_PROP_ALIAS:
skip = (alias_name == NULL);
if (!skip)
{
value_length = (uint16_t)strlen (alias_name);
p_value = (uint8_t *)alias_name;
ret = 0;
}
break;
case PF_DCP_SUB_DEV_PROP_VENDOR:
value_length = (uint16_t)strlen ((char *)p_value);
break;
case PF_DCP_SUB_DEV_PROP_ROLE:
value_length += 1;
break;
case PF_DCP_SUB_DEV_PROP_ID:
case PF_DCP_SUB_DEV_PROP_INSTANCE:
case PF_DCP_SUB_DEV_PROP_OEM_ID:
default:
break;
}
break;
case PF_DCP_OPT_DEVICE_INITIATIVE:
if (value_length == sizeof (temp16))
{
memcpy (&temp16, p_value, sizeof (temp16));
temp16 = htons (temp16);
p_value = (uint8_t *)&temp16;
}
else
{
ret = -1;
}
break;
case PF_DCP_OPT_CONTROL:
skip = true;
break;
case PF_DCP_OPT_ALL:
skip = true;
break;
case PF_DCP_OPT_DHCP:
default:
break;
}
if (ret == 0)
{
if (skip == false)
{
ret = pf_dcp_put_block (
p_dst,
p_dst_pos,
dst_max,
opt,
sub,
true,
block_info,
value_length,
p_value);
}
}
else
{
if (skip == false)
{
/* GetNegResBlock consists of:
* - option = Control 1 byte
* - suboption = Response 1 byte
* - block length 2 bytes
* - type of option involved (option + suboption) 2 bytes
* - block error 1 byte
*/
negative_response_data[0] = opt;
negative_response_data[1] = sub;
negative_response_data[2] = block_error;
ret = pf_dcp_put_block (
p_dst,
p_dst_pos,
dst_max,
PF_DCP_OPT_CONTROL,
PF_DCP_SUB_CONTROL_RESPONSE,
false,
0,
sizeof (negative_response_data),
negative_response_data);
}
}
return ret;
}
/**
* @internal
* Blink Profinet signal LED.
*
* This functions blinks the Profinet signal LED 3 times at 1 Hz.
* The LED is accessed via the pnet_signal_led_ind() callback function.
*
* This is a callback for the scheduler. Arguments should fulfill
* pf_scheduler_timeout_ftn_t
*
* @param net InOut: The p-net stack instance
* @param arg In: The current state (number of remaining
* transitions)
* @param current_time In: The current system time, in microseconds,
* when the scheduler is started to execute
* stored tasks.
* Not used here.
*/
static void pf_dcp_control_signal_led (
pnet_t * net,
void * arg,
uint32_t current_time)
{
uint32_t state = (uint32_t) (uintptr_t)arg;
if ((state % 2) == 1)
{
/* Turn LED on */
if (pf_fspm_signal_led_ind (net, true) != 0)
{
LOG_ERROR (
PF_DCP_LOG,
"DCP(%d): Could not turn signal LED on\n",
__LINE__);
}
}
else
{
/* Turn LED off */
if (pf_fspm_signal_led_ind (net, false) != 0)
{
LOG_ERROR (
PF_DCP_LOG,
"DCP(%d): Could not turn signal LED off\n",
__LINE__);
}
}
if ((state > 0) && (state < 200)) /* Plausibility test */
{
state--;
/* Schedule another round */
(void)pf_scheduler_add (
net,
PF_DCP_SIGNAL_LED_HALF_INTERVAL,
pf_dcp_control_signal_led,
(void *)(uintptr_t)state,
&net->dcp_led_timeout);
}
else
{
pf_scheduler_reset_handle (&net->dcp_led_timeout);
}
}
/**
* @internal
* Trigger blinking Profinet signal LED.
*
* If the LED already is blinking, do not restart the blinking.
*
* @param net InOut: The p-net stack instance
* @return 0 on success
* -1 on error
*/
int pf_dcp_trigger_signal_led (pnet_t * net)
{
if (pf_scheduler_is_running (&net->dcp_led_timeout) == false)
{
LOG_INFO (
PF_DCP_LOG,
"DCP(%d): Received request to flash LED\n",
__LINE__);
(void)pf_scheduler_add (
net,
PF_DCP_SIGNAL_LED_HALF_INTERVAL,
pf_dcp_control_signal_led,
(void *)(2 * PF_DCP_SIGNAL_LED_NUMBER_OF_FLASHES - 1),
&net->dcp_led_timeout);
}
else
{
LOG_INFO (
PF_DCP_LOG,
"DCP(%d): Received request to flash LED, but it is already "
"flashing.\n",
__LINE__);
}
return 0;
}
/**
* @internal
* Handle one block in a DCP set request, by inserting it into the response.
*
* Triggers the application callback \a pnet_reset_ind() for some values.
*
* @param net InOut: The p-net stack instance
* @param p_dst Out: The destination buffer.
* @param p_dst_pos InOut: Position in the destination buffer.
* @param dst_max In: Size of destination buffer.
* @param opt In: Option key.
* @param sub In: Sub-option key.
* @param block_info In: The block info argument.
* @param value_length In: The length in bytes of the p_value data.
* @param p_value In: The source data.
* @return
*/
static int pf_dcp_set_req (
pnet_t * net,
uint8_t * p_dst,
uint16_t * p_dst_pos,
uint16_t dst_max,
uint8_t opt,
uint8_t sub,
uint16_t block_qualifier,
uint16_t value_length,
const uint8_t * p_value)
{
int ret = -1;
uint8_t response_data[3];
pf_full_ip_suite_t full_ip_suite;
uint16_t ix;
response_data[0] = opt;
response_data[1] = sub;
/* Assume negative response initially */
response_data[2] = PF_DCP_BLOCK_ERROR_SUBOPTION_NOT_SUPPORTED;
switch (opt)
{
case PF_DCP_OPT_IP:
switch (sub)
{
case PF_DCP_SUB_IP_MAC:
/* Can't set MAC address */
break;
case PF_DCP_SUB_IP_PAR:
if (value_length == sizeof (full_ip_suite.ip_suite))
{
memcpy (
&full_ip_suite.ip_suite,
p_value,
sizeof (full_ip_suite.ip_suite));
full_ip_suite.ip_suite.ip_addr =
ntohl (full_ip_suite.ip_suite.ip_addr);
full_ip_suite.ip_suite.ip_mask =
ntohl (full_ip_suite.ip_suite.ip_mask);
full_ip_suite.ip_suite.ip_gateway =
ntohl (full_ip_suite.ip_suite.ip_gateway);
ret = pf_cmina_dcp_set_ind (
net,
opt,
sub,
block_qualifier,
value_length,
(uint8_t *)&full_ip_suite.ip_suite,
&response_data[2]);
}
break;
case PF_DCP_SUB_IP_SUITE:
if (value_length == sizeof (full_ip_suite.ip_suite))
{
memcpy (&full_ip_suite, p_value, sizeof (full_ip_suite));
full_ip_suite.ip_suite.ip_addr =
ntohl (full_ip_suite.ip_suite.ip_addr);
full_ip_suite.ip_suite.ip_mask =
ntohl (full_ip_suite.ip_suite.ip_mask);
full_ip_suite.ip_suite.ip_gateway =
ntohl (full_ip_suite.ip_suite.ip_gateway);
for (ix = 0; ix < NELEMENTS (full_ip_suite.dns_addr); ix++)
{
full_ip_suite.dns_addr[ix] = ntohl (full_ip_suite.dns_addr[ix]);
}
ret = pf_cmina_dcp_set_ind (
net,
opt,
sub,
block_qualifier,
value_length,
(uint8_t *)&full_ip_suite.ip_suite,
&response_data[2]);
}
break;
default:
break;
}
break;
case PF_DCP_OPT_DEVICE_PROPERTIES:
switch (sub)
{
case PF_DCP_SUB_DEV_PROP_NAME:
ret = pf_cmina_dcp_set_ind (
net,
opt,
sub,
block_qualifier,
value_length,
p_value,
&response_data[2]);
break;
case PF_DCP_SUB_DEV_PROP_VENDOR:
case PF_DCP_SUB_DEV_PROP_ID:
case PF_DCP_SUB_DEV_PROP_ROLE:
case PF_DCP_SUB_DEV_PROP_OPTIONS:
case PF_DCP_SUB_DEV_PROP_ALIAS:
case PF_DCP_SUB_DEV_PROP_INSTANCE:
case PF_DCP_SUB_DEV_PROP_OEM_ID:
case PF_DCP_SUB_DEV_PROP_GATEWAY:
/* Can't set these */
break;
default:
break;
}
break;
case PF_DCP_OPT_DHCP:
switch (sub)
{
case PF_DCP_SUB_DHCP_HOSTNAME:
case PF_DCP_SUB_DHCP_VENDOR_SPEC:
case PF_DCP_SUB_DHCP_SERVER_ID:
case PF_DCP_SUB_DHCP_PAR_REQ_LIST:
case PF_DCP_SUB_DHCP_CLASS_ID:
case PF_DCP_SUB_DHCP_CLIENT_ID:
case PF_DCP_SUB_DHCP_FQDN:
case PF_DCP_SUB_DHCP_UUID_CLIENT_ID:
/* ToDo: DHCP Not yet implemented */
break;
case PF_DCP_SUB_DHCP_CONTROL:
/* Can't touch this */
break;
default:
break;
}
break;
case PF_DCP_OPT_CONTROL:
/* Just ignore if not supported */
switch (sub)
{
case PF_DCP_SUB_CONTROL_START:
net->dcp_global_block_qualifier = block_qualifier;
response_data[2] = PF_DCP_BLOCK_ERROR_NO_ERROR;
ret = 0;
break;
case PF_DCP_SUB_CONTROL_STOP:
if (block_qualifier == net->dcp_global_block_qualifier)
{
response_data[2] = PF_DCP_BLOCK_ERROR_NO_ERROR;
ret = 0;
}
else
{
ret = 0;
}
break;
case PF_DCP_SUB_CONTROL_SIGNAL:
/* Schedule a state-machine that flashes "the LED" for 3s, 1Hz. */
ret = pf_dcp_trigger_signal_led (net);
break;
case PF_DCP_SUB_CONTROL_RESPONSE:
/* Can't set this */
break;
case PF_DCP_SUB_CONTROL_FACTORY_RESET:
case PF_DCP_SUB_CONTROL_RESET_TO_FACTORY:
if (
pf_cmina_dcp_set_ind (
net,
opt,
sub,
block_qualifier,
value_length,
p_value,
&response_data[2]) != 0)
{
LOG_ERROR (
PF_DCP_LOG,
"DCP(%d): Could not perform Factory Reset\n",
__LINE__);
}
else
{
ret = 0;
}
break;
default:
break;
}
break;
case PF_DCP_OPT_DEVICE_INITIATIVE:
default:
/* Can't set this */
response_data[2] = PF_DCP_BLOCK_ERROR_OPTION_NOT_SUPPORTED;
break;
}
if (ret == 0)
{
response_data[2] = PF_DCP_BLOCK_ERROR_NO_ERROR;
}
/* SetResBlock and SetNegResBlock consists of:
* - option = Control 1 byte
* - suboption = Response 1 byte
* - block length 2 bytes
* - type of option involved (option + suboption) 2 bytes
* - block error 1 byte (=0 when no error)
*/
(void)pf_dcp_put_block (
p_dst,
p_dst_pos,
dst_max,
PF_DCP_OPT_CONTROL,
PF_DCP_SUB_CONTROL_RESPONSE,
false,
0,
sizeof (response_data),
response_data);
return ret;
}
/**
* @internal
* Handle a DCP Set or Get request.
*
* Triggers the application callback \a pnet_reset_ind() for some values.
* Might set the IP address.
*
* This is a callback for the frame handler. Arguments should fulfill
* pf_eth_frame_handler_t
*
* @param net InOut: The p-net stack instance
* @param frame_id In: The frame id.
* @param p_buf InOut: The Ethernet frame. Will be freed.
* @param frame_id_pos In: Position of the frame id in the buffer.
* @param p_arg In: Not used.
* @return 0 If the frame was NOT handled by this function.
* 1 If the frame was handled and the buffer freed.
*/
static int pf_dcp_get_set (
pnet_t * net,
uint16_t frame_id,
pnal_buf_t * p_buf,
uint16_t frame_id_pos,
void * p_arg)
{
uint8_t * p_src;
uint16_t src_pos;
uint16_t src_dcplen;
uint16_t src_block_len;
uint16_t src_block_qualifier;
pf_ethhdr_t * p_src_ethhdr;
pf_dcp_header_t * p_src_dcphdr;
pf_dcp_block_hdr_t * p_src_block_hdr;
pnal_buf_t * p_rsp = NULL;
uint8_t * p_dst;
uint16_t dst_pos;
uint16_t dst_start;
pf_ethhdr_t * p_dst_ethhdr;
pf_dcp_header_t * p_dst_dcphdr;
const pnet_ethaddr_t * mac_address = pf_cmina_get_device_macaddr (net);
if (p_buf != NULL)
{
/* Extract info from the request */
p_src = (uint8_t *)p_buf->payload;
src_pos = 0;
p_src_ethhdr = (pf_ethhdr_t *)&p_src[src_pos];
src_pos = frame_id_pos;
src_pos += sizeof (uint16_t); /* FrameId */
p_src_dcphdr = (pf_dcp_header_t *)&p_src[src_pos];
src_pos += sizeof (pf_dcp_header_t);
src_dcplen = (src_pos + ntohs (p_src_dcphdr->data_length));
p_rsp = pnal_buf_alloc (PF_FRAME_BUFFER_SIZE); /* Get a transmit buffer
for the response */
if (
(p_rsp != NULL) &&
(pf_dcp_check_sam (net, &p_src_ethhdr->src) == true) &&
pf_dcp_check_destination_address (mac_address, &p_src_ethhdr->dest))
{
/* Prepare the response */
p_dst = (uint8_t *)p_rsp->payload;
dst_pos = 0;
p_dst_ethhdr = (pf_ethhdr_t *)&p_dst[dst_pos];
dst_pos += sizeof (pf_ethhdr_t);
/* Insert frame ID into response */
p_dst[dst_pos++] = ((PF_DCP_GET_SET_FRAME_ID & 0xff00) >> 8);
p_dst[dst_pos++] = PF_DCP_GET_SET_FRAME_ID & 0xff;
p_dst_dcphdr = (pf_dcp_header_t *)&p_dst[dst_pos];
dst_pos += sizeof (pf_dcp_header_t);
/* Save position for later */
dst_start = dst_pos;
/* Set eth header in the response */
memcpy (
p_dst_ethhdr->dest.addr,
p_src_ethhdr->src.addr,
sizeof (pnet_ethaddr_t));
memcpy (
p_dst_ethhdr->src.addr,
mac_address->addr,
sizeof (pnet_ethaddr_t));
p_dst_ethhdr->type = htons (PNAL_ETHTYPE_PROFINET);