blob: be2cda019968d9b64a0952a4cd3b363824274bf1 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* pcu_l1_if.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include <Sockets.h>
21#include <gsmtap.h>
22#include <gprs_rlcmac.h>
23#include <Threads.h>
24#include <pcu_l1_if.h>
25
26#define MAX_UDP_LENGTH 1500
27
28// TODO: We should take ports and IP from config.
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040029UDPSocket pcu_gsmtap_socket(5077, "127.0.0.1", 4729);
30
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040031// Variable for storage current FN.
32int frame_number;
33
34int get_current_fn()
35{
36 return frame_number;
37}
38
39void set_current_fn(int fn)
40{
41 frame_number = fn;
42}
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040043
44struct msgb *l1p_msgb_alloc(void)
45{
46 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
47
48 if (msg)
49 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
50
51 return msg;
52}
53
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040054struct msgb *gen_dummy_msg(void)
55{
56 int ofs = 0;
57 struct msgb *msg = l1p_msgb_alloc();
58 GsmL1_Prim_t *prim = msgb_l1prim(msg);
59 // RLC/MAC filler with USF=1
60 BitVector filler("0100000110010100001010110010101100101011001010110010101100101011001010110010101100101011001010110010101100101011001010110010101100101011001010110010101100101011001010110010101100101011");
61 prim->id = GsmL1_PrimId_PhDataReq;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040062 prim->u.phDataReq.sapi = GsmL1_Sapi_Pacch;
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040063 filler.pack((unsigned char*)&(prim->u.phDataReq.msgUnitParam.u8Buffer[ofs]));
64 ofs += filler.size() >> 3;
65 prim->u.phDataReq.msgUnitParam.u8Size = ofs;
66 return msg;
67}
68
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040069// Send RLC/MAC block to OpenBTS.
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040070void pcu_l1if_tx(BitVector * block, GsmL1_Sapi_t sapi, int len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040071{
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040072 int ofs = 0;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040073 struct msgb *msg = l1p_msgb_alloc();
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040074 struct osmo_wqueue * queue;
75 queue = &((l1fh->fl1h)->write_q);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040076 GsmL1_Prim_t *prim = msgb_l1prim(msg);
77
78 prim->id = GsmL1_PrimId_PhDataReq;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040079 prim->u.phDataReq.sapi = sapi;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040080 block->pack((unsigned char*)&(prim->u.phDataReq.msgUnitParam.u8Buffer[ofs]));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040081 ofs += block->size() >> 3;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040082 prim->u.phDataReq.msgUnitParam.u8Size = len;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040083
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040084 COUT("Add Block to WRITE QUEUE: " << *block);
85 osmo_wqueue_enqueue(queue, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040086}
87
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040088int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040089{
90 BitVector *block = new BitVector(23*8);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040091 block->unpack((const unsigned char*)data_ind->msgUnitParam.u8Buffer);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040092 COUT("RX: " << *block);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040093
94 gprs_rlcmac_rcv_block(block);
95}
96
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040097static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind)
98{
99 (l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn;
100 (l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn;
101 (l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc;
102 COUT("RX: [ PCU <- BTS ] PhConnectInd: ARFCN: " << connect_ind->u16Arfcn
103 <<" TN: " << (unsigned)connect_ind->u8Tn << " TSC: " << (unsigned)connect_ind->u8Tsc);
104}
105
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400106static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
107{
108 struct msgb *resp_msg;
109 struct osmo_wqueue * queue;
110 queue = &((l1fh->fl1h)->write_q);
111
112 set_current_fn(readytosend_ind->u32Fn);
113 resp_msg = msgb_dequeue(&queue->msg_queue);
114 if (!resp_msg) {
115 resp_msg = gen_dummy_msg();
116 if (!resp_msg)
117 return 0;
118 }
119 osmo_wqueue_enqueue(&l1fh->udp_wq, resp_msg);
120 return 1;
121}
122
123static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400124{
125 int rc = 0;
126 switch (data_ind->sapi) {
127 case GsmL1_Sapi_Rach:
128 break;
129 case GsmL1_Sapi_Pdtch:
130 case GsmL1_Sapi_Pacch:
131 pcu_l1if_rx_pdch(data_ind);
132 break;
133 case GsmL1_Sapi_Pbcch:
134 case GsmL1_Sapi_Pagch:
135 case GsmL1_Sapi_Ppch:
136 case GsmL1_Sapi_Pnch:
137 case GsmL1_Sapi_Ptcch:
138 case GsmL1_Sapi_Prach:
139 break;
140 default:
141 //LOGP(DGPRS, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
142 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400143 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400144
145 return rc;
146}
147
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400148static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind)
149{
150 int rc = 0;
151 (l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming;
152 rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming);
153 return rc;
154}
155
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400156/* handle any random indication from the L1 */
157int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
158{
159 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
160 int rc = 0;
161
162 switch (l1p->id) {
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400163 case GsmL1_PrimId_PhConnectInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400164 rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400165 break;
166 case GsmL1_PrimId_PhReadyToSendInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400167 rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400168 break;
169 case GsmL1_PrimId_PhDataInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400170 rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400171 break;
172 case GsmL1_PrimId_PhRaInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400173 rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400174 break;
175 default:
176 break;
177 }
178
179 /* Special return value '1' means: do not free */
180 if (rc != 1)
181 msgb_free(msg);
182
183 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400184}
185
186void gsmtap_send_llc(uint8_t * data, unsigned len)
187{
188 char buffer[MAX_UDP_LENGTH];
189 int ofs = 0;
190
191 // Build header
192 struct gsmtap_hdr *header = (struct gsmtap_hdr *)buffer;
193 header->version = 2;
194 header->hdr_len = sizeof(struct gsmtap_hdr) >> 2;
195 header->type = 0x08;
196 header->timeslot = 5;
197 header->arfcn = 0;
198 header->signal_dbm = 0;
199 header->snr_db = 0;
200 header->frame_number = 0;
201 header->sub_type = 0;
202 header->antenna_nr = 0;
203 header->sub_slot = 0;
204 header->res = 0;
205
206 ofs += sizeof(*header);
207
208 // Add frame data
209 unsigned j = 0;
210 for (unsigned i = ofs; i < len+ofs; i++)
211 {
212 buffer[i] = (char)data[j];
213 j++;
214 }
215 ofs += len;
216 // Write the GSMTAP packet
217 pcu_gsmtap_socket.write(buffer, ofs);
218}