NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Mon Feb 22 19:07:36 2016 -0600
Branch:
api-changes
Revision:
32:2c5fc105fc50
Parent:
28:163fbe3263f4
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 #ifndef SOCKET_H
Christopher Haster 25:ed7b2a52e8ac 18 #define SOCKET_H
Christopher Haster 25:ed7b2a52e8ac 19
Christopher Haster 25:ed7b2a52e8ac 20 #include "NetworkInterface.h"
Christopher Haster 25:ed7b2a52e8ac 21
Christopher Haster 25:ed7b2a52e8ac 22 /** Abstract socket class
Christopher Haster 25:ed7b2a52e8ac 23 * API for handling general sockets. Supports IP address operations
Christopher Haster 25:ed7b2a52e8ac 24 * and sending/recieving data.
Christopher Haster 25:ed7b2a52e8ac 25 */
Christopher Haster 25:ed7b2a52e8ac 26 class Socket
Christopher Haster 25:ed7b2a52e8ac 27 {
Christopher Haster 25:ed7b2a52e8ac 28 public:
Christopher Haster 25:ed7b2a52e8ac 29 /** Set the URL of the socket
Christopher Haster 25:ed7b2a52e8ac 30 * Performs DNS lookup if necessary
Christopher Haster 25:ed7b2a52e8ac 31 * @param url URL to connect to
Christopher Haster 28:163fbe3263f4 32 * @param port Optional port to connect to
Christopher Haster 27:d7ed39727306 33 * @return 0 on success
Christopher Haster 25:ed7b2a52e8ac 34 */
Christopher Haster 28:163fbe3263f4 35 int32_t setURL(const char *url, uint16_t port = 0);
Christopher Haster 25:ed7b2a52e8ac 36
Christopher Haster 25:ed7b2a52e8ac 37 /** Set the IP address of the socket
Christopher Haster 25:ed7b2a52e8ac 38 * @param ip IP address to connect to, copied internally
Christopher Haster 28:163fbe3263f4 39 * @param port Optional port to connect to
Christopher Haster 25:ed7b2a52e8ac 40 */
Christopher Haster 28:163fbe3263f4 41 void setIPAddress(const char *ip, uint16_t port = 0);
Christopher Haster 25:ed7b2a52e8ac 42
Christopher Haster 25:ed7b2a52e8ac 43 /** Set the port of the socket
Christopher Haster 25:ed7b2a52e8ac 44 * @param port Port to connect to
Christopher Haster 25:ed7b2a52e8ac 45 */
Christopher Haster 25:ed7b2a52e8ac 46 void setPort(uint16_t port);
Christopher Haster 25:ed7b2a52e8ac 47
Christopher Haster 25:ed7b2a52e8ac 48 /** Gets the IP address
Christopher Haster 25:ed7b2a52e8ac 49 * @return IP address to connect to
Christopher Haster 25:ed7b2a52e8ac 50 */
Christopher Haster 28:163fbe3263f4 51 const char *getIPAddress() const;
Christopher Haster 25:ed7b2a52e8ac 52
Christopher Haster 25:ed7b2a52e8ac 53 /** Gets the port
Christopher Haster 25:ed7b2a52e8ac 54 * @return Port to connect to
Christopher Haster 25:ed7b2a52e8ac 55 */
Christopher Haster 28:163fbe3263f4 56 uint16_t getPort() const;
Christopher Haster 25:ed7b2a52e8ac 57
Christopher Haster 25:ed7b2a52e8ac 58
Christopher Haster 32:2c5fc105fc50 59 /** Open a connection to the underlying address
Christopher Haster 32:2c5fc105fc50 60 * @return 0 on success
Christopher Haster 32:2c5fc105fc50 61 */
Christopher Haster 32:2c5fc105fc50 62 int32_t open();
Christopher Haster 32:2c5fc105fc50 63
Christopher Haster 32:2c5fc105fc50 64 /** Close an open connection
Christopher Haster 32:2c5fc105fc50 65 * @return 0 on success
Christopher Haster 32:2c5fc105fc50 66 */
Christopher Haster 32:2c5fc105fc50 67 int32_t close();
Christopher Haster 32:2c5fc105fc50 68
Christopher Haster 25:ed7b2a52e8ac 69 /** Send data over the socket
Christopher Haster 25:ed7b2a52e8ac 70 * @param data Buffer of data to send
Christopher Haster 25:ed7b2a52e8ac 71 * @param len Size of data to send
Christopher Haster 25:ed7b2a52e8ac 72 * @param timeout_ms Maximum amount of time to wait
Christopher Haster 25:ed7b2a52e8ac 73 * @return 0 on success
Christopher Haster 25:ed7b2a52e8ac 74 */
Christopher Haster 25:ed7b2a52e8ac 75 int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000);
Christopher Haster 25:ed7b2a52e8ac 76
Christopher Haster 25:ed7b2a52e8ac 77 /** Recieve data over the socket
Christopher Haster 25:ed7b2a52e8ac 78 * @param data Buffer to store recieved data
Christopher Haster 25:ed7b2a52e8ac 79 * @param len Size of provided buffer
Christopher Haster 25:ed7b2a52e8ac 80 * @param timeout_ms Maximum amount of time to wait
Christopher Haster 25:ed7b2a52e8ac 81 * @return Number of bytes sent or a negative value on failure
Christopher Haster 25:ed7b2a52e8ac 82 */
Christopher Haster 25:ed7b2a52e8ac 83 int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000);
Christopher Haster 25:ed7b2a52e8ac 84
Christopher Haster 25:ed7b2a52e8ac 85
Christopher Haster 25:ed7b2a52e8ac 86 protected:
Christopher Haster 28:163fbe3263f4 87 Socket(NetworkInterface *iface, socket_protocol_t proto);
Christopher Haster 25:ed7b2a52e8ac 88 ~Socket();
Christopher Haster 25:ed7b2a52e8ac 89
Christopher Haster 25:ed7b2a52e8ac 90 private:
Christopher Haster 25:ed7b2a52e8ac 91 NetworkInterface *_iface;
Christopher Haster 25:ed7b2a52e8ac 92 socket_protocol_t _proto;
Christopher Haster 25:ed7b2a52e8ac 93 SocketInterface *_socket;
Christopher Haster 27:d7ed39727306 94
Christopher Haster 28:163fbe3263f4 95 char _ip_address[SOCK_IP_SIZE];
Christopher Haster 28:163fbe3263f4 96 uint16_t _port;
Christopher Haster 25:ed7b2a52e8ac 97 };
Christopher Haster 25:ed7b2a52e8ac 98
Christopher Haster 25:ed7b2a52e8ac 99 #endif