Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
k-sdk
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈伟灿
k-sdk
Commits
36737fa0
Commit
36737fa0
authored
Aug 06, 2020
by
whmaizmy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【修改内容】增加子设备的消息订阅
【提交人】陈伟灿
parent
e6d4291d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
91 additions
and
39 deletions
+91
-39
application/kcloud/kcloud_data_handle.c
application/kcloud/kcloud_data_handle.c
+52
-35
application/kcloud/kcloud_main.c
application/kcloud/kcloud_main.c
+1
-1
application/kcloud/kk_topic_mng.c
application/kcloud/kk_topic_mng.c
+37
-2
application/kcloud/mqtt_api.h
application/kcloud/mqtt_api.h
+1
-1
No files found.
application/kcloud/kcloud_data_handle.c
View file @
36737fa0
...
...
@@ -5,54 +5,57 @@
#include "com_api.h"
#include "cJSON.h"
static
char
*
_kk_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
,
int
len
)
#define KK_FILTER_ADD_TOPIC "/thing/topo/add"
#define KK_FILTER_ADD_TOPIC_REPLY "/thing/topo/add_reply"
#define KK_FILTER_DELETE_TOPIC "/thing/topo/delete"
#define KK_FILTER_DELETE_TOPIC_REPLY "/thing/topo/delete_reply"
#define KK_FILTER_REGISTER_TOPIC "/thing/sub/register"
#define KK_FILTER_REGISTER_TOPIC_REPLY "/thing/sub/register_reply"
#define KK_FILTER_LOGIN_TOPIC "/thing/combine/login"
#define KK_FILTER_LOGIN_TOPIC_REPLY "/thing/combine/login_reply"
int
_kk_sendto_cloud
(
cJSON
*
root
)
{
cJSON
*
root
,
*
pTopic
,
*
pData
;
cJSON
*
pTopic
,
*
pData
;
char
*
topic
=
NULL
;
char
*
payload
=
NULL
;
root
=
cJSON_Parse
((
char
*
)
data
);
if
(
root
==
NULL
)
{
return
;
}
pTopic
=
cJSON_GetObjectItem
(
root
,
"topic"
);
if
(
pTopic
==
NULL
)
{
if
(
pTopic
==
NULL
){
return
;
}
pData
=
cJSON_GetObjectItem
(
root
,
"payload"
);
if
(
pData
==
NULL
)
{
if
(
pData
==
NULL
){
return
;
}
printf
(
"[%s][%d] topic:%s
\n
"
,
__FUNCTION__
,
__LINE__
,
pTopic
->
valuestring
);
printf
(
"[%s][%d] payload:%s
\n
"
,
__FUNCTION__
,
__LINE__
,
pData
->
valuestring
);
KK_MQTT_SendMsg
(
pTopic
->
valuestring
,(
const
char
*
)
pData
->
valuestring
);
KK_MQTT_SendMsg
(
pTopic
->
valuestring
,(
const
char
*
)
pData
->
valuestring
);
}
#define KK_FILTER_ADD_TOPIC "/thing/topo/add"
#define KK_FILTER_ADD_TOPIC_REPLY "/thing/topo/add_reply"
#define KK_FILTER_DELETE_TOPIC "/thing/topo/delete"
#define KK_FILTER_DELETE_TOPIC_REPLY "/thing/topo/delete_reply"
#define KK_FILTER_REGISTER_TOPIC "/thing/sub/register"
#define KK_FILTER_REGISTER_TOPIC_REPLY "/thing/sub/register_reply"
#define KK_FILTER_LOGIN_TOPIC "/thing/combine/login"
#define KK_FILTER_LOGIN_TOPIC_REPLY "/thing/combine/login_reply"
static
int
_check_invalid_topic
(
char
*
topic
)
void
KK_Data_FromDev
(
void
*
str
,
int
len
)
{
cJSON
*
root
,
*
cmd
;
if
(
str
==
NULL
){
return
;
}
root
=
cJSON_Parse
((
char
*
)
str
);
if
(
root
==
NULL
){
return
;
}
cmd
=
cJSON_GetObjectItem
(
root
,
"cmd"
);
if
(
cmd
==
NULL
){
_kk_sendto_cloud
(
root
);
}
else
{
KK_Subdev_Subscribe
(
root
);
}
cJSON_Delete
(
root
);
free
(
root
);
}
static
int
_check_invalid_topic
(
const
char
*
topic
)
{
if
(
strstr
(
topic
,
KK_FILTER_ADD_TOPIC
)
!=
NULL
&&
\
strstr
(
topic
,
KK_FILTER_ADD_TOPIC_REPLY
)
==
NULL
){
...
...
@@ -72,6 +75,20 @@ static int _check_invalid_topic(char* topic)
}
return
0
;
}
static
char
*
_kk_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_DevData
(
const
char
*
topic
,
const
char
*
data
)
{
if
(
_check_invalid_topic
(
topic
))
...
...
application/kcloud/kcloud_main.c
View file @
36737fa0
...
...
@@ -67,7 +67,7 @@ int main(int argc, char* argv[])
/*set the callback to get the device date to cloud*/
HAL_SetProduct_Type
(
PRODUCT_TPYE
);
HAL_SetProduct_Code
(
PRODUCT_CODE
);
kk_ipc_init
(
IPC_APP2MID
,
KK_
Sendto_CloudData
);
kk_ipc_init
(
IPC_APP2MID
,
KK_
Data_FromDev
);
rc
=
mqtt_start
();
return
rc
;
...
...
application/kcloud/kk_topic_mng.c
View file @
36737fa0
...
...
@@ -3,11 +3,46 @@
#include <stdlib.h>
#include <string.h>
#include "kk_product.h"
#include "cJSON.h"
const
char
KK_URI_SYS_PREFIX
[]
=
"/sys/%s/%s/#"
;
int
KK_Subdev_Subscribe
(
const
cJSON
*
root
)
{
int
res
=
0
;
cJSON
*
productType
=
NULL
;
cJSON
*
productCode
=
NULL
;
cJSON
*
data
=
NULL
;
cJSON
*
cmd
=
NULL
;
int
url_len
=
0
;
data
=
cJSON_GetObjectItem
(
root
,
"data"
);
if
(
data
==
NULL
){
return
-
1
;
}
productType
=
cJSON_GetObjectItem
(
data
,
"productType"
);
if
(
productType
==
NULL
){
return
-
1
;
}
productCode
=
cJSON_GetObjectItem
(
data
,
"productCode"
);
if
(
productCode
==
NULL
){
return
-
1
;
}
url_len
=
strlen
(
KK_URI_SYS_PREFIX
)
+
strlen
(
productType
->
valuestring
)
+
strlen
(
productCode
->
valuestring
)
+
1
;
char
*
url
=
malloc
(
url_len
);
if
(
url
==
NULL
)
{
return
-
1
;
}
memset
(
url
,
0
,
url_len
);
snprintf
(
url
,
url_len
,
KK_URI_SYS_PREFIX
,
productType
->
valuestring
,
productCode
->
valuestring
);
printf
(
"[%s][%d] URL:%s
\n
"
,
__FUNCTION__
,
__LINE__
,
url
);
res
=
KK_MQTT_SubTopic
(
url
);
free
(
url
);
return
res
;
}
int
_kk_client_subscribe
(
char
productType
[
PRODUCT_TYPE_LEN
],
char
productCode
[
PRODUCT_CODE_LEN
])
static
int
_kk_client_subscribe
(
char
productType
[
PRODUCT_TYPE_LEN
],
char
productCode
[
PRODUCT_CODE_LEN
])
{
int
res
=
0
,
index
=
0
,
fail_count
=
0
;
...
...
@@ -26,7 +61,7 @@ int _kk_client_subscribe(char productType[PRODUCT_TYPE_LEN], char productCode[PR
res
=
KK_MQTT_SubTopic
(
url
);
free
(
url
);
return
0
;
return
res
;
}
int
KK_Client_Gateway_Subscribe
(
void
)
...
...
application/kcloud/mqtt_api.h
View file @
36737fa0
...
...
@@ -4,7 +4,7 @@
#include "kcloud_log.h"
#include "kcloud_config.h"
extern
void
KK_
Sendto_CloudData
(
void
*
data
,
int
len
);
extern
void
KK_
Data_FromDev
(
void
*
str
,
int
len
);
extern
int
KK_MQTT_SubTopic
(
char
*
topicName
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment