Hi Denis,

Thanks for the review. I am working on doing a proper patch with your comments addressed. I do have some feedback below, though.

// Martin

On 24/08/2018 18.59, Denis Kenzior wrote:
Hi Martin,

On 08/24/2018 06:34 AM, Martin Hundebøll wrote:
So far the driver is only tested with GPRS.

The modem uses GSM 07.10 multiplexing, which breaks when changing
functional modes (i.e. going from CFUN=4 to CFUN=1), the driver doesn't
implement online/offline states. It does support disabling the device to
save power though, but users must go through the full setup procedure to
enable the device again.

So just a general nitpick.  oFono uses a customized version of the Linux Kernel coding style.  The main house rules are outlined in doc/coding-style.txt.  Another difference is that we don't mix tabs & spaces for indentation.

---
  Makefile.am      |   3 +
  plugins/m95.c    | 492 +++++++++++++++++++++++++++++++++++++++++++++++
  plugins/udevng.c |   1 +
  3 files changed, 496 insertions(+)
  create mode 100644 plugins/m95.c

diff --git a/Makefile.am b/Makefile.am
index 6dee4ce3..6fe4258a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -511,6 +511,9 @@ builtin_sources += plugins/telit.c
  builtin_modules += quectel
  builtin_sources += plugins/quectel.c
+builtin_modules += m95
+builtin_sources += plugins/m95.c
+
  builtin_modules += ublox
  builtin_sources += plugins/ublox.c
