

/*

	GPRS-PPP.C
	

	Here is a quick sample of the GPRS - PPP connection procedure:
	For standard GPRS connections use
	yourAPN = internet ( vodafone ) 
	and with blank username and password use
	if ((pppcom = _open("ppp:" PPPCOM, _O_RDWR | _O_BINARY)) == -1)
	instead of
	if ((pppcom = _open("ppp:" PPPCOM "/" PPPUSER "/" PPPPASS, _O_RDWR |_O_BINARY)) == -1)

	For the function "WebServer" you can cut and paste httpserv.c from:

	 *
     * Register our device for the file system.
     */
    NutRegisterDevice(&devUrom, 0, 0);
	onwards.

	Hope this will help you.
*/




#include <inttypes.h>
#include "gsm_ppp.h"
#include "configuration.h"
#include "utils.h"

#include <dev/uartavr.h>
#include <dev/ppp.h>
#include <dev/chat.h>
#include <dev/debug.h>

#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/socket.h>

#include <sys/timer.h>

#include <arpa/inet.h>
#include <netdb.h>
#include <net/if_var.h>
#include <net/route.h>
#include <netinet/ppp_fsm.h>

#ifdef NUTDEBUG
 #include <net/netdebug.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>

#include "httpserv.h"
#include "web.h"

/*
 * PPP user and password.
 */
#define PPPUSER     "yourUser"
#define PPPPASS     "yourPass"

/*
 * Modem chat script
 */
#define PPPCHAT "'' AT+CGDCONT=1,\"IP\",\"yourAPN\" OK ATD*99***1# CONNECT"
#define DISCONNECT "'' ATH OK"

/*
 * PPP device settings.
 */
#define PPPDEV      devUart0    /* Use standard UART driver. */
#define PPPCOM      "uart0"     /* Physical device name. */
#define PPPSPEED    115200      /* Baudrate. */
#define PPPRXTO     1000        /* Character receive timeout. */

extern u_char ppp_hackup;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
 * Initializes the GSM/GPRS module and opens a PPP connection to the ISP.
 */
void PPPConnect(void)
{
 int pppcom = -1;
 FILE * comm_phone = NULL;
    PPPDCB *dcb;
    u_long lctl;
 int ppp_attempts = 0,
  rc;

 // Register the UART device
 NutRegisterDevice(&PPPDEV, 0, 0);

 printf_P(PSTR("Open uart..."));

 comm_phone = fopen(PPPCOM, "r+");
 if(!comm_phone) {
        printf("Failed to open " PPPCOM "\n");
  for(;;);
    }

 printf_P(PSTR("done\n"));

 lctl = PPPSPEED;
 _ioctl(_fileno(comm_phone), UART_SETSPEED, &lctl);

 // Register PPP
 NutRegisterDevice(&devPpp, 0, 0);
 dcb = devPpp.dev_dcb;

#ifdef NUTDEBUG
 /*
  * Optionally enable PPP trace.
  */
 NutTracePPP(stdout, 1);
 //NutTraceTcp(stdout, 1);
#endif

NutSleep(5000);

 /*
     * PPP connection loop.
     */
 for (;;)
 {
  if ((pppcom != -1) && (dcb->dcb_ipcp_state != PPPS_CLOSED))
  {
   if (_close(pppcom) == 0)
   {
    ppp_hackup = 0;
    pppcom = -1;
   }

   DisconnectCall(comm_phone);
  }

  /*
         * Connect using a chat script. We may also set any
         * required hardware handshake line at this stage.
         */
        printf_P(PSTR("Connecting..."));
        if ((rc = NutChat(_fileno(comm_phone), PPPCHAT)) != 0) {
            printf("no connect, reason = %d\n", rc);
            continue;
        }
  printf_P(PSTR("done\n"));

  if (pppcom == -1)
  {
   if ((pppcom = _open("ppp:" PPPCOM "/" PPPUSER "/" PPPPASS, _O_RDWR |
_O_BINARY)) == -1)
   {
    printf_P(PSTR("Failed to open " PPPCOM "\n"));
    for (;;);
   }

   /*
    * Set PPP line speed.
    */
   lctl = PPPSPEED;
   _ioctl(pppcom, UART_SETSPEED, &lctl);

   /*
    * The PPP driver doesn't set any receive timeout, but
    * may require it.
    */
   lctl = PPPRXTO;
   _ioctl(pppcom, UART_SETREADTIMEOUT, &lctl);
  }

  printf_P(PSTR("Configure PPP..."));
  /*
   * We are connected, configure our PPP network interface.
   * This will initiate the PPP configuration negotiation
   * and authentication with the server.
   */
  if (NutNetIfConfig("ppp", 0, 0, 0))
  {
   printf_P(PSTR("failed\n"));
   // If we can't establish PPP 3 times in a row reboot
   ppp_attempts ++;
   ///if (ppp_attempts >= 3)
   //CDCS_Reboot();
   continue;
  }

  printf_P(PSTR("ok\n"));

  /*
   * Set name server and default route. Actually the PPP interface
   * should do this, but the current release doesn't.
   */
  dcb = devPpp.dev_dcb;
  NutDnsConfig2(0, 0, dcb->dcb_ip_dns1, dcb->dcb_ip_dns2);
  NutIpRouteAdd(0, 0, dcb->dcb_remote_ip, &devPpp);

  /*
   * Display our IP settings.
   */
  printf_P(PSTR("     Local IP: %s\n"), inet_ntoa(dcb->dcb_local_ip));
  printf_P(PSTR("    Remote IP: %s\n"), inet_ntoa(dcb->dcb_remote_ip));
  printf_P(PSTR("  Primary DNS: %s\n"), inet_ntoa(dcb->dcb_ip_dns1));
  printf_P(PSTR("Secondary DNS: %s\n"), inet_ntoa(dcb->dcb_ip_dns2));
  printf_P(PSTR("Available mem: %u\n"),NutHeapAvailable());

  // No DNS was supplied.
  if (dcb->dcb_ip_dns1 == 0)
  {
   printf_P(PSTR("No DNS supplied. Reconnect.\n"));
   ppp_attempts ++;
   continue;
  }

  // Execute the HTTP server.
  WebServer();
 }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
 * Disconnect the GPRS call
 */
void DisconnectCall(FILE * comm_phone)
{
 NutChat(_fileno(comm_phone), DISCONNECT);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

