C# Modem API
Collaboration diagram for C# Modem API:

Modules

 Exceptions thrown by Modem
 
 Enumerations
 
 Events related with Modem and SIM
 

Classes

class  ModemManager
 Implementation for IModemManager. Start point for interact with Modem Manager More...
 
interface  IModem
 Modem single element. Allows to interact with modem. Get information and set configuration More...
 
interface  IModemManager
 Modem Manager allows to mange all device Modem features More...
 
interface  ISim
 SIM element that allows to get information from the SIM and interact with it More...
 

Detailed Description

To work with Deepsy modem service it is necessary to use ModemManager located in GMV.ITS.HAL.DEEPSY.Modem.Facade for getting the initial ModemManager object.

ModemManager modemManager = new ModemManager("");


ModemManager has an optional parameter indicating the ip address of remote service. For instance:

ModemManager modemManager = new ModemManager("172.22.198.103");


ModemManager is an IDisposable interface. An object that may hold resources (such as file or socket handles) until it is closed. The Dispose() method of an IDisposable object is called automatically when exiting a try-with-resources block, or exiting a using clause where it was declared. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur:

using (ModemManager modemManager = new ModemManager("172.22.198.103")){
}


If this object is not inside a try with resources block, it is recommended to call to Dispose() in order to finalize properly;

modemManager.Dispose();

