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
d09c540f
Commit
d09c540f
authored
Aug 27, 2020
by
黄振令
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【修改内容】1. 修改网关添加逻辑;2. 增加网关与主机tcp通道
【提交人】huang.zhenling
parent
f187487c
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
1214 additions
and
439 deletions
+1214
-439
common/api/com_api.c
common/api/com_api.c
+4
-4
common/api/tcp_channel.c
common/api/tcp_channel.c
+609
-0
common/hal/HAL_OS_linux.c
common/hal/HAL_OS_linux.c
+2
-1
midware/midware/midware.c
midware/midware/midware.c
+97
-4
platform/zigbee/app/builder/Z3GatewayHost/kk_test.c
platform/zigbee/app/builder/Z3GatewayHost/kk_test.c
+406
-396
platform/zigbee/app/builder/Z3GatewayHost/kk_test.h
platform/zigbee/app/builder/Z3GatewayHost/kk_test.h
+5
-0
platform/zigbee/app/builder/Z3GatewayHost/libapi_com.so
platform/zigbee/app/builder/Z3GatewayHost/libapi_com.so
+0
-0
platform/zigbee/app/builder/Z3GatewayHost/rpc_api/src/rpc_interface_parse.c
...p/builder/Z3GatewayHost/rpc_api/src/rpc_interface_parse.c
+91
-34
No files found.
common/api/com_api.c
View file @
d09c540f
...
@@ -39,11 +39,11 @@ typedef struct {
...
@@ -39,11 +39,11 @@ typedef struct {
}
Bloop_ctrl_t
;
}
Bloop_ctrl_t
;
Bloop_ctrl_t
Bloop_ctrl
;
static
Bloop_ctrl_t
Bloop_ctrl
;
Bloop_ctrl_t
Mloop_ctrl
;
static
Bloop_ctrl_t
Mloop_ctrl
;
struct
ev_loop
*
gloop
=
NULL
;
st
atic
st
ruct
ev_loop
*
gloop
=
NULL
;
pthread_t
g_pTh
=
NULL
;
static
pthread_t
g_pTh
=
NULL
;
static
char
*
_parse_data_by_subscribe
(
char
*
data
,
int
len
,
int
*
outLen
,
void
**
chlMark
){
static
char
*
_parse_data_by_subscribe
(
char
*
data
,
int
len
,
int
*
outLen
,
void
**
chlMark
){
...
...
common/api/tcp_channel.c
0 → 100644
View file @
d09c540f
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "com_api.h"
#define CCU_TCP_PORT 16565
#define BUFFER_SIZE 1024
#define MAX_LISTEN_NUM 10
#define MAX_IP_LEN 16
#define DEVICE_CODE_LEN 33
typedef
struct
{
char
deviceCode
[
DEVICE_CODE_LEN
];
char
ip
[
MAX_IP_LEN
];
int
sock
;
int
isConnect
;
}
kk_tcp_ctrl_t
;
static
kk_tcp_ctrl_t
g_tcp_ctrl
[
MAX_LISTEN_NUM
];
static
struct
ev_loop
*
g_loop
=
NULL
;
static
pthread_t
g_pTh
=
NULL
;
static
int
g_init
=
0
;
static
ipc_cb
*
g_cb
=
NULL
;
static
struct
ev_io
w_accept
;
static
int
get_idx_by_ip
(
char
ip
[
MAX_IP_LEN
]){
int
i
=
0
;
if
(
ip
==
NULL
||
strlen
(
ip
)
==
0
){
return
-
1
;
}
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
strcmp
(
ip
,
g_tcp_ctrl
[
i
].
ip
)
==
0
){
return
i
;
}
}
return
-
1
;
}
static
int
get_sock_by_deviceCode
(
char
deviceCode
[
DEVICE_CODE_LEN
]){
int
i
=
0
;
if
(
deviceCode
==
NULL
||
strlen
(
deviceCode
)
==
0
){
return
-
1
;
}
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
strcmp
(
deviceCode
,
g_tcp_ctrl
[
i
].
deviceCode
)
==
0
){
return
g_tcp_ctrl
[
i
].
sock
;
}
}
return
-
1
;
}
static
kk_tcp_ctrl_t
*
get_channel_by_ip
(
char
ip
[
MAX_IP_LEN
]){
int
i
=
0
;
if
(
ip
==
NULL
||
strlen
(
ip
)
==
0
){
return
NULL
;
}
printf
(
"[%s] ip=%s
\n
"
,
__FUNCTION__
,
ip
);
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
strcmp
(
ip
,
g_tcp_ctrl
[
i
].
ip
)
==
0
){
printf
(
"[%s] idx=%d ip=%s sock=%d
\n
"
,
__FUNCTION__
,
i
,
ip
,
g_tcp_ctrl
[
i
].
sock
);
return
&
g_tcp_ctrl
[
i
];
}
}
return
NULL
;
}
static
int
set_status_by_ip
(
char
ip
[
MAX_IP_LEN
],
int
status
){
int
i
=
0
;
if
(
ip
==
NULL
||
strlen
(
ip
)
==
0
){
return
-
1
;
}
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
strcmp
(
ip
,
g_tcp_ctrl
[
i
].
ip
)
==
0
){
g_tcp_ctrl
[
i
].
isConnect
=
status
;
return
0
;
}
}
return
-
1
;
}
static
int
reset_by_sock
(
int
sock
){
int
i
=
0
;
if
(
sock
<
0
){
return
-
1
;
}
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
sock
==
g_tcp_ctrl
[
i
].
sock
){
g_tcp_ctrl
[
i
].
isConnect
=
0
;
g_tcp_ctrl
[
i
].
isConnect
=
-
1
;
return
0
;
}
}
return
-
1
;
}
static
int
set_sock_by_ip
(
char
ip
[
MAX_IP_LEN
],
int
sock
){
int
i
=
0
;
if
(
ip
==
NULL
||
strlen
(
ip
)
==
0
){
return
-
1
;
}
printf
(
"[%s] ip=%s
\n
"
,
__FUNCTION__
,
ip
);
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
strcmp
(
ip
,
g_tcp_ctrl
[
i
].
ip
)
==
0
){
g_tcp_ctrl
[
i
].
sock
=
sock
;
printf
(
"[%s] idx=%d ip=%s sock=%d
\n
"
,
__FUNCTION__
,
i
,
ip
,
g_tcp_ctrl
[
i
].
sock
);
return
0
;
}
}
return
-
1
;
}
int
kk_is_tcp_channel
(
char
devCode
[
DEVICE_CODE_LEN
]){
int
i
=
0
;
if
(
devCode
==
NULL
||
strlen
(
devCode
)
==
0
){
return
-
1
;
}
//printf("[%s] devCode=%s \n", __FUNCTION__,devCode);
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
strcmp
(
devCode
,
g_tcp_ctrl
[
i
].
deviceCode
)
==
0
){
printf
(
"[%s] idx=%d ip=%s sock=%d
\n
"
,
__FUNCTION__
,
i
,
devCode
,
g_tcp_ctrl
[
i
].
sock
);
return
i
;
}
}
return
-
1
;
}
int
kk_set_tcp_channel
(
char
devCode
[
DEVICE_CODE_LEN
],
char
ip
[
MAX_IP_LEN
]){
int
i
=
0
;
int
isEmptyIdx
=
-
1
;
if
(
devCode
==
NULL
||
strlen
(
devCode
)
==
0
||
ip
==
NULL
||
strlen
(
ip
)
==
0
){
printf
(
"paramenter error
\n
"
);
return
-
1
;
}
for
(;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
strcmp
(
devCode
,
g_tcp_ctrl
[
i
].
deviceCode
)
==
0
){
strncpy
(
g_tcp_ctrl
[
i
].
ip
,
ip
,
strlen
(
ip
));
printf
(
"find and replace it [%d][%s][%s]
\n
"
,
i
,
g_tcp_ctrl
[
i
].
ip
,
devCode
);
break
;
}
if
(
strlen
(
g_tcp_ctrl
[
i
].
deviceCode
)
==
0
&&
isEmptyIdx
==
-
1
){
isEmptyIdx
=
i
;
}
}
if
(
i
<
MAX_LISTEN_NUM
){
return
0
;
}
if
(
isEmptyIdx
!=
-
1
){
strncpy
(
g_tcp_ctrl
[
isEmptyIdx
].
ip
,
ip
,
strlen
(
ip
));
strncpy
(
g_tcp_ctrl
[
isEmptyIdx
].
deviceCode
,
devCode
,
strlen
(
devCode
));
printf
(
"idx deviceCode ip[%d][%s][%s]"
,
isEmptyIdx
,
g_tcp_ctrl
[
isEmptyIdx
].
deviceCode
,
g_tcp_ctrl
[
isEmptyIdx
].
ip
);
return
0
;
}
return
-
1
;
}
/*初始化服务端*/
static
int
server_socket_init
(
int
*
sd
,
char
*
ipaddr
,
uint16_t
port
)
{
//创建socket
int
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
-
1
==
sock
){
printf
(
"error socket
\n
"
);
goto
err1
;
}
//设置立即释放端口并可以再次使用
int
reuse
=
1
;
if
(
-
1
==
setsockopt
(
sock
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
reuse
,
sizeof
(
reuse
))){
printf
(
"error setsockopt
\n
"
);
goto
err2
;
}
//设置为非阻塞
if
(
-
1
==
fcntl
(
sock
,
F_SETFL
,
fcntl
(
sock
,
F_GETFL
)
|
O_NONBLOCK
)){
printf
(
"================== fcntl
\n
"
);
goto
err2
;
}
struct
sockaddr_in
addr
;
memset
(
&
addr
,
0
,
sizeof
(
addr
));
addr
.
sin_family
=
AF_INET
;
addr
.
sin_port
=
htons
(
port
);
if
(
NULL
==
ipaddr
)
{
addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
}
else
{
addr
.
sin_addr
.
s_addr
=
inet_addr
(
ipaddr
);
}
//绑定监听
if
(
-
1
==
bind
(
sock
,
(
struct
sockaddr
*
)
&
addr
,
sizeof
(
addr
))){
printf
(
"error bind
\n
"
);
goto
err2
;
}
if
(
-
1
==
listen
(
sock
,
MAX_LISTEN_NUM
)){
printf
(
"error listen
\n
"
);
goto
err2
;
}
*
sd
=
sock
;
return
0
;
err2:
close
(
sock
);
err1:
return
-
1
;
}
/*读回调*/
static
void
read_cb
(
struct
ev_loop
*
loop
,
struct
ev_io
*
watcher
,
int
revents
)
{
printf
(
"================== read_cb
\n
"
);
char
buffer
[
BUFFER_SIZE
]
=
{
0
};
if
(
EV_ERROR
&
revents
)
{
printf
(
"read got invalid event...
\r\n
"
);
return
;
}
int
res
=
0
;
kk_tcp_ctrl_t
*
tcp_ctrl
=
(
kk_tcp_ctrl_t
*
)
watcher
->
data
;
int32_t
bytes
=
read
(
watcher
->
fd
,
buffer
,
sizeof
(
buffer
));
if
(
-
1
==
bytes
)
{
//tcp Error
if
(
EINTR
!=
errno
&&
EAGAIN
!=
errno
)
{
res
=
1
;
}
}
else
if
(
0
==
bytes
)
{
//tcp Close
res
=
2
;
}
if
(
0
!=
res
)
{
//关闭事件循环并释放watcher
printf
(
"TCP CLOSE
\r\n
"
);
reset_by_sock
(
watcher
->
fd
);
ev_io_stop
(
loop
,
watcher
);
free
(
watcher
);
}
else
{
//intf("READ:\r\n %s\r\n", buffer);
printf
(
"read_cb deviceCode ip sock [%s][%s][%d]"
,
tcp_ctrl
->
deviceCode
,
tcp_ctrl
->
ip
,
tcp_ctrl
->
sock
);
if
(
g_cb
!=
NULL
){
g_cb
(
buffer
,
bytes
,
tcp_ctrl
->
deviceCode
);
}
}
}
/*accept回调函数*/
static
void
accept_cb
(
struct
ev_loop
*
loop
,
struct
ev_io
*
watcher
,
int
revents
)
{
printf
(
"================== accept_cb
\n
"
);
struct
sockaddr_in
client_addr
;
socklen_t
client_len
=
sizeof
(
client_addr
);
if
(
EV_ERROR
&
revents
)
{
printf
(
"accept got invalid event...
\r\n
"
);
return
;
}
//accept连接
int
sock
=
accept
(
watcher
->
fd
,
(
struct
sockaddr
*
)
&
client_addr
,
&
client_len
);
if
(
-
1
==
sock
)
{
return
;
}
//设置非阻塞
if
(
-
1
==
fcntl
(
sock
,
F_SETFL
,
fcntl
(
sock
,
F_GETFL
)
|
O_NONBLOCK
))
{
close
(
sock
);
return
;
}
if
(
set_sock_by_ip
(
inet_ntoa
(
client_addr
.
sin_addr
),
sock
)
<
0
){
//ip无效,reject it
printf
(
"This ip[%s][%u] is invaild, reject it!
\r\n
"
,
inet_ntoa
(
client_addr
.
sin_addr
),
client_addr
.
sin_port
);
close
(
sock
);
return
;
}
printf
(
"Successfully connected with client: %s:%u
\r\n
"
,
\
inet_ntoa
(
client_addr
.
sin_addr
),
client_addr
.
sin_port
);
//加入事件循环
struct
ev_io
*
w_client
=
(
struct
ev_io
*
)
malloc
(
sizeof
(
struct
ev_io
));
if
(
w_client
==
NULL
){
printf
(
"malloc w_client failed
\r\n
"
);
return
;
}
w_client
->
data
=
get_channel_by_ip
(
inet_ntoa
(
client_addr
.
sin_addr
));
ev_io_init
(
w_client
,
read_cb
,
sock
,
EV_READ
);
ev_io_start
(
loop
,
w_client
);
}
/*int run()
{
int sd;
struct ev_io w_accept;
g_loop = ev_loop_new(EVBACKEND_EPOLL);
if (NULL == g_loop) {
printf("loop create failed\r\n");
return -1;
}
if (server_socket_init(&sd, NULL, CCU_TCP_PORT) < 0) {
printf("server init failed\r\n");
return -1;
}
ev_io_init(&w_accept, accept_cb, sd, EV_READ);
ev_io_start(g_loop, &w_accept);
ev_run(g_loop, 0);
return 0;
}*/
static
void
loop_tcp_thread
(
void
*
arg
){
printf
(
"loop_tcp_thread start!
\r\n
"
);
int
sd
;
g_loop
=
ev_loop_new
(
EVBACKEND_EPOLL
);
if
(
NULL
==
g_loop
)
{
printf
(
"loop create failed
\r\n
"
);
return
-
1
;
}
if
(
server_socket_init
(
&
sd
,
NULL
,
CCU_TCP_PORT
)
<
0
)
{
printf
(
"server init failed
\r\n
"
);
return
-
1
;
}
ev_io_init
(
&
w_accept
,
accept_cb
,
sd
,
EV_READ
);
ev_io_start
(
g_loop
,
&
w_accept
);
ev_run
(
g_loop
,
0
);
close
(
sd
);
printf
(
"loop_tcp_thread================== end
\n
"
);
}
int
kk_tcp_channel_ser_send
(
char
*
data
,
int
len
,
char
chalMark
[
DEVICE_CODE_LEN
]){
int
ret
=
0
;
if
(
data
!=
NULL
){
int
sd
=
get_sock_by_deviceCode
(
chalMark
);
if
(
sd
>
-
1
){
ret
=
write
(
sd
,
data
,
len
);
if
(
ret
<
0
){
printf
(
"[%s] write failed!!!!
\n
"
,
__FUNCTION__
);
}
}
}
}
int
kk_TCP_channel_init
(
ipc_cb
cb
)
{
int
i
=
0
;
int
sd
;
if
(
g_init
==
1
){
printf
(
"kk_TCP_channel_init has been inited
\n
"
);
return
-
1
;
}
g_init
=
1
;
memset
(
g_tcp_ctrl
,
0
,
sizeof
(
kk_tcp_ctrl_t
)
*
MAX_LISTEN_NUM
);
for
(
i
=
0
;
i
<
MAX_LISTEN_NUM
;
i
++
){
g_tcp_ctrl
[
i
].
sock
=
-
1
;
}
if
(
g_pTh
==
NULL
&&
0
!=
pthread_create
(
&
g_pTh
,
NULL
,
loop_tcp_thread
,
NULL
))
{
printf
(
"create pthread failed
\r\n
"
);
return
-
1
;
};
g_cb
=
cb
;
return
0
;
}
int
kk_TCP_channel_deinit
(
ipc_type
type
)
{
int
i
=
0
;
for
(
i
=
0
;
i
<
MAX_LISTEN_NUM
;
i
++
){
if
(
g_tcp_ctrl
[
i
].
sock
>
-
1
){
//close(g_tcp_ctrl[i].sock);
}
}
ev_io_stop
(
g_loop
,
&
w_accept
);
ev_break
(
g_loop
,
EVBREAK_ALL
);
pthread_exit
(
g_pTh
);
ev_loop_destroy
(
g_loop
);
g_loop
=
NULL
;
g_pTh
=
NULL
;
g_init
=
0
;
return
0
;
}
void
*
_MutexCreate
(
void
)
{
int
err_num
;
pthread_mutex_t
*
mutex
=
(
pthread_mutex_t
*
)
malloc
(
sizeof
(
pthread_mutex_t
));
if
(
NULL
==
mutex
)
{
return
NULL
;
}
if
(
0
!=
(
err_num
=
pthread_mutex_init
(
mutex
,
NULL
)))
{
printf
(
"create mutex failed"
);
free
(
mutex
);
return
NULL
;
}
return
mutex
;
}
void
_MutexDestroy
(
void
*
mutex
)
{
int
err_num
;
if
(
!
mutex
)
{
printf
(
"mutex want to destroy is NULL!"
);
return
;
}
if
(
0
!=
(
err_num
=
pthread_mutex_destroy
((
pthread_mutex_t
*
)
mutex
)))
{
printf
(
"destroy mutex failed"
);
}
free
(
mutex
);
}
void
_MutexLock
(
void
*
mutex
)
{
int
err_num
;
if
(
0
!=
(
err_num
=
pthread_mutex_lock
((
pthread_mutex_t
*
)
mutex
)))
{
printf
(
"lock mutex failed: - '%s' (%d)"
,
strerror
(
err_num
),
err_num
);
}
}
void
_MutexUnlock
(
void
*
mutex
)
{
int
err_num
;
if
(
0
!=
(
err_num
=
pthread_mutex_unlock
((
pthread_mutex_t
*
)
mutex
)))
{
printf
(
"unlock mutex failed - '%s' (%d)"
,
strerror
(
err_num
),
err_num
);
}
}
//client
/*初始化服务端*/
typedef
struct
{
void
*
mutex
;
int
sd
;
ipc_cb
*
cb
;
}
kk_tcp_client_t
;
static
kk_tcp_client_t
g_client_ctrl
;
static
int
_init_client
(){
memset
(
&
g_client_ctrl
,
0
,
sizeof
(
kk_tcp_client_t
));
/* Create Mutex */
g_client_ctrl
.
mutex
=
_MutexCreate
();
if
(
g_client_ctrl
.
mutex
==
NULL
)
{
return
-
1
;
}
g_client_ctrl
.
sd
=
-
1
;
}
static
void
loop_tcp_client_thread
(
void
*
arg
){
printf
(
"loop_tcp_client_thread start!
\r\n
"
);
char
buf
[
1024
]
=
{
0
};
int
ret
=
0
;
while
(
1
){
_MutexLock
(
g_client_ctrl
.
mutex
);
ret
=
read
(
g_client_ctrl
.
sd
,
buf
,
sizeof
(
buf
));
_MutexUnlock
(
g_client_ctrl
.
mutex
);
if
(
-
1
==
ret
){
//printf("=================read error \n");
//break ;
}
else
if
(
ret
>
0
){
printf
(
"buf = %s
\n
"
,
buf
);
if
(
g_client_ctrl
.
cb
!=
NULL
){
g_client_ctrl
.
cb
(
buf
,
ret
,
""
);
}
}
usleep
(
100000
);
}
printf
(
"loop_tcp_client_thread================== end
\n
"
);
}
static
int
client_socket_init
(
int
*
sd
,
char
*
ipaddr
,
uint16_t
port
)
{
//创建socket
int
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
-
1
==
sock
){
printf
(
"error socket
\n
"
);
goto
err1
;
}
//设置立即释放端口并可以再次使用
int
reuse
=
1
;
if
(
-
1
==
setsockopt
(
sock
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
reuse
,
sizeof
(
reuse
))){
printf
(
"error setsockopt
\n
"
);
goto
err2
;
}
//设置为非阻塞
if
(
-
1
==
fcntl
(
sock
,
F_SETFL
,
fcntl
(
sock
,
F_GETFL
)
|
O_NONBLOCK
)){
printf
(
"================== fcntl
\n
"
);
goto
err2
;
}
struct
sockaddr_in
addr
;
memset
(
&
addr
,
0
,
sizeof
(
addr
));
addr
.
sin_family
=
AF_INET
;
addr
.
sin_port
=
htons
(
port
);
if
(
NULL
==
ipaddr
)
{
addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
}
else
{
addr
.
sin_addr
.
s_addr
=
inet_addr
(
ipaddr
);
}
//连接
int
retry
=
0
;
for
(;
retry
<
10
;
retry
++
){
if
(
-
1
!=
connect
(
sock
,
(
struct
sockaddr
*
)
&
addr
,
sizeof
(
addr
))){
break
;
}
printf
(
"==================connect retry=%d
\n
"
,
retry
);
sleep
(
1
);
}
if
(
retry
>=
10
){
printf
(
"==================connect failed
\n
"
);
goto
err2
;
}
printf
(
"==================[%s] ok sock=%d
\n
"
,
__FUNCTION__
,
sock
);
*
sd
=
sock
;
return
0
;
err2:
close
(
sock
);
err1:
return
-
1
;
}
int
kk_tcp_client_send
(
char
*
data
,
int
len
){
int
ret
=
0
;
if
(
g_client_ctrl
.
sd
>
-
1
&&
data
!=
NULL
){
_MutexLock
(
g_client_ctrl
.
mutex
);
ret
=
write
(
g_client_ctrl
.
sd
,
data
,
len
);
_MutexUnlock
(
g_client_ctrl
.
mutex
);
if
(
ret
<
0
){
printf
(
"[%s] write failed!!!!
\n
"
,
__FUNCTION__
);
}
}
}
int
kk_tcp_client_init
(
char
ip
[
MAX_IP_LEN
],
int
port
,
ipc_cb
cb
)
{
kk_tcp_client_deinit
();
_init_client
();
if
(
-
1
==
client_socket_init
(
&
g_client_ctrl
.
sd
,
ip
,
port
)){
printf
(
"connect failed
\n
"
);
return
-
1
;
}
if
(
g_pTh
==
NULL
&&
0
!=
pthread_create
(
&
g_pTh
,
NULL
,
loop_tcp_client_thread
,
NULL
))
{
printf
(
"create pthread failed
\r\n
"
);
return
-
1
;
};
g_client_ctrl
.
cb
=
cb
;
}
kk_tcp_client_deinit
(){
if
(
g_client_ctrl
.
sd
>
-
1
){
close
(
g_client_ctrl
.
sd
);
}
_MutexDestroy
(
g_client_ctrl
.
mutex
);
}
common/hal/HAL_OS_linux.c
View file @
d09c540f
...
@@ -236,7 +236,8 @@ int HAL_GetDevice_Code(_OU_ char *device_code)
...
@@ -236,7 +236,8 @@ int HAL_GetDevice_Code(_OU_ char *device_code)
{
{
int
len
=
strlen
(
g_device_code
);
int
len
=
strlen
(
g_device_code
);
memset
(
device_code
,
0x0
,
DEVICE_CODE_LEN
);
memset
(
device_code
,
0x0
,
DEVICE_CODE_LEN
);
strncpy
(
device_code
,
g_device_code
,
len
);
//strncpy(device_code, g_device_code, len);
strncpy
(
device_code
,
"CCU_66666"
,
strlen
(
"CCU_66666"
));
return
len
;
return
len
;
}
}
int
HAL_GetVersion
(
_OU_
char
*
version
)
int
HAL_GetVersion
(
_OU_
char
*
version
)
...
...
midware/midware/midware.c
View file @
d09c540f
...
@@ -43,6 +43,20 @@ static int _kk_filter_to_plat(const char* msgtype)
...
@@ -43,6 +43,20 @@ static int _kk_filter_to_plat(const char* msgtype)
}
}
return
0
;
return
0
;
}
}
void
kk_sendData2gw
(
void
*
data
,
int
len
,
char
*
chalMark
){
if
(
chalMark
==
NULL
||
strlen
(
chalMark
)
==
0
){
ERROR_PRINT
(
" chalMark is null"
);
return
;
}
if
(
kk_is_tcp_channel
(
chalMark
)
>
-
1
){
kk_tcp_channel_ser_send
(
data
,
len
,
chalMark
);
}
else
{
kk_ipc_send_ex
(
IPC_MID2PLAT
,
data
,
len
,
chalMark
);
}
}
void
mid_cb
(
void
*
data
,
int
len
){
void
mid_cb
(
void
*
data
,
int
len
){
if
(
data
!=
NULL
){
if
(
data
!=
NULL
){
...
@@ -78,7 +92,7 @@ void mid_cb(void* data, int len){
...
@@ -78,7 +92,7 @@ void mid_cb(void* data, int len){
int
devType
=
0
;
int
devType
=
0
;
dm_mgr_get_devicetype_by_devicecode
(
deviceCode
->
valuestring
,
&
devType
);
dm_mgr_get_devicetype_by_devicecode
(
deviceCode
->
valuestring
,
&
devType
);
if
(
devType
==
KK_DM_DEVICE_GATEWAY
){
if
(
devType
==
KK_DM_DEVICE_GATEWAY
){
kk_
ipc_send_ex
(
IPC_MID2PLAT
,
data
,
strlen
(
data
),
deviceCode
->
valuestring
);
//send to gw itself
kk_
sendData2gw
(
data
,
strlen
(
data
),
deviceCode
->
valuestring
);
//send to gw itself
}
else
if
(
devType
==
KK_DM_DEVICE_SUBDEV
){
}
else
if
(
devType
==
KK_DM_DEVICE_SUBDEV
){
dm_mgr_dev_node_t
*
gw_node
=
NULL
;
dm_mgr_dev_node_t
*
gw_node
=
NULL
;
res
=
dm_mgr_get_device_by_devicecode
(
deviceCode
->
valuestring
,
&
gw_node
);
res
=
dm_mgr_get_device_by_devicecode
(
deviceCode
->
valuestring
,
&
gw_node
);
...
@@ -88,7 +102,7 @@ void mid_cb(void* data, int len){
...
@@ -88,7 +102,7 @@ void mid_cb(void* data, int len){
cJSON_Delete
(
info
);
cJSON_Delete
(
info
);
return
;
return
;
}
}
kk_
ipc_send_ex
(
IPC_MID2PLAT
,
data
,
strlen
(
data
),
gw_node
->
fatherDeviceCode
);
//send to sub device
kk_
sendData2gw
(
data
,
strlen
(
data
),
gw_node
->
fatherDeviceCode
);
//send to sub device
}
}
else
{
else
{
ERROR_PRINT
(
"wrong type
\n
"
);
ERROR_PRINT
(
"wrong type
\n
"
);
...
@@ -134,6 +148,14 @@ void mid2p_cb(void* data, int len, char* chalMark){
...
@@ -134,6 +148,14 @@ void mid2p_cb(void* data, int len, char* chalMark){
}
}
}
}
void
gw2mid_cb
(
void
*
data
,
int
len
,
char
*
chalMark
){
if
(
data
!=
NULL
){
printf
(
"gw2mid_cb chalMark=%s, data: %s RECEIVED
\r\n
"
,
chalMark
,
data
);
mid2p_cb
(
data
,
len
,
chalMark
);
}
}
void
kk_platMsg_handle
(
void
*
data
,
char
*
chalMark
){
void
kk_platMsg_handle
(
void
*
data
,
char
*
chalMark
){
char
*
out
;
char
*
out
;
...
@@ -171,6 +193,8 @@ void kk_platMsg_handle(void* data, char* chalMark){
...
@@ -171,6 +193,8 @@ void kk_platMsg_handle(void* data, char* chalMark){
goto
error
;
goto
error
;
}
}
dm_mgr_update_timestamp_by_devicecode
(
info_dcode
->
valuestring
,
HAL_UptimeMs
());
if
(
strcmp
(
msgType
->
valuestring
,
"/thing/topo/add"
)
==
0
){
if
(
strcmp
(
msgType
->
valuestring
,
"/thing/topo/add"
)
==
0
){
proCode
=
cJSON_GetObjectItem
(
jsonPay
,
"productCode"
);
proCode
=
cJSON_GetObjectItem
(
jsonPay
,
"productCode"
);
devCode
=
cJSON_GetObjectItem
(
jsonPay
,
"deviceCode"
);
devCode
=
cJSON_GetObjectItem
(
jsonPay
,
"deviceCode"
);
...
@@ -297,7 +321,7 @@ void *ota_dispatch_yield(void *args)
...
@@ -297,7 +321,7 @@ void *ota_dispatch_yield(void *args)
#define UDP_LAN_PORT 25556
#define UDP_LAN_PORT 25556
#define UDP_LAN_PORT_HOST 25555
#define UDP_LAN_PORT_HOST 25555
#define test_
void
*
udp_dispatch_yield
(
void
*
args
){
void
*
udp_dispatch_yield
(
void
*
args
){
...
@@ -354,6 +378,7 @@ void *udp_dispatch_yield(void *args){
...
@@ -354,6 +378,7 @@ void *udp_dispatch_yield(void *args){
char
szDec
[
1024
]
=
{
0
};
char
szDec
[
1024
]
=
{
0
};
char
host_ip
[
32
]
=
{
0
};
char
host_ip
[
32
]
=
{
0
};
char
mac
[
32
]
=
{
0
};
char
mac
[
32
]
=
{
0
};
char
device_code
[
DEVICE_CODE_LEN
]
=
{
0
};
int
devId
=
0
;
int
devId
=
0
;
while
(
1
)
while
(
1
)
...
@@ -372,6 +397,74 @@ void *udp_dispatch_yield(void *args){
...
@@ -372,6 +397,74 @@ void *udp_dispatch_yield(void *args){
DEBUG_PRINT
(
"lan recmsg: %s
\n
"
,
szDec
);
DEBUG_PRINT
(
"lan recmsg: %s
\n
"
,
szDec
);
//DEBUG_PRINT("udp client ip:%s ,port is :%d htons(UDP_LAN_PORT)=%d \n",inet_ntoa(from.sin_addr),from.sin_port, htons(UDP_LAN_PORT));
//DEBUG_PRINT("udp client ip:%s ,port is :%d htons(UDP_LAN_PORT)=%d \n",inet_ntoa(from.sin_addr),from.sin_port, htons(UDP_LAN_PORT));
#ifdef test_
//"search_kk_ccu|deviceCode=1122334455667788;protocol=tcp"
if
(
strstr
(
szDec
,
"search_kk_ccu|"
)
!=
NULL
){
char
*
getConnet
=
szDec
+
strlen
(
"search_kk_ccu|"
);
char
*
tmp
=
NULL
;
char
*
endIdx
=
NULL
;
int
itemLen
=
0
;
int
itemConnetLen
=
0
;
char
gwDevCode
[
DEVICE_CODE_LEN
]
=
{
0
};
char
proto
[
10
]
=
{
0
};
tmp
=
strstr
(
getConnet
,
"deviceCode="
);
itemLen
=
strlen
(
"deviceCode="
);
if
(
tmp
!=
NULL
){
endIdx
=
strstr
(
tmp
,
";"
);
if
(
endIdx
==
NULL
){
itemConnetLen
=
strlen
(
tmp
)
-
itemLen
;
}
else
{
itemConnetLen
=
endIdx
-
tmp
-
itemLen
;
}
memcpy
(
gwDevCode
,
tmp
+
itemLen
,
itemConnetLen
);
}
tmp
=
strstr
(
getConnet
,
"protocol="
);
itemLen
=
strlen
(
"protocol="
);
if
(
tmp
!=
NULL
){
endIdx
=
strstr
(
tmp
,
";"
);
if
(
endIdx
==
NULL
){
itemConnetLen
=
strlen
(
tmp
)
-
itemLen
;
}
else
{
itemConnetLen
=
endIdx
-
tmp
-
itemLen
;
}
memcpy
(
proto
,
tmp
+
itemLen
,
itemConnetLen
);
}
DEBUG_PRINT
(
"gwDevCode =%s proto=%s
\n
"
,
gwDevCode
,
proto
);
if
(
strcmp
(
proto
,
"tcp"
)
==
0
){
//
kk_TCP_channel_init
(
gw2mid_cb
);
kk_set_tcp_channel
(
gwDevCode
,
inet_ntoa
(
from
.
sin_addr
));
}
memset
(
host_ip
,
0
,
sizeof
(
host_ip
));
memset
(
mac
,
0
,
sizeof
(
mac
));
memset
(
szOut
,
0
,
sizeof
(
szOut
));
HAL_Get_IP
(
host_ip
,
"ens33"
);
HAL_GetDevice_Code
(
device_code
);
sprintf
(
szOut
,
"search_kk_ccu_ack|deviceCode=%s;ip=%s;port=%d"
,
device_code
,
host_ip
,
16565
);
DEBUG_PRINT
(
"szOut:%s
\n
"
,
szOut
);
DEBUG_PRINT
(
"udp client ip:%s ,port is :%d
\n
"
,
inet_ntoa
(
from
.
sin_addr
),
from
.
sin_port
);
//sendto(sock, szOut, strlen(szOut), 0, (struct sockaddr*)&from,len);
addrto_host
.
sin_addr
.
s_addr
=
inet_addr
(
inet_ntoa
(
from
.
sin_addr
));
addrto_host
.
sin_port
=
htons
(
UDP_LAN_PORT_HOST
);
//addrto_host.sin_port = from.sin_port;
//if(strcmp(host_ip,inet_ntoa(from.sin_addr)) == 0)
//{
sendto
(
sock_host
,
szOut
,
strlen
(
szOut
),
0
,
(
struct
sockaddr
*
)
&
addrto_host
,
sizeof
(
addrto_host
));
//}
//else
//{
// DEBUG_PRINT("udp client is not local ip , refused send ack to it\n");
//}
}
#else
json
=
cJSON_Parse
(
szDec
);
json
=
cJSON_Parse
(
szDec
);
if
(
!
json
)
{
if
(
!
json
)
{
ERROR_PRINT
(
"Error before: [%s]
\n
"
,
"cJSON_Parse"
);
ERROR_PRINT
(
"Error before: [%s]
\n
"
,
"cJSON_Parse"
);
...
@@ -450,7 +543,7 @@ void *udp_dispatch_yield(void *args){
...
@@ -450,7 +543,7 @@ void *udp_dispatch_yield(void *args){
cJSON_Delete
(
json
);
cJSON_Delete
(
json
);
}
}
#endif
}
}
usleep
(
100000
);
usleep
(
100000
);
...
...
platform/zigbee/app/builder/Z3GatewayHost/kk_test.c
View file @
d09c540f
...
@@ -81,6 +81,13 @@ void kk_rpc_reportLeftDevices(EmberEUI64 mac)
...
@@ -81,6 +81,13 @@ void kk_rpc_reportLeftDevices(EmberEUI64 mac)
kk_sub_tsl_delete
(
mac
);
kk_sub_tsl_delete
(
mac
);
}
}
int
kk_sendData2CCU
(
char
*
data
,
int
len
){
if
(
strcmp
(
GW2CCU_PROTOCOL
,
"tcp"
)
==
0
){
kk_tcp_client_send
(
data
,
len
);
}
else
{
kk_ipc_send
(
IPC_PLAT2MID
,
data
,
len
);
}
}
void
kk_rpc_reportDevices
(
EmberEUI64
mac
)
void
kk_rpc_reportDevices
(
EmberEUI64
mac
)
{
{
...
@@ -216,7 +223,8 @@ int kk_test_fuc(char *mac,const char *params)
...
@@ -216,7 +223,8 @@ int kk_test_fuc(char *mac,const char *params)
int value = rpc_get_u8(propertyItem->valuestring);
int value = rpc_get_u8(propertyItem->valuestring);
printf("[%s][%d]value:%d\n",__FUNCTION__,__LINE__,value);
printf("[%s][%d]value:%d\n",__FUNCTION__,__LINE__,value);
res = g_tsl_zigbee_map[pCtrlIdx].zigbee_ctrl[index].zigbee_set(NULL,mac,&value);
res = g_tsl_zigbee_map[pCtrlIdx].zigbee_ctrl[index].zigbee_set(NULL,mac,&value);
if(res < 0) {
if(res < 0)
{
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
return -1;
return -1;
}
}
...
@@ -261,7 +269,8 @@ cJSON *rpc_read_attribue(jrpc_context * ctx, cJSON *params, cJSON *id,cJSON *mac
...
@@ -261,7 +269,8 @@ cJSON *rpc_read_attribue(jrpc_context * ctx, cJSON *params, cJSON *id,cJSON *mac
{
{
int
value
=
rpc_get_u8
(
propertyItem
->
valuestring
);
int
value
=
rpc_get_u8
(
propertyItem
->
valuestring
);
res
=
g_tsl_zigbee_map
[
pCtrlIdx
].
zigbee_ctrl
[
index
].
zigbee_set
(
ctx
,
mac
->
valuestring
,
&
value
);
res
=
g_tsl_zigbee_map
[
pCtrlIdx
].
zigbee_ctrl
[
index
].
zigbee_set
(
ctx
,
mac
->
valuestring
,
&
value
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
goto
error_return
;
goto
error_return
;
}
}
else
{
else
{
...
@@ -318,7 +327,8 @@ cJSON *rpc_Control(jrpc_context * ctx, cJSON *params, cJSON *id,cJSON *mac)
...
@@ -318,7 +327,8 @@ cJSON *rpc_Control(jrpc_context * ctx, cJSON *params, cJSON *id,cJSON *mac)
}
}
res
=
g_tsl_zigbee_map
[
pCtrlIdx
].
zigbee_ctrl
[
index
].
zigbee_set
(
ctx
,
mac
->
valuestring
,
&
value
);
res
=
g_tsl_zigbee_map
[
pCtrlIdx
].
zigbee_ctrl
[
index
].
zigbee_set
(
ctx
,
mac
->
valuestring
,
&
value
);
if
(
res
<
0
)
{
if
(
res
<
0
)
{
set_json_error_type
(
ctx
,
JRPC_INVALID_PARAMS
,
MSG_INVALID_PARAMS
);
set_json_error_type
(
ctx
,
JRPC_INVALID_PARAMS
,
MSG_INVALID_PARAMS
);
goto
error_return
;
goto
error_return
;
}
}
...
...
platform/zigbee/app/builder/Z3GatewayHost/kk_test.h
View file @
d09c540f
...
@@ -4,6 +4,8 @@
...
@@ -4,6 +4,8 @@
#include "network-creator.h"
#include "network-creator.h"
#include "network-creator-security.h"
#include "network-creator-security.h"
#include "RPC_API.h"
#include "RPC_API.h"
#include "com_api.h"
#define KK_REPORT_DEVICE_JOINED_TYPE "/thing/topo/add"
#define KK_REPORT_DEVICE_JOINED_TYPE "/thing/topo/add"
#define KK_REPORT_DEVICE_LEFT_TYPE "/thing/topo/delete"
#define KK_REPORT_DEVICE_LEFT_TYPE "/thing/topo/delete"
...
@@ -16,11 +18,14 @@
...
@@ -16,11 +18,14 @@
#define KK_READ_ATTRIBUTE_METHOD "thing.service.property.get"
#define KK_READ_ATTRIBUTE_METHOD "thing.service.property.get"
#define TEST_PRODUCT_CODE "24"
#define TEST_PRODUCT_CODE "24"
#define GW2CCU_PROTOCOL "tcp"
#define GW_DEVICE_CODE "1122334455667788"
cJSON
*
rpc_Control
(
jrpc_context
*
ctx
,
cJSON
*
params
,
cJSON
*
id
,
cJSON
*
mac
);
cJSON
*
rpc_Control
(
jrpc_context
*
ctx
,
cJSON
*
params
,
cJSON
*
id
,
cJSON
*
mac
);
cJSON
*
rpc_read_attribue
(
jrpc_context
*
ctx
,
cJSON
*
params
,
cJSON
*
id
,
cJSON
*
mac
);
cJSON
*
rpc_read_attribue
(
jrpc_context
*
ctx
,
cJSON
*
params
,
cJSON
*
id
,
cJSON
*
mac
);
int
lightStatusSet
(
jrpc_context
*
ctx
,
const
char
*
mac
,
void
*
data
);
int
lightStatusSet
(
jrpc_context
*
ctx
,
const
char
*
mac
,
void
*
data
);
int
kk_sendData2CCU
(
char
*
data
,
int
len
);
int
rpc_nwkPermitJoin
(
jrpc_context
*
ctx
,
const
char
*
mac
,
void
*
data
);
int
rpc_nwkPermitJoin
(
jrpc_context
*
ctx
,
const
char
*
mac
,
void
*
data
);
...
...
platform/zigbee/app/builder/Z3GatewayHost/libapi_com.so
View file @
d09c540f
No preview for this file type
platform/zigbee/app/builder/Z3GatewayHost/rpc_api/src/rpc_interface_parse.c
View file @
d09c540f
...
@@ -20,7 +20,6 @@
...
@@ -20,7 +20,6 @@
#include "rpc_colorControl.h"
#include "rpc_colorControl.h"
#include "rpc_onoff.h"
#include "rpc_onoff.h"
#include "com_api.h"
#include "kk_test.h"
#include "kk_test.h"
//#include "kk_log.h"
//#include "kk_log.h"
...
@@ -112,7 +111,7 @@ static int send_result_resp(cJSON * result,
...
@@ -112,7 +111,7 @@ static int send_result_resp(cJSON * result,
char
*
str_result
=
rpc_cJSON_Print
(
result_root
);
char
*
str_result
=
rpc_cJSON_Print
(
result_root
);
printf
(
"send json:
\n
%s
\n
"
,
str_result
);
printf
(
"send json:
\n
%s
\n
"
,
str_result
);
return_value
=
kk_
ipc_send
(
IPC_PLAT2MID
,
str_result
,
strlen
(
str_result
)
+
1
);
return_value
=
kk_
sendData2CCU
(
str_result
,
strlen
(
str_result
)
+
1
);
free
(
str_result
);
free
(
str_result
);
rpc_cJSON_Delete
(
result_root
);
rpc_cJSON_Delete
(
result_root
);
return
return_value
;
return
return_value
;
...
@@ -150,7 +149,7 @@ static int send_error_resp(int code, char* message,
...
@@ -150,7 +149,7 @@ static int send_error_resp(int code, char* message,
}
}
char
*
str_result
=
rpc_cJSON_Print
(
result_root
);
char
*
str_result
=
rpc_cJSON_Print
(
result_root
);
//printf("alla=========== :%d\n", strlen(str_result)+1);
//printf("alla=========== :%d\n", strlen(str_result)+1);
return_value
=
kk_
ipc_send
(
IPC_PLAT2MID
,
str_result
,
strlen
(
str_result
)
+
1
);
return_value
=
kk_
sendData2CCU
(
str_result
,
strlen
(
str_result
)
+
1
);
printf
(
"send_error_resp:
\n
%s
\n
"
,
str_result
);
printf
(
"send_error_resp:
\n
%s
\n
"
,
str_result
);
free
(
str_result
);
free
(
str_result
);
rpc_cJSON_Delete
(
result_root
);
rpc_cJSON_Delete
(
result_root
);
...
@@ -268,9 +267,11 @@ int _init_param(struct jrpc_server *server) {
...
@@ -268,9 +267,11 @@ int _init_param(struct jrpc_server *server) {
}
}
int
addGW_and_getIP
(
char
*
ip
){
//char rgMessage[128] = "search_kk_gw|null";
int
search_ccu
(
char
devcode
[
33
],
char
ip
[
16
],
int
*
port
){
char
sendCmdFmt
[]
=
"search_kk_ccu|deviceCode=%s;protocol=%s"
;
char
sendMessage
[
128
]
=
{
0
};
char
revMessage
[
128
]
=
{
0
};
char
revMessage
[
128
]
=
{
0
};
int
sock
;
int
sock
;
int
sk_recv
;
int
sk_recv
;
...
@@ -282,22 +283,23 @@ int addGW_and_getIP(char* ip){
...
@@ -282,22 +283,23 @@ int addGW_and_getIP(char* ip){
struct
sockaddr_in
Addrto
;
struct
sockaddr_in
Addrto
;
struct
sockaddr_in
AddrRev
;
struct
sockaddr_in
AddrRev
;
sprintf
(
sendMessage
,
sendCmdFmt
,
GW_DEVICE_CODE
,
GW2CCU_PROTOCOL
);
if
((
sock
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
))
==
-
1
)
if
((
sock
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
))
==
-
1
)
{
{
printf
(
"
addGW_and_getIP socket fail
\n
"
);
printf
(
"
[%s] socket fail
\n
"
,
__FUNCTION__
);
return
-
1
;
return
-
1
;
}
}
if
((
sk_recv
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
))
==
-
1
)
if
((
sk_recv
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
))
==
-
1
)
{
{
printf
(
"
addGW_and_getIP socket sk_recv fail
\n
"
);
printf
(
"
[%s] socket sk_recv fail
\n
"
,
__FUNCTION__
);
close
(
sock
);
close
(
sock
);
return
-
1
;
return
-
1
;
}
}
if
(
setsockopt
(
sock
,
SOL_SOCKET
,
SO_BROADCAST
|
SO_REUSEADDR
,
&
iOptval
,
sizeof
(
int
))
<
0
)
if
(
setsockopt
(
sock
,
SOL_SOCKET
,
SO_BROADCAST
|
SO_REUSEADDR
,
&
iOptval
,
sizeof
(
int
))
<
0
)
{
{
printf
(
"
addGW_and_getIP setsockopt failed!"
);
printf
(
"
[%s] setsockopt failed
\n
"
,
__FUNCTION__
);
close
(
sock
);
close
(
sock
);
close
(
sk_recv
);
close
(
sk_recv
);
return
-
1
;
return
-
1
;
...
@@ -305,7 +307,7 @@ int addGW_and_getIP(char* ip){
...
@@ -305,7 +307,7 @@ int addGW_and_getIP(char* ip){
if
(
setsockopt
(
sk_recv
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
iOptval
,
sizeof
(
int
))
<
0
)
if
(
setsockopt
(
sk_recv
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
iOptval
,
sizeof
(
int
))
<
0
)
{
{
printf
(
"
addGW_and_getIP setsockopt failed!"
);
printf
(
"
[%s] setsockopt failed
\n
"
,
__FUNCTION__
);
close
(
sock
);
close
(
sock
);
close
(
sk_recv
);
close
(
sk_recv
);
return
-
1
;
return
-
1
;
...
@@ -314,7 +316,7 @@ int addGW_and_getIP(char* ip){
...
@@ -314,7 +316,7 @@ int addGW_and_getIP(char* ip){
flag
=
fcntl
(
sk_recv
,
F_GETFL
,
0
);
flag
=
fcntl
(
sk_recv
,
F_GETFL
,
0
);
if
(
flag
<
0
)
if
(
flag
<
0
)
{
{
printf
(
"
addGW_and_getIP fcntl failed.
\n
"
);
printf
(
"
[%s] fcntl failed.
\n
"
,
__FUNCTION__
);
close
(
sock
);
close
(
sock
);
close
(
sk_recv
);
close
(
sk_recv
);
return
-
1
;;
return
-
1
;;
...
@@ -322,7 +324,7 @@ int addGW_and_getIP(char* ip){
...
@@ -322,7 +324,7 @@ int addGW_and_getIP(char* ip){
flag
|=
O_NONBLOCK
;
flag
|=
O_NONBLOCK
;
if
(
fcntl
(
sk_recv
,
F_SETFL
,
flag
)
<
0
)
if
(
fcntl
(
sk_recv
,
F_SETFL
,
flag
)
<
0
)
{
{
printf
(
"
addGW_and_getIP fcntl failed.
\n
"
);
printf
(
"
[%s] fcntl failed.
\n
"
,
__FUNCTION__
);
close
(
sock
);
close
(
sock
);
close
(
sk_recv
);
close
(
sk_recv
);
return
-
1
;
return
-
1
;
...
@@ -341,40 +343,76 @@ int addGW_and_getIP(char* ip){
...
@@ -341,40 +343,76 @@ int addGW_and_getIP(char* ip){
if
(
bind
(
sk_recv
,
(
struct
sockaddr
*
)
&
AddrRev
,
sizeof
(
AddrRev
))
==
-
1
)
if
(
bind
(
sk_recv
,
(
struct
sockaddr
*
)
&
AddrRev
,
sizeof
(
AddrRev
))
==
-
1
)
{
{
printf
(
"
addGW_and_getIP bind failed!
\n
"
);
printf
(
"
[%s] bind failed!
\n
"
,
__FUNCTION__
);
close
(
sock
);
close
(
sock
);
close
(
sk_recv
);
close
(
sk_recv
);
return
-
1
;
return
-
1
;
}
}
char
info
[]
=
"{
\"
msgtype
\"
:
\"
/thing/topo/add
\"
,
\"
productType
\"
:
\"
gw
\"
,
\"
productCode
\"
:
\"
2
\"
,
\"
deviceCode
\"
:
\"
1122334455667788
\"
}"
;
char
payload
[]
=
"{
\"
msgId
\"
:
\"
1
\"
,
\"
version
\"
:
\"
1.0
\"
,
\"
params
\"
:{
\"
deviceCode
\"
:
\"
1122334455667788
\"
,
\"
productCode
\"
:
\"
2
\"
,
\"
mac
\"
:
\"
1122334455667788
\"
}}"
;
cJSON
*
root
=
cJSON_CreateObject
();
cJSON
*
infoObj
=
cJSON_Parse
(
info
);
cJSON
*
payloadObj
=
cJSON_Parse
(
payload
);
cJSON_AddItemToObject
(
root
,
"info"
,
infoObj
);
cJSON_AddItemToObject
(
root
,
"payload"
,
payloadObj
);
char
*
outbuf
=
cJSON_Print
(
root
);
cJSON_Delete
(
root
);
while
(
1
)
while
(
1
)
{
{
if
((
iSendbytes
=
sendto
(
sock
,
outbuf
,
strlen
(
outbuf
)
+
1
,
0
,
(
struct
sockaddr
*
)
&
Addrto
,
sizeof
(
struct
sockaddr
)))
==
-
1
)
if
((
iSendbytes
=
sendto
(
sock
,
sendMessage
,
strlen
(
sendMessage
)
+
1
,
0
,
(
struct
sockaddr
*
)
&
Addrto
,
sizeof
(
struct
sockaddr
)))
==
-
1
)
{
{
printf
(
"
addGW_and_getIP sendto fail, errno=%d
\n
"
,
errno
);
printf
(
"
[%s] sendto fail, errno=%d
\n
"
,
__FUNCTION__
,
errno
);
close
(
sock
);
close
(
sock
);
close
(
sk_recv
);
close
(
sk_recv
);
free
(
outbuf
);
return
-
1
;
return
-
1
;
}
}
sleep
(
1
);
sleep
(
1
);
recvLen
=
recvfrom
(
sk_recv
,
revMessage
,
sizeof
(
revMessage
),
0
,
(
struct
sockaddr
*
)
&
AddrRev
,
&
iAddrLength
);
recvLen
=
recvfrom
(
sk_recv
,
revMessage
,
sizeof
(
revMessage
),
0
,
(
struct
sockaddr
*
)
&
AddrRev
,
&
iAddrLength
);
if
(
recvLen
>
0
){
if
(
recvLen
>
0
){
printf
(
"addGW_and_getIP recv:%s
\n
"
,
revMessage
);
printf
(
"[%s] recv:%s
\n
"
,
__FUNCTION__
,
revMessage
);
if
(
strstr
(
revMessage
,
"/thing/topo/add_reply"
)
!=
NULL
){
//"search_kk_ccu_ack|deviceCode=CCU_66666;ip=192.168.36.128;port=16565"
memcpy
(
ip
,
inet_ntoa
(
AddrRev
.
sin_addr
),
strlen
(
inet_ntoa
(
AddrRev
.
sin_addr
)));
if
(
strstr
(
revMessage
,
"search_kk_ccu_ack|"
)
!=
NULL
){
printf
(
" recv ip:%s
\n
"
,
ip
);
char
*
ackConnet
=
revMessage
+
strlen
(
"search_kk_ccu_ack|"
);
char
*
tmp
=
NULL
;
char
*
endIdx
=
NULL
;
int
itemLen
=
0
;
int
itemConnetLen
=
0
;
tmp
=
strstr
(
ackConnet
,
"deviceCode="
);
itemLen
=
strlen
(
"deviceCode="
);
if
(
tmp
!=
NULL
){
endIdx
=
strstr
(
tmp
,
";"
);
if
(
endIdx
==
NULL
){
itemConnetLen
=
strlen
(
tmp
)
-
itemLen
;
}
else
{
itemConnetLen
=
endIdx
-
tmp
-
itemLen
;
}
memcpy
(
devcode
,
tmp
+
itemLen
,
itemConnetLen
);
}
tmp
=
strstr
(
ackConnet
,
"ip="
);
itemLen
=
strlen
(
"ip="
);
if
(
tmp
!=
NULL
){
endIdx
=
strstr
(
tmp
,
";"
);
if
(
endIdx
==
NULL
){
itemConnetLen
=
strlen
(
tmp
)
-
itemLen
;
}
else
{
itemConnetLen
=
endIdx
-
tmp
-
itemLen
;
}
memcpy
(
ip
,
tmp
+
itemLen
,
itemConnetLen
);
}
tmp
=
strstr
(
ackConnet
,
"port="
);
itemLen
=
strlen
(
"port="
);
if
(
tmp
!=
NULL
){
endIdx
=
strstr
(
tmp
,
";"
);
if
(
endIdx
==
NULL
){
itemConnetLen
=
strlen
(
tmp
)
-
itemLen
;
}
else
{
itemConnetLen
=
endIdx
-
tmp
-
itemLen
;
}
char
portstr
[
20
]
=
{
0
};
memcpy
(
portstr
,
tmp
+
itemLen
,
itemConnetLen
);
*
port
=
atoi
(
portstr
);
}
//memcpy(ip, inet_ntoa(AddrRev.sin_addr), strlen(inet_ntoa(AddrRev.sin_addr)));
printf
(
" recv deviceCode:%s ip:%s port:%d
\n
"
,
devcode
,
ip
,
*
port
);
break
;
break
;
}
}
}
}
...
@@ -382,24 +420,43 @@ int addGW_and_getIP(char* ip){
...
@@ -382,24 +420,43 @@ int addGW_and_getIP(char* ip){
}
}
close
(
sock
);
close
(
sock
);
close
(
sk_recv
);
close
(
sk_recv
);
free
(
outbuf
);
return
0
;
return
0
;
}
}
void
ipcHandle
(
void
)
void
ipcHandle
(
void
)
{
{
char
deviceCode
[
33
]
=
{
0
};
char
ip
[
16
]
=
{
0
};
char
ip
[
16
]
=
{
0
};
int
port
=
0
;
emberAfAppPrint
(
"Thread rpc Interface Parse create
\n
"
);
emberAfAppPrint
(
"Thread rpc Interface Parse create
\n
"
);
addGW_and_getIP
(
ip
);
search_ccu
(
deviceCode
,
ip
,
&
port
);
_init_param
(
&
my_server
);
_init_param
(
&
my_server
);
kk_ipc_init
(
IPC_PLAT2MID
,
_cb
,
"1122334455667788"
,
ip
);
if
(
strcmp
(
GW2CCU_PROTOCOL
,
"tcp"
)
==
0
){
kk_tcp_client_init
(
ip
,
port
,
_cb
);
}
else
{
kk_ipc_init
(
IPC_PLAT2MID
,
_cb
,
GW_DEVICE_CODE
,
ip
);
}
emberAfAppPrint
(
"sizeof(rpc_table)=%d,sizeof(rpc_table_s)=%d,%d
\n
"
,
sizeof
(
rpc_table
),
sizeof
(
rpc_table_s
),
sizeof
(
rpc_table
)
/
sizeof
(
rpc_table_s
));
emberAfAppPrint
(
"sizeof(rpc_table)=%d,sizeof(rpc_table_s)=%d,%d
\n
"
,
sizeof
(
rpc_table
),
sizeof
(
rpc_table_s
),
sizeof
(
rpc_table
)
/
sizeof
(
rpc_table_s
));
for
(
int
i
=
0
;
i
<
sizeof
(
rpc_table
)
/
sizeof
(
rpc_table_s
);
i
++
){
for
(
int
i
=
0
;
i
<
sizeof
(
rpc_table
)
/
sizeof
(
rpc_table_s
);
i
++
){
emberAfAppPrint
(
"i=%d,%s
\r\n
"
,
i
,
rpc_table
[
i
].
name
);
emberAfAppPrint
(
"i=%d,%s
\r\n
"
,
i
,
rpc_table
[
i
].
name
);
jrpc_register_procedure
(
&
my_server
,
rpc_table
[
i
].
func
,
rpc_table
[
i
].
name
,
NULL
);
jrpc_register_procedure
(
&
my_server
,
rpc_table
[
i
].
func
,
rpc_table
[
i
].
name
,
NULL
);
}
}
//send add gw to ccu
char
info
[]
=
"{
\"
msgtype
\"
:
\"
/thing/topo/add
\"
,
\"
productType
\"
:
\"
gw
\"
,
\"
productCode
\"
:
\"
2
\"
,
\"
deviceCode
\"
:
\"
1122334455667788
\"
}"
;
char
payload
[]
=
"{
\"
msgId
\"
:
\"
1
\"
,
\"
version
\"
:
\"
1.0
\"
,
\"
params
\"
:{
\"
deviceCode
\"
:
\"
1122334455667788
\"
,
\"
productCode
\"
:
\"
2
\"
,
\"
mac
\"
:
\"
1122334455667788
\"
}}"
;
cJSON
*
root
=
cJSON_CreateObject
();
cJSON
*
infoObj
=
cJSON_Parse
(
info
);
cJSON
*
payloadObj
=
cJSON_Parse
(
payload
);
cJSON_AddItemToObject
(
root
,
"info"
,
infoObj
);
cJSON_AddItemToObject
(
root
,
"payload"
,
payloadObj
);
char
*
outbuf
=
cJSON_Print
(
root
);
cJSON_Delete
(
root
);
//kk_sendData2CCU(outbuf, strlen(outbuf));
kk_sendData2CCU
(
"allan test"
,
strlen
(
"allan test"
));
free
(
outbuf
);
//handle procidure
//handle procidure
while
(
1
){
while
(
1
){
//
//
...
@@ -422,7 +479,7 @@ int jrpc_send_msg(cJSON * msgJson) {
...
@@ -422,7 +479,7 @@ int jrpc_send_msg(cJSON * msgJson) {
return_value
=
kk_
ipc_send
(
IPC_PLAT2MID
,
str_result
,
strlen
(
str_result
)
+
1
);
return_value
=
kk_
sendData2CCU
(
str_result
,
strlen
(
str_result
)
+
1
);
free
(
str_result
);
free
(
str_result
);
return
return_value
;
return
return_value
;
}
}
...
...
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