Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
 * Copyright (c) 2016 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mqtt.h"

#include <errno.h>

int mqtt_cb_publish(struct mqtt_app_ctx_t *app,
		    int (*cb_publish)(struct mqtt_msg_t *msg))
{
	app->cb_publish = cb_publish;

	return 0;
}

int mqtt_init(struct mqtt_app_ctx_t *app)
{
	nano_sem_init(&app->sem);
	nano_sem_give(&app->sem);

	return 0;
}

int mqtt_configure(struct mqtt_app_ctx_t *app, const char *client_id,
	      int clean_session, enum mqtt_protocol proto)
{
	app->client->client_id = client_id;
	app->client->clean_session = clean_session;
	app->client->proto = proto;

	return 0;
}

int mqtt_auth(struct mqtt_app_ctx_t *app, const char *username,
	      const char *pass)
{
	app->client->username = username;
	app->client->pass = pass;

	return 0;
}

int mqtt_will(struct mqtt_app_ctx_t *app, const char *topic,
	      const char *msg, enum mqtt_qos qos, int retained)
{
	app->client->will_enabled = 1;
	app->client->will_topic = topic;
	app->client->will_payload = msg;
	app->client->will_qos = qos;
	app->client->will_retained = retained;

	return 0;
}

int mqtt_buffers(struct mqtt_app_ctx_t *app, struct app_buf_t *tx_buf,
		 struct app_buf_t *rx_buf)
{
	app->tx_buf = tx_buf;
	app->rx_buf = rx_buf;

	return 0;
}

int mqtt_network(struct mqtt_app_ctx_t *app, struct netz_ctx_t *netz_ctx)
{
	app->netz_ctx = netz_ctx;

	return 0;
}

int mqtt_connect(struct mqtt_app_ctx_t *app)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *tx_buf;
	struct app_buf_t *rx_buf;
	int session;
	int conn_ack;
	int rc;

	netz_ctx = app->netz_ctx;
	tx_buf = app->tx_buf;
	rx_buf = app->rx_buf;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	rc = mqtt_pack_connect(tx_buf, app->client);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_pack_connect;
	}

	rc = netz_tcp(netz_ctx);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_tcp;
	}

	rc = netz_tx(netz_ctx, tx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_tx;
	}

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_rx;
	}

	rc = mqtt_unpack_connack(rx_buf, &session, &conn_ack);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_unpack_connack;
	}

	rc = -EINVAL;
	if (app->client->clean_session == 1) {
		if (session == 0 && conn_ack == 0) {
			rc = 0;
		}
	}
	/*
	 * TODO: validate CleanSession = 0
	 */

error_mqtt_pack_connect:
error_netz_tcp:
error_netz_tx:
error_netz_rx:
error_mqtt_unpack_connack:
	nano_sem_give(&app->sem);
	return rc;
}

int mqtt_disconnect(struct mqtt_app_ctx_t *app)
{
	int rc;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	rc = mqtt_pack_disconnect(app->tx_buf);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_pack_disconnect;
	}

	rc = netz_tx(app->netz_ctx, app->tx_buf);
	if (rc != 0) {
		rc = -EIO;
	}

error_mqtt_pack_disconnect:
	nano_sem_give(&app->sem);
	return rc;
}


static inline int mqtt_publish_qos1(struct mqtt_app_ctx_t *app, uint16_t id)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *rx_buf;
	uint8_t pkt_type;
	uint8_t dup = 0;
	uint16_t rcv_pkt_id = 0;
	int rc;

	netz_ctx = app->netz_ctx;
	rx_buf = app->rx_buf;

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		return -EIO;
	}

	rc = mqtt_unpack_ack(rx_buf, &pkt_type, &dup, &rcv_pkt_id);
	if (rc != 0) {
		return -EINVAL;
	}

	if (pkt_type != MQTT_PUBACK) {
		return -EINVAL;
	}

	if (rcv_pkt_id != id) {
		return -EINVAL;
	}

	return 0;
}

static inline int mqtt_publish_qos2(struct mqtt_app_ctx_t *app,
				    const uint16_t pkt_id)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *tx_buf;
	struct app_buf_t *rx_buf;
	uint8_t pkt_type;
	uint8_t dup = 0;
	uint16_t rcv_pkt_id;
	int rc;

	netz_ctx = app->netz_ctx;
	tx_buf = app->tx_buf;
	rx_buf = app->rx_buf;

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		return -EIO;
	}

	rc = mqtt_unpack_ack(rx_buf, &pkt_type, &dup, &rcv_pkt_id);
	if (rc != 0) {
		return -EINVAL;
	}

	if (pkt_type != MQTT_PUBREC) {
		return -EINVAL;
	}
	if (rcv_pkt_id != pkt_id) {
		return -EINVAL;
	}

	rc = mqtt_pack_pubrel(tx_buf, dup, pkt_id);
	if (rc != 0) {
		return -EINVAL;
	}

	rc = netz_tx(netz_ctx, tx_buf);
	if (rc != 0) {
		return -EIO;
	}

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		return -EIO;
	}

	rc = mqtt_unpack_ack(rx_buf, &pkt_type, &dup, &rcv_pkt_id);
	if (rc != 0) {
		return -EINVAL;
	}

	if (pkt_type != MQTT_PUBCOMP) {
		return -EINVAL;
	}
	if (rcv_pkt_id != pkt_id) {
		return -EINVAL;
	}

	return 0;
}

