tizen 2.3 release
[framework/api/bluetooth.git] / TC / testcase / utc_network_bluetooth_gatt_positive.c
1 /*
2  * utc_network_bluetooth_gatt_positive.c
3  *
4  *  Created on: 23-Sep-2013
5  *      Author: shagun.garg
6  */
7
8 #include <tet_api.h>
9 #include <bluetooth.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include <glib.h>
13
14 #define CONFIG_FILE_PATH "/opt/home/capi-network-bluetooth/tetware.conf"
15
16 enum {
17         POSITIVE_TC_IDX = 0x01,
18         NEGATIVE_TC_IDX,
19 };
20
21 static void startup(void);
22 static void cleanup(void);
23
24 void (*tet_startup) (void) = startup;
25 void (*tet_cleanup) (void) = cleanup;
26
27 char *remote_address;
28 static GMainLoop *mainloop;
29
30 static void utc_network_bluetooth_gatt_foreach_primary_services_p(void);
31 static void utc_network_bluetooth_gatt_discover_characteristics_p(void);
32
33 static void utc_network_bluetooth_gatt_get_service_uuid_p(void);
34 static void utc_network_bluetooth_gatt_foreach_included_services_p(void);
35 static void utc_network_bluetooth_gatt_set_characteristic_changed_cb_p(void);
36 static void utc_network_bluetooth_gatt_unset_characteristic_changed_cb_p(void);
37 static void utc_network_bluetooth_gatt_get_characteristic_declaration_p(void);
38 static void utc_network_bluetooth_gatt_set_characteristic_value_p(void);
39 static void utc_network_bluetooth_gatt_clone_attribute_handle_p(void);
40 static void utc_network_bluetooth_gatt_destroy_attribute_handle_p(void);
41 static void utc_network_bluetooth_gatt_connect_p(void);
42 static void utc_network_bluetooth_gatt_disconnect_p(void);
43 static void utc_network_bluetooth_set_gatt_connection_state_changed_cb_p(void);
44 static void utc_network_bluetooth_unset_gatt_connection_state_changed_cb_p(void);
45
46 gboolean timeout_func(gpointer data);
47 void adapter_state_changed_cb_for_gatt_p(int result, bt_adapter_state_e adapter_state,
48                                         void *user_data);
49 void adapter_le_state_changed_cb_for_gatt_p(int result, bt_adapter_le_state_e adapter_state,
50                                         void *user_data);
51
52 bool primary_service_cb_for_gatt_p(bt_gatt_attribute_h service, void *user_data);
53 bool included_service_cb_for_gatt_p(bt_gatt_attribute_h service, void *user_data);
54 bool characteristics_discovered_cb_for_gatt_p(int result, int index, int total, bt_gatt_attribute_h characteristic,
55                                         void *user_data);
56 void characteristics_changed_cb_for_gatt_p(bt_gatt_attribute_h characteristic, unsigned char *value, int value_length,
57                                         void *user_data);
58 void gatt_connection_state_changed_cb(int result, bool connected,
59                                         const char *remote_address, void *user_data);
60
61 struct tet_testlist tet_testlist[] = {
62         {utc_network_bluetooth_gatt_foreach_primary_services_p, POSITIVE_TC_IDX},
63         {utc_network_bluetooth_gatt_discover_characteristics_p, POSITIVE_TC_IDX},
64         {utc_network_bluetooth_gatt_get_service_uuid_p, POSITIVE_TC_IDX},
65         {utc_network_bluetooth_gatt_foreach_included_services_p, POSITIVE_TC_IDX},
66         {utc_network_bluetooth_gatt_set_characteristic_changed_cb_p, POSITIVE_TC_IDX},
67         {utc_network_bluetooth_set_gatt_connection_state_changed_cb_p, POSITIVE_TC_IDX},
68         {utc_network_bluetooth_gatt_connect_p, POSITIVE_TC_IDX},
69         {utc_network_bluetooth_gatt_disconnect_p, POSITIVE_TC_IDX},
70         {utc_network_bluetooth_unset_gatt_connection_state_changed_cb_p, POSITIVE_TC_IDX},
71         {utc_network_bluetooth_gatt_unset_characteristic_changed_cb_p, POSITIVE_TC_IDX},
72         {utc_network_bluetooth_gatt_get_characteristic_declaration_p, POSITIVE_TC_IDX},
73         {utc_network_bluetooth_gatt_set_characteristic_value_p, POSITIVE_TC_IDX},
74         {utc_network_bluetooth_gatt_clone_attribute_handle_p, POSITIVE_TC_IDX},
75         {utc_network_bluetooth_gatt_destroy_attribute_handle_p, POSITIVE_TC_IDX},
76         {NULL, 0},
77 };
78
79 int get_value_from_file(void)
80 {
81         FILE *fp;
82         char *token;
83         char buf[100];
84
85         fp = fopen(CONFIG_FILE_PATH, "r");
86         if (fp == NULL) {
87                 tet_printf("Default configuration is used\n");
88                 return -1;
89         }
90         while (fgets(buf, sizeof(buf), fp)) {
91                 if (buf[0] == '#' || buf[0] == '\n')
92                         continue;
93
94                 token = strrchr(buf, '\n');
95                 if (token == NULL) {
96                         tet_printf("g_conf is too long\n");
97                         break;
98                 }
99                 *token = '\0';
100
101                 token = strtok(buf, "=");
102                 if (token == NULL) {
103                         continue;
104                 }
105                 if (strcasecmp(token, "BT_ADDR_LE") == 0) {
106                         token = strtok(NULL, "=");
107                         remote_address = strdup(token);
108                         return 0;
109                 }
110         }
111         return -1;
112 }
113
114 static void startup(void)
115 {
116         bt_error_e ret = BT_ERROR_NONE;
117         int timeout_id = 0;
118
119         if(get_value_from_file() == -1) {
120                 tet_printf("Failed to read.");
121         }
122
123         /* start of TC */
124         tet_printf("TC start.");
125         mainloop = g_main_loop_new(NULL, FALSE);
126
127         bt_initialize();
128
129         if (bt_adapter_set_state_changed_cb(adapter_state_changed_cb_for_gatt_p, "startup") != BT_ERROR_NONE) {
130                 tet_printf("DTS may fail because bt_adapter_set_state_changed_cb() failed");
131         }
132
133         if (bt_adapter_le_set_state_changed_cb(adapter_le_state_changed_cb_for_gatt_p, "enable") != BT_ERROR_NONE) {
134                 dts_fail("bt_adapter_le_enable",
135                         "bt_adapter_le_set_state_changed_cb() failed.");
136         }
137
138         tet_printf("bt_adapter_enable() was called.");
139         ret = bt_adapter_enable();
140         if (ret == BT_ERROR_NONE) {
141                 tet_printf("bt_adapter_enable() succeeded.");
142                 tet_printf("bt_adapter_le_enable() was called.");
143                 ret = bt_adapter_le_enable();
144                 if (ret == BT_ERROR_NONE) {
145                         tet_printf("bt_adapter_le_enable() succeeded.");
146                         timeout_id = g_timeout_add(60000, timeout_func, mainloop);
147                         g_main_loop_run(mainloop);
148                         g_source_remove(timeout_id);
149                 }
150                 else {
151                         tet_printf("DTS may fail because bt_adapter_le_enable failed");
152                         bt_adapter_le_unset_state_changed_cb();
153                 }
154         } else if (ret == BT_ERROR_NOT_ENABLED) {
155                 tet_printf("Bluetooth adapter is not enabled.");
156         } else {
157                 tet_printf("DTS may fail because bt_adapter_enable failed");
158         }
159
160         if (bt_adapter_unset_state_changed_cb() != BT_ERROR_NONE) {
161                 tet_printf("bt_adapter_set_state_changed_cb() failed.");
162         }
163
164         tet_printf("TC start");
165 }
166
167 static void cleanup(void)
168 {
169         /* end of TC */
170         bt_deinitialize();
171         tet_printf("TC end.");
172 }
173
174 gboolean timeout_func(gpointer data)
175 {
176         tet_printf("[%s] Callback: Timeout.", __FUNCTION__);
177         g_main_loop_quit((GMainLoop *) data);
178         return FALSE;
179 }
180
181
182
183 void adapter_state_changed_cb_for_gatt_p(int result, bt_adapter_state_e adapter_state, void* user_data)
184 {
185
186 }
187 void adapter_le_state_changed_cb_for_gatt_p(int result, bt_adapter_le_state_e adapter_le__state, void* user_data)
188 {
189
190 }
191
192 bool primary_service_cb_for_gatt_p(bt_gatt_attribute_h service, void *user_data)
193 {
194         return false;
195 }
196
197 bool characteristics_discovered_cb_for_gatt_p(int result, int index, int total, bt_gatt_attribute_h characteristic,
198                                 void *user_data)
199 {
200         return false;
201 }
202
203 bool included_service_cb_for_gatt_p(bt_gatt_attribute_h service, void *user_data)
204 {
205         return false;
206 }
207
208 void characteristics_changed_cb_for_gatt_p(bt_gatt_attribute_h characteristic, unsigned char *value, int value_length,
209                                 void *user_data)
210 {
211
212 }
213
214 void gatt_connection_state_changed_cb(int result, bool connected,
215                                         const char *remote_address, void *user_data)
216 {
217         tet_printf("bt_gatt_connection_state_changed_cb : %d", result);
218         if (connected)
219                 tet_printf("LE Connected");
220         else
221                 tet_printf("LE Disconnected");
222 }
223
224 /**
225  * @brief Positive test case of bt_gatt_foreach_primary_services()
226  */
227 static void utc_network_bluetooth_gatt_foreach_primary_services_p(void)
228 {
229         int ret = BT_ERROR_NONE;
230
231         ret = bt_gatt_foreach_primary_services(remote_address, primary_service_cb_for_gatt_p, NULL);
232         dts_check_eq("bt_gatt_foreach_primary_services", ret, BT_ERROR_NONE,
233                 "bt_gatt_foreach_primary_services() failed.");
234 }
235
236
237 /**
238  * @brief Positive test case of bt_gatt_discover_characteristics()
239  */
240 static void utc_network_bluetooth_gatt_discover_characteristics_p(void)
241 {
242         int ret = BT_ERROR_NONE;
243         bt_gatt_attribute_h gatt_serv;
244         gatt_serv = g_malloc0(sizeof(bt_gatt_attribute_h));
245
246         ret = bt_gatt_discover_characteristics(gatt_serv, characteristics_discovered_cb_for_gatt_p, NULL);
247         dts_check_eq("bt_gatt_discover_characteristics", ret, BT_ERROR_NONE, "bt_gatt_discover_characteristics() failed.");
248 }
249
250
251 /**
252  * @brief Positive test case of bt_gatt_get_service_uuid()
253  */
254 static void utc_network_bluetooth_gatt_get_service_uuid_p(void)
255 {
256         int ret = BT_ERROR_NONE;
257         char *uid = "dts_uid";
258         bt_gatt_attribute_h gatt_serv;
259         gatt_serv = g_malloc0(sizeof(bt_gatt_attribute_h));
260
261         ret = bt_gatt_get_service_uuid(gatt_serv, &uid);
262         dts_check_eq("bt_gatt_get_service_uuid", ret, BT_ERROR_NONE,
263                 "bt_gatt_get_service_uuid() failed.");
264 }
265
266 /**
267  * @brief Positive test case of bt_gatt_foreach_included_services()
268  */
269 static void utc_network_bluetooth_gatt_foreach_included_services_p(void)
270 {
271         int ret = BT_ERROR_NONE;
272         bt_gatt_attribute_h gatt_serv;
273         gatt_serv = g_malloc0(sizeof(bt_gatt_attribute_h));
274         ret = bt_gatt_foreach_included_services(gatt_serv, included_service_cb_for_gatt_p, NULL);
275         dts_check_eq("bt_gatt_foreach_included_services", ret, BT_ERROR_NONE,
276                 "bt_gatt_foreach_included_services() failed.");
277 }
278
279
280 /**
281  * @brief Positive test case of bt_gatt_set_characteristic_changed_cb()
282  */
283 static void utc_network_bluetooth_gatt_set_characteristic_changed_cb_p(void)
284 {
285         int ret = BT_ERROR_NONE;
286
287         ret = bt_gatt_set_characteristic_changed_cb(characteristics_changed_cb_for_gatt_p, NULL);
288         dts_check_eq("bt_gatt_set_characteristic_changed_cb", ret, BT_ERROR_NONE,
289                 "bt_gatt_set_characteristic_changed_cb() failed.");
290 }
291
292
293 /**
294  * @brief Positive test case of bt_gatt_unset_characteristic_changed_cb()
295  */
296 static void utc_network_bluetooth_gatt_unset_characteristic_changed_cb_p(void)
297 {
298         int ret = BT_ERROR_NONE;
299
300         ret = bt_gatt_unset_characteristic_changed_cb();
301         dts_check_eq("bt_gatt_unset_characteristic_changed_cb", ret, BT_ERROR_NONE,
302                         "bt_gatt_unset_characteristic_changed_cb() failed.");
303
304 }
305
306
307 /**
308  * @brief Positive test case of bt_gatt_get_characteristic_declaration()
309  */
310 static void utc_network_bluetooth_gatt_get_characteristic_declaration_p(void)
311 {
312         int ret = BT_ERROR_NONE;
313         char **uid = "dts_uid";
314         unsigned char **val = "dts_value";
315         int l = 1;
316         bt_gatt_attribute_h gatt_charac;
317         gatt_charac = g_malloc0(sizeof(bt_gatt_attribute_h));
318         ret = bt_gatt_get_characteristic_declaration(gatt_charac, uid, val, &l);
319         dts_check_eq("bt_gatt_get_characteristic_declaration", ret, BT_ERROR_NONE,
320                         "gatt_get_characteristic_declaration() failed.");
321 }
322
323
324 /**
325  * @brief Positive test case of bt_gatt_set_characteristic_value()
326  */
327 static void utc_network_bluetooth_gatt_set_characteristic_value_p(void)
328 {
329         int ret = BT_ERROR_NONE;
330         bt_gatt_attribute_h gatt_charac;
331         gatt_charac = g_malloc0(sizeof(bt_gatt_attribute_h));
332         const unsigned char *gatt_value = "dts_value";
333         ret = bt_gatt_set_characteristic_value(gatt_charac, gatt_value, strlen("dts_value"));
334         dts_check_eq("bt_gatt_set_characteristic_value", ret, BT_ERROR_NONE,
335                         "bt_gatt_set_characteristic_value() failed.");
336 }
337
338
339 /**
340  * @brief Positive test case of bt_gatt_clone_attribute_handle()
341  */
342 static void utc_network_bluetooth_gatt_clone_attribute_handle_p(void)
343 {
344         int ret = BT_ERROR_NONE;
345         bt_gatt_attribute_h gatt_origin;
346         bt_gatt_attribute_h gatt_clone;
347         gatt_origin = g_malloc0(sizeof(bt_gatt_attribute_h));
348         gatt_clone = g_malloc0(sizeof(bt_gatt_attribute_h));
349
350         ret = bt_gatt_clone_attribute_handle(&gatt_clone, gatt_origin);
351         dts_check_eq("bt_gatt_clone_attribute_handle", ret, BT_ERROR_NONE,
352                 "bt_gatt_clone_attribute_handle() failed.");
353 }
354
355
356 /**
357  * @brief Positive test case of bt_gatt_destroy_attribute_handle()
358  */
359 static void utc_network_bluetooth_gatt_destroy_attribute_handle_p(void)
360 {
361         int ret = BT_ERROR_NONE;
362         bt_gatt_attribute_h gatt_handle;
363         gatt_handle = g_malloc0(sizeof(bt_gatt_attribute_h));
364
365         ret = bt_gatt_destroy_attribute_handle(gatt_handle);
366         dts_check_eq("bt_gatt_destroy_attribute_handle", ret, BT_ERROR_NONE,
367                 "bt_gatt_destroy_attribute_handle() failed.");
368 }
369
370 static void utc_network_bluetooth_gatt_connect_p(void)
371 {
372         int ret = BT_ERROR_NONE;
373
374         ret = bt_gatt_connect(remote_address, false);
375         dts_check_eq("bt_gatt_connect", ret, BT_ERROR_NONE,
376                 "bt_gatt_connect() failed.");
377
378 }
379
380 static void utc_network_bluetooth_gatt_disconnect_p(void)
381 {
382         int ret = BT_ERROR_NONE;
383         ret = bt_gatt_disconnect(remote_address);
384         dts_check_eq("bt_gatt_connect", ret, BT_ERROR_NONE,
385                 "bt_gatt_connect() failed.");
386
387 }
388
389 static void utc_network_bluetooth_set_gatt_connection_state_changed_cb_p(void)
390 {
391         int ret = BT_ERROR_NONE;
392
393         ret = bt_gatt_set_connection_state_changed_cb(gatt_connection_state_changed_cb, NULL);
394         dts_check_eq("bt_gatt_set_connection_state_changed_cb", ret, BT_ERROR_NONE,
395                 "bt_gatt_set_connection_state_changed_cb() failed.");
396 }
397
398 static void utc_network_bluetooth_unset_gatt_connection_state_changed_cb_p(void)
399 {
400         int ret = BT_ERROR_NONE;
401
402         ret = bt_gatt_unset_connection_state_changed_cb();
403         dts_check_eq("bt_gatt_unset_connection_state_changed_cb", ret, BT_ERROR_NONE,
404                 "bt_gatt_unset_connection_state_changed_cb() failed.");
405 }