summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/proto/proto.cc
blob: d65aa58d1c63a43d43c29dd9f9ab1ba036fe3111 (plain)
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
// proto.cc
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Haller
//
// Robot APB Team/Efrei 2005.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program 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 2 of the License, or
// (at your option) any later version.
// 
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}

#include "timer/timer.hh"
#include "utils/hexa.hh"
#include "proto.hh"

/// Constructeur.
Proto::Proto(Receiver &receiver)
    :receiver_(receiver), tLastSend_(0), revState_(0)
{
}

/// Ouvre le port s�rie.
void
Proto::open(const std::string &ttyname)
{
    serial_.open(ttyname);
}

/// Ferme le port s�rie
void
Proto::close(void)
{
    serial_.close();
}

/// Teste si tout les packets ont �t� envoy�s et aquit�s, sinon, essaye de 
/// le faire.
bool
Proto::sync(void)
{
    bool reGet = true;
    //R�cup�ration de la frame
    while(reGet)
    {
	if(reGet = getFrame())
	{
	    //Si la frame est un aquittement
	    if(currentFrame_ == frameQueue_.front())
	    {
		//on vire la commande de la queue
		frameQueue_.pop();
		//Et on envoie la suivante si elle existe
		if(!frameQueue_.empty())
		    sendFrame(frameQueue_.front());
	    }
	    //Si c'est une nouvelle commande, on l'envoie avec receive
	    else
		receiver_.receive(currentFrame_.command, currentFrame_);
	}
    }
    //On regarde depuis combien de temps on a envoy� une commande
    if(!frameQueue_.empty())
    {
	//Si on d�passe la milliseconde, on renvoie
	if(Timer::getProgramTime() - tLastSend_ > 500)
	    sendFrame(frameQueue_.front());
    }	    
    return frameQueue_.empty();
}

/// Envoie un packet
void
Proto::send (const Frame & Frame)
{
    frameQueue_.push(Frame);
    sync();
}


/// Envois un packet. COMMAND est la commande � envoyer, FORMAT, donne le
/// format et le nombre de param�tres ('b' : 8 bits, 'w' : 16 bits, 'd' :
/// 32 bits, majuscule pour sign�).
void
Proto::send (uint8_t command, const char *format, int a0, int a1,
		  int a2, int a3)
{
    // Constitution de la frame
    Proto::Frame frame;
    //int nbArg = strlen(format);
    frame.command = command;
    
    if (format[0] != '\0')
    {
	newArgFrame(frame, format[0],a0);
	if (format[1] != '\0')
	{
	    newArgFrame(frame, format[1],a1);
	    if (format[2] != '\0')
	    {
		newArgFrame(frame, format[2],a2);
		if (format[3] != '\0')
		    newArgFrame(frame, format[3],a3);
	    }
	}
    }
    send(frame);
}

bool
Proto::decode (const Proto::Frame &frame)
{
    //bon, est-ce que c'est bien utile �a?
    int dummy;
    return decode(frame, "", dummy, dummy, dummy, dummy);
}

bool
Proto::decode (const Frame &frame, const char *format, int &a0)
{
    int dummy;
    return decode(frame, format, a0, dummy, dummy, dummy);
}

bool
Proto::decode (const Frame &frame, const char *format, int &a0, int &a1)
{
    int dummy;
    return decode(frame, format, a0, a1, dummy, dummy);
}

bool
Proto::decode (const Frame &frame, const char *format, int &a0,
		    int &a1, int &a2)
{

    int dummy;
    return decode(frame, format, a0, a1, a2, dummy);
}

bool
Proto::decode (const Frame &frame, const char *format, int &a0,
		    int &a1, int &a2, int &a3)
{    
    //Teste si il y a bien le bon nombre d'argument
    if (int(frame.args.size()) != argsFrameSize(format))//un cast pour virer un warning
	return false; 
    // On d�code et on envoie
    decodeArg(frame, format, a0, a1, a2, a3);
    
    return true;
}