int mqtt_publish(struct mqtt_app_ctx_t *app, struct mqtt_msg_t *msg,
		 enum mqtt_qos qos, int retained)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *tx_buf;
	int rc;

	netz_ctx = app->netz_ctx;
	tx_buf = app->tx_buf;

	msg->dup = 0;
	msg->qos = qos;
	msg->retained = retained;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	if (msg->qos != MQTT_QoS0) {
		mqtt_next_pktid(app->client, &msg->pkt_id);
	} else {
		msg->pkt_id = 0;
	}

	rc = mqtt_pack_publish(tx_buf, msg);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_pack_publish;
	}

	rc = netz_tx(netz_ctx, tx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_tx;
	}

	switch (msg->qos) {
	case MQTT_QoS0:
		break;

	case MQTT_QoS1:
		rc = mqtt_publish_qos1(app, msg->pkt_id);
		if (rc != 0) {
			rc = -EINVAL;
		}
		break;

	case MQTT_QoS2:
		rc = mqtt_publish_qos2(app, msg->pkt_id);
		if (rc != 0) {
			rc = -EINVAL;
		}
		break;

	default:
		rc = -EINVAL;
	}

error_mqtt_pack_publish:
error_netz_tx:
	nano_sem_give(&app->sem);
	return rc;
}

int mqtt_pingreq(struct mqtt_app_ctx_t *app)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *tx_buf;
	struct app_buf_t *rx_buf;
	uint8_t pkt_type;
	uint8_t dup = 0;
	uint16_t rcv_pkt_id;
	int rc;

	netz_ctx = app->netz_ctx;
	tx_buf = app->tx_buf;
	rx_buf = app->rx_buf;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	rc = mqtt_pack_pingreq(tx_buf);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_pack_pingreq;
	}

	rc = netz_tx(netz_ctx, tx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_tx;
	}

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_rx;
	}

	rc = mqtt_unpack_ack(rx_buf, &pkt_type, &dup, &rcv_pkt_id);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_unpack_ack;
	}

	if (pkt_type != MQTT_PINGRESP) {
		rc = -EINVAL;
	}

error_mqtt_pack_pingreq:
error_netz_tx:
error_netz_rx:
error_mqtt_unpack_ack:
	nano_sem_give(&app->sem);
	return rc;
}

int mqtt_subscribe(struct mqtt_app_ctx_t *app,
		   char *topic, enum mqtt_qos qos)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *tx_buf;
	struct app_buf_t *rx_buf;
	uint16_t pkt_id;
	uint16_t rcv_pkt_id;
	int granted_qos;
	int dup = 0;
	int rc;

	netz_ctx = app->netz_ctx;
	tx_buf = app->tx_buf;
	rx_buf = app->rx_buf;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	mqtt_next_pktid(app->client, &pkt_id);

	rc = mqtt_pack_subscribe(tx_buf, dup, pkt_id, topic, qos);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_pack_subscribe;
	}

	rc = netz_tx(netz_ctx, tx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_tx;
	}

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_rx;
	}

	rc = mqtt_unpack_suback(rx_buf, &rcv_pkt_id, &granted_qos);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_unpack_suback;
	}

	if (rcv_pkt_id != pkt_id) {
		rc = -EINVAL;
	} else {
		rc = granted_qos;
	}

error_mqtt_pack_subscribe:
error_netz_tx:
error_netz_rx:
error_mqtt_unpack_suback:
	nano_sem_give(&app->sem);
	return rc;
}

int mqtt_unsubscribe(struct mqtt_app_ctx_t *app, char *topic)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *tx_buf;
	struct app_buf_t *rx_buf;
	uint16_t pkt_id;
	uint16_t rcv_pkt_id;
	int dup = 0;
	int rc;

	netz_ctx = app->netz_ctx;
	tx_buf = app->tx_buf;
	rx_buf = app->rx_buf;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	mqtt_next_pktid(app->client, &pkt_id);

	rc = mqtt_pack_unsubscribe(tx_buf, dup, pkt_id, topic);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_pack_unsubscribe;
	}

	rc = netz_tx(netz_ctx, tx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_tx;
	}

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_rx;
	}

	rc = mqtt_unpack_unsuback(rx_buf, &rcv_pkt_id);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_unpack_unsuback;
	}

	if (rcv_pkt_id != pkt_id) {
		rc = -EINVAL;
	}

