1 // Copyright 2019 AT&T Intellectual Property
2 // Copyright 2019 Nokia
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
19 // TODO: High-level file comment.
23 #include <3rdparty/oranE2/RANfunctions-List.h>
24 #include "sctpThread.h"
25 #include "BuildRunName.h"
27 #include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
29 #include "pugixml/src/pugixml.hpp"
32 //using namespace std::placeholders;
33 using namespace boost::filesystem;
40 // need to expose without the include of gcov
41 extern "C" void __gcov_flush(void);
43 static void catch_function(int signal) {
49 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
51 boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
52 double cpuClock = 0.0;
53 bool jsonTrace = true;
57 mdclog_attr_init(&attr);
58 mdclog_attr_set_ident(attr, "E2Terminator");
60 mdclog_attr_destroy(attr);
62 auto start_time = std::chrono::high_resolution_clock::now();
63 typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
66 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
69 double approx_CPU_MHz(unsigned sleeptime) {
70 using namespace std::chrono_literals;
72 uint64_t cycles_start = rdtscp(aux);
73 double time_start = age();
74 std::this_thread::sleep_for(sleeptime * 1ms);
75 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
76 double elapsed_time = age() - time_start;
77 return elapsed_cycles / elapsed_time;
80 //std::atomic<int64_t> rmrCounter{0};
81 std::atomic<int64_t> num_of_messages{0};
82 std::atomic<int64_t> num_of_XAPP_messages{0};
83 static long transactionCounter = 0;
85 int buildListeningPort(sctp_params_t &sctpParams) {
86 sctpParams.listenFD = socket (AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
87 struct sockaddr_in6 servaddr {};
88 servaddr.sin6_family = AF_INET6;
89 servaddr.sin6_addr = in6addr_any;
90 servaddr.sin6_port = htons(sctpParams.sctpPort);
91 if (bind(sctpParams.listenFD, (SA *)&servaddr, sizeof(servaddr)) < 0 ) {
92 mdclog_write(MDCLOG_ERR, "Error binding. %s\n", strerror(errno));
95 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
96 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
99 if (mdclog_level_get() >= MDCLOG_DEBUG) {
100 struct sockaddr_in6 cliaddr {};
101 socklen_t len = sizeof(cliaddr);
102 getsockname(sctpParams.listenFD, (SA *)&cliaddr, &len);
104 inet_ntop(AF_INET6, &cliaddr.sin6_addr, buff, sizeof(buff));
105 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(cliaddr.sin6_port));
108 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
109 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
112 struct epoll_event event {};
113 event.events = EPOLLIN | EPOLLET;
114 event.data.fd = sctpParams.listenFD;
116 // add listening port to epoll
117 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
118 printf("Failed to add descriptor to epoll\n");
119 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
126 int buildConfiguration(sctp_params_t &sctpParams) {
127 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
129 const int size = 2048;
130 auto fileSize = file_size(p);
131 if (fileSize > size) {
132 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
136 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
141 if (conf.openConfigFile(p.string()) == -1) {
142 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
143 p.string().c_str(), strerror(errno));
146 int rmrPort = conf.getIntValue("nano");
148 mdclog_write(MDCLOG_ERR, "illigal RMR port ");
151 sctpParams.rmrPort = (uint16_t)rmrPort;
152 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
154 auto tmpStr = conf.getStringValue("loglevel");
155 if (tmpStr.length() == 0) {
156 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
159 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
161 if ((tmpStr.compare("debug")) == 0) {
162 sctpParams.logLevel = MDCLOG_DEBUG;
163 } else if ((tmpStr.compare("info")) == 0) {
164 sctpParams.logLevel = MDCLOG_INFO;
165 } else if ((tmpStr.compare("warning")) == 0) {
166 sctpParams.logLevel = MDCLOG_WARN;
167 } else if ((tmpStr.compare("error")) == 0) {
168 sctpParams.logLevel = MDCLOG_ERR;
170 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
171 sctpParams.logLevel = MDCLOG_INFO;
173 mdclog_level_set(sctpParams.logLevel);
175 tmpStr = conf.getStringValue("volume");
176 if (tmpStr.length() == 0) {
177 mdclog_write(MDCLOG_ERR, "illigal volume.");
181 char tmpLogFilespec[VOLUME_URL_SIZE];
182 tmpLogFilespec[0] = 0;
183 sctpParams.volume[0] = 0;
184 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
185 // copy the name to temp file as well
186 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
189 // define the file name in the tmp directory under the volume
190 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
192 sctpParams.myIP = conf.getStringValue("local-ip");
193 if (sctpParams.myIP.length() == 0) {
194 mdclog_write(MDCLOG_ERR, "illigal local-ip.");
198 int sctpPort = conf.getIntValue("sctp-port");
199 if (sctpPort == -1) {
200 mdclog_write(MDCLOG_ERR, "illigal SCTP port ");
203 sctpParams.sctpPort = (uint16_t)sctpPort;
205 sctpParams.fqdn = conf.getStringValue("external-fqdn");
206 if (sctpParams.fqdn.length() == 0) {
207 mdclog_write(MDCLOG_ERR, "illigal external-fqdn");
211 std::string pod = conf.getStringValue("pod_name");
212 if (pod.length() == 0) {
213 mdclog_write(MDCLOG_ERR, "illigal pod_name in config file");
216 auto *podName = getenv(pod.c_str());
217 if (podName == nullptr) {
218 mdclog_write(MDCLOG_ERR, "illigal pod_name or environment varible not exists : %s", pod.c_str());
222 sctpParams.podName.assign(podName);
223 if (sctpParams.podName.length() == 0) {
224 mdclog_write(MDCLOG_ERR, "illigal pod_name");
229 tmpStr = conf.getStringValue("trace");
230 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
231 if ((tmpStr.compare("start")) == 0) {
232 mdclog_write(MDCLOG_INFO, "Trace set to: start");
233 sctpParams.trace = true;
234 } else if ((tmpStr.compare("stop")) == 0) {
235 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
236 sctpParams.trace = false;
238 jsonTrace = sctpParams.trace;
240 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
242 "\"pod_name\": \"%s\"}",
243 (const char *)sctpParams.myIP.c_str(),
245 sctpParams.fqdn.c_str(),
246 sctpParams.podName.c_str());
248 if (mdclog_level_get() >= MDCLOG_INFO) {
249 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
250 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
251 mdclog_mdc_add("volume", sctpParams.volume);
252 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
253 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
254 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
256 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
260 // Files written to the current working directory
261 boostLogger = logging::add_file_log(
262 keywords::file_name = tmpLogFilespec, // to temp directory
263 keywords::rotation_size = 10 * 1024 * 1024,
264 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
265 keywords::format = "%Message%"
266 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
269 // Setup a destination folder for collecting rotated (closed) files --since the same volumn can use rename()
270 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
271 keywords::target = sctpParams.volume
274 // Upon restart, scan the directory for files matching the file_name pattern
275 boostLogger->locked_backend()->scan_for_files();
277 // Enable auto-flushing after each tmpStr record written
278 if (mdclog_level_get() >= MDCLOG_DEBUG) {
279 boostLogger->locked_backend()->auto_flush(true);
287 int main(const int argc, char **argv) {
288 sctp_params_t sctpParams;
291 std::random_device device{};
292 std::mt19937 generator(device());
293 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
294 transactionCounter = distribution(generator);
298 // uint32_t aux1 = 0;
299 // st = rdtscp(aux1);
301 unsigned num_cpus = std::thread::hardware_concurrency();
303 mdclog_level_set(MDCLOG_INFO);
305 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
306 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
309 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
310 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
313 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
314 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
318 cpuClock = approx_CPU_MHz(100);
320 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
322 auto result = parse(argc, argv, sctpParams);
324 if (buildConfiguration(sctpParams) != 0) {
329 sctpParams.epoll_fd = epoll_create1(0);
330 if (sctpParams.epoll_fd == -1) {
331 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
335 getRmrContext(sctpParams);
336 if (sctpParams.rmrCtx == nullptr) {
337 close(sctpParams.epoll_fd);
341 if (buildInotify(sctpParams) == -1) {
342 close(sctpParams.rmrListenFd);
343 rmr_close(sctpParams.rmrCtx);
344 close(sctpParams.epoll_fd);
348 if (buildListeningPort(sctpParams) != 0) {
349 close(sctpParams.rmrListenFd);
350 rmr_close(sctpParams.rmrCtx);
351 close(sctpParams.epoll_fd);
355 sctpParams.sctpMap = new mapWrapper();
357 std::vector<std::thread> threads(num_cpus);
358 // std::vector<std::thread> threads;
361 for (unsigned int i = 0; i < num_cpus; i++) {
362 threads[i] = std::thread(listener, &sctpParams);
367 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
369 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
373 auto statFlag = false;
374 auto statThread = std::thread(statColectorThread, (void *)&statFlag);
376 //loop over term_init until first message from xApp
377 handleTermInit(sctpParams);
379 for (auto &t : threads) {
389 void handleTermInit(sctp_params_t &sctpParams) {
390 sendTermInit(sctpParams);
391 //send to e2 manager init of e2 term
396 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
397 if (xappMessages > 0) {
398 if (mdclog_level_get() >= MDCLOG_INFO) {
399 mdclog_write(MDCLOG_INFO, "Got a message from some appliction, stop sending E2_TERM_INIT");
405 if (count % 1000 == 0) {
406 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
407 sendTermInit(sctpParams);
412 void sendTermInit(sctp_params_t &sctpParams) {
413 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
416 msg->mtype = E2_TERM_INIT;
418 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
419 static unsigned char tx[32];
420 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
421 rmr_bytes2xact(msg, tx, txLen);
422 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
423 if (msg == nullptr) {
424 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.myIP.length());
425 } else if (msg->state == 0) {
427 if (mdclog_level_get() >= MDCLOG_INFO) {
428 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT succsesfuly sent ");
432 if (count % 100 == 0) {
433 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
448 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
449 cxxopts::Options options(argv[0], "e2 term help");
450 options.positional_help("[optional args]").show_positional_help();
451 options.allow_unrecognised_options().add_options()
452 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
453 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
454 ("h,help", "Print help");
456 auto result = options.parse(argc, argv);
458 if (result.count("help")) {
459 std::cout << options.help({""}) << std::endl;
468 * @return -1 failed 0 success
470 int buildInotify(sctp_params_t &sctpParams) {
471 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
472 if (sctpParams.inotifyFD == -1) {
473 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
474 close(sctpParams.rmrListenFd);
475 rmr_close(sctpParams.rmrCtx);
476 close(sctpParams.epoll_fd);
480 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
481 (const char *)sctpParams.configFilePath.c_str(),
482 (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
483 if (sctpParams.inotifyWD == -1) {
484 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
485 sctpParams.configFilePath.c_str(),
487 close(sctpParams.inotifyFD);
491 struct epoll_event event{};
492 event.events = (EPOLLIN);
493 event.data.fd = sctpParams.inotifyFD;
494 // add listening RMR FD to epoll
495 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
496 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
497 close(sctpParams.inotifyFD);
508 void listener(sctp_params_t *params) {
509 int num_of_SCTP_messages = 0;
510 auto totalTime = 0.0;
512 mdclog_level_set(params->logLevel);
514 std::thread::id this_id = std::this_thread::get_id();
516 streambuf *oldCout = cout.rdbuf();
517 ostringstream memCout;
519 cout.rdbuf(memCout.rdbuf());
521 //return to the normal cout
525 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
526 tid[memCout.str().length()] = 0;
527 mdclog_mdc_add("thread id", tid);
529 if (mdclog_level_get() >= MDCLOG_DEBUG) {
530 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
534 RmrMessagesBuffer_t rmrMessageBuffer{};
535 //create and init RMR
536 rmrMessageBuffer.rmrCtx = params->rmrCtx;
538 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
539 struct timespec end{0, 0};
540 struct timespec start{0, 0};
542 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
543 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
545 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
546 rmrMessageBuffer.ka_message_len = params->ka_message_length;
547 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
549 if (mdclog_level_get() >= MDCLOG_DEBUG) {
550 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
553 ReportingMessages_t message {};
555 // for (int i = 0; i < MAX_RMR_BUFF_ARRY; i++) {
556 // rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
557 // rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
560 message.statCollector = StatCollector::GetInstance();
563 if (mdclog_level_get() >= MDCLOG_DEBUG) {
564 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait");
566 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, -1);
567 if (numOfEvents < 0 && errno == EINTR) {
568 if (mdclog_level_get() >= MDCLOG_DEBUG) {
569 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
573 if (numOfEvents < 0) {
574 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
577 for (auto i = 0; i < numOfEvents; i++) {
578 if (mdclog_level_get() >= MDCLOG_DEBUG) {
579 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
581 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
582 start.tv_sec = message.message.time.tv_sec;
583 start.tv_nsec = message.message.time.tv_nsec;
586 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
587 handlepoll_error(events[i], message, rmrMessageBuffer, params);
588 } else if (events[i].events & EPOLLOUT) {
589 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
590 } else if (params->listenFD == events[i].data.fd) {
591 if (mdclog_level_get() >= MDCLOG_INFO) {
592 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
594 // new connection is requested from RAN start build connection
596 struct sockaddr in_addr {};
598 char hostBuff[NI_MAXHOST];
599 char portBuff[NI_MAXSERV];
601 in_len = sizeof(in_addr);
602 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
603 peerInfo->sctpParams = params;
604 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
605 if (peerInfo->fileDescriptor == -1) {
606 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
607 /* We have processed all incoming connections. */
610 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
614 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
615 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
616 close(peerInfo->fileDescriptor);
619 auto ans = getnameinfo(&in_addr, in_len,
620 peerInfo->hostName, NI_MAXHOST,
621 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
623 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
624 close(peerInfo->fileDescriptor);
627 if (mdclog_level_get() >= MDCLOG_DEBUG) {
628 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
630 peerInfo->isConnected = false;
631 peerInfo->gotSetup = false;
632 if (addToEpoll(params->epoll_fd,
635 params->sctpMap, nullptr,
641 } else if (params->rmrListenFd == events[i].data.fd) {
642 // got message from XAPP
643 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
644 num_of_messages.fetch_add(1, std::memory_order_release);
645 if (mdclog_level_get() >= MDCLOG_DEBUG) {
646 mdclog_write(MDCLOG_DEBUG, "new message from RMR");
648 if (receiveXappMessages(params->sctpMap,
650 message.message.time) != 0) {
651 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
653 } else if (params->inotifyFD == events[i].data.fd) {
654 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
655 handleConfigChange(params);
657 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
658 * We must read whatever data is available completely, as we are running
659 * in edge-triggered mode and won't get a notification again for the same data. */
660 num_of_messages.fetch_add(1, std::memory_order_release);
661 if (mdclog_level_get() >= MDCLOG_DEBUG) {
662 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
664 receiveDataFromSctp(&events[i],
666 num_of_SCTP_messages,
668 message.message.time);
671 clock_gettime(CLOCK_MONOTONIC, &end);
672 if (mdclog_level_get() >= MDCLOG_INFO) {
673 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
674 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
676 if (mdclog_level_get() >= MDCLOG_DEBUG) {
677 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
678 end.tv_sec - start.tv_sec,
679 end.tv_nsec - start.tv_nsec);
689 void handleConfigChange(sctp_params_t *sctpParams) {
690 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
691 const struct inotify_event *event;
694 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
695 auto endlessLoop = true;
696 while (endlessLoop) {
697 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
699 if (errno != EAGAIN) {
700 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
710 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
711 event = (const struct inotify_event *)ptr;
712 if (event->mask & (uint32_t)IN_ISDIR) {
716 // the directory name
717 if (sctpParams->inotifyWD == event->wd) {
721 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
726 // only the file we want
727 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
728 if (mdclog_level_get() >= MDCLOG_INFO) {
729 mdclog_write(MDCLOG_INFO, "Configuration file changed");
732 const int size = 2048;
733 auto fileSize = file_size(p);
734 if (fileSize > size) {
735 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
739 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
744 if (conf.openConfigFile(p.string()) == -1) {
745 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
746 p.string().c_str(), strerror(errno));
750 auto tmpStr = conf.getStringValue("loglevel");
751 if (tmpStr.length() == 0) {
752 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
755 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
757 if ((tmpStr.compare("debug")) == 0) {
758 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
759 sctpParams->logLevel = MDCLOG_DEBUG;
760 } else if ((tmpStr.compare("info")) == 0) {
761 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
762 sctpParams->logLevel = MDCLOG_INFO;
763 } else if ((tmpStr.compare("warning")) == 0) {
764 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
765 sctpParams->logLevel = MDCLOG_WARN;
766 } else if ((tmpStr.compare("error")) == 0) {
767 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
768 sctpParams->logLevel = MDCLOG_ERR;
770 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
771 sctpParams->logLevel = MDCLOG_INFO;
773 mdclog_level_set(sctpParams->logLevel);
776 tmpStr = conf.getStringValue("trace");
777 if (tmpStr.length() == 0) {
778 mdclog_write(MDCLOG_ERR, "illigal trace. Set trace to stop");
782 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
783 if ((tmpStr.compare("start")) == 0) {
784 mdclog_write(MDCLOG_INFO, "Trace set to: start");
785 sctpParams->trace = true;
786 } else if ((tmpStr.compare("stop")) == 0) {
787 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
788 sctpParams->trace = false;
790 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
791 sctpParams->trace = false;
793 jsonTrace = sctpParams->trace;
804 * @param rmrMessageBuffer
807 void handleEinprogressMessages(struct epoll_event &event,
808 ReportingMessages_t &message,
809 RmrMessagesBuffer_t &rmrMessageBuffer,
810 sctp_params_t *params) {
811 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
812 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
814 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
816 socklen_t retValLen = 0;
817 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
818 if (rc != 0 || retVal != 0) {
820 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
821 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
822 peerInfo->enodbName, strerror(errno));
823 } else if (retVal != 0) {
824 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
825 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
826 peerInfo->enodbName);
829 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
830 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
831 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
832 message.message.direction = 'N';
833 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
834 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
836 memset(peerInfo->asnData, 0, peerInfo->asnLength);
837 peerInfo->asnLength = 0;
842 peerInfo->isConnected = true;
844 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
845 peerInfo->mtype) != 0) {
846 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
850 message.message.asndata = (unsigned char *)peerInfo->asnData;
851 message.message.asnLength = peerInfo->asnLength;
852 message.message.messageType = peerInfo->mtype;
853 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
854 num_of_messages.fetch_add(1, std::memory_order_release);
855 if (mdclog_level_get() >= MDCLOG_DEBUG) {
856 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
857 message.message.enodbName);
859 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
860 if (mdclog_level_get() >= MDCLOG_DEBUG) {
861 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
866 memset(peerInfo->asnData, 0, peerInfo->asnLength);
867 peerInfo->asnLength = 0;
872 void handlepoll_error(struct epoll_event &event,
873 ReportingMessages_t &message,
874 RmrMessagesBuffer_t &rmrMessageBuffer,
875 sctp_params_t *params) {
876 if (event.data.fd != params->rmrListenFd) {
877 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
878 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
879 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
881 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
882 "%s|Failed SCTP Connection",
883 peerInfo->enodbName);
884 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
885 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
887 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
888 message.message.direction = 'N';
889 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
890 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
893 close(peerInfo->fileDescriptor);
894 params->sctpMap->erase(peerInfo->enodbName);
895 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
897 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
905 int setSocketNoBlocking(int socket) {
906 auto flags = fcntl(socket, F_GETFL, 0);
909 mdclog_mdc_add("func", "fcntl");
910 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
915 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
916 if (fcntl(socket, F_SETFL, flags) == -1) {
917 mdclog_mdc_add("func", "fcntl");
918 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
931 void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
933 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
934 char searchBuff[2048]{};
936 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
937 m->erase(searchBuff);
939 m->erase(val->enodbName);
945 * @param fd file discriptor
946 * @param data the asn data to send
947 * @param len length of the data
948 * @param enodbName the enodbName as in the map for printing purpose
949 * @param m map host information
950 * @param mtype message number
951 * @return 0 success, anegative number on fail
953 int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
954 auto loglevel = mdclog_level_get();
955 int fd = peerInfo->fileDescriptor;
956 if (loglevel >= MDCLOG_DEBUG) {
957 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
958 message.message.enodbName, __FUNCTION__);
962 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
963 if (errno == EINTR) {
966 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
967 if (!peerInfo->isConnected) {
968 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
971 cleanHashEntry(peerInfo, m);
973 char key[MAX_ENODB_NAME_SIZE * 2];
974 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
975 message.message.messageType);
976 if (loglevel >= MDCLOG_DEBUG) {
977 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
979 auto tmp = m->find(key);
986 message.statCollector->incSentMessage(string(message.message.enodbName));
987 message.message.direction = 'D';
988 // send report.buffer of size
989 buildJsonMessage(message);
991 if (loglevel >= MDCLOG_DEBUG) {
992 mdclog_write(MDCLOG_DEBUG,
993 "SCTP message for CU %s sent from %s",
994 message.message.enodbName,
1004 * @param rmrMessageBuffer
1006 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1007 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *) (message.message.enodbName));
1009 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1010 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1012 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1013 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1014 message.message.enodbName, (unsigned long) message.message.asnLength);
1024 * @param numOfMessages
1025 * @param rmrMessageBuffer
1029 int receiveDataFromSctp(struct epoll_event *events,
1030 Sctp_Map_t *sctpMap,
1032 RmrMessagesBuffer_t &rmrMessageBuffer,
1033 struct timespec &ts) {
1034 /* We have data on the fd waiting to be read. Read and display it.
1035 * We must read whatever data is available completely, as we are running
1036 * in edge-triggered mode and won't get a notification again for the same data. */
1037 ReportingMessages_t message {};
1039 auto loglevel = mdclog_level_get();
1041 // get the identity of the interface
1042 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1044 message.statCollector = StatCollector::GetInstance();
1045 struct timespec start{0, 0};
1046 struct timespec decodestart{0, 0};
1047 struct timespec end{0, 0};
1049 E2AP_PDU_t *pdu = nullptr;
1053 if (loglevel >= MDCLOG_DEBUG) {
1054 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
1055 clock_gettime(CLOCK_MONOTONIC, &start);
1057 // read the buffer directly to rmr payload
1058 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1059 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1060 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1062 if (loglevel >= MDCLOG_DEBUG) {
1063 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
1064 message.peerInfo->fileDescriptor, message.message.asnLength);
1067 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
1068 message.statCollector->incRecvMessage(string(message.message.enodbName));
1069 message.message.direction = 'U';
1070 message.message.time.tv_nsec = ts.tv_nsec;
1071 message.message.time.tv_sec = ts.tv_sec;
1073 if (message.message.asnLength < 0) {
1074 if (errno == EINTR) {
1077 /* If errno == EAGAIN, that means we have read all
1078 data. So goReportingMessages_t back to the main loop. */
1079 if (errno != EAGAIN) {
1080 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1082 } else if (loglevel >= MDCLOG_DEBUG) {
1083 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
1086 } else if (message.message.asnLength == 0) {
1087 /* End of file. The remote has closed the connection. */
1088 if (loglevel >= MDCLOG_INFO) {
1089 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
1090 message.peerInfo->fileDescriptor);
1096 if (loglevel >= MDCLOG_DEBUG) {
1097 char printBuffer[4096]{};
1098 char *tmp = printBuffer;
1099 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
1100 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
1103 printBuffer[message.message.asnLength] = 0;
1104 clock_gettime(CLOCK_MONOTONIC, &end);
1105 mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1106 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1107 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1109 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1112 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1113 message.message.asndata, message.message.asnLength);
1114 if (rval.code != RC_OK) {
1115 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
1116 message.peerInfo->enodbName);
1117 //todo may need reset to pdu
1121 if (loglevel >= MDCLOG_DEBUG) {
1122 clock_gettime(CLOCK_MONOTONIC, &end);
1123 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1124 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
1127 FILE *stream = open_memstream(&printBuffer, &size);
1128 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1129 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1130 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1133 switch (pdu->present) {
1134 case E2AP_PDU_PR_initiatingMessage: {//initiating message
1135 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
1138 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
1139 asnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1142 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
1143 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1147 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1150 if (loglevel >= MDCLOG_DEBUG) {
1151 clock_gettime(CLOCK_MONOTONIC, &end);
1152 mdclog_write(MDCLOG_DEBUG,
1153 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
1154 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
1157 // remove the break for EAGAIN
1159 if (pdu != nullptr) {
1160 //TODO need to test ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); to get better performance
1161 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1162 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1165 //clock_gettime(CLOCK_MONOTONIC, &start);
1167 // in case of break to avoid memory leak
1168 // if (pdu != nullptr) {
1169 // //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1170 // ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1175 if (loglevel >= MDCLOG_INFO) {
1176 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
1178 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1179 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
1181 "%s|CU disconnected unexpectedly",
1182 message.peerInfo->enodbName);
1183 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1185 if (sendRequestToXapp(message,
1186 RIC_SCTP_CONNECTION_FAILURE,
1187 rmrMessageBuffer) != 0) {
1188 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1191 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
1192 close(message.peerInfo->fileDescriptor);
1193 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
1195 if (loglevel >= MDCLOG_DEBUG) {
1196 clock_gettime(CLOCK_MONOTONIC, &end);
1197 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1198 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1204 static void buildAndsendSetupRequest(ReportingMessages_t &message,
1205 RmrMessagesBuffer_t &rmrMessageBuffer,
1207 vector<string> repValues) {
1208 auto logLevel = mdclog_level_get();
1210 // now we can send the data to e2Mgr
1211 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1213 auto *rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size);
1214 // add addrees to message
1217 // unsigned char *buffer = &rmrMsg->payload[j];
1218 unsigned char buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1220 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1221 if (er.encoded == -1) {
1222 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1223 } else if (er.encoded > (ssize_t) buffer_size) {
1224 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1226 asn_DEF_E2AP_PDU.name, __func__, __LINE__);
1229 pugi::xml_document doc;
1230 pugi::xml_parse_result result = doc.load_string((const char *)buffer);
1232 unsigned int index = 0;
1233 for (auto tool : doc.child("E2AP-PDU")
1234 .child("initiatingMessage")
1236 .child("E2setupRequest")
1237 .child("protocolIEs")
1238 .children("E2setupRequestIEs")) {
1239 for (auto n : tool.child("value").child("RANfunctions-List").child(
1240 "ProtocolIE-SingleContainer").children()) {
1241 //ProtocolIE-SingleContainer
1242 //cout << "\t1 " << n.name() << endl;
1243 if (strcmp(n.name(), "value") == 0) {
1244 for (auto l : tool.child("value").children()) {
1245 //cout << "\t\t2 " << l.name() << endl;
1246 for (auto f : l.children()) {
1247 //cout << "\t\t\t3 " << f.name() << endl;
1248 for (auto g : f.child("value").children()) {
1249 //cout << "\t\t\t\t4 " << g.name() << endl;
1250 for (auto a : g.children()) {
1251 if (strcmp(a.name(), "ranFunctionDefinition") == 0) {
1252 if (repValues.size() > index) {
1253 a.remove_children();
1254 string val = repValues.at(index++);
1255 // here we get vector with counter
1256 a.append_child(pugi::node_pcdata).set_value(val.c_str());
1260 //cout << "\t\t\t\t\t5 " << a.name() << " " << a.child_value() << endl;
1269 // memstream strinBuf(buffer, RECEIVE_SCTP_BUFFER_SIZE * 2);
1271 // strinBuf.read(, RECEIVE_SCTP_BUFFER_SIZE * 2);
1273 streambuf *oldCout = cout.rdbuf();
1274 ostringstream memCout;
1276 cout.rdbuf(memCout.rdbuf());
1277 doc.save(std::cout);
1278 //return to the normal cout
1279 cout.rdbuf(oldCout);
1280 memcpy(buffer, memCout.str().c_str(), memCout.str().length());
1282 rmrMsg->len = snprintf((char *)rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1283 message.peerInfo->sctpParams->myIP.c_str(),
1284 message.peerInfo->sctpParams->rmrPort,
1286 if (logLevel >= MDCLOG_DEBUG) {
1287 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1290 message.message.messageType = rmrMsg->mtype = RIC_E2_SETUP_REQ;
1292 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1294 static unsigned char tx[32];
1295 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1296 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1298 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1299 if (rmrMsg == nullptr) {
1300 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1301 } else if (rmrMsg->state != 0) {
1302 char meid[RMR_MAX_MEID]{};
1303 if (rmrMsg->state == RMR_ERR_RETRY) {
1306 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1307 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1308 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1309 if (rmrMsg == nullptr) {
1310 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1311 } else if (rmrMsg->state != 0) {
1312 mdclog_write(MDCLOG_ERR,
1313 "RMR Retry failed %s sending request %d to Xapp from %s",
1314 translateRmrErrorMessages(rmrMsg->state).c_str(),
1316 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1319 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1320 translateRmrErrorMessages(rmrMsg->state).c_str(),
1322 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1325 message.peerInfo->gotSetup = true;
1326 buildJsonMessage(message);
1327 if (rmrMsg != nullptr) {
1328 rmr_free_msg(rmrMsg);
1337 * @param rmrMessageBuffer
1339 void asnInitiatingRequest(E2AP_PDU_t *pdu,
1340 Sctp_Map_t *sctpMap,
1341 ReportingMessages_t &message,
1342 RmrMessagesBuffer_t &rmrMessageBuffer) {
1343 auto logLevel = mdclog_level_get();
1344 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
1345 if (logLevel >= MDCLOG_DEBUG) {
1346 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
1348 switch (procedureCode) {
1349 case ProcedureCode_id_E2setup: {
1350 if (logLevel >= MDCLOG_DEBUG) {
1351 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
1354 // first get the message as XML buffer
1355 auto setup_xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1356 unsigned char setup_xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1357 //unsigned char *tmp_buff_cursor = setup_xml_buffer;
1359 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, setup_xml_buffer, setup_xml_buffer_size);
1360 if (er.encoded == -1) {
1361 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1362 } else if (er.encoded > (ssize_t) setup_xml_buffer_size) {
1363 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1364 (int)setup_xml_buffer_size,
1365 asn_DEF_E2AP_PDU.name, __func__, __LINE__);
1367 std::string xmlString(setup_xml_buffer_size, setup_xml_buffer_size + er.encoded);
1369 vector <string> runFunDEFXML_v;
1370 runFunDEFXML_v.clear();
1371 string runFuncStr = {};
1372 auto failed = false;
1373 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1374 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1375 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1376 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1377 // get the ran name for meid
1378 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1379 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1380 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1381 // no mesage will be sent
1384 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1385 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1388 // reformat RANFUNCTION Definition to XML
1389 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1390 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1391 for (auto j = 0; i < ie->value.choice.RANfunctions_List.list.count; i++) {
1392 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)ie->value.choice.RANfunctions_List.list.array[j];
1393 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1394 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1396 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1397 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1398 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1399 (void **)&ranFunDef,
1400 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1401 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1402 if (rval.code != RC_OK) {
1403 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1405 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1410 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1413 FILE *stream = open_memstream(&printBuffer, &size);
1414 asn_fprint(stream, &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition, ranFunDef);
1415 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU past : %s",
1416 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1419 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1420 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1422 er = asn_encode_to_buffer(nullptr,
1424 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1428 if (er.encoded == -1) {
1429 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1430 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1432 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1433 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1434 (int) xml_buffer_size,
1435 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1437 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1438 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU past : %s",
1439 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1442 //TODO replace the ranFunctionDefinition with the XML
1443 string runFuncs = (char *)(xml_buffer);
1444 runFunDEFXML_v.emplace_back(runFuncs);
1459 //build all parts and send the XML (need to copy the XML with the header to the rmrMessageBuffer payload
1460 //TODO replace with new function
1461 buildAndsendSetupRequest(message, rmrMessageBuffer, pdu, runFunDEFXML_v);
1464 case ProcedureCode_id_ErrorIndication: {
1465 if (logLevel >= MDCLOG_DEBUG) {
1466 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1468 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
1469 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
1473 case ProcedureCode_id_Reset: {
1474 if (logLevel >= MDCLOG_DEBUG) {
1475 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1477 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
1478 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
1482 case ProcedureCode_id_RICcontrol: {
1483 if (logLevel >= MDCLOG_DEBUG) {
1484 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1488 case ProcedureCode_id_RICindication: {
1489 if (logLevel >= MDCLOG_DEBUG) {
1490 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1492 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1493 auto messageSent = false;
1494 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1495 if (logLevel >= MDCLOG_DEBUG) {
1496 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1498 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1499 if (logLevel >= MDCLOG_DEBUG) {
1500 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1502 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1503 static unsigned char tx[32];
1504 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1505 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1506 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1507 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1508 (unsigned char *)message.message.enodbName,
1509 strlen(message.message.enodbName));
1510 rmrMessageBuffer.sendMessage->state = 0;
1511 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1512 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1513 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1514 rmrMessageBuffer.sendMessage->sub_id,
1515 rmrMessageBuffer.sendMessage->mtype);
1517 sendRmrMessage(rmrMessageBuffer, message);
1520 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1529 case ProcedureCode_id_RICserviceQuery: {
1530 if (logLevel >= MDCLOG_DEBUG) {
1531 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1535 case ProcedureCode_id_RICserviceUpdate: {
1536 if (logLevel >= MDCLOG_DEBUG) {
1537 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1539 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
1540 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1544 case ProcedureCode_id_RICsubscription: {
1545 if (logLevel >= MDCLOG_DEBUG) {
1546 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1550 case ProcedureCode_id_RICsubscriptionDelete: {
1551 if (logLevel >= MDCLOG_DEBUG) {
1552 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1557 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1558 message.message.messageType = 0; // no RMR message type yet
1560 buildJsonMessage(message);
1571 * @param rmrMessageBuffer
1573 void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
1574 Sctp_Map_t *sctpMap,
1575 ReportingMessages_t &message,
1576 RmrMessagesBuffer_t &rmrMessageBuffer) {
1577 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
1578 auto logLevel = mdclog_level_get();
1579 if (logLevel >= MDCLOG_INFO) {
1580 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1582 switch (procedureCode) {
1583 case ProcedureCode_id_E2setup: {
1584 if (logLevel >= MDCLOG_DEBUG) {
1585 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
1589 case ProcedureCode_id_ErrorIndication: {
1590 if (logLevel >= MDCLOG_DEBUG) {
1591 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1593 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
1594 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
1598 case ProcedureCode_id_Reset: {
1599 if (logLevel >= MDCLOG_DEBUG) {
1600 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1602 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
1603 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
1607 case ProcedureCode_id_RICcontrol: {
1608 if (logLevel >= MDCLOG_DEBUG) {
1609 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1612 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1613 auto messageSent = false;
1614 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1615 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1616 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1618 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1619 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1620 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1622 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1623 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1624 rmrMessageBuffer.sendMessage->state = 0;
1625 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1626 static unsigned char tx[32];
1627 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1628 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1629 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1630 (unsigned char *)message.message.enodbName,
1631 strlen(message.message.enodbName));
1633 sendRmrMessage(rmrMessageBuffer, message);
1636 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1646 case ProcedureCode_id_RICindication: {
1647 if (logLevel >= MDCLOG_DEBUG) {
1648 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1650 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1651 auto messageSent = false;
1652 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1653 if (logLevel >= MDCLOG_DEBUG) {
1654 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1656 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1657 if (logLevel >= MDCLOG_DEBUG) {
1658 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1660 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1661 static unsigned char tx[32];
1662 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1663 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1664 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1665 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1666 (unsigned char *)message.message.enodbName,
1667 strlen(message.message.enodbName));
1668 rmrMessageBuffer.sendMessage->state = 0;
1669 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1670 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1671 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1672 rmrMessageBuffer.sendMessage->sub_id,
1673 rmrMessageBuffer.sendMessage->mtype);
1675 sendRmrMessage(rmrMessageBuffer, message);
1678 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1687 case ProcedureCode_id_RICserviceQuery: {
1688 if (logLevel >= MDCLOG_DEBUG) {
1689 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1693 case ProcedureCode_id_RICserviceUpdate: {
1694 if (logLevel >= MDCLOG_DEBUG) {
1695 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1697 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
1698 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1702 case ProcedureCode_id_RICsubscription: {
1703 if (logLevel >= MDCLOG_DEBUG) {
1704 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1706 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
1707 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1711 case ProcedureCode_id_RICsubscriptionDelete: {
1712 if (logLevel >= MDCLOG_DEBUG) {
1713 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1715 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
1716 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1721 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1722 message.message.messageType = 0; // no RMR message type yet
1723 buildJsonMessage(message);
1734 * @param rmrMessageBuffer
1736 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
1737 Sctp_Map_t *sctpMap,
1738 ReportingMessages_t &message,
1739 RmrMessagesBuffer_t &rmrMessageBuffer) {
1740 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
1741 auto logLevel = mdclog_level_get();
1742 if (logLevel >= MDCLOG_INFO) {
1743 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1745 switch (procedureCode) {
1746 case ProcedureCode_id_E2setup: {
1747 if (logLevel >= MDCLOG_DEBUG) {
1748 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
1752 case ProcedureCode_id_ErrorIndication: {
1753 if (logLevel >= MDCLOG_DEBUG) {
1754 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1756 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
1757 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
1761 case ProcedureCode_id_Reset: {
1762 if (logLevel >= MDCLOG_DEBUG) {
1763 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1765 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
1766 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
1770 case ProcedureCode_id_RICcontrol: {
1771 if (logLevel >= MDCLOG_DEBUG) {
1772 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1775 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1776 auto messageSent = false;
1777 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
1778 if (logLevel >= MDCLOG_DEBUG) {
1779 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1781 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1782 if (logLevel >= MDCLOG_DEBUG) {
1783 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1785 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1786 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1787 rmrMessageBuffer.sendMessage->state = 0;
1788 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1789 static unsigned char tx[32];
1790 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1791 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1792 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1793 strlen(message.message.enodbName));
1794 sendRmrMessage(rmrMessageBuffer, message);
1797 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1806 case ProcedureCode_id_RICindication: {
1807 if (logLevel >= MDCLOG_DEBUG) {
1808 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1810 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1811 auto messageSent = false;
1812 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1813 if (logLevel >= MDCLOG_DEBUG) {
1814 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1816 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1817 if (logLevel >= MDCLOG_DEBUG) {
1818 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1820 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1821 static unsigned char tx[32];
1822 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1823 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1824 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1825 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1826 (unsigned char *)message.message.enodbName,
1827 strlen(message.message.enodbName));
1828 rmrMessageBuffer.sendMessage->state = 0;
1829 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1830 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1831 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1832 rmrMessageBuffer.sendMessage->sub_id,
1833 rmrMessageBuffer.sendMessage->mtype);
1835 sendRmrMessage(rmrMessageBuffer, message);
1838 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1847 case ProcedureCode_id_RICserviceQuery: {
1848 if (logLevel >= MDCLOG_DEBUG) {
1849 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1853 case ProcedureCode_id_RICserviceUpdate: {
1854 if (logLevel >= MDCLOG_DEBUG) {
1855 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1857 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
1858 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1862 case ProcedureCode_id_RICsubscription: {
1863 if (logLevel >= MDCLOG_DEBUG) {
1864 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1866 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
1867 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1871 case ProcedureCode_id_RICsubscriptionDelete: {
1872 if (logLevel >= MDCLOG_DEBUG) {
1873 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1875 if (sendRequestToXapp(message, RIC_SUB_DEL_FAILURE, rmrMessageBuffer) != 0) {
1876 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1881 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1882 message.message.messageType = 0; // no RMR message type yet
1884 buildJsonMessage(message);
1895 * @param rmrMmessageBuffer
1898 int sendRequestToXapp(ReportingMessages_t &message,
1900 RmrMessagesBuffer_t &rmrMmessageBuffer) {
1901 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1902 (unsigned char *)message.message.enodbName,
1903 strlen(message.message.enodbName));
1904 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1905 rmrMmessageBuffer.sendMessage->state = 0;
1906 static unsigned char tx[32];
1907 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1908 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1910 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
1915 void getRmrContext(sctp_params_t &pSctpParams) {
1916 pSctpParams.rmrCtx = nullptr;
1917 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RMR_MAX_RCV_BYTES, RMRFL_NONE);
1918 if (pSctpParams.rmrCtx == nullptr) {
1919 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
1923 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
1924 // we need to find that routing table exist and we can run
1925 if (mdclog_level_get() >= MDCLOG_INFO) {
1926 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1931 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
1935 if (count % 60 == 0) {
1936 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
1939 if (mdclog_level_get() >= MDCLOG_INFO) {
1940 mdclog_write(MDCLOG_INFO, "RMR running");
1942 rmr_init_trace(pSctpParams.rmrCtx, 200);
1943 // get the RMR fd for the epoll
1944 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
1945 struct epoll_event event{};
1946 // add RMR fd to epoll
1947 event.events = (EPOLLIN);
1948 event.data.fd = pSctpParams.rmrListenFd;
1949 // add listening RMR FD to epoll
1950 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
1951 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
1952 close(pSctpParams.rmrListenFd);
1953 rmr_close(pSctpParams.rmrCtx);
1954 pSctpParams.rmrCtx = nullptr;
1958 int BuildPERSetupResponseMessaeFromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1961 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1962 mdclog_write(MDCLOG_DEBUG, "got xml setup response \n %s\n", rmrMessageBuffer.rcvMessage->payload);
1964 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1965 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
1966 if (rval.code != RC_OK) {
1967 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1969 message.message.enodbName);
1973 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1974 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
1975 rmrMessageBuffer.rcvMessage->payload, buff_size);
1976 if (er.encoded == -1) {
1977 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1979 } else if (er.encoded > (ssize_t)buff_size) {
1980 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1981 (int)rmrMessageBuffer.rcvMessage->len,
1982 asn_DEF_E2AP_PDU.name,
1987 rmrMessageBuffer.rcvMessage->len = er.encoded;
1994 * @param rmrMessageBuffer
1998 int receiveXappMessages(Sctp_Map_t *sctpMap,
1999 RmrMessagesBuffer_t &rmrMessageBuffer,
2000 struct timespec &ts) {
2001 if (rmrMessageBuffer.rcvMessage == nullptr) {
2003 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
2007 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2008 mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2010 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2011 if (rmrMessageBuffer.rcvMessage == nullptr) {
2012 mdclog_write(MDCLOG_ERR, "RMR Receving message with null pointer, Realloc rmr mesage buffer");
2013 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2016 ReportingMessages_t message;
2017 message.message.direction = 'D';
2018 message.message.time.tv_nsec = ts.tv_nsec;
2019 message.message.time.tv_sec = ts.tv_sec;
2021 // get message payload
2022 //auto msgData = msg->payload;
2023 if (rmrMessageBuffer.rcvMessage->state != 0) {
2024 mdclog_write(MDCLOG_ERR, "RMR Receving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
2027 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
2028 switch (rmrMessageBuffer.rcvMessage->mtype) {
2029 case RIC_E2_SETUP_RESP : {
2030 if (BuildPERSetupResponseMessaeFromXML(message, rmrMessageBuffer) != 0) {
2034 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2035 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2040 case RIC_E2_SETUP_FAILURE : {
2041 if (BuildPERSetupResponseMessaeFromXML(message, rmrMessageBuffer) != 0) {
2044 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2045 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2050 case RIC_ERROR_INDICATION: {
2051 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2052 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
2058 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2059 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
2064 case RIC_SUB_DEL_REQ: {
2065 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2066 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
2071 case RIC_CONTROL_REQ: {
2072 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2073 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
2078 case RIC_SERVICE_QUERY: {
2079 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2080 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
2085 case RIC_SERVICE_UPDATE_ACK: {
2086 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2087 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
2092 case RIC_SERVICE_UPDATE_FAILURE: {
2093 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2094 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
2099 case RIC_X2_RESET: {
2100 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2101 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET");
2106 case RIC_X2_RESET_RESP: {
2107 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2108 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET_RESP");
2113 case RIC_SCTP_CLEAR_ALL: {
2114 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2115 // loop on all keys and close socket and then erase all map.
2117 sctpMap->getKeys(v);
2118 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2119 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2120 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2121 if (peerInfo == nullptr) {
2124 close(peerInfo->fileDescriptor);
2125 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2126 message.message.direction = 'D';
2127 message.message.time.tv_nsec = ts.tv_nsec;
2128 message.message.time.tv_sec = ts.tv_sec;
2130 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2131 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
2133 "%s|RIC_SCTP_CLEAR_ALL",
2134 peerInfo->enodbName);
2135 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2136 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
2137 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
2138 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2148 case E2_TERM_KEEP_ALIVE_REQ: {
2149 // send message back
2150 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
2151 (unsigned char *)rmrMessageBuffer.ka_message,
2152 rmrMessageBuffer.ka_message_len);
2153 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2154 rmrMessageBuffer.sendMessage->state = 0;
2155 static unsigned char tx[32];
2156 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2157 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2158 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2159 if (rmrMessageBuffer.sendMessage == nullptr) {
2160 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2161 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
2162 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2163 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
2164 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
2165 } else if (mdclog_level_get() >= MDCLOG_DEBUG) {
2166 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
2172 mdclog_write(MDCLOG_WARN, "Message Type : %d is not seported", rmrMessageBuffer.rcvMessage->mtype);
2173 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2174 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2175 message.message.time.tv_nsec = ts.tv_nsec;
2176 message.message.time.tv_sec = ts.tv_sec;
2177 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2179 buildJsonMessage(message);
2184 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2185 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2191 * Send message to the CU that is not expecting for successful or unsuccessful results
2192 * @param messageBuffer
2194 * @param failedMsgId
2198 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2199 ReportingMessages_t &message,
2201 Sctp_Map_t *sctpMap) {
2203 getRequestMetaData(message, messageBuffer);
2204 if (mdclog_level_get() >= MDCLOG_INFO) {
2205 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2208 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
2215 * @param messageBuffer
2217 * @param failedMesgId
2220 int sendMessagetoCu(Sctp_Map_t *sctpMap,
2221 RmrMessagesBuffer_t &messageBuffer,
2222 ReportingMessages_t &message,
2224 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2225 if (peerInfo == nullptr) {
2226 if (failedMesgId != 0) {
2227 sendFailedSendingMessagetoXapp(messageBuffer, message, failedMesgId);
2229 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2235 message.message.messageType = messageBuffer.rcvMessage->mtype;
2236 auto rc = sendSctpMsg(peerInfo, message, sctpMap);
2242 * @param rmrCtx the rmr context to send and receive
2243 * @param msg the msg we got fromxApp
2244 * @param metaData data from xApp in ordered struct
2245 * @param failedMesgId the return message type error
2248 sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, int failedMesgId) {
2249 rmr_mbuf_t *msg = rmrMessageBuffer.sendMessage;
2250 msg->len = snprintf((char *) msg->payload, 200, "the gNb/eNode name %s not found",
2251 message.message.enodbName);
2252 if (mdclog_level_get() >= MDCLOG_INFO) {
2253 mdclog_write(MDCLOG_INFO, "%s", msg->payload);
2255 msg->mtype = failedMesgId;
2258 static unsigned char tx[32];
2259 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2260 rmr_bytes2xact(msg, tx, strlen((const char *) tx));
2262 sendRmrMessage(rmrMessageBuffer, message);
2277 int addToEpoll(int epoll_fd,
2278 ConnectedCU_t *peerInfo,
2280 Sctp_Map_t *sctpMap,
2284 struct epoll_event event{};
2285 event.data.ptr = peerInfo;
2286 event.events = events;
2287 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2288 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2289 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here), %s, %s %d",
2290 strerror(errno), __func__, __LINE__);
2292 close(peerInfo->fileDescriptor);
2293 if (enodbName != nullptr) {
2294 cleanHashEntry(peerInfo, sctpMap);
2295 char key[MAX_ENODB_NAME_SIZE * 2];
2296 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2297 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2298 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2300 auto tmp = sctpMap->find(key);
2303 sctpMap->erase(key);
2306 peerInfo->enodbName[0] = 0;
2308 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
2324 int modifyToEpoll(int epoll_fd,
2325 ConnectedCU_t *peerInfo,
2327 Sctp_Map_t *sctpMap,
2331 struct epoll_event event{};
2332 event.data.ptr = peerInfo;
2333 event.events = events;
2334 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2335 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2336 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may chack not to quit here), %s, %s %d",
2337 strerror(errno), __func__, __LINE__);
2339 close(peerInfo->fileDescriptor);
2340 cleanHashEntry(peerInfo, sctpMap);
2341 char key[MAX_ENODB_NAME_SIZE * 2];
2342 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2343 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2344 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2346 auto tmp = sctpMap->find(key);
2350 sctpMap->erase(key);
2351 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
2358 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
2359 buildJsonMessage(message);
2361 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2363 if (rmrMessageBuffer.sendMessage == nullptr) {
2364 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2365 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2369 if (rmrMessageBuffer.sendMessage->state != 0) {
2370 char meid[RMR_MAX_MEID]{};
2371 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2373 rmrMessageBuffer.sendMessage->state = 0;
2374 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2375 rmrMessageBuffer.sendMessage->mtype,
2376 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2377 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2378 if (rmrMessageBuffer.sendMessage == nullptr) {
2379 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2380 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2382 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2383 mdclog_write(MDCLOG_ERR,
2384 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2385 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2386 rmrMessageBuffer.sendMessage->mtype,
2387 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2388 auto rc = rmrMessageBuffer.sendMessage->state;
2392 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2393 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2394 rmrMessageBuffer.sendMessage->mtype,
2395 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2396 return rmrMessageBuffer.sendMessage->state;
2402 void buildJsonMessage(ReportingMessages_t &message) {
2404 message.outLen = sizeof(message.base64Data);
2405 base64::encode((const unsigned char *) message.message.asndata,
2406 (const int) message.message.asnLength,
2409 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2410 mdclog_write(MDCLOG_DEBUG, "asn data length = %d, base64 message length = %d ",
2411 (int) message.message.asnLength,
2412 (int) message.outLen);
2415 snprintf(message.buffer, sizeof(message.buffer),
2416 "{\"header\": {\"ts\": \"%ld.%09ld\","
2417 "\"ranName\": \"%s\","
2418 "\"messageType\": %d,"
2419 "\"direction\": \"%c\"},"
2420 "\"base64Length\": %d,"
2421 "\"asnBase64\": \"%s\"}",
2422 message.message.time.tv_sec,
2423 message.message.time.tv_nsec,
2424 message.message.enodbName,
2425 message.message.messageType,
2426 message.message.direction,
2427 (int) message.outLen,
2428 message.base64Data);
2429 static src::logger_mt &lg = my_logger::get();
2431 BOOST_LOG(lg) << message.buffer;
2437 * take RMR error code to string
2441 string translateRmrErrorMessages(int state) {
2445 str = "RMR_OK - state is good";
2447 case RMR_ERR_BADARG:
2448 str = "RMR_ERR_BADARG - argument passd to function was unusable";
2450 case RMR_ERR_NOENDPT:
2451 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2454 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2457 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2459 case RMR_ERR_SENDFAILED:
2460 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2462 case RMR_ERR_CALLFAILED:
2463 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2465 case RMR_ERR_NOWHOPEN:
2466 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2469 str = "RMR_ERR_WHID - wormhole id was invalid";
2471 case RMR_ERR_OVERFLOW:
2472 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2475 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2477 case RMR_ERR_RCVFAILED:
2478 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2480 case RMR_ERR_TIMEOUT:
2481 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2484 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2487 str = "RMR_ERR_TRUNC - received message likely truncated";
2489 case RMR_ERR_INITFAILED:
2490 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2492 case RMR_ERR_NOTSUPP:
2493 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2497 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);