added my Recipes

This commit is contained in:
2024-07-11 14:16:35 +02:00
parent 38bc4f53ac
commit 09b621d929
7118 changed files with 525762 additions and 3 deletions

View File

@@ -0,0 +1,23 @@
#include "../include/RTSMonitoringTask.h"
#include <chrono>
#include <thread>
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
RTSMonitoringTask rtMon;
if (rtMon.Init()) {
// idle
while(1)
{
rtMon.CreateMonitoring(1, "Test1", 3750, 2, 10, "stopped");
// sleep 100 ms
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
return 0;
}

View File

@@ -0,0 +1,119 @@
/*
*/
#include "../../include/RTSMonitoringTask.h"
//#include "../../include/json.hpp"//((
//#include "../../include/json_fwd.hpp"
#include <string>
#include<iostream>
#include<cstring>
RTSMonitoringTask::RTSMonitoringTask(/* args */)
{
}
RTSMonitoringTask::~RTSMonitoringTask()
{
}
// bool RTSMonitoringTask::Load() {
// //later load the file
// int id = 1;
// RTSMonFrame* rtsMonFrame = new RTSMonFrame(id, "Monitoring1", 3750, 1, 4, "started");
// rtsMonFrames[id]= rtsMonFrame;
// return true;
// }
bool RTSMonitoringTask::Init() {
//load the Monitorings
// if (this->Load()) {
// printf("CA7 : Monitorings loaded\n");
// }
const std::string fwPath = "home/root/elffile";
const std::string fwName = "zephyr_openamp_rsc_table.elf";
//check if the FWIsRunning
if (coproHelper.Init(fwPath, fwName) >= 0) {
printf("CA7 : Init success \n");
// if (pthread_create( &monThread, NULL, &RTSMonitoringTask::Run, this) == 0) {
// printf("CA7 : virtual_tty_thread creation fails\n");
// return true;
// }
return true;
}
printf("CA7 : starting of Zephyr.elf fails\n");
return false;
}
// bool RTSMonitoringTask::CreateMonitoring(int id, std::string name, int samplerate, int sampleperiod, int downtime, std::string status){
// // this->rtsMonFrames[id] = new RTSMonFrame(id, name, samplerate, sampleperiod, downtime, status);
// char * ch = new char[name.length() + 1];
// // Copying the value of the string into the character array.
// strcpy(ch, name.c_str());
// this->coproHelper.Copro_writeTtyRpmsg(0,name.length()+1, ch);
// return true;
// }
// void* RTSMonitoringTask::Run(void *obj) {
// RTSMonitoringTask* rtsMonTask = static_cast<RTSMonitoringTask*>(obj);
// std::string newItem;
// while (1) {
// // genrate data
// int i = 1;
// newItem = "New Item"; //new std::string("New Item: ").concat(new std::string(i));
// i++;
// std::map<int, RTSMonFrame*> rtsMonFrame = rtsMonTask->rtsMonFrames;
// if (rtsMonFrame.size() > 0) {
// RTSMonFrame* prtsMonFrame = rtsMonFrame.begin()->second;
// prtsMonFrame->Add(newItem);
// if (i == 200) {
// //make a dictonary for the rtsMonFrame id, samplerate, sampleperiod, downtime
// // boost::python::dict rtsMonFrameDict;
// // rtsMonFrameDict["id"] = prtsMonFrame->GetId();
// // rtsMonFrameDict["samplerate"] = prtsMonFrame->GetSampleRate();
// // rtsMonFrameDict["sampleperiod"] = prtsMonFrame->GetSamplePeriod();
// // rtsMonFrameDict["downtime"] = prtsMonFrame->GetDownTime();
// // //make a dictonary for the sampleItems
// // boost::python::list rtsMonItemsList;
// // auto items = prtsMonFrame->GetItems();
// // for (auto iter = items.begin(); iter!= items.end(); ++iter)
// // {
// // rtsMonItemsList.append(*iter);
// // }
// // rtsMonFrameDict["items"]= rtsMonItemsList;
// // rtsMonTask->callback();
// }
// }
// }
// }
bool RTSMonitoringTask::Start(int samplerate, int sampleperiod, int downtime){
// char * ch = new char[name.length() + 1];
// // Copying the value of the string into the character array.
// strcpy(ch, name.c_str());
// this->coproHelper.Copro_writeTtyRpmsg(0,name.length()+1, ch);
return true;
}
bool RTSMonitoringTask::Stop(std::string id){
return true;
}

View File

@@ -0,0 +1,123 @@
#include <boost/python.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <string>
#include <RTSMonitoringTask.h>
#include <RelGILLock.h>
#include <GILLock.h>
struct RTService
{
private:
/* data */
RTSMonitoringTask rtsMonTask;
RTSCoproHelper rtsCopro;
public:
// RT_Service(/* args */) {
// }
// ~RT_Service() {}
bool isCoproRunning(){
PyRelinquishGIL scoped;
return rtsCopro.Copro_isFwRunning();
}
bool initCoproFW(std::string fwPath, std::string fwName){
PyRelinquishGIL scoped;
return rtsCopro.Copro_init(fwPath, fwName);
}
bool initLogChannel(){
PyRelinquishGIL scoped;
return rtsCopro.Copro_initLogChannel();
}
bool readLogChannel(){
PyRelinquishGIL scoped;
return rtsCopro.Copro_readLogChannel();
}
boost::python::list getMonitoringStates() {
//TODO ask the M4Core for the Monitpring Status
PyRelinquishGIL scoped;
boost::python::list list;
return list;
}
// we need all parameters like id, samplerate, sampletime, downtime, later: HW-channels with the configurations
bool startMonitoring(int samplerate, int sampleperiod, int downtime, std::string status) {
PyRelinquishGIL scoped;
//TODO ML: add this to the M4Core
if (rtsMonTask.Init()) {
return rtsMonTask.Start(samplerate, sampleperiod, downtime);
}
return false;
}
// we need all parameters like id, samplerate, sampletime, downtime, later: HW-channels with the configurations
bool stopMonitoring(std::string id) {
PyRelinquishGIL scoped;
//TODO ML: add this to the M4Core
if (rtsMonTask.Init()) {
return rtsMonTask.Stop(id);
}
return false;
}
};
using namespace boost::python;
BOOST_PYTHON_MODULE(rt_service)
{
namespace python = boost::python;
// python::class_<RTSMonScript, boost::noncopyable>("RTSMonitoring", python::no_init)
// .add_property("id", &RTSMonitoring::GetId, &RTSMonitoring::SetId)
// .add_property("samplerate", &RTSMonitoring::GetSampleRate, &RTSMonitoring::SetId)
// .add_property("samplePeriod", &RTSMonitoring::GetSamplePeriod, &RTSMonitoring::SetId)
// .add_property("downtime", &RTSMonitoring::GetDownTime, &RTSMonitoring::SetId)
// .def("add2Script", pure_virtual(&RTSMonitoring::add2Script))
// ;
// class_<std::vector<RTSMonitoring*>>("RTSMonitoringList")
// .def(vector_indexing_suite<std::vector<RTSMonitoring*>>())
// ;
// def("add2Script", +[](RTSMonitoring* object) {
// return object->add2Script();
// });
class_<RTService>("RT_Service")
.def("isCoproRunning", &RTService::isCoproRunning)
.def("initCoproFW", &RTService::initCoproFW)
.def("initLogChannel", &RTService::initLogChannel)
.def("readLogChannel", &RTService::readLogChannel)
.def("getAllMonitoringStat", &RTService::getMonitoringStates)
.def("startMonitoring", &RTService::startMonitoring)
.def("stopMonitoring", &RTService::stopMonitoring)
;
};
/**
* startmonitoring()
* stopmonitoring(id)
.def("createMonitoring", &RTService::createMonitoring)
.def("setMonitoringStatus", &RTService::setMonitoringStatus)
.def("getMonitoringStatus", &RTService::getMonitoringState)
*/

View File

@@ -0,0 +1,14 @@
#include <GILLock.h>
//Use this class in a c++ funktion that called into python : c++ => python
PyLockGIL::PyLockGIL()
: m_gstate(PyGILState_Ensure()) {
}
PyLockGIL::~PyLockGIL() {
PyGILState_Release(m_gstate);
}

View File

@@ -0,0 +1,412 @@
/*
*/
#include "../../include/RTSCoproHelper.h"
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <inttypes.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h> // for usleep
#include <math.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/eventfd.h>
#include <sys/poll.h>
#include <regex.h>
#include <sched.h>
#include <assert.h>
#include <errno.h>
#include <error.h>
#define MAX_BUF 80
#define TTY_CTRL_OPTS (CS8 | CLOCAL | CREAD)
#define TTY_INPUT_OPTS IGNPAR
#define TTY_OUTPUT_OPTS 0
#define TTY_LOCAL_OPTS 0
#define LOGCHANNEL 0
#define DATACHANNEL 1
#define TTYDEVICENAME "/dev/ttyRPMSG%d"
RTSCoproHelper::RTSCoproHelper(/* args */)
{
}
RTSCoproHelper::~RTSCoproHelper()
{
}
int RTSCoproHelper::Init(std::string fwPath, std::string fwName) {
int res = -1;
if (this->Copro_isFwRunning() != 0){
//copro is already Runnibng nothing TODO
//res = this->Copro_openTtyRpmsg(CMDCHANNEL, 0);
this->Copro_stopFw();
//return res;
//return 0;
}
//load Firmware
//set firmware path
res = this->Copro_setFwPath(fwPath.c_str());
if (res >= 0) {
printf("CA7 : Success setting the fwpath, res=%d\n", res);
res = this->Copro_setFwName(fwName.c_str());
if (res >= 0) {
printf("CA7 : Success setting the fwname, res=%d\n", res);
res = this->Copro_startFw();
if (res >= 0) {
printf("CA7 : Success starting Copro, err=%d\n", res);
}
else {
printf("CA7 : Error could not start the firmware, res=-%d\n", res);
}
}
else {
printf("CA7 : Error setting the fwName, err=-%d\n", res);
}
}
else {
printf("CA7 : Error setting the fwpath, err=-%d\n", res);
}
return res;
}
bool RTSCoproHelper::Copro_initLogChannel(){
int res = this->Copro_openTtyRpmsg(LOGCHANNEL, 1);
if (res >= 0 ){
printf("CA7 : Success opening Logchannel, err=%d\n", res);
char* firstCMD = "Test";
res = this->Copro_writeTtyRpmsg(LOGCHANNEL, strlen(firstCMD), firstCMD);
if (res >= 0 ){
printf("CA7 : Success sending data to Logchannel, err=%d\n", res);
res = 0;
}
else {
printf("CA7 : Error sending to logchannel, err=%d\n", res);
}
}
else {
printf("CA7 : Error opening logchannel, err=%d\n", res);
}
return (res >=0);
}
/**
* read1 = copro_readTtyRpmsg(1, 512, mRxTraceBuffer);
mRxTraceBuffer[read1] = 0; // to be sure to get a end of string
if (read1 > 0) {
if (strcmp(mRxTraceBuffer, "CM4 : DMA TransferError") == 0) {
// sampling is aborted, refresh the UI
mErrorDetected = 1;
//mMachineState = STATE_READY;
//gdk_threads_add_idle (refreshUI_CB, window);
}
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
if (mRxTraceBuffer[0] == 'C') {
printf("[%ld.%06ld] : %s\n",
(long int)tval_result.tv_sec, (long int)tval_result.tv_usec,
mRxTraceBuffer);
} else {
printf("[%ld.%06ld] : CA7 : tty1 got %d [%x] bytes\n",
(long int)tval_result.tv_sec, (long int)tval_result.tv_usec,
read1, mRxTraceBuffer[0]);
}
}
*/
std::string RTSCoproHelper::Copro_readLogChannel(){
int res = this->Copro_readTtyRpmsg(LOGCHANNEL, 512, mRxTraceBuffer);
struct timeval tval_before, tval_after, tval_result;
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
if (res > 0 ){
printf("CA7 : Success sending data to Logchannel, err=%d\n", res);
printf("[%ld.%06ld] : %s\n",
(long int)tval_result.tv_sec, (long int)tval_result.tv_usec,
mRxTraceBuffer);
}
else {
printf("CA7 : Error sending to logchannel, err=%d\n", res);
}
return mRxTraceBuffer;
}
/********************************************************************************
Copro functions allowing to manage a virtual TTY over RPMSG
*********************************************************************************/
int RTSCoproHelper::Copro_isFwRunning(void)
{
int fd;
size_t byte_read;
bool result = 0;
unsigned char bufRead[MAX_BUF];
// char *user = getenv("USER");
// if (user && strncmp(user, "root", 4)) {
// system("XTERM=xterm su root -c 'cat /sys/class/remoteproc/remoteproc0/state' > /tmp/remoteproc0_state");
// fd = open("/tmp/remoteproc0_state", O_RDONLY);
// } else {
fd = open("/sys/class/remoteproc/remoteproc0/state", O_RDONLY);
// }
if (fd < 0) {
printf("CA7 : Error opening remoteproc0/state, err=-%d\n", errno);
return (errno * -1);
}
byte_read = (size_t) read (fd, bufRead, MAX_BUF);
if (byte_read >= strlen("running")) {
char* pos = strstr((char*)bufRead, "running");
if(pos) {
result = 1;
}
}
close(fd);
return result;
}
int RTSCoproHelper::Copro_stopFw(void)
{
int fd;
// char *user = getenv("USER");
// if (user && strncmp(user, "root",4)) {
// system("su root -c 'echo stop > /sys/class/remoteproc/remoteproc0/state'");
// return 0;
// }
fd = open("/sys/class/remoteproc/remoteproc0/state", O_RDWR);
if (fd < 0) {
printf("CA7 : Error opening remoteproc0/state, err=-%d\n", errno);
return (errno * -1);
}
write(fd, "stop", strlen("stop"));
close(fd);
return 0;
}
int RTSCoproHelper::Copro_startFw(void)
{
int fd;
// char *user = getenv("USER");
// if (user && strncmp(user, "root",4)) {
// system("su root -c 'echo start > /sys/class/remoteproc/remoteproc0/state'");
// return 0;
// }
fd = open("/sys/class/remoteproc/remoteproc0/state", O_RDWR);
if (fd < 0) {
printf("CA7 : Error opening remoteproc0/state, err=-%d\n", errno);
return (errno * -1);
}
write(fd, "start", strlen("start"));
close(fd);
return 0;
}
int RTSCoproHelper::Copro_getFwPath(char* pathStr)
{
int fd;
int byte_read;
// char *user = getenv("USER");
// if (user && strncmp(user, "root",4)) {
// system("XTERM=xterm su root -c 'cat /sys/module/firmware_class/parameters/path' > /tmp/parameters_path");
// fd = open("/tmp/parameters_path", O_RDONLY);
// } else {
fd = open("/sys/module/firmware_class/parameters/path", O_RDONLY);
// }
if (fd < 0) {
printf("CA7 : Error opening firmware_class/parameters/path, err=-%d\n", errno);
return (errno * -1);
}
byte_read = read (fd, pathStr, MAX_BUF);
close(fd);
return byte_read;
}
int RTSCoproHelper::Copro_setFwPath(const char* pathStr)
{
int fd;
int result = 0;
// char *user = getenv("USER");
// if (user && strncmp(user, "root",4)) {
// char cmd[1024];
// snprintf(cmd, 1024, "echo %s > /sys/module/firmware_class/parameters/path", pathStr);
// //snprintf(cmd, 1024, "su root -c 'echo %s > /sys/module/firmware_class/parameters/path'", pathStr);
// system(cmd);
// return strlen(pathStr);
// }
fd = open("/sys/module/firmware_class/parameters/path", O_RDWR);
if (fd < 0) {
printf("CA7 : Error opening firmware_class/parameters/path, err=-%d\n", errno);
return (errno * -1);
}
result = write(fd, pathStr, strlen(pathStr));
close(fd);
return result;
}
int RTSCoproHelper::Copro_getFwName(char* pathStr)
{
int fd;
int byte_read;
// char *user = getenv("USER");
// if (user && strncmp(user, "root",4)) {
// system("XTERM=xterm su root -c 'cat /sys/class/remoteproc/remoteproc0/firmware' > /tmp/remoteproc0_firmware");
// fd = open("/tmp/remoteproc0_firmware", O_RDONLY);
// } else {
fd = open("/sys/class/remoteproc/remoteproc0/firmware", O_RDWR);
// }
if (fd < 0) {
printf("CA7 : Error opening remoteproc0/firmware, err=-%d\n", errno);
return (errno * -1);
}
byte_read = read (fd, pathStr, MAX_BUF);
close(fd);
return byte_read;
}
int RTSCoproHelper::Copro_setFwName(const char* nameStr)
{
int fd;
int result = 0;
// char *user = getenv("USER");
// if (user && strncmp(user, "root",4)) {
// char cmd[1024];
// snprintf(cmd, 1024, "su root -c 'echo %s > /sys/class/remoteproc/remoteproc0/firmware'", nameStr);
// system(cmd);
// return strlen(nameStr);
// }
fd = open("/sys/class/remoteproc/remoteproc0/firmware", O_RDWR);
if (fd < 0) {
printf("CA7 : Error opening remoteproc0/firmware, err=-%d\n", errno);
return (errno * -1);
}
result = write(fd, nameStr, strlen(nameStr));
close(fd);
return result;
}
int RTSCoproHelper::Copro_openTtyRpmsg(int ttyNb, [[maybe_unused]]int modeRaw)
{
//struct termios tiorpmsg;
char devName[50];
sprintf(devName, TTYDEVICENAME, ttyNb%2);
mFdRpmsg[ttyNb%2] = open(devName, O_RDWR | O_NOCTTY | O_NONBLOCK);
// mFdRpmsg[ttyNb%2] = open("rpmsg-client-sample", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (mFdRpmsg[ttyNb%2] < 0) {
printf("CA7 : Error opening %s%d, err=-%d\n", TTYDEVICENAME, ttyNb%2, errno);
return (errno * -1);
}
printf("CA7 : Success opening %s%d \n", TTYDEVICENAME, ttyNb%2);
// #if 1
// /* get current port settings */
// tcgetattr(mFdRpmsg[ttyNb%2],&tiorpmsg);
// if (modeRaw) {
// #if 0
// tiorpmsg.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
// | INLCR | IGNCR | ICRNL | IXON);
// tiorpmsg.c_oflag &= ~OPOST;
// tiorpmsg.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
// tiorpmsg.c_cflag &= ~(CSIZE | PARENB);
// tiorpmsg.c_cflag |= CS8;
// #else
// memset(&tiorpmsg, 0, sizeof(tiorpmsg));
// tiorpmsg.c_cflag = TTY_CTRL_OPTS;
// tiorpmsg.c_iflag = TTY_INPUT_OPTS;
// tiorpmsg.c_oflag = TTY_OUTPUT_OPTS;
// tiorpmsg.c_lflag = TTY_LOCAL_OPTS;
// tiorpmsg.c_cc[VTIME] = 0;
// tiorpmsg.c_cc[VMIN] = 1;
// cfmakeraw(&tiorpmsg);
// #endif
// } else {
// /* ECHO off, other bits unchanged */
// tiorpmsg.c_lflag &= ~ECHO;
// /*do not convert LF to CR LF */
// tiorpmsg.c_oflag &= ~ONLCR;
// }
// if (tcsetattr(mFdRpmsg[ttyNb%2], TCSANOW, &tiorpmsg) < 0) {
// printf("Error %d in copro_openTtyRpmsg(%d) tcsetattr\n", errno, ttyNb);
// return (errno * -1);
// }
// #endif
return 0;
}
int RTSCoproHelper::Copro_closeTtyRpmsg(int ttyNb)
{
close(mFdRpmsg[ttyNb%2]);
mFdRpmsg[ttyNb%2] = -1;
return 0;
}
int RTSCoproHelper::Copro_writeTtyRpmsg(int ttyNb, int len, char* pData)
{
int result = 0;
if (mFdRpmsg[ttyNb%2] < 0) {
printf("CA7 : Error writing ttyRPMSG%d, fileDescriptor is not set\n", ttyNb%2);
return mFdRpmsg[ttyNb%2];
}
result = write(mFdRpmsg[ttyNb%2], pData, len);
if (result >= 0) {
printf("CA7 : Succes writing ttyRPMSG%d, result=%d\n", ttyNb%2, result);
}
else {
printf("CA7 : Error writing ttyRPMSG%d, err=-%d\n", ttyNb%2, result);
}
return result;
}
int RTSCoproHelper::Copro_readTtyRpmsg(int ttyNb, int len, char* pData)
{
int byte_rd, byte_avail;
int result = 0;
if (mFdRpmsg[ttyNb%2] < 0) {
printf("CA7 : Error reading ttyRPMSG%d, fileDescriptor is not set\n", ttyNb%2);
return mFdRpmsg[ttyNb%2];
}
ioctl(mFdRpmsg[ttyNb%2], FIONREAD, &byte_avail);
if (byte_avail > 0) {
if (byte_avail >= len) {
byte_rd = read (mFdRpmsg[ttyNb%2], pData, len);
} else {
byte_rd = read (mFdRpmsg[ttyNb%2], pData, byte_avail);
}
//printf("CA7 : read successfully %d bytes to %p, [0]=0x%x\n", byte_rd, pData, pData[0]);
result = byte_rd;
} else {
result = 0;
}
return result;
}
/********************************************************************************
End of Copro functions
*********************************************************************************/

View File

@@ -0,0 +1,16 @@
#include <RelGILLock.h>
//Use this class in a c++ funktion that is called from python : python => c++
PyRelinquishGIL::PyRelinquishGIL()
: m_thread_state(PyEval_SaveThread()) {
}
PyRelinquishGIL::~PyRelinquishGIL() {
PyEval_RestoreThread(m_thread_state);
m_thread_state= NULL;
}