-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyIPFixLibESP.ino
155 lines (106 loc) · 4.1 KB
/
TinyIPFixLibESP.ino
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
#include <stdint.h>
#include <string.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include "TinyIPFixLib.h"
#define TQUEUE_SIZE 20
typedef struct Tiny_IPFIX_Fields {
uint16_t type;
TinyIPFix_Data_Field_4 field;
int is_enterprise;
uint32_t enterprise_number;
};
//queue
Tiny_IPFIX_Fields tiny_fields[TQUEUE_SIZE];
//uint16_t fields_types[TQUEUE_SIZE];
//IPFix_Data_Field_4 tiny_fields[TQUEUE_SIZE];
int tqueue_count = 0;
int tenterprise_count = 0;
struct TinyIPFix_Data_Field_4 Create_TinyIPFIX_Data_Field_4_number(uint32_t _datafield)
{
TinyIPFix_Data_Field_4 ipfixdata_data_f;
memset(&ipfixdata_data_f, 0, sizeof (TinyIPFix_Data_Field_4));
ipfixdata_data_f.DataField.number = _datafield;
tiny_swap_bytes_4(&ipfixdata_data_f.DataField);
return ipfixdata_data_f;
}
struct TinyIPFix_Data_Field_4 Create_TinyIPFIX_Data_Field_4_bytes(unsigned char byte0, unsigned char byte1, unsigned char byte2, unsigned char byte3)
{
TinyIPFix_Data_Field_4 ipfixdata_data_f;
memset(&ipfixdata_data_f, 0, sizeof (TinyIPFix_Data_Field_4));
ipfixdata_data_f.DataField.b[3] = byte3;
ipfixdata_data_f.DataField.b[2] = byte2;
ipfixdata_data_f.DataField.b[1] = byte1;
ipfixdata_data_f.DataField.b[0] = byte0;
tiny_swap_bytes_4(&ipfixdata_data_f.DataField);
return ipfixdata_data_f;
}
void enqueue_TinyIPFix_data(uint16_t type, TinyIPFix_Data_Field_4 _value)
{
if (tqueue_count < (TQUEUE_SIZE - 1))
{
tiny_fields[tqueue_count].type = type;
tiny_fields[tqueue_count].field = _value;
tiny_fields[tqueue_count].is_enterprise = 0;
tiny_fields[tqueue_count].enterprise_number = 0;
tqueue_count++;
}
}
void enqueue_TinyIPFix_data_enterprise(uint16_t type, TinyIPFix_Data_Field_4 _value, uint32_t enterprise_number)
{
if (tqueue_count < (TQUEUE_SIZE - 1))
{
tiny_fields[tqueue_count].type = type;
tiny_fields[tqueue_count].field = _value;
tiny_fields[tqueue_count].is_enterprise = 1;
tiny_fields[tqueue_count].enterprise_number = enterprise_number;
tqueue_count++;
tenterprise_count++;
}
}
void tclear_queue()
{
tqueue_count = 0;
tenterprise_count = 0;
}
void send_TinyIPFix_packet_template(WiFiUDP *udp, const char *udpAddress, const int udpPort, uint8_t template_id)
{
int countlen = tqueue_count;
TinyIPFix_Header tinyheader = Get_TinyIPFix_Header(tiny_header_length_calc(tqueue_count, tenterprise_count, 1), 1);
TinyIPFix_Set_Header settemplate = Get_TinyIPFix_Set_Header(template_id, tqueue_count);
udp->beginPacket(udpAddress, udpPort);
udp->write((byte*)&tinyheader, sizeof (TinyIPFix_Header));
udp->write((byte*)&settemplate, sizeof (TinyIPFix_Set_Header));
TinyIPFix_Data_Template_Description tinyipfixdata_temp_desc;
TinyIPFix_Data_Template_Description_Enterprise tinyipfixdata_temp_desc_ent;
for (int i = 0; i < countlen; i++)
{
//uint16_t _data_type, uint16_t _data_length
if (tiny_fields[i].is_enterprise == 1)
{
tinyipfixdata_temp_desc_ent = Get_TinyIPFIX_Template_Description_Enterprise(tiny_fields[i].type, 4, tiny_fields[i].enterprise_number, 1);
udp->write((byte*)&tinyipfixdata_temp_desc_ent, sizeof (TinyIPFix_Data_Template_Description_Enterprise));
} else {
tinyipfixdata_temp_desc = Get_TinyIPFIX_Template_Description(tiny_fields[i].type, 4);
udp->write((byte*)&tinyipfixdata_temp_desc, sizeof (TinyIPFix_Data_Template_Description));
}
}
udp->endPacket();
tclear_queue();
}
void send_TinyIPFix_packet_data(WiFiUDP *udp, const char *udpAddress, const int udpPort, uint8_t template_id)
{
int countlen = tqueue_count;
TinyIPFix_Header tinyheader = Get_TinyIPFix_Header(tiny_header_length_calc(tqueue_count, tenterprise_count, 0), 2);
TinyIPFix_Set_Header setdata = Get_TinyIPFix_Set_Header(template_id, tiny_data_length_calc(tqueue_count));
udp->beginPacket(udpAddress, udpPort);
udp->write((byte*)&tinyheader, sizeof (TinyIPFix_Header));
udp->write((byte*)&setdata, sizeof (TinyIPFix_Set_Header));
for (int i = 0; i < countlen; i++)
{
udp->write((byte*)&tiny_fields[i].field, sizeof (TinyIPFix_Data_Field_4));
}
udp->endPacket();
tiny_increment_seq_num();
tclear_queue();
}