Azure IoT common library
Fork of azure_c_shared_utility by
tcpsocketconnection_c.cpp@34:651c23af382c, 2017-08-24 (annotated)
- Committer:
- wiggly
- Date:
- Thu Aug 24 14:14:15 2017 +0100
- Revision:
- 34:651c23af382c
- Parent:
- 33:5f2a48fc7e5b
Pass in network stack to platform initialisation
Remove NTP setup from azure platform code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
Azure.IoT Build | 0:fa2de1b79154 | 4 | #include "mbed.h" |
Azure.IoT Build | 0:fa2de1b79154 | 5 | #include <stddef.h> |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include "azure_c_shared_utility/tcpsocketconnection_c.h" |
wiggly | 34:651c23af382c | 7 | #include "azure_c_shared_utility/platform.h" |
Azure.IoT Build | 0:fa2de1b79154 | 8 | |
wiggly | 33:5f2a48fc7e5b | 9 | typedef struct { |
wiggly | 33:5f2a48fc7e5b | 10 | TCPSocket* socket; |
wiggly | 33:5f2a48fc7e5b | 11 | bool connected; |
wiggly | 33:5f2a48fc7e5b | 12 | } TCPSOCKETCONNECTION_HANDLE_T; |
Azure.IoT Build | 0:fa2de1b79154 | 13 | |
Azure.IoT Build | 0:fa2de1b79154 | 14 | TCPSOCKETCONNECTION_HANDLE tcpsocketconnection_create(void) |
Azure.IoT Build | 0:fa2de1b79154 | 15 | { |
wiggly | 33:5f2a48fc7e5b | 16 | TCPSOCKETCONNECTION_HANDLE_T* conn = new TCPSOCKETCONNECTION_HANDLE_T; |
wiggly | 34:651c23af382c | 17 | conn->socket = new TCPSocket(platform_get_network_stack()); |
wiggly | 33:5f2a48fc7e5b | 18 | conn->connected = false; |
wiggly | 33:5f2a48fc7e5b | 19 | return conn; |
Azure.IoT Build | 0:fa2de1b79154 | 20 | } |
Azure.IoT Build | 0:fa2de1b79154 | 21 | |
Azure.IoT Build | 0:fa2de1b79154 | 22 | void tcpsocketconnection_set_blocking(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, bool blocking, unsigned int timeout) |
Azure.IoT Build | 0:fa2de1b79154 | 23 | { |
wiggly | 33:5f2a48fc7e5b | 24 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 25 | tsc->socket->set_blocking(blocking); |
wiggly | 33:5f2a48fc7e5b | 26 | tsc->socket->set_timeout(timeout); |
Azure.IoT Build | 0:fa2de1b79154 | 27 | } |
Azure.IoT Build | 0:fa2de1b79154 | 28 | |
Azure.IoT Build | 0:fa2de1b79154 | 29 | void tcpsocketconnection_destroy(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle) |
Azure.IoT Build | 0:fa2de1b79154 | 30 | { |
wiggly | 33:5f2a48fc7e5b | 31 | delete (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
Azure.IoT Build | 0:fa2de1b79154 | 32 | } |
Azure.IoT Build | 0:fa2de1b79154 | 33 | |
Azure.IoT Build | 0:fa2de1b79154 | 34 | int tcpsocketconnection_connect(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* host, const int port) |
Azure.IoT Build | 0:fa2de1b79154 | 35 | { |
wiggly | 33:5f2a48fc7e5b | 36 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 37 | // TODO: so, we set this but we really need to kill it if we get an error later too..... |
wiggly | 33:5f2a48fc7e5b | 38 | auto rv = tsc->socket->connect(host, port); |
wiggly | 33:5f2a48fc7e5b | 39 | if(rv == 0) { tsc->connected = true; } |
wiggly | 33:5f2a48fc7e5b | 40 | return rv; |
Azure.IoT Build | 0:fa2de1b79154 | 41 | } |
Azure.IoT Build | 0:fa2de1b79154 | 42 | |
Azure.IoT Build | 0:fa2de1b79154 | 43 | bool tcpsocketconnection_is_connected(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle) |
Azure.IoT Build | 0:fa2de1b79154 | 44 | { |
wiggly | 33:5f2a48fc7e5b | 45 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 46 | return tsc->connected; |
Azure.IoT Build | 0:fa2de1b79154 | 47 | } |
Azure.IoT Build | 0:fa2de1b79154 | 48 | |
Azure.IoT Build | 0:fa2de1b79154 | 49 | void tcpsocketconnection_close(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle) |
Azure.IoT Build | 0:fa2de1b79154 | 50 | { |
wiggly | 33:5f2a48fc7e5b | 51 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 52 | tsc->socket->close(); |
Azure.IoT Build | 0:fa2de1b79154 | 53 | } |
Azure.IoT Build | 0:fa2de1b79154 | 54 | |
Azure.IoT Build | 0:fa2de1b79154 | 55 | int tcpsocketconnection_send(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length) |
Azure.IoT Build | 0:fa2de1b79154 | 56 | { |
wiggly | 33:5f2a48fc7e5b | 57 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 58 | return tsc->socket->send((char*)data, length); |
Azure.IoT Build | 0:fa2de1b79154 | 59 | } |
Azure.IoT Build | 0:fa2de1b79154 | 60 | |
Azure.IoT Build | 0:fa2de1b79154 | 61 | int tcpsocketconnection_send_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length) |
Azure.IoT Build | 0:fa2de1b79154 | 62 | { |
wiggly | 33:5f2a48fc7e5b | 63 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 64 | // TODO: this is shonky and quickly done.... |
wiggly | 33:5f2a48fc7e5b | 65 | |
wiggly | 33:5f2a48fc7e5b | 66 | auto rv = tsc->socket->send((char*)data, length); |
wiggly | 33:5f2a48fc7e5b | 67 | |
wiggly | 33:5f2a48fc7e5b | 68 | if(rv < 0 || rv == length) { |
wiggly | 33:5f2a48fc7e5b | 69 | return rv; |
wiggly | 33:5f2a48fc7e5b | 70 | } |
wiggly | 33:5f2a48fc7e5b | 71 | |
wiggly | 33:5f2a48fc7e5b | 72 | auto remain = length - rv; |
wiggly | 33:5f2a48fc7e5b | 73 | |
wiggly | 33:5f2a48fc7e5b | 74 | while(rv > 0 && remain > 0) { |
wiggly | 33:5f2a48fc7e5b | 75 | data = data + rv; |
wiggly | 33:5f2a48fc7e5b | 76 | rv = tsc->socket->send((char*)data, length); |
wiggly | 33:5f2a48fc7e5b | 77 | remain = length - rv; |
wiggly | 33:5f2a48fc7e5b | 78 | } |
wiggly | 33:5f2a48fc7e5b | 79 | |
wiggly | 33:5f2a48fc7e5b | 80 | return rv; |
Azure.IoT Build | 0:fa2de1b79154 | 81 | } |
Azure.IoT Build | 0:fa2de1b79154 | 82 | |
Azure.IoT Build | 0:fa2de1b79154 | 83 | int tcpsocketconnection_receive(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length) |
Azure.IoT Build | 0:fa2de1b79154 | 84 | { |
wiggly | 33:5f2a48fc7e5b | 85 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 86 | return tsc->socket->recv(data, length); |
Azure.IoT Build | 0:fa2de1b79154 | 87 | } |
Azure.IoT Build | 0:fa2de1b79154 | 88 | |
Azure.IoT Build | 0:fa2de1b79154 | 89 | int tcpsocketconnection_receive_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length) |
Azure.IoT Build | 0:fa2de1b79154 | 90 | { |
wiggly | 33:5f2a48fc7e5b | 91 | TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; |
wiggly | 33:5f2a48fc7e5b | 92 | // TODO: this is shonky and quickly done.... |
wiggly | 33:5f2a48fc7e5b | 93 | |
wiggly | 33:5f2a48fc7e5b | 94 | auto rv = tsc->socket->recv((char*)data, length); |
wiggly | 33:5f2a48fc7e5b | 95 | |
wiggly | 33:5f2a48fc7e5b | 96 | if(rv < 0 || rv == length) { |
wiggly | 33:5f2a48fc7e5b | 97 | return rv; |
wiggly | 33:5f2a48fc7e5b | 98 | } |
wiggly | 33:5f2a48fc7e5b | 99 | |
wiggly | 33:5f2a48fc7e5b | 100 | auto remain = length - rv; |
wiggly | 33:5f2a48fc7e5b | 101 | |
wiggly | 33:5f2a48fc7e5b | 102 | while(rv > 0 && remain > 0) { |
wiggly | 33:5f2a48fc7e5b | 103 | data = data + rv; |
wiggly | 33:5f2a48fc7e5b | 104 | rv = tsc->socket->recv((char*)data, length); |
wiggly | 33:5f2a48fc7e5b | 105 | remain = length - rv; |
wiggly | 33:5f2a48fc7e5b | 106 | } |
wiggly | 33:5f2a48fc7e5b | 107 | |
wiggly | 33:5f2a48fc7e5b | 108 | return rv; |
Azure.IoT Build | 0:fa2de1b79154 | 109 | } |