-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_client_sub.c
286 lines (216 loc) · 13 KB
/
mqtt_client_sub.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "mqtt_structs.h"
#include <signal.h>
// Signal Handler für CTRL-C, damit das Disconnect Paket noch gesendet wird
// https://stackoverflow.com/questions/4217037/catch-ctrl-c-in-c
// Tag: C001
static volatile sig_atomic_t keep_running = 1;
// Tag: C001
static void sig_handler(int _) {
(void)_;
printf("signal handler executed\n");
keep_running = 0;
}
/*
testType->zahl32 = atoi(argv[1]);
testType->zahl16 = atoi(argv[2]);
testType->zahl8 = atoi(argv[3]);
*/
const char *SERVER_IP = "127.0.0.1";
const uint16_t SERVER_PORT = 1883;
// Aus bind(2) Beispiel
#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[]) {
// Tag: C001
signal(SIGINT, sig_handler);
// Puffer definieren
uint8_t mqttControlPacketConnectBuffer[17], mqttControlPacketSubscribeBuffer[10], mqttControlPacketPublishBuffer[10], mqttControlPacketSubackBuffer[5], mqttControlPacketConnectackBuffer[4], mqttControlPacketDisconnectBuffer[2], mqttControlPacketUnsubackBuffer[4];
// Index für Puffer
int index;
// mqttControlPacketConnect vorbereiten und in Puffer zum Versand ablegen
mqttControlPacketConnectTpl *mqttControlPacketConnect = (mqttControlPacketConnectTpl*) malloc(sizeof(mqttControlPacketConnectTpl));
index = 0;
mqttControlPacketConnect->mqttFixedHeaderByte1Bits.mqttControlPacketFlags = 0;
mqttControlPacketConnect->mqttFixedHeaderByte1Bits.mqttControlPacketType = CONNECT;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttFixedHeaderByte1;
mqttControlPacketConnect->mqttFixedHeaderRemainingLength = 15;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttFixedHeaderRemainingLength;
mqttControlPacketConnect->mqttVariableHeaderProtocolNameMSB = 0;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderProtocolNameMSB;
mqttControlPacketConnect->mqttVariableHeaderProtocolNameLSB = 4;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderProtocolNameLSB;
mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar0 = 'M';
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar0;
mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar1 = 'Q';
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar1;
mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar2 = 'T';
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar2;
mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar3 = 'T';
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderProtocolNameChar3;
mqttControlPacketConnect->mqttVariableHeaderProtocolLevel = 4;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderProtocolLevel;
mqttControlPacketConnect->mqttVariableHeaderConnectFlagsBits.mqttVariableHeaderConnectFlagsUsername = 0;
mqttControlPacketConnect->mqttVariableHeaderConnectFlagsBits.mqttVariableHeaderConnectFlagsPassword = 0;
mqttControlPacketConnect->mqttVariableHeaderConnectFlagsBits.mqttVariableHeaderConnectFlagsWillRetain = 0;
mqttControlPacketConnect->mqttVariableHeaderConnectFlagsBits.mqttVariableHeaderConnectFlagsWillFlag = 0;
mqttControlPacketConnect->mqttVariableHeaderConnectFlagsBits.mqttVariableHeaderConnectFlagsCleanSession = 1;
mqttControlPacketConnect->mqttVariableHeaderConnectFlagsBits.mqttVariableHeaderConnectFlagsReserved = 0;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderConnectFlags;
mqttControlPacketConnect->mqttVariableHeaderKeepAliveLSB = 0;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderKeepAliveLSB;
mqttControlPacketConnect->mqttVariableHeaderKeepAliveMSB = 0;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->mqttVariableHeaderKeepAliveMSB;
mqttControlPacketConnect->clientIdMSB = 0;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->clientIdMSB;
mqttControlPacketConnect->clientIdLSB = 3;
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->clientIdLSB;
mqttControlPacketConnect->clientIdChar0 = 's';
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->clientIdChar0;
mqttControlPacketConnect->clientIdChar1 = '0';
mqttControlPacketConnectBuffer[index++] = mqttControlPacketConnect->clientIdChar1;
mqttControlPacketConnect->clientIdChar2 = '1';
mqttControlPacketConnectBuffer[index] = mqttControlPacketConnect->clientIdChar2;
// mqttControlPacketConnack vorbereiten zum vergleichen mit der Antwort vom Server
mqttControlPacketConnectackTpl *mqttControlPacketConnectack = (mqttControlPacketConnectackTpl*) malloc(sizeof(mqttControlPacketConnectackTpl));
index = 0;
mqttControlPacketConnectack->mqttFixedHeaderByte1Bits.mqttControlPacketFlags = 0;
mqttControlPacketConnectack->mqttFixedHeaderByte1Bits.mqttControlPacketType = CONNACK;
mqttControlPacketConnectackBuffer[index++] = mqttControlPacketConnectack->mqttFixedHeaderByte1;
mqttControlPacketConnectack->mqttFixedHeaderRemainingLength = 2;
mqttControlPacketConnectackBuffer[index++] = mqttControlPacketConnectack->mqttFixedHeaderRemainingLength;
mqttControlPacketConnectack->mqttVariableHeaderConnectackFlags = 0;
mqttControlPacketConnectackBuffer[index++] = mqttControlPacketConnectack->mqttVariableHeaderConnectackFlags;
mqttControlPacketConnectack->mqttVariableHeaderConnectackReturncode = 0;
mqttControlPacketConnectackBuffer[index++] = mqttControlPacketConnectack->mqttVariableHeaderConnectackReturncode;
// mqttControlPacketSubscribe vorbereiten und in Puffer zum Versand ablegen
mqttControlPacketSubscribeTpl *mqttControlPacketSubscribe = (mqttControlPacketSubscribeTpl*) malloc(sizeof(mqttControlPacketSubscribeTpl));
index = 0;
mqttControlPacketSubscribe->mqttFixedHeaderByte1Bits.mqttControlPacketFlags = 2;
mqttControlPacketSubscribe->mqttFixedHeaderByte1Bits.mqttControlPacketType = SUBSCRIBE;
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttFixedHeaderByte1;
mqttControlPacketSubscribe->mqttFixedHeaderRemainingLength = 8;
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttFixedHeaderRemainingLength ;
mqttControlPacketSubscribe->mqttVariableHeaderPacketIdentifierMSB = 0;
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderPacketIdentifierMSB;
mqttControlPacketSubscribe->mqttVariableHeaderPacketIdentifierLSB = 1;
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderPacketIdentifierLSB;
mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterMSB = 0;
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterMSB;
mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterLSB = 3;
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterLSB;
mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterChar0 = 't';
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterChar0;
mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterChar1 = '0';
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterChar1;
mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterChar2 = '1';
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterChar2;
mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterQos = 0;
mqttControlPacketSubscribeBuffer[index++] = mqttControlPacketSubscribe->mqttVariableHeaderTopicFilterQos;
// mqttControlPacketUnsuback vorbereiten zum Vergleichen mit Server
mqttControlPacketUnsubackTpl *mqttControlPacketUnsuback = (mqttControlPacketUnsubackTpl*) malloc(sizeof(mqttControlPacketUnsubackTpl));
index = 0;
mqttControlPacketUnsuback->mqttFixedHeaderByte1Bits.mqttControlPacketFlags = 0;
mqttControlPacketUnsuback->mqttFixedHeaderByte1Bits.mqttControlPacketType = UNSUBACK;
mqttControlPacketUnsubackBuffer[index++] = mqttControlPacketUnsuback->mqttFixedHeaderByte1;
mqttControlPacketUnsuback->mqttFixedHeaderRemainingLength = 0;
mqttControlPacketUnsubackBuffer[index++] = mqttControlPacketUnsuback->mqttFixedHeaderRemainingLength;
mqttControlPacketUnsuback->mqttVariableHeaderPacketIdentifierMSB = 0;
mqttControlPacketUnsubackBuffer[index++] = mqttControlPacketUnsuback->mqttVariableHeaderPacketIdentifierMSB;
mqttControlPacketUnsuback->mqttVariableHeaderPacketIdentifierLSB = 1;
mqttControlPacketUnsubackBuffer[index++] = mqttControlPacketUnsuback->mqttVariableHeaderPacketIdentifierLSB;
// mqttControlPacketDisconnect vorbereiten und in Puffer zum Versand ablegen
mqttControlPacketDisconnectTpl *mqttControlPacketDisconnect = (mqttControlPacketDisconnectTpl*) malloc(sizeof(mqttControlPacketDisconnectTpl));
index = 0;
mqttControlPacketDisconnect->mqttFixedHeaderByte1Bits.mqttControlPacketFlags = 0;
mqttControlPacketDisconnect->mqttFixedHeaderByte1Bits.mqttControlPacketType = DISCONNECT;
mqttControlPacketDisconnectBuffer[index++] = mqttControlPacketDisconnect->mqttFixedHeaderByte1;
mqttControlPacketDisconnect->mqttFixedHeaderRemainingLength = 0;
mqttControlPacketDisconnectBuffer[index++] = mqttControlPacketDisconnect->mqttFixedHeaderRemainingLength;
int serverSocketFD, clientSocketFD;
struct sockaddr_in serverSocketSettings, clientSocketSettings;
// serverSocketFD erstellen
errno = 0;
serverSocketFD = socket(AF_INET, SOCK_STREAM, 0);
if ( errno != 0 ) { handle_error("serverSocketFD socket()"); }
// Speicher für Struktur sockaddr_in setzen
errno = 0;
bzero(&serverSocketSettings, sizeof(serverSocketSettings));
if( errno != 0 ) { handle_error("bzero()"); };
// Speicher für Struktur in_addr setzen
errno = 0;
struct in_addr serverSocketIP;
bzero(&serverSocketIP, sizeof(serverSocketIP));
if( errno != 0 ) { handle_error("bzero()"); };
// Zuweisen der Protokollfamilie
serverSocketSettings.sin_family = AF_INET;
// Zuweisen der IP-Adresse zur in_addr Struktur serverSocketIP
errno = 0;
inet_aton(SERVER_IP, &serverSocketIP);
if( errno != 0 ) { handle_error("inet_aton()"); };
serverSocketSettings.sin_addr = serverSocketIP;
// TCP-Port definieren
errno = 0;
serverSocketSettings.sin_port = htons(SERVER_PORT);
if( errno != 0 ) { handle_error("htons()"); };
// Socket binden
errno = 0;
clientSocketFD = connect(serverSocketFD, (struct sockaddr*)&serverSocketSettings, sizeof(serverSocketSettings));
if( errno != 0 ) { handle_error("connect()"); };
errno = 0;
send(serverSocketFD, &mqttControlPacketConnectBuffer, sizeof(mqttControlPacketConnectBuffer), 0);
if( errno != 0 ) { handle_error("send() mqttControlPacketConnectBuffer"); };
errno = 0;
recv(serverSocketFD, &mqttControlPacketConnectackBuffer, sizeof(mqttControlPacketConnectackBuffer), 0);
if( errno != 0 ) { handle_error("recv() mqttControlPacketConnectackBuffer"); };
if(mqttControlPacketConnectackBuffer[3] != 0) {
errno=1;
if( errno != 0 ) { handle_error("recv() mqttControlPacketConnectackBuffer Returncode != 0"); };
}
if(mqttControlPacketConnectackBuffer[2] != 0) {
errno=1;
if( errno != 0 ) { handle_error("recv() mqttControlPacketConnectackBuffer != 0, Session Present is not supported"); };
}
errno = 0;
send(serverSocketFD, &mqttControlPacketSubscribeBuffer, sizeof(mqttControlPacketSubscribeBuffer), 0);
if( errno != 0 ) { handle_error("send() mqttControlPacketSubscribe"); };
errno = 0;
recv(serverSocketFD, &mqttControlPacketSubackBuffer, sizeof(mqttControlPacketSubackBuffer), 0);
if( errno != 0 ) { handle_error("send() mqttControlPacketSubackBuffer"); };
switch(mqttControlPacketSubackBuffer[4]) {
case 128:
errno=1;
if( errno != 0 ) { handle_error("recv() mqttControlPacketSuback failed"); };
break;
case 2:
errno=1;
if( errno != 0 ) { handle_error("recv() mqttControlPacketSuback QoS 2 is not supported"); };
break;
case 1:
errno=1;
if( errno != 0 ) { handle_error("recv() mqttControlPacketSuback QoS 1 is not supported"); };
break;
}
// Tag: C001
while(keep_running) {
errno = 0;
recv(serverSocketFD, &mqttControlPacketPublishBuffer, sizeof(mqttControlPacketPublishBuffer), 0);
if( errno != 0 ) { handle_error("send() mqttControlPacketPublishBuffer"); };
printf("%c%c%c\n", mqttControlPacketPublishBuffer[7], mqttControlPacketPublishBuffer[8], mqttControlPacketPublishBuffer[9]);
}
errno = 0;
recv(serverSocketFD, &mqttControlPacketUnsubackBuffer, sizeof(mqttControlPacketUnsubackBuffer), 0);
if( errno != 0 ) { handle_error("send() mqttControlPacketUnsubackBuffer"); };
errno = 0;
send(serverSocketFD, &mqttControlPacketDisconnectBuffer, sizeof(mqttControlPacketDisconnectBuffer), 0);
if( errno != 0 ) { handle_error("send() mqttControlPacketDisconnectBuffer"); };
errno = 0;
close(clientSocketFD);
if( errno != 0 ) { handle_error("close() clientSocketFD"); };
return EXIT_SUCCESS;
}