3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/socket.h>
34 #include "lib/bluetooth.h"
37 #include "src/shared/mgmt.h"
41 struct mgmt *mgmt_client;
58 static void mgmt_debug(const char *str, void *user_data)
60 const char *prefix = user_data;
62 g_print("%s%s\n", prefix, str);
65 static void context_quit(struct context *context)
67 g_main_loop_quit(context->main_loop);
70 static void check_actions(struct context *context,
71 const void *data, uint16_t size)
75 for (list = g_list_first(context->handler_list); list;
76 list = g_list_next(list)) {
77 struct handler *handler = list->data;
79 if (handler->match_prefix) {
80 if (size < handler->cmd_size)
83 if (size != handler->cmd_size)
87 if (memcmp(data, handler->cmd_data, handler->cmd_size))
90 switch (handler->action) {
92 context_quit(context);
99 g_test_message("Command not handled\n");
100 g_assert_not_reached();
103 static gboolean server_handler(GIOChannel *channel, GIOCondition cond,
106 struct context *context = user_data;
107 unsigned char buf[512];
111 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
114 fd = g_io_channel_unix_get_fd(channel);
116 result = read(fd, buf, sizeof(buf));
120 check_actions(context, buf, result);
125 static struct context *create_context(void)
127 struct context *context = g_new0(struct context, 1);
131 context->main_loop = g_main_loop_new(NULL, FALSE);
132 g_assert(context->main_loop);
134 err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
137 channel = g_io_channel_unix_new(sv[0]);
139 g_io_channel_set_close_on_unref(channel, TRUE);
140 g_io_channel_set_encoding(channel, NULL, NULL);
141 g_io_channel_set_buffered(channel, FALSE);
143 context->server_source = g_io_add_watch(channel,
144 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
145 server_handler, context);
146 g_assert(context->server_source > 0);
148 g_io_channel_unref(channel);
150 context->mgmt_client = mgmt_new(sv[1]);
151 g_assert(context->mgmt_client);
153 if (g_test_verbose() == TRUE)
154 mgmt_set_debug(context->mgmt_client,
155 mgmt_debug, "mgmt: ", NULL);
157 mgmt_set_close_on_unref(context->mgmt_client, true);
162 static void execute_context(struct context *context)
164 g_main_loop_run(context->main_loop);
166 g_list_free_full(context->handler_list, g_free);
168 g_source_remove(context->server_source);
170 mgmt_unref(context->mgmt_client);
172 g_main_loop_unref(context->main_loop);
177 static void add_action(struct context *context,
178 const void *cmd_data, uint16_t cmd_size,
179 bool match_prefix, enum action action)
181 struct handler *handler = g_new0(struct handler, 1);
183 handler->cmd_data = cmd_data;
184 handler->cmd_size = cmd_size;
185 handler->match_prefix = match_prefix;
186 handler->action = action;
188 context->handler_list = g_list_append(context->handler_list, handler);
191 struct command_test_data {
196 const void *cmd_data;
200 static const unsigned char read_version_command[] =
201 { 0x01, 0x00, 0xff, 0xff, 0x00, 0x00 };
203 static const struct command_test_data command_test_1 = {
204 .opcode = MGMT_OP_READ_VERSION,
205 .index = MGMT_INDEX_NONE,
206 .cmd_data = read_version_command,
207 .cmd_size = sizeof(read_version_command),
210 static const unsigned char read_info_command[] =
211 { 0x04, 0x00, 0x00, 0x02, 0x00, 0x00 };
213 static const struct command_test_data command_test_2 = {
214 .opcode = MGMT_OP_READ_INFO,
216 .cmd_data = read_info_command,
217 .cmd_size = sizeof(read_info_command),
220 static void test_command(gconstpointer data)
222 const struct command_test_data *test = data;
223 struct context *context = create_context();
225 add_action(context, test->cmd_data, test->cmd_size,
226 false, ACTION_PASSED);
228 mgmt_send(context->mgmt_client, test->opcode, test->index,
229 test->length, test->param,
232 execute_context(context);
235 int main(int argc, char *argv[])
237 g_test_init(&argc, &argv, NULL);
239 g_test_add_data_func("/mgmt/command/1", &command_test_1, test_command);
240 g_test_add_data_func("/mgmt/command/2", &command_test_2, test_command);