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