pefect / NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   WizFi310_TCP_Echo_Server_Example

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
geky
Date:
Mon Feb 22 21:03:38 2016 +0000
Branch:
api-changes
Revision:
30:3cc78f5db99d
Parent:
29:7bcec3189a93
Child:
31:7f15b95f2a1d
Standardized formatting

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 27:d7ed39727306 25
Christopher Haster 28:163fbe3263f4 26 memset(_ip_address, 0, SOCK_IP_SIZE);
Christopher Haster 28:163fbe3263f4 27 _port = 0;
Christopher Haster 25:ed7b2a52e8ac 28 }
Christopher Haster 25:ed7b2a52e8ac 29
geky 30:3cc78f5db99d 30 Socket::~Socket()
geky 30:3cc78f5db99d 31 {
Christopher Haster 28:163fbe3263f4 32 if (_socket) {
Christopher Haster 28:163fbe3263f4 33 _iface->destroySocket(_socket);
Christopher Haster 28:163fbe3263f4 34 }
Christopher Haster 25:ed7b2a52e8ac 35 }
Christopher Haster 25:ed7b2a52e8ac 36
geky 30:3cc78f5db99d 37 SocketInterface *Socket::_get_socket()
geky 30:3cc78f5db99d 38 {
Christopher Haster 27:d7ed39727306 39 if (!_socket) {
Christopher Haster 27:d7ed39727306 40 _socket = _iface->createSocket(_proto);
Christopher Haster 27:d7ed39727306 41
Christopher Haster 28:163fbe3263f4 42 if (_ip_address[0]) {
Christopher Haster 28:163fbe3263f4 43 _socket->setIPAddress(_ip_address);
Christopher Haster 27:d7ed39727306 44 }
Christopher Haster 27:d7ed39727306 45
Christopher Haster 28:163fbe3263f4 46 if (_port) {
Christopher Haster 28:163fbe3263f4 47 _socket->setPort(_port);
Christopher Haster 27:d7ed39727306 48 }
Christopher Haster 27:d7ed39727306 49 }
Christopher Haster 27:d7ed39727306 50
Christopher Haster 25:ed7b2a52e8ac 51 return _socket;
Christopher Haster 25:ed7b2a52e8ac 52 }
Christopher Haster 25:ed7b2a52e8ac 53
geky 30:3cc78f5db99d 54 int32_t Socket::setURL(const char *url, uint16_t port)
geky 30:3cc78f5db99d 55 {
Christopher Haster 25:ed7b2a52e8ac 56 SocketInterface *s = _get_socket();
Christopher Haster 27:d7ed39727306 57 if (!s) return -2;
Christopher Haster 28:163fbe3263f4 58
Christopher Haster 28:163fbe3263f4 59 int32_t error = s->setURL(url);
Christopher Haster 28:163fbe3263f4 60 if (error < 0) return error;
Christopher Haster 28:163fbe3263f4 61
Christopher Haster 28:163fbe3263f4 62 if (port) {
Christopher Haster 28:163fbe3263f4 63 setPort(port);
Christopher Haster 28:163fbe3263f4 64 }
Christopher Haster 28:163fbe3263f4 65
Christopher Haster 28:163fbe3263f4 66 return 0;
Christopher Haster 25:ed7b2a52e8ac 67 }
Christopher Haster 25:ed7b2a52e8ac 68
geky 30:3cc78f5db99d 69 void Socket::setIPAddress(const char *ip, uint16_t port)
geky 30:3cc78f5db99d 70 {
Christopher Haster 28:163fbe3263f4 71 strcpy(_ip_address, ip);
Christopher Haster 28:163fbe3263f4 72
Christopher Haster 28:163fbe3263f4 73 if (_socket) {
Christopher Haster 27:d7ed39727306 74 _socket->setIPAddress(ip);
Christopher Haster 27:d7ed39727306 75 }
Christopher Haster 28:163fbe3263f4 76
Christopher Haster 28:163fbe3263f4 77 if (port) {
Christopher Haster 28:163fbe3263f4 78 setPort(port);
Christopher Haster 28:163fbe3263f4 79 }
Christopher Haster 25:ed7b2a52e8ac 80 }
Christopher Haster 25:ed7b2a52e8ac 81
geky 30:3cc78f5db99d 82 void Socket::setPort(uint16_t port)
geky 30:3cc78f5db99d 83 {
Christopher Haster 28:163fbe3263f4 84 _port = port;
Christopher Haster 28:163fbe3263f4 85
Christopher Haster 28:163fbe3263f4 86 if (_socket) {
Christopher Haster 27:d7ed39727306 87 _socket->setPort(port);
Christopher Haster 27:d7ed39727306 88 }
Christopher Haster 25:ed7b2a52e8ac 89 }
Christopher Haster 25:ed7b2a52e8ac 90
geky 30:3cc78f5db99d 91 const char *Socket::getIPAddress() const
geky 30:3cc78f5db99d 92 {
Christopher Haster 28:163fbe3263f4 93 return _ip_address;
Christopher Haster 25:ed7b2a52e8ac 94 }
Christopher Haster 25:ed7b2a52e8ac 95
geky 30:3cc78f5db99d 96 uint16_t Socket::getPort() const
geky 30:3cc78f5db99d 97 {
Christopher Haster 28:163fbe3263f4 98 return _port;
Christopher Haster 25:ed7b2a52e8ac 99 }
Christopher Haster 25:ed7b2a52e8ac 100
geky 30:3cc78f5db99d 101 int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 102 {
Christopher Haster 25:ed7b2a52e8ac 103 SocketInterface *s = _get_socket();
geky 30:3cc78f5db99d 104 if (!s) return -2;
geky 30:3cc78f5db99d 105 return s->send(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 106 }
Christopher Haster 25:ed7b2a52e8ac 107
geky 30:3cc78f5db99d 108 int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 109 {
Christopher Haster 25:ed7b2a52e8ac 110 SocketInterface *s = _get_socket();
geky 30:3cc78f5db99d 111 if (!s) return -2;
geky 30:3cc78f5db99d 112 return s->recv(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 113 }
Christopher Haster 25:ed7b2a52e8ac 114