blob: c3d1b12d8207a200d34273ee48fe18ac221303eb [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;
62 filler.pack((unsigned char*)&(prim->u.phDataReq.msgUnitParam.u8Buffer[ofs]));
63 ofs += filler.size() >> 3;
64 prim->u.phDataReq.msgUnitParam.u8Size = ofs;
65 return msg;
66}
67
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040068// Send RLC/MAC block to OpenBTS.
69void pcu_l1if_tx(BitVector * block)
70{
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040071 int ofs = 0;
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;
78 block->pack((unsigned char*)&(prim->u.phDataReq.msgUnitParam.u8Buffer[ofs]));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040079 ofs += block->size() >> 3;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040080 prim->u.phDataReq.msgUnitParam.u8Size = ofs;
81
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040082 COUT("Add Block to WRITE QUEUE: " << *block);
83 osmo_wqueue_enqueue(queue, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040084}
85
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040086int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040087{
88 BitVector *block = new BitVector(23*8);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040089 block->unpack((const unsigned char*)data_ind->msgUnitParam.u8Buffer);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040090 COUT("RX: " << *block);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040091
92 gprs_rlcmac_rcv_block(block);
93}
94
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040095static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
96{
97 struct msgb *resp_msg;
98 struct osmo_wqueue * queue;
99 queue = &((l1fh->fl1h)->write_q);
100
101 set_current_fn(readytosend_ind->u32Fn);
102 resp_msg = msgb_dequeue(&queue->msg_queue);
103 if (!resp_msg) {
104 resp_msg = gen_dummy_msg();
105 if (!resp_msg)
106 return 0;
107 }
108 osmo_wqueue_enqueue(&l1fh->udp_wq, resp_msg);
109 return 1;
110}
111
112static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400113{
114 int rc = 0;
115 switch (data_ind->sapi) {
116 case GsmL1_Sapi_Rach:
117 break;
118 case GsmL1_Sapi_Pdtch:
119 case GsmL1_Sapi_Pacch:
120 pcu_l1if_rx_pdch(data_ind);
121 break;
122 case GsmL1_Sapi_Pbcch:
123 case GsmL1_Sapi_Pagch:
124 case GsmL1_Sapi_Ppch:
125 case GsmL1_Sapi_Pnch:
126 case GsmL1_Sapi_Ptcch:
127 case GsmL1_Sapi_Prach:
128 break;
129 default:
130 //LOGP(DGPRS, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
131 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400132 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400133
134 return rc;
135}
136
137/* handle any random indication from the L1 */
138int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
139{
140 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
141 int rc = 0;
142
143 switch (l1p->id) {
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400144 case GsmL1_PrimId_PhConnectInd:
145 break;
146 case GsmL1_PrimId_PhReadyToSendInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400147 rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400148 break;
149 case GsmL1_PrimId_PhDataInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400150 COUT("RX GsmL1_PrimId_PhDataInd ");
151 rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400152 break;
153 case GsmL1_PrimId_PhRaInd:
154 break;
155 default:
156 break;
157 }
158
159 /* Special return value '1' means: do not free */
160 if (rc != 1)
161 msgb_free(msg);
162
163 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400164}
165
166void gsmtap_send_llc(uint8_t * data, unsigned len)
167{
168 char buffer[MAX_UDP_LENGTH];
169 int ofs = 0;
170
171 // Build header
172 struct gsmtap_hdr *header = (struct gsmtap_hdr *)buffer;
173 header->version = 2;
174 header->hdr_len = sizeof(struct gsmtap_hdr) >> 2;
175 header->type = 0x08;
176 header->timeslot = 5;
177 header->arfcn = 0;
178 header->signal_dbm = 0;
179 header->snr_db = 0;
180 header->frame_number = 0;
181 header->sub_type = 0;
182 header->antenna_nr = 0;
183 header->sub_slot = 0;
184 header->res = 0;
185
186 ofs += sizeof(*header);
187
188 // Add frame data
189 unsigned j = 0;
190 for (unsigned i = ofs; i < len+ofs; i++)
191 {
192 buffer[i] = (char)data[j];
193 j++;
194 }
195 ofs += len;
196 // Write the GSMTAP packet
197 pcu_gsmtap_socket.write(buffer, ofs);
198}