Skip to content

Commit

Permalink
Merge pull request #253 from aligungr/dev
Browse files Browse the repository at this point in the history
v3.1.0
  • Loading branch information
aligungr committed Feb 15, 2021
2 parents ed54433 + 245a233 commit b797eb7
Show file tree
Hide file tree
Showing 89 changed files with 3,779 additions and 2,567 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ add_library(devbnd SHARED src/binder.cpp)
target_compile_options(devbnd PRIVATE -D_GNU_SOURCE -Wall -Wextra)
target_link_options(devbnd PRIVATE -nostartfiles)
target_link_libraries(devbnd dl)

#################### CLI EXECUTABLE ####################
add_executable(nr-cli src/cli.cpp)
target_link_libraries(nr-cli pthread)
target_compile_options(nr-cli PRIVATE -Wall -Wextra -pedantic)

target_link_libraries(nr-cli app)
target_link_libraries(nr-cli udp)
target_link_libraries(nr-cli utils)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<a href="https://github.com/aligungr/UERANSIM"><img src="/.github/logo.png" width="75" title="UERANSIM"></a>
</p>
<p align="center">
<img src="https://img.shields.io/badge/UERANSIM-v3.0.3-blue" />
<img src="https://img.shields.io/badge/UERANSIM-v3.1.0-blue" />
<img src="https://img.shields.io/badge/3GPP-R15-orange" />
<img src="https://img.shields.io/badge/License-GPL--3.0-green"/>
</p>
Expand Down
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ build: FORCE

cp cmake-build-release/nr-gnb build/
cp cmake-build-release/nr-ue build/
cp cmake-build-release/nr-cli build/
cp cmake-build-release/libdevbnd.so build/
cp tools/nr-binder build/

Expand Down
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ add_library(app ${HDR_FILES} ${SRC_FILES})
target_compile_options(app PRIVATE -Wall -Wextra -pedantic -Wno-unused-parameter)

target_link_libraries(app utils)
target_link_libraries(app udp)
56 changes: 56 additions & 0 deletions src/app/base_app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//

#include "base_app.hpp"

#include <atomic>
#include <csignal>
#include <cstdio>
#include <exception>
#include <iostream>
#include <utils/constants.hpp>
#include <vector>

static std::atomic_int g_instanceCount{};
static std::vector<void (*)()> g_runAtExit{};
static std::vector<std::string> g_deleteAtExit{};

extern "C" void BaseSignalHandler(int num)
{
for (auto &fun : g_runAtExit)
fun();
for (auto &file : g_deleteAtExit)
std::remove(file.c_str());

if (num == SIGTERM || num == SIGINT)
exit(0);
}

namespace app
{

void Initialize()
{
if (g_instanceCount++ != 0)
std::terminate();

std::signal(SIGTERM, BaseSignalHandler);
std::signal(SIGINT, BaseSignalHandler);
}

void RunAtExit(void (*fun)())
{
g_runAtExit.push_back(fun);
}

void DeleteAtExit(const std::string &file)
{
g_deleteAtExit.push_back(file);
}

} // namespace app
24 changes: 24 additions & 0 deletions src/app/base_app.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//

#pragma once

#include <string>
#include <unordered_map>
#include <vector>

namespace app
{

void Initialize();

void RunAtExit(void (*fun)());

void DeleteAtExit(const std::string &file);

} // namespace app
69 changes: 69 additions & 0 deletions src/app/cli_base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//

#include "cli_base.hpp"
#include <utils/octet_string.hpp>
#include <utils/octet_view.hpp>

#define CMD_BUFFER_SIZE 8192
#define CMD_RCV_TIMEOUT 2500
#define CMD_MIN_LENGTH (3 + 4 + 4 + 1)

