| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stddef.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#include "azure_c_shared_utility/socketio.h" |
| 9 |
#include "azure_c_shared_utility/singlylinkedlist.h" |
| 10 |
#include "azure_c_shared_utility/tcpsocketconnection_c.h" |
| 11 |
#include "azure_c_shared_utility/optimize_size.h" |
| 12 |
#include "azure_c_shared_utility/xlogging.h" |
| 13 |
#include "netsocket/nsapi_types.h" |
| 14 |
|
| 15 |
#define MBED_XIO_RECEIVE_BUFFER_SIZE 128 |
| 16 |
|
| 17 |
typedef enum IO_STATE_TAG |
| 18 |
{ |
| 19 |
IO_STATE_CLOSED, |
| 20 |
IO_STATE_OPENING, |
| 21 |
IO_STATE_OPEN, |
| 22 |
IO_STATE_CLOSING, |
| 23 |
IO_STATE_ERROR |
| 24 |
} IO_STATE; |
| 25 |
|
| 26 |
typedef struct PENDING_SOCKET_IO_TAG |
| 27 |
{ |
| 28 |
unsigned char* bytes; |
| 29 |
size_t size; |
| 30 |
ON_SEND_COMPLETE on_send_complete; |
| 31 |
void* callback_context; |
| 32 |
SINGLYLINKEDLIST_HANDLE pending_io_list; |
| 33 |
} PENDING_SOCKET_IO; |
| 34 |
|
| 35 |
typedef struct SOCKET_IO_INSTANCE_TAG |
| 36 |
{ |
| 37 |
TCPSOCKETCONNECTION_HANDLE tcp_socket_connection; |
| 38 |
ON_BYTES_RECEIVED on_bytes_received; |
| 39 |
ON_IO_ERROR on_io_error; |
| 40 |
void* on_bytes_received_context; |
| 41 |
void* on_io_error_context; |
| 42 |
char* hostname; |
| 43 |
int port; |
| 44 |
IO_STATE io_state; |
| 45 |
SINGLYLINKEDLIST_HANDLE pending_io_list; |
| 46 |
} SOCKET_IO_INSTANCE; |
| 47 |
|
| 48 |
/*this function will clone an option given by name and value*/ |
| 49 |
static void* socketio_CloneOption(const char* name, const void* value) |
| 50 |
{ |
| 51 |
(void)name; |
| 52 |
(void)value; |
| 53 |
return NULL; |
| 54 |
} |
| 55 |
|
| 56 |
/*this function destroys an option previously created*/ |
| 57 |
static void socketio_DestroyOption(const char* name, const void* value) |
| 58 |
{ |
| 59 |
(void)name; |
| 60 |
(void)value; |
| 61 |
} |
| 62 |
|
| 63 |
static OPTIONHANDLER_HANDLE socketio_retrieveoptions(CONCRETE_IO_HANDLE socket_io) |
| 64 |
{ |
| 65 |
OPTIONHANDLER_HANDLE result; |
| 66 |
(void)socket_io; |
| 67 |
result = OptionHandler_Create(socketio_CloneOption, socketio_DestroyOption, socketio_setoption); |
| 68 |
if (result == NULL) |
| 69 |
{ |
| 70 |
/*return as is*/ |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
/*insert here work to add the options to "result" handle*/ |
| 75 |
} |
| 76 |
return result; |
| 77 |
} |
| 78 |
|
| 79 |
static const IO_INTERFACE_DESCRIPTION socket_io_interface_description = |
| 80 |
{ |
| 81 |
socketio_retrieveoptions, |
| 82 |
socketio_create, |
| 83 |
socketio_destroy, |
| 84 |
socketio_open, |
| 85 |
socketio_close, |
| 86 |
socketio_send, |
| 87 |
socketio_dowork, |
| 88 |
socketio_setoption |
| 89 |
}; |
| 90 |
|
| 91 |
static void indicate_error(SOCKET_IO_INSTANCE* socket_io_instance) |
| 92 |
{ |
| 93 |
if ((socket_io_instance->io_state == IO_STATE_CLOSED) |
| 94 |
|| (socket_io_instance->io_state == IO_STATE_ERROR)) |
| 95 |
{ |
| 96 |
return; |
| 97 |
} |
| 98 |
socket_io_instance->io_state = IO_STATE_ERROR; |
| 99 |
if (socket_io_instance->on_io_error != NULL) |
| 100 |
{ |
| 101 |
socket_io_instance->on_io_error(socket_io_instance->on_io_error_context); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
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) |
| 106 |
{ |
| 107 |
int result; |
| 108 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)malloc(sizeof(PENDING_SOCKET_IO)); |
| 109 |
if (pending_socket_io == NULL) |
| 110 |
{ |
| 111 |
result = MU_FAILURE; |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
pending_socket_io->bytes = (unsigned char*)malloc(size); |
| 116 |
if (pending_socket_io->bytes == NULL) |
| 117 |
{ |
| 118 |
free(pending_socket_io); |
| 119 |
result = MU_FAILURE; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
pending_socket_io->size = size; |
| 124 |
pending_socket_io->on_send_complete = on_send_complete; |
| 125 |
pending_socket_io->callback_context = callback_context; |
| 126 |
pending_socket_io->pending_io_list = socket_io_instance->pending_io_list; |
| 127 |
(void)memcpy(pending_socket_io->bytes, buffer, size); |
| 128 |
if (singlylinkedlist_add(socket_io_instance->pending_io_list, pending_socket_io) == NULL) |
| 129 |
{ |
| 130 |
free(pending_socket_io->bytes); |
| 131 |
free(pending_socket_io); |
| 132 |
result = MU_FAILURE; |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
result = 0; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return result; |
| 142 |
} |
| 143 |
|
| 144 |
static int retrieve_data(SOCKET_IO_INSTANCE* socket_io_instance) |
| 145 |
{ |
| 146 |
int received = 1; |
| 147 |
int total_received = 0; |
| 148 |
|
| 149 |
unsigned char* recv_bytes = malloc(MBED_XIO_RECEIVE_BUFFER_SIZE); |
| 150 |
if (recv_bytes == NULL) |
| 151 |
{ |
| 152 |
LogError("Socketio_Failure: NULL allocating input buffer."); |
| 153 |
indicate_error(socket_io_instance); |
| 154 |
return -1; |
| 155 |
} |
| 156 |
|
| 157 |
while (received > 0) |
| 158 |
{ |
| 159 |
/* Codes_SRS_SOCKETIO_MBED_OS5_99_005: [ retrieve_data shall succeed if tcp receive bytes succeed ]*/ |
| 160 |
received = tcpsocketconnection_receive(socket_io_instance->tcp_socket_connection, (char*)recv_bytes, MBED_XIO_RECEIVE_BUFFER_SIZE); |
| 161 |
if (received > 0) |
| 162 |
{ |
| 163 |
total_received += received; |
| 164 |
if (socket_io_instance->on_bytes_received != NULL) |
| 165 |
{ |
| 166 |
/* explictly ignoring here the result of the callback */ |
| 167 |
socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, recv_bytes, received); |
| 168 |
} |
| 169 |
} |
| 170 |
else if (received < 0) |
| 171 |
{ |
| 172 |
if(received != NSAPI_ERROR_WOULD_BLOCK) // NSAPI_ERROR_WOULD_BLOCK is not a real error but pending. |
| 173 |
{ |
| 174 |
indicate_error(socket_io_instance); |
| 175 |
LogError("Socketio_Failure: underlying IO error %d.", received); |
| 176 |
free(recv_bytes); |
| 177 |
return -1; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
free(recv_bytes); |
| 182 |
|
| 183 |
return total_received; |
| 184 |
} |
| 185 |
|
| 186 |
static int send_queued_data(SOCKET_IO_INSTANCE* socket_io_instance) |
| 187 |
{ |
| 188 |
int errors = 0; |
| 189 |
int sent = 0; |
| 190 |
|
| 191 |
LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 192 |
while (first_pending_io != NULL) |
| 193 |
{ |
| 194 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
| 195 |
if (pending_socket_io == NULL) |
| 196 |
{ |
| 197 |
indicate_error(socket_io_instance); |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
int send_result = tcpsocketconnection_send(socket_io_instance->tcp_socket_connection, (const char*)pending_socket_io->bytes, pending_socket_io->size); |
| 202 |
if (send_result != (int)pending_socket_io->size) |
| 203 |
{ |
| 204 |
if (send_result == 0) |
| 205 |
{ |
| 206 |
// The underlying network layer may encounter hardware / environment issues, |
| 207 |
// but the driver doesn't handle it properly. So here the send API always return 0, |
| 208 |
// this causes the program running into dead loop if not check it here. |
| 209 |
if (errors++ >= 10) |
| 210 |
{ |
| 211 |
// Treat it as a network error after try 10 times. |
| 212 |
LogError("Socketio_Failure: encountered unknow connection issue, the connection will be restarted."); |
| 213 |
indicate_error(socket_io_instance); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
wait_ms(10); |
| 217 |
} |
| 218 |
else if (send_result < 0) |
| 219 |
{ |
| 220 |
indicate_error(socket_io_instance); |
| 221 |
return -1; |
| 222 |
} |
| 223 |
else |
| 224 |
{ |
| 225 |
/* send something, wait for the rest */ |
| 226 |
memmove(pending_socket_io->bytes, pending_socket_io->bytes + send_result, pending_socket_io->size - send_result); |
| 227 |
sent += send_result; |
| 228 |
} |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
sent += send_result; |
| 233 |
if (pending_socket_io->on_send_complete != NULL) |
| 234 |
{ |
| 235 |
pending_socket_io->on_send_complete(pending_socket_io->callback_context, IO_SEND_OK); |
| 236 |
} |
| 237 |
|
| 238 |
free(pending_socket_io->bytes); |
| 239 |
free(pending_socket_io); |
| 240 |
if (singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io) != 0) |
| 241 |
{ |
| 242 |
indicate_error(socket_io_instance); |
| 243 |
return -1; |
| 244 |
} |
| 245 |
errors = 0; |
| 246 |
} |
| 247 |
|
| 248 |
first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 249 |
} |
| 250 |
|
| 251 |
return sent; |
| 252 |
} |
| 253 |
|
| 254 |
static void close_tcp_connection(SOCKET_IO_INSTANCE* socket_io_instance) |
| 255 |
{ |
| 256 |
if (socket_io_instance->io_state != IO_STATE_CLOSED) |
| 257 |
{ |
| 258 |
if (socket_io_instance->tcp_socket_connection != NULL) |
| 259 |
{ |
| 260 |
tcpsocketconnection_close(socket_io_instance->tcp_socket_connection); |
| 261 |
tcpsocketconnection_destroy(socket_io_instance->tcp_socket_connection); |
| 262 |
socket_io_instance->tcp_socket_connection = NULL; |
| 263 |
} |
| 264 |
socket_io_instance->io_state = IO_STATE_CLOSED; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
CONCRETE_IO_HANDLE socketio_create(void* io_create_parameters) |
| 269 |
{ |
| 270 |
SOCKETIO_CONFIG* socket_io_config = io_create_parameters; |
| 271 |
SOCKET_IO_INSTANCE* result; |
| 272 |
|
| 273 |
if (socket_io_config == NULL) |
| 274 |
{ |
| 275 |
result = NULL; |
| 276 |
} |
| 277 |
else |
| 278 |
{ |
| 279 |
result = (SOCKET_IO_INSTANCE*)malloc(sizeof(SOCKET_IO_INSTANCE)); |
| 280 |
if (result != NULL) |
| 281 |
{ |
| 282 |
result->pending_io_list = singlylinkedlist_create(); |
| 283 |
if (result->pending_io_list == NULL) |
| 284 |
{ |
| 285 |
free(result); |
| 286 |
result = NULL; |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
result->hostname = strdup(socket_io_config->hostname); |
| 291 |
if (result->hostname == NULL) |
| 292 |
{ |
| 293 |
singlylinkedlist_destroy(result->pending_io_list); |
| 294 |
free(result); |
| 295 |
result = NULL; |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
result->port = socket_io_config->port; |
| 300 |
result->on_bytes_received = NULL; |
| 301 |
result->on_io_error = NULL; |
| 302 |
result->on_bytes_received_context = NULL; |
| 303 |
result->on_io_error_context = NULL; |
| 304 |
result->io_state = IO_STATE_CLOSED; |
| 305 |
result->tcp_socket_connection = NULL; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
return result; |
| 312 |
} |
| 313 |
|
| 314 |
void socketio_destroy(CONCRETE_IO_HANDLE socket_io) |
| 315 |
{ |
| 316 |
if (socket_io != NULL) |
| 317 |
{ |
| 318 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 319 |
|
| 320 |
// Close the tcp connection |
| 321 |
close_tcp_connection(socket_io_instance); |
| 322 |
|
| 323 |
// Clear all pending IOs |
| 324 |
LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 325 |
while (first_pending_io != NULL) |
| 326 |
{ |
| 327 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
| 328 |
if (pending_socket_io != NULL) |
| 329 |
{ |
| 330 |
free(pending_socket_io->bytes); |
| 331 |
free(pending_socket_io); |
| 332 |
} |
| 333 |
|
| 334 |
(void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io); |
| 335 |
first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 336 |
} |
| 337 |
singlylinkedlist_destroy(socket_io_instance->pending_io_list); |
| 338 |
|
| 339 |
if(socket_io_instance->hostname != NULL) |
| 340 |
{ |
| 341 |
free(socket_io_instance->hostname); |
| 342 |
socket_io_instance->hostname = NULL; |
| 343 |
} |
| 344 |
free(socket_io); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
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) |
| 349 |
{ |
| 350 |
int result; |
| 351 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 352 |
|
| 353 |
|
| 354 |
if (socket_io_instance == NULL || |
| 355 |
socket_io_instance->io_state != IO_STATE_CLOSED) |
| 356 |
{ |
| 357 |
result = MU_FAILURE; |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
socket_io_instance->tcp_socket_connection = tcpsocketconnection_create(); |
| 362 |
if (socket_io_instance->tcp_socket_connection == NULL) |
| 363 |
{ |
| 364 |
result = MU_FAILURE; |
| 365 |
} |
| 366 |
else |
| 367 |
{ |
| 368 |
if (tcpsocketconnection_connect(socket_io_instance->tcp_socket_connection, socket_io_instance->hostname, socket_io_instance->port) != 0) |
| 369 |
{ |
| 370 |
tcpsocketconnection_destroy(socket_io_instance->tcp_socket_connection); |
| 371 |
socket_io_instance->tcp_socket_connection = NULL; |
| 372 |
result = MU_FAILURE; |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
tcpsocketconnection_set_blocking(socket_io_instance->tcp_socket_connection, false, 0); |
| 377 |
|
| 378 |
socket_io_instance->on_bytes_received = on_bytes_received; |
| 379 |
socket_io_instance->on_bytes_received_context = on_bytes_received_context; |
| 380 |
|
| 381 |
socket_io_instance->on_io_error = on_io_error; |
| 382 |
socket_io_instance->on_io_error_context = on_io_error_context; |
| 383 |
|
| 384 |
socket_io_instance->io_state = IO_STATE_OPEN; |
| 385 |
|
| 386 |
result = 0; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
if (on_io_open_complete != NULL) |
| 392 |
{ |
| 393 |
on_io_open_complete(on_io_open_complete_context, result == 0 ? IO_OPEN_OK : IO_OPEN_ERROR); |
| 394 |
} |
| 395 |
|
| 396 |
return result; |
| 397 |
} |
| 398 |
|
| 399 |
int socketio_close(CONCRETE_IO_HANDLE socket_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) |
| 400 |
{ |
| 401 |
int result = 0; |
| 402 |
|
| 403 |
if (socket_io == NULL) |
| 404 |
{ |
| 405 |
result = MU_FAILURE; |
| 406 |
} |
| 407 |
else |
| 408 |
{ |
| 409 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 410 |
if (socket_io_instance->io_state == IO_STATE_CLOSED || |
| 411 |
socket_io_instance->io_state == IO_STATE_ERROR) |
| 412 |
{ |
| 413 |
result = MU_FAILURE; |
| 414 |
} |
| 415 |
else |
| 416 |
{ |
| 417 |
close_tcp_connection(socket_io_instance); |
| 418 |
if (on_io_close_complete != NULL) |
| 419 |
{ |
| 420 |
on_io_close_complete(callback_context); |
| 421 |
} |
| 422 |
result = 0; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
return result; |
| 427 |
} |
| 428 |
|
| 429 |
int socketio_send(CONCRETE_IO_HANDLE socket_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
| 430 |
{ |
| 431 |
int result; |
| 432 |
|
| 433 |
if ((socket_io == NULL) || |
| 434 |
(buffer == NULL) || |
| 435 |
(size == 0)) |
| 436 |
{ |
| 437 |
/* Invalid arguments */ |
| 438 |
result = MU_FAILURE; |
| 439 |
} |
| 440 |
else |
| 441 |
{ |
| 442 |
result = 0; |
| 443 |
|
| 444 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 445 |
if (socket_io_instance->io_state != IO_STATE_OPEN) |
| 446 |
{ |
| 447 |
result = MU_FAILURE; |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
// Queue the data, and the socketio_dowork sends the package later |
| 452 |
if (add_pending_io(socket_io_instance, buffer, size, on_send_complete, callback_context) != 0) |
| 453 |
{ |
| 454 |
result = MU_FAILURE; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
return result; |
| 460 |
} |
| 461 |
|
| 462 |
void socketio_dowork(CONCRETE_IO_HANDLE socket_io) |
| 463 |
{ |
| 464 |
if (socket_io != NULL) |
| 465 |
{ |
| 466 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 467 |
if (socket_io_instance->io_state == IO_STATE_OPEN) |
| 468 |
{ |
| 469 |
// Retrieve all data from IoT Hub |
| 470 |
if (retrieve_data(socket_io_instance) < 0) |
| 471 |
{ |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
// Send all packages in the queue |
| 476 |
send_queued_data(socket_io_instance); |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
int socketio_setoption(CONCRETE_IO_HANDLE socket_io, const char* optionName, const void* value) |
| 482 |
{ |
| 483 |
/* Not implementing any options, do nothing */ |
| 484 |
return OPTIONHANDLER_OK; |
| 485 |
} |
| 486 |
|
| 487 |
const IO_INTERFACE_DESCRIPTION* socketio_get_interface_description(void) |
| 488 |
{ |
| 489 |
return &socket_io_interface_description; |
| 490 |
} |