diff --git a/plugins/m95.c b/plugins/m95.c
new file mode 100644
index 00000000..32746996
--- /dev/null
+++ b/plugins/m95.c
@@ -0,0 +1,492 @@
+/*
+ *
+ *  oFono - Open Source Telephony
+ *
+ *  Copyright (C) 2008-2018  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 <stdlib.h>
+#include <unistd.h>
+#include <glib.h>
+#include <gatchat.h>
+#include <gattty.h>
+#include <gatmux.h>
+
+#define OFONO_API_SUBJECT_TO_CHANGE
+#include <ofono/plugin.h>
+#include <ofono/modem.h>
+#include <ofono/devinfo.h>
+#include <ofono/netreg.h>
+#include <ofono/sim.h>
+#include <ofono/sms.h>
+#include <ofono/ussd.h>
+#include <ofono/gprs.h>
+#include <ofono/gprs-context.h>
+#include <ofono/phonebook.h>
+#include <ofono/history.h>
+#include <ofono/log.h>
+#include <ofono/voicecall.h>
+#include <ofono/call-volume.h>
+#include <drivers/atmodem/vendor.h>
+
+#define NUM_DLC 5
+
+#define VOICE_DLC   0
+#define NETREG_DLC  1
+#define SMS_DLC     2
+#define GPRS_DLC    3
+#define SETUP_DLC   4
+
+static char *dlc_prefixes[NUM_DLC] = {"Voice: ", "Net: ", "SMS: ",
+                      "GPRS: " , "Setup: "};
+
+static const char *reset_prefix[]   = {"+CFUN:", NULL};
+static const char *enable_prefix[]  = {"+CPIN:", NULL};
+

These names are a bit strange...  What does cpin have to do with enable?

This modem instantly responds with "OK" when issuing a reset, but isn't ready until it later prints "+CPIN:\r\nVoicecall Ready"

I'll add a comment to the variable decleration.

+struct m95_data {
+    GIOChannel *tty;
+    GAtMux *mux;
+    GAtChat *dlcs[NUM_DLC];
+    guint cfun_reset;
+    guint cfun_enable;
+    guint cfun_disable_timeout;

No enable timeout?

+};
+
+static int m95_probe(struct ofono_modem *modem)
+{
+    struct m95_data *data;
+
+    DBG("%p", modem);
+
+    data = g_try_new0(struct m95_data, 1);
+    if (data == NULL)
+        return -ENOMEM;

So these days we prefer not to use g_try_new0 for small allocations. Just use g_new0 here.  On Linux it is almost impossible to run out memory in these cases anyway.

+
+    ofono_modem_set_data(modem, data);
+
+    return 0;
+}
+
+static void m95_remove(struct ofono_modem *modem)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+
+    DBG("%p", modem);
+
+    ofono_modem_set_data(modem, NULL);
+
+    g_free(data);
+}
+
+static void m95_debug(const char *str, void *user_data)
+{
+    const char *prefix = user_data;
+
+    ofono_info("%s%s", prefix, str);
+}
+
+static GAtChat *open_device(struct ofono_modem *modem,
+            const char *key, char *debug)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+    const char *device;
+    GAtSyntax *syntax;
+    GAtChat *chat;
+    GHashTable *options;
+
+    device = ofono_modem_get_string(modem, key);
+    if (device == NULL)
+        return NULL;
+
+    DBG("%s %s", key, device);
+
+    options = g_hash_table_new(g_str_hash, g_str_equal);
+    if (options == NULL)
+        return NULL;
+
+    g_hash_table_insert(options, "Baud", "115200");
+    g_hash_table_insert(options, "Parity", "none");
+    g_hash_table_insert(options, "StopBits", "1");
+    g_hash_table_insert(options, "DataBits", "8");
+    g_hash_table_insert(options, "XonXoff", "off");
+    g_hash_table_insert(options, "Local", "off");
+    g_hash_table_insert(options, "RtsCts", "off");
+    g_hash_table_insert(options, "Read", "on");
+
+    /* open serial device */
+    data->tty = g_at_tty_open(device, options);
+    g_hash_table_destroy(options);
+
+    if (data->tty == NULL)
+        return NULL;
+
+    /* create AT chat instance */
+    syntax = g_at_syntax_new_gsm_permissive();
+    chat = g_at_chat_new(data->tty, syntax);
+    g_at_syntax_unref(syntax);
+
+    if (chat == NULL) {
+        g_io_channel_unref(data->tty);
+        data->tty = NULL;
+
+        return NULL;
+    }
+
+    if (getenv("OFONO_AT_DEBUG"))
+        g_at_chat_set_debug(chat, m95_debug, debug);
+
+    return chat;
+}
+
+static GAtChat *create_chat(GIOChannel *channel, struct ofono_modem *modem,
+                char *debug)
+{
+    GAtSyntax *syntax;
+    GAtChat *chat;
+
+    if (channel == NULL)
+        return NULL;
+
+    syntax = g_at_syntax_new_gsmv1();
+    chat = g_at_chat_new(channel, syntax);
+    g_at_syntax_unref(syntax);
+
+    if (chat == NULL)
+        return NULL;
+
+    if (getenv("OFONO_AT_DEBUG"))
+        g_at_chat_set_debug(chat, m95_debug, debug);
+
+    return chat;
+}
+
+static void shutdown_device(struct m95_data *data)
+{
+    int i;
+

Might consider just doing

if (!data->mux)
     goto close_tty;

+    for (i = 0; i < NUM_DLC; i++) {
+        if (data->dlcs[i] == NULL)
+            continue;
+

And skip this part

I'll reorder the shutdown routine, but keep this check in place. The setup_internal_mux() function below might break in its for-loop due to a NULL-pointer returned from create_chat() and then call this function.

+        DBG("clean AT chat channel %i", i);
+        g_at_chat_cancel_all(data->dlcs[i]);
+        g_at_chat_unregister_all(data->dlcs[i]);
+        g_at_chat_unref(data->dlcs[i]);
+        data->dlcs[i] = NULL;
+    }
+
+    if (data->mux) {

And this if might not be needed then either

+        DBG("clean mux instance");
+        g_at_mux_shutdown(data->mux);
+        g_at_mux_unref(data->mux);
+        data->mux = NULL;
+    }
+

close_tty:
+    if (data->tty) {

Can this ever be non-NULL?  Even if it is, calling g_io_channel_unref is safe anyway.

+        DBG("clean tty io instance");
+        g_io_channel_unref(data->tty);
+        data->tty = NULL;
+    }
+}
+
+static void setup_internal_mux(struct ofono_modem *modem)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+    int i;
+
+    DBG("");
+
+    /* prepare the multiplexing instance */
+    data->mux = g_at_mux_new_gsm0710_basic(data->tty, 127);
+    if (data->mux == NULL)
+        goto error;
+
+    if (getenv("OFONO_MUX_DEBUG"))
+        g_at_mux_set_debug(data->mux, m95_debug, "MUX: ");
+
+    if (!g_at_mux_start(data->mux)) {
+        g_at_mux_shutdown(data->mux);
+        g_at_mux_unref(data->mux);

data->mux = NULL?

Or alternatively, why not just goto error directly.

+        goto error;
+    }
+
+    /* create multiplexing channels for gprs, net, voice, text, and
+     * control */
+    for (i = 0; i < NUM_DLC; i++) {

Note that you're not using SETUP_DLC for anything after the MUX is created.  So not sure whether you want to treat is specially.

I'll replace the SETUP_DLC entry in .dlcs[] with a dedicated member instead.

+        GIOChannel *channel = g_at_mux_create_channel(data->mux);
+
+        data->dlcs[i] = create_chat(channel, modem, dlc_prefixes[i]);
+        if (data->dlcs[i] == NULL) {
+            ofono_error("Failed to create channel");
+            goto error;
+        }
+
+        g_io_channel_unref(channel);
+    }
+
+    ofono_modem_set_powered(modem, TRUE);
+
+    return;
+
+error:
+    shutdown_device(data);
+    ofono_modem_set_powered(modem, FALSE);
+}
+
+static void mux_setup_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+    struct ofono_modem *modem = user_data;
+    struct m95_data *data = ofono_modem_get_data(modem);
+
+    DBG("");
+
+    if (!ok)
+        goto error;
+
+    /* free the initial setup channel, as it is going to be created
+     * using multiplexing */
+    g_at_chat_unref(data->dlcs[SETUP_DLC]);
+    data->dlcs[SETUP_DLC] = NULL;

