Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Dec 28, 2017
2 parents 4a8e4cc + 65a9c9e commit ee77c43
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
6 changes: 4 additions & 2 deletions demo/mqtt/include/demo_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#define BROKER_IP "mqtt.neucrack.com"
#define BROKER_PORT 1883
#define CLIENT_ID "Neucrack"
#define SUBSCRIBE_TOPIC "$neucrack/gprs"
#define PUBLISH_TOPIC "$neucrack/app"
#define CLIENT_USER "mqtt"
#define CLIENT_PASS "mqtt"
#define SUBSCRIBE_TOPIC "$neucrack/app"
#define PUBLISH_TOPIC "$neucrack/gprs"
#define PUBLISH_INTERVAL 10000 //10s
#define PUBLISH_PAYLOEAD "hello I'm from gprs module"

Expand Down
36 changes: 29 additions & 7 deletions demo/mqtt/src/demo_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "demo_mqtt.h"



#define MAIN_TASK_STACK_SIZE (2048 * 2)
#define MAIN_TASK_PRIORITY 0
#define MAIN_TASK_NAME "Main Test Task"
Expand All @@ -29,6 +30,7 @@ static HANDLE semMqttStart = NULL;

typedef enum{
MQTT_EVENT_CONNECTED = 0,
MQTT_EVENT_DISCONNECTED ,
MQTT_EVENT_MAX
}MQTT_Event_ID_t;

Expand All @@ -37,6 +39,13 @@ typedef struct {
MQTT_Client_t* client;
}MQTT_Event_t;

typedef enum{
MQTT_STATUS_DISCONNECTED = 0,
MQTT_STATUS_CONNECTED ,
MQTT_STATUS_MAX
}MQTT_Status_t;

MQTT_Status_t mqttStatus = MQTT_STATUS_DISCONNECTED;


static void EventDispatch(API_Event_t* pEvent)
Expand Down Expand Up @@ -113,22 +122,25 @@ void OnMqttReceiedData(void* arg, const uint8_t* data, uint16_t len, MQTT_Flags_
void OnMqttConnection(MQTT_Client_t *client, void *arg, MQTT_Connection_Status_t status)
{
Trace(1,"MQTT connection status:%d",status);
MQTT_Event_t* event = (MQTT_Event_t*)OS_Malloc(sizeof(MQTT_Event_t));
if(!event)
{
Trace(1,"MQTT no memory");
return ;
}
if(status == MQTT_CONNECTION_ACCEPTED)
{
Trace(1,"MQTT succeed connect to broker");
MQTT_Event_t* event = (MQTT_Event_t*)OS_Malloc(sizeof(MQTT_Event_t));
if(!event)
{
Trace(1,"MQTT no memory");
return ;
}
//!!! DO NOT suscribe here(interrupt function), do MQTT suscribe in task, or it will not excute
event->id = MQTT_EVENT_CONNECTED;
event->client = client;
OS_SendEvent(secondTaskHandle,event,OS_TIME_OUT_WAIT_FOREVER,OS_EVENT_PRI_NORMAL);
}
else
{
event->id = MQTT_EVENT_DISCONNECTED;
event->client = client;
OS_SendEvent(secondTaskHandle,event,OS_TIME_OUT_WAIT_FOREVER,OS_EVENT_PRI_NORMAL);
Trace(1,"MQTT connect to broker fail,error code:%d",status);
}
Trace(1,"MQTT OnMqttConnection() end");
Expand All @@ -147,7 +159,11 @@ void OnTimerPublish(void* param)
{
MQTT_Error_t err;
MQTT_Client_t* client = (MQTT_Client_t*)param;

if(mqttStatus != MQTT_STATUS_CONNECTED)
{
Trace(1,"MQTT not connected to broker! can not publish");
return;
}
Trace(1,"MQTT OnTimerPublish");
err = MQTT_Publish(client,PUBLISH_TOPIC,PUBLISH_PAYLOEAD,strlen(PUBLISH_PAYLOEAD),1,2,0,OnPublish,NULL);
if(err != MQTT_ERROR_NONE)
Expand All @@ -165,6 +181,7 @@ void SecondTaskEventDispatch(MQTT_Event_t* pEvent)
switch(pEvent->id)
{
case MQTT_EVENT_CONNECTED:
mqttStatus = MQTT_STATUS_CONNECTED;
Trace(1,"MQTT connected, now subscribe topic:%s",SUBSCRIBE_TOPIC);
MQTT_Error_t err;
MQTT_SetInPubCallback(pEvent->client, OnMqttReceived, OnMqttReceiedData, NULL);
Expand All @@ -173,6 +190,9 @@ void SecondTaskEventDispatch(MQTT_Event_t* pEvent)
Trace(1,"MQTT subscribe error, error code:%d",err);
StartTimerPublish(PUBLISH_INTERVAL,pEvent->client);
break;
case MQTT_EVENT_DISCONNECTED:
mqttStatus = MQTT_STATUS_DISCONNECTED;
break;
default:
break;
}
Expand All @@ -190,6 +210,8 @@ void SecondTask(void *pData)
MQTT_Error_t err;
memset(&ci,0,sizeof(MQTT_Connect_Info_t));
ci.client_id = CLIENT_ID;
ci.client_user = CLIENT_USER;
ci.client_pass = CLIENT_PASS;
ci.keep_alive = 60;
ci.clean_session = 1;
ci.use_ssl = false;
Expand Down
2 changes: 1 addition & 1 deletion include/api_inc/api_inc_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ typedef enum
/** Refused user credentials */
MQTT_CONNECTION_REFUSED_USERNAME_PASS = 4,
/** Refused not authorized */
MQTT_CONNECTION_REFUSED_NOT_AUTHORIZED_ = 5,
MQTT_CONNECTION_REFUSED_NOT_AUTHORIZED = 5,
/** Disconnected */
MQTT_CONNECTION_DISCONNECTED = 256,
/** Timeout */
Expand Down
2 changes: 1 addition & 1 deletion platform/csdk
Submodule csdk updated 3 files
+ SW_V1401_csdk.elf
+4,922 −4,922 SW_V1401_csdk.elf.lod
+15 −15 memd.def

0 comments on commit ee77c43

Please sign in to comment.