cake
/
libcasio
Archived
1
1
Fork 0
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.
libcasio/lib/link/open.c

122 lines
3.3 KiB
C

/* ****************************************************************************
* link/open.c -- link utilities.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
* libcasio 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 3.0 of the License,
* or (at your option) any later version.
*
* libcasio 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 libcasio; if not, see <http://www.gnu.org/licenses/>.
*
* The `casio_open_link` is the function that really initializes
* the communication using the given stream (once the stream is initialized).
* It went public recently to give the possibility to the user to make a
* custom stream, probably for its platform.
* ************************************************************************* */
#include "link.h"
/**
* casio_open_link:
* Open a link handle.
*
* @arg h the handle to create
* @arg flags the flags.
* @arg stream the stream to use.
* @return the error (0 if ok)
*/
int CASIO_EXPORT casio_open_link(casio_link_t **h, unsigned long flags,
casio_stream_t *stream)
{
int err = 0;
casio_link_t *handle;
/* Allocate the handle. */
msg((ll_info, "Allocating and building the link handle!"));
*h = NULL;
if (!(*h = casio_alloc(1, sizeof(casio_link_t)))) {
err = casio_error_alloc;
goto fail;
}
/* Initialize the handle. */
handle = *h;
memset(handle, 0, sizeof(casio_link_t)); /* important! */
casio_init_lock(&handle->casio_link_lock);
handle->casio_link_stream = stream;
/* Initialize the flags. */
handle->casio_link_flags = 0;
if (flags & CASIO_LINKFLAG_ACTIVE)
handle->casio_link_flags |= casio_linkflag_active;
if (flags & CASIO_LINKFLAG_TERM)
handle->casio_link_flags |= casio_linkflag_term;
if (flags & CASIO_LINKFLAG_CHECK)
handle->casio_link_flags |= casio_linkflag_check;
if (~flags & CASIO_LINKFLAG_NODISC)
handle->casio_link_flags |= casio_linkflag_disc;
msg((ll_info, "[Options] Active: %s",
flags & CASIO_LINKFLAG_ACTIVE ? "yes" : "no"));
msg((ll_info, "[Options] Check: %s",
flags & CASIO_LINKFLAG_CHECK ? "yes" : "no"));
ifmsg(flags & CASIO_LINKFLAG_CHECK, (ll_info,
"[Options] Make discovery: %s",
flags & CASIO_LINKFLAG_NODISC ? "no" : "yes"));
msg((ll_info, "[Options] Terminate: %s",
flags & CASIO_LINKFLAG_TERM ? "yes" : "no"));
/* If active, start. */
err = casio_seven_start(handle);
if (err)
goto fail;
return (0);
fail:
if (*h)
casio_close_link(*h);
*h = NULL;
return (err);
}
/**
* casio_close_link:
* De-initialize a link handle..
*
* @arg handle the handle to free.
*/
void CASIO_EXPORT casio_close_link(casio_link_t *handle)
{
/* Check if handle is already freed. */
if (!handle)
return ;
msg((ll_info, "Let's end that mess."));
/* End communication -- FIXME: check error? */
casio_seven_end(handle);
/* Close stream. */
casio_close(handle->casio_link_stream);
/* Free the handle. */
msg((ll_info, "freeing the handle!"));
casio_free(handle);
}