/// R�cup�re les infos de l'AVR pour construire une frame
bool
Proto::getFrame(void)
{
    int receivedChar;
    //tant que le tampon n'est pas vide, on teste
    while((receivedChar = serial_.getchar()) != -1)
    {
	//si la donn�e n'est pas erronn�e
	if(receivedChar != 0xff)
	{
	    //Si on re�oit un bang
	    if(receivedChar == '!')
	    {
		revState_ = 1;
		currentFrame_.command = 0;
		currentFrame_.args.clear();
	    }
	    //Si on re�oit le retour chariot et que on re�evait les args
	    if(receivedChar == '\n' && revState_ == 3)
	    {
		revState_ = 0;
		return true;
	    }
	    //Pour les autres charact�res
	    //Si on attend la commande
	    switch(revState_)
	    {
		case 1:
		    currentFrame_.command = (uint8_t(hex2digit( receivedChar )))
					     << 4;
		    revState_ = 2;
		    break;
		case 2:
		    currentFrame_.command |= uint8_t(hex2digit( receivedChar ));
		    revState_ = 3;
		    break;
		case 3:
		    currentFrame_.args.push_back(uint8_t(
				  hex2digit( receivedChar )) << 4);
		    revState_ = 3;
		    break;
	    }
	    //Si revState == 0 alors on jette
	}
    }
    return false;
}

    void
Proto::sendFrame(const Frame & frame)
{
    //envoyer le bang
    serial_.putchar('!');

    //Envoyer la commande
    serial_.putchar(digit2hex(frame.command >> 4));
    serial_.putchar(digit2hex(frame.command & 0x0f));

    //Envoyer les arguments
    for(int i = 0; i < int(frame.args.size()); i++) //le cast est pour virer un warning
    {
	serial_.putchar(digit2hex(frame.args[i] >> 4));
	serial_.putchar(digit2hex(frame.args[i] & 0x0f));
    }

    //Envoyer le retour chariot
    serial_.putchar('\n');

    //actualiser le timer
    tLastSend_ = Timer::getProgramTime();
}

    void
Proto::newArgFrame(Proto::Frame & frame, char format, int arg)
{
    switch(format)
    {
	case 'b':
	case 'B':
	    frame.args.push_back(uint8_t(arg));
	    break;
	case 'w':
	case 'W':
	    frame.args.push_back(uint8_t(arg >> 8));
	    frame.args.push_back(uint8_t(arg));
	    break;
	case 'd':
	case 'D':
	    frame.args.push_back(uint8_t(arg >> 24));
	    frame.args.push_back(uint8_t(arg >> 16));
	    frame.args.push_back(uint8_t(arg >> 8));
	    frame.args.push_back(uint8_t(arg));
	    break;
    }
}

    int
Proto::argsFrameSize(const char *format)
{
    int size = 0;
    for(; *format != '\0'; format++)
	switch(*format)
	{
	    case 'b':
	    case 'B':
		size += 1;
	    case 'w':
	    case 'W':
		size += 1;
	    case 'd':
	    case 'D':
		size += 1;
	    default:
		size += 1;
	}
    return size;
}

void
Proto::decodeArg(const Frame & frame, const char *format, int &a0, int &a1, int &a2, int &a3)
{
    int temp[4];
    int pos = 0;

    for(int i = 0; *format != '\0'; format++,i++)
    { 
	switch(*format)
	{
	    case 'b':
		temp[i] = int(frame.args[pos]);
		pos++;
		break;
	    case 'B':
		{
		    int8_t t = int8_t(frame.args[pos]);
		    temp[i] = int(t);
		    pos++;
		    break;
		}
	    case 'w':
		temp[i] = int(frame.args[pos]) << 8 
		    |int(frame.args[pos + 1]);
		pos += 2;
		break;
	    case 'W':
		{
		    int8_t t = int8_t(frame.args[pos]);
		    temp[i] = int(t) << 8
			|int(frame.args[pos + 1]);
		    pos += 2;
		    break;
		}
	    case 'd':
		temp[i] = int(frame.args[pos]) << 24
		    |int(frame.args[pos + 1]) << 16
		    |int(frame.args[pos + 2]) << 8
		    |int(frame.args[pos + 3]);
		pos += 4;
		break;

	    case 'D':
		int8_t t = int8_t(frame.args[pos]);
		temp[i] = int(t) << 24
		    |int(frame.args[pos + 1]) << 16
		    |int(frame.args[pos + 2]) << 8
		    |int(frame.args[pos + 3]);
		break;
	}
    }
    a0 = temp[0];
    a1 = temp[1];
    a2 = temp[2];
    a3 = temp[3];
}

    bool
Proto::Frame::operator==(const Frame& frame)
{
    return this->command == frame.command && this->args == frame.args;
}