namespace app
{

InetAddress CliServer::assignedAddress() const
{
return m_socket.getAddress();
}

CliMessage CliServer::receiveMessage()
{
uint8_t buffer[CMD_BUFFER_SIZE] = {0};
InetAddress address;

int size = m_socket.receive(buffer, CMD_BUFFER_SIZE, CMD_RCV_TIMEOUT, address);
if (size < CMD_MIN_LENGTH || size >= CMD_BUFFER_SIZE)
return {};

OctetView v{buffer, static_cast<size_t>(size)};
if (v.readI() != cons::Major)
return {};
if (v.readI() != cons::Minor)
return {};
if (v.readI() != cons::Patch)
return {};

CliMessage res{};
res.type = static_cast<CliMessage::Type>(v.readI());
int nodeNameLength = v.read4I();
res.nodeName = v.readUtf8String(nodeNameLength);
int valueLength = v.read4I();
res.value = v.readUtf8String(valueLength);
res.clientAddr = address;
return res;
}

void CliServer::sendMessage(const CliMessage &msg)
{
OctetString stream{};
stream.appendOctet(cons::Major);
stream.appendOctet(cons::Minor);
stream.appendOctet(cons::Patch);
stream.appendOctet(static_cast<int>(msg.type));
stream.appendOctet4(static_cast<size_t>(msg.nodeName.size()));
for (char c : msg.nodeName)
stream.appendOctet(static_cast<uint8_t>(c));
stream.appendOctet4(static_cast<size_t>(msg.value.size()));
for (char c : msg.value)
stream.appendOctet(static_cast<uint8_t>(c));

m_socket.send(msg.clientAddr, stream.data(), static_cast<size_t>(stream.length()));
}

} // namespace app
143 changes: 143 additions & 0 deletions src/app/cli_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//

#pragma once

#include <string>
#include <udp/server.hpp>
#include <utility>
#include <utils/constants.hpp>
#include <utils/network.hpp>
#include <utils/nts.hpp>
#include <vector>

namespace app
{

struct CliMessage
{
enum class Type
{
EMPTY = 0,
ECHO,
ERROR,
RESULT,
COMMAND
} type{};

std::string nodeName{};
std::string value{};
InetAddress clientAddr{};

static CliMessage Error(InetAddress addr, std::string msg, std::string node = "")
{
CliMessage m{};
m.type = Type::ERROR;
m.value = std::move(msg);
m.nodeName = std::move(node);
m.clientAddr = addr;
return m;
}

static CliMessage Result(InetAddress addr, std::string msg, std::string node = "")
{
CliMessage m{};
m.type = Type::RESULT;
m.value = std::move(msg);
m.nodeName = std::move(node);
m.clientAddr = addr;
return m;
}

static CliMessage Echo(InetAddress addr, std::string msg)
{
CliMessage m{};
m.type = Type::ECHO;
m.value = std::move(msg);
m.nodeName = "";
m.clientAddr = addr;
return m;
}

static CliMessage Command(InetAddress addr, std::string msg, std::string node = "")
{
CliMessage m{};
m.type = Type::COMMAND;
m.value = std::move(msg);
m.nodeName = std::move(node);
m.clientAddr = addr;
return m;
}
};

class CliServer
{
private:
Socket m_socket;

public:
explicit CliServer() : m_socket{Socket::CreateAndBindUdp({cons::CMD_SERVER_IP, 0})}
{
}

~CliServer()
{
m_socket.close();
}

[[nodiscard]] InetAddress assignedAddress() const;

CliMessage receiveMessage();
void sendMessage(const CliMessage &msg);
};

struct NwCliSendResponse : NtsMessage
{
InetAddress address{};
std::string output{};
bool isError{};

NwCliSendResponse(const InetAddress &address, std::string output, bool isError)
: NtsMessage(NtsMessageType::CLI_SEND_RESPONSE), address(address), output(std::move(output)), isError(isError)
{
}
};

class CliResponseTask : public NtsTask
{
private:
app::CliServer *cliServer;

public:
explicit CliResponseTask(CliServer *cliServer) : cliServer(cliServer)
{
}

protected:
void onStart() override
{
}
void onLoop() override
{
auto *msg = take();
if (msg == nullptr)
return;
if (msg->msgType == NtsMessageType::CLI_SEND_RESPONSE)
{
auto *w = dynamic_cast<NwCliSendResponse *>(msg);
cliServer->sendMessage(w->isError ? CliMessage::Error(w->address, w->output)
: CliMessage::Result(w->address, w->output));
}
delete msg;
}
void onQuit() override
{
}
};

} // namespace app
Loading

0 comments on commit b797eb7

Please sign in to comment.