Examples

  • Enabling/disabling modem
    For enabling/disabling modem it is needed to call to enable and disable methods in modem.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.DataConnection
    {
    class ModemEnableDisableApp
    {
    static void Main(string[] args)
    {
    ModemManager modemManager = new ModemManager("192.168.0.144");
    List<IModem> modemList = modemManager.GetModems().ToList();
    foreach(IModem modem in modemList)
    {
    PowerStatus.Power modemStatusPower = modem.GetModemStatus().PowerStatus;
    Console.WriteLine($"Power Modem status: {modem.GetModemStatus().PowerStatus}");
    if(modemStatusPower == PowerStatus.Power.MODEM_DISABLED)
    {
    modem.Enable();
    }
    else
    {
    modem.Disable();
    }
    }
    }
    }
    }


  • Subscribe to events
    There are 5 types of events in the modem system:
    • ModemListEvent : This event happens when a modem is added to or removed from the system. To receive this kind of events, it is necessary to subscribe to the ModemManager object with an object implementing interface ModemListObserver
    • ModemEvent:ModemStatusEvent : This event happens when a modem status changes. To receive this kind of events, it is necessary to subscribe to the Modem object with an object implementing interface ModemObserver
    • ModemEvent:ModemWarningEvent : This event happens when a modem has a warning. To receive this kind of events, it is necessary to subscribe to the Modem object with an object implementing interface ModemObserver
    • ModemEvent:SimListEvent : This event happens when a sim is added or removed from a modem. To receive this kind of events, it is necessary to subscribe to the Modem object with an object implementing interface ModemObserver
    • SimEvent : This event happens when a sim status changes. To receive this kind of events, it is necessary to subscribe to the Sim object with an object implementing interface SimObserver
    This example subscribes to every event.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.DataConnection
    {
    class ModemEventsApp
    {
    static void Main(string[] args)
    {
    ModemManager modemManager = new ModemManager("192.168.0.144");
    modemManager.ModemListEvent += ModemManager_ModemListEvent;
    List<IModem> modemList = modemManager.GetModems().ToList();
    foreach (IModem modem in modemList)
    {
    SubscribeToModemEvents(modem);
    SubscribeToSimEvents(modem.SimList);
    }
    }
    private static void SubscribeToModemEvents(IModem modem)
    {
    modem.ModemStatusEvent += Modem_ModemStatusEvent;
    modem.ModemWarningEvent += Modem_ModemWarningEvent;
    modem.SimListEvent += Modem_SimListEvent;
    }
    private static void SubscribeToSimEvents(IEnumerable<ISim> sims)
    {
    if (sims.Any())
    {
    foreach (ISim sim in sims)
    {
    sim.SimStatusEvent += Sim_SimStatusEvent;
    }
    }
    }
    private static void Sim_SimStatusEvent(object sender, ISimStatusEvent e)
    {
    Console.WriteLine($"Sim {e.Sim.GetInformation().Imsi} status changed to: {e.Status}");
    }
    private static void Modem_SimListEvent(object sender, ISimListEvent e)
    {
    Console.WriteLine($"SIM {e.Sim.GetInformation().Imsi} event: {((e.Action == SimListAction.Action.Added) ? "ADDED" : "REMOVED")}");
    if (e.Action == SimListAction.Action.Added)
    {
    SubscribeToSimEvents(new List<ISim> { e.Sim });
    }
    }
    private static void Modem_ModemWarningEvent(object sender, IModemWarningEvent e)
    {
    Console.WriteLine($"Modem warning {e.Modem.GetModemInformation().Imei}: {e.Warning}");
    }
    private static void Modem_ModemStatusEvent(object sender, IModemStatusEvent e)
    {
    Console.WriteLine($"Modem {e.Modem.GetModemInformation().Imei} status changed to:");
    Console.WriteLine($"State {e.Status.PowerStatus}");
    Console.WriteLine($"Power {e.Status.PowerStatus}");
    Console.WriteLine($"Signal {e.Status.SignalQuality}");
    Console.WriteLine($"Network Status {e.Status.CellularNetworkStatus}");
    Console.WriteLine($"Access Technology {e.Status.AccessTechnology}");
    }
    private static void ModemManager_ModemListEvent(object sender, IModemListEvent e)
    {
    Console.WriteLine($"Modem {e.Modem.GetModemInformation().Imei} event: {((e.Action == ModemListAction.Action.Added) ? "ADDED" : "REMOVED")}");
    if (e.Action == ModemListAction.Action.Added)
    {
    SubscribeToModemEvents(e.Modem);
    SubscribeToSimEvents(e.Modem.GetSimList());
    }
    }
    }
    }

  • Manage Forced APN After setting a forced APN it is important to restart modem in order to connect with the new APN parameters
    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.DataConnection
    {
    class ModemForcedApnApp
    {
    static void Main(string[] args)
    {
    ModemManager modemManager = new ModemManager("192.168.0.144");
    //Get Forced APN
    Console.WriteLine("\nGet Forced APN: ");
    ApnInformation apn = modemManager.GetForcedApn();
    printAPN(apn);
    //Set a Forced APN
    Console.WriteLine("\nSet a new Forced APN: ");
    modemManager.SetForcedApn(new ApnInformation()
    {
    Apn = "myapnForced",
    User = "myuserForced",
    Password = "mypasswordForced"
    });
    apn = modemManager.GetForcedApn();
    printAPN(apn);
    //Stop the use of a Forced APN
    Console.WriteLine("\nStop the use of a Forced APN: ");
    modemManager.StopForcedApn();
    apn = modemManager.GetForcedApn();
    printAPN(apn);
    }
    static void printAPN(ApnInformation _apn)
    {
    Console.WriteLine($"Current Forced APN:" +
    "\n\tAPN: [" + _apn.Apn + "]" +
    "\n\tUser: [" + _apn.User + "]" +
    "\n\tPassword: [" + _apn.Password + "]");
    }
    }
    }

  • Get Modem data connection information and statistics
    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.DataConnection
    {
    class ModemInformationApp
    {
    static void Main(string[] args)
    {
    ModemManager modemManager = new ModemManager("192.168.0.144");
    List<IModem> modemList = modemManager.GetModems().ToList();
    foreach(IModem modem in modemList)
    {
    Console.WriteLine($"Modem Name: {modem.GetInterfaceInformation().Name}");
    Console.WriteLine($"Modem IP: {modem.GetInterfaceInformation().IpAddress}");
    List<IInterfaceStatistics> stats = modem.GetInterfaceInformation().Statistics.ToList();
    foreach(IInterfaceStatistics stat in stats)
    {
    Console.WriteLine($"{stat.Type}: {stat.Value}");
    }
    }
    }
    }
    }

  • Sim Operations
    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.DataConnection
    {
    class ModemSimOperationsApp
    {
    static void Main(string[] args)
    {
    ModemManager modemManager = new ModemManager("192.168.0.144");
    List<IModem> modemList = modemManager.GetModems().ToList();
    foreach(IModem modem in modemList)
    {
    Console.WriteLine($"Modem: {modem.GetModemInformation().Imei}, data status enable: {(modem.DataConnectionStatus() ? "TRUE" : "FALSE")}");
    List<ISim> activeSims = modem.GetActiveSimList().ToList();
    foreach (ISim sim in activeSims)
    {
    Console.WriteLine($"SIM IMSI: {sim.GetInformation().Imsi} is activated");
    }
    }
    }
    }
    }