From 6c93c1d6f28eb53a27e4b9d628d3a083c60408d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Wed, 29 Mar 2017 12:49:56 +0200 Subject: [PATCH] updated example --- example/main.c | 133 ++++--------------------------------------------- 1 file changed, 10 insertions(+), 123 deletions(-) diff --git a/example/main.c b/example/main.c index 1a2a2f5..3f2cb7d 100644 --- a/example/main.c +++ b/example/main.c @@ -1,34 +1,16 @@ #include #include -#include #include #include #define COMMAND_TIMEOUT 5000 -char *topic = "hello"; - -#define PAYLOAD_LEN 256 -char payload[PAYLOAD_LEN + 1]; - -volatile int counter; - static void message_arrived(lwmqtt_client_t *c, lwmqtt_string_t *t, lwmqtt_message_t *m) { - if (lwmqtt_strcmp(t, topic) != 0) { - printf("topic does not match\n"); - exit(1); - } - - if (strncmp(payload, m->payload, (size_t)m->payload_len) != 0) { - printf("payload does not match\n"); - exit(1); - } - - counter++; + printf("message_arrived: %.*s => %.*s\n", t->len, t->data, m->payload_len, (char*)m->payload); } -static void testSendAndReceive(lwmqtt_qos_t qos) { +int main() { unsigned char buf1[512], buf2[512]; lwmqtt_unix_network_t network; @@ -42,7 +24,7 @@ static void testSendAndReceive(lwmqtt_qos_t qos) { lwmqtt_set_timers(&client, &timer1, &timer2, lwmqtt_unix_timer_set, lwmqtt_unix_timer_get); lwmqtt_set_callback(&client, message_arrived); - lwmqtt_err_t err = lwmqtt_unix_network_connect(&network, "127.0.0.1", 1883); + lwmqtt_err_t err = lwmqtt_unix_network_connect(&network, "broker.shiftr.io", 1883); if (err != LWMQTT_SUCCESS) { printf("failed lwmqtt_unix_network_connect: %d\n", err); exit(1); @@ -50,6 +32,9 @@ static void testSendAndReceive(lwmqtt_qos_t qos) { lwmqtt_options_t data = lwmqtt_default_options; data.client_id = lwmqtt_str("lwmqtt"); + data.username = lwmqtt_str("try"); + data.password = lwmqtt_str("try"); + data.keep_alive = 5; lwmqtt_return_code_t return_code; err = lwmqtt_connect(&client, &data, NULL, &return_code, COMMAND_TIMEOUT); @@ -58,87 +43,11 @@ static void testSendAndReceive(lwmqtt_qos_t qos) { exit(1); } - err = lwmqtt_subscribe(&client, "hello", qos, COMMAND_TIMEOUT); - if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_subscribe: %d\n", err); - exit(1); - } - - counter = 0; + printf("connected!\n"); - for (int i = 0; i < 5; i++) { - lwmqtt_message_t msg = lwmqtt_default_message; - msg.qos = qos; - msg.payload = payload; - msg.payload_len = PAYLOAD_LEN; - - err = lwmqtt_publish(&client, "hello", &msg, COMMAND_TIMEOUT); - if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_publish: %d (%d)\n", err, counter); - exit(1); - } - } - - while (counter < 5) { - int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); - if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_unix_network_peek: %d (%d)\n", err, counter); - exit(1); - } - - if (available > 0) { - err = lwmqtt_yield(&client, available, COMMAND_TIMEOUT); - if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_yield: %d (%d)\n", err, counter); - exit(1); - } - } - } - - err = lwmqtt_unsubscribe(&client, "hello", COMMAND_TIMEOUT); + err = lwmqtt_subscribe(&client, "hello", LWMQTT_QOS0, COMMAND_TIMEOUT); if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_unsubscribe: %d\n", err); - exit(1); - } - - err = lwmqtt_disconnect(&client, COMMAND_TIMEOUT); - if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_disconnect: %d\n", err); - exit(1); - } - - lwmqtt_unix_network_disconnect(&network); -} - -static void testKeepAlive() { - unsigned char buf1[512], buf2[512]; - - lwmqtt_unix_network_t network; - lwmqtt_unix_timer_t timer1, timer2; - - lwmqtt_client_t client; - - lwmqtt_init(&client, buf1, 512, buf2, 512); - - lwmqtt_set_network(&client, &network, lwmqtt_unix_network_read, lwmqtt_unix_network_write); - lwmqtt_set_timers(&client, &timer1, &timer2, lwmqtt_unix_timer_set, lwmqtt_unix_timer_get); - lwmqtt_set_callback(&client, message_arrived); - - lwmqtt_err_t err = lwmqtt_unix_network_connect(&network, "127.0.0.1", 1883); - if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_unix_network_connect: %d\n", err); - exit(1); - } - - lwmqtt_options_t data = lwmqtt_default_options; - data.client_id = lwmqtt_str("lwmqtt"); - data.keep_alive = 5; - - lwmqtt_return_code_t return_code; - err = lwmqtt_connect(&client, &data, NULL, &return_code, COMMAND_TIMEOUT); - if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_connect: %d (%d)\n", err, return_code); + printf("failed lwmqtt_subscribe: %d (%d)\n", err, return_code); exit(1); } @@ -151,30 +60,8 @@ static void testKeepAlive() { err = lwmqtt_keep_alive(&client, COMMAND_TIMEOUT); if (err != LWMQTT_SUCCESS) { - printf("failed lwmqtt_keep_alive: %d (%d)\n", err, counter); + printf("failed lwmqtt_keep_alive: %d\n", err); exit(1); } } } - -int main() { - for (int i = 0; i < PAYLOAD_LEN; i++) { - payload[i] = 'x'; - } - - payload[PAYLOAD_LEN] = 0; - - printf("Running QoS 0 tests...\n"); - testSendAndReceive(LWMQTT_QOS0); - - printf("Running QoS 1 tests...\n"); - testSendAndReceive(LWMQTT_QOS1); - - printf("Running QoS 2 tests...\n"); - testSendAndReceive(LWMQTT_QOS2); - - printf("Running Keep Alive test...\n"); - testKeepAlive(); - - return 0; -}