‏הצגת רשומות עם תוויות Linux User space. הצג את כל הרשומות
‏הצגת רשומות עם תוויות Linux User space. הצג את כל הרשומות

יום שבת, 26 באפריל 2014

Displaying video and annotations using QT

The following code demonstrates displaying video and annotations using QT .
Capture.PNG3
The project is based on QT and libv4l2 library.

The QT project structure in the QT designer :
Capture3
The code :
capturethread.h

class CaptureThread : public QThread
{
public:
    explicit CaptureThread(QWidget *parent = 0);
    ~CaptureThread();
    bool devam;
    videowidget *parent;
    struct v4l2_format              fmt;
    struct v4l2_buffer              buf;
    struct v4l2_requestbuffers      req;
    enum v4l2_buf_type              type;
    fd_set                          fds;
    struct timeval                  tv;
    int                             r, fd;
    unsigned int                    n_buffers;
    char                            *dev_name;
    char                            out_name[256];
    FILE                            *fout;
    struct buffer {
            void   *start;
            size_t length;
    };
    static void xioctl(int fh, int request, void *arg)
    {
            int r;
            do {
                    r = v4l2_ioctl(fh, request, arg);
            } while (r == -1 && ((errno == EINTR) || (errno == EAGAIN)));
            if (r == -1) {
                    fprintf(stderr, "error %d, %s\n", errno, strerror(errno));
                    return;
            }
    };
    void run();
    struct buffer  *buffers;
    void stopUlan();
    void startUlan();
};
#endif // CAPTURETHREAD_H

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private slots:
    void on_pushButton_clicked();
private:
    Ui::MainWindow *ui;
    void pushButtonClicked();
};
#endif // MAINWINDOW_H

videowidget.h


#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H
#include <QWidget>
#include <QPainter>
class videowidget : public QWidget
{
Q_OBJECT
public:
    explicit videowidget(QWidget *parent = 0);
    QImage img;
protected:
    void paintEvent(QPaintEvent *event);
signals:
public slots:
};
#endif // VIDEOWIDGET_H

capturethread.cpp


