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
Socket/Socket.cpp@1:28ba13dd96f7, 2016-06-23 (annotated)
- 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?
User | Revision | Line number | New 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 <cstring> |
DieterGraef | 0:d26c1b55cfca | 20 | |
DieterGraef | 0:d26c1b55cfca | 21 | using std::memset; |
DieterGraef | 0:d26c1b55cfca | 22 | |
DieterGraef | 0:d26c1b55cfca | 23 | Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) { |
DieterGraef | 0:d26c1b55cfca | 24 | |
DieterGraef | 0:d26c1b55cfca | 25 | } |
DieterGraef | 0:d26c1b55cfca | 26 | |
DieterGraef | 0:d26c1b55cfca | 27 | void Socket::set_blocking(bool blocking, unsigned int timeout) { |
DieterGraef | 0:d26c1b55cfca | 28 | _blocking = blocking; |
DieterGraef | 0:d26c1b55cfca | 29 | _timeout = timeout; |
DieterGraef | 0:d26c1b55cfca | 30 | } |
DieterGraef | 0:d26c1b55cfca | 31 | |
DieterGraef | 0:d26c1b55cfca | 32 | int Socket::init_socket(int type) { |
DieterGraef | 0:d26c1b55cfca | 33 | if (_sock_fd != -1) |
DieterGraef | 0:d26c1b55cfca | 34 | return -1; |
DieterGraef | 0:d26c1b55cfca | 35 | |
DieterGraef | 0:d26c1b55cfca | 36 | int fd = lwip_socket(AF_INET, type, 0); |
DieterGraef | 0:d26c1b55cfca | 37 | if (fd < 0) |
DieterGraef | 0:d26c1b55cfca | 38 | return -1; |
DieterGraef | 0:d26c1b55cfca | 39 | |
DieterGraef | 0:d26c1b55cfca | 40 | _sock_fd = fd; |
DieterGraef | 0:d26c1b55cfca | 41 | return 0; |
DieterGraef | 0:d26c1b55cfca | 42 | } |
DieterGraef | 0:d26c1b55cfca | 43 | |
DieterGraef | 0:d26c1b55cfca | 44 | int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { |
DieterGraef | 0:d26c1b55cfca | 45 | return lwip_setsockopt(_sock_fd, level, optname, optval, optlen); |
DieterGraef | 0:d26c1b55cfca | 46 | } |
DieterGraef | 0:d26c1b55cfca | 47 | |
DieterGraef | 0:d26c1b55cfca | 48 | int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { |
DieterGraef | 0:d26c1b55cfca | 49 | return lwip_getsockopt(_sock_fd, level, optname, optval, optlen); |
DieterGraef | 0:d26c1b55cfca | 50 | } |
DieterGraef | 0:d26c1b55cfca | 51 | |
DieterGraef | 0:d26c1b55cfca | 52 | int Socket::select(struct timeval *timeout, bool read, bool write) { |
DieterGraef | 0:d26c1b55cfca | 53 | fd_set fdSet; |
DieterGraef | 0:d26c1b55cfca | 54 | FD_ZERO(&fdSet); |
DieterGraef | 0:d26c1b55cfca | 55 | FD_SET(_sock_fd, &fdSet); |
DieterGraef | 0:d26c1b55cfca | 56 | |
DieterGraef | 0:d26c1b55cfca | 57 | fd_set* readset = (read ) ? (&fdSet) : (NULL); |
DieterGraef | 0:d26c1b55cfca | 58 | fd_set* writeset = (write) ? (&fdSet) : (NULL); |
DieterGraef | 0:d26c1b55cfca | 59 | |
DieterGraef | 0:d26c1b55cfca | 60 | int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout); |
DieterGraef | 0:d26c1b55cfca | 61 | return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); |
DieterGraef | 0:d26c1b55cfca | 62 | } |
DieterGraef | 0:d26c1b55cfca | 63 | |
DieterGraef | 0:d26c1b55cfca | 64 | int Socket::wait_readable(TimeInterval& timeout) { |
DieterGraef | 0:d26c1b55cfca | 65 | return select(&timeout._time, true, false); |
DieterGraef | 0:d26c1b55cfca | 66 | } |
DieterGraef | 0:d26c1b55cfca | 67 | |
DieterGraef | 0:d26c1b55cfca | 68 | int Socket::wait_writable(TimeInterval& timeout) { |
DieterGraef | 0:d26c1b55cfca | 69 | return select(&timeout._time, false, true); |
DieterGraef | 0:d26c1b55cfca | 70 | } |
DieterGraef | 0:d26c1b55cfca | 71 | |
DieterGraef | 0:d26c1b55cfca | 72 | int Socket::close(bool shutdown) { |
DieterGraef | 0:d26c1b55cfca | 73 | if (_sock_fd < 0) |
DieterGraef | 0:d26c1b55cfca | 74 | return -1; |
DieterGraef | 0:d26c1b55cfca | 75 | |
DieterGraef | 0:d26c1b55cfca | 76 | if (shutdown) |
DieterGraef | 0:d26c1b55cfca | 77 | lwip_shutdown(_sock_fd, SHUT_RDWR); |
DieterGraef | 0:d26c1b55cfca | 78 | lwip_close(_sock_fd); |
DieterGraef | 0:d26c1b55cfca | 79 | _sock_fd = -1; |
DieterGraef | 0:d26c1b55cfca | 80 | |
DieterGraef | 0:d26c1b55cfca | 81 | return 0; |
DieterGraef | 0:d26c1b55cfca | 82 | } |
DieterGraef | 0:d26c1b55cfca | 83 | |
DieterGraef | 0:d26c1b55cfca | 84 | Socket::~Socket() { |
DieterGraef | 0:d26c1b55cfca | 85 | close(); //Don't want to leak |
DieterGraef | 0:d26c1b55cfca | 86 | } |
DieterGraef | 0:d26c1b55cfca | 87 | |
DieterGraef | 0:d26c1b55cfca | 88 | TimeInterval::TimeInterval(unsigned int ms) { |
DieterGraef | 0:d26c1b55cfca | 89 | _time.tv_sec = ms / 1000; |
DieterGraef | 0:d26c1b55cfca | 90 | _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; |
DieterGraef | 0:d26c1b55cfca | 91 | } |