blob: a58a65b1bfad507f6ce0d3a006a7e16d6c95e05c [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
21#include <stdint.h>
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010022#include <string.h>
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010023
24#define LLC_MAX_LEN 1543
25
Jacob Erlbeck1d0a52a2015-06-02 11:28:07 +020026struct BTS;
27struct timeval;
28struct msgb;
29
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010030/**
31 * I represent the LLC data to a MS
32 */
33struct gprs_llc {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010034 static bool is_user_data_frame(uint8_t *data, size_t len);
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +010035
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010036 void init();
37 void reset();
38 void reset_frame_space();
39
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010040 void put_frame(const uint8_t *data, size_t len);
Jacob Erlbeck612e93e2015-03-20 13:57:27 +010041 void put_dummy_frame(size_t req_len);
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010042 void append_frame(const uint8_t *data, size_t len);
43
44 void consume(size_t len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010045 void consume(uint8_t *data, size_t len);
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010046
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010047 uint16_t chunk_size() const;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010048 uint16_t remaining_space() const;
49 uint16_t frame_length() const;
50
51 bool fits_in_current_frame(uint8_t size) const;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010052
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010053 uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010054 uint16_t m_index; /* current write/read position of frame */
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010055 uint16_t m_length; /* len of current DL LLC_frame, 0 == no frame */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020056};
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010057
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020058/**
59 * I store the LLC frames that come from the SGSN.
60 */
61struct gprs_llc_queue {
62 static void calc_pdu_lifetime(BTS *bts, const uint16_t pdu_delay_csec, struct timeval *tv);
63 static bool is_frame_expired(struct timeval *now, struct timeval *tv);
64 static bool is_user_data_frame(uint8_t *data, size_t len);
65
66 void init();
67
68 void enqueue(struct msgb *llc_msg);
69 struct msgb *dequeue();
70 void clear(BTS *bts);
71 size_t size() const;
72
73private:
Daniel Willmann9c623892013-12-04 18:11:47 +010074 uint32_t m_avg_queue_delay; /* Average delay of data going through the queue */
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010075 size_t m_queue_size;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020076 struct llist_head m_queue; /* queued LLC DL data */
77
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010078};
79
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020080
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010081inline uint16_t gprs_llc::chunk_size() const
82{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010083 return m_length - m_index;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010084}
85
86inline uint16_t gprs_llc::remaining_space() const
87{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +010088 return LLC_MAX_LEN - m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010089}
90
91inline uint16_t gprs_llc::frame_length() const
92{
93 return m_length;
94}
95
96inline void gprs_llc::consume(size_t len)
97{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010098 m_index += len;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010099}
100
101inline void gprs_llc::consume(uint8_t *data, size_t len)
102{
103 /* copy and increment index */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +0100104 memcpy(data, frame + m_index, len);
105 consume(len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100106}
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100107
108inline bool gprs_llc::fits_in_current_frame(uint8_t chunk_size) const
109{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100110 return m_length + chunk_size <= LLC_MAX_LEN;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100111}
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200112
113inline size_t gprs_llc_queue::size() const
114{
115 return this ? m_queue_size : 0;
116}