Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/tcpsocketconnection_mbed_os5.cpp

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include "mbed.h"
5
6 #include <stddef.h>
7 #include "TCPSocket.h"
8 #include "azure_c_shared_utility/tcpsocketconnection_c.h"
9
10 // The NetworkInterface instance of network device
11 extern NetworkInterface *_defaultSystemNetwork;
12
13 static volatile bool tcpsocketconnection_isConnected = false;
14
15 TCPSOCKETCONNECTION_HANDLE tcpsocketconnection_create(void)
16 {
17 TCPSocket* tcpSocket = new TCPSocket();
18 tcpSocket->open(_defaultSystemNetwork);
19 return tcpSocket;
20 }
21
22 void tcpsocketconnection_set_blocking(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, bool blocking, unsigned int timeout)
23 {
24 if (tcpSocketConnectionHandle != NULL)
25 {
26 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
27 tsc->set_blocking(blocking);
28 tsc->set_timeout(timeout);
29 }
30 }
31
32 void tcpsocketconnection_destroy(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle)
33 {
34 if (tcpSocketConnectionHandle != NULL)
35 {
36 delete (TCPSocket*)tcpSocketConnectionHandle;
37 }
38 }
39
40 int tcpsocketconnection_connect(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* host, const int port)
41 {
42 int ret = 0;
43 if (tcpSocketConnectionHandle != NULL && !tcpsocketconnection_isConnected)
44 {
45 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
46 if ( (ret = tsc->connect(host, port)) == 0)
47 {
48 tcpsocketconnection_isConnected = true;
49 }
50 }
51 return ret;
52 }
53
54 bool tcpsocketconnection_is_connected(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle)
55 {
56 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
57 return tcpsocketconnection_isConnected;
58 }
59
60 void tcpsocketconnection_close(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle)
61 {
62 if (tcpSocketConnectionHandle != NULL && tcpsocketconnection_isConnected)
63 {
64 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
65 tsc->close();
66 tcpsocketconnection_isConnected = false;
67 }
68 }
69
70 int tcpsocketconnection_send(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length)
71 {
72 if (tcpSocketConnectionHandle != NULL)
73 {
74 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
75 return tsc->send((char*)data, length);
76 }
77
78 return -1;
79 }
80
81 int tcpsocketconnection_send_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length)
82 {
83 if (tcpSocketConnectionHandle != NULL)
84 {
85 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
86 return tsc->send((char*)data, length);
87 }
88 return -1;
89 }
90
91 int tcpsocketconnection_receive(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length)
92 {
93 if (tcpSocketConnectionHandle != NULL)
94 {
95 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
96 return tsc->recv(data, length);
97 }
98 return -1;
99 }
100
101 int tcpsocketconnection_receive_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length)
102 {
103 if (tcpSocketConnectionHandle != NULL)
104 {
105 TCPSocket* tsc = (TCPSocket*)tcpSocketConnectionHandle;
106 return tsc->recv(data, length);
107 }
108 return -1;
109 }