blob: b95c512a131a523275057e03d3bca7112a118923 [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>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040023#include <pcu_l1_if.h>
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040024#include <gprs_debug.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040025
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 Kluchnikov4277f0c2012-04-12 14:24:35 +040081 osmo_wqueue_enqueue(queue, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040082}
83
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040084int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040085{
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040086 bitvec *block = bitvec_alloc(data_ind->msgUnitParam.u8Size);
87 bitvec_unpack(block, data_ind->msgUnitParam.u8Buffer);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040088 gprs_rlcmac_rcv_block(block);
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040089 bitvec_free(block);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040090}
91
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040092static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind)
93{
94 (l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn;
95 (l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn;
96 (l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc;
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040097 LOGP(DL1IF, LOGL_NOTICE, "RX: [ PCU <- BTS ] PhConnectInd: ARFCN: %u TN: %u TSC: %u \n",
98 connect_ind->u16Arfcn, (unsigned)connect_ind->u8Tn, (unsigned)connect_ind->u8Tsc);
Ivan Kluchnikov5310d452012-04-17 22:00:31 +040099}
100
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400101static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
102{
103 struct msgb *resp_msg;
104 struct osmo_wqueue * queue;
105 queue = &((l1fh->fl1h)->write_q);
106
107 set_current_fn(readytosend_ind->u32Fn);
108 resp_msg = msgb_dequeue(&queue->msg_queue);
109 if (!resp_msg) {
110 resp_msg = gen_dummy_msg();
111 if (!resp_msg)
112 return 0;
113 }
114 osmo_wqueue_enqueue(&l1fh->udp_wq, resp_msg);
115 return 1;
116}
117
118static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400119{
120 int rc = 0;
121 switch (data_ind->sapi) {
122 case GsmL1_Sapi_Rach:
123 break;
124 case GsmL1_Sapi_Pdtch:
125 case GsmL1_Sapi_Pacch:
126 pcu_l1if_rx_pdch(data_ind);
127 break;
128 case GsmL1_Sapi_Pbcch:
129 case GsmL1_Sapi_Pagch:
130 case GsmL1_Sapi_Ppch:
131 case GsmL1_Sapi_Pnch:
132 case GsmL1_Sapi_Ptcch:
133 case GsmL1_Sapi_Prach:
134 break;
135 default:
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400136 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400137 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400138 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400139
140 return rc;
141}
142
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400143static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind)
144{
145 int rc = 0;
146 (l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming;
147 rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming);
148 return rc;
149}
150
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400151/* handle any random indication from the L1 */
152int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
153{
154 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
155 int rc = 0;
156
157 switch (l1p->id) {
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400158 case GsmL1_PrimId_PhConnectInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400159 rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400160 break;
161 case GsmL1_PrimId_PhReadyToSendInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400162 rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400163 break;
164 case GsmL1_PrimId_PhDataInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400165 rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400166 break;
167 case GsmL1_PrimId_PhRaInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400168 rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400169 break;
170 default:
171 break;
172 }
173
174 /* Special return value '1' means: do not free */
175 if (rc != 1)
176 msgb_free(msg);
177
178 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400179}
180
181void gsmtap_send_llc(uint8_t * data, unsigned len)
182{
183 char buffer[MAX_UDP_LENGTH];
184 int ofs = 0;
185
186 // Build header
187 struct gsmtap_hdr *header = (struct gsmtap_hdr *)buffer;
188 header->version = 2;
189 header->hdr_len = sizeof(struct gsmtap_hdr) >> 2;
190 header->type = 0x08;
191 header->timeslot = 5;
192 header->arfcn = 0;
193 header->signal_dbm = 0;
194 header->snr_db = 0;
195 header->frame_number = 0;
196 header->sub_type = 0;
197 header->antenna_nr = 0;
198 header->sub_slot = 0;
199 header->res = 0;
200
201 ofs += sizeof(*header);
202
203 // Add frame data
204 unsigned j = 0;
205 for (unsigned i = ofs; i < len+ofs; i++)
206 {
207 buffer[i] = (char)data[j];
208 j++;
209 }
210 ofs += len;
211 // Write the GSMTAP packet
212 pcu_gsmtap_socket.write(buffer, ofs);
213}