ST Americas mbed Team / Mbed 2 deprecated m2x-accel_ethernet_demo

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed

Fork of m2x-seeed_ethernet_demo by Sean Newton

Committer:
SeanNewton
Date:
Tue Sep 30 00:32:20 2014 +0000
Revision:
3:0fba8849a883
Initial Release

Who changed what in which revision?

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