Merge branch 'jolly_new'
Merge is based on jolly_new branch with two modifications.
1. Modified PCU L1 interface.
pcu_l1_if.cpp - common functions for tx and rx messages on L1 interface.
sysmo_sock.cpp - SYSMO-PCU socket functions.
openbts_sock.cpp - OpenBTS-PCU socket functions.
pcuif_proto.h - L1 interface's primitives.
2. Modified encoding of RLC/MAC Control messages, now we use structures and encode_gsm_rlcmac_downlink() function for encode control blocks (without  hand-coding).
diff --git a/configure.ac b/configure.ac
index 9fe9fdd..c572c88 100644
--- a/configure.ac
+++ b/configure.ac
@@ -24,7 +24,7 @@
 PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore  >= 0.3.9)
 PKG_CHECK_MODULES(LIBOSMOVTY, libosmovty)
 PKG_CHECK_MODULES(LIBOSMOGSM, libosmogsm >= 0.3.3)
-PKG_CHECK_MODULES(LIBOSMOGB, libosmogb >= 0.5.2)
+PKG_CHECK_MODULES(LIBOSMOGB, libosmogb >= 0.5.1.4)
 
 AC_MSG_CHECKING([whether to enable sysmocom-bts hardware support])
 AC_ARG_ENABLE(sysmocom-bts,
diff --git a/src/Makefile.am b/src/Makefile.am
index 78c01e1..ca586fd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -32,14 +32,15 @@
 	gprs_rlcmac_data.cpp \
 	gprs_rlcmac_sched.cpp \
 	gsm_timer.cpp \
-	bitvector.cpp
+	bitvector.cpp \
+	pcu_l1_if.cpp
 
 if ENABLE_SYSMOBTS
 libgprs_la_SOURCES += \
-	sysmo_l1_if.cpp
+	sysmo_sock.cpp
 else
 libgprs_la_SOURCES += \
-	pcu_l1_if.cpp
+	openbts_sock.cpp
 endif
 
 noinst_PROGRAMS = \
@@ -52,6 +53,7 @@
 	gsm_rlcmac.h \
 	gprs_bssgp_pcu.h \
 	gprs_rlcmac.h \
+	pcuif_proto.h \
 	pcu_l1_if.h \
 	gsm_timer.h \
 	bitvector.h
diff --git a/src/gprs_bssgp_pcu.cpp b/src/gprs_bssgp_pcu.cpp
index 733d4e2..f9a421a 100644
--- a/src/gprs_bssgp_pcu.cpp
+++ b/src/gprs_bssgp_pcu.cpp
@@ -30,6 +30,7 @@
 int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
 {
 	struct bssgp_ud_hdr *budh;
+
 	int tfi;
 	uint32_t tlli;
 	int i, j;
@@ -41,6 +42,10 @@
 	budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
 	tlli = ntohl(budh->tlli);
 
+	if (!tbf)
+	{
+		return -1;
+	}
 	/* LLC_PDU is mandatory IE */
 	if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
 	{
@@ -127,6 +132,7 @@
 
 	return 0;
 }
+
 /* Receive a BSSGP PDU from a BSS on a PTP BVCI */
 int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
 {
diff --git a/src/gprs_rlcmac.cpp b/src/gprs_rlcmac.cpp
index 7d28de1..9d2601c 100644
--- a/src/gprs_rlcmac.cpp
+++ b/src/gprs_rlcmac.cpp
@@ -21,7 +21,6 @@
 #include <gprs_bssgp_pcu.h>
 #include <pcu_l1_if.h>
 #include <gprs_rlcmac.h>
-#include <gsmL1prim.h>
 
 LLIST_HEAD(gprs_rlcmac_tbfs);
 void *rlcmac_tall_ctx;
@@ -247,7 +246,7 @@
 
 	switch (tbf->fT) {
 	case 0:
-hier alles überdenken
+hier alles berdenken
 		// This is timer for delay RLC/MAC data sending after Downlink Immediate Assignment on CCCH.
 		gprs_rlcmac_segment_llc_pdu(tbf);
 		LOGP(DRLCMAC, LOGL_NOTICE, "TBF: [DOWNLINK] END TFI: %u TLLI: 0x%08x \n", tbf->tfi, tbf->tlli);
@@ -485,66 +484,79 @@
 //	bitvec_write_field(dest, wp,0x0,1); // Measurement Mapping struct not present
 }
 
+
 /* generate downlink assignment */
-void write_packet_downlink_assignment(bitvec * dest, uint8_t old_tfi,
+void write_packet_downlink_assignment(RlcMacDownlink_t * block, uint8_t old_tfi,
 	uint8_t old_downlink, uint8_t new_tfi, uint16_t arfcn,
 	uint8_t tn, uint8_t ta, uint8_t tsc, uint8_t poll)
 {
-	// TODO We should use our implementation of encode RLC/MAC Control messages.
-	unsigned wp = 0;
+	// Packet downlink assignment TS 44.060 11.2.7
+
 	int i;
-	bitvec_write_field(dest, wp,0x1,2);  // Payload Type
-	bitvec_write_field(dest, wp,0x0,2);  // Uplink block with TDMA framenumber (FN+13)
-	bitvec_write_field(dest, wp,poll,1);  // Suppl/Polling Bit
-	bitvec_write_field(dest, wp,0x0,3);  // Uplink state flag
-	bitvec_write_field(dest, wp,0x2,6);  // MESSAGE TYPE
-	bitvec_write_field(dest, wp,0x0,2);  // Page Mode
 
-	bitvec_write_field(dest, wp,0x0,1); // switch PERSIST_LEVEL: off
-	bitvec_write_field(dest, wp,0x0,1); // switch TFI : on
-	bitvec_write_field(dest, wp,old_downlink,1); // 0=UPLINK TFI, 1=DL TFI
-	bitvec_write_field(dest, wp,old_tfi,5); // TFI
+	block->PAYLOAD_TYPE = 0x1;  // RLC/MAC control block that does not include the optional octets of the RLC/MAC control header
+	block->RRBP         = 0x0;  // N+13
+	block->SP           = poll; // RRBP field is valid
+	block->USF          = 0x0;  // Uplink state flag
 
-	bitvec_write_field(dest, wp,0x0,1); // Message escape
-	bitvec_write_field(dest, wp,0x0,2); // Medium Access Method: Dynamic Allocation
-	bitvec_write_field(dest, wp,0x0,1); // RLC acknowledged mode
+	block->u.Packet_Downlink_Assignment.MESSAGE_TYPE = 0x2;  // Packet Downlink Assignment
+	block->u.Packet_Downlink_Assignment.PAGE_MODE    = 0x0;  // Normal Paging
 
-	bitvec_write_field(dest, wp,old_downlink,1); // the network establishes no new downlink TBF for the mobile station
-	bitvec_write_field(dest, wp,0x80 >> tn,8); // timeslot(s)
+	block->u.Packet_Downlink_Assignment.Exist_PERSISTENCE_LEVEL      = 0x0;          // PERSISTENCE_LEVEL: off
 
-	bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_VALUE = on
-	bitvec_write_field(dest, wp,ta,6); // TIMING_ADVANCE_VALUE
-	bitvec_write_field(dest, wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off
+	block->u.Packet_Downlink_Assignment.ID.UnionType                 = 0x0;          // TFI = on
+	block->u.Packet_Downlink_Assignment.ID.u.Global_TFI.UnionType    = old_downlink; // 0=UPLINK TFI, 1=DL TFI
+	block->u.Packet_Downlink_Assignment.ID.u.Global_TFI.u.UPLINK_TFI = old_tfi;      // TFI
 
-	bitvec_write_field(dest, wp,0x0,1); // switch POWER CONTROL = off
-	bitvec_write_field(dest, wp,0x1,1); // Frequency Parameters information elements = present
+	block->u.Packet_Downlink_Assignment.MAC_MODE            = 0x0;          // Dynamic Allocation
+	block->u.Packet_Downlink_Assignment.RLC_MODE            = 0x0;          // RLC acknowledged mode
+	block->u.Packet_Downlink_Assignment.CONTROL_ACK         = old_downlink; // NW establishes no new DL TBF for the MS with running timer T3192
+	block->u.Packet_Downlink_Assignment.TIMESLOT_ALLOCATION = 0x80 >> tn;   // timeslot(s)
 
-	bitvec_write_field(dest, wp,tsc,3); // Training Sequence Code (TSC) = 2
-	bitvec_write_field(dest, wp,0x0,2); // ARFCN = present
-	bitvec_write_field(dest, wp,arfcn,10); // ARFCN
+	block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_TIMING_ADVANCE_VALUE = 0x1; // TIMING_ADVANCE_VALUE = on
+	block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.TIMING_ADVANCE_VALUE       = ta;  // TIMING_ADVANCE_VALUE
+	block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_IndexAndtimeSlot     = 0x0; // TIMING_ADVANCE_INDEX = off
 
-	bitvec_write_field(dest, wp,0x1,1); // switch TFI   : on
-	bitvec_write_field(dest, wp,new_tfi,5);// TFI
+	block->u.Packet_Downlink_Assignment.Exist_P0_and_BTS_PWR_CTRL_MODE = 0x0;   // POWER CONTROL = off
 
-	bitvec_write_field(dest, wp,0x1,1); // Power Control Parameters IE = present
-	bitvec_write_field(dest, wp,0x0,4); // ALPHA power control parameter
+	block->u.Packet_Downlink_Assignment.Exist_Frequency_Parameters     = 0x1;   // Frequency Parameters = on
+	block->u.Packet_Downlink_Assignment.Frequency_Parameters.TSC       = tsc;   // Training Sequence Code (TSC)
+	block->u.Packet_Downlink_Assignment.Frequency_Parameters.UnionType = 0x0;   // ARFCN = on
+	block->u.Packet_Downlink_Assignment.Frequency_Parameters.u.ARFCN   = arfcn; // ARFCN
+
+	block->u.Packet_Downlink_Assignment.Exist_DOWNLINK_TFI_ASSIGNMENT  = 0x1;     // DOWNLINK TFI ASSIGNMENT = on
+	block->u.Packet_Downlink_Assignment.DOWNLINK_TFI_ASSIGNMENT        = new_tfi; // TFI
+
+	block->u.Packet_Downlink_Assignment.Exist_Power_Control_Parameters = 0x1;   // Power Control Parameters = on
+	block->u.Packet_Downlink_Assignment.Power_Control_Parameters.ALPHA = 0x0;   // ALPHA
+
 	for (i = 0; i < 8; i++)
-		bitvec_write_field(dest, wp,(tn == i),1); // switch GAMMA_TN[i] = on or off
-	bitvec_write_field(dest, wp,0x0,5); // GAMMA_TN[tn]
+	{
+		if (tn == i)
+		{
+			block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[i].Exist    = 0x1; // Slot[i] = on
+			block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[i].GAMMA_TN = 0x0; // GAMMA_TN
+		}
+		else
+		{
+			block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[i].Exist    = 0x0; // Slot[i] = off
+		}
+	}
 
-	bitvec_write_field(dest, wp,0x0,1); // TBF Starting TIME IE not present
-	bitvec_write_field(dest, wp,0x0,1); // Measurement Mapping struct not present
-	bitvec_write_field(dest, wp,0x0,1);
+	block->u.Packet_Downlink_Assignment.Exist_TBF_Starting_Time   = 0x0; // TBF Starting TIME = off
+	block->u.Packet_Downlink_Assignment.Exist_Measurement_Mapping = 0x0; // Measurement_Mapping = off
+	block->u.Packet_Downlink_Assignment.Exist_AdditionsR99        = 0x0; // AdditionsR99 = off
 }
 
 /* generate uplink ack */
-void write_packet_uplink_ack(bitvec * dest, struct gprs_rlcmac_tbf *tbf,
+void write_packet_uplink_ack(RlcMacDownlink_t * block, struct gprs_rlcmac_tbf *tbf,
 	uint8_t final)
 {
+	// Packet Uplink Ack/Nack  TS 44.060 11.2.28
+
 	char show_v_n[65];
 
-	// TODO We should use our implementation of encode RLC/MAC Control messages.
-	unsigned wp = 0;
+	uint8_t rbb = 0;
 	uint16_t i, bbn;
 	uint16_t mod_sns_half = (tbf->sns >> 1) - 1;
 	char bit;
@@ -552,30 +564,19 @@
 	LOGP(DRLCMACUL, LOGL_DEBUG, "Sending Ack/Nack for TBF=%d "
 		"(final=%d)\n", tbf->tfi, final);
 
-	bitvec_write_field(dest, wp,0x1,2);  // payload
-	bitvec_write_field(dest, wp,0x0,2);  // Uplink block with TDMA framenumber (N+13)
-	bitvec_write_field(dest, wp,final,1);  // Suppl/Polling Bit
-	bitvec_write_field(dest, wp,0x0,3);  // Uplink state flag
-	
-	//bitvec_write_field(dest, wp,0x0,1);  // Reduced block sequence number
-	//bitvec_write_field(dest, wp,BSN+6,5);  // Radio transaction identifier
-	//bitvec_write_field(dest, wp,0x1,1);  // Final segment
-	//bitvec_write_field(dest, wp,0x1,1);  // Address control
+	block->PAYLOAD_TYPE = 0x1;   // RLC/MAC control block that does not include the optional octets of the RLC/MAC control header
+	block->RRBP         = 0x0;   // N+13
+	block->SP           = final; // RRBP field is valid, if it is final ack
+	block->USF          = 0x0;   // Uplink state flag
 
-	//bitvec_write_field(dest, wp,0x0,2);  // Power reduction: 0
-	//bitvec_write_field(dest, wp,TFI,5);  // Temporary flow identifier
-	//bitvec_write_field(dest, wp,0x1,1);  // Direction
+	block->u.Packet_Uplink_Ack_Nack.MESSAGE_TYPE = 0x9;      // Packet Downlink Assignment
+	block->u.Packet_Uplink_Ack_Nack.PAGE_MODE    = 0x0;      // Normal Paging
+	block->u.Packet_Uplink_Ack_Nack.UPLINK_TFI   = tbf->tfi; // Uplink TFI
 
-	bitvec_write_field(dest, wp,0x09,6); // MESSAGE TYPE
-	bitvec_write_field(dest, wp,0x0,2);  // Page Mode
-
-	bitvec_write_field(dest, wp,0x0,2);
-	bitvec_write_field(dest, wp,tbf->tfi,5); // Uplink TFI
-	bitvec_write_field(dest, wp,0x0,1);
-	
-	bitvec_write_field(dest, wp,0x0,2);  // CS1
-	bitvec_write_field(dest, wp,final,1);  // FINAL_ACK_INDICATION
-	bitvec_write_field(dest, wp,tbf->dir.ul.v_r,7); // STARTING_SEQUENCE_NUMBER
+	block->u.Packet_Uplink_Ack_Nack.UnionType    = 0x0;      // PU_AckNack_GPRS = on
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.CHANNEL_CODING_COMMAND                        = 0x0;             // CS1
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Ack_Nack_Description.FINAL_ACK_INDICATION     = final;           // FINAL ACK INDICATION
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Ack_Nack_Description.STARTING_SEQUENCE_NUMBER = tbf->dir.ul.v_r; // STARTING_SEQUENCE_NUMBER
 	// RECEIVE_BLOCK_BITMAP
 	for (i = 0, bbn = (tbf->dir.ul.v_r - 64) & mod_sns_half; i < 64;
 	     i++, bbn = (bbn + 1) & mod_sns_half) {
@@ -583,15 +584,29 @@
 		if (bit == 0)
 			bit = ' ';
 		show_v_n[i] = bit;
-		bitvec_write_field(dest, wp,(bit == 'R'),1);
+		if (bit == 'R')
+			rbb = (rbb << 1)|1;
+		else
+			rbb = (rbb << 1);
+		if((i%8) == 7)
+		{
+			block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Ack_Nack_Description.RECEIVED_BLOCK_BITMAP[i/8] = rbb;
+			rbb = 0;
+		}
 	}
 	show_v_n[64] = '\0';
 	LOGP(DRLCMACUL, LOGL_DEBUG, "- V(N): \"%s\" R=Received "
 		"N=Not-Received\n", show_v_n);
-	bitvec_write_field(dest, wp,0x1,1);  // CONTENTION_RESOLUTION_TLLI = present
-	bitvec_write_field(dest, wp,tbf->tlli,8*4);
-	bitvec_write_field(dest, wp,0x00,4); //spare
-	bitvec_write_field(dest, wp,0x5,4); //0101
+
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.UnionType              = 0x0; // Fixed Allocation Dummy = on
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.u.FixedAllocationDummy = 0x0; // Fixed Allocation Dummy
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Exist_AdditionsR99     = 0x0; // AdditionsR99 = off
+
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Common_Uplink_Ack_Nack_Data.Exist_CONTENTION_RESOLUTION_TLLI = 0x1;
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Common_Uplink_Ack_Nack_Data.CONTENTION_RESOLUTION_TLLI       = tbf->tlli;
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Common_Uplink_Ack_Nack_Data.Exist_Packet_Timing_Advance      = 0x0;
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Common_Uplink_Ack_Nack_Data.Exist_Extension_Bits             = 0x0;
+	block->u.Packet_Uplink_Ack_Nack.u.PU_AckNack_GPRS_Struct.Common_Uplink_Ack_Nack_Data.Exist_Power_Control_Parameters   = 0x0;
 }
 
 /* Send Uplink unit-data to SGSN. */
@@ -613,4 +628,3 @@
 
 	return 0;
 }
-
diff --git a/src/gprs_rlcmac.h b/src/gprs_rlcmac.h
index abb9980..635a6b1 100644
--- a/src/gprs_rlcmac.h
+++ b/src/gprs_rlcmac.h
@@ -228,11 +228,11 @@
         uint8_t usf, uint16_t arfcn, uint8_t tn, uint8_t ta, uint8_t tsc,
         uint8_t poll);
 
-void write_packet_downlink_assignment(bitvec * dest, uint8_t old_tfi,
+void write_packet_downlink_assignment(RlcMacDownlink_t * block, uint8_t old_tfi,
         uint8_t old_downlink, uint8_t new_tfi, uint16_t arfcn,
         uint8_t tn, uint8_t ta, uint8_t tsc, uint8_t poll);
 
-void write_packet_uplink_ack(bitvec * dest, struct gprs_rlcmac_tbf *tbf,
+void write_packet_uplink_ack(RlcMacDownlink_t * block, struct gprs_rlcmac_tbf *tbf,
         uint8_t final);
 
 int gprs_rlcmac_tx_ul_ud(gprs_rlcmac_tbf *tbf);
diff --git a/src/gprs_rlcmac_data.cpp b/src/gprs_rlcmac_data.cpp
index a87fef1..7beca38 100644
--- a/src/gprs_rlcmac_data.cpp
+++ b/src/gprs_rlcmac_data.cpp
@@ -540,9 +540,12 @@
 	}
 	bitvec_unhex(ack_vec,
 		"2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
-	write_packet_uplink_ack(ack_vec,  tbf, final);
+	RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)malloc(sizeof(RlcMacDownlink_t));
+	write_packet_uplink_ack(mac_control_block, tbf, final);
+	encode_gsm_rlcmac_downlink(ack_vec, mac_control_block);
 	bitvec_pack(ack_vec, msgb_put(msg, 23));
 	bitvec_free(ack_vec);
+	free(mac_control_block);
 
 	if (final) {
 		tbf->poll_state = GPRS_RLCMAC_POLL_SCHED;
@@ -1314,17 +1317,18 @@
 	bitvec_unhex(ass_vec,
 		"2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
 	LOGP(DRLCMAC, LOGL_INFO, "TBF: START TFI: %u TLLI: 0x%08x Packet Downlink Assignment (PACCH)\n", new_tbf->tfi, new_tbf->tlli);
-	write_packet_downlink_assignment(ass_vec, tbf->tfi,
+	RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)malloc(sizeof(RlcMacDownlink_t));
+	write_packet_downlink_assignment(mac_control_block, tbf->tfi,
 		(tbf->direction == GPRS_RLCMAC_DL_TBF), new_tbf->tfi,
 		new_tbf->arfcn, new_tbf->ts, new_tbf->ta, new_tbf->tsc,
 		POLLING_ASSIGNMENT);
-	bitvec_pack(ass_vec, msgb_put(msg, 23));
-	RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)malloc(sizeof(RlcMacDownlink_t));
 	LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++\n");
-	decode_gsm_rlcmac_downlink(ass_vec, mac_control_block);
+	encode_gsm_rlcmac_downlink(ass_vec, mac_control_block);
 	LOGPC(DCSN1, LOGL_NOTICE, "\n");
 	LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Downlink Assignment -------------------------\n");
+	bitvec_pack(ass_vec, msgb_put(msg, 23));
 	bitvec_free(ass_vec);
+	free(mac_control_block);
 
 #if POLLING_ASSIGNMENT == 1
 	tbf->poll_state = GPRS_RLCMAC_POLL_SCHED;
diff --git a/src/gsmL1prim.h b/src/gsmL1prim.h
deleted file mode 100644
index b348a1a..0000000
--- a/src/gsmL1prim.h
+++ /dev/null
@@ -1,415 +0,0 @@
-/*
-* Copyright 2012 Thomas Cooper <tacooper@vt.edu>
-*
-* This software is distributed under the terms of the GNU Affero Public License.
-* See the COPYING file in the main directory for details.
-*
-* This use of this software may be subject to additional restrictions.
-* See the LEGAL file in the main directory for details.
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as published by
-the Free Software Foundation, either version 3 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 Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-*/
-
-#ifndef GSML1PRIM_H
-#define GSML1PRIM_H
-
-#include <stdint.h>
-
-typedef enum GsmL1_SubCh_t {
-GsmL1_SubCh_NA,
-} GsmL1_SubCh_t;
-
-typedef enum GsmL1_Sapi_t {
-GsmL1_Sapi_Fcch,
-GsmL1_Sapi_Sch,
-GsmL1_Sapi_Sacch,
-GsmL1_Sapi_Sdcch,
-GsmL1_Sapi_Bcch,
-GsmL1_Sapi_Pch,
-GsmL1_Sapi_Agch,
-GsmL1_Sapi_Cbch,
-GsmL1_Sapi_Rach,
-GsmL1_Sapi_TchF,
-GsmL1_Sapi_FacchF,
-GsmL1_Sapi_TchH,
-GsmL1_Sapi_FacchH,
-GsmL1_Sapi_Nch,
-GsmL1_Sapi_Pdtch,
-GsmL1_Sapi_Pacch,
-GsmL1_Sapi_Pbcch,
-GsmL1_Sapi_Pagch,
-GsmL1_Sapi_Ppch,
-GsmL1_Sapi_Pnch,
-GsmL1_Sapi_Ptcch,
-GsmL1_Sapi_Prach,
-GsmL1_Sapi_Idle,
-GsmL1_Sapi_NUM,
-} GsmL1_Sapi_t;
-
-typedef enum GsmL1_Status_t {
-GsmL1_Status_Success,
-GsmL1_Status_Generic,
-GsmL1_Status_NoMemory,
-GsmL1_Status_Timeout,
-GsmL1_Status_InvalidParam,
-GsmL1_Status_Busy,
-GsmL1_Status_NoRessource,
-GsmL1_Status_Uninitialized,
-GsmL1_Status_NullInterface,
-GsmL1_Status_NullFctnPtr,
-GsmL1_Status_BadCrc,
-GsmL1_Status_BadUsf,
-GsmL1_Status_InvalidCPS,
-GsmL1_Status_UnexpectedBurst,
-GsmL1_Status_UnavailCodec,
-GsmL1_Status_CriticalError,
-GsmL1_Status_OverheatError,
-GsmL1_Status_DeviceError,
-GsmL1_Status_FacchError,
-GsmL1_Status_AlreadyDeactivated,
-GsmL1_Status_TxBurstFifoOvrn,
-GsmL1_Status_TxBurstFifoUndr,
-GsmL1_Status_NotSynchronized,
-GsmL1_Status_Unsupported,
-GSML1_STATUS_NUM,
-} GsmL1_Status_t;
-
-typedef enum GsmL1_PrimId_t {
-GsmL1_PrimId_MphInitReq,
-GsmL1_PrimId_MphCloseReq,
-GsmL1_PrimId_MphConnectReq,
-GsmL1_PrimId_MphDisconnectReq,
-GsmL1_PrimId_MphActivateReq,
-GsmL1_PrimId_MphDeactivateReq,
-GsmL1_PrimId_MphConfigReq,
-GsmL1_PrimId_MphMeasureReq,
-GsmL1_PrimId_MphInitCnf,
-GsmL1_PrimId_MphCloseCnf,
-GsmL1_PrimId_MphConnectCnf,
-GsmL1_PrimId_MphDisconnectCnf,
-GsmL1_PrimId_MphActivateCnf,
-GsmL1_PrimId_MphDeactivateCnf,
-GsmL1_PrimId_MphConfigCnf,
-GsmL1_PrimId_MphMeasureCnf,
-GsmL1_PrimId_MphTimeInd,
-GsmL1_PrimId_MphSyncInd,
-GsmL1_PrimId_PhEmptyFrameReq,
-GsmL1_PrimId_PhDataReq,
-GsmL1_PrimId_PhConnectInd,
-GsmL1_PrimId_PhReadyToSendInd,
-GsmL1_PrimId_PhDataInd,
-GsmL1_PrimId_PhRaInd,
-GsmL1_PrimId_NUM,
-} GsmL1_PrimId_t;
-
-typedef enum GsmL1_Dir_t {
-GsmL1_Dir_TxDownlink,
-GsmL1_Dir_RxUplink,
-} GsmL1_Dir_t;
-
-typedef enum GsmL1_DevType_t {
-GsmL1_DevType_TxdRxu,
-} GsmL1_DevType_t;
-
-typedef enum GsmL1_TchPlType_t {
-GsmL1_TchPlType_NA,
-GsmL1_TchPlType_Efr,
-GsmL1_TchPlType_Fr,
-GsmL1_TchPlType_Hr,
-GsmL1_TchPlType_Amr,
-GsmL1_TchPlType_Amr_SidBad,
-GsmL1_TchPlType_Amr_Onset,
-GsmL1_TchPlType_Amr_Ratscch,
-GsmL1_TchPlType_Amr_SidUpdateInH,
-GsmL1_TchPlType_Amr_SidFirstP1,
-GsmL1_TchPlType_Amr_SidFirstP2,
-GsmL1_TchPlType_Amr_SidFirstInH,
-GsmL1_TchPlType_Amr_RatscchMarker,
-GsmL1_TchPlType_Amr_RatscchData,
-} GsmL1_TchPlType_t;
-
-typedef enum GsmL1_ConfigParamId_t {
-GsmL1_ConfigParamId_SetNbTsc,
-GsmL1_ConfigParamId_SetTxPowerLevel,
-GsmL1_ConfigParamId_SetLogChParams,
-GsmL1_ConfigParamId_SetCipheringParams,
-} GsmL1_ConfigParamId_t;
-
-typedef struct GsmL1_DeviceParam_t {
-enum GsmL1_DevType_t devType;
-int freqBand;
-uint16_t u16Arfcn;
-uint16_t u16BcchArfcn;
-uint8_t u8NbTsc;
-uint8_t u8Ncc;
-float fRxPowerLevel;
-float fTxPowerLevel;
-} GsmL1_DeviceParam_t;
-
-typedef struct GsmL1_MsgUnitParam_t {
-uint8_t u8Buffer[256];
-uint8_t u8Size;
-} GsmL1_MsgUnitParam_t;
-
-typedef struct GsmL1_MeasParam_t {
-float fRssi;
-float fLinkQuality;
-float fBer;
-int16_t i16BurstTiming;
-} GsmL1_MeasParam_t;
-
-typedef struct GsmL1_LogChParam_t {
-union {
-struct {
-enum GsmL1_TchPlType_t tchPlType;
-        
-enum {
-GsmL1_AmrCmiPhase_NA,
-GsmL1_AmrCmiPhase_Odd,
-} amrCmiPhase;
-        
-enum {
-GsmL1_AmrCodecMode_Unset,
-} amrInitCodecMode;
-
-enum {
-GsmL1_AmrCodec_Unset,
-GsmL1_AmrCodec_4_75,
-GsmL1_AmrCodec_5_15,
-GsmL1_AmrCodec_5_9,
-GsmL1_AmrCodec_6_7,
-GsmL1_AmrCodec_7_4,
-GsmL1_AmrCodec_7_95,
-GsmL1_AmrCodec_10_2,
-GsmL1_AmrCodec_12_2,
-} amrActiveCodecSet[8];
-} tch;
-
-struct {
-uint8_t u8Bsic;
-uint8_t u8NbrOfAgch;
-} rach;
-
-struct {
-uint8_t u8MsPowerLevel;
-} sacch;
-
-struct {
-uint8_t u8NbrOfAgch;
-} agch;
-};
-} GsmL1_LogChParam_t;
-
-typedef enum GsmL1_LogChComb_t {
-GsmL1_LogChComb_0,
-GsmL1_LogChComb_I,
-GsmL1_LogChComb_II,
-GsmL1_LogChComb_IV,
-GsmL1_LogChComb_V,
-GsmL1_LogChComb_VII,
-GsmL1_LogChComb_XIII,
-} GsmL1_LogChComb_t;
-
-enum {
-GsmL1_FreqBand_850,
-GsmL1_FreqBand_900,
-GsmL1_FreqBand_1800,
-GsmL1_FreqBand_1900,
-};
-
-typedef struct GsmL1_MphInitReq_t {
-struct GsmL1_DeviceParam_t deviceParam;
-} GsmL1_MphInitReq_t;
-
-typedef struct GsmL1_MphCloseReq_t {
-uint32_t hLayer1;
-} GsmL1_MphCloseReq_t;
-
-typedef struct GsmL1_MphConnectReq_t {
-uint32_t hLayer1;
-uint8_t u8Tn;
-enum GsmL1_LogChComb_t logChComb;
-} GsmL1_MphConnectReq_t;
-
-typedef struct GsmL1_MphDisconnectReq_t {
-uint32_t hLayer1;
-} GsmL1_MphDisconnectReq_t;
-
-typedef struct GsmL1_MphActivateReq_t {
-uint32_t hLayer1;
-struct GsmL1_LogChParam_t logChPrm;
-uint8_t u8Tn;
-enum GsmL1_SubCh_t subCh;
-enum GsmL1_Dir_t dir;
-enum GsmL1_Sapi_t sapi;
-uint32_t hLayer2;
-float fBFILevel;
-} GsmL1_MphActivateReq_t;
-
-typedef struct GsmL1_MphDeactivateReq_t {
-uint32_t hLayer1;
-uint8_t u8Tn;
-enum GsmL1_SubCh_t subCh;
-enum GsmL1_Dir_t dir;
-enum GsmL1_Sapi_t sapi;
-} GsmL1_MphDeactivateReq_t;
-
-typedef struct GsmL1_ConfigParam_t {
-struct {
-enum GsmL1_Sapi_t sapi;
-uint8_t u8Tn;
-enum GsmL1_SubCh_t subCh;
-enum GsmL1_Dir_t dir;
-struct GsmL1_LogChParam_t logChParams;
-} setLogChParams;
-} GsmL1_ConfigParam_t;
-
-typedef struct GsmL1_MphConfigReq_t {
-uint32_t hLayer1;
-enum GsmL1_ConfigParamId_t cfgParamId;
-struct GsmL1_ConfigParam_t cfgParams;
-} GsmL1_MphConfigReq_t;
-
-typedef struct GsmL1_MphConfigCnf_t {
-enum GsmL1_Status_t status;
-enum GsmL1_ConfigParamId_t cfgParamId;
-struct GsmL1_ConfigParam_t cfgParams;
-} GsmL1_MphConfigCnf_t;
-
-typedef struct GsmL1_MphMeasureReq_t {
-uint32_t hLayer1;
-} GsmL1_MphMeasureReq_t;
-
-typedef struct GsmL1_MphInitCnf_t {
-uint32_t hLayer1;
-enum GsmL1_Status_t status;
-} GsmL1_MphInitCnf_t;
-
-typedef struct GsmL1_MphCloseCnf_t {
-enum GsmL1_Status_t status;
-} GsmL1_MphCloseCnf_t;
-
-typedef struct GsmL1_MphConnectCnf_t {
-enum GsmL1_Status_t status;
-} GsmL1_MphConnectCnf_t;
-
-typedef struct GsmL1_MphDisconnectCnf_t {
-enum GsmL1_Status_t status;
-} GsmL1_MphDisconnectCnf_t;
-
-typedef struct GsmL1_MphActivateCnf_t {
-enum GsmL1_Status_t status;
-uint8_t u8Tn;
-int sapi;
-} GsmL1_MphActivateCnf_t;
-
-typedef struct GsmL1_MphDeactivateCnf_t {
-enum GsmL1_Status_t status;
-uint8_t u8Tn;
-enum GsmL1_Sapi_t sapi;
-} GsmL1_MphDeactivateCnf_t;
-
-typedef struct GsmL1_MphMeasureCnf_t {
-enum GsmL1_Status_t status;
-} GsmL1_MphMeasureCnf_t;
-
-typedef struct GsmL1_MphTimeInd_t {
-uint32_t u32Fn;
-} GsmL1_MphTimeInd_t;
-
-typedef struct GsmL1_MphSyncInd_t {
-} GsmL1_MphSyncInd_t;
-
-typedef struct GsmL1_PhEmptyFrameReq_t {
-uint32_t hLayer1;
-uint8_t u8Tn;
-uint32_t u32Fn;
-enum GsmL1_Sapi_t sapi;
-enum GsmL1_SubCh_t subCh;
-uint8_t u8BlockNbr;
-} GsmL1_PhEmptyFrameReq_t;
-
-typedef struct GsmL1_PhDataReq_t {
-uint32_t hLayer1;
-uint8_t u8Tn;
-uint32_t u32Fn;
-enum GsmL1_Sapi_t sapi;
-enum GsmL1_SubCh_t subCh;
-uint8_t u8BlockNbr;
-struct GsmL1_MsgUnitParam_t msgUnitParam;
-} GsmL1_PhDataReq_t;
-
-typedef struct GsmL1_PhConnectInd_t {
-uint8_t u8Tn;
-uint8_t u8Tsc;
-uint16_t u16Arfcn;
-} GsmL1_PhConnectInd_t;
-
-typedef struct GsmL1_PhReadyToSendInd_t {
-uint32_t hLayer1;
-uint8_t u8Tn;
-uint32_t u32Fn;
-enum GsmL1_Sapi_t sapi;
-enum GsmL1_SubCh_t subCh;
-uint8_t u8BlockNbr;
-uint32_t hLayer2;
-} GsmL1_PhReadyToSendInd_t;
-
-typedef struct GsmL1_PhDataInd_t {
-struct GsmL1_MeasParam_t measParam;
-struct GsmL1_MsgUnitParam_t msgUnitParam;
-enum GsmL1_Sapi_t sapi;
-uint32_t hLayer2;
-} GsmL1_PhDataInd_t;
-
-typedef struct GsmL1_PhRaInd_t {
-struct GsmL1_MeasParam_t measParam;
-struct GsmL1_MsgUnitParam_t msgUnitParam;
-uint32_t u32Fn;
-uint32_t hLayer2;
-} GsmL1_PhRaInd_t;
-
-typedef struct GsmL1_Prim_t {
-union {
-struct GsmL1_MphInitReq_t mphInitReq;
-struct GsmL1_MphCloseReq_t mphCloseReq;
-struct GsmL1_MphConnectReq_t mphConnectReq;
-struct GsmL1_MphDisconnectReq_t mphDisconnectReq;
-struct GsmL1_MphActivateReq_t mphActivateReq;
-struct GsmL1_MphDeactivateReq_t mphDeactivateReq;
-struct GsmL1_MphConfigReq_t mphConfigReq;
-struct GsmL1_MphMeasureReq_t mphMeasureReq;
-struct GsmL1_MphInitCnf_t mphInitCnf;
-struct GsmL1_MphCloseCnf_t mphCloseCnf;
-struct GsmL1_MphConnectCnf_t mphConnectCnf;
-struct GsmL1_MphDisconnectCnf_t mphDisconnectCnf;
-struct GsmL1_MphActivateCnf_t mphActivateCnf;
-struct GsmL1_MphDeactivateCnf_t mphDeactivateCnf;
-struct GsmL1_MphConfigCnf_t mphConfigCnf;
-struct GsmL1_MphMeasureCnf_t mphMeasureCnf;
-struct GsmL1_MphTimeInd_t mphTimeInd;
-struct GsmL1_MphSyncInd_t mphSyncInd;
-struct GsmL1_PhEmptyFrameReq_t phEmptyFrameReq;
-struct GsmL1_PhDataReq_t phDataReq;
-struct GsmL1_PhConnectInd_t phConnectInd;
-struct GsmL1_PhReadyToSendInd_t phReadyToSendInd;
-struct GsmL1_PhDataInd_t phDataInd;
-struct GsmL1_PhRaInd_t phRaInd;
-} u;
-
-enum GsmL1_PrimId_t id;
-} GsmL1_Prim_t;
-
-#endif
diff --git a/src/openbts_sock.cpp b/src/openbts_sock.cpp
new file mode 100644
index 0000000..cbf7adc
--- /dev/null
+++ b/src/openbts_sock.cpp
@@ -0,0 +1,183 @@
+/* openbts_sock.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 <errno.h>
+#include <string.h>
+#include <gprs_rlcmac.h>
+#include <gprs_bssgp_pcu.h>
+#include <pcu_l1_if.h>
+#include <gprs_debug.h>
+#include <bitvector.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+extern "C" {
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/write_queue.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/gsm/gsm_utils.h>
+#include <pcuif_proto.h>
+}
+
+struct femtol1_hdl {
+	struct gsm_time gsm_time;
+	uint32_t hLayer1;			/* handle to the L1 instance in the DSP */
+	uint32_t dsp_trace_f;
+	uint16_t clk_cal;
+	struct llist_head wlc_list;
+
+	void *priv;			/* user reference */
+
+	struct osmo_timer_list alive_timer;
+	unsigned int alive_prim_cnt;
+
+	struct osmo_fd read_ofd;	/* osmo file descriptors */
+	struct osmo_wqueue write_q;
+
+	struct {
+		uint16_t arfcn;
+		uint8_t tn;
+		uint8_t tsc;
+		uint16_t ta;
+	} channel_info;
+
+};
+
+struct l1fwd_hdl {
+	struct sockaddr_storage remote_sa;
+	socklen_t remote_sa_len;
+
+	struct osmo_wqueue udp_wq;
+
+	struct femtol1_hdl *fl1h;
+};
+
+struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl);
+
+// TODO: We should move this parameters to config file.
+#define PCU_L1_IF_PORT 5944
+
+/* OpenBTS socket functions */
+
+int pcu_sock_send(struct msgb *msg)
+{
+	osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
+	return 0;
+}
+
+/* data has arrived on the udp socket */
+static int udp_read_cb(struct osmo_fd *ofd)
+{
+	struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
+	struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
+	struct femtol1_hdl *fl1h = l1fh->fl1h;
+	int rc;
+
+	if (!msg)
+		return -ENOMEM;
+
+	msg->l1h = msg->data;
+
+	l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
+	rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
+			(struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
+	if (rc < 0) {
+		perror("read from udp");
+		msgb_free(msg);
+		return rc;
+	} else if (rc == 0) {
+		perror("len=0 read from udp");
+		msgb_free(msg);
+		return rc;
+	}
+	msgb_put(msg, rc);
+
+	struct gsm_pcu_if *pcu_prim = (gsm_pcu_if *)(msg->l1h);
+	rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
+	return rc;
+}
+
+/* callback when we can write to the UDP socket */
+static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
+{
+	int rc;
+	struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
+
+	//LOGP(DPCU, LOGL_ERROR, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_length(msg));
+
+	rc = sendto(ofd->fd, msgb_data(msg), msgb_length(msg), 0,
+			(const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
+	if (rc < 0) {
+		LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
+			strerror(errno));
+		return rc;
+	} else if (rc < (int)msgb_length(msg)) {
+		LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
+			"%u < %u\n", rc, msgb_length(msg));
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int pcu_l1if_open()
+{
+	struct femtol1_hdl *fl1h;
+	int rc;
+
+	/* allocate new femtol1_handle */
+	fl1h = talloc_zero(NULL, struct femtol1_hdl);
+	INIT_LLIST_HEAD(&fl1h->wlc_list);
+
+	l1fh->fl1h = fl1h;
+	fl1h->priv = l1fh;
+
+	struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
+	osmo_wqueue_init(queue, 10);
+	queue->bfd.when |= BSC_FD_READ;
+	queue->bfd.data = l1fh;
+	queue->bfd.priv_nr = 0;
+
+	/* Open UDP */
+	struct osmo_wqueue *wq = &l1fh->udp_wq;
+
+	osmo_wqueue_init(wq, 10);
+	wq->write_cb = udp_write_cb;
+	wq->read_cb = udp_read_cb;
+	wq->bfd.when |= BSC_FD_READ;
+	wq->bfd.data = l1fh;
+	wq->bfd.priv_nr = 0;
+	rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
+				IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
+				OSMO_SOCK_F_BIND);
+	if (rc < 0) {
+		perror("sock_init");
+		exit(1);
+	}
+
+	return 0;
+}
+
+void pcu_l1if_close(void)
+{
+	gprs_bssgp_destroy();
+
+	/* FIXME: cleanup l1if */
+	talloc_free(l1fh->fl1h);
+}
diff --git a/src/pcu_l1_if.cpp b/src/pcu_l1_if.cpp
index 865a911..fd8b3c3 100644
--- a/src/pcu_l1_if.cpp
+++ b/src/pcu_l1_if.cpp
@@ -1,6 +1,6 @@
 /* pcu_l1_if.cpp
  *
- * Copyright (C) 2012 Ivan Klyuchnikov
+ * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -17,62 +17,25 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
-#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
 #include <string.h>
-#include <gprs_rlcmac.h>
-#include <gprs_bssgp_pcu.h>
-#include <pcu_l1_if.h>
-#include <gprs_debug.h>
-#include <bitvector.h>
-#include <gsmL1prim.h>
+#include <errno.h>
+#include <assert.h>
 #include <sys/socket.h>
-#include <arpa/inet.h>
+#include <sys/un.h>
 extern "C" {
 #include <osmocom/core/talloc.h>
-#include <osmocom/core/write_queue.h>
-#include <osmocom/core/socket.h>
-#include <osmocom/core/timer.h>
-#include <osmocom/gsm/gsm_utils.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/msgb.h>
 }
 
-#define MAX_UDP_LENGTH 1500
-
-#define msgb_l1prim(msg)	((GsmL1_Prim_t *)(msg)->l1h)
-
-struct femtol1_hdl {
-	struct gsm_time gsm_time;
-	uint32_t hLayer1;			/* handle to the L1 instance in the DSP */
-	uint32_t dsp_trace_f;
-	uint16_t clk_cal;
-	struct llist_head wlc_list;
-
-	void *priv;			/* user reference */
-
-	struct osmo_timer_list alive_timer;
-	unsigned int alive_prim_cnt;
-
-	struct osmo_fd read_ofd;	/* osmo file descriptors */
-	struct osmo_wqueue write_q;
-
-	struct {
-		uint16_t arfcn;
-		uint8_t tn;
-		uint8_t tsc;
-		uint16_t ta;
-	} channel_info;
-
-};
-
-struct l1fwd_hdl {
-	struct sockaddr_storage remote_sa;
-	socklen_t remote_sa_len;
-
-	struct osmo_wqueue udp_wq;
-
-	struct femtol1_hdl *fl1h;
-};
-
-struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl);
+#include <gprs_rlcmac.h>
+#include <pcu_l1_if.h>
+#include <gprs_debug.h>
+#include <gprs_bssgp_pcu.h>
+#include <pcuif_proto.h>
 
 // Variable for storage current FN.
 int frame_number;
@@ -87,277 +50,398 @@
 	frame_number = fn;
 }
 
-struct msgb *l1p_msgb_alloc(void)
-{
-	struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
+/*
+ * PCU messages
+ */
 
-	if (msg)
-		msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
+struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
+{
+	struct msgb *msg;
+	struct gsm_pcu_if *pcu_prim;
+
+	msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
+	if (!msg)
+		return NULL;
+	msgb_put(msg, sizeof(struct gsm_pcu_if));
+	pcu_prim = (struct gsm_pcu_if *) msg->data;
+	pcu_prim->msg_type = msg_type;
+	pcu_prim->bts_nr = bts_nr;
 
 	return msg;
 }
 
-// Send RLC/MAC block to OpenBTS.
-void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
-        uint32_t fn, uint8_t block_nr)
+static int pcu_tx_act_req(uint8_t trx, uint8_t ts, uint8_t activate)
 {
-	struct msgb *nmsg = l1p_msgb_alloc();
-	GsmL1_Prim_t *prim = msgb_l1prim(nmsg);
-	
-	prim->id = GsmL1_PrimId_PhDataReq;
-	prim->u.phDataReq.sapi = GsmL1_Sapi_Pdtch;
-	memcpy(prim->u.phDataReq.msgUnitParam.u8Buffer, msg->data, msg->len);
-	prim->u.phDataReq.msgUnitParam.u8Size = msg->len;
-	osmo_wqueue_enqueue(&l1fh->udp_wq, nmsg);
+	struct msgb *msg;
+	struct gsm_pcu_if *pcu_prim;
+	struct gsm_pcu_if_act_req *act_req;
+
+	LOGP(DL1IF, LOGL_INFO, "Sending %s request: trx=%d ts=%d\n",
+		(activate) ? "activate" : "deactivate", trx, ts);
+
+	msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, 0);
+	if (!msg)
+		return -ENOMEM;
+	pcu_prim = (struct gsm_pcu_if *) msg->data;
+	act_req = &pcu_prim->u.act_req;
+	act_req->activate = activate;
+	act_req->trx_nr = trx;
+	act_req->ts_nr = ts;
+
+	return pcu_sock_send(msg);
+}
+
+static int pcu_tx_data_req(uint8_t trx, uint8_t ts, uint8_t sapi,
+	uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data,
+	uint8_t len)
+{
+	struct msgb *msg;
+	struct gsm_pcu_if *pcu_prim;
+	struct gsm_pcu_if_data *data_req;
+
+	LOGP(DL1IF, LOGL_DEBUG, "Sending data request: trx=%d ts=%d sapi=%d "
+		"arfcn=%d fn=%d block=%d data=%s\n", trx, ts, sapi, arfcn, fn,
+		block_nr, osmo_hexdump(data, len));
+
+	msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, 0);
+	if (!msg)
+		return -ENOMEM;
+	pcu_prim = (struct gsm_pcu_if *) msg->data;
+	data_req = &pcu_prim->u.data_req;
+
+	data_req->sapi = sapi;
+	data_req->fn = fn;
+	data_req->arfcn = arfcn;
+	data_req->trx_nr = trx;
+	data_req->ts_nr = ts;
+	data_req->block_nr = block_nr;
+	memcpy(data_req->data, data, len);
+	data_req->len = len;
+
+	return pcu_sock_send(msg);
+}
+
+void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
+	uint32_t fn, uint8_t block_nr)
+{
+	pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
+		msg->data, msg->len);
+	msgb_free(msg);
+}
+
+void pcu_l1if_tx_ptcch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
+	uint32_t fn, uint8_t block_nr)
+{
+	pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr,
+		msg->data, msg->len);
 	msgb_free(msg);
 }
 
 void pcu_l1if_tx_agch(bitvec * block, int plen)
 {
-	struct msgb *msg = l1p_msgb_alloc();
-	GsmL1_Prim_t *prim = msgb_l1prim(msg);
+	uint8_t data[23]; /* prefix PLEN */
 	
-	prim->id = GsmL1_PrimId_PhDataReq;
-	prim->u.phDataReq.sapi = GsmL1_Sapi_Agch;
-	bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer);
-	prim->u.phDataReq.msgUnitParam.u8Size = 22;
-	osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
+	/* FIXME: why does OpenBTS has no PLEN and no fill in message? */
+	bitvec_pack(block, data + 1);
+	data[0] = (plen << 2) | 0x01;
+	pcu_tx_data_req(0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, 23);
 }
 
 void pcu_l1if_tx_pch(bitvec * block, int plen, char *imsi)
 {
-	struct msgb *msg = l1p_msgb_alloc();
-	GsmL1_Prim_t *prim = msgb_l1prim(msg);
-	
-	prim->id = GsmL1_PrimId_PhDataReq;
-	prim->u.phDataReq.sapi = GsmL1_Sapi_Pch;
-	bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer);
-	prim->u.phDataReq.msgUnitParam.u8Size = 22;
-	osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
+	uint8_t data[23+3]; /* prefix PLEN */
+
+	/* paging group */
+	if (!imsi || strlen(imsi) < 3)
+		return;
+	imsi += strlen(imsi) - 3;
+	data[0] = imsi[0];
+	data[1] = imsi[1];
+	data[2] = imsi[2];
+
+	bitvec_pack(block, data + 3+1);
+	data[3] = (plen << 2) | 0x01;
+	pcu_tx_data_req(0, 0, PCU_IF_SAPI_PCH, 0, 0, 0, data, 23+3);
 }
 
