/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */

/*
 * ----------------------------------------------------------------------------------
 * The code in this file is copied (with a slight modifications) from:
 *     lwIPDhcpServer-NonOS.cpp - DHCP server wrapper
 * ----------------------------------------------------------------------------------
 * Copyright (c) 2020 esp8266 arduino. All rights reserved.
 *
 * This file is part of the esp8266 core for Arduino environment.
 *
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */


#include <lwip/netif.h>

#include "LwipDhcpServer.h"


extern netif netif_git[2];


// Global DHCP instance for softAP interface
eCxx::NodeMCU::DHCPServer dhcpSoftAP( &netif_git[SOFTAP_IF] );


extern "C" {

    void dhcps_start(struct ip_info* /*info*/, netif* /*apnetif*/)
    {
        // The ESP interface 'apnetif' is replaced replaced by lwip2's
        // netif_git[SOFTAP_IF] interface in constructor

        // Emulate SDK in DHCPServer class before trying to change legacy behavior
        //
        // `fw_has_started_softap_dhcps` will be read in DHCPServer::DHCPServer
        // which is called when C++ ctors are initialized, specifically dhcpSoftAP
        // intialized with AP interface number above.
        fw_has_started_softap_dhcps = 1;
    }

    void dhcps_stop()
    { dhcpSoftAP.end(); }

} // extern "C"
