Blame view

app/gui/packet/mainwindow.cpp 3.47 KB
81bee1ec4   김태훈   원격 패킷 출력 프로그램 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  #include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  #include <QTableWidgetItem>
  
  #include "tablevalue.h"
  
  typedef struct {
      int header;
      char body[];
  } STRUCT_PACK packet_t;
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui->setupUi(this);
      sock = new QUdpSocket(this);
      if (!sock->bind(4000))
          exit(EXIT_FAILURE);
  
      connect(sock, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
  
      ui->controlTable->setRowCount(sizeof(oven_control_t) / sizeof(U16));
      ui->controlTable->setColumnCount(3);
      ui->controlTable->setHorizontalHeaderLabels(QString("Address,Value,Description").split(","));
  
      for (int row = 0; row < ui->controlTable->rowCount(); row++)
      {
          ui->controlTable->setItem(row, 0, new QTableWidgetItem(QString("").sprintf("0x%04X", row)));
  
          TableValue *item = new TableValue;
          item->setText("0x0000");
          ui->controlTable->setCellWidget(row, 1, item);
      }
  
      ui->stateTable->setRowCount(sizeof(oven_state_t) / sizeof(U16));
      ui->stateTable->setColumnCount(3);
      ui->stateTable->setHorizontalHeaderLabels(QString("Address,Value,Description").split(","));
  
      for (int row = 0; row < ui->stateTable->rowCount(); row++)
      {
          ui->stateTable->setItem(row, 0, new QTableWidgetItem(QString().sprintf("0x%04X", row)));
  
          TableValue *item = new TableValue;
          item->setText("0x0000");
          ui->stateTable->setCellWidget(row, 1, item);
      }
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  void MainWindow::readPendingDatagrams()
  {
      while (sock->hasPendingDatagrams()) {
          QByteArray datagram;
          datagram.resize(sock->pendingDatagramSize());
          QHostAddress sender;
          quint16 senderPort;
  
          sock->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
  
          processDatagram(datagram);
      }
  }
  
  void MainWindow::processDatagram(QByteArray &datagram)
  {
      packet_t *packet = (packet_t *) datagram.data();
      switch (packet->header)
      {
      case HDR_OVEN_CONTROL:
          processControl((oven_control_t *) packet->body);
          break;
      case HDR_OVEN_STATE:
          processState((oven_state_t *) packet->body);
          break;
      case HDR_ERROR_CODE:
          break;
      }
  }
  
  void MainWindow::processControl(oven_control_t *control)
  {
      if (memcmp(&this->control, control, sizeof(this->control)))
          updateControl(control);
  }
  
  void MainWindow::processState(oven_state_t *state)
  {
      if (memcmp(&this->state, state, sizeof(this->state)))
          updateState(state);
  }
  
  void MainWindow::updateControl(oven_control_t *control)
  {
      U16 *base = (U16 *) &this->control;
      U16 *operand = (U16 *) control;
      for (int row = 0; row < ui->controlTable->rowCount(); row++)
      {
          U16 b = *(base + row);
          U16 o = *(operand + row);
          if (b != o)
          {
              *(base + row) = o;
              TableValue *val = (TableValue *) ui->controlTable->cellWidget(row, 1);
              val->setText(QString().sprintf("0x%04X", o));
          }
      }
  }
  
  void MainWindow::updateState(oven_state_t *state)
  {
      U16 *base = (U16 *) &this->state;
      U16 *operand = (U16 *) state;
      for (int row = 0; row < ui->stateTable->rowCount(); row++)
      {
          U16 b = *(base + row);
          U16 o = *(operand + row);
          if (b != o)
          {
              *(base + row) = o;
              TableValue *val = (TableValue *) ui->stateTable->cellWidget(row, 1);
              val->setText(QString().sprintf("0x%04X", o));
          }
      }
  }