diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2010-01-28 11:25:54 +0100 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2010-01-28 11:25:54 +0100 |
commit | 648eded47e8674ece5dad76b255c539004864069 (patch) | |
tree | 7267677041a5bf433a3b8801738f8c6f1ee221d5 /unit/test-caif.c | |
parent | 392f092ee04dd7d13391fb89daaa949b8e566835 (diff) | |
download | ofono-648eded47e8674ece5dad76b255c539004864069.tar.bz2 |
Add simple unit test for CAIF sockets
Diffstat (limited to 'unit/test-caif.c')
-rw-r--r-- | unit/test-caif.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/unit/test-caif.c b/unit/test-caif.c new file mode 100644 index 00000000..fd936438 --- /dev/null +++ b/unit/test-caif.c @@ -0,0 +1,140 @@ +/* + * + * oFono - Open Source Telephony + * + * Copyright (C) 2008-2010 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 version 2 as + * published by the Free Software Foundation. + * + * 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 <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> + +#include <glib.h> +#include <glib/gprintf.h> + +#include <gatchat.h> + +#include <drivers/stemodem/caif_socket.h> +#include <drivers/stemodem/if_caif.h> + +static GMainLoop *mainloop; + +static int do_connect(void) +{ + struct sockaddr_caif addr; + int sk, err; + + /* Create a CAIF socket for AT Service */ + sk = socket(AF_CAIF, SOCK_SEQPACKET, CAIFPROTO_AT); + if (sk < 0) { + g_printerr("CAIF socket creation failed (%d)\n", errno); + return -EIO; + } + memset(&addr, 0, sizeof(addr)); + addr.family = AF_CAIF; + addr.u.at.type = CAIF_ATTYPE_PLAIN; + + /* Connect to the AT Service at the modem */ + err = connect(sk, (struct sockaddr *) &addr, sizeof(addr)); + if (err < 0) { + g_printerr("CAIF socket connect failed (%d)\n", errno); + close(sk); + return err; + } + + return sk; +} + +static void caif_debug(const char *str, void *data) +{ + g_print("%s\n", str); +} + +static void caif_init(gboolean ok, GAtResult *result, gpointer data) +{ + GAtChat *chat = data; + + g_print("caif_init: %d\n", ok); + + if (ok == FALSE) { + g_at_chat_unref(chat); + g_main_loop_quit(mainloop); + return; + } + + g_at_chat_unref(chat); + g_main_loop_quit(mainloop); +} + +static void test_connect(void) +{ + GIOChannel *io; + GAtChat *chat; + GAtSyntax *syntax; + int sk; + + sk = do_connect(); + if (sk < 0) + return; + + io = g_io_channel_unix_new(sk); + g_io_channel_set_close_on_unref(io, TRUE); + + syntax = g_at_syntax_new_gsm_permissive(); + chat = g_at_chat_new(io, syntax); + g_at_syntax_unref(syntax); + + g_io_channel_unref(io); + + if (!chat) { + g_printerr("Chat creation failed\n"); + return; + } + + g_at_chat_set_debug(chat, caif_debug, NULL); + g_at_chat_send(chat, "ATE0 +CMEE=1", NULL, caif_init, chat, NULL); + + mainloop = g_main_loop_new(NULL, FALSE); + + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); +} + +static void test_basic(void) +{ + if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) { + test_connect(); + exit(0); + } + + g_test_trap_assert_passed(); + //g_test_trap_assert_stderr("failed"); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/testcaif/basic", test_basic); + + return g_test_run(); +} |