If you make the changes I suggested above, make sure to destroy this chat regardless of the error condition above.

+
+    /* setup multiplexing */
+    setup_internal_mux(modem);
+
+    return;
+
+error:
+    shutdown_device(data);
+    ofono_modem_set_powered(modem, FALSE);
+}
+
+static void cfun_enable(GAtResult *result, gpointer user_data)

This sets up the mux, so a better name might be send_cmux or something to that effect.

+{
+    struct ofono_modem *modem = user_data;
+    struct m95_data *data = ofono_modem_get_data(modem);
+    GAtResultIter iter;
+
+    DBG("%p", modem);
+
+    g_at_result_iter_init(&iter, result);
+    if (!g_at_result_iter_next(&iter, enable_prefix[0]))
+        return;
+
+    /* remove the enable-completed listener */
+    if (data->cfun_enable) {
+        g_at_chat_unregister(data->dlcs[SETUP_DLC], data->cfun_enable);
+        data->cfun_enable = 0;
+    }
+
+    /* setup multiplexing */
+    g_at_chat_send(data->dlcs[SETUP_DLC], "AT+CMUX=0,0,5,127,10,3,30,10,2",
+               NULL, mux_setup_cb, modem, NULL);
+}
+
+static void cfun_enable_query(gboolean ok, GAtResult *result,
+                  gpointer user_data)
+{
+    DBG("ok %d", ok);
+
+    if (ok)
+        cfun_enable(result, user_data);
+}
+
+static void cfun_reset(GAtResult *result, gpointer user_data)
+{
+    struct ofono_modem *modem = user_data;
+    struct m95_data *data = ofono_modem_get_data(modem);
+    GAtResultIter iter;
+
+    DBG("%p", modem);
+
+    g_at_result_iter_init(&iter, result);
+    if (!g_at_result_iter_next(&iter, reset_prefix[0]))
+        return;
+
+    /* remove the reset-completed listener */
+    g_at_chat_unregister(data->dlcs[SETUP_DLC], data->cfun_reset);
+    data->cfun_reset = 0;
+
+    /* prepare to enable the modem */
+    g_at_chat_send(data->dlcs[SETUP_DLC], "ATE0", NULL, NULL, NULL, NULL); +    g_at_chat_send(data->dlcs[SETUP_DLC], "AT+IFC=2,2", NULL, NULL, NULL,
+               NULL);
+    g_at_chat_send(data->dlcs[SETUP_DLC], "AT+GMM", NULL, NULL, NULL, NULL);
+    g_at_chat_send(data->dlcs[SETUP_DLC], "AT", NULL, NULL, NULL, NULL);
+    g_at_chat_send(data->dlcs[SETUP_DLC], "AT+IPR=115200&w", NULL, NULL,
+               NULL, NULL);
+
+    /* setup a listener to delay multiplex-setup until modem is ready */
+    data->cfun_enable = g_at_chat_register(data->dlcs[SETUP_DLC],
+                           "+CPIN:", cfun_enable, FALSE,
+                           modem, NULL);
+
+    /* enable the modem */
+    g_at_chat_send(data->dlcs[SETUP_DLC], "AT+CFUN=1", enable_prefix,
+               cfun_enable_query, modem, NULL);

This isn't a query :)  cfun_enable_cb?

+}
+
+static void cfun_reset_query(gboolean ok, GAtResult *result, gpointer user_data)
+{
+    DBG("ok %d", ok);
+
+    if (ok)
+        cfun_reset(result, user_data);
+}
+
+static int m95_enable(struct ofono_modem *modem)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+
+    DBG("%p", modem);
+
+    data->dlcs[SETUP_DLC] = open_device(modem, "Device", "Setup: ");
+    if (data->dlcs[SETUP_DLC] == NULL)
+        return -EINVAL;
+
+    /* when issuing the reset below, the modem is not ready until it
+     * responds with "+CFUN: 0", so set up a listener for this */
+    data->cfun_reset = g_at_chat_register(data->dlcs[SETUP_DLC], "+CFUN:",
+                          cfun_reset, FALSE, modem, NULL);
+
+    /* bring the modem into a known state by issuing a reset */
+    g_at_chat_send(data->dlcs[SETUP_DLC], "AT+CFUN=4,1", reset_prefix,
+               cfun_reset_query, modem, NULL);

