Skip to content

Commit

Permalink
Client in C started.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jun 21, 2019
1 parent 832ac68 commit c5edcf5
Show file tree
Hide file tree
Showing 6 changed files with 565 additions and 34 deletions.
34 changes: 0 additions & 34 deletions mqttools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,40 +97,6 @@ class DisconnectReasonCode(enum.IntEnum):
WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED = 162


class PubackReasonCode(enum.IntEnum):
SUCCESS = 0
NO_MATCHING_SUBSCRIBERS = 16
UNSPECIFIED_ERROR = 128
IMPLEMENTATION_SPECIFIC_ERROR = 131
NOT_AUTHORIZED = 135
TOPIC_NAME_INVALID = 144
PACKET_IDENTIFIER_IN_USE = 145
QUOTA_EXCEEDED = 151
PAYLOAD_FORMAT_INVALID = 153


class PubrecReasonCode(enum.IntEnum):
SUCCESS = 0
NO_MATCHING_SUBSCRIBERS = 16
UNSPECIFIED_ERROR = 128
IMPLEMENTATION_SPECIFIC_ERROR = 131
NOT_AUTHORIZED = 135
TOPIC_NAME_INVALID = 144
PACKET_IDENTIFIER_IN_USE = 145
QUOTA_EXCEEDED = 151
PAYLOAD_FORMAT_INVALID = 153


class PubrelReasonCode(enum.IntEnum):
SUCCESS = 0
PACKET_IDENTIFIER_NOT_FOUND = 146


class PubcompReasonCode(enum.IntEnum):
SUCCESS = 0
PACKET_IDENTIFIER_NOT_FOUND = 146


class SubackReasonCode(enum.IntEnum):
GRANTED_QOS_0 = 0
GRANTED_QOS_1 = 1
Expand Down
4 changes: 4 additions & 0 deletions src/c/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
An MQTT library in C
====================

A single thread with epoll.
62 changes: 62 additions & 0 deletions src/c/examples/publish/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the mqttools project.
*/

#include "mqttools.h"

int main()
{
int res;
struct mqttools_client_t client;

mqttools_client_init(&client, "broker.hivemq.com", 1883, NULL);

res = mqttools_client_start(&client, false);

if (res != MQTTOOLS_OK) {
return (2);
}

res = mqttools_client_publish(&client,
"/test/mqttools/foo",
(const uint8_t *)"bar",
3);

if (res != MQTTOOLS_OK) {
return (3);
}

printf("Successfully published b'bar' on '/test/mqttools/foo'.");

res = mqttools_client_stop(&client);

if (res != MQTTOOLS_OK) {
return (4);
}

return (0);
}
84 changes: 84 additions & 0 deletions src/c/examples/subscribe/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the mqttools project.
*/

#include "mqttools.h"

int main()
{
int res;
ssize_t i;
ssize_t size;
struct mqttools_client_t client;
char topic[128];
uint8_t message[256];

mqttools_client_init(&client, "broker.hivemq.com", 1883);

res = mqttools_client_start(&client, false);

if (res != MQTTOOLS_OK) {
return (2);
}

res = mqttools_client_subscribe(&client, "/test/mqttools/#");

if (res != MQTTOOLS_OK) {
return (3);
}

printf("Successfully subscribed to '/test/mqttools/#'.\n");

while (true) {
size = mqttools_client_read_message(&client,
&topic[0],
sizeof(topic),
&message[0],
sizeof(message));

if (size < 0) {
return (4);
}

printf("Topic: %s\n", &topic[0]);
printf("Message: ");

for (i = 0; i < size; i++) {
printf("%02x", message[i]);
}

printf("\n");
}

res = mqttools_client_stop(&client);

if (res != MQTTOOLS_OK) {
return (5);
}

return (0);
}

0 comments on commit c5edcf5

Please sign in to comment.