119 lines
2.9 KiB
C
119 lines
2.9 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* core/init.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2017/01/19 12:25:05 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
#include <libp7/internals/sleep.h>
|
|
#include <stdio.h>
|
|
|
|
/**
|
|
* p7_init:
|
|
* Initialize and automatically find a USB device.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg flags the flags.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int p7_init(p7_handle_t **handle, unsigned int flags)
|
|
{
|
|
#ifdef P7_NOUSB
|
|
(void)handle;
|
|
(void)flags;
|
|
return (p7_error_nocalc);
|
|
#else
|
|
|
|
int err = p7_error_nocalc;
|
|
int tries = INIT_TRIES;
|
|
int failed = 0;
|
|
|
|
do /* a barrel roll */ {
|
|
if (failed) {
|
|
logr_info("Trying again in one second.");
|
|
sleep(1);
|
|
}
|
|
|
|
/* platform-specific communication methods */
|
|
# if !defined(P7_DISABLED_STREAMS)
|
|
err = p7_fdinit(handle, flags, NULL, -1, -1);
|
|
# elif !defined(P7_DISABLED_WINDOWS)
|
|
err = p7_winit(handle, flags, NULL);
|
|
# endif
|
|
/* check error */
|
|
if (err != p7_error_nocalc)
|
|
return (err);
|
|
|
|
/* libusb communication methods - more or less platform-independent */
|
|
# if !defined(P7_DISABLED_LIBUSB)
|
|
logr_info("Looking for general libusb devices");
|
|
err = p7_libusbinit(handle, flags);
|
|
if (err != p7_error_nocalc)
|
|
return (err);
|
|
# endif
|
|
|
|
/* wait a little */
|
|
logr_error("didn't find the calculator!");
|
|
failed = 1;
|
|
} while (--tries);
|
|
|
|
/* no found calc, by the look of it. */
|
|
return (p7_error_nocalc);
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* p7_cominit:
|
|
* Initialize on a COM port.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg flags the flags.
|
|
* @arg com the COM port ID.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int p7_cominit(p7_handle_t **handle, unsigned int flags, int com)
|
|
{
|
|
#ifdef P7_NOSERIAL
|
|
(void)handle;
|
|
(void)active;
|
|
(void)check;
|
|
(void)com;
|
|
return (p7_error_nocalc);
|
|
#else
|
|
int err, tries = INIT_TRIES;
|
|
int failed = 0;
|
|
|
|
/* check the com port number */
|
|
if (com < 1 || com > 20)
|
|
return (p7_error_com);
|
|
|
|
do /* the flop */ {
|
|
if (failed) {
|
|
logr_info("Trying again in one second.");
|
|
sleep(1);
|
|
}
|
|
|
|
/* platform-specific communication methods */
|
|
# if !defined(P7_DISABLED_STREAMS)
|
|
err = p7_fdcominit(handle, flags, com);
|
|
# elif !defined(P7_DISABLED_WINDOWS)
|
|
err = p7_wcominit(handle, flags, com);
|
|
# endif
|
|
|
|
/* check error */
|
|
if (err != p7_error_nocalc)
|
|
return (err);
|
|
|
|
failed = 1;
|
|
} while (--tries);
|
|
|
|
/* finish. */
|
|
return (p7_error_nocalc);
|
|
#endif
|
|
}
|