5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <connman/log.h>
49 struct modem_callback {
62 static int send_command(struct modem_data *modem, struct modem_cmd *cmd)
67 if (cmd->arg == NULL) {
68 DBG("AT%s", cmd->cmd);
69 buf = g_strdup_printf("AT%s\r\n", cmd->cmd);
71 DBG("AT%s=%s", cmd->cmd, cmd->arg);
72 buf = g_strdup_printf("AT%s=%s\r\n", cmd->cmd, cmd->arg);
75 fd = g_io_channel_unix_get_fd(modem->channel);
76 err = write(fd, buf, strlen(buf));
85 static int queue_command(struct modem_data *modem, struct modem_cmd *cmd)
87 modem->commands = g_slist_append(modem->commands, cmd);
89 if (g_slist_length(modem->commands) > 1)
92 return send_command(modem, cmd);
95 struct modem_data *modem_create(const char *device)
97 struct modem_data *modem;
99 DBG("device %s", device);
101 modem = g_try_new0(struct modem_data, 1);
105 modem->device = g_strdup(device);
107 DBG("modem %p", modem);
112 void modem_destroy(struct modem_data *modem)
114 DBG("modem %p", modem);
119 g_free(modem->device);
123 static gboolean modem_event(GIOChannel *channel,
124 GIOCondition condition, gpointer user_data)
126 struct modem_data *modem = user_data;
127 struct modem_cmd *cmd;
132 if (condition & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
135 err = g_io_channel_read(channel, modem->buf + modem->offset,
136 sizeof(modem->buf) - modem->offset, &len);
138 if (err == G_IO_ERROR_AGAIN)
143 DBG("Read %d bytes (offset %d)", len, modem->offset);
145 if (g_str_has_suffix(modem->buf, "\r\n") == TRUE) {
146 for (list = modem->callbacks; list; list = list->next) {
147 struct modem_callback *callback = list->data;
149 if (callback->function == NULL)
152 if (g_strrstr(modem->buf, callback->command) != NULL)
153 callback->function(modem->buf,
154 callback->user_data);
158 if (g_strrstr(modem->buf, "\r\nERROR\r\n") == NULL &&
159 g_strrstr(modem->buf, "\r\nOK\r\n") == NULL) {
160 modem->offset += len;
164 memset(modem->buf, 0, sizeof(modem->buf));
167 cmd = g_slist_nth_data(modem->commands, 0);
171 modem->commands = g_slist_remove(modem->commands, cmd);
173 DBG("AT%s", cmd->cmd);
176 cmd->callback(modem->buf, cmd->user_data);
182 cmd = g_slist_nth_data(modem->commands, 0);
186 send_command(modem, cmd);
191 static int open_device(const char *device)
196 fd = open(device, O_RDWR | O_NOCTTY);
200 tcflush(fd, TCIOFLUSH);
202 /* Switch TTY to raw mode */
204 tcsetattr(fd, TCSANOW, &ti);
209 int modem_open(struct modem_data *modem)
213 DBG("modem %p", modem);
219 fd = open_device(modem->device);
228 connman_error("Can't open %s device", modem->device);
232 modem->channel = g_io_channel_unix_new(fd);
233 g_io_channel_set_close_on_unref(modem->channel, TRUE);
235 g_io_add_watch(modem->channel,
236 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
242 int modem_close(struct modem_data *modem)
244 DBG("modem %p", modem);
249 g_io_channel_shutdown(modem->channel, TRUE, NULL);
250 g_io_channel_unref(modem->channel);
252 modem->channel = NULL;
257 int modem_add_callback(struct modem_data *modem, const char *command,
258 modem_cb_t function, void *user_data)
260 struct modem_callback *callback;
262 callback = g_try_new0(struct modem_callback, 1);
263 if (callback == NULL)
266 callback->command = g_strdup(command);
267 callback->function = function;
268 callback->user_data = user_data;
270 modem->callbacks = g_slist_append(modem->callbacks, callback);
275 static int modem_command_valist(struct modem_data *modem, modem_cb_t callback,
276 void *user_data, const char *command,
277 const char *format, va_list var_args)
279 struct modem_cmd *cmd;
281 cmd = g_try_new0(struct modem_cmd, 1);
285 cmd->cmd = g_strdup(command);
287 cmd->arg = g_strdup_vprintf(format, var_args);
289 cmd->callback = callback;
290 cmd->user_data = user_data;
292 return queue_command(modem, cmd);
295 int modem_command(struct modem_data *modem,
296 modem_cb_t callback, void *user_data,
297 const char *command, const char *format, ...)
302 DBG("modem %p", modem);
307 va_start(args, format);
308 err = modem_command_valist(modem, callback, user_data,
309 command, format, args);