#include #include #include "../common/comm.h" #include "../common/iobuffer.h" #include "api.h" #include "protocol.pb.h" #include "message_queue.h" #include #include #include #include #include #include //#include "modbus/modbus-tcp.h" /* #include #include #include #include #include #include */ //#define command "echo \"nvidia\" | sudo -S /sbin/ip link set can0 type can bitrate 500000" //#define up "echo \"nvidia\" | sudo -S ifconfig can0 up" //#define down "echo \"nvidia\" | sudo -S ifconfig can0 down" /* extern "C" { #include "/usr/local/ffmpeg/include/libavcodec/avcodec.h" //#include "/usr/local/ffmpeg/include/libavutil/time.h" //#include "/usr/local/ffmpeg/include/libavutil/opt.h" //#include "/usr/local/ffmpeg/include/libavutil/imgutils.h" //#include "/usr/local/x264/include/x264.h" } */ //#include //#include //20230413 MQTT //#include "../thirdparty/Mqtt/include/MQTTAsync.h" //#include "../thirdparty/jsoncpp/include/json/json.h" // //MQTTAsync mqttClient; ////static const char* subTopic = "hello1"; //订阅主题 // // //void sendMessage(char* data, int qos ,const char * pubTopic) //{ // int rc; // MQTTAsync_message pubmsg = MQTTAsync_message_initializer; // MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; // // opts.context = mqttClient; // // pubmsg.payload = data; // pubmsg.payloadlen = strlen(data); // pubmsg.qos = qos; // printf("mqtt send: %s\n", (char*)pubmsg.payload); // // if ((rc = MQTTAsync_sendMessage(mqttClient, pubTopic, &pubmsg, &opts)) != MQTTASYNC_SUCCESS) // { // printf("MQTTAsync_sendMessage() fail, error code:%d\n", rc); // } //} // //void onConnectCallCBack(void* context, char* cause) //连接成功callback //{ // //printf("Connection successful.\n"); // //int rc, qos; // //MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; // // //qos = 0; // //opts.context = mqttClient; // // //if ((rc = MQTTAsync_subscribe(mqttClient, subTopic, qos, &opts)) != MQTTASYNC_SUCCESS) //subTopic // //{ // // printf("MQTTAsync_subscribe() fail, error code:%d\n", rc); // //} // // printf("Connection successful.\n"); // int rc; // const int qos[2] = {0,1}; // MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; // // char* const subTopic[2] = { "hello" ,"hello1"}; //订阅主题 // // opts.context = mqttClient; // // if ((rc = MQTTAsync_subscribeMany(mqttClient,2,subTopic, qos, &opts)) != MQTTASYNC_SUCCESS) //subTopic // { // printf("MQTTAsync_subscribe() fail, error code:%d\n", rc); // } //} // //void onConnectFailure(void* context, MQTTAsync_failureData* response) //连接失败callback //{ // printf("connection fail, error code: %d, error message:%s\n", response ? response->code : 0, response ? response->message : 0); //} // //void Disconnect(void* context, char* cause) //连接断开callback //{ // printf("disconnect, cause: %s\n", cause); //} // //int RecevieMessage(void* context, char* pubTopic, int topicLen, MQTTAsync_message* message) //{ // char buf[message->payloadlen + 1]; // // memcpy(buf, message->payload, message->payloadlen); // printf("mqtt recv: %s\n", buf); // MQTTAsync_freeMessage(&message); // MQTTAsync_free(pubTopic); // // return 1; //} void daemonize(void) { signal(SIGTTOU, SIG_IGN); signal(SIGTTIN, SIG_IGN); signal(SIGTSTP, SIG_IGN); if (0 != fork()) exit(0); if (-1 == setsid()) exit(0); signal(SIGHUP, SIG_IGN); if (0 != fork()) exit(0); //if (0 != chdir("/")) exit(0); } bool checkOnly() { const char filename[] = "./lockfile"; int fd = open (filename, O_WRONLY | O_CREAT , 0644); int flock = lockf(fd, F_TLOCK, 0 ); if (fd == -1) { return false; } //给文件加锁 if (flock == -1) { return false; } //程序退出后,文件自动解锁 return true; } std::string getpath() { char exec_name [BUFSIZ]; readlink ("/proc/self/exe", exec_name, BUFSIZ); int32_t len=strlen(exec_name); for(int i=len;i>=0;i--) { if(exec_name[i]=='/') { exec_name[i]=0; break; } } return std::string(exec_name); } //const char *inFileName = "/home/zhouchao/2.yuv"; //const char *outFileName = "/home/zhouchao/3.h264"; /* int encode(AVCodecContext* codecContent, AVPacket* packet, AVFrame* frame, FILE* outFile) { //编码 int ret = avcodec_send_frame(codecContent, frame); if (ret < 0) printf("Error sending a frame for encoding\r\n"); while (ret == 0) { ret = avcodec_receive_packet(codecContent, packet); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { return 0; } else if (ret < 0) printf("Error encoding video frame\r\n"); if (ret == 0) fwrite(packet->data, 1, packet->size, outFile); } } */ int main(int argc,char *argv[]) { // daemonize(); auto path=getpath(); std::cout<type==SENSORS_FEATURE_TEMP) { auto sf = sensors_get_subfeature(chip, feature,SENSORS_SUBFEATURE_TEMP_INPUT); if(sf->type == SENSORS_SUBFEATURE_TEMP_INPUT) { sensors_get_value(chip, sf->number,&temp); //temp = temp * (9.0F / 5.0F) + 32.0F; std::cout << "System_Temp : " << temp << std::endl; } } } } }m sensors_cleanup(); */ //20231106-YUV转h.264 /* AVCodec* codec = NULL; //AVCodecContext* codecContent = NULL; //AVPacket* packet = NULL; //AVFrame* frame = NULL; FILE* inFile = nullptr; FILE* outFile = nullptr; //查找指定编码器 codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (codec == NULL) printf("could not find h264 encoder!\r\n"); else printf("could find h264 encoder!\r\n"); */ /* //申请编码器上下文 codecContent = avcodec_alloc_context3(codec); if (codecContent == nullptr) printf("could not alloc h264 content!\r\n"); //必设参数 codecContent->width = 1280; codecContent->height = 720; codecContent->time_base = AVRational{ 1, 25 }; codecContent->max_b_frames = 1; codecContent->pix_fmt = AV_PIX_FMT_YUV420P; codecContent->gop_size = 10; //关键帧间隔,默认250 codecContent->framerate = AVRational{ 25, 1 }; //初始化编码器上下文 ret = avcodec_open2(codecContent, codec, NULL); if (ret < 0) printf("Could not open codec: \r\n"); packet = av_packet_alloc(); if (packet == nullptr) printf("alloc packet error\r\n"); frame = av_frame_alloc(); if (packet == nullptr) printf("alloc frame error\r\n"); //必设参数 frame->width = codecContent->width; frame->height = codecContent->height; frame->format = codecContent->pix_fmt; //申请视频数据存储空间 ret = av_frame_get_buffer(frame, 0); if (ret) printf("alloc frame buffer error!\r\n"); inFile = fopen(inFileName, "rb"); if (inFile == nullptr) printf("error to open inFile: %s\r\n", inFileName); outFile = fopen(outFileName, "wb+"); if (inFile == nullptr) printf("error to open outFile: %s\n", outFileName); int framecount = 0; frame->pts = 0; int start_time = av_gettime() / 1000; //毫秒级 while (!feof(inFile)) { ret = av_frame_is_writable(frame); if (ret < 0) ret = av_frame_make_writable(frame); fread(frame->data[0], 1, frame->width * frame->height, inFile); //y fread(frame->data[1], 1, frame->width * frame->height / 4, inFile); //u fread(frame->data[2], 1, frame->width * frame->height / 4, inFile); //v printf("encode frame num: %d\n", ++framecount); frame->pts += 1000 / (codecContent->time_base.den / codecContent->time_base.num); encode(codecContent, packet, frame, outFile); } encode(codecContent, packet, nullptr, outFile); printf("encode time cost: %d ms\n ", av_gettime() / 1000 - start_time); av_packet_free(&packet); av_frame_free(&frame); avcodec_free_context(&codecContent); fclose(inFile); fclose(outFile); */ //20231127 can //system(down); //system(command); //system(up); /* struct ifreq ifr = {0}; struct sockaddr_can can_addr = {0}; struct can_frame Sendframe = {0},Reciveframe = {0}; int sockfd = -1; int ret,nbytes = 0,len = 0; sockfd = socket(PF_CAN, SOCK_RAW, CAN_RAW); if(0 > sockfd) printf("socket error\r\n"); strcpy(ifr.ifr_name, "can0"); ioctl(sockfd, SIOCGIFINDEX, &ifr); can_addr.can_family = AF_CAN; can_addr.can_ifindex = ifr.ifr_ifindex; ret = bind(sockfd, (struct sockaddr *)&can_addr, sizeof(can_addr)); if (0 > ret) { printf("bind error\r\n"); close(sockfd); } int loopback = 0; setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback)); int ro = 1; setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, &ro, sizeof(ro)); Sendframe.data[0] = 0xB1; Sendframe.data[1] = 0x10; Sendframe.data[2] = 0xFF; Sendframe.can_dlc = 3; Sendframe.can_id = 0x601; ret = write(sockfd, &Sendframe, sizeof(Sendframe)); if(sizeof(Sendframe) != ret) printf("write error\r\n"); int flag = fcntl(sockfd, F_GETFL, 0); if (flag < 0) { printf("fcntl F_GETFL fail\r\n"); } if (fcntl(sockfd, F_SETFL, flag | O_NONBLOCK) < 0) printf("fcntl F_SETFL fail\r\n"); while(true) { struct timeval timeout = {0,0}; timeout.tv_usec = 20000; fd_set fds; FD_ZERO(&fds); FD_SET(sockfd, &fds); int err = select(sockfd + 1, &fds, NULL, NULL, &timeout); if (err != -1 && FD_ISSET(sockfd, &fds)) { nbytes = read(sockfd, &Reciveframe, sizeof(Reciveframe)); if(nbytes > 0) printf("CAN frame:\nID = %x\nDLC = %x\nDATA = %s\n", Reciveframe.can_id,Reciveframe.can_dlc, Reciveframe.data); } else std::cout << "111" << std::endl; } close(sockfd); */ /* int fd; fd = open("/home/nvidia/Log.txt",O_CREAT | O_RDWR,0777); if(fd < 0) std::cout << "open /home/nvidia/Log.txt error" << std::endl; else std::cout << "open /home/nvidia/Log.txt suss" << std::endl; write(fd,"444",3); close(fd); */ //20231127 modbus tcp /* modbus_t *mb; uint8_t dest[1024]; //setup memory for data uint16_t * dest16 = (uint16_t *) dest; mb = modbus_new_tcp("127.0.0.1", 502); int ret = modbus_set_slave(mb, 1); ret = modbus_set_debug(mb,1); if(!ret) std::cout << "Set Slave ID ok" << std::endl; if (modbus_connect(mb) == -1) modbus_free(mb); else { std::cout << "connect Slave ok" << std::endl; ret = modbus_set_response_timeout(mb, 1, 200000); modbus_read_input_registers(mb,0x10A,1,dest16); modbus_close(mb); modbus_free(mb); } */ CMessageQueue Q; Q.Create(); while(true) { Q.Process(); } //gtk_main(); return 0; }