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:
0:fa2de1b79154
Child:
7:1af47e3a19b6
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 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 9 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 10 #include "azure_c_shared_utility/xio.h"
Azure.IoT Build 0:fa2de1b79154 11
Azure.IoT Build 0:fa2de1b79154 12 typedef struct XIO_INSTANCE_TAG
Azure.IoT Build 0:fa2de1b79154 13 {
Azure.IoT Build 0:fa2de1b79154 14 const IO_INTERFACE_DESCRIPTION* io_interface_description;
Azure.IoT Build 0:fa2de1b79154 15 XIO_HANDLE concrete_xio_handle;
Azure.IoT Build 0:fa2de1b79154 16 } XIO_INSTANCE;
Azure.IoT Build 0:fa2de1b79154 17
Azure.IoT Build 6:c55b013dfc2a 18 XIO_HANDLE xio_create(const IO_INTERFACE_DESCRIPTION* io_interface_description, const void* xio_create_parameters)
Azure.IoT Build 0:fa2de1b79154 19 {
Azure.IoT Build 0:fa2de1b79154 20 XIO_INSTANCE* xio_instance;
Azure.IoT Build 0:fa2de1b79154 21 /* Codes_SRS_XIO_01_003: [If the argument io_interface_description is NULL, xio_create shall return NULL.] */
Azure.IoT Build 0:fa2de1b79154 22 if ((io_interface_description == NULL) ||
Azure.IoT Build 0:fa2de1b79154 23 /* Codes_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
Azure.IoT Build 0:fa2de1b79154 24 (io_interface_description->concrete_io_create == NULL) ||
Azure.IoT Build 0:fa2de1b79154 25 (io_interface_description->concrete_io_destroy == NULL) ||
Azure.IoT Build 0:fa2de1b79154 26 (io_interface_description->concrete_io_open == NULL) ||
Azure.IoT Build 0:fa2de1b79154 27 (io_interface_description->concrete_io_close == NULL) ||
Azure.IoT Build 0:fa2de1b79154 28 (io_interface_description->concrete_io_send == NULL) ||
Azure.IoT Build 0:fa2de1b79154 29 (io_interface_description->concrete_io_dowork == NULL) ||
Azure.IoT Build 0:fa2de1b79154 30 (io_interface_description->concrete_io_setoption == NULL))
Azure.IoT Build 0:fa2de1b79154 31 {
Azure.IoT Build 0:fa2de1b79154 32 xio_instance = NULL;
Azure.IoT Build 0:fa2de1b79154 33 }
Azure.IoT Build 0:fa2de1b79154 34 else
Azure.IoT Build 0:fa2de1b79154 35 {
Azure.IoT Build 0:fa2de1b79154 36 xio_instance = (XIO_INSTANCE*)malloc(sizeof(XIO_INSTANCE));
Azure.IoT Build 0:fa2de1b79154 37
Azure.IoT Build 0:fa2de1b79154 38 /* Codes_SRS_XIO_01_017: [If allocating the memory needed for the IO interface fails then xio_create shall return NULL.] */
Azure.IoT Build 0:fa2de1b79154 39 if (xio_instance != NULL)
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 /* Codes_SRS_XIO_01_001: [xio_create shall return on success a non-NULL handle to a new IO interface.] */
Azure.IoT Build 0:fa2de1b79154 42 xio_instance->io_interface_description = io_interface_description;
Azure.IoT Build 0:fa2de1b79154 43
Azure.IoT Build 6:c55b013dfc2a 44 /* Codes_SRS_XIO_01_002: [In order to instantiate the concrete IO implementation the function concrete_io_create from the io_interface_description shall be called, passing the xio_create_parameters argument.] */
Azure.IoT Build 6:c55b013dfc2a 45 xio_instance->concrete_xio_handle = xio_instance->io_interface_description->concrete_io_create((void*)xio_create_parameters);
Azure.IoT Build 0:fa2de1b79154 46
Azure.IoT Build 0:fa2de1b79154 47 /* Codes_SRS_XIO_01_016: [If the underlying concrete_io_create call fails, xio_create shall return NULL.] */
Azure.IoT Build 0:fa2de1b79154 48 if (xio_instance->concrete_xio_handle == NULL)
Azure.IoT Build 0:fa2de1b79154 49 {
Azure.IoT Build 0:fa2de1b79154 50 free(xio_instance);
Azure.IoT Build 0:fa2de1b79154 51 xio_instance = NULL;
Azure.IoT Build 0:fa2de1b79154 52 }
Azure.IoT Build 0:fa2de1b79154 53 }
Azure.IoT Build 0:fa2de1b79154 54 }
Azure.IoT Build 0:fa2de1b79154 55 return (XIO_HANDLE)xio_instance;
Azure.IoT Build 0:fa2de1b79154 56 }
Azure.IoT Build 0:fa2de1b79154 57
Azure.IoT Build 0:fa2de1b79154 58 void xio_destroy(XIO_HANDLE xio)
Azure.IoT Build 0:fa2de1b79154 59 {
Azure.IoT Build 0:fa2de1b79154 60 /* Codes_SRS_XIO_01_007: [If the argument io is NULL, xio_destroy shall do nothing.] */
Azure.IoT Build 0:fa2de1b79154 61 if (xio != NULL)
Azure.IoT Build 0:fa2de1b79154 62 {
Azure.IoT Build 0:fa2de1b79154 63 XIO_INSTANCE* xio_instance = (XIO_INSTANCE*)xio;
Azure.IoT Build 0:fa2de1b79154 64
Azure.IoT Build 0:fa2de1b79154 65 /* Codes_SRS_XIO_01_006: [xio_destroy shall also call the concrete_io_destroy function that is member of the io_interface_description argument passed to xio_create, while passing as argument to concrete_io_destroy the result of the underlying concrete_io_create handle that was called as part of the xio_create call.] */
Azure.IoT Build 0:fa2de1b79154 66 xio_instance->io_interface_description->concrete_io_destroy(xio_instance->concrete_xio_handle);
Azure.IoT Build 0:fa2de1b79154 67
Azure.IoT Build 0:fa2de1b79154 68 /* Codes_SRS_XIO_01_005: [xio_destroy shall free all resources associated with the IO handle.] */
Azure.IoT Build 0:fa2de1b79154 69 free(xio_instance);
Azure.IoT Build 0:fa2de1b79154 70 }
Azure.IoT Build 0:fa2de1b79154 71 }
Azure.IoT Build 0:fa2de1b79154 72
Azure.IoT Build 0:fa2de1b79154 73 int xio_open(XIO_HANDLE xio, 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 74 {
Azure.IoT Build 0:fa2de1b79154 75 int result;
Azure.IoT Build 0:fa2de1b79154 76
Azure.IoT Build 0:fa2de1b79154 77 if (xio == NULL)
Azure.IoT Build 0:fa2de1b79154 78 {
Azure.IoT Build 0:fa2de1b79154 79 /* Codes_SRS_XIO_01_021: [If handle is NULL, xio_open shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 80 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 81 }
Azure.IoT Build 0:fa2de1b79154 82 else
Azure.IoT Build 0:fa2de1b79154 83 {
Azure.IoT Build 0:fa2de1b79154 84 XIO_INSTANCE* xio_instance = (XIO_INSTANCE*)xio;
Azure.IoT Build 0:fa2de1b79154 85
Azure.IoT Build 0:fa2de1b79154 86 /* Codes_SRS_XIO_01_019: [xio_open shall call the specific concrete_xio_open function specified in xio_create, passing callback function and context arguments for three events: open completed, bytes received, and IO error.] */
Azure.IoT Build 0:fa2de1b79154 87 if (xio_instance->io_interface_description->concrete_io_open(xio_instance->concrete_xio_handle, on_io_open_complete, on_io_open_complete_context, on_bytes_received, on_bytes_received_context, on_io_error, on_io_error_context) != 0)
Azure.IoT Build 0:fa2de1b79154 88 {
Azure.IoT Build 0:fa2de1b79154 89 /* Codes_SRS_XIO_01_022: [If the underlying concrete_io_open fails, xio_open shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 90 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 91 }
Azure.IoT Build 0:fa2de1b79154 92 else
Azure.IoT Build 0:fa2de1b79154 93 {
Azure.IoT Build 0:fa2de1b79154 94 /* Codes_SRS_XIO_01_020: [On success, xio_open shall return 0.] */
Azure.IoT Build 0:fa2de1b79154 95 result = 0;
Azure.IoT Build 0:fa2de1b79154 96 }
Azure.IoT Build 0:fa2de1b79154 97 }
Azure.IoT Build 0:fa2de1b79154 98
Azure.IoT Build 0:fa2de1b79154 99 return result;
Azure.IoT Build 0:fa2de1b79154 100 }
Azure.IoT Build 0:fa2de1b79154 101
Azure.IoT Build 0:fa2de1b79154 102 int xio_close(XIO_HANDLE xio, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context)
Azure.IoT Build 0:fa2de1b79154 103 {
Azure.IoT Build 0:fa2de1b79154 104 int result;
Azure.IoT Build 0:fa2de1b79154 105
Azure.IoT Build 0:fa2de1b79154 106 if (xio == NULL)
Azure.IoT Build 0:fa2de1b79154 107 {
Azure.IoT Build 0:fa2de1b79154 108 /* Codes_SRS_XIO_01_025: [If handle is NULL, xio_close shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 109 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 110 }
Azure.IoT Build 0:fa2de1b79154 111 else
Azure.IoT Build 0:fa2de1b79154 112 {
Azure.IoT Build 0:fa2de1b79154 113 XIO_INSTANCE* xio_instance = (XIO_INSTANCE*)xio;
Azure.IoT Build 0:fa2de1b79154 114
Azure.IoT Build 0:fa2de1b79154 115 /* Codes_SRS_XIO_01_023: [xio_close shall call the specific concrete_io_close function specified in xio_create.] */
Azure.IoT Build 0:fa2de1b79154 116 if (xio_instance->io_interface_description->concrete_io_close(xio_instance->concrete_xio_handle, on_io_close_complete, callback_context) != 0)
Azure.IoT Build 0:fa2de1b79154 117 {
Azure.IoT Build 0:fa2de1b79154 118 /* Codes_SRS_XIO_01_026: [If the underlying concrete_io_close fails, xio_close shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 119 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 120 }
Azure.IoT Build 0:fa2de1b79154 121 else
Azure.IoT Build 0:fa2de1b79154 122 {
Azure.IoT Build 0:fa2de1b79154 123 /* Codes_SRS_XIO_01_024: [On success, xio_close shall return 0.] */
Azure.IoT Build 0:fa2de1b79154 124 result = 0;
Azure.IoT Build 0:fa2de1b79154 125 }
Azure.IoT Build 0:fa2de1b79154 126 }
Azure.IoT Build 0:fa2de1b79154 127
Azure.IoT Build 0:fa2de1b79154 128 return result;
Azure.IoT Build 0:fa2de1b79154 129 }
Azure.IoT Build 0:fa2de1b79154 130
Azure.IoT Build 0:fa2de1b79154 131 int xio_send(XIO_HANDLE xio, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
Azure.IoT Build 0:fa2de1b79154 132 {
Azure.IoT Build 0:fa2de1b79154 133 int result;
Azure.IoT Build 0:fa2de1b79154 134
Azure.IoT Build 0:fa2de1b79154 135 /* Codes_SRS_XIO_01_011: [No error check shall be performed on buffer and size.] */
Azure.IoT Build 0:fa2de1b79154 136 /* Codes_SRS_XIO_01_010: [If handle is NULL, xio_send shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 137 if (xio == NULL)
Azure.IoT Build 0:fa2de1b79154 138 {
Azure.IoT Build 0:fa2de1b79154 139 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 140 }
Azure.IoT Build 0:fa2de1b79154 141 else
Azure.IoT Build 0:fa2de1b79154 142 {
Azure.IoT Build 0:fa2de1b79154 143 XIO_INSTANCE* xio_instance = (XIO_INSTANCE*)xio;
Azure.IoT Build 0:fa2de1b79154 144
Azure.IoT Build 0:fa2de1b79154 145 /* Codes_SRS_XIO_01_008: [xio_send shall pass the sequence of bytes pointed to by buffer to the concrete IO implementation specified in xio_create, by calling the concrete_io_send function while passing down the buffer and size arguments to it.] */
Azure.IoT Build 0:fa2de1b79154 146 /* Codes_SRS_XIO_01_009: [On success, xio_send shall return 0.] */
Azure.IoT Build 0:fa2de1b79154 147 /* Codes_SRS_XIO_01_015: [If the underlying concrete_io_send fails, xio_send shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 148 /* Codes_SRS_XIO_01_027: [xio_send shall pass to the concrete_io_send function the on_send_complete and callback_context arguments.] */
Azure.IoT Build 0:fa2de1b79154 149 result = xio_instance->io_interface_description->concrete_io_send(xio_instance->concrete_xio_handle, buffer, size, on_send_complete, callback_context);
Azure.IoT Build 0:fa2de1b79154 150 }
Azure.IoT Build 0:fa2de1b79154 151
Azure.IoT Build 0:fa2de1b79154 152 return result;
Azure.IoT Build 0:fa2de1b79154 153 }
Azure.IoT Build 0:fa2de1b79154 154
Azure.IoT Build 0:fa2de1b79154 155 void xio_dowork(XIO_HANDLE xio)
Azure.IoT Build 0:fa2de1b79154 156 {
Azure.IoT Build 0:fa2de1b79154 157 /* Codes_SRS_XIO_01_018: [When the handle argument is NULL, xio_dowork shall do nothing.] */
Azure.IoT Build 0:fa2de1b79154 158 if (xio != NULL)
Azure.IoT Build 0:fa2de1b79154 159 {
Azure.IoT Build 0:fa2de1b79154 160 XIO_INSTANCE* xio_instance = (XIO_INSTANCE*)xio;
Azure.IoT Build 0:fa2de1b79154 161
Azure.IoT Build 0:fa2de1b79154 162 /* Codes_SRS_XIO_01_012: [xio_dowork shall call the concrete XIO implementation specified in xio_create, by calling the concrete_io_dowork function.] */
Azure.IoT Build 0:fa2de1b79154 163 xio_instance->io_interface_description->concrete_io_dowork(xio_instance->concrete_xio_handle);
Azure.IoT Build 0:fa2de1b79154 164 }
Azure.IoT Build 0:fa2de1b79154 165 }
Azure.IoT Build 0:fa2de1b79154 166
Azure.IoT Build 0:fa2de1b79154 167 int xio_setoption(XIO_HANDLE xio, const char* optionName, const void* value)
Azure.IoT Build 0:fa2de1b79154 168 {
Azure.IoT Build 0:fa2de1b79154 169 int result;
Azure.IoT Build 0:fa2de1b79154 170
Azure.IoT Build 0:fa2de1b79154 171 /* Codes_SRS_XIO_03_030: [If the xio argumnent or the optionName argument is NULL, xio_setoption shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 172 if (xio == NULL || optionName == NULL)
Azure.IoT Build 0:fa2de1b79154 173 {
Azure.IoT Build 0:fa2de1b79154 174 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 175 }
Azure.IoT Build 0:fa2de1b79154 176 else
Azure.IoT Build 0:fa2de1b79154 177 {
Azure.IoT Build 0:fa2de1b79154 178 XIO_INSTANCE* xio_instance = (XIO_INSTANCE*)xio;
Azure.IoT Build 0:fa2de1b79154 179
Azure.IoT Build 0:fa2de1b79154 180 /* Codes_SRS_XIO_003_028: [xio_setoption shall pass the optionName and value to the concrete IO implementation specified in xio_create by invoking the concrete_xio_setoption function.] */
Azure.IoT Build 0:fa2de1b79154 181 /* Codes_SRS_XIO_03_029: [xio_setoption shall return 0 upon success.] */
Azure.IoT Build 0:fa2de1b79154 182 /* Codes_SRS_XIO_03_031: [If the underlying concrete_xio_setoption fails, xio_setOption shall return a non-zero value.] */
Azure.IoT Build 0:fa2de1b79154 183 result = xio_instance->io_interface_description->concrete_io_setoption(xio_instance->concrete_xio_handle, optionName, value);
Azure.IoT Build 0:fa2de1b79154 184 }
Azure.IoT Build 0:fa2de1b79154 185
Azure.IoT Build 0:fa2de1b79154 186 return result;
Azure.IoT Build 0:fa2de1b79154 187 }