提交 715e60fd authored 作者: blu's avatar blu

test

上级 f59cb819
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, string> m;
m.emplace("a","b");
m.emplace("a","c");
for(auto&[k,v]:m) {
cout <<k <<v<<endl;
}
}
\ No newline at end of file
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
string s = " I am seprated by spaces ";
cout <<s;
}
\ No newline at end of file
#include "reverse_tun.hpp"
int main()
{
thread th;
spdlog::info("port remote: {}", createReverseTun("47.56.83.236", 2222, "root", "Hz123456", th));
th.join();
}
\ No newline at end of file
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h> // needed for memset
#include <thread>
#include <chrono>
#include <signal.h>
#include <spdlog/spdlog.h>
#include <fmt/format.h>
using namespace std;
using namespace std::chrono_literals;
#define WATCHDOG_STOP uint8_t(0xFD)
#define WATCH
int watchdog_init(string ttyPath)
{
const char *tty = ttyPath.c_str();
struct termios tio;
int tty_fd;
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(tty, O_RDWR | O_NONBLOCK);
if(tty_fd == -1) {
spdlog::error("evdaemon failed to open tty {}", tty);
return -1;
}
cfsetospeed(&tio,B115200); // 115200 baud
//cfsetispeed(&tio,B115200);
if(-1 == tcsetattr(tty_fd,TCSANOW,&tio)){
spdlog::error("evdaemon failed to set tty {}", tty);
return -1;
}
return tty_fd;
}
int watchdog_feed(int fd, uint8_t intervalMs){
if(fd <=0) {
spdlog::error("evdaemon invalid watchdog fd");
return -1;
}
uint8_t check = ~intervalMs;
uint8_t data[] = {0xFE, 0xFE, intervalMs, check, 0xEF};
spdlog::info("evdaemon feed watch dog: {}, {}", intervalMs, check);
if( -1 == write(fd,data,5)){
spdlog::error("evdaemon failed to write watchdog fd");
return -1;
}else{
return 0;
}
}
int _watchdog_stop(int fd){
return watchdog_feed(fd, WATCHDOG_STOP);
}
int watchdog_close(int fd) {
spdlog::info("evdaemon disabling watch dog..");
if(fd > 0) {
_watchdog_stop(fd);
close(fd);
}
return 0;
}
int fd_dog = 0;
bool watchdog_disable= false;
void sig_handler( int signum) {
spdlog::warn("evdaemon received control signal {}, waiting to stop watch dog", signum);
if(fd_dog > 0 && !watchdog_disable) {
watchdog_disable = true;
while(fd_dog != 0) {
spdlog::info("evdaemon waiting watchdog to stop");
this_thread::sleep_for(chrono::milliseconds(500));
}
}
spdlog::info("evdaemon watchdog stopped, exiting");
exit(signum);
}
int main(int argc,char** argv){
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
thread th = thread([](){
fd_dog = watchdog_init("/dev/ttyS1");
uint8_t intval = 10;
if(fd_dog > 0) {
while(!watchdog_disable) {
watchdog_feed(fd_dog, intval);
this_thread::sleep_for(chrono::milliseconds(intval/2*100));
}
watchdog_close(fd_dog);
fd_dog = 0;
}else{
fd_dog = 0;
}
});
th.join();
return 0;
}
\ No newline at end of file
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h> // needed for memset
#include <thread>
#include <chrono>
#include <signal.h>
#include <spdlog/spdlog.h>
#include <fmt/format.h>
using namespace std;
using namespace std::chrono_literals;
#define EV_WATCHDOG_STOP uint8_t(0xFD)
class EvWatchDog {
private:
int fd_dog = 0;
string tty;
bool disabled = false;
int watchdog_init(string ttyPath)
{
const char *tty = ttyPath.c_str();
struct termios tio;
int tty_fd;
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(tty, O_RDWR | O_NONBLOCK);
if(tty_fd == -1) {
spdlog::error("evdaemon failed to open tty {}", tty);
return -1;
}
cfsetospeed(&tio,B115200); // 115200 baud
//cfsetispeed(&tio,B115200);
if(-1 == tcsetattr(tty_fd,TCSANOW,&tio)){
spdlog::error("evdaemon failed to set tty {}", tty);
return -1;
}
return tty_fd;
}
int watchdog_feed(int fd, uint8_t intervalMs){
if(fd <=0) {
spdlog::error("evdaemon invalid watchdog fd");
return -1;
}
uint8_t check = ~intervalMs;
uint8_t data[] = {0xFE, 0xFE, intervalMs, check, 0xEF};
spdlog::info("evdaemon feed watch dog: {}, {}", intervalMs, check);
if( -1 == write(fd,data,5)){
spdlog::error("evdaemon failed to write watchdog fd");
return -1;
}else{
return 0;
}
}
int _EV_WATCHDOG_STOP(int fd){
return watchdog_feed(fd, EV_WATCHDOG_STOP);
}
int watchdog_close(int fd) {
spdlog::info("evdaemon disabling watch dog..");
if(fd > 0) {
_EV_WATCHDOG_STOP(fd);
close(fd);
}
return 0;
}
public:
EvWatchDog() = delete;
EvWatchDog(string tty):tty(tty){
if((fd_dog = watchdog_init(tty)) < 0) {
spdlog::error("evdaemon can't open watchdog tty, ignore watchdog");
fd_dog = 0;
}
}
thread run(){
thread th = thread([this](){
uint8_t intval = 10;
if(this->fd_dog > 0) {
while(!this->disabled) {
watchdog_feed(this->fd_dog, intval);
this_thread::sleep_for(chrono::milliseconds(intval/2*100));
}
watchdog_close(this->fd_dog);
this->fd_dog = 0;
}else{
this->fd_dog = 0;
}
});
return th;
}
void stop(){
if(fd_dog > 0 && !disabled) {
disabled = true;
while(fd_dog != 0) {
spdlog::info("evdaemon waiting watchdog to stop");
this_thread::sleep_for(chrono::milliseconds(500));
}
}
}
};
EvWatchDog gWatchDog = EvWatchDog("/dev/ttyS1");
void sig_handler( int signum) {
spdlog::warn("evdaemon received control signal {}, waiting to stop watch dog", signum);
gWatchDog.stop();
spdlog::info("evdaemon watchdog stopped, exiting");
exit(signum);
}
int main(int argc,char** argv){
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
gWatchDog.run().join();
return 0;
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论