-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomSNMPApp.c
1956 lines (1737 loc) · 59.9 KB
/
CustomSNMPApp.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
/*********************************************************************
*
* Application to Demo SNMP Server
* Support for SNMP module in Microchip TCP/IP Stack
* - Implements the SNMP application
*
*********************************************************************
* FileName: CustomSNMPApp.c
* Dependencies: TCP/IP stack
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.05 or higher
* Microchip C30 v3.12 or higher
* Microchip C18 v3.30 or higher
* HI-TECH PICC-18 PRO 9.63PL2 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2009 Microchip Technology Inc. All rights
* reserved.
*
* Microchip licenses to you the right to use, modify, copy, and
* distribute:
* (i) the Software when embedded on a Microchip microcontroller or
* digital signal controller product ("Device") which is
* integrated into Licensee's product; or
* (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
* ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
* used in conjunction with a Microchip ethernet controller for
* the sole purpose of interfacing with the ethernet controller.
*
* You should refer to the license agreement accompanying this
* Software for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* E. Wood 4/26/08 Moved from MainDemo.c
* Amit Shirbhate 09/24/08 SNMPv2c Support added.
* Hrisikesh Sahu 04/15/10 SNMPv2 trap format support.
********************************************************************/
#define __CUSTOMSNMPAPP_C
#include "TCPIPConfig.h"
#if defined(STACK_USE_SNMP_SERVER)
#include "TCPIP Stack/TCPIP.h"
#include "MainDemo.h"
#include "TCPIP Stack/SNMP.h"
#include "TCPIP Stack/SNMPv3.h"
/****************************************************************************
Section:
Global Variables
***************************************************************************/
/*This Macro is used to provide maximum try for a failure Trap server address */
#define MAX_TRY_TO_SEND_TRAP (10)
BYTE gSendTrapFlag=FALSE;//global flag to send Trap
BYTE gOIDCorrespondingSnmpMibID=MICROCHIP;
BYTE gGenericTrapNotification=ENTERPRISE_SPECIFIC;
BYTE gSpecificTrapNotification=VENDOR_TRAP_DEFAULT; // Vendor specific trap code
#ifdef STACK_USE_SNMPV3_SERVER
static BYTE gSnmpv3UserSecurityName[USER_SECURITY_NAME_LEN];
#endif
/*
#if defined(SNMP_STACK_USE_V2_TRAP) || defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
//if gSetTrapSendFlag == FALSE then the last varbind variable for
//multiple varbind variable pdu structure or if there is only varbind variable send.
// if gSetTrapSendFlag == TRUE, then v2 trap pdu is expecting more varbind variable.
BYTE gSetTrapSendFlag = FALSE;
#endif
*/
BYTE gSetTrapSendFlag = FALSE;
/*Initialize trap table with no entries.*/
TRAP_INFO trapInfo = { TRAP_TABLE_SIZE };
static DWORD SNMPGetTimeStamp(void);
static BOOL SendNotification(BYTE receiverIndex, SNMP_ID var, SNMP_VAL val,UINT8 targetIndex);
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
UINT8 gSendTrapSMstate=0;
#endif
/****************************************************************************
===========================================================================
Section:
SNMP Routines
===========================================================================
***************************************************************************/
#if !defined(SNMP_TRAP_DISABLED)
/****************************************************************************
Function:
BOOL SendNotification(BYTE receiverIndex, SNMP_ID var, SNMP_VAL val,UINT8 targetIndex)
Summary:
Prepare, validate remote node which will receive trap and send trap pdu.
Description:
This routine prepares the trap notification pdu, sends ARP and get
remote device MAC address to which notification to sent, sends
the notification.
Notofication state machine is getting updated if there is any ARP resolution failure for
a perticular trap destination address.
PreCondition:
SNMPTrapDemo() is called.
parameters:
receiverIndex - The index to array where remote ip address is stored.
var - SNMP var ID that is to be used in notification
val - Value of var. Only value of BYTE, WORD or DWORD can be sent.
targetIndex - snmpv3 target index
Return Values:
TRUE - If notification send is successful.
FALSE - If send notification failed.
Remarks:
None.
*************************************************************************/
static BOOL SendNotification(BYTE receiverIndex, SNMP_ID var, SNMP_VAL val,UINT8 targetIndex)
{
static enum { SM_PREPARE, SM_NOTIFY_WAIT } smState = SM_PREPARE;
IP_ADDR IPAddress;
static BYTE tempRxIndex;
static IP_ADDR tempIpAddress;
// Convert local to network order.
IPAddress.v[0] = trapInfo.table[receiverIndex].IPAddress.v[3];
IPAddress.v[1] = trapInfo.table[receiverIndex].IPAddress.v[2];
IPAddress.v[2] = trapInfo.table[receiverIndex].IPAddress.v[1];
IPAddress.v[3] = trapInfo.table[receiverIndex].IPAddress.v[0];
switch(smState)
{
case SM_PREPARE:
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
gSendTrapSMstate = smState;
#endif
tempRxIndex=receiverIndex;
// Convert local to network order.
tempIpAddress.v[0] = trapInfo.table[receiverIndex].IPAddress.v[3];
tempIpAddress.v[1] = trapInfo.table[receiverIndex].IPAddress.v[2];
tempIpAddress.v[2] = trapInfo.table[receiverIndex].IPAddress.v[1];
tempIpAddress.v[3] = trapInfo.table[receiverIndex].IPAddress.v[0];
SNMPNotifyPrepare(&IPAddress,
trapInfo.table[receiverIndex].community,
trapInfo.table[receiverIndex].communityLen,
MICROCHIP, // Agent ID Var
gSpecificTrapNotification, // Specifc Trap notification code
SNMPGetTimeStamp());
smState = SM_NOTIFY_WAIT;
break;
case SM_NOTIFY_WAIT:
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
gSendTrapSMstate = smState;
#endif
if ( SNMPIsNotifyReady(&IPAddress) )
{
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
if(targetIndex == (SNMPV3_USM_MAX_USER-1))
smState = SM_PREPARE;
if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV3_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV3_USM_SECURITY_MODEL))
Snmpv3Notify(var, val, 0,targetIndex);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV2C_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV2C_SECURITY_MODEL))
SNMPNotify(var, val, 0);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV1_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV1_SECURITY_MODEL))
SNMPNotify(var, val, 0);
else
return TRUE;
#else
smState = SM_PREPARE;
SNMPNotify(var, val, 0);
#endif
return TRUE;
}
/* if trapInfo table address for a perticular index is different comparing to the SM_PREPARE IP address
then change the state to SM_PREPARE*/
if((tempIpAddress.Val != IPAddress.Val) &&
(tempRxIndex == receiverIndex))
{
smState = SM_PREPARE;
}
/* Change state machine from SM_NOTIFY_WAIT to SM_PREPARE if incoming trap destination
index is different from the SM_PREPARE trap destination index*/
if(tempRxIndex != receiverIndex)
smState=SM_PREPARE;
}
return FALSE;
}
#if defined(SNMP_STACK_USE_V2_TRAP) || defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
/**************************************************************************
Function:
void SNMPV2TrapDemo(void)
Summary:
Send SNMP V2 notification with multiple varbinds.
Description:
This routine sends a trap v2 pdu with multiple varbind variables
for the predefined ip addresses with the agent. And as per RFC1905
the first two variable bindings in the varbind pdu list of an
SNMPv2-Trap-PDU are sysUpTime.0 and snmpTrapOID.0 respectively.
To support multiple varbind, user need to call SendNotification()
for the first varbind variable and SendNotification() will do the
arp resolve and adds sysUpTime.0 and snmpTrapOID.0 variable to
the pdu. For the second varbind variable onwards user need to
call only SNMPNotify().
In this demo , snmpv2 trap includes ANALOG_POT0,PUSH_BUTTON and LED_D5
variable bindains and this trap can be generated by using portmeter value.
and SNMPv2-Trap-PDU will be generated only when pot meter reading exceeds 501.
gSetTrapSendFlag Should be set to TRUE when user is trying to send first
variable binding and gSetTrapSendFlag should be set to FALSE before
sending the last variable binding.
* if user is sending only one variable binding then
* gSetTrapSendFlag should be set to False.
* user can add more variable bindings.
PreCondition:
Application defined event occurs to send the trap.
parameters:
None.
Returns:
None.
Remarks:
This routine guides how to build a event generated trap notification.
*************************************************************************/
void SNMPV2TrapDemo(void)
{
static DWORD tempTimerRead = 0;
static BYTE trapIndex=0;
static SNMP_VAL analogPotVal;
static BYTE potReadLock = FALSE;
static BYTE timeLock = FALSE;
static BYTE maxTryToSendTrap=0;
UINT8 targetIndex = 0;
UINT8 retVal = 0;
if(timeLock==(BYTE)FALSE)
{
tempTimerRead=TickGet();
timeLock=TRUE;
}
for(;trapIndex<TRAP_TABLE_SIZE;trapIndex++)
{
if(!trapInfo.table[trapIndex].Flags.bEnabled)
continue;
//Read POT reading once and send trap to all configured recipient
if(potReadLock ==(BYTE)FALSE)
{
#if defined(__18CXX)
// Wait until A/D conversion is done
ADCON0bits.GO = 1;
while(ADCON0bits.GO);
// Convert 10-bit value into ASCII string
analogPotVal.word= (WORD)ADRES;
#else
analogPotVal.word= (WORD)ADC1BUF0;
#endif
potReadLock = TRUE;
}
// if(analogPotVal.word >512u)
if(analogPotVal.word >12u)
{
/*
* prepare and send multivarbind pdu using pot meter value.
* for SNMP v2 trap sysUpTime.0 and SNMPv2TrapOID.0 are mandatory
* apart from these varbinds, push button and potmeter OID are included
* to this pdu.
*/
//gSpecificTrapNotification = 1; //expecting 1 should be the specific trap.
//gGenericTrapNotification = ENTERPRISE_SPECIFIC;
//gSetTrapSendFlag = TRUE;
// insert ANALOG_POT0 OID value and OID to the varbind pdu
//set global flag gSetTrapSendFlag to TRUE , it signifies that there are more than one
// variable need to be the part of SNMP v2 TRAP.
// if there is only varbind variable to be the part of SNMP v2 trap,
// then user should set gSetTrapSendFlag to FALSE.
//gSetTrapSendFlag = FALSE;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
for(targetIndex=0;targetIndex<SNMPV3_USM_MAX_USER;targetIndex++)
{
#endif
gSpecificTrapNotification = 1; //expecting 1 should be the specific trap.
gGenericTrapNotification = ENTERPRISE_SPECIFIC;
gSetTrapSendFlag = TRUE;
retVal =SendNotification(trapIndex,ANALOG_POT0,analogPotVal,targetIndex);
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
if((gSendTrapSMstate == 0x0) && (retVal == FALSE)) // gSendTrapSMstate == SM_PREPARE
{
retVal = SendNotification(trapIndex, ANALOG_POT0, analogPotVal,targetIndex);
}
if((retVal==TRUE)&& (targetIndex == (SNMPV3_USM_MAX_USER-1)))
trapIndex++;
else if(retVal == FALSE)
#else
if(retVal == FALSE)
#endif
{
if(maxTryToSendTrap >= MAX_TRY_TO_SEND_TRAP)
{
trapIndex++;
maxTryToSendTrap = 0;
return;
}
maxTryToSendTrap++;
return ;
}
//prepare PUSH_BUTTON trap .for the next trap varbind we need to use snmp_notify instead of
// SendNotification(), because we have already prepared SNMP v2 trap header
//and arp has been resolved already.
analogPotVal.byte = BUTTON0_IO;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV3_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV3_USM_SECURITY_MODEL))
Snmpv3Notify(PUSH_BUTTON,analogPotVal,0,targetIndex);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV2C_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV2C_SECURITY_MODEL))
SNMPNotify(PUSH_BUTTON,analogPotVal,0);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV1_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV1_SECURITY_MODEL))
SNMPNotify(PUSH_BUTTON,analogPotVal,0);
else
continue;
//Snmpv3Notify(PUSH_BUTTON,analogPotVal,0);
#else
SNMPNotify(PUSH_BUTTON,analogPotVal,0);
#endif
// if this is the last trap variable need to be the part of SNMP v2 Trap,
// then we should disable gSetTrapSendFlag to FALSE
gSetTrapSendFlag = FALSE;
analogPotVal.byte = LED0_IO;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
//Snmpv3Notify(LED_D5,analogPotVal,0);
if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV3_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV3_USM_SECURITY_MODEL))
Snmpv3Notify(LED_D5,analogPotVal,0,targetIndex);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV2C_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV2C_SECURITY_MODEL))
SNMPNotify(LED_D5,analogPotVal,0);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV1_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV1_SECURITY_MODEL))
SNMPNotify(LED_D5,analogPotVal,0);
#if 0
if(targetIndex == )
{
if(maxTryToSendTrap >= MAX_TRY_TO_SEND_TRAP)
{
trapIndex++;
maxTryToSendTrap = 0;
return;
}
maxTryToSendTrap++;
return ;
}
#endif
#else
SNMPNotify(LED_D5,analogPotVal,0);
#endif
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
}
#endif
}
}
//Try for max 5 seconds to send TRAP, do not get block in while()
if((TickGet()-tempTimerRead)>(5*TICK_SECOND))
{
UDPDiscard();
potReadLock = FALSE;
timeLock = FALSE;
trapIndex = 0;
analogPotVal.word = 0;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
gSendTrapSMstate = 0;
#endif
return;
}
}
#endif /* SNMP_STACK_USE_V2_TRAP || SNMP_V1_V2_TRAP_WITH_SNMPV3 */
/**************************************************************************
Function:
void SNMPTrapDemo(void)
Summary:
Send trap pdu demo application.
Description:
This routine sends a trap pdu for the predefined ip addresses with the
agent. The "event" to generate this trap pdu is "BUTTON_PUSH_EVENT". Whenever
there occurs a specific button push, this routine is called and sends
a trap containing PUSH_BUTTON mib var OID and notification type
as authentication failure.
PreCondition:
Application defined event occurs to send the trap.
parameters:
None.
Returns:
None.
Remarks:
This routine guides how to build a event generated trap notification.
The application should make use of SNMPSendTrap() routine to generate
and send trap.
*************************************************************************/
void SNMPTrapDemo(void)
{
static DWORD TimerRead=0;
static BOOL analogPotNotify = FALSE,buttonPushNotify=FALSE;
static BYTE anaPotNotfyCntr=0,buttonPushNotfyCntr=0;
static SNMP_VAL buttonPushval,analogPotVal;
static BYTE potReadLock=FALSE,buttonLock=FALSE;
static BYTE timeLock=FALSE;
static BYTE maxTryToSendTrap=0;
UINT8 targetIndex;
BOOL retVal=TRUE;
targetIndex = 0;
if(timeLock==(BYTE)FALSE)
{
TimerRead=TickGet();
timeLock=TRUE;
}
#if 1
if(anaPotNotfyCntr >= trapInfo.Size)
{
anaPotNotfyCntr = 0;
potReadLock=FALSE;
//analogPotNotify = FALSE;
analogPotNotify = TRUE;
}
#endif
if(!analogPotNotify)
{
//Read POT reading once and send trap to all configured recipient
if(potReadLock ==(BYTE)FALSE)
{
#if defined(__18CXX)
// Wait until A/D conversion is done
ADCON0bits.GO = 1;
while(ADCON0bits.GO);
// Convert 10-bit value into ASCII string
analogPotVal.word= (WORD)ADRES;
#else
analogPotVal.word= (WORD)ADC1BUF0;
#endif
//Avoids Reading POT for every iteration unless trap sent to each configured recipients
potReadLock=TRUE;
}
if(trapInfo.table[anaPotNotfyCntr].Flags.bEnabled)
{
if(analogPotVal.word >12u)
{
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
for(targetIndex=0;targetIndex<SNMPV3_USM_MAX_USER;targetIndex++)
{
#endif
gSpecificTrapNotification=POT_READING_MORE_512;
gGenericTrapNotification=ENTERPRISE_SPECIFIC;
gSetTrapSendFlag = FALSE;
retVal = SendNotification(anaPotNotfyCntr, ANALOG_POT0, analogPotVal,targetIndex);
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
if((gSendTrapSMstate == 0x0) && (retVal == FALSE)) // gSendTrapSMstate == SM_PREPARE
{
retVal = SendNotification(anaPotNotfyCntr, ANALOG_POT0, analogPotVal,targetIndex);
}
else if((retVal == TRUE) && (targetIndex == (SNMPV3_USM_MAX_USER-1)))
anaPotNotfyCntr++;
else if(retVal == FALSE)
#else
if(retVal == TRUE)
anaPotNotfyCntr++;
else
#endif
{
if(maxTryToSendTrap>=MAX_TRY_TO_SEND_TRAP)
{
anaPotNotfyCntr++;
maxTryToSendTrap = 0;
return;
}
maxTryToSendTrap++;
return ;
}
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
}
#endif
}
}
else
anaPotNotfyCntr++;
}
if(buttonPushNotfyCntr==trapInfo.Size)
{
buttonPushNotfyCntr = 0;
buttonLock=FALSE;
buttonPushNotify = FALSE;
}
if(buttonLock == (BYTE)FALSE)
{
if(BUTTON3_IO == 0u)
{
buttonPushNotify = TRUE;
buttonLock =TRUE;
}
}
if(buttonPushNotify)
{
buttonPushval.byte = 0;
if ( trapInfo.table[buttonPushNotfyCntr].Flags.bEnabled )
{
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
for(targetIndex=0;targetIndex<SNMPV3_USM_MAX_USER;targetIndex++)
{
#endif
gSpecificTrapNotification=POT_READING_MORE_512;
gGenericTrapNotification=ENTERPRISE_SPECIFIC;
gSetTrapSendFlag = FALSE;
retVal = SendNotification(buttonPushNotfyCntr, PUSH_BUTTON, buttonPushval,targetIndex);
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
if((gSendTrapSMstate == 0x0) && (retVal == FALSE)) // gSendTrapSMstate == SM_PREPARE
{
retVal = SendNotification(anaPotNotfyCntr, ANALOG_POT0, analogPotVal,targetIndex);
}
if((retVal==TRUE)&& (targetIndex == (SNMPV3_USM_MAX_USER-1)))
#else
if(retVal==TRUE)
#endif
buttonPushNotfyCntr++;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
}
#endif
}
else
buttonPushNotfyCntr++;
}
//Try for max 5 seconds to send TRAP, do not get block in while()
if((TickGet()-TimerRead)>(5*TICK_SECOND))
{
UDPDiscard();
buttonPushNotfyCntr = 0;
buttonLock=FALSE;
buttonPushNotify = FALSE;
anaPotNotfyCntr = 0;
potReadLock=FALSE;
analogPotNotify = FALSE;
timeLock=FALSE;
gSpecificTrapNotification=VENDOR_TRAP_DEFAULT;
gGenericTrapNotification=ENTERPRISE_SPECIFIC;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
gSendTrapSMstate = 0;
#endif
return;
}
}
/**************************************************************************
Function:
void SNMPSendTrap(void)
Summary:
Prepare, validate remote node which will receive trap and send trap pdu.
Description:
This function is used to send trap notification to previously
configured ip address if trap notification is enabled. There are
different trap notification code. The current implementation
sends trap for authentication failure (4).
PreCondition:
If application defined event occurs to send the trap.
parameters:
None.
Returns:
None.
Remarks:
This is a callback function called by the application on certain
predefined events. This routine only implemented to send a
authentication failure Notification-type macro with PUSH_BUTTON
oid stored in MPFS. If the ARP is no resolved i.e. if
SNMPIsNotifyReady() returns FALSE, this routine times
out in 5 seconds. This routine should be modified according to
event occured and should update corrsponding OID and notification
type to the trap pdu.
*************************************************************************/
void SNMPSendTrap(void)
{
static BYTE timeLock=FALSE;
static BYTE receiverIndex=0; ///is application specific
IP_ADDR remHostIPAddress,* remHostIpAddrPtr;
SNMP_VAL val;
static DWORD TimerRead;
static enum
{
SM_PREPARE,
SM_NOTIFY_WAIT
} smState = SM_PREPARE;
if(trapInfo.table[receiverIndex].Flags.bEnabled)
{
remHostIPAddress.v[0] = trapInfo.table[receiverIndex].IPAddress.v[3];
remHostIPAddress.v[1] = trapInfo.table[receiverIndex].IPAddress.v[2];
remHostIPAddress.v[2] = trapInfo.table[receiverIndex].IPAddress.v[1];
remHostIPAddress.v[3] = trapInfo.table[receiverIndex].IPAddress.v[0];
remHostIpAddrPtr = &remHostIPAddress;
if(timeLock==(BYTE)FALSE)
{
TimerRead=TickGet();
timeLock=TRUE;
}
}
else
{
receiverIndex++;
if((receiverIndex == (BYTE)TRAP_TABLE_SIZE))
{
receiverIndex=0;
timeLock=FALSE;
gSendTrapFlag=FALSE;
UDPDiscard();
}
return;
}
switch(smState)
{
case SM_PREPARE:
SNMPNotifyPrepare(remHostIpAddrPtr,trapInfo.table[receiverIndex].community,
trapInfo.table[receiverIndex].communityLen,
MICROCHIP, // Agent ID Var
gSpecificTrapNotification, // Notification code.
SNMPGetTimeStamp());
smState++;
break;
case SM_NOTIFY_WAIT:
if(SNMPIsNotifyReady(remHostIpAddrPtr))
{
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
UINT8 targetIndex = 0;
#endif
smState = SM_PREPARE;
val.byte = 0;
receiverIndex++;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
for(targetIndex=0;targetIndex<SNMPV3_USM_MAX_USER;targetIndex++)
{
if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV3_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV3_USM_SECURITY_MODEL))
Snmpv3Notify(gOIDCorrespondingSnmpMibID, val, 0,targetIndex);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV2C_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV2C_SECURITY_MODEL))
SNMPNotify(gOIDCorrespondingSnmpMibID, val, 0);
else if((gSnmpv3TrapConfigData[targetIndex].messageProcessingModelType == SNMPV1_MSG_PROCESSING_MODEL)
&& (gSnmpv3TrapConfigData[targetIndex].securityModelType == SNMPV1_SECURITY_MODEL))
SNMPNotify(gOIDCorrespondingSnmpMibID, val, 0);
}
//Snmpv3Notify(PUSH_BUTTON,analogPotVal,0);
#else
SNMPNotify(gOIDCorrespondingSnmpMibID, val, 0);
#endif
//application has to decide on which SNMP var OID to send. Ex. PUSH_BUTTON
//SNMPNotify(gOIDCorrespondingSnmpMibID, val, 0);
smState = SM_PREPARE;
UDPDiscard();
break;
}
}
//Try for max 5 seconds to send TRAP, do not get block in while()
if((TickGet()-TimerRead)>(5*TICK_SECOND)|| (receiverIndex == (BYTE)TRAP_TABLE_SIZE))
{
UDPDiscard();
smState = SM_PREPARE;
receiverIndex=0;
timeLock=FALSE;
gSendTrapFlag=FALSE;
return;
}
}
#endif
/*********************************************************************
Function:
BYTE SNMPValidateCommunity(BYTE* community)
Summary:
Validates community name for access control.
Description:
This function validates the community name for the mib access to NMS.
The snmp community name received in the request pdu is validated for
read and write community names. The agent gives an access to the mib
variables only if the community matches with the predefined values.
This routine also sets a gloabal flag to send trap if authentication
failure occurs.
PreCondition:
SNMPInit is already called.
parameters:
community - Pointer to community string as sent by NMS.
Returns:
This routine returns the community validation result as
READ_COMMUNITY or WRITE_COMMUNITY or INVALID_COMMUNITY
Remarks:
This is a callback function called by module. User application must
implement this function and verify that community matches with
predefined value. This validation occurs for each NMS request.
********************************************************************/
BYTE SNMPValidateCommunity(BYTE * community)
{
BYTE i;
BYTE *ptr;
/*
If the community name is encrypted in the request from the Manager,
agent required to decrypt it to match with the community it is
configured for. The response from the agent should contain encrypted community
name using the same encryption algorithm which Manager used while
making the request.
*/
// Validate that community string is a legal size
if(strlen((char*)community) <= SNMP_COMMUNITY_MAX_LEN)
{
// Search to see if this is a write community. This is done before
// searching read communities so that full read/write access is
// granted if a read and write community name happen to be the same.
for(i = 0; i < SNMP_MAX_COMMUNITY_SUPPORT; i++)
{
ptr = AppConfig.writeCommunity[i];
if(ptr == NULL)
continue;
if(*ptr == 0x00u)
continue;
if(strncmp((char*)community, (char*)ptr, SNMP_COMMUNITY_MAX_LEN) == 0)
return WRITE_COMMUNITY;
}
// Did not find in write communities, search read communities
for(i = 0; i < SNMP_MAX_COMMUNITY_SUPPORT; i++)
{
ptr = AppConfig.readCommunity[i];
if(ptr == NULL)
continue;
if(*ptr == 0x00u)
continue;
if(strncmp((char*)community, (char*)ptr, SNMP_COMMUNITY_MAX_LEN) == 0)
return READ_COMMUNITY;
}
}
// Could not find any matching community, set up to send a trap
gSpecificTrapNotification=VENDOR_TRAP_DEFAULT;
gGenericTrapNotification=AUTH_FAILURE;
gSendTrapFlag=TRUE;
return INVALID_COMMUNITY;
}
/*********************************************************************
Function:
BOOL SNMPIsValidSetLen(SNMP_ID var, BYTE len,BYTE index)
Summary:
Validates the set variable data length to data type.
Description:
This routine is used to validate the dyanmic variable data length
to the variable data type. It is used when SET request is processed.
This is a callback function called by module. User application
must implement this function.
PreCondition:
ProcessSetVar() is called.
Parameters:
var - Variable id whose value is to be set
len - Length value that is to be validated.
index - instance of a OID
Return Values:
TRUE - if given var can be set to given len
FALSE - if otherwise.
Remarks:
This function will be called for only dynamic variables that are
defined as ASCII_STRING and OCTET_STRING (i.e. data length greater
than 4 bytes)
********************************************************************/
BOOL SNMPIsValidSetLen(SNMP_ID var, BYTE len,BYTE index)
{
switch(var)
{
case TRAP_COMMUNITY:
if ( len < (BYTE)TRAP_COMMUNITY_MAX_LEN+1 )
return TRUE;
break;
#ifdef STACK_USE_SNMPV3_SERVER
case USER_SECURITY_NAME:
if(len <= USER_SECURITY_NAME_LEN)
{
//snmpV3UserDataBase[index].userNameLength = len;
//memset(snmpV3UserDataBase[index].userName,'\0',USER_SECURITY_NAME_LEN);
memset(gSnmpv3UserSecurityName,'\0',USER_SECURITY_NAME_LEN);
return TRUE;
}
break;
case USM_AUTH_KEY:
if(len == AUTH_LOCALIZED_PASSWORD_KEY_LEN)
{
memset(snmpV3UserDataBase[index].userAuthPswdLoclizdKey,'\0',AUTH_LOCALIZED_PASSWORD_KEY_LEN);
return TRUE;
}
break;
case USM_PRIV_KEY:
if(len == PRIV_LOCALIZED_PASSWORD_KEY_LEN)
{
memset(snmpV3UserDataBase[index].userPrivPswdLoclizdKey,'\0',PRIV_LOCALIZED_PASSWORD_KEY_LEN);
return TRUE;
}
break;
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
case SNMP_TARGET_SECURITY_NAME : // 43.6.1.4.1.17095.5.1.1.4: READWRITE ASCII_STRING.
if(len <= USER_SECURITY_NAME_LEN)
{
//gSnmpv3TrapConfigData[index].userNameLength = len;
memset(gSnmpv3UserSecurityName,'\0',USER_SECURITY_NAME_LEN);
return TRUE;
}
break;
#endif
#endif
#if defined(USE_LCD)
case LCD_DISPLAY:
if ( len < sizeof(LCDText)+1 )
return TRUE;
break;
#endif
}
return FALSE;
}
/*********************************************************************
Function:
BOOL SNMPSetVar(SNMP_ID var, SNMP_INDEX index,
BYTE ref, SNMP_VAL val)
Summary:
This routine Set the mib variable with the requested value.
Description:
This is a callback function called by module for the snmp
SET request.User application must modify this function
for the new variables address.
Precondition:
ProcessVariables() is called.
Parameters:
var - Variable id whose value is to be set
ref - Variable reference used to transfer multi-byte data
0 if first byte is set otherwise nonzero value to indicate
corresponding byte being set.
val - Up to 4 byte data value.
If var data type is BYTE, variable
value is in val->byte
If var data type is WORD, variable
value is in val->word
If var data type is DWORD, variable
value is in val->dword.
If var data type is IP_ADDRESS, COUNTER32,
or GAUGE32, value is in val->dword
If var data type is OCTET_STRING, ASCII_STRING
value is in val->byte; multi-byte transfer
will be performed to transfer remaining
bytes of data.
Return Values:
TRUE - if it is OK to set more byte(s).
FALSE - if otherwise.
Remarks:
This function may get called more than once depending on number
of bytes in a specific set request for given variable.
only dynamic read-write variables needs to be handled.
********************************************************************/
BOOL SNMPSetVar(SNMP_ID var, SNMP_INDEX index, BYTE ref, SNMP_VAL val)
{
#if defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
BYTE tempUserNameLen = 0;
#endif
switch(var)
{
case LED_D5:
LED2_IO = val.byte;
return TRUE;
case LED_D6:
LED1_IO = val.byte;
return TRUE;
case TRAP_RECEIVER_IP:
// Make sure that index is within our range.
if ( index < trapInfo.Size )
{
// This is just an update to an existing entry.
trapInfo.table[index].IPAddress.Val = val.dword;
return TRUE;
}
else if ( index < (BYTE)TRAP_TABLE_SIZE )
{
// This is an addition to table.
trapInfo.table[index].IPAddress.Val = val.dword;
trapInfo.table[index].communityLen = 0;
trapInfo.Size++;
return TRUE;
}
break;
case TRAP_RECEIVER_ENABLED:
// Make sure that index is within our range.
if ( index < trapInfo.Size )
{
// Value of '1' means Enabled".
if ( val.byte == 1u )
trapInfo.table[index].Flags.bEnabled = 1;
// Value of '0' means "Disabled.
else if ( val.byte == 0u )