Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:d26c1b55cfca 1 /* Copyright (C) 2012 mbed.org, MIT License
DieterGraef 0:d26c1b55cfca 2 *
DieterGraef 0:d26c1b55cfca 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
DieterGraef 0:d26c1b55cfca 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
DieterGraef 0:d26c1b55cfca 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
DieterGraef 0:d26c1b55cfca 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
DieterGraef 0:d26c1b55cfca 7 * furnished to do so, subject to the following conditions:
DieterGraef 0:d26c1b55cfca 8 *
DieterGraef 0:d26c1b55cfca 9 * The above copyright notice and this permission notice shall be included in all copies or
DieterGraef 0:d26c1b55cfca 10 * substantial portions of the Software.
DieterGraef 0:d26c1b55cfca 11 *
DieterGraef 0:d26c1b55cfca 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
DieterGraef 0:d26c1b55cfca 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
DieterGraef 0:d26c1b55cfca 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DieterGraef 0:d26c1b55cfca 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DieterGraef 0:d26c1b55cfca 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DieterGraef 0:d26c1b55cfca 17 */
DieterGraef 0:d26c1b55cfca 18 #include "TCPSocketConnection.h"
DieterGraef 0:d26c1b55cfca 19 #include <cstring>
DieterGraef 0:d26c1b55cfca 20
DieterGraef 0:d26c1b55cfca 21 using std::memset;
DieterGraef 0:d26c1b55cfca 22 using std::memcpy;
DieterGraef 0:d26c1b55cfca 23
DieterGraef 0:d26c1b55cfca 24 TCPSocketConnection::TCPSocketConnection() :
DieterGraef 0:d26c1b55cfca 25 _is_connected(false) {
DieterGraef 0:d26c1b55cfca 26 }
DieterGraef 0:d26c1b55cfca 27
DieterGraef 0:d26c1b55cfca 28 int TCPSocketConnection::set_source_port(const int port) {
DieterGraef 0:d26c1b55cfca 29 tcp_preselect_port(port);
DieterGraef 0:d26c1b55cfca 30 return 0;
DieterGraef 0:d26c1b55cfca 31 }
DieterGraef 0:d26c1b55cfca 32
DieterGraef 0:d26c1b55cfca 33 int TCPSocketConnection::connect(const char* host, const int port) {
DieterGraef 0:d26c1b55cfca 34
DieterGraef 0:d26c1b55cfca 35 if (init_socket(SOCK_STREAM) < 0)
DieterGraef 0:d26c1b55cfca 36 return -1;
DieterGraef 0:d26c1b55cfca 37
DieterGraef 0:d26c1b55cfca 38 if (set_address(host, port) != 0)
DieterGraef 0:d26c1b55cfca 39 return -1;
DieterGraef 0:d26c1b55cfca 40
DieterGraef 0:d26c1b55cfca 41 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
DieterGraef 0:d26c1b55cfca 42 close();
DieterGraef 0:d26c1b55cfca 43 return -1;
DieterGraef 0:d26c1b55cfca 44 }
DieterGraef 0:d26c1b55cfca 45 _is_connected = true;
DieterGraef 0:d26c1b55cfca 46
DieterGraef 0:d26c1b55cfca 47 return 0;
DieterGraef 0:d26c1b55cfca 48 }
DieterGraef 0:d26c1b55cfca 49
DieterGraef 0:d26c1b55cfca 50 bool TCPSocketConnection::is_connected(void) {
DieterGraef 0:d26c1b55cfca 51 if(_sock_fd<0)_is_connected=false;
DieterGraef 0:d26c1b55cfca 52 return _is_connected;
DieterGraef 0:d26c1b55cfca 53 }
DieterGraef 0:d26c1b55cfca 54
DieterGraef 0:d26c1b55cfca 55 int TCPSocketConnection::send(char* data, int length) {
DieterGraef 0:d26c1b55cfca 56 if ((_sock_fd < 0) || !_is_connected)
DieterGraef 0:d26c1b55cfca 57 return -1;
DieterGraef 0:d26c1b55cfca 58
DieterGraef 0:d26c1b55cfca 59 if (!_blocking) {
DieterGraef 0:d26c1b55cfca 60 TimeInterval timeout(_timeout);
DieterGraef 0:d26c1b55cfca 61 if (wait_writable(timeout) != 0)
DieterGraef 0:d26c1b55cfca 62 return -1;
DieterGraef 0:d26c1b55cfca 63 }
DieterGraef 0:d26c1b55cfca 64
DieterGraef 0:d26c1b55cfca 65 int n = lwip_send(_sock_fd, data, length, 0);
DieterGraef 0:d26c1b55cfca 66 _is_connected = (n != 0);
DieterGraef 0:d26c1b55cfca 67
DieterGraef 0:d26c1b55cfca 68 return n;
DieterGraef 0:d26c1b55cfca 69 }
DieterGraef 0:d26c1b55cfca 70
DieterGraef 0:d26c1b55cfca 71
DieterGraef 0:d26c1b55cfca 72 // -1 if unsuccessful, else number of bytes written
DieterGraef 0:d26c1b55cfca 73 int TCPSocketConnection::send_all(char* data, int length) {
DieterGraef 0:d26c1b55cfca 74 if ((_sock_fd < 0) || !_is_connected)
DieterGraef 0:d26c1b55cfca 75 return -1;
DieterGraef 0:d26c1b55cfca 76
DieterGraef 0:d26c1b55cfca 77 int writtenLen = 0;
DieterGraef 0:d26c1b55cfca 78 TimeInterval timeout(_timeout);
DieterGraef 0:d26c1b55cfca 79 while (writtenLen < length) {
DieterGraef 0:d26c1b55cfca 80 if (!_blocking) {
DieterGraef 0:d26c1b55cfca 81 // Wait for socket to be writeable
DieterGraef 0:d26c1b55cfca 82 if (wait_writable(timeout) != 0)
DieterGraef 0:d26c1b55cfca 83 return writtenLen;
DieterGraef 0:d26c1b55cfca 84 }
DieterGraef 0:d26c1b55cfca 85
DieterGraef 0:d26c1b55cfca 86 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
DieterGraef 0:d26c1b55cfca 87 if (ret > 0) {
DieterGraef 0:d26c1b55cfca 88 writtenLen += ret;
DieterGraef 0:d26c1b55cfca 89 continue;
DieterGraef 0:d26c1b55cfca 90 } else if (ret == 0) {
DieterGraef 0:d26c1b55cfca 91 _is_connected = false;
DieterGraef 0:d26c1b55cfca 92 return writtenLen;
DieterGraef 0:d26c1b55cfca 93 } else {
DieterGraef 0:d26c1b55cfca 94 return -1; //Connnection error
DieterGraef 0:d26c1b55cfca 95 }
DieterGraef 0:d26c1b55cfca 96 }
DieterGraef 0:d26c1b55cfca 97 return writtenLen;
DieterGraef 0:d26c1b55cfca 98 }
DieterGraef 0:d26c1b55cfca 99
DieterGraef 0:d26c1b55cfca 100 int TCPSocketConnection::receive(char* data, int length) {
DieterGraef 0:d26c1b55cfca 101 if ((_sock_fd < 0) || !_is_connected)
DieterGraef 0:d26c1b55cfca 102 return -1;
DieterGraef 0:d26c1b55cfca 103
DieterGraef 0:d26c1b55cfca 104 if (!_blocking) {
DieterGraef 0:d26c1b55cfca 105 TimeInterval timeout(_timeout);
DieterGraef 0:d26c1b55cfca 106 if (wait_readable(timeout) != 0)
DieterGraef 0:d26c1b55cfca 107 return -1;
DieterGraef 0:d26c1b55cfca 108 }
DieterGraef 0:d26c1b55cfca 109
DieterGraef 0:d26c1b55cfca 110 int n = lwip_recv(_sock_fd, data, length, 0);
DieterGraef 0:d26c1b55cfca 111 _is_connected = (n != 0);
DieterGraef 0:d26c1b55cfca 112
DieterGraef 0:d26c1b55cfca 113 return n;
DieterGraef 0:d26c1b55cfca 114 }
DieterGraef 0:d26c1b55cfca 115
DieterGraef 0:d26c1b55cfca 116 // -1 if unsuccessful, else number of bytes received
DieterGraef 0:d26c1b55cfca 117 int TCPSocketConnection::receive_all(char* data, int length) {
DieterGraef 0:d26c1b55cfca 118 if ((_sock_fd < 0) || !_is_connected)
DieterGraef 0:d26c1b55cfca 119 return -1;
DieterGraef 0:d26c1b55cfca 120
DieterGraef 0:d26c1b55cfca 121 int readLen = 0;
DieterGraef 0:d26c1b55cfca 122 TimeInterval timeout(_timeout);
DieterGraef 0:d26c1b55cfca 123 while (readLen < length) {
DieterGraef 0:d26c1b55cfca 124 if (!_blocking) {
DieterGraef 0:d26c1b55cfca 125 //Wait for socket to be readable
DieterGraef 0:d26c1b55cfca 126 if (wait_readable(timeout) != 0)
DieterGraef 0:d26c1b55cfca 127 return readLen;
DieterGraef 0:d26c1b55cfca 128 }
DieterGraef 0:d26c1b55cfca 129
DieterGraef 0:d26c1b55cfca 130 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
DieterGraef 0:d26c1b55cfca 131 if (ret > 0) {
DieterGraef 0:d26c1b55cfca 132 readLen += ret;
DieterGraef 0:d26c1b55cfca 133 } else if (ret == 0) {
DieterGraef 0:d26c1b55cfca 134 _is_connected = false;
DieterGraef 0:d26c1b55cfca 135 return readLen;
DieterGraef 0:d26c1b55cfca 136 } else {
DieterGraef 0:d26c1b55cfca 137 return -1; //Connnection error
DieterGraef 0:d26c1b55cfca 138 }
DieterGraef 0:d26c1b55cfca 139 }
DieterGraef 0:d26c1b55cfca 140 return readLen;
DieterGraef 0:d26c1b55cfca 141 }