tapasco_init.c 2.78 KB
Newer Older
1
2
3
//
// Copyright (C) 2014 Jens Korinth, TU Darmstadt
//
4
// This file is part of Tapasco (TPC).
5
//
6
// Tapasco is free software: you can redistribute it and/or modify
7
8
9
10
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
11
// Tapasco is distributed in the hope that it will be useful,
12
13
14
15
16
// 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
17
// along with Tapasco.  If not, see <http://www.gnu.org/licenses/>.
18
//
19
//! @file	tapasco-sim.c
20
21
22
23
24
25
26
//! @brief	TPC API intialization implementation for the Zynq platform.
//! @authors	J. Korinth, TU Darmstadt (jk@esa.cs.tu-darmstadt.de)
//!
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
27
28
29
30
31
#include <tapasco_api.h>
#include <tapasco_errors.h>
#include <tapasco_jobs.h>
#include <tapasco_functions.h>
#include <tapasco_logging.h>
32
#include <platform.h>
33
34
35
36
37
38
#include <platform_errors.h>

// declare logging exit for flushing
// TODO: is it possible to handle this more nicely?
extern void platform_logging_exit(void);

39
struct tapasco_ctx {
40
41
42
43
44
	int conn_sock;
};

static void _flush_logs_on_sigint(int sig) {
	LOG(LALL_INIT, "caught SIGINT, flushing logs and leaving the sinking ship");
45
	tapasco_logging_exit();
46
47
48
49
	platform_logging_exit();
	exit(sig);
}

50
static int _tapasco_install_sigint_handler() {
51
52
53
54
55
56
	struct sigaction act;
	memset(&act, '\0', sizeof(act));
	act.sa_handler = &_flush_logs_on_sigint;
	return sigaction(SIGINT, &act, NULL);
}

57
58
59
60
61
62
tapasco_res_t _tapasco_init(const char *const version, tapasco_ctx_t **pctx) {
	tapasco_logging_init();
	LOG(LALL_INIT, "version: %s, expected version: %s", tapasco_version(), version);
	if (tapasco_check_version(version) != TAPASCO_SUCCESS) {
		ERR("version mismatch: found %s, expected %s", tapasco_version(), version);
		return TAPASCO_ERR_VERSION_MISMATCH;
63
64
	}

65
	*pctx = (tapasco_ctx_t *)malloc(sizeof(struct tapasco_ctx));
66
67
68
	platform_res_t pr;
	if ((pr = platform_init()) != PLATFORM_SUCCESS) {
		ERR("Platform error: %s", platform_strerror(pr));
69
		return TAPASCO_ERR_PLATFORM_FAILURE;
70
71
72
	}

	// install signal handler
73
	if (_tapasco_install_sigint_handler()) {
74
75
76
		ERR("could not install signal handler: %s", strerror(errno));
		platform_deinit();
		free(pctx);
77
		return TAPASCO_FAILURE;
78
	}
79
80
	LOG(LALL_INIT, "tapasco initialization done");
	if (*pctx) return TAPASCO_SUCCESS;
81

82
	return TAPASCO_FAILURE;
83
84
}

85
void tapasco_deinit(tapasco_ctx_t *ctx) {
86
87
88
89
90
91
	LOG(LALL_INIT, "shutting down TPC");
	if (ctx) {
		platform_deinit();
		free(ctx);
	}
	LOG(LALL_INIT, "all's well that ends well, bye");
92
	tapasco_logging_exit();
93
94
}