Re: [PATCH 1/4] Add gatutil.c to share common APIs with GAtServer

2010-01-14 Thread Denis Kenzior
Hi Zhenhua,

> +gboolean g_at_util_set_io(GIOChannel *io)

Rename this g_at_util_setup_io

> +{
> + GIOFlags io_flags;
> +
> + if (g_io_channel_set_encoding(io, NULL, NULL) !=
> + G_IO_STATUS_NORMAL)
> + return FALSE;
> +
> + io_flags = g_io_channel_get_flags(io);
> +
> + io_flags |= G_IO_FLAG_NONBLOCK;
> + io_flags |= G_IO_FLAG_IS_READABLE;
> + io_flags |= G_IO_FLAG_IS_WRITEABLE;

According to GLib docs the READABLE and WRITEABLE flags are not settable.  
Don't see the point in setting them anyway, please get rid of this.

> +
> + if (g_io_channel_set_flags(io, io_flags, NULL) !=
> + G_IO_STATUS_NORMAL)
> + return FALSE;
> +
> + g_io_channel_set_close_on_unref(io, TRUE);
> +
> + return TRUE;
> +}
> +
> diff --git a/gatchat/gatutil.h b/gatchat/gatutil.h
> new file mode 100644
> index 000..63bbfff
> --- /dev/null
> +++ b/gatchat/gatutil.h
> @@ -0,0 +1,46 @@
> +/*
> + *
> + *  AT chat library with GLib integration
> + *
> + *  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 + *
> + */
> +
> +#ifndef __GATUTIL_H
> +#define __GATUTIL_H
> +
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
> +#include "gatresult.h"

Get rid of this include

> +
> +typedef void (*GAtResultFunc)(gboolean success, GAtResult *result,
> + gpointer user_data);
> +typedef void (*GAtNotifyFunc)(GAtResult *result, gpointer user_data);

The GAtNotify and GAtResultFunc should stay in gatchat.h

> +typedef void (*GAtDisconnectFunc)(gpointer user_data);
> +typedef void (*GAtDebugFunc)(const char *str, gpointer user_data);

These don't belong here, create a new file called gat.h and put them there.

Regards,
-Denis
___
ofono mailing list
ofono@ofono.org
http://lists.ofono.org/listinfo/ofono


Re: [PATCH 2/4] Add GAtServer basic parsing support

2010-01-14 Thread Denis Kenzior
Hi Zhenhua,

> +
> +#include 
> +
> +#include "ringbuffer.h"
> +#include "gatresult.h"

Is this include really necessary?

> +#include "gatserver.h"
> +
> +#define DBG(fmt, arg...) g_print("%s:%s() " fmt, __FILE__, __FUNCTION__ ,
>  ## arg) +

Move this to gat.h

