M2X Ethernet demo using Seeed Ethernet W5200 Shield

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed Nucleo_Sensor_Shield

Fork of m2x-seeed_ethernet_demo by Sean Newton

Committer:
dangriffin
Date:
Wed Dec 17 20:45:13 2014 +0000
Revision:
11:40d8cfc941ed
Parent:
7:a94ba2e0cd04
Use the public Nucleo Sensor Shield library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SeanNewton 7:a94ba2e0cd04 1 /* Copyright (C) 2012 mbed.org, MIT License
SeanNewton 7:a94ba2e0cd04 2 *
SeanNewton 7:a94ba2e0cd04 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
SeanNewton 7:a94ba2e0cd04 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
SeanNewton 7:a94ba2e0cd04 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
SeanNewton 7:a94ba2e0cd04 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
SeanNewton 7:a94ba2e0cd04 7 * furnished to do so, subject to the following conditions:
SeanNewton 7:a94ba2e0cd04 8 *
SeanNewton 7:a94ba2e0cd04 9 * The above copyright notice and this permission notice shall be included in all copies or
SeanNewton 7:a94ba2e0cd04 10 * substantial portions of the Software.
SeanNewton 7:a94ba2e0cd04 11 *
SeanNewton 7:a94ba2e0cd04 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
SeanNewton 7:a94ba2e0cd04 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
SeanNewton 7:a94ba2e0cd04 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
SeanNewton 7:a94ba2e0cd04 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SeanNewton 7:a94ba2e0cd04 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SeanNewton 7:a94ba2e0cd04 17 */
SeanNewton 7:a94ba2e0cd04 18
SeanNewton 7:a94ba2e0cd04 19 #include "TCPSocketConnection.h"
SeanNewton 7:a94ba2e0cd04 20
SeanNewton 7:a94ba2e0cd04 21 TCPSocketConnection::TCPSocketConnection()
SeanNewton 7:a94ba2e0cd04 22 {
SeanNewton 7:a94ba2e0cd04 23 }
SeanNewton 7:a94ba2e0cd04 24
SeanNewton 7:a94ba2e0cd04 25 int TCPSocketConnection::connect(const char* host, const int port)
SeanNewton 7:a94ba2e0cd04 26 {
SeanNewton 7:a94ba2e0cd04 27 if (_sock_fd < 0) {
SeanNewton 7:a94ba2e0cd04 28 _sock_fd = eth->new_socket();
SeanNewton 7:a94ba2e0cd04 29 if (_sock_fd < 0) {
SeanNewton 7:a94ba2e0cd04 30 return -1;
SeanNewton 7:a94ba2e0cd04 31 }
SeanNewton 7:a94ba2e0cd04 32 }
SeanNewton 7:a94ba2e0cd04 33 if (set_address(host, port) != 0) {
SeanNewton 7:a94ba2e0cd04 34 return -1;
SeanNewton 7:a94ba2e0cd04 35 }
SeanNewton 7:a94ba2e0cd04 36 if (!eth->connect(_sock_fd, get_address(), port)) {
SeanNewton 7:a94ba2e0cd04 37 return -1;
SeanNewton 7:a94ba2e0cd04 38 }
SeanNewton 7:a94ba2e0cd04 39 return 0;
SeanNewton 7:a94ba2e0cd04 40 }
SeanNewton 7:a94ba2e0cd04 41
SeanNewton 7:a94ba2e0cd04 42 bool TCPSocketConnection::is_connected(void)
SeanNewton 7:a94ba2e0cd04 43 {
SeanNewton 7:a94ba2e0cd04 44 return eth->is_connected(_sock_fd);
SeanNewton 7:a94ba2e0cd04 45 }
SeanNewton 7:a94ba2e0cd04 46
SeanNewton 7:a94ba2e0cd04 47 int TCPSocketConnection::send(char* data, int length)
SeanNewton 7:a94ba2e0cd04 48 {
SeanNewton 7:a94ba2e0cd04 49 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
SeanNewton 7:a94ba2e0cd04 50 if (size < 0) {
SeanNewton 7:a94ba2e0cd04 51 return -1;
SeanNewton 7:a94ba2e0cd04 52 }
SeanNewton 7:a94ba2e0cd04 53 if (size > length) {
SeanNewton 7:a94ba2e0cd04 54 size = length;
SeanNewton 7:a94ba2e0cd04 55 }
SeanNewton 7:a94ba2e0cd04 56 return eth->send(_sock_fd, data, size);
SeanNewton 7:a94ba2e0cd04 57 }
SeanNewton 7:a94ba2e0cd04 58
SeanNewton 7:a94ba2e0cd04 59 // -1 if unsuccessful, else number of bytes written
SeanNewton 7:a94ba2e0cd04 60 int TCPSocketConnection::send_all(char* data, int length)
SeanNewton 7:a94ba2e0cd04 61 {
SeanNewton 7:a94ba2e0cd04 62 int writtenLen = 0;
SeanNewton 7:a94ba2e0cd04 63 while (writtenLen < length) {
SeanNewton 7:a94ba2e0cd04 64 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
SeanNewton 7:a94ba2e0cd04 65 if (size < 0) {
SeanNewton 7:a94ba2e0cd04 66 return -1;
SeanNewton 7:a94ba2e0cd04 67 }
SeanNewton 7:a94ba2e0cd04 68 if (size > (length-writtenLen)) {
SeanNewton 7:a94ba2e0cd04 69 size = (length-writtenLen);
SeanNewton 7:a94ba2e0cd04 70 }
SeanNewton 7:a94ba2e0cd04 71 int ret = eth->send(_sock_fd, data + writtenLen, size);
SeanNewton 7:a94ba2e0cd04 72 if (ret < 0) {
SeanNewton 7:a94ba2e0cd04 73 return -1;
SeanNewton 7:a94ba2e0cd04 74 }
SeanNewton 7:a94ba2e0cd04 75 writtenLen += ret;
SeanNewton 7:a94ba2e0cd04 76 }
SeanNewton 7:a94ba2e0cd04 77 return writtenLen;
SeanNewton 7:a94ba2e0cd04 78 }
SeanNewton 7:a94ba2e0cd04 79
SeanNewton 7:a94ba2e0cd04 80 // -1 if unsuccessful, else number of bytes received
SeanNewton 7:a94ba2e0cd04 81 int TCPSocketConnection::receive(char* data, int length)
SeanNewton 7:a94ba2e0cd04 82 {
SeanNewton 7:a94ba2e0cd04 83 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
SeanNewton 7:a94ba2e0cd04 84 if (size < 0) {
SeanNewton 7:a94ba2e0cd04 85 return -1;
SeanNewton 7:a94ba2e0cd04 86 }
SeanNewton 7:a94ba2e0cd04 87 if (size > length) {
SeanNewton 7:a94ba2e0cd04 88 size = length;
SeanNewton 7:a94ba2e0cd04 89 }
SeanNewton 7:a94ba2e0cd04 90 return eth->recv(_sock_fd, data, size);
SeanNewton 7:a94ba2e0cd04 91 }
SeanNewton 7:a94ba2e0cd04 92
SeanNewton 7:a94ba2e0cd04 93 // -1 if unsuccessful, else number of bytes received
SeanNewton 7:a94ba2e0cd04 94 int TCPSocketConnection::receive_all(char* data, int length)
SeanNewton 7:a94ba2e0cd04 95 {
SeanNewton 7:a94ba2e0cd04 96 int readLen = 0;
SeanNewton 7:a94ba2e0cd04 97 while (readLen < length) {
SeanNewton 7:a94ba2e0cd04 98 int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
SeanNewton 7:a94ba2e0cd04 99 if (size <= 0) {
SeanNewton 7:a94ba2e0cd04 100 break;
SeanNewton 7:a94ba2e0cd04 101 }
SeanNewton 7:a94ba2e0cd04 102 if (size > (length - readLen)) {
SeanNewton 7:a94ba2e0cd04 103 size = length - readLen;
SeanNewton 7:a94ba2e0cd04 104 }
SeanNewton 7:a94ba2e0cd04 105 int ret = eth->recv(_sock_fd, data + readLen, size);
SeanNewton 7:a94ba2e0cd04 106 if (ret < 0) {
SeanNewton 7:a94ba2e0cd04 107 return -1;
SeanNewton 7:a94ba2e0cd04 108 }
SeanNewton 7:a94ba2e0cd04 109 readLen += ret;
SeanNewton 7:a94ba2e0cd04 110 }
SeanNewton 7:a94ba2e0cd04 111 return readLen;
SeanNewton 7:a94ba2e0cd04 112 }
SeanNewton 7:a94ba2e0cd04 113