KL25 driver for Tango Control System

Dependencies:   mbed

Committer:
jskl
Date:
Thu Aug 28 07:50:06 2014 +0000
Revision:
2:9fe6f1e273b4
Parent:
0:5d27c333afa6
Fixed bugs in JSON format replies

Who changed what in which revision?

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