M2X Ethernet demo using Seeed Ethernet W5200 Shield

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed Nucleo_Sensor_Shield

Fork of m2x-seeed_ethernet_demo by Sean Newton

Committer:
SeanNewton
Date:
Thu Sep 25 16:36:51 2014 +0000
Revision:
7:a94ba2e0cd04
CHange WIZnet_Library to folder to keep configuration changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SeanNewton 7:a94ba2e0cd04 1 /* Copyright (C) 2012 mbed.org, MIT License
SeanNewton 7:a94ba2e0cd04 2 *
SeanNewton 7:a94ba2e0cd04 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
SeanNewton 7:a94ba2e0cd04 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
SeanNewton 7:a94ba2e0cd04 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
SeanNewton 7:a94ba2e0cd04 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
SeanNewton 7:a94ba2e0cd04 7 * furnished to do so, subject to the following conditions:
SeanNewton 7:a94ba2e0cd04 8 *
SeanNewton 7:a94ba2e0cd04 9 * The above copyright notice and this permission notice shall be included in all copies or
SeanNewton 7:a94ba2e0cd04 10 * substantial portions of the Software.
SeanNewton 7:a94ba2e0cd04 11 *
SeanNewton 7:a94ba2e0cd04 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
SeanNewton 7:a94ba2e0cd04 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
SeanNewton 7:a94ba2e0cd04 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
SeanNewton 7:a94ba2e0cd04 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SeanNewton 7:a94ba2e0cd04 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SeanNewton 7:a94ba2e0cd04 17 */
SeanNewton 7:a94ba2e0cd04 18
SeanNewton 7:a94ba2e0cd04 19 #include "UDPSocket.h"
SeanNewton 7:a94ba2e0cd04 20
SeanNewton 7:a94ba2e0cd04 21 static int udp_local_port;
SeanNewton 7:a94ba2e0cd04 22
SeanNewton 7:a94ba2e0cd04 23 UDPSocket::UDPSocket()
SeanNewton 7:a94ba2e0cd04 24 {
SeanNewton 7:a94ba2e0cd04 25 }
SeanNewton 7:a94ba2e0cd04 26
SeanNewton 7:a94ba2e0cd04 27 // After init function, bind() should be called.
SeanNewton 7:a94ba2e0cd04 28 int UDPSocket::init(void)
SeanNewton 7:a94ba2e0cd04 29 {
SeanNewton 7:a94ba2e0cd04 30 if (_sock_fd < 0) {
SeanNewton 7:a94ba2e0cd04 31 _sock_fd = eth->new_socket();
SeanNewton 7:a94ba2e0cd04 32 }
SeanNewton 7:a94ba2e0cd04 33 if (eth->setProtocol(_sock_fd, UDP) == false) return -1;
SeanNewton 7:a94ba2e0cd04 34 return 0;
SeanNewton 7:a94ba2e0cd04 35 }
SeanNewton 7:a94ba2e0cd04 36
SeanNewton 7:a94ba2e0cd04 37 // Server initialization
SeanNewton 7:a94ba2e0cd04 38 int UDPSocket::bind(int port)
SeanNewton 7:a94ba2e0cd04 39 {
SeanNewton 7:a94ba2e0cd04 40 if (_sock_fd < 0) {
SeanNewton 7:a94ba2e0cd04 41 _sock_fd = eth->new_socket();
SeanNewton 7:a94ba2e0cd04 42 if (_sock_fd < 0) {
SeanNewton 7:a94ba2e0cd04 43 return -1;
SeanNewton 7:a94ba2e0cd04 44 }
SeanNewton 7:a94ba2e0cd04 45 }
SeanNewton 7:a94ba2e0cd04 46 // set local port
SeanNewton 7:a94ba2e0cd04 47 if (port != 0) {
SeanNewton 7:a94ba2e0cd04 48 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
SeanNewton 7:a94ba2e0cd04 49 } else {
SeanNewton 7:a94ba2e0cd04 50 udp_local_port++;
SeanNewton 7:a94ba2e0cd04 51 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port);
SeanNewton 7:a94ba2e0cd04 52 }
SeanNewton 7:a94ba2e0cd04 53 // set udp protocol
SeanNewton 7:a94ba2e0cd04 54 eth->setProtocol(_sock_fd, UDP);
SeanNewton 7:a94ba2e0cd04 55 eth->scmd(_sock_fd, OPEN);
SeanNewton 7:a94ba2e0cd04 56 return 0;
SeanNewton 7:a94ba2e0cd04 57 }
SeanNewton 7:a94ba2e0cd04 58
SeanNewton 7:a94ba2e0cd04 59 // -1 if unsuccessful, else number of bytes written
SeanNewton 7:a94ba2e0cd04 60 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
SeanNewton 7:a94ba2e0cd04 61 {
SeanNewton 7:a94ba2e0cd04 62 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
SeanNewton 7:a94ba2e0cd04 63 if (size < 0) {
SeanNewton 7:a94ba2e0cd04 64 return -1;
SeanNewton 7:a94ba2e0cd04 65 }
SeanNewton 7:a94ba2e0cd04 66 confEndpoint(remote);
SeanNewton 7:a94ba2e0cd04 67 int ret = eth->send(_sock_fd, packet, length);
SeanNewton 7:a94ba2e0cd04 68 return ret;
SeanNewton 7:a94ba2e0cd04 69 }
SeanNewton 7:a94ba2e0cd04 70
SeanNewton 7:a94ba2e0cd04 71 // -1 if unsuccessful, else number of bytes received
SeanNewton 7:a94ba2e0cd04 72 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
SeanNewton 7:a94ba2e0cd04 73 {
SeanNewton 7:a94ba2e0cd04 74 uint8_t info[8];
SeanNewton 7:a94ba2e0cd04 75 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
SeanNewton 7:a94ba2e0cd04 76 if (size < 0) {
SeanNewton 7:a94ba2e0cd04 77 return -1;
SeanNewton 7:a94ba2e0cd04 78 }
SeanNewton 7:a94ba2e0cd04 79 eth->recv(_sock_fd, (char*)info, sizeof(info));
SeanNewton 7:a94ba2e0cd04 80 readEndpoint(remote, info);
SeanNewton 7:a94ba2e0cd04 81 int udp_size = info[6]<<8|info[7];
SeanNewton 7:a94ba2e0cd04 82 //TEST_ASSERT(udp_size <= (size-sizeof(info)));
SeanNewton 7:a94ba2e0cd04 83 if (udp_size > (size-sizeof(info))) {
SeanNewton 7:a94ba2e0cd04 84 return -1;
SeanNewton 7:a94ba2e0cd04 85 }
SeanNewton 7:a94ba2e0cd04 86 if (udp_size > length)
SeanNewton 7:a94ba2e0cd04 87 {
SeanNewton 7:a94ba2e0cd04 88 //printf("udp_size: %d\n",udp_size);
SeanNewton 7:a94ba2e0cd04 89 return -1;
SeanNewton 7:a94ba2e0cd04 90 }
SeanNewton 7:a94ba2e0cd04 91 return eth->recv(_sock_fd, buffer, udp_size);
SeanNewton 7:a94ba2e0cd04 92 }
SeanNewton 7:a94ba2e0cd04 93
SeanNewton 7:a94ba2e0cd04 94 void UDPSocket::confEndpoint(Endpoint & ep)
SeanNewton 7:a94ba2e0cd04 95 {
SeanNewton 7:a94ba2e0cd04 96 char * host = ep.get_address();
SeanNewton 7:a94ba2e0cd04 97 // set remote host
SeanNewton 7:a94ba2e0cd04 98 eth->sreg_ip(_sock_fd, Sn_DIPR, host);
SeanNewton 7:a94ba2e0cd04 99 // set remote port
SeanNewton 7:a94ba2e0cd04 100 eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
SeanNewton 7:a94ba2e0cd04 101 }
SeanNewton 7:a94ba2e0cd04 102
SeanNewton 7:a94ba2e0cd04 103 void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
SeanNewton 7:a94ba2e0cd04 104 {
SeanNewton 7:a94ba2e0cd04 105 char addr[17];
SeanNewton 7:a94ba2e0cd04 106 snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
SeanNewton 7:a94ba2e0cd04 107 uint16_t port = info[4]<<8|info[5];
SeanNewton 7:a94ba2e0cd04 108 ep.set_address(addr, port);
SeanNewton 7:a94ba2e0cd04 109 }
SeanNewton 7:a94ba2e0cd04 110