Azure IoT common library
Fork of azure_c_shared_utility by
constbuffer.c@34:651c23af382c, 2017-08-24 (annotated)
- Committer:
- wiggly
- Date:
- Thu Aug 24 14:14:15 2017 +0100
- Revision:
- 34:651c23af382c
- Parent:
- 19:2e0811512ceb
Pass in network stack to platform initialisation
Remove NTP setup from azure platform code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
Azure.IoT Build | 0:fa2de1b79154 | 4 | // |
Azure.IoT Build | 0:fa2de1b79154 | 5 | // PUT NO INCLUDES BEFORE HERE |
Azure.IoT Build | 0:fa2de1b79154 | 6 | // |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #include <stdlib.h> |
AzureIoTClient | 19:2e0811512ceb | 8 | #include <stddef.h> |
Azure.IoT Build | 0:fa2de1b79154 | 9 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include "azure_c_shared_utility/constbuffer.h" |
Azure.IoT Build | 6:c55b013dfc2a | 11 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:fa2de1b79154 | 12 | #include "azure_c_shared_utility/refcount.h" |
Azure.IoT Build | 0:fa2de1b79154 | 13 | |
Azure.IoT Build | 0:fa2de1b79154 | 14 | typedef struct CONSTBUFFER_HANDLE_DATA_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 15 | { |
Azure.IoT Build | 0:fa2de1b79154 | 16 | CONSTBUFFER alias; |
Azure.IoT Build | 0:fa2de1b79154 | 17 | }CONSTBUFFER_HANDLE_DATA; |
Azure.IoT Build | 0:fa2de1b79154 | 18 | |
Azure.IoT Build | 0:fa2de1b79154 | 19 | DEFINE_REFCOUNT_TYPE(CONSTBUFFER_HANDLE_DATA); |
Azure.IoT Build | 0:fa2de1b79154 | 20 | |
Azure.IoT Build | 0:fa2de1b79154 | 21 | static CONSTBUFFER_HANDLE CONSTBUFFER_Create_Internal(const unsigned char* source, size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 22 | { |
Azure.IoT Build | 0:fa2de1b79154 | 23 | CONSTBUFFER_HANDLE_DATA* result; |
Azure.IoT Build | 0:fa2de1b79154 | 24 | /*Codes_SRS_CONSTBUFFER_02_005: [The non-NULL handle returned by CONSTBUFFER_Create shall have its ref count set to "1".]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 25 | /*Codes_SRS_CONSTBUFFER_02_010: [The non-NULL handle returned by CONSTBUFFER_CreateFromBuffer shall have its ref count set to "1".]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 26 | result = REFCOUNT_TYPE_CREATE(CONSTBUFFER_HANDLE_DATA); |
Azure.IoT Build | 0:fa2de1b79154 | 27 | if (result == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 28 | { |
Azure.IoT Build | 0:fa2de1b79154 | 29 | /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 30 | /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 31 | LogError("unable to malloc"); |
Azure.IoT Build | 0:fa2de1b79154 | 32 | /*return as is*/ |
Azure.IoT Build | 0:fa2de1b79154 | 33 | } |
Azure.IoT Build | 0:fa2de1b79154 | 34 | else |
Azure.IoT Build | 0:fa2de1b79154 | 35 | { |
Azure.IoT Build | 0:fa2de1b79154 | 36 | /*Codes_SRS_CONSTBUFFER_02_002: [Otherwise, CONSTBUFFER_Create shall create a copy of the memory area pointed to by source having size bytes.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 37 | result->alias.size = size; |
Azure.IoT Build | 0:fa2de1b79154 | 38 | if (size == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 39 | { |
Azure.IoT Build | 0:fa2de1b79154 | 40 | result->alias.buffer = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 41 | } |
Azure.IoT Build | 0:fa2de1b79154 | 42 | else |
Azure.IoT Build | 0:fa2de1b79154 | 43 | { |
Azure.IoT Build | 0:fa2de1b79154 | 44 | unsigned char* temp = (unsigned char*)malloc(size); |
Azure.IoT Build | 0:fa2de1b79154 | 45 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 46 | { |
Azure.IoT Build | 0:fa2de1b79154 | 47 | /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 48 | /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 49 | LogError("unable to malloc"); |
Azure.IoT Build | 0:fa2de1b79154 | 50 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 51 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 52 | } |
Azure.IoT Build | 0:fa2de1b79154 | 53 | else |
Azure.IoT Build | 0:fa2de1b79154 | 54 | { |
Azure.IoT Build | 0:fa2de1b79154 | 55 | |
Azure.IoT Build | 0:fa2de1b79154 | 56 | /*Codes_SRS_CONSTBUFFER_02_004: [Otherwise CONSTBUFFER_Create shall return a non-NULL handle.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 57 | /*Codes_SRS_CONSTBUFFER_02_007: [Otherwise, CONSTBUFFER_CreateFromBuffer shall copy the content of buffer.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 58 | /*Codes_SRS_CONSTBUFFER_02_009: [Otherwise, CONSTBUFFER_CreateFromBuffer shall return a non-NULL handle.]*/ |
AzureIoTClient | 19:2e0811512ceb | 59 | (void)memcpy(temp, source, size); |
Azure.IoT Build | 0:fa2de1b79154 | 60 | result->alias.buffer = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 61 | } |
Azure.IoT Build | 0:fa2de1b79154 | 62 | } |
Azure.IoT Build | 0:fa2de1b79154 | 63 | } |
Azure.IoT Build | 0:fa2de1b79154 | 64 | return (CONSTBUFFER_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 65 | } |
Azure.IoT Build | 0:fa2de1b79154 | 66 | |
Azure.IoT Build | 0:fa2de1b79154 | 67 | CONSTBUFFER_HANDLE CONSTBUFFER_Create(const unsigned char* source, size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 68 | { |
Azure.IoT Build | 0:fa2de1b79154 | 69 | CONSTBUFFER_HANDLE_DATA* result; |
Azure.IoT Build | 0:fa2de1b79154 | 70 | /*Codes_SRS_CONSTBUFFER_02_001: [If source is NULL and size is different than 0 then CONSTBUFFER_Create shall fail and return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 71 | if ( |
Azure.IoT Build | 0:fa2de1b79154 | 72 | (source == NULL) && |
Azure.IoT Build | 0:fa2de1b79154 | 73 | (size != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 74 | ) |
Azure.IoT Build | 0:fa2de1b79154 | 75 | { |
Azure.IoT Build | 0:fa2de1b79154 | 76 | LogError("invalid arguments passes to CONSTBUFFER_Create"); |
Azure.IoT Build | 0:fa2de1b79154 | 77 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 78 | } |
Azure.IoT Build | 0:fa2de1b79154 | 79 | else |
Azure.IoT Build | 0:fa2de1b79154 | 80 | { |
Azure.IoT Build | 0:fa2de1b79154 | 81 | result = (CONSTBUFFER_HANDLE_DATA*)CONSTBUFFER_Create_Internal(source, size); |
Azure.IoT Build | 0:fa2de1b79154 | 82 | } |
Azure.IoT Build | 0:fa2de1b79154 | 83 | return (CONSTBUFFER_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 84 | } |
Azure.IoT Build | 0:fa2de1b79154 | 85 | |
Azure.IoT Build | 0:fa2de1b79154 | 86 | /*this creates a new constbuffer from an existing BUFFER_HANDLE*/ |
Azure.IoT Build | 0:fa2de1b79154 | 87 | CONSTBUFFER_HANDLE CONSTBUFFER_CreateFromBuffer(BUFFER_HANDLE buffer) |
Azure.IoT Build | 0:fa2de1b79154 | 88 | { |
Azure.IoT Build | 0:fa2de1b79154 | 89 | CONSTBUFFER_HANDLE_DATA* result; |
Azure.IoT Build | 0:fa2de1b79154 | 90 | /*Codes_SRS_CONSTBUFFER_02_006: [If buffer is NULL then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 91 | if (buffer == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 92 | { |
Azure.IoT Build | 0:fa2de1b79154 | 93 | LogError("invalid arg passed to CONSTBUFFER_CreateFromBuffer"); |
Azure.IoT Build | 0:fa2de1b79154 | 94 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 95 | } |
Azure.IoT Build | 0:fa2de1b79154 | 96 | else |
Azure.IoT Build | 0:fa2de1b79154 | 97 | { |
AzureIoTClient | 11:77df6d7e65ae | 98 | size_t length = BUFFER_length(buffer); |
AzureIoTClient | 11:77df6d7e65ae | 99 | unsigned char* rawBuffer = BUFFER_u_char(buffer); |
AzureIoTClient | 11:77df6d7e65ae | 100 | result = (CONSTBUFFER_HANDLE_DATA*)CONSTBUFFER_Create_Internal(rawBuffer, length); |
Azure.IoT Build | 0:fa2de1b79154 | 101 | } |
Azure.IoT Build | 0:fa2de1b79154 | 102 | return (CONSTBUFFER_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 103 | } |
Azure.IoT Build | 0:fa2de1b79154 | 104 | |
Azure.IoT Build | 0:fa2de1b79154 | 105 | CONSTBUFFER_HANDLE CONSTBUFFER_Clone(CONSTBUFFER_HANDLE constbufferHandle) |
Azure.IoT Build | 0:fa2de1b79154 | 106 | { |
Azure.IoT Build | 0:fa2de1b79154 | 107 | if (constbufferHandle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 108 | { |
Azure.IoT Build | 0:fa2de1b79154 | 109 | /*Codes_SRS_CONSTBUFFER_02_013: [If constbufferHandle is NULL then CONSTBUFFER_Clone shall fail and return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 110 | LogError("invalid arg"); |
Azure.IoT Build | 0:fa2de1b79154 | 111 | } |
Azure.IoT Build | 0:fa2de1b79154 | 112 | else |
Azure.IoT Build | 0:fa2de1b79154 | 113 | { |
Azure.IoT Build | 0:fa2de1b79154 | 114 | /*Codes_SRS_CONSTBUFFER_02_014: [Otherwise, CONSTBUFFER_Clone shall increment the reference count and return constbufferHandle.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 115 | INC_REF(CONSTBUFFER_HANDLE_DATA, constbufferHandle); |
Azure.IoT Build | 0:fa2de1b79154 | 116 | } |
Azure.IoT Build | 0:fa2de1b79154 | 117 | return constbufferHandle; |
Azure.IoT Build | 0:fa2de1b79154 | 118 | } |
Azure.IoT Build | 0:fa2de1b79154 | 119 | |
Azure.IoT Build | 0:fa2de1b79154 | 120 | const CONSTBUFFER* CONSTBUFFER_GetContent(CONSTBUFFER_HANDLE constbufferHandle) |
Azure.IoT Build | 0:fa2de1b79154 | 121 | { |
Azure.IoT Build | 0:fa2de1b79154 | 122 | const CONSTBUFFER* result; |
Azure.IoT Build | 0:fa2de1b79154 | 123 | if (constbufferHandle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 124 | { |
Azure.IoT Build | 0:fa2de1b79154 | 125 | /*Codes_SRS_CONSTBUFFER_02_011: [If constbufferHandle is NULL then CONSTBUFFER_GetContent shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 126 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 127 | LogError("invalid arg"); |
Azure.IoT Build | 0:fa2de1b79154 | 128 | } |
Azure.IoT Build | 0:fa2de1b79154 | 129 | else |
Azure.IoT Build | 0:fa2de1b79154 | 130 | { |
Azure.IoT Build | 0:fa2de1b79154 | 131 | /*Codes_SRS_CONSTBUFFER_02_012: [Otherwise, CONSTBUFFER_GetContent shall return a const CONSTBUFFER* that matches byte by byte the original bytes used to created the const buffer and has the same length.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 132 | result = &(((CONSTBUFFER_HANDLE_DATA*)constbufferHandle)->alias); |
Azure.IoT Build | 0:fa2de1b79154 | 133 | } |
Azure.IoT Build | 0:fa2de1b79154 | 134 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 135 | } |
Azure.IoT Build | 0:fa2de1b79154 | 136 | |
Azure.IoT Build | 0:fa2de1b79154 | 137 | void CONSTBUFFER_Destroy(CONSTBUFFER_HANDLE constbufferHandle) |
Azure.IoT Build | 0:fa2de1b79154 | 138 | { |
Azure.IoT Build | 0:fa2de1b79154 | 139 | /*Codes_SRS_CONSTBUFFER_02_015: [If constbufferHandle is NULL then CONSTBUFFER_Destroy shall do nothing.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 140 | if (constbufferHandle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 141 | { |
Azure.IoT Build | 0:fa2de1b79154 | 142 | /*Codes_SRS_CONSTBUFFER_02_016: [Otherwise, CONSTBUFFER_Destroy shall decrement the refcount on the constbufferHandle handle.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 143 | if (DEC_REF(CONSTBUFFER_HANDLE_DATA, constbufferHandle) == DEC_RETURN_ZERO) |
Azure.IoT Build | 0:fa2de1b79154 | 144 | { |
Azure.IoT Build | 0:fa2de1b79154 | 145 | /*Codes_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_Destroy shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 146 | CONSTBUFFER_HANDLE_DATA* constbufferHandleData = (CONSTBUFFER_HANDLE_DATA*)constbufferHandle; |
Azure.IoT Build | 0:fa2de1b79154 | 147 | free((void*)constbufferHandleData->alias.buffer); |
Azure.IoT Build | 0:fa2de1b79154 | 148 | free(constbufferHandleData); |
Azure.IoT Build | 0:fa2de1b79154 | 149 | } |
Azure.IoT Build | 0:fa2de1b79154 | 150 | } |
Azure.IoT Build | 0:fa2de1b79154 | 151 | } |