NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
NetworkInterface.h@11:47c32687a44c, 2015-07-15 (annotated)
- Committer:
- bridadan
- Date:
- Wed Jul 15 23:21:17 2015 +0000
- Revision:
- 11:47c32687a44c
- Parent:
- 10:50b0a3f840df
- Child:
- 12:ab3679eb4d9d
Moved underscored_functions to camelCase, added allocate/deallocate socket functions, added data structures to keep track of multiple sockets.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
bridadan | 1:291a9d61e58a | 17 | #ifndef NETWORKINTERFACE_H |
bridadan | 1:291a9d61e58a | 18 | #define NETWORKINTERFACE_H |
bridadan | 1:291a9d61e58a | 19 | |
sam_grove | 7:b147c08301be | 20 | #include "stdint.h" |
sam_grove | 7:b147c08301be | 21 | |
bridadan | 11:47c32687a44c | 22 | #include "SocketInterface.h" |
bridadan | 11:47c32687a44c | 23 | #include <map> |
bridadan | 11:47c32687a44c | 24 | |
bridadan | 1:291a9d61e58a | 25 | /** NetworkInterface class. |
sam_grove | 7:b147c08301be | 26 | This is a common interface that is shared between all hardware that connect |
sam_grove | 7:b147c08301be | 27 | to a network over IP. |
bridadan | 1:291a9d61e58a | 28 | */ |
sam_grove | 3:167dd63981b6 | 29 | class NetworkInterface |
sam_grove | 3:167dd63981b6 | 30 | { |
bridadan | 1:291a9d61e58a | 31 | public: |
austin.blackstone@arm.com | 4:f09f0932db4a | 32 | |
sam_grove | 7:b147c08301be | 33 | /** Initialize the network interface with DHCP. |
sam_grove | 7:b147c08301be | 34 | @returns 0 on success, a negative number on failure |
bridadan | 1:291a9d61e58a | 35 | */ |
sam_grove | 7:b147c08301be | 36 | virtual int32_t init(void) const = 0; |
sam_grove | 3:167dd63981b6 | 37 | |
sam_grove | 7:b147c08301be | 38 | /** Initialize the network interface with a static IP address. |
sam_grove | 7:b147c08301be | 39 | @param ip The static IP address to use |
sam_grove | 7:b147c08301be | 40 | @param mask The IP address mask |
sam_grove | 7:b147c08301be | 41 | @param gateway The gateway to use |
sam_grove | 7:b147c08301be | 42 | @returns 0 on success, a negative number on failure |
bridadan | 1:291a9d61e58a | 43 | */ |
sam_grove | 7:b147c08301be | 44 | virtual int32_t init(const char *ip, const char *mask, const char *gateway) const = 0; |
sam_grove | 7:b147c08301be | 45 | |
sam_grove | 3:167dd63981b6 | 46 | |
sam_grove | 7:b147c08301be | 47 | /** Start the interface, using DHCP if needed. |
sam_grove | 7:b147c08301be | 48 | @param timeout_ms Time in miliseconds to wait while attempting to connect before timing out |
sam_grove | 7:b147c08301be | 49 | @returns 0 on success, a negative number on failure |
sam_grove | 3:167dd63981b6 | 50 | */ |
sam_grove | 10:50b0a3f840df | 51 | virtual int32_t connect(uint32_t timeout_ms = 15000) const = 0; |
sam_grove | 3:167dd63981b6 | 52 | |
sam_grove | 7:b147c08301be | 53 | /** Stop the interface, bringing down dhcp if necessary. |
sam_grove | 7:b147c08301be | 54 | @returns 0 on success, a negative number on failure |
bridadan | 1:291a9d61e58a | 55 | */ |
sam_grove | 7:b147c08301be | 56 | virtual int32_t disconnect(void) const = 0; |
sam_grove | 3:167dd63981b6 | 57 | |
sam_grove | 7:b147c08301be | 58 | /** Get the current IP address. |
sam_grove | 7:b147c08301be | 59 | @returns a pointer to a string containing the IP address. |
bridadan | 1:291a9d61e58a | 60 | */ |
sam_grove | 3:167dd63981b6 | 61 | virtual char *getIPAddress(void) const = 0; |
sam_grove | 3:167dd63981b6 | 62 | |
sam_grove | 7:b147c08301be | 63 | /** Get the current gateway address. |
sam_grove | 7:b147c08301be | 64 | @returns a pointer to a string containing the gateway address. |
bridadan | 1:291a9d61e58a | 65 | */ |
sam_grove | 3:167dd63981b6 | 66 | virtual char *getGateway(void) const = 0; |
sam_grove | 3:167dd63981b6 | 67 | |
sam_grove | 3:167dd63981b6 | 68 | |
sam_grove | 7:b147c08301be | 69 | /** Get the current network mask. |
sam_grove | 7:b147c08301be | 70 | @returns a pointer to a string containing the network mask. |
bridadan | 1:291a9d61e58a | 71 | */ |
sam_grove | 3:167dd63981b6 | 72 | virtual char *getNetworkMask(void) const = 0; |
sam_grove | 3:167dd63981b6 | 73 | |
sam_grove | 7:b147c08301be | 74 | /** Get the devices MAC address. |
sam_grove | 7:b147c08301be | 75 | @returns a pointer to a string containing the mac address. |
sam_grove | 3:167dd63981b6 | 76 | */ |
sam_grove | 3:167dd63981b6 | 77 | virtual char *getMACAddress(void) const = 0; |
sam_grove | 3:167dd63981b6 | 78 | |
sam_grove | 7:b147c08301be | 79 | /** Get the current status of the interface connection. |
sam_grove | 7:b147c08301be | 80 | @returns true if connected, a negative number on failure |
bridadan | 1:291a9d61e58a | 81 | */ |
sam_grove | 7:b147c08301be | 82 | virtual int32_t isConnected(void) const = 0; |
bridadan | 11:47c32687a44c | 83 | |
bridadan | 11:47c32687a44c | 84 | /** Allocate space for a socket. |
bridadan | 11:47c32687a44c | 85 | @returns a pointer to the allocated socket. |
bridadan | 11:47c32687a44c | 86 | */ |
bridadan | 11:47c32687a44c | 87 | virtual SocketInterface* allocateSocket(socket_protocol_t socketProtocol) const = 0; |
bridadan | 11:47c32687a44c | 88 | |
bridadan | 11:47c32687a44c | 89 | /** Deallocate space for a socket. |
bridadan | 11:47c32687a44c | 90 | @returns a pointer to the deallocated socket. |
bridadan | 11:47c32687a44c | 91 | */ |
bridadan | 11:47c32687a44c | 92 | virtual int deallocateSocket(SocketInterface* socket) const = 0; |
bridadan | 11:47c32687a44c | 93 | |
bridadan | 11:47c32687a44c | 94 | protected: |
bridadan | 11:47c32687a44c | 95 | /** Map used to keep track of all SocketInterface instances */ |
bridadan | 11:47c32687a44c | 96 | std::map<int, SocketInterface*> sockets; |
bridadan | 11:47c32687a44c | 97 | |
bridadan | 11:47c32687a44c | 98 | /** Counter used to create unique handles for new sockets. |
bridadan | 11:47c32687a44c | 99 | Should be incremented whenever a new socket is created. |
bridadan | 11:47c32687a44c | 100 | */ |
bridadan | 11:47c32687a44c | 101 | uint32_t uuidCounter; |
bridadan | 1:291a9d61e58a | 102 | }; |
bridadan | 1:291a9d61e58a | 103 | |
bridadan | 1:291a9d61e58a | 104 | #endif |