A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Thu Oct 04 09:16:13 2018 -0700
Revision:
47:365a93fdb5bb
Parent:
28:add19eb7defa
1.2.10

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
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 21 RECEIVE_FRAME_STATE_FRAME_SIZE,
AzureIoTClient 28:add19eb7defa 22 RECEIVE_FRAME_STATE_DOFF,
AzureIoTClient 28:add19eb7defa 23 RECEIVE_FRAME_STATE_FRAME_TYPE,
AzureIoTClient 28:add19eb7defa 24 RECEIVE_FRAME_STATE_TYPE_SPECIFIC,
AzureIoTClient 28:add19eb7defa 25 RECEIVE_FRAME_STATE_FRAME_BODY,
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 31 uint8_t frame_type;
AzureIoTClient 28:add19eb7defa 32 ON_FRAME_RECEIVED on_frame_received;
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 38 /* subscriptions */
AzureIoTClient 28:add19eb7defa 39 SINGLYLINKEDLIST_HANDLE subscription_list;
Azure.IoT Build 0:6ae2f7bca550 40
AzureIoTClient 28:add19eb7defa 41 /* decode frame */
AzureIoTClient 28:add19eb7defa 42 RECEIVE_FRAME_STATE receive_frame_state;
AzureIoTClient 28:add19eb7defa 43 size_t receive_frame_pos;
AzureIoTClient 28:add19eb7defa 44 uint32_t receive_frame_size;
AzureIoTClient 28:add19eb7defa 45 uint32_t type_specific_size;
AzureIoTClient 28:add19eb7defa 46 uint8_t receive_frame_doff;
AzureIoTClient 28:add19eb7defa 47 uint8_t receive_frame_type;
AzureIoTClient 28:add19eb7defa 48 SUBSCRIPTION* receive_frame_subscription;
AzureIoTClient 28:add19eb7defa 49 unsigned char* receive_frame_bytes;
AzureIoTClient 28:add19eb7defa 50 ON_FRAME_CODEC_ERROR on_frame_codec_error;
AzureIoTClient 28:add19eb7defa 51 void* on_frame_codec_error_callback_context;
Azure.IoT Build 0:6ae2f7bca550 52
AzureIoTClient 28:add19eb7defa 53 /* configuration */
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 59 bool result;
AzureIoTClient 28:add19eb7defa 60 SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item);
Azure.IoT Build 0:6ae2f7bca550 61
AzureIoTClient 28:add19eb7defa 62 if (subscription == NULL)
AzureIoTClient 28:add19eb7defa 63 {
AzureIoTClient 22:524bded3f7a8 64 LogError("Could not get subscription information from the list item");
AzureIoTClient 28:add19eb7defa 65 result = false;
AzureIoTClient 28:add19eb7defa 66 }
AzureIoTClient 28:add19eb7defa 67 else
AzureIoTClient 28:add19eb7defa 68 {
AzureIoTClient 28:add19eb7defa 69 result = subscription->frame_type == *((uint8_t*)match_context) ? true : false;
AzureIoTClient 28:add19eb7defa 70 }
Azure.IoT Build 0:6ae2f7bca550 71
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 77 FRAME_CODEC_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 78
AzureIoTClient 28:add19eb7defa 79 /* Codes_SRS_FRAME_CODEC_01_020: [If the on_frame_codec_error argument is NULL, frame_codec_create shall return NULL.] */
AzureIoTClient 28:add19eb7defa 80 /* Codes_SRS_FRAME_CODEC_01_104: [The callback_context shall be allowed to be NULL.] */
AzureIoTClient 28:add19eb7defa 81 if (on_frame_codec_error == NULL)
AzureIoTClient 28:add19eb7defa 82 {
AzureIoTClient 22:524bded3f7a8 83 LogError("NULL on_frame_codec_error");
AzureIoTClient 22:524bded3f7a8 84 result = NULL;
AzureIoTClient 28:add19eb7defa 85 }
AzureIoTClient 28:add19eb7defa 86 else
AzureIoTClient 28:add19eb7defa 87 {
AzureIoTClient 28:add19eb7defa 88 result = (FRAME_CODEC_INSTANCE*)malloc(sizeof(FRAME_CODEC_INSTANCE));
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 97 result->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE;
AzureIoTClient 28:add19eb7defa 98 result->on_frame_codec_error = on_frame_codec_error;
AzureIoTClient 28:add19eb7defa 99 result->on_frame_codec_error_callback_context = callback_context;
AzureIoTClient 28:add19eb7defa 100 result->receive_frame_pos = 0;
AzureIoTClient 28:add19eb7defa 101 result->receive_frame_size = 0;
AzureIoTClient 28:add19eb7defa 102 result->receive_frame_bytes = NULL;
AzureIoTClient 28:add19eb7defa 103 result->subscription_list = singlylinkedlist_create();
Azure.IoT Build 0:6ae2f7bca550 104
AzureIoTClient 28:add19eb7defa 105 /* Codes_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */
AzureIoTClient 28:add19eb7defa 106 result->max_frame_size = 512;
AzureIoTClient 28:add19eb7defa 107 }
AzureIoTClient 28:add19eb7defa 108 }
Azure.IoT Build 0:6ae2f7bca550 109
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 122 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 123
AzureIoTClient 28:add19eb7defa 124 singlylinkedlist_destroy(frame_codec_data->subscription_list);
AzureIoTClient 28:add19eb7defa 125 if (frame_codec_data->receive_frame_bytes != NULL)
AzureIoTClient 28:add19eb7defa 126 {
AzureIoTClient 28:add19eb7defa 127 free(frame_codec_data->receive_frame_bytes);
AzureIoTClient 28:add19eb7defa 128 }
Azure.IoT Build 0:6ae2f7bca550 129
AzureIoTClient 28:add19eb7defa 130 /* Codes_SRS_FRAME_CODEC_01_023: [frame_codec_destroy shall free all resources associated with a frame_codec instance.] */
AzureIoTClient 28:add19eb7defa 131 free(frame_codec);
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 137 int result;
AzureIoTClient 28:add19eb7defa 138 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 139
AzureIoTClient 28:add19eb7defa 140 /* Codes_SRS_FRAME_CODEC_01_077: [If frame_codec is NULL, frame_codec_set_max_frame_size shall return a non-zero value.] */
AzureIoTClient 28:add19eb7defa 141 if ((frame_codec == NULL) ||
AzureIoTClient 28:add19eb7defa 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)
AzureIoTClient 28:add19eb7defa 154 {
AzureIoTClient 22:524bded3f7a8 155 LogError("Frame codec in error state");
AzureIoTClient 22:524bded3f7a8 156 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 157 }
AzureIoTClient 28:add19eb7defa 158 else
AzureIoTClient 28:add19eb7defa 159 {
AzureIoTClient 28:add19eb7defa 160 /* Codes_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 162 frame_codec_data->max_frame_size = max_frame_size;
Azure.IoT Build 0:6ae2f7bca550 163
AzureIoTClient 28:add19eb7defa 164 /* Codes_SRS_FRAME_CODEC_01_076: [On success, frame_codec_set_max_frame_size shall return 0.] */
AzureIoTClient 28:add19eb7defa 165 result = 0;
AzureIoTClient 28:add19eb7defa 166 }
AzureIoTClient 22:524bded3f7a8 167
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 181 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 182
AzureIoTClient 28:add19eb7defa 183 /* Codes_SRS_FRAME_CODEC_01_026: [If frame_codec or buffer are NULL, frame_codec_receive_bytes shall return a non-zero value.] */
AzureIoTClient 28:add19eb7defa 184 if ((frame_codec == NULL) ||
AzureIoTClient 28:add19eb7defa 185 (buffer == NULL) ||
AzureIoTClient 28:add19eb7defa 186 /* Codes_SRS_FRAME_CODEC_01_027: [If size is zero, frame_codec_receive_bytes shall return a non-zero value.] */
AzureIoTClient 28:add19eb7defa 187 (size == 0))
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 194 }
AzureIoTClient 28:add19eb7defa 195 else
AzureIoTClient 28:add19eb7defa 196 {
AzureIoTClient 28:add19eb7defa 197 while (size > 0)
AzureIoTClient 28:add19eb7defa 198 {
AzureIoTClient 28:add19eb7defa 199 switch (frame_codec_data->receive_frame_state)
AzureIoTClient 28:add19eb7defa 200 {
AzureIoTClient 28:add19eb7defa 201 default:
AzureIoTClient 28:add19eb7defa 202 case RECEIVE_FRAME_STATE_ERROR:
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 205 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 206 size = 0;
AzureIoTClient 28:add19eb7defa 207 break;
Azure.IoT Build 0:6ae2f7bca550 208
AzureIoTClient 28:add19eb7defa 209 /* Codes_SRS_FRAME_CODEC_01_008: [SIZE Bytes 0-3 of the frame header contain the frame size.] */
AzureIoTClient 28:add19eb7defa 210 case RECEIVE_FRAME_STATE_FRAME_SIZE:
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 212 frame_codec_data->receive_frame_size += buffer[0] << (24 - frame_codec_data->receive_frame_pos * 8);
AzureIoTClient 28:add19eb7defa 213 buffer++;
AzureIoTClient 28:add19eb7defa 214 size--;
AzureIoTClient 28:add19eb7defa 215 frame_codec_data->receive_frame_pos++;
Azure.IoT Build 0:6ae2f7bca550 216
AzureIoTClient 28:add19eb7defa 217 if (frame_codec_data->receive_frame_pos == 4)
AzureIoTClient 28:add19eb7defa 218 {
AzureIoTClient 28:add19eb7defa 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).] */
AzureIoTClient 28:add19eb7defa 220 if ((frame_codec_data->receive_frame_size < FRAME_HEADER_SIZE) ||
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 222 (frame_codec_data->receive_frame_size > frame_codec_data->max_frame_size))
AzureIoTClient 28:add19eb7defa 223 {
AzureIoTClient 28:add19eb7defa 224 /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
AzureIoTClient 28:add19eb7defa 225 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR;
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 229 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 230 }
AzureIoTClient 28:add19eb7defa 231 else
AzureIoTClient 28:add19eb7defa 232 {
AzureIoTClient 28:add19eb7defa 233 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_DOFF;
AzureIoTClient 28:add19eb7defa 234 result = 0;
AzureIoTClient 28:add19eb7defa 235 }
AzureIoTClient 28:add19eb7defa 236 }
AzureIoTClient 28:add19eb7defa 237 else
AzureIoTClient 28:add19eb7defa 238 {
AzureIoTClient 28:add19eb7defa 239 result = 0;
AzureIoTClient 28:add19eb7defa 240 }
Azure.IoT Build 0:6ae2f7bca550 241
AzureIoTClient 28:add19eb7defa 242 break;
Azure.IoT Build 0:6ae2f7bca550 243
AzureIoTClient 28:add19eb7defa 244 case RECEIVE_FRAME_STATE_DOFF:
AzureIoTClient 28:add19eb7defa 245 /* Codes_SRS_FRAME_CODEC_01_011: [DOFF Byte 4 of the frame header is the data offset.] */
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 247 /* Codes_SRS_FRAME_CODEC_01_012: [This gives the position of the body within the frame.] */
AzureIoTClient 28:add19eb7defa 248 frame_codec_data->receive_frame_doff = buffer[0];
AzureIoTClient 28:add19eb7defa 249 buffer++;
AzureIoTClient 28:add19eb7defa 250 size--;
Azure.IoT Build 0:6ae2f7bca550 251
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 253 if (frame_codec_data->receive_frame_doff < 2)
AzureIoTClient 28:add19eb7defa 254 {
AzureIoTClient 28:add19eb7defa 255 /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
AzureIoTClient 28:add19eb7defa 256 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 257
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 263 }
AzureIoTClient 28:add19eb7defa 264 else
AzureIoTClient 28:add19eb7defa 265 {
AzureIoTClient 28:add19eb7defa 266 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_TYPE;
AzureIoTClient 28:add19eb7defa 267 result = 0;
AzureIoTClient 28:add19eb7defa 268 }
Azure.IoT Build 0:6ae2f7bca550 269
AzureIoTClient 28:add19eb7defa 270 break;
Azure.IoT Build 0:6ae2f7bca550 271
AzureIoTClient 28:add19eb7defa 272 case RECEIVE_FRAME_STATE_FRAME_TYPE:
AzureIoTClient 28:add19eb7defa 273 {
AzureIoTClient 28:add19eb7defa 274 LIST_ITEM_HANDLE item_handle;
AzureIoTClient 28:add19eb7defa 275 frame_codec_data->type_specific_size = (frame_codec_data->receive_frame_doff * 4) - 6;
Azure.IoT Build 0:6ae2f7bca550 276
AzureIoTClient 28:add19eb7defa 277 /* Codes_SRS_FRAME_CODEC_01_015: [TYPE Byte 5 of the frame header is a type code.] */
AzureIoTClient 28:add19eb7defa 278 frame_codec_data->receive_frame_type = buffer[0];
AzureIoTClient 28:add19eb7defa 279 buffer++;
AzureIoTClient 28:add19eb7defa 280 size--;
Azure.IoT Build 0:6ae2f7bca550 281
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 283 item_handle = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &frame_codec_data->receive_frame_type);
AzureIoTClient 28:add19eb7defa 284 if (item_handle == NULL)
AzureIoTClient 28:add19eb7defa 285 {
AzureIoTClient 28:add19eb7defa 286 frame_codec_data->receive_frame_subscription = NULL;
AzureIoTClient 28:add19eb7defa 287 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC;
AzureIoTClient 28:add19eb7defa 288 result = 0;
AzureIoTClient 28:add19eb7defa 289 break;
AzureIoTClient 28:add19eb7defa 290 }
AzureIoTClient 28:add19eb7defa 291 else
AzureIoTClient 28:add19eb7defa 292 {
AzureIoTClient 28:add19eb7defa 293 frame_codec_data->receive_frame_subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(item_handle);
AzureIoTClient 28:add19eb7defa 294 if (frame_codec_data->receive_frame_subscription == NULL)
AzureIoTClient 28:add19eb7defa 295 {
AzureIoTClient 28:add19eb7defa 296 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC;
AzureIoTClient 28:add19eb7defa 297 result = 0;
AzureIoTClient 28:add19eb7defa 298 break;
AzureIoTClient 28:add19eb7defa 299 }
AzureIoTClient 28:add19eb7defa 300 else
AzureIoTClient 28:add19eb7defa 301 {
AzureIoTClient 28:add19eb7defa 302 frame_codec_data->receive_frame_pos = 0;
Azure.IoT Build 0:6ae2f7bca550 303
AzureIoTClient 28:add19eb7defa 304 /* Codes_SRS_FRAME_CODEC_01_102: [frame_codec_receive_bytes shall allocate memory to hold the frame_body bytes.] */
AzureIoTClient 28:add19eb7defa 305 frame_codec_data->receive_frame_bytes = (unsigned char*)malloc(frame_codec_data->receive_frame_size - 6);
AzureIoTClient 28:add19eb7defa 306 if (frame_codec_data->receive_frame_bytes == NULL)
AzureIoTClient 28:add19eb7defa 307 {
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 309 /* Codes_SRS_FRAME_CODEC_01_030: [If a decoding error occurs, frame_codec_data_receive_bytes shall return a non-zero value.] */
AzureIoTClient 28:add19eb7defa 310 /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
AzureIoTClient 28:add19eb7defa 311 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 312
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 318 break;
AzureIoTClient 28:add19eb7defa 319 }
AzureIoTClient 28:add19eb7defa 320 else
AzureIoTClient 28:add19eb7defa 321 {
AzureIoTClient 28:add19eb7defa 322 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC;
AzureIoTClient 28:add19eb7defa 323 result = 0;
AzureIoTClient 28:add19eb7defa 324 break;
AzureIoTClient 28:add19eb7defa 325 }
AzureIoTClient 28:add19eb7defa 326 }
AzureIoTClient 28:add19eb7defa 327 }
AzureIoTClient 28:add19eb7defa 328 }
Azure.IoT Build 0:6ae2f7bca550 329
AzureIoTClient 28:add19eb7defa 330 case RECEIVE_FRAME_STATE_TYPE_SPECIFIC:
AzureIoTClient 28:add19eb7defa 331 {
AzureIoTClient 28:add19eb7defa 332 size_t to_copy = frame_codec_data->type_specific_size - frame_codec_data->receive_frame_pos;
AzureIoTClient 28:add19eb7defa 333 if (to_copy > size)
AzureIoTClient 28:add19eb7defa 334 {
AzureIoTClient 28:add19eb7defa 335 to_copy = size;
AzureIoTClient 28:add19eb7defa 336 }
Azure.IoT Build 0:6ae2f7bca550 337
AzureIoTClient 28:add19eb7defa 338 if (frame_codec_data->receive_frame_subscription != NULL)
AzureIoTClient 28:add19eb7defa 339 {
AzureIoTClient 28:add19eb7defa 340 (void)memcpy(&frame_codec_data->receive_frame_bytes[frame_codec_data->receive_frame_pos], buffer, to_copy);
AzureIoTClient 28:add19eb7defa 341 frame_codec_data->receive_frame_pos += to_copy;
AzureIoTClient 28:add19eb7defa 342 buffer += to_copy;
AzureIoTClient 28:add19eb7defa 343 size -= to_copy;
AzureIoTClient 28:add19eb7defa 344 }
AzureIoTClient 28:add19eb7defa 345 else
AzureIoTClient 28:add19eb7defa 346 {
AzureIoTClient 28:add19eb7defa 347 frame_codec_data->receive_frame_pos += to_copy;
AzureIoTClient 28:add19eb7defa 348 buffer += to_copy;
AzureIoTClient 28:add19eb7defa 349 size -= to_copy;
AzureIoTClient 28:add19eb7defa 350 }
Azure.IoT Build 0:6ae2f7bca550 351
AzureIoTClient 28:add19eb7defa 352 if (frame_codec_data->receive_frame_pos == frame_codec_data->type_specific_size)
AzureIoTClient 28:add19eb7defa 353 {
AzureIoTClient 28:add19eb7defa 354 if (frame_codec_data->receive_frame_size == FRAME_HEADER_SIZE)
AzureIoTClient 28:add19eb7defa 355 {
AzureIoTClient 28:add19eb7defa 356 if (frame_codec_data->receive_frame_subscription != NULL)
AzureIoTClient 28:add19eb7defa 357 {
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 360 /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */
AzureIoTClient 28:add19eb7defa 361 /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 364 free(frame_codec_data->receive_frame_bytes);
AzureIoTClient 28:add19eb7defa 365 frame_codec_data->receive_frame_bytes = NULL;
AzureIoTClient 28:add19eb7defa 366 }
Azure.IoT Build 0:6ae2f7bca550 367
AzureIoTClient 28:add19eb7defa 368 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE;
AzureIoTClient 28:add19eb7defa 369 frame_codec_data->receive_frame_size = 0;
AzureIoTClient 28:add19eb7defa 370 }
AzureIoTClient 28:add19eb7defa 371 else
AzureIoTClient 28:add19eb7defa 372 {
AzureIoTClient 28:add19eb7defa 373 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_BODY;
AzureIoTClient 28:add19eb7defa 374 }
Azure.IoT Build 0:6ae2f7bca550 375
AzureIoTClient 28:add19eb7defa 376 frame_codec_data->receive_frame_pos = 0;
AzureIoTClient 28:add19eb7defa 377 }
Azure.IoT Build 0:6ae2f7bca550 378
AzureIoTClient 28:add19eb7defa 379 result = 0;
AzureIoTClient 28:add19eb7defa 380 break;
AzureIoTClient 28:add19eb7defa 381 }
Azure.IoT Build 0:6ae2f7bca550 382
AzureIoTClient 28:add19eb7defa 383 case RECEIVE_FRAME_STATE_FRAME_BODY:
AzureIoTClient 28:add19eb7defa 384 {
AzureIoTClient 28:add19eb7defa 385 uint32_t frame_body_size = frame_codec_data->receive_frame_size - (frame_codec_data->receive_frame_doff * 4);
AzureIoTClient 28:add19eb7defa 386 size_t to_copy = frame_body_size - frame_codec_data->receive_frame_pos;
Azure.IoT Build 0:6ae2f7bca550 387
AzureIoTClient 28:add19eb7defa 388 if (to_copy > size)
AzureIoTClient 28:add19eb7defa 389 {
AzureIoTClient 28:add19eb7defa 390 to_copy = size;
AzureIoTClient 28:add19eb7defa 391 }
Azure.IoT Build 0:6ae2f7bca550 392
AzureIoTClient 28:add19eb7defa 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
AzureIoTClient 28:add19eb7defa 395 buffer += to_copy;
AzureIoTClient 28:add19eb7defa 396 size -= to_copy;
AzureIoTClient 28:add19eb7defa 397 frame_codec_data->receive_frame_pos += to_copy;
Azure.IoT Build 0:6ae2f7bca550 398
AzureIoTClient 28:add19eb7defa 399 if (frame_codec_data->receive_frame_pos == frame_body_size)
AzureIoTClient 28:add19eb7defa 400 {
AzureIoTClient 28:add19eb7defa 401 if (frame_codec_data->receive_frame_subscription != NULL)
AzureIoTClient 28:add19eb7defa 402 {
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 405 /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */
AzureIoTClient 28:add19eb7defa 406 /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */
AzureIoTClient 28:add19eb7defa 407 /* Codes_SRS_FRAME_CODEC_01_099: [A pointer to the frame_body bytes shall also be passed to the on_frame_received.] */
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 409 free(frame_codec_data->receive_frame_bytes);
AzureIoTClient 28:add19eb7defa 410 frame_codec_data->receive_frame_bytes = NULL;
AzureIoTClient 28:add19eb7defa 411 }
Azure.IoT Build 0:6ae2f7bca550 412
AzureIoTClient 28:add19eb7defa 413 frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE;
AzureIoTClient 28:add19eb7defa 414 frame_codec_data->receive_frame_pos = 0;
AzureIoTClient 28:add19eb7defa 415 frame_codec_data->receive_frame_size = 0;
AzureIoTClient 28:add19eb7defa 416 }
AzureIoTClient 28:add19eb7defa 417 result = 0;
Azure.IoT Build 0:6ae2f7bca550 418
AzureIoTClient 28:add19eb7defa 419 break;
AzureIoTClient 28:add19eb7defa 420 }
AzureIoTClient 28:add19eb7defa 421 }
AzureIoTClient 28:add19eb7defa 422 }
AzureIoTClient 28:add19eb7defa 423 }
Azure.IoT Build 0:6ae2f7bca550 424
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 431 int result;
Azure.IoT Build 0:6ae2f7bca550 432
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 434 if ((frame_codec == NULL) ||
AzureIoTClient 28:add19eb7defa 435 (on_frame_received == NULL))
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 440 }
AzureIoTClient 28:add19eb7defa 441 else
AzureIoTClient 28:add19eb7defa 442 {
AzureIoTClient 28:add19eb7defa 443 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
AzureIoTClient 28:add19eb7defa 444 SUBSCRIPTION* subscription;
Azure.IoT Build 0:6ae2f7bca550 445
AzureIoTClient 28:add19eb7defa 446 /* Codes_SRS_FRAME_CODEC_01_036: [Only one callback pair shall be allowed to be registered for a given frame type.] */
AzureIoTClient 28:add19eb7defa 447 /* find the subscription for this frame type */
AzureIoTClient 28:add19eb7defa 448 LIST_ITEM_HANDLE list_item = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &type);
AzureIoTClient 28:add19eb7defa 449 if (list_item != NULL)
AzureIoTClient 28:add19eb7defa 450 {
AzureIoTClient 28:add19eb7defa 451 subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item);
AzureIoTClient 28:add19eb7defa 452 if (subscription == NULL)
AzureIoTClient 28:add19eb7defa 453 {
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 457 }
AzureIoTClient 28:add19eb7defa 458 else
AzureIoTClient 28:add19eb7defa 459 {
AzureIoTClient 28:add19eb7defa 460 /* a subscription was found */
AzureIoTClient 28:add19eb7defa 461 subscription->on_frame_received = on_frame_received;
AzureIoTClient 28:add19eb7defa 462 subscription->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 463
AzureIoTClient 28:add19eb7defa 464 /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */
AzureIoTClient 28:add19eb7defa 465 result = 0;
AzureIoTClient 28:add19eb7defa 466 }
AzureIoTClient 28:add19eb7defa 467 }
AzureIoTClient 28:add19eb7defa 468 else
AzureIoTClient 28:add19eb7defa 469 {
AzureIoTClient 28:add19eb7defa 470 /* add a new subscription */
AzureIoTClient 28:add19eb7defa 471 subscription = (SUBSCRIPTION*)malloc(sizeof(SUBSCRIPTION));
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 473 if (subscription == NULL)
AzureIoTClient 28:add19eb7defa 474 {
AzureIoTClient 22:524bded3f7a8 475 LogError("Cannot allocate memory for new subscription");
AzureIoTClient 22:524bded3f7a8 476 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 477 }
AzureIoTClient 28:add19eb7defa 478 else
AzureIoTClient 28:add19eb7defa 479 {
AzureIoTClient 28:add19eb7defa 480 subscription->on_frame_received = on_frame_received;
AzureIoTClient 28:add19eb7defa 481 subscription->callback_context = callback_context;
AzureIoTClient 28:add19eb7defa 482 subscription->frame_type = type;
Azure.IoT Build 0:6ae2f7bca550 483
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 485 if (singlylinkedlist_add(frame_codec_data->subscription_list, subscription) == NULL)
AzureIoTClient 28:add19eb7defa 486 {
AzureIoTClient 28:add19eb7defa 487 free(subscription);
AzureIoTClient 22:524bded3f7a8 488 LogError("Cannot add subscription to list");
AzureIoTClient 22:524bded3f7a8 489 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 490 }
AzureIoTClient 28:add19eb7defa 491 else
AzureIoTClient 28:add19eb7defa 492 {
AzureIoTClient 28:add19eb7defa 493 /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */
AzureIoTClient 28:add19eb7defa 494 result = 0;
AzureIoTClient 28:add19eb7defa 495 }
AzureIoTClient 28:add19eb7defa 496 }
AzureIoTClient 28:add19eb7defa 497 }
AzureIoTClient 28:add19eb7defa 498 }
Azure.IoT Build 0:6ae2f7bca550 499
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 505 int result;
Azure.IoT Build 0:6ae2f7bca550 506
AzureIoTClient 28:add19eb7defa 507 /* Codes_SRS_FRAME_CODEC_01_039: [If frame_codec is NULL, frame_codec_unsubscribe shall return a non-zero value.] */
AzureIoTClient 28:add19eb7defa 508 if (frame_codec == NULL)
AzureIoTClient 28:add19eb7defa 509 {
AzureIoTClient 22:524bded3f7a8 510 LogError("NULL frame_codec");
AzureIoTClient 22:524bded3f7a8 511 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 512 }
AzureIoTClient 28:add19eb7defa 513 else
AzureIoTClient 28:add19eb7defa 514 {
AzureIoTClient 28:add19eb7defa 515 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
AzureIoTClient 28:add19eb7defa 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
AzureIoTClient 28:add19eb7defa 518 if (list_item == NULL)
AzureIoTClient 28:add19eb7defa 519 {
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 524 }
AzureIoTClient 28:add19eb7defa 525 else
AzureIoTClient 28:add19eb7defa 526 {
AzureIoTClient 28:add19eb7defa 527 SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item);
AzureIoTClient 28:add19eb7defa 528 if (subscription == NULL)
AzureIoTClient 28:add19eb7defa 529 {
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 533 }
AzureIoTClient 28:add19eb7defa 534 else
AzureIoTClient 28:add19eb7defa 535 {
AzureIoTClient 28:add19eb7defa 536 free(subscription);
AzureIoTClient 28:add19eb7defa 537 if (singlylinkedlist_remove(frame_codec_data->subscription_list, list_item) != 0)
AzureIoTClient 28:add19eb7defa 538 {
AzureIoTClient 28:add19eb7defa 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__;
AzureIoTClient 28:add19eb7defa 542 }
AzureIoTClient 28:add19eb7defa 543 else
AzureIoTClient 28:add19eb7defa 544 {
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 546 result = 0;
AzureIoTClient 28:add19eb7defa 547 }
AzureIoTClient 28:add19eb7defa 548 }
AzureIoTClient 28:add19eb7defa 549 }
AzureIoTClient 28:add19eb7defa 550 }
Azure.IoT Build 0:6ae2f7bca550 551
AzureIoTClient 28:add19eb7defa 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 {
AzureIoTClient 28:add19eb7defa 557 int result;
Azure.IoT Build 0:6ae2f7bca550 558
AzureIoTClient 28:add19eb7defa 559 FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec;
Azure.IoT Build 0:6ae2f7bca550 560
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 562 if ((frame_codec == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 563 (on_bytes_encoded == NULL) ||
AzureIoTClient 28:add19eb7defa 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.] */
AzureIoTClient 28:add19eb7defa 565 ((type_specific_size > 0) && (type_specific_bytes == NULL)) ||
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 567 (type_specific_size > MAX_TYPE_SPECIFIC_SIZE))
AzureIoTClient 28:add19eb7defa 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 28:add19eb7defa 571 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 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
AzureIoTClient 28:add19eb7defa 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 }
AzureIoTClient 28:add19eb7defa 691 }
Azure.IoT Build 0:6ae2f7bca550 692
AzureIoTClient 28:add19eb7defa 693 return result;
Azure.IoT Build 0:6ae2f7bca550 694 }