Remove thread-scope variables for sharing handles between threads
[platform/core/api/tethering.git] / src / tethering_client.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 <stdlib.h>
18 #include <string.h>
19 #include "tethering_private.h"
20
21 API int tethering_client_clone(tethering_client_h *dest, tethering_client_h origin)
22 {
23         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
24
25         _retvm_if(dest == NULL, TETHERING_ERROR_INVALID_PARAMETER,
26                         "Parameter(dest) is NULL\n");
27         _retvm_if(origin == NULL, TETHERING_ERROR_INVALID_PARAMETER,
28                         "Parameter(origin) is NULL\n");
29
30         __tethering_client_h *si = NULL;
31         __tethering_client_h *source = NULL;
32
33         source = (__tethering_client_h *)origin;
34
35         si = malloc(sizeof(__tethering_client_h));
36         if (si == NULL) {
37                 ERR("malloc is failed\n");
38                 return TETHERING_ERROR_OUT_OF_MEMORY;
39         }
40
41         memcpy(si, source, sizeof(__tethering_client_h));
42         si->hostname = g_strdup(source->hostname);
43         if (si->hostname == NULL) {
44                 ERR("malloc is failed\n");
45                 free(si);
46                 return TETHERING_ERROR_OUT_OF_MEMORY;
47         }
48
49         *dest = (tethering_client_h)si;
50
51         return TETHERING_ERROR_NONE;
52 }
53
54 API int tethering_client_destroy(tethering_client_h client)
55 {
56         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
57
58         _retvm_if(client == NULL, TETHERING_ERROR_INVALID_PARAMETER,
59                         "Parameter(client) is NULL\n");
60
61         __tethering_client_h *si = NULL;
62
63         si = (__tethering_client_h *)client;
64
65         g_free(si->hostname);
66
67         free(client);
68
69         return TETHERING_ERROR_NONE;
70 }
71
72 API int tethering_client_get_tethering_type(tethering_client_h client, tethering_type_e *type)
73 {
74         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
75
76         _retvm_if(client == NULL, TETHERING_ERROR_INVALID_PARAMETER,
77                         "Parameter(client) is NULL\n");
78         _retvm_if(type == NULL, TETHERING_ERROR_INVALID_PARAMETER,
79                         "Parameter(type) is NULL\n");
80
81         __tethering_client_h *si = (__tethering_client_h *)client;
82
83         *type = si->interface;
84
85         return TETHERING_ERROR_NONE;
86 }
87
88 API int tethering_client_get_tethering_band(tethering_client_h client, tethering_band_e *band)
89 {
90         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
91
92         _retvm_if(client == NULL, TETHERING_ERROR_INVALID_PARAMETER,
93                         "Parameter(client) is NULL\n");
94         _retvm_if(band == NULL, TETHERING_ERROR_INVALID_PARAMETER,
95                         "Parameter(band) is NULL\n");
96
97         __tethering_client_h *si = (__tethering_client_h *)client;
98
99         *band = si->band;
100
101         return TETHERING_ERROR_NONE;
102 }
103
104 API int tethering_client_get_name(tethering_client_h client, char **name)
105 {
106         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
107
108         _retvm_if(client == NULL, TETHERING_ERROR_INVALID_PARAMETER,
109                         "Parameter(client) is NULL\n");
110         _retvm_if(name == NULL, TETHERING_ERROR_INVALID_PARAMETER,
111                         "Parameter(name) is NULL\n");
112
113         __tethering_client_h *si = (__tethering_client_h *)client;
114
115         *name = strdup(si->hostname);
116         if (*name == NULL) {
117                 ERR("strdup is failed\n");
118                 return TETHERING_ERROR_OUT_OF_MEMORY;
119         }
120
121         return TETHERING_ERROR_NONE;
122 }
123
124 API int tethering_client_get_ip_address(tethering_client_h client, tethering_address_family_e address_family, char **ip_address)
125 {
126         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
127
128         _retvm_if(client == NULL, TETHERING_ERROR_INVALID_PARAMETER,
129                         "Parameter(client) is NULL\n");
130         _retvm_if(ip_address == NULL, TETHERING_ERROR_INVALID_PARAMETER,
131                         "Parameter(ip_address) is NULL\n");
132
133         __tethering_client_h *si = (__tethering_client_h *)client;
134
135         *ip_address = strdup(si->ip);
136         if (*ip_address == NULL) {
137                 ERR("strdup is failed\n");
138                 return TETHERING_ERROR_OUT_OF_MEMORY;
139         }
140
141         return TETHERING_ERROR_NONE;
142 }
143
144 API int tethering_client_get_mac_address(tethering_client_h client, char **mac_address)
145 {
146         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
147
148         _retvm_if(client == NULL, TETHERING_ERROR_INVALID_PARAMETER,
149                         "Parameter(client) is NULL\n");
150         _retvm_if(mac_address == NULL, TETHERING_ERROR_INVALID_PARAMETER,
151                         "Parameter(mac_address) is NULL\n");
152
153         __tethering_client_h *si = (__tethering_client_h *)client;
154
155         *mac_address = strdup(si->mac);
156         if (*mac_address == NULL) {
157                 ERR("strdup is failed\n");
158                 return TETHERING_ERROR_OUT_OF_MEMORY;
159         }
160
161         return TETHERING_ERROR_NONE;
162 }
163
164 API int tethering_client_get_time(tethering_client_h client, time_t *timestamp)
165 {
166         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
167
168         _retvm_if(client == NULL, TETHERING_ERROR_INVALID_PARAMETER,
169                         "Parameter(client) is NULL\n");
170         _retvm_if(timestamp == NULL, TETHERING_ERROR_INVALID_PARAMETER,
171                         "Parameter(timestamp) is NULL\n");
172
173
174         __tethering_client_h *si = (__tethering_client_h *)client;
175
176         *timestamp = si->tm;
177
178         return TETHERING_ERROR_NONE;
179 }
180