| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include "azure_c_shared_utility/gballoc.h" |
| 6 |
#include "azure_c_shared_utility/xlogging.h" |
| 7 |
|
| 8 |
#include "target.h" |
| 9 |
#include "callee.h" |
| 10 |
|
| 11 |
static CALLEE_HANDLE s_handle = NULL; |
| 12 |
|
| 13 |
TARGET_RESULT target_create(size_t size) |
| 14 |
{ |
| 15 |
TARGET_RESULT result; |
| 16 |
|
| 17 |
if (s_handle == NULL) |
| 18 |
{ |
| 19 |
/* Codes_SRS_TEMPLATE_21_001: [ The target_create shall call callee_open to do stuff and allocate the memory. ]*/ |
| 20 |
s_handle = callee_open(size); |
| 21 |
|
| 22 |
if (s_handle == NULL) |
| 23 |
{ |
| 24 |
/* Codes_SRS_TEMPLATE_21_002: [ If callee_open return error, the target_create shall return TARGET_RESULT_FAIL. ]*/ |
| 25 |
LogError("callee open failed"); |
| 26 |
result = TARGET_RESULT_FAIL; |
| 27 |
} |
| 28 |
else |
| 29 |
{ |
| 30 |
char* a = (char*)malloc(100); |
| 31 |
if (a == NULL) |
| 32 |
{ |
| 33 |
/* Codes_SRS_TEMPLATE_21_003: [ If there is no memory to control the target_create information, it shall return TARGET_RESULT_OUT_OF_MEMORY. ]*/ |
| 34 |
LogError("There is no enough memory"); |
| 35 |
result = TARGET_RESULT_OUT_OF_MEMORY; |
| 36 |
target_destroy(); |
| 37 |
} |
| 38 |
else |
| 39 |
{ |
| 40 |
free(a); |
| 41 |
/* Codes_SRS_TEMPLATE_21_008: [ If callee_open got success, it shall return TARGET_RESULT_OK. ]*/ |
| 42 |
result = TARGET_RESULT_OK; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
/* Codes_SRS_TEMPLATE_21_009: [ If callee_open is called but the connection is already created, it shall return TARGET_RESULT_OK. ]*/ |
| 49 |
result = TARGET_RESULT_OK; |
| 50 |
} |
| 51 |
|
| 52 |
return result; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
void target_destroy(void) |
| 57 |
{ |
| 58 |
if (s_handle == NULL) |
| 59 |
{ |
| 60 |
/* Codes_SRS_TEMPLATE_21_007: [ If target_destroy is called but the connection is not created, the target_destroy shall not do anything. ]*/ |
| 61 |
LogError("try to destroy a connection that was not created"); |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
/* Codes_SRS_TEMPLATE_21_006: [ The target_destroy shall call callee_close to do stuff and free the memory. ]*/ |
| 66 |
callee_close(s_handle); |
| 67 |
s_handle = NULL; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
TARGET_RESULT target_foo(void) |
| 72 |
{ |
| 73 |
TARGET_RESULT result; |
| 74 |
|
| 75 |
if (s_handle == NULL) |
| 76 |
{ |
| 77 |
/* Codes_SRS_TEMPLATE_21_005: [ If target_foo is called but the connection is not created, the target_foo shall return TARGET_RESULT_FAIL. ]*/ |
| 78 |
LogError("try to call foo in a connection that was not created"); |
| 79 |
result = TARGET_RESULT_FAIL; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
/* Codes_SRS_TEMPLATE_21_004: [ The target_foo shall do stuff calling callee_bar_1 and callee_bar_2. ]*/ |
| 84 |
(void)callee_bar_1(); |
| 85 |
|
| 86 |
if (callee_bar_2('a') == CALLEE_RESULT_OK) |
| 87 |
{ |
| 88 |
result = TARGET_RESULT_OK; |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
/* Codes_SRS_TEMPLATE_21_010: [ If target_foo cannot execute foo, the target_foo shall return TARGET_RESULT_FAIL. ]*/ |
| 93 |
result = TARGET_RESULT_FAIL; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return result; |
| 98 |
} |
| 99 |
|