A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
header_detect_io.c@27:d74f1cea23e1, 2017-06-02 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jun 02 15:53:07 2017 -0700
- Revision:
- 27:d74f1cea23e1
- Parent:
- 21:f9c433d8e6ca
- Child:
- 28:add19eb7defa
1.1.16
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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | IO_STATE_NOT_OPEN, |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | IO_STATE_OPENING_UNDERLYING_IO, |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | IO_STATE_WAIT_FOR_HEADER, |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | IO_STATE_OPEN, |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | IO_STATE_CLOSING, |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | XIO_HANDLE underlying_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | size_t header_pos; |
Azure.IoT Build | 0:6ae2f7bca550 | 24 | IO_STATE io_state; |
Azure.IoT Build | 0:6ae2f7bca550 | 25 | ON_IO_OPEN_COMPLETE on_io_open_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 26 | ON_IO_CLOSE_COMPLETE on_io_close_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 27 | ON_IO_ERROR on_io_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 28 | ON_BYTES_RECEIVED on_bytes_received; |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | void* on_io_open_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | if (header_detect_io_instance->on_io_error != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | header_detect_io_instance->on_io_error(header_detect_io_instance->on_io_error_context); |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 47 | if (header_detect_io_instance->on_io_open_complete != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 48 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | header_detect_io_instance->on_io_open_complete(header_detect_io_instance->on_io_open_complete_context, open_result); |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 71 | |
Azure.IoT Build | 0:6ae2f7bca550 | 72 | while (size > 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 73 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 74 | switch (header_detect_io_instance->io_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | default: |
Azure.IoT Build | 0:6ae2f7bca550 | 77 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | |
Azure.IoT Build | 0:6ae2f7bca550 | 79 | case IO_STATE_WAIT_FOR_HEADER: |
Azure.IoT Build | 0:6ae2f7bca550 | 80 | if (amqp_header[header_detect_io_instance->header_pos] != buffer[0]) |
Azure.IoT Build | 0:6ae2f7bca550 | 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; |
Azure.IoT Build | 0:6ae2f7bca550 | 86 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | size = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 88 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | header_detect_io_instance->header_pos++; |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | size--; |
Azure.IoT Build | 0:6ae2f7bca550 | 93 | buffer++; |
Azure.IoT Build | 0:6ae2f7bca550 | 94 | if (header_detect_io_instance->header_pos == sizeof(amqp_header)) |
Azure.IoT Build | 0:6ae2f7bca550 | 95 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 96 | if (xio_send(header_detect_io_instance->underlying_io, amqp_header, sizeof(amqp_header), NULL, NULL) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 97 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
Azure.IoT Build | 0:6ae2f7bca550 | 99 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 103 | header_detect_io_instance->io_state = IO_STATE_OPEN; |
Azure.IoT Build | 0:6ae2f7bca550 | 104 | indicate_open_complete(header_detect_io_instance, IO_OPEN_OK); |
Azure.IoT Build | 0:6ae2f7bca550 | 105 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 108 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 109 | |
Azure.IoT Build | 0:6ae2f7bca550 | 110 | case IO_STATE_OPEN: |
Azure.IoT Build | 0:6ae2f7bca550 | 111 | header_detect_io_instance->on_bytes_received(header_detect_io_instance->on_bytes_received_context, buffer, size); |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | size = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 113 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 114 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 120 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 121 | |
Azure.IoT Build | 0:6ae2f7bca550 | 122 | switch (header_detect_io_instance->io_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 123 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 124 | default: |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | |
Azure.IoT Build | 0:6ae2f7bca550 | 132 | case IO_STATE_WAIT_FOR_HEADER: |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | case IO_STATE_OPENING_UNDERLYING_IO: |
Azure.IoT Build | 0:6ae2f7bca550 | 134 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
Azure.IoT Build | 0:6ae2f7bca550 | 135 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
Azure.IoT Build | 0:6ae2f7bca550 | 136 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 142 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 143 | |
Azure.IoT Build | 0:6ae2f7bca550 | 144 | switch (header_detect_io_instance->io_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 145 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 146 | default: |
Azure.IoT Build | 0:6ae2f7bca550 | 147 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 148 | |
Azure.IoT Build | 0:6ae2f7bca550 | 149 | case IO_STATE_OPENING_UNDERLYING_IO: |
Azure.IoT Build | 0:6ae2f7bca550 | 150 | if (open_result == IO_OPEN_OK) |
Azure.IoT Build | 0:6ae2f7bca550 | 151 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 152 | header_detect_io_instance->io_state = IO_STATE_WAIT_FOR_HEADER; |
Azure.IoT Build | 0:6ae2f7bca550 | 153 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 154 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 155 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 156 | if (xio_close(header_detect_io_instance->underlying_io, on_underlying_io_close_complete, header_detect_io_instance) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 157 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 158 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
Azure.IoT Build | 0:6ae2f7bca550 | 159 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
Azure.IoT Build | 0:6ae2f7bca550 | 160 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 161 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 162 | |
Azure.IoT Build | 0:6ae2f7bca550 | 163 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 169 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 170 | |
Azure.IoT Build | 0:6ae2f7bca550 | 171 | switch (header_detect_io_instance->io_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 172 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 173 | default: |
Azure.IoT Build | 0:6ae2f7bca550 | 174 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 175 | |
Azure.IoT Build | 0:6ae2f7bca550 | 176 | case IO_STATE_WAIT_FOR_HEADER: |
Azure.IoT Build | 0:6ae2f7bca550 | 177 | case IO_STATE_OPENING_UNDERLYING_IO: |
Azure.IoT Build | 0:6ae2f7bca550 | 178 | header_detect_io_instance->io_state = IO_STATE_NOT_OPEN; |
Azure.IoT Build | 0:6ae2f7bca550 | 179 | indicate_open_complete(header_detect_io_instance, IO_OPEN_ERROR); |
Azure.IoT Build | 0:6ae2f7bca550 | 180 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 181 | |
Azure.IoT Build | 0:6ae2f7bca550 | 182 | case IO_STATE_OPEN: |
Azure.IoT Build | 0:6ae2f7bca550 | 183 | header_detect_io_instance->io_state = IO_STATE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 184 | indicate_error(header_detect_io_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 185 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 191 | HEADER_DETECT_IO_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 192 | |
Azure.IoT Build | 0:6ae2f7bca550 | 193 | if (io_create_parameters == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 194 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 195 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 196 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 197 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 198 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 199 | HEADERDETECTIO_CONFIG* header_detect_io_config = (HEADERDETECTIO_CONFIG*)io_create_parameters; |
AzureIoTClient | 21:f9c433d8e6ca | 200 | result = (HEADER_DETECT_IO_INSTANCE*)malloc(sizeof(HEADER_DETECT_IO_INSTANCE)); |
Azure.IoT Build | 0:6ae2f7bca550 | 201 | if (result != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 202 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 203 | result->underlying_io = header_detect_io_config->underlying_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 204 | result->on_io_open_complete = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 205 | result->on_io_close_complete = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 206 | result->on_io_error = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 207 | result->on_bytes_received = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | |
Azure.IoT Build | 0:6ae2f7bca550 | 213 | result->io_state = IO_STATE_NOT_OPEN; |
Azure.IoT Build | 0:6ae2f7bca550 | 214 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 215 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 216 | |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 222 | if (header_detect_io != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 223 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 224 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 225 | (void)headerdetectio_close(header_detect_io, NULL, NULL); |
Azure.IoT Build | 0:6ae2f7bca550 | 226 | xio_destroy(header_detect_io_instance->underlying_io); |
AzureIoTClient | 21:f9c433d8e6ca | 227 | free(header_detect_io); |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 233 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 234 | |
Azure.IoT Build | 0:6ae2f7bca550 | 235 | if (header_detect_io == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 236 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 237 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 238 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 239 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 240 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 248 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 249 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 250 | header_detect_io_instance->on_bytes_received = on_bytes_received; |
Azure.IoT Build | 0:6ae2f7bca550 | 251 | header_detect_io_instance->on_io_open_complete = on_io_open_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 276 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 277 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 278 | |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 284 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 285 | |
Azure.IoT Build | 0:6ae2f7bca550 | 286 | if (header_detect_io == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 287 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 288 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 289 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 290 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 291 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 292 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 293 | |
Azure.IoT Build | 0:6ae2f7bca550 | 294 | if ((header_detect_io_instance->io_state == IO_STATE_NOT_OPEN) || |
Azure.IoT Build | 0:6ae2f7bca550 | 295 | (header_detect_io_instance->io_state == IO_STATE_CLOSING)) |
Azure.IoT Build | 0:6ae2f7bca550 | 296 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 297 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 298 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 299 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 300 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | |
Azure.IoT Build | 0:6ae2f7bca550 | 305 | if (xio_close(header_detect_io_instance->underlying_io, on_underlying_io_close_complete, header_detect_io_instance) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 306 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 307 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 308 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 309 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 310 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 311 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 312 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 313 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 314 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 315 | |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 321 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 322 | |
Azure.IoT Build | 0:6ae2f7bca550 | 323 | if (header_detect_io == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 324 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 325 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 326 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 327 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 328 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 329 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 330 | |
Azure.IoT Build | 0:6ae2f7bca550 | 331 | if (header_detect_io_instance->io_state != IO_STATE_OPEN) |
Azure.IoT Build | 0:6ae2f7bca550 | 332 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 333 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 334 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 335 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 336 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 337 | if (xio_send(header_detect_io_instance->underlying_io, buffer, size, on_send_complete, callback_context) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 338 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 339 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 340 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 341 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 342 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 343 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 344 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 345 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 346 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 347 | |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 353 | if (header_detect_io != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 354 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 355 | HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 356 | |
Azure.IoT Build | 0:6ae2f7bca550 | 357 | if ((header_detect_io_instance->io_state != IO_STATE_NOT_OPEN) && |
Azure.IoT Build | 0:6ae2f7bca550 | 358 | (header_detect_io_instance->io_state != IO_STATE_ERROR)) |
Azure.IoT Build | 0:6ae2f7bca550 | 359 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 360 | xio_dowork(header_detect_io_instance->underlying_io); |
Azure.IoT Build | 0:6ae2f7bca550 | 361 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 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, |
Azure.IoT Build | 0:6ae2f7bca550 | 425 | headerdetectio_create, |
Azure.IoT Build | 0:6ae2f7bca550 | 426 | headerdetectio_destroy, |
Azure.IoT Build | 0:6ae2f7bca550 | 427 | headerdetectio_open, |
Azure.IoT Build | 0:6ae2f7bca550 | 428 | headerdetectio_close, |
Azure.IoT Build | 0:6ae2f7bca550 | 429 | headerdetectio_send, |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 436 | return &header_detect_io_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 437 | } |