-int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
+static void pcu_l1if_tx_bcch(uint8_t *data, int len)
 {
-	gprs_rlcmac_rcv_block(data_ind->msgUnitParam.u8Buffer,
-		data_ind->msgUnitParam.u8Size, data_ind->u32Fn);
-
-	return 0;
+	pcu_tx_data_req(0, 0, PCU_IF_SAPI_BCCH, 0, 0, 0, data, len);
 }
 
-static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind)
+static int pcu_rx_data_ind(struct gsm_pcu_if_data *data_ind)
+{
+	int rc = 0;
+
+	LOGP(DL1IF, LOGL_DEBUG, "Data indication received: sapi=%d arfcn=%d "
+		"block=%d data=%s\n", data_ind->sapi,
+		data_ind->arfcn, data_ind->block_nr,
+		osmo_hexdump(data_ind->data, data_ind->len));
+
+	switch (data_ind->sapi) {
+	case PCU_IF_SAPI_PDTCH:
+		rc = gprs_rlcmac_rcv_block(data_ind->data, data_ind->len,
+			data_ind->fn);
+		break;
+	default:
+		LOGP(DL1IF, LOGL_ERROR, "Received PCU data indication with "
+			"unsupported sapi %d\n", data_ind->sapi);
+		rc = -EINVAL;
+	}
+
+	return rc;
+}
+
+static int pcu_rx_rts_req(struct gsm_pcu_if_rts_req *rts_req)
+{
+	int rc = 0;
+
+	LOGP(DL1IF, LOGL_DEBUG, "RTS request received: trx=%d ts=%d sapi=%d "
+		"arfcn=%d fn=%d block=%d\n", rts_req->trx_nr, rts_req->ts_nr,
+		rts_req->sapi, rts_req->arfcn, rts_req->fn, rts_req->block_nr);
+
+	switch (rts_req->sapi) {
+	case PCU_IF_SAPI_PDTCH:
+		gprs_rlcmac_rcv_rts_block(rts_req->trx_nr, rts_req->ts_nr,
+			rts_req->arfcn, rts_req->fn, rts_req->block_nr);
+		break;
+	case PCU_IF_SAPI_PTCCH:
+		/* FIXME */
+		{
+			struct msgb *msg = msgb_alloc(23, "l1_prim");
+			memset(msgb_put(msg, 23), 0x2b, 23);
+			pcu_l1if_tx_ptcch(msg, rts_req->trx_nr, rts_req->ts_nr,
+				rts_req->arfcn, rts_req->fn, rts_req->block_nr);
+		}
+		break;
+	default:
+		LOGP(DL1IF, LOGL_ERROR, "Received PCU RTS request with "
+			"unsupported sapi %d\n", rts_req->sapi);
+		rc = -EINVAL;
+	}
+
+	return rc;
+}
+
+static int pcu_rx_rach_ind(struct gsm_pcu_if_rach_ind *rach_ind)
+{
+	int rc = 0;
+
+	LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d "
+		"qta=%d, ra=%d, fn=%d\n", rach_ind->sapi, rach_ind->qta,
+		rach_ind->ra, rach_ind->fn);
+
+	switch (rach_ind->sapi) {
+	case PCU_IF_SAPI_RACH:
+		rc = gprs_rlcmac_rcv_rach(rach_ind->ra, rach_ind->fn,
+			rach_ind->qta);
+		break;
+	default:
+		LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with "
+			"unsupported sapi %d\n", rach_ind->sapi);
+		rc = -EINVAL;
+	}
+
+	return rc;
+}
+
+static int pcu_rx_info_ind(struct gsm_pcu_if_info_ind *info_ind)
 {
 	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
+	int rc = 0;
+	int trx, ts, tfi;
+	struct gprs_rlcmac_tbf *tbf;
+	int i;
 
-	bts->trx[0].arfcn = connect_ind->u16Arfcn;
-	bts->trx[0].pdch[connect_ind->u8Tn].enable = 1;
-	bts->trx[0].pdch[connect_ind->u8Tn].tsc = connect_ind->u8Tsc;
-	(l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn;
-	(l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn;
-	(l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc;
-	LOGP(DL1IF, LOGL_NOTICE, "RX: [ PCU <- BTS ] PhConnectInd: ARFCN: %u TN: %u TSC: %u \n",
-	        connect_ind->u16Arfcn, (unsigned)connect_ind->u8Tn, (unsigned)connect_ind->u8Tsc);
+	LOGP(DL1IF, LOGL_DEBUG, "Info indication received:\n");
+
+	if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) {
+		LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n");
+bssgp_failed:
+		/* free all TBF */
+		for (trx = 0; trx < 8; trx++) {
+			bts->trx[trx].arfcn = info_ind->trx[trx].arfcn;
+			for (ts = 0; ts < 8; ts++) {
+				for (tfi = 0; tfi < 32; tfi++) {
+					tbf = bts->trx[trx].pdch[ts].tbf[tfi];
+					if (tbf)
+						tbf_free(tbf);
+				}
+			}
+		}
+		gprs_bssgp_destroy();
+		return 0;
+	}
+	LOGP(DL1IF, LOGL_INFO, "BTS available\n");
+	LOGP(DL1IF, LOGL_DEBUG, " mcc=%d\n", info_ind->mcc);
+	LOGP(DL1IF, LOGL_DEBUG, " mnc=%d\n", info_ind->mnc);
+	LOGP(DL1IF, LOGL_DEBUG, " lac=%d\n", info_ind->lac);
+	LOGP(DL1IF, LOGL_DEBUG, " rac=%d\n", info_ind->rac);
+	LOGP(DL1IF, LOGL_DEBUG, " cell_id=%d\n", info_ind->cell_id);
+	LOGP(DL1IF, LOGL_DEBUG, " nsei=%d\n", info_ind->nsei);
+	LOGP(DL1IF, LOGL_DEBUG, " nse_timer=%d %d %d %d %d %d %d\n",
+		info_ind->nse_timer[0], info_ind->nse_timer[1],
+		info_ind->nse_timer[2], info_ind->nse_timer[3],
+		info_ind->nse_timer[4], info_ind->nse_timer[5],
+		info_ind->nse_timer[6]);
+	LOGP(DL1IF, LOGL_DEBUG, " cell_timer=%d %d %d %d %d %d %d %d %d %d "
+		"%d\n",
+		info_ind->cell_timer[0], info_ind->cell_timer[1],
+		info_ind->cell_timer[2], info_ind->cell_timer[3],
+		info_ind->cell_timer[4], info_ind->cell_timer[5],
+		info_ind->cell_timer[6], info_ind->cell_timer[7],
+		info_ind->cell_timer[8], info_ind->cell_timer[9],
+		info_ind->cell_timer[10]);
+	LOGP(DL1IF, LOGL_DEBUG, " repeat_time=%d\n", info_ind->repeat_time);
+	LOGP(DL1IF, LOGL_DEBUG, " repeat_count=%d\n", info_ind->repeat_count);
+	LOGP(DL1IF, LOGL_DEBUG, " bvci=%d\n", info_ind->bvci);
+	LOGP(DL1IF, LOGL_DEBUG, " t3142=%d\n", info_ind->t3142);
+	LOGP(DL1IF, LOGL_DEBUG, " t3169=%d\n", info_ind->t3169);
+	LOGP(DL1IF, LOGL_DEBUG, " t3191=%d\n", info_ind->t3191);
+	LOGP(DL1IF, LOGL_DEBUG, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10);
+	LOGP(DL1IF, LOGL_DEBUG, " t3195=%d\n", info_ind->t3195);
+	LOGP(DL1IF, LOGL_DEBUG, " n3101=%d\n", info_ind->n3101);
+	LOGP(DL1IF, LOGL_DEBUG, " n3103=%d\n", info_ind->n3103);
+	LOGP(DL1IF, LOGL_DEBUG, " n3105=%d\n", info_ind->n3105);
+	LOGP(DL1IF, LOGL_DEBUG, " cv_countdown=%d\n", info_ind->cv_countdown);
+	LOGP(DL1IF, LOGL_DEBUG, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext);
+	LOGP(DL1IF, LOGL_DEBUG, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext);
+	for (i = 0; i < 4; i++) {
+		if ((info_ind->flags & (PCU_IF_FLAG_CS1 << i)))
+			LOGP(DL1IF, LOGL_DEBUG, " Use CS%d\n", i+1);
+	}
+	for (i = 0; i < 9; i++) {
+		if ((info_ind->flags & (PCU_IF_FLAG_MCS1 << i)))
+			LOGP(DL1IF, LOGL_DEBUG, " Use MCS%d\n", i+1);
+	}
+	LOGP(DL1IF, LOGL_DEBUG, " initial_cs=%d\n", info_ind->initial_cs);
+	LOGP(DL1IF, LOGL_DEBUG, " initial_mcs=%d\n", info_ind->initial_mcs);
+	LOGP(DL1IF, LOGL_DEBUG, " nsvci=%d\n", info_ind->nsvci[0]);
+	LOGP(DL1IF, LOGL_DEBUG, " local_port=%d\n", info_ind->local_port[0]);
+	LOGP(DL1IF, LOGL_DEBUG, " remote_port=%d\n", info_ind->remote_port[0]);
+	LOGP(DL1IF, LOGL_DEBUG, " remote_ip=%d\n", info_ind->remote_ip[0]);
+
+	rc = gprs_bssgp_create(info_ind->remote_ip[0], info_ind->remote_port[0],
+		info_ind->nsei, info_ind->nsvci[0], info_ind->bvci,
+		info_ind->mcc, info_ind->mnc, info_ind->lac, info_ind->rac,
+		info_ind->cell_id);
+	if (rc < 0) {
+		LOGP(DL1IF, LOGL_NOTICE, "SGSN not available\n");
+		goto bssgp_failed;
+	}
+
+	bts->cs1 = !!(info_ind->flags & PCU_IF_FLAG_CS1);
+	bts->cs2 = !!(info_ind->flags & PCU_IF_FLAG_CS2);
+	bts->cs3 = !!(info_ind->flags & PCU_IF_FLAG_CS3);
+	bts->cs4 = !!(info_ind->flags & PCU_IF_FLAG_CS4);
+	if (!bts->cs1 && !bts->cs2 && !bts->cs3 && !bts->cs4)
+		bts->cs1 = 1;
+	if (info_ind->t3142) { /* if timer values are set */
+		bts->t3142 = info_ind->t3142;
+		bts->t3169 = info_ind->t3169;
+		bts->t3191 = info_ind->t3191;
+		bts->t3193_msec = info_ind->t3193_10ms * 10;
+		bts->t3195 = info_ind->t3195;
+		bts->n3101 = info_ind->n3101;
+		bts->n3103 = info_ind->n3103;
+		bts->n3105 = info_ind->n3105;
+	}
+	if (info_ind->initial_cs < 1 || info_ind->initial_cs > 4)
+		bts->initial_cs = 1;
+	else
+		bts->initial_cs = info_ind->initial_cs;
+
+	for (trx = 0; trx < 8; trx++) {
+		bts->trx[trx].arfcn = info_ind->trx[trx].arfcn;
+		for (ts = 0; ts < 8; ts++) {
+			if ((info_ind->trx[trx].pdch_mask & (1 << ts))) {
+				/* FIXME: activate dynamically at RLCMAC */
+				if (!bts->trx[trx].pdch[ts].enable)
+					pcu_tx_act_req(trx, ts, 1);
+				bts->trx[trx].pdch[ts].enable = 1;
+				bts->trx[trx].pdch[ts].tsc =
+					info_ind->trx[trx].tsc[ts];
+				LOGP(DL1IF, LOGL_INFO, "PDCH: trx=%d ts=%d\n",
+					trx, ts);
+			} else {
+				if (bts->trx[trx].pdch[ts].enable)
+					pcu_tx_act_req(trx, ts, 0);
+				bts->trx[trx].pdch[ts].enable = 0;
+				/* kick all tbf  FIXME: multislot  */
+				for (tfi = 0; tfi < 32; tfi++) {
+					tbf = bts->trx[trx].pdch[ts].tbf[tfi];
+					if (tbf)
+						tbf_free(tbf);
+				}
+			}
+		}
+	}
+
+	return rc;
+}
+
+static int pcu_rx_time_ind(struct gsm_pcu_if_time_ind *time_ind)
+{
+	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
+	int trx, ts, tfi;
+	struct gprs_rlcmac_tbf *tbf;
+	uint32_t elapsed;
+	uint8_t fn13 = time_ind->fn % 13;
+
+	/* omit frame numbers not starting at a MAC block */
+	if (fn13 != 0 && fn13 != 4 && fn13 != 8)
+		return 0;
+
+	LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n",
+		time_ind->fn % 52);
+
+	set_current_fn(time_ind->fn);
+
+	/* check for poll timeout */
+	for (trx = 0; trx < 8; trx++) {
+		for (ts = 0; ts < 8; ts++) {
+			for (tfi = 0; tfi < 32; tfi++) {
+				tbf = bts->trx[trx].pdch[ts].tbf[tfi];
+				if (!tbf)
+					continue;
+				if (tbf->poll_state != GPRS_RLCMAC_POLL_SCHED)
+					continue;
+				elapsed = (frame_number - tbf->poll_fn)
+							% 2715648;
+				if (elapsed >= 20 && elapsed < 200)
+					gprs_rlcmac_poll_timeout(tbf);
+			}
+		}
+	}
 
 	return 0;
 }
 
-static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
-{
-	gprs_rlcmac_rcv_rts_block(0,0, (l1fh->fl1h)->channel_info.arfcn, readytosend_ind->u32Fn, 0);
-	return 1;
-}
-
-static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
+int pcu_rx(uint8_t msg_type, struct gsm_pcu_if *pcu_prim)
 {
 	int rc = 0;
-	switch (data_ind->sapi) {
-	case GsmL1_Sapi_Rach:
+
+	switch (msg_type) {
+	case PCU_IF_MSG_DATA_IND:
+		rc = pcu_rx_data_ind(&pcu_prim->u.data_ind);
 		break;
-	case GsmL1_Sapi_Pdtch:
-	case GsmL1_Sapi_Pacch:
-		pcu_l1if_rx_pdch(data_ind);
+	case PCU_IF_MSG_RTS_REQ:
+		rc = pcu_rx_rts_req(&pcu_prim->u.rts_req);
 		break;
-	case GsmL1_Sapi_Pbcch:
-	case GsmL1_Sapi_Pagch:
-	case GsmL1_Sapi_Ppch:
-	case GsmL1_Sapi_Pnch:
-	case GsmL1_Sapi_Ptcch:
-	case GsmL1_Sapi_Prach:
+	case PCU_IF_MSG_RACH_IND:
+		rc = pcu_rx_rach_ind(&pcu_prim->u.rach_ind);
+		break;
+	case PCU_IF_MSG_INFO_IND:
+		rc = pcu_rx_info_ind(&pcu_prim->u.info_ind);
+		break;
+	case PCU_IF_MSG_TIME_IND:
+		rc = pcu_rx_time_ind(&pcu_prim->u.time_ind);
 		break;
 	default:
-		LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
-		break;
+		LOGP(DL1IF, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
+			msg_type);
+		rc = -EINVAL;
 	}
 
 	return rc;
 }
-
-static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind)
-{
-	int rc = 0;
-	(l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming;
-	rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming);
-	return rc;
-}
-
-/* handle any random indication from the L1 */
-int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
-{
-	GsmL1_Prim_t *l1p = msgb_l1prim(msg);
-	int rc = 0;
-
-	switch (l1p->id) {
-	case GsmL1_PrimId_PhConnectInd:
-		rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd);
-		break;
-	case GsmL1_PrimId_PhReadyToSendInd:
-		rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
-		break;
-	case GsmL1_PrimId_PhDataInd:
-		rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
-		break;
-	case GsmL1_PrimId_PhRaInd:
-		rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd);
-		break;
-	default:
-		break;
-	}
-
-	/* Special return value '1' means: do not free */
-	if (rc != 1)
-		msgb_free(msg);
-
-	return rc;
-}
-
-PCU will currently not work without getting a GSM time or BFI indidication.
-In order to fix this, i will discuss this on the mailing list.
-Andreas
-
-/* OpenBTS socket functions */
-
-// TODO: We should move this parameters to config file.
-#define PCU_L1_IF_PORT 5944
-
-/* data has arrived on the udp socket */
-static int udp_read_cb(struct osmo_fd *ofd)
-{
-	struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
-	struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
-	struct femtol1_hdl *fl1h = l1fh->fl1h;
-	int rc;
-
-	if (!msg)
-		return -ENOMEM;
-
-	msg->l1h = msg->data;
-
-	l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
-	rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
-			(struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
-	if (rc < 0) {
-		perror("read from udp");
-		msgb_free(msg);
-		return rc;
-	} else if (rc == 0) {
-		perror("len=0 read from udp");
-		msgb_free(msg);
-		return rc;
-	}
-	msgb_put(msg, rc);
-
-	rc = pcu_l1if_handle_l1prim(fl1h, msg);
-	return rc;
-}
-
-/* callback when we can write to the UDP socket */
-static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
-{
-	int rc;
-	struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
-
-	//DEBUGP(DGPRS, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_l1len(msg));
-
-	rc = sendto(ofd->fd, msg->l1h, msgb_l1len(msg), 0,
-			(const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
-	if (rc < 0) {
-		LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
-			strerror(errno));
-		return rc;
-	} else if (rc < (int)msgb_l1len(msg)) {
-		LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
-			"%u < %u\n", rc, msgb_l1len(msg));
-		return -EIO;
-	}
-
-	return 0;
-}
-
-// TODO: We should move this parameters to config file.
-#define SGSN_IP 127.0.0.1
-#define SGSN_PORT 23000
-#define NSEI 3
-#define NSVCI 4
-
-#define BVCI 7
-
-#define CELL_ID 0
-#define MNC 2
-#define MCC 262
-#define PCU_LAC 1
-#define PCU_RAC 0
-
-int pcu_l1if_open()
-{
-	//struct l1fwd_hdl *l1fh;
-	struct femtol1_hdl *fl1h;
-	int rc;
-
-	struct sockaddr_in dest;
-	dest.sin_family = AF_INET;
-	dest.sin_port = htons(SGSN_PORT);
-	inet_aton(SGSN_IP, &dest.sin_addr);
-
-	rc = gprs_bssgp_create(ntohl(dest.sin_addr.s_addr), SGSN_PORT, NSEI, NSVCI, BVCI, MCC, MNC, PCU_LAC, PCU_RAC, CELL_ID);
-	if (rc < 0)
-		return rc;
-
-	/* allocate new femtol1_handle */
-	fl1h = talloc_zero(NULL, struct femtol1_hdl);
-	INIT_LLIST_HEAD(&fl1h->wlc_list);
-
-	l1fh->fl1h = fl1h;
-	fl1h->priv = l1fh;
-
-	struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
-	osmo_wqueue_init(queue, 10);
-	queue->bfd.when |= BSC_FD_READ;
-	queue->bfd.data = l1fh;
-	queue->bfd.priv_nr = 0;
-
-	/* Open UDP */
-	struct osmo_wqueue *wq = &l1fh->udp_wq;
-
-	osmo_wqueue_init(wq, 10);
-	wq->write_cb = udp_write_cb;
-	wq->read_cb = udp_read_cb;
-	wq->bfd.when |= BSC_FD_READ;
-	wq->bfd.data = l1fh;
-	wq->bfd.priv_nr = 0;
-	rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
-				IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
-				OSMO_SOCK_F_BIND);
-	if (rc < 0) {
-		perror("sock_init");
-		exit(1);
-	}
-
-	return 0;
-}
-
-void pcu_l1if_close(void)
-{
-	gprs_bssgp_destroy();
-
-	/* FIXME: cleanup l1if */
-	talloc_free(fl1h);
-}
diff --git a/src/pcu_l1_if.h b/src/pcu_l1_if.h
index 2aac553..f3ac597 100644
--- a/src/pcu_l1_if.h
+++ b/src/pcu_l1_if.h
@@ -36,9 +36,12 @@
 void pcu_l1if_tx_ptcch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn, 
         uint32_t fn, uint8_t block_nr);
 void pcu_l1if_tx_agch(bitvec * block, int len);
