cake
/
libp7
Archived
1
0
Fork 1
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libp7/src/core/init.c

119 lines
2.9 KiB
C
Raw Normal View History

2016-08-17 00:04:19 +02:00
/* ************************************************************************** */
/* _____ _ */
/* core/init.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
2016-08-17 00:04:19 +02:00
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2017/01/19 12:25:05 |___/ */
2016-08-17 00:04:19 +02:00
/* */
/* ************************************************************************** */
2016-10-05 19:08:14 +02:00
#include <libp7/internals.h>
#include <libp7/internals/sleep.h>
#include <stdio.h>
2016-08-17 00:04:19 +02:00
/**
2017-01-05 15:34:14 +01:00
* p7_init:
* Initialize and automatically find a USB device.
*
2017-01-05 15:34:14 +01:00
* @arg handle the handle.
2017-01-26 14:25:43 +01:00
* @arg flags the flags.
2017-01-05 15:34:14 +01:00
* @return the error code (0 if ok).
*/
2017-01-26 14:25:43 +01:00
int p7_init(p7_handle_t **handle, unsigned int flags)
{
2017-02-07 17:54:11 +01:00
#ifdef P7_NOUSB
(void)handle;
(void)flags;
return (p7_error_nocalc);
#else
2017-01-12 10:03:45 +01:00
int err = p7_error_nocalc;
2017-01-05 15:34:14 +01:00
int tries = INIT_TRIES;
int failed = 0;
do /* a barrel roll */ {
if (failed) {
2017-01-13 13:33:09 +01:00
logr_info("Trying again in one second.");
2017-01-05 15:34:14 +01:00
sleep(1);
}
/* platform-specific communication methods */
2017-02-07 17:54:11 +01:00
# if !defined(P7_DISABLED_STREAMS)
err = p7_fdinit(handle, flags, NULL, -1, -1);
# elif !defined(P7_DISABLED_WINDOWS)
2017-01-26 14:25:43 +01:00
err = p7_winit(handle, flags, NULL);
2017-02-07 17:54:11 +01:00
# endif
2017-01-12 10:03:45 +01:00
/* check error */
2017-01-05 15:34:14 +01:00
if (err != p7_error_nocalc)
return (err);
/* libusb communication methods - more or less platform-independent */
2017-02-07 17:54:11 +01:00
# if !defined(P7_DISABLED_LIBUSB)
2017-01-13 13:33:09 +01:00
logr_info("Looking for general libusb devices");
2017-01-26 14:25:43 +01:00
err = p7_libusbinit(handle, flags);
2017-01-05 15:34:14 +01:00
if (err != p7_error_nocalc)
return (err);
2017-02-07 17:54:11 +01:00
# endif
2017-01-05 15:34:14 +01:00
/* wait a little */
2017-01-13 13:33:09 +01:00
logr_error("didn't find the calculator!");
2017-01-05 15:34:14 +01:00
failed = 1;
} while (--tries);
/* no found calc, by the look of it. */
return (p7_error_nocalc);
2017-02-07 17:54:11 +01:00
#endif
}
/**
* p7_cominit:
* Initialize on a COM port.
*
* @arg handle the handle.
2017-01-26 14:25:43 +01:00
* @arg flags the flags.
* @arg com the COM port ID.
* @return the error code (0 if ok).
*/
2017-01-26 14:25:43 +01:00
int p7_cominit(p7_handle_t **handle, unsigned int flags, int com)
{
2017-02-07 17:54:11 +01:00
#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) {
2017-01-13 13:33:09 +01:00
logr_info("Trying again in one second.");
sleep(1);
}
/* platform-specific communication methods */
2017-02-07 17:54:11 +01:00
# if !defined(P7_DISABLED_STREAMS)
err = p7_fdcominit(handle, flags, com);
# elif !defined(P7_DISABLED_WINDOWS)
err = p7_wcominit(handle, flags, com);
# endif
2017-01-12 10:03:45 +01:00
/* check error */
if (err != p7_error_nocalc)
return (err);
failed = 1;
} while (--tries);
/* finish. */
return (p7_error_nocalc);
#endif
}