// $Id: ucon.c,v 1.46 2013/03/22 17:37:17 lpoulsen Exp $
// Copyright (C) 2001 Wi_LAN Technologies, Inc
//
// $Log: ucon.c,v $
// Revision 1.46  2013/03/22 17:37:17  lpoulsen
// Allow "econ -r name" to make one connection with the named radio
//     without ever entering the Econsole> command prompt.
//
// Revision 1.45  2012/05/08 18:24:00  lpoulsen
// Compile all modules with -pthread on Linux
// Clean up all compiler warnings
// Count packets in OUT OF MEMORY message
//
// Revision 1.44  2011/12/14 23:35:09  lpoulsen
// At then end of DISCOVER, say "Use CONNECT"
//
// Revision 1.43  2011/08/25 22:32:26  lpoulsen
// Updates to allow Linux build
//
// Revision 1.42  2011/05/06 22:15:14  lpoulsen
// Preparing for ECON 04.07 release
//
// Revision 1.41  2011/03/22 00:26:10  lpoulsen
// Resolve sequence errors on very weak links
// Resolve hangs when Ctrl-C between "dir" and "download"
// Resolve conflicting build date information
//
// Revision 1.40  2011/02/22 00:02:23  lpoulsen
// Show device table after F4 disconnect
// Clear device table before new discovery
// Use device type = 0x8086 for PCs
//
// Revision 1.39  2011/02/04 22:29:07  lpoulsen
// Many little fixes.
//
// Revision 1.38  2011/01/22 01:29:05  lpoulsen
// Release candidate:
// - allow "show first=1 max=45" to support large systems
// - allow "sort by=distance" to support "connect all reboot"
// - improve download efficiency
//
// Revision 1.37  2011/01/11 00:51:13  lpoulsen
// When operating in batch mode,
// - print progress after each session (2 of 5)
// - save session result code to device table to be printed with "show"
//
// Revision 1.36  2010/12/22 17:36:34  lpoulsen
// Select/Deselect list of ranges
// Expand to up to 511 radios
// Protocol fixes
//
// Revision 1.35  2008/08/06 22:48:16  lpoulsen
// "Nuno mode" - F4 prompts for reselect instead of Econsole>
//
// Revision 1.34  2008/08/05 23:58:37  lpoulsen
// User interface updates.
//
// Revision 1.33  2008/07/26 02:00:48  lpoulsen
// Econ protocol version 4 - prompt bit
//
// Revision 1.32  2008/07/16 00:00:28  lpoulsen
// Add deselect command.
//
// Revision 1.31  2008/07/11 20:14:10  lpoulsen
// Resolve many compiler warnings on Linux
//
// Revision 1.30  2008/07/11 17:10:27  lpoulsen
// Resolve compiler warnings.
//
// Revision 1.29  2008/07/11 16:28:46  lpoulsen
// Econ 4.00 release candidate supports UPDATE and SELECT commands.
//
// Revision 1.28  2008/06/10 00:06:10  lpoulsen
// New -X option is now working
// Updated -I and -O implementation
//
// Revision 1.27  2008/05/31 00:35:09  lpoulsen
// Econ 4.x preview - now with command shell
//
// Revision 1.26  2008/05/10 01:17:37  lpoulsen
// Making econ -x filename work (not yet complete)
//
// Revision 1.25  2008/04/26 00:48:25  lpoulsen
// Rename selectedAdapterId global var to option_C
//
// Revision 1.24  2006/02/17 21:48:10  lpoulsen
// Make sure SendThread is not looping.
// Clean up some formatting add comments.
//
// Revision 1.23  2005/06/23 00:13:30  lpoulsen
// Reduce dependency on pthread.h implementation specifics.
//
// Revision 1.22  2004/07/13 18:37:22  lpoulsen
// Fix bug where F3 or F4 did not send CLOSE_REQUEST
//
// Revision 1.21  2003/12/23 04:35:42  lpoulsen
// EConsole protocol version 2 support.
// Respond to IDENTIFY_REQUEST (version 1 and version 2 format) with
//      version 2 format IDENTIFY_RESPONSE.
//
// Revision 1.20  2003/12/17 18:19:08  lpoulsen
// Respond to IDENTIFY_REQUEST (to support "show econ" on radio/mux)
//
// Revision 1.19  2003/08/25 20:23:49  lpoulsen
// Replace get_next_packet() with call to adapter_receive()
//
// Revision 1.18  2003/08/25 20:16:46  lpoulsen
// Clean up exit for FileThread ...
//
// Revision 1.17  2003/08/21 23:46:51  lpoulsen
// Improved handling of -r option to allow selection by name
// Miscellaneous minor cleanup
//
// Revision 1.16  2003/08/19 03:08:25  lpoulsen
// Remove many compiler warnings.
//
// Revision 1.15  2003/03/11 01:14:13  lpoulsen
// Correct compile errors found in Calgary.
//
// Revision 1.14  2002/10/16 16:59:47  mourlouk
// Improved way of retrieving MAC addresses - VAL support
//
// Revision 1.12  2002/05/20 19:14:12  lpoulsen
// Improve readability.
//
// Revision 1.11  2002/05/15 00:22:16  mourlouk
// Ok, finished with the linux compilation
//
// Revision 1.10  2002/05/14 23:44:01  mourlouk
// more minor changes ...
//
// Revision 1.9  2002/04/19 19:04:44  mourlouk
// Release 1.15 - It fixes a bug that was introduced in version 1.13
//    (econ unable to read properly multiple MAC addresses)
//
// Revision 1.7  2001/11/30 19:27:46  lpoulsen
// Allow "econ -c 1" to specify adapter #1 on command line.
//
// Revision 1.6  2001/11/12 23:06:48  mourlouk
// making happy compiler (II)
//
// Revision 1.5  2001/11/07 02:47:25  lpoulsen
// Running econ on linux:
// - make the executable name be "econ"
// - remove a few compiler warnings
//
// Revision 1.4  2001/11/07 00:02:41  mourlouk
// Fixed a small bug on Linux client
// - now if an error occurs, and we return to Linux environment,
//   everything is normal
//
// Revision 1.3  2001/10/31 17:38:03  lpoulsen
// Generate version display from CVS tag.
//
// Revision 1.2  2001/10/30 01:07:45  lpoulsen
// Add "module ID" statement, audit trail generator and copyright line
// Remove "win_or_linux.h" and use a -D in Linux makefile instead.
//
//

