NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 23:1e86d9fb3d86, committed 2016-02-18
- Comitter:
- Christopher Haster
- Date:
- Thu Feb 18 04:05:09 2016 -0600
- Branch:
- api-changes
- Parent:
- 22:4fca633c0633
- Child:
- 24:a5e959bdd2dd
- Commit message:
- Added shallow implementation of simple methods in interfaces
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetworkInterface.cpp Thu Feb 18 04:05:09 2016 -0600 @@ -0,0 +1,65 @@ +/* NetworkInterface Base Class + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "NetworkInterface.h" +#include <stdlib.h> +#include <string.h> + +#define IP_SIZE 16 + +NetworkInterface::NetworkInterface() + : _ip_address(0) + , _network_mask(0) + , _gateway(0) { +} + +NetworkInterface::~NetworkInterface() { + if (_ip_address) free(_ip_address); + if (_network_mask) free(_network_mask); + if (_gateway) free(_gateway); +} + +void NetworkInterface::setIPAddress(const char *ip) { + _ip_address = (char*)malloc(IP_SIZE); + strcpy(_ip_address, ip); +} + +void NetworkInterface::setNetworkMask(const char *mask) { + _network_mask = (char*)malloc(IP_SIZE); + strcpy(_network_mask, mask); +} + +void NetworkInterface::setGateway(const char *gateway) { + _gateway = (char*)malloc(IP_SIZE); + strcpy(_gateway, gateway); +} + +const char *NetworkInterface::getIPAddress() { + return _ip_address; +} + +const char *NetworkInterface::getNetworkMask() { + return _network_mask; +} + +const char *NetworkInterface::getGateway() { + return _gateway; +} + +bool NetworkInterface::isConnected() { + return getIPAddress() != 0; +} +
--- a/NetworkInterface.h Thu Feb 18 03:29:39 2016 -0600 +++ b/NetworkInterface.h Thu Feb 18 04:05:09 2016 -0600 @@ -28,7 +28,8 @@ class NetworkInterface { public: - virtual ~NetworkInterface() = default; + NetworkInterface(); + virtual ~NetworkInterface(); /** Enables DHCP and clears any static address @@ -75,7 +76,7 @@ /** Get the current status of the interface @return true if connected */ - virtual bool isConnected(void) = 0; + virtual bool isConnected(void); private: @@ -95,9 +96,9 @@ virtual void destroySocket(SocketInterface *socket) = 0; private: - const char *_ip_address; - const char *_network_mask; - const char *_gateway; + char *_ip_address; + char *_network_mask; + char *_gateway; }; #endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SocketInterface.cpp Thu Feb 18 04:05:09 2016 -0600 @@ -0,0 +1,48 @@ +/* SocketInterface Base Class + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SocketInterface.h" +#include <stdlib.h> +#include <string.h> + +#define IP_SIZE 16 + +SocketInterface::SocketInterface() + : _ip_address(0) + , _port(0) { +} + +SocketInterface::~SocketInterface() { + if (_ip_address) free(_ip_address); +} + +void SocketInterface::setIPAddress(const char *ip) { + _ip_address = (char*)malloc(IP_SIZE); + strcpy(_ip_address, ip); +} + +void SocketInterface::setPort(uint16_t port) { + _port = port; +} + +const char *SocketInterface::getIPAddress() { + return _ip_address; +} + +uint16_t SocketInterface::getPort() { + return _port; +} +
--- a/SocketInterface.h Thu Feb 18 03:29:39 2016 -0600 +++ b/SocketInterface.h Thu Feb 18 04:05:09 2016 -0600 @@ -36,7 +36,8 @@ class SocketInterface { public: - virtual ~SocketInterface() = default; + SocketInterface(); + virtual ~SocketInterface(); /** Set the IP address of the socket