A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

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?

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