bluetooth_test/artik_bluetooth_test_spp.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>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#include <gio/gio.h>
#pragma GCC diagnostic pop
#include <stdbool.h>
#include <errno.h>
#include <signal.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <artik_module.h>
#include <artik_loop.h>
#include <artik_bluetooth.h>
#define MAX_PACKET_SIZE 1024
#define DISCOVERABLE_TIMEOUT 120
static char buffer[MAX_PACKET_SIZE];
static int watch_id;
static int spp_fd;
static int uninit(void *user_data)
{
fprintf(stdout, "SPP loop quit\n");
if (watch_id) {
close(spp_fd);
loop->remove_fd_watch(watch_id);
}
loop->quit();
return true;
}
static void ask(char *prompt)
{
printf("%s\n", prompt);
if (fgets(buffer, MAX_PACKET_SIZE, stdin) == NULL)
fprintf(stdout, "\ncmd fgets error\n");
}
{
uint8_t buffer[MAX_PACKET_SIZE];
int num_bytes = 0;
num_bytes = recv(fd, buffer, MAX_PACKET_SIZE, 0);
if (num_bytes == -1) {
printf("Error in recvfrom()\n");
} else {
printf("<SPP>: Received %d bytes\n", num_bytes);
buffer[num_bytes] = '\0';
printf("%s\n", buffer);
if (send(fd, "Hello\n", 6, 0) != 6)
printf("Failed to send data\n");
}
printf("Socket error occured.\n");
close(fd);
watch_id = 0;
return 0;
}
return 1;
}
void *data, void *user_data)
{
fprintf(stdout, "<SPP>: %s\n", __func__);
artik_bt_spp_connect_property *spp_property =
(artik_bt_spp_connect_property *)data;
loop->add_fd_watch(spp_fd,
on_socket, NULL, &watch_id);
}
void *data, void *user_data)
{
fprintf(stdout, "<SPP>: %s\n", __func__);
}
void *data, void *user_data)
{
fprintf(stdout, "<SPP>: %s\n", __func__);
}
void callback_on_agent_request_pincode(artik_bt_event event,
void *data, void *user_data)
{
artik_bt_agent_request_property *request_property =
(artik_bt_agent_request_property *)data;
artik_bluetooth_module *bt = (artik_bluetooth_module *)
fprintf(stdout, "<AGENT>: Request pincode (%s)\n",
ask("Enter PIN Code: ");
}
void callback_on_agent_request_passkey(artik_bt_event event,
void *data, void *user_data)
{
unsigned long passkey;
artik_bt_agent_request_property *request_property =
(artik_bt_agent_request_property *)data;
artik_bluetooth_module *bt = (artik_bluetooth_module *)
artik_request_api_module("bluetooth");
fprintf(stdout, "<AGENT>: Request passkey (%s)\n",
request_property->device);
ask("Enter passkey (1~999999): ");
passkey = strtoul(buffer, NULL, 10);
if ((passkey > 0) && (passkey < 999999))
else
fprintf(stdout, "<AGENT>: get passkey error\n");
}
void callback_on_agent_confirmation(artik_bt_event event,
void *data, void *user_data)
{
artik_bt_agent_confirmation_property *confirmation_property =
(artik_bt_agent_confirmation_property *)data;
artik_bluetooth_module *bt = (artik_bluetooth_module *)
artik_request_api_module("bluetooth");
fprintf(stdout, "<AGENT>: Request confirmation (%s)\nPasskey: %06u\n",
ask("Confirm passkey? (yes/no): ");
if (!strncmp(buffer, "yes", 3))
else
bt->agent_send_error(confirmation_property->handle,
}
void callback_on_agent_authorization(artik_bt_event event,
void *data, void *user_data)
{
artik_bt_agent_request_property *request_property =
(artik_bt_agent_request_property *)data;
artik_bluetooth_module *bt = (artik_bluetooth_module *)
artik_request_api_module("bluetooth");
fprintf(stdout, "<AGENT>: Request authorization (%s)\n",
request_property->device);
ask("Authorize? (yes/no): ");
if (!strncmp(buffer, "yes", 3))
bt->agent_send_empty_response(request_property->handle);
else
bt->agent_send_error(request_property->handle,
}
void callback_on_agent_authorize_service(artik_bt_event event,
void *data, void *user_data)
{
artik_bt_agent_authorize_property *authorize_property =
(artik_bt_agent_authorize_property *)data;
artik_bluetooth_module *bt = (artik_bluetooth_module *)
artik_request_api_module("bluetooth");
fprintf(stdout, "<AGENT>: Authorize Service (%s, %s)\n",
ask("Authorize connection? (yes/no): ");
if (!strncmp(buffer, "yes", 3))
bt->agent_send_empty_response(authorize_property->handle);
else
bt->agent_send_error(authorize_property->handle,
}
{
artik_bt_agent_capability g_capa = BT_CAPA_KEYBOARDDISPLAY;
bt->set_discoverableTimeout(DISCOVERABLE_TIMEOUT);
if (ret != S_OK)
return ret;
ret = bt->agent_register_capability(g_capa);
if (ret != S_OK)
return ret;
ret = bt->agent_set_default();
return ret;
}
{
artik_error ret = S_OK;
artik_bt_callback_property callback_property[] = {
{BT_EVENT_SPP_CONNECT, callback_on_spp_connect, NULL},
{BT_EVENT_SPP_RELEASE, callback_on_spp_release, NULL},
{BT_EVENT_SPP_DISCONNECT, callback_on_spp_disconnect, NULL},
{BT_EVENT_AGENT_REQUEST_PINCODE, callback_on_agent_request_pincode,
NULL},
{BT_EVENT_AGENT_REQUEST_PASSKEY, callback_on_agent_request_passkey,
NULL},
{BT_EVENT_AGENT_CONFIRM, callback_on_agent_confirmation, NULL},
{BT_EVENT_AGENT_AUTHORIZE, callback_on_agent_authorization, NULL},
{BT_EVENT_AGENT_AUTHORIZE_SERVICE, callback_on_agent_authorize_service,
NULL}
};
ret = bt->set_callbacks(callback_property, 8);
return ret;
}
{
artik_error ret = S_OK;
artik_bt_spp_profile_option profile_option;
profile_option.channel = 22;
profile_option.PSM = 3;
profile_option.version = 10;
profile_option.features = 20;
ret = bt->spp_register_profile(&profile_option);
return ret;
}
int main(int argc, char *argv[])
{
artik_error ret = S_OK;
printf("<SPP>: Bluetooth module is not available\n");
return -1;
}
if (!artik_is_module_available(ARTIK_MODULE_LOOP)) {
printf("<SPP>: Loop module is not available\n");
return -1;
}
if (!bt || !loop)
goto loop_quit;
bt->init();
ret = spp_profile_register();
if (ret != S_OK) {
fprintf(stdout, "<SPP>: SPP register error!\n");
goto spp_quit;
}
fprintf(stdout, "<SPP>: SPP register profile success!\n");
ret = set_callback();
if (ret != S_OK) {
fprintf(stdout, "<SPP>: SPP set callback error!\n");
goto spp_quit;
}
fprintf(stdout, "<SPP>: SPP set callback success!\n");
ret = agent_register();
if (ret != S_OK) {
fprintf(stdout, "<SPP>: SPP register agent error!\n");
goto spp_quit;
}
fprintf(stdout, "<SPP>: SPP register agent success!\n");
loop->add_signal_watch(SIGINT, uninit, NULL, NULL);
loop->run();
spp_quit:
ret = bt->spp_unregister_profile();
if (ret != S_OK)
fprintf(stdout, "<SPP>: Unregister SPP profile error!\n");
ret = bt->agent_unregister();
if (ret != S_OK)
fprintf(stdout, "<SPP>: Unregister agent error!\n");
bt->deinit();
loop_quit:
if (bt)
if (loop)
artik_release_api_module(loop);
fprintf(stdout, "<SPP>: SPP profile quit!\n");
return S_OK;
}
Last updated on: