Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /* SocketAddress
bryantaylor 0:eafc3fd41f75 2 * Copyright (c) 2015 ARM Limited
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 5 * you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 6 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 9 *
bryantaylor 0:eafc3fd41f75 10 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 11 * distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 13 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 14 * limitations under the License.
bryantaylor 0:eafc3fd41f75 15 */
bryantaylor 0:eafc3fd41f75 16
bryantaylor 0:eafc3fd41f75 17 #ifndef SOCKET_ADDRESS_H
bryantaylor 0:eafc3fd41f75 18 #define SOCKET_ADDRESS_H
bryantaylor 0:eafc3fd41f75 19
bryantaylor 0:eafc3fd41f75 20 #include "nsapi_types.h"
bryantaylor 0:eafc3fd41f75 21 #include "toolchain.h"
bryantaylor 0:eafc3fd41f75 22
bryantaylor 0:eafc3fd41f75 23 // Predeclared classes
bryantaylor 0:eafc3fd41f75 24 class NetworkStack;
bryantaylor 0:eafc3fd41f75 25 class NetworkInterface;
bryantaylor 0:eafc3fd41f75 26
bryantaylor 0:eafc3fd41f75 27
bryantaylor 0:eafc3fd41f75 28 /** SocketAddress class
bryantaylor 0:eafc3fd41f75 29 *
bryantaylor 0:eafc3fd41f75 30 * Representation of an IP address and port pair.
bryantaylor 0:eafc3fd41f75 31 */
bryantaylor 0:eafc3fd41f75 32 class SocketAddress {
bryantaylor 0:eafc3fd41f75 33 public:
bryantaylor 0:eafc3fd41f75 34 /** Create a SocketAddress from a hostname and port
bryantaylor 0:eafc3fd41f75 35 *
bryantaylor 0:eafc3fd41f75 36 * The hostname may be either a domain name or an IP address. If the
bryantaylor 0:eafc3fd41f75 37 * hostname is an IP address, no network transactions will be performed.
bryantaylor 0:eafc3fd41f75 38 *
bryantaylor 0:eafc3fd41f75 39 * On failure, the IP address and port will be set to zero
bryantaylor 0:eafc3fd41f75 40 *
bryantaylor 0:eafc3fd41f75 41 * @param stack Network stack to use for DNS resolution
bryantaylor 0:eafc3fd41f75 42 * @param host Hostname to resolve
bryantaylor 0:eafc3fd41f75 43 * @param port Optional 16-bit port
bryantaylor 0:eafc3fd41f75 44 */
bryantaylor 0:eafc3fd41f75 45 template <typename S>
bryantaylor 0:eafc3fd41f75 46 SocketAddress(S *stack, const char *host, uint16_t port = 0)
bryantaylor 0:eafc3fd41f75 47 {
bryantaylor 0:eafc3fd41f75 48 _SocketAddress(nsapi_create_stack(stack), host, port);
bryantaylor 0:eafc3fd41f75 49 }
bryantaylor 0:eafc3fd41f75 50
bryantaylor 0:eafc3fd41f75 51 /** Create a SocketAddress from a raw IP address and port
bryantaylor 0:eafc3fd41f75 52 *
bryantaylor 0:eafc3fd41f75 53 * @param addr Raw IP address
bryantaylor 0:eafc3fd41f75 54 * @param port Optional 16-bit port
bryantaylor 0:eafc3fd41f75 55 */
bryantaylor 0:eafc3fd41f75 56 SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
bryantaylor 0:eafc3fd41f75 57
bryantaylor 0:eafc3fd41f75 58 /** Create a SocketAddress from an IP address and port
bryantaylor 0:eafc3fd41f75 59 *
bryantaylor 0:eafc3fd41f75 60 * @param host Null-terminated representation of the IP address
bryantaylor 0:eafc3fd41f75 61 * @param port Optional 16-bit port
bryantaylor 0:eafc3fd41f75 62 */
bryantaylor 0:eafc3fd41f75 63 SocketAddress(const char *addr, uint16_t port = 0);
bryantaylor 0:eafc3fd41f75 64
bryantaylor 0:eafc3fd41f75 65 /** Create a SocketAddress from raw IP bytes, IP version, and port
bryantaylor 0:eafc3fd41f75 66 *
bryantaylor 0:eafc3fd41f75 67 * @param bytes Raw IP address in big-endian order
bryantaylor 0:eafc3fd41f75 68 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
bryantaylor 0:eafc3fd41f75 69 * @param port Optional 16-bit port
bryantaylor 0:eafc3fd41f75 70 */
bryantaylor 0:eafc3fd41f75 71 SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
bryantaylor 0:eafc3fd41f75 72
bryantaylor 0:eafc3fd41f75 73 /** Create a SocketAddress from another SocketAddress
bryantaylor 0:eafc3fd41f75 74 *
bryantaylor 0:eafc3fd41f75 75 * @param address SocketAddress to copy
bryantaylor 0:eafc3fd41f75 76 */
bryantaylor 0:eafc3fd41f75 77 SocketAddress(const SocketAddress &addr);
bryantaylor 0:eafc3fd41f75 78
bryantaylor 0:eafc3fd41f75 79 /** Set the IP address
bryantaylor 0:eafc3fd41f75 80 *
bryantaylor 0:eafc3fd41f75 81 * @param addr Null-terminated represention of the IP address
bryantaylor 0:eafc3fd41f75 82 */
bryantaylor 0:eafc3fd41f75 83 void set_ip_address(const char *addr);
bryantaylor 0:eafc3fd41f75 84
bryantaylor 0:eafc3fd41f75 85 /** Set the raw IP bytes and IP version
bryantaylor 0:eafc3fd41f75 86 *
bryantaylor 0:eafc3fd41f75 87 * @param bytes Raw IP address in big-endian order
bryantaylor 0:eafc3fd41f75 88 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
bryantaylor 0:eafc3fd41f75 89 */
bryantaylor 0:eafc3fd41f75 90 void set_ip_bytes(const void *bytes, nsapi_version_t version);
bryantaylor 0:eafc3fd41f75 91
bryantaylor 0:eafc3fd41f75 92 /** Set the raw IP address
bryantaylor 0:eafc3fd41f75 93 *
bryantaylor 0:eafc3fd41f75 94 * @param addr Raw IP address
bryantaylor 0:eafc3fd41f75 95 */
bryantaylor 0:eafc3fd41f75 96 void set_addr(nsapi_addr_t addr);
bryantaylor 0:eafc3fd41f75 97
bryantaylor 0:eafc3fd41f75 98 /** Set the port
bryantaylor 0:eafc3fd41f75 99 *
bryantaylor 0:eafc3fd41f75 100 * @param port 16-bit port
bryantaylor 0:eafc3fd41f75 101 */
bryantaylor 0:eafc3fd41f75 102 void set_port(uint16_t port);
bryantaylor 0:eafc3fd41f75 103
bryantaylor 0:eafc3fd41f75 104 /** Get the IP address
bryantaylor 0:eafc3fd41f75 105 *
bryantaylor 0:eafc3fd41f75 106 * @return Null-terminated representation of the IP Address
bryantaylor 0:eafc3fd41f75 107 */
bryantaylor 0:eafc3fd41f75 108 const char *get_ip_address() const;
bryantaylor 0:eafc3fd41f75 109
bryantaylor 0:eafc3fd41f75 110 /* Get the raw IP bytes
bryantaylor 0:eafc3fd41f75 111 *
bryantaylor 0:eafc3fd41f75 112 * @return Raw IP address in big-endian order
bryantaylor 0:eafc3fd41f75 113 */
bryantaylor 0:eafc3fd41f75 114 const void *get_ip_bytes() const;
bryantaylor 0:eafc3fd41f75 115
bryantaylor 0:eafc3fd41f75 116 /** Get the IP address version
bryantaylor 0:eafc3fd41f75 117 *
bryantaylor 0:eafc3fd41f75 118 * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
bryantaylor 0:eafc3fd41f75 119 */
bryantaylor 0:eafc3fd41f75 120 nsapi_version_t get_ip_version() const;
bryantaylor 0:eafc3fd41f75 121
bryantaylor 0:eafc3fd41f75 122 /** Get the raw IP address
bryantaylor 0:eafc3fd41f75 123 *
bryantaylor 0:eafc3fd41f75 124 * @return Raw IP address
bryantaylor 0:eafc3fd41f75 125 */
bryantaylor 0:eafc3fd41f75 126 nsapi_addr_t get_addr() const;
bryantaylor 0:eafc3fd41f75 127
bryantaylor 0:eafc3fd41f75 128 /** Get the port
bryantaylor 0:eafc3fd41f75 129 *
bryantaylor 0:eafc3fd41f75 130 * @return The 16-bit port
bryantaylor 0:eafc3fd41f75 131 */
bryantaylor 0:eafc3fd41f75 132 uint16_t get_port() const;
bryantaylor 0:eafc3fd41f75 133
bryantaylor 0:eafc3fd41f75 134 /** Test if address is zero
bryantaylor 0:eafc3fd41f75 135 *
bryantaylor 0:eafc3fd41f75 136 * @return True if address is not zero
bryantaylor 0:eafc3fd41f75 137 */
bryantaylor 0:eafc3fd41f75 138 operator bool() const;
bryantaylor 0:eafc3fd41f75 139
bryantaylor 0:eafc3fd41f75 140 private:
bryantaylor 0:eafc3fd41f75 141 void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
bryantaylor 0:eafc3fd41f75 142
bryantaylor 0:eafc3fd41f75 143 char _ip_address[NSAPI_IP_SIZE];
bryantaylor 0:eafc3fd41f75 144 nsapi_addr_t _addr;
bryantaylor 0:eafc3fd41f75 145 uint16_t _port;
bryantaylor 0:eafc3fd41f75 146 };
bryantaylor 0:eafc3fd41f75 147
bryantaylor 0:eafc3fd41f75 148
bryantaylor 0:eafc3fd41f75 149 #endif