blob: 54c5b368ece2c9c235288192487b69bb63c40a7e [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.
29UDPSocket pcu_l1if_socket(5070, "127.0.0.1", 5934);
30UDPSocket pcu_gsmtap_socket(5077, "127.0.0.1", 4729);
31
32// Send RLC/MAC block to OpenBTS.
33void pcu_l1if_tx(BitVector * block)
34{
35 char buffer[MAX_UDP_LENGTH];
36 int ofs = 0;
37 block->pack((unsigned char*)&buffer[ofs]);
38 ofs += block->size() >> 3;
39 COUT("Send to OpenBTS: " << *block);
40 pcu_l1if_socket.write(buffer, ofs);
41}
42
43// Recieve RLC/MAC block from OpenBTS.
44void *pcu_l1if_rx(void *)
45{
46 BitVector *block = new BitVector(23*8);
47 pcu_l1if_socket.nonblocking();
48 while (1) {
49 char buf[MAX_UDP_LENGTH];
50 int count = pcu_l1if_socket.read(buf, 3000);
51 if (count>0) {
52 block->unpack((const unsigned char*)buf);
53 COUT("Recieve from OpenBTS (MS): " << *block);
54 gprs_rlcmac_rcv_block(block);
55 }
56 }
57}
58
59void gsmtap_send_llc(uint8_t * data, unsigned len)
60{
61 char buffer[MAX_UDP_LENGTH];
62 int ofs = 0;
63
64 // Build header
65 struct gsmtap_hdr *header = (struct gsmtap_hdr *)buffer;
66 header->version = 2;
67 header->hdr_len = sizeof(struct gsmtap_hdr) >> 2;
68 header->type = 0x08;
69 header->timeslot = 5;
70 header->arfcn = 0;
71 header->signal_dbm = 0;
72 header->snr_db = 0;
73 header->frame_number = 0;
74 header->sub_type = 0;
75 header->antenna_nr = 0;
76 header->sub_slot = 0;
77 header->res = 0;
78
79 ofs += sizeof(*header);
80
81 // Add frame data
82 unsigned j = 0;
83 for (unsigned i = ofs; i < len+ofs; i++)
84 {
85 buffer[i] = (char)data[j];
86 j++;
87 }
88 ofs += len;
89 // Write the GSMTAP packet
90 pcu_gsmtap_socket.write(buffer, ofs);
91}