> +struct _GAtServer {
> + gint ref_count; /* Ref count */
> + struct v250_settings *v250; /* V.250 command setting */

This one doesn't need to be a pointer.

> + GIOChannel *server_io;  /* Server IO */
> + int server_watch;   /* Watch for server IO */
> + char *modem_path;   /* Emulated modem path */

Get rid of this, there's no purpose for it.

> +static int at_server_parse(GAtServer *server, char *buf)
> +{
> + int res = G_AT_SERVER_RESULT_ERROR;
> + struct v250_settings *v250 = server->v250;
> + gsize i = 0;
> + char c;
> +
> + /* skip space after "AT" or previous command */
> + i = skip_space(buf, i);
> +
> + c = buf[i];
> + /* skip semicolon */
> + if (c == ';')
> + c = buf[++i];
> +
> + if (g_ascii_isalpha(c) || c == '&')
> + res = parse_v250_settings(server, buf + i);

This part makes no sense

> + else if (c == v250->s3)
> + res = G_AT_SERVER_RESULT_OK;
> +
> + return res;
> +}
> +
> +static void parse_buffer(GAtServer *server, unsigned int len)
> +{
> + int res = G_AT_SERVER_RESULT_ERROR;
> + gsize i = 0;
> + char *buf = NULL;
> +
> + g_at_server_ref(server);

Lets get rid of this part for now.  It was a necessary evil inside GAtChat 
since the client can close the channel in the command result callback.  I 
don't think this is relevant for GAtServer.  If it ever becomes relevant we 
can add this back in.

> +
> + buf = g_try_new0(char, len+1);

You're leaking the buf memory in this function.

> +
> + if (!buf)
> + goto out;
> +
> + if (-1 == ring_buffer_read(server->buf, buf, len))
> + goto out;
> +
> + buf[len] = '\0';
> +
> + DBG("received %s\n", buf);
> +
> + /* skip header space */
> + buf += skip_space(buf, i);
> +
> + /* Make sure the command line prefix is "AT" or "at" */
> + if (g_str_has_prefix(buf, "AT") ||
> + g_str_has_prefix(buf, "at"))
> + res = at_server_parse(server, (char *) buf + 2);
> +
> + g_at_server_send_result_code(server, res);
> +
> +out:
> + /* We're overflowing the buffer, shutdown the socket */
> + if (server->buf && ring_buffer_avail(server->buf) == 0)
> + g_at_server_shutdown(server);
> +
> + g_at_server_unref(server);

Same here, see above.

> +}
> +
> +static gboolean received_data(GIOChannel *chan, GIOCondition cond,
> + gpointer data)
> +{
> + GAtServer *server = data;
> + struct v250_settings *v250 = server->v250;
> + GIOError err;
> + gsize rbytes;
> + gsize total_read = 0;
> + unsigned char *total_buf = ring_buffer_write_ptr(server->buf);

You're totally abusing the ring buffer concept here, the fact that this works 
is pure luck.

> + static gsize total_len;
> +
> + if (cond & G_IO_NVAL)
> + return FALSE;
> +
> + if (cond & (G_IO_HUP | G_IO_ERR)) {
> + g_at_server_shutdown(server);
> +
> + return FALSE;
> + }
> +
> + /* Regardless of condition, try to read all the data available */
> + do {
> + unsigned char *buf;
> + gsize toread;
> +
> + rbytes = 0;
> +
> + toread = ring_buffer_avail_no_wrap(server->buf);
> +
> + if (toread == 0)
> + break;
> +
> + buf = ring_buffer_write_ptr(server->buf);
> +
> + err = g_io_channel_read(chan, (char *) buf, toread, &rbytes);
> +
> + total_read += rbytes;
> +
> + if (rbytes > 0)
> + ring_buffer_write_advance(server->buf, rbytes);
> +
> + } while (err == G_IO_ERROR_NONE && rbytes > 0);
> +
> + g_at_util_debug_chat(server->debugf, TRUE, (char *) total_buf,
> + total_read, server->debug_data);
> +
> + if (total_read == 0) {
> + g_at_server_shutdown(server);

You should be checking rbytes == 0 && err != EAGAIN here.  Your check below is 
also redundant.

> +
> + return FALSE;
> + }
> +
> + total_len += total_read;
> +
> + /* Add '\0' to perform strchr */
> + total_buf[total_read] = '\0';
> +
> + /* Parse buffer until we meet the terminator so that
> +  * all preceding characters will be processed
> +  */
> + if (strchr((char *) total_buf, v250->s3)) {
> + parse_buffer(server, total_len);
> +
> + total_len = 0;
> + }

Should this be inside a while loop? Several commands can come in at once.

> +
> + if (err != G_IO_ERROR_NONE && err != G_I

Re: [PATCH 2/4] Add GAtServer basic parsing support

2010-01-14 Thread Marcel Holtmann
Hi Denis,

> > +#include "gatserver.h"
> > +
> > +#define DBG(fmt, arg...) g_print("%s:%s() " fmt, __FILE__, __FUNCTION__ ,
> >  ## arg) +
> 
> Move this to gat.h

can we actually not be using DBG inside gatchat. Why is that suddenly
needed?

Regards

Marcel


___
ofono mailing list
ofono@ofono.org
http://lists.ofono.org/listinfo/ofono


Re: [PATCH 2/4] Add GAtServer basic parsing support

2010-01-14 Thread Denis Kenzior
Hi Marcel,

> Hi Denis,
> 
> > > +#include "gatserver.h"
> > > +
> > > +#define DBG(fmt, arg...) g_print("%s:%s() " fmt, __FILE__,
> > > __FUNCTION__ , ## arg) +
> >
> > Move this to gat.h
> 
> can we actually not be using DBG inside gatchat. Why is that suddenly
> needed?

Actually good point. I agree this should be removed.

Regards,
-Denis
___
ofono mailing list
ofono@ofono.org
http://lists.ofono.org/listinfo/ofono


RE: [PATCH 1/4] Add gatutil.c to share common APIs with GAtServer

2010-01-14 Thread Zhang, Zhenhua
Hi Denis,

Denis Kenzior wrote:
> Hi Zhenhua,
> 
>> +gboolean g_at_util_set_io(GIOChannel *io)
> 
> Rename this g_at_util_setup_io
> 
>> +typedef void (*GAtDisconnectFunc)(gpointer user_data); typedef void
>> +(*GAtDebugFunc)(const char *str, gpointer user_data);
> 
> These don't belong here, create a new file called gat.h and put them
> there. 
> 
> Regards,
> -Denis

Thanks for your comments. Updated patch is attached.

Regards,
Zhenhua


0001-Add-gatutil.c-to-share-common-APIs-with-GAtServer.patch
Description: 0001-Add-gatutil.c-to-share-common-APIs-with-GAtServer.patch
___
ofono mailing list
ofono@ofono.org
http://lists.ofono.org/listinfo/ofono