| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- /*
- * Copyright STREAMIT BV, 2010.
- *
- * Project : SIR
- * Module : Session
- * File name $Workfile: Session.c $
- * Last Save $Date: 2003/08/16 $
- * $Revision: 0.1 $
- * Creation Date : 2003/08/16
- *
- * Description : Handles the connection to the Internet via
- * ethernet or modem/ppp
- *
- */
- #define LOG_MODULE LOG_SESSION_MODULE
- /*--------------------------------------------------------------------------*/
- /* Include files */
- /*--------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <string.h>
- #include <io.h>
- #include <fcntl.h>
- #include <dev/nicrtl.h>
- #include <dev/uartavr.h>
- #include <dev/ppp.h>
- #include <sys/heap.h>
- #include <sys/thread.h>
- #include <sys/timer.h>
- #include <sys/confnet.h>
- #include <netdb.h>
- #include <net/route.h>
- #include <arpa/inet.h>
- #include <pro/httpd.h>
- #include <pro/dhcp.h>
- #ifdef NUTDEBUG
- #include <sys/osdebug.h>
- #include <net/netdebug.h>
- #endif
- #include <sys/confos.h>
- //#pragma text:appcode
- #include "system.h"
- #include "session.h"
- #include "log.h"
- //#include "settings.h"
- #include "display.h"
- #include "version.h"
- /*!
- * \addtogroup Session
- */
- /*@{*/
- /*--------------------------------------------------------------------------*/
- /* Constant definitions */
- /*--------------------------------------------------------------------------*/
- /*!\brief Ethernet chip definitions */
- #define ETH0_BASE 0xC300
- #define ETH0_IRQ 5
- /*--------------------------------------------------------------------------*/
- /* Type declarations */
- /*--------------------------------------------------------------------------*/
- /*!\brief State of this module */
- typedef enum T_SESSION_STATE
- {
- STATE_IDLE = 0, /* We are idle */
- STATE_STARTING, /* We're setting up the session */
- STATE_OPEN, /* We have a session */
- STATE_STOPPING /* We're stopping */
- } TStreamerState;
- /*--------------------------------------------------------------------------*/
- /* Local variables */
- /*--------------------------------------------------------------------------*/
- /*!\brief Uart device */
- static FILE *g_pUart;
- /*!\brief Name of the ethernet device */
- static char szEthernetIfName[sizeof(devEth0.dev_name)];
- /*!\brief Global error code. */
- static TError g_tError;
- /*--------------------------------------------------------------------------*/
- /* Global variables */
- /*--------------------------------------------------------------------------*/
- /*--------------------------------------------------------------------------*/
- /* Local functions */
- /*--------------------------------------------------------------------------*/
- static void SetDhcpDnsServers(u_long dwDns1, u_long dwDns2);
- static void SetFixedDnsServers(void);
- static INLINE TError NetConfig(CONST char *szIfName);
- /*!
- * \brief Set and save DNS server settings
- *
- * If no servers are specified, NutOS is asked
- * for the DNS servers. If NutOS doesn't have any,
- * we retrieve the previously saved servers
- * Finally, the servers that are current, are stored
- * in the settings and NutOS itself
- *
- * \param dwDns1 IP address of first DNS server
- * \param dwDns2 IP address of second DNS server
- *
- * \return -
- */
- static void SetDhcpDnsServers(u_long dwDns1, u_long dwDns2)
- {
- // //char szDns[sizeof(SETTINGS_POINTER->Isp.szDns1)];
- // /*
- // * If not specified get current DNS' from NutOs
- // */
- // if ((dwDns1 == 0) && (dwDns2 == 0))
- // {
- // NutGetDnsServers(&dwDns1, &dwDns2);
- // }
- // /*
- // * If still no DNS servers, get previously saved config
- // */
- // if ((dwDns1 == 0) && (dwDns2 == 0))
- // {
- // //SettingsGet(szDns, &SETTINGS_POINTER->Isp.szDns1, sizeof(szDns));
- // if ((dwDns1 = inet_addr(szDns)) == (u_long)-1)
- // {
- // dwDns1 = 0;
- // }
- // //SettingsGet(szDns, &SETTINGS_POINTER->Isp.szDns2, sizeof(szDns));
- // if ((dwDns2 = inet_addr(szDns)) == (u_long)-1)
- // {
- // dwDns2 = 0;
- // }
- // }
- // /*
- // * Save DNS servers and let NutOs use them
- // */
- // if ((dwDns1 != 0) || (dwDns2 != 0))
- // {
- // strcpy(szDns, inet_ntoa(dwDns1));
- // //SettingsSet(szDns, &SETTINGS_POINTER->Isp.szDns1, sizeof(szDns));
- // strcpy(szDns, inet_ntoa(dwDns2));
- // //SettingsSet(szDns, &SETTINGS_POINTER->Isp.szDns2, sizeof(szDns));
- // NutDnsConfig2(0, 0, dwDns1, dwDns2);
- // }
- }
- static void SetFixedDnsServers(void)
- {
- // u_long dwDns1, dwDns2;
- // char szDns[sizeof(SETTINGS_POINTER->Isp.szDns1)];
- // //SettingsGet(szDns, &SETTINGS_POINTER->Isp.szDns1, sizeof(szDns));
- // if ((dwDns1 = inet_addr(szDns)) == (u_long)-1)
- // {
- // dwDns1 = 0;
- // }
- // //SettingsGet(szDns, &SETTINGS_POINTER->Isp.szDns2, sizeof(szDns));
- // if ((dwDns2 = inet_addr(szDns)) == (u_long)-1)
- // {
- // dwDns2 = 0;
- // }
- // NutDnsConfig2(0, 0, dwDns1, dwDns2);
- }
- static void TryGetDhcp(u_long timeout)
- {
- /*
- * Make sure DHCP is started by setting the
- * *CONFIGURED* IP address to zero.
- * AND erase the previous IP address, just in case
- * our MAC address changed.
- */
- confnet.cdn_cip_addr = 0;
- confnet.cdn_ip_addr = 0;
- confnet.cdn_ip_mask = 0;
- #ifdef NUTDEBUG
- NutTraceTcp(stdout, 1);
- NutTraceHeap(stdout, 0); // doesn't function !!
- NutTraceOs(stdout, 0); // doesn't function !!
- #endif
- /*
- * Start DHCP, and wait for the answer (or timeout)
- */
- LogMsg_P(LOG_DEBUG, PSTR("DHCP client started"));
- if (NutDhcpIfConfig(szEthernetIfName, confnet.cdn_mac, timeout))
- {
- LogMsg_P(LOG_DEBUG, PSTR("No DHCP address retrieved"));
- }
- else
- {
- LogMsg_P(LOG_INFO, PSTR("Ethernet interface %s ready"), inet_ntoa(confnet.cdn_ip_addr));
- #ifdef NUTDEBUG
- NutTraceTcp(stdout, 0);
- NutTraceHeap(stdout, 0);
- NutTraceOs(stdout, 0);
- #endif
- }
- }
- /*!
- * \brief Configures the ethernet interface
- *
- * Send out a DHCP request.
- * An error is returned if no response from a DHCP server
- * was received
- *
- * \param szIfName Name of the device.
- *
- * \return OK if success, TError otherwise
- */
- static INLINE TError NetConfig(CONST char *szIfName)
- {
- // u_long ulMac;
- // u_long ulSerialNumber;
- // u_long ulIpAddress;
- // u_char byTempValue;
- // char szIp[sizeof(SETTINGS_POINTER->Isp.szIp)];
- // LogMsg_P(LOG_DEBUG, PSTR("Configuring ethernet %s"), szIfName);
- // /*
- // * LAN configuration using EEPROM values or DHCP/ARP method.
- // * If it fails, use fixed values.
- // */
- // if (NutNetLoadConfig(szIfName) != 0)
- // {
- // /*
- // * No previous config, ignore
- // */
- // }
- // /*
- // * Override any previously used MAC address by
- // * the one from our own setup
- // *
- // * The MAC address is 00:xx:xx:0y:yy:yy
- // * where x = 4 digits from the IEEE assigned adres
- // * y = 5 digits from our serial number
- // */
- // ulMac = SettingsGetMacIeee();
- // ulMac = __byte_swap4(ulMac) >> 8;
- // memcpy(confnet.cdn_mac, &ulMac, sizeof(confnet.cdn_mac)/2);
- // //ulSerialNumber = SettingsGetSerialnumber();
- // ulSerialNumber = __byte_swap4(ulSerialNumber) >> 8;
- // memcpy(&confnet.cdn_mac[sizeof(confnet.cdn_mac)/2], &ulSerialNumber, sizeof(confnet.cdn_mac)/2);
- // LogMsg_P(LOG_INFO, PSTR("MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x"),
- // confnet.cdn_mac[0],
- // confnet.cdn_mac[1],
- // confnet.cdn_mac[2],
- // confnet.cdn_mac[3],
- // confnet.cdn_mac[4],
- // confnet.cdn_mac[5]);
- // /*
- // * Save the new MAC address
- // */
- // NutNetSaveConfig();
- // /*
- // * Bring up the network.
- // * Use fixed settings if DHCP is disabled.
- // * If the fixed IP address is invalid use DHCP anyway.
- // */
- // //SettingsGet(szIp, &SETTINGS_POINTER->Isp.szIp, sizeof(szIp));
- // ulIpAddress = inet_addr(szIp);
- // if (ulIpAddress == -1)
- // {
- // ulIpAddress = 0;
- // }
- // //SettingsGet(&byTempValue, &SETTINGS_POINTER->Isp.bDhcp, sizeof(byTempValue));
- // if ((byTempValue == 0) &&
- // (ulIpAddress != 0))
- // {
- // /*
- // * Use fixed settings.
- // */
- // LogMsg_P(LOG_INFO, PSTR("Fixed IP address used"));
- // confnet.cdn_cip_addr = inet_addr(szIp);
- // //SettingsGet(szIp, &SETTINGS_POINTER->Isp.szGateway, sizeof(szIp));
- // confnet.cdn_gateway = inet_addr(szIp);
- // if (confnet.cdn_gateway == -1)
- // {
- // confnet.cdn_gateway = 0;
- // }
- // //SettingsGet(szIp, &SETTINGS_POINTER->Isp.szNetmask, sizeof(szIp));
- // confnet.cdn_ip_mask = inet_addr(szIp);
- // if (confnet.cdn_ip_mask == -1)
- // {
- // confnet.cdn_ip_mask = 0;
- // }
- // if (NutNetIfConfig(szIfName, confnet.cdn_mac, confnet.cdn_cip_addr, confnet.cdn_ip_mask) == 0)
- // {
- // NUTDEVICE *dev;
- // /*
- // * Add the default route
- // */
- // if ((dev = NutDeviceLookup(szIfName)) != 0 && dev->dev_type == IFTYP_NET)
- // {
- // NutIpRouteAdd(0, 0, confnet.cdn_gateway, dev);
- // }
- // LogMsg_P(LOG_INFO, PSTR("Ethernet interface %s ready"), inet_ntoa(confnet.cdn_ip_addr));
- // }
- // else
- // {
- // LogMsg_P(LOG_ERR, PSTR("Incorrect static Ip settings"));
- // }
- // SetFixedDnsServers();
- // }
- // else
- // {
- // /*
- // * Use DHCP.
- // */
- // TryGetDhcp(20000L);
- // SetDhcpDnsServers(0,0);
- // }
- return(OK);
- }
- /*!
- * \brief Starts the ethernet interface
- *
- * Waits for DHCP to finish configuring the interface
- * If that doesn't happen the interface is configured
- * using the previous settings.
- *
- * \return OK if success, TError otherwise
- */
- static INLINE TError StartNet(void)
- {
- TError tError = OK;
- u_char byTempValue = 0;
- /*
- * Check if we use DHCP
- */
- //SettingsGet(&byTempValue, &SETTINGS_POINTER->Isp.bDhcp, sizeof(byTempValue));
- if (byTempValue != 0)
- {
- if (NutDhcpIsConfigured() == 0)
- {
- if (NutNetLoadConfig(szEthernetIfName) ||
- NutNetIfConfig(szEthernetIfName, confnet.cdn_mac, confnet.cdn_ip_addr, confnet.cdn_ip_mask))
- {
- LogMsg_P(LOG_ERR, PSTR("No usable network"));
- tError = SESSION_NODHCP_NOEEPROM;
- }
- else
- {
- NUTDEVICE *dev;
- /*
- * Add the default route
- */
- if ((dev = NutDeviceLookup(szEthernetIfName)) != 0 && dev->dev_type == IFTYP_NET)
- {
- NutIpRouteAdd(0, 0, confnet.cdn_gateway, dev);
- }
- LogMsg_P(LOG_WARNING, PSTR("No DHCP response, trying previous network settings..."));
- }
- /*
- * Set current DNS servers
- */
- SetDhcpDnsServers(0, 0);
- }
- }
- /*
- * Stop all debugging
- */
- #ifdef NUTDEBUG
- NutTraceTcp(stdout, 0);
- NutTraceHeap(stdout, 0);
- NutTraceOs(stdout, 0);
- #endif
- return(tError);
- }
- /*--------------------------------------------------------------------------*/
- /* Global functions */
- /*--------------------------------------------------------------------------*/
- /*!
- * \brief Initialises this module
- *
- * \note With NutOS 3.2.1 it is not possible
- * to have both a ppp and eth0 device
- * registered at the same time and still
- * have DHCP working.
- * So switching interface is currently
- * a reason for reboot!
- *
- * \return OK if success, TError otherwise
- */
- TError SessionInit(void)
- {
- TError tError = OK;
- /*
- * Initialise globals
- */
- g_pUart = 0;
- g_tError = OK;
- strncpy_P(szEthernetIfName, PSTR("eth0"), sizeof(szEthernetIfName));
- /*
- * Create a unique hostname from our serial number
- * Save the hostname in NutOS
- */
- //sprintf_P(confos.hostname, PSTR("%.10s%5.5lX"), VersionGetAppProductName(), SettingsGetSerialnumber());
- NutSaveConfig();
- /*
- * Try to bring up the selected
- * interface. Error if none configured
- */
- /*
- * Register Realtek controller
- */
- if (NutRegisterDevice(&devEth0, ETH0_BASE, ETH0_IRQ))
- {
- LogMsg_P(LOG_EMERG, PSTR("Registering ethernet failed"));
- tError = SESSION_NODEVICE;
- }
- else
- {
- /* Let the chip settle down from init */
- NutSleep(1000);
- }
- /*
- * If we have an ethernet interface start it on init
- */
- if (tError == OK)
- {
- tError = NetConfig(szEthernetIfName);
- }
- g_tError = tError;
- return(tError);
- }
- /*!
- * \brief Opens a session
- *
- * If this succeeds you can start communicating
- * to the Internet
- *
- * \return OK if success, TError otherwise
- */
- TError SessionOpen(void)
- {
- TError tError = OK;
- g_tError = OK;
- /*
- * Try to bring up the selected
- * interface. Error if none configured
- */
- tError = StartNet();
- /*
- * Display our network settings
- */
- if (tError == OK)
- {
- u_long ulPrimaryDNS;
- u_long ulSecondaryDNS;
- /*
- * Display our IP settings.
- */
- LogMsg_P(LOG_INFO, PSTR(" Local IP: %s"), inet_ntoa(confnet.cdn_ip_addr));
- LogMsg_P(LOG_INFO, PSTR("Gateway IP: %s"), inet_ntoa(confnet.cdn_gateway));
- NutGetDnsServers(&ulPrimaryDNS, &ulSecondaryDNS);
- LogMsg_P(LOG_INFO, PSTR(" Pri. DNS: %s"), inet_ntoa(ulPrimaryDNS));
- LogMsg_P(LOG_INFO, PSTR(" Sec. DNS: %s"), inet_ntoa(ulSecondaryDNS));
- }
- g_tError = tError;
- return(tError);
- }
- /*!
- * \brief Return the session status
- *
- * Call to check the status of a session.
- *
- * \return see above
- */
- TError SessionStatus(void)
- {
- return(g_tError);
- }
- /*!
- * \brief Stop the session
- *
- * \return OK if success, TError otherwise
- */
- TError SessionClose(void)
- {
- g_tError = USER_ABORT;
- return(OK);
- }
- /*@}*/
|