bluetooth_test/artik_bluetooth_test.c
/*
*
* Copyright 2017 Samsung Electronics All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <artik_module.h>
#include <artik_loop.h>
#include <artik_bluetooth.h>
#define MAX_BDADDR_LEN 17
{
int i = 0, j = 0;
for (i = 0; i < num; i++) {
fprintf(stdout, "Address: %s\n",
devices[i].remote_address ? devices[i].remote_address : "(null)");
fprintf(stdout, "Name: %s\n",
devices[i].remote_name ? devices[i].remote_name : "(null)");
fprintf(stdout, "RSSI: %d\n", devices[i].rssi);
fprintf(stdout, "Bonded: %s\n",
devices[i].is_bonded ? "true" : "false");
fprintf(stdout, "Connected: %s\n",
devices[i].is_connected ? "true" : "false");
fprintf(stdout, "Authorized: %s\n",
devices[i].is_authorized ? "true" : "false");
fprintf(stdout, "Class:\n");
fprintf(stdout, "\tMajor:0x%02x\n", devices[i].cod.major);
fprintf(stdout, "\tMinor:0x%02x\n", devices[i].cod.minor);
fprintf(stdout, "\tService:0x%04x\n",
devices[i].cod.service_class);
if (devices[i].uuid_length > 0) {
fprintf(stdout, "UUIDs:\n");
fprintf(stdout, "\t%s [%s]\n",
devices[i].uuid_list[j].uuid_name,
devices[i].uuid_list[j].uuid);
}
}
fprintf(stdout, "\n");
}
}
{
artik_bt_device *dev = (artik_bt_device *) data;
print_devices(dev, 1);
}
static void on_timeout_callback(void *user_data)
{
fprintf(stdout, "TEST: %s stop scanning, exiting loop\n", __func__);
loop->quit();
}
artik_error test_bluetooth_scan(void)
{
int timeout_id = 0;
fprintf(stdout, "TEST: %s starting\n", __func__);
ret = bt->set_callback(BT_EVENT_SCAN, scan_callback, NULL);
if (ret != S_OK)
goto exit;
ret = bt->start_scan();
if (ret != S_OK)
goto exit;
loop->add_timeout_callback(&timeout_id, 10000, on_timeout_callback,
(void *)loop);
loop->run();
exit:
bt->stop_scan();
bt->unset_callback(BT_EVENT_SCAN);
fprintf(stdout, "TEST: %s %s\n", __func__,
artik_release_api_module(loop);
return ret;
}
{
artik_error ret = S_OK;
artik_bt_device *devices = NULL;
int num = 0;
fprintf(stdout, "TEST: %s starting\n", __func__);
fprintf(stdout, "TEST: %s - list devices\n", __func__);
ret = bt->get_devices(BT_DEVICE_ALL, &devices, &num);
if (ret != S_OK)
goto exit;
print_devices(devices, num);
bt->free_devices(&devices, num);
devices = NULL;
num = 0;
fprintf(stdout, "TEST: %s - list paired devices\n", __func__);
if (ret != S_OK)
goto exit;
print_devices(devices, num);
bt->free_devices(&devices, num);
devices = NULL;
num = 0;
fprintf(stdout, "TEST: %s - list connected devices\n", __func__);
if (ret != S_OK)
goto exit;
print_devices(devices, num);
bt->free_devices(&devices, num);
devices = NULL;
num = 0;
exit:
fprintf(stdout, "TEST: %s %s\n", __func__,
if (devices && (num > 0))
bt->free_devices(&devices, num);
return ret;
}
static void on_scan(void *data, void *user_data)
{
artik_bt_device *dev = (artik_bt_device *)data;
char *target_address = (char *)user_data;
if (strncasecmp(target_address, dev->remote_address, MAX_BDADDR_LEN) == 0) {
bt->stop_scan();
bt->start_bond(target_address);
}
}
static void on_bond(void *data, void *user_data)
{
char *remote_address = (char *)user_data;
artik_bt_device dev = *(artik_bt_device *)data;
bt->connect(remote_address);
}
static void on_connect(void *data, void *user_data)
{
artik_bt_device dev = *(artik_bt_device *)data;
fprintf(stdout, "%s %s\n", __func__,
}
static void on_proximity(void *data, void *user_data)
{
fprintf(stdout, "%s : property [%s] value [%s]\n", __func__,
}
{
switch (event) {
case BT_EVENT_SCAN:
on_scan(data, user_data);
break;
case BT_EVENT_BOND:
on_bond(data, user_data);
break;
case BT_EVENT_CONNECT:
on_connect(data, user_data);
break;
case BT_EVENT_PROXIMITY:
on_proximity(data, user_data);
break;
default:
break;
}
}
static void on_connect_timeout(void *user_data)
{
fprintf(stdout, "TEST: %s reached timeout\n", __func__);
bt->unset_callback(BT_EVENT_PROXIMITY);
bt->unset_callback(BT_EVENT_SCAN);
bt->unset_callback(BT_EVENT_CONNECT);
loop->quit();
artik_release_api_module(loop);
}
{
artik_error ret = S_OK;
int timeout_id = 0;
fprintf(stdout, "TEST: %s starting\n", __func__);
ret =
bt->set_callback(BT_EVENT_SCAN, user_callback, (void *)target);
if (ret != S_OK)
goto exit;
ret =
bt->set_callback(BT_EVENT_BOND, user_callback, (void *)target);
if (ret != S_OK)
goto exit;
ret =
bt->set_callback(BT_EVENT_CONNECT, user_callback, (void *)target);
if (ret != S_OK)
goto exit;
ret =
bt->set_callback(BT_EVENT_PROXIMITY, user_callback, (void *)target);
if (ret != S_OK)
goto exit;
ret = bt->start_scan();
if (ret != S_OK)
goto exit;
loop->add_timeout_callback(&timeout_id, 60000, on_connect_timeout, (void *)target);
loop->run();
exit:
fprintf(stdout, "TEST: %s %s\n", __func__,
artik_release_api_module(loop);
return ret;
}
{
artik_error ret = S_OK;
artik_bt_device *devices = NULL;
int num = 0;
int i;
fprintf(stdout, "TEST: %s starting\n", __func__);
fprintf(stdout, "TEST: %s - remove unpaired devices\n", __func__);
ret = bt->remove_unpaired_devices();
if (ret != S_OK)
goto exit;
fprintf(stdout, "TEST: %s - remove connected devices\n", __func__);
ret = bt->get_devices(BT_DEVICE_CONNECTED, &devices, &num);
if (ret != S_OK)
goto exit;
for (i = 0; i < num; i++) {
ret = bt->disconnect(devices[i].remote_address);
if (ret != S_OK)
goto exit;
}
bt->free_devices(&devices, num);
devices = NULL;
num = 0;
fprintf(stdout, "TEST: %s - remove paired devices\n", __func__);
ret = bt->get_devices(BT_DEVICE_PARIED, &devices, &num);
if (ret != S_OK)
goto exit;
for (i = 0; i < num; i++) {
ret = bt->remove_device(devices[i].remote_address);
if (ret != S_OK)
goto exit;
}
bt->free_devices(&devices, num);
devices = NULL;
num = 0;
exit:
return ret;
}
int main(int argc, char *argv[])
{
int opt;
artik_error ret = S_OK;
char target_address[MAX_BDADDR_LEN+1] = "";
fprintf(stdout,
"TEST: Bluetooth module is not available, skipping test...\n");
return -1;
}
while ((opt = getopt(argc, argv, "t:")) != -1) {
switch (opt) {
case 't':
strncpy(target_address, optarg, MAX_BDADDR_LEN);
break;
default:
printf("Usage: bluetooth-test -t <target BDADDR to connect to>\r\n");
return 0;
}
}
if (!bt) {
fprintf(stdout,
"TEST: Failed tor request bluetooth module skipping test...\n");
return -1;
}
bt->init();
ret = test_bluetooth_scan();
if (ret != S_OK)
goto exit;
/* Only call this test if a target address was provided */
if (strncmp(target_address, "", MAX_BDADDR_LEN) != 0) {
ret = test_bluetooth_connect(target_address);
if (ret != S_OK)
goto exit;
}
ret = test_bluetooth_devices();
if (ret != S_OK)
goto exit;
ret = test_bluetooth_disconnect_devices();
if (ret != S_OK)
goto exit;
ret = test_bluetooth_devices();
if (ret != S_OK)
goto exit;
exit:
bt->deinit();
return (ret == S_OK) ? 0 : -1;
}
Last updated on: