Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
Azure.IoT Build
Date:
Fri Jul 01 10:43:23 2016 -0700
Revision:
6:c55b013dfc2a
Parent:
1:9190c0f4d23a
Child:
10:1be0bc9a9deb
1.0.10

Who changed what in which revision?

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