USE OF CALLBACK ON NS3 LENA CTTC

7 views
Skip to first unread message

Joao Victor Osorio

unread,
Nov 5, 2025, 12:11:24 PM (20 hours ago) Nov 5
to ns-developers
Hi everyone, I'm working with NS3 software for my final college project, however I'm having trouble implementing callbacks to obtain parameters such as received power and signal-to-noise ratio. Could you help me? Initially, I'm using a simplified scenario to learn how to use the software. I'll forward the output with the error I'm getting, as well as the code I'm trying to implement.
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/buildings-module.h"
#include "ns3/nr-module.h"

#include <iostream>
#include <memory>
#include <vector>
#include <functional>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("TesteCallbackParede");

// Callback que imprime RSRP e SINR a cada atualização
void PrintRsrpSinr(std::string context, uint16_t cellId, uint16_t rnti, double rsrp, double sinr)
{
    std::cout << Simulator::Now().GetSeconds()
              << "s | CellId: " << cellId
              << " | RNTI: " << rnti
              << " | RSRP: " << rsrp << " dBm"
              << " | SINR: " << sinr << " dB"
              << std::endl;
}

int main(int argc, char *argv[])
{
    CommandLine cmd;
    cmd.Parse(argc, argv);

    // ======= CRIAÇÃO DOS NÓS =======
    NodeContainer gNbNodes, ueNodes;
    gNbNodes.Create(1);
    ueNodes.Create(1);

    // ======= POSIÇÕES =======
    Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
    positionAlloc->Add(Vector(0.0, 0.0, 1.5));   // gNodeB
    positionAlloc->Add(Vector(40.0, 0.0, 1.5));  // UE

    MobilityHelper mobility;
    mobility.SetPositionAllocator(positionAlloc);
    mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
    mobility.Install(gNbNodes);
    mobility.Install(ueNodes);

    // ======= PAREDE =======
    Ptr<Building> wall = CreateObject<Building>();
    wall->SetBoundaries(Box(20.0, 25.0, -10.0, 10.0, 0.0, 3.0));
    wall->SetBuildingType(Building::Residential);
    wall->SetExtWallsType(Building::ConcreteWithWindows);
    wall->SetNFloors(1);

    BuildingsHelper::Install(gNbNodes);
    BuildingsHelper::Install(ueNodes);

    // ======= CONFIGURAÇÃO DO NR-LENA =======
    Ptr<NrHelper> nrHelper = CreateObject<NrHelper>();

    // Criação de Bandwidth Parts no formato correto (com unique_ptr)
    std::vector<std::unique_ptr<BandwidthPartInfo>> gnbBwps;
    gnbBwps.emplace_back(std::make_unique<BandwidthPartInfo>());

    std::vector<std::unique_ptr<BandwidthPartInfo>> ueBwps;
    ueBwps.emplace_back(std::make_unique<BandwidthPartInfo>());

    // Referências exigidas pelo NrHelper
    std::vector<std::reference_wrapper<std::unique_ptr<BandwidthPartInfo>>> gnbBwpRefs;
    for (auto &bwp : gnbBwps)
    {
        gnbBwpRefs.push_back(std::ref(bwp));
    }

    std::vector<std::reference_wrapper<std::unique_ptr<BandwidthPartInfo>>> ueBwpRefs;
    for (auto &bwp : ueBwps)
    {
        ueBwpRefs.push_back(std::ref(bwp));
    }

    // Instalação dos dispositivos NR (gNB e UE)
    NetDeviceContainer gNbDevs = nrHelper->InstallGnbDevice(gNbNodes, gnbBwpRefs, 0);
    NetDeviceContainer ueDevs  = nrHelper->InstallUeDevice(ueNodes, ueBwpRefs, 0);

    // Conexão do UE ao gNB mais próximo
    nrHelper->AttachToClosestEnb(ueDevs, gNbDevs);

    // ======= POTÊNCIA DE TRANSMISSÃO =======
    Config::Set("/NodeList/0/DeviceList/0/Phy/TxPower", DoubleValue(30.0)); // gNB
    Config::Set("/NodeList/1/DeviceList/0/Phy/TxPower", DoubleValue(23.0)); // UE

    // ======= CALLBACK =======
    Config::ConnectWithoutContext(
        "/NodeList/*/DeviceList/*/nrUePhy/ReportCurrentCellRsrpSinr",
        MakeCallback(&PrintRsrpSinr));

    // ======= INFORMAÇÕES INICIAIS =======
    Vector posEnb = gNbNodes.Get(0)->GetObject<MobilityModel>()->GetPosition();
    Vector posUe  = ueNodes.Get(0)->GetObject<MobilityModel>()->GetPosition();
    double dist   = CalculateDistance(posEnb, posUe);

    std::cout << "\n=== SIMULAÇÃO NR-LENA CALLBACK ===\n";
    std::cout << "gNodeB: " << posEnb.x << ", " << posEnb.y << ", " << posEnb.z << std::endl;
    std::cout << "UE: " << posUe.x << ", " << posUe.y << ", " << posUe.z << std::endl;
    std::cout << "Distância: " << dist << " m\n";
    std::cout << "Parede entre X=20 e X=25\n\n";

    // ======= EXECUÇÃO =======
    Simulator::Stop(Seconds(1.0));
    Simulator::Run();
    Simulator::Destroy();

    std::cout << "\n=== FIM DA SIMULAÇÃO ===\n";
    return 0;
}
Sem título.png

Tom Henderson

unread,
Nov 5, 2025, 12:17:18 PM (20 hours ago) Nov 5
to ns-dev...@googlegroups.com
Hi Joao, this list is for development discussions, and not debugging, so
please post any follow-up messages instead to the
5g-len...@googlegroups.com forum.

In looking briefly at the problem here, I don't think it is
callback-related but instead I think there is some missing object
(possibly a SpectrumChannel) that is causing a segmentation fault during
scenario construction. In general, you can usually trace these back to
the problem if you install and use gdb:

./ns3 run --gdb <name_of_program>
(gdb) run

See here:
https://www.nsnam.org/wiki/HOWTO_use_gdb_to_debug_program_errors#running_gdb_with_ns3_script

If you want further help from the 5g-lena-users forum, please specify
the nr version that you are using (I see that you are using ns-3.36,
which is an older version of the mainline).

- Tom
> --
> You received this message because you are subscribed to the Google
> Groups "ns-developers" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ns-developer...@googlegroups.com <mailto:ns-
> developers+...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/ns-
> developers/906435ae-cc5b-43d9-9c1b-8897e4db496dn%40googlegroups.com
> <https://groups.google.com/d/msgid/ns-developers/906435ae-
> cc5b-43d9-9c1b-8897e4db496dn%40googlegroups.com?
> utm_medium=email&utm_source=footer>.


Reply all
Reply to author
Forward
0 new messages