+
 void pcu_l1if_tx_pch(bitvec * block, int plen, char *imsi);
 
 int pcu_l1if_open(void);
 void pcu_l1if_close(void);
 
+int pcu_rx(uint8_t msg_type, struct gsm_pcu_if *pcu_prim);
+int pcu_sock_send(struct msgb *msg);
 #endif // PCU_L1_IF_H
diff --git a/src/pcu_main.cpp b/src/pcu_main.cpp
index ea507e5..6c30c1e 100644
--- a/src/pcu_main.cpp
+++ b/src/pcu_main.cpp
@@ -105,8 +105,8 @@
 			"together.\n");
 		exit(0);
 	}
-
 	rc = pcu_l1if_open();
+
 	if (rc < 0)
 		return rc;
 
@@ -124,4 +124,3 @@
 
 	return 0;
 }
-
diff --git a/src/pcuif_proto.h b/src/pcuif_proto.h
new file mode 100644
index 0000000..3609f79
--- /dev/null
+++ b/src/pcuif_proto.h
@@ -0,0 +1,137 @@
+#ifndef _PCUIF_PROTO_H
+#define _PCUIF_PROTO_H
+
+/* msg_type */
+#define PCU_IF_MSG_DATA_REQ	0x00	/* send data to given channel */
+#define PCU_IF_MSG_DATA_IND	0x02	/* receive data from given channel */	
+#define PCU_IF_MSG_RTS_REQ	0x10	/* ready to send data to given chan. */
+#define PCU_IF_MSG_RACH_IND	0x22	/* receive rach */
+#define PCU_IF_MSG_INFO_IND	0x32	/* retrieve BTS info */
+#define PCU_IF_MSG_ACT_REQ	0x40	/* activate/deactivate PDCH */
+#define PCU_IF_MSG_TIME_IND	0x52	/* gsm time indication */
+
+/* sapi */
+#define PCU_IF_SAPI_RACH	0x01	/* channel request on CCCH */
+#define PCU_IF_SAPI_AGCH	0x02	/* assignment on CCCH */
+#define PCU_IF_SAPI_PCH		0x03	/* paging request on CCCH */
+#define PCU_IF_SAPI_BCCH	0x04	/* SI on BCCH */
+#define PCU_IF_SAPI_PDTCH	0x05	/* packet data/control/ccch block */
+#define PCU_IF_SAPI_PRACH	0x06	/* packet random access channel */
+#define PCU_IF_SAPI_PTCCH	0x07	/* packet TA control channel */
+
+/* flags */
+#define PCU_IF_FLAG_ACTIVE	(1 << 0)/* BTS is active */
+#define PCU_IF_FLAG_SYSMO	(1 << 1)/* access PDCH of sysmoBTS directly */
+#define PCU_IF_FLAG_CS1		(1 << 16)
+#define PCU_IF_FLAG_CS2		(1 << 17)
+#define PCU_IF_FLAG_CS3		(1 << 18)
+#define PCU_IF_FLAG_CS4		(1 << 19)
+#define PCU_IF_FLAG_MCS1	(1 << 20)
+#define PCU_IF_FLAG_MCS2	(1 << 21)
+#define PCU_IF_FLAG_MCS3	(1 << 22)
+#define PCU_IF_FLAG_MCS4	(1 << 23)
+#define PCU_IF_FLAG_MCS5	(1 << 24)
+#define PCU_IF_FLAG_MCS6	(1 << 25)
+#define PCU_IF_FLAG_MCS7	(1 << 26)
+#define PCU_IF_FLAG_MCS8	(1 << 27)
+#define PCU_IF_FLAG_MCS9	(1 << 28)
+
+struct gsm_pcu_if_data {
+	uint8_t		sapi;
+	uint8_t		len;
+	uint8_t		data[162];
+	uint32_t	fn;
+	uint16_t	arfcn;
+	uint8_t		trx_nr;
+	uint8_t		ts_nr;
+	uint8_t		block_nr;
+} __attribute__ ((packed));
+
+struct gsm_pcu_if_rts_req {
+	uint8_t		sapi;
+	uint8_t		spare[3];
+	uint32_t	fn;
+	uint16_t	arfcn;
+	uint8_t		trx_nr;
+	uint8_t		ts_nr;
+	uint8_t		block_nr;
+} __attribute__ ((packed));
+
+struct gsm_pcu_if_rach_ind {
+	uint8_t		sapi;
+	uint8_t		ra;
+	int16_t		qta;
+	uint32_t	fn;
+	uint16_t	arfcn;
+} __attribute__ ((packed));
+
+struct gsm_pcu_if_info_trx {
+	uint16_t	arfcn;
+	uint8_t		pdch_mask;		/* PDCH channels per TS */
+	uint8_t		spare;
+	uint8_t		tsc[8];			/* TSC per channel */
+} __attribute__ ((packed));
+
+struct gsm_pcu_if_info_ind {
+	uint32_t	flags;
+	struct gsm_pcu_if_info_trx trx[8];	/* TRX infos per BTS */
+	/* RAI */
+	uint16_t	mcc, mnc, lac, rac;
+	/* NSE */
+	uint16_t	nsei;
+	uint8_t		nse_timer[7];
+	uint8_t		cell_timer[11];
+	/* cell  */
+	uint16_t	cell_id;
+	uint16_t	repeat_time;
+	uint8_t		repeat_count;
+	uint16_t	bvci;
+	uint8_t		t3142;
+	uint8_t		t3169;
+	uint8_t		t3191;
+	uint8_t		t3193_10ms;
+	uint8_t		t3195;
+	uint8_t		n3101;
+	uint8_t		n3103;
+	uint8_t		n3105;
+	uint8_t		cv_countdown;
+	uint16_t	dl_tbf_ext;
+	uint16_t	ul_tbf_ext;
+	uint8_t		initial_cs;
+	uint8_t		initial_mcs;
+	/* NSVC */
+	uint16_t	nsvci[2];
+	uint16_t	local_port[2];
+	uint16_t	remote_port[2];
+	uint32_t	remote_ip[2];
+} __attribute__ ((packed));
+
+struct gsm_pcu_if_act_req {
+	uint8_t		activate;
+	uint8_t		trx_nr;
+	uint8_t		ts_nr;
+	uint8_t		spare;
+} __attribute__ ((packed));
+
+struct gsm_pcu_if_time_ind {
+	uint32_t	fn;
+} __attribute__ ((packed));
+
+struct gsm_pcu_if {
+	/* context based information */
+	uint8_t		msg_type;	/* message type */
+	uint8_t		bts_nr;		/* bts number */
+	uint8_t		spare[2];
+
+	union {
+		struct gsm_pcu_if_data		data_req;
+		struct gsm_pcu_if_data		data_ind;
+		struct gsm_pcu_if_rts_req	rts_req;
+		struct gsm_pcu_if_rach_ind	rach_ind;
+		struct gsm_pcu_if_info_ind	info_ind;
+		struct gsm_pcu_if_act_req	act_req;
+		struct gsm_pcu_if_time_ind	time_ind;
+	} u;
+} __attribute__ ((packed));
+
+#endif /* _PCUIF_PROTO_H */
diff --git a/src/sysmo_l1_if.cpp b/src/sysmo_l1_if.cpp
deleted file mode 100644
index ceb28d2..0000000
--- a/src/sysmo_l1_if.cpp
+++ /dev/null
@@ -1,716 +0,0 @@
-/* sysmo_l1_if.cpp
- *
- * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
- *
- * 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 <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <assert.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-extern "C" {
-#include <osmocom/core/talloc.h>
-#include <osmocom/core/select.h>
-#include <osmocom/core/msgb.h>
-}
-
-#include <gprs_rlcmac.h>
-#include <pcu_l1_if.h>
-#include <gprs_debug.h>
-#include <gprs_bssgp_pcu.h>
-#include "../../osmo-bts/include/osmo-bts/pcuif_proto.h"
-
-static int pcu_sock_send(struct msgb *msg);
-static void pcu_sock_timeout(void *_priv);
-
-// Variable for storage current FN.
-int frame_number;
-
-int get_current_fn()
-{
-	return frame_number;
-}
-
-void set_current_fn(int fn)
-{
-	frame_number = fn;
-}
-
-/*
- * PCU messages
- */
-
-struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
-{
-	struct msgb *msg;
-	struct gsm_pcu_if *pcu_prim;
-
-	msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
-	if (!msg)
-		return NULL;
-	msgb_put(msg, sizeof(struct gsm_pcu_if));
-	pcu_prim = (struct gsm_pcu_if *) msg->data;
-	pcu_prim->msg_type = msg_type;
-	pcu_prim->bts_nr = bts_nr;
-
-	return msg;
-}
-
-static int pcu_tx_act_req(uint8_t trx, uint8_t ts, uint8_t activate)
-{
-	struct msgb *msg;
-	struct gsm_pcu_if *pcu_prim;
-	struct gsm_pcu_if_act_req *act_req;
-
-	LOGP(DL1IF, LOGL_INFO, "Sending %s request: trx=%d ts=%d\n",
-		(activate) ? "activate" : "deactivate", trx, ts);
-
-	msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, 0);
-	if (!msg)
-		return -ENOMEM;
-	pcu_prim = (struct gsm_pcu_if *) msg->data;
-	act_req = &pcu_prim->u.act_req;
-	act_req->activate = activate;
-	act_req->trx_nr = trx;
-	act_req->ts_nr = ts;
-
-	return pcu_sock_send(msg);
-}
-
-static int pcu_tx_data_req(uint8_t trx, uint8_t ts, uint8_t sapi,
-	uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data,
-	uint8_t len)
-{
-	struct msgb *msg;
-	struct gsm_pcu_if *pcu_prim;
-	struct gsm_pcu_if_data *data_req;
-
-	LOGP(DL1IF, LOGL_DEBUG, "Sending data request: trx=%d ts=%d sapi=%d "
-		"arfcn=%d fn=%d block=%d data=%s\n", trx, ts, sapi, arfcn, fn,
-		block_nr, osmo_hexdump(data, len));
-
-	msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, 0);
-	if (!msg)
-		return -ENOMEM;
-	pcu_prim = (struct gsm_pcu_if *) msg->data;
-	data_req = &pcu_prim->u.data_req;
-
-	data_req->sapi = sapi;
-	data_req->fn = fn;
-	data_req->arfcn = arfcn;
-	data_req->trx_nr = trx;
-	data_req->ts_nr = ts;
-	data_req->block_nr = block_nr;
-	memcpy(data_req->data, data, len);
-	data_req->len = len;
-
-	return pcu_sock_send(msg);
-}
-
-void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
-	uint32_t fn, uint8_t block_nr)
-{
-	pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
-		msg->data, msg->len);
-	msgb_free(msg);
-}
-
-void pcu_l1if_tx_ptcch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
-	uint32_t fn, uint8_t block_nr)
-{
-	pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr,
-		msg->data, msg->len);
-	msgb_free(msg);
-}
-
-void pcu_l1if_tx_agch(bitvec * block, int plen)
-{
-	uint8_t data[23]; /* prefix PLEN */
-	
-	/* FIXME: why does OpenBTS has no PLEN and no fill in message? */
-	bitvec_pack(block, data + 1);
-	data[0] = (plen << 2) | 0x01;
-	pcu_tx_data_req(0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, 23);
-}
-
-void pcu_l1if_tx_pch(bitvec * block, int plen, char *imsi)
-{
-	uint8_t data[23+3]; /* prefix PLEN */
-
-	/* paging group */
-	if (!imsi || strlen(imsi) < 3)
-		return;
-	imsi += strlen(imsi) - 3;
-	data[0] = imsi[0];
-	data[1] = imsi[1];
-	data[2] = imsi[2];
-
-	bitvec_pack(block, data + 3+1);
-	data[3] = (plen << 2) | 0x01;
-	pcu_tx_data_req(0, 0, PCU_IF_SAPI_PCH, 0, 0, 0, data, 23+3);
-}
-
-static void pcu_l1if_tx_bcch(uint8_t *data, int len)
-{
-	pcu_tx_data_req(0, 0, PCU_IF_SAPI_BCCH, 0, 0, 0, data, len);
-}
-
-static int pcu_rx_data_ind(struct gsm_pcu_if_data *data_ind)
-{
-	int rc = 0;
-
-	LOGP(DL1IF, LOGL_DEBUG, "Data indication received: sapi=%d arfcn=%d "
-		"block=%d data=%s\n", data_ind->sapi,
-		data_ind->arfcn, data_ind->block_nr,
-		osmo_hexdump(data_ind->data, data_ind->len));
-
-	switch (data_ind->sapi) {
-	case PCU_IF_SAPI_PDTCH:
-		rc = gprs_rlcmac_rcv_block(data_ind->data, data_ind->len,
-			data_ind->fn);
-		break;
-	default:
-		LOGP(DL1IF, LOGL_ERROR, "Received PCU data indication with "
-			"unsupported sapi %d\n", data_ind->sapi);
-		rc = -EINVAL;
-	}
-
-	return rc;
-}
-
-static int pcu_rx_rts_req(struct gsm_pcu_if_rts_req *rts_req)
-{
-	int rc = 0;
-
-	LOGP(DL1IF, LOGL_DEBUG, "RTS request received: trx=%d ts=%d sapi=%d "
-		"arfcn=%d fn=%d block=%d\n", rts_req->trx_nr, rts_req->ts_nr,
-		rts_req->sapi, rts_req->arfcn, rts_req->fn, rts_req->block_nr);
-
-	switch (rts_req->sapi) {
-	case PCU_IF_SAPI_PDTCH:
-		gprs_rlcmac_rcv_rts_block(rts_req->trx_nr, rts_req->ts_nr,
-			rts_req->arfcn, rts_req->fn, rts_req->block_nr);
-		break;
-	case PCU_IF_SAPI_PTCCH:
-		/* FIXME */
-		{
-			struct msgb *msg = msgb_alloc(23, "l1_prim");
-			memset(msgb_put(msg, 23), 0x2b, 23);
-			pcu_l1if_tx_ptcch(msg, rts_req->trx_nr, rts_req->ts_nr,
-				rts_req->arfcn, rts_req->fn, rts_req->block_nr);
-		}
-		break;
-	default:
-		LOGP(DL1IF, LOGL_ERROR, "Received PCU RTS request with "
-			"unsupported sapi %d\n", rts_req->sapi);
-		rc = -EINVAL;
-	}
-
-	return rc;
-}
-
-static int pcu_rx_rach_ind(struct gsm_pcu_if_rach_ind *rach_ind)
-{
-	int rc = 0;
-
-	LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d "
-		"qta=%d, ra=%d, fn=%d\n", rach_ind->sapi, rach_ind->qta,
-		rach_ind->ra, rach_ind->fn);
-
-	switch (rach_ind->sapi) {
-	case PCU_IF_SAPI_RACH:
-		rc = gprs_rlcmac_rcv_rach(rach_ind->ra, rach_ind->fn,
-			rach_ind->qta);
-		break;
-	default:
-		LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with "
-			"unsupported sapi %d\n", rach_ind->sapi);
-		rc = -EINVAL;
-	}
-
-	return rc;
-}
-
-static int pcu_rx_info_ind(struct gsm_pcu_if_info_ind *info_ind)
-{
-	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
-	int rc = 0;
-	int trx, ts, tfi;
-	struct gprs_rlcmac_tbf *tbf;
-	int i;
-
-	LOGP(DL1IF, LOGL_DEBUG, "Info indication received:\n");
-
-	if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) {
-		LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n");
-bssgp_failed:
-		/* free all TBF */
-		for (trx = 0; trx < 8; trx++) {
-			bts->trx[trx].arfcn = info_ind->trx[trx].arfcn;
-			for (ts = 0; ts < 8; ts++) {
-				for (tfi = 0; tfi < 32; tfi++) {
-					tbf = bts->trx[trx].pdch[ts].tbf[tfi];
-					if (tbf)
-						tbf_free(tbf);
-				}
-			}
-		}
-		gprs_bssgp_destroy();
-		return 0;
-	}
-	LOGP(DL1IF, LOGL_INFO, "BTS available\n");
-	LOGP(DL1IF, LOGL_DEBUG, " mcc=%d\n", info_ind->mcc);
-	LOGP(DL1IF, LOGL_DEBUG, " mnc=%d\n", info_ind->mnc);
-	LOGP(DL1IF, LOGL_DEBUG, " lac=%d\n", info_ind->lac);
-	LOGP(DL1IF, LOGL_DEBUG, " rac=%d\n", info_ind->rac);
-	LOGP(DL1IF, LOGL_DEBUG, " cell_id=%d\n", info_ind->cell_id);
-	LOGP(DL1IF, LOGL_DEBUG, " nsei=%d\n", info_ind->nsei);
-	LOGP(DL1IF, LOGL_DEBUG, " nse_timer=%d %d %d %d %d %d %d\n",
-		info_ind->nse_timer[0], info_ind->nse_timer[1],
-		info_ind->nse_timer[2], info_ind->nse_timer[3],
-		info_ind->nse_timer[4], info_ind->nse_timer[5],
-		info_ind->nse_timer[6]);
-	LOGP(DL1IF, LOGL_DEBUG, " cell_timer=%d %d %d %d %d %d %d %d %d %d "
-		"%d\n",
-		info_ind->cell_timer[0], info_ind->cell_timer[1],
-		info_ind->cell_timer[2], info_ind->cell_timer[3],
-		info_ind->cell_timer[4], info_ind->cell_timer[5],
-		info_ind->cell_timer[6], info_ind->cell_timer[7],
-		info_ind->cell_timer[8], info_ind->cell_timer[9],
-		info_ind->cell_timer[10]);
-	LOGP(DL1IF, LOGL_DEBUG, " repeat_time=%d\n", info_ind->repeat_time);
-	LOGP(DL1IF, LOGL_DEBUG, " repeat_count=%d\n", info_ind->repeat_count);
-	LOGP(DL1IF, LOGL_DEBUG, " bvci=%d\n", info_ind->bvci);
-	LOGP(DL1IF, LOGL_DEBUG, " t3142=%d\n", info_ind->t3142);
-	LOGP(DL1IF, LOGL_DEBUG, " t3169=%d\n", info_ind->t3169);
-	LOGP(DL1IF, LOGL_DEBUG, " t3191=%d\n", info_ind->t3191);
-	LOGP(DL1IF, LOGL_DEBUG, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10);
-	LOGP(DL1IF, LOGL_DEBUG, " t3195=%d\n", info_ind->t3195);
-	LOGP(DL1IF, LOGL_DEBUG, " n3101=%d\n", info_ind->n3101);
-	LOGP(DL1IF, LOGL_DEBUG, " n3103=%d\n", info_ind->n3103);
-	LOGP(DL1IF, LOGL_DEBUG, " n3105=%d\n", info_ind->n3105);
-	LOGP(DL1IF, LOGL_DEBUG, " cv_countdown=%d\n", info_ind->cv_countdown);
-	LOGP(DL1IF, LOGL_DEBUG, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext);
-	LOGP(DL1IF, LOGL_DEBUG, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext);
-	for (i = 0; i < 4; i++) {
-		if ((info_ind->flags & (PCU_IF_FLAG_CS1 << i)))
-			LOGP(DL1IF, LOGL_DEBUG, " Use CS%d\n", i+1);
-	}
-	for (i = 0; i < 9; i++) {
-		if ((info_ind->flags & (PCU_IF_FLAG_MCS1 << i)))
-			LOGP(DL1IF, LOGL_DEBUG, " Use MCS%d\n", i+1);
-	}
-	LOGP(DL1IF, LOGL_DEBUG, " initial_cs=%d\n", info_ind->initial_cs);
-	LOGP(DL1IF, LOGL_DEBUG, " initial_mcs=%d\n", info_ind->initial_mcs);
-	LOGP(DL1IF, LOGL_DEBUG, " nsvci=%d\n", info_ind->nsvci[0]);
-	LOGP(DL1IF, LOGL_DEBUG, " local_port=%d\n", info_ind->local_port[0]);
-	LOGP(DL1IF, LOGL_DEBUG, " remote_port=%d\n", info_ind->remote_port[0]);
-	LOGP(DL1IF, LOGL_DEBUG, " remote_ip=%d\n", info_ind->remote_ip[0]);
-
-	rc = gprs_bssgp_create(info_ind->remote_ip[0], info_ind->remote_port[0],
-		info_ind->nsei, info_ind->nsvci[0], info_ind->bvci,
-		info_ind->mcc, info_ind->mnc, info_ind->lac, info_ind->rac,
-		info_ind->cell_id);
-	if (rc < 0) {
-		LOGP(DL1IF, LOGL_NOTICE, "SGSN not available\n");
-		goto bssgp_failed;
-	}
-
-	bts->cs1 = !!(info_ind->flags & PCU_IF_FLAG_CS1);
-	bts->cs2 = !!(info_ind->flags & PCU_IF_FLAG_CS2);
-	bts->cs3 = !!(info_ind->flags & PCU_IF_FLAG_CS3);
-	bts->cs4 = !!(info_ind->flags & PCU_IF_FLAG_CS4);
-	if (!bts->cs1 && !bts->cs2 && !bts->cs3 && !bts->cs4)
-		bts->cs1 = 1;
-	if (info_ind->t3142) { /* if timer values are set */
-		bts->t3142 = info_ind->t3142;
-		bts->t3169 = info_ind->t3169;
-		bts->t3191 = info_ind->t3191;
-		bts->t3193_msec = info_ind->t3193_10ms * 10;
-		bts->t3195 = info_ind->t3195;
-		bts->n3101 = info_ind->n3101;
-		bts->n3103 = info_ind->n3103;
-		bts->n3105 = info_ind->n3105;
-	}
-	if (info_ind->initial_cs < 1 || info_ind->initial_cs > 4)
-		bts->initial_cs = 1;
-	else
-		bts->initial_cs = info_ind->initial_cs;
-
-	for (trx = 0; trx < 8; trx++) {
-		bts->trx[trx].arfcn = info_ind->trx[trx].arfcn;
-		for (ts = 0; ts < 8; ts++) {
-			if ((info_ind->trx[trx].pdch_mask & (1 << ts))) {
-				/* FIXME: activate dynamically at RLCMAC */
-				if (!bts->trx[trx].pdch[ts].enable)
-					pcu_tx_act_req(trx, ts, 1);
-				bts->trx[trx].pdch[ts].enable = 1;
-				bts->trx[trx].pdch[ts].tsc =
-					info_ind->trx[trx].tsc[ts];
-				LOGP(DL1IF, LOGL_INFO, "PDCH: trx=%d ts=%d\n",
-					trx, ts);
-			} else {
-				if (bts->trx[trx].pdch[ts].enable)
-					pcu_tx_act_req(trx, ts, 0);
-				bts->trx[trx].pdch[ts].enable = 0;
-				/* kick all tbf  FIXME: multislot  */
-				for (tfi = 0; tfi < 32; tfi++) {
-					tbf = bts->trx[trx].pdch[ts].tbf[tfi];
-					if (tbf)
-						tbf_free(tbf);
-				}
-			}
-		}
-	}
-
-	return rc;
-}
-
-static int pcu_rx_time_ind(struct gsm_pcu_if_time_ind *time_ind)
-{
-	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
-	int trx, ts, tfi;
-	struct gprs_rlcmac_tbf *tbf;
-	uint32_t elapsed;
-	uint8_t fn13 = time_ind->fn % 13;
-
-	/* omit frame numbers not starting at a MAC block */
-	if (fn13 != 0 && fn13 != 4 && fn13 != 8)
-		return 0;
-
-	LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n",
-		time_ind->fn % 52);
-
-	set_current_fn(time_ind->fn);
-
-	/* check for poll timeout */
-	for (trx = 0; trx < 8; trx++) {
-		for (ts = 0; ts < 8; ts++) {
-			for (tfi = 0; tfi < 32; tfi++) {
-				tbf = bts->trx[trx].pdch[ts].tbf[tfi];
-				if (!tbf)
-					continue;
-				if (tbf->poll_state != GPRS_RLCMAC_POLL_SCHED)
-					continue;
-				elapsed = (frame_number - tbf->poll_fn)
-							% 2715648;
-				if (elapsed >= 20 && elapsed < 200)
-					gprs_rlcmac_poll_timeout(tbf);
-			}
-		}
-	}
-
-	return 0;
-}
-
-static int pcu_rx(uint8_t msg_type, struct gsm_pcu_if *pcu_prim)
-{
-	int rc = 0;
-
-	switch (msg_type) {
-	case PCU_IF_MSG_DATA_IND:
-		rc = pcu_rx_data_ind(&pcu_prim->u.data_ind);
-		break;
-	case PCU_IF_MSG_RTS_REQ:
-		rc = pcu_rx_rts_req(&pcu_prim->u.rts_req);
-		break;
-	case PCU_IF_MSG_RACH_IND:
-		rc = pcu_rx_rach_ind(&pcu_prim->u.rach_ind);
-		break;
-	case PCU_IF_MSG_INFO_IND:
-		rc = pcu_rx_info_ind(&pcu_prim->u.info_ind);
-		break;
-	case PCU_IF_MSG_TIME_IND:
-		rc = pcu_rx_time_ind(&pcu_prim->u.time_ind);
-		break;
-	default:
-		LOGP(DL1IF, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
-			msg_type);
-		rc = -EINVAL;
-	}
-
-	return rc;
-}
-
-/*
- * SYSMO-PCU socket functions
- */
-
-struct pcu_sock_state {
-	struct osmo_fd conn_bfd;	/* fd for connection to lcr */
-	struct osmo_timer_list timer;	/* socket connect retry timer */
-	struct llist_head upqueue;	/* queue for sending messages */
-} *pcu_sock_state = NULL;
-
-static int pcu_sock_send(struct msgb *msg)
-{
-	struct pcu_sock_state *state = pcu_sock_state;
-	struct osmo_fd *conn_bfd;
-
-	if (!state) {
-		LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
-			"message\n");
-		return -EINVAL;
-	}
-	conn_bfd = &state->conn_bfd;
-	if (conn_bfd->fd <= 0) {
-		LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
-			"message\n");
-		return -EIO;
-	}
-	msgb_enqueue(&state->upqueue, msg);
-	conn_bfd->when |= BSC_FD_WRITE;
-
-	return 0;
-}
-
-static void pcu_sock_close(struct pcu_sock_state *state)
-{
-	struct osmo_fd *bfd = &state->conn_bfd;
-	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
-	struct gprs_rlcmac_tbf *tbf;
-	uint8_t trx, ts, tfi;
-
-	LOGP(DL1IF, LOGL_NOTICE, "PCU socket has LOST connection\n");
-
-	close(bfd->fd);
-	bfd->fd = -1;
-	osmo_fd_unregister(bfd);
-
-	/* flush the queue */
-	while (!llist_empty(&state->upqueue)) {
-		struct msgb *msg = msgb_dequeue(&state->upqueue);
-		msgb_free(msg);
-	}
-
-	/* disable all slots, kick all TBFs */
-	for (trx = 0; trx < 8; trx++) {
-		for (ts = 0; ts < 8; ts++) {
-			bts->trx[trx].pdch[ts].enable = 0;
-			for (tfi = 0; tfi < 32; tfi++) {
-				tbf = bts->trx[trx].pdch[ts].tbf[tfi];
-				if (tbf)
-					tbf_free(tbf);
-			}
-		}
-	}
-
-	gprs_bssgp_destroy();
-
-	state->timer.cb = pcu_sock_timeout;
-	osmo_timer_schedule(&state->timer, 5, 0);
-}
-
-static int pcu_sock_read(struct osmo_fd *bfd)
-{
-	struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
-	struct gsm_pcu_if *pcu_prim;
-	struct msgb *msg;
-	int rc;
-
-	msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
-	if (!msg)
-		return -ENOMEM;
-
-	pcu_prim = (struct gsm_pcu_if *) msg->tail;
-
-	rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
-	if (rc == 0)
-		goto close;
-
-	if (rc < 0) {
-		if (errno == EAGAIN)
-			return 0;
-		goto close;
-	}
-
-	rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
-
-	/* as we always synchronously process the message in pcu_rx() and
-	 * its callbacks, we can free the message here. */
-	msgb_free(msg);
-
-	return rc;
-
-close:
-	msgb_free(msg);
-	pcu_sock_close(state);
-	return -1;
-}
-
-static int pcu_sock_write(struct osmo_fd *bfd)
-{
-	struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
-	int rc;
-
-	while (!llist_empty(&state->upqueue)) {
-		struct msgb *msg, *msg2;
-		struct gsm_pcu_if *pcu_prim;
-
-		/* peek at the beginning of the queue */
-		msg = llist_entry(state->upqueue.next, struct msgb, list);
-		pcu_prim = (struct gsm_pcu_if *)msg->data;
-
-		bfd->when &= ~BSC_FD_WRITE;
-
-		/* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
-		if (!msgb_length(msg)) {
-			LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
-				"bytes!\n", pcu_prim->msg_type);
-			goto dontsend;
-		}
-
-		/* try to send it over the socket */
-		rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
-		if (rc == 0)
-			goto close;
-		if (rc < 0) {
-			if (errno == EAGAIN) {
-				bfd->when |= BSC_FD_WRITE;
-				break;
-			}
-			goto close;
-		}
-
-dontsend:
-		/* _after_ we send it, we can deueue */
-		msg2 = msgb_dequeue(&state->upqueue);
-		assert(msg == msg2);
-		msgb_free(msg);
-	}
-	return 0;
-
-close:
-	pcu_sock_close(state);
-
-	return -1;
-}
-
-static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
-{
-	int rc = 0;
-
-	if (flags & BSC_FD_READ)
-		rc = pcu_sock_read(bfd);
-	if (rc < 0)
-		return rc;
-
-	if (flags & BSC_FD_WRITE)
-		rc = pcu_sock_write(bfd);
-
-	return rc;
-}
-
-int pcu_l1if_open(void)
-{
-	struct pcu_sock_state *state;
-	struct osmo_fd *bfd;
-	struct sockaddr_un local;
-	unsigned int namelen;
-	int rc;
-
-	state = pcu_sock_state;
-	if (!state) {
-		state = talloc_zero(NULL, struct pcu_sock_state);
-		if (!state)
-			return -ENOMEM;
-		INIT_LLIST_HEAD(&state->upqueue);
-	}
-
-	bfd = &state->conn_bfd;
-
-	bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
-	if (bfd->fd < 0) {
-		LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n");
-		talloc_free(state);
-		return -1;
-	}
-
-	local.sun_family = AF_UNIX;
-	strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path));
-	local.sun_path[sizeof(local.sun_path) - 1] = '\0';
-
-	/* we use the same magic that X11 uses in Xtranssock.c for
-	 * calculating the proper length of the sockaddr */
-#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
-	local.sun_len = strlen(local.sun_path);
-#endif
-#if defined(BSD44SOCKETS) || defined(SUN_LEN)
-	namelen = SUN_LEN(&local);
-#else
-	namelen = strlen(local.sun_path) +
-		  offsetof(struct sockaddr_un, sun_path);
-#endif
-	rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
-	if (rc != 0) {
-		LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO "
-			"socket, delaying... '%s'\n", local.sun_path);
-		close(bfd->fd);
-		bfd->fd = -1;
-		state->timer.cb = pcu_sock_timeout;
-		osmo_timer_schedule(&state->timer, 5, 0);
-		return 0;
-	}
-
-	bfd->when = BSC_FD_READ;
-	bfd->cb = pcu_sock_cb;
-	bfd->data = state;
-
-	rc = osmo_fd_register(bfd);
-	if (rc < 0) {
-		LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
-		close(bfd->fd);
-		talloc_free(state);
-		return rc;
-	}
-
-	LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n");
-
-	pcu_sock_state = state;
-
-	return 0;
-}
-
-void pcu_l1if_close(void)
-{
-	struct pcu_sock_state *state = pcu_sock_state;
-	struct osmo_fd *bfd;
-
-	if (!state)
-		return;
-
-	if (osmo_timer_pending(&state->timer))
-		osmo_timer_del(&state->timer);
-
-	bfd = &state->conn_bfd;
-	if (bfd->fd > 0)
-		pcu_sock_close(state);
-	talloc_free(state);
-	pcu_sock_state = NULL;
-}
-
-static void pcu_sock_timeout(void *_priv)
-{
-	pcu_l1if_open();
-}
-
-
-
-
diff --git a/src/sysmo_sock.cpp b/src/sysmo_sock.cpp
new file mode 100644
index 0000000..8d83ca2
--- /dev/null
+++ b/src/sysmo_sock.cpp
@@ -0,0 +1,303 @@
+/* sysmo_sock.cpp
+ *
+ * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
+ *
+ * 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 <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+extern "C" {
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/msgb.h>
+}
+
+#include <gprs_rlcmac.h>
+#include <pcu_l1_if.h>
+#include <gprs_debug.h>
+#include <gprs_bssgp_pcu.h>
+#include <pcuif_proto.h>
+
+
+/*
+ * SYSMO-PCU socket functions
+ */
+
+struct pcu_sock_state {
+	struct osmo_fd conn_bfd;	/* fd for connection to lcr */
+	struct osmo_timer_list timer;	/* socket connect retry timer */
+	struct llist_head upqueue;	/* queue for sending messages */
+} *pcu_sock_state = NULL;
+
+static void pcu_sock_timeout(void *_priv);
+
+int pcu_sock_send(struct msgb *msg)
+{
+	struct pcu_sock_state *state = pcu_sock_state;
+	struct osmo_fd *conn_bfd;
+
+	if (!state) {
+		LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
+			"message\n");
+		return -EINVAL;
+	}
+	conn_bfd = &state->conn_bfd;
+	if (conn_bfd->fd <= 0) {
+		LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
+			"message\n");
+		return -EIO;
+	}
+	msgb_enqueue(&state->upqueue, msg);
+	conn_bfd->when |= BSC_FD_WRITE;
+
+	return 0;
+}
+
+static void pcu_sock_close(struct pcu_sock_state *state)
+{
+	struct osmo_fd *bfd = &state->conn_bfd;
+	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
+	struct gprs_rlcmac_tbf *tbf;
+	uint8_t trx, ts, tfi;
+
+	LOGP(DL1IF, LOGL_NOTICE, "PCU socket has LOST connection\n");
+
+	close(bfd->fd);
+	bfd->fd = -1;
+	osmo_fd_unregister(bfd);
+
+	/* flush the queue */
+	while (!llist_empty(&state->upqueue)) {
+		struct msgb *msg = msgb_dequeue(&state->upqueue);
+		msgb_free(msg);
+	}
+
+	/* disable all slots, kick all TBFs */
+	for (trx = 0; trx < 8; trx++) {
+		for (ts = 0; ts < 8; ts++) {
+			bts->trx[trx].pdch[ts].enable = 0;
+			for (tfi = 0; tfi < 32; tfi++) {
+				tbf = bts->trx[trx].pdch[ts].tbf[tfi];
+				if (tbf)
+					tbf_free(tbf);
+			}
+		}
+	}
+
+	gprs_bssgp_destroy();
+
+	state->timer.cb = pcu_sock_timeout;
+	osmo_timer_schedule(&state->timer, 5, 0);
+}
+
+static int pcu_sock_read(struct osmo_fd *bfd)
+{
+	struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
+	struct gsm_pcu_if *pcu_prim;
+	struct msgb *msg;
+	int rc;
+
+	msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
+	if (!msg)
+		return -ENOMEM;
+
+	pcu_prim = (struct gsm_pcu_if *) msg->tail;
+
+	rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
+	if (rc == 0)
+		goto close;
+
+	if (rc < 0) {
+		if (errno == EAGAIN)
+			return 0;
+		goto close;
+	}
+
+	rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
+
+	/* as we always synchronously process the message in pcu_rx() and
+	 * its callbacks, we can free the message here. */
+	msgb_free(msg);
+
+	return rc;
+
+close:
+	msgb_free(msg);
+	pcu_sock_close(state);
+	return -1;
+}
+
+static int pcu_sock_write(struct osmo_fd *bfd)
+{
+	struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
+	int rc;
+
+	while (!llist_empty(&state->upqueue)) {
+		struct msgb *msg, *msg2;
+		struct gsm_pcu_if *pcu_prim;
+
+		/* peek at the beginning of the queue */
+		msg = llist_entry(state->upqueue.next, struct msgb, list);
+		pcu_prim = (struct gsm_pcu_if *)msg->data;
+
+		bfd->when &= ~BSC_FD_WRITE;
+
+		/* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
+		if (!msgb_length(msg)) {
+			LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
+				"bytes!\n", pcu_prim->msg_type);
+			goto dontsend;
+		}
+
+		/* try to send it over the socket */
+		rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
+		if (rc == 0)
+			goto close;
+		if (rc < 0) {
+			if (errno == EAGAIN) {
+				bfd->when |= BSC_FD_WRITE;
+				break;
+			}
+			goto close;
+		}
+
+dontsend:
+		/* _after_ we send it, we can deueue */
+		msg2 = msgb_dequeue(&state->upqueue);
+		assert(msg == msg2);
+		msgb_free(msg);
+	}
+	return 0;
+
+close:
+	pcu_sock_close(state);
+
+	return -1;
+}
+
+static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
+{
+	int rc = 0;
+
+	if (flags & BSC_FD_READ)
+		rc = pcu_sock_read(bfd);
+	if (rc < 0)
+		return rc;
+
+	if (flags & BSC_FD_WRITE)
+		rc = pcu_sock_write(bfd);
+
+	return rc;
+}
+
+int pcu_l1if_open(void)
+{
+	struct pcu_sock_state *state;
+	struct osmo_fd *bfd;
+	struct sockaddr_un local;
+	unsigned int namelen;
+	int rc;
+
+	state = pcu_sock_state;
+	if (!state) {
+		state = talloc_zero(NULL, struct pcu_sock_state);
+		if (!state)
+			return -ENOMEM;
+		INIT_LLIST_HEAD(&state->upqueue);
+	}
+
+	bfd = &state->conn_bfd;
+
+	bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+	if (bfd->fd < 0) {
+		LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n");
+		talloc_free(state);
+		return -1;
+	}
+
+	local.sun_family = AF_UNIX;
+	strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path));
+	local.sun_path[sizeof(local.sun_path) - 1] = '\0';
+
+	/* we use the same magic that X11 uses in Xtranssock.c for
+	 * calculating the proper length of the sockaddr */
+#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
+	local.sun_len = strlen(local.sun_path);
+#endif
+#if defined(BSD44SOCKETS) || defined(SUN_LEN)
+	namelen = SUN_LEN(&local);
+#else
+	namelen = strlen(local.sun_path) +
+		  offsetof(struct sockaddr_un, sun_path);
+#endif
+	rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
+	if (rc != 0) {
+		LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO "
+			"socket, delaying... '%s'\n", local.sun_path);
+		close(bfd->fd);
+		bfd->fd = -1;
+		state->timer.cb = pcu_sock_timeout;
+		osmo_timer_schedule(&state->timer, 5, 0);
+		return 0;
+	}
+
+	bfd->when = BSC_FD_READ;
+	bfd->cb = pcu_sock_cb;
+	bfd->data = state;
+
+	rc = osmo_fd_register(bfd);
+	if (rc < 0) {
+		LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
+		close(bfd->fd);
+		talloc_free(state);
+		return rc;
+	}
+
+	LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n");
+
+	pcu_sock_state = state;
+
+	return 0;
+}
+
+void pcu_l1if_close(void)
+{
+	struct pcu_sock_state *state = pcu_sock_state;
+	struct osmo_fd *bfd;
+
+	if (!state)
+		return;
+
+	if (osmo_timer_pending(&state->timer))
+		osmo_timer_del(&state->timer);
+
+	bfd = &state->conn_bfd;
+	if (bfd->fd > 0)
+		pcu_sock_close(state);
+	talloc_free(state);
+	pcu_sock_state = NULL;
+}
+
+static void pcu_sock_timeout(void *_priv)
+{
+	pcu_l1if_open();
+}