A new object oriented network api that can be used to replace the one provided by the EthernetInterface library.

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Object oriented network interface for the mbed platform

Currently implemented:

  • Address
  • Endpoint
  • UDP Socket
  • TCP Socket
  • Databuffer
  • Select API

It depends on the EthernetInterface for the lwip network stack.

Please do not hesitate to contact me with any remarks, improvements or questions.

The API is also available for unix at GitHub: LibNosa

Examples

Committer:
NegativeBlack
Date:
Sat Nov 15 21:35:31 2014 +0000
Revision:
9:7ac7c29fea3d
Parent:
8:cdee0f2b6ff0
Fixed type warnings in address::formString().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NegativeBlack 4:d854fa394f85 1 /**
NegativeBlack 4:d854fa394f85 2 * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
NegativeBlack 4:d854fa394f85 3 * All rights reserved.
NegativeBlack 4:d854fa394f85 4 *
NegativeBlack 4:d854fa394f85 5 * Redistribution and use in source and binary forms, with or without
NegativeBlack 4:d854fa394f85 6 * modification, are permitted provided that the following conditions are met:
NegativeBlack 4:d854fa394f85 7 *
NegativeBlack 4:d854fa394f85 8 * 1. Redistributions of source code must retain the above copyright notice, this
NegativeBlack 4:d854fa394f85 9 * list of conditions and the following disclaimer.
NegativeBlack 4:d854fa394f85 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
NegativeBlack 4:d854fa394f85 11 * this list of conditions and the following disclaimer in the documentation
NegativeBlack 4:d854fa394f85 12 * and/or other materials provided with the distribution.
NegativeBlack 4:d854fa394f85 13 *
NegativeBlack 4:d854fa394f85 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
NegativeBlack 4:d854fa394f85 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
NegativeBlack 4:d854fa394f85 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NegativeBlack 4:d854fa394f85 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
NegativeBlack 4:d854fa394f85 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
NegativeBlack 4:d854fa394f85 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
NegativeBlack 4:d854fa394f85 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
NegativeBlack 4:d854fa394f85 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
NegativeBlack 4:d854fa394f85 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
NegativeBlack 4:d854fa394f85 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NegativeBlack 4:d854fa394f85 24 */
NegativeBlack 4:d854fa394f85 25
NegativeBlack 4:d854fa394f85 26 #include "buffer.hpp"
NegativeBlack 4:d854fa394f85 27 using namespace network;
NegativeBlack 4:d854fa394f85 28
NegativeBlack 4:d854fa394f85 29 Buffer::Buffer():
NegativeBlack 4:d854fa394f85 30 _memory(NULL), _size(0), _length(0)
NegativeBlack 4:d854fa394f85 31 {}
NegativeBlack 4:d854fa394f85 32
NegativeBlack 4:d854fa394f85 33 Buffer::Buffer(const size_t size):
NegativeBlack 4:d854fa394f85 34 _size(size), _length(0)
NegativeBlack 4:d854fa394f85 35 {
NegativeBlack 4:d854fa394f85 36 this->_memory_allocate(size);
NegativeBlack 4:d854fa394f85 37 }
NegativeBlack 4:d854fa394f85 38
NegativeBlack 4:d854fa394f85 39 Buffer::Buffer(const Buffer &other):
NegativeBlack 4:d854fa394f85 40 _size(other._size), _length(other._length)
NegativeBlack 4:d854fa394f85 41 {
NegativeBlack 4:d854fa394f85 42 if (this->_memory_allocate(other._size) != -1) {
NegativeBlack 4:d854fa394f85 43 std::memcpy(this->_memory, other._memory, other._size);
NegativeBlack 4:d854fa394f85 44 }
NegativeBlack 4:d854fa394f85 45 }
NegativeBlack 4:d854fa394f85 46
NegativeBlack 8:cdee0f2b6ff0 47 Buffer::Buffer(const std::string &other):
NegativeBlack 8:cdee0f2b6ff0 48 _size(other.size()), _length(other.length())
NegativeBlack 8:cdee0f2b6ff0 49 {
NegativeBlack 8:cdee0f2b6ff0 50 if (this->_memory_allocate(other.size()) != -1) {
NegativeBlack 8:cdee0f2b6ff0 51 std::memcpy(this->_memory, other.data(), other.size());
NegativeBlack 8:cdee0f2b6ff0 52 }
NegativeBlack 8:cdee0f2b6ff0 53 }
NegativeBlack 8:cdee0f2b6ff0 54
NegativeBlack 4:d854fa394f85 55 Buffer::~Buffer()
NegativeBlack 4:d854fa394f85 56 {
NegativeBlack 4:d854fa394f85 57 this->_memory_free();
NegativeBlack 4:d854fa394f85 58 }
NegativeBlack 4:d854fa394f85 59
NegativeBlack 4:d854fa394f85 60 Buffer &
NegativeBlack 4:d854fa394f85 61 Buffer::operator=(const Buffer &other)
NegativeBlack 4:d854fa394f85 62 {
NegativeBlack 8:cdee0f2b6ff0 63 // Resize buffer if necessary
NegativeBlack 8:cdee0f2b6ff0 64 if (this->_size < other._size) {
NegativeBlack 8:cdee0f2b6ff0 65 this->_memory_free();
NegativeBlack 8:cdee0f2b6ff0 66 if (this->_memory_allocate(other._size) < 0) {
NegativeBlack 8:cdee0f2b6ff0 67 return (*this);
NegativeBlack 8:cdee0f2b6ff0 68 }
NegativeBlack 4:d854fa394f85 69 }
NegativeBlack 4:d854fa394f85 70
NegativeBlack 8:cdee0f2b6ff0 71 // Copy information
NegativeBlack 4:d854fa394f85 72 std::memcpy(this->_memory, other._memory, other._size);
NegativeBlack 4:d854fa394f85 73 this->_size = other._size;
NegativeBlack 4:d854fa394f85 74 this->_length = other._length;
NegativeBlack 4:d854fa394f85 75
NegativeBlack 4:d854fa394f85 76 return (*this);
NegativeBlack 4:d854fa394f85 77 }
NegativeBlack 4:d854fa394f85 78
NegativeBlack 8:cdee0f2b6ff0 79 Buffer &
NegativeBlack 8:cdee0f2b6ff0 80 Buffer::operator=(const std::string &other)
NegativeBlack 8:cdee0f2b6ff0 81 {
NegativeBlack 8:cdee0f2b6ff0 82 // Resize buffer if necessary
NegativeBlack 8:cdee0f2b6ff0 83 if (this->_size < other.size()) {
NegativeBlack 8:cdee0f2b6ff0 84 this->_memory_free();
NegativeBlack 8:cdee0f2b6ff0 85 if (this->_memory_allocate(other.size()) < 0) {
NegativeBlack 8:cdee0f2b6ff0 86 return (*this);
NegativeBlack 8:cdee0f2b6ff0 87 }
NegativeBlack 8:cdee0f2b6ff0 88 }
NegativeBlack 8:cdee0f2b6ff0 89
NegativeBlack 8:cdee0f2b6ff0 90 // Copy information
NegativeBlack 8:cdee0f2b6ff0 91 std::memcpy(this->_memory, other.data(), other.size());
NegativeBlack 8:cdee0f2b6ff0 92 this->_size = other.size();
NegativeBlack 8:cdee0f2b6ff0 93 this->_length = other.length();
NegativeBlack 8:cdee0f2b6ff0 94
NegativeBlack 8:cdee0f2b6ff0 95 return (*this);
NegativeBlack 8:cdee0f2b6ff0 96 }
NegativeBlack 8:cdee0f2b6ff0 97
NegativeBlack 4:d854fa394f85 98 int
NegativeBlack 4:d854fa394f85 99 Buffer::read(void *data, size_t max_length, size_t offset)
NegativeBlack 4:d854fa394f85 100 {
NegativeBlack 4:d854fa394f85 101 if ((offset >= this->_size) || (this->_memory == NULL)) {
NegativeBlack 4:d854fa394f85 102 return 0;
NegativeBlack 4:d854fa394f85 103 }
NegativeBlack 4:d854fa394f85 104
NegativeBlack 4:d854fa394f85 105 int bytes_to_copy = (max_length > this->_size - offset)
NegativeBlack 4:d854fa394f85 106 ? this->_size - offset
NegativeBlack 4:d854fa394f85 107 : max_length;
NegativeBlack 4:d854fa394f85 108
NegativeBlack 4:d854fa394f85 109 std::memcpy(data, (unsigned char*)this->_memory + offset, bytes_to_copy);
NegativeBlack 4:d854fa394f85 110 return bytes_to_copy;
NegativeBlack 4:d854fa394f85 111 }
NegativeBlack 4:d854fa394f85 112
NegativeBlack 4:d854fa394f85 113 int
NegativeBlack 4:d854fa394f85 114 Buffer::write(const void *data, size_t length, size_t offset)
NegativeBlack 4:d854fa394f85 115 {
NegativeBlack 4:d854fa394f85 116 if ((offset >= this->_size) || (this->_memory == NULL)) {
NegativeBlack 4:d854fa394f85 117 return 0;
NegativeBlack 4:d854fa394f85 118 }
NegativeBlack 4:d854fa394f85 119
NegativeBlack 4:d854fa394f85 120 int bytes_to_copy = (length > this->_size - offset)
NegativeBlack 4:d854fa394f85 121 ? this->_size - offset
NegativeBlack 4:d854fa394f85 122 : length;
NegativeBlack 4:d854fa394f85 123
NegativeBlack 4:d854fa394f85 124 std::memcpy((unsigned char*)this->_memory + offset, data, bytes_to_copy);
NegativeBlack 4:d854fa394f85 125 this->_length += bytes_to_copy;
NegativeBlack 4:d854fa394f85 126 return bytes_to_copy;
NegativeBlack 4:d854fa394f85 127 }
NegativeBlack 4:d854fa394f85 128
NegativeBlack 7:9796742904fa 129 int
NegativeBlack 7:9796742904fa 130 Buffer::flush()
NegativeBlack 7:9796742904fa 131 {
NegativeBlack 7:9796742904fa 132 if (this->_memory == NULL) {
NegativeBlack 7:9796742904fa 133 return -1;
NegativeBlack 7:9796742904fa 134 }
NegativeBlack 7:9796742904fa 135
NegativeBlack 8:cdee0f2b6ff0 136 this->_length = 0;
NegativeBlack 7:9796742904fa 137 std::memset(this->_memory, 0, this->_size);
NegativeBlack 8:cdee0f2b6ff0 138
NegativeBlack 8:cdee0f2b6ff0 139 return 0;
NegativeBlack 7:9796742904fa 140 }
NegativeBlack 7:9796742904fa 141
NegativeBlack 4:d854fa394f85 142 void *
NegativeBlack 8:cdee0f2b6ff0 143 Buffer::data(size_t offset)
NegativeBlack 4:d854fa394f85 144 {
NegativeBlack 4:d854fa394f85 145 if (this->_memory == NULL) {
NegativeBlack 4:d854fa394f85 146 return NULL;
NegativeBlack 4:d854fa394f85 147 }
NegativeBlack 4:d854fa394f85 148
NegativeBlack 4:d854fa394f85 149 if (offset >= this->_size) {
NegativeBlack 4:d854fa394f85 150 return NULL;
NegativeBlack 4:d854fa394f85 151 }
NegativeBlack 4:d854fa394f85 152
NegativeBlack 4:d854fa394f85 153 return ((unsigned char*)this->_memory + offset);
NegativeBlack 4:d854fa394f85 154 }
NegativeBlack 4:d854fa394f85 155
NegativeBlack 4:d854fa394f85 156 void
NegativeBlack 8:cdee0f2b6ff0 157 Buffer::length(size_t length)
NegativeBlack 4:d854fa394f85 158 {
NegativeBlack 4:d854fa394f85 159 if (length > this->_size) {
NegativeBlack 4:d854fa394f85 160 this->_length = this->_size;
NegativeBlack 4:d854fa394f85 161 } else {
NegativeBlack 4:d854fa394f85 162 this->_length = length;
NegativeBlack 4:d854fa394f85 163 }
NegativeBlack 4:d854fa394f85 164 }
NegativeBlack 4:d854fa394f85 165
NegativeBlack 4:d854fa394f85 166 size_t
NegativeBlack 8:cdee0f2b6ff0 167 Buffer::length()
NegativeBlack 8:cdee0f2b6ff0 168 {
NegativeBlack 8:cdee0f2b6ff0 169 return this->_length;
NegativeBlack 8:cdee0f2b6ff0 170 }
NegativeBlack 8:cdee0f2b6ff0 171
NegativeBlack 8:cdee0f2b6ff0 172 size_t
NegativeBlack 4:d854fa394f85 173 Buffer::size()
NegativeBlack 4:d854fa394f85 174 {
NegativeBlack 4:d854fa394f85 175 return this->_size;
NegativeBlack 4:d854fa394f85 176 }
NegativeBlack 4:d854fa394f85 177
NegativeBlack 4:d854fa394f85 178 size_t
NegativeBlack 4:d854fa394f85 179 Buffer::free()
NegativeBlack 4:d854fa394f85 180 {
NegativeBlack 4:d854fa394f85 181 return this->_size - this->_length;
NegativeBlack 4:d854fa394f85 182 }
NegativeBlack 4:d854fa394f85 183
NegativeBlack 4:d854fa394f85 184 int
NegativeBlack 4:d854fa394f85 185 Buffer::_memory_allocate(size_t size)
NegativeBlack 4:d854fa394f85 186 {
NegativeBlack 4:d854fa394f85 187 this->_memory = std::calloc(size, sizeof(unsigned char));
NegativeBlack 4:d854fa394f85 188
NegativeBlack 4:d854fa394f85 189 if (this->_memory == NULL) {
NegativeBlack 4:d854fa394f85 190 this->_size = 0;
NegativeBlack 4:d854fa394f85 191 this->_length = 0;
NegativeBlack 4:d854fa394f85 192 return -1;
NegativeBlack 4:d854fa394f85 193 }
NegativeBlack 4:d854fa394f85 194
NegativeBlack 4:d854fa394f85 195 return 0;
NegativeBlack 4:d854fa394f85 196 }
NegativeBlack 4:d854fa394f85 197
NegativeBlack 4:d854fa394f85 198 void
NegativeBlack 4:d854fa394f85 199 Buffer::_memory_free()
NegativeBlack 4:d854fa394f85 200 {
NegativeBlack 4:d854fa394f85 201 if (this->_memory != NULL) {
NegativeBlack 4:d854fa394f85 202 std::free(this->_memory);
NegativeBlack 4:d854fa394f85 203 }
NegativeBlack 4:d854fa394f85 204 }