ofono/unit/test-mux.c

238 lines
5.3 KiB
C
Raw Normal View History

/*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2008-2009 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
2009-09-06 06:35:34 +00:00
#include <unistd.h>
2009-09-08 07:05:56 +00:00
#include <stdlib.h>
2009-09-06 06:35:34 +00:00
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <glib.h>
#include <glib/gprintf.h>
2009-09-08 07:05:56 +00:00
#include "gatmux.h"
2009-09-06 06:35:34 +00:00
static int do_connect(const char *address, unsigned short port)
{
struct sockaddr_in addr;
int sk, err;
sk = socket(PF_INET, SOCK_STREAM, 0);
if (sk < 0)
return sk;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(address);
addr.sin_port = htons(port);
err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
if (err < 0) {
close(sk);
return err;
}
return sk;
}
2009-09-08 07:05:56 +00:00
static GMainLoop *mainloop;
2009-10-09 22:17:03 +00:00
static GAtMux *mux;
2009-09-08 07:05:56 +00:00
static gboolean cleanup_callback(gpointer data)
{
2009-10-09 22:17:03 +00:00
g_at_mux_unref(mux);
2009-09-08 07:05:56 +00:00
g_main_loop_quit(mainloop);
return FALSE;
}
2009-10-12 03:36:23 +00:00
static gboolean chat_cleanup(gpointer data)
{
GAtChat *chat = data;
g_at_chat_shutdown(chat);
g_at_chat_unref(chat);
2009-10-14 18:28:17 +00:00
return FALSE;
2009-10-12 03:36:23 +00:00
}
2009-09-08 07:05:56 +00:00
static void chat_callback(gboolean ok, GAtResult *result, gpointer user_data)
{
GAtResultIter iter;
g_at_result_iter_init(&iter, result);
g_print("chat: callback [ok %d]\n", ok);
g_print("%s\n", g_at_result_final_response(result));
2009-10-12 03:36:23 +00:00
g_idle_add(chat_cleanup, user_data);
2009-09-08 07:05:56 +00:00
}
static void mux_debug(const char *str, void *data)
{
g_print("%s: %s\n", (char *) data, str);
}
2009-10-12 03:36:23 +00:00
static void mux_setup(GAtMux *m, gpointer data)
2009-09-08 07:05:56 +00:00
{
2009-10-09 22:17:03 +00:00
GAtChat *chat = data;
GIOChannel *io;
2009-09-08 07:05:56 +00:00
GAtSyntax *syntax;
2009-10-12 03:36:23 +00:00
mux = m;
2009-10-09 22:17:03 +00:00
g_print("mux_setup: %p\n", mux);
2009-09-08 07:05:56 +00:00
2009-10-09 22:17:03 +00:00
if (mux == NULL) {
g_at_chat_unref(chat);
2009-09-08 07:05:56 +00:00
g_main_loop_quit(mainloop);
2009-10-09 22:17:03 +00:00
return;
2009-09-08 07:05:56 +00:00
}
2009-10-09 22:17:03 +00:00
g_at_mux_start(mux);
2009-09-08 07:05:56 +00:00
2009-10-09 22:17:03 +00:00
io = g_at_mux_create_channel(mux);
syntax = g_at_syntax_new_gsm_permissive();
chat = g_at_chat_new(io, syntax);
g_at_syntax_unref(syntax);
g_io_channel_unref(io);
2009-09-08 07:05:56 +00:00
2009-10-12 03:36:23 +00:00
g_at_chat_set_debug(chat, mux_debug, "CHAT1");
g_at_chat_set_wakeup_command(chat, "\r", 1000, 5000);
g_at_chat_send(chat, "AT+CGMI", NULL, NULL, NULL, NULL);
g_at_chat_send(chat, "AT+CGMR", NULL, chat_callback, chat, NULL);
io = g_at_mux_create_channel(mux);
syntax = g_at_syntax_new_gsm_permissive();
chat = g_at_chat_new(io, syntax);
g_at_syntax_unref(syntax);
g_io_channel_unref(io);
g_at_chat_set_debug(chat, mux_debug, "CHAT2");
g_at_chat_set_wakeup_command(chat, "\r", 1000, 5000);
g_at_chat_send(chat, "AT+CGMI", NULL, NULL, NULL, NULL);
g_at_chat_send(chat, "AT+CGMR", NULL, chat_callback, chat, NULL);
io = g_at_mux_create_channel(mux);
syntax = g_at_syntax_new_gsm_permissive();
chat = g_at_chat_new(io, syntax);
g_at_syntax_unref(syntax);
g_io_channel_unref(io);
g_at_chat_set_debug(chat, mux_debug, "CHAT3");
2009-10-09 22:17:03 +00:00
g_at_chat_set_wakeup_command(chat, "\r", 1000, 5000);
g_at_chat_send(chat, "AT+CGMI", NULL, NULL, NULL, NULL);
2009-09-08 07:05:56 +00:00
g_at_chat_send(chat, "AT+CGMR", NULL, chat_callback, chat, NULL);
2009-10-12 03:36:23 +00:00
io = g_at_mux_create_channel(mux);
syntax = g_at_syntax_new_gsm_permissive();
chat = g_at_chat_new(io, syntax);
g_at_syntax_unref(syntax);
g_io_channel_unref(io);
g_at_chat_set_debug(chat, mux_debug, "CHAT4");
g_at_chat_set_wakeup_command(chat, "\r", 1000, 5000);
g_at_chat_send(chat, "AT+CGMI", NULL, NULL, NULL, NULL);
g_at_chat_send(chat, "AT+CGMR", NULL, chat_callback, chat, NULL);
g_timeout_add_seconds(7, cleanup_callback, NULL);
2009-10-09 22:17:03 +00:00
}
2009-09-08 07:05:56 +00:00
2009-10-09 22:17:03 +00:00
static void mux_init(gboolean ok, GAtResult *result, gpointer data)
{
GAtChat *chat = data;
g_print("mux_init: %d\n", ok);
if (ok == FALSE) {
g_at_chat_unref(chat);
g_main_loop_quit(mainloop);
return;
}
g_at_mux_setup_gsm0710(chat, mux_setup, chat, NULL);
2009-09-08 07:05:56 +00:00
}
static void test_mux(void)
{
2009-09-08 07:05:56 +00:00
GIOChannel *io;
2009-10-09 22:17:03 +00:00
GAtChat *chat;
GAtSyntax *syntax;
2009-09-08 07:05:56 +00:00
int sk;
2009-09-08 07:05:56 +00:00
sk= do_connect("192.168.0.202", 2000);
if (sk < 0) {
g_printerr("connect failed\n");
return;
}
2009-10-09 22:17:03 +00:00
mux = NULL;
2009-09-08 07:05:56 +00:00
io = g_io_channel_unix_new(sk);
2009-10-09 22:17:03 +00:00
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);
2009-09-08 07:05:56 +00:00
g_io_channel_unref(io);
2009-10-09 22:17:03 +00:00
if (!chat) {
g_printerr("Chat creation failed\n");
2009-09-06 06:35:34 +00:00
return;
2009-09-08 07:05:56 +00:00
}
2009-09-06 06:35:34 +00:00
2009-10-09 22:17:03 +00:00
g_at_chat_set_debug(chat, mux_debug, "MUX");
g_at_chat_set_wakeup_command(chat, "\r", 1000, 5000);
g_at_chat_send(chat, "ATE0", NULL, mux_init, chat, NULL);
2009-09-06 06:35:34 +00:00
2009-09-08 07:05:56 +00:00
mainloop = g_main_loop_new(NULL, FALSE);
2009-09-06 06:35:34 +00:00
2009-09-08 07:05:56 +00:00
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_mux();
exit(0);
}
2009-09-08 07:05:56 +00:00
g_test_trap_assert_passed();
//g_test_trap_assert_stderr("failed");
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
2009-09-08 07:05:56 +00:00
g_test_add_func("/testmux/basic", test_basic);
return g_test_run();
}