Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
socketio_mbed.c@45:1119d0f2c4d8, 2018-06-11 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Mon Jun 11 15:41:10 2018 -0700
- Revision:
- 45:1119d0f2c4d8
- Parent:
- 21:b92006c5b9ff
- Child:
- 48:81866008bba4
1.2.5
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 <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 5 | #include <stddef.h> |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include <stdio.h> |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #include <string.h> |
Azure.IoT Build | 0:fa2de1b79154 | 8 | #include "azure_c_shared_utility/socketio.h" |
AzureIoTClient | 13:920e00014ee3 | 9 | #include "azure_c_shared_utility/singlylinkedlist.h" |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include "azure_c_shared_utility/tcpsocketconnection_c.h" |
AzureIoTClient | 21:b92006c5b9ff | 11 | #include "azure_c_shared_utility/optimize_size.h" |
Azure.IoT Build | 6:c55b013dfc2a | 12 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:fa2de1b79154 | 13 | |
Azure.IoT Build | 0:fa2de1b79154 | 14 | #define UNABLE_TO_COMPLETE -2 |
AzureIoTClient | 1:9190c0f4d23a | 15 | #define MBED_RECEIVE_BYTES_VALUE 128 |
Azure.IoT Build | 0:fa2de1b79154 | 16 | |
Azure.IoT Build | 0:fa2de1b79154 | 17 | typedef enum IO_STATE_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 18 | { |
Azure.IoT Build | 0:fa2de1b79154 | 19 | IO_STATE_CLOSED, |
Azure.IoT Build | 0:fa2de1b79154 | 20 | IO_STATE_OPENING, |
Azure.IoT Build | 0:fa2de1b79154 | 21 | IO_STATE_OPEN, |
Azure.IoT Build | 0:fa2de1b79154 | 22 | IO_STATE_CLOSING, |
Azure.IoT Build | 0:fa2de1b79154 | 23 | IO_STATE_ERROR |
Azure.IoT Build | 0:fa2de1b79154 | 24 | } IO_STATE; |
Azure.IoT Build | 0:fa2de1b79154 | 25 | |
Azure.IoT Build | 0:fa2de1b79154 | 26 | typedef struct PENDING_SOCKET_IO_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 27 | { |
Azure.IoT Build | 0:fa2de1b79154 | 28 | unsigned char* bytes; |
Azure.IoT Build | 0:fa2de1b79154 | 29 | size_t size; |
Azure.IoT Build | 0:fa2de1b79154 | 30 | ON_SEND_COMPLETE on_send_complete; |
Azure.IoT Build | 0:fa2de1b79154 | 31 | void* callback_context; |
AzureIoTClient | 13:920e00014ee3 | 32 | SINGLYLINKEDLIST_HANDLE pending_io_list; |
Azure.IoT Build | 0:fa2de1b79154 | 33 | } PENDING_SOCKET_IO; |
Azure.IoT Build | 0:fa2de1b79154 | 34 | |
Azure.IoT Build | 0:fa2de1b79154 | 35 | typedef struct SOCKET_IO_INSTANCE_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 36 | { |
Azure.IoT Build | 0:fa2de1b79154 | 37 | TCPSOCKETCONNECTION_HANDLE tcp_socket_connection; |
Azure.IoT Build | 0:fa2de1b79154 | 38 | ON_BYTES_RECEIVED on_bytes_received; |
Azure.IoT Build | 0:fa2de1b79154 | 39 | ON_IO_ERROR on_io_error; |
Azure.IoT Build | 0:fa2de1b79154 | 40 | void* on_bytes_received_context; |
Azure.IoT Build | 0:fa2de1b79154 | 41 | void* on_io_error_context; |
Azure.IoT Build | 0:fa2de1b79154 | 42 | char* hostname; |
Azure.IoT Build | 0:fa2de1b79154 | 43 | int port; |
Azure.IoT Build | 0:fa2de1b79154 | 44 | IO_STATE io_state; |
AzureIoTClient | 13:920e00014ee3 | 45 | SINGLYLINKEDLIST_HANDLE pending_io_list; |
Azure.IoT Build | 0:fa2de1b79154 | 46 | } SOCKET_IO_INSTANCE; |
Azure.IoT Build | 0:fa2de1b79154 | 47 | |
AzureIoTClient | 10:1be0bc9a9deb | 48 | /*this function will clone an option given by name and value*/ |
AzureIoTClient | 10:1be0bc9a9deb | 49 | static void* socketio_CloneOption(const char* name, const void* value) |
AzureIoTClient | 10:1be0bc9a9deb | 50 | { |
AzureIoTClient | 45:1119d0f2c4d8 | 51 | (void)name; |
AzureIoTClient | 45:1119d0f2c4d8 | 52 | (void)value; |
AzureIoTClient | 10:1be0bc9a9deb | 53 | return NULL; |
AzureIoTClient | 10:1be0bc9a9deb | 54 | } |
AzureIoTClient | 10:1be0bc9a9deb | 55 | |
AzureIoTClient | 10:1be0bc9a9deb | 56 | /*this function destroys an option previously created*/ |
AzureIoTClient | 10:1be0bc9a9deb | 57 | static void socketio_DestroyOption(const char* name, const void* value) |
AzureIoTClient | 10:1be0bc9a9deb | 58 | { |
AzureIoTClient | 45:1119d0f2c4d8 | 59 | (void)name; |
AzureIoTClient | 45:1119d0f2c4d8 | 60 | (void)value; |
AzureIoTClient | 10:1be0bc9a9deb | 61 | } |
AzureIoTClient | 10:1be0bc9a9deb | 62 | |
AzureIoTClient | 10:1be0bc9a9deb | 63 | static OPTIONHANDLER_HANDLE socketio_retrieveoptions(CONCRETE_IO_HANDLE socket_io) |
AzureIoTClient | 10:1be0bc9a9deb | 64 | { |
AzureIoTClient | 10:1be0bc9a9deb | 65 | OPTIONHANDLER_HANDLE result; |
AzureIoTClient | 10:1be0bc9a9deb | 66 | (void)socket_io; |
AzureIoTClient | 10:1be0bc9a9deb | 67 | result = OptionHandler_Create(socketio_CloneOption, socketio_DestroyOption, socketio_setoption); |
AzureIoTClient | 10:1be0bc9a9deb | 68 | if (result == NULL) |
AzureIoTClient | 10:1be0bc9a9deb | 69 | { |
AzureIoTClient | 10:1be0bc9a9deb | 70 | /*return as is*/ |
AzureIoTClient | 10:1be0bc9a9deb | 71 | } |
AzureIoTClient | 10:1be0bc9a9deb | 72 | else |
AzureIoTClient | 10:1be0bc9a9deb | 73 | { |
AzureIoTClient | 10:1be0bc9a9deb | 74 | /*insert here work to add the options to "result" handle*/ |
AzureIoTClient | 10:1be0bc9a9deb | 75 | } |
AzureIoTClient | 10:1be0bc9a9deb | 76 | return result; |
AzureIoTClient | 10:1be0bc9a9deb | 77 | } |
AzureIoTClient | 10:1be0bc9a9deb | 78 | |
Azure.IoT Build | 0:fa2de1b79154 | 79 | static const IO_INTERFACE_DESCRIPTION socket_io_interface_description = |
Azure.IoT Build | 0:fa2de1b79154 | 80 | { |
AzureIoTClient | 10:1be0bc9a9deb | 81 | socketio_retrieveoptions, |
Azure.IoT Build | 0:fa2de1b79154 | 82 | socketio_create, |
Azure.IoT Build | 0:fa2de1b79154 | 83 | socketio_destroy, |
Azure.IoT Build | 0:fa2de1b79154 | 84 | socketio_open, |
Azure.IoT Build | 0:fa2de1b79154 | 85 | socketio_close, |
Azure.IoT Build | 0:fa2de1b79154 | 86 | socketio_send, |
Azure.IoT Build | 0:fa2de1b79154 | 87 | socketio_dowork, |
Azure.IoT Build | 0:fa2de1b79154 | 88 | socketio_setoption |
Azure.IoT Build | 0:fa2de1b79154 | 89 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 90 | |
Azure.IoT Build | 0:fa2de1b79154 | 91 | static void indicate_error(SOCKET_IO_INSTANCE* socket_io_instance) |
Azure.IoT Build | 0:fa2de1b79154 | 92 | { |
Azure.IoT Build | 0:fa2de1b79154 | 93 | if (socket_io_instance->on_io_error != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 94 | { |
Azure.IoT Build | 0:fa2de1b79154 | 95 | socket_io_instance->on_io_error(socket_io_instance->on_io_error_context); |
Azure.IoT Build | 0:fa2de1b79154 | 96 | } |
Azure.IoT Build | 0:fa2de1b79154 | 97 | } |
Azure.IoT Build | 0:fa2de1b79154 | 98 | |
Azure.IoT Build | 0:fa2de1b79154 | 99 | static int add_pending_io(SOCKET_IO_INSTANCE* socket_io_instance, const unsigned char* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
Azure.IoT Build | 0:fa2de1b79154 | 100 | { |
Azure.IoT Build | 0:fa2de1b79154 | 101 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 102 | PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)malloc(sizeof(PENDING_SOCKET_IO)); |
Azure.IoT Build | 0:fa2de1b79154 | 103 | if (pending_socket_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 104 | { |
AzureIoTClient | 21:b92006c5b9ff | 105 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 106 | } |
Azure.IoT Build | 0:fa2de1b79154 | 107 | else |
Azure.IoT Build | 0:fa2de1b79154 | 108 | { |
Azure.IoT Build | 0:fa2de1b79154 | 109 | pending_socket_io->bytes = (unsigned char*)malloc(size); |
Azure.IoT Build | 0:fa2de1b79154 | 110 | if (pending_socket_io->bytes == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 111 | { |
Azure.IoT Build | 0:fa2de1b79154 | 112 | free(pending_socket_io); |
AzureIoTClient | 21:b92006c5b9ff | 113 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 114 | } |
Azure.IoT Build | 0:fa2de1b79154 | 115 | else |
Azure.IoT Build | 0:fa2de1b79154 | 116 | { |
Azure.IoT Build | 0:fa2de1b79154 | 117 | pending_socket_io->size = size; |
Azure.IoT Build | 0:fa2de1b79154 | 118 | pending_socket_io->on_send_complete = on_send_complete; |
Azure.IoT Build | 0:fa2de1b79154 | 119 | pending_socket_io->callback_context = callback_context; |
Azure.IoT Build | 0:fa2de1b79154 | 120 | pending_socket_io->pending_io_list = socket_io_instance->pending_io_list; |
Azure.IoT Build | 0:fa2de1b79154 | 121 | (void)memcpy(pending_socket_io->bytes, buffer, size); |
Azure.IoT Build | 0:fa2de1b79154 | 122 | |
AzureIoTClient | 13:920e00014ee3 | 123 | if (singlylinkedlist_add(socket_io_instance->pending_io_list, pending_socket_io) == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 124 | { |
Azure.IoT Build | 0:fa2de1b79154 | 125 | free(pending_socket_io->bytes); |
Azure.IoT Build | 0:fa2de1b79154 | 126 | free(pending_socket_io); |
AzureIoTClient | 21:b92006c5b9ff | 127 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 128 | } |
Azure.IoT Build | 0:fa2de1b79154 | 129 | else |
Azure.IoT Build | 0:fa2de1b79154 | 130 | { |
Azure.IoT Build | 0:fa2de1b79154 | 131 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 132 | } |
Azure.IoT Build | 0:fa2de1b79154 | 133 | } |
Azure.IoT Build | 0:fa2de1b79154 | 134 | } |
Azure.IoT Build | 0:fa2de1b79154 | 135 | |
Azure.IoT Build | 0:fa2de1b79154 | 136 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 137 | } |
Azure.IoT Build | 0:fa2de1b79154 | 138 | |
Azure.IoT Build | 6:c55b013dfc2a | 139 | CONCRETE_IO_HANDLE socketio_create(void* io_create_parameters) |
Azure.IoT Build | 0:fa2de1b79154 | 140 | { |
Azure.IoT Build | 0:fa2de1b79154 | 141 | SOCKETIO_CONFIG* socket_io_config = io_create_parameters; |
Azure.IoT Build | 0:fa2de1b79154 | 142 | SOCKET_IO_INSTANCE* result; |
Azure.IoT Build | 0:fa2de1b79154 | 143 | |
Azure.IoT Build | 0:fa2de1b79154 | 144 | if (socket_io_config == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 145 | { |
Azure.IoT Build | 0:fa2de1b79154 | 146 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 147 | } |
Azure.IoT Build | 0:fa2de1b79154 | 148 | else |
Azure.IoT Build | 0:fa2de1b79154 | 149 | { |
Azure.IoT Build | 0:fa2de1b79154 | 150 | result = malloc(sizeof(SOCKET_IO_INSTANCE)); |
Azure.IoT Build | 0:fa2de1b79154 | 151 | if (result != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 152 | { |
AzureIoTClient | 13:920e00014ee3 | 153 | result->pending_io_list = singlylinkedlist_create(); |
Azure.IoT Build | 0:fa2de1b79154 | 154 | if (result->pending_io_list == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 155 | { |
Azure.IoT Build | 0:fa2de1b79154 | 156 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 157 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 158 | } |
Azure.IoT Build | 0:fa2de1b79154 | 159 | else |
Azure.IoT Build | 0:fa2de1b79154 | 160 | { |
Azure.IoT Build | 0:fa2de1b79154 | 161 | result->hostname = (char*)malloc(strlen(socket_io_config->hostname) + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 162 | if (result->hostname == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 163 | { |
AzureIoTClient | 13:920e00014ee3 | 164 | singlylinkedlist_destroy(result->pending_io_list); |
Azure.IoT Build | 0:fa2de1b79154 | 165 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 166 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 167 | } |
Azure.IoT Build | 0:fa2de1b79154 | 168 | else |
Azure.IoT Build | 0:fa2de1b79154 | 169 | { |
Azure.IoT Build | 0:fa2de1b79154 | 170 | strcpy(result->hostname, socket_io_config->hostname); |
Azure.IoT Build | 0:fa2de1b79154 | 171 | result->port = socket_io_config->port; |
Azure.IoT Build | 0:fa2de1b79154 | 172 | result->on_bytes_received = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 173 | result->on_io_error = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 174 | result->on_bytes_received_context = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 175 | result->on_io_error_context = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 176 | result->io_state = IO_STATE_CLOSED; |
Azure.IoT Build | 0:fa2de1b79154 | 177 | result->tcp_socket_connection = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 178 | } |
Azure.IoT Build | 0:fa2de1b79154 | 179 | } |
Azure.IoT Build | 0:fa2de1b79154 | 180 | } |
Azure.IoT Build | 0:fa2de1b79154 | 181 | } |
Azure.IoT Build | 0:fa2de1b79154 | 182 | |
Azure.IoT Build | 0:fa2de1b79154 | 183 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 184 | } |
Azure.IoT Build | 0:fa2de1b79154 | 185 | |
Azure.IoT Build | 0:fa2de1b79154 | 186 | void socketio_destroy(CONCRETE_IO_HANDLE socket_io) |
Azure.IoT Build | 0:fa2de1b79154 | 187 | { |
Azure.IoT Build | 0:fa2de1b79154 | 188 | if (socket_io != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 189 | { |
Azure.IoT Build | 0:fa2de1b79154 | 190 | SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
Azure.IoT Build | 0:fa2de1b79154 | 191 | |
Azure.IoT Build | 0:fa2de1b79154 | 192 | tcpsocketconnection_destroy(socket_io_instance->tcp_socket_connection); |
Azure.IoT Build | 0:fa2de1b79154 | 193 | |
Azure.IoT Build | 0:fa2de1b79154 | 194 | /* clear all pending IOs */ |
AzureIoTClient | 13:920e00014ee3 | 195 | LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
Azure.IoT Build | 0:fa2de1b79154 | 196 | while (first_pending_io != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 197 | { |
AzureIoTClient | 13:920e00014ee3 | 198 | PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
Azure.IoT Build | 0:fa2de1b79154 | 199 | if (pending_socket_io != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 200 | { |
Azure.IoT Build | 0:fa2de1b79154 | 201 | free(pending_socket_io->bytes); |
Azure.IoT Build | 0:fa2de1b79154 | 202 | free(pending_socket_io); |
Azure.IoT Build | 0:fa2de1b79154 | 203 | } |
Azure.IoT Build | 0:fa2de1b79154 | 204 | |
AzureIoTClient | 13:920e00014ee3 | 205 | (void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io); |
AzureIoTClient | 13:920e00014ee3 | 206 | first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
Azure.IoT Build | 0:fa2de1b79154 | 207 | } |
Azure.IoT Build | 0:fa2de1b79154 | 208 | |
AzureIoTClient | 13:920e00014ee3 | 209 | singlylinkedlist_destroy(socket_io_instance->pending_io_list); |
Azure.IoT Build | 0:fa2de1b79154 | 210 | free(socket_io_instance->hostname); |
Azure.IoT Build | 0:fa2de1b79154 | 211 | free(socket_io); |
Azure.IoT Build | 0:fa2de1b79154 | 212 | } |
Azure.IoT Build | 0:fa2de1b79154 | 213 | } |
Azure.IoT Build | 0:fa2de1b79154 | 214 | |
Azure.IoT Build | 0:fa2de1b79154 | 215 | int socketio_open(CONCRETE_IO_HANDLE socket_io, ON_IO_OPEN_COMPLETE on_io_open_complete, void* on_io_open_complete_context, ON_BYTES_RECEIVED on_bytes_received, void* on_bytes_received_context, ON_IO_ERROR on_io_error, void* on_io_error_context) |
Azure.IoT Build | 0:fa2de1b79154 | 216 | { |
Azure.IoT Build | 0:fa2de1b79154 | 217 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 218 | |
Azure.IoT Build | 0:fa2de1b79154 | 219 | SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
Azure.IoT Build | 0:fa2de1b79154 | 220 | if (socket_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 221 | { |
AzureIoTClient | 21:b92006c5b9ff | 222 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 223 | } |
Azure.IoT Build | 0:fa2de1b79154 | 224 | else |
Azure.IoT Build | 0:fa2de1b79154 | 225 | { |
Azure.IoT Build | 0:fa2de1b79154 | 226 | socket_io_instance->tcp_socket_connection = tcpsocketconnection_create(); |
Azure.IoT Build | 0:fa2de1b79154 | 227 | if (socket_io_instance->tcp_socket_connection == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 228 | { |
AzureIoTClient | 21:b92006c5b9ff | 229 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 230 | } |
Azure.IoT Build | 0:fa2de1b79154 | 231 | else |
Azure.IoT Build | 0:fa2de1b79154 | 232 | { |
Azure.IoT Build | 0:fa2de1b79154 | 233 | if (tcpsocketconnection_connect(socket_io_instance->tcp_socket_connection, socket_io_instance->hostname, socket_io_instance->port) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 234 | { |
Azure.IoT Build | 0:fa2de1b79154 | 235 | tcpsocketconnection_destroy(socket_io_instance->tcp_socket_connection); |
Azure.IoT Build | 0:fa2de1b79154 | 236 | socket_io_instance->tcp_socket_connection = NULL; |
AzureIoTClient | 21:b92006c5b9ff | 237 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 238 | } |
Azure.IoT Build | 0:fa2de1b79154 | 239 | else |
Azure.IoT Build | 0:fa2de1b79154 | 240 | { |
Azure.IoT Build | 0:fa2de1b79154 | 241 | tcpsocketconnection_set_blocking(socket_io_instance->tcp_socket_connection, false, 0); |
Azure.IoT Build | 0:fa2de1b79154 | 242 | |
Azure.IoT Build | 0:fa2de1b79154 | 243 | socket_io_instance->on_bytes_received = on_bytes_received; |
Azure.IoT Build | 0:fa2de1b79154 | 244 | socket_io_instance->on_bytes_received_context = on_bytes_received_context; |
Azure.IoT Build | 0:fa2de1b79154 | 245 | |
Azure.IoT Build | 0:fa2de1b79154 | 246 | socket_io_instance->on_io_error = on_io_error; |
Azure.IoT Build | 0:fa2de1b79154 | 247 | socket_io_instance->on_io_error_context = on_io_error_context; |
Azure.IoT Build | 0:fa2de1b79154 | 248 | |
Azure.IoT Build | 0:fa2de1b79154 | 249 | socket_io_instance->io_state = IO_STATE_OPEN; |
Azure.IoT Build | 0:fa2de1b79154 | 250 | |
Azure.IoT Build | 0:fa2de1b79154 | 251 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 252 | } |
Azure.IoT Build | 0:fa2de1b79154 | 253 | } |
Azure.IoT Build | 0:fa2de1b79154 | 254 | } |
AzureIoTClient | 19:2e0811512ceb | 255 | |
AzureIoTClient | 19:2e0811512ceb | 256 | if (on_io_open_complete != NULL) |
AzureIoTClient | 19:2e0811512ceb | 257 | { |
AzureIoTClient | 19:2e0811512ceb | 258 | on_io_open_complete(on_io_open_complete_context, result == 0 ? IO_OPEN_OK : IO_OPEN_ERROR); |
AzureIoTClient | 19:2e0811512ceb | 259 | } |
Azure.IoT Build | 0:fa2de1b79154 | 260 | |
Azure.IoT Build | 0:fa2de1b79154 | 261 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 262 | } |
Azure.IoT Build | 0:fa2de1b79154 | 263 | |
Azure.IoT Build | 0:fa2de1b79154 | 264 | int socketio_close(CONCRETE_IO_HANDLE socket_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) |
Azure.IoT Build | 0:fa2de1b79154 | 265 | { |
Azure.IoT Build | 0:fa2de1b79154 | 266 | int result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 267 | |
Azure.IoT Build | 0:fa2de1b79154 | 268 | if (socket_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 269 | { |
AzureIoTClient | 21:b92006c5b9ff | 270 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 271 | } |
Azure.IoT Build | 0:fa2de1b79154 | 272 | else |
Azure.IoT Build | 0:fa2de1b79154 | 273 | { |
Azure.IoT Build | 0:fa2de1b79154 | 274 | SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
Azure.IoT Build | 0:fa2de1b79154 | 275 | |
Azure.IoT Build | 0:fa2de1b79154 | 276 | if ((socket_io_instance->io_state == IO_STATE_CLOSED) || |
Azure.IoT Build | 0:fa2de1b79154 | 277 | (socket_io_instance->io_state == IO_STATE_CLOSING)) |
Azure.IoT Build | 0:fa2de1b79154 | 278 | { |
AzureIoTClient | 21:b92006c5b9ff | 279 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 280 | } |
Azure.IoT Build | 0:fa2de1b79154 | 281 | else |
Azure.IoT Build | 0:fa2de1b79154 | 282 | { |
Azure.IoT Build | 0:fa2de1b79154 | 283 | tcpsocketconnection_close(socket_io_instance->tcp_socket_connection); |
Azure.IoT Build | 0:fa2de1b79154 | 284 | socket_io_instance->tcp_socket_connection = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 285 | socket_io_instance->io_state = IO_STATE_CLOSED; |
Azure.IoT Build | 0:fa2de1b79154 | 286 | |
Azure.IoT Build | 0:fa2de1b79154 | 287 | if (on_io_close_complete != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 288 | { |
Azure.IoT Build | 0:fa2de1b79154 | 289 | on_io_close_complete(callback_context); |
Azure.IoT Build | 0:fa2de1b79154 | 290 | } |
Azure.IoT Build | 0:fa2de1b79154 | 291 | |
Azure.IoT Build | 0:fa2de1b79154 | 292 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 293 | } |
Azure.IoT Build | 0:fa2de1b79154 | 294 | } |
Azure.IoT Build | 0:fa2de1b79154 | 295 | |
Azure.IoT Build | 0:fa2de1b79154 | 296 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 297 | } |
Azure.IoT Build | 0:fa2de1b79154 | 298 | |
Azure.IoT Build | 0:fa2de1b79154 | 299 | int socketio_send(CONCRETE_IO_HANDLE socket_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
Azure.IoT Build | 0:fa2de1b79154 | 300 | { |
Azure.IoT Build | 0:fa2de1b79154 | 301 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 302 | |
Azure.IoT Build | 0:fa2de1b79154 | 303 | if ((socket_io == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 304 | (buffer == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 305 | (size == 0)) |
Azure.IoT Build | 0:fa2de1b79154 | 306 | { |
Azure.IoT Build | 0:fa2de1b79154 | 307 | /* Invalid arguments */ |
AzureIoTClient | 21:b92006c5b9ff | 308 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 309 | } |
Azure.IoT Build | 0:fa2de1b79154 | 310 | else |
Azure.IoT Build | 0:fa2de1b79154 | 311 | { |
Azure.IoT Build | 0:fa2de1b79154 | 312 | SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
Azure.IoT Build | 0:fa2de1b79154 | 313 | if (socket_io_instance->io_state != IO_STATE_OPEN) |
Azure.IoT Build | 0:fa2de1b79154 | 314 | { |
AzureIoTClient | 21:b92006c5b9ff | 315 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 316 | } |
Azure.IoT Build | 0:fa2de1b79154 | 317 | else |
Azure.IoT Build | 0:fa2de1b79154 | 318 | { |
AzureIoTClient | 13:920e00014ee3 | 319 | LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
Azure.IoT Build | 0:fa2de1b79154 | 320 | if (first_pending_io != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 321 | { |
Azure.IoT Build | 0:fa2de1b79154 | 322 | if (add_pending_io(socket_io_instance, buffer, size, on_send_complete, callback_context) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 323 | { |
AzureIoTClient | 21:b92006c5b9ff | 324 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 325 | } |
Azure.IoT Build | 0:fa2de1b79154 | 326 | else |
Azure.IoT Build | 0:fa2de1b79154 | 327 | { |
Azure.IoT Build | 0:fa2de1b79154 | 328 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 329 | } |
Azure.IoT Build | 0:fa2de1b79154 | 330 | } |
Azure.IoT Build | 0:fa2de1b79154 | 331 | else |
Azure.IoT Build | 0:fa2de1b79154 | 332 | { |
Azure.IoT Build | 0:fa2de1b79154 | 333 | int send_result = tcpsocketconnection_send(socket_io_instance->tcp_socket_connection, buffer, size); |
Azure.IoT Build | 0:fa2de1b79154 | 334 | if (send_result != size) |
Azure.IoT Build | 0:fa2de1b79154 | 335 | { |
Azure.IoT Build | 0:fa2de1b79154 | 336 | if (send_result < 0) |
Azure.IoT Build | 0:fa2de1b79154 | 337 | { |
Azure.IoT Build | 0:fa2de1b79154 | 338 | send_result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 339 | } |
Azure.IoT Build | 0:fa2de1b79154 | 340 | |
Azure.IoT Build | 0:fa2de1b79154 | 341 | /* queue data */ |
Azure.IoT Build | 0:fa2de1b79154 | 342 | if (add_pending_io(socket_io_instance, (unsigned char*)buffer + send_result, size - send_result, on_send_complete, callback_context) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 343 | { |
AzureIoTClient | 21:b92006c5b9ff | 344 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 345 | } |
Azure.IoT Build | 0:fa2de1b79154 | 346 | else |
Azure.IoT Build | 0:fa2de1b79154 | 347 | { |
Azure.IoT Build | 0:fa2de1b79154 | 348 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 349 | } |
Azure.IoT Build | 0:fa2de1b79154 | 350 | } |
Azure.IoT Build | 0:fa2de1b79154 | 351 | else |
Azure.IoT Build | 0:fa2de1b79154 | 352 | { |
Azure.IoT Build | 0:fa2de1b79154 | 353 | if (on_send_complete != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 354 | { |
Azure.IoT Build | 0:fa2de1b79154 | 355 | on_send_complete(callback_context, IO_SEND_OK); |
Azure.IoT Build | 0:fa2de1b79154 | 356 | } |
Azure.IoT Build | 0:fa2de1b79154 | 357 | |
Azure.IoT Build | 0:fa2de1b79154 | 358 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 359 | } |
Azure.IoT Build | 0:fa2de1b79154 | 360 | } |
Azure.IoT Build | 0:fa2de1b79154 | 361 | } |
Azure.IoT Build | 0:fa2de1b79154 | 362 | } |
Azure.IoT Build | 0:fa2de1b79154 | 363 | |
Azure.IoT Build | 0:fa2de1b79154 | 364 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 365 | } |
Azure.IoT Build | 0:fa2de1b79154 | 366 | |
Azure.IoT Build | 0:fa2de1b79154 | 367 | void socketio_dowork(CONCRETE_IO_HANDLE socket_io) |
Azure.IoT Build | 0:fa2de1b79154 | 368 | { |
Azure.IoT Build | 0:fa2de1b79154 | 369 | if (socket_io != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 370 | { |
Azure.IoT Build | 0:fa2de1b79154 | 371 | SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
Azure.IoT Build | 0:fa2de1b79154 | 372 | if (socket_io_instance->io_state == IO_STATE_OPEN) |
Azure.IoT Build | 0:fa2de1b79154 | 373 | { |
Azure.IoT Build | 0:fa2de1b79154 | 374 | int received = 1; |
Azure.IoT Build | 0:fa2de1b79154 | 375 | |
AzureIoTClient | 13:920e00014ee3 | 376 | LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
Azure.IoT Build | 0:fa2de1b79154 | 377 | while (first_pending_io != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 378 | { |
AzureIoTClient | 13:920e00014ee3 | 379 | PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
Azure.IoT Build | 0:fa2de1b79154 | 380 | if (pending_socket_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 381 | { |
Azure.IoT Build | 0:fa2de1b79154 | 382 | socket_io_instance->io_state = IO_STATE_ERROR; |
Azure.IoT Build | 0:fa2de1b79154 | 383 | indicate_error(socket_io_instance); |
Azure.IoT Build | 0:fa2de1b79154 | 384 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 385 | } |
Azure.IoT Build | 0:fa2de1b79154 | 386 | |
Azure.IoT Build | 0:fa2de1b79154 | 387 | int send_result = tcpsocketconnection_send(socket_io_instance->tcp_socket_connection, (const char*)pending_socket_io->bytes, pending_socket_io->size); |
Azure.IoT Build | 0:fa2de1b79154 | 388 | if (send_result != pending_socket_io->size) |
Azure.IoT Build | 0:fa2de1b79154 | 389 | { |
Azure.IoT Build | 0:fa2de1b79154 | 390 | if (send_result < 0) |
Azure.IoT Build | 0:fa2de1b79154 | 391 | { |
Azure.IoT Build | 0:fa2de1b79154 | 392 | if (send_result < UNABLE_TO_COMPLETE) |
Azure.IoT Build | 0:fa2de1b79154 | 393 | { |
Azure.IoT Build | 0:fa2de1b79154 | 394 | // Bad error. Indicate as much. |
Azure.IoT Build | 0:fa2de1b79154 | 395 | socket_io_instance->io_state = IO_STATE_ERROR; |
Azure.IoT Build | 0:fa2de1b79154 | 396 | indicate_error(socket_io_instance); |
Azure.IoT Build | 0:fa2de1b79154 | 397 | } |
Azure.IoT Build | 0:fa2de1b79154 | 398 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 399 | } |
Azure.IoT Build | 0:fa2de1b79154 | 400 | else |
Azure.IoT Build | 0:fa2de1b79154 | 401 | { |
Azure.IoT Build | 0:fa2de1b79154 | 402 | /* send something, wait for the rest */ |
Azure.IoT Build | 0:fa2de1b79154 | 403 | (void)memmove(pending_socket_io->bytes, pending_socket_io->bytes + send_result, pending_socket_io->size - send_result); |
Azure.IoT Build | 0:fa2de1b79154 | 404 | } |
Azure.IoT Build | 0:fa2de1b79154 | 405 | } |
Azure.IoT Build | 0:fa2de1b79154 | 406 | else |
Azure.IoT Build | 0:fa2de1b79154 | 407 | { |
Azure.IoT Build | 0:fa2de1b79154 | 408 | if (pending_socket_io->on_send_complete != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 409 | { |
Azure.IoT Build | 0:fa2de1b79154 | 410 | pending_socket_io->on_send_complete(pending_socket_io->callback_context, IO_SEND_OK); |
Azure.IoT Build | 0:fa2de1b79154 | 411 | } |
Azure.IoT Build | 0:fa2de1b79154 | 412 | |
Azure.IoT Build | 0:fa2de1b79154 | 413 | free(pending_socket_io->bytes); |
Azure.IoT Build | 0:fa2de1b79154 | 414 | free(pending_socket_io); |
AzureIoTClient | 13:920e00014ee3 | 415 | if (singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 416 | { |
Azure.IoT Build | 0:fa2de1b79154 | 417 | socket_io_instance->io_state = IO_STATE_ERROR; |
Azure.IoT Build | 0:fa2de1b79154 | 418 | indicate_error(socket_io_instance); |
Azure.IoT Build | 0:fa2de1b79154 | 419 | } |
Azure.IoT Build | 0:fa2de1b79154 | 420 | } |
Azure.IoT Build | 0:fa2de1b79154 | 421 | |
AzureIoTClient | 13:920e00014ee3 | 422 | first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
Azure.IoT Build | 0:fa2de1b79154 | 423 | } |
Azure.IoT Build | 0:fa2de1b79154 | 424 | |
Azure.IoT Build | 0:fa2de1b79154 | 425 | while (received > 0) |
Azure.IoT Build | 0:fa2de1b79154 | 426 | { |
AzureIoTClient | 1:9190c0f4d23a | 427 | unsigned char* recv_bytes = malloc(MBED_RECEIVE_BYTES_VALUE); |
AzureIoTClient | 1:9190c0f4d23a | 428 | if (recv_bytes == NULL) |
AzureIoTClient | 1:9190c0f4d23a | 429 | { |
AzureIoTClient | 1:9190c0f4d23a | 430 | LogError("Socketio_Failure: NULL allocating input buffer."); |
AzureIoTClient | 1:9190c0f4d23a | 431 | indicate_error(socket_io_instance); |
AzureIoTClient | 1:9190c0f4d23a | 432 | } |
AzureIoTClient | 1:9190c0f4d23a | 433 | else |
Azure.IoT Build | 0:fa2de1b79154 | 434 | { |
AzureIoTClient | 1:9190c0f4d23a | 435 | received = tcpsocketconnection_receive(socket_io_instance->tcp_socket_connection, (char*)recv_bytes, MBED_RECEIVE_BYTES_VALUE); |
AzureIoTClient | 1:9190c0f4d23a | 436 | if (received > 0) |
Azure.IoT Build | 0:fa2de1b79154 | 437 | { |
AzureIoTClient | 1:9190c0f4d23a | 438 | if (socket_io_instance->on_bytes_received != NULL) |
AzureIoTClient | 1:9190c0f4d23a | 439 | { |
AzureIoTClient | 1:9190c0f4d23a | 440 | /* explictly ignoring here the result of the callback */ |
AzureIoTClient | 1:9190c0f4d23a | 441 | (void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, recv_bytes, received); |
AzureIoTClient | 1:9190c0f4d23a | 442 | } |
Azure.IoT Build | 0:fa2de1b79154 | 443 | } |
AzureIoTClient | 1:9190c0f4d23a | 444 | free(recv_bytes); |
Azure.IoT Build | 0:fa2de1b79154 | 445 | } |
Azure.IoT Build | 0:fa2de1b79154 | 446 | } |
Azure.IoT Build | 0:fa2de1b79154 | 447 | } |
Azure.IoT Build | 0:fa2de1b79154 | 448 | } |
Azure.IoT Build | 0:fa2de1b79154 | 449 | } |
Azure.IoT Build | 0:fa2de1b79154 | 450 | |
Azure.IoT Build | 0:fa2de1b79154 | 451 | int socketio_setoption(CONCRETE_IO_HANDLE socket_io, const char* optionName, const void* value) |
Azure.IoT Build | 0:fa2de1b79154 | 452 | { |
Azure.IoT Build | 0:fa2de1b79154 | 453 | /* Not implementing any options */ |
AzureIoTClient | 21:b92006c5b9ff | 454 | return __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 455 | } |
Azure.IoT Build | 0:fa2de1b79154 | 456 | |
Azure.IoT Build | 0:fa2de1b79154 | 457 | const IO_INTERFACE_DESCRIPTION* socketio_get_interface_description(void) |
Azure.IoT Build | 0:fa2de1b79154 | 458 | { |
Azure.IoT Build | 0:fa2de1b79154 | 459 | return &socket_io_interface_description; |
Azure.IoT Build | 0:fa2de1b79154 | 460 | } |