#include <QApplication>
#include <QDataStream>
#include <QString>
#include <QDebug>
#include <QBuffer>
#include <typeinfo>
#include <iostream>
#include <QFile>
#include "videowidget.h"
#include "capturethread.h"
CaptureThread::CaptureThread(QWidget *parent) :
    QThread()
{
    //qDebug()<<parent->objectName();
    this->parent=(videowidget*)parent;
    devam=false;
    fd = -1;
}
void CaptureThread::run(){
//do real stuff
fd = -1;
dev_name = "/dev/video0";
    fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
    if (fd < 0) {
           qDebug("Cannot open device");
           //exit(EXIT_FAILURE);
           return;
    }
    static struct v4lconvert_data *v4lconvert_data;
    static struct v4l2_format src_fmt;
    static unsigned char *dst_buf;
    CLEAR(fmt);
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    fmt.fmt.pix.width       = 640;
    fmt.fmt.pix.height      = 480;
    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
    fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;
    xioctl(fd, VIDIOC_S_FMT, &fmt);
    if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) {
           printf("Libv4l didn't accept RGB24 format. Can't proceed.\n");
           //exit(EXIT_FAILURE);
           return;
    }
    if ((fmt.fmt.pix.width != 640) || (fmt.fmt.pix.height != 480))
           printf("Warning: driver is sending image at %dx%d\n",
                   fmt.fmt.pix.width, fmt.fmt.pix.height);
    v4lconvert_data = v4lconvert_create(fd);
    if (v4lconvert_data == NULL)
        qDebug("v4lconvert_create");
    if (v4lconvert_try_format(v4lconvert_data, &fmt, &src_fmt) != 0)
        qDebug("v4lconvert_try_format");
    xioctl(fd, VIDIOC_S_FMT, &src_fmt);
    dst_buf = (unsigned char*)malloc(fmt.fmt.pix.sizeimage);
    CLEAR(req);
    req.count = 2;
    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    req.memory = V4L2_MEMORY_MMAP;
    xioctl(fd, VIDIOC_REQBUFS, &req);
    buffers = (buffer*)calloc(req.count, sizeof(*buffers));
    for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
           CLEAR(buf);
           buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
           buf.memory      = V4L2_MEMORY_MMAP;
           buf.index       = n_buffers;
           xioctl(fd, VIDIOC_QUERYBUF, &buf);
           buffers[n_buffers].length = buf.length;
           buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
                         PROT_READ | PROT_WRITE, MAP_SHARED,
                         fd, buf.m.offset);
           if (MAP_FAILED == buffers[n_buffers].start) {
                   qDebug("mmap");
                   //exit(EXIT_FAILURE);
                   return;
           }
    }
    for (int i = 0; i < n_buffers; ++i) {
           CLEAR(buf);
           buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
           buf.memory = V4L2_MEMORY_MMAP;
           buf.index = i;
           xioctl(fd, VIDIOC_QBUF, &buf);
    }
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    xioctl(fd, VIDIOC_STREAMON, &type);
    int di=0;
    char header[]="P6\n640 480 255\n";
    devam = true ;
    while(devam){
        /* bu döngü datanın birikmesini sağlıyor */
        do {
                FD_ZERO(&fds);
                FD_SET(fd, &fds);
                /* Timeout. */
                tv.tv_sec = 2;
                tv.tv_usec = 0;
                r = select(fd + 1, &fds, NULL, NULL, &tv);
        } while ((r == -1 && (errno = EINTR)));
        if (r == -1) {
                qDebug("select");
                //exit(1) ;
                return;
        }
        CLEAR(buf);
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;
        xioctl(fd, VIDIOC_DQBUF, &buf);
        try{
            
        if (v4lconvert_convert(v4lconvert_data,
                                &src_fmt,
                                &fmt,
                                (unsigned char*)buffers[buf.index].start, buf.bytesused,
                                dst_buf, fmt.fmt.pix.sizeimage) < 0) {
                if (errno != EAGAIN)
                        qDebug("v4l_convert");
        }
        unsigned char* asil=(unsigned char*)malloc(fmt.fmt.pix.sizeimage+qstrlen(header));
        memmove(asil, dst_buf, fmt.fmt.pix.sizeimage);
        memmove(asil+qstrlen(header), asil, fmt.fmt.pix.sizeimage);
        memcpy(asil,header,qstrlen(header));
        QImage qq;//=new QImage(dst_buf,640,480,QImage::Format_RGB32);
        if(qq.loadFromData(asil,fmt.fmt.pix.sizeimage+qstrlen(header),"PPM")){
            if(parent->isVisible()){
                QImage q1(qq);
                parent->img=q1;
                parent->update();
              //this->msleep(50);
            }
        //qApp->processEvents();
            if(asil)
                free(asil);
        }
        }catch(...){}
        xioctl(fd, VIDIOC_QBUF, &buf);
        di++;
   }
    try{
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    xioctl(fd, VIDIOC_STREAMOFF, &type);
    for (int i = 0; i < n_buffers; ++i)
           v4l2_munmap(buffers[i].start, buffers[i].length);
        v4l2_close(fd);
    }catch(...){}
}
CaptureThread::~CaptureThread()
{
    try{
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    xioctl(fd, VIDIOC_STREAMOFF, &type);
    /*for (int i = 0; i < n_buffers; ++i)
           v4l2_munmap(buffers[i].start, buffers[i].length);*/
        v4l2_close(fd);
    }catch(...){}
    fd = -1;
}
void CaptureThread::stopUlan()
{
    devam=false;
    try{
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    xioctl(fd, VIDIOC_STREAMOFF, &type);
    /*for (int i = 0; i < n_buffers; ++i)
           v4l2_munmap(buffers[i].start, buffers[i].length);*/
        v4l2_close(fd);
    }catch(...){}
    fd = -1;
}
void CaptureThread::startUlan()
{
    this->start();
}
/*
void CaptureThread::onStopCapture()
{
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    xioctl(fd, VIDIOC_STREAMOFF, &type);
    for (int i = 0; i < n_buffers; ++i)
           v4l2_munmap(buffers[i].start, buffers[i].length);
    v4l2_close(fd);
}*/

