aboutsummaryrefslogtreecommitdiffstats
path: root/src/nmt/transaction.c
blob: ae35f979fe42a855edcca4b932592f9cb9f626c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* NMT transaction handling
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "../libsample/sample.h"
#include "../common/debug.h"
#include "nmt.h"
#include "transaction.h"

static transaction_t *trans_list = NULL;
static void transaction_timeout(struct timer *timer);

/* link transaction to list */
static void link_transaction(transaction_t *trans)
{
	transaction_t **transp;

	/* attach to end of list, so first transaction is served first */
	PDEBUG(DTRANS, DEBUG_DEBUG, "Linking transaction %p to list\n", trans);
	trans->next = NULL;
	transp = &trans_list;
	while (*transp)
		transp = &((*transp)->next);
	*transp = trans;
}

/* unlink transaction from list */
static void unlink_transaction(transaction_t *trans)
{
	transaction_t **transp;
	sender_t *sender;
	nmt_t *nmt;

	/* unlink */
	PDEBUG(DTRANS, DEBUG_DEBUG, "Unlinking transaction %p from list\n", trans);
	transp = &trans_list;
	while (*transp && *transp != trans)
		transp = &((*transp)->next);
	if (!(*transp)) {
		PDEBUG(DTRANS, DEBUG_ERROR, "Transaction not in list, please fix!!\n");
		abort();
	}
	*transp = trans->next;
	trans->next = NULL;

	/* unbind from channel */
	trans->nmt = NULL;
	for (sender = sender_head; sender; sender = sender->next) {
		nmt = (nmt_t *)sender;
		if (nmt->trans == trans)
			nmt_go_idle(nmt);
	}
}

/* create transaction */
transaction_t *create_transaction(struct nmt_subscriber *subscr)
{
	transaction_t *trans;

	trans = calloc(1, sizeof(*trans));
	if (!trans) {
		PDEBUG(DTRANS, DEBUG_ERROR, "No memory!\n");
		return NULL;
	}

	timer_init(&trans->timer, transaction_timeout, trans);

	memcpy(&trans->subscriber, subscr, sizeof(struct nmt_subscriber));

	PDEBUG(DTRANS, DEBUG_INFO, "Created transaction for subscriber '%c,%s'\n", subscr->country, subscr->number);

	link_transaction(trans);

	return trans;
}

/* destroy transaction */
void destroy_transaction(transaction_t *trans)
{
	unlink_transaction(trans);

	PDEBUG(DTRANS, DEBUG_INFO, "Destroying transaction for subscriber '%c,%s'\n", trans->subscriber.country, trans->subscriber.number);

	timer_exit(&trans->timer);

	free(trans);
}

/* Timeout handling */
static void transaction_timeout(struct timer *timer)
{
	transaction_t *trans = (transaction_t *)timer->priv;

	timeout_mt_paging(trans);
}

transaction_t *get_transaction_by_callref(int callref)
{
	transaction_t *trans;

	trans = trans_list;
	while (trans) {
		if (trans->callref == callref)
			break;
		trans = trans->next;
	}

	return trans;
}

transaction_t *get_transaction_by_number(struct nmt_subscriber *subscr)
{
	transaction_t *trans;

	trans = trans_list;
	while (trans) {
		if (trans->subscriber.country == subscr->country
		 && !strcmp(trans->subscriber.number, subscr->number))
			break;
		trans = trans->next;
	}

	return trans;
}