Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Sun Apr 24 16:41:14 2016 -0700
Revision:
1:9190c0f4d23a
Parent:
0:fa2de1b79154
Child:
6:c55b013dfc2a
1.0.5

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