Thu, 19 Nov 2015 21:17:26 +0100
Moved global bit defines to config.h
420
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (C) 2015 |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * mbsePi-apps is free software; you can redistribute it and/or modify it |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * along with mbsePi-apps; see the file COPYING. If not, write to the Free |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | #include "brewpanel.h" |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | #include "slcd.h" |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | #include "futil.h" |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | #include "sockio.h" |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | #ifdef HAVE_SDL_SDL_H |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | |
422
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
31 | int sock = -1; /* Unix datagram socket */ |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
32 | struct sockaddr_in servaddr; /* Server socket address */ |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
33 | uint16_t keys = SLCD_KEYS; /* Pressed keys bits */ |
420
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | void socket_recv(void) |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | uint16_t data; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | struct sockaddr_in clntaddr; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | socklen_t clntlen = sizeof(clntaddr); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | ssize_t recv_len; |
422
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
42 | int fd = 0, my_error; |
420
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | recv_len = recvfrom(sock, &data, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *)&clntaddr, &clntlen); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | if (recv_len == sizeof(uint16_t)) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | /* |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | * Get device from data |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | */ |
422
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
49 | fd = (data & SLCD_MDEV) >> 13; |
420
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | if (fd) |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | fprintf(stdout, "Device %d ", fd); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | if ((data & SLCD_MCLEAR) == SLCD_CLEAR) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | slcdClear(fd); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | } else if ((data & SLCD_MHOME) == SLCD_HOME) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | slcdHome(fd); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | } else if ((data & SLCD_MDGRAM) == SLCD_DGRAM) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | slcdPosition(fd, data & 0x001f, (data & 0x0060) >> 5); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | } else if ((data & 0xfe00) == SLCD_DATA) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | slcdPutchar(fd, data & 0x00ff); |
422
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
60 | } else if (data != 0x0000) { |
420
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | fprintf(stdout, "socket_recv got %04x\n", data); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | } |
422
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
63 | |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
64 | /* |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
65 | * Reply with the current keys state |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
66 | */ |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
67 | if (sendto(sock, &keys, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *) &clntaddr, clntlen) == -1) { |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
68 | fprintf(stdout, "sendto error: %s\n", strerror(errno)); |
13502d0dda65
The brewpanel works, but still needs a lot more
Michiel Broek <mbroek@mbse.eu>
parents:
420
diff
changeset
|
69 | } |
420
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | } else if (recv_len < 0) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | my_error = errno; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | if (my_error != EAGAIN) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | fprintf(stdout, "socket_recv error: %s", strerror(my_error)); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | } |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | } else { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | fprintf(stdout, "socket_recv unknown len=%d\n", (int)recv_len); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | } |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | } |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | int socket_connect(void) |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | int s; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | s = socket(AF_INET, SOCK_DGRAM, 0); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | if (s == -1) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | syslog(LOG_NOTICE, "socket_connect() can't create: %s", strerror(errno)); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | return -1; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | } |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | /* |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | * Setup address structure for the server socket. |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | */ |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | memset(&servaddr, 0, sizeof(servaddr)); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | servaddr.sin_family = AF_INET; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
97 | servaddr.sin_port = htons(6554); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
98 | servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
100 | if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
101 | close(s); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
102 | syslog(LOG_NOTICE, "socket_connect() can't bind socket: %s", strerror(errno)); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | return -1; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
104 | } |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | syslog(LOG_NOTICE, "socket_connect() using socket %d", s); |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | sock = s; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
108 | return sock; |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | } |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
111 | |
644a6106d712
The first ideas of the brewpanel simulation are in place.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
112 | #endif |