Blame view

app/gui/oven_control/udphandler.cpp 2.64 KB
8c2952457   김태훈   응용 프로그램 추가
1
2
3
  #include "udphandler.h"
  
  #include <QtDebug>
81bee1ec4   김태훈   원격 패킷 출력 프로그램 추가
4
  #define IPC_UDP_SYS_HOST    "127.0.0.1"
8c2952457   김태훈   응용 프로그램 추가
5
6
7
8
9
10
11
12
13
14
  #define IPC_UDP_SYS_PORT    4001
  #define IPC_UDP_GUI_PORT    4000
  
  typedef struct {
      int header;
      char body[];
  } STRUCT_PACK packet_t;
  
  UdpHandler::UdpHandler(QObject *parent) : QObject(parent)
  {
6a965b9f1   고영탁   엔지니어 모드 2차 구현
15
16
      bzero(&control, sizeof(control));
      bzero(&state, sizeof(state));
8c2952457   김태훈   응용 프로그램 추가
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
  }
  
  bool UdpHandler::init()
  {
      sock = new QUdpSocket(this);
      if (!sock->bind(IPC_UDP_GUI_PORT))
          return false;
  
      connect(sock, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
  
      return true;
  }
  
  void UdpHandler::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 UdpHandler::processDatagram(QByteArray &datagram)
  {
d7c3a65b4   김태훈   - 디버깅 포트 변경(4000 ...
46
      sock->writeDatagram(datagram, QHostAddress("192.168.4.191"), 40001);
8c2952457   김태훈   응용 프로그램 추가
47
48
49
50
      packet_t *packet = (packet_t *) datagram.data();
      switch (packet->header)
      {
      case HDR_OVEN_CONTROL:
d7c3a65b4   김태훈   - 디버깅 포트 변경(4000 ...
51
          qDebug() << "Received Control";
8c2952457   김태훈   응용 프로그램 추가
52
53
54
          processControl((oven_control_t *) packet->body);
          break;
      case HDR_OVEN_STATE:
d7c3a65b4   김태훈   - 디버깅 포트 변경(4000 ...
55
          qDebug() << "Received State";
8c2952457   김태훈   응용 프로그램 추가
56
57
58
          processState((oven_state_t *) packet->body);
          break;
      case HDR_ERROR_CODE:
d66d7f5b4   김태훈   GUI 버전 0.1.2
59
          qDebug() << "Received Error";
8c2952457   김태훈   응용 프로그램 추가
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
          break;
      }
  }
  
  void UdpHandler::processControl(oven_control_t *control)
  {
      if (memcmp(&this->control, control, sizeof(this->control)))
      {
          memcpy(&this->control, control, sizeof(this->control));
          emit changed();
      }
  }
  
  void UdpHandler::processState(oven_state_t *state)
  {
      if (memcmp(&this->state, state, sizeof(this->state)))
      {
          memcpy(&this->state, state, sizeof(this->state));
          emit changed();
      }
  }
  
  void UdpHandler::turnOn(int target)
  {
      sendCommand(CMD_ONOFF, target, SWITCH_ON);
  }
  
  void UdpHandler::turnOff(int target)
  {
      sendCommand(CMD_ONOFF, target, SWITCH_OFF);
  }
  
  void UdpHandler::set(int target, int value)
  {
      sendCommand(CMD_VALUE, target, value);
  }
  
  void UdpHandler::sendCommand(int cmd, int target, int value)
  {
      command_t command = { cmd, target, value };
      qDebug() << "Send" << cmd << target << value;
      sock->writeDatagram((const char *) &command, sizeof(command), QHostAddress(IPC_UDP_SYS_HOST), IPC_UDP_SYS_PORT);
  }
  
  void UdpHandler::fillControl(oven_control_t &container)
  {
      memcpy(&container, &control, sizeof(control));
  }
  
  void UdpHandler::fillData(oven_state_t &container)
  {
      memcpy(&container, &state, sizeof(state));
  }