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:
Sun Jun 19 16:23:40 2016 +0000
Revision:
0:d26c1b55cfca
Ethernet Library for Nucleo stm32f746ZG and Disco stm32f746NG  works under arm and gcc environment

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 "Socket/Socket.h"
DieterGraef 0:d26c1b55cfca 19 #include "Socket/Endpoint.h"
DieterGraef 0:d26c1b55cfca 20 #include <cstring>
DieterGraef 0:d26c1b55cfca 21 #include <cstdio>
DieterGraef 0:d26c1b55cfca 22
DieterGraef 0:d26c1b55cfca 23 Endpoint::Endpoint() {
DieterGraef 0:d26c1b55cfca 24 reset_address();
DieterGraef 0:d26c1b55cfca 25 }
DieterGraef 0:d26c1b55cfca 26 Endpoint::~Endpoint() {}
DieterGraef 0:d26c1b55cfca 27
DieterGraef 0:d26c1b55cfca 28 void Endpoint::reset_address(void) {
DieterGraef 0:d26c1b55cfca 29 std::memset(&_remoteHost, 0, sizeof(struct sockaddr_in));
DieterGraef 0:d26c1b55cfca 30 _ipAddress[0] = '\0';
DieterGraef 0:d26c1b55cfca 31 }
DieterGraef 0:d26c1b55cfca 32
DieterGraef 0:d26c1b55cfca 33 #include "stdio.h"
DieterGraef 0:d26c1b55cfca 34
DieterGraef 0:d26c1b55cfca 35 int Endpoint::set_address(const char* host, const int port) {
DieterGraef 0:d26c1b55cfca 36 reset_address();
DieterGraef 0:d26c1b55cfca 37
DieterGraef 0:d26c1b55cfca 38 // IP Address
DieterGraef 0:d26c1b55cfca 39 char address[5];
DieterGraef 0:d26c1b55cfca 40 char *p_address = address;
DieterGraef 0:d26c1b55cfca 41
DieterGraef 0:d26c1b55cfca 42 // Dot-decimal notation
DieterGraef 0:d26c1b55cfca 43 int result = std::sscanf(host, "%3u.%3u.%3u.%3u",
DieterGraef 0:d26c1b55cfca 44 (unsigned int*)&address[0], (unsigned int*)&address[1],
DieterGraef 0:d26c1b55cfca 45 (unsigned int*)&address[2], (unsigned int*)&address[3]);
DieterGraef 0:d26c1b55cfca 46
DieterGraef 0:d26c1b55cfca 47 if (result != 4) {
DieterGraef 0:d26c1b55cfca 48 // Resolve address with DNS
DieterGraef 0:d26c1b55cfca 49 struct hostent *host_address = lwip_gethostbyname(host);
DieterGraef 0:d26c1b55cfca 50 if (host_address == NULL)
DieterGraef 0:d26c1b55cfca 51 return -1; //Could not resolve address
DieterGraef 0:d26c1b55cfca 52 p_address = (char*)host_address->h_addr_list[0];
DieterGraef 0:d26c1b55cfca 53 }
DieterGraef 0:d26c1b55cfca 54 std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4);
DieterGraef 0:d26c1b55cfca 55
DieterGraef 0:d26c1b55cfca 56 // Address family
DieterGraef 0:d26c1b55cfca 57 _remoteHost.sin_family = AF_INET;
DieterGraef 0:d26c1b55cfca 58
DieterGraef 0:d26c1b55cfca 59 // Set port
DieterGraef 0:d26c1b55cfca 60 _remoteHost.sin_port = htons(port);
DieterGraef 0:d26c1b55cfca 61
DieterGraef 0:d26c1b55cfca 62 return 0;
DieterGraef 0:d26c1b55cfca 63 }
DieterGraef 0:d26c1b55cfca 64
DieterGraef 0:d26c1b55cfca 65 int Endpoint::set_address(in_addr host) {
DieterGraef 0:d26c1b55cfca 66 reset_address();
DieterGraef 0:d26c1b55cfca 67 inet_ntoa_r(host, _ipAddress, sizeof(_ipAddress));
DieterGraef 0:d26c1b55cfca 68 return 0;
DieterGraef 0:d26c1b55cfca 69 }
DieterGraef 0:d26c1b55cfca 70
DieterGraef 0:d26c1b55cfca 71 char* Endpoint::get_address() {
DieterGraef 0:d26c1b55cfca 72 if ((_ipAddress[0] == '\0') && (_remoteHost.sin_addr.s_addr != 0))
DieterGraef 0:d26c1b55cfca 73 inet_ntoa_r(_remoteHost.sin_addr, _ipAddress, sizeof(_ipAddress));
DieterGraef 0:d26c1b55cfca 74 return _ipAddress;
DieterGraef 0:d26c1b55cfca 75 }
DieterGraef 0:d26c1b55cfca 76
DieterGraef 0:d26c1b55cfca 77 int Endpoint::get_port() {
DieterGraef 0:d26c1b55cfca 78 return ntohs(_remoteHost.sin_port);
DieterGraef 0:d26c1b55cfca 79 }