-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathpnet_api.c
651 lines (568 loc) · 12.8 KB
/
pnet_api.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
/*********************************************************************
* _ _ _
* _ __ | |_ _ | | __ _ | |__ ___
* | '__|| __|(_)| | / _` || '_ \ / __|
* | | | |_ _ | || (_| || |_) |\__ \
* |_| \__|(_)|_| \__,_||_.__/ |___/
*
* 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.
********************************************************************/
#ifdef UNIT_TEST
#define pnal_snmp_init mock_pnal_snmp_init
#endif
#include <stdlib.h>
#include <string.h>
#include "pf_includes.h"
#include "pf_block_reader.h"
int pnet_init_only (pnet_t * net, const pnet_cfg_t * p_cfg)
{
memset (net, 0, sizeof (*net));
net->cmdev_initialized = false; /* TODO How to handle that pf_cmdev_exit()
is used before pf_cmdev_init()? */
pf_cpm_init (net);
pf_ppm_init (net);
pf_alarm_init (net);
/* initialize configuration */
if (pf_fspm_init (net, p_cfg) != 0)
{
return -1;
}
if (pf_eth_init (net, p_cfg) != 0)
{
LOG_ERROR (PNET_LOG, "Failed to initialise network interfaces\n");
return -1;
}
pf_scheduler_init (net, p_cfg->tick_us);
pf_cmina_init (net); /* Read from permanent pool */
pf_dcp_exit (net); /* Prepare for re-init. */
pf_dcp_init (net); /* Start DCP */
pf_port_init (net);
pf_port_main_interface_init (net);
pf_lldp_init (net);
pf_pdport_init (net);
/* Configure SNMP server if enabled */
#if PNET_OPTION_SNMP == 1
if (pnal_snmp_init (net, &p_cfg->pnal_cfg) != 0)
{
LOG_ERROR (PNET_LOG, "Failed to configure SNMP\n");
return -1;
}
#endif
pf_cmdev_exit (net); /* Prepare for re-init */
pf_cmdev_init (net);
pf_cmrpc_init (net);
return 0;
}
void pnet_free_members (pnet_t * net)
{
pf_fspm_exit (net);
pf_scheduler_exit (net);
pf_lldp_exit (net);
pf_cmdev_exit (net);
pf_cmrpc_exit (net);
}
pnet_t * pnet_init (const pnet_cfg_t * p_cfg)
{
pnet_t * net = NULL;
net = os_malloc (sizeof (*net));
if (net == NULL)
{
LOG_ERROR (
PNET_LOG,
"Failed to allocate memory for pnet_t (%zu bytes)\n",
sizeof (*net));
return NULL;
}
if (pnet_init_only (net, p_cfg) != 0)
{
free (net);
return NULL;
}
return net;
}
void pnet_handle_periodic (pnet_t * net)
{
pf_cmrpc_periodic (net);
pf_alarm_periodic (net);
/* Handle expired timeout events */
pf_scheduler_tick (net);
pf_pdport_periodic (net);
}
void pnet_show (pnet_t * net, unsigned level)
{
if (net != NULL)
{
if (level & 0x0010)
{
pf_fspm_option_show (net);
}
if (level & 0x0020)
{
pf_cmdev_device_show (net);
}
pf_cmrpc_show (net, level);
if (level & 0x0200)
{
pf_cmdev_diag_show (net);
}
if (level & 0x0400)
{
pf_fspm_logbook_show (net);
}
if (level & 0x2000)
{
printf ("\n\n");
pf_cmina_show (net);
}
if (level & 0x4000)
{
printf ("\n\n");
pf_scheduler_show (net);
}
if (level & 0x8000)
{
printf ("\n\n");
pf_fspm_im_show (net);
}
}
else
{
printf ("p-net not yet initialized.\n");
}
}
void pnet_create_log_book_entry (
pnet_t * net,
uint32_t arep,
const pnet_pnio_status_t * p_pnio_status,
uint32_t entry_detail)
{
pf_fspm_create_log_book_entry (net, arep, p_pnio_status, entry_detail);
}
int pnet_input_set_data_and_iops (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
const uint8_t * p_data,
uint16_t data_len,
uint8_t iops)
{
uint8_t iops_len = 1;
return pf_ppm_set_data_and_iops (
net,
api,
slot,
subslot,
p_data,
data_len,
&iops,
iops_len);
}
int pnet_input_get_iocs (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
uint8_t * p_iocs)
{
uint8_t iocs_len = 1;
return pf_cpm_get_iocs (net, api, slot, subslot, p_iocs, &iocs_len);
}
int pnet_output_get_data_and_iops (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
bool * p_new_flag,
uint8_t * p_data,
uint16_t * p_data_len,
uint8_t * p_iops)
{
uint8_t iops_len = 1;
return pf_cpm_get_data_and_iops (
net,
api,
slot,
subslot,
p_new_flag,
p_data,
p_data_len,
p_iops,
&iops_len);
}
int pnet_output_set_iocs (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
uint8_t iocs)
{
uint8_t iocs_len = 1;
return pf_ppm_set_iocs (net, api, slot, subslot, &iocs, iocs_len);
}
int pnet_plug_module (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint32_t module_ident)
{
return pf_cmdev_plug_module (net, api, slot, module_ident);
}
int pnet_plug_submodule (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
uint32_t module_ident,
uint32_t submodule_ident,
pnet_submodule_dir_t direction,
uint16_t length_input,
uint16_t length_output)
{
return pf_cmdev_plug_submodule (
net,
api,
slot,
subslot,
module_ident,
submodule_ident,
direction,
length_input,
length_output,
false);
}
int pnet_pull_module (pnet_t * net, uint32_t api, uint16_t slot)
{
return pf_cmdev_pull_module (net, api, slot);
}
int pnet_pull_submodule (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot)
{
return pf_cmdev_pull_submodule (net, api, slot, subslot);
}
int pnet_set_primary_state (pnet_t * net, bool primary)
{
int ret = 0; /* Assume all goes well */
uint16_t ar_ix;
uint16_t cr_ix;
pf_ar_t * p_ar = NULL;
for (ar_ix = 0; ar_ix < PNET_MAX_AR; ar_ix++)
{
p_ar = pf_ar_find_by_index (net, ar_ix);
if ((p_ar != NULL) && (p_ar->in_use == true))
{
for (cr_ix = 0; cr_ix < p_ar->nbr_iocrs; cr_ix++)
{
if (pf_ppm_set_data_status_state (p_ar, cr_ix, primary) != 0)
{
ret = -1;
}
}
}
}
return ret;
}
int pnet_set_redundancy_state (pnet_t * net, bool redundant)
{
int ret = 0; /* Assume all goes well */
uint16_t ar_ix;
uint16_t cr_ix;
pf_ar_t * p_ar = NULL;
for (ar_ix = 0; ar_ix < PNET_MAX_AR; ar_ix++)
{
p_ar = pf_ar_find_by_index (net, ar_ix);
if ((p_ar != NULL) && (p_ar->in_use == true))
{
for (cr_ix = 0; cr_ix < p_ar->nbr_iocrs; cr_ix++)
{
if (pf_ppm_set_data_status_redundancy (p_ar, cr_ix, redundant) != 0)
{
ret = -1;
}
}
}
}
return ret;
}
int pnet_set_provider_state (pnet_t * net, bool run)
{
int ret = 0; /* Assume all goes well */
uint16_t ar_ix;
uint16_t cr_ix;
pf_ar_t * p_ar = NULL;
for (ar_ix = 0; ar_ix < PNET_MAX_AR; ar_ix++)
{
p_ar = pf_ar_find_by_index (net, ar_ix);
if ((p_ar != NULL) && (p_ar->in_use == true))
{
for (cr_ix = 0; cr_ix < p_ar->nbr_iocrs; cr_ix++)
{
if (pf_ppm_set_data_status_provider (p_ar, cr_ix, run) != 0)
{
ret = -1;
}
}
}
}
return ret;
}
int pnet_application_ready (pnet_t * net, uint32_t arep)
{
int ret = -1;
pf_ar_t * p_ar = NULL;
if (pf_ar_find_by_arep (net, arep, &p_ar) == 0)
{
ret = pf_cmdev_cm_ccontrol_req (net, p_ar);
}
return ret;
}
int pnet_ar_abort (pnet_t * net, uint32_t arep)
{
int ret = -1;
pf_ar_t * p_ar = NULL;
if (pf_ar_find_by_arep (net, arep, &p_ar) == 0)
{
ret = pf_cmdev_cm_abort (net, p_ar);
}
return ret;
}
int pnet_factory_reset (pnet_t * net)
{
uint16_t ix;
pf_ar_t * p_ar = NULL;
/* Look for active connections */
for (ix = 0; ix < PNET_MAX_AR; ix++)
{
p_ar = pf_ar_find_by_index (net, ix);
if ((p_ar != NULL) && (p_ar->in_use == true))
{
(void)pf_cmdev_cm_abort (net, p_ar);
}
}
(void)pf_cmina_set_default_cfg (net, 99);
pf_cmina_dcp_set_commit (net);
return 0;
}
int pnet_remove_data_files (const char * file_directory)
{
return pf_cmina_remove_all_data_files (file_directory);
}
int pnet_get_ar_error_codes (
pnet_t * net,
uint32_t arep,
uint16_t * p_err_cls,
uint16_t * p_err_code)
{
int ret = -1;
pf_ar_t * p_ar = NULL;
if (pf_ar_find_by_arep (net, arep, &p_ar) == 0)
{
*p_err_cls = p_ar->err_cls;
*p_err_code = p_ar->err_code;
ret = 0;
}
return ret;
}
int pnet_alarm_send_process_alarm (
pnet_t * net,
uint32_t arep,
uint32_t api,
uint16_t slot,
uint16_t subslot,
uint16_t payload_usi,
uint16_t payload_len,
const uint8_t * p_payload)
{
int ret = -1;
pf_ar_t * p_ar = NULL;
if (pf_ar_find_by_arep (net, arep, &p_ar) == 0)
{
ret = pf_alarm_send_process (
net,
p_ar,
api,
slot,
subslot,
payload_usi,
payload_len,
p_payload);
}
return ret;
}
int pnet_alarm_send_ack (
pnet_t * net,
uint32_t arep,
const pnet_alarm_argument_t * p_alarm_argument,
const pnet_pnio_status_t * p_pnio_status)
{
int ret = -1;
pf_ar_t * p_ar = NULL;
if (pf_ar_find_by_arep (net, arep, &p_ar) == 0)
{
ret =
pf_alarm_alpmr_alarm_ack (net, p_ar, p_alarm_argument, p_pnio_status);
}
return ret;
}
/************************** Low-level diagnosis functions ******************/
int pnet_diag_add (
pnet_t * net,
const pnet_diag_source_t * p_diag_source,
pnet_diag_ch_prop_type_values_t ch_bits,
pnet_diag_ch_prop_maint_values_t severity,
uint16_t ch_error_type,
uint16_t ext_ch_error_type,
uint32_t ext_ch_add_value,
uint32_t qual_ch_qualifier,
uint16_t usi,
uint16_t manuf_data_len,
const uint8_t * p_manuf_data)
{
return pf_diag_add (
net,
p_diag_source,
ch_bits,
severity,
ch_error_type,
ext_ch_error_type,
ext_ch_add_value,
qual_ch_qualifier,
usi,
manuf_data_len,
p_manuf_data);
}
int pnet_diag_update (
pnet_t * net,
const pnet_diag_source_t * p_diag_source,
uint16_t ch_error_type,
uint16_t ext_ch_error_type,
uint32_t ext_ch_add_value,
uint16_t usi,
uint16_t manuf_data_len,
const uint8_t * p_manuf_data)
{
return pf_diag_update (
net,
p_diag_source,
ch_error_type,
ext_ch_error_type,
ext_ch_add_value,
usi,
manuf_data_len,
p_manuf_data);
}
int pnet_diag_remove (
pnet_t * net,
const pnet_diag_source_t * p_diag_source,
uint16_t ch_error_type,
uint16_t ext_ch_error_type,
uint16_t usi)
{
return pf_diag_remove (
net,
p_diag_source,
ch_error_type,
ext_ch_error_type,
usi);
}
/************************** Diagnosis in standard format *******************/
int pnet_diag_std_add (
pnet_t * net,
const pnet_diag_source_t * p_diag_source,
pnet_diag_ch_prop_type_values_t ch_bits,
pnet_diag_ch_prop_maint_values_t severity,
uint16_t ch_error_type,
uint16_t ext_ch_error_type,
uint32_t ext_ch_add_value,
uint32_t qual_ch_qualifier)
{
return pf_diag_std_add (
net,
p_diag_source,
ch_bits,
severity,
ch_error_type,
ext_ch_error_type,
ext_ch_add_value,
qual_ch_qualifier);
}
int pnet_diag_std_update (
pnet_t * net,
const pnet_diag_source_t * p_diag_source,
uint16_t ch_error_type,
uint16_t ext_ch_error_type,
uint32_t ext_ch_add_value)
{
return pf_diag_std_update (
net,
p_diag_source,
ch_error_type,
ext_ch_error_type,
ext_ch_add_value);
}
int pnet_diag_std_remove (
pnet_t * net,
const pnet_diag_source_t * p_diag_source,
uint16_t ch_error_type,
uint16_t ext_ch_error_type)
{
return pf_diag_std_remove (
net,
p_diag_source,
ch_error_type,
ext_ch_error_type);
}
/************************** Diagnosis in USI format ************************/
int pnet_diag_usi_add (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
uint16_t usi,
uint16_t manuf_data_len,
const uint8_t * p_manuf_data)
{
return pf_diag_usi_add (
net,
api,
slot,
subslot,
usi,
manuf_data_len,
p_manuf_data);
}
int pnet_diag_usi_update (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
uint16_t usi,
uint16_t manuf_data_len,
const uint8_t * p_manuf_data)
{
return pf_diag_usi_update (
net,
api,
slot,
subslot,
usi,
manuf_data_len,
p_manuf_data);
}
int pnet_diag_usi_remove (
pnet_t * net,
uint32_t api,
uint16_t slot,
uint16_t subslot,
uint16_t usi)
{
return pf_diag_usi_remove (net, api, slot, subslot, usi);
}