1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdbool.h>
#include <unistd.h>
#include <sys/socket.h>
#include <glib.h>
#include "lib/bluetooth.h"
#include "lib/mgmt.h"
#include "src/shared/mgmt.h"
struct context {
GMainLoop *main_loop;
struct mgmt *mgmt_client;
guint server_source;
GList *handler_list;
};
enum action {
ACTION_PASSED,
ACTION_IGNORE,
};
struct handler {
const void *cmd_data;
uint16_t cmd_size;
bool match_prefix;
enum action action;
};
static void mgmt_debug(const char *str, void *user_data)
{
const char *prefix = user_data;
g_print("%s%s\n", prefix, str);
}
static void context_quit(struct context *context)
{
g_main_loop_quit(context->main_loop);
}
static void check_actions(struct context *context,
const void *data, uint16_t size)
{
GList *list;
for (list = g_list_first(context->handler_list); list;
list = g_list_next(list)) {
struct handler *handler = list->data;
if (handler->match_prefix) {
if (size < handler->cmd_size)
continue;
} else {
if (size != handler->cmd_size)
continue;
}
if (memcmp(data, handler->cmd_data, handler->cmd_size))
continue;
switch (handler->action) {
case ACTION_PASSED:
context_quit(context);
return;
case ACTION_IGNORE:
return;
}
}
g_test_message("Command not handled\n");
g_assert_not_reached();
}
static gboolean server_handler(GIOChannel *channel, GIOCondition cond,
gpointer user_data)
{
struct context *context = user_data;
unsigned char buf[512];
ssize_t result;
int fd;
if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
return FALSE;
fd = g_io_channel_unix_get_fd(channel);
result = read(fd, buf, sizeof(buf));
if (result < 0)
return FALSE;
check_actions(context, buf, result);
return TRUE;
}
static struct context *create_context(void)
{
struct context *context = g_new0(struct context, 1);
GIOChannel *channel;
int err, sv[2];
context->main_loop = g_main_loop_new(NULL, FALSE);
g_assert(context->main_loop);
err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
g_assert(err == 0);
channel = g_io_channel_unix_new(sv[0]);
g_io_channel_set_close_on_unref(channel, TRUE);
g_io_channel_set_encoding(channel, NULL, NULL);
g_io_channel_set_buffered(channel, FALSE);
context->server_source = g_io_add_watch(channel,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
server_handler, context);
g_assert(context->server_source > 0);
g_io_channel_unref(channel);
context->mgmt_client = mgmt_new(sv[1]);
g_assert(context->mgmt_client);
if (g_test_verbose() == TRUE)
mgmt_set_debug(context->mgmt_client,
mgmt_debug, "mgmt: ", NULL);
mgmt_set_close_on_unref(context->mgmt_client, true);
return context;
}
static void execute_context(struct context *context)
{
g_main_loop_run(context->main_loop);
g_list_free_full(context->handler_list, g_free);
g_source_remove(context->server_source);
mgmt_unref(context->mgmt_client);
g_main_loop_unref(context->main_loop);
g_free(context);
}
static void add_action(struct context *context,
const void *cmd_data, uint16_t cmd_size,
bool match_prefix, enum action action)
{
struct handler *handler = g_new0(struct handler, 1);
handler->cmd_data = cmd_data;
handler->cmd_size = cmd_size;
handler->match_prefix = match_prefix;
handler->action = action;
context->handler_list = g_list_append(context->handler_list, handler);
}
struct command_test_data {
uint16_t opcode;
uint16_t index;
uint16_t length;
const void *param;
const void *cmd_data;
uint16_t cmd_size;
};
static const unsigned char read_version_command[] =
{ 0x01, 0x00, 0xff, 0xff, 0x00, 0x00 };
static const struct command_test_data command_test_1 = {
.opcode = MGMT_OP_READ_VERSION,
.index = MGMT_INDEX_NONE,
.cmd_data = read_version_command,
.cmd_size = sizeof(read_version_command),
};
static const unsigned char read_info_command[] =
{ 0x04, 0x00, 0x00, 0x02, 0x00, 0x00 };
static const struct command_test_data command_test_2 = {
.opcode = MGMT_OP_READ_INFO,
.index = 512,
.cmd_data = read_info_command,
.cmd_size = sizeof(read_info_command),
};
static void test_command(gconstpointer data)
{
const struct command_test_data *test = data;
struct context *context = create_context();
add_action(context, test->cmd_data, test->cmd_size,
false, ACTION_PASSED);
mgmt_send(context->mgmt_client, test->opcode, test->index,
test->length, test->param,
NULL ,NULL, NULL);
execute_context(context);
}
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_data_func("/mgmt/command/1", &command_test_1, test_command);
g_test_add_data_func("/mgmt/command/2", &command_test_2, test_command);
return g_test_run();
}
|