ap

Dependencies:   DnsQuery

Dependents:   WizFi310_TCP_Echo_Server_Example

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Mon Feb 22 19:07:36 2016 -0600
Branch:
api-changes
Revision:
32:2c5fc105fc50
Parent:
31:7f15b95f2a1d
Child:
34:c17745683385
Added open/close calls to all sockets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 25:ed7b2a52e8ac 1 /* Socket
Christopher Haster 25:ed7b2a52e8ac 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 25:ed7b2a52e8ac 3 *
Christopher Haster 25:ed7b2a52e8ac 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 25:ed7b2a52e8ac 5 * you may not use this file except in compliance with the License.
Christopher Haster 25:ed7b2a52e8ac 6 * You may obtain a copy of the License at
Christopher Haster 25:ed7b2a52e8ac 7 *
Christopher Haster 25:ed7b2a52e8ac 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 25:ed7b2a52e8ac 9 *
Christopher Haster 25:ed7b2a52e8ac 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 25:ed7b2a52e8ac 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 25:ed7b2a52e8ac 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 25:ed7b2a52e8ac 13 * See the License for the specific language governing permissions and
Christopher Haster 25:ed7b2a52e8ac 14 * limitations under the License.
Christopher Haster 25:ed7b2a52e8ac 15 */
Christopher Haster 25:ed7b2a52e8ac 16
Christopher Haster 25:ed7b2a52e8ac 17 #include "Socket.h"
geky 29:7bcec3189a93 18 #include <string.h>
Christopher Haster 25:ed7b2a52e8ac 19
Christopher Haster 28:163fbe3263f4 20 Socket::Socket(NetworkInterface *iface, socket_protocol_t proto)
geky 30:3cc78f5db99d 21 : _iface(iface)
geky 30:3cc78f5db99d 22 , _proto(proto)
geky 30:3cc78f5db99d 23 , _socket(0)
geky 30:3cc78f5db99d 24 {
Christopher Haster 28:163fbe3263f4 25 memset(_ip_address, 0, SOCK_IP_SIZE);
Christopher Haster 28:163fbe3263f4 26 _port = 0;
Christopher Haster 25:ed7b2a52e8ac 27 }
Christopher Haster 25:ed7b2a52e8ac 28
geky 30:3cc78f5db99d 29 Socket::~Socket()
geky 30:3cc78f5db99d 30 {
Christopher Haster 32:2c5fc105fc50 31 if (_socket) close();
Christopher Haster 25:ed7b2a52e8ac 32 }
Christopher Haster 25:ed7b2a52e8ac 33
geky 30:3cc78f5db99d 34 int32_t Socket::setURL(const char *url, uint16_t port)
geky 30:3cc78f5db99d 35 {
geky 31:7f15b95f2a1d 36 int32_t err = _iface->getHostByName(url, _ip_address);
Christopher Haster 32:2c5fc105fc50 37 if (err) return err;
geky 31:7f15b95f2a1d 38
geky 31:7f15b95f2a1d 39 if (_socket) {
geky 31:7f15b95f2a1d 40 _socket->setIPAddress(_ip_address);
geky 31:7f15b95f2a1d 41 }
Christopher Haster 28:163fbe3263f4 42
Christopher Haster 28:163fbe3263f4 43 if (port) {
Christopher Haster 28:163fbe3263f4 44 setPort(port);
Christopher Haster 28:163fbe3263f4 45 }
Christopher Haster 28:163fbe3263f4 46
Christopher Haster 28:163fbe3263f4 47 return 0;
Christopher Haster 25:ed7b2a52e8ac 48 }
Christopher Haster 25:ed7b2a52e8ac 49
geky 30:3cc78f5db99d 50 void Socket::setIPAddress(const char *ip, uint16_t port)
geky 30:3cc78f5db99d 51 {
Christopher Haster 28:163fbe3263f4 52 strcpy(_ip_address, ip);
Christopher Haster 28:163fbe3263f4 53
Christopher Haster 28:163fbe3263f4 54 if (_socket) {
geky 31:7f15b95f2a1d 55 _socket->setIPAddress(_ip_address);
Christopher Haster 27:d7ed39727306 56 }
Christopher Haster 28:163fbe3263f4 57
Christopher Haster 28:163fbe3263f4 58 if (port) {
Christopher Haster 28:163fbe3263f4 59 setPort(port);
Christopher Haster 28:163fbe3263f4 60 }
Christopher Haster 25:ed7b2a52e8ac 61 }
Christopher Haster 25:ed7b2a52e8ac 62
geky 30:3cc78f5db99d 63 void Socket::setPort(uint16_t port)
geky 30:3cc78f5db99d 64 {
Christopher Haster 28:163fbe3263f4 65 _port = port;
Christopher Haster 28:163fbe3263f4 66
Christopher Haster 28:163fbe3263f4 67 if (_socket) {
geky 31:7f15b95f2a1d 68 _socket->setPort(_port);
Christopher Haster 27:d7ed39727306 69 }
Christopher Haster 25:ed7b2a52e8ac 70 }
Christopher Haster 25:ed7b2a52e8ac 71
geky 30:3cc78f5db99d 72 const char *Socket::getIPAddress() const
geky 30:3cc78f5db99d 73 {
Christopher Haster 28:163fbe3263f4 74 return _ip_address;
Christopher Haster 25:ed7b2a52e8ac 75 }
Christopher Haster 25:ed7b2a52e8ac 76
geky 30:3cc78f5db99d 77 uint16_t Socket::getPort() const
geky 30:3cc78f5db99d 78 {
Christopher Haster 28:163fbe3263f4 79 return _port;
Christopher Haster 25:ed7b2a52e8ac 80 }
Christopher Haster 25:ed7b2a52e8ac 81
Christopher Haster 32:2c5fc105fc50 82 int32_t Socket::open()
Christopher Haster 32:2c5fc105fc50 83 {
Christopher Haster 32:2c5fc105fc50 84 if (_socket) close();
Christopher Haster 32:2c5fc105fc50 85
Christopher Haster 32:2c5fc105fc50 86 _socket = _iface->createSocket(_proto);
Christopher Haster 32:2c5fc105fc50 87 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 88
Christopher Haster 32:2c5fc105fc50 89 if (_ip_address[0]) {
Christopher Haster 32:2c5fc105fc50 90 _socket->setIPAddress(_ip_address);
Christopher Haster 32:2c5fc105fc50 91 }
Christopher Haster 32:2c5fc105fc50 92
Christopher Haster 32:2c5fc105fc50 93 if (_port) {
Christopher Haster 32:2c5fc105fc50 94 _socket->setPort(_port);
Christopher Haster 32:2c5fc105fc50 95 }
Christopher Haster 32:2c5fc105fc50 96
Christopher Haster 32:2c5fc105fc50 97 int32_t err = _socket->open();
Christopher Haster 32:2c5fc105fc50 98
Christopher Haster 32:2c5fc105fc50 99 if (err) {
Christopher Haster 32:2c5fc105fc50 100 _iface->destroySocket(_socket);
Christopher Haster 32:2c5fc105fc50 101 }
Christopher Haster 32:2c5fc105fc50 102
Christopher Haster 32:2c5fc105fc50 103 return err;
Christopher Haster 32:2c5fc105fc50 104 }
Christopher Haster 32:2c5fc105fc50 105
Christopher Haster 32:2c5fc105fc50 106 int32_t Socket::close()
Christopher Haster 32:2c5fc105fc50 107 {
Christopher Haster 32:2c5fc105fc50 108 if (!_socket) return 0;
Christopher Haster 32:2c5fc105fc50 109
Christopher Haster 32:2c5fc105fc50 110 int32_t err = _socket->close();
Christopher Haster 32:2c5fc105fc50 111
Christopher Haster 32:2c5fc105fc50 112 if (!err) {
Christopher Haster 32:2c5fc105fc50 113 _iface->destroySocket(_socket);
Christopher Haster 32:2c5fc105fc50 114 }
Christopher Haster 32:2c5fc105fc50 115
Christopher Haster 32:2c5fc105fc50 116 return err;
Christopher Haster 32:2c5fc105fc50 117 }
Christopher Haster 32:2c5fc105fc50 118
geky 30:3cc78f5db99d 119 int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 120 {
Christopher Haster 32:2c5fc105fc50 121 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 122
Christopher Haster 32:2c5fc105fc50 123 return _socket->send(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 124 }
Christopher Haster 25:ed7b2a52e8ac 125
geky 30:3cc78f5db99d 126 int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 127 {
Christopher Haster 32:2c5fc105fc50 128 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 129
Christopher Haster 32:2c5fc105fc50 130 return _socket->recv(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 131 }
Christopher Haster 25:ed7b2a52e8ac 132