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/protocol/seven/init.c

108 lines
3.1 KiB
C

/* *****************************************************************************
* protocol/seven/init.c -- initialize a P7 handle.
* 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>
#define checkflags (p7_intflag_active | p7_intflag_check)
#define endflags (p7_intflag_active | p7_intflag_term)
/**
* p7_seven_start:
* Start the communication.
*
* @arg handle the handle.
* @return the error, if any.
*/
int p7_seven_start(p7_handle_t *handle)
{
if ((handle->_flags & checkflags) != checkflags)
return (0);
/* start */
int err = 0;
log_info("so we're active? let's do what active people do then!");
/* send initial check */
if (handle->_flags & p7_intflag_check) {
log_info("sending initial check packet");
if ((err = p7_seven_send_ini_check(handle))) {
log_fatal("couldn't send check/didn't receive answer");
return (err);
} else if (response.type != p7_seven_type_ack) {
log_fatal("received packet wasn't ack, ask your dentist");
return (p7_error_unknown);
}
#if LOGLEVEL <= ll_info
} else {
log_info("skipping initial check, we suppose already initialized");
#endif
}
/* discover environment */
log_info("checkin' up da environment");
if ((err = p7_seven_send_cmdsys_getinfo(handle))) {
log_fatal("couldn't send env query/receive answer");
return (err);
} else if (response.type != p7_seven_type_ack || !response.extended) {
log_fatal("response to env query wasn't an extended ack packet");
return (p7_error_unknown);
}
/* save server */
handle->_server = response.info;
/* get environment based on sent hardware id */
handle->_env = p7_get_env(handle->_server.hwid);
log_info("environment is '%s'", handle->_env->name);
/* no error */
return (0);
}
/**
* p7_seven_end:
* End the communication.
*
* @arg handle the handle.
* @return the error code (0 if none).
*/
int p7_seven_end(p7_handle_t *handle)
{
if ((handle->_flags & endflags) != endflags
|| ~handle->_flags & p7_intflag_terminated)
return (0);
/* send the termination packet */
log_info("we were active, send sending terminate packet");
int err = p7_seven_send_term(handle);
if (err) {
log_warn("couldn't terminate, already disconnected?");
return (err);
}
/* check ack */
if (response.type != p7_seven_type_ack) {
log_warn("answer to terminate packet wasn't ack!");
return (p7_error_unknown);
}
/* no error! */
return (0);
}