A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
Diff: frame_codec.c
- Revision:
- 28:add19eb7defa
- Parent:
- 25:1101516ee67d
--- a/frame_codec.c Fri Jun 02 15:53:07 2017 -0700 +++ b/frame_codec.c Fri Jun 30 10:41:22 2017 -0700 @@ -14,132 +14,132 @@ #include "azure_uamqp_c/amqpvalue.h" #define FRAME_HEADER_SIZE 8 -#define MAX_TYPE_SPECIFIC_SIZE ((255 * 4) - 6) +#define MAX_TYPE_SPECIFIC_SIZE ((255 * 4) - 6) typedef enum RECEIVE_FRAME_STATE_TAG { - RECEIVE_FRAME_STATE_FRAME_SIZE, - RECEIVE_FRAME_STATE_DOFF, - RECEIVE_FRAME_STATE_FRAME_TYPE, - RECEIVE_FRAME_STATE_TYPE_SPECIFIC, - RECEIVE_FRAME_STATE_FRAME_BODY, - RECEIVE_FRAME_STATE_ERROR + RECEIVE_FRAME_STATE_FRAME_SIZE, + RECEIVE_FRAME_STATE_DOFF, + RECEIVE_FRAME_STATE_FRAME_TYPE, + RECEIVE_FRAME_STATE_TYPE_SPECIFIC, + RECEIVE_FRAME_STATE_FRAME_BODY, + RECEIVE_FRAME_STATE_ERROR } RECEIVE_FRAME_STATE; typedef struct SUBSCRIPTION_TAG { - uint8_t frame_type; - ON_FRAME_RECEIVED on_frame_received; - void* callback_context; + uint8_t frame_type; + ON_FRAME_RECEIVED on_frame_received; + void* callback_context; } SUBSCRIPTION; typedef struct FRAME_CODEC_INSTANCE_TAG { - /* subscriptions */ - SINGLYLINKEDLIST_HANDLE subscription_list; + /* subscriptions */ + SINGLYLINKEDLIST_HANDLE subscription_list; - /* decode frame */ - RECEIVE_FRAME_STATE receive_frame_state; - size_t receive_frame_pos; - uint32_t receive_frame_size; - uint32_t type_specific_size; - uint8_t receive_frame_doff; - uint8_t receive_frame_type; - SUBSCRIPTION* receive_frame_subscription; - unsigned char* receive_frame_bytes; - ON_FRAME_CODEC_ERROR on_frame_codec_error; - void* on_frame_codec_error_callback_context; + /* decode frame */ + RECEIVE_FRAME_STATE receive_frame_state; + size_t receive_frame_pos; + uint32_t receive_frame_size; + uint32_t type_specific_size; + uint8_t receive_frame_doff; + uint8_t receive_frame_type; + SUBSCRIPTION* receive_frame_subscription; + unsigned char* receive_frame_bytes; + ON_FRAME_CODEC_ERROR on_frame_codec_error; + void* on_frame_codec_error_callback_context; - /* configuration */ - uint32_t max_frame_size; + /* configuration */ + uint32_t max_frame_size; } FRAME_CODEC_INSTANCE; static bool find_subscription_by_frame_type(LIST_ITEM_HANDLE list_item, const void* match_context) { - bool result; - SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item); + bool result; + SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item); - if (subscription == NULL) - { + if (subscription == NULL) + { LogError("Could not get subscription information from the list item"); - result = false; - } - else - { - result = subscription->frame_type == *((uint8_t*)match_context) ? true : false; - } + result = false; + } + else + { + result = subscription->frame_type == *((uint8_t*)match_context) ? true : false; + } - return result; + return result; } FRAME_CODEC_HANDLE frame_codec_create(ON_FRAME_CODEC_ERROR on_frame_codec_error, void* callback_context) { - FRAME_CODEC_INSTANCE* result; + FRAME_CODEC_INSTANCE* result; - /* Codes_SRS_FRAME_CODEC_01_020: [If the on_frame_codec_error argument is NULL, frame_codec_create shall return NULL.] */ - /* Codes_SRS_FRAME_CODEC_01_104: [The callback_context shall be allowed to be NULL.] */ - if (on_frame_codec_error == NULL) - { + /* Codes_SRS_FRAME_CODEC_01_020: [If the on_frame_codec_error argument is NULL, frame_codec_create shall return NULL.] */ + /* Codes_SRS_FRAME_CODEC_01_104: [The callback_context shall be allowed to be NULL.] */ + if (on_frame_codec_error == NULL) + { LogError("NULL on_frame_codec_error"); result = NULL; - } - else - { - result = (FRAME_CODEC_INSTANCE*)malloc(sizeof(FRAME_CODEC_INSTANCE)); - /* Codes_SRS_FRAME_CODEC_01_022: [If allocating memory for the frame_codec instance fails, frame_codec_create shall return NULL.] */ + } + else + { + result = (FRAME_CODEC_INSTANCE*)malloc(sizeof(FRAME_CODEC_INSTANCE)); + /* Codes_SRS_FRAME_CODEC_01_022: [If allocating memory for the frame_codec instance fails, frame_codec_create shall return NULL.] */ if (result == NULL) { LogError("Could not allocate frame codec"); } else { - /* 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.] */ - result->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE; - result->on_frame_codec_error = on_frame_codec_error; - result->on_frame_codec_error_callback_context = callback_context; - result->receive_frame_pos = 0; - result->receive_frame_size = 0; - result->receive_frame_bytes = NULL; - result->subscription_list = singlylinkedlist_create(); + /* 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.] */ + result->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE; + result->on_frame_codec_error = on_frame_codec_error; + result->on_frame_codec_error_callback_context = callback_context; + result->receive_frame_pos = 0; + result->receive_frame_size = 0; + result->receive_frame_bytes = NULL; + result->subscription_list = singlylinkedlist_create(); - /* Codes_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */ - result->max_frame_size = 512; - } - } + /* Codes_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */ + result->max_frame_size = 512; + } + } - return result; + return result; } void frame_codec_destroy(FRAME_CODEC_HANDLE frame_codec) { - /* Codes_SRS_FRAME_CODEC_01_024: [If frame_codec is NULL, frame_codec_destroy shall do nothing.] */ + /* Codes_SRS_FRAME_CODEC_01_024: [If frame_codec is NULL, frame_codec_destroy shall do nothing.] */ if (frame_codec == NULL) { LogError("NULL frame_codec"); } else { - FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; + FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; - singlylinkedlist_destroy(frame_codec_data->subscription_list); - if (frame_codec_data->receive_frame_bytes != NULL) - { - free(frame_codec_data->receive_frame_bytes); - } + singlylinkedlist_destroy(frame_codec_data->subscription_list); + if (frame_codec_data->receive_frame_bytes != NULL) + { + free(frame_codec_data->receive_frame_bytes); + } - /* Codes_SRS_FRAME_CODEC_01_023: [frame_codec_destroy shall free all resources associated with a frame_codec instance.] */ - free(frame_codec); - } + /* Codes_SRS_FRAME_CODEC_01_023: [frame_codec_destroy shall free all resources associated with a frame_codec instance.] */ + free(frame_codec); + } } int frame_codec_set_max_frame_size(FRAME_CODEC_HANDLE frame_codec, uint32_t max_frame_size) { - int result; - FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; + int result; + FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; - /* Codes_SRS_FRAME_CODEC_01_077: [If frame_codec is NULL, frame_codec_set_max_frame_size shall return a non-zero value.] */ - if ((frame_codec == NULL) || - /* 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.] */ + /* Codes_SRS_FRAME_CODEC_01_077: [If frame_codec is NULL, frame_codec_set_max_frame_size shall return a non-zero value.] */ + if ((frame_codec == NULL) || + /* 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.] */ (max_frame_size < FRAME_HEADER_SIZE) || /* 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.] */ ((max_frame_size < frame_codec_data->receive_frame_size) && (frame_codec_data->receive_frame_state != RECEIVE_FRAME_STATE_FRAME_SIZE))) @@ -151,21 +151,21 @@ } /* Codes_SRS_FRAME_CODEC_01_097: [Setting a frame size on a frame_codec that had a decode error shall fail.] */ else if (frame_codec_data->receive_frame_state == RECEIVE_FRAME_STATE_ERROR) - { + { LogError("Frame codec in error state"); result = __FAILURE__; - } - else - { - /* Codes_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */ - /* 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.] */ - frame_codec_data->max_frame_size = max_frame_size; + } + else + { + /* Codes_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */ + /* 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.] */ + frame_codec_data->max_frame_size = max_frame_size; - /* Codes_SRS_FRAME_CODEC_01_076: [On success, frame_codec_set_max_frame_size shall return 0.] */ - result = 0; - } + /* Codes_SRS_FRAME_CODEC_01_076: [On success, frame_codec_set_max_frame_size shall return 0.] */ + result = 0; + } - return result; + return result; } /* 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.] */ @@ -178,398 +178,398 @@ int frame_codec_receive_bytes(FRAME_CODEC_HANDLE frame_codec, const unsigned char* buffer, size_t size) { int result = __FAILURE__; - FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; + FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; - /* Codes_SRS_FRAME_CODEC_01_026: [If frame_codec or buffer are NULL, frame_codec_receive_bytes shall return a non-zero value.] */ - if ((frame_codec == NULL) || - (buffer == NULL) || - /* Codes_SRS_FRAME_CODEC_01_027: [If size is zero, frame_codec_receive_bytes shall return a non-zero value.] */ - (size == 0)) - { + /* Codes_SRS_FRAME_CODEC_01_026: [If frame_codec or buffer are NULL, frame_codec_receive_bytes shall return a non-zero value.] */ + if ((frame_codec == NULL) || + (buffer == NULL) || + /* Codes_SRS_FRAME_CODEC_01_027: [If size is zero, frame_codec_receive_bytes shall return a non-zero value.] */ + (size == 0)) + { LogError("Bad arguments: frame_codec = %p, buffer = %p, size = %u", frame_codec, buffer, (unsigned int)size); result = __FAILURE__; - } - else - { - while (size > 0) - { - switch (frame_codec_data->receive_frame_state) - { - default: - case RECEIVE_FRAME_STATE_ERROR: - /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ + } + else + { + while (size > 0) + { + switch (frame_codec_data->receive_frame_state) + { + default: + case RECEIVE_FRAME_STATE_ERROR: + /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ LogError("Frame codec is in error state"); - result = __FAILURE__; - size = 0; - break; + result = __FAILURE__; + size = 0; + break; - /* Codes_SRS_FRAME_CODEC_01_008: [SIZE Bytes 0-3 of the frame header contain the frame size.] */ - case RECEIVE_FRAME_STATE_FRAME_SIZE: - /* 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.] */ - frame_codec_data->receive_frame_size += buffer[0] << (24 - frame_codec_data->receive_frame_pos * 8); - buffer++; - size--; - frame_codec_data->receive_frame_pos++; + /* Codes_SRS_FRAME_CODEC_01_008: [SIZE Bytes 0-3 of the frame header contain the frame size.] */ + case RECEIVE_FRAME_STATE_FRAME_SIZE: + /* 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.] */ + frame_codec_data->receive_frame_size += buffer[0] << (24 - frame_codec_data->receive_frame_pos * 8); + buffer++; + size--; + frame_codec_data->receive_frame_pos++; - if (frame_codec_data->receive_frame_pos == 4) - { - /* Codes_SRS_FRAME_CODEC_01_010: [The frame is malformed if the size is less than the size of the frame header (8 bytes).] */ - if ((frame_codec_data->receive_frame_size < FRAME_HEADER_SIZE) || - /* 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.] */ - (frame_codec_data->receive_frame_size > frame_codec_data->max_frame_size)) - { - /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR; - /* 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.] */ - frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context); + if (frame_codec_data->receive_frame_pos == 4) + { + /* Codes_SRS_FRAME_CODEC_01_010: [The frame is malformed if the size is less than the size of the frame header (8 bytes).] */ + if ((frame_codec_data->receive_frame_size < FRAME_HEADER_SIZE) || + /* 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.] */ + (frame_codec_data->receive_frame_size > frame_codec_data->max_frame_size)) + { + /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR; + /* 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.] */ + frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context); LogError("Received frame size is too big"); - result = __FAILURE__; - } - else - { - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_DOFF; - result = 0; - } - } - else - { - result = 0; - } + result = __FAILURE__; + } + else + { + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_DOFF; + result = 0; + } + } + else + { + result = 0; + } - break; + break; - case RECEIVE_FRAME_STATE_DOFF: - /* Codes_SRS_FRAME_CODEC_01_011: [DOFF Byte 4 of the frame header is the data offset.] */ - /* 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.] */ - /* Codes_SRS_FRAME_CODEC_01_012: [This gives the position of the body within the frame.] */ - frame_codec_data->receive_frame_doff = buffer[0]; - buffer++; - size--; + case RECEIVE_FRAME_STATE_DOFF: + /* Codes_SRS_FRAME_CODEC_01_011: [DOFF Byte 4 of the frame header is the data offset.] */ + /* 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.] */ + /* Codes_SRS_FRAME_CODEC_01_012: [This gives the position of the body within the frame.] */ + frame_codec_data->receive_frame_doff = buffer[0]; + buffer++; + size--; - /* 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.] */ - if (frame_codec_data->receive_frame_doff < 2) - { - /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR; + /* 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.] */ + if (frame_codec_data->receive_frame_doff < 2) + { + /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR; - /* 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.] */ - frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context); + /* 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.] */ + frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context); LogError("Malformed frame received"); result = __FAILURE__; - } - else - { - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_TYPE; - result = 0; - } + } + else + { + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_TYPE; + result = 0; + } - break; + break; - case RECEIVE_FRAME_STATE_FRAME_TYPE: - { - LIST_ITEM_HANDLE item_handle; - frame_codec_data->type_specific_size = (frame_codec_data->receive_frame_doff * 4) - 6; + case RECEIVE_FRAME_STATE_FRAME_TYPE: + { + LIST_ITEM_HANDLE item_handle; + frame_codec_data->type_specific_size = (frame_codec_data->receive_frame_doff * 4) - 6; - /* Codes_SRS_FRAME_CODEC_01_015: [TYPE Byte 5 of the frame header is a type code.] */ - frame_codec_data->receive_frame_type = buffer[0]; - buffer++; - size--; + /* Codes_SRS_FRAME_CODEC_01_015: [TYPE Byte 5 of the frame header is a type code.] */ + frame_codec_data->receive_frame_type = buffer[0]; + buffer++; + size--; - /* 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.] */ - item_handle = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &frame_codec_data->receive_frame_type); - if (item_handle == NULL) - { - frame_codec_data->receive_frame_subscription = NULL; - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC; - result = 0; - break; - } - else - { - frame_codec_data->receive_frame_subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(item_handle); - if (frame_codec_data->receive_frame_subscription == NULL) - { - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC; - result = 0; - break; - } - else - { - frame_codec_data->receive_frame_pos = 0; + /* 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.] */ + item_handle = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &frame_codec_data->receive_frame_type); + if (item_handle == NULL) + { + frame_codec_data->receive_frame_subscription = NULL; + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC; + result = 0; + break; + } + else + { + frame_codec_data->receive_frame_subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(item_handle); + if (frame_codec_data->receive_frame_subscription == NULL) + { + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC; + result = 0; + break; + } + else + { + frame_codec_data->receive_frame_pos = 0; - /* Codes_SRS_FRAME_CODEC_01_102: [frame_codec_receive_bytes shall allocate memory to hold the frame_body bytes.] */ - frame_codec_data->receive_frame_bytes = (unsigned char*)malloc(frame_codec_data->receive_frame_size - 6); - if (frame_codec_data->receive_frame_bytes == NULL) - { - /* 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.] */ - /* Codes_SRS_FRAME_CODEC_01_030: [If a decoding error occurs, frame_codec_data_receive_bytes shall return a non-zero value.] */ - /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR; + /* Codes_SRS_FRAME_CODEC_01_102: [frame_codec_receive_bytes shall allocate memory to hold the frame_body bytes.] */ + frame_codec_data->receive_frame_bytes = (unsigned char*)malloc(frame_codec_data->receive_frame_size - 6); + if (frame_codec_data->receive_frame_bytes == NULL) + { + /* 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.] */ + /* Codes_SRS_FRAME_CODEC_01_030: [If a decoding error occurs, frame_codec_data_receive_bytes shall return a non-zero value.] */ + /* Codes_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */ + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_ERROR; - /* 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.] */ - frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context); + /* 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.] */ + frame_codec_data->on_frame_codec_error(frame_codec_data->on_frame_codec_error_callback_context); LogError("Cannot allocate memort for frame bytes"); result = __FAILURE__; - break; - } - else - { - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC; - result = 0; - break; - } - } - } - } + break; + } + else + { + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_TYPE_SPECIFIC; + result = 0; + break; + } + } + } + } - case RECEIVE_FRAME_STATE_TYPE_SPECIFIC: - { - size_t to_copy = frame_codec_data->type_specific_size - frame_codec_data->receive_frame_pos; - if (to_copy > size) - { - to_copy = size; - } + case RECEIVE_FRAME_STATE_TYPE_SPECIFIC: + { + size_t to_copy = frame_codec_data->type_specific_size - frame_codec_data->receive_frame_pos; + if (to_copy > size) + { + to_copy = size; + } - if (frame_codec_data->receive_frame_subscription != NULL) - { - (void)memcpy(&frame_codec_data->receive_frame_bytes[frame_codec_data->receive_frame_pos], buffer, to_copy); - frame_codec_data->receive_frame_pos += to_copy; - buffer += to_copy; - size -= to_copy; - } - else - { - frame_codec_data->receive_frame_pos += to_copy; - buffer += to_copy; - size -= to_copy; - } + if (frame_codec_data->receive_frame_subscription != NULL) + { + (void)memcpy(&frame_codec_data->receive_frame_bytes[frame_codec_data->receive_frame_pos], buffer, to_copy); + frame_codec_data->receive_frame_pos += to_copy; + buffer += to_copy; + size -= to_copy; + } + else + { + frame_codec_data->receive_frame_pos += to_copy; + buffer += to_copy; + size -= to_copy; + } - if (frame_codec_data->receive_frame_pos == frame_codec_data->type_specific_size) - { - if (frame_codec_data->receive_frame_size == FRAME_HEADER_SIZE) - { - if (frame_codec_data->receive_frame_subscription != NULL) - { - /* 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.] */ - /* 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.] */ - /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */ - /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */ - /* 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.] */ - 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); - free(frame_codec_data->receive_frame_bytes); - frame_codec_data->receive_frame_bytes = NULL; - } + if (frame_codec_data->receive_frame_pos == frame_codec_data->type_specific_size) + { + if (frame_codec_data->receive_frame_size == FRAME_HEADER_SIZE) + { + if (frame_codec_data->receive_frame_subscription != NULL) + { + /* 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.] */ + /* 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.] */ + /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */ + /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */ + /* 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.] */ + 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); + free(frame_codec_data->receive_frame_bytes); + frame_codec_data->receive_frame_bytes = NULL; + } - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE; - frame_codec_data->receive_frame_size = 0; - } - else - { - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_BODY; - } + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE; + frame_codec_data->receive_frame_size = 0; + } + else + { + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_BODY; + } - frame_codec_data->receive_frame_pos = 0; - } + frame_codec_data->receive_frame_pos = 0; + } - result = 0; - break; - } + result = 0; + break; + } - case RECEIVE_FRAME_STATE_FRAME_BODY: - { - uint32_t frame_body_size = frame_codec_data->receive_frame_size - (frame_codec_data->receive_frame_doff * 4); - size_t to_copy = frame_body_size - frame_codec_data->receive_frame_pos; + case RECEIVE_FRAME_STATE_FRAME_BODY: + { + uint32_t frame_body_size = frame_codec_data->receive_frame_size - (frame_codec_data->receive_frame_doff * 4); + size_t to_copy = frame_body_size - frame_codec_data->receive_frame_pos; - if (to_copy > size) - { - to_copy = size; - } + if (to_copy > size) + { + to_copy = size; + } - (void)memcpy(frame_codec_data->receive_frame_bytes + frame_codec_data->receive_frame_pos + frame_codec_data->type_specific_size, buffer, to_copy); + (void)memcpy(frame_codec_data->receive_frame_bytes + frame_codec_data->receive_frame_pos + frame_codec_data->type_specific_size, buffer, to_copy); - buffer += to_copy; - size -= to_copy; - frame_codec_data->receive_frame_pos += to_copy; + buffer += to_copy; + size -= to_copy; + frame_codec_data->receive_frame_pos += to_copy; - if (frame_codec_data->receive_frame_pos == frame_body_size) - { - if (frame_codec_data->receive_frame_subscription != NULL) - { - /* 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.] */ - /* 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.] */ - /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */ - /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */ - /* Codes_SRS_FRAME_CODEC_01_099: [A pointer to the frame_body bytes shall also be passed to the on_frame_received.] */ - 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); - free(frame_codec_data->receive_frame_bytes); - frame_codec_data->receive_frame_bytes = NULL; - } + if (frame_codec_data->receive_frame_pos == frame_body_size) + { + if (frame_codec_data->receive_frame_subscription != NULL) + { + /* 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.] */ + /* 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.] */ + /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */ + /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */ + /* Codes_SRS_FRAME_CODEC_01_099: [A pointer to the frame_body bytes shall also be passed to the on_frame_received.] */ + 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); + free(frame_codec_data->receive_frame_bytes); + frame_codec_data->receive_frame_bytes = NULL; + } - frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE; - frame_codec_data->receive_frame_pos = 0; - frame_codec_data->receive_frame_size = 0; - } - result = 0; + frame_codec_data->receive_frame_state = RECEIVE_FRAME_STATE_FRAME_SIZE; + frame_codec_data->receive_frame_pos = 0; + frame_codec_data->receive_frame_size = 0; + } + result = 0; - break; - } - } - } - } + break; + } + } + } + } - return result; + return result; } /* 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.] */ int frame_codec_subscribe(FRAME_CODEC_HANDLE frame_codec, uint8_t type, ON_FRAME_RECEIVED on_frame_received, void* callback_context) { - int result; + int result; - /* 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.] */ - if ((frame_codec == NULL) || - (on_frame_received == NULL)) - { + /* 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.] */ + if ((frame_codec == NULL) || + (on_frame_received == NULL)) + { LogError("Bad arguments: frame_codec = %p, on_frame_received = %p", frame_codec, on_frame_received); result = __FAILURE__; - } - else - { - FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; - SUBSCRIPTION* subscription; + } + else + { + FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; + SUBSCRIPTION* subscription; - /* Codes_SRS_FRAME_CODEC_01_036: [Only one callback pair shall be allowed to be registered for a given frame type.] */ - /* find the subscription for this frame type */ - LIST_ITEM_HANDLE list_item = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &type); - if (list_item != NULL) - { - subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item); - if (subscription == NULL) - { - /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */ + /* Codes_SRS_FRAME_CODEC_01_036: [Only one callback pair shall be allowed to be registered for a given frame type.] */ + /* find the subscription for this frame type */ + LIST_ITEM_HANDLE list_item = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &type); + if (list_item != NULL) + { + subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item); + if (subscription == NULL) + { + /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */ LogError("Cannot retrieve subscription information from the list for type %u", (unsigned int)type); result = __FAILURE__; - } - else - { - /* a subscription was found */ - subscription->on_frame_received = on_frame_received; - subscription->callback_context = callback_context; + } + else + { + /* a subscription was found */ + subscription->on_frame_received = on_frame_received; + subscription->callback_context = callback_context; - /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */ - result = 0; - } - } - else - { - /* add a new subscription */ - subscription = (SUBSCRIPTION*)malloc(sizeof(SUBSCRIPTION)); - /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */ - if (subscription == NULL) - { + /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */ + result = 0; + } + } + else + { + /* add a new subscription */ + subscription = (SUBSCRIPTION*)malloc(sizeof(SUBSCRIPTION)); + /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */ + if (subscription == NULL) + { LogError("Cannot allocate memory for new subscription"); result = __FAILURE__; - } - else - { - subscription->on_frame_received = on_frame_received; - subscription->callback_context = callback_context; - subscription->frame_type = type; + } + else + { + subscription->on_frame_received = on_frame_received; + subscription->callback_context = callback_context; + subscription->frame_type = type; - /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */ - if (singlylinkedlist_add(frame_codec_data->subscription_list, subscription) == NULL) - { - free(subscription); + /* Codes_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */ + if (singlylinkedlist_add(frame_codec_data->subscription_list, subscription) == NULL) + { + free(subscription); LogError("Cannot add subscription to list"); result = __FAILURE__; - } - else - { - /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */ - result = 0; - } - } - } - } + } + else + { + /* Codes_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */ + result = 0; + } + } + } + } - return result; + return result; } int frame_codec_unsubscribe(FRAME_CODEC_HANDLE frame_codec, uint8_t type) { - int result; + int result; - /* Codes_SRS_FRAME_CODEC_01_039: [If frame_codec is NULL, frame_codec_unsubscribe shall return a non-zero value.] */ - if (frame_codec == NULL) - { + /* Codes_SRS_FRAME_CODEC_01_039: [If frame_codec is NULL, frame_codec_unsubscribe shall return a non-zero value.] */ + if (frame_codec == NULL) + { LogError("NULL frame_codec"); result = __FAILURE__; - } - else - { - FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; - LIST_ITEM_HANDLE list_item = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &type); + } + else + { + FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; + LIST_ITEM_HANDLE list_item = singlylinkedlist_find(frame_codec_data->subscription_list, find_subscription_by_frame_type, &type); - if (list_item == NULL) - { - /* Codes_SRS_FRAME_CODEC_01_040: [If no subscription for the type frame type exists, frame_codec_unsubscribe shall return a non-zero value.] */ - /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */ + if (list_item == NULL) + { + /* Codes_SRS_FRAME_CODEC_01_040: [If no subscription for the type frame type exists, frame_codec_unsubscribe shall return a non-zero value.] */ + /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */ LogError("Cannot find subscription for type %u", (unsigned int)type); result = __FAILURE__; - } - else - { - SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item); - if (subscription == NULL) - { - /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */ + } + else + { + SUBSCRIPTION* subscription = (SUBSCRIPTION*)singlylinkedlist_item_get_value(list_item); + if (subscription == NULL) + { + /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */ LogError("singlylinkedlist_item_get_value failed when unsubscribing"); result = __FAILURE__; - } - else - { - free(subscription); - if (singlylinkedlist_remove(frame_codec_data->subscription_list, list_item) != 0) - { - /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */ + } + else + { + free(subscription); + if (singlylinkedlist_remove(frame_codec_data->subscription_list, list_item) != 0) + { + /* Codes_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */ LogError("Cannot remove subscription from list"); result = __FAILURE__; - } - else - { - /* 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.] */ - result = 0; - } - } - } - } + } + else + { + /* 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.] */ + result = 0; + } + } + } + } - return result; + return result; } 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) { - int result; + int result; - FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; + FRAME_CODEC_INSTANCE* frame_codec_data = (FRAME_CODEC_INSTANCE*)frame_codec; - /* 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.] */ - if ((frame_codec == NULL) || + /* 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.] */ + if ((frame_codec == NULL) || (on_bytes_encoded == NULL) || - /* 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.] */ - ((type_specific_size > 0) && (type_specific_bytes == NULL)) || - /* 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.] */ - (type_specific_size > MAX_TYPE_SPECIFIC_SIZE)) - { + /* 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.] */ + ((type_specific_size > 0) && (type_specific_bytes == NULL)) || + /* 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.] */ + (type_specific_size > MAX_TYPE_SPECIFIC_SIZE)) + { LogError("Bad arguments: frame_codec = %p, on_bytes_encoded = %p, type_specific_size = %u, type_specific_bytes = %p", frame_codec, on_bytes_encoded, (unsigned int)type_specific_size, type_specific_bytes); - result = __FAILURE__; - } + result = __FAILURE__; + } else if ((payloads == NULL) && (payload_count > 0)) { /* 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.]*/ @@ -577,7 +577,7 @@ result = __FAILURE__; } else - { + { /* round up to the 4 bytes for doff */ /* 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.] */ /* 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.] */ @@ -688,7 +688,7 @@ } } } - } + } - return result; + return result; }