2 * utc_network_bluetooth_gatt_positive.c
4 * Created on: 23-Sep-2013
14 #define CONFIG_FILE_PATH "/opt/home/capi-network-bluetooth/tetware.conf"
17 POSITIVE_TC_IDX = 0x01,
21 static void startup(void);
22 static void cleanup(void);
24 void (*tet_startup) (void) = startup;
25 void (*tet_cleanup) (void) = cleanup;
28 static GMainLoop *mainloop;
30 static void utc_network_bluetooth_gatt_foreach_primary_services_p(void);
31 static void utc_network_bluetooth_gatt_discover_characteristics_p(void);
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);
46 gboolean timeout_func(gpointer data);
47 void adapter_state_changed_cb_for_gatt_p(int result, bt_adapter_state_e adapter_state,
49 void adapter_le_state_changed_cb_for_gatt_p(int result, bt_adapter_le_state_e adapter_state,
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,
56 void characteristics_changed_cb_for_gatt_p(bt_gatt_attribute_h characteristic, unsigned char *value, int value_length,
58 void gatt_connection_state_changed_cb(int result, bool connected,
59 const char *remote_address, void *user_data);
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},
79 int get_value_from_file(void)
85 fp = fopen(CONFIG_FILE_PATH, "r");
87 tet_printf("Default configuration is used\n");
90 while (fgets(buf, sizeof(buf), fp)) {
91 if (buf[0] == '#' || buf[0] == '\n')
94 token = strrchr(buf, '\n');
96 tet_printf("g_conf is too long\n");
101 token = strtok(buf, "=");
105 if (strcasecmp(token, "BT_ADDR_LE") == 0) {
106 token = strtok(NULL, "=");
107 remote_address = strdup(token);
114 static void startup(void)
116 bt_error_e ret = BT_ERROR_NONE;
119 if(get_value_from_file() == -1) {
120 tet_printf("Failed to read.");
124 tet_printf("TC start.");
125 mainloop = g_main_loop_new(NULL, FALSE);
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");
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.");
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);
151 tet_printf("DTS may fail because bt_adapter_le_enable failed");
152 bt_adapter_le_unset_state_changed_cb();
154 } else if (ret == BT_ERROR_NOT_ENABLED) {
155 tet_printf("Bluetooth adapter is not enabled.");
157 tet_printf("DTS may fail because bt_adapter_enable failed");
160 if (bt_adapter_unset_state_changed_cb() != BT_ERROR_NONE) {
161 tet_printf("bt_adapter_set_state_changed_cb() failed.");
164 tet_printf("TC start");
167 static void cleanup(void)
171 tet_printf("TC end.");
174 gboolean timeout_func(gpointer data)
176 tet_printf("[%s] Callback: Timeout.", __FUNCTION__);
177 g_main_loop_quit((GMainLoop *) data);
183 void adapter_state_changed_cb_for_gatt_p(int result, bt_adapter_state_e adapter_state, void* user_data)
187 void adapter_le_state_changed_cb_for_gatt_p(int result, bt_adapter_le_state_e adapter_le__state, void* user_data)
192 bool primary_service_cb_for_gatt_p(bt_gatt_attribute_h service, void *user_data)
197 bool characteristics_discovered_cb_for_gatt_p(int result, int index, int total, bt_gatt_attribute_h characteristic,
203 bool included_service_cb_for_gatt_p(bt_gatt_attribute_h service, void *user_data)
208 void characteristics_changed_cb_for_gatt_p(bt_gatt_attribute_h characteristic, unsigned char *value, int value_length,
214 void gatt_connection_state_changed_cb(int result, bool connected,
215 const char *remote_address, void *user_data)
217 tet_printf("bt_gatt_connection_state_changed_cb : %d", result);
219 tet_printf("LE Connected");
221 tet_printf("LE Disconnected");
225 * @brief Positive test case of bt_gatt_foreach_primary_services()
227 static void utc_network_bluetooth_gatt_foreach_primary_services_p(void)
229 int ret = BT_ERROR_NONE;
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.");
238 * @brief Positive test case of bt_gatt_discover_characteristics()
240 static void utc_network_bluetooth_gatt_discover_characteristics_p(void)
242 int ret = BT_ERROR_NONE;
243 bt_gatt_attribute_h gatt_serv;
244 gatt_serv = g_malloc0(sizeof(bt_gatt_attribute_h));
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.");
252 * @brief Positive test case of bt_gatt_get_service_uuid()
254 static void utc_network_bluetooth_gatt_get_service_uuid_p(void)
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));
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.");
267 * @brief Positive test case of bt_gatt_foreach_included_services()
269 static void utc_network_bluetooth_gatt_foreach_included_services_p(void)
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.");
281 * @brief Positive test case of bt_gatt_set_characteristic_changed_cb()
283 static void utc_network_bluetooth_gatt_set_characteristic_changed_cb_p(void)
285 int ret = BT_ERROR_NONE;
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.");
294 * @brief Positive test case of bt_gatt_unset_characteristic_changed_cb()
296 static void utc_network_bluetooth_gatt_unset_characteristic_changed_cb_p(void)
298 int ret = BT_ERROR_NONE;
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.");
308 * @brief Positive test case of bt_gatt_get_characteristic_declaration()
310 static void utc_network_bluetooth_gatt_get_characteristic_declaration_p(void)
312 int ret = BT_ERROR_NONE;
313 char **uid = "dts_uid";
314 unsigned char **val = "dts_value";
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.");
325 * @brief Positive test case of bt_gatt_set_characteristic_value()
327 static void utc_network_bluetooth_gatt_set_characteristic_value_p(void)
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.");
340 * @brief Positive test case of bt_gatt_clone_attribute_handle()
342 static void utc_network_bluetooth_gatt_clone_attribute_handle_p(void)
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));
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.");
357 * @brief Positive test case of bt_gatt_destroy_attribute_handle()
359 static void utc_network_bluetooth_gatt_destroy_attribute_handle_p(void)
361 int ret = BT_ERROR_NONE;
362 bt_gatt_attribute_h gatt_handle;
363 gatt_handle = g_malloc0(sizeof(bt_gatt_attribute_h));
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.");
370 static void utc_network_bluetooth_gatt_connect_p(void)
372 int ret = BT_ERROR_NONE;
374 ret = bt_gatt_connect(remote_address, false);
375 dts_check_eq("bt_gatt_connect", ret, BT_ERROR_NONE,
376 "bt_gatt_connect() failed.");
380 static void utc_network_bluetooth_gatt_disconnect_p(void)
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.");
389 static void utc_network_bluetooth_set_gatt_connection_state_changed_cb_p(void)
391 int ret = BT_ERROR_NONE;
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.");
398 static void utc_network_bluetooth_unset_gatt_connection_state_changed_cb_p(void)
400 int ret = BT_ERROR_NONE;
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.");