[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / net_nfc_client_data.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <string.h>
18
19 #include "net_nfc_data.h"
20 #include "net_nfc_typedef_internal.h"
21 #include "net_nfc_util_internal.h"
22
23 #ifndef NET_NFC_EXPORT_API
24 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
25 #endif
26
27 NET_NFC_EXPORT_API net_nfc_error_e net_nfc_create_data_only(data_h *data)
28 {
29         return net_nfc_create_data(data, NULL, 0);
30 }
31
32 NET_NFC_EXPORT_API net_nfc_error_e net_nfc_create_data(data_h *data,
33         const uint8_t *bytes, size_t length)
34 {
35         data_s *tmp_data = NULL;
36
37         if (data == NULL)
38         {
39                 return NET_NFC_NULL_PARAMETER;
40         }
41
42         *data = NULL;
43
44         tmp_data = net_nfc_util_create_data(length);
45         if (tmp_data == NULL)
46                 return NET_NFC_ALLOC_FAIL;
47
48         if (length > 0 && bytes != NULL)
49         {
50                 memcpy(tmp_data->buffer, bytes, length);
51         }
52
53         *data = (data_h)tmp_data;
54
55         return NET_NFC_OK;
56 }
57
58 NET_NFC_EXPORT_API net_nfc_error_e net_nfc_get_data(const data_h data,
59         uint8_t **bytes, size_t *length)
60 {
61         data_s *tmp_data = (data_s *)data;
62
63         if (bytes == NULL || length == NULL)
64                 return NET_NFC_NULL_PARAMETER;
65
66         *bytes = NULL;
67         *length = 0;
68
69         if (data == NULL)
70         {
71                 return NET_NFC_NULL_PARAMETER;
72         }
73
74         *bytes = tmp_data->buffer;
75         *length = tmp_data->length;
76
77         return NET_NFC_OK;
78 }
79
80 NET_NFC_EXPORT_API net_nfc_error_e net_nfc_set_data(data_h data,
81         const uint8_t *bytes, size_t length)
82 {
83         data_s *tmp_data = (data_s *)data;
84
85         if (data == NULL)
86         {
87                 return NET_NFC_NULL_PARAMETER;
88         }
89
90         if (tmp_data->buffer == bytes && tmp_data->length == length)
91         {
92                 return NET_NFC_OK;
93         }
94
95         net_nfc_util_clear_data(tmp_data);
96
97         if (length > 0)
98         {
99                 net_nfc_util_init_data(tmp_data, length);
100
101                 if (bytes != NULL)
102                 {
103                         memcpy(tmp_data->buffer, bytes, length);
104                 }
105         }
106
107         return NET_NFC_OK;
108 }
109
110 NET_NFC_EXPORT_API size_t net_nfc_get_data_length(const data_h data)
111 {
112         data_s *tmp_data = (data_s *)data;
113
114         if (data == NULL)
115         {
116                 return 0;
117         }
118
119         return tmp_data->length;
120 }
121
122 NET_NFC_EXPORT_API uint8_t *net_nfc_get_data_buffer(const data_h data)
123 {
124         data_s *tmp_data = (data_s *)data;
125
126         if (data == NULL)
127         {
128                 return NULL;
129         }
130
131         return tmp_data->buffer;
132 }
133
134 NET_NFC_EXPORT_API net_nfc_error_e net_nfc_free_data(data_h data)
135 {
136         data_s *tmp_data = (data_s *)data;
137
138         if (data == NULL)
139         {
140                 return NET_NFC_NULL_PARAMETER;
141         }
142
143         if (tmp_data->buffer != NULL)
144         {
145                 _net_nfc_util_free_mem(tmp_data->buffer);
146         }
147         _net_nfc_util_free_mem(tmp_data);
148
149         return NET_NFC_OK;
150 }