mbed OS5
Fork of UIPEthernet by
UIPServer.cpp@2:049ce85163c5, 2014-12-20 (annotated)
- Committer:
- hudakz
- Date:
- Sat Dec 20 11:08:11 2014 +0000
- Revision:
- 2:049ce85163c5
- Parent:
- 0:5350a66d5279
- Child:
- 4:d774541a34da
02 Name clash with "Ethernet" fixed for LPC1768
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:5350a66d5279 | 1 | /* |
hudakz | 0:5350a66d5279 | 2 | UIPServer.cpp - Arduino implementation of a uIP wrapper class. |
hudakz | 0:5350a66d5279 | 3 | Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> |
hudakz | 0:5350a66d5279 | 4 | All rights reserved. |
hudakz | 0:5350a66d5279 | 5 | |
hudakz | 0:5350a66d5279 | 6 | This program is free software: you can redistribute it and/or modify |
hudakz | 0:5350a66d5279 | 7 | it under the terms of the GNU General Public License as published by |
hudakz | 0:5350a66d5279 | 8 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 0:5350a66d5279 | 9 | (at your option) any later version. |
hudakz | 0:5350a66d5279 | 10 | |
hudakz | 0:5350a66d5279 | 11 | This program is distributed in the hope that it will be useful, |
hudakz | 0:5350a66d5279 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 0:5350a66d5279 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 0:5350a66d5279 | 14 | GNU General Public License for more details. |
hudakz | 0:5350a66d5279 | 15 | |
hudakz | 0:5350a66d5279 | 16 | You should have received a copy of the GNU General Public License |
hudakz | 0:5350a66d5279 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 0:5350a66d5279 | 18 | */ |
hudakz | 0:5350a66d5279 | 19 | #include "UIPEthernet.h" |
hudakz | 0:5350a66d5279 | 20 | #include "UIPServer.h" |
hudakz | 0:5350a66d5279 | 21 | extern "C" |
hudakz | 0:5350a66d5279 | 22 | { |
hudakz | 2:049ce85163c5 | 23 | #include "utility/uip-conf.h" |
hudakz | 0:5350a66d5279 | 24 | } |
hudakz | 0:5350a66d5279 | 25 | /** |
hudakz | 0:5350a66d5279 | 26 | * @brief |
hudakz | 0:5350a66d5279 | 27 | * @note |
hudakz | 0:5350a66d5279 | 28 | * @param |
hudakz | 0:5350a66d5279 | 29 | * @retval |
hudakz | 0:5350a66d5279 | 30 | */ |
hudakz | 0:5350a66d5279 | 31 | UIPServer::UIPServer(uint16_t port) : |
hudakz | 0:5350a66d5279 | 32 | _port(htons(port)) { |
hudakz | 0:5350a66d5279 | 33 | UIPEthernet.set_uip_callback(&UIPClient::uip_callback); |
hudakz | 0:5350a66d5279 | 34 | } |
hudakz | 0:5350a66d5279 | 35 | |
hudakz | 0:5350a66d5279 | 36 | /** |
hudakz | 0:5350a66d5279 | 37 | * @brief |
hudakz | 0:5350a66d5279 | 38 | * @note |
hudakz | 0:5350a66d5279 | 39 | * @param |
hudakz | 0:5350a66d5279 | 40 | * @retval |
hudakz | 0:5350a66d5279 | 41 | */ |
hudakz | 0:5350a66d5279 | 42 | UIPClient UIPServer::available(void) { |
hudakz | 0:5350a66d5279 | 43 | UIPEthernet.tick(); |
hudakz | 0:5350a66d5279 | 44 | |
hudakz | 0:5350a66d5279 | 45 | uip_userdata_t* u; |
hudakz | 0:5350a66d5279 | 46 | for(uint8_t sock = 0; sock < UIP_CONNS; sock++) { |
hudakz | 0:5350a66d5279 | 47 | struct uip_conn* conn = &uip_conns[sock]; |
hudakz | 0:5350a66d5279 | 48 | if(conn->lport == _port && (u = (uip_userdata_t*)conn->appstate.user)) { |
hudakz | 0:5350a66d5279 | 49 | if(UIPClient::_available(u)) |
hudakz | 0:5350a66d5279 | 50 | return UIPClient(conn); |
hudakz | 0:5350a66d5279 | 51 | } |
hudakz | 0:5350a66d5279 | 52 | } |
hudakz | 0:5350a66d5279 | 53 | |
hudakz | 0:5350a66d5279 | 54 | uip_userdata_closed_t ** cc = &UIPClient::closed_conns[0]; |
hudakz | 0:5350a66d5279 | 55 | for(uint8_t i = 0; i < UIP_CONNS; i++) { |
hudakz | 0:5350a66d5279 | 56 | if(*cc && (*cc)->lport == _port) { |
hudakz | 0:5350a66d5279 | 57 | if((*cc)->packets_in[0] == NOBLOCK) { |
hudakz | 0:5350a66d5279 | 58 | free(*cc); |
hudakz | 0:5350a66d5279 | 59 | *cc = NULL; |
hudakz | 0:5350a66d5279 | 60 | } |
hudakz | 0:5350a66d5279 | 61 | else |
hudakz | 0:5350a66d5279 | 62 | return UIPClient(*cc); |
hudakz | 0:5350a66d5279 | 63 | } |
hudakz | 0:5350a66d5279 | 64 | |
hudakz | 0:5350a66d5279 | 65 | cc++; |
hudakz | 0:5350a66d5279 | 66 | } |
hudakz | 0:5350a66d5279 | 67 | |
hudakz | 0:5350a66d5279 | 68 | return UIPClient(); |
hudakz | 0:5350a66d5279 | 69 | } |
hudakz | 0:5350a66d5279 | 70 | |
hudakz | 0:5350a66d5279 | 71 | /** |
hudakz | 0:5350a66d5279 | 72 | * @brief |
hudakz | 0:5350a66d5279 | 73 | * @note |
hudakz | 0:5350a66d5279 | 74 | * @param |
hudakz | 0:5350a66d5279 | 75 | * @retval |
hudakz | 0:5350a66d5279 | 76 | */ |
hudakz | 0:5350a66d5279 | 77 | void UIPServer::begin(void) { |
hudakz | 0:5350a66d5279 | 78 | uip_listen(_port); |
hudakz | 0:5350a66d5279 | 79 | UIPEthernet.tick(); |
hudakz | 0:5350a66d5279 | 80 | } |
hudakz | 0:5350a66d5279 | 81 | |
hudakz | 0:5350a66d5279 | 82 | /** |
hudakz | 0:5350a66d5279 | 83 | * @brief |
hudakz | 0:5350a66d5279 | 84 | * @note |
hudakz | 0:5350a66d5279 | 85 | * @param |
hudakz | 0:5350a66d5279 | 86 | * @retval |
hudakz | 0:5350a66d5279 | 87 | */ |
hudakz | 0:5350a66d5279 | 88 | size_t UIPServer::write(uint8_t c) { |
hudakz | 0:5350a66d5279 | 89 | return write(&c, 1); |
hudakz | 0:5350a66d5279 | 90 | } |
hudakz | 0:5350a66d5279 | 91 | |
hudakz | 0:5350a66d5279 | 92 | /** |
hudakz | 0:5350a66d5279 | 93 | * @brief |
hudakz | 0:5350a66d5279 | 94 | * @note |
hudakz | 0:5350a66d5279 | 95 | * @param |
hudakz | 0:5350a66d5279 | 96 | * @retval |
hudakz | 0:5350a66d5279 | 97 | */ |
hudakz | 0:5350a66d5279 | 98 | size_t UIPServer::write(const uint8_t* buf, size_t size) { |
hudakz | 0:5350a66d5279 | 99 | size_t ret = 0; |
hudakz | 0:5350a66d5279 | 100 | for(int sock = 0; sock < UIP_CONNS; sock++) { |
hudakz | 0:5350a66d5279 | 101 | struct uip_conn* conn = &uip_conns[sock]; |
hudakz | 0:5350a66d5279 | 102 | if(conn->lport == _port && (conn->tcpstateflags != UIP_CLOSE)) |
hudakz | 0:5350a66d5279 | 103 | ret = UIPClient::_write(conn, buf, size); |
hudakz | 0:5350a66d5279 | 104 | } |
hudakz | 0:5350a66d5279 | 105 | |
hudakz | 0:5350a66d5279 | 106 | return ret; |
hudakz | 0:5350a66d5279 | 107 | } |
hudakz | 0:5350a66d5279 | 108 | |
hudakz | 0:5350a66d5279 | 109 |