Xin Zhang / azure-iot-c-sdk-f767zi

Dependents:   samplemqtt

Committer:
XinZhangMS
Date:
Thu Aug 23 06:52:14 2018 +0000
Revision:
0:f7f1f0d76dd6
azure-c-sdk for mbed os supporting NUCLEO_F767ZI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
XinZhangMS 0:f7f1f0d76dd6 1 // Copyright (c) Microsoft. All rights reserved.
XinZhangMS 0:f7f1f0d76dd6 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
XinZhangMS 0:f7f1f0d76dd6 3
XinZhangMS 0:f7f1f0d76dd6 4 #include <stdlib.h>
XinZhangMS 0:f7f1f0d76dd6 5 #include <stdint.h>
XinZhangMS 0:f7f1f0d76dd6 6 #include <stddef.h>
XinZhangMS 0:f7f1f0d76dd6 7 #include <string.h>
XinZhangMS 0:f7f1f0d76dd6 8 #include "azure_c_shared_utility/optimize_size.h"
XinZhangMS 0:f7f1f0d76dd6 9 #include "azure_c_shared_utility/gballoc.h"
XinZhangMS 0:f7f1f0d76dd6 10 #include "azure_c_shared_utility/xlogging.h"
XinZhangMS 0:f7f1f0d76dd6 11 #include "azure_uamqp_c/async_operation.h"
XinZhangMS 0:f7f1f0d76dd6 12
XinZhangMS 0:f7f1f0d76dd6 13 typedef struct ASYNC_OPERATION_INSTANCE_TAG
XinZhangMS 0:f7f1f0d76dd6 14 {
XinZhangMS 0:f7f1f0d76dd6 15 ASYNC_OPERATION_CANCEL_HANDLER_FUNC async_operation_cancel_handler;
XinZhangMS 0:f7f1f0d76dd6 16 } ASYNC_OPERATION_INSTANCE;
XinZhangMS 0:f7f1f0d76dd6 17
XinZhangMS 0:f7f1f0d76dd6 18 ASYNC_OPERATION_HANDLE async_operation_create(ASYNC_OPERATION_CANCEL_HANDLER_FUNC async_operation_cancel_handler, size_t context_size)
XinZhangMS 0:f7f1f0d76dd6 19 {
XinZhangMS 0:f7f1f0d76dd6 20 ASYNC_OPERATION_INSTANCE* async_operation;
XinZhangMS 0:f7f1f0d76dd6 21
XinZhangMS 0:f7f1f0d76dd6 22 if (async_operation_cancel_handler == NULL)
XinZhangMS 0:f7f1f0d76dd6 23 {
XinZhangMS 0:f7f1f0d76dd6 24 /* Codes_SRS_ASYNC_OPERATION_01_002: [ If `async_operation_cancel_handler` is NULL, `async_operation_create` shall fail and return NULL.]*/
XinZhangMS 0:f7f1f0d76dd6 25 LogError("Cannot allocate memory for async operation");
XinZhangMS 0:f7f1f0d76dd6 26 async_operation = NULL;
XinZhangMS 0:f7f1f0d76dd6 27 }
XinZhangMS 0:f7f1f0d76dd6 28 else if (context_size < sizeof(ASYNC_OPERATION_INSTANCE))
XinZhangMS 0:f7f1f0d76dd6 29 {
XinZhangMS 0:f7f1f0d76dd6 30 /* Codes_SRS_ASYNC_OPERATION_01_003: [ If `context_size` is less than the size of the `async_operation_cancel_handler` argument, `async_operation_create` shall fail and return NULL.]*/
XinZhangMS 0:f7f1f0d76dd6 31 LogError("Context size too small");
XinZhangMS 0:f7f1f0d76dd6 32 async_operation = NULL;
XinZhangMS 0:f7f1f0d76dd6 33 }
XinZhangMS 0:f7f1f0d76dd6 34 else
XinZhangMS 0:f7f1f0d76dd6 35 {
XinZhangMS 0:f7f1f0d76dd6 36 async_operation = (ASYNC_OPERATION_INSTANCE*)malloc(context_size);
XinZhangMS 0:f7f1f0d76dd6 37 if (async_operation == NULL)
XinZhangMS 0:f7f1f0d76dd6 38 {
XinZhangMS 0:f7f1f0d76dd6 39 /* Codes_SRS_ASYNC_OPERATION_01_004: [ If allocating memory for the new asynchronous operation instance fails, `async_operation_create` shall fail and return NULL.]*/
XinZhangMS 0:f7f1f0d76dd6 40 LogError("Cannot allocate memory for async operation");
XinZhangMS 0:f7f1f0d76dd6 41 }
XinZhangMS 0:f7f1f0d76dd6 42 else
XinZhangMS 0:f7f1f0d76dd6 43 {
XinZhangMS 0:f7f1f0d76dd6 44 /* Codes_SRS_ASYNC_OPERATION_01_001: [ `async_operation_create` shall return a non-NULL handle to a newly created asynchronous operation instance.]*/
XinZhangMS 0:f7f1f0d76dd6 45 async_operation->async_operation_cancel_handler = async_operation_cancel_handler;
XinZhangMS 0:f7f1f0d76dd6 46 }
XinZhangMS 0:f7f1f0d76dd6 47 }
XinZhangMS 0:f7f1f0d76dd6 48
XinZhangMS 0:f7f1f0d76dd6 49 return async_operation;
XinZhangMS 0:f7f1f0d76dd6 50 }
XinZhangMS 0:f7f1f0d76dd6 51
XinZhangMS 0:f7f1f0d76dd6 52 void async_operation_destroy(ASYNC_OPERATION_HANDLE async_operation)
XinZhangMS 0:f7f1f0d76dd6 53 {
XinZhangMS 0:f7f1f0d76dd6 54 if (async_operation == NULL)
XinZhangMS 0:f7f1f0d76dd6 55 {
XinZhangMS 0:f7f1f0d76dd6 56 /* Codes_SRS_ASYNC_OPERATION_01_006: [ If `async_operation` is NULL, `async_operation_destroy` shall do nothing.]*/
XinZhangMS 0:f7f1f0d76dd6 57 LogError("NULL async_operation");
XinZhangMS 0:f7f1f0d76dd6 58 }
XinZhangMS 0:f7f1f0d76dd6 59 else
XinZhangMS 0:f7f1f0d76dd6 60 {
XinZhangMS 0:f7f1f0d76dd6 61 /* Codes_SRS_ASYNC_OPERATION_01_005: [ `async_operation_destroy` shall free all recources associated with the asyncronous operation instance.]*/
XinZhangMS 0:f7f1f0d76dd6 62 free(async_operation);
XinZhangMS 0:f7f1f0d76dd6 63 }
XinZhangMS 0:f7f1f0d76dd6 64 }
XinZhangMS 0:f7f1f0d76dd6 65
XinZhangMS 0:f7f1f0d76dd6 66 int async_operation_cancel(ASYNC_OPERATION_HANDLE async_operation)
XinZhangMS 0:f7f1f0d76dd6 67 {
XinZhangMS 0:f7f1f0d76dd6 68 int result;
XinZhangMS 0:f7f1f0d76dd6 69
XinZhangMS 0:f7f1f0d76dd6 70 if (async_operation == NULL)
XinZhangMS 0:f7f1f0d76dd6 71 {
XinZhangMS 0:f7f1f0d76dd6 72 LogError("NULL async_operation");
XinZhangMS 0:f7f1f0d76dd6 73 result = __FAILURE__;
XinZhangMS 0:f7f1f0d76dd6 74 }
XinZhangMS 0:f7f1f0d76dd6 75 else
XinZhangMS 0:f7f1f0d76dd6 76 {
XinZhangMS 0:f7f1f0d76dd6 77 /* Codes_SRS_ASYNC_OPERATION_01_007: [ `async_operation_cancel` shall cancel the operation by calling the cancel handler function passed to `async_operation_create`.]*/
XinZhangMS 0:f7f1f0d76dd6 78 async_operation->async_operation_cancel_handler(async_operation);
XinZhangMS 0:f7f1f0d76dd6 79
XinZhangMS 0:f7f1f0d76dd6 80 /* Codes_SRS_ASYNC_OPERATION_01_008: [ On success `async_operation_cancel` shall return 0.]*/
XinZhangMS 0:f7f1f0d76dd6 81 result = 0;
XinZhangMS 0:f7f1f0d76dd6 82 }
XinZhangMS 0:f7f1f0d76dd6 83
XinZhangMS 0:f7f1f0d76dd6 84 return result;
XinZhangMS 0:f7f1f0d76dd6 85 }