A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Mar 10 11:47:49 2017 -0800
Revision:
20:206846c14c80
Parent:
19:000ab4e6a2c1
Child:
23:1111ee8bcba4
1.1.9

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"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/amqp_frame_codec.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/frame_codec.h"
Azure.IoT Build 0:6ae2f7bca550 12 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 13
Azure.IoT Build 0:6ae2f7bca550 14 typedef enum AMQP_FRAME_DECODE_STATE_TAG
Azure.IoT Build 0:6ae2f7bca550 15 {
Azure.IoT Build 0:6ae2f7bca550 16 AMQP_FRAME_DECODE_FRAME,
Azure.IoT Build 0:6ae2f7bca550 17 AMQP_FRAME_DECODE_ERROR
Azure.IoT Build 0:6ae2f7bca550 18 } AMQP_FRAME_DECODE_STATE;
Azure.IoT Build 0:6ae2f7bca550 19
Azure.IoT Build 0:6ae2f7bca550 20 typedef struct AMQP_FRAME_CODEC_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 21 {
Azure.IoT Build 0:6ae2f7bca550 22 FRAME_CODEC_HANDLE frame_codec;
Azure.IoT Build 0:6ae2f7bca550 23
Azure.IoT Build 0:6ae2f7bca550 24 /* decode */
Azure.IoT Build 0:6ae2f7bca550 25 AMQP_FRAME_RECEIVED_CALLBACK frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 26 AMQP_EMPTY_FRAME_RECEIVED_CALLBACK empty_frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 27 AMQP_FRAME_CODEC_ERROR_CALLBACK error_callback;
Azure.IoT Build 0:6ae2f7bca550 28 void* callback_context;
Azure.IoT Build 0:6ae2f7bca550 29 AMQPVALUE_DECODER_HANDLE decoder;
Azure.IoT Build 0:6ae2f7bca550 30 AMQP_FRAME_DECODE_STATE decode_state;
Azure.IoT Build 0:6ae2f7bca550 31 AMQP_VALUE decoded_performative;
Azure.IoT Build 0:6ae2f7bca550 32 } AMQP_FRAME_CODEC_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 33
Azure.IoT Build 0:6ae2f7bca550 34 static void amqp_value_decoded(void* context, AMQP_VALUE decoded_value)
Azure.IoT Build 0:6ae2f7bca550 35 {
Azure.IoT Build 0:6ae2f7bca550 36 AMQP_FRAME_CODEC_INSTANCE* amqp_frame_codec_instance = (AMQP_FRAME_CODEC_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 37 uint64_t performative_descriptor_ulong;
Azure.IoT Build 0:6ae2f7bca550 38 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 39
Azure.IoT Build 0:6ae2f7bca550 40 /* 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 41 if ((descriptor == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 42 (amqpvalue_get_ulong(descriptor, &performative_descriptor_ulong) != 0) ||
Azure.IoT Build 0:6ae2f7bca550 43 /* 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 44 (performative_descriptor_ulong < AMQP_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 45 (performative_descriptor_ulong > AMQP_CLOSE))
Azure.IoT Build 0:6ae2f7bca550 46 {
Azure.IoT Build 0:6ae2f7bca550 47 /* 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 48 amqp_frame_codec_instance->decode_state = AMQP_FRAME_DECODE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 49 }
Azure.IoT Build 0:6ae2f7bca550 50 else
Azure.IoT Build 0:6ae2f7bca550 51 {
Azure.IoT Build 0:6ae2f7bca550 52 amqp_frame_codec_instance->decoded_performative = decoded_value;
Azure.IoT Build 0:6ae2f7bca550 53 }
Azure.IoT Build 0:6ae2f7bca550 54 }
Azure.IoT Build 0:6ae2f7bca550 55
Azure.IoT Build 0:6ae2f7bca550 56 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 57 {
Azure.IoT Build 0:6ae2f7bca550 58 AMQP_FRAME_CODEC_INSTANCE* amqp_frame_codec_instance = (AMQP_FRAME_CODEC_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 59 uint16_t channel;
Azure.IoT Build 0:6ae2f7bca550 60
Azure.IoT Build 0:6ae2f7bca550 61 switch (amqp_frame_codec_instance->decode_state)
Azure.IoT Build 0:6ae2f7bca550 62 {
Azure.IoT Build 0:6ae2f7bca550 63 default:
Azure.IoT Build 0:6ae2f7bca550 64 /* 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 65 case AMQP_FRAME_DECODE_ERROR:
Azure.IoT Build 0:6ae2f7bca550 66 break;
Azure.IoT Build 0:6ae2f7bca550 67
Azure.IoT Build 0:6ae2f7bca550 68 case AMQP_FRAME_DECODE_FRAME:
Azure.IoT Build 0:6ae2f7bca550 69 /* 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 70 if (type_specific_size < 2)
Azure.IoT Build 0:6ae2f7bca550 71 {
Azure.IoT Build 0:6ae2f7bca550 72 amqp_frame_codec_instance->decode_state = AMQP_FRAME_DECODE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 73
Azure.IoT Build 0:6ae2f7bca550 74 /* 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 75 amqp_frame_codec_instance->error_callback(amqp_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 76 }
Azure.IoT Build 0:6ae2f7bca550 77 else
Azure.IoT Build 0:6ae2f7bca550 78 {
Azure.IoT Build 0:6ae2f7bca550 79 /* Codes_SRS_AMQP_FRAME_CODEC_01_001: [Bytes 6 and 7 of an AMQP frame contain the channel number ] */
Azure.IoT Build 0:6ae2f7bca550 80 channel = ((uint16_t)type_specific[0]) << 8;
Azure.IoT Build 0:6ae2f7bca550 81 channel += type_specific[1];
Azure.IoT Build 0:6ae2f7bca550 82
Azure.IoT Build 0:6ae2f7bca550 83 if (frame_body_size == 0)
Azure.IoT Build 0:6ae2f7bca550 84 {
Azure.IoT Build 0:6ae2f7bca550 85 /* 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 86 /* 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 87 amqp_frame_codec_instance->empty_frame_received_callback(amqp_frame_codec_instance->callback_context, channel);
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 /* 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 92 /* 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 93 amqp_frame_codec_instance->decoded_performative = NULL;
Azure.IoT Build 0:6ae2f7bca550 94
Azure.IoT Build 0:6ae2f7bca550 95 while ((frame_body_size > 0) &&
Azure.IoT Build 0:6ae2f7bca550 96 (amqp_frame_codec_instance->decoded_performative == NULL) &&
Azure.IoT Build 0:6ae2f7bca550 97 (amqp_frame_codec_instance->decode_state != AMQP_FRAME_DECODE_ERROR))
Azure.IoT Build 0:6ae2f7bca550 98 {
Azure.IoT Build 0:6ae2f7bca550 99 /* 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 100 if (amqpvalue_decode_bytes(amqp_frame_codec_instance->decoder, frame_body, 1) != 0)
Azure.IoT Build 0:6ae2f7bca550 101 {
Azure.IoT Build 0:6ae2f7bca550 102 /* 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 103 amqp_frame_codec_instance->decode_state = AMQP_FRAME_DECODE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 104 }
Azure.IoT Build 0:6ae2f7bca550 105 else
Azure.IoT Build 0:6ae2f7bca550 106 {
Azure.IoT Build 0:6ae2f7bca550 107 frame_body_size--;
Azure.IoT Build 0:6ae2f7bca550 108 frame_body++;
Azure.IoT Build 0:6ae2f7bca550 109 }
Azure.IoT Build 0:6ae2f7bca550 110 }
Azure.IoT Build 0:6ae2f7bca550 111
Azure.IoT Build 0:6ae2f7bca550 112 if (amqp_frame_codec_instance->decode_state == AMQP_FRAME_DECODE_ERROR)
Azure.IoT Build 0:6ae2f7bca550 113 {
Azure.IoT Build 0:6ae2f7bca550 114 /* 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 115 amqp_frame_codec_instance->error_callback(amqp_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 116 }
Azure.IoT Build 0:6ae2f7bca550 117 else
Azure.IoT Build 0:6ae2f7bca550 118 {
Azure.IoT Build 0:6ae2f7bca550 119 /* 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 120 /* 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 121 /* 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 122 /* 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 123 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 124 }
Azure.IoT Build 0:6ae2f7bca550 125 }
Azure.IoT Build 0:6ae2f7bca550 126 }
Azure.IoT Build 0:6ae2f7bca550 127 break;
Azure.IoT Build 0:6ae2f7bca550 128 }
Azure.IoT Build 0:6ae2f7bca550 129 }
Azure.IoT Build 0:6ae2f7bca550 130
Azure.IoT Build 0:6ae2f7bca550 131 static int encode_bytes(void* context, const unsigned char* bytes, size_t length)
Azure.IoT Build 0:6ae2f7bca550 132 {
Azure.IoT Build 0:6ae2f7bca550 133 PAYLOAD* payload = (PAYLOAD*)context;
Azure.IoT Build 0:6ae2f7bca550 134 (void)memcpy((unsigned char*)payload->bytes + payload->length, bytes, length);
Azure.IoT Build 0:6ae2f7bca550 135 payload->length += length;
Azure.IoT Build 0:6ae2f7bca550 136 return 0;
Azure.IoT Build 0:6ae2f7bca550 137 }
Azure.IoT Build 0:6ae2f7bca550 138
Azure.IoT Build 0:6ae2f7bca550 139 /* 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 140 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 141 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 142 {
Azure.IoT Build 0:6ae2f7bca550 143 AMQP_FRAME_CODEC_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 144
Azure.IoT Build 0:6ae2f7bca550 145 /* 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 146 if ((frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 147 (frame_received_callback == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 148 (empty_frame_received_callback == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 149 (amqp_frame_codec_error_callback == NULL))
Azure.IoT Build 0:6ae2f7bca550 150 {
Azure.IoT Build 0:6ae2f7bca550 151 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 152 }
Azure.IoT Build 0:6ae2f7bca550 153 else
Azure.IoT Build 0:6ae2f7bca550 154 {
AzureIoTClient 20:206846c14c80 155 result = (AMQP_FRAME_CODEC_INSTANCE*)malloc(sizeof(AMQP_FRAME_CODEC_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 156 /* 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.] */
Azure.IoT Build 0:6ae2f7bca550 157 if (result != NULL)
Azure.IoT Build 0:6ae2f7bca550 158 {
Azure.IoT Build 0:6ae2f7bca550 159 result->frame_codec = frame_codec;
Azure.IoT Build 0:6ae2f7bca550 160 result->frame_received_callback = frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 161 result->empty_frame_received_callback = empty_frame_received_callback;
Azure.IoT Build 0:6ae2f7bca550 162 result->error_callback = amqp_frame_codec_error_callback;
Azure.IoT Build 0:6ae2f7bca550 163 result->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 164 result->decode_state = AMQP_FRAME_DECODE_FRAME;
Azure.IoT Build 0:6ae2f7bca550 165
Azure.IoT Build 0:6ae2f7bca550 166 /* 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 167 result->decoder = amqpvalue_decoder_create(amqp_value_decoded, result);
Azure.IoT Build 0:6ae2f7bca550 168 if (result->decoder == NULL)
Azure.IoT Build 0:6ae2f7bca550 169 {
Azure.IoT Build 0:6ae2f7bca550 170 /* Codes_SRS_AMQP_FRAME_CODEC_01_019: [If creating the decoder fails, amqp_frame_codec_create shall fail and return NULL.] */
AzureIoTClient 20:206846c14c80 171 free(result);
Azure.IoT Build 0:6ae2f7bca550 172 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 173 }
Azure.IoT Build 0:6ae2f7bca550 174 else
Azure.IoT Build 0:6ae2f7bca550 175 {
Azure.IoT Build 0:6ae2f7bca550 176 /* 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 177 if (frame_codec_subscribe(frame_codec, FRAME_TYPE_AMQP, frame_received, result) != 0)
Azure.IoT Build 0:6ae2f7bca550 178 {
Azure.IoT Build 0:6ae2f7bca550 179 /* Codes_SRS_AMQP_FRAME_CODEC_01_014: [If subscribing for AMQP frames fails, amqp_frame_codec_create shall fail and return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 180 amqpvalue_decoder_destroy(result->decoder);
AzureIoTClient 20:206846c14c80 181 free(result);
Azure.IoT Build 0:6ae2f7bca550 182 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 183 }
Azure.IoT Build 0:6ae2f7bca550 184 }
Azure.IoT Build 0:6ae2f7bca550 185 }
Azure.IoT Build 0:6ae2f7bca550 186 }
Azure.IoT Build 0:6ae2f7bca550 187
Azure.IoT Build 0:6ae2f7bca550 188 return result;
Azure.IoT Build 0:6ae2f7bca550 189 }
Azure.IoT Build 0:6ae2f7bca550 190
Azure.IoT Build 0:6ae2f7bca550 191 void amqp_frame_codec_destroy(AMQP_FRAME_CODEC_HANDLE amqp_frame_codec)
Azure.IoT Build 0:6ae2f7bca550 192 {
Azure.IoT Build 0:6ae2f7bca550 193 if (amqp_frame_codec != NULL)
Azure.IoT Build 0:6ae2f7bca550 194 {
Azure.IoT Build 0:6ae2f7bca550 195 /* 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 196 (void)frame_codec_unsubscribe(amqp_frame_codec->frame_codec, FRAME_TYPE_AMQP);
Azure.IoT Build 0:6ae2f7bca550 197
Azure.IoT Build 0:6ae2f7bca550 198 /* 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 199 amqpvalue_decoder_destroy(amqp_frame_codec->decoder);
Azure.IoT Build 0:6ae2f7bca550 200
Azure.IoT Build 0:6ae2f7bca550 201 /* 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 202 free(amqp_frame_codec);
Azure.IoT Build 0:6ae2f7bca550 203 }
Azure.IoT Build 0:6ae2f7bca550 204 }
Azure.IoT Build 0:6ae2f7bca550 205
Azure.IoT Build 0:6ae2f7bca550 206 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 207 {
Azure.IoT Build 0:6ae2f7bca550 208 int result;
Azure.IoT Build 0:6ae2f7bca550 209
Azure.IoT Build 0:6ae2f7bca550 210 /* 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 211 if ((amqp_frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 212 (performative == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 213 (on_bytes_encoded == NULL))
Azure.IoT Build 0:6ae2f7bca550 214 {
AzureIoTClient 19:000ab4e6a2c1 215 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 216 }
Azure.IoT Build 0:6ae2f7bca550 217 else
Azure.IoT Build 0:6ae2f7bca550 218 {
Azure.IoT Build 0:6ae2f7bca550 219 AMQP_VALUE descriptor;
Azure.IoT Build 0:6ae2f7bca550 220 uint64_t performative_ulong;
Azure.IoT Build 0:6ae2f7bca550 221 size_t encoded_size;
Azure.IoT Build 0:6ae2f7bca550 222
Azure.IoT Build 0:6ae2f7bca550 223 if (((descriptor = amqpvalue_get_inplace_descriptor(performative)) == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 224 (amqpvalue_get_ulong(descriptor, &performative_ulong) != 0) ||
Azure.IoT Build 0:6ae2f7bca550 225 /* 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.] */
Azure.IoT Build 0:6ae2f7bca550 226 (performative_ulong < AMQP_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 227 (performative_ulong > AMQP_CLOSE))
Azure.IoT Build 0:6ae2f7bca550 228 {
Azure.IoT Build 0:6ae2f7bca550 229 /* 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 19:000ab4e6a2c1 230 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 231 }
Azure.IoT Build 0:6ae2f7bca550 232 /* 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 233 else if (amqpvalue_get_encoded_size(performative, &encoded_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 234 {
Azure.IoT Build 0:6ae2f7bca550 235 /* 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 19:000ab4e6a2c1 236 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 237 }
Azure.IoT Build 0:6ae2f7bca550 238 else
Azure.IoT Build 0:6ae2f7bca550 239 {
AzureIoTClient 20:206846c14c80 240 unsigned char* amqp_performative_bytes = (unsigned char*)malloc(encoded_size);
Azure.IoT Build 0:6ae2f7bca550 241 if (amqp_performative_bytes == NULL)
Azure.IoT Build 0:6ae2f7bca550 242 {
AzureIoTClient 19:000ab4e6a2c1 243 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 244 }
Azure.IoT Build 0:6ae2f7bca550 245 else
Azure.IoT Build 0:6ae2f7bca550 246 {
AzureIoTClient 20:206846c14c80 247 PAYLOAD* new_payloads = (PAYLOAD*)malloc(sizeof(PAYLOAD) * (payload_count + 1));
Azure.IoT Build 0:6ae2f7bca550 248 if (new_payloads == NULL)
Azure.IoT Build 0:6ae2f7bca550 249 {
AzureIoTClient 19:000ab4e6a2c1 250 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 251 }
Azure.IoT Build 0:6ae2f7bca550 252 else
Azure.IoT Build 0:6ae2f7bca550 253 {
Azure.IoT Build 0:6ae2f7bca550 254 /* 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 255 /* 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 256 new_payloads[0].bytes = amqp_performative_bytes;
Azure.IoT Build 0:6ae2f7bca550 257 new_payloads[0].length = 0;
Azure.IoT Build 0:6ae2f7bca550 258
Azure.IoT Build 0:6ae2f7bca550 259 if (payload_count > 0)
Azure.IoT Build 0:6ae2f7bca550 260 {
Azure.IoT Build 0:6ae2f7bca550 261 (void)memcpy(new_payloads + 1, payloads, sizeof(PAYLOAD) * payload_count);
Azure.IoT Build 0:6ae2f7bca550 262 }
Azure.IoT Build 0:6ae2f7bca550 263
Azure.IoT Build 0:6ae2f7bca550 264 if (amqpvalue_encode(performative, encode_bytes, &new_payloads[0]) != 0)
Azure.IoT Build 0:6ae2f7bca550 265 {
AzureIoTClient 19:000ab4e6a2c1 266 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 267 }
Azure.IoT Build 0:6ae2f7bca550 268 else
Azure.IoT Build 0:6ae2f7bca550 269 {
AzureIoTClient 6:641a9672db08 270 unsigned char channel_bytes[2];
AzureIoTClient 6:641a9672db08 271
AzureIoTClient 6:641a9672db08 272 channel_bytes[0] = channel >> 8;
AzureIoTClient 6:641a9672db08 273 channel_bytes[1] = channel & 0xFF;
Azure.IoT Build 0:6ae2f7bca550 274
Azure.IoT Build 0:6ae2f7bca550 275 /* Codes_SRS_AMQP_FRAME_CODEC_01_005: [Bytes 6 and 7 of an AMQP frame contain the channel number ] */
Azure.IoT Build 0:6ae2f7bca550 276 /* 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 277 /* 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 278 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 279 {
Azure.IoT Build 0:6ae2f7bca550 280 /* 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 19:000ab4e6a2c1 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_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 286 result = 0;
Azure.IoT Build 0:6ae2f7bca550 287 }
Azure.IoT Build 0:6ae2f7bca550 288 }
Azure.IoT Build 0:6ae2f7bca550 289
AzureIoTClient 20:206846c14c80 290 free(new_payloads);
Azure.IoT Build 0:6ae2f7bca550 291 }
Azure.IoT Build 0:6ae2f7bca550 292
AzureIoTClient 20:206846c14c80 293 free(amqp_performative_bytes);
Azure.IoT Build 0:6ae2f7bca550 294 }
Azure.IoT Build 0:6ae2f7bca550 295 }
Azure.IoT Build 0:6ae2f7bca550 296 }
Azure.IoT Build 0:6ae2f7bca550 297
Azure.IoT Build 0:6ae2f7bca550 298 return result;
Azure.IoT Build 0:6ae2f7bca550 299 }
Azure.IoT Build 0:6ae2f7bca550 300
Azure.IoT Build 0:6ae2f7bca550 301 /* 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 302 /* 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 303 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 304 {
Azure.IoT Build 0:6ae2f7bca550 305 int result;
Azure.IoT Build 0:6ae2f7bca550 306
Azure.IoT Build 0:6ae2f7bca550 307 /* 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 308 if (amqp_frame_codec == NULL)
Azure.IoT Build 0:6ae2f7bca550 309 {
AzureIoTClient 19:000ab4e6a2c1 310 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 311 }
Azure.IoT Build 0:6ae2f7bca550 312 else
Azure.IoT Build 0:6ae2f7bca550 313 {
AzureIoTClient 6:641a9672db08 314 unsigned char channel_bytes[2];
AzureIoTClient 6:641a9672db08 315
AzureIoTClient 6:641a9672db08 316 channel_bytes[0] = channel >> 8;
AzureIoTClient 6:641a9672db08 317 channel_bytes[1] = channel & 0xFF;
Azure.IoT Build 0:6ae2f7bca550 318
Azure.IoT Build 0:6ae2f7bca550 319 /* 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 320 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 321 {
Azure.IoT Build 0:6ae2f7bca550 322 /* 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 19:000ab4e6a2c1 323 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 324 }
Azure.IoT Build 0:6ae2f7bca550 325 else
Azure.IoT Build 0:6ae2f7bca550 326 {
Azure.IoT Build 0:6ae2f7bca550 327 /* Codes_SRS_AMQP_FRAME_CODEC_01_043: [On success, amqp_frame_codec_encode_empty_frame shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 328 result = 0;
Azure.IoT Build 0:6ae2f7bca550 329 }
Azure.IoT Build 0:6ae2f7bca550 330 }
Azure.IoT Build 0:6ae2f7bca550 331
Azure.IoT Build 0:6ae2f7bca550 332 return result;
Azure.IoT Build 0:6ae2f7bca550 333 }