summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/layer1/tdma_sched.c
blob: 56174a904a8bcca46c217cb7bd1ff4f44278c99a (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* TDMA Scheduler Implementation */

/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <debug.h>
#include <gsm.h>

#include <layer1/tdma_sched.h>
#include <layer1/sync.h>

#include <calypso/dsp.h>

/* dummy function to mark end of set */
void tdma_end_set(uint16_t p1, uint16_t p2)
{

}

static uint8_t wrap_bucket(uint8_t offset)
{
	uint16_t bucket;

	bucket = (l1s.tdma_sched.cur_bucket + offset)
					% ARRAY_SIZE(l1s.tdma_sched.bucket);

	return bucket;
}

/* Schedule an item at 'frame_offset' TDMA frames in the future */
int tdma_schedule(uint8_t frame_offset, tdma_sched_cb *cb, uint16_t p1, uint16_t p2)
{
	struct tdma_scheduler *sched = &l1s.tdma_sched;
	uint8_t bucket_nr = wrap_bucket(frame_offset);
	struct tdma_sched_bucket *bucket = &sched->bucket[bucket_nr];
	struct tdma_sched_item *sched_item;

	if (bucket->num_items >= ARRAY_SIZE(bucket->item)) {
		puts("tdma_schedule bucket overflow\n");
		return -1;
	}

	sched_item = &bucket->item[bucket->num_items++];

	sched_item->cb = cb;
	sched_item->p1 = p1;
	sched_item->p2 = p2;

	return 0;
}

/* Schedule a set of items starting from 'frame_offset' TDMA frames in the future */
int tdma_schedule_set(uint8_t frame_offset, const struct tdma_sched_item *item_set)
{
	struct tdma_scheduler *sched = &l1s.tdma_sched;
	uint8_t bucket_nr = wrap_bucket(frame_offset);
	int i = 0;

	for (i = 0; 1; i++) {
		const struct tdma_sched_item *sched_item = &item_set[i];
		struct tdma_sched_bucket *bucket = &sched->bucket[bucket_nr];

		if (sched_item->cb == &tdma_end_set) {
			/* end of scheduler set, return */
			break;
		}

		if (sched_item->cb == NULL) {
			/* advance to next bucket (== TDMA frame) */
			bucket_nr = wrap_bucket(++frame_offset);
			continue;
		}
		/* check for bucket overflow */
		if (bucket->num_items >= ARRAY_SIZE(bucket->item)) {
			puts("tdma_schedule bucket overflow\n");
			return -1;
		}
		/* copy the item from the set into the current bucket item position */
		memcpy(&bucket->item[bucket->num_items++], sched_item, sizeof(*sched_item));
	}

	return i;
}

/* Execute pre-scheduled events for current frame */
int tdma_sched_execute(void)
{
	struct tdma_scheduler *sched = &l1s.tdma_sched;
	struct tdma_sched_bucket *bucket;
	uint8_t next_bucket;
	int i, num_events = 0;

	/* determine current bucket */
	bucket = &sched->bucket[sched->cur_bucket];

	/* iterate over items in this bucket and call callback function */
	for (i = 0; i < bucket->num_items; i++) {
		struct tdma_sched_item *item = &bucket->item[i];
		int rc;

		num_events++;

		rc = item->cb(item->p1, item->p2);
		if (rc < 0) {
			printf("Error %d during processing of item %u of bucket %u\n",
				rc, i, sched->cur_bucket);
			return rc;
		}
		/* if the cb() we just called has scheduled more items for the
		 * current TDMA, bucket->num_items will have increased and we
		 * will simply continue to execute them as intended */
	}

	/* clear/reset the bucket */
	bucket->num_items = 0;

	/* advance to the next bucket */
	next_bucket = wrap_bucket(1);
	sched->cur_bucket = next_bucket;

	/* return number of items that we called */
	return num_events;
}

void tdma_sched_reset(void)
{
	struct tdma_scheduler *sched = &l1s.tdma_sched;
	unsigned int bucket_nr;

	for (bucket_nr = 0; bucket_nr < ARRAY_SIZE(sched->bucket); bucket_nr++) {
		struct tdma_sched_bucket *bucket = &sched->bucket[bucket_nr];
		/* current bucket will be reset by iteration code above! */
		if (bucket_nr != sched->cur_bucket)
			bucket->num_items = 0;
	}

	/* Don't reset cur_bucket, as it would upset the bucket iteration code
	 * in tdma_sched_execute() */
}

void tdma_sched_dump(void)
{
	unsigned int i;

	printf("\n(%2u)", l1s.tdma_sched.cur_bucket);
	for (i = 0; i < ARRAY_SIZE(l1s.tdma_sched.bucket); i++) {
		int bucket_nr = wrap_bucket(i);
		struct tdma_sched_bucket *bucket = &l1s.tdma_sched.bucket[bucket_nr];
		printf("%u:", bucket->num_items);
	}
	putchar('\n');
}