error_mqtt_pack_unsubscribe:
error_netz_tx:
error_netz_rx:
error_mqtt_unpack_unsuback:
	nano_sem_give(&app->sem);
	return rc;
}

int mqtt_handle_publish(struct mqtt_app_ctx_t *app)
{
	struct mqtt_msg_t msg;
	uint16_t rcv_pkt_id;
	int8_t rcv_pkt_type;
	uint8_t dup;
	int rc;

	rc = mqtt_unpack_publish(app->rx_buf, &msg);
	if (rc != 0) {
		return -EINVAL;
	}

	switch (msg.qos) {
	case MQTT_QoS0:
		break;

	case MQTT_QoS1:
		rc = mqtt_pack_msg(app->tx_buf, MQTT_PUBACK, msg.pkt_id, 0);
		if (rc != 0) {
			return -EINVAL;
		}

		rc = netz_tx(app->netz_ctx, app->tx_buf);
		if (rc != 0) {
			return -EIO;
		}

		break;

	case MQTT_QoS2:
		rc = mqtt_pack_msg(app->tx_buf, MQTT_PUBREC, msg.pkt_id, 0);
		if (rc != 0) {
			return -EINVAL;
		}

		rc = netz_tx(app->netz_ctx, app->tx_buf);
		if (rc != 0) {
			return -EIO;
		}

		rc = netz_rx(app->netz_ctx, app->rx_buf);
		if (rc != 0) {
			return -EIO;
		}

		rc = mqtt_unpack_ack(app->rx_buf, &rcv_pkt_type, &dup,
				     &rcv_pkt_id);
		if (rc != 0) {
			return -EINVAL;
		}

		if (rcv_pkt_type != MQTT_PUBREL) {
			return -EINVAL;
		}

		if (msg.pkt_id != rcv_pkt_id) {
			return -EINVAL;
		}

		rc = mqtt_pack_msg(app->tx_buf, MQTT_PUBCOMP, msg.pkt_id, 0);
		if (rc != 0) {
			return -EINVAL;
		}

		rc = netz_tx(app->netz_ctx, app->tx_buf);
		if (rc != 0) {
			return -EIO;
		}

		break;

	default:
		return -EINVAL;
	}

	if (app->cb_publish != NULL) {
		return app->cb_publish(&msg);
	}

	return 0;
}

int mqtt_handle_pingreq(struct mqtt_app_ctx_t *app)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *tx_buf;
	int rc;

	netz_ctx = app->netz_ctx;
	tx_buf = app->tx_buf;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	rc = mqtt_pack_pingresp(tx_buf);
	if (rc != 0) {
		rc = -EINVAL;
		goto error_mqtt_pack_pingresp;
	}

	rc = netz_tx(netz_ctx, tx_buf);
	if (rc != 0) {
		rc = -EIO;
	}

error_mqtt_pack_pingresp:
	nano_sem_give(&app->sem);
	return rc;
}

int mqtt_read(struct mqtt_app_ctx_t *app)
{
	struct netz_ctx_t *netz_ctx;
	struct app_buf_t *rx_buf;
	int pkt_type;
	int rc;

	netz_ctx = app->netz_ctx;
	rx_buf = app->rx_buf;

	nano_sem_take(&app->sem, TICKS_UNLIMITED);

	rc = netz_rx(netz_ctx, rx_buf);
	if (rc != 0) {
		rc = -EIO;
		goto error_netz_rx;
	}

	if (rx_buf->length < 2) {
		rc = -ENOMEM;
		goto error_nomem;
	}

	pkt_type = (rx_buf->buf[0] & 0xF0) >> 4;

	/* This switch-case will be used for packet-handling routines
	 * that will be coded in future releases.
	 */
	switch (pkt_type) {
	case MQTT_PUBLISH:
		rc = mqtt_handle_publish(app);
		break;
	case MQTT_CONNECT:
	case MQTT_CONNACK:
	case MQTT_PUBACK:
	case MQTT_PUBREC:
	case MQTT_PUBREL:
	case MQTT_PUBCOMP:
	case MQTT_SUBSCRIBE:
	case MQTT_SUBACK:
	case MQTT_UNSUBSCRIBE:
	case MQTT_UNSUBACK:
		break;
	case MQTT_PINGREQ:
		rc = mqtt_handle_pingreq(app);
		break;
	case MQTT_PINGRESP:
		break;
	case MQTT_DISCONNECT:
		break;
	default:
		rc = -EINVAL;
		break;
	}

error_netz_rx:
error_nomem:
	nano_sem_give(&app->sem);
	return rc;
}