NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Wed Feb 24 23:05:54 2016 -0600
Branch:
api-changes
Revision:
45:c8aca7c1e93f
Parent:
43:09ea32f2eb54
Child:
46:ac37605ca91d
Added inexaustive list of standardized error codes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:d35446f60233 1 /* NetworkInterface Base Class
sam_grove 0:d35446f60233 2 * Copyright (c) 2015 ARM Limited
sam_grove 0:d35446f60233 3 *
sam_grove 0:d35446f60233 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:d35446f60233 5 * you may not use this file except in compliance with the License.
sam_grove 0:d35446f60233 6 * You may obtain a copy of the License at
sam_grove 0:d35446f60233 7 *
sam_grove 0:d35446f60233 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:d35446f60233 9 *
sam_grove 0:d35446f60233 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:d35446f60233 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:d35446f60233 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:d35446f60233 13 * See the License for the specific language governing permissions and
sam_grove 0:d35446f60233 14 * limitations under the License.
sam_grove 0:d35446f60233 15 */
sam_grove 3:167dd63981b6 16
Christopher Haster 21:35ed15069189 17 #ifndef NETWORK_INTERFACE_H
Christopher Haster 21:35ed15069189 18 #define NETWORK_INTERFACE_H
bridadan 1:291a9d61e58a 19
Christopher Haster 42:49893d13c432 20 #include "SocketInterface.h"
sam_grove 7:b147c08301be 21 #include "stdint.h"
Christopher Haster 42:49893d13c432 22
Christopher Haster 21:35ed15069189 23
Christopher Haster 42:49893d13c432 24 /** Maximum storage needed for IP address and MAC addresses
Christopher Haster 42:49893d13c432 25 */
Christopher Haster 42:49893d13c432 26 #define NS_IP_SIZE 16
Christopher Haster 42:49893d13c432 27 #define NS_MAC_SIZE 18
Christopher Haster 28:163fbe3263f4 28
Christopher Haster 45:c8aca7c1e93f 29 /** Inexhaustive enum of standardized error codes
Christopher Haster 45:c8aca7c1e93f 30 */
Christopher Haster 45:c8aca7c1e93f 31 enum ns_error_t {
Christopher Haster 45:c8aca7c1e93f 32 NS_ERROR_TIMEOUT = -3001,
Christopher Haster 45:c8aca7c1e93f 33 NS_ERROR_NO_CONNECTION = -3002,
Christopher Haster 45:c8aca7c1e93f 34 NS_ERROR_NO_SOCKET = -3003,
Christopher Haster 45:c8aca7c1e93f 35 NS_ERROR_NO_ADDRESS = -3004,
Christopher Haster 45:c8aca7c1e93f 36 NS_ERROR_NO_MEMORY = -3005,
Christopher Haster 45:c8aca7c1e93f 37 NS_ERROR_DNS_FAILURE = -3006,
Christopher Haster 45:c8aca7c1e93f 38 NS_ERROR_DHCP_FAILURE = -3007,
Christopher Haster 45:c8aca7c1e93f 39 NS_ERROR_CRED_FAILURE = -3008
Christopher Haster 45:c8aca7c1e93f 40 };
Christopher Haster 45:c8aca7c1e93f 41
bridadan 11:47c32687a44c 42
Christopher Haster 21:35ed15069189 43 /** NetworkInterface class
Christopher Haster 21:35ed15069189 44 * Common interface that is shared between all hardware that
Christopher Haster 21:35ed15069189 45 * can connect to a network over IP.
bridadan 1:291a9d61e58a 46 */
sam_grove 3:167dd63981b6 47 class NetworkInterface
sam_grove 3:167dd63981b6 48 {
bridadan 1:291a9d61e58a 49 public:
Christopher Haster 21:35ed15069189 50 /** Enables DHCP and clears any static address
Christopher Haster 40:11d4a94df3f7 51 * DHCP resolution does not occur until connect call
Christopher Haster 21:35ed15069189 52 * DHCP is enabled by default
Christopher Haster 21:35ed15069189 53 */
Christopher Haster 40:11d4a94df3f7 54 virtual void useDHCP();
Christopher Haster 21:35ed15069189 55
Christopher Haster 21:35ed15069189 56 /** Set the static IP address of the network interface
Christopher Haster 21:35ed15069189 57 * @param ip Static IP address, copied internally
bridadan 1:291a9d61e58a 58 */
Christopher Haster 21:35ed15069189 59 virtual void setIPAddress(const char *ip);
Christopher Haster 21:35ed15069189 60
Christopher Haster 21:35ed15069189 61 /** Set the static network mask of the network interface
Christopher Haster 21:35ed15069189 62 * @param mask Static network mask, copied internally
Christopher Haster 21:35ed15069189 63 */
Christopher Haster 21:35ed15069189 64 virtual void setNetworkMask(const char *mask);
Christopher Haster 21:35ed15069189 65
Christopher Haster 21:35ed15069189 66 /** Set the static gateway of the network interface
Christopher Haster 21:35ed15069189 67 * @param gateway Gateway address, copied internally
Christopher Haster 21:35ed15069189 68 */
Christopher Haster 21:35ed15069189 69 virtual void setGateway(const char *gateway);
Christopher Haster 21:35ed15069189 70
Christopher Haster 21:35ed15069189 71 /** Get the IP address
Christopher Haster 21:35ed15069189 72 * @return IP address of the interface
Christopher Haster 21:35ed15069189 73 */
Christopher Haster 21:35ed15069189 74 virtual const char *getIPAddress();
sam_grove 3:167dd63981b6 75
Christopher Haster 21:35ed15069189 76 /** Get the network mask
Christopher Haster 21:35ed15069189 77 * @return Network mask of the interface
sam_grove 3:167dd63981b6 78 */
Christopher Haster 21:35ed15069189 79 virtual const char *getNetworkMask();
Christopher Haster 21:35ed15069189 80
Christopher Haster 21:35ed15069189 81 /** Get the gateway
Christopher Haster 21:35ed15069189 82 * @return Gateway address of the interface
Christopher Haster 21:35ed15069189 83 */
Christopher Haster 21:35ed15069189 84 virtual const char *getGateway();
Christopher Haster 21:35ed15069189 85
Christopher Haster 21:35ed15069189 86 /** Get the current MAC address
Christopher Haster 21:35ed15069189 87 * @return String MAC address of the interface
Christopher Haster 21:35ed15069189 88 */
Christopher Haster 21:35ed15069189 89 virtual const char *getMACAddress() = 0;
sam_grove 3:167dd63981b6 90
Christopher Haster 21:35ed15069189 91 /** Get the current status of the interface
geky 31:7f15b95f2a1d 92 * @return true if connected
bridadan 1:291a9d61e58a 93 */
Christopher Haster 42:49893d13c432 94 virtual bool isConnected();
Christopher Haster 41:3ec1c97e9bbf 95
geky 31:7f15b95f2a1d 96 /** Looks up the specified host's IP address
geky 31:7f15b95f2a1d 97 * @param name URL of host
geky 31:7f15b95f2a1d 98 * @param ip Buffer to hold IP address, must be at least SOCK_IP_SIZE
geky 31:7f15b95f2a1d 99 * @return 0 on success
geky 31:7f15b95f2a1d 100 */
geky 31:7f15b95f2a1d 101 int32_t getHostByName(const char *name, char *ip);
Christopher Haster 21:35ed15069189 102
Christopher Haster 26:9774a2edad71 103 protected:
Christopher Haster 26:9774a2edad71 104 NetworkInterface();
Christopher Haster 21:35ed15069189 105
Christopher Haster 25:ed7b2a52e8ac 106 friend class Socket;
Christopher Haster 21:35ed15069189 107
Christopher Haster 21:35ed15069189 108 /** Internally create a socket
Christopher Haster 21:35ed15069189 109 * @param proto The type of socket to open, SOCK_TCP or SOCK_UDP
Christopher Haster 21:35ed15069189 110 * @return The allocated socket
Christopher Haster 21:35ed15069189 111 */
Christopher Haster 21:35ed15069189 112 virtual SocketInterface *createSocket(socket_protocol_t proto) = 0;
geky 30:3cc78f5db99d 113
Christopher Haster 21:35ed15069189 114 /** Internally destroy a socket
Christopher Haster 21:35ed15069189 115 * @param socket An allocated SocketInterface
Christopher Haster 21:35ed15069189 116 * @returns 0 on success
bridadan 11:47c32687a44c 117 */
Christopher Haster 21:35ed15069189 118 virtual void destroySocket(SocketInterface *socket) = 0;
Christopher Haster 21:35ed15069189 119
Christopher Haster 21:35ed15069189 120 private:
Christopher Haster 42:49893d13c432 121 char _ip_address[NS_IP_SIZE];
Christopher Haster 42:49893d13c432 122 char _network_mask[NS_IP_SIZE];
Christopher Haster 42:49893d13c432 123 char _gateway[NS_IP_SIZE];
bridadan 1:291a9d61e58a 124 };
bridadan 1:291a9d61e58a 125
bridadan 1:291a9d61e58a 126 #endif