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