From 8ee6051b723032232b811c264e6fc64f0e5a4b5f Mon Sep 17 00:00:00 2001 From: Ivan Kluchnikov Date: Mon, 5 Mar 2012 19:24:57 +0400 Subject: [PATCH] Separation of GPRS PCU application from main OpenBTS code and changing PCU architecture (First step). 1. pcu - main program for GPRS PCU. 2. gprs_rlcmac - RLC/MAC layer implementation for PCU. Added list for TBF, TBF allocation, establishment, release. 3. gprs_bssgp_pcu - BSSGP protocol implementation for PCU. 4. pcu_l1_if - interface for communication PCU application with OpenBTS. --- GPRSSocket.cpp | 421 ------------------------- GPRSSocket.h | 49 --- Makefile.am | 20 +- bssgp.cpp => gprs_bssgp_pcu.cpp | 263 +++------------- bssgp.h => gprs_bssgp_pcu.h | 41 ++- gprs_rlcmac.cpp | 527 ++++++++++++++++++++++++++++++++ gprs_rlcmac.h | 88 ++++++ pcu_l1_if.cpp | 91 ++++++ RLCMAC.cpp => pcu_l1_if.h | 21 +- pcu_main.cpp | 94 ++++++ 10 files changed, 898 insertions(+), 717 deletions(-) delete mode 100644 GPRSSocket.cpp delete mode 100644 GPRSSocket.h rename bssgp.cpp => gprs_bssgp_pcu.cpp (50%) rename bssgp.h => gprs_bssgp_pcu.h (66%) create mode 100644 gprs_rlcmac.cpp create mode 100644 gprs_rlcmac.h create mode 100644 pcu_l1_if.cpp rename RLCMAC.cpp => pcu_l1_if.h (73%) create mode 100644 pcu_main.cpp diff --git a/GPRSSocket.cpp b/GPRSSocket.cpp deleted file mode 100644 index 6bcf55d7..00000000 --- a/GPRSSocket.cpp +++ /dev/null @@ -1,421 +0,0 @@ -/*GPRSSocket.cpp - * - * Copyright (C) 2011 Ivan Klyuchnikov - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include -#include -#include -#include -#include "GPRSSocket.h" -#include "bssgp.h" - -#define MAX_UDP_LENGTH 1500 - -#define RLCMAC_DATA_BLOCK 0 -#define RLCMAC_CONTROL_BLOCK 1 - -// TODO: We should take ports and IP from config. -UDPSocket GPRSRLCMACSocket(5070, "127.0.0.1", 5934); -UDPSocket GSMTAPSocket(5077, "127.0.0.1", 4729); - -void sendToGSMTAP(uint8_t * data, unsigned len) -{ - char buffer[MAX_UDP_LENGTH]; - int ofs = 0; - - // Build header - struct gsmtap_hdr *header = (struct gsmtap_hdr *)buffer; - header->version = 2; - header->hdr_len = sizeof(struct gsmtap_hdr) >> 2; - header->type = 0x08; - header->timeslot = 5; - header->arfcn = 0; - header->signal_dbm = 0; - header->snr_db = 0; - header->frame_number = 0; - header->sub_type = 0; - header->antenna_nr = 0; - header->sub_slot = 0; - header->res = 0; - - ofs += sizeof(*header); - - // Add frame data - unsigned j = 0; - for (unsigned i = ofs; i < len+ofs; i++) - { - buffer[i] = (char)data[j]; - j++; - } - ofs += len; - // Write the GSMTAP packet - GSMTAPSocket.write(buffer, ofs); -} - - -void sendToOpenBTS(BitVector * vector) -{ - char buffer[MAX_UDP_LENGTH]; - int ofs = 0; - vector->pack((unsigned char*)&buffer[ofs]); - ofs += vector->size() >> 3; - COUT("Send to OpenBTS: " << *vector); - GPRSRLCMACSocket.write(buffer, ofs); -} - -void writePDassignment(BitVector * dest, uint8_t TFI, uint32_t TLLI) -{ - // TODO We should use our implementation of encode RLC/MAC Control messages. - unsigned wp = 0; - dest->writeField(wp,0x1,2); // Payload Type - dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber - dest->writeField(wp,0x1,1); // Suppl/Polling Bit - dest->writeField(wp,0x1,3); // Uplink state flag - dest->writeField(wp,0x2,6); // MESSAGE TYPE - dest->writeField(wp,0x0,2); // Page Mode - - dest->writeField(wp,0x0,1); // switch PERSIST_LEVEL: off - dest->writeField(wp,0x2,2); // switch TLLI : on - dest->writeField(wp,TLLI,32); // TLLI - - dest->writeField(wp,0x0,1); // Message escape - dest->writeField(wp,0x0,2); // Medium Access Method: Dynamic Allocation - dest->writeField(wp,0x0,1); // RLC acknowledged mode - - dest->writeField(wp,0x0,1); // the network establishes no new downlink TBF for the mobile station - dest->writeField(wp,0x1,8); // timeslot 7 - dest->writeField(wp,0x1,8); // TIMING_ADVANCE_INDEX - - dest->writeField(wp,0x0,1); // switch TIMING_ADVANCE_VALUE = off - dest->writeField(wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on - dest->writeField(wp,0xC,4); // TIMING_ADVANCE_INDEX - dest->writeField(wp,0x7,3); // TIMING_ADVANCE_TIMESLOT_NUMBER - - dest->writeField(wp,0x0,1); // switch POWER CONTROL = off - dest->writeField(wp,0x1,1); // Frequency Parameters information elements = present - - dest->writeField(wp,0x2,3); // Training Sequence Code (TSC) = 2 - dest->writeField(wp,0x1,2); // Indirect encoding struct = present - dest->writeField(wp,0x0,6); // MAIO - dest->writeField(wp,0xE,4); // MA_Number - dest->writeField(wp,0x8,4); // CHANGE_MARK_1 CHANGE_MARK_2 - - dest->writeField(wp,0x1,1); // switch TFI : on - dest->writeField(wp,0x14,5);// TFI - - dest->writeField(wp,0x1,1); // Power Control Parameters IE = present - dest->writeField(wp,0x0,4); // ALPHA power control parameter - dest->writeField(wp,0x0,1); // switch GAMMA_TN0 = off - dest->writeField(wp,0x0,1); // switch GAMMA_TN1 = off - dest->writeField(wp,0x0,1); // switch GAMMA_TN2 = off - dest->writeField(wp,0x0,1); // switch GAMMA_TN3 = off - dest->writeField(wp,0x0,1); // switch GAMMA_TN4 = off - dest->writeField(wp,0x0,1); // switch GAMMA_TN5 = off - dest->writeField(wp,0x0,1); // switch GAMMA_TN6 = off - dest->writeField(wp,0x1,1); // switch GAMMA_TN7 = on - dest->writeField(wp,0x0,5); // GAMMA_TN7 - - dest->writeField(wp,0x0,1); // TBF Starting TIME IE not present - dest->writeField(wp,0x0,1); // Measurement Mapping struct not present -} - -void writePUassignment(BitVector * dest, uint8_t TFI, uint32_t TLLI) -{ - // TODO We should use our implementation of encode RLC/MAC Control messages. - unsigned wp = 0; - dest->writeField(wp,0x1,2); // Payload Type - dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber - dest->writeField(wp,0x1,1); // Suppl/Polling Bit - dest->writeField(wp,0x1,3); // Uplink state flag - - - dest->writeField(wp,0xa,6); // MESSAGE TYPE - - dest->writeField(wp,0x0,2); // Page Mode - - dest->writeField(wp,0x0,1); // switch PERSIST_LEVEL: off - dest->writeField(wp,0x2,2); // switch TLLI : on - dest->writeField(wp,TLLI,32); // TLLI - - dest->writeField(wp,0x0,1); // Message escape - dest->writeField(wp,0x0,2); // CHANNEL_CODING_COMMAND - dest->writeField(wp,0x0,1); // TLLI_BLOCK_CHANNEL_CODING - - dest->writeField(wp,0x1,1); // switch TIMING_ADVANCE_VALUE = on - dest->writeField(wp,0x0,6); // TIMING_ADVANCE_VALUE - dest->writeField(wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off - - dest->writeField(wp,0x0,1); // Frequency Parameters = off - - dest->writeField(wp,0x1,2); // Dynamic Allocation = off - - dest->writeField(wp,0x0,1); // Dynamic Allocation - dest->writeField(wp,0x0,1); // P0 = off - - dest->writeField(wp,0x1,1); // USF_GRANULARITY - dest->writeField(wp,0x1,1); // switch TFI : on - dest->writeField(wp,TFI,5);// TFI - - dest->writeField(wp,0x0,1); // - dest->writeField(wp,0x0,1); // TBF Starting Time = off - dest->writeField(wp,0x0,1); // Timeslot Allocation - - dest->writeField(wp,0x0,5); // USF_TN 0 - 4 - dest->writeField(wp,0x1,1); // USF_TN 5 - dest->writeField(wp,0x1,3); // USF_TN 5 - dest->writeField(wp,0x0,2); // USF_TN 6 - 7 -// dest->writeField(wp,0x0,1); // Measurement Mapping struct not present -} - -void writeIARestOctetsDownlinkAssignment(BitVector * dest, uint8_t TFI, uint32_t TLLI) -{ - // GMS 04.08 10.5.2.37b 10.5.2.16 - unsigned wp = 0; - dest->writeField(wp, 3, 2); // "HH" - dest->writeField(wp, 1, 2); // "01" Packet Downlink Assignment - dest->writeField(wp,TLLI,32); // TLLI - dest->writeField(wp,0x1,1); // switch TFI : on - dest->writeField(wp,TFI,5); // TFI - dest->writeField(wp,0x0,1); // RLC acknowledged mode - dest->writeField(wp,0x0,1); // ALPHA = present - //dest->writeField(wp,0x0,4); // ALPHA power control parameter - dest->writeField(wp,0x0,5); // GAMMA power control parameter - dest->writeField(wp,0x1,1); // Polling Bit - dest->writeField(wp,0x1,1); // TA_VALID ??? - dest->writeField(wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on - dest->writeField(wp,0xC,4); // TIMING_ADVANCE_INDEX - dest->writeField(wp,0x1,1); // TBF Starting TIME present - dest->writeField(wp,0xffff,16); // TBF Starting TIME (we should set it in OpenBTS) - dest->writeField(wp,0x0,1); // P0 not present -} - -void writePUack(BitVector * dest, uint8_t TFI, uint32_t TLLI, unsigned CV, unsigned BSN) -{ - // TODO We should use our implementation of encode RLC/MAC Control messages. - unsigned wp = 0; - dest->writeField(wp,0x1,2); // payload - dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber - if (CV == 0) dest->writeField(wp,0x1,1); // Suppl/Polling Bit - else dest->writeField(wp,0x0,1); //Suppl/Polling Bit - dest->writeField(wp,0x1,3); // Uplink state flag - - //dest->writeField(wp,0x0,1); // Reduced block sequence number - //dest->writeField(wp,BSN+6,5); // Radio transaction identifier - //dest->writeField(wp,0x1,1); // Final segment - //dest->writeField(wp,0x1,1); // Address control - - //dest->writeField(wp,0x0,2); // Power reduction: 0 - //dest->writeField(wp,TFI,5); // Temporary flow identifier - //dest->writeField(wp,0x1,1); // Direction - - dest->writeField(wp,0x09,6); // MESSAGE TYPE - dest->writeField(wp,0x0,2); // Page Mode - - dest->writeField(wp,0x0,2); - dest->writeField(wp,TFI,5); // Uplink TFI - dest->writeField(wp,0x0,1); - - dest->writeField(wp,0x0,2); // CS1 - if (CV == 0) dest->writeField(wp,0x1,1); // FINAL_ACK_INDICATION - else dest->writeField(wp,0x0,1); // FINAL_ACK_INDICATION - dest->writeField(wp,BSN+1,7); // STARTING_SEQUENCE_NUMBER - // RECEIVE_BLOCK_BITMAP - for (unsigned i=0; i<8; i++) { - dest->writeField(wp,0xff,8); - } - dest->writeField(wp,0x1,1); // CONTENTION_RESOLUTION_TLLI = present - dest->writeField(wp,TLLI,8*4); - dest->writeField(wp,0x00,4); //spare -} - -void RLCMACExtractData(uint8_t* tfi, uint32_t* tlli, RlcMacUplinkDataBlock_t * dataBlock, uint8_t* rlc_data, unsigned* dataIndex) -{ - unsigned blockDataLen = 0; - unsigned dataOctetNum = 0; - - *tfi = dataBlock->TFI; - if (dataBlock->E_1 == 0) // Extension octet follows immediately - { - // TODO We should implement case with several LLC PDU in one data block. - blockDataLen = dataBlock->LENGTH_INDICATOR[0]; - } - else - { - blockDataLen = 20; // RLC data length without 3 header octets. - if(dataBlock->TI == 1) // TLLI field is present - { - *tlli = dataBlock->TLLI; - blockDataLen -= 4; // TLLI length - if (dataBlock->PI == 1) // PFI is present if TI field indicates presence of TLLI - { - blockDataLen -= 1; // PFI length - } - } - } - - for (unsigned i = *dataIndex; i < *dataIndex + blockDataLen; i++) - { - rlc_data[i] = dataBlock->RLC_DATA[dataOctetNum]; - dataOctetNum++; - } - *dataIndex += blockDataLen; -} - -void sendUplinkAck(uint8_t tfi, uint32_t tlli, RlcMacUplinkDataBlock_t * dataBlock) -{ - BitVector packetUplinkAck(23*8); - packetUplinkAck.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); - writePUack(&packetUplinkAck, tfi, tlli, dataBlock->CV, dataBlock->BSN); - COUT("RLCMAC_CONTROL_BLOCK>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - RlcMacDownlink_t * pUA = (RlcMacDownlink_t *)malloc(sizeof(RlcMacUplink_t)); - decode_gsm_rlcmac_downlink(&packetUplinkAck, pUA); - free(pUA); - COUT("RLCMAC_CONTROL_BLOCK_END------------------------------"); - sendToOpenBTS(&packetUplinkAck); -} - -void RLCMACDispatchDataBlock(unsigned* waitData, BitVector *vector, uint8_t* tfi, uint32_t* tlli, uint8_t* rlc_data, unsigned* dataIndex) -{ - static DataBlockDispatcherState state = WaitSequenceStart; - static unsigned prevBSN = -1; - if ((*waitData == 1)&&(state == WaitNextSequence)) - { - state = WaitSequenceStart; - } - - COUT("RLCMAC_DATA_BLOCK<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); - RlcMacUplinkDataBlock_t * dataBlock = (RlcMacUplinkDataBlock_t *)malloc(sizeof(RlcMacUplinkDataBlock_t)); - decode_gsm_rlcmac_uplink_data(vector, dataBlock); - COUT("RLCMAC_DATA_BLOCK_END------------------------------"); - - switch (state) { - case WaitSequenceStart: - if (dataBlock->BSN == 0) - { - *dataIndex = 0; - RLCMACExtractData(tfi, tlli, dataBlock, rlc_data, dataIndex); - sendUplinkAck(*tfi, *tlli, dataBlock); - state = WaitNextBlock; - prevBSN = 0; - } - break; - case WaitNextBlock: - if (prevBSN == (dataBlock->BSN - 1)) - { - RLCMACExtractData(tfi, tlli, dataBlock, rlc_data, dataIndex); - sendUplinkAck(*tfi, *tlli, dataBlock); - if (dataBlock->CV == 0) - { - // Recieved last Data Block in this sequence. - sendToGSMTAP(rlc_data, *dataIndex); - state = WaitNextSequence; - prevBSN = -1; - *waitData = 0; - } - else - { - prevBSN = dataBlock->BSN; - state = WaitNextBlock; - } - } - else - { - // Recieved Data Block with unexpected BSN. - // We should try to find nesessary Data Block. - state = WaitNextBlock; - } - break; - case WaitNextSequence: - // Now we just ignore all Data Blocks and wait next Uplink TBF - break; - } - free(dataBlock); -} - -void RLCMACDispatchControlBlock(unsigned* waitData, BitVector *vector, uint8_t* tfi, uint32_t* tlli, uint8_t* rlc_data, unsigned* dataIndex) -{ - static unsigned shutUp = 0; - COUT("RLCMAC_CONTROL_BLOCK<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); - RlcMacUplink_t * controlBlock = (RlcMacUplink_t *)malloc(sizeof(RlcMacUplink_t)); - decode_gsm_rlcmac_uplink(vector, controlBlock); - COUT("RLCMAC_CONTROL_BLOCK_END------------------------------"); - switch (controlBlock->u.MESSAGE_TYPE) { - case MT_PACKET_CONTROL_ACK: - if (shutUp == 0) - { - COUT("SEND IA Rest Octets Downlink Assignment>>>>>>>>>>>>>>>>>>"); - BitVector IARestOctetsDownlinkAssignment(23*8); - IARestOctetsDownlinkAssignment.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); - writeIARestOctetsDownlinkAssignment(&IARestOctetsDownlinkAssignment, 20, *tlli); - sendToOpenBTS(&IARestOctetsDownlinkAssignment); - usleep(500000); - sendToSGSN(*tfi, *tlli, rlc_data, *dataIndex); - //sendToGSMTAP(rlc_data, *dataIndex); - shutUp = 1; - } - break; - case MT_PACKET_DOWNLINK_ACK_NACK: - COUT("SEND PacketUplinkAssignment>>>>>>>>>>>>>>>>>>"); - BitVector PacketUplinkAssignment(23*8); - PacketUplinkAssignment.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); - writePUassignment(&PacketUplinkAssignment, 21, *tlli); - sendToOpenBTS(&PacketUplinkAssignment); - *waitData = 1; - break; - } - free(controlBlock); - -} - -void RLCMACDispatchBlock(BitVector *vector) -{ - static uint8_t rlc_data[60]; - static uint8_t *tfi = (uint8_t *)malloc(sizeof(uint8_t)); - static uint32_t *tlli = (uint32_t *)malloc(sizeof(uint32_t)); - static unsigned *dataIndex = (unsigned *)malloc(sizeof(unsigned)); - static unsigned waitData = 1; - - unsigned readIndex = 0; - unsigned payload = vector->readField(readIndex, 2); - - switch (payload) { - case RLCMAC_DATA_BLOCK: - RLCMACDispatchDataBlock(&waitData,vector, tfi, tlli, rlc_data, dataIndex); - break; - case RLCMAC_CONTROL_BLOCK: - RLCMACDispatchControlBlock(&waitData, vector, tfi, tlli, rlc_data, dataIndex); - break; - default: - COUT("Unknown RLCMAC block payload\n"); - } -} - -void *RLCMACSocket(void *) -{ - BitVector *vector = new BitVector(23*8); - GPRSRLCMACSocket.nonblocking(); - while (1) { - char buf[MAX_UDP_LENGTH]; - int count = GPRSRLCMACSocket.read(buf, 3000); - if (count>0) { - vector->unpack((const unsigned char*)buf); - COUT("Recieve from OpenBTS (MS): " << *vector); - RLCMACDispatchBlock(vector); - } - } -} diff --git a/GPRSSocket.h b/GPRSSocket.h deleted file mode 100644 index decce6bc..00000000 --- a/GPRSSocket.h +++ /dev/null @@ -1,49 +0,0 @@ -/*GPRSSocket.h - * - * Copyright (C) 2011 Ivan Klyuchnikov - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#ifndef GPRSSOCKET_H -#define GPRSSOCKET_H - -#include -#include "gsm_rlcmac.h" - - -enum DataBlockDispatcherState { - WaitSequenceStart, - WaitNextBlock, - WaitNextSequence -}; - -void sendToGSMTAP(uint8_t * data, unsigned len); - -void sendToOpenBTS(BitVector * vector); - -void writePUack(BitVector * dest, uint8_t TFI, uint32_t TLLI, unsigned CV, unsigned BSN); - -void RLCMACExtractData(uint8_t* tfi, uint32_t* tlli, RlcMacUplinkDataBlock_t * dataBlock, uint8_t* rlc_data, unsigned* dataIndex); - -void sendUplinkAck(uint8_t tfi, uint32_t tlli, RlcMacUplinkDataBlock_t * dataBlock); - -void RLCMACDispatchDataBlock(BitVector *vector, uint8_t* tfi, uint32_t* tlli, uint8_t* rlc_data, unsigned* dataIndex); - -void RLCMACDispatchBlock(BitVector *vector); - -void *RLCMACSocket(void *); - -#endif // GPRSSOCKET_H diff --git a/Makefile.am b/Makefile.am index 3808f835..3ecb896f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,29 +28,31 @@ noinst_LTLIBRARIES = libgprs.la libgprs_la_SOURCES = \ csn1.cpp \ gsm_rlcmac.cpp \ - bssgp.cpp \ - GPRSSocket.cpp + gprs_bssgp_pcu.cpp \ + gprs_rlcmac.cpp \ + pcu_l1_if.cpp noinst_PROGRAMS = \ RLCMACTest \ - RLCMAC + pcu noinst_HEADERS = \ csn1.h \ gsm_rlcmac.h \ - bssgp.h \ - GPRSSocket.h + gprs_bssgp_pcu.h \ + gprs_rlcmac.h \ + pcu_l1_if.h RLCMACTest_SOURCES = RLCMACTest.cpp RLCMACTest_LDADD = \ libgprs.la \ $(COMMON_LA) -#OPENBSC_DIR = /path to OpenBSC dir -#OPENGGSN_DIR = /path to OpenGGGSN dir +OPENBSC_DIR = /home/ivan/work/openbsc/openbsc/openbsc +OPENGGSN_DIR = /home/ivan/work/openbsc/openggsn -RLCMAC_SOURCES = RLCMAC.cpp -RLCMAC_LDADD = \ +pcu_SOURCES = pcu_main.cpp +pcu_LDADD = \ libgprs.la \ $(OPENBSC_DIR)/src/libgb/gprs_ns.o \ $(OPENBSC_DIR)/src/libgb/gprs_bssgp.o \ diff --git a/bssgp.cpp b/gprs_bssgp_pcu.cpp similarity index 50% rename from bssgp.cpp rename to gprs_bssgp_pcu.cpp index 696e194f..4ccea440 100644 --- a/bssgp.cpp +++ b/gprs_bssgp_pcu.cpp @@ -1,6 +1,6 @@ -/* bssgp.cpp +/* gprs_bssgp_pcu.cpp * - * Copyright (C) 2011 Ivan Klyuchnikov + * Copyright (C) 2012 Ivan Klyuchnikov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -17,98 +17,62 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include -#include -#include "GPRSSocket.h" -#include "gsm_rlcmac.h" -#include "bssgp.h" +#include +#include +#include -// TODO: We should move this parameters to config file. -#define SGSN_IP "127.0.0.1" -#define SGSN_PORT 23000 -#define CELL_ID 3 -#define BVCI 7 -#define NSEI 3 -#define NSVCI 4 -#define MNC 1 -#define MCC 1 -#define LAC 1000 -#define RAC 1 - -#define QOS_PROFILE 0 -#define BSSGP_HDR_LEN 20 -#define NS_HDR_LEN 4 -#define MAX_LEN_PDU 100 -#define IE_PDU 14 -#define BLOCK_DATA_LEN 19 - -#define BLOCK_LEN 23 - -uint16_t bvci = BVCI; -uint16_t nsei = NSEI; -uint8_t TFI; -struct bssgp_bvc_ctx *bctx = btsctx_alloc(bvci, nsei); -struct gprs_nsvc *nsvc; -struct gprs_ns_inst *sgsn_nsi; struct sgsn_instance *sgsn; void *tall_bsc_ctx; +struct bssgp_bvc_ctx *bctx = btsctx_alloc(BVCI, NSEI); -// Send RLC data to OpenBTS. -void sendRLC(uint32_t tlli, uint8_t *pdu, unsigned startIndex, unsigned endIndex, unsigned bsn, unsigned fbi) +int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg) { - unsigned spareLen = 0; - BitVector resultVector(BLOCK_LEN*8); - resultVector.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); - RlcMacDownlinkDataBlock_t * dataBlock = (RlcMacDownlinkDataBlock_t *)malloc(sizeof(RlcMacDownlinkDataBlock_t)); - dataBlock->PAYLOAD_TYPE = 0; - dataBlock->RRBP = 0; - dataBlock->SP = 1; - dataBlock->USF = 1; - dataBlock->PR = 0; - dataBlock->TFI = 20; - dataBlock->FBI = fbi; - dataBlock->BSN = bsn; - if ((endIndex-startIndex) < 20) - { - dataBlock->E_1 = 0; - dataBlock->LENGTH_INDICATOR[0] = endIndex-startIndex; - dataBlock->M[0] = 0; - dataBlock->E[0] = 1; - spareLen = 19 - dataBlock->LENGTH_INDICATOR[0]; + struct bssgp_ud_hdr *budh; + int tfi; + int data_index = 0; + int i = 0; + int pdu_index = 0; + + budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg); + struct gprs_rlcmac_tbf *tbf; + // Create new TBF + tfi = tfi_alloc(); + if (tfi < 0) { + return tfi; } - else + tbf = tbf_alloc(tfi); + tbf->direction = GPRS_RLCMAC_DL_TBF; + tbf->state = GPRS_RLCMAC_WAIT_DATA_SEQ_START; + tbf->tlli = ntohl(budh->tlli); + + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP TLLI=0x%08x \n", ntohl(budh->tlli)); + for (i = 4; i < MAX_LEN_PDU; i++) { - dataBlock->E_1 = 1; + //LOGP(DBSSGP, LOGL_NOTICE, "SERCH data = -0x%02x\n", budh ->data[i]); + if(budh->data[i] == IE_PDU) + { + pdu_index = i + 2; + break; + } } - unsigned j = 0; - for(unsigned i = startIndex; i < endIndex; i++) + for (i = pdu_index; i < pdu_index + (budh->data[pdu_index-1]&0x7f); i++) { - dataBlock->RLC_DATA[j] = pdu[i]; - j++; + //LOGP(DBSSGP, LOGL_NOTICE, "-0x%02x\n", budh ->data[i]); + tbf->rlc_data[data_index] = budh->data[i]; + data_index++; } - for(unsigned i = j; i < j + spareLen; i++) - { - dataBlock->RLC_DATA[i] = 0x2b; - } - encode_gsm_rlcmac_downlink_data(&resultVector, dataBlock); - free(dataBlock); - sendToOpenBTS(&resultVector); + DEBUGP(DBSSGP, "BSSGP Catch from SGSN=%u octets. Send it to OpenBTS.\n", data_index); + gsmtap_send_llc(tbf->rlc_data,data_index); + tbf->data_index = data_index; + gprs_rlcmac_segment_llc_pdu(tbf); } /* Receive a BSSGP PDU from a BSS on a PTP BVCI */ -int gprs_bssgp_bss_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) +int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) { struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); uint8_t pdu_type = bgph->pdu_type; - uint8_t pdu[MAX_LEN_PDU]; unsigned rc = 0; - unsigned dataIndex = 0; - unsigned numBlocks = 0; - unsigned i = 0; - unsigned j = 0; - unsigned pduIndex = 0; - unsigned fbi = 0; - struct bssgp_ud_hdr *budh; /* If traffic is received on a BVC that is marked as blocked, the * received PDU shall not be accepted and a STATUS PDU (Cause value: @@ -123,55 +87,7 @@ int gprs_bssgp_bss_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_ switch (pdu_type) { case BSSGP_PDUT_DL_UNITDATA: LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_DL_UNITDATA\n"); - budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); - LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP TLLI=0x%08x \n", ntohl(budh->tlli)); - for (i = 4; i < MAX_LEN_PDU; i++) - { - //LOGP(DBSSGP, LOGL_NOTICE, "SERCH data = -0x%02x\n", budh ->data[i]); - if(budh ->data[i] == IE_PDU) - { - pduIndex = i+2; - break; - } - } - for (i = pduIndex; i < pduIndex + (budh->data[pduIndex-1]&0x7f); i++) - { - //LOGP(DBSSGP, LOGL_NOTICE, "-0x%02x\n", budh ->data[i]); - pdu[dataIndex] = budh ->data[i]; - dataIndex++; - } - DEBUGP(DBSSGP, "BSSGP Catch from SGSN=%u octets. Send it to OpenBTS.\n", dataIndex); - sendToGSMTAP(pdu,dataIndex); - if (dataIndex > BLOCK_DATA_LEN + 1) - { - int blockDataLen = BLOCK_DATA_LEN; - numBlocks = dataIndex/BLOCK_DATA_LEN; - int ost = dataIndex%BLOCK_DATA_LEN; - int startIndex = 0; - int endIndex = 0; - if (dataIndex%BLOCK_DATA_LEN > 0) - { - numBlocks++; - } - for (i = 0; i < numBlocks; i++) - { - if (i == numBlocks-1) - { - if (ost > 0) - { - blockDataLen = ost; - } - fbi = 1; - } - endIndex = startIndex + blockDataLen; - sendRLC(ntohl(budh->tlli), pdu, startIndex, endIndex, i, fbi); - startIndex += blockDataLen; - } - } - else - { - sendRLC(ntohl(budh->tlli), pdu, 0, dataIndex, 0, 1); - } + gprs_bssgp_pcu_rx_dl_ud(msg); break; case BSSGP_PDUT_PAGING_PS: LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n"); @@ -197,7 +113,7 @@ int gprs_bssgp_bss_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_ } /* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */ -int gprs_bssgp_bss_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) +int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) { struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); int rc = 0; @@ -248,7 +164,7 @@ int gprs_bssgp_bss_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp return rc; } -int gprs_bssgp_bss_rcvmsg(struct msgb *msg) +int gprs_bssgp_pcu_rcvmsg(struct msgb *msg) { struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); @@ -299,7 +215,7 @@ int gprs_bssgp_bss_rcvmsg(struct msgb *msg) if (ns_bvci == BVCI_SIGNALLING) { LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n"); - rc = gprs_bssgp_bss_rx_sign(msg, &tp, bctx); + rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx); } else if (ns_bvci == BVCI_PTM) { @@ -309,94 +225,7 @@ int gprs_bssgp_bss_rcvmsg(struct msgb *msg) else { LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTP gprs_bssgp_rx_ptp\n"); - rc = gprs_bssgp_bss_rx_ptp(msg, &tp, bctx); + rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx); } return rc; } - - -int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci) -{ - int rc = 0; - switch (event) { - case GPRS_NS_EVT_UNIT_DATA: - /* hand the message into the BSSGP implementation */ - rc = gprs_bssgp_bss_rcvmsg(msg); - break; - default: - LOGP(DGPRS, LOGL_ERROR, "RLCMAC: Unknown event %u from NS\n", event); - if (msg) - talloc_free(msg); - rc = -EIO; - break; - } - return rc; -} - -// Send RLC data to SGSN. -void sendToSGSN(uint8_t tfi, uint32_t tlli, uint8_t * rlc_data, unsigned dataLen) -{ - const uint8_t qos_profile = QOS_PROFILE; - struct msgb *llc_pdu; - unsigned msgLen = NS_HDR_LEN + BSSGP_HDR_LEN + dataLen; - TFI = tfi; - bctx->cell_id = CELL_ID; - bctx->nsei = NSEI; - bctx->ra_id.mnc = MNC; - bctx->ra_id.mcc = MCC; - bctx->ra_id.lac = LAC; - bctx->ra_id.rac = RAC; - bctx->bvci = BVCI; - LOGP(DBSSGP, LOGL_DEBUG, "Data len %u TLLI 0x%08x , TFI 0x%02x", dataLen, tlli, tfi); - //for (unsigned i = 0; i < dataLen; i++) - // LOGP(DBSSGP, LOGL_DEBUG, " Data[%u] = %u", i, rlc_data[i]); - llc_pdu = msgb_alloc_headroom(msgLen, msgLen,"llc_pdu"); - msgb_tvlv_push(llc_pdu, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*dataLen, rlc_data); - bssgp_tx_ul_ud(bctx, tlli, &qos_profile, llc_pdu); -} - -void RLCMACServer() -{ - uint16_t nsvci = NSVCI; - - // Socket for reading BitVectors (RLC/MAC Frames) from OpenBTS application. - Thread RLCMACInterface; - RLCMACInterface.start(RLCMACSocket,NULL); - - osmo_init_logging(&log_info); - sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb); - bssgp_nsi = sgsn_nsi; - - if (!bssgp_nsi) - { - LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); - exit(1); - } - - bctx->cell_id = CELL_ID; - bctx->nsei = NSEI; - bctx->ra_id.mnc = MNC; - bctx->ra_id.mcc = MCC; - bctx->ra_id.lac = LAC; - bctx->ra_id.rac = RAC; - bctx->bvci = BVCI; - uint8_t cause = 39; - gprs_ns_nsip_listen(sgsn_nsi); - - struct sockaddr_in dest; - dest.sin_family = AF_INET; - dest.sin_port = htons(SGSN_PORT); - inet_aton(SGSN_IP, &dest.sin_addr); - - nsvc = nsip_connect(sgsn_nsi, &dest, nsei, nsvci); - unsigned i = 0; - while (1) - { - osmo_select_main(0); - if (i == 7) - { - bssgp_tx_bvc_reset(bctx, bvci, cause); - } - i++; - } -} diff --git a/bssgp.h b/gprs_bssgp_pcu.h similarity index 66% rename from bssgp.h rename to gprs_bssgp_pcu.h index 1a6c1b1e..1308cf7a 100644 --- a/bssgp.h +++ b/gprs_bssgp_pcu.h @@ -1,6 +1,6 @@ -/* bssgp.h +/* gprs_bssgp_pcu.h * - * Copyright (C) 2011 Ivan Klyuchnikov + * Copyright (C) 2012 Ivan Klyuchnikov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -16,9 +16,10 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#ifndef BSSGP_H -#define BSSGP_H + +#ifndef GPRS_BSSGP_PCU_H +#define GPRS_BSSGP_PCU_H + extern "C" { #include @@ -36,18 +37,32 @@ int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli, const uint8_t *qos struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei); } -void sendRLC(uint32_t tlli, uint8_t *pdu, unsigned startIndex, unsigned endIndex, unsigned bsn); +#define BVCI 7 +#define NSEI 3 -int gprs_bssgp_bss_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx); +#define QOS_PROFILE 0 +#define BSSGP_HDR_LEN 20 +#define NS_HDR_LEN 4 +#define MAX_LEN_PDU 60 +#define IE_PDU 14 +#define BLOCK_DATA_LEN 19 +#define BLOCK_LEN 23 -int gprs_bssgp_bss_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx); +#define CELL_ID 3 +#define MNC 55 +#define MCC 905 +#define PCU_LAC 1000 +#define PCU_RAC 1 -int gprs_bssgp_bss_rcvmsg(struct msgb *msg); -int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci); +extern struct bssgp_bvc_ctx *bctx; -void sendToSGSN(uint8_t tfi, uint32_t tlli, uint8_t * rlc_data, unsigned dataLen); +int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg); -void RLCMACServer(); +int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx); -#endif // BSSGP_H +int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx); + +int gprs_bssgp_pcu_rcvmsg(struct msgb *msg); + +#endif // GPRS_BSSGP_PCU_H diff --git a/gprs_rlcmac.cpp b/gprs_rlcmac.cpp new file mode 100644 index 00000000..c350a7bc --- /dev/null +++ b/gprs_rlcmac.cpp @@ -0,0 +1,527 @@ +/* gprs_rlcmac.cpp + * + * Copyright (C) 2012 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include +#include +#include + + +LLIST_HEAD(gprs_rlcmac_tbfs); +void *rlcmac_tall_ctx; + +int tfi_alloc() +{ + struct gprs_rlcmac_tbf *tbf; + uint32_t tfi_map = 0; + uint32_t tfi_ind = 0; + uint32_t mask = 1; + uint8_t i; + + llist_for_each_entry(tbf, &gprs_rlcmac_tbfs, list) { + tfi_ind = 1 << tbf->tfi; + tfi_map = tfi_map|tfi_ind; + } + + for (i = 0; i < 32; i++) { + if(((tfi_map >> i) & mask) == 0) { + return i; + } + } + return -1; +} + +/* lookup TBF Entity (by TFI) */ +static struct gprs_rlcmac_tbf *tbf_by_tfi(uint8_t tfi) +{ + struct gprs_rlcmac_tbf *tbf; + + llist_for_each_entry(tbf, &gprs_rlcmac_tbfs, list) { + if (tbf->tfi == tfi) + return tbf; + } + return NULL; +} + +static struct gprs_rlcmac_tbf *tbf_by_tlli(uint8_t tlli) +{ + struct gprs_rlcmac_tbf *tbf; + + llist_for_each_entry(tbf, &gprs_rlcmac_tbfs, list) { + if ((tbf->tlli == tlli)&&(tbf->direction == GPRS_RLCMAC_UL_TBF)) + return tbf; + } + return NULL; +} + +struct gprs_rlcmac_tbf *tbf_alloc(uint8_t tfi) +{ + struct gprs_rlcmac_tbf *tbf; + + tbf = talloc_zero(rlcmac_tall_ctx, struct gprs_rlcmac_tbf); + if (!tbf) + return NULL; + + tbf->tfi = tfi; + + llist_add(&tbf->list, &gprs_rlcmac_tbfs); + + return tbf; +} + +static void tbf_free(struct gprs_rlcmac_tbf *tbf) +{ + llist_del(&tbf->list); + talloc_free(tbf); +} + +void write_packet_downlink_assignment(BitVector * dest, uint8_t tfi, uint32_t tlli) +{ + // TODO We should use our implementation of encode RLC/MAC Control messages. + unsigned wp = 0; + dest->writeField(wp,0x1,2); // Payload Type + dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber + dest->writeField(wp,0x1,1); // Suppl/Polling Bit + dest->writeField(wp,0x1,3); // Uplink state flag + dest->writeField(wp,0x2,6); // MESSAGE TYPE + dest->writeField(wp,0x0,2); // Page Mode + + dest->writeField(wp,0x0,1); // switch PERSIST_LEVEL: off + dest->writeField(wp,0x2,2); // switch TLLI : on + dest->writeField(wp,tlli,32); // TLLI + + dest->writeField(wp,0x0,1); // Message escape + dest->writeField(wp,0x0,2); // Medium Access Method: Dynamic Allocation + dest->writeField(wp,0x0,1); // RLC acknowledged mode + + dest->writeField(wp,0x0,1); // the network establishes no new downlink TBF for the mobile station + dest->writeField(wp,0x1,8); // timeslot 7 + dest->writeField(wp,0x1,8); // TIMING_ADVANCE_INDEX + + dest->writeField(wp,0x0,1); // switch TIMING_ADVANCE_VALUE = off + dest->writeField(wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on + dest->writeField(wp,0xC,4); // TIMING_ADVANCE_INDEX + dest->writeField(wp,0x7,3); // TIMING_ADVANCE_TIMESLOT_NUMBER + + dest->writeField(wp,0x0,1); // switch POWER CONTROL = off + dest->writeField(wp,0x1,1); // Frequency Parameters information elements = present + + dest->writeField(wp,0x2,3); // Training Sequence Code (TSC) = 2 + dest->writeField(wp,0x1,2); // Indirect encoding struct = present + dest->writeField(wp,0x0,6); // MAIO + dest->writeField(wp,0xE,4); // MA_Number + dest->writeField(wp,0x8,4); // CHANGE_MARK_1 CHANGE_MARK_2 + + dest->writeField(wp,0x1,1); // switch TFI : on + dest->writeField(wp,tfi,5);// TFI + + dest->writeField(wp,0x1,1); // Power Control Parameters IE = present + dest->writeField(wp,0x0,4); // ALPHA power control parameter + dest->writeField(wp,0x0,1); // switch GAMMA_TN0 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN1 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN2 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN3 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN4 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN5 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN6 = off + dest->writeField(wp,0x1,1); // switch GAMMA_TN7 = on + dest->writeField(wp,0x0,5); // GAMMA_TN7 + + dest->writeField(wp,0x0,1); // TBF Starting TIME IE not present + dest->writeField(wp,0x0,1); // Measurement Mapping struct not present +} + +void write_packet_uplink_assignment(BitVector * dest, uint8_t tfi, uint32_t tlli) +{ + // TODO We should use our implementation of encode RLC/MAC Control messages. + unsigned wp = 0; + dest->writeField(wp,0x1,2); // Payload Type + dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber + dest->writeField(wp,0x1,1); // Suppl/Polling Bit + dest->writeField(wp,0x1,3); // Uplink state flag + + + dest->writeField(wp,0xa,6); // MESSAGE TYPE + + dest->writeField(wp,0x0,2); // Page Mode + + dest->writeField(wp,0x0,1); // switch PERSIST_LEVEL: off + dest->writeField(wp,0x2,2); // switch TLLI : on + dest->writeField(wp,tlli,32); // TLLI + + dest->writeField(wp,0x0,1); // Message escape + dest->writeField(wp,0x0,2); // CHANNEL_CODING_COMMAND + dest->writeField(wp,0x0,1); // TLLI_BLOCK_CHANNEL_CODING + + dest->writeField(wp,0x1,1); // switch TIMING_ADVANCE_VALUE = on + dest->writeField(wp,0x0,6); // TIMING_ADVANCE_VALUE + dest->writeField(wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off + + dest->writeField(wp,0x0,1); // Frequency Parameters = off + + dest->writeField(wp,0x1,2); // Dynamic Allocation = off + + dest->writeField(wp,0x0,1); // Dynamic Allocation + dest->writeField(wp,0x0,1); // P0 = off + + dest->writeField(wp,0x1,1); // USF_GRANULARITY + dest->writeField(wp,0x1,1); // switch TFI : on + dest->writeField(wp,tfi,5);// TFI + + dest->writeField(wp,0x0,1); // + dest->writeField(wp,0x0,1); // TBF Starting Time = off + dest->writeField(wp,0x0,1); // Timeslot Allocation + + dest->writeField(wp,0x0,5); // USF_TN 0 - 4 + dest->writeField(wp,0x1,1); // USF_TN 5 + dest->writeField(wp,0x1,3); // USF_TN 5 + dest->writeField(wp,0x0,2); // USF_TN 6 - 7 +// dest->writeField(wp,0x0,1); // Measurement Mapping struct not present +} + +void write_ia_rest_octets_downlink_assignment(BitVector * dest, uint8_t tfi, uint32_t tlli) +{ + // GMS 04.08 10.5.2.37b 10.5.2.16 + unsigned wp = 0; + dest->writeField(wp, 3, 2); // "HH" + dest->writeField(wp, 1, 2); // "01" Packet Downlink Assignment + dest->writeField(wp,tlli,32); // TLLI + dest->writeField(wp,0x1,1); // switch TFI : on + dest->writeField(wp,tfi,5); // TFI + dest->writeField(wp,0x0,1); // RLC acknowledged mode + dest->writeField(wp,0x0,1); // ALPHA = present + //dest->writeField(wp,0x0,4); // ALPHA power control parameter + dest->writeField(wp,0x0,5); // GAMMA power control parameter + dest->writeField(wp,0x1,1); // Polling Bit + dest->writeField(wp,0x1,1); // TA_VALID ??? + dest->writeField(wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on + dest->writeField(wp,0xC,4); // TIMING_ADVANCE_INDEX + dest->writeField(wp,0x1,1); // TBF Starting TIME present + dest->writeField(wp,0xffff,16); // TBF Starting TIME (we should set it in OpenBTS) + dest->writeField(wp,0x0,1); // P0 not present +} + +void write_packet_uplink_ack(BitVector * dest, uint8_t tfi, uint32_t tlli, unsigned cv, unsigned bsn) +{ + // TODO We should use our implementation of encode RLC/MAC Control messages. + unsigned wp = 0; + dest->writeField(wp,0x1,2); // payload + dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber + if (cv == 0) dest->writeField(wp,0x1,1); // Suppl/Polling Bit + else dest->writeField(wp,0x0,1); //Suppl/Polling Bit + dest->writeField(wp,0x1,3); // Uplink state flag + + //dest->writeField(wp,0x0,1); // Reduced block sequence number + //dest->writeField(wp,BSN+6,5); // Radio transaction identifier + //dest->writeField(wp,0x1,1); // Final segment + //dest->writeField(wp,0x1,1); // Address control + + //dest->writeField(wp,0x0,2); // Power reduction: 0 + //dest->writeField(wp,TFI,5); // Temporary flow identifier + //dest->writeField(wp,0x1,1); // Direction + + dest->writeField(wp,0x09,6); // MESSAGE TYPE + dest->writeField(wp,0x0,2); // Page Mode + + dest->writeField(wp,0x0,2); + dest->writeField(wp,tfi,5); // Uplink TFI + dest->writeField(wp,0x0,1); + + dest->writeField(wp,0x0,2); // CS1 + if (cv == 0) dest->writeField(wp,0x1,1); // FINAL_ACK_INDICATION + else dest->writeField(wp,0x0,1); // FINAL_ACK_INDICATION + dest->writeField(wp,bsn + 1,7); // STARTING_SEQUENCE_NUMBER + // RECEIVE_BLOCK_BITMAP + for (unsigned i=0; i<8; i++) { + dest->writeField(wp,0xff,8); + } + dest->writeField(wp,0x1,1); // CONTENTION_RESOLUTION_TLLI = present + dest->writeField(wp,tlli,8*4); + dest->writeField(wp,0x00,4); //spare +} + +void gprs_rlcmac_tx_ul_ack(uint8_t tfi, uint32_t tlli, RlcMacUplinkDataBlock_t * ul_data_block) +{ + BitVector packet_uplink_ack_vec(23*8); + packet_uplink_ack_vec.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); + write_packet_uplink_ack(&packet_uplink_ack_vec, tfi, tlli, ul_data_block->CV, ul_data_block->BSN); + COUT("RLCMAC_CONTROL_BLOCK>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + RlcMacDownlink_t * packet_uplink_ack = (RlcMacDownlink_t *)malloc(sizeof(RlcMacUplink_t)); + decode_gsm_rlcmac_downlink(&packet_uplink_ack_vec, packet_uplink_ack); + free(packet_uplink_ack); + COUT("RLCMAC_CONTROL_BLOCK_END------------------------------"); + pcu_l1if_tx(&packet_uplink_ack_vec); +} + +void gprs_rlcmac_data_block_parse(gprs_rlcmac_tbf* tbf, RlcMacUplinkDataBlock_t * ul_data_block) +{ + unsigned block_data_len = 0; + unsigned data_octet_num = 0; + + if (ul_data_block->E_1 == 0) // Extension octet follows immediately + { + // TODO We should implement case with several LLC PDU in one data block. + block_data_len = ul_data_block->LENGTH_INDICATOR[0]; + } + else + { + block_data_len = 20; // RLC data length without 3 header octets. + if(ul_data_block->TI == 1) // TLLI field is present + { + tbf->tlli = ul_data_block->TLLI; + block_data_len -= 4; // TLLI length + if (ul_data_block->PI == 1) // PFI is present if TI field indicates presence of TLLI + { + block_data_len -= 1; // PFI length + } + } + } + + for (unsigned i = tbf->data_index; i < tbf->data_index + block_data_len; i++) + { + tbf->rlc_data[i] = ul_data_block->RLC_DATA[data_octet_num]; + data_octet_num++; + } + tbf->data_index += block_data_len; +} + +/* Received Uplink RLC data block. */ +int gprs_rlcmac_rcv_data_block(BitVector *rlc_block) +{ + struct gprs_rlcmac_tbf *tbf; + + COUT("RLCMAC_DATA_BLOCK<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); + RlcMacUplinkDataBlock_t * ul_data_block = (RlcMacUplinkDataBlock_t *)malloc(sizeof(RlcMacUplinkDataBlock_t)); + decode_gsm_rlcmac_uplink_data(rlc_block, ul_data_block); + COUT("RLCMAC_DATA_BLOCK_END------------------------------"); + + tbf = tbf_by_tfi(ul_data_block->TFI); + if (!tbf) { + tbf = tbf_alloc(ul_data_block->TFI); + if (tbf) { + tbf->direction = GPRS_RLCMAC_UL_TBF; + tbf->state = GPRS_RLCMAC_WAIT_DATA_SEQ_START; + } else { + return 0; + } + } + + switch (tbf->state) { + case GPRS_RLCMAC_WAIT_DATA_SEQ_START: + if (ul_data_block->BSN == 0) { + tbf->data_index = 0; + gprs_rlcmac_data_block_parse(tbf, ul_data_block); + gprs_rlcmac_tx_ul_ack(tbf->tfi, tbf->tlli, ul_data_block); + tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK; + tbf->bsn = ul_data_block->BSN; + } + break; + case GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK: + if (tbf->bsn == (ul_data_block->BSN - 1)) { + gprs_rlcmac_data_block_parse(tbf, ul_data_block); + gprs_rlcmac_tx_ul_ack(tbf->tfi, tbf->tlli, ul_data_block); + if (ul_data_block->CV == 0) { + // Recieved last Data Block in this sequence. + gsmtap_send_llc(tbf->rlc_data, tbf->data_index); + tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_SEQ; + } else { + tbf->bsn = ul_data_block->BSN; + tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK; + } + } else { + // Recieved Data Block with unexpected BSN. + // We should try to find nesessary Data Block. + tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK; + } + break; + case GPRS_RLCMAC_WAIT_NEXT_DATA_SEQ: + // Now we just ignore all Data Blocks and wait next Uplink TBF + break; + } + + free(ul_data_block); + return 1; +} + +/* Received Uplink RLC control block. */ +int gprs_rlcmac_rcv_control_block(BitVector *rlc_block) +{ + //static unsigned shutUp = 0; + uint8_t tfi = 0; + uint32_t tlli = 0; + struct gprs_rlcmac_tbf *tbf; + + COUT("RLCMAC_CONTROL_BLOCK<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); + RlcMacUplink_t * ul_control_block = (RlcMacUplink_t *)malloc(sizeof(RlcMacUplink_t)); + decode_gsm_rlcmac_uplink(rlc_block, ul_control_block); + COUT("RLCMAC_CONTROL_BLOCK_END------------------------------"); + + //gprs_rlcmac_control_block_get_tfi_tlli(ul_control_block, &tfi, &tlli); + tbf = tbf_by_tfi(tfi); + if (!tbf) { + return 0; + } + + switch (ul_control_block->u.MESSAGE_TYPE) { + case MT_PACKET_CONTROL_ACK: + /* + COUT("SEND IA Rest Octets Downlink Assignment>>>>>>>>>>>>>>>>>>"); + BitVector IARestOctetsDownlinkAssignment(23*8); + IARestOctetsDownlinkAssignment.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); + writeIARestOctetsDownlinkAssignment(&IARestOctetsDownlinkAssignment, 20, *tlli); + sendToOpenBTS(&IARestOctetsDownlinkAssignment); + */ + //usleep(500000); + tlli = ul_control_block->u.Packet_Control_Acknowledgement.TLLI; + tbf = tbf_by_tlli(tlli); + if (!tbf) { + return 0; + } + gprs_rlcmac_tx_ul_ud(tbf); + tbf_free(tbf); + break; + case MT_PACKET_DOWNLINK_ACK_NACK: + tfi = ul_control_block->u.Packet_Downlink_Ack_Nack.DOWNLINK_TFI; + tbf = tbf_by_tfi(tfi); + if (!tbf) { + return 0; + } + //COUT("SEND PacketUplinkAssignment>>>>>>>>>>>>>>>>>>"); + //BitVector PacketUplinkAssignment(23*8); + //PacketUplinkAssignment.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); + //writePUassignment(&PacketUplinkAssignment, tbf->tfi, tbf->tlli); + //sendToOpenBTS(&PacketUplinkAssignment); + break; + } + free(ul_control_block); + return 1; +} + +void gprs_rlcmac_rcv_block(BitVector *rlc_block) +{ + unsigned readIndex = 0; + unsigned payload = rlc_block->readField(readIndex, 2); + + switch (payload) { + case GPRS_RLCMAC_DATA_BLOCK: + gprs_rlcmac_rcv_data_block(rlc_block); + break; + case GPRS_RLCMAC_CONTROL_BLOCK: + gprs_rlcmac_rcv_control_block(rlc_block); + break; + case GPRS_RLCMAC_CONTROL_BLOCK_OPT: + COUT("GPRS_RLCMAC_CONTROL_BLOCK_OPT block payload is not supported.\n"); + default: + COUT("Unknown RLCMAC block payload.\n"); + } +} + +// Send RLC data to OpenBTS. +void gprs_rlcmac_tx_dl_data_block(uint32_t tlli, uint8_t *pdu, int start_index, int end_index, uint8_t bsn, uint8_t fbi) +{ + int spare_len = 0; + BitVector data_block_vector(BLOCK_LEN*8); + data_block_vector.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); + RlcMacDownlinkDataBlock_t * data_block = (RlcMacDownlinkDataBlock_t *)malloc(sizeof(RlcMacDownlinkDataBlock_t)); + data_block->PAYLOAD_TYPE = 0; + data_block->RRBP = 0; + data_block->SP = 1; + data_block->USF = 1; + data_block->PR = 0; + data_block->TFI = 20; + data_block->FBI = fbi; + data_block->BSN = bsn; + if ((end_index - start_index) < 20) { + data_block->E_1 = 0; + data_block->LENGTH_INDICATOR[0] = end_index-start_index; + data_block->M[0] = 0; + data_block->E[0] = 1; + spare_len = 19 - data_block->LENGTH_INDICATOR[0]; + } else { + data_block->E_1 = 1; + } + int j = 0; + int i = 0; + for(i = start_index; i < end_index; i++) { + data_block->RLC_DATA[j] = pdu[i]; + j++; + } + + for(i = j; i < j + spare_len; i++) { + data_block->RLC_DATA[i] = 0x2b; + } + encode_gsm_rlcmac_downlink_data(&data_block_vector, data_block); + free(data_block); + pcu_l1if_tx(&data_block_vector); +} + +int gprs_rlcmac_segment_llc_pdu(struct gprs_rlcmac_tbf *tbf) +{ + int fbi = 0; + int num_blocks = 0; + int i; + + if (tbf->data_index > BLOCK_DATA_LEN + 1) + { + int block_data_len = BLOCK_DATA_LEN; + num_blocks = tbf->data_index/BLOCK_DATA_LEN; + int rest_len = tbf->data_index%BLOCK_DATA_LEN; + int start_index = 0; + int end_index = 0; + if (tbf->data_index%BLOCK_DATA_LEN > 0) + { + num_blocks++; + } + for (i = 0; i < num_blocks; i++) + { + if (i == num_blocks-1) + { + if (rest_len > 0) + { + block_data_len = rest_len; + } + fbi = 1; + } + end_index = start_index + block_data_len; + gprs_rlcmac_tx_dl_data_block(tbf->tlli, tbf->rlc_data, start_index, end_index, i, fbi); + start_index += block_data_len; + } + } + else + { + gprs_rlcmac_tx_dl_data_block(tbf->tlli, tbf->rlc_data, 0, tbf->data_index, 0, 1); + } +} + +/* Send Uplink unit-data to SGSN. */ +void gprs_rlcmac_tx_ul_ud(gprs_rlcmac_tbf *tbf) +{ + const uint8_t qos_profile = QOS_PROFILE; + struct msgb *llc_pdu; + unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + tbf->data_index; + + LOGP(DBSSGP, LOGL_DEBUG, "Data len %u TLLI 0x%08x , TFI 0x%02x", tbf->data_index, tbf->tlli, tbf->tfi); + //for (unsigned i = 0; i < dataLen; i++) + // LOGP(DBSSGP, LOGL_DEBUG, " Data[%u] = %u", i, rlc_data[i]); + + llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu"); + msgb_tvlv_push(llc_pdu, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*tbf->data_index, tbf->rlc_data); + bssgp_tx_ul_ud(bctx, tbf->tlli, &qos_profile, llc_pdu); +} + diff --git a/gprs_rlcmac.h b/gprs_rlcmac.h new file mode 100644 index 00000000..c67363c9 --- /dev/null +++ b/gprs_rlcmac.h @@ -0,0 +1,88 @@ +/* gprs_rlcmac.h + * + * Copyright (C) 2012 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef GPRS_RLCMAC_H +#define GPRS_RLCMAC_H + +#include +#include + +extern "C" { +#include +} + +enum gprs_rlcmac_tbf_state { + GPRS_RLCMAC_WAIT_DATA_SEQ_START, + GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK, + GPRS_RLCMAC_WAIT_NEXT_DATA_SEQ +}; + +enum gprs_rlcmac_tbf_direction { + GPRS_RLCMAC_DL_TBF, + GPRS_RLCMAC_UL_TBF +}; + +struct gprs_rlcmac_tbf { + struct llist_head list; + enum gprs_rlcmac_tbf_state state; + enum gprs_rlcmac_tbf_direction direction; + uint8_t tfi; + uint32_t tlli; + uint8_t rlc_data[60]; + uint16_t data_index; + uint8_t bsn; +}; + +extern struct llist_head gprs_rlcmac_tbfs; + +int tfi_alloc(); + +struct gprs_rlcmac_tbf *tbf_alloc(uint8_t tfi); + +static struct gprs_rlcmac_tbf *tbf_by_tfi(uint8_t tfi); + +static struct gprs_rlcmac_tbf *tbf_by_tlli(uint8_t tlli); + +static void tbf_free(struct gprs_rlcmac_tbf *tbf); + +/* TS 44.060 Section 10.4.7 Table 10.4.7.1: Payload Type field */ +enum gprs_rlcmac_block_type { + GPRS_RLCMAC_DATA_BLOCK = 0x0, + GPRS_RLCMAC_CONTROL_BLOCK = 0x1, + GPRS_RLCMAC_CONTROL_BLOCK_OPT = 0x2, + GPRS_RLCMAC_RESERVED = 0x3 +}; + +void gprs_rlcmac_tx_ul_ack(uint8_t tfi, uint32_t tlli, RlcMacUplinkDataBlock_t * ul_data_block); + +void gprs_rlcmac_data_block_parse(gprs_rlcmac_tbf* tbf, RlcMacUplinkDataBlock_t * ul_data_block); + +int gprs_rlcmac_rcv_data_block(BitVector *rlc_block); + +int gprs_rlcmac_rcv_control_block(BitVector *rlc_block); + +void gprs_rlcmac_rcv_block(BitVector *rlc_block); + +void gprs_rlcmac_tx_dl_data_block(uint32_t tlli, uint8_t *pdu, int start_index, int end_index, uint8_t bsn, uint8_t fbi); + +int gprs_rlcmac_segment_llc_pdu(struct gprs_rlcmac_tbf *tbf); + +void gprs_rlcmac_tx_ul_ud(gprs_rlcmac_tbf *tbf); + +#endif // GPRS_RLCMAC_H diff --git a/pcu_l1_if.cpp b/pcu_l1_if.cpp new file mode 100644 index 00000000..54c5b368 --- /dev/null +++ b/pcu_l1_if.cpp @@ -0,0 +1,91 @@ +/* pcu_l1_if.cpp + * + * Copyright (C) 2012 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include +#include +#include +#include + +#define MAX_UDP_LENGTH 1500 + +// TODO: We should take ports and IP from config. +UDPSocket pcu_l1if_socket(5070, "127.0.0.1", 5934); +UDPSocket pcu_gsmtap_socket(5077, "127.0.0.1", 4729); + +// Send RLC/MAC block to OpenBTS. +void pcu_l1if_tx(BitVector * block) +{ + char buffer[MAX_UDP_LENGTH]; + int ofs = 0; + block->pack((unsigned char*)&buffer[ofs]); + ofs += block->size() >> 3; + COUT("Send to OpenBTS: " << *block); + pcu_l1if_socket.write(buffer, ofs); +} + +// Recieve RLC/MAC block from OpenBTS. +void *pcu_l1if_rx(void *) +{ + BitVector *block = new BitVector(23*8); + pcu_l1if_socket.nonblocking(); + while (1) { + char buf[MAX_UDP_LENGTH]; + int count = pcu_l1if_socket.read(buf, 3000); + if (count>0) { + block->unpack((const unsigned char*)buf); + COUT("Recieve from OpenBTS (MS): " << *block); + gprs_rlcmac_rcv_block(block); + } + } +} + +void gsmtap_send_llc(uint8_t * data, unsigned len) +{ + char buffer[MAX_UDP_LENGTH]; + int ofs = 0; + + // Build header + struct gsmtap_hdr *header = (struct gsmtap_hdr *)buffer; + header->version = 2; + header->hdr_len = sizeof(struct gsmtap_hdr) >> 2; + header->type = 0x08; + header->timeslot = 5; + header->arfcn = 0; + header->signal_dbm = 0; + header->snr_db = 0; + header->frame_number = 0; + header->sub_type = 0; + header->antenna_nr = 0; + header->sub_slot = 0; + header->res = 0; + + ofs += sizeof(*header); + + // Add frame data + unsigned j = 0; + for (unsigned i = ofs; i < len+ofs; i++) + { + buffer[i] = (char)data[j]; + j++; + } + ofs += len; + // Write the GSMTAP packet + pcu_gsmtap_socket.write(buffer, ofs); +} diff --git a/RLCMAC.cpp b/pcu_l1_if.h similarity index 73% rename from RLCMAC.cpp rename to pcu_l1_if.h index 4a174685..e618cba9 100644 --- a/RLCMAC.cpp +++ b/pcu_l1_if.h @@ -1,6 +1,6 @@ -/* RLCMAC.cpp +/* pcu_l1_if.h * - * Copyright (C) 2011 Ivan Klyuchnikov + * Copyright (C) 2012 Ivan Klyuchnikov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -17,10 +17,15 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "bssgp.h" +#ifndef PCU_L1_IF_H +#define PCU_L1_IF_H -int main(int argc, char *argv[]) -{ - // Start RLC/MAC server for communication with OpenBTS and OsmoSGSN. - RLCMACServer(); -} +#include + +void pcu_l1if_tx(BitVector * block); + +void *pcu_l1if_rx(void *); + +void gsmtap_send_llc(uint8_t * data, unsigned len); + +#endif // PCU_L1_IF_H diff --git a/pcu_main.cpp b/pcu_main.cpp new file mode 100644 index 00000000..939e1140 --- /dev/null +++ b/pcu_main.cpp @@ -0,0 +1,94 @@ +/* pcu_main.cpp + * + * Copyright (C) 2012 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include +#include +#include + +// TODO: We should move this parameters to config file. +#define SGSN_IP "127.0.0.1" +#define SGSN_PORT 23000 +#define NSVCI 4 + + +int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci) +{ + int rc = 0; + switch (event) { + case GPRS_NS_EVT_UNIT_DATA: + /* hand the message into the BSSGP implementation */ + rc = gprs_bssgp_pcu_rcvmsg(msg); + break; + default: + LOGP(DGPRS, LOGL_ERROR, "RLCMAC: Unknown event %u from NS\n", event); + if (msg) + talloc_free(msg); + rc = -EIO; + break; + } + return rc; +} + +int main(int argc, char *argv[]) +{ + uint16_t nsvci = NSVCI; + struct gprs_ns_inst *sgsn_nsi; + struct gprs_nsvc *nsvc; + // Socket for reading BitVectors (RLC/MAC Frames) from OpenBTS application. + Thread pcu_l1if; + pcu_l1if.start(pcu_l1if_rx, NULL); + + osmo_init_logging(&log_info); + sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb); + bssgp_nsi = sgsn_nsi; + + if (!bssgp_nsi) + { + LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); + exit(1); + } + bctx = btsctx_alloc(BVCI, NSEI); + bctx->cell_id = CELL_ID; + bctx->nsei = NSEI; + bctx->ra_id.mnc = MNC; + bctx->ra_id.mcc = MCC; + bctx->ra_id.lac = PCU_LAC; + bctx->ra_id.rac = PCU_RAC; + bctx->bvci = BVCI; + uint8_t cause = 39; + gprs_ns_nsip_listen(sgsn_nsi); + + struct sockaddr_in dest; + dest.sin_family = AF_INET; + dest.sin_port = htons(SGSN_PORT); + inet_aton(SGSN_IP, &dest.sin_addr); + + nsvc = nsip_connect(sgsn_nsi, &dest, NSEI, nsvci); + unsigned i = 0; + while (1) + { + osmo_select_main(0); + if (i == 7) + { + bssgp_tx_bvc_reset(bctx, BVCI, cause); + } + i++; + } +}