main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "videowidget.h"
#include "capturethread.h"
#include <QtGui>
#include <QtGui/QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton->connect( ui->pushButton, SIGNAL( clicked() ), this, SLOT( pushButtonClicked() )  );
    connect ( ui->pushButton, SIGNAL( clicked() ), this, SLOT( pushButtonClicked() ) );
    QVBoxLayout* layout = new QVBoxLayout();
         layout->addWidget(new videowidget (ui->myFrame));
         ui->myFrame ->setLayout(layout);
         ui->myFrame->show();
}
void MainWindow::pushButtonClicked() // defined in .h under public/private slots:
{
    QMessageBox::information( this, "Information", "Just clicked Ui PushButton" ); // #include <QtGui/QMessageBox>
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    QMessageBox::information( this, "Information", "Just clicked Ui PushButton" ); // #include <QtGui/QMessageBox>
}

videowidget.cpp


#include "videowidget.h"
#include "capturethread.h"
videowidget::videowidget(QWidget *parent) :
    QWidget(parent)
{
    setAutoFillBackground(true);
    //setObjectName("video göstergeci");
    CaptureThread * theCaptureThread = new CaptureThread (this);
    theCaptureThread->start ();
}
void videowidget::paintEvent(QPaintEvent *event) {
     try{
    QPainter painter(this);
        if(!img.isNull()){
            painter.drawImage(QPoint(0,0), img);
            painter.setPen(Qt::blue);
            painter.setFont(QFont("Arial", 30));
            painter.drawText(rect(), Qt::AlignCenter, "Qt");
        }
    }catch(...){}
}

The layout file :
Capture4
Note in order to capture the screen in linux ubuntu use the :
gnome-screenshot
command in bash shell‏



 


Check if tcp port is open using scapy

The simple way to scan the open port
sudo nmap -sS -O 192.168.0.1

The following python script  checks if a port is open using scapy .

import logging
import sys
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "192.168.0.1"
src_port = 400
dst_port=80
 
tcp_connect_scan_resp = sr1(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=3)
if(tcp_connect_scan_resp is None):
    print ("The port is Closed")
    sys.exit()
     
print ("The flags:" + str (tcp_connect_scan_resp.getlayer(TCP).flags))    
    
if(tcp_connect_scan_resp.haslayer(TCP)):
    if(tcp_connect_scan_resp.getlayer(TCP).flags == 0x12):
        #send_rst = sr(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="AR"),timeout=3)
        print ("The port is Open")
        sys.exit ();
            
#(tcp_connect_scan_resp.getlayer(TCP).flags == 0x14):
print ("The port is Closed ")

notes:
Currently scapy supports only Python 2.7 .
Needed sudo privilege in order to execute script with scapy .

The results:
zvika@ubuntu:~/myStaff/myCode$ sudo python PortsScan.py
Begin emission:
..Finished to send 1 packets.
*
Received 3 packets, got 1 answers, remaining 0 packets
The flags:18
The port is Open


The code is based on :
http://resources.infosecinstitute.com/port-scanning-using-scapy/

Other good references : 
http://theitgeekchronicles.files.wordpress.com/2012/05/scapyguide1.pdf
http://thesprawl.org/research/scapy/
http://thepacketgeek.com/scapy-p-06-sending-and-receiving-with-scapy/

יום שבת, 18 במאי 2013

Turning on aled on beagleboard from user space

Linux kernel documentation here.
Using MMap

void TurnOnLED ()
{
   int fd = open("/dev/mem", O_RDWR);
    volatile ulong *A;
    if (fd < 0)
    {
        printf("Could not open /dev/mem\n");
        return;
    }
    A = (ulong*) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE,
            MAP_SHARED, fd, 0x49050000);
    if (A == MAP_FAILED)
    {
        printf("Mapping IO failed\n");
        close(fd);
        return;
    }
    A[0x603C / 4] |= 0x600000;  
    munmap(A, 0x10000);
    close (fd);
}

the use of "volatile"  tells the compiler that each use of the variable A in the program must result in an actual load or store instruction.

Using sysfs
#Export the led pin:
echo 139 > /sys/class/gpio/export

for i in `seq 1 10`;
do
    #Turn on the led
    echo "high" >  /sys/class/gpio/gpio139/direction
    sleep 1
    #Turn off the led
    echo "low"  > /sys/class/gpio/gpio139/direction
    sleep 1
done
       
#unexport the led pin
echo 139 > /sys/class/gpio/unexport


calculating the pin io instruction can be found in the following video: