Skip to content

Commit

Permalink
Merge pull request #26 from manchoz/sleeping_client
Browse files Browse the repository at this point in the history
Add support for sleeping client.
  • Loading branch information
njh authored Jun 15, 2018
2 parents 0df927a + b2cbbd9 commit ff65892
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Publishing
-h <host> MQTT-SN host to connect to. Defaults to '127.0.0.1'.
-i <clientid> ID to use for this client. Defaults to 'mqtt-sn-tools-' with process id.
-k <keepalive> keep alive in seconds for this client. Defaults to 10.
-e <sleep> sleep duration in seconds when disconnecting. Defaults to 0.
-m <message> Message payload to send.
-l Read from STDIN, one message per line.
-n Send a null (zero length) message.
Expand All @@ -71,6 +72,7 @@ Subscribing
-h <host> MQTT-SN host to connect to. Defaults to '127.0.0.1'.
-i <clientid> ID to use for this client. Defaults to 'mqtt-sn-tools-' with process id.
-k <keepalive> keep alive in seconds for this client. Defaults to 10.
-e <sleep> sleep duration in seconds when disconnecting. Defaults to 0.
-p <port> Network port to connect to. Defaults to 1883.
-q <qos> QoS level to subscribe with (0 or 1). Defaults to 0.
-t <topic> MQTT-SN topic name to subscribe to. It may repeat multiple times.
Expand Down
10 changes: 8 additions & 2 deletions mqtt-sn-pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const char *mqtt_sn_port = MQTT_SN_DEFAULT_PORT;
uint16_t topic_id = 0;
uint8_t topic_id_type = MQTT_SN_TOPIC_TYPE_NORMAL;
uint16_t keep_alive = MQTT_SN_DEFAULT_KEEP_ALIVE;
uint16_t sleep_duration = 0;
int8_t qos = 0;
uint8_t retain = FALSE;
uint8_t one_message_per_line = FALSE;
Expand All @@ -60,6 +61,7 @@ static void usage()
fprintf(stderr, " -h <host> MQTT-SN host to connect to. Defaults to '%s'.\n", mqtt_sn_host);
fprintf(stderr, " -i <clientid> ID to use for this client. Defaults to 'mqtt-sn-tools-' with process id.\n");
fprintf(stderr, " -k <keepalive> keep alive in seconds for this client. Defaults to %d.\n", keep_alive);
fprintf(stderr, " -e <sleep> sleep duration in seconds when disconnecting. Defaults to %d.\n", sleep_duration);
fprintf(stderr, " -m <message> Message payload to send.\n");
fprintf(stderr, " -l Read from STDIN, one message per line.\n");
fprintf(stderr, " -n Send a null (zero length) message.\n");
Expand Down Expand Up @@ -89,7 +91,7 @@ static void parse_opts(int argc, char** argv)
int option_index = 0;

