Skip to content

Commit

Permalink
Merge version 2.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Feng Lee committed Dec 26, 2017
2 parents d1724e0 + 17da491 commit ab161f1
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 38 deletions.
6 changes: 3 additions & 3 deletions Makefile
@@ -1,13 +1,13 @@
PROJECT = emqx_sn
PROJECT_DESCRIPTION = EMQ X MQTT-SN Gateway
PROJECT_VERSION = 2.3.0
PROJECT_VERSION = 2.3.2

DEPS = esockd clique
dep_esockd = git https://github.com/emqtt/esockd master
dep_esockd = git https://github.com/emqtt/esockd v5.2
dep_clique = git https://github.com/emqtt/clique

BUILD_DEPS = emqx cuttlefish
dep_emqx = git git@github.com:emqx/emqx-enterprise
dep_emqx = git git@github.com:emqx/emqx-enterprise master
dep_cuttlefish = git https://github.com/emqtt/cuttlefish

ERLC_OPTS += +debug_info
Expand Down
2 changes: 2 additions & 0 deletions include/emqx_sn.hrl
Expand Up @@ -54,6 +54,8 @@
-define(SN_RC_INVALID_TOPIC_ID, 16#02).
-define(SN_RC_NOT_SUPPORTED, 16#03).

-define(SN_RC_MQTT_FAILURE, 16#80).

-define(QOS_NEG1, 3).

-type(mqtt_sn_return_code() :: ?SN_RC_ACCECPTED .. ?SN_RC_NOT_SUPPORTED).
Expand Down
39 changes: 20 additions & 19 deletions intergration_test/Makefile
@@ -1,4 +1,3 @@

.PHONY: clean, clean_result, start_broker stop_broker case1

RELX_CONF = emqx-rel/relx.config
Expand All @@ -19,24 +18,24 @@ PAHO_SRC = paho.mqtt-sn.embedded-c/MQTTSNPacket/src/MQTTSNConnectClient.c
client/int_test_result.c

INCLUDE_PATH = -Ipaho.mqtt-sn.embedded-c/MQTTSNPacket/src -Ipaho.mqtt-sn.embedded-c/MQTTSNPacket/samples -Iclient


all: clean_result $(RELX_CONF) $(PAHO_GIT) start_broker case1 case2 case3 case4 case5 stop_broker disable_qos3
@echo " "
@echo " test complete"
@echo " "

clean_result:
-rm -f client/*_FAIL.txt
-rm -f client/*_PASS.txt

-rm -f emq-relx/_rel/emqttd/log/*

start_broker:
-emqx-rel/_rel/emqx/bin/emqx stop
sleep 3
emqx-rel/_rel/emqx/bin/emqx start
sleep 5
emqx-rel/_rel/emqx/bin/emqx_ctl plugins load emqx_sn

stop_broker:
emqx-rel/_rel/emqx/bin/emqx stop

Expand Down Expand Up @@ -87,10 +86,12 @@ case5:
client/case5_qos3sub.exe &
sleep 1
client/case5_qos3pub.exe





case6:
-ps aux|grep case6_sleep|awk '{print $$2}'|xargs kill -9
gcc client/case6_sleep.c $(PAHO_SRC) $(INCLUDE_PATH) -o client/case6_sleep.exe
client/case6_sleep.exe

$(RELX_CONF):
git clone -b free https://github.com/emqx/emqx-rel.git
git clone https://github.com/emqx/emqx-sn.git
Expand All @@ -111,17 +112,20 @@ $(RELX_CONF):
python ./enable_qos3.py
@echo "start building ..."
make -C emqx-rel -f Makefile

paho:
make $(PAHO_GIT)


paho: $(PAHO_GIT)
@echo "clone paho"

$(PAHO_GIT):
git clone https://github.com/eclipse/paho.mqtt-sn.embedded-c/

r: rebuild_emq
# short for rebuild_emq
@echo "rebuild complete"

rebuild_emq:
-rm -rf emqx-rel/deps/emqx_sn/etc
-rm -rf emqx-rel/deps/emqx_sn/include
Expand All @@ -136,16 +140,13 @@ rebuild_emq:
cp -rf ../Makefile emqx-rel/deps/emqx_sn/Makefile
python ./enable_qos3.py
make -C emqx-rel -f Makefile



clean: clean_result
-rm -f client/*.exe
-rm -f client/*.o
-rm -rf emqx-rel
-rm -rf paho.mqtt-sn.embedded-c



lazy: clean_result start_broker case1 case2 case3 case4 case5 stop_broker disable_qos3
@echo "lazy work"



149 changes: 149 additions & 0 deletions intergration_test/client/case6_sleep.c
@@ -0,0 +1,149 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - clarifications and/or documentation extension
*
* Description:
* Short topic name used to avoid registration process
*******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>

#include "MQTTSNPacket.h"
#include "transport.h"

#include "int_test_result.h"


#define TLOG(fmt, ...) tlog("case6: ", fmt, ## __VA_ARGS__)





int main(int argc, char** argv)
{
int rc = 0;
int mysock;
unsigned char buf[200];
int buflen = sizeof(buf);

unsigned char payload[16];
int payloadlen = 0;
int len = 0;
int qos = 0;
int retained = 0;
short packetid = 0;
char ascii = 0;
// char *topicname = "a long topic name";
char *host = "127.0.0.1";
int port = 1884;
int i = 0;
MQTTSNString msstr;
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;

mysock = transport_open();
if(mysock < 0)
return mysock;

if (argc > 1)
host = argv[1];

if (argc > 2)
port = atoi(argv[2]);

TLOG("Sending to hostname %s port %d\n", host, port);

options.clientID.cstring = "testclientid_case1";
len = MQTTSNSerialize_connect(buf, buflen, &options);
rc = transport_sendPacketBuffer(host, port, buf, len);

/* wait for connack */
if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
{
int connack_rc = -1;

if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
{
TLOG("Unable to connect, return code %d\n", connack_rc);
goto exit;
}
else
TLOG("connected rc %d\n", connack_rc);
}
else
goto exit;



len = MQTTSNSerialize_disconnect(buf, buflen, 5);
rc = transport_sendPacketBuffer(host, port, buf, len);
/* wait for DISCONNECT */
if ((rc = MQTTSNPacket_read(buf, buflen, transport_getdata)) == MQTTSN_DISCONNECT)
{
int duration = 0;
if (MQTTSNDeserialize_disconnect(&duration, buf, buflen) != 1)
{
TLOG("Unable to diconnect, return code %d\n", duration);
goto exit;
}
else
TLOG("duration %d\n", duration);
}
else
{
TLOG("rc %d\n", rc);
goto exit;
}


sleep(2);


msstr.cstring = "testclientid_case1";
len = MQTTSNSerialize_pingreq(buf, buflen, msstr);
rc = transport_sendPacketBuffer(host, port, buf, len);
/* wait for PINGRSP */
if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PINGRESP)
{
if (MQTTSNDeserialize_pingresp(buf, buflen) != 1)
{
TLOG("Unable to ping\n");
goto exit;
}
else
TLOG("get pingresp\n");
}
else
goto exit;


mark_result(argv[0], RESULT_PASS);
transport_close();
return 0;

exit:
mark_result(argv[0], RESULT_FAIL);
transport_close();

return 0;
}




33 changes: 17 additions & 16 deletions src/emqx_sn_gateway.erl
Expand Up @@ -522,16 +522,9 @@ handle_info(Info, StateName, StateData) ->
?LOG(error, "UNEXPECTED INFO: ~p", [Info], StateData),
{next_state, StateName, StateData}.

terminate(Reason, _StateName, StateData = #state{client_id = ClientId, keepalive = KeepAlive, protocol = Proto}) ->
case Reason of
asleep_timeout -> do_publish_will(StateData);
{shutdown, keepalive_timeout} -> do_publish_will(StateData);
{shutdown, kick} -> do_publish_will(StateData);
{shutdown, conflict} -> do_publish_will(StateData);
_ -> ok
end,
emqx_sn_topic_manager:unregister_topic(ClientId),
emqx_keepalive:cancel(KeepAlive),
terminate(Reason, _StateName, #state{client_id = ClientId, keepalive = KeepAlive, protocol = Proto}) ->
emq_sn_topic_manager:unregister_topic(ClientId),
emqttd_keepalive:cancel(KeepAlive),
case {Proto, Reason} of
{undefined, _} ->
ok;
Expand Down Expand Up @@ -591,10 +584,12 @@ transform(?PUBACK_PACKET(?PUBREL, MsgId), _FuncMsgIdToTopicId) ->
transform(?PUBACK_PACKET(?PUBCOMP, MsgId), _FuncMsgIdToTopicId) ->
?SN_PUBREC_MSG(?SN_PUBCOMP, MsgId);

transform(?SUBACK_PACKET(_MsgId, _QosTable), _FuncMsgIdToTopicId)->
% SUBACK is not sent in this way
% please refer to handle_info({suback, MsgId, [GrantedQos]}, ...)
error(false);
transform(?SUBACK_PACKET(MsgId, _QosTable), _FuncMsgIdToTopicId)->
% if success, suback is sent by handle_info({suback, MsgId, [GrantedQos]}, ...)
% if failure, suback is sent in this function.
Flags = #mqtt_sn_flags{qos = 0},
?SN_SUBACK_MSG(Flags, ?SN_INVALID_TOPIC_ID, MsgId, ?SN_RC_MQTT_FAILURE);


transform(?UNSUBACK_PACKET(MsgId), _FuncMsgIdToTopicId)->
?SN_UNSUBACK_MSG(MsgId).
Expand Down Expand Up @@ -635,10 +630,16 @@ goto_asleep_state(StateData=#state{asleep_timer = AsleepTimer}, Duration) ->
{next_state, asleep, StateData#state{asleep_timer = NewTimer}, hibernate}.

shutdown(Error, StateData) ->
{stop, {shutdown, Error}, StateData}.
?LOG(error, "shutdown due to ~p", [Error], StateData),
stop({shutdown, Error}, StateData).

stop(Reason, StateData) ->
{stop, Reason, StateData}.
case Reason of
asleep_timeout -> do_publish_will(StateData);
{shutdown, keepalive_timeout} -> do_publish_will(StateData);
_ -> ok
end,
{stop, normal, StateData}.

mqttsn_to_mqtt(?SN_PUBACK) -> ?PUBACK;
mqttsn_to_mqtt(?SN_PUBREC) -> ?PUBREC;
Expand Down

0 comments on commit ab161f1

Please sign in to comment.