custom_data.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file custom_data.c
9 * @date Jul 27, 2016
10 * @brief Custom data module implementation file.
11 *
12 ************************************************************************/
13 
14 #include "custom_data.h"
15 #include "memory.h"
16 #include "log_util.h"
17 #include "definitions.h"
18 #include "mcl/mcl_custom_data.h"
19 #include "json_util.h"
20 
21 // Private Function Prototypes:
22 static E_MCL_ERROR_CODE _initialize_custom_data_meta_fields(custom_data_t *custom_data, const char *type, const char *version, const char *routing);
23 
24 E_MCL_ERROR_CODE custom_data_initialize(const char *version, const char *type, const char *routing, custom_data_t **custom_data)
25 {
26  DEBUG_ENTRY("const char *version = <%s>, const char *type = <%s>, const char *routing = <%p>, custom_data_t **custom_data = <%p>", version, type, routing, custom_data)
27 
28  E_MCL_ERROR_CODE code;
29 
30  // New custom_data:
31  MCL_NEW(*custom_data);
32  ASSERT_CODE_MESSAGE(MCL_NULL != *custom_data, MCL_OUT_OF_MEMORY, "Memory can not be allocated for custom data.");
33 
34  // Initialize custom data struct members.
35  code = _initialize_custom_data_meta_fields(*custom_data, type, version, routing);
36  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, custom_data_destroy(custom_data), code, "custom_data initialization fail.");
37 
38  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
39  return MCL_OK;
40 }
41 
43 {
44  DEBUG_ENTRY("mcl_custom_data_t *custom_data = <%p>, const mcl_json_t *details = <%p>", custom_data, details)
45 
46  E_MCL_ERROR_CODE code;
47 
48  ASSERT_NOT_NULL(custom_data);
49  ASSERT_NOT_NULL(details);
50 
51  code = json_util_duplicate(details, MCL_TRUE, &custom_data->meta.payload.details.custom_details.details_json);
52 
53  DEBUG_LEAVE("retVal = <%d>", code);
54  return code;
55 }
56 
58 {
59  DEBUG_ENTRY("mcl_custom_data_t *custom_data = <%p>, const mcl_uint8_t *content = <%p>, mcl_size_t content_size = <%u>", custom_data, content, content_size)
60 
61  ASSERT_NOT_NULL(custom_data);
62  ASSERT_NOT_NULL(content);
63  ASSERT_CODE_MESSAGE(0 < content_size, MCL_INVALID_PARAMETER, "content_size is not bigger than zero.");
64 
65  custom_data->payload.size = content_size;
66  custom_data->payload.buffer = (mcl_uint8_t *)content;
67 
68  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
69  return MCL_OK;
70 }
71 
73 {
74  DEBUG_ENTRY("custom_data_t **custom_data = <%p>", custom_data)
75 
76  if (MCL_NULL != *custom_data)
77  {
78  string_destroy(&((*custom_data)->meta.content_id));
79  string_destroy(&((*custom_data)->meta.type));
80  string_destroy(&((*custom_data)->meta.version));
81  string_destroy(&((*custom_data)->meta.details.routing));
82  string_destroy(&((*custom_data)->meta.payload.type));
83  string_destroy(&((*custom_data)->meta.payload.version));
84  json_util_destroy(&((*custom_data)->meta.payload.details.custom_details.details_json));
85  MCL_FREE(*custom_data);
86  }
87 
88  DEBUG_LEAVE("retVal = void");
89 }
90 
91 // Private Functions:
92 static E_MCL_ERROR_CODE _initialize_custom_data_meta_fields(custom_data_t *custom_data, const char *type, const char *version, const char *routing)
93 {
94  DEBUG_ENTRY("custom_data_t *custom_data = <%p>, const char *type = <%s>, const char *version = <%s>, const char *routing = <%p>", custom_data, type, version, routing)
95 
96  E_MCL_ERROR_CODE code;
97 
98  custom_data->meta.content_id = MCL_NULL;
99  custom_data->meta.type = MCL_NULL;
100  custom_data->meta.version = MCL_NULL;
101  custom_data->meta.details.routing = MCL_NULL;
102  custom_data->meta.payload.type = MCL_NULL;
103  custom_data->meta.payload.version = MCL_NULL;
105  custom_data->payload.size = 0;
106  custom_data->payload.buffer = MCL_NULL;
107 
108  // Set meta.type.
110  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "String initialize fail for meta_type.");
111 
112  // Set meta.version.
114  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "String initialize fail for meta_version.");
115 
116  // Set routing which is optional.
117  if (MCL_NULL != routing)
118  {
119  code = string_initialize_new(routing, 0, &custom_data->meta.details.routing);
120  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "String initialize fail for routing.");
121  }
122 
123  // Set Fill meta_payload_type.
124  code = string_initialize_new(type, 0, &(custom_data->meta.payload.type));
125  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "String initialize fail for meta_payload_type.");
126 
127  // Set meta.payload.version.
128  code = string_initialize_new(version, 0, &(custom_data->meta.payload.version));
129  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "String initialize fail for meta_payload_version.");
130 
131  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
132  return MCL_OK;
133 }
struct mcl_json_t mcl_json_t
This struct is used for json handling.
item_meta_t meta
Meta of custom data.
Definition: custom_data.h:26
void string_destroy(string_t **string)
Destroys the allocated resources of the string.
Definition: string_type.c:326
string_t * routing
Information helping the server-side routing mechanism.
Definition: data_types.h:32
item_meta_payload_details_union_t details
Type and version specific meta information about the payload.
Definition: data_types.h:78
string_t * version
Version of payload.
Definition: data_types.h:77
string_t * type
Type of payload.
Definition: data_types.h:76
Memory module header file.
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
Json util module header file.
string_t * content_id
Unique identifier of the group.
Definition: data_types.h:86
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
#define MCL_NEW(p)
Definition: memory.h:121
static E_MCL_ERROR_CODE _initialize_custom_data_meta_fields(custom_data_t *custom_data, const char *type, const char *version, const char *routing)
Definition: custom_data.c:92
E_MCL_ERROR_CODE string_initialize_new(const char *value, mcl_size_t value_length, string_t **string)
Initializes a new string_t object with the given value and length.
Definition: string_type.c:46
#define MCL_TRUE
Definition: mcl_common.h:54
string_t * type
Type of meta.
Definition: data_types.h:87
E_MCL_ERROR_CODE mcl_custom_data_set_meta_details(mcl_custom_data_t *custom_data, const mcl_json_t *details)
Adds a custom details object to custom_data.
Definition: custom_data.c:42
mcl_uint8_t * buffer
Binary payload buffer.
Definition: data_types.h:166
E_MCL_ERROR_CODE custom_data_initialize(const char *version, const char *type, const char *routing, custom_data_t **custom_data)
Initializes custom_data.
Definition: custom_data.c:24
#define ASSERT_NOT_NULL(argument)
Definition: definitions.h:129
mcl_size_t size
Binary payload size.
Definition: data_types.h:167
Log utility module header file.
Current version of meta field.
Definition: data_types.h:284
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
binary_payload_t payload
Payload of custom data.
Definition: custom_data.h:27
void json_util_destroy(json_t **root)
This function destroys root.
Definition: json_util.c:863
struct mcl_custom_data_t mcl_custom_data_t
This struct is used for building the custom data type.
#define MCL_FREE(p)
Definition: memory.h:125
item_meta_payload_t payload
Information describing the payload part following this meta or a collection of tuples referencing it...
Definition: data_types.h:91
#define ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
Definition: definitions.h:121
item_meta_payload_details_custom_t custom_details
Custom details.
Definition: data_types.h:68
Custom data module interface header file.
General invalid parameter fail.
Definition: mcl_common.h:144
uint8_t mcl_uint8_t
Definition: mcl_common.h:43
#define ASSERT_CODE_MESSAGE(condition, return_code,...)
Definition: definitions.h:105
Definitions module header file.
string_t * version
Version of meta.
Definition: data_types.h:88
Item type of meta field.
Definition: data_types.h:283
This struct is used for building the custom data type.
Definition: custom_data.h:24
string_t meta_field_values[META_FIELD_VALUES_END]
Definition: data_types.c:36
size_t mcl_size_t
Definition: mcl_common.h:38
E_MCL_ERROR_CODE json_util_duplicate(const json_t *source_json, mcl_bool_t with_children, json_t **duplicated_json)
This function duplicates source_json as duplicated_json.
Definition: json_util.c:805
Success.
Definition: mcl_common.h:140
Custom data module header file.
E_MCL_ERROR_CODE string_initialize_static(const char *value, mcl_size_t value_length, string_t **string)
Initializes a static string_t object with the given value and length.
Definition: string_type.c:90
mcl_json_t * details_json
Any details.
Definition: data_types.h:58
void custom_data_destroy(custom_data_t **custom_data)
Destroys custom_data.
Definition: custom_data.c:72
Memory allocation fail.
Definition: mcl_common.h:143
#define MCL_NULL
Definition: definitions.h:24
E_MCL_ERROR_CODE mcl_custom_data_set_payload(mcl_custom_data_t *custom_data, const mcl_uint8_t *content, mcl_size_t content_size)
Sets payload contents.
Definition: custom_data.c:57
item_meta_details_t details
Type and version-specific meta-information.
Definition: data_types.h:90