// Parse the options/switches
while ((ch = getopt_long (argc, argv, "df:h:i:k:lm:np:q:rst:T:?", long_options, &option_index)) != -1)
while ((ch = getopt_long (argc, argv, "df:h:i:k:e:lm:np:q:rst:T:?", long_options, &option_index)) != -1)
{
switch (ch) {
case 'd':
Expand Down Expand Up @@ -121,6 +123,10 @@ static void parse_opts(int argc, char** argv)
keep_alive = atoi(optarg);
break;

case 'e':
sleep_duration = atoi(optarg);
break;

case 'n':
message_data = "";
break;
Expand Down Expand Up @@ -291,7 +297,7 @@ int main(int argc, char* argv[])
// Finally, disconnect
if (qos >= 0) {
mqtt_sn_log_debug("Disconnecting...");
mqtt_sn_send_disconnect(sock);
mqtt_sn_send_disconnect(sock, sleep_duration);
mqtt_sn_receive_disconnect(sock);
}

Expand Down
10 changes: 8 additions & 2 deletions mqtt-sn-sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ uint16_t predef_topic_id_index = 0;
const char *mqtt_sn_host = "127.0.0.1";
const char *mqtt_sn_port = MQTT_SN_DEFAULT_PORT;
uint16_t keep_alive = MQTT_SN_DEFAULT_KEEP_ALIVE;
uint16_t sleep_duration = 0;
int8_t qos = 0;
uint8_t retain = FALSE;
uint8_t debug = 0;
Expand All @@ -71,6 +72,7 @@ static void usage()
fprintf(stderr, " -h <host> MQTT-SN host to connect to. Defaults to '%s'.\n", mqtt_sn_host);
fprintf(stderr, " -i <clientid> ID to use for this client. Defaults to 'mqtt-sn-tools-' with process id.\n");
fprintf(stderr, " -k <keepalive> keep alive in seconds for this client. Defaults to %d.\n", keep_alive);
fprintf(stderr, " -e <sleep> sleep duration in seconds when disconnecting. Defaults to %d.\n", sleep_duration);
fprintf(stderr, " -p <port> Network port to connect to. Defaults to %s.\n", mqtt_sn_port);
fprintf(stderr, " -q <qos> QoS level to subscribe with (0 or 1). Defaults to %d.\n", qos);
fprintf(stderr, " -t <topic> MQTT-SN topic name to subscribe to. It may repeat multiple times.\n");
Expand All @@ -97,7 +99,7 @@ static void parse_opts(int argc, char** argv)
int option_index = 0;

// Parse the options/switches
while ((ch = getopt_long(argc, argv, "1cdh:i:k:p:q:t:T:vV?", long_options, &option_index)) != -1)
while ((ch = getopt_long(argc, argv, "1cdh:i:k:e:p:q:t:T:vV?", long_options, &option_index)) != -1)
switch (ch) {
case '1':
single_message = TRUE;
Expand All @@ -123,6 +125,10 @@ static void parse_opts(int argc, char** argv)
keep_alive = atoi(optarg);
break;

case 'e':
sleep_duration = atoi(optarg);
break;

case 'p':
mqtt_sn_port = optarg;
break;
Expand Down Expand Up @@ -262,7 +268,7 @@ int main(int argc, char* argv[])

// Finally, disconnect
mqtt_sn_log_debug("Disconnecting...");
mqtt_sn_send_disconnect(sock);
mqtt_sn_send_disconnect(sock, sleep_duration);
mqtt_sn_receive_disconnect(sock);

close(sock);
Expand Down
14 changes: 9 additions & 5 deletions mqtt-sn.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,20 +514,24 @@ void mqtt_sn_send_pingreq(int sock)
mqtt_sn_send_packet(sock, &packet);
}

void mqtt_sn_send_disconnect(int sock)
void mqtt_sn_send_disconnect(int sock, uint16_t duration)
{
disconnect_packet_t packet;
memset(&packet, 0, sizeof(packet));

packet.type = MQTT_SN_TYPE_DISCONNECT;
packet.length = 0x02;

mqtt_sn_log_debug("Sending DISCONNECT packet...");
if (duration == 0) {
packet.length = 0x02;
mqtt_sn_log_debug("Sending DISCONNECT packet...");
} else {
packet.length = sizeof(packet);
packet.duration = htons(duration);
mqtt_sn_log_debug("Sending DISCONNECT packet with Duration %d...", duration);
}

mqtt_sn_send_packet(sock, &packet);
}


void mqtt_sn_receive_disconnect(int sock)
{
disconnect_packet_t *packet = mqtt_sn_wait_for(MQTT_SN_TYPE_DISCONNECT, sock);
Expand Down
2 changes: 1 addition & 1 deletion mqtt-sn.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void mqtt_sn_send_puback(int sock, publish_packet_t* publish, uint8_t return_cod
void mqtt_sn_send_subscribe_topic_name(int sock, const char* topic_name, uint8_t qos);
void mqtt_sn_send_subscribe_topic_id(int sock, uint16_t topic_id, uint8_t qos);
void mqtt_sn_send_pingreq(int sock);
void mqtt_sn_send_disconnect(int sock);
void mqtt_sn_send_disconnect(int sock, uint16_t duration);
void mqtt_sn_receive_disconnect(int sock);
void mqtt_sn_receive_connack(int sock);
uint16_t mqtt_sn_receive_regack(int sock);
Expand Down

0 comments on commit ff65892

Please sign in to comment.