ap

Dependencies:   DnsQuery

Dependents:   WizFi310_TCP_Echo_Server_Example

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Mon Feb 22 21:50:15 2016 -0600
Branch:
api-changes
Revision:
39:47138420ea42
Parent:
38:157fb2ab965f
Child:
41:3ec1c97e9bbf
Added proper handling of missing ip/port

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 39:47138420ea42 66 if (_ip_address[0]) {
Christopher Haster 39:47138420ea42 67 return _ip_address;
Christopher Haster 39:47138420ea42 68 } else {
Christopher Haster 39:47138420ea42 69 return 0;
Christopher Haster 39:47138420ea42 70 }
Christopher Haster 25:ed7b2a52e8ac 71 }
Christopher Haster 25:ed7b2a52e8ac 72
geky 30:3cc78f5db99d 73 uint16_t Socket::getPort() const
geky 30:3cc78f5db99d 74 {
Christopher Haster 28:163fbe3263f4 75 return _port;
Christopher Haster 25:ed7b2a52e8ac 76 }
Christopher Haster 25:ed7b2a52e8ac 77
Christopher Haster 38:157fb2ab965f 78 bool Socket::isConnected()
Christopher Haster 38:157fb2ab965f 79 {
Christopher Haster 38:157fb2ab965f 80 if (!_socket) {
Christopher Haster 38:157fb2ab965f 81 return false;
Christopher Haster 38:157fb2ab965f 82 }
Christopher Haster 38:157fb2ab965f 83
Christopher Haster 38:157fb2ab965f 84 return _socket->isConnected();
Christopher Haster 38:157fb2ab965f 85 }
Christopher Haster 38:157fb2ab965f 86
Christopher Haster 34:c17745683385 87 int32_t Socket::open(const char *url, uint16_t port)
Christopher Haster 32:2c5fc105fc50 88 {
Christopher Haster 34:c17745683385 89 if (_socket) {
Christopher Haster 34:c17745683385 90 int32_t err = close();
Christopher Haster 34:c17745683385 91 if (err) return err;
Christopher Haster 34:c17745683385 92 }
Christopher Haster 34:c17745683385 93
Christopher Haster 34:c17745683385 94 if (url) {
Christopher Haster 34:c17745683385 95 int32_t err = setURL(url);
Christopher Haster 34:c17745683385 96 if (err) return err;
Christopher Haster 34:c17745683385 97 }
Christopher Haster 34:c17745683385 98
Christopher Haster 34:c17745683385 99 if (port) {
Christopher Haster 34:c17745683385 100 setPort(port);
Christopher Haster 34:c17745683385 101 }
Christopher Haster 34:c17745683385 102
Christopher Haster 39:47138420ea42 103 if (!getIPAddress() || !getPort()) {
Christopher Haster 35:838393fbc2ca 104 return -3;
Christopher Haster 35:838393fbc2ca 105 }
Christopher Haster 32:2c5fc105fc50 106
Christopher Haster 32:2c5fc105fc50 107 _socket = _iface->createSocket(_proto);
Christopher Haster 32:2c5fc105fc50 108 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 109
Christopher Haster 36:eab792dfb0d8 110 int32_t err = _socket->open(_ip_address, _port);
Christopher Haster 34:c17745683385 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
Christopher Haster 32:2c5fc105fc50 119 int32_t Socket::close()
Christopher Haster 32:2c5fc105fc50 120 {
Christopher Haster 32:2c5fc105fc50 121 if (!_socket) return 0;
Christopher Haster 32:2c5fc105fc50 122
Christopher Haster 32:2c5fc105fc50 123 int32_t err = _socket->close();
Christopher Haster 32:2c5fc105fc50 124
Christopher Haster 32:2c5fc105fc50 125 if (!err) {
Christopher Haster 32:2c5fc105fc50 126 _iface->destroySocket(_socket);
Christopher Haster 32:2c5fc105fc50 127 }
Christopher Haster 32:2c5fc105fc50 128
Christopher Haster 32:2c5fc105fc50 129 return err;
Christopher Haster 32:2c5fc105fc50 130 }
Christopher Haster 32:2c5fc105fc50 131
geky 30:3cc78f5db99d 132 int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 133 {
Christopher Haster 32:2c5fc105fc50 134 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 135 return _socket->send(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 136 }
Christopher Haster 25:ed7b2a52e8ac 137
geky 30:3cc78f5db99d 138 int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 139 {
Christopher Haster 32:2c5fc105fc50 140 if (!_socket) return -2;
Christopher Haster 32:2c5fc105fc50 141 return _socket->recv(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 142 }
Christopher Haster 25:ed7b2a52e8ac 143