2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/timemanager/Clock.h>
|
2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/serviceinterface/ServiceInterfaceBuffer.h>
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <cstring>
|
2020-01-31 00:54:34 +01:00
|
|
|
#include <inttypes.h>
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2018-07-13 15:56:37 +02:00
|
|
|
// to be implemented by bsp
|
|
|
|
extern "C" void printChar(const char*);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2019-10-23 12:03:32 +02:00
|
|
|
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
int ServiceInterfaceBuffer::overflow(int c) {
|
|
|
|
// Handle output
|
|
|
|
putChars(pbase(), pptr());
|
|
|
|
if (c != Traits::eof()) {
|
|
|
|
char c2 = c;
|
|
|
|
// Handle the one character that didn't fit to buffer
|
|
|
|
putChars(&c2, &c2 + 1);
|
|
|
|
}
|
|
|
|
// This tells that buffer is empty again
|
|
|
|
setp(buf, buf + BUF_SIZE - 1);
|
|
|
|
// I'm not sure about this return value!
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ServiceInterfaceBuffer::sync(void) {
|
|
|
|
if (this->isActive) {
|
2018-07-12 16:29:32 +02:00
|
|
|
Clock::TimeOfDay_t loggerTime;
|
|
|
|
Clock::getDateAndTime(&loggerTime);
|
2016-06-15 23:48:41 +02:00
|
|
|
char preamble[96] = { 0 };
|
2020-04-10 17:06:06 +02:00
|
|
|
sprintf(preamble, "\r%s: | %" PRIu32 ":%02" PRIu32 ":%02" PRIu32
|
|
|
|
".%03" PRIu32 " | ", this->log_message.c_str(),
|
2020-01-31 00:54:34 +01:00
|
|
|
loggerTime.hour,
|
|
|
|
loggerTime.minute,
|
|
|
|
loggerTime.second,
|
|
|
|
loggerTime.usecond /1000);
|
2016-06-15 23:48:41 +02:00
|
|
|
// Write log_message and time
|
|
|
|
this->putChars(preamble, preamble + sizeof(preamble));
|
|
|
|
// Handle output
|
|
|
|
this->putChars(pbase(), pptr());
|
|
|
|
}
|
|
|
|
// This tells that buffer is empty again
|
|
|
|
setp(buf, buf + BUF_SIZE - 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:28:26 +02:00
|
|
|
#ifndef UT699
|
2018-07-13 15:56:37 +02:00
|
|
|
|
2020-04-10 17:06:06 +02:00
|
|
|
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message,
|
|
|
|
uint16_t port) {
|
2018-07-13 15:56:37 +02:00
|
|
|
this->log_message = set_message;
|
|
|
|
this->isActive = true;
|
|
|
|
setp( buf, buf + BUF_SIZE );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
|
|
|
char array[BUF_SIZE];
|
|
|
|
uint32_t length = end - begin;
|
|
|
|
if (length > sizeof(array)) {
|
|
|
|
length = sizeof(array);
|
|
|
|
}
|
|
|
|
memcpy(array, begin, length);
|
|
|
|
|
2020-04-10 17:06:06 +02:00
|
|
|
for(; begin != end; begin++){
|
2018-07-13 15:56:37 +02:00
|
|
|
printChar(begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
#endif
|
2018-07-13 15:56:37 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
#ifdef UT699
|
2018-07-13 18:28:26 +02:00
|
|
|
#include <framework/osal/rtems/Interrupt.h>
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, uint16_t port) {
|
|
|
|
this->log_message = set_message;
|
|
|
|
this->isActive = true;
|
|
|
|
setp( buf, buf + BUF_SIZE );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
|
|
|
char array[BUF_SIZE];
|
|
|
|
uint32_t length = end - begin;
|
|
|
|
if (length > sizeof(array)) {
|
|
|
|
length = sizeof(array);
|
|
|
|
}
|
|
|
|
memcpy(array, begin, length);
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
if (!Interrupt::isInterruptInProgress()) {
|
2016-06-15 23:48:41 +02:00
|
|
|
std::cout << array;
|
|
|
|
} else {
|
|
|
|
//Uncomment the following line if you need ISR debug output.
|
|
|
|
// printk(array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif //UT699
|
|
|
|
|
|
|
|
#ifdef ML505
|
|
|
|
#include <bsp_flp/network/networkconfig.h>
|
|
|
|
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message,
|
|
|
|
uint16_t port) :
|
|
|
|
isActive(true), log_message(set_message), udpSocket(0), remoteAddressLength(
|
|
|
|
sizeof(remoteAddress)) {
|
|
|
|
setp(buf, buf + BUF_SIZE);
|
|
|
|
memset((uint8_t*) &remoteAddress, 0, sizeof(remoteAddress));
|
|
|
|
remoteAddress.sin_family = AF_INET;
|
|
|
|
remoteAddress.sin_port = htons(port);
|
|
|
|
remoteAddress.sin_addr.s_addr = htonl(inet_addr("192.168.250.100"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
|
|
|
|
|
|
|
char array[BUF_SIZE];
|
|
|
|
uint32_t length = end - begin;
|
|
|
|
if (length > sizeof(array)) {
|
|
|
|
length = sizeof(array);
|
|
|
|
}
|
|
|
|
memcpy(array, begin, length);
|
|
|
|
|
|
|
|
if (udpSocket <= 0) {
|
|
|
|
initSocket();
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
if (udpSocket > 0) {
|
|
|
|
sendto(udpSocket, array, length, 0, (sockaddr*) &remoteAddress,
|
|
|
|
sizeof(remoteAddress));
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceInterfaceBuffer::initSocket() {
|
|
|
|
sockaddr_in address;
|
|
|
|
memset((uint8_t*) &address, 0, sizeof(address));
|
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_port = htons(0);
|
|
|
|
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (socket < 0) {
|
|
|
|
printf("Error opening socket!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timeval timeout = { 0, 20 };
|
|
|
|
if (setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout,
|
|
|
|
sizeof(timeout)) < 0) {
|
|
|
|
printf("Error setting SO_RCVTIMEO socket options!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (setsockopt(udpSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout,
|
|
|
|
sizeof(timeout)) < 0) {
|
|
|
|
printf("Error setting SO_SNDTIMEO socket options!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bind(udpSocket, (sockaddr *) &address, sizeof(address)) < 0) {
|
|
|
|
printf("Error binding socket!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //ML505
|