ap

Dependencies:   DnsQuery

Dependents:   WizFi310_TCP_Echo_Server_Example

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Mon Feb 22 20:51:09 2016 -0600
Branch:
api-changes
Revision:
38:157fb2ab965f
Parent:
36:eab792dfb0d8
Child:
39:47138420ea42
Added isConnected method to Socket/SocketInterface

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
Christopher Haster 34:c17745683385 34 int32_t Socket::setURL(const char *url)
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 return 0;
Christopher Haster 25:ed7b2a52e8ac 44 }
Christopher Haster 25:ed7b2a52e8ac 45
Christopher Haster 34:c17745683385 46 void Socket::setIPAddress(const char *ip)
geky 30:3cc78f5db99d 47 {
Christopher Haster 28:163fbe3263f4 48 strcpy(_ip_address, ip);
Christopher Haster 28:163fbe3263f4 49
Christopher Haster 28:163fbe3263f4 50 if (_socket) {
geky 31:7f15b95f2a1d 51 _socket->setIPAddress(_ip_address);
Christopher Haster 27:d7ed39727306 52 }
Christopher Haster 25:ed7b2a52e8ac 53 }
Christopher Haster 25:ed7b2a52e8ac 54
geky 30:3cc78f5db99d 55 void Socket::setPort(uint16_t port)
geky 30:3cc78f5db99d 56 {
Christopher Haster 28:163fbe3263f4 57 _port = port;
Christopher Haster 28:163fbe3263f4 58
Christopher Haster 28:163fbe3263f4 59 if (_socket) {
geky 31:7f15b95f2a1d 60 _socket->setPort(_port);
Christopher Haster 27:d7ed39727306 61 }
Christopher Haster 25:ed7b2a52e8ac 62 }
Christopher Haster 25:ed7b2a52e8ac 63
geky 30:3cc78f5db99d 64 const char *Socket::getIPAddress() const
geky 30:3cc78f5db99d 65 {
Christopher Haster 28:163fbe3263f4 66 return _ip_address;
Christopher Haster 25:ed7b2a52e8ac 67 }
Christopher Haster 25:ed7b2a52e8ac 68
geky 30:3cc78f5db99d 69 uint16_t Socket::getPort() const
geky 30:3cc78f5db99d 70 {
Christopher Haster 28:163fbe3263f4 71 return _port;
Christopher Haster 25:ed7b2a52e8ac 72 }
Christopher Haster 25:ed7b2a52e8ac 73
Christopher Haster 38:157fb2ab965f 74 bool Socket::isConnected()
Christopher Haster 38:157fb2ab965f 75 {
Christopher Haster 38:157fb2ab965f 76 if (!_socket) {
Christopher Haster 38:157fb2ab965f 77 return false;
Christopher Haster 38:157fb2ab965f 78 }
Christopher Haster 38:157fb2ab965f 79
Christopher Haster 38:157fb2ab965f 80 return _socket->isConnected();
Christopher Haster 38:157fb2ab965f 81 }
Christopher Haster 38:157fb2ab965f 82
Christopher Haster 34:c17745683385 83 int32_t Socket::open(const char *url, uint16_t port)
Christopher Haster 32:2c5fc105fc50 84 {
Christopher Haster 34:c17745683385 85 if (_socket) {
Christopher Haster 34:c17745683385 86 int32_t err = close();
Christopher Haster 34:c17745683385 87 if (err) return err;
Christopher Haster 34:c17745683385 88 }
Christopher Haster 34:c17745683385 89
Christopher Haster 34:c17745683385 90 if (url) {
Christopher Haster 34:c17745683385 91 int32_t err = setURL(url);
Christopher Haster 34:c17745683385 92 if (err) return err;
Christopher Haster 34:c17745683385 93 }
Christopher Haster 34:c17745683385 94
Christopher Haster 34:c17745683385 95 if (port) {
Christopher Haster 34:c17745683385 96 setPort(port);
Christopher Haster 34:c17745683385 97 }
Christopher Haster 34:c17745683385 98
Christopher Haster 35:838393fbc2ca 99 if (!_ip_address[0] || !_port) {
Christopher Haster 35:838393fbc2ca 100 return -3;
Christopher Haster 35:838393fbc2ca 101 }
Christopher Haster 32:2c5fc105fc50 102
Christopher Haster 32:2c5fc105fc50 103 _socket = _iface->createSocket(_proto);
Christopher Haster 32:2c5fc105fc50 104 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 105
Christopher Haster 36:eab792dfb0d8 106 int32_t err = _socket->open(_ip_address, _port);
Christopher Haster 34:c17745683385 107
Christopher Haster 32:2c5fc105fc50 108 if (err) {
Christopher Haster 32:2c5fc105fc50 109 _iface->destroySocket(_socket);
Christopher Haster 32:2c5fc105fc50 110 }
Christopher Haster 32:2c5fc105fc50 111
Christopher Haster 32:2c5fc105fc50 112 return err;
Christopher Haster 32:2c5fc105fc50 113 }
Christopher Haster 32:2c5fc105fc50 114
Christopher Haster 32:2c5fc105fc50 115 int32_t Socket::close()
Christopher Haster 32:2c5fc105fc50 116 {
Christopher Haster 32:2c5fc105fc50 117 if (!_socket) return 0;
Christopher Haster 32:2c5fc105fc50 118
Christopher Haster 32:2c5fc105fc50 119 int32_t err = _socket->close();
Christopher Haster 32:2c5fc105fc50 120
Christopher Haster 32:2c5fc105fc50 121 if (!err) {
Christopher Haster 32:2c5fc105fc50 122 _iface->destroySocket(_socket);
Christopher Haster 32:2c5fc105fc50 123 }
Christopher Haster 32:2c5fc105fc50 124
Christopher Haster 32:2c5fc105fc50 125 return err;
Christopher Haster 32:2c5fc105fc50 126 }
Christopher Haster 32:2c5fc105fc50 127
geky 30:3cc78f5db99d 128 int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 129 {
Christopher Haster 32:2c5fc105fc50 130 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 131 return _socket->send(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 132 }
Christopher Haster 25:ed7b2a52e8ac 133
geky 30:3cc78f5db99d 134 int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 135 {
Christopher Haster 32:2c5fc105fc50 136 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 137 return _socket->recv(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 138 }
Christopher Haster 25:ed7b2a52e8ac 139