Commit a706052d authored by 尹佳钦's avatar 尹佳钦
parents 64166551 890c7106
LIBA_TARGET := libiot_kcloud.a
SRCS_kcloud := kcloud_main.c
$(call Append_Conditional, LIB_SRCS_EXCLUDE, kcloud_main.c)
$(call Append_Conditional, SRCS_kcloud, kcloud_main.c)
$(call Append_Conditional, TARGET, kcloud)
......
......@@ -6,6 +6,10 @@
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include "com_api.h"
#define CCU_TCP_PORT 16565
......@@ -554,14 +558,14 @@ typedef struct {
char ip[MAX_IP_LEN];
int port;
ipc_cb* cb;
int retry;
} kk_tcp_client_t;
static kk_tcp_client_t g_client_ctrl = {NULL, -1, 0,{0},0, NULL};
static kk_tcp_client_t g_client_ctrl = {NULL, -1, 0,{0},0, NULL,0};
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;
......@@ -601,14 +605,14 @@ static int client_socket_init(int *sd, char *ipaddr, uint16_t port)
}
//连接
int retry = 0;
for(;retry < 10; retry++){
for(;retry < 3; retry++){
if(-1 != connect(sock, (struct sockaddr *)&addr, sizeof(addr))){
break;
}
printf("==================connect retry=%d \n", retry);
//printf("==================connect retry=%d \n", retry);
sleep(1);
}
if (retry >= 10){
if (retry >= 3){
printf("==================connect failed \n");
goto err2;
}
......@@ -626,28 +630,56 @@ static void loop_tcp_client_thread(void *arg){
printf("loop_tcp_client_thread start!\r\n");
char buf[1024]= {0};
int ret = 0;
fd_set fds;
struct timeval timeout={0,200}; //select等待3秒,3秒轮询,要非阻塞就置0
while(1){
if(-1 == client_socket_init(&g_client_ctrl.sd,g_client_ctrl.ip, g_client_ctrl.port)){
printf("connect failed \n");
sleep(1);
g_client_ctrl.retry++;
continue;
}
g_client_ctrl.isConnect = 1;
g_client_ctrl.retry = 0;
while(g_client_ctrl.isConnect){
_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,"");
FD_ZERO(&fds); //每次循环都要清空集合,否则不能检测描述符变化
FD_SET(g_client_ctrl.sd,&fds); //添加描述符
switch(select(g_client_ctrl.sd + 1,&fds,NULL,NULL,&timeout)) //select使用
{
case -1:
g_client_ctrl.isConnect = 0;
printf(" [%s] select error ret=%d \n", __FUNCTION__, ret);
break; //select错误 退出循环
case 0:
break; //再次轮询
default:
if(FD_ISSET(g_client_ctrl.sd,&fds)) //测试sock是否可读,即是否网络上有数据
{
//接受网络数据
_MutexLock(g_client_ctrl.mutex);
ret = read(g_client_ctrl.sd, buf, sizeof(buf));
_MutexUnlock(g_client_ctrl.mutex);
if( ret <= 0){
printf("=================read error ret=%d \n",ret);
if (errno != EINTR){
g_client_ctrl.isConnect = 0;
printf("read error reconnect!! \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);
break;
}// end switch
//usleep(100000);
}
printf("network error, try connect again! \n");
......@@ -658,6 +690,14 @@ static void loop_tcp_client_thread(void *arg){
}
int kk_get_retry_num(){
return g_client_ctrl.retry;
}
int kk_reset_retry_num(){
return g_client_ctrl.retry = 0;
}
int kk_tcp_client_send(char* data, int len){
int ret = 0;
int cnt = 0;
......@@ -698,6 +738,7 @@ int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb)
kk_tcp_client_deinit(){
if (g_client_ctrl.sd > -1){
close(g_client_ctrl.sd);
g_client_ctrl.sd = -1;
}
_MutexDestroy(g_client_ctrl.mutex);
}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -502,7 +502,7 @@ int iotx_report_id(void)
return g_report_id++;
}
int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len)
int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len,_IN_ int isAsync)
{
int res = 0;
dm_msg_request_t request;
......@@ -522,7 +522,7 @@ int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload, _IN_
//request.callback = dm_client_thing_event_post_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,isAsync);
free(request.msgTypeStr);
free(request.params);
return res;
......@@ -549,7 +549,7 @@ int dm_mgr_upstream_thing_event_post(_IN_ int devid, _IN_ char *identifier, _IN_
//request.callback = dm_client_thing_event_post_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
free(request.msgTypeStr);
free(request.params);
return res;
......@@ -669,7 +669,7 @@ int dm_mgr_upstream_thing_sub_register(_IN_ int devid)
//request.callback = dm_client_thing_sub_register_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
......@@ -723,7 +723,7 @@ int dm_mgr_upstream_thing_sub_unregister(_IN_ int devid)
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -781,7 +781,7 @@ int dm_mgr_upstream_thing_topo_add(_IN_ int devid)
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -838,7 +838,7 @@ int dm_mgr_upstream_thing_topo_delete(_IN_ int devid)
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -882,7 +882,7 @@ int dm_mgr_upstream_thing_topo_get(void)
//request.callback = dm_client_thing_topo_get_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
......@@ -935,7 +935,7 @@ int dm_mgr_upstream_thing_list_found(_IN_ int devid)
//request.callback = dm_client_thing_list_found_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
......@@ -986,7 +986,7 @@ int dm_mgr_ccu_status_cloud(_IN_ int devid)
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -1049,7 +1049,7 @@ int dm_mgr_upstream_status_online(_IN_ int devid)
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -1109,7 +1109,7 @@ int dm_mgr_upstream_status_offline(_IN_ int devid)
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -1165,7 +1165,7 @@ int dm_mgr_upstream_combine_login(_IN_ int devid)
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -1220,7 +1220,7 @@ int dm_mgr_upstream_combine_logout(_IN_ int devid)
/* Send Message To Cloud */
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -1273,7 +1273,7 @@ int dm_mgr_ota_report_version(_IN_ int devid, char *version)
/* Send Message To Cloud */
/* Send Message To Cloud */
res = dm_msg_request(&request);
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
......@@ -1304,12 +1304,18 @@ int dm_mgr_subdev_delete(_IN_ char deviceCode[DEVICE_CODE_MAXLEN])
dm_mgr_dev_node_t *node = NULL;
INFO_PRINT("dm_mgr_subdev_delete deviceCode:%s\n",deviceCode);
res = kk_subDev_delete_by_dcode(deviceCode);//delete db data
res = kk_subDev_delete_by_dcode(deviceCode);//delete sub db data
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = kk_property_delete_by_dcode(deviceCode);//delete properties db data
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(deviceCode,&node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
......
This diff is collapsed.
......@@ -319,7 +319,7 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
if(s_CloudStatus){
iotx_dm_dev_online(0);//first online,report the online status
usleep(200000);
kk_tsl_post_property(0,NULL);
kk_tsl_post_property(0,NULL,0);
}
}else if (strstr(typeJson->valuestring,KK_THING_OTA_DEVICE_UPGRADE)){
INFO_PRINT("ota upgrade... \n");
......
......@@ -379,6 +379,29 @@ int kk_property_sync_values(const char *deviceCode)
return SUCCESS_RETURN;
}
int kk_property_delete_by_dcode(char deviceCode[DEVICE_CODE_MAXLEN])
{
const char *deleteCmd = "delete from PropertiesInfo where deviceCode = '%s';";
char *sqlCmd = NULL;
int rc = 0;
char *zErrMsg = 0;
kk_property_db_ctx_t *ctx = _kk_property_db_get_ctx();
_kk_property_db_lock();
sqlCmd = sqlite3_mprintf(deleteCmd,deviceCode);
INFO_PRINT("Table delete data sqlCmd:%s\n",sqlCmd);
rc = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
INFO_PRINT("Table delete data successfully\n");
}
sqlite3_free(sqlCmd);
_kk_property_db_unlock();
return SUCCESS_RETURN;
}
......
......@@ -115,15 +115,16 @@ static int _kk_load_subDevice(void)
ctx->subDevNum++;
}
usleep(100000);
//如果为认证,需要认证
//再上线
// send the topc info
iotx_dm_subscribe(devId);
if(sqlite3_column_int(stmt, DB_DEVTYPE) == KK_DM_DEVICE_SUBDEV){
kk_property_sync_values(sqlite3_column_text(stmt, DB_DEVICECODE));
}
//sync the data from property db
kk_property_sync_values(sqlite3_column_text(stmt, DB_DEVICECODE));
kk_dm_ota_report_version(devId,sqlite3_column_text(stmt, DB_VERSION));//version
usleep(100000);
//post the property to cloud
dm_msg_thing_property_post_all(sqlite3_column_text(stmt, DB_DEVICECODE));
//kk_dm_ota_report_version(devId,sqlite3_column_text(stmt, DB_VERSION));//post version
//usleep(100000);
}
sqlite3_finalize(stmt);
......
......@@ -180,7 +180,7 @@ void kk_platMsg_handle(void* data, char* chalMark){
info = cJSON_GetObjectItem(json, "info");
payload = cJSON_GetObjectItem(json, "payload");
if (info == NULL || payload == NULL){
ERROR_PRINT("info or payload params is error\n");
ERROR_PRINT("info or payload params error\n");
goto error;
}
......@@ -194,6 +194,10 @@ void kk_platMsg_handle(void* data, char* chalMark){
goto error;
}
if (chalMark != NULL){
dm_mgr_update_timestamp_by_devicecode(chalMark,HAL_UptimeMs());
}
dm_mgr_update_timestamp_by_devicecode(info_dcode->valuestring,HAL_UptimeMs());
if (strcmp(msgType->valuestring, "/thing/topo/add")==0){
......@@ -218,6 +222,7 @@ void kk_platMsg_handle(void* data, char* chalMark){
INFO_PRINT("save property and send to cloud \n");
char* outstr = cJSON_Print(payload);
kk_tsl_property_set_by_devicecode(info_dcode->valuestring, outstr, strlen(outstr)+1);
dm_msg_thing_property_post_by_identify(info_dcode->valuestring,jsonPay);
kk_property_db_update(info_dcode->valuestring);
free(outstr);
......@@ -458,7 +463,7 @@ void *udp_dispatch_yield(void *args){
memset(host_ip, 0, sizeof(host_ip));
memset(mac, 0, sizeof(mac));
memset(szOut, 0, sizeof(szOut));
HAL_Get_IP(host_ip,"ens33");
HAL_Get_IP(host_ip,NULL);
HAL_GetDevice_Code(device_code);
sprintf(szOut,"search_kk_ccu_ack|deviceCode=%s;ip=%s;port=%d",device_code,host_ip,16565);
......@@ -598,7 +603,7 @@ void *ccu_property_monitor(void *args)
}
if(needReport&&(kk_get_cloudstatus() == 1)){
needReport = 0;
kk_tsl_post_property(0,NULL);
kk_tsl_post_property(0,NULL,0);
}
sleep(time_second);
}
......
......@@ -1260,7 +1260,7 @@ int kk_tsl_property_set_by_devicecode(const char deviceCode[DEVICE_CODE_MAXLEN],
res = _kk_msg_property_set(devid, &request);
/* Response */
kk_tsl_post_property(devid,NULL);
//kk_tsl_post_property(devid,NULL);
#if 0
#define EVENT_ERROR_IDENTIFIER "Error"
......@@ -1317,7 +1317,7 @@ int kk_tsl_service_property_set(const char *topic, const char *payload, unsigned
res = _kk_msg_property_set(devid, &request);
/* Response */
kk_tsl_post_property(devid,NULL);
kk_tsl_post_property(devid,NULL,0);
#if 0
#define EVENT_ERROR_IDENTIFIER "Error"
......@@ -1468,7 +1468,7 @@ int kk_tsl_post_property_add(_IN_ void *handle, _IN_ char *identifier, _IN_ int
}
return ret;
}
static int kk_tsl_post_property_end(_IN_ void *handle)
static int kk_tsl_post_property_end(_IN_ void *handle,_IN_ int isAsync)
{
int res = 0;
char *payload = NULL;
......@@ -1485,7 +1485,7 @@ static int kk_tsl_post_property_end(_IN_ void *handle)
return MEMORY_NOT_ENOUGH;
}
INFO_PRINT("Post Payload, Length: %d, Payload: %s\n", strlen(payload), payload);
res = dm_mgr_upstream_thing_property_post(dapi_property->devid, payload, strlen(payload));
res = dm_mgr_upstream_thing_property_post(dapi_property->devid, payload, strlen(payload),isAsync);
lite_cjson_delete(dapi_property->lite);
free(dapi_property);
free(payload);
......@@ -1493,7 +1493,7 @@ static int kk_tsl_post_property_end(_IN_ void *handle)
return res;
}
int kk_tsl_post_property(int devId, const char *property_identifier)
int kk_tsl_post_property(int devId, const char *property_identifier,int isAsync)
{
int res = 0, msgid = 0, property_identifier_len = 0, post_property_reply = 0;
void *property_handle = NULL;
......@@ -1508,11 +1508,11 @@ int kk_tsl_post_property(int devId, const char *property_identifier)
property_identifier_len = (property_identifier) ? (strlen((char *)property_identifier)) : (0);
res = kk_tsl_post_property_add(property_handle, (char *)property_identifier, property_identifier_len);
if (res != SUCCESS_RETURN) {
kk_tsl_post_property_end(property_handle);
kk_tsl_post_property_end(property_handle,isAsync);
_kk_tsl_api_unlock();
return FAIL_RETURN;
}
res = kk_tsl_post_property_end(property_handle);
res = kk_tsl_post_property_end(property_handle,isAsync);
if (res < SUCCESS_RETURN) {
_kk_tsl_api_unlock();
return FAIL_RETURN;
......
......@@ -81,7 +81,7 @@ extern int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int star
_OU_ char productType[PRODUCT_TYPE_MAXLEN], _OU_ char deviceCode[DEVICE_CODE_MAXLEN]);
extern int kk_tsl_service_property_set(const char *topic, const char *payload, unsigned int payload_len,
void *context);
extern int kk_tsl_post_property(int devId, const char *property_identifier);
extern int kk_tsl_post_property(int devId, const char *property_identifier,int isAsync);
extern int kk_tsl_post_event(int devId, const char *event_identifier);
extern int kk_tsl_post_service(int devId, const char *service_identifier, int response_id,int code);
#endif
......@@ -349,6 +349,7 @@ int search_ccu(char devcode[33], char ip[16], int* port){
return -1;
}
printf("[%s] start search ccu!! \n", __FUNCTION__);
while (1)
{
if ((iSendbytes = sendto(sock, sendMessage, strlen(sendMessage)+1, 0, (struct sockaddr*)&Addrto, sizeof(struct sockaddr))) == -1)
......@@ -456,7 +457,6 @@ void* _msg_event_property_post(char ip[16], int port){
}
void ipcHandle(void)
{
char deviceCode[33] = {0};
......@@ -477,6 +477,8 @@ void ipcHandle(void)
jrpc_register_procedure(&my_server, rpc_table[i].func, rpc_table[i].name, NULL );
}
//send add gw to ccu
char* outbuf = _msg_topo_add();
if (strcmp(GW2CCU_PROTOCOL, "tcp") != 0){
......@@ -497,11 +499,32 @@ void ipcHandle(void)
kk_sendData2CCU(postmsg, strlen(postmsg));
free(postmsg);
}
if (kk_get_retry_num() > 20){
//discover ccu
search_ccu(deviceCode, ip, &port);
if(strcmp(GW2CCU_PROTOCOL, "tcp") == 0){
kk_tcp_client_init(ip, port, _cb);
}else{
//kk_ipc_init(IPC_PLAT2MID, _cb, macString/*GW_DEVICE_CODE*/, ip);
}
//send add gw to ccu
outbuf = _msg_topo_add();
if (outbuf == NULL){
printf("[%s] topo add msg failed, exit\n",__FUNCTION__);
return;
}
kk_sendData2CCU(outbuf, strlen(outbuf));
free(outbuf);
cnt = 0;
kk_reset_retry_num();
}
}
//jrpc_server_run(&my_server);
//jrpc_server_destroy(&my_server);
}
......
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