-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_layout.cpp
More file actions
243 lines (228 loc) · 6.47 KB
/
message_layout.cpp
File metadata and controls
243 lines (228 loc) · 6.47 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
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
/*
* This file is part of btcontrol
*
* Copyright (C) Christian Ferbar
*
* btcontrol 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.
*
* btcontrol 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 btcontrol. If not, see <http://www.gnu.org/licenses/>.
*
* message generation
*/
#include <string.h>
#include <sstream>
#include <errno.h>
#include <assert.h>
#include "message_layout.h"
#include "fbtctl_message.h"
#include "utils.h"
MessageLayouts messageLayouts;
/**
* nur für structs sinnvoll
*/
const MessageLayout& getMessageLayout(MessageLayout::DataType type)
{
assert(messageLayouts.isLoaded());
if(type <= MessageLayout::STRUCT) {
ERRORF("getMessageLayout: non-struct (%d)\n", type);
throw std::runtime_error("invalid type");
}
MessageLayout tmp;
MessageLayouts::const_iterator it;
for(it=messageLayouts.begin(); it != messageLayouts.end(); ++it) {
// printf("searching for %d ... ?= %s\n", type, it->first.c_str());
if(type == it->second.type)
return messageLayouts[it->first];
}
throw std::runtime_error(utils::format("invalid struct type (%d)", type));
}
/**
* @param pos zeigt auf name:TYP
* TODO: Das in die MessageLayout klasse tun *****************************
*/
MessageLayout parseMessageLayout(const char *&pos)
{
// printf("parse \"%s\"\n",pos);
MessageLayout messageLayout;
if(*pos=='\0') { // leere zeile
return messageLayout;
}
while(true) {
const char *namestart=pos;
while(*pos && *pos != ':') pos++;
if(*pos=='\0')
throw std::runtime_error("error finding name");
// printf("pos=\"%c\"\n",*pos);
MessageLayout info;
std::string name=std::string(namestart, pos-namestart);
info.name=name;
pos++;
switch(*pos) {
case 'I': info.type=MessageLayout::INT;
pos++;
break;
case 'S': info.type=MessageLayout::STRING;
pos++;
break;
case 'A': info.type=MessageLayout::ARRAY;
pos++;
if(*pos != '{') throw std::runtime_error(utils::format("A must be followed by { @%s", pos));
pos++;
info=parseMessageLayout(pos);
info.name=name;
info.type=MessageLayout::ARRAY;
break;
case '\0': // ende/leer
break;
default:
throw std::runtime_error(utils::format("error parsing protocol.dat invalid type @%s", pos));
}
messageLayout.childLayouts.push_back(info);
if(*pos == '}' || *pos == '\0') {
break;
}
// printf("pos2=\"%s\"\n",pos);
if(*pos != ',') {
throw std::runtime_error(", expected");
}
pos++;
}
return messageLayout;
}
/**
* tut die protocol.dat laden
* @return hash von protocol.dat (zum vergleich ob das tel die selbe protocol.dat verwendet)
* kommentare werden ignoriert
*/
int MessageLayouts::load()
{
int hash=0;
if(this->loaded)
return 0;
this->loaded=true;
std::string protocolDat = readFile("protocol.dat");
int typeNr=MessageLayout::STRUCT+1;
std::string line;
const char *buffer;
std::istringstream f(protocolDat);
while (std::getline(f, line)) {
buffer = line.c_str();
/*
int n=strlen(buffer);
if((n>=1) && ((buffer[n-1] == '\n') || (buffer[n-1] == '\r'))) {
buffer[n-1]='\0';
}
if((n>=2) && ((buffer[n-2] == '\n') || (buffer[n-2] == '\r'))) {
buffer[n-2]='\0';
}
*/
if(buffer[0]=='#') {
continue;
}
{ // hash für die zeile berechnen
int lineHash=0;
const unsigned char *n=(unsigned char *) buffer;
while(*n) { lineHash+=*n; n++;}
hash+=lineHash;
}
const char *pos=buffer;
while(*pos && isspace(*pos)) pos++;
if(*pos=='\0')
continue;
// printf("line: %s\n",buffer);
const char *namestart=pos;
while(*pos && *pos!='=') pos++;
if(*pos=='\0') {
throw std::runtime_error("invalid identifier");
}
std::string name(namestart,pos-namestart);
// printf("identifier found: %s\n",name.c_str());
pos++;
MessageLayout messageLayout(parseMessageLayout(pos));
MessageLayout::DataType type=(MessageLayout::DataType) (typeNr);
messageLayout.type=type;
// printf("messayLayout: type: %d line:%s\n",type,buffer);
messageLayouts[name] = messageLayout;
typeNr++;
}
MessageLayouts::protocolHash=hash;
return hash;
}
void MessageLayout::dump(int indent) const
{
printf("sub:%zu\n", this->childLayouts.size());
std::vector<MessageLayout>::const_iterator it;
for(it=this->childLayouts.begin(); it != this->childLayouts.end(); ++it) {
INDENT();
std::string typeName;
try {
typeName=messageTypeName(it->type);
} catch( const char *e) {
typeName=e;
}
printf("[%s]:[%s]\n",it->name.c_str(), typeName.c_str());
if(it->type >= MessageLayout::ARRAY) {
it->dump(indent+1);
}
}
}
void MessageLayouts::dump()
{
printf("dumpMessageLayouts\nloaded:%d\n",this->loaded);
MessageLayouts::const_iterator it;
for(it=messageLayouts.begin(); it != messageLayouts.end(); ++it) {
printf("%s=%d:\n",it->first.c_str(),it->second.type);
it->second.dump(1);
}
}
std::string messageTypeName(MessageLayout::DataType type)
{
switch(type) {
case MessageLayout::UNDEF: return "(UNDEF)";
case MessageLayout::INT: return "(INT)";
case MessageLayout::STRING: return "(STRING)";
case MessageLayout::ARRAY: return "(ARRAY)";
case MessageLayout::STRUCT: return "(STRUCT)";
default: {
std::string tmp;
tmp +="(STRUCT ";
MessageLayouts::const_iterator it;
for(it=messageLayouts.begin(); it != messageLayouts.end(); ++it) {
// printf("searching for %d (%s)\n",type, it->first.c_str());
if(type == it->second.type)
break;
}
if(it == messageLayouts.end()) {
throw std::runtime_error(utils::format("invalid id(%d)", type));
}
tmp += it->first;
tmp +=")";
return tmp;
}
}
assert(!"no way out of the switch");
}
MessageLayout::DataType messageTypeID(const std::string &name)
{
MessageLayout::DataType type=messageLayouts[name].type;
if(type == MessageLayout::UNDEF)
throw std::runtime_error(utils::format("invalid message name: \"%s\"", name.c_str()));
return type;
/*
MessageLayouts::const_iterator it;
for(it=messageLayouts.begin(); it != messageLayouts.end(); ++it) {
printf("searching for %s ... ?= %s\n",name.c_str(), it->second.name.c_str());
if(name == it->second.name)
return it->first;
}
*/
}