ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:f9b6112278fe 1 /* Copyright (C) 2012 mbed.org, MIT License
DieterGraef 0:f9b6112278fe 2 *
DieterGraef 0:f9b6112278fe 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
DieterGraef 0:f9b6112278fe 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
DieterGraef 0:f9b6112278fe 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
DieterGraef 0:f9b6112278fe 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
DieterGraef 0:f9b6112278fe 7 * furnished to do so, subject to the following conditions:
DieterGraef 0:f9b6112278fe 8 *
DieterGraef 0:f9b6112278fe 9 * The above copyright notice and this permission notice shall be included in all copies or
DieterGraef 0:f9b6112278fe 10 * substantial portions of the Software.
DieterGraef 0:f9b6112278fe 11 *
DieterGraef 0:f9b6112278fe 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
DieterGraef 0:f9b6112278fe 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
DieterGraef 0:f9b6112278fe 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DieterGraef 0:f9b6112278fe 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DieterGraef 0:f9b6112278fe 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DieterGraef 0:f9b6112278fe 17 */
DieterGraef 0:f9b6112278fe 18 #include "Socket/Socket.h"
DieterGraef 0:f9b6112278fe 19 #include <cstring>
DieterGraef 0:f9b6112278fe 20
DieterGraef 0:f9b6112278fe 21 using std::memset;
DieterGraef 0:f9b6112278fe 22
DieterGraef 0:f9b6112278fe 23 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
DieterGraef 0:f9b6112278fe 24
DieterGraef 0:f9b6112278fe 25 }
DieterGraef 0:f9b6112278fe 26
DieterGraef 0:f9b6112278fe 27 void Socket::set_blocking(bool blocking, unsigned int timeout) {
DieterGraef 0:f9b6112278fe 28 _blocking = blocking;
DieterGraef 0:f9b6112278fe 29 _timeout = timeout;
DieterGraef 0:f9b6112278fe 30 }
DieterGraef 0:f9b6112278fe 31
DieterGraef 0:f9b6112278fe 32 int Socket::init_socket(int type) {
DieterGraef 0:f9b6112278fe 33 if (_sock_fd != -1)
DieterGraef 0:f9b6112278fe 34 return -1;
DieterGraef 0:f9b6112278fe 35
DieterGraef 0:f9b6112278fe 36 int fd = lwip_socket(AF_INET, type, 0);
DieterGraef 0:f9b6112278fe 37 if (fd < 0)
DieterGraef 0:f9b6112278fe 38 return -1;
DieterGraef 0:f9b6112278fe 39
DieterGraef 0:f9b6112278fe 40 _sock_fd = fd;
DieterGraef 0:f9b6112278fe 41 return 0;
DieterGraef 0:f9b6112278fe 42 }
DieterGraef 0:f9b6112278fe 43
DieterGraef 0:f9b6112278fe 44 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
DieterGraef 0:f9b6112278fe 45 return lwip_setsockopt(_sock_fd, level, optname, optval, optlen);
DieterGraef 0:f9b6112278fe 46 }
DieterGraef 0:f9b6112278fe 47
DieterGraef 0:f9b6112278fe 48 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
DieterGraef 0:f9b6112278fe 49 return lwip_getsockopt(_sock_fd, level, optname, optval, optlen);
DieterGraef 0:f9b6112278fe 50 }
DieterGraef 0:f9b6112278fe 51
DieterGraef 0:f9b6112278fe 52 int Socket::select(struct timeval *timeout, bool read, bool write) {
DieterGraef 0:f9b6112278fe 53 fd_set fdSet;
DieterGraef 0:f9b6112278fe 54 FD_ZERO(&fdSet);
DieterGraef 0:f9b6112278fe 55 FD_SET(_sock_fd, &fdSet);
DieterGraef 0:f9b6112278fe 56
DieterGraef 0:f9b6112278fe 57 fd_set* readset = (read ) ? (&fdSet) : (NULL);
DieterGraef 0:f9b6112278fe 58 fd_set* writeset = (write) ? (&fdSet) : (NULL);
DieterGraef 0:f9b6112278fe 59
DieterGraef 0:f9b6112278fe 60 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
DieterGraef 0:f9b6112278fe 61 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
DieterGraef 0:f9b6112278fe 62 }
DieterGraef 0:f9b6112278fe 63
DieterGraef 0:f9b6112278fe 64 int Socket::wait_readable(TimeInterval& timeout) {
DieterGraef 0:f9b6112278fe 65 return select(&timeout._time, true, false);
DieterGraef 0:f9b6112278fe 66 }
DieterGraef 0:f9b6112278fe 67
DieterGraef 0:f9b6112278fe 68 int Socket::wait_writable(TimeInterval& timeout) {
DieterGraef 0:f9b6112278fe 69 return select(&timeout._time, false, true);
DieterGraef 0:f9b6112278fe 70 }
DieterGraef 0:f9b6112278fe 71
DieterGraef 0:f9b6112278fe 72 int Socket::close(bool shutdown) {
DieterGraef 0:f9b6112278fe 73 if (_sock_fd < 0)
DieterGraef 0:f9b6112278fe 74 return -1;
DieterGraef 0:f9b6112278fe 75
DieterGraef 0:f9b6112278fe 76 if (shutdown)
DieterGraef 0:f9b6112278fe 77 lwip_shutdown(_sock_fd, SHUT_RDWR);
DieterGraef 0:f9b6112278fe 78 lwip_close(_sock_fd);
DieterGraef 0:f9b6112278fe 79 _sock_fd = -1;
DieterGraef 0:f9b6112278fe 80
DieterGraef 0:f9b6112278fe 81 return 0;
DieterGraef 0:f9b6112278fe 82 }
DieterGraef 0:f9b6112278fe 83
DieterGraef 0:f9b6112278fe 84 Socket::~Socket() {
DieterGraef 0:f9b6112278fe 85 close(); //Don't want to leak
DieterGraef 0:f9b6112278fe 86 }
DieterGraef 0:f9b6112278fe 87
DieterGraef 0:f9b6112278fe 88 TimeInterval::TimeInterval(unsigned int ms) {
DieterGraef 0:f9b6112278fe 89 _time.tv_sec = ms / 1000;
DieterGraef 0:f9b6112278fe 90 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
DieterGraef 0:f9b6112278fe 91 }