libraries / Mitov / Mitov_GPRS.hon commit Added link to project report (97a3ba0)
   1////////////////////////////////////////////////////////////////////////////////
   2//                                                                            //
   3//     This software is supplied under the terms of a license agreement or    //
   4//     nondisclosure agreement with Mitov Software and may not be copied      //
   5//     or disclosed except in accordance with the terms of that agreement.    //
   6//         Copyright(c) 2002-2016 Mitov Software. All Rights Reserved.        //
   7//                                                                            //
   8////////////////////////////////////////////////////////////////////////////////
   9
  10#ifndef _MITOV_GPRS_h
  11#define _MITOV_GPRS_h
  12
  13#include <Mitov.h>
  14#include <Mitov_BasicEthernet.h>
  15#include <Mitov_GSMShield.h>
  16#include <GSM.h>
  17
  18namespace Mitov
  19{
  20//---------------------------------------------------------------------------
  21        class GPRSModule : public Mitov::GSMModule
  22        {
  23                typedef Mitov::GSMModule inherited;
  24
  25        public:
  26                Mitov::SimpleList<BasicEthernetSocket<GPRSModule>*>     Sockets;
  27
  28        public:
  29                bool    Enabled = true;
  30
  31                Mitov::SimpleObjectList<GPRSAccessPoint *>      AccessPoints;
  32
  33        protected:
  34                GSMShield &FOwner;
  35
  36        protected:
  37                GPRS    FGprs;
  38                bool    FConnected = false;
  39
  40        public:
  41                bool    IsStarted = false;
  42
  43        public:
  44                void SetEnabled( bool AValue )
  45                {
  46            if( Enabled == AValue )
  47                return;
  48
  49            Enabled = AValue;
  50                        if( Enabled )
  51                                StartEthernet();
  52
  53                        else
  54                                StopEthernet();
  55
  56                }
  57
  58        protected:
  59                virtual void StartModule()
  60                {
  61                        if( Enabled )
  62                                StartEthernet();
  63
  64                        inherited::StartModule();
  65                }
  66
  67                void StopEthernet()
  68                {
  69                        for( int i = 0; i < Sockets.size(); ++i )
  70                                Sockets[ i ]->StopSocket();
  71
  72//                      LWiFi.end();
  73                        IsStarted = false;
  74                }
  75
  76                void StartEthernet()
  77                {
  78                        if( ! FOwner.IsStarted )
  79                                return;
  80/*
  81                        if( ! AccessPoints.length() )
  82                                return;
  83
  84                        if( ! Sockets.length() )
  85                                return;
  86*/
  87                        TryConnect( true, 0 );
  88//                      if( AccessPoint != "" )
  89//                              IsStarted = FGprs.attachGPRS( (char *)AccessPoint.c_str(), (char *)UserName.c_str(), (char *)Password.c_str() );
  90
  91//                      else
  92//                              AConnected = FGprs.attachGPRS();
  93
  94/*
  95                        if( AConnected )
  96                        {
  97                        }
  98*/
  99                }
 100
 101                void TryConnect( bool FromStart, unsigned long currentMicros )
 102                {
 103                        for( int i = 0; i < AccessPoints.size(); i ++ )
 104                                if( AccessPoints[ i ]->Enabled )
 105                                {
 106                                        if( ! FromStart )
 107                                                if( ! AccessPoints[ i ]->CanRetry( currentMicros ) )
 108                                                        continue;
 109
 110                                        FConnected = FGprs.attachGPRS( (char *)AccessPoints[ i ]->AccessPoint.c_str(), (char *)AccessPoints[ i ]->UserName.c_str(), (char *)AccessPoints[ i ]->Password.c_str() );
 111                                        if( FConnected )
 112                                                break;
 113
 114                                        AccessPoints[ i ]->FLastTime = currentMicros;
 115                                        ++ AccessPoints[ i ]->FRetryCount;
 116                                }
 117
 118                }
 119
 120                virtual void SystemLoopBegin( unsigned long currentMicros )
 121                {
 122                        if( ! FConnected )
 123                                TryConnect( false, currentMicros );
 124
 125                        inherited::SystemLoopBegin( currentMicros );
 126                }
 127
 128        public:
 129                GPRSModule( GSMShield &AOwner ) :
 130                        FOwner( AOwner )
 131                {                       
 132                        AOwner.Modules.push_back( this );
 133                }
 134
 135        };
 136//---------------------------------------------------------------------------
 137/*
 138        class LinkItWiFiTCPClientSocket : public TCPClientSocket<Mitov::LinkItWiFiModule,LWiFiClient>
 139        {
 140                typedef TCPClientSocket<Mitov::LinkItWiFiModule,LWiFiClient> inherited;
 141
 142        public:
 143                virtual bool CanSend()
 144                {
 145                        return inherited::Enabled && inherited::FOwner.Enabled && FClient;
 146                }
 147
 148        public:
 149                LinkItWiFiTCPClientSocket( LinkItWiFiModule &AOwner, ::IPAddress AIPAddress ) :
 150                        inherited( AOwner, AIPAddress )
 151                {
 152                }
 153
 154        };
 155//---------------------------------------------------------------------------
 156        class LinkItWiFiTCPServerSocket : public TCPServerSocket<Mitov::LinkItWiFiModule,LWiFiServer,LWiFiClient>
 157        {
 158                typedef TCPServerSocket<Mitov::LinkItWiFiModule,LWiFiServer,LWiFiClient> inherited;
 159
 160        public:
 161                virtual bool CanSend()
 162                {
 163                        return inherited::Enabled && inherited::FOwner.Enabled && FClient;
 164                }
 165
 166        public:
 167                LinkItWiFiTCPServerSocket( LinkItWiFiModule &AOwner ) :
 168                        inherited( AOwner )
 169                {
 170                }
 171        };
 172//---------------------------------------------------------------------------
 173        class LinkItWiFiUDPSocket : public UDPSocket<Mitov::LinkItWiFiModule,LWiFiUDP>
 174        {
 175                typedef UDPSocket<Mitov::LinkItWiFiModule,LWiFiUDP> inherited;
 176
 177        public:
 178                LinkItWiFiUDPSocket( LinkItWiFiModule &AOwner, ::IPAddress ARemoteIPAddress ) :
 179                        inherited( AOwner, ARemoteIPAddress )
 180                {
 181                }
 182        };
 183//---------------------------------------------------------------------------
 184*/
 185}
 186
 187#endif