This is not a query.  A query is something with a question mark in the AT command.  Might want to just name this cfun_reset_cb

+
+    return -EINPROGRESS;
+}
+
+static void cfun_disable(gboolean ok, GAtResult *result, gpointer user_data)
+{
+    struct ofono_modem *modem = user_data;
+    struct m95_data *data = ofono_modem_get_data(modem);
+
+    /* device is now in non-functional mode, so shut down it down */
+    shutdown_device(data);
+    ofono_modem_set_powered(modem, FALSE);
+}
+
+static gboolean cfun_disable_timeout(gpointer user_data)
+{
+    struct ofono_modem *modem = user_data;
+    struct m95_data *data = ofono_modem_get_data(modem);
+
+    /* now put device in non-functional mode */
+    g_at_chat_send(data->dlcs[SETUP_DLC], "AT+CFUN=0,1", NULL, cfun_disable,

cfun_disable_cb

+               modem, NULL);
+
+    return FALSE;
+}
+
+static int m95_disable(struct ofono_modem *modem)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+
+    DBG("%p", modem);
+
+    /* shut down the device to tear down the muxing channels */
+    shutdown_device(data);
+
+    /* reopen device to get a plain AT control channel */
+    data->dlcs[SETUP_DLC] = open_device(modem, "Device", "Disable: ");
+    if (data->dlcs[SETUP_DLC] == NULL)
+        return -EINVAL;
+
+    /* add a timer to wait for device to drop GSM 07.10 multiplexing */
+    data->cfun_disable_timeout = g_timeout_add(500, cfun_disable_timeout,
+                           modem);

