A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Mon May 22 10:35:21 2017 -0700
Revision:
25:1101516ee67d
Parent:
23:1111ee8bcba4
Child:
28:add19eb7defa
1.1.15

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 <stdbool.h>
Azure.IoT Build 0:6ae2f7bca550 7 #include <string.h>
AzureIoTClient 22:524bded3f7a8 8 #include <inttypes.h>
AzureIoTClient 19:000ab4e6a2c1 9 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 21:f9c433d8e6ca 10 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 12:b30dacf113f2 12 #include "azure_c_shared_utility/singlylinkedlist.h"
Azure.IoT Build 0:6ae2f7bca550 13 #include "azure_uamqp_c/frame_codec.h"
Azure.IoT Build 0:6ae2f7bca550 14 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 15
Azure.IoT Build 0:6ae2f7bca550 16 #define FRAME_HEADER_SIZE 8
Azure.IoT Build 0:6ae2f7bca550 17 #define MAX_TYPE_SPECIFIC_SIZE ((255 * 4) - 6)
Azure.IoT Build 0:6ae2f7bca550 18
Azure.IoT Build 0:6ae2f7bca550 19 typedef enum RECEIVE_FRAME_STATE_TAG
Azure.IoT Build 0:6ae2f7bca550 20 {
Azure.IoT Build 0:6ae2f7bca550 21 RECEIVE_FRAME_STATE_FRAME_SIZE,
Azure.IoT Build 0:6ae2f7bca550 22 RECEIVE_FRAME_STATE_DOFF,
Azure.IoT Build 0:6ae2f7bca550 23 RECEIVE_FRAME_STATE_FRAME_TYPE,
Azure.IoT Build 0:6ae2f7bca550 24 RECEIVE_FRAME_STATE_TYPE_SPECIFIC,
Azure.IoT Build 0:6ae2f7bca550 25 RECEIVE_FRAME_STATE_FRAME_BODY,
Azure.IoT Build 0:6ae2f7bca550 26 RECEIVE_FRAME_STATE_ERROR
Azure.IoT Build 0:6ae2f7bca550 27 } RECEIVE_FRAME_STATE;
Azure.IoT Build 0:6ae2f7bca550 28
Azure.IoT Build 0:6ae2f7bca550 29 typedef struct SUBSCRIPTION_TAG
Azure.IoT Build 0:6ae2f7bca550 30 {
Azure.IoT Build 0:6ae2f7bca550 31 uint8_t frame_type;
Azure.IoT Build 0:6ae2f7bca550 32 ON_FRAME_RECEIVED on_frame_received;
Azure.IoT Build 0:6ae2f7bca550 33 void* callback_context;
Azure.IoT Build 0:6ae2f7bca550 34 } SUBSCRIPTION;
Azure.IoT Build 0:6ae2f7bca550 35
Azure.IoT Build 0:6ae2f7bca550 36 typedef struct FRAME_CODEC_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 37 {
Azure.IoT Build 0:6ae2f7bca550 38 /* subscriptions */
AzureIoTClient 12:b30dacf113f2 39 SINGLYLINKEDLIST_HANDLE subscription_list;
Azure.IoT Build 0:6ae2f7bca550 40
Azure.IoT Build 0:6ae2f7bca550 41 /* decode frame */
Azure.IoT Build 0:6ae2f7bca550 42 RECEIVE_FRAME_STATE receive_frame_state;
Azure.IoT Build 0:6ae2f7bca550 43 size_t receive_frame_pos;
Azure.IoT Build 0:6ae2f7bca550 44 uint32_t receive_frame_size;
Azure.IoT Build 0:6ae2f7bca550 45 uint32_t type_specific_size;
Azure.IoT Build 0:6ae2f7bca550 46 uint8_t receive_frame_doff;
Azure.IoT Build 0:6ae2f7bca550 47 uint8_t receive_frame_type;
Azure.IoT Build 0:6ae2f7bca550 48 SUBSCRIPTION* receive_frame_subscription;
Azure.IoT Build 0:6ae2f7bca550 49 unsigned char* receive_frame_bytes;
Azure.IoT Build 0:6ae2f7bca550 50 ON_FRAME_CODEC_ERROR on_frame_codec_error;
Azure.IoT Build 0:6ae2f7bca550 51 void* on_frame_codec_error_callback_context;
Azure.IoT Build 0:6ae2f7bca550 52
Azure.IoT Build 0:6ae2f7bca550 53 /* configuration */
Azure.IoT Build 0:6ae2f7bca550 54 uint32_t max_frame_size;
Azure.IoT Build 0:6ae2f7bca550 55 } FRAME_CODEC_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 56
Azure.IoT Build 0:6ae2f7bca550 57 static bool find_subscription_by_frame_type(LIST_ITEM_HANDLE list_item, const void* match_context)
Azure.IoT Build 0:6ae2f7bca550 58 {
Azure.IoT Build 0:6ae2f7bca550 59 bool result;
AzureIoTClient 12:b30dacf113f2 60 SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item);
Azure.IoT Build 0:6ae2f7bca550 61
Azure.IoT Build 0:6ae2f7bca550 62 if (subscription == NULL)
Azure.IoT Build 0:6ae2f7bca550 63 {
AzureIoTClient 22:524bded3f7a8 64 LogError("Could not get subscription information from the list item");
Azure.IoT Build 0:6ae2f7bca550 65 result = false;
Azure.IoT Build 0:6ae2f7bca550 66 }
Azure.IoT Build 0:6ae2f7bca550 67 else
Azure.IoT Build 0:6ae2f7bca550 68 {
Azure.IoT Build 0:6ae2f7bca550 69 result = subscription->frame_type == *((uint8_t*)match_context) ? true : false;
Azure.IoT Build 0:6ae2f7bca550 70 }
Azure.IoT Build 0:6ae2f7bca550 71
Azure.IoT Build 0:6ae2f7bca550 72 return result;
Azure.IoT Build 0:6ae2f7bca550 73 }
Azure.IoT Build 0:6ae2f7bca550 74
Azure.IoT Build 5:ae49385aff34 75 FRAME_CODEC_HANDLE frame_codec_create(ON_FRAME_CODEC_ERROR on_frame_codec_error, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 76 {
Azure.IoT Build 0:6ae2f7bca550 77 FRAME_CODEC_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 78
Azure.IoT Build 0:6ae2f7bca550 79 /* Codes_SRS_FRAME_CODEC_01_020: [If the on_frame_codec_error argument is NULL, frame_codec_create shall return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 80 /* Codes_SRS_FRAME_CODEC_01_104: [The callback_context shall be allowed to be NULL.] */
Azure.IoT Build 0:6ae2f7bca550 81 if (on_frame_codec_error == NULL)
Azure.IoT Build 0:6ae2f7bca550 82 {
AzureIoTClient 22:524bded3f7a8 83 LogError("NULL on_frame_codec_error");
AzureIoTClient 22:524bded3f7a8 84 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 85 }
Azure.IoT Build 0:6ae2f7bca550 86 else
Azure.IoT Build 0:6ae2f7bca550 87 {
AzureIoTClient 23:1111ee8bcba4 88 result = (FRAME_CODEC_INSTANCE*)malloc(sizeof(FRAME_CODEC_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 89 /* Codes_SRS_FRAME_CODEC_01_022: [If allocating memory for the frame_codec instance fails, frame_codec_create shall return NULL.] */
AzureIoTClient 22:524bded3f7a8 90 if (result == NULL)
AzureIoTClient 22:524bded3f7a8 91 {
AzureIoTClient 22:524bded3f7a8 92 LogError("Could not allocate frame codec");
AzureIoTClient 22:524bded3f7a8 93 }
AzureIoTClient 22:524bded3f7a8 94 else
AzureIoTClient 22:524bded3f7a8 95 {
Azure.IoT Build 0:6ae2f7bca550 96 /* Codes_SRS_FRAME_CODEC_01_021: [frame_codec_create shall create a new instance of frame_codec and return a non-NULL handle to it on success.] */
Azure.IoT Build 0:6ae2f7bca550 97 result->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE;
Azure.IoT Build 0:6ae2f7bca550 98 result->on_frame_codec_error = on_frame_codec_error;
Azure.IoT Build 0:6ae2f7bca550 99 result->on_frame_codec_error_callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 100 result->receive_frame_pos = 0;
Azure.IoT Build 0:6ae2f7bca550 101 result->receive_frame_size = 0;
Azure.IoT Build 0:6ae2f7bca550 102 result->receive_frame_bytes = NULL;
AzureIoTClient 12:b30dacf113f2 103 result->subscription_list = singlylinkedlist_create();
Azure.IoT Build 0:6ae2f7bca550 104
Azure.IoT Build 0:6ae2f7bca550 105 /* Codes_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */
Azure.IoT Build 0:6ae2f7bca550 106 result->max_frame_size = 512;
Azure.IoT Build 0:6ae2f7bca550 107 }
Azure.IoT Build 0:6ae2f7bca550 108 }
Azure.IoT Build 0:6ae2f7bca550 109
Azure.IoT Build 0:6ae2f7bca550 110 return result;
Azure.IoT Build 0:6ae2f7bca550 111 }
Azure.IoT Build 0:6ae2f7bca550 112
Azure.IoT Build 0:6ae2f7bca550 113 void frame_codec_destroy(FRAME_CODEC_HANDLE frame_codec)
Azure.IoT Build 0:6ae2f7bca550 114 {
Azure.IoT Build 0:6ae2f7bca550 115 /* Codes_SRS_FRAME_CODEC_01_024: [If frame_codec is NULL, frame_codec_destroy shall do nothing.] */
AzureIoTClient 22:524bded3f7a8 116 if (frame_codec == NULL)
AzureIoTClient 22:524bded3f7a8 117 {
AzureIoTClient 22:524bded3f7a8 118 LogError("NULL frame_codec");
AzureIoTClient 22:524bded3f7a8 119 }
AzureIoTClient 22:524bded3f7a8 120 else
AzureIoTClient 22:524bded3f7a8 121 {
Azure.IoT Build 0:6ae2f7bca550 122 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 123
AzureIoTClient 12:b30dacf113f2 124 singlylinkedlist_destroy(frame_codec_data->subscription_list);
Azure.IoT Build 0:6ae2f7bca550 125 if (frame_codec_data->receive_frame_bytes != NULL)
Azure.IoT Build 0:6ae2f7bca550 126 {
AzureIoTClient 21:f9c433d8e6ca 127 free(frame_codec_data->receive_frame_bytes);
Azure.IoT Build 0:6ae2f7bca550 128 }
Azure.IoT Build 0:6ae2f7bca550 129
Azure.IoT Build 0:6ae2f7bca550 130 /* Codes_SRS_FRAME_CODEC_01_023: [frame_codec_destroy shall free all resources associated with a frame_codec instance.] */
AzureIoTClient 21:f9c433d8e6ca 131 free(frame_codec);
Azure.IoT Build 0:6ae2f7bca550 132 }
Azure.IoT Build 0:6ae2f7bca550 133 }
Azure.IoT Build 0:6ae2f7bca550 134
Azure.IoT Build 0:6ae2f7bca550 135 int frame_codec_set_max_frame_size(FRAME_CODEC_HANDLE frame_codec, uint32_t max_frame_size)
Azure.IoT Build 0:6ae2f7bca550 136 {
Azure.IoT Build 0:6ae2f7bca550 137 int result;
Azure.IoT Build 0:6ae2f7bca550 138 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 139
Azure.IoT Build 0:6ae2f7bca550 140 /* Codes_SRS_FRAME_CODEC_01_077: [If frame_codec is NULL, frame_codec_set_max_frame_size shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 141 if ((frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 142 /* Codes_SRS_FRAME_CODEC_01_078: [If max_frame_size is invalid according to the AMQP standard, frame_codec_set_max_frame_size shall return a non-zero value.] */
AzureIoTClient 22:524bded3f7a8 143 (max_frame_size < FRAME_HEADER_SIZE) ||
AzureIoTClient 22:524bded3f7a8 144 /* Codes_SRS_FRAME_CODEC_01_081: [If a frame being decoded already has a size bigger than the max_frame_size argument then frame_codec_set_max_frame_size shall return a non-zero value and the previous frame size shall be kept.] */
AzureIoTClient 22:524bded3f7a8 145 ((max_frame_size < frame_codec_data->receive_frame_size) && (frame_codec_data->receive_frame_state != RECEIVE_FRAME_STATE_FRAME_SIZE)))
AzureIoTClient 22:524bded3f7a8 146 {
AzureIoTClient 22:524bded3f7a8 147 LogError("Bad arguments: frame_codec = %p, max_frame_size = %" PRIu32,
AzureIoTClient 22:524bded3f7a8 148 frame_codec,
AzureIoTClient 22:524bded3f7a8 149 max_frame_size);
AzureIoTClient 22:524bded3f7a8 150 result = __FAILURE__;
AzureIoTClient 22:524bded3f7a8 151 }
AzureIoTClient 22:524bded3f7a8 152 /* Codes_SRS_FRAME_CODEC_01_097: [Setting a frame size on a frame_codec that had a decode error shall fail.] */
AzureIoTClient 22:524bded3f7a8 153 else if (frame_codec_data->receive_frame_state == RECEIVE_FRAME_STATE_ERROR)
Azure.IoT Build 0:6ae2f7bca550 154 {
AzureIoTClient 22:524bded3f7a8 155 LogError("Frame codec in error state");
AzureIoTClient 22:524bded3f7a8 156 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 157 }
Azure.IoT Build 0:6ae2f7bca550 158 else
Azure.IoT Build 0:6ae2f7bca550 159 {
Azure.IoT Build 0:6ae2f7bca550 160 /* Codes_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */
Azure.IoT Build 0:6ae2f7bca550 161 /* Codes_SRS_FRAME_CODEC_01_079: [The new frame size shall take effect immediately, even for a frame that is being decoded at the time of the call.] */
Azure.IoT Build 0:6ae2f7bca550 162 frame_codec_data->max_frame_size = max_frame_size;
Azure.IoT Build 0:6ae2f7bca550 163
Azure.IoT Build 0:6ae2f7bca550 164 /* Codes_SRS_FRAME_CODEC_01_076: [On success, frame_codec_set_max_frame_size shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 165 result = 0;
Azure.IoT Build 0:6ae2f7bca550 166 }
AzureIoTClient 22:524bded3f7a8 167
Azure.IoT Build 0:6ae2f7bca550 168 return result;
Azure.IoT Build 0:6ae2f7bca550 169 }
Azure.IoT Build 0:6ae2f7bca550 170
Azure.IoT Build 0:6ae2f7bca550 171 /* Codes_SRS_FRAME_CODEC_01_001: [Frames are divided into three distinct areas: a fixed width frame header, a variable width extended header, and a variable width frame body.] */
Azure.IoT Build 0:6ae2f7bca550 172 /* Codes_SRS_FRAME_CODEC_01_002: [frame header The frame header is a fixed size (8 byte) structure that precedes each frame.] */
Azure.IoT Build 0:6ae2f7bca550 173 /* Codes_SRS_FRAME_CODEC_01_003: [The frame header includes mandatory information necessary to parse the rest of the frame including size and type information.] */
Azure.IoT Build 0:6ae2f7bca550 174 /* Codes_SRS_FRAME_CODEC_01_004: [extended header The extended header is a variable width area preceding the frame body.] */
Azure.IoT Build 0:6ae2f7bca550 175 /* Codes_SRS_FRAME_CODEC_01_007: [frame body The frame body is a variable width sequence of bytes the format of which depends on the frame type.] */
Azure.IoT Build 0:6ae2f7bca550 176 /* Codes_SRS_FRAME_CODEC_01_028: [The sequence of bytes shall be decoded according to the AMQP ISO.] */
Azure.IoT Build 0:6ae2f7bca550 177 /* Codes_SRS_FRAME_CODEC_01_029: [The sequence of bytes does not have to be a complete frame, frame_codec shall be responsible for maintaining decoding state between frame_codec_receive_bytes calls.] */
Azure.IoT Build 0:6ae2f7bca550 178 int frame_codec_receive_bytes(FRAME_CODEC_HANDLE frame_codec, const unsigned char* buffer, size_t size)
Azure.IoT Build 0:6ae2f7bca550 179 {
AzureIoTClient 19:000ab4e6a2c1 180 int result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 181 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 182
Azure.IoT Build 0:6ae2f7bca550 183 /* Codes_SRS_FRAME_CODEC_01_026: [If frame_codec or buffer are NULL, frame_codec_receive_bytes shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 184 if ((frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 185 (buffer == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 186 /* Codes_SRS_FRAME_CODEC_01_027: [If size is zero, frame_codec_receive_bytes shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 187 (size == 0))
Azure.IoT Build 0:6ae2f7bca550 188 {
AzureIoTClient 22:524bded3f7a8 189 LogError("Bad arguments: frame_codec = %p, buffer = %p, size = %u",
AzureIoTClient 22:524bded3f7a8 190 frame_codec,
AzureIoTClient 22:524bded3f7a8 191 buffer,
AzureIoTClient 22:524bded3f7a8 192 (unsigned int)size);
AzureIoTClient 22:524bded3f7a8 193 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 194 }
Azure.IoT Build 0:6ae2f7bca550 195 else
Azure.IoT Build 0:6ae2f7bca550 196 {
Azure.IoT Build 0:6ae2f7bca550 197 while (size > 0)
Azure.IoT Build 0:6ae2f7bca550 198 {
Azure.IoT Build 0:6ae2f7bca550 199 switch (frame_codec_data->receive_frame_state)
Azure.IoT Build 0:6ae2f7bca550 200 {
Azure.IoT Build 0:6ae2f7bca550 201 default:
Azure.IoT Build 0:6ae2f7bca550 202 case RECEIVE_FRAME_STATE_ERROR:
Azure.IoT Build 0:6ae2f7bca550 203 /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
AzureIoTClient 22:524bded3f7a8 204 LogError("Frame codec is in error state");
AzureIoTClient 19:000ab4e6a2c1 205 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 206 size = 0;
Azure.IoT Build 0:6ae2f7bca550 207 break;
Azure.IoT Build 0:6ae2f7bca550 208
Azure.IoT Build 0:6ae2f7bca550 209 /* Codes_SRS_FRAME_CODEC_01_008: [SIZE Bytes 0-3 of the frame header contain the frame size.] */
Azure.IoT Build 0:6ae2f7bca550 210 case RECEIVE_FRAME_STATE_FRAME_SIZE:
Azure.IoT Build 0:6ae2f7bca550 211 /* Codes_SRS_FRAME_CODEC_01_009: [This is an unsigned 32-bit integer that MUST contain the total frame size of the frame header, extended header, and frame body.] */
Azure.IoT Build 0:6ae2f7bca550 212 frame_codec_data->receive_frame_size += buffer[0] << (24 - frame_codec_data->receive_frame_pos * 8);
Azure.IoT Build 0:6ae2f7bca550 213 buffer++;
Azure.IoT Build 0:6ae2f7bca550 214 size--;
Azure.IoT Build 0:6ae2f7bca550 215 frame_codec_data->receive_frame_pos++;
Azure.IoT Build 0:6ae2f7bca550 216
Azure.IoT Build 0:6ae2f7bca550 217 if (frame_codec_data->receive_frame_pos == 4)
Azure.IoT Build 0:6ae2f7bca550 218 {
Azure.IoT Build 0:6ae2f7bca550 219 /* Codes_SRS_FRAME_CODEC_01_010: [The frame is malformed if the size is less than the size of the frame header (8 bytes).] */
Azure.IoT Build 0:6ae2f7bca550 220 if ((frame_codec_data->receive_frame_size < FRAME_HEADER_SIZE) ||
Azure.IoT Build 0:6ae2f7bca550 221 /* Codes_SRS_FRAME_CODEC_01_096: [If a frame bigger than the current max frame size is received, frame_codec_receive_bytes shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 222 (frame_codec_data->receive_frame_size > frame_codec_data->max_frame_size))
Azure.IoT Build 0:6ae2f7bca550 223 {
Azure.IoT Build 0:6ae2f7bca550 224 /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
Azure.IoT Build 0:6ae2f7bca550 225 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 226 /* Codes_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the on_frame_codec_error_callback_context argument passed to frame_codec_create.] */
Azure.IoT Build 0:6ae2f7bca550 227 frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context);
AzureIoTClient 22:524bded3f7a8 228 LogError("Received frame size is too big");
AzureIoTClient 19:000ab4e6a2c1 229 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 230 }
Azure.IoT Build 0:6ae2f7bca550 231 else
Azure.IoT Build 0:6ae2f7bca550 232 {
Azure.IoT Build 0:6ae2f7bca550 233 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_DOFF;
Azure.IoT Build 0:6ae2f7bca550 234 result = 0;
Azure.IoT Build 0:6ae2f7bca550 235 }
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 result = 0;
Azure.IoT Build 0:6ae2f7bca550 240 }
Azure.IoT Build 0:6ae2f7bca550 241
Azure.IoT Build 0:6ae2f7bca550 242 break;
Azure.IoT Build 0:6ae2f7bca550 243
Azure.IoT Build 0:6ae2f7bca550 244 case RECEIVE_FRAME_STATE_DOFF:
Azure.IoT Build 0:6ae2f7bca550 245 /* Codes_SRS_FRAME_CODEC_01_011: [DOFF Byte 4 of the frame header is the data offset.] */
Azure.IoT Build 0:6ae2f7bca550 246 /* Codes_SRS_FRAME_CODEC_01_013: [The value of the data offset is an unsigned, 8-bit integer specifying a count of 4-byte words.] */
Azure.IoT Build 0:6ae2f7bca550 247 /* Codes_SRS_FRAME_CODEC_01_012: [This gives the position of the body within the frame.] */
Azure.IoT Build 0:6ae2f7bca550 248 frame_codec_data->receive_frame_doff = buffer[0];
Azure.IoT Build 0:6ae2f7bca550 249 buffer++;
Azure.IoT Build 0:6ae2f7bca550 250 size--;
Azure.IoT Build 0:6ae2f7bca550 251
Azure.IoT Build 0:6ae2f7bca550 252 /* Codes_SRS_FRAME_CODEC_01_014: [Due to the mandatory 8-byte frame header, the frame is malformed if the value is less than 2.] */
Azure.IoT Build 0:6ae2f7bca550 253 if (frame_codec_data->receive_frame_doff < 2)
Azure.IoT Build 0:6ae2f7bca550 254 {
Azure.IoT Build 0:6ae2f7bca550 255 /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
Azure.IoT Build 0:6ae2f7bca550 256 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 257
Azure.IoT Build 0:6ae2f7bca550 258 /* Codes_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the on_frame_codec_error_callback_context argument passed to frame_codec_create.] */
Azure.IoT Build 0:6ae2f7bca550 259 frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context);
Azure.IoT Build 0:6ae2f7bca550 260
AzureIoTClient 22:524bded3f7a8 261 LogError("Malformed frame received");
AzureIoTClient 22:524bded3f7a8 262 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 263 }
Azure.IoT Build 0:6ae2f7bca550 264 else
Azure.IoT Build 0:6ae2f7bca550 265 {
Azure.IoT Build 0:6ae2f7bca550 266 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_TYPE;
Azure.IoT Build 0:6ae2f7bca550 267 result = 0;
Azure.IoT Build 0:6ae2f7bca550 268 }
Azure.IoT Build 0:6ae2f7bca550 269
Azure.IoT Build 0:6ae2f7bca550 270 break;
Azure.IoT Build 0:6ae2f7bca550 271
Azure.IoT Build 0:6ae2f7bca550 272 case RECEIVE_FRAME_STATE_FRAME_TYPE:
Azure.IoT Build 0:6ae2f7bca550 273 {
Azure.IoT Build 0:6ae2f7bca550 274 LIST_ITEM_HANDLE item_handle;
Azure.IoT Build 0:6ae2f7bca550 275 frame_codec_data->type_specific_size = (frame_codec_data->receive_frame_doff * 4) - 6;
Azure.IoT Build 0:6ae2f7bca550 276
Azure.IoT Build 0:6ae2f7bca550 277 /* Codes_SRS_FRAME_CODEC_01_015: [TYPE Byte 5 of the frame header is a type code.] */
Azure.IoT Build 0:6ae2f7bca550 278 frame_codec_data->receive_frame_type = buffer[0];
Azure.IoT Build 0:6ae2f7bca550 279 buffer++;
Azure.IoT Build 0:6ae2f7bca550 280 size--;
Azure.IoT Build 0:6ae2f7bca550 281
Azure.IoT Build 0:6ae2f7bca550 282 /* Codes_SRS_FRAME_CODEC_01_035: [After successfully registering a callback for a certain frame type, when subsequently that frame type is received the callbacks shall be invoked, passing to it the received frame and the callback_context value.] */
AzureIoTClient 12:b30dacf113f2 283 item_handle = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &frame_codec_data->receive_frame_type);
Azure.IoT Build 0:6ae2f7bca550 284 if (item_handle == NULL)
Azure.IoT Build 0:6ae2f7bca550 285 {
Azure.IoT Build 0:6ae2f7bca550 286 frame_codec_data->receive_frame_subscription = NULL;
Azure.IoT Build 0:6ae2f7bca550 287 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC;
Azure.IoT Build 0:6ae2f7bca550 288 result = 0;
Azure.IoT Build 0:6ae2f7bca550 289 break;
Azure.IoT Build 0:6ae2f7bca550 290 }
Azure.IoT Build 0:6ae2f7bca550 291 else
Azure.IoT Build 0:6ae2f7bca550 292 {
AzureIoTClient 12:b30dacf113f2 293 frame_codec_data->receive_frame_subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(item_handle);
Azure.IoT Build 0:6ae2f7bca550 294 if (frame_codec_data->receive_frame_subscription == NULL)
Azure.IoT Build 0:6ae2f7bca550 295 {
Azure.IoT Build 0:6ae2f7bca550 296 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC;
Azure.IoT Build 0:6ae2f7bca550 297 result = 0;
Azure.IoT Build 0:6ae2f7bca550 298 break;
Azure.IoT Build 0:6ae2f7bca550 299 }
Azure.IoT Build 0:6ae2f7bca550 300 else
Azure.IoT Build 0:6ae2f7bca550 301 {
Azure.IoT Build 0:6ae2f7bca550 302 frame_codec_data->receive_frame_pos = 0;
Azure.IoT Build 0:6ae2f7bca550 303
Azure.IoT Build 0:6ae2f7bca550 304 /* Codes_SRS_FRAME_CODEC_01_102: [frame_codec_receive_bytes shall allocate memory to hold the frame_body bytes.] */
AzureIoTClient 21:f9c433d8e6ca 305 frame_codec_data->receive_frame_bytes = (unsigned char*)malloc(frame_codec_data->receive_frame_size - 6);
Azure.IoT Build 0:6ae2f7bca550 306 if (frame_codec_data->receive_frame_bytes == NULL)
Azure.IoT Build 0:6ae2f7bca550 307 {
Azure.IoT Build 0:6ae2f7bca550 308 /* Codes_SRS_FRAME_CODEC_01_101: [If the memory for the frame_body bytes cannot be allocated, frame_codec_receive_bytes shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 309 /* Codes_SRS_FRAME_CODEC_01_030: [If a decoding error occurs, frame_codec_data_receive_bytes shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 310 /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
Azure.IoT Build 0:6ae2f7bca550 311 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 312
Azure.IoT Build 0:6ae2f7bca550 313 /* Codes_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the on_frame_codec_error_callback_context argument passed to frame_codec_create.] */
Azure.IoT Build 0:6ae2f7bca550 314 frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context);
Azure.IoT Build 0:6ae2f7bca550 315
AzureIoTClient 22:524bded3f7a8 316 LogError("Cannot allocate memort for frame bytes");
AzureIoTClient 22:524bded3f7a8 317 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 318 break;
Azure.IoT Build 0:6ae2f7bca550 319 }
Azure.IoT Build 0:6ae2f7bca550 320 else
Azure.IoT Build 0:6ae2f7bca550 321 {
Azure.IoT Build 0:6ae2f7bca550 322 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC;
Azure.IoT Build 0:6ae2f7bca550 323 result = 0;
Azure.IoT Build 0:6ae2f7bca550 324 break;
Azure.IoT Build 0:6ae2f7bca550 325 }
Azure.IoT Build 0:6ae2f7bca550 326 }
Azure.IoT Build 0:6ae2f7bca550 327 }
Azure.IoT Build 0:6ae2f7bca550 328 }
Azure.IoT Build 0:6ae2f7bca550 329
Azure.IoT Build 0:6ae2f7bca550 330 case RECEIVE_FRAME_STATE_TYPE_SPECIFIC:
Azure.IoT Build 0:6ae2f7bca550 331 {
AzureIoTClient 6:641a9672db08 332 size_t to_copy = frame_codec_data->type_specific_size - frame_codec_data->receive_frame_pos;
Azure.IoT Build 0:6ae2f7bca550 333 if (to_copy > size)
Azure.IoT Build 0:6ae2f7bca550 334 {
Azure.IoT Build 0:6ae2f7bca550 335 to_copy = size;
Azure.IoT Build 0:6ae2f7bca550 336 }
Azure.IoT Build 0:6ae2f7bca550 337
Azure.IoT Build 0:6ae2f7bca550 338 if (frame_codec_data->receive_frame_subscription != NULL)
Azure.IoT Build 0:6ae2f7bca550 339 {
Azure.IoT Build 0:6ae2f7bca550 340 (void)memcpy(&frame_codec_data->receive_frame_bytes[frame_codec_data->receive_frame_pos], buffer, to_copy);
Azure.IoT Build 0:6ae2f7bca550 341 frame_codec_data->receive_frame_pos += to_copy;
Azure.IoT Build 0:6ae2f7bca550 342 buffer += to_copy;
Azure.IoT Build 0:6ae2f7bca550 343 size -= to_copy;
Azure.IoT Build 0:6ae2f7bca550 344 }
Azure.IoT Build 0:6ae2f7bca550 345 else
Azure.IoT Build 0:6ae2f7bca550 346 {
Azure.IoT Build 0:6ae2f7bca550 347 frame_codec_data->receive_frame_pos += to_copy;
Azure.IoT Build 0:6ae2f7bca550 348 buffer += to_copy;
Azure.IoT Build 0:6ae2f7bca550 349 size -= to_copy;
Azure.IoT Build 0:6ae2f7bca550 350 }
Azure.IoT Build 0:6ae2f7bca550 351
Azure.IoT Build 0:6ae2f7bca550 352 if (frame_codec_data->receive_frame_pos == frame_codec_data->type_specific_size)
Azure.IoT Build 0:6ae2f7bca550 353 {
Azure.IoT Build 0:6ae2f7bca550 354 if (frame_codec_data->receive_frame_size == FRAME_HEADER_SIZE)
Azure.IoT Build 0:6ae2f7bca550 355 {
Azure.IoT Build 0:6ae2f7bca550 356 if (frame_codec_data->receive_frame_subscription != NULL)
Azure.IoT Build 0:6ae2f7bca550 357 {
Azure.IoT Build 0:6ae2f7bca550 358 /* Codes_SRS_FRAME_CODEC_01_031: [When a complete frame is successfully decoded it shall be indicated to the upper layer by invoking the on_frame_received passed to frame_codec_subscribe.] */
Azure.IoT Build 0:6ae2f7bca550 359 /* Codes_SRS_FRAME_CODEC_01_032: [Besides passing the frame information, the callback_context value passed to frame_codec_data_subscribe shall be passed to the on_frame_received function.] */
Azure.IoT Build 0:6ae2f7bca550 360 /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */
Azure.IoT Build 0:6ae2f7bca550 361 /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */
Azure.IoT Build 0:6ae2f7bca550 362 /* Codes_SRS_FRAME_CODEC_01_100: [If the frame body size is 0, the frame_body pointer passed to on_frame_received shall be NULL.] */
Azure.IoT Build 0:6ae2f7bca550 363 frame_codec_data->receive_frame_subscription->on_frame_received(frame_codec_data->receive_frame_subscription->callback_context, frame_codec_data->receive_frame_bytes, frame_codec_data->type_specific_size, NULL, 0);
AzureIoTClient 21:f9c433d8e6ca 364 free(frame_codec_data->receive_frame_bytes);
Azure.IoT Build 0:6ae2f7bca550 365 frame_codec_data->receive_frame_bytes = NULL;
Azure.IoT Build 0:6ae2f7bca550 366 }
Azure.IoT Build 0:6ae2f7bca550 367
Azure.IoT Build 0:6ae2f7bca550 368 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE;
Azure.IoT Build 0:6ae2f7bca550 369 frame_codec_data->receive_frame_size = 0;
Azure.IoT Build 0:6ae2f7bca550 370 }
Azure.IoT Build 0:6ae2f7bca550 371 else
Azure.IoT Build 0:6ae2f7bca550 372 {
Azure.IoT Build 0:6ae2f7bca550 373 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_BODY;
Azure.IoT Build 0:6ae2f7bca550 374 }
Azure.IoT Build 0:6ae2f7bca550 375
Azure.IoT Build 0:6ae2f7bca550 376 frame_codec_data->receive_frame_pos = 0;
Azure.IoT Build 0:6ae2f7bca550 377 }
Azure.IoT Build 0:6ae2f7bca550 378
Azure.IoT Build 0:6ae2f7bca550 379 result = 0;
Azure.IoT Build 0:6ae2f7bca550 380 break;
Azure.IoT Build 0:6ae2f7bca550 381 }
Azure.IoT Build 0:6ae2f7bca550 382
Azure.IoT Build 0:6ae2f7bca550 383 case RECEIVE_FRAME_STATE_FRAME_BODY:
Azure.IoT Build 0:6ae2f7bca550 384 {
Azure.IoT Build 0:6ae2f7bca550 385 uint32_t frame_body_size = frame_codec_data->receive_frame_size - (frame_codec_data->receive_frame_doff * 4);
AzureIoTClient 6:641a9672db08 386 size_t to_copy = frame_body_size - frame_codec_data->receive_frame_pos;
Azure.IoT Build 0:6ae2f7bca550 387
Azure.IoT Build 0:6ae2f7bca550 388 if (to_copy > size)
Azure.IoT Build 0:6ae2f7bca550 389 {
Azure.IoT Build 0:6ae2f7bca550 390 to_copy = size;
Azure.IoT Build 0:6ae2f7bca550 391 }
Azure.IoT Build 0:6ae2f7bca550 392
Azure.IoT Build 0:6ae2f7bca550 393 (void)memcpy(frame_codec_data->receive_frame_bytes + frame_codec_data->receive_frame_pos + frame_codec_data->type_specific_size, buffer, to_copy);
Azure.IoT Build 0:6ae2f7bca550 394
Azure.IoT Build 0:6ae2f7bca550 395 buffer += to_copy;
Azure.IoT Build 0:6ae2f7bca550 396 size -= to_copy;
Azure.IoT Build 0:6ae2f7bca550 397 frame_codec_data->receive_frame_pos += to_copy;
Azure.IoT Build 0:6ae2f7bca550 398
Azure.IoT Build 0:6ae2f7bca550 399 if (frame_codec_data->receive_frame_pos == frame_body_size)
Azure.IoT Build 0:6ae2f7bca550 400 {
Azure.IoT Build 0:6ae2f7bca550 401 if (frame_codec_data->receive_frame_subscription != NULL)
Azure.IoT Build 0:6ae2f7bca550 402 {
Azure.IoT Build 0:6ae2f7bca550 403 /* Codes_SRS_FRAME_CODEC_01_031: [When a complete frame is successfully decoded it shall be indicated to the upper layer by invoking the on_frame_received passed to frame_codec_subscribe.] */
Azure.IoT Build 0:6ae2f7bca550 404 /* Codes_SRS_FRAME_CODEC_01_032: [Besides passing the frame information, the callback_context value passed to frame_codec_data_subscribe shall be passed to the on_frame_received function.] */
Azure.IoT Build 0:6ae2f7bca550 405 /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */
Azure.IoT Build 0:6ae2f7bca550 406 /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */
Azure.IoT Build 0:6ae2f7bca550 407 /* Codes_SRS_FRAME_CODEC_01_099: [A pointer to the frame_body bytes shall also be passed to the on_frame_received.] */
Azure.IoT Build 0:6ae2f7bca550 408 frame_codec_data->receive_frame_subscription->on_frame_received(frame_codec_data->receive_frame_subscription->callback_context, frame_codec_data->receive_frame_bytes, frame_codec_data->type_specific_size, frame_codec_data->receive_frame_bytes + frame_codec_data->type_specific_size, frame_body_size);
AzureIoTClient 21:f9c433d8e6ca 409 free(frame_codec_data->receive_frame_bytes);
Azure.IoT Build 0:6ae2f7bca550 410 frame_codec_data->receive_frame_bytes = NULL;
Azure.IoT Build 0:6ae2f7bca550 411 }
Azure.IoT Build 0:6ae2f7bca550 412
Azure.IoT Build 0:6ae2f7bca550 413 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE;
Azure.IoT Build 0:6ae2f7bca550 414 frame_codec_data->receive_frame_pos = 0;
Azure.IoT Build 0:6ae2f7bca550 415 frame_codec_data->receive_frame_size = 0;
Azure.IoT Build 0:6ae2f7bca550 416 }
Azure.IoT Build 0:6ae2f7bca550 417 result = 0;
Azure.IoT Build 0:6ae2f7bca550 418
Azure.IoT Build 0:6ae2f7bca550 419 break;
Azure.IoT Build 0:6ae2f7bca550 420 }
Azure.IoT Build 0:6ae2f7bca550 421 }
Azure.IoT Build 0:6ae2f7bca550 422 }
Azure.IoT Build 0:6ae2f7bca550 423 }
Azure.IoT Build 0:6ae2f7bca550 424
Azure.IoT Build 0:6ae2f7bca550 425 return result;
Azure.IoT Build 0:6ae2f7bca550 426 }
Azure.IoT Build 0:6ae2f7bca550 427
Azure.IoT Build 0:6ae2f7bca550 428 /* Codes_SRS_FRAME_CODEC_01_033: [frame_codec_subscribe subscribes for a certain type of frame received by the frame_codec instance identified by frame_codec.] */
Azure.IoT Build 0:6ae2f7bca550 429 int frame_codec_subscribe(FRAME_CODEC_HANDLE frame_codec, uint8_t type, ON_FRAME_RECEIVED on_frame_received, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 430 {
Azure.IoT Build 0:6ae2f7bca550 431 int result;
Azure.IoT Build 0:6ae2f7bca550 432
Azure.IoT Build 0:6ae2f7bca550 433 /* Codes_SRS_FRAME_CODEC_01_034: [If any of the frame_codec or on_frame_received arguments is NULL, frame_codec_subscribe shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 434 if ((frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 435 (on_frame_received == NULL))
Azure.IoT Build 0:6ae2f7bca550 436 {
AzureIoTClient 22:524bded3f7a8 437 LogError("Bad arguments: frame_codec = %p, on_frame_received = %p",
AzureIoTClient 22:524bded3f7a8 438 frame_codec, on_frame_received);
AzureIoTClient 22:524bded3f7a8 439 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 440 }
Azure.IoT Build 0:6ae2f7bca550 441 else
Azure.IoT Build 0:6ae2f7bca550 442 {
Azure.IoT Build 0:6ae2f7bca550 443 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 444 SUBSCRIPTION* subscription;
Azure.IoT Build 0:6ae2f7bca550 445
Azure.IoT Build 0:6ae2f7bca550 446 /* Codes_SRS_FRAME_CODEC_01_036: [Only one callback pair shall be allowed to be registered for a given frame type.] */
Azure.IoT Build 0:6ae2f7bca550 447 /* find the subscription for this frame type */
AzureIoTClient 12:b30dacf113f2 448 LIST_ITEM_HANDLE list_item = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &type);
Azure.IoT Build 0:6ae2f7bca550 449 if (list_item != NULL)
Azure.IoT Build 0:6ae2f7bca550 450 {
AzureIoTClient 12:b30dacf113f2 451 subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item);
Azure.IoT Build 0:6ae2f7bca550 452 if (subscription == NULL)
Azure.IoT Build 0:6ae2f7bca550 453 {
Azure.IoT Build 0:6ae2f7bca550 454 /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */
AzureIoTClient 22:524bded3f7a8 455 LogError("Cannot retrieve subscription information from the list for type %u", (unsigned int)type);
AzureIoTClient 22:524bded3f7a8 456 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 457 }
Azure.IoT Build 0:6ae2f7bca550 458 else
Azure.IoT Build 0:6ae2f7bca550 459 {
Azure.IoT Build 0:6ae2f7bca550 460 /* a subscription was found */
Azure.IoT Build 0:6ae2f7bca550 461 subscription->on_frame_received = on_frame_received;
Azure.IoT Build 0:6ae2f7bca550 462 subscription->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 463
Azure.IoT Build 0:6ae2f7bca550 464 /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */
Azure.IoT Build 0:6ae2f7bca550 465 result = 0;
Azure.IoT Build 0:6ae2f7bca550 466 }
Azure.IoT Build 0:6ae2f7bca550 467 }
Azure.IoT Build 0:6ae2f7bca550 468 else
Azure.IoT Build 0:6ae2f7bca550 469 {
Azure.IoT Build 0:6ae2f7bca550 470 /* add a new subscription */
AzureIoTClient 21:f9c433d8e6ca 471 subscription = (SUBSCRIPTION*)malloc(sizeof(SUBSCRIPTION));
Azure.IoT Build 0:6ae2f7bca550 472 /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 473 if (subscription == NULL)
Azure.IoT Build 0:6ae2f7bca550 474 {
AzureIoTClient 22:524bded3f7a8 475 LogError("Cannot allocate memory for new subscription");
AzureIoTClient 22:524bded3f7a8 476 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 477 }
Azure.IoT Build 0:6ae2f7bca550 478 else
Azure.IoT Build 0:6ae2f7bca550 479 {
Azure.IoT Build 0:6ae2f7bca550 480 subscription->on_frame_received = on_frame_received;
Azure.IoT Build 0:6ae2f7bca550 481 subscription->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 482 subscription->frame_type = type;
Azure.IoT Build 0:6ae2f7bca550 483
Azure.IoT Build 0:6ae2f7bca550 484 /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */
AzureIoTClient 12:b30dacf113f2 485 if (singlylinkedlist_add(frame_codec_data->subscription_list, subscription) == NULL)
Azure.IoT Build 0:6ae2f7bca550 486 {
AzureIoTClient 21:f9c433d8e6ca 487 free(subscription);
AzureIoTClient 22:524bded3f7a8 488 LogError("Cannot add subscription to list");
AzureIoTClient 22:524bded3f7a8 489 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 490 }
Azure.IoT Build 0:6ae2f7bca550 491 else
Azure.IoT Build 0:6ae2f7bca550 492 {
Azure.IoT Build 0:6ae2f7bca550 493 /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */
Azure.IoT Build 0:6ae2f7bca550 494 result = 0;
Azure.IoT Build 0:6ae2f7bca550 495 }
Azure.IoT Build 0:6ae2f7bca550 496 }
Azure.IoT Build 0:6ae2f7bca550 497 }
Azure.IoT Build 0:6ae2f7bca550 498 }
Azure.IoT Build 0:6ae2f7bca550 499
Azure.IoT Build 0:6ae2f7bca550 500 return result;
Azure.IoT Build 0:6ae2f7bca550 501 }
Azure.IoT Build 0:6ae2f7bca550 502
Azure.IoT Build 0:6ae2f7bca550 503 int frame_codec_unsubscribe(FRAME_CODEC_HANDLE frame_codec, uint8_t type)
Azure.IoT Build 0:6ae2f7bca550 504 {
Azure.IoT Build 0:6ae2f7bca550 505 int result;
Azure.IoT Build 0:6ae2f7bca550 506
Azure.IoT Build 0:6ae2f7bca550 507 /* Codes_SRS_FRAME_CODEC_01_039: [If frame_codec is NULL, frame_codec_unsubscribe shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 508 if (frame_codec == NULL)
Azure.IoT Build 0:6ae2f7bca550 509 {
AzureIoTClient 22:524bded3f7a8 510 LogError("NULL frame_codec");
AzureIoTClient 22:524bded3f7a8 511 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 512 }
Azure.IoT Build 0:6ae2f7bca550 513 else
Azure.IoT Build 0:6ae2f7bca550 514 {
Azure.IoT Build 0:6ae2f7bca550 515 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
AzureIoTClient 12:b30dacf113f2 516 LIST_ITEM_HANDLE list_item = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &type);
Azure.IoT Build 0:6ae2f7bca550 517
Azure.IoT Build 0:6ae2f7bca550 518 if (list_item == NULL)
Azure.IoT Build 0:6ae2f7bca550 519 {
Azure.IoT Build 0:6ae2f7bca550 520 /* Codes_SRS_FRAME_CODEC_01_040: [If no subscription for the type frame type exists, frame_codec_unsubscribe shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 521 /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */
AzureIoTClient 22:524bded3f7a8 522 LogError("Cannot find subscription for type %u", (unsigned int)type);
AzureIoTClient 22:524bded3f7a8 523 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 524 }
Azure.IoT Build 0:6ae2f7bca550 525 else
Azure.IoT Build 0:6ae2f7bca550 526 {
AzureIoTClient 12:b30dacf113f2 527 SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item);
Azure.IoT Build 0:6ae2f7bca550 528 if (subscription == NULL)
Azure.IoT Build 0:6ae2f7bca550 529 {
Azure.IoT Build 0:6ae2f7bca550 530 /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */
AzureIoTClient 22:524bded3f7a8 531 LogError("singlylinkedlist_item_get_value failed when unsubscribing");
AzureIoTClient 22:524bded3f7a8 532 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 533 }
Azure.IoT Build 0:6ae2f7bca550 534 else
Azure.IoT Build 0:6ae2f7bca550 535 {
AzureIoTClient 21:f9c433d8e6ca 536 free(subscription);
AzureIoTClient 12:b30dacf113f2 537 if (singlylinkedlist_remove(frame_codec_data->subscription_list, list_item) != 0)
Azure.IoT Build 0:6ae2f7bca550 538 {
Azure.IoT Build 0:6ae2f7bca550 539 /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */
AzureIoTClient 22:524bded3f7a8 540 LogError("Cannot remove subscription from list");
AzureIoTClient 22:524bded3f7a8 541 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 542 }
Azure.IoT Build 0:6ae2f7bca550 543 else
Azure.IoT Build 0:6ae2f7bca550 544 {
Azure.IoT Build 0:6ae2f7bca550 545 /* Codes_SRS_FRAME_CODEC_01_038: [frame_codec_unsubscribe removes a previous subscription for frames of type type and on success it shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 546 result = 0;
Azure.IoT Build 0:6ae2f7bca550 547 }
Azure.IoT Build 0:6ae2f7bca550 548 }
Azure.IoT Build 0:6ae2f7bca550 549 }
Azure.IoT Build 0:6ae2f7bca550 550 }
Azure.IoT Build 0:6ae2f7bca550 551
Azure.IoT Build 0:6ae2f7bca550 552 return result;
Azure.IoT Build 0:6ae2f7bca550 553 }
Azure.IoT Build 0:6ae2f7bca550 554
Azure.IoT Build 0:6ae2f7bca550 555 int frame_codec_encode_frame(FRAME_CODEC_HANDLE frame_codec, uint8_t type, const PAYLOAD* payloads, size_t payload_count, const unsigned char* type_specific_bytes, uint32_t type_specific_size, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 556 {
Azure.IoT Build 0:6ae2f7bca550 557 int result;
Azure.IoT Build 0:6ae2f7bca550 558
Azure.IoT Build 0:6ae2f7bca550 559 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 560
AzureIoTClient 21:f9c433d8e6ca 561 /* Codes_SRS_FRAME_CODEC_01_044: [If any of arguments `frame_codec` or `on_bytes_encoded` is NULL, `frame_codec_encode_frame` shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 562 if ((frame_codec == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 563 (on_bytes_encoded == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 564 /* Codes_SRS_FRAME_CODEC_01_091: [If the argument type_specific_size is greater than 0 and type_specific_bytes is NULL, frame_codec_encode_frame shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 565 ((type_specific_size > 0) && (type_specific_bytes == NULL)) ||
Azure.IoT Build 0:6ae2f7bca550 566 /* Codes_SRS_FRAME_CODEC_01_092: [If type_specific_size is too big to allow encoding the frame according to the AMQP ISO then frame_codec_encode_frame shall return a non-zero value.] */
AzureIoTClient 21:f9c433d8e6ca 567 (type_specific_size > MAX_TYPE_SPECIFIC_SIZE))
Azure.IoT Build 0:6ae2f7bca550 568 {
AzureIoTClient 21:f9c433d8e6ca 569 LogError("Bad arguments: frame_codec = %p, on_bytes_encoded = %p, type_specific_size = %u, type_specific_bytes = %p",
AzureIoTClient 21:f9c433d8e6ca 570 frame_codec, on_bytes_encoded, (unsigned int)type_specific_size, type_specific_bytes);
AzureIoTClient 19:000ab4e6a2c1 571 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 572 }
AzureIoTClient 21:f9c433d8e6ca 573 else if ((payloads == NULL) && (payload_count > 0))
AzureIoTClient 21:f9c433d8e6ca 574 {
AzureIoTClient 21:f9c433d8e6ca 575 /* Codes_SRS_FRAME_CODEC_01_107: [If the argument `payloads` is NULL and `payload_count` is non-zero, `frame_codec_encode_frame` shall return a non-zero value.]*/
AzureIoTClient 21:f9c433d8e6ca 576 LogError("NULL payloads argument with non-zero payload count");
AzureIoTClient 21:f9c433d8e6ca 577 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 578 }
AzureIoTClient 21:f9c433d8e6ca 579 else
Azure.IoT Build 0:6ae2f7bca550 580 {
AzureIoTClient 6:641a9672db08 581 /* round up to the 4 bytes for doff */
AzureIoTClient 6:641a9672db08 582 /* Codes_SRS_FRAME_CODEC_01_067: [The value of the data offset is an unsigned, 8-bit integer specifying a count of 4-byte words.] */
AzureIoTClient 6:641a9672db08 583 /* Codes_SRS_FRAME_CODEC_01_068: [Due to the mandatory 8-byte frame header, the frame is malformed if the value is less than 2.] */
AzureIoTClient 6:641a9672db08 584 uint8_t padding_byte_count;
AzureIoTClient 6:641a9672db08 585 uint32_t frame_body_offset = type_specific_size + 6;
AzureIoTClient 6:641a9672db08 586 uint8_t doff = (uint8_t)((frame_body_offset + 3) / 4);
AzureIoTClient 6:641a9672db08 587 size_t i;
AzureIoTClient 6:641a9672db08 588 size_t frame_size;
AzureIoTClient 6:641a9672db08 589 size_t frame_body_size = 0;
AzureIoTClient 6:641a9672db08 590 frame_body_offset = doff * 4;
AzureIoTClient 6:641a9672db08 591 padding_byte_count = (uint8_t)(frame_body_offset - type_specific_size - 6);
Azure.IoT Build 0:6ae2f7bca550 592
AzureIoTClient 6:641a9672db08 593 for (i = 0; i < payload_count; i++)
AzureIoTClient 6:641a9672db08 594 {
AzureIoTClient 21:f9c433d8e6ca 595 /* Codes_SRS_FRAME_CODEC_01_110: [ If the `bytes` member of a payload entry is NULL, `frame_codec_encode_frame` shall fail and return a non-zero value. ] */
AzureIoTClient 21:f9c433d8e6ca 596 if ((payloads[i].bytes == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 597 /* Codes_SRS_FRAME_CODEC_01_111: [ If the `length` member of a payload entry is 0, `frame_codec_encode_frame` shall fail and return a non-zero value. ] */
AzureIoTClient 21:f9c433d8e6ca 598 (payloads[i].length == 0))
AzureIoTClient 21:f9c433d8e6ca 599 {
AzureIoTClient 21:f9c433d8e6ca 600 break;
AzureIoTClient 21:f9c433d8e6ca 601 }
AzureIoTClient 21:f9c433d8e6ca 602
AzureIoTClient 6:641a9672db08 603 frame_body_size += payloads[i].length;
AzureIoTClient 6:641a9672db08 604 }
AzureIoTClient 6:641a9672db08 605
AzureIoTClient 21:f9c433d8e6ca 606 if (i < payload_count)
AzureIoTClient 6:641a9672db08 607 {
AzureIoTClient 21:f9c433d8e6ca 608 LogError("Bad payload entry");
AzureIoTClient 19:000ab4e6a2c1 609 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 610 }
AzureIoTClient 6:641a9672db08 611 else
AzureIoTClient 6:641a9672db08 612 {
AzureIoTClient 6:641a9672db08 613 /* Codes_SRS_FRAME_CODEC_01_063: [This is an unsigned 32-bit integer that MUST contain the total frame size of the frame header, extended header, and frame body.] */
AzureIoTClient 21:f9c433d8e6ca 614 frame_size = frame_body_size + frame_body_offset;
Azure.IoT Build 0:6ae2f7bca550 615
AzureIoTClient 21:f9c433d8e6ca 616 if (frame_size > frame_codec_data->max_frame_size)
AzureIoTClient 21:f9c433d8e6ca 617 {
AzureIoTClient 21:f9c433d8e6ca 618 /* Codes_SRS_FRAME_CODEC_01_095: [If the frame_size needed for the frame is bigger than the maximum frame size, frame_codec_encode_frame shall fail and return a non-zero value.] */
AzureIoTClient 21:f9c433d8e6ca 619 LogError("Encoded frame size exceeds the maximum allowed frame size");
AzureIoTClient 21:f9c433d8e6ca 620 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 621 }
AzureIoTClient 21:f9c433d8e6ca 622 else
AzureIoTClient 21:f9c433d8e6ca 623 {
AzureIoTClient 21:f9c433d8e6ca 624 /* Codes_SRS_FRAME_CODEC_01_108: [ Memory shall be allocated to hold the entire frame. ]*/
AzureIoTClient 21:f9c433d8e6ca 625 unsigned char* encoded_frame = (unsigned char*)malloc(frame_size);
AzureIoTClient 21:f9c433d8e6ca 626 if (encoded_frame == NULL)
AzureIoTClient 21:f9c433d8e6ca 627 {
AzureIoTClient 21:f9c433d8e6ca 628 /* Codes_SRS_FRAME_CODEC_01_109: [ If allocating memory fails, `frame_codec_encode_frame` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 629 LogError("Cannot allocate memory for frame");
AzureIoTClient 21:f9c433d8e6ca 630 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 631 }
AzureIoTClient 21:f9c433d8e6ca 632 else
AzureIoTClient 21:f9c433d8e6ca 633 {
AzureIoTClient 21:f9c433d8e6ca 634 /* Codes_SRS_FRAME_CODEC_01_042: [frame_codec_encode_frame encodes the header, type specific bytes and frame payload of a frame that has frame_payload_size bytes.]*/
AzureIoTClient 21:f9c433d8e6ca 635 /* Codes_SRS_FRAME_CODEC_01_055: [Frames are divided into three distinct areas: a fixed width frame header, a variable width extended header, and a variable width frame body.] */
AzureIoTClient 21:f9c433d8e6ca 636 /* Codes_SRS_FRAME_CODEC_01_056: [frame header The frame header is a fixed size (8 byte) structure that precedes each frame.] */
AzureIoTClient 21:f9c433d8e6ca 637 /* Codes_SRS_FRAME_CODEC_01_057: [The frame header includes mandatory information necessary to parse the rest of the frame including size and type information.] */
AzureIoTClient 21:f9c433d8e6ca 638 /* Codes_SRS_FRAME_CODEC_01_058: [extended header The extended header is a variable width area preceding the frame body.] */
AzureIoTClient 21:f9c433d8e6ca 639 /* Codes_SRS_FRAME_CODEC_01_059: [This is an extension point defined for future expansion.] */
AzureIoTClient 21:f9c433d8e6ca 640 /* Codes_SRS_FRAME_CODEC_01_060: [The treatment of this area depends on the frame type.]*/
AzureIoTClient 21:f9c433d8e6ca 641 /* Codes_SRS_FRAME_CODEC_01_062: [SIZE Bytes 0-3 of the frame header contain the frame size.] */
AzureIoTClient 21:f9c433d8e6ca 642 /* Codes_SRS_FRAME_CODEC_01_063: [This is an unsigned 32-bit integer that MUST contain the total frame size of the frame header, extended header, and frame body.] */
AzureIoTClient 21:f9c433d8e6ca 643 /* Codes_SRS_FRAME_CODEC_01_064: [The frame is malformed if the size is less than the size of the frame header (8 bytes).] */
AzureIoTClient 21:f9c433d8e6ca 644 unsigned char frame_header[6];
AzureIoTClient 21:f9c433d8e6ca 645 size_t current_pos = 0;
AzureIoTClient 25:1101516ee67d 646 /* Codes_SRS_FRAME_CODEC_01_090: [If the type_specific_size - 2 does not divide by 4, frame_codec_encode_frame shall pad the type_specific bytes with zeroes so that type specific data is according to the AMQP ISO.] */
AzureIoTClient 25:1101516ee67d 647 unsigned char padding_bytes[] = { 0x00, 0x00, 0x00 };
AzureIoTClient 6:641a9672db08 648
AzureIoTClient 21:f9c433d8e6ca 649 frame_header[0] = (frame_size >> 24) & 0xFF;
AzureIoTClient 21:f9c433d8e6ca 650 frame_header[1] = (frame_size >> 16) & 0xFF;
AzureIoTClient 21:f9c433d8e6ca 651 frame_header[2] = (frame_size >> 8) & 0xFF;
AzureIoTClient 21:f9c433d8e6ca 652 frame_header[3] = frame_size & 0xFF;
AzureIoTClient 21:f9c433d8e6ca 653 /* Codes_SRS_FRAME_CODEC_01_065: [DOFF Byte 4 of the frame header is the data offset.] */
AzureIoTClient 21:f9c433d8e6ca 654 frame_header[4] = doff;
AzureIoTClient 21:f9c433d8e6ca 655 /* Codes_SRS_FRAME_CODEC_01_069: [TYPE Byte 5 of the frame header is a type code.] */
AzureIoTClient 21:f9c433d8e6ca 656 frame_header[5] = type;
AzureIoTClient 6:641a9672db08 657
AzureIoTClient 21:f9c433d8e6ca 658 (void)memcpy(encoded_frame, frame_header, sizeof(frame_header));
AzureIoTClient 21:f9c433d8e6ca 659 current_pos += sizeof(frame_header);
AzureIoTClient 21:f9c433d8e6ca 660
AzureIoTClient 21:f9c433d8e6ca 661 if (type_specific_size > 0)
AzureIoTClient 21:f9c433d8e6ca 662 {
AzureIoTClient 21:f9c433d8e6ca 663 (void)memcpy(encoded_frame + current_pos, type_specific_bytes, type_specific_size);
AzureIoTClient 21:f9c433d8e6ca 664 current_pos += type_specific_size;
AzureIoTClient 21:f9c433d8e6ca 665 }
AzureIoTClient 21:f9c433d8e6ca 666
AzureIoTClient 21:f9c433d8e6ca 667 /* send padding bytes */
AzureIoTClient 21:f9c433d8e6ca 668 if (padding_byte_count > 0)
AzureIoTClient 21:f9c433d8e6ca 669 {
AzureIoTClient 21:f9c433d8e6ca 670 (void)memcpy(encoded_frame + current_pos, padding_bytes, padding_byte_count);
AzureIoTClient 21:f9c433d8e6ca 671 current_pos += padding_byte_count;
AzureIoTClient 21:f9c433d8e6ca 672 }
Azure.IoT Build 0:6ae2f7bca550 673
AzureIoTClient 21:f9c433d8e6ca 674 /* Codes_SRS_FRAME_CODEC_01_106: [All payloads shall be encoded in order as part of the frame.] */
AzureIoTClient 21:f9c433d8e6ca 675 for (i = 0; i < payload_count; i++)
AzureIoTClient 21:f9c433d8e6ca 676 {
AzureIoTClient 21:f9c433d8e6ca 677 (void)memcpy(encoded_frame + current_pos, payloads[i].bytes, payloads[i].length);
AzureIoTClient 21:f9c433d8e6ca 678 current_pos += payloads[i].length;
AzureIoTClient 21:f9c433d8e6ca 679 }
AzureIoTClient 21:f9c433d8e6ca 680
AzureIoTClient 21:f9c433d8e6ca 681 /* Codes_SRS_FRAME_CODEC_01_088: [Encoded bytes shall be passed to the `on_bytes_encoded` callback in a single call, while setting the `encode complete` argument to true.] */
AzureIoTClient 21:f9c433d8e6ca 682 on_bytes_encoded(callback_context, encoded_frame, frame_size, true);
AzureIoTClient 21:f9c433d8e6ca 683
AzureIoTClient 21:f9c433d8e6ca 684 free(encoded_frame);
AzureIoTClient 21:f9c433d8e6ca 685
AzureIoTClient 21:f9c433d8e6ca 686 /* Codes_SRS_FRAME_CODEC_01_043: [On success it shall return 0.] */
AzureIoTClient 21:f9c433d8e6ca 687 result = 0;
AzureIoTClient 21:f9c433d8e6ca 688 }
AzureIoTClient 6:641a9672db08 689 }
AzureIoTClient 6:641a9672db08 690 }
Azure.IoT Build 0:6ae2f7bca550 691 }
Azure.IoT Build 0:6ae2f7bca550 692
Azure.IoT Build 0:6ae2f7bca550 693 return result;
Azure.IoT Build 0:6ae2f7bca550 694 }