root/branches/follower/libw5100/src/libw5100/interface.cpp

Revision 140, 2.9 KB (checked in by follower, 11 months ago)

Add author, license and copyright details. (Which weren't permitted in competition entry source.)

Line 
1/*
2
3   Network Interface
4
5   Generic wrapper around a specific network device.
6
7   A Network Interface (representing the physical network device) returns a
8   connecting or listening Network Connection (representing a socket in either
9   client or server state).
10
11   Author:
12
13      Philip Lindsay <follower@rancidbacon.com>
14
15   License:
16
17      Copyright 2007-2008 // LGPL
18
19*/
20
21#include "interface.h"
22
23#define HANDLE_BAD_ERROR() while (1) {};
24
25NetworkInterface::NetworkInterface(W5100Device& networkDevice) : device (networkDevice) {
26  /*
27
28     Initialise the physical device with default network details.
29
30     Note: The "weirdness" in this function declaration is a "initalization list",
31          i.e. this bit:
32   
33         ) : device (networkDevice) {
34 
35          it is needed to give the 'device' Reference a value supplied to the method
36          rather than having a new 'W5100Device' instance created.
37   */
38   
39   // This may not be the best place to do all this, but it'll do for now:
40  device.setMac(0x02,0xDE,0xAD,0xBE,0xEF,0x00);
41   
42  device.setIp(169,254,254,169); // A local-link IP -- NOTE: This is out of spec.
43  device.setMask(255,255,0,0);   // Matches local-link IP /16
44 
45  device.setGateway(0,0,0,0);   // We can't even guess this, so just skip it.
46   
47};
48
49NetworkConnection NetworkInterface::listen(uint16_t port) {
50   /*
51 
52      Start to listen for a request from a client.
53
54 
55      NOTE: For reasons I don't understand, this FAILS TO WORK if port >255.
56            The port that gets opened is ~(port mod 255) but if you limit
57            port to be uint8_t for example you don't get an error until
58            the port number is bigger than 'long'.
59           
60      TODO: Track and fix this for port numbers > 255.
61     
62
63      Returns a NetworkConnection listening on the specified TCP port.
64 
65    */
66    NetworkConnection connection = NetworkConnection(port);
67   
68    // TODO: How to best handle errors?
69    //       There's two major categories I can think of currently:
70    //         1) A socket wasn't available to listen in the first place
71    //         2) The listen attempt failed for some reason
72    //       Using 'HANDLE_BAD_ERROR()' (currently an infinite loop) is
73    //       not ideal but should prevent the above errors slipping by unnoticed.
74    if (!connection.listen()) {
75      HANDLE_BAD_ERROR(); 
76    }
77   
78    return connection;
79}
80
81
82NetworkConnection NetworkInterface::connect(byte b0, byte b1, byte b2, byte b3, uint16 port) {
83   /*
84
85      Connect to a server.
86   
87    */
88  NetworkConnection connection = NetworkConnection(4000);
89   
90  byte _scratchBuffer[4];   // TODO: Move this?
91
92  // TODO: Better?
93  _scratchBuffer[0] = b0;
94  _scratchBuffer[1] = b1;
95  _scratchBuffer[2] = b2;
96  _scratchBuffer[3] = b3;
97 
98    if (!connection.connect(_scratchBuffer, port)) {
99      HANDLE_BAD_ERROR(); 
100    }
101 
102  return connection;
103}
104
105//#include "w5100_device.h"
106
107//NetworkInterface Network = NetworkInterface(W5100);
Note: See TracBrowser for help on using the browser.