blob: 80d2f7aceb7b54d97bd61107bf28c7bf3ccc46f7 [file] [log] [blame]
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +01001/*
2 * Copyright (C) 2013 by Holger Hans Peter Freyther
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#pragma once
20
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020021extern "C" {
22 #include <osmocom/core/linuxlist.h>
23}
24
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010025#include <stdint.h>
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010026#include <string.h>
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010027
28#define LLC_MAX_LEN 1543
29
Jacob Erlbeck1d0a52a2015-06-02 11:28:07 +020030struct BTS;
31struct timeval;
32struct msgb;
33
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010034/**
35 * I represent the LLC data to a MS
36 */
37struct gprs_llc {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010038 static bool is_user_data_frame(uint8_t *data, size_t len);
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +010039
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010040 void init();
41 void reset();
42 void reset_frame_space();
43
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010044 void put_frame(const uint8_t *data, size_t len);
Jacob Erlbeck612e93e2015-03-20 13:57:27 +010045 void put_dummy_frame(size_t req_len);
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010046 void append_frame(const uint8_t *data, size_t len);
47
48 void consume(size_t len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010049 void consume(uint8_t *data, size_t len);
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010050
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010051 uint16_t chunk_size() const;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010052 uint16_t remaining_space() const;
53 uint16_t frame_length() const;
54
55 bool fits_in_current_frame(uint8_t size) const;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010056
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010057 uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010058 uint16_t m_index; /* current write/read position of frame */
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010059 uint16_t m_length; /* len of current DL LLC_frame, 0 == no frame */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020060};
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010061
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020062/**
63 * I store the LLC frames that come from the SGSN.
64 */
65struct gprs_llc_queue {
66 static void calc_pdu_lifetime(BTS *bts, const uint16_t pdu_delay_csec, struct timeval *tv);
67 static bool is_frame_expired(struct timeval *now, struct timeval *tv);
68 static bool is_user_data_frame(uint8_t *data, size_t len);
69
70 void init();
71
72 void enqueue(struct msgb *llc_msg);
73 struct msgb *dequeue();
74 void clear(BTS *bts);
75 size_t size() const;
76
77private:
Daniel Willmann9c623892013-12-04 18:11:47 +010078 uint32_t m_avg_queue_delay; /* Average delay of data going through the queue */
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010079 size_t m_queue_size;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020080 struct llist_head m_queue; /* queued LLC DL data */
81
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010082};
83
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020084
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010085inline uint16_t gprs_llc::chunk_size() const
86{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010087 return m_length - m_index;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010088}
89
90inline uint16_t gprs_llc::remaining_space() const
91{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +010092 return LLC_MAX_LEN - m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010093}
94
95inline uint16_t gprs_llc::frame_length() const
96{
97 return m_length;
98}
99
100inline void gprs_llc::consume(size_t len)
101{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +0100102 m_index += len;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100103}
104
105inline void gprs_llc::consume(uint8_t *data, size_t len)
106{
107 /* copy and increment index */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +0100108 memcpy(data, frame + m_index, len);
109 consume(len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100110}
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100111
112inline bool gprs_llc::fits_in_current_frame(uint8_t chunk_size) const
113{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100114 return m_length + chunk_size <= LLC_MAX_LEN;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100115}
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200116
117inline size_t gprs_llc_queue::size() const
118{
119 return this ? m_queue_size : 0;
120}