-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathpython_parser.cpp
More file actions
205 lines (182 loc) · 6.19 KB
/
python_parser.cpp
File metadata and controls
205 lines (182 loc) · 6.19 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
/*
* Copyright (c) 2013 Croatia Control Ltd. (www.crocontrol.hr)
*
* This file is part of Asterix.
*
* Asterix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Asterix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Asterix. If not, see <http://www.gnu.org/licenses/>.
*
*
* AUTHORS: Damir Salantic, Croatia Control Ltd.
*
*/
#define LOGDEBUG(cond, ...)
#define LOGERROR(cond, ...)
#include "python_parser.h"
#include "AsterixDefinition.h"
#include "XMLParser.h"
#include "InputParser.h"
#include <sys/time.h>
static AsterixDefinition *pDefinition = NULL;
static InputParser *inputParser = NULL;
bool gFiltering = false;
bool gSynchronous = false;
const char *gAsterixDefinitionsFile = NULL;
bool gVerbose = false;
bool gForceRouting = false;
int gHeartbeat = 0;
static void debug_trace(char const *format, ...) {
/* TODO
char buffer[1024];
va_list args;
va_start (args, format);
vsnprintf (buffer, 1023, format, args);
va_end (args);
strcat(buffer, "\n");
LOGERROR(1, "%s", buffer); // TODO
*/
// TODO PyErr_SetString(PyExc_RuntimeError, buffer);
}
/*
* Initialize Asterix Python with XML configuration file
*/
int python_init(const char *xml_config_file) {
Tracer::Configure(debug_trace);
if (!pDefinition)
pDefinition = new AsterixDefinition();
if (!inputParser)
inputParser = new InputParser(pDefinition);
FILE *fp = fopen(xml_config_file, "rt");
if (!fp) {
PyErr_SetString(PyExc_IOError, "Input file not found.");
return -1;
}
// parse format file
XMLParser Parser;
if (!Parser.Parse(fp, pDefinition, xml_config_file)) {
fclose(fp);
return -2;
}
fclose(fp);
return 0;
}
PyObject *python_parse(const unsigned char *pBuf, Py_ssize_t len, int verbose) {
// get current timstamp in ms since epoch
struct timeval tp;
gettimeofday(&tp, NULL);
unsigned long nTimestamp = tp.tv_sec * 1000 + tp.tv_usec / 1000;
if (inputParser) {
AsterixData *pData = inputParser->parsePacket(pBuf, len, nTimestamp);
if (inputParser->raisedErrorFlag()){
PyErr_SetString(PyExc_RuntimeError, "Error parsing packet");
inputParser->clearErrorFlag();
return NULL;
}
if (pData) { // convert to Python format
PyObject *lst = pData->getData(verbose);
delete pData;
return lst;
}
}
return NULL;
}
PyObject *
python_parse_with_offset(const unsigned char *pBuf, Py_ssize_t len, unsigned int offset, unsigned int blocks_count,
int verbose)
/* AUTHOR: Krzysztof Rutkowski, ICM UW, krutk@icm.edu.pl
*/
{
// get current timstamp in ms since epoch
struct timeval tp;
gettimeofday(&tp, NULL);
unsigned long nTimestamp = tp.tv_sec * 1000 + tp.tv_usec / 1000;
if (inputParser) {
AsterixData *pData = new AsterixData();
unsigned int m_nPos = offset;
unsigned int current_blocks_count = 0;
while (m_nPos < len && current_blocks_count < blocks_count) {
unsigned int m_nDataLength = len - m_nPos;
while (m_nDataLength > 3 && current_blocks_count < blocks_count) {
const unsigned char *pBuf_offset = (pBuf + m_nPos);
DataBlock *block = inputParser->parse_next_data_block(
pBuf_offset, m_nPos, len, nTimestamp, m_nDataLength);
if (block) {
pData->m_lDataBlocks.push_back(block);
current_blocks_count++;
}
}
}
if (pData) { // convert to Python format
PyObject *lst = pData->getData(verbose);
delete pData;
PyObject *py_m_nPos = Py_BuildValue("l", m_nPos);
PyObject *py_output = PyTuple_Pack(2, lst, py_m_nPos);
// Decrease references since the tuple holds references to them now
Py_DECREF(lst);
Py_DECREF(py_m_nPos);
return py_output;
}
}
return NULL;
}
PyObject *python_describe(int category, const char *item = NULL, const char *field = NULL, const char *value = NULL) {
if (!pDefinition)
return Py_BuildValue("s", "Not initialized");
const char *description = pDefinition->getDescription(category, item, field, value);
if (description == NULL)
return Py_BuildValue("s", "");
return Py_BuildValue("s", description);
/*
Category* cat = pDefinition->getCategory(category);
if (!cat)
{
return Py_BuildValue("s", "Unknown category");
}
if (item == NULL && field == NULL && value == NULL)
{ // return Category description
return Py_BuildValue("s", cat->m_strName.c_str());
}
std::list<DataItemDescription*>::iterator it;
DataItemDescription* di = NULL;
std::string item_number = format("%s", &item[1]);
for ( it=cat->m_lDataItems.begin() ; it != cat->m_lDataItems.end(); it++ )
{
di = (DataItemDescription*)(*it);
if (di->m_strID.compare(item_number) == 0)
break;
di = NULL;
}
if (di == NULL)
return Py_BuildValue("s", "Unknown item");
if (field == NULL && value == NULL)
{ // Return Item name and description
return Py_BuildValue("s", (di->m_strName+" ("+di->m_strDefinition+" )").c_str());
}
if (value == NULL)
{
return Py_BuildValue("s", "field todo");
}
return Py_BuildValue("s", "value todo");
*/
}
/*
CAsterixFormatDescriptor& Descriptor((CAsterixFormatDescriptor&)formatDescriptor);
PyObject *lst = Descriptor.m_pAsterixData->getData();
PyObject *arg = Py_BuildValue("(O)", lst);
PyObject *result = PyObject_CallObject(my_callback, arg);
Py_DECREF(lst);
if (result != NULL)
/// use result...
Py_DECREF(result);
return true;
*/