Blame view

app/gui/oven_control/udphandler.cpp 2.57 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)
  {
d43c6c172   김태훈   안 쓰는 루틴 정리
15
8c2952457   김태훈   응용 프로그램 추가
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
  }
  
  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)
  {
81bee1ec4   김태훈   원격 패킷 출력 프로그램 추가
45
      sock->writeDatagram(datagram, QHostAddress("192.168.4.191"), 4000);
8c2952457   김태훈   응용 프로그램 추가
46
47
48
49
      packet_t *packet = (packet_t *) datagram.data();
      switch (packet->header)
      {
      case HDR_OVEN_CONTROL:
d66d7f5b4   김태훈   GUI 버전 0.1.2
50
          qDebug() << "Received Control";
8c2952457   김태훈   응용 프로그램 추가
51
52
53
          processControl((oven_control_t *) packet->body);
          break;
      case HDR_OVEN_STATE:
d66d7f5b4   김태훈   GUI 버전 0.1.2
54
          qDebug() << "Received State";
8c2952457   김태훈   응용 프로그램 추가
55
56
57
          processState((oven_state_t *) packet->body);
          break;
      case HDR_ERROR_CODE:
d66d7f5b4   김태훈   GUI 버전 0.1.2
58
          qDebug() << "Received Error";
8c2952457   김태훈   응용 프로그램 추가
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
          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));
  }