Gathering RF info to a Data Base

4 minute read

Published:

Abstract: In order to document the experience of gathering RF information from a real dataset comming from a real 4G (pre-5G) network, I am sharing some key steps used on this experiment, most of them related to Data Base Setup.

Data Base Setup

Creating the DB platform

The main reference is coming from this ArchLinux documentation{}

  1. sudo pacman -S mariadb
    
  2. sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
    
  3. sudo systemctl enable mariadb.service
    
  4. sudo systemctl start mariadb.service
    

Creating the DB table

  1. sudo mysql -u root -p
    

    Note: to simplify, I have used a blank password for the db root user, it is the default one, however, you can set up another one.

  2. Create the database RFINFO
    create database RFINFO;
    
  3. Provide grant access to user lut on RFINFO database
    grant all privileges on RFINFO.* TO 'lut'@'%';
    grant all privileges on RFINFO.* TO 'lut'@'%' identified by 'lut';
    flush privileges;
    
  4. Create the table and column label for the database
    CREATE TABLE information( id INT NOT NULL AUTO_INCREMENT, device_id VARCHAR(100) NOT NULL, RSRP TINYINT SIGNED, RSRQ TINYINT SIGNED, RSSI TINYINT SIGNED, SINR TINYINT SIGNED, ts_remote TIMESTAMP, ts_local  TIMESTAMP, PRIMARY KEY (id) );
    
  5. To show the content of the database. In some cases it is necessarily to indicate the name of the database to check with the command USE RFINFO;
    show tables;
    SELECT * FROM information;
    
  6. Only in case the user is interested to delete data saved in the database, run the following command
    DELETE from information 
    

Python Script that save info on Data Base

  1. The script is triggered everytime that any message is received in the socket.
import socket                                                                                                                                       
from datetime import datetime                                                                                                                                                                  
import sys                                                                                                                              
import string                                                                                                                                       
import mysql.connector                                                                                                                                                                  

portalLocal = int(sys.argv[1])

s = socket.socket()
#port = 12345
s.bind(('',portalLocal))
s.listen(5)
c, addr = s.accept()


mydb = mysql.connector.connect(
        host= "100.100.100.10",
        user= "lut",
        password = "lut",
        database= "RFINFO"
        )

mycursor = mydb.cursor()

sql = "INSERT INTO information VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"

while True:
    try:
        rcvData = c.recv(1024).decode()
    except socket.error:
        pass
    else:
        print('ok')

    if(rcvData == "Bye" or rcvData == "bye"):
        c.close()
        break

    #print("info: ", rcvData)
    rcvData = str(rcvData)
    tmp_list = rcvData.strip().split(' ')
    device_id = tmp_list[0]
    RSRP = int(tmp_list[1])
    RSRQ = int(tmp_list[2])
    RSSI = int(tmp_list[3])
    SINR = int(tmp_list[4])

    pre_ts = tmp_list[6]
    tmp_list_ts = pre_ts.strip().split('|')
    ts_remote = tmp_list_ts[1]+'-'+tmp_list_ts[2]+'-'+tmp_list_ts[3]+' '+tmp_list_ts[4]+':'+tmp_list_ts[5]+':'+tmp_list_ts[6]


    val = (None, device_id, RSRP, RSRQ, RSSI, SINR, ts_remote, None)

    mycursor.execute(sql, val)
    mydb.commit()
    print("info received from id: %s"%(device_id))    
    #print(device_id)
    #print(RSRP)
    #print(RSRQ)
    #print(RSSI)
    #print(SINR)
    #print(ts_remote)

    #if(rcvData == "Bye" or rcvData == "bye"):
    #    c.close()
    #    break
  1. This script is located in the RF device that gather RF metrics and transmit it to the previous Python script.
#!/usr/bin/env python3
#
# Copyright (c) 2015, Daynix Computing LTD (www.daynix.com)
# Edited by Dick Carrillo 2020
# All rights reserved.
#
#
# For documentation please refer to README.md available at https://github.com/daynix/NetMeter
#
# This code is licensed under standard 3-clause BSD license.
# See file LICENSE supplied with this package for the full license text.

#import numpy as np
import sys
import signal
from datetime import datetime, timedelta
from time import sleep
from subprocess import Popen, PIPE
from os import makedirs
from os.path import isdir, isfile, join
from ntpath import dirname, basename
import os
import socket
import time


ipLocal = str(sys.argv[1])
portaLocal = int(sys.argv[2])
idDevice = str(sys.argv[3])
#SOCKET FEATURE:
s = socket.socket()
s.connect((ipLocal,portaLocal))

def get_rf_info():
 # This Hard solution run a shell script that already has access to the embbedded system
 # It save the output in a file rf_raw_info.txt
 os.system('./script.sh | grep = > rf_raw_info.txt')


def reading_file(outputfile):
 # we open a file and create the vector information
 info_data = ''
 counter = 0
 info_data = idDevice
 with open(outputfile, encoding='utf-8', errors='ignore') as inputfile:
    for line in inputfile:
        counter = counter + 1
        if counter < 5:
             tmp_lst = line.strip().split('=')
             data = (tmp_lst[1]);
             #info_data.append([data])
        elif counter == 5:
             print('Do nothing ...')
             data = ''
        else:
             tmp_lst = line.strip().split('=')
             date1 = (tmp_lst[1])
             #date2 = tmp_lst[2]
             #date3 = tmp_lst[3]
             #date4 = tmp_lst[4]
             #date5 = tmp_lst[5]
             #date6 = tmp_lst[6]
             #date.append([date1, date2, date3, date4, date5, date6])
             data = date1

        #info_data.append([ data ])
        info_data = info_data + ' ' +  data


 if not info_data:
        raise ValueError('Nothing reached the server.')

 #iinfo_data = np.array(info_data)

 return info_data

def transmitting(info):
    s = socket.socket()
    s.connect(('100.100.100.10',12345))
    print('transmitting data ..')
    s.send(info.encode());
    info = 'bye'
    s.send(info.encode());
    s.close()

def keyboardInterruptHandler(signal, frame):
    print("KeyboardInterrupt (ID: {}) has been caught. Cleaning up...".format(signal))
    info = 'Bye'
    s.send(info.encode())
    s.close()
    print('INTERRUPTION is ON')
    exit(0)

signal.signal(signal.SIGINT, keyboardInterruptHandler)

#MAIN LOOP 
flag = 0
while True:
            get_rf_info()
            outputinfo = reading_file('rf_raw_info.txt')
            outf = str(outputinfo)
            print(outf)
            s.send(outf.encode())
            with  open('coringa.txt', encoding='utf-8', errors='ignore') as inputfile:
                for lineinfo in inputfile:
                    if lineinfo[0] == 'B':
                        flag = 1
            if flag > 0: #TO CHECK SIGNAL TO STOP TRANSMITTION
                info = 'Bye'
                s.send(info.encode())
                s.close()
                break
s.close()