#include "afartype.h"
#ifdef WIN32
#include <winsock.h>
#endif
#include <time.h>
#include <locale.h>
#include "epacket.h"
#include "util.h"
#include "adapter.h"
#include "cio.h"
#include "parser.h"
#include "ucon.h"

extern void start_all_threads();
extern void wait_for_all_threads_to_terminate();
extern char *fmt_session_result(int);
extern cmd_ret_t econ_discover(void *, void *);

// -- Global data

uint32           debug_flags;
int              debug_level;
enum econ_phases econ_phase = ECON_STARTING;
boolean          show_progress = TRUE;

int      option_C = -1;
char    *option_I = NULL;
boolean  option_K = false;
char    *option_O = NULL;
char    *option_R = NULL;
boolean  option_Z = FALSE;

// -- Local data

boolean exiting        = FALSE;
int  session_result = 0;

ringbuf_256 econ_outq =
{0, 0,
 {0}};

int
main(int argc, char **argv)
{
    process_command_arguments(argc, argv);
    show_logo();

    //** select network adapter
    option_C = get_adapters();

    if (option_C < 0)
        terminate(100, INVALID_ADAPTER);

   if (debug_level)
        printf("Locale: %s\n", setlocale(LC_ALL, NULL));

    //** initialize econ
    init_econ();

    // the normal discovery & session establishment phase
    econ_discover(NULL,NULL);

    cmd_loop("Econsole");
    close_econ();
    exit(0);
}
