2 Copyright 2006-2020 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
29 const char* InputUDP::myModName = "InputUDP";
33 if (initted) return false;
35 log = Log::getInstance();
36 log->log("InputUDP", Log::DEBUG, "Starting InputUDP command server");
40 log->log("InputUDP", Log::DEBUG, "UDP4 init error");
46 quitPipe = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
47 if (quitPipe == INVALID_SOCKET)
49 Log::getInstance()->log("InputUDP", Log::ERR, "Win32 socket fail");
51 if (pipe2(pfds, O_NONBLOCK) == -1)
53 Log::getInstance()->log("InputUDP", Log::ERR, "pipe2() fail");
63 void InputUDP::shutdown()
66 CLOSESOCKET(quitPipe);
79 bool InputUDP::start()
81 threadStartProtect.lock(); // Make sure listenThread is fully initted before start returns
82 listenThread = std::thread( [this]
84 threadStartProtect.lock();
85 threadStartProtect.unlock();
88 threadStartProtect.unlock();
90 log->log("InputUDP", Log::DEBUG, "InputUDP command server started");
96 std::lock_guard<std::mutex> lg(threadStartProtect); // Also use it to protect against starting while stopping
100 if (listenThread.joinable())
103 Log::getInstance()->log("InputUDP", Log::DEBUG, "Calling CLOSESOCKET on WIN32 quitPipe");
105 CLOSESOCKET(quitPipe);
107 write(pfds[1], "1", 1); // break the select in listenLoop
113 void InputUDP::listenLoop()
119 retval = udp4.waitforMessage(3, quitPipe);
121 retval = udp4.waitforMessage(3, pfds[0]);
123 Log::getInstance()->log("InputUDP", Log::DEBUG, "Back from waitForMessage");
127 processRequest(udp4.getData(), udp4.getDataLength());
129 else if (retval == 3) // quit
131 Log::getInstance()->log("InputUDP", Log::DEBUG, "quit");
136 log->log("InputUDP", Log::CRIT, "Wait for packet error");
142 void InputUDP::processRequest(const void* data, UINT length)
144 log->log("InputUDP", Log::DEBUG, "Got request");
146 char* temp = new char[length + 1];
147 memcpy(temp, data, length);
149 UINT command = static_cast<UINT>(atoi(temp));
152 log->log("InputUDP", Log::DEBUG, "Command %i recieved", command);
153 sendInputKey(command);
156 const char* InputUDP::getHardCodedHardwareKeyNamesForVompKey(UCHAR /* vompKey */)
161 std::string InputUDP::getHardwareKeyName(HWC_TYPE /* hardwareKey */)
174 //void dump(unsigned char* data, USHORT size);
175 //unsigned char dcc(UCHAR c);
177 void dump(unsigned char* data, USHORT size)
179 printf("Size = %u\n", size);
186 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
187 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
188 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14], data[c+15],
189 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
190 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]), dcc(data[c+15]));
198 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
199 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
200 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14],
201 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
202 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]));
206 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
207 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
208 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13],
209 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
210 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]));
214 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c\n",
215 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
216 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12],
217 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
218 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]));
222 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c\n",
223 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
224 data[c+8], data[c+9], data[c+10], data[c+11],
225 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
226 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]));
230 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c\n",
231 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
232 data[c+8], data[c+9], data[c+10],
233 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
234 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]));
238 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c\n",
239 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
240 data[c+8], data[c+9],
241 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
242 dcc(data[c+8]), dcc(data[c+9]));
246 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c\n",
247 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
249 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
254 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c\n",
255 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
256 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]));
260 printf(" %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c\n",
261 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6],
262 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]));
266 printf(" %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c\n",
267 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5],
268 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]));
272 printf(" %02X %02X %02X %02X %02X %c%c%c%c%c\n",
273 data[c], data[c+1], data[c+2], data[c+3], data[c+4],
274 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]));
278 printf(" %02X %02X %02X %02X %c%c%c%c\n",
279 data[c], data[c+1], data[c+2], data[c+3],
280 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]));
284 printf(" %02X %02X %02X %c%c%c\n",
285 data[c], data[c+1], data[c+2],
286 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]));
290 printf(" %02X %02X %c%c\n",
292 dcc(data[c]), dcc(data[c+1]));
306 unsigned char dcc(UCHAR c)
308 if (isspace(c)) return ' ';
309 if (isprint(c)) return c;