Ah, mux_dropped_timeout then.  And cfun_disable_timeout to send_cfun_disable or something.

+
+    return -EINPROGRESS;
+}
+
+static void m95_pre_sim(struct ofono_modem *modem)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+    struct ofono_sim *sim;
+
+    DBG("%p", modem);
+
+    ofono_devinfo_create(modem, 0, "atmodem", data->dlcs[VOICE_DLC]);
+    sim = ofono_sim_create(modem, OFONO_VENDOR_QUECTEL, "atmodem",
+                        data->dlcs[VOICE_DLC]);
+
+    if (sim)
+        ofono_sim_inserted_notify(sim, TRUE);

I assume this modem doesn't support hotswap?

Hmm.. The modem does support dual-sim, but my test piece has only one slot.

The AT command reference specifies that the modem must be disabled to switch slot. So I guess it doesn't support hot-swap.

The SIM slot isn't accessible in my mockup, so I cannot really test it.

+}
+
+static void m95_post_sim(struct ofono_modem *modem)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+    struct ofono_gprs *gprs;
+    struct ofono_gprs_context *gc;
+
+    DBG("%p", modem);
+
+    ofono_phonebook_create(modem, 0, "atmodem", data->dlcs[VOICE_DLC]);
+    ofono_sms_create(modem, OFONO_VENDOR_QUECTEL, "atmodem",
+             data->dlcs[SMS_DLC]);
+
+    gprs = ofono_gprs_create(modem, 0, "atmodem", data->dlcs[GPRS_DLC]);
+    if (gprs == NULL)
+        return;
+
+    gc = ofono_gprs_context_create(modem, OFONO_VENDOR_QUECTEL, "atmodem",
+                       data->dlcs[GPRS_DLC]);
+    if (gc)
+        ofono_gprs_add_context(gprs, gc);
+}
+
+static void m95_post_online(struct ofono_modem *modem)
+{
+    struct m95_data *data = ofono_modem_get_data(modem);
+
+    DBG("%p", modem);
+
+    ofono_netreg_create(modem, OFONO_VENDOR_QUECTEL, "atmodem",
+                data->dlcs[NETREG_DLC]);
+    ofono_ussd_create(modem, 0, "atmodem", data->dlcs[VOICE_DLC]);
+    ofono_voicecall_create(modem, 0, "atmodem", data->dlcs[VOICE_DLC]);

Generally voicecalls are created in pre_sim state for emergency calls...

OK, I'll move the call to m95_pre_sim(), and test with voice calls/text messages.

+    ofono_call_volume_create(modem, 0, "atmodem", data->dlcs[VOICE_DLC]);
+}
+
+static struct ofono_modem_driver m95_driver = {
+    .name        = "m95",
+    .probe        = m95_probe,
+    .remove        = m95_remove,
+    .enable        = m95_enable,
+    .disable    = m95_disable,
+    .pre_sim    = m95_pre_sim,
+    .post_sim    = m95_post_sim,
+    .post_online    = m95_post_online,
+};
+
+static int m95_init(void)
+{
+    return ofono_modem_driver_register(&m95_driver);
+}
+
+static void m95_exit(void)
+{
+    ofono_modem_driver_unregister(&m95_driver);
+}
+
+OFONO_PLUGIN_DEFINE(m95, "Quectel M95 modem driver", VERSION,
+            OFONO_PLUGIN_PRIORITY_DEFAULT, m95_init, m95_exit)
diff --git a/plugins/udevng.c b/plugins/udevng.c
index 71a70f0b..e58051d4 100644
--- a/plugins/udevng.c
+++ b/plugins/udevng.c
@@ -1303,6 +1303,7 @@ static struct {
      { "wavecom",    setup_wavecom        },
      { "tc65",    setup_tc65        },
      { "ehs6",    setup_ehs6        },
+    { "m95",    setup_serial_modem },
      { }
  };


Regards,
-Denis
_______________________________________________
ofono mailing list
[email protected]
https://lists.ofono.org/mailman/listinfo/ofono

Reply via email to