blob: 4aa97073e6c3dc2f7d26f7eab5aea8d877ae8eba [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{
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040056 struct msgb *msg = l1p_msgb_alloc();
57 GsmL1_Prim_t *prim = msgb_l1prim(msg);
58 // RLC/MAC filler with USF=1
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040059 bitvec *filler = bitvec_alloc(23);
60 bitvec_unhex(filler, "41942b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040061 prim->id = GsmL1_PrimId_PhDataReq;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040062 prim->u.phDataReq.sapi = GsmL1_Sapi_Pacch;
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040063 bitvec_pack(filler, prim->u.phDataReq.msgUnitParam.u8Buffer);
64 prim->u.phDataReq.msgUnitParam.u8Size = filler->data_len;
65 bitvec_free(filler);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040066 return msg;
67}
68
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040069// Send RLC/MAC block to OpenBTS.
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040070void pcu_l1if_tx(bitvec * block, GsmL1_Sapi_t sapi, int len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040071{
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040072 struct msgb *msg = l1p_msgb_alloc();
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040073 struct osmo_wqueue * queue;
74 queue = &((l1fh->fl1h)->write_q);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040075 GsmL1_Prim_t *prim = msgb_l1prim(msg);
76
77 prim->id = GsmL1_PrimId_PhDataReq;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040078 prim->u.phDataReq.sapi = sapi;
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040079 bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer);
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040080 prim->u.phDataReq.msgUnitParam.u8Size = len;
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040081 //COUT("Add Block to WRITE QUEUE: " << *block);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040082 osmo_wqueue_enqueue(queue, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040083}
84
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040085int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040086{
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040087 bitvec *block = bitvec_alloc(data_ind->msgUnitParam.u8Size);
88 bitvec_unpack(block, data_ind->msgUnitParam.u8Buffer);
89 //COUT("RX: " << *block);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040090 gprs_rlcmac_rcv_block(block);
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040091 bitvec_free(block);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040092}
93
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040094static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind)
95{
96 (l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn;
97 (l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn;
98 (l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc;
99 COUT("RX: [ PCU <- BTS ] PhConnectInd: ARFCN: " << connect_ind->u16Arfcn
100 <<" TN: " << (unsigned)connect_ind->u8Tn << " TSC: " << (unsigned)connect_ind->u8Tsc);
101}
102
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400103static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
104{
105 struct msgb *resp_msg;
106 struct osmo_wqueue * queue;
107 queue = &((l1fh->fl1h)->write_q);
108
109 set_current_fn(readytosend_ind->u32Fn);
110 resp_msg = msgb_dequeue(&queue->msg_queue);
111 if (!resp_msg) {
112 resp_msg = gen_dummy_msg();
113 if (!resp_msg)
114 return 0;
115 }
116 osmo_wqueue_enqueue(&l1fh->udp_wq, resp_msg);
117 return 1;
118}
119
120static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400121{
122 int rc = 0;
123 switch (data_ind->sapi) {
124 case GsmL1_Sapi_Rach:
125 break;
126 case GsmL1_Sapi_Pdtch:
127 case GsmL1_Sapi_Pacch:
128 pcu_l1if_rx_pdch(data_ind);
129 break;
130 case GsmL1_Sapi_Pbcch:
131 case GsmL1_Sapi_Pagch:
132 case GsmL1_Sapi_Ppch:
133 case GsmL1_Sapi_Pnch:
134 case GsmL1_Sapi_Ptcch:
135 case GsmL1_Sapi_Prach:
136 break;
137 default:
138 //LOGP(DGPRS, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
139 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400140 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400141
142 return rc;
143}
144
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400145static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind)
146{
147 int rc = 0;
148 (l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming;
149 rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming);
150 return rc;
151}
152
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400153/* handle any random indication from the L1 */
154int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
155{
156 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
157 int rc = 0;
158
159 switch (l1p->id) {
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400160 case GsmL1_PrimId_PhConnectInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400161 rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400162 break;
163 case GsmL1_PrimId_PhReadyToSendInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400164 rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400165 break;
166 case GsmL1_PrimId_PhDataInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400167 rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400168 break;
169 case GsmL1_PrimId_PhRaInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400170 rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400171 break;
172 default:
173 break;
174 }
175
176 /* Special return value '1' means: do not free */
177 if (rc != 1)
178 msgb_free(msg);
179
180 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400181}
182
183void gsmtap_send_llc(uint8_t * data, unsigned len)
184{
185 char buffer[MAX_UDP_LENGTH];
186 int ofs = 0;
187
188 // Build header
189 struct gsmtap_hdr *header = (struct gsmtap_hdr *)buffer;
190 header->version = 2;
191 header->hdr_len = sizeof(struct gsmtap_hdr) >> 2;
192 header->type = 0x08;
193 header->timeslot = 5;
194 header->arfcn = 0;
195 header->signal_dbm = 0;
196 header->snr_db = 0;
197 header->frame_number = 0;
198 header->sub_type = 0;
199 header->antenna_nr = 0;
200 header->sub_slot = 0;
201 header->res = 0;
202
203 ofs += sizeof(*header);
204
205 // Add frame data
206 unsigned j = 0;
207 for (unsigned i = ofs; i < len+ofs; i++)
208 {
209 buffer[i] = (char)data[j];
210 j++;
211 }
212 ofs += len;
213 // Write the GSMTAP packet
214 pcu_gsmtap_socket.write(buffer, ofs);
215}