blob: d2c05b706e3450ff827d4d710d79f828324e9072 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm/gprs_rlc.c
2 * helper functions for (E)GPRS RLC according to 3GPP TS 44.060.
3 */
4
Tom Tsou9a5bbf32016-07-14 15:13:45 -07005#include <errno.h>
6#include <string.h>
7
Harald Welte35b26322017-07-31 19:36:52 +02008#include <osmocom/core/utils.h>
Tom Tsou9a5bbf32016-07-14 15:13:45 -07009#include <osmocom/gprs/gprs_rlc.h>
Maxc8cf8202017-05-22 16:07:04 +020010#include <osmocom/coding/gsm0503_coding.h>
Tom Tsou9a5bbf32016-07-14 15:13:45 -070011#include <osmocom/gprs/protocol/gsm_04_60.h>
12
13#define EGPRS_CPS_TYPE1_TBL_SZ 29
14#define EGPRS_CPS_TYPE2_TBL_SZ 8
15#define EGPRS_CPS_TYPE3_TBL_SZ 16
16
17/* 3GPP TS 44.060 10.4.8a.1.1 "Header type 1" */
18static const struct egprs_cps egprs_cps_table_type1[EGPRS_CPS_TYPE1_TBL_SZ] = {
Maxc8cf8202017-05-22 16:07:04 +020019 { .bits = 0, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
20 { .bits = 1, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
21 { .bits = 2, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
22 { .bits = 3, .mcs = EGPRS_NUM_MCS, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } }, /* reserved for future use */
23 { .bits = 4, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
24 { .bits = 5, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
25 { .bits = 6, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
26 { .bits = 7, .mcs = EGPRS_NUM_MCS, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } }, /* reserved for future use */
27 { .bits = 8, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
28 { .bits = 9, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
29 { .bits = 10, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
30 { .bits = 11, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
31 { .bits = 12, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
32 { .bits = 13, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
33 { .bits = 14, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
34 { .bits = 15, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
35 { .bits = 16, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
36 { .bits = 17, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
37 { .bits = 18, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
38 { .bits = 19, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
39 { .bits = 20, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
40 { .bits = 21, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
41 { .bits = 22, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
42 { .bits = 23, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
43 { .bits = 24, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
44 { .bits = 25, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
45 { .bits = 26, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
46 { .bits = 27, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
47 { .bits = 28, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
Tom Tsou9a5bbf32016-07-14 15:13:45 -070048};
49
50/*
51 * 3GPP TS 44.060 10.4.8a.2.1
52 * "Header type 2 in EGPRS TBF or uplink EGPRS2-A TBF"
53 */
54static const struct egprs_cps egprs_cps_table_type2[EGPRS_CPS_TYPE2_TBL_SZ] = {
Maxc8cf8202017-05-22 16:07:04 +020055 { .bits = 0, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
56 { .bits = 1, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
57 { .bits = 2, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
58 { .bits = 3, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
59 { .bits = 4, .mcs = EGPRS_MCS5, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
60 { .bits = 5, .mcs = EGPRS_MCS5, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
61 { .bits = 6, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
62 { .bits = 7, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
Tom Tsou9a5bbf32016-07-14 15:13:45 -070063};
64
65/* 3GPP TS 44.060 10.4.8a.3 "Header type 3" */
66static const struct egprs_cps egprs_cps_table_type3[EGPRS_CPS_TYPE3_TBL_SZ] = {
Maxc8cf8202017-05-22 16:07:04 +020067 { .bits = 0, .mcs = EGPRS_MCS4, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
68 { .bits = 1, .mcs = EGPRS_MCS4, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
69 { .bits = 2, .mcs = EGPRS_MCS4, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
70 { .bits = 3, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
71 { .bits = 4, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
72 { .bits = 5, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
73 { .bits = 6, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
74 { .bits = 7, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
75 { .bits = 8, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
76 { .bits = 9, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
77 { .bits = 10, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
78 { .bits = 11, .mcs = EGPRS_MCS1, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
79 { .bits = 12, .mcs = EGPRS_MCS1, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
80 { .bits = 13, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
81 { .bits = 14, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
82 { .bits = 15, .mcs = EGPRS_MCS0, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } },
Tom Tsou9a5bbf32016-07-14 15:13:45 -070083};
84
85int egprs_get_cps(struct egprs_cps *cps, uint8_t type, uint8_t bits)
86{
87 const struct egprs_cps *table_cps;
88
89 switch (type) {
90 case EGPRS_HDR_TYPE1:
91 if (bits >= EGPRS_CPS_TYPE1_TBL_SZ)
92 return -EINVAL;
93 table_cps = &egprs_cps_table_type1[bits];
94 break;
95 case EGPRS_HDR_TYPE2:
96 if (bits >= EGPRS_CPS_TYPE2_TBL_SZ)
97 return -EINVAL;
98 table_cps = &egprs_cps_table_type2[bits];
99 break;
100 case EGPRS_HDR_TYPE3:
101 if (bits >= EGPRS_CPS_TYPE3_TBL_SZ)
102 return -EINVAL;
103 table_cps = &egprs_cps_table_type3[bits];
104 break;
105 default:
106 return -EINVAL;
107 }
108
109 memcpy(cps, table_cps, sizeof *cps);
110
111 return 0;
112}
Harald Welte35b26322017-07-31 19:36:52 +0200113
114struct gprs_cs_desc {
115 struct {
116 uint8_t bytes;
117 uint8_t bits;
118 } uplink, downlink;
119};
120
121const struct gprs_cs_desc gprs_cs_desc[_NUM_OSMO_GPRS_CS] = {
122 [OSMO_GPRS_CS1] = { {23, 0}, {23, 0} },
123 [OSMO_GPRS_CS2] = { {33, 7}, {33, 7} },
124 [OSMO_GPRS_CS3] = { {39, 3}, {39, 3} },
125 [OSMO_GPRS_CS4] = { {53, 7}, {53, 7} },
126
127 [OSMO_GPRS_MCS1] = { {26, 1}, {26, 1} },
128 [OSMO_GPRS_MCS2] = { {32, 1}, {32, 1} },
129 [OSMO_GPRS_MCS3] = { {41, 1}, {41, 1} },
130 [OSMO_GPRS_MCS4] = { {48, 1}, {48, 1} },
131
132 [OSMO_GPRS_MCS5] = { {60, 7}, {59, 6} },
133 [OSMO_GPRS_MCS6] = { {78, 7}, {77, 6} },
134 [OSMO_GPRS_MCS7] = { {118, 2}, {117, 4} },
135 [OSMO_GPRS_MCS8] = { {142, 2}, {141, 4} },
136 [OSMO_GPRS_MCS9] = { {154, 2}, {153, 4} },
137};
138
139/*! Return size of (E)GPRS uplink block for given coding scheme in bits */
140int osmo_gprs_ul_block_size_bits(enum osmo_gprs_cs cs)
141{
142 if (cs >= ARRAY_SIZE(gprs_cs_desc))
143 return -EINVAL;
144 return gprs_cs_desc[cs].uplink.bytes * 8 + gprs_cs_desc[cs].uplink.bits;
145}
146
147/*! Return size of (E)GPRS downlink block for given coding scheme in bits */
148int osmo_gprs_dl_block_size_bits(enum osmo_gprs_cs cs)
149{
150 if (cs >= ARRAY_SIZE(gprs_cs_desc))
151 return -EINVAL;
152 return gprs_cs_desc[cs].downlink.bytes * 8 + gprs_cs_desc[cs].downlink.bits;
153}
154
155/*! Return size of (E)GPRS uplink block for given coding scheme in bytes */
156int osmo_gprs_ul_block_size_bytes(enum osmo_gprs_cs cs)
157{
158 int rc;
159 if (cs >= ARRAY_SIZE(gprs_cs_desc))
160 return -EINVAL;
161 rc = gprs_cs_desc[cs].uplink.bytes;
162 if (gprs_cs_desc[cs].uplink.bits)
163 rc++;
164 return rc;
165}
166
167/*! Return size of (E)GPRS downlink block for given coding scheme in bytes */
168int osmo_gprs_dl_block_size_bytes(enum osmo_gprs_cs cs)
169{
170 int rc;
171 if (cs >= ARRAY_SIZE(gprs_cs_desc))
172 return -EINVAL;
173 rc = gprs_cs_desc[cs].downlink.bytes;
174 if (gprs_cs_desc[cs].downlink.bits)
175 rc++;
176 return rc;
177}
178
179/*! Return coding scheme for given (E)GPRS uplink block size */
180enum osmo_gprs_cs osmo_gprs_ul_cs_by_block_bytes(uint8_t block_size)
181{
182 unsigned int i;
183
184 for (i = 0; i < ARRAY_SIZE(gprs_cs_desc); i++) {
185 if (block_size == osmo_gprs_ul_block_size_bytes(i))
186 return i;
187 }
188 return OSMO_GPRS_CS_NONE;
189}
190
191/*! Return coding scheme for given (E)GPRS downlink block size */
192enum osmo_gprs_cs osmo_gprs_dl_cs_by_block_bytes(uint8_t block_size)
193{
194 unsigned int i;
195
196 for (i = 0; i < ARRAY_SIZE(gprs_cs_desc); i++) {
197 if (block_size == osmo_gprs_dl_block_size_bytes(i))
198 return i;
199 }
200 return OSMO_GPRS_CS_NONE;
201}