A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
header_detect_io.c@28:add19eb7defa, 2017-06-30 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jun 30 10:41:22 2017 -0700
- Revision:
- 28:add19eb7defa
- Parent:
- 27:d74f1cea23e1
- Child:
- 29:4a11413cf217
1.1.18
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:6ae2f7bca550 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:6ae2f7bca550 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:6ae2f7bca550 | 3 | |
AzureIoTClient | 21:f9c433d8e6ca | 4 | #include <stdlib.h> |
AzureIoTClient | 19:000ab4e6a2c1 | 5 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 21:f9c433d8e6ca | 6 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 27:d74f1cea23e1 | 7 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 8 | #include "azure_uamqp_c/header_detect_io.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | typedef enum IO_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | { |
AzureIoTClient | 28:add19eb7defa | 12 | IO_STATE_NOT_OPEN, |
AzureIoTClient | 28:add19eb7defa | 13 | IO_STATE_OPENING_UNDERLYING_IO, |
AzureIoTClient | 28:add19eb7defa | 14 | IO_STATE_WAIT_FOR_HEADER, |
AzureIoTClient | 28:add19eb7defa | 15 | IO_STATE_OPEN, |
AzureIoTClient | 28:add19eb7defa | 16 | IO_STATE_CLOSING, |
AzureIoTClient | 28:add19eb7defa | 17 | IO_STATE_ERROR |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | } IO_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | typedef struct HEADER_DETECT_IO_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | { |
AzureIoTClient | 28:add19eb7defa | 22 | XIO_HANDLE underlying_io; |
AzureIoTClient | 28:add19eb7defa | 23 | size_t header_pos; |
AzureIoTClient | 28:add19eb7defa | 24 | IO_STATE io_state; |
AzureIoTClient | 28:add19eb7defa | 25 | ON_IO_OPEN_COMPLETE on_io_open_complete; |
AzureIoTClient | 28:add19eb7defa | 26 | ON_IO_CLOSE_COMPLETE on_io_close_complete; |
AzureIoTClient | 28:add19eb7defa | 27 | ON_IO_ERROR on_io_error; |
AzureIoTClient | 28:add19eb7defa | 28 | ON_BYTES_RECEIVED on_bytes_received; |
AzureIoTClient | 28:add19eb7defa | 29 | void* on_io_open_complete_context; |
AzureIoTClient | 28:add19eb7defa | 30 | void* on_io_close_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | void* on_io_error_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | void* on_bytes_received_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 33 | } HEADER_DETECT_IO_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 34 | |
Azure.IoT Build | 0:6ae2f7bca550 | 35 | static const unsigned char amqp_header[] = { 'A', 'M', 'Q', 'P', 0, 1, 0, 0 }; |
Azure.IoT Build | 0:6ae2f7bca550 | 36 | |
Azure.IoT Build | 0:6ae2f7bca550 | 37 | static void indicate_error(HEADER_DETECT_IO_INSTANCE* header_detect_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 38 | { |
AzureIoTClient | 28:add19eb7defa | 39 | if (header_detect_io_instance->on_io_error != NULL) |
AzureIoTClient | 28:add19eb7defa | 40 | { |
AzureIoTClient | 28:add19eb7defa | 41 | header_detect_io_instance->on_io_error(header_detect_io_instance->on_io_error_context); |
AzureIoTClient | 28:add19eb7defa | 42 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 43 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 44 | |
Azure.IoT Build | 0:6ae2f7bca550 | 45 | static void indicate_open_complete(HEADER_DETECT_IO_INSTANCE* header_detect_io_instance, IO_OPEN_RESULT open_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 46 | { |
AzureIoTClient | 28:add19eb7defa | 47 | if (header_detect_io_instance->on_io_open_complete != NULL) |
AzureIoTClient | 28:add19eb7defa | 48 | { |
AzureIoTClient | 28:add19eb7defa | 49 | header_detect_io_instance->on_io_open_complete(header_detect_io_instance->on_io_open_complete_context, open_result); |
AzureIoTClient | 28:add19eb7defa | 50 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 51 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 52 | |
Azure.IoT Build | 0:6ae2f7bca550 | 53 | static void indicate_close_complete(HEADER_DETECT_IO_INSTANCE* header_detect_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 55 | if (header_detect_io_instance->on_io_close_complete != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 56 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 57 | header_detect_io_instance->on_io_close_complete(header_detect_io_instance->on_io_close_complete_context); |
Azure.IoT Build | 0:6ae2f7bca550 | 58 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | static void on_underlying_io_error(void* context); |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | static void on_send_complete_close(void* context, IO_SEND_RESULT send_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 63 | { |
AzureIoTClient | 6:641a9672db08 | 64 | (void)send_result; |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | on_underlying_io_error(context); |
Azure.IoT Build | 0:6ae2f7bca550 | 66 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 67 | |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | static void on_underlying_io_bytes_received(void* context, const unsigned char* buffer, size_t size) |
Azure.IoT Build | 0:6ae2f7bca550 | 69 | { |
AzureIoTClient | 28:add19eb7defa | 70 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 71 | |
AzureIoTClient | 28:add19eb7defa | 72 | while (size > 0) |
AzureIoTClient | 28:add19eb7defa | 73 | { |
AzureIoTClient | 28:add19eb7defa | 74 | switch (header_detect_io_instance->io_state) |
AzureIoTClient | 28:add19eb7defa | 75 | { |
AzureIoTClient | 28:add19eb7defa | 76 | default: |
AzureIoTClient | 28:add19eb7defa | 77 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | |
AzureIoTClient | 28:add19eb7defa | 79 | case IO_STATE_WAIT_FOR_HEADER: |
AzureIoTClient | 28:add19eb7defa | 80 | if (amqp_header[header_detect_io_instance->header_pos] != buffer[0]) |
AzureIoTClient | 28:add19eb7defa | 81 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 82 | /* Send expected header, then close as per spec. We do not care if we fail */ |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | (void)xio_send(header_detect_io_instance->underlying_io, amqp_header, sizeof(amqp_header), on_send_complete_close, context); |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 86 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 28:add19eb7defa | 87 | size = 0; |
AzureIoTClient | 28:add19eb7defa | 88 | } |
AzureIoTClient | 28:add19eb7defa | 89 | else |
AzureIoTClient | 28:add19eb7defa | 90 | { |
AzureIoTClient | 28:add19eb7defa | 91 | header_detect_io_instance->header_pos++; |
AzureIoTClient | 28:add19eb7defa | 92 | size--; |
AzureIoTClient | 28:add19eb7defa | 93 | buffer++; |
AzureIoTClient | 28:add19eb7defa | 94 | if (header_detect_io_instance->header_pos == sizeof(amqp_header)) |
AzureIoTClient | 28:add19eb7defa | 95 | { |
AzureIoTClient | 28:add19eb7defa | 96 | if (xio_send(header_detect_io_instance->underlying_io, amqp_header, sizeof(amqp_header), NULL, NULL) != 0) |
AzureIoTClient | 28:add19eb7defa | 97 | { |
AzureIoTClient | 28:add19eb7defa | 98 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 99 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 28:add19eb7defa | 100 | } |
AzureIoTClient | 28:add19eb7defa | 101 | else |
AzureIoTClient | 28:add19eb7defa | 102 | { |
AzureIoTClient | 28:add19eb7defa | 103 | header_detect_io_instance->io_state = IO_STATE_OPEN; |
AzureIoTClient | 28:add19eb7defa | 104 | indicate_open_complete(header_detect_io_instance, IO_OPEN_OK); |
AzureIoTClient | 28:add19eb7defa | 105 | } |
AzureIoTClient | 28:add19eb7defa | 106 | } |
AzureIoTClient | 28:add19eb7defa | 107 | } |
AzureIoTClient | 28:add19eb7defa | 108 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 109 | |
AzureIoTClient | 28:add19eb7defa | 110 | case IO_STATE_OPEN: |
AzureIoTClient | 28:add19eb7defa | 111 | header_detect_io_instance->on_bytes_received(header_detect_io_instance->on_bytes_received_context, buffer, size); |
AzureIoTClient | 28:add19eb7defa | 112 | size = 0; |
AzureIoTClient | 28:add19eb7defa | 113 | break; |
AzureIoTClient | 28:add19eb7defa | 114 | } |
AzureIoTClient | 28:add19eb7defa | 115 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 116 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 117 | |
Azure.IoT Build | 0:6ae2f7bca550 | 118 | static void on_underlying_io_close_complete(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 119 | { |
AzureIoTClient | 28:add19eb7defa | 120 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 121 | |
AzureIoTClient | 28:add19eb7defa | 122 | switch (header_detect_io_instance->io_state) |
AzureIoTClient | 28:add19eb7defa | 123 | { |
AzureIoTClient | 28:add19eb7defa | 124 | default: |
AzureIoTClient | 28:add19eb7defa | 125 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 126 | |
Azure.IoT Build | 0:6ae2f7bca550 | 127 | case IO_STATE_CLOSING: |
Azure.IoT Build | 0:6ae2f7bca550 | 128 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
Azure.IoT Build | 0:6ae2f7bca550 | 129 | indicate_close_complete(header_detect_io_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 131 | |
AzureIoTClient | 28:add19eb7defa | 132 | case IO_STATE_WAIT_FOR_HEADER: |
AzureIoTClient | 28:add19eb7defa | 133 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 28:add19eb7defa | 134 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 135 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 28:add19eb7defa | 136 | break; |
AzureIoTClient | 28:add19eb7defa | 137 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 138 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 139 | |
Azure.IoT Build | 0:6ae2f7bca550 | 140 | static void on_underlying_io_open_complete(void* context, IO_OPEN_RESULT open_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 141 | { |
AzureIoTClient | 28:add19eb7defa | 142 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 143 | |
AzureIoTClient | 28:add19eb7defa | 144 | switch (header_detect_io_instance->io_state) |
AzureIoTClient | 28:add19eb7defa | 145 | { |
AzureIoTClient | 28:add19eb7defa | 146 | default: |
AzureIoTClient | 28:add19eb7defa | 147 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 148 | |
AzureIoTClient | 28:add19eb7defa | 149 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 28:add19eb7defa | 150 | if (open_result == IO_OPEN_OK) |
AzureIoTClient | 28:add19eb7defa | 151 | { |
AzureIoTClient | 28:add19eb7defa | 152 | header_detect_io_instance->io_state = IO_STATE_WAIT_FOR_HEADER; |
AzureIoTClient | 28:add19eb7defa | 153 | } |
AzureIoTClient | 28:add19eb7defa | 154 | else |
AzureIoTClient | 28:add19eb7defa | 155 | { |
AzureIoTClient | 28:add19eb7defa | 156 | if (xio_close(header_detect_io_instance->underlying_io, on_underlying_io_close_complete, header_detect_io_instance) != 0) |
AzureIoTClient | 28:add19eb7defa | 157 | { |
AzureIoTClient | 28:add19eb7defa | 158 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 159 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 28:add19eb7defa | 160 | } |
AzureIoTClient | 28:add19eb7defa | 161 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 162 | |
AzureIoTClient | 28:add19eb7defa | 163 | break; |
AzureIoTClient | 28:add19eb7defa | 164 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 165 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 166 | |
Azure.IoT Build | 0:6ae2f7bca550 | 167 | static void on_underlying_io_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 168 | { |
AzureIoTClient | 28:add19eb7defa | 169 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 170 | |
AzureIoTClient | 28:add19eb7defa | 171 | switch (header_detect_io_instance->io_state) |
AzureIoTClient | 28:add19eb7defa | 172 | { |
AzureIoTClient | 28:add19eb7defa | 173 | default: |
AzureIoTClient | 28:add19eb7defa | 174 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 175 | |
AzureIoTClient | 28:add19eb7defa | 176 | case IO_STATE_WAIT_FOR_HEADER: |
AzureIoTClient | 28:add19eb7defa | 177 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 28:add19eb7defa | 178 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 179 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 28:add19eb7defa | 180 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 181 | |
AzureIoTClient | 28:add19eb7defa | 182 | case IO_STATE_OPEN: |
AzureIoTClient | 28:add19eb7defa | 183 | header_detect_io_instance->io_state = IO_STATE_ERROR; |
AzureIoTClient | 28:add19eb7defa | 184 | indicate_error(header_detect_io_instance); |
AzureIoTClient | 28:add19eb7defa | 185 | break; |
AzureIoTClient | 28:add19eb7defa | 186 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 187 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 188 | |
Azure.IoT Build | 5:ae49385aff34 | 189 | CONCRETE_IO_HANDLE headerdetectio_create(void* io_create_parameters) |
Azure.IoT Build | 0:6ae2f7bca550 | 190 | { |
AzureIoTClient | 28:add19eb7defa | 191 | HEADER_DETECT_IO_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 192 | |
AzureIoTClient | 28:add19eb7defa | 193 | if (io_create_parameters == NULL) |
AzureIoTClient | 28:add19eb7defa | 194 | { |
AzureIoTClient | 28:add19eb7defa | 195 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 196 | } |
AzureIoTClient | 28:add19eb7defa | 197 | else |
AzureIoTClient | 28:add19eb7defa | 198 | { |
AzureIoTClient | 28:add19eb7defa | 199 | HEADERDETECTIO_CONFIG* header_detect_io_config = (HEADERDETECTIO_CONFIG*)io_create_parameters; |
AzureIoTClient | 28:add19eb7defa | 200 | result = (HEADER_DETECT_IO_INSTANCE*)malloc(sizeof(HEADER_DETECT_IO_INSTANCE)); |
AzureIoTClient | 28:add19eb7defa | 201 | if (result != NULL) |
AzureIoTClient | 28:add19eb7defa | 202 | { |
AzureIoTClient | 28:add19eb7defa | 203 | result->underlying_io = header_detect_io_config->underlying_io; |
AzureIoTClient | 28:add19eb7defa | 204 | result->on_io_open_complete = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 205 | result->on_io_close_complete = NULL; |
AzureIoTClient | 28:add19eb7defa | 206 | result->on_io_error = NULL; |
AzureIoTClient | 28:add19eb7defa | 207 | result->on_bytes_received = NULL; |
AzureIoTClient | 28:add19eb7defa | 208 | result->on_io_open_complete_context = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 209 | result->on_io_close_complete_context = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 210 | result->on_io_error_context = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 211 | result->on_bytes_received_context = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 212 | |
AzureIoTClient | 28:add19eb7defa | 213 | result->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 214 | } |
AzureIoTClient | 28:add19eb7defa | 215 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 216 | |
AzureIoTClient | 28:add19eb7defa | 217 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 218 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 219 | |
Azure.IoT Build | 0:6ae2f7bca550 | 220 | void headerdetectio_destroy(CONCRETE_IO_HANDLE header_detect_io) |
Azure.IoT Build | 0:6ae2f7bca550 | 221 | { |
AzureIoTClient | 28:add19eb7defa | 222 | if (header_detect_io != NULL) |
AzureIoTClient | 28:add19eb7defa | 223 | { |
AzureIoTClient | 28:add19eb7defa | 224 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
AzureIoTClient | 28:add19eb7defa | 225 | (void)headerdetectio_close(header_detect_io, NULL, NULL); |
AzureIoTClient | 28:add19eb7defa | 226 | xio_destroy(header_detect_io_instance->underlying_io); |
AzureIoTClient | 28:add19eb7defa | 227 | free(header_detect_io); |
AzureIoTClient | 28:add19eb7defa | 228 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 229 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 230 | |
Azure.IoT Build | 0:6ae2f7bca550 | 231 | int headerdetectio_open(CONCRETE_IO_HANDLE header_detect_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:6ae2f7bca550 | 232 | { |
AzureIoTClient | 28:add19eb7defa | 233 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 234 | |
AzureIoTClient | 28:add19eb7defa | 235 | if (header_detect_io == NULL) |
AzureIoTClient | 28:add19eb7defa | 236 | { |
AzureIoTClient | 28:add19eb7defa | 237 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 238 | } |
AzureIoTClient | 28:add19eb7defa | 239 | else |
AzureIoTClient | 28:add19eb7defa | 240 | { |
AzureIoTClient | 28:add19eb7defa | 241 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 242 | |
Azure.IoT Build | 0:6ae2f7bca550 | 243 | if (header_detect_io_instance->io_state != IO_STATE_NOT_OPEN && |
Azure.IoT Build | 0:6ae2f7bca550 | 244 | header_detect_io_instance->io_state != IO_STATE_OPEN) |
Azure.IoT Build | 0:6ae2f7bca550 | 245 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 246 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 247 | } |
AzureIoTClient | 28:add19eb7defa | 248 | else |
AzureIoTClient | 28:add19eb7defa | 249 | { |
AzureIoTClient | 28:add19eb7defa | 250 | header_detect_io_instance->on_bytes_received = on_bytes_received; |
AzureIoTClient | 28:add19eb7defa | 251 | header_detect_io_instance->on_io_open_complete = on_io_open_complete; |
AzureIoTClient | 28:add19eb7defa | 252 | header_detect_io_instance->on_io_error = on_io_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 253 | header_detect_io_instance->on_bytes_received_context = on_bytes_received_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 254 | header_detect_io_instance->on_io_open_complete_context = on_io_open_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 255 | header_detect_io_instance->on_io_error_context = on_io_error_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 256 | |
Azure.IoT Build | 0:6ae2f7bca550 | 257 | if (header_detect_io_instance->io_state == IO_STATE_OPEN) |
Azure.IoT Build | 0:6ae2f7bca550 | 258 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 259 | indicate_open_complete(header_detect_io_instance, IO_OPEN_OK); |
Azure.IoT Build | 0:6ae2f7bca550 | 260 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 261 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 262 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 263 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 264 | header_detect_io_instance->header_pos = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 265 | header_detect_io_instance->io_state = IO_STATE_OPENING_UNDERLYING_IO; |
Azure.IoT Build | 0:6ae2f7bca550 | 266 | |
Azure.IoT Build | 0:6ae2f7bca550 | 267 | if (xio_open(header_detect_io_instance->underlying_io, on_underlying_io_open_complete, header_detect_io_instance, on_underlying_io_bytes_received, header_detect_io_instance, on_underlying_io_error, header_detect_io_instance) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 268 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 269 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 270 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 271 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 272 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 273 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 274 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 275 | } |
AzureIoTClient | 28:add19eb7defa | 276 | } |
AzureIoTClient | 28:add19eb7defa | 277 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 278 | |
AzureIoTClient | 28:add19eb7defa | 279 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 280 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 281 | |
Azure.IoT Build | 0:6ae2f7bca550 | 282 | int headerdetectio_close(CONCRETE_IO_HANDLE header_detect_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 283 | { |
AzureIoTClient | 28:add19eb7defa | 284 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 285 | |
AzureIoTClient | 28:add19eb7defa | 286 | if (header_detect_io == NULL) |
AzureIoTClient | 28:add19eb7defa | 287 | { |
AzureIoTClient | 28:add19eb7defa | 288 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 289 | } |
AzureIoTClient | 28:add19eb7defa | 290 | else |
AzureIoTClient | 28:add19eb7defa | 291 | { |
AzureIoTClient | 28:add19eb7defa | 292 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 293 | |
AzureIoTClient | 28:add19eb7defa | 294 | if ((header_detect_io_instance->io_state == IO_STATE_NOT_OPEN) || |
AzureIoTClient | 28:add19eb7defa | 295 | (header_detect_io_instance->io_state == IO_STATE_CLOSING)) |
AzureIoTClient | 28:add19eb7defa | 296 | { |
AzureIoTClient | 28:add19eb7defa | 297 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 298 | } |
AzureIoTClient | 28:add19eb7defa | 299 | else |
AzureIoTClient | 28:add19eb7defa | 300 | { |
AzureIoTClient | 28:add19eb7defa | 301 | header_detect_io_instance->io_state = IO_STATE_CLOSING; |
Azure.IoT Build | 0:6ae2f7bca550 | 302 | header_detect_io_instance->on_io_close_complete = on_io_close_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 303 | header_detect_io_instance->on_io_close_complete_context = callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 304 | |
AzureIoTClient | 28:add19eb7defa | 305 | if (xio_close(header_detect_io_instance->underlying_io, on_underlying_io_close_complete, header_detect_io_instance) != 0) |
AzureIoTClient | 28:add19eb7defa | 306 | { |
AzureIoTClient | 28:add19eb7defa | 307 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 308 | } |
AzureIoTClient | 28:add19eb7defa | 309 | else |
AzureIoTClient | 28:add19eb7defa | 310 | { |
AzureIoTClient | 28:add19eb7defa | 311 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 312 | } |
AzureIoTClient | 28:add19eb7defa | 313 | } |
AzureIoTClient | 28:add19eb7defa | 314 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 315 | |
AzureIoTClient | 28:add19eb7defa | 316 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 317 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 318 | |
Azure.IoT Build | 0:6ae2f7bca550 | 319 | int headerdetectio_send(CONCRETE_IO_HANDLE header_detect_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 320 | { |
AzureIoTClient | 28:add19eb7defa | 321 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 322 | |
AzureIoTClient | 28:add19eb7defa | 323 | if (header_detect_io == NULL) |
AzureIoTClient | 28:add19eb7defa | 324 | { |
AzureIoTClient | 28:add19eb7defa | 325 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 326 | } |
AzureIoTClient | 28:add19eb7defa | 327 | else |
AzureIoTClient | 28:add19eb7defa | 328 | { |
AzureIoTClient | 28:add19eb7defa | 329 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 330 | |
AzureIoTClient | 28:add19eb7defa | 331 | if (header_detect_io_instance->io_state != IO_STATE_OPEN) |
AzureIoTClient | 28:add19eb7defa | 332 | { |
AzureIoTClient | 28:add19eb7defa | 333 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 334 | } |
AzureIoTClient | 28:add19eb7defa | 335 | else |
AzureIoTClient | 28:add19eb7defa | 336 | { |
AzureIoTClient | 28:add19eb7defa | 337 | if (xio_send(header_detect_io_instance->underlying_io, buffer, size, on_send_complete, callback_context) != 0) |
AzureIoTClient | 28:add19eb7defa | 338 | { |
AzureIoTClient | 28:add19eb7defa | 339 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 340 | } |
AzureIoTClient | 28:add19eb7defa | 341 | else |
AzureIoTClient | 28:add19eb7defa | 342 | { |
AzureIoTClient | 28:add19eb7defa | 343 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 344 | } |
AzureIoTClient | 28:add19eb7defa | 345 | } |
AzureIoTClient | 28:add19eb7defa | 346 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 347 | |
AzureIoTClient | 28:add19eb7defa | 348 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 349 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 350 | |
Azure.IoT Build | 0:6ae2f7bca550 | 351 | void headerdetectio_dowork(CONCRETE_IO_HANDLE header_detect_io) |
Azure.IoT Build | 0:6ae2f7bca550 | 352 | { |
AzureIoTClient | 28:add19eb7defa | 353 | if (header_detect_io != NULL) |
AzureIoTClient | 28:add19eb7defa | 354 | { |
AzureIoTClient | 28:add19eb7defa | 355 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 356 | |
AzureIoTClient | 28:add19eb7defa | 357 | if ((header_detect_io_instance->io_state != IO_STATE_NOT_OPEN) && |
AzureIoTClient | 28:add19eb7defa | 358 | (header_detect_io_instance->io_state != IO_STATE_ERROR)) |
AzureIoTClient | 28:add19eb7defa | 359 | { |
AzureIoTClient | 28:add19eb7defa | 360 | xio_dowork(header_detect_io_instance->underlying_io); |
AzureIoTClient | 28:add19eb7defa | 361 | } |
AzureIoTClient | 28:add19eb7defa | 362 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 363 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 364 | |
AzureIoTClient | 1:eab586236bfe | 365 | int headerdetectio_setoption(CONCRETE_IO_HANDLE header_detect_io, const char* optionName, const void* value) |
Azure.IoT Build | 0:6ae2f7bca550 | 366 | { |
AzureIoTClient | 1:eab586236bfe | 367 | int result; |
AzureIoTClient | 1:eab586236bfe | 368 | |
AzureIoTClient | 1:eab586236bfe | 369 | if (header_detect_io == NULL) |
AzureIoTClient | 1:eab586236bfe | 370 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 371 | result = __FAILURE__; |
AzureIoTClient | 1:eab586236bfe | 372 | } |
AzureIoTClient | 1:eab586236bfe | 373 | else |
AzureIoTClient | 1:eab586236bfe | 374 | { |
AzureIoTClient | 1:eab586236bfe | 375 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
AzureIoTClient | 1:eab586236bfe | 376 | |
AzureIoTClient | 1:eab586236bfe | 377 | if (header_detect_io_instance->underlying_io == NULL) |
AzureIoTClient | 1:eab586236bfe | 378 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 379 | result = __FAILURE__; |
AzureIoTClient | 1:eab586236bfe | 380 | } |
AzureIoTClient | 1:eab586236bfe | 381 | else |
AzureIoTClient | 1:eab586236bfe | 382 | { |
AzureIoTClient | 1:eab586236bfe | 383 | result = xio_setoption(header_detect_io_instance->underlying_io, optionName, value); |
AzureIoTClient | 1:eab586236bfe | 384 | } |
AzureIoTClient | 1:eab586236bfe | 385 | } |
AzureIoTClient | 1:eab586236bfe | 386 | |
AzureIoTClient | 1:eab586236bfe | 387 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 388 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 389 | |
AzureIoTClient | 6:641a9672db08 | 390 | /*this function will clone an option given by name and value*/ |
AzureIoTClient | 6:641a9672db08 | 391 | static void* headerdetectio_CloneOption(const char* name, const void* value) |
AzureIoTClient | 6:641a9672db08 | 392 | { |
AzureIoTClient | 17:923575db8b2d | 393 | (void)name; |
AzureIoTClient | 17:923575db8b2d | 394 | (void)value; |
AzureIoTClient | 6:641a9672db08 | 395 | return NULL; |
AzureIoTClient | 6:641a9672db08 | 396 | } |
AzureIoTClient | 6:641a9672db08 | 397 | |
AzureIoTClient | 6:641a9672db08 | 398 | /*this function destroys an option previously created*/ |
AzureIoTClient | 6:641a9672db08 | 399 | static void headerdetectio_DestroyOption(const char* name, const void* value) |
AzureIoTClient | 6:641a9672db08 | 400 | { |
AzureIoTClient | 17:923575db8b2d | 401 | (void)name; |
AzureIoTClient | 17:923575db8b2d | 402 | (void)value; |
AzureIoTClient | 6:641a9672db08 | 403 | } |
AzureIoTClient | 6:641a9672db08 | 404 | |
AzureIoTClient | 6:641a9672db08 | 405 | static OPTIONHANDLER_HANDLE headerdetectio_retrieveoptions(CONCRETE_IO_HANDLE handle) |
AzureIoTClient | 6:641a9672db08 | 406 | { |
AzureIoTClient | 6:641a9672db08 | 407 | OPTIONHANDLER_HANDLE result; |
AzureIoTClient | 6:641a9672db08 | 408 | (void)handle; |
AzureIoTClient | 6:641a9672db08 | 409 | result = OptionHandler_Create(headerdetectio_CloneOption, headerdetectio_DestroyOption, headerdetectio_setoption); |
AzureIoTClient | 6:641a9672db08 | 410 | if (result == NULL) |
AzureIoTClient | 6:641a9672db08 | 411 | { |
AzureIoTClient | 6:641a9672db08 | 412 | LogError("unable to OptionHandler_Create"); |
AzureIoTClient | 6:641a9672db08 | 413 | /*return as is*/ |
AzureIoTClient | 6:641a9672db08 | 414 | } |
AzureIoTClient | 6:641a9672db08 | 415 | else |
AzureIoTClient | 6:641a9672db08 | 416 | { |
AzureIoTClient | 6:641a9672db08 | 417 | /*insert here work to add the options to "result" handle*/ |
AzureIoTClient | 6:641a9672db08 | 418 | } |
AzureIoTClient | 6:641a9672db08 | 419 | return result; |
AzureIoTClient | 6:641a9672db08 | 420 | } |
AzureIoTClient | 6:641a9672db08 | 421 | |
Azure.IoT Build | 0:6ae2f7bca550 | 422 | static const IO_INTERFACE_DESCRIPTION header_detect_io_interface_description = |
Azure.IoT Build | 0:6ae2f7bca550 | 423 | { |
AzureIoTClient | 6:641a9672db08 | 424 | headerdetectio_retrieveoptions, |
AzureIoTClient | 28:add19eb7defa | 425 | headerdetectio_create, |
AzureIoTClient | 28:add19eb7defa | 426 | headerdetectio_destroy, |
AzureIoTClient | 28:add19eb7defa | 427 | headerdetectio_open, |
AzureIoTClient | 28:add19eb7defa | 428 | headerdetectio_close, |
AzureIoTClient | 28:add19eb7defa | 429 | headerdetectio_send, |
AzureIoTClient | 28:add19eb7defa | 430 | headerdetectio_dowork, |
Azure.IoT Build | 0:6ae2f7bca550 | 431 | headerdetectio_setoption |
Azure.IoT Build | 0:6ae2f7bca550 | 432 | }; |
Azure.IoT Build | 0:6ae2f7bca550 | 433 | |
Azure.IoT Build | 0:6ae2f7bca550 | 434 | const IO_INTERFACE_DESCRIPTION* headerdetectio_get_interface_description(void) |
Azure.IoT Build | 0:6ae2f7bca550 | 435 | { |
AzureIoTClient | 28:add19eb7defa | 436 | return &header_detect_io_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 437 | } |