Commit f98fac3b authored by whmaizmy's avatar whmaizmy

【修改内容】调整kcloud 相关代码,测试用

【提交人】陈伟灿
parent c8ddb8c4
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#define ADDRESS "tcp://106.13.117.117:1883" #define ADDRESS "tcp://106.13.117.117:1883"
#define CLIENTID "1234" #define CLIENTID "1234"
#define TOPIC "cwctest" #define TOPIC "/sys/a1OYuSBt23u/aIqEbWno8yDdsjCX15iq/thing/service/property/set"
#define PAYLOAD "Hello cwc World!" #define PAYLOAD "Hello cwc World!"
#define QOS 2 #define QOS 2
......
...@@ -3,14 +3,30 @@ ...@@ -3,14 +3,30 @@
#include <string.h> #include <string.h>
#include "mqtt_api.h" #include "mqtt_api.h"
#include "com_api.h" #include "com_api.h"
#include "cJSON.h"
static char * data_create(const char *topic,const char *data)
{
cJSON *root;
char *out;
root=cJSON_CreateObject();
cJSON_AddStringToObject(root,"topic",topic);
cJSON_AddStringToObject(root,"payload",data);
out=cJSON_Print(root);
cJSON_Delete(root);
printf("[%s][%d]%s\n",__FUNCTION__,__LINE__,out);
return out;
//free(out); /* Print to text, Delete the cJSON, print it, release the string. */
}
void KK_Sendto_CloudData(void *data) void KK_Sendto_CloudData(void *data)
{ {
KK_MQTT_SendMsg(TOPIC,(const char*)data,QOS); KK_MQTT_SendMsg(TOPIC,(const char*)data);
} }
void KK_Sendto_DevData(char *data) void KK_Sendto_DevData(const char *topic,const char *data)
{ {
kk_ipc_send(IPC_APP2MID, data, strlen(data)+1); char *send_data = data_create(topic,data);
kk_ipc_send(IPC_APP2MID, send_data, strlen(send_data)+1);
} }
...@@ -63,6 +63,8 @@ int main(int argc, char* argv[]) ...@@ -63,6 +63,8 @@ int main(int argc, char* argv[])
{ {
int rc = 0; int rc = 0;
//KK_Data_Hdl_Init();
/*set the callback to get the device date to cloud*/ /*set the callback to get the device date to cloud*/
kk_ipc_init(IPC_APP2MID,KK_Sendto_CloudData); kk_ipc_init(IPC_APP2MID,KK_Sendto_CloudData);
......
...@@ -106,7 +106,7 @@ int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_messa ...@@ -106,7 +106,7 @@ int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_messa
/* not expecting any messages */ /* not expecting any messages */
INFO_PRINT("onMessageArrived topic:%s,message length:%d.\n",topicName,message->payloadlen); INFO_PRINT("onMessageArrived topic:%s,message length:%d.\n",topicName,message->payloadlen);
INFO_PRINT("payload:%s,\n",message->payload); INFO_PRINT("payload:%s,\n",message->payload);
KK_MQTT_RecvMsg((MQTTAsync)context,topicName,message->payload); KK_MQTT_RecvMsg(topicName,message->payload);
MQTTAsync_freeMessage(&message); MQTTAsync_freeMessage(&message);
MQTTAsync_free(topicName); MQTTAsync_free(topicName);
return 1; return 1;
...@@ -124,7 +124,7 @@ static void onConnectBuild(void *context, char *cause) ...@@ -124,7 +124,7 @@ static void onConnectBuild(void *context, char *cause)
{ {
int rc = 0; int rc = 0;
INFO_PRINT("onConnectBuild:%s \n",cause); INFO_PRINT("onConnectBuild:%s \n",cause);
rc = KK_MQTT_SubTopic(s_Client,TOPIC,QOS,2000); rc = KK_MQTT_SubTopic(TOPIC);
if(rc != 0) if(rc != 0)
{ {
ERROR_PRINT("KK_MQTT_SubTopic ERROR rc = %d\n",rc); ERROR_PRINT("KK_MQTT_SubTopic ERROR rc = %d\n",rc);
...@@ -219,7 +219,7 @@ MQTTAsync KK_MQTT_Connect(void) ...@@ -219,7 +219,7 @@ MQTTAsync KK_MQTT_Connect(void)
return s_Client; return s_Client;
} }
int KK_MQTT_SubTopic(MQTTAsync handle,char *topicName,int qos,int waitTimeout) int KK_MQTT_SubTopic(char *topicName)
{ {
INFO_PRINT("to subtopic:%s \n",topicName); INFO_PRINT("to subtopic:%s \n",topicName);
...@@ -230,7 +230,7 @@ int KK_MQTT_SubTopic(MQTTAsync handle,char *topicName,int qos,int waitTimeout) ...@@ -230,7 +230,7 @@ int KK_MQTT_SubTopic(MQTTAsync handle,char *topicName,int qos,int waitTimeout)
opts.onFailure = onOptFail; opts.onFailure = onOptFail;
opts.context = (void*)OPT_SUB; opts.context = (void*)OPT_SUB;
if ((rc = MQTTAsync_subscribe(handle,topicName, qos, &opts)) != MQTTASYNC_SUCCESS) if ((rc = MQTTAsync_subscribe(s_Client,topicName, QOS, &opts)) != MQTTASYNC_SUCCESS)
{ {
ERROR_PRINT("Failed to start subscribe, return code:%d.\n", rc); ERROR_PRINT("Failed to start subscribe, return code:%d.\n", rc);
return -1; return -1;
...@@ -238,7 +238,7 @@ int KK_MQTT_SubTopic(MQTTAsync handle,char *topicName,int qos,int waitTimeout) ...@@ -238,7 +238,7 @@ int KK_MQTT_SubTopic(MQTTAsync handle,char *topicName,int qos,int waitTimeout)
return 0; return 0;
} }
int KK_MQTT_SendMsg(char *topicName,const char *payload,int qos) int KK_MQTT_SendMsg(char *topicName,const char *payload)
{ {
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
...@@ -250,10 +250,10 @@ int KK_MQTT_SendMsg(char *topicName,const char *payload,int qos) ...@@ -250,10 +250,10 @@ int KK_MQTT_SendMsg(char *topicName,const char *payload,int qos)
pubmsg.payload = (void*)payload; pubmsg.payload = (void*)payload;
pubmsg.payloadlen = strlen(payload); pubmsg.payloadlen = strlen(payload);
pubmsg.qos = qos; pubmsg.qos = QOS;
pubmsg.retained = 0; pubmsg.retained = 0;
INFO_PRINT("mqtt send payload len:%d,qos:%d.\n",pubmsg.payloadlen,qos); INFO_PRINT("mqtt send payload len:%d.\n",pubmsg.payloadlen);
if ((rc = MQTTAsync_sendMessage(s_Client, topicName, &pubmsg, &opts)) != MQTTASYNC_SUCCESS) if ((rc = MQTTAsync_sendMessage(s_Client, topicName, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
{ {
...@@ -262,18 +262,18 @@ int KK_MQTT_SendMsg(char *topicName,const char *payload,int qos) ...@@ -262,18 +262,18 @@ int KK_MQTT_SendMsg(char *topicName,const char *payload,int qos)
} }
return rc; return rc;
} }
int KK_MQTT_RecvMsg(MQTTAsync handle,const char *topicName,const char *payload) int KK_MQTT_RecvMsg(const char *topicName,const char *payload)
{ {
if(topicName == NULL || payload ==NULL) if(topicName == NULL || payload ==NULL)
{ {
ERROR_PRINT("PARAM ERROR\n"); ERROR_PRINT("PARAM ERROR\n");
return -1; return -1;
} }
KK_Sendto_DevData(payload); KK_Sendto_DevData(topicName,payload);
return 0; return 0;
} }
int KK_MQTT_UnsubTopic(MQTTAsync handle,const char *topicName) int KK_MQTT_UnsubTopic(const char *topicName)
{ {
INFO_PRINT("to unsubtopic:%s \n",topicName); INFO_PRINT("to unsubtopic:%s \n",topicName);
...@@ -284,7 +284,7 @@ int KK_MQTT_UnsubTopic(MQTTAsync handle,const char *topicName) ...@@ -284,7 +284,7 @@ int KK_MQTT_UnsubTopic(MQTTAsync handle,const char *topicName)
opts.onFailure = onOptFail; opts.onFailure = onOptFail;
opts.context = (void*)OPT_UNSUB; opts.context = (void*)OPT_UNSUB;
if ((rc = MQTTAsync_unsubscribe(handle,topicName,&opts)) != MQTTASYNC_SUCCESS) if ((rc = MQTTAsync_unsubscribe(s_Client,topicName,&opts)) != MQTTASYNC_SUCCESS)
{ {
ERROR_PRINT("Failed to start unubscribe, return code:%d.\n", rc); ERROR_PRINT("Failed to start unubscribe, return code:%d.\n", rc);
return -1; return -1;
......
...@@ -6,13 +6,13 @@ ...@@ -6,13 +6,13 @@
extern void KK_Sendto_CloudData(void *data); extern void KK_Sendto_CloudData(void *data);
extern int KK_MQTT_SubTopic(MQTTAsync handle,char *topicName,int qos,int waitTimeout); extern int KK_MQTT_SubTopic(char *topicName);
extern int KK_MQTT_SendMsg(char *topicName,const char *payload,int qos); extern int KK_MQTT_SendMsg(char *topicName,const char *payload);
extern int KK_MQTT_RecvMsg(MQTTAsync handle,const char *topicName,const char *payload); extern int KK_MQTT_RecvMsg(const char *topicName,const char *payload);
extern int KK_MQTT_UnsubTopic(MQTTAsync handle,const char *topicName); extern int KK_MQTT_UnsubTopic(const char *topicName);
extern MQTTAsync KK_MQTT_Connect(void); extern MQTTAsync KK_MQTT_Connect(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment