A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Jun 02 15:53:07 2017 -0700
Revision:
27:d74f1cea23e1
Parent:
23:1111ee8bcba4
Child:
28:add19eb7defa
1.1.16

Who changed what in which revision?

UserRevisionLine numberNew 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 <stdlib.h>
Azure.IoT Build 0:6ae2f7bca550 5 #include <stdint.h>
Azure.IoT Build 0:6ae2f7bca550 6 #include <stddef.h>
Azure.IoT Build 0:6ae2f7bca550 7 #include <string.h>
AzureIoTClient 19:000ab4e6a2c1 8 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 20:206846c14c80 9 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 27:d74f1cea23e1 10 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/amqp_frame_codec.h"
Azure.IoT Build 0:6ae2f7bca550 12 #include "azure_uamqp_c/frame_codec.h"
Azure.IoT Build 0:6ae2f7bca550 13 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 14
Azure.IoT Build 0:6ae2f7bca550 15 typedef enum AMQP_FRAME_DECODE_STATE_TAG
Azure.IoT Build 0:6ae2f7bca550 16 {
Azure.IoT Build 0:6ae2f7bca550 17 AMQP_FRAME_DECODE_FRAME,
Azure.IoT Build 0:6ae2f7bca550 18 AMQP_FRAME_DECODE_ERROR
Azure.IoT Build 0:6ae2f7bca550 19 } AMQP_FRAME_DECODE_STATE;
Azure.IoT Build 0:6ae2f7bca550 20
Azure.IoT Build 0:6ae2f7bca550 21 typedef struct AMQP_FRAME_CODEC_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 22 {
Azure.IoT Build 0:6ae2f7bca550 23 FRAME_CODEC_HANDLE frame_codec;
Azure.IoT Build 0:6ae2f7bca550 24
Azure.IoT Build 0:6ae2f7bca550 25 /* decode */
Azure.IoT Build 0:6ae2f7bca550 26 AMQP_FRAME_RECEIVED_CALLBACK frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 27 AMQP_EMPTY_FRAME_RECEIVED_CALLBACK empty_frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 28 AMQP_FRAME_CODEC_ERROR_CALLBACK error_callback;
Azure.IoT Build 0:6ae2f7bca550 29 void* callback_context;
Azure.IoT Build 0:6ae2f7bca550 30 AMQPVALUE_DECODER_HANDLE decoder;
Azure.IoT Build 0:6ae2f7bca550 31 AMQP_FRAME_DECODE_STATE decode_state;
Azure.IoT Build 0:6ae2f7bca550 32 AMQP_VALUE decoded_performative;
Azure.IoT Build 0:6ae2f7bca550 33 } AMQP_FRAME_CODEC_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 34
Azure.IoT Build 0:6ae2f7bca550 35 static void amqp_value_decoded(void* context, AMQP_VALUE decoded_value)
Azure.IoT Build 0:6ae2f7bca550 36 {
Azure.IoT Build 0:6ae2f7bca550 37 AMQP_FRAME_CODEC_INSTANCE* amqp_frame_codec_instance = (AMQP_FRAME_CODEC_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 38 uint64_t performative_descriptor_ulong;
Azure.IoT Build 0:6ae2f7bca550 39 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 40
Azure.IoT Build 0:6ae2f7bca550 41 /* Codes_SRS_AMQP_FRAME_CODEC_01_060: [If any error occurs while decoding a frame, the decoder shall switch to an error state where decoding shall not be possible anymore.] */
Azure.IoT Build 0:6ae2f7bca550 42 if ((descriptor == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 43 (amqpvalue_get_ulong(descriptor, &performative_descriptor_ulong) != 0) ||
Azure.IoT Build 0:6ae2f7bca550 44 /* Codes_SRS_AMQP_FRAME_CODEC_01_003: [The performative MUST be one of those defined in section 2.7 and is encoded as a described type in the AMQP type system.] */
Azure.IoT Build 0:6ae2f7bca550 45 (performative_descriptor_ulong < AMQP_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 46 (performative_descriptor_ulong > AMQP_CLOSE))
Azure.IoT Build 0:6ae2f7bca550 47 {
Azure.IoT Build 0:6ae2f7bca550 48 /* Codes_SRS_AMQP_FRAME_CODEC_01_060: [If any error occurs while decoding a frame, the decoder shall switch to an error state where decoding shall not be possible anymore.] */
Azure.IoT Build 0:6ae2f7bca550 49 amqp_frame_codec_instance->decode_state = AMQP_FRAME_DECODE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 50 }
Azure.IoT Build 0:6ae2f7bca550 51 else
Azure.IoT Build 0:6ae2f7bca550 52 {
Azure.IoT Build 0:6ae2f7bca550 53 amqp_frame_codec_instance->decoded_performative = decoded_value;
Azure.IoT Build 0:6ae2f7bca550 54 }
Azure.IoT Build 0:6ae2f7bca550 55 }
Azure.IoT Build 0:6ae2f7bca550 56
Azure.IoT Build 0:6ae2f7bca550 57 static void frame_received(void* context, const unsigned char* type_specific, uint32_t type_specific_size, const unsigned char* frame_body, uint32_t frame_body_size)
Azure.IoT Build 0:6ae2f7bca550 58 {
Azure.IoT Build 0:6ae2f7bca550 59 AMQP_FRAME_CODEC_INSTANCE* amqp_frame_codec_instance = (AMQP_FRAME_CODEC_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 60 uint16_t channel;
Azure.IoT Build 0:6ae2f7bca550 61
Azure.IoT Build 0:6ae2f7bca550 62 switch (amqp_frame_codec_instance->decode_state)
Azure.IoT Build 0:6ae2f7bca550 63 {
Azure.IoT Build 0:6ae2f7bca550 64 default:
Azure.IoT Build 0:6ae2f7bca550 65 /* Codes_SRS_AMQP_FRAME_CODEC_01_050: [All subsequent decoding shall fail and no AMQP frames shall be indicated from that point on to the consumers of amqp_frame_codec.] */
Azure.IoT Build 0:6ae2f7bca550 66 case AMQP_FRAME_DECODE_ERROR:
Azure.IoT Build 0:6ae2f7bca550 67 break;
Azure.IoT Build 0:6ae2f7bca550 68
Azure.IoT Build 0:6ae2f7bca550 69 case AMQP_FRAME_DECODE_FRAME:
Azure.IoT Build 0:6ae2f7bca550 70 /* Codes_SRS_AMQP_FRAME_CODEC_01_049: [If not enough type specific bytes are received to decode the channel number, the decoding shall stop with an error.] */
Azure.IoT Build 0:6ae2f7bca550 71 if (type_specific_size < 2)
Azure.IoT Build 0:6ae2f7bca550 72 {
Azure.IoT Build 0:6ae2f7bca550 73 amqp_frame_codec_instance->decode_state = AMQP_FRAME_DECODE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 74
Azure.IoT Build 0:6ae2f7bca550 75 /* Codes_SRS_AMQP_FRAME_CODEC_01_069: [If any error occurs while decoding a frame, the decoder shall indicate the error by calling the amqp_frame_codec_error_callback and passing to it the callback context argument that was given in amqp_frame_codec_create.] */
Azure.IoT Build 0:6ae2f7bca550 76 amqp_frame_codec_instance->error_callback(amqp_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 77 }
Azure.IoT Build 0:6ae2f7bca550 78 else
Azure.IoT Build 0:6ae2f7bca550 79 {
Azure.IoT Build 0:6ae2f7bca550 80 /* Codes_SRS_AMQP_FRAME_CODEC_01_001: [Bytes 6 and 7 of an AMQP frame contain the channel number ] */
Azure.IoT Build 0:6ae2f7bca550 81 channel = ((uint16_t)type_specific[0]) << 8;
Azure.IoT Build 0:6ae2f7bca550 82 channel += type_specific[1];
Azure.IoT Build 0:6ae2f7bca550 83
Azure.IoT Build 0:6ae2f7bca550 84 if (frame_body_size == 0)
Azure.IoT Build 0:6ae2f7bca550 85 {
Azure.IoT Build 0:6ae2f7bca550 86 /* Codes_SRS_AMQP_FRAME_CODEC_01_048: [When a frame header is received from frame_codec and the frame payload size is 0, empty_frame_received_callback shall be invoked, while passing the channel number as argument.] */
Azure.IoT Build 0:6ae2f7bca550 87 /* Codes_SRS_AMQP_FRAME_CODEC_01_007: [An AMQP frame with no body MAY be used to generate artificial traffic as needed to satisfy any negotiated idle timeout interval ] */
Azure.IoT Build 0:6ae2f7bca550 88 amqp_frame_codec_instance->empty_frame_received_callback(amqp_frame_codec_instance->callback_context, channel);
Azure.IoT Build 0:6ae2f7bca550 89 }
Azure.IoT Build 0:6ae2f7bca550 90 else
Azure.IoT Build 0:6ae2f7bca550 91 {
Azure.IoT Build 0:6ae2f7bca550 92 /* Codes_SRS_AMQP_FRAME_CODEC_01_051: [If the frame payload is greater than 0, amqp_frame_codec shall decode the performative as a described AMQP type.] */
Azure.IoT Build 0:6ae2f7bca550 93 /* Codes_SRS_AMQP_FRAME_CODEC_01_002: [The frame body is defined as a performative followed by an opaque payload.] */
Azure.IoT Build 0:6ae2f7bca550 94 amqp_frame_codec_instance->decoded_performative = NULL;
Azure.IoT Build 0:6ae2f7bca550 95
Azure.IoT Build 0:6ae2f7bca550 96 while ((frame_body_size > 0) &&
Azure.IoT Build 0:6ae2f7bca550 97 (amqp_frame_codec_instance->decoded_performative == NULL) &&
Azure.IoT Build 0:6ae2f7bca550 98 (amqp_frame_codec_instance->decode_state != AMQP_FRAME_DECODE_ERROR))
Azure.IoT Build 0:6ae2f7bca550 99 {
Azure.IoT Build 0:6ae2f7bca550 100 /* Codes_SRS_AMQP_FRAME_CODEC_01_052: [Decoding the performative shall be done by feeding the bytes to the decoder create in amqp_frame_codec_create.] */
Azure.IoT Build 0:6ae2f7bca550 101 if (amqpvalue_decode_bytes(amqp_frame_codec_instance->decoder, frame_body, 1) != 0)
Azure.IoT Build 0:6ae2f7bca550 102 {
Azure.IoT Build 0:6ae2f7bca550 103 /* Codes_SRS_AMQP_FRAME_CODEC_01_060: [If any error occurs while decoding a frame, the decoder shall switch to an error state where decoding shall not be possible anymore.] */
Azure.IoT Build 0:6ae2f7bca550 104 amqp_frame_codec_instance->decode_state = AMQP_FRAME_DECODE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 105 }
Azure.IoT Build 0:6ae2f7bca550 106 else
Azure.IoT Build 0:6ae2f7bca550 107 {
Azure.IoT Build 0:6ae2f7bca550 108 frame_body_size--;
Azure.IoT Build 0:6ae2f7bca550 109 frame_body++;
Azure.IoT Build 0:6ae2f7bca550 110 }
Azure.IoT Build 0:6ae2f7bca550 111 }
Azure.IoT Build 0:6ae2f7bca550 112
Azure.IoT Build 0:6ae2f7bca550 113 if (amqp_frame_codec_instance->decode_state == AMQP_FRAME_DECODE_ERROR)
Azure.IoT Build 0:6ae2f7bca550 114 {
Azure.IoT Build 0:6ae2f7bca550 115 /* Codes_SRS_AMQP_FRAME_CODEC_01_069: [If any error occurs while decoding a frame, the decoder shall indicate the error by calling the amqp_frame_codec_error_callback and passing to it the callback context argument that was given in amqp_frame_codec_create.] */
Azure.IoT Build 0:6ae2f7bca550 116 amqp_frame_codec_instance->error_callback(amqp_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 117 }
Azure.IoT Build 0:6ae2f7bca550 118 else
Azure.IoT Build 0:6ae2f7bca550 119 {
Azure.IoT Build 0:6ae2f7bca550 120 /* Codes_SRS_AMQP_FRAME_CODEC_01_004: [The remaining bytes in the frame body form the payload for that frame.] */
Azure.IoT Build 0:6ae2f7bca550 121 /* Codes_SRS_AMQP_FRAME_CODEC_01_067: [When the performative is decoded, the rest of the frame_bytes shall not be given to the AMQP decoder, but they shall be buffered so that later they are given to the frame_received callback.] */
Azure.IoT Build 0:6ae2f7bca550 122 /* Codes_SRS_AMQP_FRAME_CODEC_01_054: [Once the performative is decoded and all frame payload bytes are received, the callback frame_received_callback shall be called.] */
Azure.IoT Build 0:6ae2f7bca550 123 /* Codes_SRS_AMQP_FRAME_CODEC_01_068: [A pointer to all the payload bytes shall also be passed to frame_received_callback.] */
Azure.IoT Build 0:6ae2f7bca550 124 amqp_frame_codec_instance->frame_received_callback(amqp_frame_codec_instance->callback_context, channel, amqp_frame_codec_instance->decoded_performative, frame_body, frame_body_size);
Azure.IoT Build 0:6ae2f7bca550 125 }
Azure.IoT Build 0:6ae2f7bca550 126 }
Azure.IoT Build 0:6ae2f7bca550 127 }
Azure.IoT Build 0:6ae2f7bca550 128 break;
Azure.IoT Build 0:6ae2f7bca550 129 }
Azure.IoT Build 0:6ae2f7bca550 130 }
Azure.IoT Build 0:6ae2f7bca550 131
Azure.IoT Build 0:6ae2f7bca550 132 static int encode_bytes(void* context, const unsigned char* bytes, size_t length)
Azure.IoT Build 0:6ae2f7bca550 133 {
Azure.IoT Build 0:6ae2f7bca550 134 PAYLOAD* payload = (PAYLOAD*)context;
Azure.IoT Build 0:6ae2f7bca550 135 (void)memcpy((unsigned char*)payload->bytes + payload->length, bytes, length);
Azure.IoT Build 0:6ae2f7bca550 136 payload->length += length;
Azure.IoT Build 0:6ae2f7bca550 137 return 0;
Azure.IoT Build 0:6ae2f7bca550 138 }
Azure.IoT Build 0:6ae2f7bca550 139
Azure.IoT Build 0:6ae2f7bca550 140 /* Codes_SRS_AMQP_FRAME_CODEC_01_011: [amqp_frame_codec_create shall create an instance of an amqp_frame_codec and return a non-NULL handle to it.] */
Azure.IoT Build 0:6ae2f7bca550 141 AMQP_FRAME_CODEC_HANDLE amqp_frame_codec_create(FRAME_CODEC_HANDLE frame_codec, AMQP_FRAME_RECEIVED_CALLBACK frame_received_callback,
Azure.IoT Build 0:6ae2f7bca550 142 AMQP_EMPTY_FRAME_RECEIVED_CALLBACK empty_frame_received_callback, AMQP_FRAME_CODEC_ERROR_CALLBACK amqp_frame_codec_error_callback, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 143 {
Azure.IoT Build 0:6ae2f7bca550 144 AMQP_FRAME_CODEC_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 145
Azure.IoT Build 0:6ae2f7bca550 146 /* Codes_SRS_AMQP_FRAME_CODEC_01_012: [If any of the arguments frame_codec, frame_received_callback, amqp_frame_codec_error_callback or empty_frame_received_callback is NULL, amqp_frame_codec_create shall return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 147 if ((frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 148 (frame_received_callback == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 149 (empty_frame_received_callback == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 150 (amqp_frame_codec_error_callback == NULL))
Azure.IoT Build 0:6ae2f7bca550 151 {
AzureIoTClient 23:1111ee8bcba4 152 LogError("Bad arguments: frame_codec = %p, frame_received_callback = %p, empty_frame_received_callback = %p, amqp_frame_codec_error_callback = %p",
AzureIoTClient 23:1111ee8bcba4 153 frame_codec, frame_received_callback, empty_frame_received_callback, amqp_frame_codec_error_callback);
Azure.IoT Build 0:6ae2f7bca550 154 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 155 }
Azure.IoT Build 0:6ae2f7bca550 156 else
Azure.IoT Build 0:6ae2f7bca550 157 {
AzureIoTClient 20:206846c14c80 158 result = (AMQP_FRAME_CODEC_INSTANCE*)malloc(sizeof(AMQP_FRAME_CODEC_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 159 /* Codes_SRS_AMQP_FRAME_CODEC_01_020: [If allocating memory for the new amqp_frame_codec fails, then amqp_frame_codec_create shall fail and return NULL.] */
AzureIoTClient 23:1111ee8bcba4 160 if (result == NULL)
AzureIoTClient 23:1111ee8bcba4 161 {
AzureIoTClient 23:1111ee8bcba4 162 LogError("Could not allocate memory for AMQP frame codec");
AzureIoTClient 23:1111ee8bcba4 163 }
AzureIoTClient 23:1111ee8bcba4 164 else
AzureIoTClient 23:1111ee8bcba4 165 {
Azure.IoT Build 0:6ae2f7bca550 166 result->frame_codec = frame_codec;
Azure.IoT Build 0:6ae2f7bca550 167 result->frame_received_callback = frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 168 result->empty_frame_received_callback = empty_frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 169 result->error_callback = amqp_frame_codec_error_callback;
Azure.IoT Build 0:6ae2f7bca550 170 result->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 171 result->decode_state = AMQP_FRAME_DECODE_FRAME;
Azure.IoT Build 0:6ae2f7bca550 172
Azure.IoT Build 0:6ae2f7bca550 173 /* Codes_SRS_AMQP_FRAME_CODEC_01_018: [amqp_frame_codec_create shall create a decoder to be used for decoding AMQP values.] */
Azure.IoT Build 0:6ae2f7bca550 174 result->decoder = amqpvalue_decoder_create(amqp_value_decoded, result);
Azure.IoT Build 0:6ae2f7bca550 175 if (result->decoder == NULL)
Azure.IoT Build 0:6ae2f7bca550 176 {
Azure.IoT Build 0:6ae2f7bca550 177 /* Codes_SRS_AMQP_FRAME_CODEC_01_019: [If creating the decoder fails, amqp_frame_codec_create shall fail and return NULL.] */
AzureIoTClient 23:1111ee8bcba4 178 LogError("Could not create AMQP decoder");
AzureIoTClient 23:1111ee8bcba4 179 free(result);
Azure.IoT Build 0:6ae2f7bca550 180 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 181 }
Azure.IoT Build 0:6ae2f7bca550 182 else
Azure.IoT Build 0:6ae2f7bca550 183 {
Azure.IoT Build 0:6ae2f7bca550 184 /* Codes_SRS_AMQP_FRAME_CODEC_01_013: [amqp_frame_codec_create shall subscribe for AMQP frames with the given frame_codec.] */
Azure.IoT Build 0:6ae2f7bca550 185 if (frame_codec_subscribe(frame_codec, FRAME_TYPE_AMQP, frame_received, result) != 0)
Azure.IoT Build 0:6ae2f7bca550 186 {
Azure.IoT Build 0:6ae2f7bca550 187 /* Codes_SRS_AMQP_FRAME_CODEC_01_014: [If subscribing for AMQP frames fails, amqp_frame_codec_create shall fail and return NULL.] */
AzureIoTClient 23:1111ee8bcba4 188 LogError("Could not subscribe for received AMQP frames");
AzureIoTClient 23:1111ee8bcba4 189 amqpvalue_decoder_destroy(result->decoder);
AzureIoTClient 20:206846c14c80 190 free(result);
Azure.IoT Build 0:6ae2f7bca550 191 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 192 }
Azure.IoT Build 0:6ae2f7bca550 193 }
Azure.IoT Build 0:6ae2f7bca550 194 }
Azure.IoT Build 0:6ae2f7bca550 195 }
Azure.IoT Build 0:6ae2f7bca550 196
Azure.IoT Build 0:6ae2f7bca550 197 return result;
Azure.IoT Build 0:6ae2f7bca550 198 }
Azure.IoT Build 0:6ae2f7bca550 199
Azure.IoT Build 0:6ae2f7bca550 200 void amqp_frame_codec_destroy(AMQP_FRAME_CODEC_HANDLE amqp_frame_codec)
Azure.IoT Build 0:6ae2f7bca550 201 {
AzureIoTClient 23:1111ee8bcba4 202 if (amqp_frame_codec == NULL)
AzureIoTClient 23:1111ee8bcba4 203 {
AzureIoTClient 23:1111ee8bcba4 204 /* Codes_SRS_AMQP_FRAME_CODEC_01_016: [If amqp_frame_codec is NULL, amqp_frame_codec_destroy shall do nothing.] */
AzureIoTClient 23:1111ee8bcba4 205 LogError("NULL amqp_frame_codec");
AzureIoTClient 23:1111ee8bcba4 206 }
AzureIoTClient 23:1111ee8bcba4 207 else
AzureIoTClient 23:1111ee8bcba4 208 {
Azure.IoT Build 0:6ae2f7bca550 209 /* Codes_SRS_AMQP_FRAME_CODEC_01_017: [amqp_frame_codec_destroy shall unsubscribe from receiving AMQP frames from the frame_codec that was passed to amqp_frame_codec_create.] */
Azure.IoT Build 0:6ae2f7bca550 210 (void)frame_codec_unsubscribe(amqp_frame_codec->frame_codec, FRAME_TYPE_AMQP);
Azure.IoT Build 0:6ae2f7bca550 211
Azure.IoT Build 0:6ae2f7bca550 212 /* Codes_SRS_AMQP_FRAME_CODEC_01_021: [The decoder created in amqp_frame_codec_create shall be destroyed by amqp_frame_codec_destroy.] */
Azure.IoT Build 0:6ae2f7bca550 213 amqpvalue_decoder_destroy(amqp_frame_codec->decoder);
Azure.IoT Build 0:6ae2f7bca550 214
Azure.IoT Build 0:6ae2f7bca550 215 /* Codes_SRS_AMQP_FRAME_CODEC_01_015: [amqp_frame_codec_destroy shall free all resources associated with the amqp_frame_codec instance.] */
AzureIoTClient 20:206846c14c80 216 free(amqp_frame_codec);
Azure.IoT Build 0:6ae2f7bca550 217 }
Azure.IoT Build 0:6ae2f7bca550 218 }
Azure.IoT Build 0:6ae2f7bca550 219
Azure.IoT Build 0:6ae2f7bca550 220 int amqp_frame_codec_encode_frame(AMQP_FRAME_CODEC_HANDLE amqp_frame_codec, uint16_t channel, const AMQP_VALUE performative, const PAYLOAD* payloads, size_t payload_count, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 221 {
Azure.IoT Build 0:6ae2f7bca550 222 int result;
Azure.IoT Build 0:6ae2f7bca550 223
Azure.IoT Build 0:6ae2f7bca550 224 /* Codes_SRS_AMQP_FRAME_CODEC_01_024: [If frame_codec, performative or on_bytes_encoded is NULL, amqp_frame_codec_encode_frame shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 225 if ((amqp_frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 226 (performative == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 227 (on_bytes_encoded == NULL))
Azure.IoT Build 0:6ae2f7bca550 228 {
AzureIoTClient 23:1111ee8bcba4 229 LogError("Bad arguments: amqp_frame_codec = %p, performative = %p, on_bytes_encoded = %p",
AzureIoTClient 23:1111ee8bcba4 230 amqp_frame_codec, performative, on_bytes_encoded);
AzureIoTClient 23:1111ee8bcba4 231 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 232 }
Azure.IoT Build 0:6ae2f7bca550 233 else
Azure.IoT Build 0:6ae2f7bca550 234 {
Azure.IoT Build 0:6ae2f7bca550 235 AMQP_VALUE descriptor;
Azure.IoT Build 0:6ae2f7bca550 236 uint64_t performative_ulong;
Azure.IoT Build 0:6ae2f7bca550 237 size_t encoded_size;
Azure.IoT Build 0:6ae2f7bca550 238
AzureIoTClient 23:1111ee8bcba4 239 if ((descriptor = amqpvalue_get_inplace_descriptor(performative)) == NULL)
AzureIoTClient 23:1111ee8bcba4 240 {
AzureIoTClient 23:1111ee8bcba4 241 /* Codes_SRS_AMQP_FRAME_CODEC_01_029: [If any error occurs during encoding, amqp_frame_codec_encode_frame shall fail and return a non-zero value.] */
AzureIoTClient 23:1111ee8bcba4 242 LogError("Getting the descriptor failed");
AzureIoTClient 23:1111ee8bcba4 243 result = __FAILURE__;
AzureIoTClient 23:1111ee8bcba4 244 }
AzureIoTClient 23:1111ee8bcba4 245 else if (amqpvalue_get_ulong(descriptor, &performative_ulong) != 0)
AzureIoTClient 23:1111ee8bcba4 246 {
AzureIoTClient 23:1111ee8bcba4 247 /* Codes_SRS_AMQP_FRAME_CODEC_01_029: [If any error occurs during encoding, amqp_frame_codec_encode_frame shall fail and return a non-zero value.] */
AzureIoTClient 23:1111ee8bcba4 248 LogError("Getting the descriptor ulong failed");
AzureIoTClient 23:1111ee8bcba4 249 result = __FAILURE__;
AzureIoTClient 23:1111ee8bcba4 250 }
AzureIoTClient 23:1111ee8bcba4 251 /* Codes_SRS_AMQP_FRAME_CODEC_01_008: [The performative MUST be one of those defined in section 2.7 and is encoded as a described type in the AMQP type system.] */
AzureIoTClient 23:1111ee8bcba4 252 else if ((performative_ulong < AMQP_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 253 (performative_ulong > AMQP_CLOSE))
Azure.IoT Build 0:6ae2f7bca550 254 {
Azure.IoT Build 0:6ae2f7bca550 255 /* Codes_SRS_AMQP_FRAME_CODEC_01_029: [If any error occurs during encoding, amqp_frame_codec_encode_frame shall fail and return a non-zero value.] */
AzureIoTClient 23:1111ee8bcba4 256 LogError("Bad arguments: amqp_frame_codec = %p, performative = %p, on_bytes_encoded = %p",
AzureIoTClient 23:1111ee8bcba4 257 amqp_frame_codec, performative, on_bytes_encoded);
AzureIoTClient 23:1111ee8bcba4 258 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 259 }
Azure.IoT Build 0:6ae2f7bca550 260 /* Codes_SRS_AMQP_FRAME_CODEC_01_027: [The encoded size of the performative and its fields shall be obtained by calling amqpvalue_get_encoded_size.] */
Azure.IoT Build 0:6ae2f7bca550 261 else if (amqpvalue_get_encoded_size(performative, &encoded_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 262 {
Azure.IoT Build 0:6ae2f7bca550 263 /* Codes_SRS_AMQP_FRAME_CODEC_01_029: [If any error occurs during encoding, amqp_frame_codec_encode_frame shall fail and return a non-zero value.] */
AzureIoTClient 23:1111ee8bcba4 264 LogError("Getting the encoded size failed");
AzureIoTClient 23:1111ee8bcba4 265 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 266 }
Azure.IoT Build 0:6ae2f7bca550 267 else
Azure.IoT Build 0:6ae2f7bca550 268 {
AzureIoTClient 20:206846c14c80 269 unsigned char* amqp_performative_bytes = (unsigned char*)malloc(encoded_size);
Azure.IoT Build 0:6ae2f7bca550 270 if (amqp_performative_bytes == NULL)
Azure.IoT Build 0:6ae2f7bca550 271 {
AzureIoTClient 23:1111ee8bcba4 272 LogError("Could not allocate performative bytes");
AzureIoTClient 23:1111ee8bcba4 273 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 274 }
Azure.IoT Build 0:6ae2f7bca550 275 else
Azure.IoT Build 0:6ae2f7bca550 276 {
AzureIoTClient 20:206846c14c80 277 PAYLOAD* new_payloads = (PAYLOAD*)malloc(sizeof(PAYLOAD) * (payload_count + 1));
Azure.IoT Build 0:6ae2f7bca550 278 if (new_payloads == NULL)
Azure.IoT Build 0:6ae2f7bca550 279 {
AzureIoTClient 23:1111ee8bcba4 280 LogError("Could not allocate frame payloads");
AzureIoTClient 23:1111ee8bcba4 281 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 282 }
Azure.IoT Build 0:6ae2f7bca550 283 else
Azure.IoT Build 0:6ae2f7bca550 284 {
Azure.IoT Build 0:6ae2f7bca550 285 /* Codes_SRS_AMQP_FRAME_CODEC_01_070: [The payloads argument for frame_codec_encode_frame shall be made of the payload for the encoded performative and the payloads passed to amqp_frame_codec_encode_frame.] */
Azure.IoT Build 0:6ae2f7bca550 286 /* Codes_SRS_AMQP_FRAME_CODEC_01_028: [The encode result for the performative shall be placed in a PAYLOAD structure.] */
Azure.IoT Build 0:6ae2f7bca550 287 new_payloads[0].bytes = amqp_performative_bytes;
Azure.IoT Build 0:6ae2f7bca550 288 new_payloads[0].length = 0;
Azure.IoT Build 0:6ae2f7bca550 289
Azure.IoT Build 0:6ae2f7bca550 290 if (payload_count > 0)
Azure.IoT Build 0:6ae2f7bca550 291 {
Azure.IoT Build 0:6ae2f7bca550 292 (void)memcpy(new_payloads + 1, payloads, sizeof(PAYLOAD) * payload_count);
Azure.IoT Build 0:6ae2f7bca550 293 }
Azure.IoT Build 0:6ae2f7bca550 294
Azure.IoT Build 0:6ae2f7bca550 295 if (amqpvalue_encode(performative, encode_bytes, &new_payloads[0]) != 0)
Azure.IoT Build 0:6ae2f7bca550 296 {
AzureIoTClient 23:1111ee8bcba4 297 LogError("amqpvalue_encode failed");
AzureIoTClient 23:1111ee8bcba4 298 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 299 }
Azure.IoT Build 0:6ae2f7bca550 300 else
Azure.IoT Build 0:6ae2f7bca550 301 {
AzureIoTClient 6:641a9672db08 302 unsigned char channel_bytes[2];
AzureIoTClient 6:641a9672db08 303
AzureIoTClient 6:641a9672db08 304 channel_bytes[0] = channel >> 8;
AzureIoTClient 6:641a9672db08 305 channel_bytes[1] = channel & 0xFF;
Azure.IoT Build 0:6ae2f7bca550 306
Azure.IoT Build 0:6ae2f7bca550 307 /* Codes_SRS_AMQP_FRAME_CODEC_01_005: [Bytes 6 and 7 of an AMQP frame contain the channel number ] */
Azure.IoT Build 0:6ae2f7bca550 308 /* Codes_SRS_AMQP_FRAME_CODEC_01_025: [amqp_frame_codec_encode_frame shall encode the frame header by using frame_codec_encode_frame.] */
Azure.IoT Build 0:6ae2f7bca550 309 /* Codes_SRS_AMQP_FRAME_CODEC_01_006: [The frame body is defined as a performative followed by an opaque payload.] */
Azure.IoT Build 0:6ae2f7bca550 310 if (frame_codec_encode_frame(amqp_frame_codec->frame_codec, FRAME_TYPE_AMQP, new_payloads, payload_count + 1, channel_bytes, sizeof(channel_bytes), on_bytes_encoded, callback_context) != 0)
Azure.IoT Build 0:6ae2f7bca550 311 {
Azure.IoT Build 0:6ae2f7bca550 312 /* Codes_SRS_AMQP_FRAME_CODEC_01_029: [If any error occurs during encoding, amqp_frame_codec_encode_frame shall fail and return a non-zero value.] */
AzureIoTClient 23:1111ee8bcba4 313 LogError("frame_codec_encode_frame failed");
AzureIoTClient 23:1111ee8bcba4 314 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 315 }
Azure.IoT Build 0:6ae2f7bca550 316 else
Azure.IoT Build 0:6ae2f7bca550 317 {
Azure.IoT Build 0:6ae2f7bca550 318 /* Codes_SRS_AMQP_FRAME_CODEC_01_022: [amqp_frame_codec_begin_encode_frame shall encode the frame header and AMQP performative in an AMQP frame and on success it shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 319 result = 0;
Azure.IoT Build 0:6ae2f7bca550 320 }
Azure.IoT Build 0:6ae2f7bca550 321 }
Azure.IoT Build 0:6ae2f7bca550 322
AzureIoTClient 20:206846c14c80 323 free(new_payloads);
Azure.IoT Build 0:6ae2f7bca550 324 }
Azure.IoT Build 0:6ae2f7bca550 325
AzureIoTClient 20:206846c14c80 326 free(amqp_performative_bytes);
Azure.IoT Build 0:6ae2f7bca550 327 }
Azure.IoT Build 0:6ae2f7bca550 328 }
Azure.IoT Build 0:6ae2f7bca550 329 }
Azure.IoT Build 0:6ae2f7bca550 330
Azure.IoT Build 0:6ae2f7bca550 331 return result;
Azure.IoT Build 0:6ae2f7bca550 332 }
Azure.IoT Build 0:6ae2f7bca550 333
Azure.IoT Build 0:6ae2f7bca550 334 /* Codes_SRS_AMQP_FRAME_CODEC_01_042: [amqp_frame_codec_encode_empty_frame shall encode a frame with no payload.] */
Azure.IoT Build 0:6ae2f7bca550 335 /* Codes_SRS_AMQP_FRAME_CODEC_01_010: [An AMQP frame with no body MAY be used to generate artificial traffic as needed to satisfy any negotiated idle timeout interval ] */
Azure.IoT Build 0:6ae2f7bca550 336 int amqp_frame_codec_encode_empty_frame(AMQP_FRAME_CODEC_HANDLE amqp_frame_codec, uint16_t channel, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 337 {
Azure.IoT Build 0:6ae2f7bca550 338 int result;
Azure.IoT Build 0:6ae2f7bca550 339
Azure.IoT Build 0:6ae2f7bca550 340 /* Codes_SRS_AMQP_FRAME_CODEC_01_045: [If amqp_frame_codec is NULL, amqp_frame_codec_encode_empty_frame shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 341 if (amqp_frame_codec == NULL)
Azure.IoT Build 0:6ae2f7bca550 342 {
AzureIoTClient 23:1111ee8bcba4 343 LogError("NULL amqp_frame_codec");
AzureIoTClient 23:1111ee8bcba4 344 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 345 }
Azure.IoT Build 0:6ae2f7bca550 346 else
Azure.IoT Build 0:6ae2f7bca550 347 {
AzureIoTClient 6:641a9672db08 348 unsigned char channel_bytes[2];
AzureIoTClient 6:641a9672db08 349
AzureIoTClient 6:641a9672db08 350 channel_bytes[0] = channel >> 8;
AzureIoTClient 6:641a9672db08 351 channel_bytes[1] = channel & 0xFF;
Azure.IoT Build 0:6ae2f7bca550 352
Azure.IoT Build 0:6ae2f7bca550 353 /* Codes_SRS_AMQP_FRAME_CODEC_01_044: [amqp_frame_codec_encode_empty_frame shall use frame_codec_encode_frame to encode the frame.] */
Azure.IoT Build 0:6ae2f7bca550 354 if (frame_codec_encode_frame(amqp_frame_codec->frame_codec, FRAME_TYPE_AMQP, NULL, 0, channel_bytes, sizeof(channel_bytes), on_bytes_encoded, callback_context) != 0)
Azure.IoT Build 0:6ae2f7bca550 355 {
Azure.IoT Build 0:6ae2f7bca550 356 /* Codes_SRS_AMQP_FRAME_CODEC_01_046: [If encoding fails in any way, amqp_frame_codec_encode_empty_frame shall fail and return a non-zero value.] */
AzureIoTClient 23:1111ee8bcba4 357 LogError("frame_codec_encode_frame failed when encoding empty frame");
AzureIoTClient 23:1111ee8bcba4 358 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 359 }
Azure.IoT Build 0:6ae2f7bca550 360 else
Azure.IoT Build 0:6ae2f7bca550 361 {
Azure.IoT Build 0:6ae2f7bca550 362 /* Codes_SRS_AMQP_FRAME_CODEC_01_043: [On success, amqp_frame_codec_encode_empty_frame shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 363 result = 0;
Azure.IoT Build 0:6ae2f7bca550 364 }
Azure.IoT Build 0:6ae2f7bca550 365 }
Azure.IoT Build 0:6ae2f7bca550 366
Azure.IoT Build 0:6ae2f7bca550 367 return result;
Azure.IoT Build 0:6ae2f7bca550 368 }