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/usage/setlink.c

78 lines
2.5 KiB
C

/* *****************************************************************************
* usage/setlink.c -- set link settings.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libp7.
* libp7 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.
*
* libp7 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 libp7; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libp7/internals.h>
#include <libp7/internals/sleep.h>
/**
* p7_setlink:
* Set link settings.
*
* @arg handle the libp7 handle.
* @arg settings the settings to set.
* @return the error code.
*/
p7_define_ufunc(p7_setlink, p7_attrs_setlink,
p7_handle_t *handle, const p7_streamsettings_t *settings)
{
int err;
/* check settings */
p7_streamsettings_t localsettings;
if (!settings) {
p7_initcomm(&localsettings);
settings = &localsettings;
}
/* get the information */
unsigned int speed = settings->p7_streamsettings_speed;
int stopbits = (settings->p7_streamsettings_flags & P7_TWOSTOPBITS) + 1;
int parity = (~settings->p7_streamsettings_flags & P7_PARENB) ? 0
: (settings->p7_streamsettings_flags & P7_PARODD) ? 1 : 2;
/* make checks */
chk_handle(handle);
chk_seven(handle);
chk_active(handle);
/* send command */
log_info("sending command");
if ((err = p7_seven_send_cmdsys_setlink(handle, speed, parity, stopbits))) {
log_fatal("could not set command/receive answer");
return (err);
} else if (response.p7_seven_packet_type != p7_seven_type_ack) {
err = p7_error_unknown;
goto end;
}
/* set communication properties */
log_info("updating communication settings accordingly");
handle->_settings.p7_streamsettings_speed = speed;
handle->_settings.p7_streamsettings_flags &=
~(P7_STOPBITSMASK | P7_PARMASK);
handle->_settings.p7_streamsettings_flags |=
settings->p7_streamsettings_flags & (P7_STOPBITSMASK | P7_PARMASK);
p7_setcomm(&handle->_stream, &handle->_settings);
end:
/* wait for the calculator to set its serial interface, and we're done */
p7_sleep(100);
return (0);
}