94 lines
2.8 KiB
C
94 lines
2.8 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* streams/tty.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2017/01/04 15:01:48 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
#include <string.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* *nix serial device initialization and callbacks */
|
|
/* ************************************************************************** */
|
|
#ifdef __linux__
|
|
# include <fcntl.h>
|
|
# include <errno.h>
|
|
|
|
/**
|
|
* p7_cinit:
|
|
* Initialize libp7 with char device.
|
|
*
|
|
* @arg handle the handle to create
|
|
* @arg flags the flags.
|
|
* @arg path path to char device.
|
|
* @return the error (0 if ok)
|
|
*/
|
|
|
|
int p7_cinit(p7_handle_t **handle, unsigned int flags, const char *path)
|
|
{
|
|
int fd = -1;
|
|
|
|
/* if no path, as there are no native drivers, we're fucked */
|
|
if (!path) return (p7_error_nocalc);
|
|
|
|
/* open the stream */
|
|
fd = open(path, O_RDWR | O_NOCTTY);
|
|
if (fd < 0) switch (errno) {
|
|
/* no such device */
|
|
case ENODEV: case ENOENT: case ENXIO:
|
|
case EPIPE: case ESPIPE:
|
|
logr_error("couldn't open calculator");
|
|
return (p7_error_nocalc);
|
|
|
|
/* no access */
|
|
case EACCES:
|
|
logr_error("permission denied");
|
|
return (p7_error_noaccess);
|
|
|
|
/* default */
|
|
default:
|
|
logr_error("unknown error: %s (0x%X)", strerror(errno), errno);
|
|
return (p7_error_unknown);
|
|
}
|
|
|
|
/* check if we have got the thing */
|
|
if (!path) return (p7_error_nocalc);
|
|
if (fd < 0) switch (errno) {
|
|
case ENOENT: return (p7_error_nocalc);
|
|
case EACCES: return (p7_error_noaccess);
|
|
default: return (p7_error_nocalc);
|
|
}
|
|
|
|
/* init to real */
|
|
return (p7_fdinit(handle, flags, NULL, fd, fd));
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Use a character device on other systems (placebo) */
|
|
/* ************************************************************************** */
|
|
#else
|
|
/**
|
|
* p7_cinit:
|
|
* Initialize libp7 with char device. Placebo.
|
|
*
|
|
* @arg handle the handle to create
|
|
* @arg flags the flags
|
|
* @arg path path to char device.
|
|
* @arg tries number of tries (0 means default).
|
|
* @return the error (0 if ok)
|
|
*/
|
|
|
|
int p7_cinit(p7_handle_t **handle, unsigned int flags,
|
|
const char *path)
|
|
{
|
|
(void)handle;
|
|
(void)flags;
|
|
(void)path;
|
|
return (p7_error_nocalc);
|
|
}
|
|
|
|
#endif
|