1st Version with ECAL
This commit is contained in:
149
src/RTService.cpp
Normal file
149
src/RTService.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
#include <boost/interprocess/ipc/message_queue.hpp>
|
||||
#include <ecal/ecal.h>
|
||||
#include <ecal/msg/protobuf/server.h>
|
||||
#include "RTService.pb.h"
|
||||
#include "../include/RTSMonitoringTask.h"
|
||||
|
||||
using namespace boost::interprocess;
|
||||
using namespace proto_messages;
|
||||
|
||||
|
||||
class RTServiceImpl : public RTService
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
|
||||
RTSMonitoringTask rtsMonTask;
|
||||
|
||||
const std::string RTSCOMMANDQUEUE = "RTSSERVICECOMMANDQUEUE";
|
||||
const int MAXMSGSIZE = 250;
|
||||
const int MAXMSGCOUNT = 25;
|
||||
|
||||
// message_queue shared_ptr<message_queue> rtsMQ;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
virtual void createMonitoring(::google::protobuf::RpcController* /* controller_ */, const SCreateIn* request_, ::SResult* response_, ::google::protobuf::Closure* /* done_ */)
|
||||
{
|
||||
// print request and
|
||||
std::cout << "Received request RTService / CreateMonitoring : " << request_->id() << " and " << request_->name() << std::endl << std::endl;
|
||||
bool res = rtsMonTask.CreateMonitoring(request_->id(), request_->samplerate(), request_->sampleperiod(), request_->downtime(), request_->status());
|
||||
|
||||
// create response
|
||||
response_->set_result(res);
|
||||
}
|
||||
|
||||
// we need all parameters like id, samplerate, sampletime, downtime, later: HW-channels with the configurations
|
||||
bool createMonitoring(std::string id, int samplerate, int sampleperiod, int downtime, std::string status) {
|
||||
//TODO ML: add this to the M4Core
|
||||
if (rtsMonTask.Init()) {
|
||||
return rtsMonTask.CreateMonitoring(id, samplerate, sampleperiod, downtime, status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool deleteMonitoring(std::string id) {
|
||||
//TODO ML: add this to the M4Core
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string getMonitoringState(std::string id) {
|
||||
//TODO ask the M4Core for the Monitpring Status
|
||||
return "Start";
|
||||
}
|
||||
|
||||
std::list<std::string> getMonitoringStates() {
|
||||
//TODO ask the M4Core for the Monitpring Status
|
||||
std::list<std::string> list;
|
||||
return list;
|
||||
}
|
||||
|
||||
bool setMonitoringStatus(std::string id, std::string status) {
|
||||
//set the Status
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void Run() {
|
||||
BOOST_TRY{
|
||||
//Erase previous message queue
|
||||
message_queue::remove(RTSCOMMANDQUEUE.c_str());
|
||||
|
||||
//Create a message_queue.
|
||||
message_queue rtsMQ
|
||||
(create_only //only create
|
||||
,RTSCOMMANDQUEUE.c_str() //name
|
||||
,MAXMSGCOUNT //max message number
|
||||
,MAXMSGSIZE //max message size
|
||||
);
|
||||
|
||||
bool bCmdQueueRun = true;
|
||||
unsigned int priority;
|
||||
message_queue::size_type recvd_size;
|
||||
boost::property_tree::ptree pt;
|
||||
|
||||
while (bCmdQueueRun) {
|
||||
char rtsCmdbuffer [MAXMSGSIZE];
|
||||
//mq.receive(buffer, sizeof(buffer), recvd_size, pri);
|
||||
|
||||
rtsMQ.receive(rtsCmdbuffer, sizeof(rtsCmdbuffer), recvd_size, priority);
|
||||
//read json and go to cmd execution
|
||||
boost::property_tree::read_json(rtsCmdbuffer,pt);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// //Send 100 numbers
|
||||
// for(int i = 0; i < 100; ++i){
|
||||
// mq.send(&i, sizeof(i), 0);
|
||||
// }
|
||||
}
|
||||
BOOST_CATCH(interprocess_exception &ex){
|
||||
std::cout << ex.what() << std::endl;
|
||||
message_queue::remove(RTSCOMMANDQUEUE.c_str());
|
||||
} BOOST_CATCH_END
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// initialize eCAL API
|
||||
eCAL::Initialize(argc, argv, "RTService");
|
||||
|
||||
// create minimal service server
|
||||
eCAL::CServiceServer rtService("RTSMonService");
|
||||
|
||||
|
||||
// create Math service server
|
||||
std::shared_ptr<RTService> rt_service = std::make_shared<RTServiceImpl>();
|
||||
eCAL::protobuf::CServiceServer<RTService> math_server(rt_service);
|
||||
|
||||
|
||||
|
||||
// idle
|
||||
while(eCAL::Ok())
|
||||
{
|
||||
// sleep 100 ms
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
// finalize eCAL API
|
||||
eCAL::Finalize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
76
src/monitoringTask/RTSMonitoringTask.cpp
Normal file
76
src/monitoringTask/RTSMonitoringTask.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#include "../../include/RTSMonitoringTask.h"
|
||||
#include "../../include/json.hpp"
|
||||
#include "../../include/json_fwd.hpp"
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
|
||||
RTSMonitoringTask::RTSMonitoringTask(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
RTSMonitoringTask::~RTSMonitoringTask()
|
||||
{
|
||||
}
|
||||
|
||||
bool RTSMonitoringTask::Init() {
|
||||
//check if the FWIsRunning
|
||||
//if (coproHelper.)
|
||||
if (1) {
|
||||
if (pthread_create( &monThread, NULL, &RTSMonitoringTask::Run, this) == 0) {
|
||||
printf("CA7 : virtual_tty_thread creation fails\n");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool RTSMonitoringTask::CreateMonitoring(std::string id, int samplerate, int sampleperiod, int downtime, std::string status){
|
||||
this->rtsMonFrames[id] = new RTSMonFrame(id, samplerate, sampleperiod, downtime);
|
||||
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<std::string, 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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
231
src/monitoringTask/RTSMonitoringTask.cpp.old
Normal file
231
src/monitoringTask/RTSMonitoringTask.cpp.old
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#include "../../include/RTSMonitoring.h"
|
||||
|
||||
//static RTSMonitoringTask* RTMonitoringTask::rTSMonitoringTask = NULL;
|
||||
|
||||
|
||||
RTMonitoringTask::RTMonitoringTask(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
RTSMonitoringTask::~RTSMonitoringTask()
|
||||
{
|
||||
}
|
||||
|
||||
// static RTSMonitoringTask& RTSMonitoringTask::Instance() {
|
||||
// if (rTSMonitoringTask == NULL) {
|
||||
// rTSMonitoringTask = new RTSMonitoringTask();
|
||||
// }
|
||||
// return *rTSMonitoringTask;
|
||||
// }
|
||||
|
||||
|
||||
bool RTSMonitoringTask::Init() {
|
||||
//check if the FWIsRunning
|
||||
//if (coproHelper.)
|
||||
if (1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/********************************************************************************
|
||||
* Threading: Run Method
|
||||
*******************************************************************************/
|
||||
void *virtual_tty_thread(void *arg)
|
||||
{
|
||||
int read0, read1;
|
||||
int32_t wsize;
|
||||
int nb2copy = 0;
|
||||
char cmdmsg[20];
|
||||
|
||||
// open tty0
|
||||
if (copro_openTtyRpmsg(0, 1)) {
|
||||
printf("CA7 : fails to open the ttyRPMSG0\n");
|
||||
return (errno * -1);
|
||||
}
|
||||
//system("stty -F /dev/ttyRPMGS0 -isig");
|
||||
|
||||
// needed to allow M4 to send any data over virtualTTY
|
||||
copro_writeTtyRpmsg(0, 1, "r");
|
||||
|
||||
// open tty1
|
||||
if (copro_openTtyRpmsg(1, 1)) {
|
||||
printf("CA7 : fails to open the ttyRPMSG0\n");
|
||||
return (errno * -1);
|
||||
}
|
||||
// needed to allow M4 to send any data over virtualTTY
|
||||
copro_writeTtyRpmsg(1, 1, "r");
|
||||
|
||||
usleep(500000);
|
||||
sprintf(cmdmsg, "B%02d", NB_BUF);
|
||||
copro_writeTtyRpmsg(0, strlen(cmdmsg), cmdmsg);
|
||||
|
||||
while (1) {
|
||||
if (mThreadCancel) break; // kill thread requested
|
||||
|
||||
// tty0 is used for low rate compressed data transfer (less or equal to 5MHz sampling)
|
||||
read0 = copro_readTtyRpmsg(0, SAMP_SRAM_PACKET_SIZE, mByteBuffer);
|
||||
if (read0 > 0) {
|
||||
mNbTty0Frame++;
|
||||
mNbUncompData += read0;
|
||||
|
||||
mNbUncompMB = mNbUncompData / 1024 / 1024;
|
||||
if (mNbUncompMB != mNbPrevUncompMB) {
|
||||
// a new MB has been received, update display
|
||||
mNbPrevUncompMB = mNbUncompMB;
|
||||
mByteBuffCpy[0] = mByteBuffer[0];
|
||||
gdk_threads_add_idle (refreshUI_CB, window);
|
||||
}
|
||||
}
|
||||
|
||||
// tty1 is dedicated to trace of M4
|
||||
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]);
|
||||
}
|
||||
}
|
||||
//usleep(500);
|
||||
|
||||
//sleep_ms(1); // give time to UI
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
* Threading: Run Method
|
||||
*******************************************************************************/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret = 0, i, cmd;
|
||||
char FwName[30];
|
||||
strcpy(FIRM_NAME, "how2eldb04140.elf"); //!!! check the name ML
|
||||
/* check if copro is already running */
|
||||
ret = copro_isFwRunning();
|
||||
if (ret) {
|
||||
// check FW name
|
||||
int nameLn = copro_getFwName(FwName);
|
||||
if (FwName[nameLn-1] == 0x0a) {
|
||||
FwName[nameLn-1] = 0x00; // replace \n by \0
|
||||
}
|
||||
if (strcmp(FwName, FIRM_NAME) == 0) {
|
||||
printf("CA7 : %s is already running.\n", FIRM_NAME);
|
||||
goto fwrunning;
|
||||
}else {
|
||||
printf("CA7 : wrong FW running. Try to stop it... \n");
|
||||
if (copro_stopFw()) {
|
||||
printf("CA7 : fails to stop firmware\n");
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setname:
|
||||
/* set the firmware name to load */
|
||||
ret = copro_setFwName(FIRM_NAME);
|
||||
if (ret <= 0) {
|
||||
printf("CA7 : fails to change the firmware name\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* start the firmware */
|
||||
if (copro_startFw()) {
|
||||
printf("CA7 : fails to start firmware\n");
|
||||
goto end;
|
||||
}
|
||||
/* wait for 1 seconds the creation of the virtual ttyRPMSGx */
|
||||
sleep_ms(1000);
|
||||
|
||||
fwrunning:
|
||||
// signal(SIGINT, exit_fct); /* Ctrl-C signal */
|
||||
// signal(SIGTERM, exit_fct); /* kill command */
|
||||
// gettimeofday(&tval_before, NULL); // get current time
|
||||
|
||||
if (pthread_create( &threadTTY, NULL, virtual_tty_thread, NULL) != 0) {
|
||||
printf("CA7 : virtual_tty_thread creation fails\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
// sleep_ms(500); // let tty send the DDR buffer command
|
||||
// if (pthread_create( &threadSDB, NULL, sdb_thread, NULL) != 0) {
|
||||
// printf("CA7 : sdb_thread creation fails\n");
|
||||
// goto end;
|
||||
// }
|
||||
|
||||
/****** new production way => use rpmsg-sdb driver to perform CMA buff allocation ******/
|
||||
|
||||
mMachineState = STATE_READY;
|
||||
mSampFreq_Hz = 4;
|
||||
mSampParmCount = 0;
|
||||
|
||||
// gtk_init (&argc, &argv);
|
||||
|
||||
// if (pthread_create( &threadUI, NULL, ui_thread, NULL) != 0) {
|
||||
// printf("CA7 : ui_thread creation fails\n");
|
||||
// goto end;
|
||||
// }
|
||||
|
||||
// printf("CA7 : Entering in Main loop\n");
|
||||
|
||||
// while (1) {
|
||||
// if (mExitRequested) break;
|
||||
// if (mErrorDetected) {
|
||||
// if (mMachineState >= STATE_SAMPLING_LOW) {
|
||||
// virtual_tty_send_command(strlen("Exit"), "Exit");
|
||||
// if (mErrorDetected == 2) printf("CA7 : ERROR in DDR Buffer order => Stop sampling!!!\n");
|
||||
// //else if (mErrorDetected == 2) printf("CA7 : File System full => Stop sampling!!!\n");
|
||||
// else if (mErrorDetected == 1) printf("CA7 : M4 reported DMA error !!!\n");
|
||||
// mErrorDetected = 0;
|
||||
// mMachineState = STATE_READY;
|
||||
// gdk_threads_add_idle (refreshUI_CB, window);
|
||||
// #if 0
|
||||
// close_raw_file();
|
||||
// #endif
|
||||
// }
|
||||
// }
|
||||
// sleep_ms(1); // give time to UI
|
||||
// }
|
||||
for (i=0;i<NB_BUF;i++){
|
||||
int rc = munmap(mmappedData[i], DATA_BUF_POOL_SIZE);
|
||||
assert(rc == 0);
|
||||
}
|
||||
fMappedData = 0;
|
||||
printf("CA7 : Buffers successfully unmapped\n");
|
||||
|
||||
end:
|
||||
mThreadCancel = 1;
|
||||
sleep_ms(100);
|
||||
/* check if copro is already running */
|
||||
if (copro_isFwRunning()) {
|
||||
printf("CA7 : stop the firmware before exit\n");
|
||||
copro_closeTtyRpmsg(0);
|
||||
copro_closeTtyRpmsg(1);
|
||||
copro_stopFw();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
74
src/proto_messages/RTService.proto
Normal file
74
src/proto_messages/RTService.proto
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ========================= LICENSE =================================
|
||||
*
|
||||
* Copyright (C) 2024 Markus Lehr
|
||||
*
|
||||
+ closed License property of Markus Lehr
|
||||
*
|
||||
* ========================= LICENSE =================================
|
||||
*/
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
option cc_generic_services = true;
|
||||
|
||||
package proto_messages;
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
// RT Service
|
||||
///////////////////////////////////////////////////////
|
||||
message SCreateIn
|
||||
{
|
||||
string name = 1;
|
||||
string id = 2;
|
||||
int64 samplerate = 3;
|
||||
int64 sampleperiod = 4;
|
||||
int64 downtime = 5;
|
||||
string status = 6;
|
||||
}
|
||||
|
||||
message SDeleteIn
|
||||
{
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message SMonStateIn
|
||||
{
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message SMonStatesIn
|
||||
{
|
||||
string query = 1;
|
||||
}
|
||||
|
||||
|
||||
message SSetMonStateIn
|
||||
{
|
||||
string id = 1;
|
||||
string status = 2;
|
||||
}
|
||||
|
||||
|
||||
message SResult
|
||||
{
|
||||
bool result = 1;
|
||||
|
||||
}
|
||||
|
||||
message SStateResult
|
||||
{
|
||||
repeated string result = 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
service RTService
|
||||
{
|
||||
rpc CreateMonitoring(SCreateIn) returns (SResult);
|
||||
rpc DeleteMonitoring (SDeleteIn) returns (SResult);
|
||||
rpc GetMonitoringState (SMonStateIn) returns (SResult);
|
||||
rpc GetMonitoringStates (SMonStatesIn) returns (SStateResult);
|
||||
rpc SetMonitoringState (SSetMonStateIn) returns (SResult);
|
||||
|
||||
}
|
||||
86
src/rt_service.cpp
Normal file
86
src/rt_service.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <boost/python.hpp>
|
||||
#include <boost/python/extract.hpp>
|
||||
#include <string>
|
||||
#include "../include/RTSMonitoringTask.h"
|
||||
#include "../include/RelGILLock.h"
|
||||
//#include "../include/GILLock.h"
|
||||
//#include <sstream>
|
||||
//#include <vector>
|
||||
|
||||
|
||||
|
||||
struct RTService
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
|
||||
RTSMonitoringTask rtsMonTask;
|
||||
|
||||
public:
|
||||
|
||||
// RT_Service(/* args */) {
|
||||
// }
|
||||
|
||||
// ~RT_Service() {}
|
||||
|
||||
// we need all parameters like id, samplerate, sampletime, downtime, later: HW-channels with the configurations
|
||||
bool createMonitoring(std::string id, int samplerate, int sampleperiod, int downtime, std::string status, boost::python::object callback) {
|
||||
PyRelinquishGIL scoped;
|
||||
//TODO ML: add this to the M4Core
|
||||
if (rtsMonTask.Init()) {
|
||||
return rtsMonTask.CreateMonitoring(id, samplerate, sampleperiod, downtime, status, callback);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool deleteMonitoring(std::string id) {
|
||||
//TODO ML: add this to the M4Core
|
||||
PyRelinquishGIL scoped;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string getMonitoringState(std::string id) {
|
||||
//TODO ask the M4Core for the Monitpring Status
|
||||
PyRelinquishGIL scoped;
|
||||
return "Start";
|
||||
}
|
||||
|
||||
boost::python::list getMonitoringStates() {
|
||||
//TODO ask the M4Core for the Monitpring Status
|
||||
PyRelinquishGIL scoped;
|
||||
boost::python::list list;
|
||||
return list;
|
||||
}
|
||||
|
||||
bool setMonitoringStatus(std::string id, std::string status) {
|
||||
//set the Status
|
||||
PyRelinquishGIL scoped;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
BOOST_PYTHON_MODULE(rt_service)
|
||||
{
|
||||
class_<RTService>("RT_Service")
|
||||
.def("createMonitoring", &RTService::createMonitoring)
|
||||
.def("getMonitoringStatus", &RTService::getMonitoringState)
|
||||
.def("getAllMonitoringStat", &RTService::getMonitoringStates)
|
||||
.def("setMonitoringStatus", &RTService::setMonitoringStatus)
|
||||
;
|
||||
};
|
||||
|
||||
|
||||
|
||||
//examples
|
||||
// long l = len(msgs);
|
||||
// std::stringstream ss;
|
||||
// for (long i = 0; i<l; ++i) {
|
||||
// if (i>0) ss << ", ";
|
||||
// std::string s = boost::python::extract<std::string>(msgs[i]);
|
||||
// ss << s;
|
||||
// }
|
||||
// mMsg = ss.str();
|
||||
14
src/utilities/GILLock.cpp
Normal file
14
src/utilities/GILLock.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
#include "../../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);
|
||||
}
|
||||
298
src/utilities/RTSCoproHelper.cpp
Normal file
298
src/utilities/RTSCoproHelper.cpp
Normal file
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#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
|
||||
|
||||
|
||||
|
||||
RTSCoproHelper::RTSCoproHelper(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
RTSCoproHelper::~RTSCoproHelper()
|
||||
{
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Init(std::string fwPath, std::string fwName) {
|
||||
if (this->Copro_isFwRunning() != 0){
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
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(char* pathStr)
|
||||
{
|
||||
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/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(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, int modeRaw)
|
||||
{
|
||||
struct termios tiorpmsg;
|
||||
char devName[50];
|
||||
sprintf(devName, "/dev/ttyRPMSG%d", ttyNb%2);
|
||||
mFdRpmsg[ttyNb%2] = open(devName, O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
if (mFdRpmsg[ttyNb%2] < 0) {
|
||||
printf("CA7 : Error opening ttyRPMSG%d, err=-%d\n", ttyNb%2, errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
#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", 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);
|
||||
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
|
||||
*********************************************************************************/
|
||||
16
src/utilities/RelGILLock.cpp
Normal file
16
src/utilities/RelGILLock.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
#include "../../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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user