-
Notifications
You must be signed in to change notification settings - Fork 33
/
test.c
446 lines (338 loc) · 7.87 KB
/
test.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
//Copyright 2012 <>< Charles Lohr, under the MIT-x11 or NewBSD license. You choose.
//Err this is a total spaghetti code file. My interest is in the libraries, not this.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "avr_print.h"
#include <stdio.h>
#include "iparpetc.h"
#include "enc424j600.h"
#include <avr/pgmspace.h>
#include "dumbcraft.h"
#include <http.h>
#include <string.h>
#include <basicfat.h>
#include <avr/eeprom.h>
/*
Useful ports:
DDRC 0x07 = 0x2d
PORTC 0x08 = 0x02
For Internal Temperature sensor:
ADCSRA (5A) = 0xE4;
ADCSRB (5B) = 0;
ADMUX (5C) = 0xE8;
ADCH = 0x59 / ADCL = 0x58
*/
#define NOOP asm volatile("nop" ::)
static void setup_clock( void )
{
/*Examine Page 33*/
CLKPR = 0x80; /*Setup CLKPCE to be receptive*/
CLKPR = 0x00; /*No scalar*/
OSCCAL=0xff;
}
unsigned short frameno;
unsigned char cc;
#define RPORT 8001
#define POP enc424j600_pop8()
#define POP16 enc424j600_pop16()
#define PUSH(x) enc424j600_push8(x)
#define PUSH16(x) enc424j600_push16(x)
int8_t lastconnection = -1; //for dumbcraft
uint16_t bytespushed; //for dumbcraft
uint8_t EEMEM my_server_name_eeprom[16];
char my_server_name[16];
void SetServerName( const char * stname )
{
memcpy( my_server_name, stname, strlen( stname ) + 1 );
eeprom_write_block( stname, my_server_name_eeprom, strlen( stname ) + 1 );
}
#ifdef NO_HTTP
#undef HTTP_CONNECTIONS
#define HTTP_CONNECTIONS 0
#endif
int8_t MyGetFreeConnection( uint8_t minc, uint8_t max_not_inclusive )
{
uint8_t i;
for( i = minc; i < max_not_inclusive; i++ )
{
struct tcpconnection * t = &TCPs[i];
if( !t->state )
{
memset( t, 0, sizeof( struct tcpconnection ) );
t->sendptr = TX_SCRATCHPAD_END + TCP_BUFFERSIZE * (i-1);
return i;
}
}
return 0;
}
uint8_t TCPReceiveSyn( uint16_t portno )
{
if( portno == MINECRAFT_PORT ) //Must bump these up by 8...
{
uint8_t ret = MyGetFreeConnection(HTTP_CONNECTIONS+1, TCP_SOCKETS );
sendstr( "Conn attempt: " );
sendhex2( ret );
sendchr( '\n' );
AddPlayer( ret - HTTP_CONNECTIONS - 1 );
return ret;
}
#ifndef NO_HTTP
else if( portno == 80 )
{
uint8_t ret = MyGetFreeConnection(1, HTTP_CONNECTIONS+1 );
// sendchr( 0 ); sendchr( 'x' ); sendhex2( ret );
HTTPInit( ret-1, ret );
return ret;
}
#endif
return 0;
}
void TCPConnectionClosing( uint8_t conn )
{
if( conn >= HTTP_CONNECTIONS )
{
sendstr( "Lostconn\n" );
RemovePlayer( conn - HTTP_CONNECTIONS - 1 );
}
#ifndef NO_HTTP
else
{
HTTPClose( conn-1 );
}
#endif
}
/////////////////////////////////////////UDP AREA//////////////////////////////////////////////
void HandleUDP( uint16_t len )
{
POP16; //Checksum
len -= 8; //remove header.
//You could pop things, or check ports, etc. here.
return;
}
///////////////////////////////////////DUMBCRAFT AREA//////////////////////////////////////////
uint8_t disable;
uint8_t CanSend( uint8_t playerno ) //DUMBCRAFT
{
return TCPCanSend( playerno + HTTP_CONNECTIONS + 1 );
}
void SendStart( uint8_t playerno ) //DUMBCRAFT
{
bytespushed = 0;
lastconnection = playerno + HTTP_CONNECTIONS + 1;
disable = 0;
}
//Must be power-of-two
#define CIRC_BUFFER_SIZE 4096
#if ( (FREE_ENC_END) - (FREE_ENC_START) ) <= (CIRC_BUFFER_SIZE)
#error Not enough free space on enc424j600
#endif
#define CIRC_END ((FREE_ENC_START) + (CIRC_BUFFER_SIZE))
uint16_t circ_buffer_at = 0;
uint8_t incirc = 0;
uint16_t GetCurrentCircHead()
{
return circ_buffer_at;
}
uint8_t UnloadCircularBufferOnThisClient( uint16_t * whence )
{
//TRICKY: we're in the middle of a transaction now.
//We need to back out, copy the memory, then pretend nothing happened.
uint16_t w = *whence;
uint16_t togomaxB = (circ_buffer_at - w)&((CIRC_BUFFER_SIZE)-1);
if( togomaxB == 0 ) return 0;
ETCSPORT |= ETCS;
uint16_t esat = enc424j600_read_ctrl_reg16( EEGPWRPTL );
uint16_t togomaxA = 512 - ( esat - sendbaseaddress );
uint16_t togo = (togomaxA>togomaxB)?togomaxB:togomaxA;
//printf( "w: %04x togo: %04x (%04x/%04x) %04x %04x %04x\n", w, togo, togomaxA, togomaxB, FREE_ENC_START, CIRC_END, circ_buffer_at );
enc424j600_copy_memory( esat, w + (FREE_ENC_START), togo, (FREE_ENC_START), CIRC_END-1 );
enc424j600_write_ctrl_reg16( EEGPWRPTL, esat + togo );
ETCSPORT &= ~ETCS;
espiW( EWGPDATA );
*whence = (w + togo)&((CIRC_BUFFER_SIZE)-1);
return togo != togomaxB;
}
void StartupBroadcast()
{
ETCSPORT |= ETCS;
//is_in_outcirc = 1;
enc424j600_write_ctrl_reg16( EEUDAWRPTL, circ_buffer_at + (FREE_ENC_START) );
enc424j600_write_ctrl_reg16( EEUDASTL, FREE_ENC_START );
enc424j600_write_ctrl_reg16( EEUDANDL, CIRC_END - 1);
ETCSPORT &= ~ETCS;
espiW( EWUDADATA );
incirc = 1;
}
void DoneBroadcast()
{
ETCSPORT |= ETCS;
circ_buffer_at = enc424j600_read_ctrl_reg16( EEUDAWRPTL ) - (FREE_ENC_START);
incirc = 0;
}
void Sbyte( uint8_t byte ) //DUMBCRAFT
{
/*
if( disable )
{
return;
}
if( !TCPCanSend( lastconnection ) )
{
sendstr( "WARNING: TRYING TO SEND WHEN CANNOT SEND\n" );
disable = 1;
return;
}
*/
if( !incirc )
{
if( bytespushed == 0 )
{
TCPs[lastconnection].sendtype = ACKBIT | PSHBIT;
StartTCPWrite( lastconnection );
}
bytespushed++;
}
PUSH( byte );
}
void EndSend( ) //DUMBCRAFT
{
if( bytespushed == 0 )
{
}
else
{
EndTCPWrite( lastconnection );
//EmitTCP( lastconnection );
}
}
void ForcePlayerClose( uint8_t playerno, uint8_t reason ) //DUMBCRAFT
{
RequestClosure( playerno + HTTP_CONNECTIONS );
RemovePlayer( playerno );
}
uint16_t totaldatalen;
uint16_t readsofar;
uint8_t Rbyte() //DUMBCRAFT
{
if( readsofar++ > totaldatalen ) return 0;
return POP;
}
uint8_t CanRead() //DUMBCRAFT
{
return readsofar < totaldatalen;
}
uint8_t TCPReceiveData( uint8_t connection, uint16_t totallen )
{
if( connection > HTTP_CONNECTIONS ) //DUMBCRAFT
{
totaldatalen = totallen;
readsofar = 0;
GotData( connection - HTTP_CONNECTIONS - 1 );
return 0; //Do this if we didn't send an ack.
}
#ifndef NO_HTTP
else
{
HTTPGotData( connection-1, totallen );
return 0;
}
#endif
return 0;
}
//HTTP
#ifndef NO_HTTP
void HTTPCustomStart( )
{
//curhttp->is404 = 1;
curhttp->bytesleft = 0xffffffff;
}
void HTTPCustomCallback( )
{
uint8_t i = 0;
if( curhttp->isfirst || curhttp->is404 || curhttp->isdone )
{
HTTPHandleInternalCallback();
return;
}
StartTCPWrite( curhttp->socket );
do
{
enc424j600_push8( 0 );
enc424j600_push8( 0xff );
} while( ++i );
EndTCPWrite( curhttp->socket );
}
#endif
unsigned char MyIP[4] = { 192, 168, 0, 142 };
unsigned char MyMask[4] = { 255, 255, 255, 0 };
unsigned char MyGateway[4] = { 192, 168, 0, 1 };
unsigned char MyMAC[6];
int main( void )
{
uint8_t delayctr;
uint8_t marker;
//Input the interrupt.
DDRD &= ~_BV(2);
cli();
setup_spi();
sendstr( "HELLO\n" );
setup_clock();
eeprom_read_block( &my_server_name[0], &my_server_name_eeprom[0], 16 );
if( my_server_name[0] == 0 || my_server_name[0] == (char)0xff )
SetServerName( "AVRCraft" );
//Configure T2 to "overflow" at 100 Hz, this lets us run the TCP clock
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21) | _BV(CS20);
//T2 operates on clkIO, fast PWM. Fast PWM's TOP is OCR2A
#define T2CNT ((F_CPU/1024)/100)
#if( T2CNT > 254 )
#undef T2CNT
#define T2CNT 254
#endif
OCR2A = T2CNT;
sei();
//unsigned short phys[32];
#ifndef NO_HTTP
if( initSD() )
{
sendstr( "Fatal error. Cannot open SD card.\n" );
//return -1;
}
openFAT();
#endif
InitTCP();
DDRC &= 0;
if( enc424j600_init( MyMAC ) )
{
sendstr( "Failure.\n" );
while(1);
}
sendstr( "OK.\n" );
SetupDHCPName( my_server_name );
InitDumbcraft();
while(1)
{
unsigned short r;
r = enc424j600_recvpack( );
if( r ) continue;
UpdateServer();
#ifndef NO_HTTP
HTTPTick();
#endif
if( TIFR2 & _BV(TOV2) )
{
TIFR2 |= _BV(TOV2);
sendchr( 0 );
TickTCP();
delayctr++;
if( delayctr==10 )
{
TickDHCP();
delayctr = 0;
TickServer();
}
}
}
return 0;
}