| 1 | /* |
|---|
| 2 | |
|---|
| 3 | NetworkConnection |
|---|
| 4 | |
|---|
| 5 | High level socket instance wrapper |
|---|
| 6 | |
|---|
| 7 | Author: |
|---|
| 8 | |
|---|
| 9 | Philip Lindsay <follower@rancidbacon.com> |
|---|
| 10 | |
|---|
| 11 | License: |
|---|
| 12 | |
|---|
| 13 | Copyright 2007-2008 // LGPL |
|---|
| 14 | |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #include "connection.h" |
|---|
| 18 | |
|---|
| 19 | int NetworkConnection::_nextSocket = 0; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | NetworkConnection::NetworkConnection(uint16_t port) { |
|---|
| 23 | /* |
|---|
| 24 | |
|---|
| 25 | Create the low-level socket this instance wraps (for server connections). |
|---|
| 26 | |
|---|
| 27 | */ |
|---|
| 28 | _socket = _nextSocket; // TODO: Do properly (& max) |
|---|
| 29 | _nextSocket++; |
|---|
| 30 | socket(_socket, Sn_MR_TCP, port, 0); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | NetworkConnection::NetworkConnection() { |
|---|
| 35 | /* |
|---|
| 36 | |
|---|
| 37 | Create the low-level socket this instance wraps (for client connections). |
|---|
| 38 | |
|---|
| 39 | */ |
|---|
| 40 | NetworkConnection(0); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | int NetworkConnection::listen() { // TODO: Make private or protected? |
|---|
| 45 | /* |
|---|
| 46 | |
|---|
| 47 | Start listening for a connection from a client. |
|---|
| 48 | |
|---|
| 49 | */ |
|---|
| 50 | return !!::listen(_socket); // TODO: Use C++ namespaces for the driver functions? |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | int NetworkConnection::connect(uint8 * addr, uint16 port) { // TODO: Make private or protected? |
|---|
| 55 | /* |
|---|
| 56 | |
|---|
| 57 | Client connection to a server. |
|---|
| 58 | |
|---|
| 59 | */ |
|---|
| 60 | |
|---|
| 61 | // TODO: Accept bytes here for addr? |
|---|
| 62 | int result = 0; |
|---|
| 63 | |
|---|
| 64 | result = !!::connect(_socket, addr, port); // TODO: Use C++ namespaces for the driver functions? |
|---|
| 65 | return result; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | int NetworkConnection::available() { |
|---|
| 70 | /* |
|---|
| 71 | |
|---|
| 72 | Functionality matches 'Serial.available()'. |
|---|
| 73 | |
|---|
| 74 | Note: If the socket is not connected then this will return 0, |
|---|
| 75 | but it is intended that 'isConnected()' would be checked first. |
|---|
| 76 | |
|---|
| 77 | Returns: |
|---|
| 78 | "The number of bytes available to read ... or 0 if none are available. |
|---|
| 79 | If any data has come in, [...].available() will be greater than 0." |
|---|
| 80 | |
|---|
| 81 | */ |
|---|
| 82 | // TODO: Do we want to check for 'isConnected' as well, or not? |
|---|
| 83 | // We have to, I guess, for valid behaviour with 'getSn_*'. |
|---|
| 84 | if (!isConnected()) { |
|---|
| 85 | return 0; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | return getSn_RX_RSR(_socket); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | #define NO_READ_DATA_AVAILABLE -1 |
|---|
| 93 | |
|---|
| 94 | int NetworkConnection::read() { |
|---|
| 95 | /* |
|---|
| 96 | |
|---|
| 97 | Functionality matches 'Serial.read()'. |
|---|
| 98 | |
|---|
| 99 | Returns: |
|---|
| 100 | "an int, the first byte of incoming serial data available |
|---|
| 101 | (or -1 if no data is available)." |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | Note: I thought 'recv' blocked until data was available, |
|---|
| 105 | but it currently seems not to block after all. |
|---|
| 106 | However, because I'm seeking to match the 'Serial.read' |
|---|
| 107 | behaviour we don't actually want to block anyway, |
|---|
| 108 | so we'll ignore it for the moment and handle things |
|---|
| 109 | ourself for now. |
|---|
| 110 | |
|---|
| 111 | */ |
|---|
| 112 | uint8_t theByte; |
|---|
| 113 | |
|---|
| 114 | if (!available()) { |
|---|
| 115 | return NO_READ_DATA_AVAILABLE; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | recv(_socket, &theByte, 1); |
|---|
| 119 | return theByte; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | int NetworkConnection::isConnected() { |
|---|
| 124 | /* |
|---|
| 125 | |
|---|
| 126 | Returns true if the connection is established. |
|---|
| 127 | |
|---|
| 128 | */ |
|---|
| 129 | // TODO: If we want the 'Network*' classes to be generic we |
|---|
| 130 | // would need to handle this differently: |
|---|
| 131 | return (getSn_SR(_socket) == SOCK_ESTABLISHED); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | void NetworkConnection::print(uint8_t b) { |
|---|
| 136 | /* |
|---|
| 137 | |
|---|
| 138 | Send a character over connection. Naming matches 'Serial' API. |
|---|
| 139 | |
|---|
| 140 | */ |
|---|
| 141 | if (isConnected()) { |
|---|
| 142 | send(_socket, &b, 1); |
|---|
| 143 | } else { |
|---|
| 144 | // Just drop it if we're not connected. |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | void NetworkConnection::print(const char * text) { |
|---|
| 150 | /* |
|---|
| 151 | |
|---|
| 152 | Send a string over connection. Naming matches 'Serial' API. |
|---|
| 153 | |
|---|
| 154 | */ |
|---|
| 155 | for (unsigned int idx = 0; idx < strlen(text); idx++) { |
|---|
| 156 | print(text[idx]); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void NetworkConnection::close() { |
|---|
| 161 | /* |
|---|
| 162 | |
|---|
| 163 | Close the connection. |
|---|
| 164 | |
|---|
| 165 | */ |
|---|
| 166 | // TODO: Determine if we need/want the disconnect (see pg 26 W5100) |
|---|
| 167 | ::close(_socket); |
|---|
| 168 | disconnect(_socket); |
|---|
| 169 | } |
|---|
| 170 | |
|---|