Commit 0dacf7ba authored by 陈伟灿's avatar 陈伟灿

Merge branch 'cwc' into 'master'

Cwc

See merge request chenweican/k-sdk!84
parents e29b38b0 88cc317e
...@@ -62,6 +62,28 @@ static char *s_split_product[] = { ...@@ -62,6 +62,28 @@ static char *s_split_product[] = {
"3023", //肖邦系列双路面板 "3023", //肖邦系列双路面板
"3024" //肖邦系列三路面板 "3024" //肖邦系列三路面板
}; };
static int kk_utils_memtok(_IN_ char *input, _IN_ int input_len, _IN_ char delimiter, _IN_ int index, _OU_ int *offset)
{
int item_index = 0;
int count = 0;
if (input == NULL || input_len <= 0 || offset == NULL) {
return -2;
}
for (item_index = 0; item_index < input_len; item_index++) {
if (input[item_index] == delimiter && (item_index + 1) < input_len) {
count++;
if (count == index) {
*offset = item_index;
return 0;
}
}
}
return -1;
}
/************************************************************ /************************************************************
*功能描述:灯控面板需要分开上报属性 *功能描述:灯控面板需要分开上报属性
*输入参数: info:所上报数据 *输入参数: info:所上报数据
...@@ -249,15 +271,18 @@ void KK_Data_FromDev(void* str,int len) ...@@ -249,15 +271,18 @@ void KK_Data_FromDev(void* str,int len)
ERROR_PRINT("[%s][%d] root is null \n",__FUNCTION__,__LINE__); ERROR_PRINT("[%s][%d] root is null \n",__FUNCTION__,__LINE__);
return; return;
} }
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
cmd = cJSON_GetObjectItem(root, "cmd"); cmd = cJSON_GetObjectItem(root, "cmd");
if(cmd == NULL){ if(cmd == NULL){
_kk_sendto_cloud(root); _kk_sendto_cloud(root);
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
} }
else{ else{
KK_Subdev_Subscribe(root); KK_Subdev_Subscribe(root);
} }
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
cJSON_Delete(root); cJSON_Delete(root);
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
} }
/************************************************************ /************************************************************
*功能描述:无效主题过滤 *功能描述:无效主题过滤
......
...@@ -52,6 +52,7 @@ static int mqtt_start(void) ...@@ -52,6 +52,7 @@ static int mqtt_start(void)
return rc; return rc;
} }
int kk_start_ccu_register(void);
extern int kk_info_report_start(void); extern int kk_info_report_start(void);
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
...@@ -59,7 +60,7 @@ int main(int argc, char* argv[]) ...@@ -59,7 +60,7 @@ int main(int argc, char* argv[])
open("kcloud",LOG_PID,LOG_USER); open("kcloud",LOG_PID,LOG_USER);
/*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_Data_FromDev,NULL,NULL); kk_ipc_init(IPC_APP2MID,(ipc_cb*)KK_Data_FromDev,NULL,NULL);
kk_info_report_start(); kk_info_report_start();
again: again:
rc = kk_start_ccu_register(); rc = kk_start_ccu_register();
......
...@@ -84,6 +84,7 @@ int kk_parse_token(char *info){ ...@@ -84,6 +84,7 @@ int kk_parse_token(char *info){
fp = fopen("/usr/kk/kk_token.txt", "w+"); fp = fopen("/usr/kk/kk_token.txt", "w+");
if(fp != NULL){ if(fp != NULL){
fwrite(jwt->valuestring,strlen(jwt->valuestring),1,fp); fwrite(jwt->valuestring,strlen(jwt->valuestring),1,fp);
fsync(fp);
fclose(fp); fclose(fp);
} }
//cJSON *ccuid = cJSON_GetObjectItem(infoObj,"ccu_id"); //cJSON *ccuid = cJSON_GetObjectItem(infoObj,"ccu_id");
......
...@@ -276,7 +276,10 @@ int KK_MQTT_SendMsg(char *topicName,const char *payload) ...@@ -276,7 +276,10 @@ int KK_MQTT_SendMsg(char *topicName,const char *payload)
//cJSON_Minify(payload); //cJSON_Minify(payload);
INFO_PRINT("mqtt send payload :%s.\n",payload); INFO_PRINT("mqtt send payload :%s.\n",payload);
if(s_cloudStatus == DEVICE_OFFLINE){
ERROR_PRINT("Failed to start sendMessage, s_cloudStatus == DEVICE_OFFLINE\n");
return -1;
}
if ((rc = MQTTAsync_sendMessage(s_Client, topicName, &pubmsg, &opts)) != MQTTASYNC_SUCCESS) if ((rc = MQTTAsync_sendMessage(s_Client, topicName, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
{ {
ERROR_PRINT("Failed to start sendMessage, return code:%d.\n", rc); ERROR_PRINT("Failed to start sendMessage, return code:%d.\n", rc);
......
LIBA_TARGET := libiot_lan.a
$(call Append_Conditional, LIB_SRCS_EXCLUDE, kk_lan_main.c)
$(call Append_Conditional, SRCS_kk_lan, kk_lan_main.c)
$(call Append_Conditional, TARGET, kk_lan)
CFLAGS += -I$(TOP_DIR)/common/api
CFLAGS += -I$(TOP_DIR)/common/json
CFLAGS += -I$(TOP_DIR)/common/nanomsg/include
CFLAGS += -I$(TOP_DIR)/common/ev/include
LDFLAGS += -lkk_tsl
LDFLAGS += -lapi_com
ifeq ($(CONFIG_MODEL),x86)
LDFLAGS += -L$(TOP_DIR)/common/nanomsg -lnanomsg_ubuntu
LDFLAGS += -L$(TOP_DIR)/common/ev -lev_ubuntu
else ifeq ($(CONFIG_MODEL),nx5)
LDFLAGS += -L$(TOP_DIR)/common/nanomsg -lnanomsg_nx5
LDFLAGS += -L$(TOP_DIR)/common/ev -lev_nx5
else
LDFLAGS += -L$(TOP_DIR)/common/nanomsg -lnanomsg
LDFLAGS += -L$(TOP_DIR)/common/ev -lev
endif
LDFLAGS += -liot_cjson -ldl -lm -lanl -lkk_hal
#include<stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include "kk_data_handle.h"
#include "kk_opcode.h"
#include "com_api.h"
#include "kk_data_mng.h"
const char DM_MSG_TO_MIDDWARE[] = "{\"msgtype\":\"%s\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"}";
char *strrpl(char *in, char *out, int outlen, char *src, char *dst)
{
char *p = in;
unsigned int len = outlen - 1;
// 这几段检查参数合法性
if((NULL == src) || (NULL == dst) || (NULL == in) || (NULL == out))
{
return NULL;
}
if((strcmp(in, "") == 0) || (strcmp(src, "") == 0))
{
return NULL;
}
if(outlen <= 0)
{
return NULL;
}
while((*p != '\0') && (len > 0))
{
if(strncmp(p, src, strlen(src)) != 0)
{
int n = strlen(out);
out[n] = *p;
out[n + 1] = '\0';
p++;
len--;
}
else
{
strcat(out, dst);
p += strlen(src);
len -= strlen(dst);
}
}
return out;
}
static char * _kk_data_create(char *msgtype,const char *productCode,const char *deviceCode,const char *param)
{
cJSON *root;
cJSON *payload;
cJSON* infoObj;
char *out;
char *infoStr = NULL;
int infoStr_len = 0;
int res = 0;
char method[128] = {0};
strrpl(msgtype+1,method,sizeof(method),"/",".");
printf("method:%s\n",method);
infoStr_len = strlen(DM_MSG_TO_MIDDWARE)+strlen(productCode)+strlen(deviceCode)+strlen(msgtype)+10;
infoStr = malloc(infoStr_len);
if(infoStr == NULL){
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
return NULL;
}
memset(infoStr,0x0,infoStr_len);
snprintf(infoStr,infoStr_len,DM_MSG_TO_MIDDWARE,msgtype,productCode,deviceCode);
root=cJSON_CreateObject();
infoObj = cJSON_Parse(infoStr);
cJSON_AddItemToObject(root, MSG_INFO_STR, infoObj);
payload = cJSON_CreateObject();
if(payload){
cJSON_AddItemToObject(root, MSG_PAYLOAD_STR, payload);
cJSON_AddStringToObject(payload, "msgId", "*");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "method", method);
cJSON_AddStringToObject(payload, "params", param);
}
out=cJSON_Print(root);
cJSON_Minify(out);
cJSON_Delete(root);
free(infoStr);
INFO_PRINT("[out]%s\n",out);
return out;
//free(out); /* Print to text, Delete the cJSON, print it, release the string. */
}
static void kk_handle_sync_info(void)
{
char *send_data = NULL;
send_data = _kk_data_create(SYNC_MSG_TYPE,"*","*","*");
if(send_data == NULL){
return;
}
kk_ipc_send(IPC_APP2MID, send_data, strlen(send_data)+1);
free(send_data);
}
static int kk_send_ack(cJSON *root,int sockfd)
{
char *out = NULL;
char *tmpBuf = NULL;
out=cJSON_Print(root);
cJSON_Minify((char*)out);
tmpBuf = calloc(strlen(out) + 4,1);
if(tmpBuf == NULL){
cJSON_Delete(root);
free(out);
return -1;
}
strcat(tmpBuf,"!");
strcat(tmpBuf,out);
strcat(tmpBuf,"$");
printf("tmpBuf:%s\n",tmpBuf);
send(sockfd, tmpBuf, strlen(tmpBuf), 0);
cJSON_Delete(root);
free(out);
free(tmpBuf);
return 0;
}
static int _kk_send_data_to_sdk(char *nodeid,char *opcode,char *arg)
{
cJSON *root;
char *out = NULL;
root=cJSON_CreateObject();
if(root){
cJSON_AddStringToObject(root, "nodeid", nodeid);
cJSON_AddStringToObject(root, "opcode", opcode);
cJSON_AddStringToObject(root, "status", "success");
cJSON_AddStringToObject(root, "arg", arg);
out=cJSON_Print(root);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
printf("out:%s\n",out);
cJSON_Minify((char*)out);
kk_send_data_to_sdk(out);
}
free(arg);
cJSON_Delete(root);
return 0;
}
static int kk_heartbeat_ack(int sockfd)
{
cJSON *root;
root=cJSON_CreateObject();
if(root){
cJSON_AddStringToObject(root, "nodeid", "*");
cJSON_AddStringToObject(root, "opcode", HEARTBEAT_OPCODE);
cJSON_AddStringToObject(root, "status", "success");
cJSON_AddStringToObject(root, "arg", "*");
kk_send_ack(root,sockfd);
}
cJSON_Delete(root);
return 0;
}
static int kk_loginccu_ack(cJSON *arg,int sockfd)
{
cJSON *zkid;
cJSON *root;
cJSON *args;
char *out = NULL;
char *tmpBuf = NULL;
if(arg == NULL){
return -1;
}
zkid = cJSON_GetObjectItem(arg, ZKID_STRING);
if(zkid != NULL){
if(strstr(KK_CCU_ID,zkid->valuestring) != NULL){
root=cJSON_CreateObject();
if(root){
args = cJSON_CreateObject();
if(args){
cJSON_AddItemToObject(root, "arg", args);
cJSON_AddStringToObject(args, "error_code", "0");
cJSON_AddStringToObject(args, "seq", "");
cJSON_AddStringToObject(args, "zkid", zkid->valuestring);
}
cJSON_AddStringToObject(root, "nodeid", "*");
cJSON_AddStringToObject(root, "opcode", LOGIN_OPCODE);
cJSON_AddStringToObject(root, "status", "success");
kk_send_ack(root,sockfd);
}
cJSON_Delete(root);
}
}else{
ERROR_PRINT("data error...\n");
return -1;
}
return 0;
}
int kk_data_handle(cJSON *json,int sockfd)
{
cJSON *opcode;
cJSON *arg;
opcode = cJSON_GetObjectItem(json, OPCODE_STRING);
if(opcode != NULL){
if(strcmp(opcode->valuestring,LOGIN_OPCODE) == 0){
arg = cJSON_GetObjectItem(json, ARG_STRING);
kk_loginccu_ack(arg,sockfd);
}else if(strcmp(opcode->valuestring,HEARTBEAT_OPCODE) == 0){
kk_heartbeat_ack(sockfd);
}else if(strcmp(opcode->valuestring,SYNC_OPCODE) == 0){
kk_handle_sync_info();
}
}
return 0;
}
static int kk_parse_syncinfo(cJSON *payload)
{
cJSON *paramStr,*dataStr;
cJSON *gwdevices,*subdevices;
cJSON * gwitem,*properties;
char *gwdevicecode;
char *deviceCode,*productCode;
char *identifier,*valuetype;
cJSON * subitem;
cJSON * newccuItem;
cJSON *valuejson;
int value;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_map_dev_node_t *node = NULL;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
paramStr = cJSON_GetObjectItem(payload, DATA_STRING);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if(paramStr == NULL) return -1;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
dataStr = cJSON_GetObjectItem(paramStr, DATA_STRING);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if(dataStr == NULL) return -1;
gwdevices = cJSON_GetObjectItem(dataStr, DEVICES_STRING);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if(gwdevices == NULL) return -1;
gwitem = gwdevices->child;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
while(gwitem != NULL){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
subdevices = cJSON_GetObjectItem(gwitem,DEVICES_STRING);
gwdevicecode = cJSON_GetObjectItem(gwitem,MSG_DEVICE_CODE_STR)->valuestring;
if(subdevices != NULL){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
subitem = subdevices->child;
while(subitem != NULL){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
deviceCode = cJSON_GetObjectItem(subitem,MSG_DEVICE_CODE_STR)->valuestring;
productCode = cJSON_GetObjectItem(subitem,MSG_PRODUCT_CODE_STR)->valuestring;
properties = cJSON_GetObjectItem(subitem,MSG_PROPERTIES_STR);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
node = kk_map_dev_add(deviceCode,productCode,gwdevicecode);
if(node != NULL && node->newccu != NULL){
newccuItem = node->newccu->child;
while(newccuItem != NULL){
identifier = cJSON_GetObjectItem(newccuItem,MSG_INDENTIFIER_STR)->valuestring;
valuetype = cJSON_GetObjectItem(newccuItem,MSG_INDENTIFIER_STR)->valuestring;
valuejson = cJSON_GetObjectItem(properties,identifier);
if(valuejson != NULL){
if(strcmp(valuetype,"bool") == 0 || strcmp(valuetype,"int") == 0){
value = valuejson->valueint;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_map_dev_update_int_value(node,identifier,value);
}
}
newccuItem = newccuItem->next;
}
}
subitem = subitem->next;
}
}
gwitem = gwitem->next;
}
return 0;
}
static int kk_create_floors_to_sdk(cJSON *root)
{
cJSON *floors = cJSON_CreateArray();
cJSON *floorItem = cJSON_CreateObject();
cJSON_AddStringToObject(floorItem, "floor_icon", "");
cJSON_AddStringToObject(floorItem, "floor_pos", "1");
cJSON_AddStringToObject(floorItem, "id", "1");
cJSON_AddStringToObject(floorItem, "name", "一楼");
cJSON_AddItemToArray(floors, floorItem);
cJSON_AddItemToObject(root, "floors", floors);
return 0;
}
static int kk_create_roominfo_to_sdk(cJSON *root)
{
cJSON *rooms = cJSON_CreateArray();
cJSON *roomsItem = cJSON_CreateObject();
cJSON *room_status;
cJSON_AddStringToObject(roomsItem, "floor_id", "1");
cJSON_AddStringToObject(roomsItem, "id", "1");
cJSON_AddStringToObject(roomsItem, "room_icon", "");
cJSON_AddStringToObject(roomsItem, "name", "卧室");
room_status = cJSON_CreateObject();
cJSON_AddItemToObject(roomsItem, "room_status", room_status);
cJSON_AddItemToArray(rooms, roomsItem);
cJSON_AddItemToObject(root, "rooms", rooms);
return 0;
}
static int kk_create_scene_to_sdk(cJSON *root)
{
cJSON *scenes = cJSON_CreateArray();
cJSON_AddItemToObject(root, "scenes", scenes);
return 0;
}
int kk_create_syncinfo_to_sdk(void)
{
cJSON *air_box_devices;
cJSON *alarms;
cJSON *ccu_link_status;
cJSON *link_arg;
cJSON *ccu_version;
cJSON *central_ac_gws;
cJSON *central_ac_indoorunits;
cJSON *cnwise_music_controllers;
cJSON *code_lib_controllers;
cJSON *controllers;
cJSON *expand_rules;
cJSON *group;
cJSON *guard;
cJSON *gw_version;
char *out = NULL;
cJSON *root=cJSON_CreateObject();
cJSON *aiks_controllers = cJSON_CreateArray();
cJSON_AddItemToObject(root, "aiks_controllers", aiks_controllers);
air_box_devices = cJSON_CreateArray();
cJSON_AddItemToObject(root, "air_box_devices", air_box_devices);
alarms = cJSON_CreateArray();
cJSON_AddItemToObject(root, "alarms", alarms);
ccu_link_status = cJSON_CreateObject();
link_arg = cJSON_CreateObject();
cJSON_AddItemToObject(ccu_link_status, "link_arg", link_arg);
cJSON_AddStringToObject(ccu_link_status, "link_type", "broadband_net");
cJSON_AddItemToObject(root, "ccu_link_status", ccu_link_status);
ccu_version = cJSON_CreateObject();
cJSON_AddStringToObject(ccu_version, "cur_ccu_version", "1.0.0");
cJSON_AddStringToObject(ccu_version, "downloaded_ccu_version", "1.0.0");
cJSON_AddItemToObject(root, "ccu_version", ccu_version);
central_ac_gws = cJSON_CreateArray();
cJSON_AddItemToObject(root, "central_ac_gws", central_ac_gws);
central_ac_indoorunits = cJSON_CreateArray();
cJSON_AddItemToObject(root, "central_ac_indoorunits", central_ac_indoorunits);
cnwise_music_controllers = cJSON_CreateArray();
cJSON_AddItemToObject(root, "cnwise_music_controllers", cnwise_music_controllers);
code_lib_controllers = cJSON_CreateArray();
cJSON_AddItemToObject(root, "code_lib_controllers", code_lib_controllers);
controllers = cJSON_CreateArray();
cJSON_AddItemToObject(root, "controllers", controllers);
kk_create_devicestatus_to_sdk(root);
kk_create_devices_to_sdk(root);
#if 1
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
expand_rules = cJSON_CreateArray();
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
cJSON_AddItemToObject(root, "expand_rules", expand_rules);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_create_floors_to_sdk(root);
group = cJSON_CreateArray();
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
cJSON_AddItemToObject(root, "group", group);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
guard = cJSON_CreateObject();
cJSON_AddItemToObject(root, "guard", guard);
gw_version = cJSON_CreateObject();
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
cJSON_AddItemToObject(root, "gw_version", gw_version);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_create_roominfo_to_sdk(root);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_create_scene_to_sdk(root);
#endif
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
out=cJSON_Print(root);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
printf("out:%s\n",out);
cJSON_Minify((char*)out);
_kk_send_data_to_sdk("*","*",out);
return 0;
}
void KK_Data_FromMid(void* str,int len)
{
cJSON *json;
cJSON *info_root;
cJSON *payload,*typeJson;
printf("str:%s\n",str);
json = cJSON_Parse(str);
if(json == NULL){
return;
}
info_root = cJSON_GetObjectItem(json, MSG_INFO_STR);
if(info_root == NULL) return;
payload = cJSON_GetObjectItem(json, MSG_PAYLOAD_STR);
if(payload == NULL) return;
typeJson = cJSON_GetObjectItem(info_root, MSG_TYPE_STR);
if(typeJson == NULL) return;
if (strstr(typeJson->valuestring,SYNC_MSG_TYPE_REPLY) != NULL){
kk_parse_syncinfo(payload);
kk_create_syncinfo_to_sdk();
}
}
\ No newline at end of file
#ifndef KK_DATA_HANDLE_H_
#define KK_DATA_HANDLE_H_
#include "kk_log.h"
#include "cJSON.h"
#include "kk_product.h"
int kk_data_handle(cJSON *json,int sockfd);
void KK_Data_FromMid(void* str,int len);
#endif
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kk_data_mng.h"
static kk_map_dev_ctx g_map_dev_mgr = {0};
static kk_map_dev_ctx *_kk_map_dev_ctx(void)
{
return &g_map_dev_mgr;
}
static void _kk_map_dev_mutex_lock(void)
{
kk_map_dev_ctx *ctx = _kk_map_dev_ctx();
if (ctx->mutex) {
kk_MutexLock(ctx->mutex);
}
}
static void _kk_map_dev_mutex_unlock(void)
{
kk_map_dev_ctx *ctx = _kk_map_dev_ctx();
if (ctx->mutex) {
kk_MutexUnLock(ctx->mutex);
}
}
int kk_map_dev_init(void)
{
kk_map_dev_ctx *ctx = _kk_map_dev_ctx();
memset(ctx, 0, sizeof(kk_map_dev_ctx));
ctx->mutex = kk_MutexCreate();
if (ctx->mutex == NULL) {
return -1;
}
INIT_LIST_HEAD(&ctx->dev_list);
return 0;
}
static int kk_open_cfg_file(char *deviceCode,kk_map_dev_node_t *node)
{
char path[128] = {0};
unsigned int filesize;
FILE *fp;
char *buf = NULL;
cJSON *json = NULL;
cJSON *optype = NULL;
sprintf(path,KK_DEVICE_MAP_FILE_PATH,deviceCode);
printf("kk_open_cfg_file path:%s\n",path);
if(!(fp = fopen(path,"r")))
{
ERROR_PRINT("can't open the file tslPath:%s\n",path);
return -1;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
fseek(fp, 0L, SEEK_END);
filesize = ftell(fp);
buf = malloc(filesize+1);
if(buf == NULL)
{
ERROR_PRINT("MALLOC FAIL!!!\n");
fclose(fp);
return -1;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
memset(buf,0x0,filesize+1);
fseek(fp, 0L, SEEK_SET);
fread(buf, 1, filesize, fp);
printf("buf:%s\n",buf);
json = cJSON_Parse(buf);
if(json == NULL){
ERROR_PRINT("cJSON_Parse FAIL!!!\n");
free(buf);
fclose(fp);
return -1;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
optype = cJSON_GetObjectItem(json, OPEARTETYPE_STRING);
if(optype != NULL){
memcpy(node->opearteType,optype->valuestring,strlen(optype->valuestring));
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
node->channelNum = cJSON_GetObjectItem(json, CHANNEL_STRING)->valueint;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
node->newccu = cJSON_GetObjectItem(json, NEWCCU_STRING);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
node->oldccu = cJSON_GetObjectItem(json, OLDCCU_STRING);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if(node->newccu == NULL || node->oldccu == NULL){
ERROR_PRINT("cJSON_Parse DATA FAIL!!!\n");
free(buf);
fclose(fp);
return -1;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
free(buf);
fclose(fp);
return 0;
}
static int kk_creater_nodeid(char *deviceCode,char *channel,char *nodeId)
{
if(deviceCode == NULL || channel == NULL || nodeId == NULL){
return -1;
}
strcat(nodeId,deviceCode);
strcat(nodeId,"_");
strcat(nodeId,channel);
printf("nodeId:%s\n",nodeId);
return 0;
}
static char* kk_value_int_map_string(char *opcodemap,kk_map_dev_node_t *node,int idx)
{
if(opcodemap == NULL || node == NULL){
return -1;
}
cJSON * oldccuItem = node->oldccu->child;
while(oldccuItem != NULL){
char *opcode = cJSON_GetObjectItem(oldccuItem,OPCODE_STRING)->valuestring;
char *dataType = cJSON_GetObjectItem(oldccuItem,DATATYPE_STRING)->valuestring;
if(strcmp(opcode,opcodemap) == 0){
cJSON *range = cJSON_GetObjectItem(oldccuItem,VALUERANGE_STRING);
if(range != NULL){
cJSON * pSub = cJSON_GetArrayItem(range, idx);
if(pSub != NULL){
if(strcmp(dataType,"string") == 0){
return pSub->valuestring;
}
}
}
}
}
return "";
}
static int kk_get_int_value_idx(cJSON * range,int val)
{
int i = 0;
int array_size = cJSON_GetArraySize (range);
for(i = 0; i < array_size; i++){
cJSON * pSub = cJSON_GetArrayItem(range, i);
if(pSub != NULL && pSub->valueint == val){
return i;
}
}
return -1;
}
static int _deviceCode_switchto_mac(char * deviceCode,char *mac)
{
char deviceCode_bak[33] = {0};
int i, j;
int len = strlen(deviceCode);
for(i=0; i < len; i++) /*将串s拷贝至串t*/
deviceCode_bak[i]=deviceCode[i];
deviceCode_bak[i]='\0';
for(i=0,j=0; i < len; i++){
if( i % 2 == 0 && i != 0)
{
mac[j++]=':';
mac[j++]=deviceCode_bak[i];
}
else
{
mac[j++]=deviceCode_bak[i];
}
}
mac[j]='\0'; /*在串s结尾加结束标志*/
return 0;
}
int kk_create_devices_to_sdk(cJSON *root)
{
kk_map_dev_node_t *node = NULL;
char nodeid[32] = {0};
char channel[4] = {0};
char gwmac[32] = {0};
char submac[32] = {0};
int idx = 1;
kk_map_dev_ctx *ctx = _kk_map_dev_ctx();
cJSON *devices = cJSON_CreateArray();
list_for_each_entry(node, &ctx->dev_list, linked_list, kk_map_dev_node_t) {
if (node != NULL) {
for(idx = 1; idx <= node->channelNum; idx++){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
cJSON *subdevicesItem = cJSON_CreateObject();
sprintf(channel,"%d",idx);
cJSON_AddStringToObject(subdevicesItem, CHANNEL_STRING, channel);
cJSON_AddStringToObject(subdevicesItem, DEVICE_FIELD_STRING, "");
cJSON_AddStringToObject(subdevicesItem, DEVICE_FIELD_IDNDEX_STRING, "1");
cJSON_AddStringToObject(subdevicesItem, DEVICE_ICON_STRING, "");
cJSON_AddStringToObject(subdevicesItem, DEVICE_POS_STRING, "1");
_deviceCode_switchto_mac(node->gwDeviceCode,gwmac);
printf("gwmac:%s\n",gwmac);
cJSON_AddStringToObject(subdevicesItem, GW_MAC_STRING, gwmac);
_deviceCode_switchto_mac(node->deviceCode,submac);
cJSON_AddStringToObject(subdevicesItem, MAC_STRING, submac);
cJSON_AddStringToObject(subdevicesItem, NAME_STRING, "默认灯");
memset(nodeid,0x0,sizeof(nodeid));
kk_creater_nodeid(node->deviceCode,channel,nodeid);
cJSON_AddStringToObject(subdevicesItem, NODEID_STRING, nodeid);
cJSON_AddStringToObject(subdevicesItem, OPERATE_TYPE_STRING, node->opearteType);
cJSON_AddStringToObject(subdevicesItem, ROOM_ID_STRING, "1");
cJSON_AddItemToArray(devices, subdevicesItem);
}
}
}
cJSON_AddItemToObject(root, DEVICES_STRING, devices);
return 0;
}
int kk_create_devicestatus_to_sdk(cJSON *root)
{
kk_map_dev_node_t *node = NULL;
char nodeid[32] = {0};
char indxId[4] = {0};
int idx = 1;
kk_map_dev_ctx *ctx = _kk_map_dev_ctx();
cJSON *device_status = cJSON_CreateArray();
#if 1
list_for_each_entry(node, &ctx->dev_list, linked_list, kk_map_dev_node_t) {
if (node != NULL) {
cJSON * newccuItem = node->newccu->child;
while(newccuItem != NULL){
cJSON *subdevicesItem = cJSON_CreateObject();
memset(nodeid,0x0,sizeof(nodeid));
char *opcodemap = cJSON_GetObjectItem(newccuItem,OPCODEMAP_STRING)->valuestring;
char *channel = cJSON_GetObjectItem(newccuItem,CHANNEL_STRING)->valuestring;
char *dataType = cJSON_GetObjectItem(newccuItem,DATATYPE_STRING)->valuestring;
cJSON *range = cJSON_GetObjectItem(newccuItem,VALUERANGE_STRING);
kk_creater_nodeid(node->deviceCode,channel,nodeid);
cJSON_AddStringToObject(subdevicesItem, OPCODE_STRING, opcodemap);
cJSON_AddStringToObject(subdevicesItem, NODEID_STRING, nodeid);
sprintf(indxId,"%d",idx++);
cJSON_AddStringToObject(subdevicesItem, "index", indxId);
if(strcmp(dataType,"int") == 0){
int value = cJSON_GetObjectItem(newccuItem,VALUE_STRING)->valueint;
if(range != NULL){
int index = kk_get_int_value_idx(range,value);
cJSON_AddStringToObject(subdevicesItem, "arg", kk_value_int_map_string(opcodemap,node,index));
}
}
cJSON_AddItemToArray(device_status, subdevicesItem);
newccuItem = newccuItem->next;
}
}
}
#endif
cJSON_AddItemToObject(root, DEVICE_STATUS_STRING, device_status);
return 0;
}
int kk_map_dev_update_int_value(kk_map_dev_node_t *node,char *identifier,int val)
{
int ret = 0;
if(node != NULL && node->newccu != NULL){
cJSON * newccuItem = node->newccu->child;
while(newccuItem != NULL){
char *identifier_tmp = cJSON_GetObjectItem(newccuItem,MSG_INDENTIFIER_STR)->valuestring;
if(strcmp(identifier_tmp,identifier) == 0){
cJSON_ReplaceItemInObject(newccuItem, "value", cJSON_CreateNumber(val));
break;
}
newccuItem = newccuItem->next;
}
}
return 0;
}
int kk_map_dev_update_int_value_by_devicecode(char *deviceCode,char *identifier,int val)
{
int ret = 0;
kk_map_dev_node_t *node = NULL;
ret = kk_map_dev_search_by_deviceCode(deviceCode,&node);
if(ret != 0){
ERROR_PRINT("kk_map_dev_update_int_value, err: %s", deviceCode);
return ret;
}
if(node != NULL && node->newccu != NULL){
cJSON * newccuItem = node->newccu->child;
while(newccuItem != NULL){
char *identifier_tmp = cJSON_GetObjectItem(newccuItem,MSG_INDENTIFIER_STR)->valuestring;
if(strcmp(identifier_tmp,identifier) == 0){
cJSON_ReplaceItemInObject(newccuItem, "value", cJSON_CreateNumber(val));
break;
}
newccuItem = newccuItem->next;
}
}
return 0;
}
int kk_map_dev_search_by_deviceCode(char *deviceCode, kk_map_dev_node_t **node)
{
kk_map_dev_ctx *ctx = _kk_map_dev_ctx();
kk_map_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, kk_map_dev_node_t) {
if ( (strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, devid: %d", devid); */
if (node) {
*node = search_node;
}
return 0;
}
}
ERROR_PRINT("Device Not Found, deviceCode: %s", deviceCode);
return -1;
}
kk_map_dev_node_t *kk_map_dev_add(char *deviceCode,char *productCode,char *gwdeviceCode)
{
#if 1
kk_map_dev_node_t *node = NULL;
kk_map_dev_ctx *ctx = _kk_map_dev_ctx();
node = malloc(sizeof(kk_map_dev_node_t));
if (node == NULL) {
return NULL;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
_kk_map_dev_mutex_lock();
memset(node,0x0,sizeof(kk_map_dev_node_t));
memcpy(node->gwDeviceCode,gwdeviceCode,strlen(gwdeviceCode));
memcpy(node->deviceCode,deviceCode,strlen(deviceCode));
memcpy(node->productCode,productCode,strlen(productCode));
kk_open_cfg_file(productCode,node);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
INIT_LIST_HEAD(&node->linked_list);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
list_add_tail(&node->linked_list, &ctx->dev_list);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
_kk_map_dev_mutex_unlock();
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
return node;
#endif
}
\ No newline at end of file
#ifndef KK_DATA_MNG_H_
#define KK_DATA_MNG_H_
#include "kk_log.h"
#include "cJSON.h"
#include "klist.h"
#include "kk_product.h"
#include "com_api.h"
#include "kk_opcode.h"
typedef struct {
cJSON *newccu;
cJSON *oldccu;
int channelNum;
char gwDeviceCode[DEVICE_CODE_LEN];
char productCode[PRODUCT_CODE_LEN];
char deviceCode[DEVICE_CODE_LEN];
char opearteType[8];
struct list_head linked_list;
} kk_map_dev_node_t;
typedef struct {
void *mutex;
struct list_head dev_list;
} kk_map_dev_ctx;
kk_map_dev_node_t *kk_map_dev_add(char *deviceCode,char *productCode,char *gwdeviceCode);
int kk_create_devicestatus_to_sdk(cJSON *root);
int kk_create_devices_to_sdk(cJSON *root);
#endif
\ No newline at end of file
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "kk_findccu_handle.h"
#include "kk_hal.h"
#include "cJSON.h"
static int kk_findccu_ack(int sockfd,struct sockaddr_in *addr){
cJSON *json = NULL,*args = NULL;
cJSON *ccu = NULL;
char *out = NULL;
char *tmpBuf = NULL;
char s_IP[NETWORK_ADDR_LEN] = {0};
if(addr == NULL || sockfd < 0){
return -1;
}
json=cJSON_CreateObject();
if(json){
args = cJSON_CreateObject();
if(args){
cJSON_AddItemToObject(json, "arg", args);
cJSON_AddStringToObject(args, "zkid", KK_CCU_ID);
cJSON_AddStringToObject(args, "zk", KK_CCU_NAME);
HAL_Get_IP(s_IP,NULL);
cJSON_AddStringToObject(args, "ip", s_IP);
cJSON_AddBoolToObject(args, "ssl", FALSE);
}
cJSON_AddStringToObject(json, "nodeid", "*");
cJSON_AddStringToObject(json, "opcode", FINDCCU_OPCODE);
cJSON_AddStringToObject(json, "status", "success");
out=cJSON_Print(json);
cJSON_Minify((char*)out);
tmpBuf = calloc(strlen(out) + 4,1);
if(tmpBuf == NULL){
cJSON_Delete(json);
free(out);
return -1;
}
strcat(tmpBuf,"!");
strcat(tmpBuf,out);
strcat(tmpBuf,"$");
printf("tmpBuf:%s\n",tmpBuf);
sendto(sockfd, tmpBuf, strlen(tmpBuf), 0, (struct sockaddr*)addr, sizeof(struct sockaddr_in));
cJSON_Delete(json);
free(out);
free(tmpBuf);
}
return 0;
}
void *kk_findccu_handle(void *data)
{
int sockfd;
struct sockaddr_in saddr;
int r;
char recvline[1025] = {0};
char recvline_tmp[1025] = {0};
struct sockaddr_in presaddr;
socklen_t len;
cJSON *json;
cJSON *opcode;
char *pStart = NULL,*pEnd = NULL;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(FINDCCU_LOCAL_PORT);
if(bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1)
{
ERROR_PRINT("bind error...\n");
}
while (1)
{
r = recvfrom(sockfd, recvline, sizeof(recvline), 0 , (struct sockaddr*)&presaddr, &len);
if (r <= 0){
WARNING_PRINT("read error....\n");
}else{
DEBUG_PRINT("findccu recmsg: %s\n", recvline);
pStart = strstr(recvline, "!");
pEnd = strstr(recvline, "$");
if(pStart != NULL && pEnd != NULL){
memset(recvline_tmp,0x0,sizeof(recvline_tmp));
memcpy(recvline_tmp,pStart+1,(pEnd - pStart - 1));
json=cJSON_Parse(recvline_tmp);
if (!json) {
WARNING_PRINT("Error before: [%s]\n","cJSON_Parse");
}else{
opcode = cJSON_GetObjectItem(json, OPCODE_STRING);
if(opcode != NULL){
if(strcmp(opcode->valuestring,FINDCCU_OPCODE) == 0){
kk_findccu_ack(sockfd,&presaddr);
}
}
cJSON_Delete(json);
}
}else{
WARNING_PRINT("data error....\n");
}
}
}
return NULL;
}
int kk_findccu_handle_init(void)
{
int rc = 0;
size_t s = 1500;
pthread_t findccu_thread_handle;
pthread_attr_t findccu_thread_attr;
//pthread_mutex_init(&s_data_mutex, NULL);
/*创建线程*/
pthread_attr_init(&findccu_thread_attr);
pthread_attr_setstacksize(&findccu_thread_attr, s);
pthread_attr_setdetachstate(&findccu_thread_attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&findccu_thread_handle, &findccu_thread_attr, kk_findccu_handle, (void *)NULL); //收数据
if (rc)
{
WARNING_PRINT("Error : unable to create thread udp_recv \r\n");
return -1;
}
pthread_attr_destroy(&findccu_thread_attr);
return 0;
}
\ No newline at end of file
#ifndef KK_FINDCCU_HANDLE_H_
#define KK_FINDCCU_HANDLE_H_
#include "kk_product.h"
#include "kk_log.h"
#include "kk_opcode.h"
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#define FINDCCU_LOCAL_PORT 8989
int kk_findccu_handle_init(void);
#endif
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2012, 2020 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/
#include <stdio.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "com_api.h"
#include "kk_product.h"
#include "kk_findccu_handle.h"
#include "kk_login_handle.h"
#include "kk_data_handle.h"
//#include "kcloud_log.h"
int main(int argc, char* argv[])
{
int rc = 0;
open("kk_lan",LOG_PID,LOG_USER);
/*set the callback to get the device date to cloud*/
kk_ipc_init(IPC_APP2MID,(ipc_cb*)KK_Data_FromMid,NULL,NULL);
kk_findccu_handle_init();
kk_map_dev_init();
kk_login_init();
while(1){
//count++;
//if(count == 10){
//kk_ipc_send(IPC_APP2MID, "wooooooooooooooooooooooooooooooooooollllll!", strlen("wooooooooooooooooooooooooooooooooooollllll!")+1);
//}
sleep(1);
}
return rc;
}
#include<stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include "kk_login_handle.h"
#include "kk_data_handle.h"
#define LISTEN_MAX 5
#define BUF_SIZE 1500
typedef struct {
int socketfd;
char ip[18];
} kk_clientInfo_t;
static kk_clientInfo_t s_ConnInfo[LISTEN_MAX] = {0};
static pthread_mutex_t data_mutex;
fd_set fds;
int kk_send_data_to_sdk(char *buf)
{
char *tmpBuf = NULL;
int i = 0;
tmpBuf = calloc(strlen(buf) + 4,1);
if(tmpBuf == NULL){
return -1;
}
strcat(tmpBuf,"!");
strcat(tmpBuf,buf);
strcat(tmpBuf,"$");
printf("tmpBuf:%s\n",tmpBuf);
for(i = 0; i < LISTEN_MAX; i++){
if(s_ConnInfo[i].socketfd != -1){
send(s_ConnInfo[i].socketfd, tmpBuf, strlen(tmpBuf), 0);
}
}
free(buf);
free(tmpBuf);
return 0;
}
static int _kk_handle_data(char *buf,int sockfd){
char *pStart = NULL,*pEnd = NULL;
char tmpBuf[BUF_SIZE] = {0};
cJSON *json;
if(buf == NULL){
return -1;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
pStart = strstr(buf, "!");
pEnd = strstr(buf, "$");
if(pStart != NULL && pEnd != NULL){
memset(tmpBuf,0x0,sizeof(tmpBuf));
memcpy(tmpBuf,pStart+1,(pEnd - pStart - 1));
}
json=cJSON_Parse(tmpBuf);
if (!json) {
WARNING_PRINT("Error before: [%s]\n","cJSON_Parse");
}else{
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_data_handle(json,sockfd);
cJSON_Delete(json);
}
}
static void PrintMesg(int i , char buf[])
{
printf("fd : %d, msg: %s\n", i , buf);
}
void *TCP_Analyzer(void *pPara)
{
char Buf[BUF_SIZE] = {0};
ssize_t Size = 0;
int index = 0;
unsigned char *pReceiveData = NULL;
int ReceiveLen = 0;
if (NULL == pPara)
{
ERROR_PRINT("TCP_Analyzer: TCP_Analyzer Failed!\n");
return NULL;
}
index = *(int *)pPara;
printf("index:%d\n",*(int *)pPara);
if(index < 0 || index >= LISTEN_MAX)
{
ERROR_PRINT("TCP_Analyzer:Get pConnfd Failed \n" );
return NULL;
}
while(1)
{
memset(Buf, '\0', sizeof(Buf));
Size = read(s_ConnInfo[index].socketfd, Buf,sizeof(Buf) );
if (Size <= 0) //没有接收到数据,关闭描述符,释放在TCPServer申请的空间
{
ERROR_PRINT("TCP_Analyzer:remote client close:%d\n",s_ConnInfo[index].socketfd );
pthread_mutex_lock(&data_mutex);
close(s_ConnInfo[index].socketfd);
s_ConnInfo[index].socketfd = -1;
memset(s_ConnInfo[index].ip,0x0,sizeof(s_ConnInfo[index].ip));
pthread_mutex_unlock(&data_mutex);
return NULL;
}
else
{
printf("TCP_Analyzer:%s,%d\n",Buf,(int)Size);
_kk_handle_data(Buf,s_ConnInfo[index].socketfd);
}
}
return NULL;
}
static int kk_check_is_connect(char *ip){
int i = 0;
if(ip == NULL){
return -1;
}
for(i = 0; i < LISTEN_MAX; i ++){
if(s_ConnInfo[i].socketfd != -1 && strcmp(s_ConnInfo[i].ip,ip) == 0){
return 1;
}
}
return 0;
}
void *TCPServer()
{
pthread_t threadID = 0;
struct sockaddr_in Server;
struct sockaddr_in Client;
int Listenfd = 0;
int i = 0;
int j = 0;
int yes = 1;
int index = 0;
int Connfd = 0;
int ret = 0;
char clientIp[18] = {0};
socklen_t len = 0;
//char logMessage[128] = {0};
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
Listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (Listenfd < 0)
{
perror("socket");
pthread_attr_destroy(&attr);//线程属性销毁
return NULL;
}
DEBUG_PRINT("TCPServer:create socket success\n");
setsockopt(Listenfd, SOL_SOCKET, SO_REUSEADDR, &yes ,sizeof(int));// 允许IP地址复用
bzero(&Server, sizeof(Server));
Server.sin_family = AF_INET;
Server.sin_port = htons(SERVER_LISTEN_PORT);
Server.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(Listenfd, (struct sockaddr*)&Server, sizeof(Server)) < 0)
{
perror("bind");
close(Listenfd);
pthread_attr_destroy(&attr);//线程属性销毁
return NULL;
}
DEBUG_PRINT("TCPServer:bind socket success\n");
if (listen(Listenfd, LISTEN_MAX) < 0)
{
perror("listen");
close(Listenfd);
pthread_attr_destroy(&attr);//线程属性销毁
return NULL;
}
DEBUG_PRINT("TCPServer:listen socket success\n");
while (1)
{
//index = -1;
FD_ZERO(&fds);//描述符集合初始化
FD_SET(Listenfd,&fds);
struct timeval timeout = {1, 0};
ret = select(Listenfd + 1, &fds,NULL, NULL, &timeout );
if(ret <= 0){
DEBUG_PRINT("TCPServer:TCP receiving nothing......\n");
//break;
}else{
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if (FD_ISSET(Listenfd, &fds))
{
len = sizeof(Client);
bzero(&Client, len);
Connfd = accept(Listenfd, (struct sockaddr*)&Client,&len );
printf("addr:%s\n",inet_ntoa(Client.sin_addr));
printf("[%s][%d]Connfd:%d\n",__FUNCTION__,__LINE__,Connfd);
//若有新的连接
if (Connfd != -1)
{
memset(clientIp,0x0,sizeof(clientIp));
strcpy(clientIp,inet_ntoa(Client.sin_addr));
if(kk_check_is_connect(clientIp) == 1){
DEBUG_PRINT("already connect!!!\n");
continue;
}
for(i = 0; i < LISTEN_MAX; i ++){
if(s_ConnInfo[i].socketfd != -1){
if(i == 4){
DEBUG_PRINT("more then max client!!!\n");
}
continue;
}else{
pthread_mutex_lock(&data_mutex);
s_ConnInfo[i].socketfd = Connfd;
memcpy(s_ConnInfo[i].ip,clientIp,strlen(clientIp));
index = i;
printf("index:%d\n",index);
pthread_mutex_unlock(&data_mutex);
break;
}
}
ret = pthread_create(&threadID, &attr, TCP_Analyzer, &index);
if(0 != ret)
{
DEBUG_PRINT("TCPServer: TCP_Analyzer build Fail!\n");
FD_CLR(Listenfd, &fds);// 清除 fds中相应的文件描述符
close(Listenfd);
pthread_attr_destroy(&attr);//线程属性销毁
return NULL;
}
}
}
}
}
FD_CLR(Listenfd, &fds);// 清除 fds中相应的文件描述符
close(Listenfd);
pthread_attr_destroy(&attr);//线程属性销毁
return NULL;
}
int kk_login_init()
{
int ret = 0;
int i = 0;
pthread_t threadID = 0;
DEBUG_PRINT("kk_login_init Init OK!\n");
for(i = 0; i < LISTEN_MAX; i ++){
s_ConnInfo[i].socketfd = -1;
memset(s_ConnInfo[i].ip,0x0,sizeof(s_ConnInfo[i].ip));
}
if (pthread_mutex_init(&data_mutex, NULL) != 0) {
ERROR_PRINT("pthread_mutex_init kk_login_init err\n");
return -1;
}
ret = pthread_create(&threadID, NULL, TCPServer, NULL);
if(0 != ret)
{
ERROR_PRINT("TCPServer: TCPServer build Fail!\n");
return -1;
}
return 0;
}
#ifndef KK_LOGIN_HANDLE_H_
#define KK_LOGIN_HANDLE_H_
#include "kk_log.h"
#include "cJSON.h"
#define SERVER_LISTEN_PORT 5000
int kk_login_init();
#endif
\ No newline at end of file
#ifndef KK_OPCODE_H_
#define KK_OPCODE_H_
#define SYNC_MSG_TYPE "/thing/service/syncinfo"
#define SYNC_MSG_TYPE_REPLY "/thing/service/syncinfo_reply"
#define DATA_STRING "data"
#define VALUE_STRING "value"
#define OPCODEMAP_STRING "opcodemap"
#define CHANNEL_STRING "channel"
#define NODEID_STRING "nodeid"
#define DEVICES_STRING "devices"
#define DATATYPE_STRING "dataType"
#define OPCODE_STRING "opcode"
#define ARG_STRING "arg"
#define ZKID_STRING "zkid"
#define OPEARTETYPE_STRING "operateType"
#define NEWCCU_STRING "newccu"
#define OLDCCU_STRING "oldccu"
#define FINDCCU_OPCODE "FIND_CCU"
#define LOGIN_OPCODE "LOGIN"
#define HEARTBEAT_OPCODE "CCU_HB"
#define SYNC_OPCODE "SYNC_INFO"
#define VALUERANGE_STRING "valueRange"
#define DEVICE_FIELD_STRING "device_field"
#define DEVICE_FIELD_IDNDEX_STRING "device_field_index"
#define DEVICE_ICON_STRING "device_icon"
#define DEVICE_POS_STRING "device_pos"
#define GW_MAC_STRING "gw_mac"
#define MAC_STRING "mac"
#define NAME_STRING "name"
#define OPERATE_TYPE_STRING "operate_type"
#define ROOM_ID_STRING "room_id"
#define DEVICE_STATUS_STRING "device_status"
#endif
\ No newline at end of file
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
#include "kk_log.h" #include "kk_log.h"
#define APP2MID "ipc:///tmp/app2mid.ipc" #define APP2MID "ipc:///tmp/app2mid.ipc"
#define APP2MID_PUBSUB "ipc:///tmp/app2mid_pubsub.ipc"
#define PLAT2MID "ipc:///tmp/plat2mid.ipc" #define PLAT2MID "ipc:///tmp/plat2mid.ipc"
#define GW2CCU_PIPE "tcp://%s:5555" #define GW2CCU_PIPE "tcp://%s:35567"
#define GW2CCU_PUBSUB "tcp://%s:5557" #define GW2CCU_PUBSUB "tcp://%s:35568"
#define MAGIC "magic12" #define MAGIC "magic12"
#define MAGIC_ACK "magic12ack" #define MAGIC_ACK "magic12ack"
#define FILTERSTR "|" #define FILTERSTR "|"
typedef struct { typedef struct {
...@@ -67,7 +67,7 @@ static void watcher_cb (struct ev_loop *loop ,struct ev_io *w, int revents) ...@@ -67,7 +67,7 @@ static void watcher_cb (struct ev_loop *loop ,struct ev_io *w, int revents)
uint32_t bytes =0; uint32_t bytes =0;
uint32_t validLen =0; uint32_t validLen =0;
char *dat_bak = NULL; char *dat_bak = NULL;
if (loop_ctrl->type == IPC_PLAT2MID){ if (loop_ctrl->type == IPC_PLAT2MID ||loop_ctrl->type == IPC_APP2MID){
bytes = nn_recv(loop_ctrl->ab.n, &dat, NN_MSG, NN_DONTWAIT); bytes = nn_recv(loop_ctrl->ab.n, &dat, NN_MSG, NN_DONTWAIT);
}else{ }else{
bytes = nn_recv(loop_ctrl->ba.n, &dat, NN_MSG, NN_DONTWAIT); bytes = nn_recv(loop_ctrl->ba.n, &dat, NN_MSG, NN_DONTWAIT);
...@@ -120,7 +120,8 @@ void __loop_init(Bloop_ctrl_t *loop_ctrl, struct ev_loop* loop) ...@@ -120,7 +120,8 @@ void __loop_init(Bloop_ctrl_t *loop_ctrl, struct ev_loop* loop)
{ {
loop_ctrl->watcher.data = loop_ctrl; loop_ctrl->watcher.data = loop_ctrl;
if (loop_ctrl->type == IPC_PLAT2MID){ if (loop_ctrl->type == IPC_PLAT2MID || loop_ctrl->type == IPC_APP2MID){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
ev_io_init (&(loop_ctrl->watcher), watcher_cb, loop_ctrl->ab.s, EV_READ); ev_io_init (&(loop_ctrl->watcher), watcher_cb, loop_ctrl->ab.s, EV_READ);
}else{ }else{
ev_io_init (&(loop_ctrl->watcher), watcher_cb, loop_ctrl->ba.s, EV_READ); ev_io_init (&(loop_ctrl->watcher), watcher_cb, loop_ctrl->ba.s, EV_READ);
...@@ -146,27 +147,50 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i ...@@ -146,27 +147,50 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i
} }
memcpy(loop_ctrl->subscrStr, chlMark, strlen(chlMark)); memcpy(loop_ctrl->subscrStr, chlMark, strlen(chlMark));
} }
switch (type) { switch (type) {
case IPC_APP2MID:{ case IPC_APP2MID:{
loop_ctrl->ba.n = nn_socket(AF_SP, NN_PAIR); loop_ctrl->ba.n = nn_socket(AF_SP, NN_PUSH);
if (loop_ctrl->ba.n < 0) { if (loop_ctrl->ba.n < 0) {
ERROR_PRINT("__nanomsg_init loop_ctrl->ba.n=%d \r\n",loop_ctrl->ba.n); ERROR_PRINT("__nanomsg_init loop_ctrl->ba.n=%d \r\n",loop_ctrl->ba.n);
return -1; return -1;
} }
loop_ctrl->ab.n = nn_socket(AF_SP, NN_SUB);
if (loop_ctrl->ab.n < 0) {
ERROR_PRINT("loop_ctrl->ab.n =%d \r\n",loop_ctrl->ab.n);
return -1;
}
if (nn_connect(loop_ctrl->ba.n, APP2MID) < 0) { if (nn_connect(loop_ctrl->ba.n, APP2MID) < 0) {
return -1; return -1;
} }
//订阅
if (nn_setsockopt(loop_ctrl->ab.n, NN_SUB, NN_SUB_SUBSCRIBE, "", 0) < 0) {
ERROR_PRINT("nn_setsockopt failed ");
return -1;
}
if (nn_connect(loop_ctrl->ab.n, APP2MID_PUBSUB) < 0) {//sub
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
return -1;
}
} }
break; break;
case IPC_MID2APP: { case IPC_MID2APP: {
loop_ctrl->ba.n = nn_socket(AF_SP, NN_PAIR); loop_ctrl->ba.n = nn_socket(AF_SP, NN_PULL);
if (loop_ctrl->ba.n < 0) { if (loop_ctrl->ba.n < 0) {
ERROR_PRINT("__nanomsg_init loop_ctrl->ba.n=%d \r\n",loop_ctrl->ba.n); ERROR_PRINT("__nanomsg_init loop_ctrl->ba.n=%d \r\n",loop_ctrl->ba.n);
return -1; return -1;
} }
loop_ctrl->ab.n = nn_socket(AF_SP, NN_PUB);
if (loop_ctrl->ab.n < 0) {
ERROR_PRINT("__nanomsg_init loop_ctrl->ab.n =%d \r\n",loop_ctrl->ab.n);
return -1;
}
if (nn_bind(loop_ctrl->ba.n, APP2MID) < 0) { if (nn_bind(loop_ctrl->ba.n, APP2MID) < 0) {
ERROR_PRINT("__nanomsg_init loop_ctrl->ba.n nn_bind error \r\n");
return -1;
}
if (nn_bind(loop_ctrl->ab.n, APP2MID_PUBSUB) < 0) {//pub
ERROR_PRINT("__nanomsg_init loop_ctrl->ab.n nn_bind error \r\n");
return -1; return -1;
} }
} }
...@@ -174,7 +198,7 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i ...@@ -174,7 +198,7 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i
case IPC_PLAT2MID: { case IPC_PLAT2MID: {
//创建2个通道 pipe和pub/sub //创建2个通道 pipe和pub/sub
char addr[30] = {0}; char addr[30] = {0};
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
loop_ctrl->ba.n = nn_socket(AF_SP, NN_PUSH); loop_ctrl->ba.n = nn_socket(AF_SP, NN_PUSH);
if (loop_ctrl->ba.n < 0) { if (loop_ctrl->ba.n < 0) {
ERROR_PRINT("loop_ctrl->ba.n =%d \r\n",loop_ctrl->ba.n); ERROR_PRINT("loop_ctrl->ba.n =%d \r\n",loop_ctrl->ba.n);
...@@ -188,7 +212,9 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i ...@@ -188,7 +212,9 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i
} }
sprintf(addr, GW2CCU_PIPE, ip); sprintf(addr, GW2CCU_PIPE, ip);
printf("---------addr:%s\n",addr);
if (nn_connect(loop_ctrl->ba.n, addr) < 0) {//pipe if (nn_connect(loop_ctrl->ba.n, addr) < 0) {//pipe
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
return -1; return -1;
} }
//订阅 //订阅
...@@ -202,7 +228,9 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i ...@@ -202,7 +228,9 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i
memset(addr,0, sizeof(addr)); memset(addr,0, sizeof(addr));
sprintf(addr, GW2CCU_PUBSUB, ip); sprintf(addr, GW2CCU_PUBSUB, ip);
printf("---------addr:%s\n",addr);
if (nn_connect(loop_ctrl->ab.n, addr) < 0) {//sub if (nn_connect(loop_ctrl->ab.n, addr) < 0) {//sub
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
return -1; return -1;
} }
...@@ -243,7 +271,8 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i ...@@ -243,7 +271,8 @@ int __nanomsg_init(Bloop_ctrl_t *loop_ctrl, ipc_type type,char* chlMark, char* i
} }
size_t size = sizeof(size_t); size_t size = sizeof(size_t);
if (IPC_PLAT2MID == type){ if (IPC_PLAT2MID == type || IPC_APP2MID == type){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if (nn_getsockopt(loop_ctrl->ab.n, NN_SOL_SOCKET, NN_RCVFD, (char *)&loop_ctrl->ab.s, &size) < 0) { if (nn_getsockopt(loop_ctrl->ab.n, NN_SOL_SOCKET, NN_RCVFD, (char *)&loop_ctrl->ab.s, &size) < 0) {
ERROR_PRINT("nn_getsockopt IPC_PLAT2MID loop_ctrl->ab.s = %d \n", loop_ctrl->ab.s); ERROR_PRINT("nn_getsockopt IPC_PLAT2MID loop_ctrl->ab.s = %d \n", loop_ctrl->ab.s);
return -1; return -1;
...@@ -287,24 +316,24 @@ int kk_ipc_init(ipc_type type, ipc_cb cb, char* chlMark, char* ip) ...@@ -287,24 +316,24 @@ int kk_ipc_init(ipc_type type, ipc_cb cb, char* chlMark, char* ip)
}else { }else {
loop_ctrl = &Bloop_ctrl; loop_ctrl = &Bloop_ctrl;
} }
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if (type == IPC_PLAT2MID && (chlMark == NULL || ip == NULL)){ if (type == IPC_PLAT2MID && (chlMark == NULL || ip == NULL)){
ERROR_PRINT("parameter is error \r\n"); ERROR_PRINT("parameter is error \r\n");
return -1; return -1;
} }
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if(loop_ctrl->cb != NULL){ if(loop_ctrl->cb != NULL){
WARNING_PRINT("middleware to platform ipc has been inited!\r\n"); WARNING_PRINT("middleware to platform ipc has been inited!\r\n");
return -1; return -1;
} }
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if (__nanomsg_init(loop_ctrl, type, chlMark, ip) < 0) { if (__nanomsg_init(loop_ctrl, type, chlMark, ip) < 0) {
ERROR_PRINT("nanomsg init failed\r\n"); ERROR_PRINT("nanomsg init failed\r\n");
return -1; return -1;
} }
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if (gloop == NULL){ if (gloop == NULL){
gloop = ev_loop_new(EVBACKEND_EPOLL); gloop = ev_loop_new(EVBACKEND_EPOLL);
if (NULL == gloop) { if (NULL == gloop) {
...@@ -313,7 +342,7 @@ int kk_ipc_init(ipc_type type, ipc_cb cb, char* chlMark, char* ip) ...@@ -313,7 +342,7 @@ int kk_ipc_init(ipc_type type, ipc_cb cb, char* chlMark, char* ip)
} }
} }
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
loop_ctrl->type = type; loop_ctrl->type = type;
__loop_init(loop_ctrl, gloop); __loop_init(loop_ctrl, gloop);
...@@ -321,7 +350,7 @@ int kk_ipc_init(ipc_type type, ipc_cb cb, char* chlMark, char* ip) ...@@ -321,7 +350,7 @@ int kk_ipc_init(ipc_type type, ipc_cb cb, char* chlMark, char* ip)
ERROR_PRINT("create pthread failed\r\n"); ERROR_PRINT("create pthread failed\r\n");
return -1; return -1;
} }
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
loop_ctrl->cb = cb; loop_ctrl->cb = cb;
loop_ctrl->type = type; loop_ctrl->type = type;
return 0; return 0;
...@@ -374,7 +403,7 @@ int kk_ipc_send(ipc_type type, void* data, int len) ...@@ -374,7 +403,7 @@ int kk_ipc_send(ipc_type type, void* data, int len)
chalMark = Bloop_ctrl.subscrStr; chalMark = Bloop_ctrl.subscrStr;
} }
} }
printf("kk_ipc_send data:%s\n",data);
return kk_ipc_send_ex(type, data, len, chalMark); return kk_ipc_send_ex(type, data, len, chalMark);
} }
...@@ -403,10 +432,14 @@ int kk_ipc_send_ex(ipc_type type, void* data, int len, char* chalMark) ...@@ -403,10 +432,14 @@ int kk_ipc_send_ex(ipc_type type, void* data, int len, char* chalMark)
} }
memcpy(buf + filterlen, data, len); memcpy(buf + filterlen, data, len);
if (type == IPC_MID2PLAT){ if (type == IPC_MID2PLAT ){
loop_ctrl = &Mloop_ctrl; loop_ctrl = &Mloop_ctrl;
ret =nn_send(Mloop_ctrl.ab.n, &buf, NN_MSG, NN_DONTWAIT);//NN_DONTWAIT ret =nn_send(Mloop_ctrl.ab.n, &buf, NN_MSG, NN_DONTWAIT);//NN_DONTWAIT
}else{ }else if(type == IPC_MID2APP){
loop_ctrl = &Bloop_ctrl;
ret = nn_send(Bloop_ctrl.ab.n, &buf, NN_MSG, NN_DONTWAIT);
}
else{
loop_ctrl = &Bloop_ctrl; loop_ctrl = &Bloop_ctrl;
ret = nn_send(Bloop_ctrl.ba.n, &buf, NN_MSG, NN_DONTWAIT); ret = nn_send(Bloop_ctrl.ba.n, &buf, NN_MSG, NN_DONTWAIT);
} }
......
...@@ -17,7 +17,7 @@ extern "C" { ...@@ -17,7 +17,7 @@ extern "C" {
#include "pair.h" #include "pair.h"
#include "pubsub.h" #include "pubsub.h"
#include "pipeline.h" #include "pipeline.h"
#include "kk_product.h"
//=====kk====================== //=====kk======================
typedef enum { typedef enum {
...@@ -62,7 +62,7 @@ typedef enum { ...@@ -62,7 +62,7 @@ typedef enum {
#define MSG_TIMER_SETCOUNTDOWN_CURRENTTIME "CurrentTime" #define MSG_TIMER_SETCOUNTDOWN_CURRENTTIME "CurrentTime"
#define MSG_TIMER_SETCOUNTDOWN_GETCOUNTDOWN "getCountDown" #define MSG_TIMER_SETCOUNTDOWN_GETCOUNTDOWN "getCountDown"
#define MSG_PROPERTY_STR "property" #define MSG_PROPERTY_STR "property"
#define MSG_PROPERTIES_STR "properties"
/************************LOCK KEY*************************/ /************************LOCK KEY*************************/
#define MSG_KEYDELETE_NOTIFICATION_KEYID "KeyDeletedNotification.KeyID" #define MSG_KEYDELETE_NOTIFICATION_KEYID "KeyDeletedNotification.KeyID"
#define MSG_KEYDELETE_NOTIFICATION_KEYTYPE "KeyDeletedNotification.KeyType" #define MSG_KEYDELETE_NOTIFICATION_KEYTYPE "KeyDeletedNotification.KeyType"
...@@ -129,12 +129,29 @@ typedef enum { ...@@ -129,12 +129,29 @@ typedef enum {
#define MSG_SCENE_EXECUTENOTIFICATION_SCENEID "executeSceneNotification.sceneId" #define MSG_SCENE_EXECUTENOTIFICATION_SCENEID "executeSceneNotification.sceneId"
#define MSG_SCENE_EXECUTENOTIFICATION "executeSceneNotification" #define MSG_SCENE_EXECUTENOTIFICATION "executeSceneNotification"
#define CCU_TCP_PORT 16565
#define BUFFER_SIZE 1024
#define MAX_LISTEN_NUM 10
#define MAX_IP_LEN 16
//#ifndef DEVICE_CODE_LEN
//#define DEVICE_CODE_LEN 33
//#endif
typedef void ipc_cb(void* data, int len, char* chalMark); typedef void ipc_cb(void* data, int len, char* chalMark);
int kk_ipc_init(ipc_type type, ipc_cb cb, char* chalMark, char* ip); int kk_ipc_init(ipc_type type, ipc_cb cb, char* chalMark, char* ip);
int kk_ipc_deinit(ipc_type type); int kk_ipc_deinit(ipc_type type);
int kk_ipc_send(ipc_type type, void* data, int len); int kk_ipc_send(ipc_type type, void* data, int len);
int kk_ipc_send_ex(ipc_type type, void* data, int len, char* chalMark); int kk_ipc_send_ex(ipc_type type, void* data, int len, char* chalMark);
int kk_tcp_channel_ser_send(char* data, int len, char chalMark[DEVICE_CODE_LEN]);
int kk_TCP_channel_init(ipc_cb cb);
int kk_TCP_channel_deinit(ipc_type type);
int kk_is_tcp_channel(char devCode[DEVICE_CODE_LEN]);
int kk_set_tcp_channel_by_idx(int idx, char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]);
int kk_set_tcp_channel(char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]);
int kk_get_retry_num();
int kk_reset_retry_num();
int kk_tcp_client_send(char* data, int len);
int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb);
void kk_tcp_client_deinit(void);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif
......
#include <stdio.h> #include <stdio.h>
#include <pthread.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -13,12 +14,6 @@ ...@@ -13,12 +14,6 @@
#include "com_api.h" #include "com_api.h"
#include "kk_log.h" #include "kk_log.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{ typedef struct{
...@@ -86,13 +81,14 @@ static void _MutexUnlock(void *mutex) ...@@ -86,13 +81,14 @@ static void _MutexUnlock(void *mutex)
static char *_next_json(char *str, int* hasFloor) static char *_next_json(char *str, int* hasFloor)
{ {
if(str == NULL) {
return NULL;
}
char *ptr = str; char *ptr = str;
int floor = 0; int floor = 0;
// judge if inside the "..." // judge if inside the "..."
int quotes = 0; int quotes = 0;
if(str == NULL) {
return NULL;
}
while(*ptr) { while(*ptr) {
if(*ptr++ == '{') { if(*ptr++ == '{') {
++floor; ++floor;
...@@ -149,7 +145,7 @@ static char* __do_data(char* buf, int buflen,ipc_cb* cb, char* deviceCode){ ...@@ -149,7 +145,7 @@ static char* __do_data(char* buf, int buflen,ipc_cb* cb, char* deviceCode){
char* startIdx = buf; char* startIdx = buf;
int hasfloor = 0; int hasfloor = 0;
char buf2[1024] = {0}; char buf2[1024] = {0};
int cplen = 0;
while(startIdx < buf + buflen){ while(startIdx < buf + buflen){
hasfloor = 0; hasfloor = 0;
endIdx = _next_json(startIdx,&hasfloor); endIdx = _next_json(startIdx,&hasfloor);
...@@ -163,7 +159,7 @@ static char* __do_data(char* buf, int buflen,ipc_cb* cb, char* deviceCode){ ...@@ -163,7 +159,7 @@ static char* __do_data(char* buf, int buflen,ipc_cb* cb, char* deviceCode){
return startIdx; return startIdx;
} }
int cplen = endIdx - startIdx; cplen = endIdx - startIdx;
memset(buf2,0, sizeof(buf2)); memset(buf2,0, sizeof(buf2));
memcpy(buf2,startIdx, cplen); memcpy(buf2,startIdx, cplen);
...@@ -220,13 +216,13 @@ static void kk_gw_list_load(void) ...@@ -220,13 +216,13 @@ static void kk_gw_list_load(void)
FILE *fp; FILE *fp;
int readLen = MAX_LISTEN_NUM * sizeof(kk_tcp_ctrl_t); int readLen = MAX_LISTEN_NUM * sizeof(kk_tcp_ctrl_t);
int retLen = 0; int retLen = 0;
char buf[1024] = {0};
fp = fopen("gwlist.txt", "r"); fp = fopen("gwlist.txt", "r");
if (!fp) { if (!fp) {
INFO_PRINT("open gatewaylist.txt failed! \n"); INFO_PRINT("open gatewaylist.txt failed! \n");
goto error1; goto error1;
} }
char buf[1024] = {0};
retLen = fread(g_tcp_ctrl, readLen, 1, fp); retLen = fread(g_tcp_ctrl, readLen, 1, fp);
INFO_PRINT("read gatewaylist.txt retLen= %d, readLen=%d ! \n", retLen,readLen); INFO_PRINT("read gatewaylist.txt retLen= %d, readLen=%d ! \n", retLen,readLen);
if (retLen != readLen ){ if (retLen != readLen ){
...@@ -1012,7 +1008,7 @@ int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb) ...@@ -1012,7 +1008,7 @@ int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb)
g_client_ctrl.cb = cb; g_client_ctrl.cb = cb;
} }
kk_tcp_client_deinit(){ void kk_tcp_client_deinit(void){
if (g_client_ctrl.sd > -1){ if (g_client_ctrl.sd > -1){
close(g_client_ctrl.sd); close(g_client_ctrl.sd);
g_client_ctrl.sd = -1; g_client_ctrl.sd = -1;
......
...@@ -267,39 +267,26 @@ int HAL_SetDevice_Code(_IN_ char *device_code) ...@@ -267,39 +267,26 @@ int HAL_SetDevice_Code(_IN_ char *device_code)
return len; return len;
} }
int HAL_GetProduct_Type(_OU_ char *product_type)
{
int len = strlen(g_product_type);
memset(product_type, 0x0, PRODUCT_TYPE_LEN);
strncpy(product_type, g_product_type, len);
return len;
}
int HAL_GetProduct_Code(_OU_ char *product_code) int HAL_GetProduct_Code(_OU_ char *product_code)
{ {
int len = strlen(g_product_code);
memset(product_code, 0x0, PRODUCT_CODE_LEN);
//strncpy(product_code, g_product_code, len);
strncpy(product_code, KK_CCU_PRODUCTID, strlen(KK_CCU_PRODUCTID)); strncpy(product_code, KK_CCU_PRODUCTID, strlen(KK_CCU_PRODUCTID));
return len; product_code[strlen(KK_CCU_PRODUCTID)] = '\0';
return strlen(KK_CCU_PRODUCTID);
} }
int HAL_GetDevice_Code(_OU_ char *device_code) int HAL_GetDevice_Code(_OU_ char *device_code)
{ {
int len = strlen(g_device_code);
memset(device_code, 0x0, DEVICE_CODE_LEN);
//strncpy(device_code, g_device_code, len);
strncpy(device_code, KK_CCU_ID, strlen(KK_CCU_ID)); strncpy(device_code, KK_CCU_ID, strlen(KK_CCU_ID));
return len; device_code[strlen(KK_CCU_ID)] = '\0';
return strlen(KK_CCU_ID);
} }
int HAL_GetVersion(_OU_ char *version) int HAL_GetVersion(_OU_ char *version)
{ {
int len = strlen(KK_CCU_VERSION); strncpy(version, KK_CCU_VERSION, strlen(KK_CCU_VERSION));
memset(version, 0x0, VERSION_MAXLEN); version[strlen(KK_CCU_VERSION)] = '\0';
strncpy(version, KK_CCU_VERSION, len); return strlen(KK_CCU_VERSION);
version[len] = '\0';
return strlen(version);
} }
......
...@@ -35,6 +35,8 @@ ...@@ -35,6 +35,8 @@
#define KK_CCU_PRODUCTID "ccu_n12" #define KK_CCU_PRODUCTID "ccu_n12"
#define KK_GW_PRODUCTID "gateway_2" #define KK_GW_PRODUCTID "gateway_2"
#define KK_CCU_RANDOM "0000000000" #define KK_CCU_RANDOM "0000000000"
#define KK_CCU_NAME "NEW_CCU"
#define KK_DEVICE_MAP_FILE_PATH "/usr/kk/map/device_%s.json"
enum { enum {
DEVICE_OFFLINE = 0, DEVICE_OFFLINE = 0,
DEVICE_ONLINE, DEVICE_ONLINE,
...@@ -44,7 +46,6 @@ enum { ...@@ -44,7 +46,6 @@ enum {
int HAL_SetProduct_Type(_IN_ char *product_type); int HAL_SetProduct_Type(_IN_ char *product_type);
int HAL_SetProduct_Code(_IN_ char *product_code); int HAL_SetProduct_Code(_IN_ char *product_code);
int HAL_SetDevice_Code(_IN_ char *device_code); int HAL_SetDevice_Code(_IN_ char *device_code);
int HAL_GetProduct_Type(_OU_ char *product_type);
int HAL_GetProduct_Code(_OU_ char *product_code); int HAL_GetProduct_Code(_OU_ char *product_code);
int HAL_GetDevice_Code(_OU_ char *device_code); int HAL_GetDevice_Code(_OU_ char *device_code);
......
/**
* @file cson.c
* @author Letter (NevermindZZT@gmail.com)
* @brief cson
* @version 0.1
* @date 2019-08-26
*
* @copyright (c) 2019 Letter
*
*/
#include "cson.h"
#include "cJSON.h"
#include "stddef.h"
#include "string.h"
#include "stdio.h"
#include <stdlib.h>
/**
* @brief 基本类型链表数据模型
*
*/
CsonModel csonBasicListModel[] =
{
[0] = {CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(char)},
[1] = {CSON_TYPE_CHAR, NULL, 0},
[2] = {CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(short)},
[3] = {CSON_TYPE_SHORT, NULL, 0},
[4] = {CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(int)},
[5] = {CSON_TYPE_INT, NULL, 0},
[6] = {CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(long)},
[7] = {CSON_TYPE_LONG, NULL, 0},
[8] = {CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(float)},
[9] = {CSON_TYPE_FLOAT, NULL, 0},
[10] = {CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(double)},
[11] = {CSON_TYPE_DOUBLE, NULL, 0},
[12] = {CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(char *)},
[13] = {CSON_TYPE_STRING, NULL, 0},
};
/**
* @brief cson
*
*/
struct {
void *(*malloc)(int);
void (*free)(void *);
} cson;
/**
* @brief CSON初始化
*
* @param malloc 内存分配函数
* @param free 内存释放函数
*/
void *cson_malloc(int size)
{
return malloc(size);
}
void *cson_free(void*data)
{
free(data);
}
void csonInit(void *os_malloc, void *os_free)
{
if(os_malloc == NULL || os_free == NULL)
{
cson.malloc = (void *(*)(int))cson_malloc;
cson.free = (void (*)(void *))cson_free;
}
else
{
cson.malloc = (void *(*)(int))os_malloc;
cson.free = (void (*)(void *))os_free;
}
cJSON_InitHooks(&(cJSON_Hooks){(void *(*)(size_t))cson.malloc, cson.free});
}
/**
* @brief 解析JSON整型
*
* @param json JSON对象
* @param key key
* @return int 解析出的整型数
*/
int csonDecodeNumber(cJSON *json, char *key)
{
cJSON *item = key ? cJSON_GetObjectItem(json, key) : json;
if (item && item->type == cJSON_Number)
{
return item->valueint;
}
return 0;
}
/**
* @brief 解析JSON浮点型
*
* @param json JSON对象
* @param key key
* @return double 解析出的浮点型数
*/
double csonDecodeDouble(cJSON *json, char *key)
{
cJSON *item = key ? cJSON_GetObjectItem(json, key) : json;
if (item && item->type == cJSON_Number)
{
return item->valuedouble;
}
return 0.0;
}
/**
* @brief 解析JSON字符串数据
*
* @param json JSON对象
* @param key key
* @return char* 解析出的字符串
*/
char* csonDecodeString(cJSON *json, char *key)
{
char *p = NULL;
char *str = NULL;
short strLen = 0;
cJSON *item = key ? cJSON_GetObjectItem(json, key) : json;
if (item && item->type == cJSON_String)
{
str = item->valuestring;
if (item->valuestring)
{
strLen = strlen(str);
p = cson.malloc(strLen + 1);
if (p)
{
memcpy((void *)p, (void *)str, strLen);
*(p + strLen) = 0;
return p;
}
}
}
return NULL;
}
/**
* @brief 解析JOSN布尔型数据
*
* @param json JSON对象
* @param key key
* @return char 解析出的bool
*/
char csonDecodeBool(cJSON *json, char *key)
{
cJSON *item = cJSON_GetObjectItem(json, key);
if (item && item->type == cJSON_True)
{
return 1;
}
return 0;
}
/**
* @brief 解析CsonList数据
*
* @param json JSON对象
* @param key key
* @param model CsonList成员数据模型
* @param modelSize SconList成员模型数量
* @return void* CsonList对象
*/
void *csonDecodeList(cJSON *json, char *key, CsonModel *model, int modelSize)
{
CsonList *list = NULL;
cJSON *array = cJSON_GetObjectItem(json, key);
if (array && array->type == cJSON_Array)
{
for (short i = 0; i < cJSON_GetArraySize(array); i++)
{
list = csonListAdd(list, csonDecodeObject(cJSON_GetArrayItem(array, i), model, modelSize));
}
}
return list;
}
/**
* @brief 解析数组
*
* @param json json对象
* @param key key
* @param base 数组基址
* @param elementType 数组元素类型
* @param arraySize 数组大小
*/
void csonDecodeArray(cJSON *json, char *key, void * base, CsonType elementType, short arraySize)
{
cJSON *array = cJSON_GetObjectItem(json, key);
cJSON *item;
char *str;
short strLen;
if (array && array->type == cJSON_Array)
{
for (short i = 0; i < cJSON_GetArraySize(array); i++)
{
item = cJSON_GetArrayItem(array, i);
switch (elementType)
{
case CSON_TYPE_CHAR:
*(char *)((int)base + (i * sizeof(char))) = (char)item->valueint;
break;
case CSON_TYPE_SHORT:
*(short *)((int)base + (i * sizeof(short))) = (short)item->valueint;
break;
case CSON_TYPE_INT:
*(int *)((int)base + (i * sizeof(int))) = (int)item->valueint;
break;
case CSON_TYPE_LONG:
*(long *)((int)base + (i * sizeof(long))) = (long)item->valueint;
break;
case CSON_TYPE_FLOAT:
*(float *)((int)base + (i * sizeof(float))) = (float)item->valuedouble;
break;
case CSON_TYPE_DOUBLE:
*(double *)((int)base + (i * sizeof(double))) = (double)item->valuedouble;
break;
case CSON_TYPE_STRING:
strLen = strlen(item->valuestring);
str = cson.malloc(strLen + 1);
memcpy(str, item->valuestring, strLen);
*(str + strLen) = 0;
*(int *)((int)base + (i * sizeof(int))) = (int)str;
break;
default:
break;
}
}
}
}
/**
* @brief 解析JSON对象
*
* @param json JSON对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @return void* 解析得到的对象
*/
void *csonDecodeObject(cJSON *json, CsonModel *model, int modelSize)
{
CSON_ASSERT(json, return NULL);
if (json->type == cJSON_NULL) {
return NULL;
}
short objSize = 0;
for (short i = 0; i < modelSize; i++)
{
if (model[i].type == CSON_TYPE_OBJ)
{
objSize = model[i].param.objSize;
}
}
void *obj = cson.malloc(objSize);
CSON_ASSERT(obj, return NULL);
for (short i = 0; i < modelSize; i++)
{
switch (model[i].type)
{
case CSON_TYPE_CHAR:
*(char *)((int)obj + model[i].offset) = (char)csonDecodeNumber(json, model[i].key);
break;
case CSON_TYPE_SHORT:
*(short *)((int)obj + model[i].offset) = (short)csonDecodeNumber(json, model[i].key);
break;
case CSON_TYPE_INT:
*(int *)((int)obj + model[i].offset) = (int)csonDecodeNumber(json, model[i].key);
break;
case CSON_TYPE_LONG:
*(long *)((int)obj + model[i].offset) = (long)csonDecodeNumber(json, model[i].key);
break;
case CSON_TYPE_FLOAT:
*(float *)((int)obj + model[i].offset) = (float)csonDecodeDouble(json, model[i].key);
break;
case CSON_TYPE_DOUBLE:
*(double *)((int)obj + model[i].offset) = csonDecodeDouble(json, model[i].key);
break;
case CSON_TYPE_BOOL:
*(char *)((int)obj + model[i].offset) = (char)csonDecodeBool(json, model[i].key);
break;
case CSON_TYPE_STRING:
*(int *)((int)obj + model[i].offset) = (int)csonDecodeString(json, model[i].key);
break;
case CSON_TYPE_LIST:
*(int *)((int)obj + model[i].offset) = (int)csonDecodeList(json,
model[i].key, model[i].param.sub.model, model[i].param.sub.size);
break;
case CSON_TYPE_STRUCT:
*(int *)((int)obj + model[i].offset) = (int)csonDecodeObject(
cJSON_GetObjectItem(json, model[i].key),
model[i].param.sub.model, model[i].param.sub.size);
break;
case CSON_TYPE_ARRAY:
csonDecodeArray(json, model[i].key, (void *)((int)obj + model[i].offset),
model[i].param.array.eleType, model[i].param.array.size);
break;
case CSON_TYPE_JSON:
*(int *)((int)obj + model[i].offset) = (int)cJSON_PrintUnformatted(
cJSON_GetObjectItem(json, model[i].key));
break;
default:
break;
}
}
return obj;
}
/**
* @brief 解析JSON字符串
*
* @param jsonStr json字符串
* @param model 数据模型
* @param modelSize 数据模型数量
* @return void* 解析得到的对象
*/
void *csonDecode(const char *jsonStr, CsonModel *model, int modelSize)
{
void *obj;
cJSON *json = cJSON_Parse(jsonStr);
CSON_ASSERT(json, return NULL);
obj = csonDecodeObject(json, model, modelSize);
cJSON_Delete(json);
return obj;
}
/**
* @brief 数字编码编码JSON
*
* @param json json对象
* @param key key
* @param num 数值
*/
void csonEncodeNumber(cJSON *json, char *key, double num)
{
if (key)
{
cJSON_AddNumberToObject(json, key, num);
}
else
{
json->type = cJSON_Number;
json->valuedouble = num;
json->valueint = (int)num;
}
}
/**
* @brief 字符串编码编码JSON
*
* @param json json对象
* @param key key
* @param str 字符串
*/
void csonEncodeString(cJSON *json, char *key, char *str)
{
if (key)
{
cJSON_AddStringToObject(json, key, str);
}
else
{
json->type = cJSON_String;
json->valuestring = str;
}
}
/**
* @brief CsonList编码成JSON对象
*
* @param list CsonList对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @return cJSON* 编码得到的JOSN对象
*/
cJSON* csonEncodeList(CsonList *list, CsonModel *model, int modelSize)
{
cJSON *root = cJSON_CreateArray();
cJSON *item;
CsonList *p = list;
while (p)
{
if (p->obj)
{
item = csonEncodeObject(p->obj, model, modelSize);
cJSON_AddItemToArray(root, item);
}
p = p->next;
}
return root;
}
/**
* @brief 数组编码成JSON对象
*
* @param base 数组基址
* @param elementType 数组元素类型
* @param arraySize 数组大小
* @return cJSON* 编码得到的JOSN对象
*/
cJSON* csonEncodeArray(void *base, CsonType elementType, short arraySize)
{
cJSON *root = cJSON_CreateArray();
cJSON *item;
for (short i = 0; i < arraySize; i++)
{
switch (elementType)
{
case CSON_TYPE_CHAR:
item = cJSON_CreateNumber(*(char *)((int)base + (i * sizeof(char))));
break;
case CSON_TYPE_SHORT:
item = cJSON_CreateNumber(*(short *)((int)base + (i * sizeof(short))));
break;
case CSON_TYPE_INT:
item = cJSON_CreateNumber(*(int *)((int)base + (i * sizeof(int))));
break;
case CSON_TYPE_LONG:
item = cJSON_CreateNumber(*(long *)((int)base + (i * sizeof(int))));
break;
case CSON_TYPE_FLOAT:
item = cJSON_CreateNumber(*(float *)((int)base + (i * sizeof(float))));
break;
case CSON_TYPE_DOUBLE:
item = cJSON_CreateNumber(*(double *)((int)base + (i * sizeof(double))));
break;
case CSON_TYPE_STRING:
item = cJSON_CreateString((char *)*(int *)((int)base + (i * sizeof(int))));
break;
default:
break;
}
cJSON_AddItemToArray(root, item);
}
return root;
}
/**
* @brief 编码JSON对象
*
* @param obj 对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @return cJSON* 编码得到的json对象
*/
cJSON* csonEncodeObject(void *obj, CsonModel *model, int modelSize)
{
if (!obj) {
return cJSON_CreateNull();
}
cJSON *root = cJSON_CreateObject();
for (short i = 0; i < modelSize; i++)
{
switch (model[i].type)
{
case CSON_TYPE_CHAR:
csonEncodeNumber(root, model[i].key, *(char *)((int)obj + model[i].offset));
break;
case CSON_TYPE_SHORT:
csonEncodeNumber(root, model[i].key, *(short *)((int)obj + model[i].offset));
break;
case CSON_TYPE_INT:
csonEncodeNumber(root, model[i].key, *(int *)((int)obj + model[i].offset));
break;
case CSON_TYPE_LONG:
csonEncodeNumber(root, model[i].key, *(long *)((int)obj + model[i].offset));
break;
case CSON_TYPE_FLOAT:
csonEncodeNumber(root, model[i].key, *(float *)((int)obj + model[i].offset));
break;
case CSON_TYPE_DOUBLE:
csonEncodeNumber(root, model[i].key, *(double *)((int)obj + model[i].offset));
break;
case CSON_TYPE_BOOL:
cJSON_AddBoolToObject(root, model[i].key, *(char *)((int)obj + model[i].offset));
break;
case CSON_TYPE_STRING:
if ((char *)(*(int *)((int)obj + model[i].offset)))
{
csonEncodeString(root, model[i].key, (char *)(*(int *)((int)obj + model[i].offset)));
}
break;
case CSON_TYPE_LIST:
if ((CsonList *)*(int *)((int)obj + model[i].offset))
{
cJSON_AddItemToObject(root, model[i].key,
csonEncodeList((CsonList *)*(int *)((int)obj + model[i].offset),
model[i].param.sub.model, model[i].param.sub.size));
}
break;
case CSON_TYPE_STRUCT:
if ((void *)(*(int *)((int)obj + model[i].offset)))
{
cJSON_AddItemToObject(root, model[i].key, csonEncodeObject(
(void *)(*(int *)((int)obj + model[i].offset)),
model[i].param.sub.model, model[i].param.sub.size));
}
break;
case CSON_TYPE_ARRAY:
cJSON_AddItemToObject(root, model[i].key, csonEncodeArray(
(void *)((int)obj + model[i].offset),
model[i].param.array.eleType, model[i].param.array.size));
break;
case CSON_TYPE_JSON:
if ((char *)(*(int *)((int)obj + model[i].offset)))
{
cJSON_AddItemToObject(root, model[i].key,
cJSON_Parse((char *)(*(int *)((int)obj + model[i].offset))));
}
break;
default:
break;
}
}
return root;
}
/**
* @brief 编码成json字符串
*
* @param obj 对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @param bufferSize 分配给json字符串的空间大小
* @param fmt 是否格式化json字符串
* @return char* 编码得到的json字符串
*/
char* csonEncode(void *obj, CsonModel *model, int modelSize, int bufferSize, int fmt)
{
cJSON *json = csonEncodeObject(obj, model, modelSize);
CSON_ASSERT(json, return NULL);
char *jsonStr = cJSON_PrintBuffered(json, bufferSize, fmt);
cJSON_Delete(json);
return jsonStr;
}
/**
* @brief 编码成json字符串
*
* @param obj 对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @return char* 编码得到的json字符串
*/
char* csonEncodeUnformatted(void *obj, CsonModel *model, int modelSize)
{
cJSON *json = csonEncodeObject(obj, model, modelSize);
CSON_ASSERT(json, return NULL);
char *jsonStr = cJSON_PrintUnformatted(json);
cJSON_Delete(json);
return jsonStr;
}
/**
* @brief 释放CSON解析出的对象
*
* @param obj 对象
* @param model 对象模型
* @param modelSize 对象模型数量
*/
void csonFree(void *obj, CsonModel *model, int modelSize)
{
CsonList *list, *p;
for (short i = 0; i < modelSize; i++)
{
switch ((int)model[i].type)
{
case CSON_TYPE_CHAR:
case CSON_TYPE_SHORT:
case CSON_TYPE_INT:
case CSON_TYPE_LONG:
case CSON_TYPE_FLOAT:
case CSON_TYPE_DOUBLE:
break;
case CSON_TYPE_STRING:
case CSON_TYPE_JSON:
cson.free((char *)(*(int *)((int)obj + model[i].offset)));
break;
case CSON_TYPE_LIST:
list = (CsonList *)*(int *)((int)obj + model[i].offset);
while (list)
{
p = list;
list = list->next;
if (p->obj)
{
csonFree(p->obj,
model[i].param.sub.model, model[i].param.sub.size);
}
cson.free(p);
}
break;
case CSON_TYPE_STRUCT:
csonFree((void *)(*(int *)((int)obj + model[i].offset)),
model[i].param.sub.model, model[i].param.sub.size);
break;
case CSON_TYPE_ARRAY:
if (model[i].param.array.eleType == CSON_TYPE_STRING)
{
for (short j = 0; j< model[i].param.array.size; j++)
{
if (*(int *)((int)obj + model[i].offset + (j << 2)))
{
cson.free((void *)*(int *)((int)obj + model[i].offset + (j << 2)));
}
}
}
break;
default:
break;
}
}
cson.free(obj);
}
/**
* @brief 释放cson编码生成的json字符串
*
* @param jsonStr json字符串
*/
void csonFreeJson(const char *jsonStr)
{
CSON_ASSERT(jsonStr, return);
cson.free((void *)jsonStr);
}
/**
* @brief CSON链表添加节点
*
* @param list 链表
* @param obj 节点对象
* @return CsonList 链表
*/
CsonList* csonListAdd(CsonList *list, void *obj)
{
if (!list)
{
list = cson.malloc(sizeof(CsonList));
if (!list)
{
return NULL;
}
list->next = NULL;
list->obj = NULL;
}
CsonList *p = list;
while (p->next)
{
p = p->next;
}
if (!p->obj)
{
p->obj = obj;
p->next = NULL;
}
else
{
CsonList *node = cson.malloc(sizeof(CsonList));
if (node)
{
node->obj = obj;
node->next = NULL;
p->next = node;
}
}
return list;
}
/**
* @brief CSON链表删除节点
*
* @param list 链表
* @param obj 节点对象
* @param freeMem 释放内存
* @return CsonList 链表
*/
CsonList *csonListDelete(CsonList *list, void *obj, char freeMem)
{
CSON_ASSERT(list, return NULL);
CsonList head = {0};
head.next = list;
CsonList *p = &head;
CsonList *tmp;
while (p->next)
{
if (p->next->obj && p->next->obj == obj)
{
tmp = p->next;
p->next = p->next->next ? p->next->next : NULL;
if (freeMem)
{
cson.free(tmp->obj);
cson.free(tmp);
}
break;
}
p = p->next;
}
return head.next;
}
/**
* @brief CSON新字符串
*
* @param src 源字符串
* @return char* 新字符串
* @note 此函数用于复制字符串,建议对结构体中字符串成员赋值时,使用此函数,
* 方便使用`csonFree`进行内存释放
*/
char* csonNewString(const char *src)
{
int len = strlen(src);
char *dest = cson.malloc(len + 1);
strcpy(dest, src);
return dest;
}
/**
* @file cson.h
* @author Letter (NevermindZZT@gmail.com)
* @brief cson
* @version 0.1
* @date 2019-08-26
*
* @copyright (c) 2019 Letter
*
*/
#ifndef __CSON_H__
#define __CSON_H__
#include "stddef.h"
#include "cJSON.h"
#define CSON_VERSION "1.0.3" /**< CSON版本 */
/**
* @defgroup CSON cson
* @brief json tools for C
* @addtogroup CSON
* @{
*/
/**
* @brief CSON数据类型定义
*
*/
typedef enum
{
CSON_TYPE_OBJ = 0,
CSON_TYPE_CHAR,
CSON_TYPE_SHORT,
CSON_TYPE_INT,
CSON_TYPE_LONG,
CSON_TYPE_FLOAT,
CSON_TYPE_DOUBLE,
CSON_TYPE_BOOL,
CSON_TYPE_STRING,
CSON_TYPE_STRUCT,
CSON_TYPE_LIST,
CSON_TYPE_ARRAY,
CSON_TYPE_JSON,
} CsonType;
/**
* @brief CSON数据模型定义
*
*/
typedef struct cson_model
{
CsonType type; /**< 数据类型 */
char *key; /**< 元素键值 */
short offset; /**< 元素偏移 */
union
{
struct
{
struct cson_model *model; /**< 子结构体模型 */
short size; /**< 子结构体模型大小 */
} sub; /**< 子结构体 */
struct
{
CsonType eleType; /**< 数组元素类型 */
short size; /**< 数组大小 */
} array; /**< 数组 */
int objSize; /**< 对象大小 */
CsonType basicListType; /**< 基础数据链表类型 */
} param;
} CsonModel;
/**
* @brief Cson链表
*
*/
typedef struct cson_list
{
struct cson_list *next; /**< 下一个元素 */
void *obj; /**< 对象 */
} CsonList;
extern CsonModel csonBasicListModel[]; /**< 基础类型链表数据模型 */
#define CSON_MODEL_CHAR_LIST &csonBasicListModel[0] /**< char型链表数据模型 */
#define CSON_MODEL_SHORT_LIST &csonBasicListModel[2] /**< short型链表数据模型 */
#define CSON_MODEL_INT_LIST &csonBasicListModel[4] /**< int型链表数据模型 */
#define CSON_MODEL_LONG_LIST &csonBasicListModel[6] /**< long型链表数据模型 */
#define CSON_MODEL_FLOAT_LIST &csonBasicListModel[8] /**< float型链表数据模型 */
#define CSON_MODEL_DOUBLE_LIST &csonBasicListModel[10] /**< double型链表数据模型 */
#define CSON_MODEL_STRING_LIST &csonBasicListModel[12] /**< string型链表数据模型 */
#define CSON_BASIC_LIST_MODEL_SIZE 2 /**< 基础类型链表数据模型大小 */
/**
* @brief 对象数据模型
*
* @param type 对象类型
*/
#define CSON_MODEL_OBJ(type) \
{CSON_TYPE_OBJ, NULL, 0, .param.objSize=sizeof(type)}
/**
* @brief char型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_CHAR(type, key) \
{CSON_TYPE_CHAR, #key, offsetof(type, key)}
/**
* @brief short型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_SHORT(type, key) \
{CSON_TYPE_SHORT, #key, offsetof(type, key)}
/**
* @brief int型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_INT(type, key) \
{CSON_TYPE_INT, #key, offsetof(type, key)}
/**
* @brief long型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_LONG(type, key) \
{CSON_TYPE_LONG, #key, offsetof(type, key)}
/**
* @brief float型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_FLOAT(type, key) \
{CSON_TYPE_FLOAT, #key, offsetof(type, key)}
/**
* @brief double型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_DOUBLE(type, key) \
{CSON_TYPE_DOUBLE, #key, offsetof(type, key)}
/**
* @brief bool型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_BOOL(type, key) \
{CSON_TYPE_BOOL, #key, offsetof(type, key)}
/**
* @brief 字符串型数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_STRING(type, key) \
{CSON_TYPE_STRING, #key, offsetof(type, key)}
/**
* @brief 结构体型数据模型
*
* @param type 对象模型
* @param key 数据键值
* @param submodel 子结构体模型
* @param subsize 子结构体模型大小
*/
#define CSON_MODEL_STRUCT(type, key, submodel, subsize) \
{CSON_TYPE_STRUCT, #key, offsetof(type, key), .param.sub.model=submodel, .param.sub.size=subsize}
/**
* @brief list型数据模型
*
* @param type 对象模型
* @param key 数据键值
* @param submodel 子结构体模型
* @param subsize 子结构体模型大小
*/
#define CSON_MODEL_LIST(type, key, submodel, subsize) \
{CSON_TYPE_LIST, #key, offsetof(type, key), .param.sub.model=submodel, .param.sub.size=subsize}
/**
* @brief list型数据模型
*
* @param type 对象模型
* @param key 数据键值
* @param elementType 数组元素类型
* @param arraySize 数组大小
*/
#define CSON_MODEL_ARRAY(type, key, elementType, arraySize) \
{CSON_TYPE_ARRAY, #key, offsetof(type, key), .param.array.eleType=elementType, .param.array.size=arraySize}
/**
* @brief 子json数据模型
*
* @param type 对象模型
* @param key 数据键值
*/
#define CSON_MODEL_JSON(type, key) \
{CSON_TYPE_JSON, #key, offsetof(type, key)}
/**
* @brief CSON断言
*
* @param expr 表达式
* @param action 断言失败执行动作
*/
#define CSON_ASSERT(expr, action) \
if (!(expr)) { \
printf(#expr " assert failed at file: %s, line: %d\r\n", __FILE__, __LINE__); \
action; \
}
/**
* @brief CSON初始化
*
* @param malloc 内存分配函数
* @param free 内存释放函数
*/
void csonInit(void *malloc, void *free);
/**
* @brief 解析JSON对象
*
* @param json JSON对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @return void* 解析得到的对象
*/
void *csonDecodeObject(cJSON *json, CsonModel *model, int modelSize);
/**
* @brief 解析JSON对象
*
* @param json JSON对象
* @param model 数据模型
* @return void* 解析得到的对象
*/
#define csonDecodeObjectEx(json, model) \
csonDecodeObject(json, model, sizeof(model) / sizeof(CsonModel))
/**
* @brief 解析JSON字符串
*
* @param jsonStr json字符串
* @param model 数据模型
* @param modelSize 数据模型数量
* @return void* 解析得到的对象
*/
void *csonDecode(const char *jsonStr, CsonModel *model, int modelSize);
/**
* @brief 解析JSON字符串
*
* @param jsonStr json字符串
* @param model 数据模型
* @return void* 解析得到的对象
*/
#define csonDecodeEx(jsonStr, model) \
csonDecode(jsonStr, model, sizeof(model) / sizeof(CsonModel));
/**
* @brief 编码成json字符串
*
* @param obj 对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @return char* 编码得到的json字符串
*/
cJSON* csonEncodeObject(void *obj, CsonModel *model, int modelSize);
/**
* @brief 编码成json字符串
*
* @param obj 对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @param bufferSize 分配给json字符串的空间大小
* @param fmt 是否格式化json字符串
* @return char* 编码得到的json字符串
*/
char* csonEncode(void *obj, CsonModel *model, int modelSize, int bufferSize, int fmt);
/**
* @brief 编码成json字符串
*
* @param obj 对象
* @param model 数据模型
* @param bufferSize 分配给json字符串的空间大小
* @param fmt 是否格式化json字符串
* @return char* 编码得到的json字符串
*/
#define csonEncodeEx(obj, model, bufferSize, fmt) \
csonEncode(obj, model, sizeof(model)/sizeof(CsonModel), bufferSize, fmt)
/**
* @brief 编码成json字符串
*
* @param obj 对象
* @param model 数据模型
* @param modelSize 数据模型数量
* @return char* 编码得到的json字符串
*/
char* csonEncodeUnformatted(void *obj, CsonModel *model, int modelSize);
/**
* @brief 编码成json字符串
*
* @param obj 对象
* @param model 数据模型
* @return char* 编码得到的json字符串
*/
#define csonEncodeUnformattedEx(obj, model) \
csonEncodeUnformatted(obj, model, sizeof(model) / sizeof(CsonModel))
/**
* @brief 释放CSON解析出的对象
*
* @param obj 对象
* @param model 对象模型
* @param modelSize 对象模型数量
*/
void csonFree(void *obj, CsonModel *model, int modelSize);
/**
* @brief 释放CSON解析出的对象
*
* @param obj 对象
* @param model 对象模型
*/
#define csonFreeEx(obj, model) \
csonFree(obj, model, sizeof(model) / sizeof(CsonModel))
/**
* @brief 释放cson编码生成的json字符串
*
* @param jsonStr json字符串
*/
void csonFreeJson(const char *jsonStr);
/**
* @brief CSON链表添加节点
*
* @param list 链表
* @param obj 节点对象
* @return CsonList 链表
*/
CsonList* csonListAdd(CsonList *list, void *obj);
/**
* @brief CSON链表删除节点
*
* @param list 链表
* @param obj 节点对象
* @param freeMem 释放内存
* @return CsonList 链表
*/
CsonList *csonListDelete(CsonList *list, void *obj, char freeMem);
/**
* @brief CSON新字符串
*
* @param src 源字符串
* @return char* 新字符串
* @note 此函数用于复制字符串,建议对结构体中字符串成员赋值时,使用此函数,
* 方便使用`csonFree`进行内存释放
*/
char* csonNewString(const char *src);
/**
* @}
*/
#endif
...@@ -14,6 +14,7 @@ COMP_LIB_COMPONENTS := \ ...@@ -14,6 +14,7 @@ COMP_LIB_COMPONENTS := \
$(call Append_Conditional, SUBDIRS, application/kcloud, KCLOUD_PLATFORM_SUPPORT) $(call Append_Conditional, SUBDIRS, application/kcloud, KCLOUD_PLATFORM_SUPPORT)
$(call Append_Conditional, SUBDIRS, application/kk_luoma, LUOMA_PLATFORM_SUPPORT) $(call Append_Conditional, SUBDIRS, application/kk_luoma, LUOMA_PLATFORM_SUPPORT)
SUBDIRS += application/oled SUBDIRS += application/oled
SUBDIRS += application/klansdk
SUBDIRS += midware/midware SUBDIRS += midware/midware
SUBDIRS += common/mqtt SUBDIRS += common/mqtt
SUBDIRS += common/json SUBDIRS += common/json
......
...@@ -20,4 +20,5 @@ int dm_ota_sub(void); ...@@ -20,4 +20,5 @@ int dm_ota_sub(void);
int dm_ota_deinit(void); int dm_ota_deinit(void);
int dm_ota_get_ota_handle(void **handle); int dm_ota_get_ota_handle(void **handle);
int dm_ota_switch_device(int devid); int dm_ota_switch_device(int devid);
int dm_ota_yield(int timeout_ms);
#endif #endif
...@@ -117,11 +117,9 @@ int iotx_dm_ccu_cloud_check(void) ...@@ -117,11 +117,9 @@ int iotx_dm_ccu_cloud_check(void)
int iotx_dm_dev_online(_IN_ int devid) int iotx_dm_dev_online(_IN_ int devid)
{ {
int res = 0; int res = 0;
if (devid < 0) { if (devid < 0) {
return INVALID_PARAMETER; return INVALID_PARAMETER;
} }
_dm_api_lock(); _dm_api_lock();
res = dm_mgr_upstream_status_online(devid); res = dm_mgr_upstream_status_online(devid);
......
...@@ -25,5 +25,22 @@ typedef struct { ...@@ -25,5 +25,22 @@ typedef struct {
lite_cjson_item_t *lite; lite_cjson_item_t *lite;
} dm_api_property_t; } dm_api_property_t;
#endif #endif
int iotx_dm_ccu_cloud_check(void);
int iotx_dm_subdev_topo_del(_IN_ int devid);
int kk_dm_subdev_register(_IN_ int devid);
int kk_dm_subdev_unregister(_IN_ int devid);
int kk_dm_subdev_topo_add(_IN_ int devid);
int iotx_dm_subdev_topo_del(_IN_ int devid);
int iotx_dm_ccu_cloud_check(void);
int iotx_dm_dev_online(_IN_ int devid);
int iotx_dm_dev_offline(_IN_ int devid);
int iotx_dm_subdev_login(_IN_ int devid);
int iotx_dm_subdev_logout(_IN_ int devid);
void iotx_dm_dispatch(void);
int iotx_dm_connect(_IN_ iotx_dm_event_callback cb);
int iotx_dm_open(void);
int iotx_dm_subscribe(_IN_ int devid);
void kk_dm_ota_report_version(_IN_ int devid, char *version);
void kk_dm_ota_send(void *data, int len);
int kk_dm_get_property_type(const char* deviceCode,const char *key);
#endif #endif
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "kk_tsl_common.h" #include "kk_tsl_common.h"
#include "kk_dm_mng.h" #include "kk_dm_mng.h"
/************************************************************* /*************************************************************
全局变量定义 全局变量定义
*************************************************************/ *************************************************************/
......
#ifndef _KK_DM_HEARTBEAT_H_
#define _KK_DM_HEARTBEAT_H_
#include "kk_tsl_common.h"
int kk_heartbeat_init(void);
int kk_dm_gw_status_update_online(const char *deviceCode);
#endif
...@@ -270,6 +270,7 @@ int dm_mgr_device_create(_IN_ int dev_type,_IN_ char productCode[PRODUCT_CODE_MA ...@@ -270,6 +270,7 @@ int dm_mgr_device_create(_IN_ int dev_type,_IN_ char productCode[PRODUCT_CODE_MA
if(res != 0){ if(res != 0){
return FAIL_RETURN; return FAIL_RETURN;
} }
printf("--------------------------------------------------------->heartbeat:%d\n",heartbeat);
node->hb_timeout = heartbeat; node->hb_timeout = heartbeat;
} }
else{ else{
...@@ -1349,7 +1350,6 @@ int dm_mgr_upstream_status_offline(_IN_ int devid) ...@@ -1349,7 +1350,6 @@ int dm_mgr_upstream_status_offline(_IN_ int devid)
return res; return res;
} }
const char DM_URI_COMBINE_LOGIN[] = "/thing/combine/login"; const char DM_URI_COMBINE_LOGIN[] = "/thing/combine/login";
int dm_mgr_upstream_combine_login(_IN_ int devid) int dm_mgr_upstream_combine_login(_IN_ int devid)
{ {
......
...@@ -62,5 +62,45 @@ typedef struct { ...@@ -62,5 +62,45 @@ typedef struct {
iotx_dm_error_code_t code; iotx_dm_error_code_t code;
} kk_msg_response_t; } kk_msg_response_t;
int dm_mgr_search_dev_by_devid(_IN_ int devid, _OU_ dm_mgr_dev_node_t **node);
int dm_mgr_properities_db_create(kk_tsl_t* dev_shadow,char *deviceCode,int devType );
int dm_mgr_get_device_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_mgr_dev_node_t **node);
int dm_mgr_device_create(_IN_ int dev_type,_IN_ char productCode[PRODUCT_CODE_MAXLEN], _IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_IN_ char mac[DEVICE_MAC_MAXLEN],_IN_ char fatherDeviceCode[DEVICE_CODE_MAXLEN],_IN_ int isOffline, _OU_ int *devid,_OU_ int *heartbeat);
int dm_mgr_search_device_by_pkdn( _IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ int *devid);
int dm_mgr_get_devicetype_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ int* deviceType);
int dm_mgr_get_device_shadow_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ kk_tsl_t **shadow);
int dm_mgr_get_device_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_mgr_dev_node_t **node);
int dm_mgr_get_device_by_productType(_IN_ char *productType, _OU_ dm_mgr_dev_node_t **node);
int dm_mgr_update_timestamp_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],time_t timestamp);
int dm_mgr_set_dev_onoffline(dm_mgr_dev_node_t *node,int isOffline);
int dm_mgr_check_heartbeat_timeout(time_t timestamp);
int dm_mgr_get_devId_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ int *devid);
int dm_mgr_init(void);
int dm_mgr_deinit(void);
int iotx_report_id(void);
int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len,_IN_ int isAsync);
int dm_mgr_upstream_thing_event_post(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *method,
_IN_ char *payload, _IN_ int payload_len);
int dm_mgr_upstream_thing_event_post_ex(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *method,
_IN_ char *payload, _IN_ int payload_len,const char *msgId);
int dm_mgr_upstream_thing_service_post(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *method,
_IN_ char *payload, _IN_ int payload_len);
int dm_mgr_deprecated_upstream_thing_service_response(_IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
_IN_ char *identifier, _IN_ int identifier_len, _IN_ char *payload, _IN_ int payload_len);
int dm_mgr_upstream_thing_sub_register(_IN_ int devid);
int dm_mgr_upstream_thing_sub_unregister(_IN_ int devid);
int dm_mgr_upstream_thing_topo_add(_IN_ int devid);
int dm_mgr_upstream_thing_topo_delete(_IN_ int devid);
int dm_mgr_upstream_thing_topo_get(void);
int dm_mgr_upstream_thing_list_found(_IN_ int devid);
int dm_mgr_ccu_status_cloud(_IN_ int devid);
int dm_mgr_upstream_status_online(_IN_ int devid);
int dm_mgr_upstream_status_offline(_IN_ int devid);
int dm_mgr_upstream_combine_login(_IN_ int devid);
int dm_mgr_upstream_combine_logout(_IN_ int devid);
int dm_mgr_ota_report_version(_IN_ int devid, char *version);
int dm_mgr_subdev_create(int devtype,_IN_ char productCode[PRODUCT_CODE_MAXLEN], _IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_IN_ char mac[DEVICE_MAC_MAXLEN],_IN_ char fatherDeviceCode[DEVICE_CODE_MAXLEN],_IN_ int isOffline, _OU_ int *devid,_OU_ int *heartbeat);
int dm_mgr_subdev_delete(_IN_ char deviceCode[DEVICE_CODE_MAXLEN]);
#endif #endif
#include "kk_dm_msg.h" #include "kk_dm_msg.h"
#include "kk_dm_mng.h"
#include "kk_tsl_api.h"
#include "kk_dm_queue.h"
#include "lite-cjson.h" #include "lite-cjson.h"
#include "cJSON.h" #include "kk_hal.h"
#include "com_api.h" #include "com_api.h"
#include "kk_log.h" #include "kk_log.h"
...@@ -12,12 +11,13 @@ const char DM_MSG_REQUEST[] DM_READ_ONLY = "{\"msgId\":\"%s\",\"version\":\"%s\" ...@@ -12,12 +11,13 @@ const char DM_MSG_REQUEST[] DM_READ_ONLY = "{\"msgId\":\"%s\",\"version\":\"%s\"
const char DM_MSG_INFO[] DM_READ_ONLY = "{\"msgtype\":\"%s\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"}"; const char DM_MSG_INFO[] DM_READ_ONLY = "{\"msgtype\":\"%s\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"}";
void kk_sendData2app(void *info, void *payload,int isAsync){ void kk_sendData2app(void *info, void *payload,int isAsync){
void *buf = NULL;
cJSON *root=cJSON_CreateObject(); cJSON *root=cJSON_CreateObject();
cJSON* infoObj = cJSON_Parse(info); cJSON* infoObj = cJSON_Parse(info);
cJSON* payloadObj = cJSON_Parse(payload); cJSON* payloadObj = cJSON_Parse(payload);
cJSON_AddItemToObject(root, "info", infoObj); cJSON_AddItemToObject(root, "info", infoObj);
cJSON_AddItemToObject(root, "payload",payloadObj); cJSON_AddItemToObject(root, "payload",payloadObj);
void *buf = cJSON_Print(root); buf = cJSON_Print(root);
if(isAsync){ if(isAsync){
dm_queue_msg_insert4(buf); dm_queue_msg_insert4(buf);
...@@ -706,6 +706,40 @@ static int dm_msg_request_parse(_IN_ char *payload, _IN_ int payload_len, _OU_ k ...@@ -706,6 +706,40 @@ static int dm_msg_request_parse(_IN_ char *payload, _IN_ int payload_len, _OU_ k
return SUCCESS_RETURN; return SUCCESS_RETURN;
} }
int dm_msg_thing_syncinfo_reply(void){
kk_msg_request_payload_t request;
kk_msg_response_t response;
dm_mgr_dev_node_t *node = NULL;
char *info = NULL;
int res = 0, devid = 0;
memset(&request, 0, sizeof(kk_msg_request_payload_t));
memset(&response, 0, sizeof(kk_msg_response_t));
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
res = dm_mgr_search_dev_by_devid(KK_DM_DEVICE_CCU_DEVICEID, &node);
if (res != SUCCESS_RETURN) {
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
//res = dm_msg_request_parse((char *)payload, payload_len, &request);
//if (res < SUCCESS_RETURN) {
//ERROR_PRINT("dm_msg_request_parse failed");
//return res ;
//}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
response.msgTypeStr = KK_THING_SERVICE_SYNCINFO_REPLY;
memcpy(response.productCode, node->productCode, strlen( node->productCode));
memcpy(response.deviceCode, node->deviceCode, strlen(node->deviceCode));
response.code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS) : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
info = kk_sync_get_info();
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
dm_msg_response(&request, &response, info, strlen(info), NULL);
free(info);
return SUCCESS_RETURN;
}
const char DM_URI_THING_SERVICE_PROPERTY_SET_REPLY[] = "/thing/service/property/set_reply"; const char DM_URI_THING_SERVICE_PROPERTY_SET_REPLY[] = "/thing/service/property/set_reply";
int dm_msg_thing_property_set_reply(char deviceCode[DEVICE_CODE_MAXLEN],char *payload, unsigned int payload_len, int dm_msg_thing_property_set_reply(char deviceCode[DEVICE_CODE_MAXLEN],char *payload, unsigned int payload_len,
......
#ifndef __KK_DM_MSG__ #ifndef __KK_DM_MSG__
#define __KK_DM_MSG__ #define __KK_DM_MSG__
#include "kk_tsl_common.h" #include "kk_tsl_common.h"
#include "cJSON.h"
#include "../tsl_handle/lite-cjson.h" #include "../tsl_handle/lite-cjson.h"
#include "kk_dm_mng.h"
#include "kk_tsl_api.h"
#define DM_MSG_KEY_ID "id" #define DM_MSG_KEY_ID "id"
#define DM_MSG_KEY_VERSION "version" #define DM_MSG_KEY_VERSION "version"
...@@ -79,14 +80,55 @@ const char DM_MSG_INFO[] DM_READ_ONLY; ...@@ -79,14 +80,55 @@ const char DM_MSG_INFO[] DM_READ_ONLY;
#define KK_THING_METHOD_UPDATESCENC "thing.service.updateScene" #define KK_THING_METHOD_UPDATESCENC "thing.service.updateScene"
#define KK_THING_METHOD_EXECUTESCENE "thing.service.executeScene" #define KK_THING_METHOD_EXECUTESCENE "thing.service.executeScene"
#define KK_THING_METHOD_ADDSCENC "thing.service.addScene" #define KK_THING_METHOD_ADDSCENC "thing.service.addScene"
#define KK_THING_SERVICE_SYNCINFO "/thing/service/syncinfo"
#define KK_THING_SERVICE_SYNCINFO_REPLY "/thing/service/syncinfo_reply"
#define KK_THING_EVENT_MESSAGE "/thing/event/" #define KK_THING_EVENT_MESSAGE "/thing/event/"
#define KK_THING_EVENT_POST "/post" #define KK_THING_EVENT_POST "/post"
void kk_sendData2app(void *info, void *payload,int isAsync);
int dm_msg_thing_sub_register(_IN_ char productCode[DEVICE_CODE_MAXLEN], _IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_OU_ dm_msg_request_t *request);
int dm_msg_thing_sub_unregister(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ dm_msg_request_t *request);
int dm_msg_thing_topo_add(_IN_ char productCode[PRODUCT_CODE_MAXLEN],
_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _IN_ char mac[DEVICE_MAC_MAXLEN],
_OU_ dm_msg_request_t *request);
int dm_msg_ccu_property_post(dm_mgr_dev_node_t *node);
int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params);
int dm_msg_thing_topo_delete(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ dm_msg_request_t *request);
int dm_msg_thing_topo_get(_OU_ dm_msg_request_t *request);
int dm_msg_thing_list_found(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_OU_ dm_msg_request_t *request);
int dm_msg_status_cloud(_IN_ char productCode[PRODUCT_CODE_MAXLEN],
_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_msg_request_t *request);
int dm_msg_status_online(_IN_ char productCode[PRODUCT_CODE_MAXLEN],
_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_msg_request_t *request);
int dm_msg_status_offline(_IN_ char productCode[PRODUCT_CODE_MAXLEN],
_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_msg_request_t *request);
int dm_msg_combine_login(_IN_ char productCode[PRODUCT_CODE_MAXLEN],
_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_msg_request_t *request);
int dm_msg_combine_logout(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ dm_msg_request_t *request);
int dm_msg_ota_report_version(_IN_ char *version,
_OU_ dm_msg_request_t *request);
int dm_msg_request (_IN_ dm_msg_request_t *request,_IN_ int isAsync);
int dm_msg_response(_IN_ kk_msg_request_payload_t *request, _IN_ kk_msg_response_t *response,
_IN_ char *data, _IN_ int data_len, _IN_ void *user_data);
int dm_msg_response_parse(_IN_ char *payload, _IN_ int payload_len, _OU_ dm_msg_response_payload_t *response);
int dm_msg_thing_property_set_reply(char deviceCode[DEVICE_CODE_MAXLEN],char *payload, unsigned int payload_len,
void *context);
int dm_msg_thing_event_post(const char *deviceCode, const char *identifier,const char *msgId);
int dm_msg_scene_event_post(const char *identifier,kk_tsl_t *dev_shadow,const char *msgId);
int dm_msg_thing_service_post(const char *deviceCode, const char *identifier);
int dm_msg_thing_property_post_all(char *deviceCode);
int dm_msg_ccu_property_post(dm_mgr_dev_node_t *node);
int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params);
int kk_msg_execute_property_set(const char *productCode,const char *deviceCode,const char* params,const char *fatherDeviceCode);
int kk_msg_execute_property_get(dm_mgr_dev_node_t *node);
int kk_msg_parse_productType(char *str,char *productType);
int kk_msg_execute_scene_set(const char* params,const char *fatherDeviceCode,int isUpdate);
int kk_msg_execute_scene_action(const char* params,const char *fatherDeviceCode);
int kk_msg_execute_scene_delete(const char* params,const char *fatherDeviceCode);
int dm_msg_thing_syncinfo_reply(void);
//const char DM_URI_SYS_PREFIX[] DM_READ_ONLY = "/sys/%s/%s/"; //const char DM_URI_SYS_PREFIX[] DM_READ_ONLY = "/sys/%s/%s/";
......
...@@ -42,5 +42,10 @@ int dm_queue_init(int max_size); ...@@ -42,5 +42,10 @@ int dm_queue_init(int max_size);
void dm_queue_deinit(void); void dm_queue_deinit(void);
int dm_queue_msg_insert(void *data); int dm_queue_msg_insert(void *data);
int dm_queue_msg_next(void **data); int dm_queue_msg_next(void **data);
int dm_queue_msg_insert2(void *data);
int dm_queue_msg_next2(void **data);
int dm_queue_msg_insert3(void *data);
int dm_queue_msg_next3(void **data);
int dm_queue_msg_insert4(void *data);
int dm_queue_msg_next4(void **data);
#endif #endif
...@@ -991,49 +991,6 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data) ...@@ -991,49 +991,6 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
kk_service_executeRoom_handle(paramStr); kk_service_executeRoom_handle(paramStr);
} }
#if 0
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_SETLOCALTIMER) == 0){
INFO_PRINT(" setlocaltimer \n");
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
kk_service_setLocalTimer_handle(paramStr,deviceCode->valuestring);
}
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_GETLOCALTIMER) == 0){
INFO_PRINT(" getlocaltimer \n");
kk_service_getLocalTimer_handle(deviceCode->valuestring);
}
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_SETCOUNTDOWN) == 0){
INFO_PRINT(" set count down \n");
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
kk_service_setCountDown_handle(paramStr,deviceCode->valuestring);
}
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_GETCOUNTDOWN) == 0){
INFO_PRINT(" get count down \n");
kk_service_getCountDown_handle(deviceCode->valuestring);
}
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_GETKEYLIST) == 0){
INFO_PRINT(" get key list \n");
kk_service_getLockKeylist_handle(deviceCode->valuestring);
}
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_ADDKEY) == 0){
INFO_PRINT(" add key \n");
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
kk_service_addKey_handle(deviceCode->valuestring,paramStr);
}
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_MODIFYKEY) == 0){
INFO_PRINT(" modify key \n");
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
kk_service_modifyKey_handle(deviceCode->valuestring,paramStr);
}
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_DELETEKEY) == 0){
INFO_PRINT(" delete key \n");
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
kk_service_deleteKey_handle(deviceCode->valuestring,paramStr);
}
#endif
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_ADDSCENC) == 0){ else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_ADDSCENC) == 0){
INFO_PRINT(" add scene \n"); INFO_PRINT(" add scene \n");
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR); cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
...@@ -1082,6 +1039,10 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data) ...@@ -1082,6 +1039,10 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
} }
} }
else if(strcmp(typeJson->valuestring,KK_THING_SERVICE_SYNCINFO) == 0){
INFO_PRINT("SYNCINFO service \n");
dm_msg_thing_syncinfo_reply();
}
else{ else{
INFO_PRINT("Error msgtype!!! \n"); INFO_PRINT("Error msgtype!!! \n");
......
#ifndef _KK_LINKKIT_H_
#define _KK_LINKKIT_H_
#include "kk_tsl_common.h"
int kk_mid_subdev_batch_add( char productCode[PRODUCT_CODE_MAXLEN], char deviceCode[DEVICE_CODE_MAXLEN],char mac[DEVICE_MAC_MAXLEN],char fatherDeviceCode[DEVICE_CODE_MAXLEN]);
int kk_topo_delete_handle(cJSON *payload);
int kk_get_cloudstatus(void);
int kk_get_cloud_recv_status(void);
int kk_init_dmproc(void);
int _iotx_linkkit_slave_connect(int devid);
int _iotx_linkkit_slave_register(int devid);
void IOT_Linkkit_Yield(int timeout_ms);
int IOT_Linkkit_Close(int devid);
int kk_mid_subdev_add(int devType, char productCode[PRODUCT_CODE_MAXLEN], char deviceCode[DEVICE_CODE_MAXLEN],char mac[DEVICE_MAC_MAXLEN],char fatherDeviceCode[DEVICE_CODE_MAXLEN]);
int kk_mid_subdev_batch_add( char productCode[PRODUCT_CODE_MAXLEN], char deviceCode[DEVICE_CODE_MAXLEN],char mac[DEVICE_MAC_MAXLEN],char fatherDeviceCode[DEVICE_CODE_MAXLEN]);
#endif
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include <stdio.h> #include <stdio.h>
#include "kk_tsl_api.h" #include "kk_tsl_api.h"
#include "sqlite3.h"
#include "kk_log.h" #include "kk_log.h"
#include "kk_dm_mng.h" #include "kk_dm_mng.h"
#include "kk_property_db.h" #include "kk_property_db.h"
...@@ -24,25 +24,6 @@ ...@@ -24,25 +24,6 @@
全局变量定义 全局变量定义
*************************************************************/ *************************************************************/
extern sqlite3 *g_kk_pDb; extern sqlite3 *g_kk_pDb;
typedef struct {
void *mutex;
sqlite3 *pDb;
} kk_property_db_ctx_t;
typedef enum{
DB_LOCKKEY_IDX = 0,
DB_LOCKKEY_DEVICECODE,
DB_LOCKKEY_KEYID,
DB_LOCKKEY_KEYTYPE,
DB_LOCKKEY_KEYROLE,
DB_LOCKKEY_ISVALID,
DB_LOCKKEY_KEYNAME,
DB_LOCKKEY_KEYEFFECTIVE,
DB_LOCKKEY_KEYEXPIRE
};
static kk_property_db_ctx_t s_kk_property_db_ctx = {0}; static kk_property_db_ctx_t s_kk_property_db_ctx = {0};
static kk_property_db_ctx_t *_kk_property_db_get_ctx(void) static kk_property_db_ctx_t *_kk_property_db_get_ctx(void)
{ {
...@@ -71,7 +52,7 @@ static void _kk_property_db_unlock(void) ...@@ -71,7 +52,7 @@ static void _kk_property_db_unlock(void)
*返 回 值: 0:成功;其他:失败 *返 回 值: 0:成功;其他:失败
*其他说明: *其他说明:
*************************************************************/ *************************************************************/
static int kk_property_db_Init(void) static int _kk_property_db_Init(void)
{ {
kk_property_db_ctx_t *ctx = _kk_property_db_get_ctx(); kk_property_db_ctx_t *ctx = _kk_property_db_get_ctx();
...@@ -145,7 +126,7 @@ int kk_property_db_init(void) ...@@ -145,7 +126,7 @@ int kk_property_db_init(void)
return FAIL_RETURN; return FAIL_RETURN;
} }
res = kk_property_db_Init(); res = _kk_property_db_Init();
if(res != SUCCESS_RETURN){ if(res != SUCCESS_RETURN){
ERROR_PRINT("[%s][%d]kk_wlist_db_Init FAIL!!!\n",__FUNCTION__,__LINE__); ERROR_PRINT("[%s][%d]kk_wlist_db_Init FAIL!!!\n",__FUNCTION__,__LINE__);
......
#ifndef _KK_PROPERTY_DB_H_ #ifndef _KK_PROPERTY_DB_H_
#define _KK_PROPERTY_DB_H_ #define _KK_PROPERTY_DB_H_
#include "kk_tsl_common.h" #include "kk_tsl_common.h"
#include "sqlite3.h"
enum{
DB_LOCKKEY_IDX = 0,
DB_LOCKKEY_DEVICECODE,
DB_LOCKKEY_KEYID,
DB_LOCKKEY_KEYTYPE,
DB_LOCKKEY_KEYROLE,
DB_LOCKKEY_ISVALID,
DB_LOCKKEY_KEYNAME,
DB_LOCKKEY_KEYEFFECTIVE,
DB_LOCKKEY_KEYEXPIRE
};
typedef struct {
void *mutex;
sqlite3 *pDb;
} kk_property_db_ctx_t;
typedef struct{ typedef struct{
int type; int type;
char deviceCode[DEVICE_CODE_MAXLEN]; char deviceCode[DEVICE_CODE_MAXLEN];
char raw[60]; char raw[60];
}kk_prop_raw_struct_t; }kk_prop_raw_struct_t;
typedef enum{ enum{
DB_IDX = 0, DB_IDX = 0,
DB_DEVICECODE, DB_DEVICECODE,
DB_IDENTIFITER, DB_IDENTIFITER,
...@@ -16,7 +32,19 @@ typedef enum{ ...@@ -16,7 +32,19 @@ typedef enum{
DB_VALUETYPE, DB_VALUETYPE,
DB_DEVTYPE DB_DEVTYPE
}; };
int kk_property_db_init(void);
int kk_property_db_init(void);
int kk_property_db_get_rawdata(const char *identifier,const int dev_type, kk_prop_raw_struct_t* raw, int count);
int kk_property_db_update_value(const char *deviceCode,const char *identifier,const char* value);
int kk_property_db_insert(const char *deviceCode,const char *identifier,kk_tsl_data_type_e valuetype,int devtype);
int kk_property_db_update_value(const char *deviceCode,const char *identifier,const char* value);
int kk_property_db_get_value_directly(const char *deviceCode,const char *identifier,char* value);
int kk_property_db_get_value(const char *deviceCode,const char *identifier,void* value);
int kk_property_db_update(const char *deviceCode);
int kk_property_sync_values(const char *deviceCode);
int kk_property_delete_by_dcode(char deviceCode[DEVICE_CODE_MAXLEN]);
int kk_property_update_lockkeys(char deviceCode[DEVICE_CODE_MAXLEN],char *keyId,int KeyType, int KeyRole, \
int IsValid,char *KeyName,int KeyEffectiveTime,int KeyExpiryTime);
int kk_property_delete_lockkeys(char deviceCode[DEVICE_CODE_MAXLEN],char *keyId);
#endif #endif
...@@ -10,7 +10,17 @@ int kk_subDev_delete_byMac(char device_mac[DEVICE_MAC_MAXLEN]); ...@@ -10,7 +10,17 @@ int kk_subDev_delete_byMac(char device_mac[DEVICE_MAC_MAXLEN]);
int kk_subDev_update_offline(int isOnline,const char *device_mac); int kk_subDev_update_offline(int isOnline,const char *device_mac);
int kk_subDev_update_auth(int isAuth,const char *deviceCode); int kk_subDev_update_auth(int isAuth,const char *deviceCode);
typedef enum{ int kk_subDev_update_sceneSupport(int sceneSupport,const char *deviceCode);
int kk_subDev_delete_by_dcode(char deviceCode[DEVICE_CODE_MAXLEN]);
int kk_subDev_update_productType(char *productType,const char *deviceCode);
int kk_subDev_update_auth(int isAuth,const char *deviceCode);
int kk_subDev_send_property_get_from_db(void);
int kk_subDev_send_property_get(const char *fatherDeviceCode);
int kk_subDev_check_scene_support(const char *deviceCode);
int kk_subDev_set_action_by_productType(const char *productType,const char *sceneId,const char *propertyName,const char *propertyValue,const char *type);
enum{
DB_SUB_IDX = 0, DB_SUB_IDX = 0,
DB_SUB_ONLINE, DB_SUB_ONLINE,
DB_SUB_PRODUCTCODE, DB_SUB_PRODUCTCODE,
......
...@@ -146,6 +146,7 @@ static int kk_get_properties_info(char *deviceCode,cJSON *obj) ...@@ -146,6 +146,7 @@ static int kk_get_properties_info(char *deviceCode,cJSON *obj)
propertyStr = sqlite3_column_text(stmt, DB_IDENTIFITER); propertyStr = sqlite3_column_text(stmt, DB_IDENTIFITER);
valueType = sqlite3_column_int(stmt, DB_VALUETYPE); valueType = sqlite3_column_int(stmt, DB_VALUETYPE);
valueStr = sqlite3_column_text(stmt, DB_VALUE); valueStr = sqlite3_column_text(stmt, DB_VALUE);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
if(valueType == KK_TSL_DATA_TYPE_TEXT){ if(valueType == KK_TSL_DATA_TYPE_TEXT){
cJSON_AddStringToObject(propertyItem, propertyStr, valueStr); cJSON_AddStringToObject(propertyItem, propertyStr, valueStr);
}else if(valueType == KK_TSL_DATA_TYPE_INT|| }else if(valueType == KK_TSL_DATA_TYPE_INT||
...@@ -174,6 +175,7 @@ static int kk_get_sub_devices_info(cJSON *gwdevicesItem,char *deviceCode) ...@@ -174,6 +175,7 @@ static int kk_get_sub_devices_info(cJSON *gwdevicesItem,char *deviceCode)
return FAIL_RETURN; return FAIL_RETURN;
} }
cJSON *subDevices = cJSON_CreateArray(); cJSON *subDevices = cJSON_CreateArray();
#if 1
sqlCmd = sqlite3_mprintf("select * from SubDeviceInfo WHERE fatherDeviceCode = '%s' ",deviceCode); sqlCmd = sqlite3_mprintf("select * from SubDeviceInfo WHERE fatherDeviceCode = '%s' ",deviceCode);
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL); sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){ while(sqlite3_step(stmt) == SQLITE_ROW){
...@@ -191,8 +193,9 @@ static int kk_get_sub_devices_info(cJSON *gwdevicesItem,char *deviceCode) ...@@ -191,8 +193,9 @@ static int kk_get_sub_devices_info(cJSON *gwdevicesItem,char *deviceCode)
cJSON_AddStringToObject(subdevicesItem, KK_SYNC_SN_STR, ""); cJSON_AddStringToObject(subdevicesItem, KK_SYNC_SN_STR, "");
cJSON_AddStringToObject(subdevicesItem, KK_SYNC_STATUS_STR, "启动"); cJSON_AddStringToObject(subdevicesItem, KK_SYNC_STATUS_STR, "启动");
kk_get_properties_info(subDeviceCode,subdevicesItem); kk_get_properties_info(subDeviceCode,subdevicesItem);
cJSON_AddItemToArray(subDevices, gwdevicesItem); cJSON_AddItemToArray(subDevices, subdevicesItem);
} }
#endif
cJSON_AddItemToObject(gwdevicesItem, KK_SYNC_DEVICES_STR, subDevices); cJSON_AddItemToObject(gwdevicesItem, KK_SYNC_DEVICES_STR, subDevices);
sqlite3_free(sqlCmd); sqlite3_free(sqlCmd);
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
...@@ -224,6 +227,8 @@ static int kk_get_gw_devices_info(cJSON *gwdevices) ...@@ -224,6 +227,8 @@ static int kk_get_gw_devices_info(cJSON *gwdevices)
gw_productCode = sqlite3_column_text(stmt, DB_SUB_PRODUCTCODE); gw_productCode = sqlite3_column_text(stmt, DB_SUB_PRODUCTCODE);
gw_version = sqlite3_column_text(stmt, DB_SUB_VERSION); gw_version = sqlite3_column_text(stmt, DB_SUB_VERSION);
gw_isline = sqlite3_column_text(stmt, DB_SUB_ONLINE); gw_isline = sqlite3_column_text(stmt, DB_SUB_ONLINE);
printf("gw_deviceCode:%s\n",gw_deviceCode);
printf("gw_productCode:%s\n",gw_productCode);
cJSON_AddStringToObject(gwdevicesItem, KK_SYNC_DEVICECODE_STR, gw_deviceCode); cJSON_AddStringToObject(gwdevicesItem, KK_SYNC_DEVICECODE_STR, gw_deviceCode);
cJSON_AddStringToObject(gwdevicesItem, KK_SYNC_VERSION_STR, gw_version); cJSON_AddStringToObject(gwdevicesItem, KK_SYNC_VERSION_STR, gw_version);
cJSON_AddStringToObject(gwdevicesItem, KK_SYNC_MAC_STR, gw_deviceCode); cJSON_AddStringToObject(gwdevicesItem, KK_SYNC_MAC_STR, gw_deviceCode);
...@@ -293,11 +298,11 @@ static int kk_get_scenes_trigger_info(char *triggerArray,char *sceneId) ...@@ -293,11 +298,11 @@ static int kk_get_scenes_trigger_info(char *triggerArray,char *sceneId)
char *sqlCmd = NULL; char *sqlCmd = NULL;
char *zErrMsg = 0; char *zErrMsg = 0;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
char *deviceCode = NULL; const unsigned char *deviceCode = NULL;
char *propertyName = NULL; const unsigned char *propertyName = NULL;
char *compareType = NULL; const unsigned char *compareType = NULL;
char *compareValue = NULL; const unsigned char *compareValue = NULL;
char *type = NULL; const unsigned char *type = NULL;
int delay = 0,epNum = 0; int delay = 0,epNum = 0;
kk_sync_ctx_t *ctx = _kk_sync_get_ctx(); kk_sync_ctx_t *ctx = _kk_sync_get_ctx();
if(triggerArray == NULL || sceneId == NULL){ if(triggerArray == NULL || sceneId == NULL){
...@@ -313,11 +318,11 @@ static int kk_get_scenes_trigger_info(char *triggerArray,char *sceneId) ...@@ -313,11 +318,11 @@ static int kk_get_scenes_trigger_info(char *triggerArray,char *sceneId)
compareType = sqlite3_column_text(stmt, DB_SCENETRIGGER_COMPARETYPE); compareType = sqlite3_column_text(stmt, DB_SCENETRIGGER_COMPARETYPE);
compareValue = sqlite3_column_text(stmt, DB_SCENETRIGGER_COMPAREVALUE); compareValue = sqlite3_column_text(stmt, DB_SCENETRIGGER_COMPAREVALUE);
type = sqlite3_column_text(stmt, DB_SCENETRIGGER_TYPE); type = sqlite3_column_text(stmt, DB_SCENETRIGGER_TYPE);
cJSON_AddStringToObject(triggerItem, KK_SYNC_DEVICECODE_STR, deviceCode); cJSON_AddStringToObject(triggerItem, KK_SYNC_DEVICECODE_STR, (char*)deviceCode);
cJSON_AddNumberToObject(triggerItem, KK_SYNC_SCENE_EPNUM_STR, epNum); cJSON_AddNumberToObject(triggerItem, KK_SYNC_SCENE_EPNUM_STR, epNum);
cJSON_AddStringToObject(triggerItem, KK_SYNC_SCENE_PROPERTYNAME_STR, propertyName); cJSON_AddStringToObject(triggerItem, KK_SYNC_SCENE_PROPERTYNAME_STR, (char*)propertyName);
cJSON_AddStringToObject(triggerItem, KK_SYNC_SCENE_COMPARETYPE_STR, compareType); cJSON_AddStringToObject(triggerItem, KK_SYNC_SCENE_COMPARETYPE_STR, (char*)compareType);
cJSON_AddStringToObject(triggerItem, KK_SYNC_SCENE_COMPAREValue_STR, compareValue); cJSON_AddStringToObject(triggerItem, KK_SYNC_SCENE_COMPAREValue_STR, (char*)compareValue);
cJSON_AddItemToArray(triggerArray, triggerItem); cJSON_AddItemToArray(triggerArray, triggerItem);
} }
sqlite3_free(sqlCmd); sqlite3_free(sqlCmd);
...@@ -445,6 +450,8 @@ static int kk_get_ccu_properties(cJSON *property) ...@@ -445,6 +450,8 @@ static int kk_get_ccu_properties(cJSON *property)
*************************************************************/ *************************************************************/
char *kk_sync_get_info() char *kk_sync_get_info()
{ {
char *out = NULL;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
char ccu_deviceCode[DEVICE_CODE_MAXLEN] = {0}; char ccu_deviceCode[DEVICE_CODE_MAXLEN] = {0};
char ccu_productCode[PRODUCT_CODE_MAXLEN] = {0}; char ccu_productCode[PRODUCT_CODE_MAXLEN] = {0};
char version[32] = {0}; char version[32] = {0};
...@@ -453,13 +460,17 @@ char *kk_sync_get_info() ...@@ -453,13 +460,17 @@ char *kk_sync_get_info()
cJSON_AddStringToObject(root,KK_SYNC_CODE_STR,"0"); cJSON_AddStringToObject(root,KK_SYNC_CODE_STR,"0");
cJSON *data = cJSON_CreateObject(); cJSON *data = cJSON_CreateObject();
cJSON_AddItemToObject(root, KK_SYNC_DATA_STR, data); cJSON_AddItemToObject(root, KK_SYNC_DATA_STR, data);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_get_rooms_info(data); kk_get_rooms_info(data);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_get_devices_info(data); kk_get_devices_info(data);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_get_scenes_info(data); kk_get_scenes_info(data);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
HAL_GetDevice_Code(ccu_deviceCode); HAL_GetDevice_Code(ccu_deviceCode);
HAL_GetProduct_Code(ccu_productCode); HAL_GetProduct_Code(ccu_productCode);
HAL_GetVersion(version); HAL_GetVersion(version);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
cJSON_AddStringToObject(data, KK_SYNC_DEVICECODE_STR,ccu_deviceCode); cJSON_AddStringToObject(data, KK_SYNC_DEVICECODE_STR,ccu_deviceCode);
cJSON_AddStringToObject(data, KK_SYNC_NANE_STR, "newccu"); cJSON_AddStringToObject(data, KK_SYNC_NANE_STR, "newccu");
sprintf(cloudstatus_str, "%d", kk_get_cloudstatus()); sprintf(cloudstatus_str, "%d", kk_get_cloudstatus());
...@@ -467,12 +478,14 @@ char *kk_sync_get_info() ...@@ -467,12 +478,14 @@ char *kk_sync_get_info()
cJSON_AddStringToObject(data, KK_SYNC_PRODUCTCODE_STR, ccu_productCode); cJSON_AddStringToObject(data, KK_SYNC_PRODUCTCODE_STR, ccu_productCode);
cJSON_AddStringToObject(data, KK_SYNC_VERSION_STR, version); cJSON_AddStringToObject(data, KK_SYNC_VERSION_STR, version);
cJSON *property = cJSON_CreateObject(); cJSON *property = cJSON_CreateObject();
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_get_ccu_properties(property); kk_get_ccu_properties(property);
cJSON_AddItemToObject(data, KK_SYNC_PROPERTY_STR, property); cJSON_AddItemToObject(data, KK_SYNC_PROPERTY_STR, property);
char *out=cJSON_Print(root); printf("[%s][%d]\n",__FUNCTION__,__LINE__);
out=cJSON_Print(root);
printf("kk_sync_get_info:%s\n",out); printf("kk_sync_get_info:%s\n",out);
cJSON_Delete(root); cJSON_Delete(root);
free(out); //free(out);
return NULL; return out;
} }
...@@ -2,17 +2,21 @@ ...@@ -2,17 +2,21 @@
#ifndef __KK_HISTORY_DB_H__ #ifndef __KK_HISTORY_DB_H__
#define __KK_HISTORY_DB_H__ #define __KK_HISTORY_DB_H__
typedef enum{ enum{
DB_SENSOR_DEVICECODE = 0, DB_SENSOR_DEVICECODE = 0,
DB_SENSOR_IDENTIFIER, DB_SENSOR_IDENTIFIER,
DB_SENSOR_VALUE, DB_SENSOR_VALUE,
DB_SENSOR_RECORDTIME, DB_SENSOR_RECORDTIME,
}; };
typedef enum{ enum{
DB_OUTLET_DEVICECODE = 0, DB_OUTLET_DEVICECODE = 0,
DB_OUTLET_POWER, DB_OUTLET_POWER,
DB_OUTLET_METERING, DB_OUTLET_METERING,
DB_OUTLET_RECORDTIME, DB_OUTLET_RECORDTIME,
}; };
int kk_history_db_init(void);
int kk_history_delete_by_recordtime(const char *table,time_t time);
int kk_history_insert_sensor_info(const char* deviceCode,const char* identifier,const char* valueStr,time_t time);
int kk_history_insert_Outlet_info(const char* deviceCode,const char* power,const char* metering,time_t time);
#endif #endif
...@@ -32,15 +32,21 @@ ...@@ -32,15 +32,21 @@
#include "kk_dm_queue.h" #include "kk_dm_queue.h"
#include "kk_property_db.h" #include "kk_property_db.h"
#include "sqlite3.h" #include "sqlite3.h"
#include "kk_linkkit.h"
#include "kk_dm_heartbeat.h"
#include "kk_sub_db.h"
#include "kk_hal.h"
#include "kk_history_db.h"
#include "kk_scene_handle.h"
#include "dm_ota.h"
int g_timezone = 8; int g_timezone = 8;
char * g_filerToPlatTable[] = char * g_filerToPlatTable[] =
{ {
{KK_REGISTER_TOPIC_REPLY}, (char *){KK_REGISTER_TOPIC_REPLY},
{KK_ADD_TOPIC_REPLY}, (char *){KK_ADD_TOPIC_REPLY},
{KK_LOGIN_TOPIC_REPLY}, (char *){KK_LOGIN_TOPIC_REPLY},
{KK_THING_SERVICE_PROPERTY_GET}, (char *){KK_THING_SERVICE_PROPERTY_GET},
{KK_THING_CLOUDSTATE_MSG}, (char *){KK_THING_CLOUDSTATE_MSG},
#if 0 #if 0
{KK_THING_SERVICE_SETLOCALTIMER}, {KK_THING_SERVICE_SETLOCALTIMER},
{KK_THING_SERVICE_GETLOCALTIMER}, {KK_THING_SERVICE_GETLOCALTIMER},
...@@ -50,10 +56,11 @@ char * g_filerToPlatTable[] = ...@@ -50,10 +56,11 @@ char * g_filerToPlatTable[] =
{KK_THING_SERVICE_DELETEKEY}, {KK_THING_SERVICE_DELETEKEY},
{KK_THING_SERVICE_MODIFYKEY}, {KK_THING_SERVICE_MODIFYKEY},
#endif #endif
{KK_THING_SERVICE_ADDSCENC}, (char *){KK_THING_SERVICE_ADDSCENC},
{KK_THING_SERVICE_UPDATESCENC}, (char *){KK_THING_SERVICE_UPDATESCENC},
{KK_THING_SERVICE_DELETESCENC}, (char *){KK_THING_SERVICE_DELETESCENC},
{KK_THING_SERVICE_NEGATIVE}, (char *){KK_THING_SERVICE_NEGATIVE},
(char *){KK_THING_SERVICE_SYNCINFO},
}; };
/************************************************************ /************************************************************
*功能描述:过滤不需要下发给网关的消息 *功能描述:过滤不需要下发给网关的消息
...@@ -149,7 +156,7 @@ static int kk_property_set_data_check(cJSON * payload,const char *deviceCode){ ...@@ -149,7 +156,7 @@ static int kk_property_set_data_check(cJSON * payload,const char *deviceCode){
int res = 0; int res = 0;
int idx = 0; int idx = 0;
int value = 0; int value = 0;
res = dm_mgr_get_device_by_devicecode(deviceCode,&node); res = dm_mgr_get_device_by_devicecode((char*)deviceCode,&node);
if (res != SUCCESS_RETURN) { if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res); ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN; return FAIL_RETURN;
...@@ -394,7 +401,7 @@ void mid2p_cb(void* data, int len, char* chalMark){ ...@@ -394,7 +401,7 @@ void mid2p_cb(void* data, int len, char* chalMark){
*************************************************************/ *************************************************************/
void gw2mid_cb(void* data, int len, char* chalMark){ void gw2mid_cb(void* data, int len, char* chalMark){
if (data != NULL){ if (data != NULL){
printf("gw2mid_cb chalMark=%s, data: %s [%d]RECEIVED \r\n", chalMark, data,len); printf("gw2mid_cb chalMark=%s, data: %s [%d]RECEIVED \r\n", chalMark, (char*)data,len);
mid2p_cb(data,len,chalMark); mid2p_cb(data,len,chalMark);
} }
} }
...@@ -692,9 +699,7 @@ void kk_platMsg_dispatch(void) ...@@ -692,9 +699,7 @@ void kk_platMsg_dispatch(void)
if (dm_queue_msg_next2(&data) == SUCCESS_RETURN) { if (dm_queue_msg_next2(&data) == SUCCESS_RETURN) {
dm_queue_msg_t *msg = (dm_queue_msg_t *)data; dm_queue_msg_t *msg = (dm_queue_msg_t *)data;
//INFO_PRINT("kk_handle_platMsg_dispatch get call \n"); //INFO_PRINT("kk_handle_platMsg_dispatch get call \n");
if (kk_platMsg_handle) { kk_platMsg_handle(msg->data,(char*)msg->chalMark);
kk_platMsg_handle(msg->data,msg->chalMark);
}
if (msg->data != NULL){ if (msg->data != NULL){
free(msg->data); free(msg->data);
......
...@@ -2,21 +2,21 @@ ...@@ -2,21 +2,21 @@
#ifndef __KK_SCENE_DB_H__ #ifndef __KK_SCENE_DB_H__
#define __KK_SCENE_DB_H__ #define __KK_SCENE_DB_H__
typedef enum{ enum{
DB_SCENETYPE_SCENE = 0, DB_SCENETYPE_SCENE = 0,
DB_SCENETYPE_IFTT, DB_SCENETYPE_IFTT,
DB_SCENETYPE_TIMING, DB_SCENETYPE_TIMING,
DB_SCENETYPE_MUTICONTROL, DB_SCENETYPE_MUTICONTROL,
}; };
typedef enum{ enum{
DB_SCENEINFO_NAME = 0, DB_SCENEINFO_NAME = 0,
DB_SCENEINFO_SCENETYPE, DB_SCENEINFO_SCENETYPE,
DB_SCENEINFO_ENABLE, DB_SCENEINFO_ENABLE,
DB_SCENEINFO_SCENEID, DB_SCENEINFO_SCENEID,
}; };
typedef enum{ enum{
DB_SCENETRIGGER_TYPE = 0, DB_SCENETRIGGER_TYPE = 0,
DB_SCENETRIGGER_DEVICECODE, DB_SCENETRIGGER_DEVICECODE,
DB_SCENETRIGGER_EPNUM, DB_SCENETRIGGER_EPNUM,
...@@ -27,7 +27,7 @@ typedef enum{ ...@@ -27,7 +27,7 @@ typedef enum{
DB_SCENETRIGGER_ISAND, DB_SCENETRIGGER_ISAND,
}; };
typedef enum{ enum{
DB_SCENECONDITION_TYPE = 0, DB_SCENECONDITION_TYPE = 0,
DB_SCENECONDITION_STARTTIME, DB_SCENECONDITION_STARTTIME,
DB_SCENECONDITION_ENDTIME, DB_SCENECONDITION_ENDTIME,
...@@ -36,7 +36,7 @@ typedef enum{ ...@@ -36,7 +36,7 @@ typedef enum{
DB_SCENECONDITION_SCENEID, DB_SCENECONDITION_SCENEID,
}; };
typedef enum{ enum{
DB_SCENEACTION_TYPE = 0, DB_SCENEACTION_TYPE = 0,
DB_SCENEACTION_DEVICECODE, DB_SCENEACTION_DEVICECODE,
DB_SCENEACTION_EPNUM, DB_SCENEACTION_EPNUM,
...@@ -47,21 +47,21 @@ typedef enum{ ...@@ -47,21 +47,21 @@ typedef enum{
DB_SCENEACTION_GWDEVICECODE, DB_SCENEACTION_GWDEVICECODE,
}; };
typedef enum{ enum{
DB_SCENETIMER_WEEK = 0, DB_SCENETIMER_WEEK = 0,
DB_SCENETIMER_TIME, DB_SCENETIMER_TIME,
DB_SCENETIMER_SCENEID, DB_SCENETIMER_SCENEID,
}; };
typedef enum{ enum{
DB_SCENEEMBED_WEEK = 0, DB_SCENEEMBED_WEEK = 0,
DB_SCENEEMBED_EXECUTESCENEID, DB_SCENEEMBED_EXECUTESCENEID,
DB_SCENEMBED_SCENEID, DB_SCENEMBED_SCENEID,
}; };
typedef enum{ enum{
DB_SCENEINVOKESERVICE_TYPE = 0, DB_SCENEINVOKESERVICE_TYPE = 0,
DB_SCENEINVOKESERVICE_IDENTIFIER, DB_SCENEINVOKESERVICE_IDENTIFIER,
DB_SCENEINVOKESERVICE_SCENEID, DB_SCENEINVOKESERVICE_SCENEID,
......
...@@ -12,11 +12,9 @@ ...@@ -12,11 +12,9 @@
*************************************************************/ *************************************************************/
#include <stdio.h> #include <stdio.h>
#include "kk_tsl_api.h"
#include "kk_dm_mng.h"
#include "kk_scene_handle.h" #include "kk_scene_handle.h"
#include "kk_scene_db.h"
#include "cJSON.h"
#include <time.h> #include <time.h>
......
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
#include "kk_tsl_common.h" #include "kk_tsl_common.h"
#include "sqlite3.h" #include "sqlite3.h"
#include "kk_log.h" #include "kk_log.h"
#include "kk_scene_db.h"
#include "cJSON.h"
#include "kk_tsl_api.h"
#include "kk_dm_mng.h"
typedef struct kk_scene_action_detail{ typedef struct kk_scene_action_detail{
char deviceCode[DEVICE_CODE_MAXLEN]; char deviceCode[DEVICE_CODE_MAXLEN];
char propertyName[DEVICE_PROPERTY_NAME_MAX]; char propertyName[DEVICE_PROPERTY_NAME_MAX];
...@@ -61,6 +64,13 @@ typedef struct { ...@@ -61,6 +64,13 @@ typedef struct {
int kk_scene_init(void); int kk_scene_init(void);
int kk_scene_iftt_check(const char*deviceCode,cJSON *param);
int kk_scene_execute_scene(const char *sceneId,const char *msgId);
void kk_scene_execute_event_post(const char* sceneId,const char *msgId);
int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param);
int kk_scene_execute_action(const char* sceneId,char *msgId);
int kk_scene_check_condition(const char *sceneId);
int kk_scene_check_trigger_condition(const char *sceneId);
int kk_scene_get_scene_info(const char* sceneId,int *sceneType,int *enable);
#endif #endif
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "kk_dm_mng.h" #include "kk_dm_mng.h"
#include "kk_dm_msg.h" #include "kk_dm_msg.h"
#include "kk_log.h" #include "kk_log.h"
#include "kk_utils.h"
#include "kk_tsl_parse.h"
typedef enum { typedef enum {
KK_MSG_PROPERTY_SET, KK_MSG_PROPERTY_SET,
...@@ -1064,7 +1066,8 @@ int kk_tsl_set_value(kk_tsl_set_t set, kk_tsl_t * dev_shadow, const char *identi ...@@ -1064,7 +1066,8 @@ int kk_tsl_set_value(kk_tsl_set_t set, kk_tsl_t * dev_shadow, const char *identi
for (index = 0; index < dev_shadow->event_number; index++) { for (index = 0; index < dev_shadow->event_number; index++) {
property = dev_shadow->events + index; property = dev_shadow->events + index;
if(strcmp(property->identifier,"property") == 0 || if(strcmp(property->identifier,"property") == 0 ||
strcmp(property->identifier,"post") == 0){ strcmp(property->identifier,"post") == 0)
{
e_identifier = property->identifier; e_identifier = property->identifier;
break; break;
} }
...@@ -1136,7 +1139,7 @@ int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int start_deli, ...@@ -1136,7 +1139,7 @@ int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int start_deli,
return SUCCESS_RETURN; return SUCCESS_RETURN;
} }
int kk_tsl_property_set_by_shadow(kk_tsl_t * dev_shadow, const char *payload, unsigned int payload_len) int kk_tsl_property_set_by_shadow(kk_tsl_t* dev_shadow, const char *payload, unsigned int payload_len)
{ {
kk_msg_request_payload_t request; kk_msg_request_payload_t request;
int res = 0, devid = 0; int res = 0, devid = 0;
...@@ -1233,7 +1236,8 @@ int kk_tsl_get_property_number(_IN_ kk_tsl_t *shadow, _OU_ int *number) ...@@ -1233,7 +1236,8 @@ int kk_tsl_get_property_number(_IN_ kk_tsl_t *shadow, _OU_ int *number)
for (index = 0; index < shadow->event_number; index++) { for (index = 0; index < shadow->event_number; index++) {
property = shadow->events + index; property = shadow->events + index;
if(strcmp(property->identifier,"property") == 0 || if(strcmp(property->identifier,"property") == 0 ||
strcmp(property->identifier,"post") == 0){ strcmp(property->identifier,"post") == 0)
{
*number = property->output_data_number; *number = property->output_data_number;
return SUCCESS_RETURN; return SUCCESS_RETURN;
} }
...@@ -1288,7 +1292,8 @@ int kk_tsl_post_property_add(_IN_ void *handle, _IN_ char *identifier, _IN_ int ...@@ -1288,7 +1292,8 @@ int kk_tsl_post_property_add(_IN_ void *handle, _IN_ char *identifier, _IN_ int
for (index = 0; index < dapi_property->shadow->event_number; index++) { for (index = 0; index < dapi_property->shadow->event_number; index++) {
event = dapi_property->shadow->events + index; event = dapi_property->shadow->events + index;
if(strcmp(event->identifier,"property") == 0 || if(strcmp(event->identifier,"property") == 0 ||
strcmp(event->identifier,"post") == 0){ strcmp(event->identifier,"post") == 0)
{
pEventoutput = (kk_tsl_data_t *)(event->output_datas); pEventoutput = (kk_tsl_data_t *)(event->output_datas);
number = event->output_data_number; number = event->output_data_number;
break; break;
......
...@@ -83,5 +83,16 @@ extern int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int star ...@@ -83,5 +83,16 @@ extern int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int star
extern char* kk_tsl_get_post_property_str(kk_tsl_t *dev_shadow, const char *property_identifier); extern char* kk_tsl_get_post_property_str(kk_tsl_t *dev_shadow, const char *property_identifier);
extern char* kk_tsl_get_post_event_str(_IN_ kk_tsl_t *dev_shadow, _IN_ char *identifier, _IN_ int identifier_len); extern char* kk_tsl_get_post_event_str(_IN_ kk_tsl_t *dev_shadow, _IN_ char *identifier, _IN_ int identifier_len);
extern char * kk_tsl_get_post_service_str(kk_tsl_t *dev_shadow, const char *service_identifier); extern char * kk_tsl_get_post_service_str(kk_tsl_t *dev_shadow, const char *service_identifier);
int kk_tsl_property_set_by_shadow(kk_tsl_t* dev_shadow, const char *payload, unsigned int payload_len);
int kk_tsl_get_property_data(_IN_ kk_tsl_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int dm_tsl_get_event_output_data(_IN_ kk_tsl_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int dm_tls_set_event_output_value(_IN_ kk_tsl_t *shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int dm_tsl_get_service_input_output_data(_IN_ kk_tsl_data_target_e type, _IN_ kk_tsl_t *shadow, _IN_ char *key,
_IN_ int key_len, _OU_ void **data);
int dm_tsl_set_service_input_output_value(_IN_ kk_tsl_data_target_e type, _IN_ kk_tsl_t *shadow, _IN_ char *key,
_IN_ int key_len, _IN_ void *value, _IN_ int value_len);
int dm_tsl_set_property_value(_IN_ kk_tsl_t *shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int kk_tsl_get_data_type(_IN_ void *data, _OU_ kk_tsl_data_type_e *type);
#endif #endif
#include "kk_tsl_common.h" #include "kk_tsl_common.h"
#include "kk_utils.h"
#include "kk_tsl_parse.h"
typedef int (*kk_tsl_data_set)(_IN_ kk_tsl_data_value_t *data_value, _IN_ void *value, _IN_ int value_len); typedef int (*kk_tsl_data_set)(_IN_ kk_tsl_data_value_t *data_value, _IN_ void *value, _IN_ int value_len);
typedef int (*kk_tsl_array_set)(_IN_ kk_tsl_data_value_t *data_value, _IN_ void *value, _IN_ int value_len, typedef int (*kk_tsl_array_set)(_IN_ kk_tsl_data_value_t *data_value, _IN_ void *value, _IN_ int value_len,
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "kk_tsl_common.h" #include "kk_tsl_common.h"
#include "kk_dm_mng.h" #include "kk_dm_mng.h"
#include "kk_log.h" #include "kk_log.h"
#include "kk_tsl_parse.h"
#define TSL_SUBDEVICE_PATH_FILE "/usr/kk/tsl/product_%s.json" #define TSL_SUBDEVICE_PATH_FILE "/usr/kk/tsl/product_%s.json"
#define TSL_GATEWAY_PATH_FILE "/usr/kk/tsl/gateway_%s.json" #define TSL_GATEWAY_PATH_FILE "/usr/kk/tsl/gateway_%s.json"
#define TSL_CCU_PATH_FILE "/usr/kk/tsl/ccu_%s.json" #define TSL_CCU_PATH_FILE "/usr/kk/tsl/ccu_%s.json"
......
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "lite-cjson.h"
#include "kk_tsl_common.h"
#include "kk_log.h"
#include "kk_log.h"
#include "kk_tsl_parse.h"
typedef int (*kk_tsl_data_parse)(_IN_ kk_tsl_data_value_t *data_value, _IN_ lite_cjson_t *root); typedef int (*kk_tsl_data_parse)(_IN_ kk_tsl_data_value_t *data_value, _IN_ lite_cjson_t *root);
typedef int (*kk_tsl_array_parse)(_IN_ kk_tsl_data_value_t *data_value, _IN_ lite_cjson_t *root); typedef int (*kk_tsl_array_parse)(_IN_ kk_tsl_data_value_t *data_value, _IN_ lite_cjson_t *root);
......
#ifndef __KK_TSL_PARSE_H__
#define __KK_TSL_PARSE_H__
#include "kk_tsl_common.h"
#include "lite-cjson.h"
int _kk_tsl_property_search(_IN_ kk_tsl_t *shadow, _IN_ char *key, _IN_ int key_len,
_OU_ kk_tsl_data_t **property, _OU_ int *index);
int kk_tsl_get_property_data(_IN_ kk_tsl_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int kk_tsl_data_search(_IN_ kk_tsl_data_t *input, _IN_ char *key,
_IN_ int key_len, _OU_ kk_tsl_data_t **output, _OU_ int *index);
int kk_tsl_assemble_property(_IN_ kk_tsl_t *shadow, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
int kk_tsl_assemble_event_output(_IN_ kk_tsl_t *shadow, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
int kk_tsl_assemble_service_output(_IN_ kk_tsl_t *shadow, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
int kk_tsl_create(_IN_ const char *tsl, _IN_ int tsl_len, _OU_ kk_tsl_t **shadow,_OU_ int *heartbeat,_OU_ int *isDormancyDev);
#endif
\ No newline at end of file
...@@ -472,11 +472,12 @@ static int parse_value(_IN_ lite_cjson_t *const lite, _IN_ parse_buffer *const i ...@@ -472,11 +472,12 @@ static int parse_value(_IN_ lite_cjson_t *const lite, _IN_ parse_buffer *const i
int lite_cjson_parse(_IN_ const char *src, _IN_ int src_len, _OU_ lite_cjson_t *lite) int lite_cjson_parse(_IN_ const char *src, _IN_ int src_len, _OU_ lite_cjson_t *lite)
{ {
parse_buffer buffer;
if (!lite || !src || !lite || src_len <= 0) { if (!lite || !src || !lite || src_len <= 0) {
return -1; return -1;
} }
parse_buffer buffer;
memset(&buffer, 0, sizeof(parse_buffer)); memset(&buffer, 0, sizeof(parse_buffer));
buffer.content = (const unsigned char *)src; buffer.content = (const unsigned char *)src;
...@@ -560,22 +561,25 @@ int lite_cjson_is_object(_IN_ lite_cjson_t *lite) ...@@ -560,22 +561,25 @@ int lite_cjson_is_object(_IN_ lite_cjson_t *lite)
int lite_cjson_array_item(_IN_ lite_cjson_t *lite, _IN_ int index, _OU_ lite_cjson_t *lite_item) int lite_cjson_array_item(_IN_ lite_cjson_t *lite, _IN_ int index, _OU_ lite_cjson_t *lite_item)
{ {
parse_buffer buffer;
parse_buffer *p_buffer = &buffer;
lite_cjson_t current_item;
int iter_index = 0;
if (!lite || lite->type != cJSON_Array || !lite->value || if (!lite || lite->type != cJSON_Array || !lite->value ||
index < 0 || index >= lite->size || !lite_item) { index < 0 || index >= lite->size || !lite_item) {
return -1; return -1;
} }
parse_buffer buffer;
parse_buffer *p_buffer = &buffer;
memset(&buffer, 0, sizeof(parse_buffer)); memset(&buffer, 0, sizeof(parse_buffer));
buffer.content = (const unsigned char *)lite->value; buffer.content = (const unsigned char *)lite->value;
buffer.length = lite->value_length; buffer.length = lite->value_length;
buffer.offset = 0; buffer.offset = 0;
lite_cjson_t current_item;
//int start_pos = p_buffer->offset; //int start_pos = p_buffer->offset;
int iter_index = 0;
if (buffer_at_offset(p_buffer)[0] != '[') { if (buffer_at_offset(p_buffer)[0] != '[') {
/* not an array */ /* not an array */
...@@ -626,22 +630,24 @@ int lite_cjson_array_item(_IN_ lite_cjson_t *lite, _IN_ int index, _OU_ lite_cjs ...@@ -626,22 +630,24 @@ int lite_cjson_array_item(_IN_ lite_cjson_t *lite, _IN_ int index, _OU_ lite_cjs
static int _lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ int key_len, static int _lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ int key_len,
_OU_ lite_cjson_t *lite_item) _OU_ lite_cjson_t *lite_item)
{ {
parse_buffer buffer;
parse_buffer *p_buffer = &buffer;
lite_cjson_t current_item_key;
lite_cjson_t current_item_value;
int index = 0;
if (!lite || lite->type != cJSON_Object || !lite->value || lite->size == 0 || !key || key_len <= 0 || !lite_item) { if (!lite || lite->type != cJSON_Object || !lite->value || lite->size == 0 || !key || key_len <= 0 || !lite_item) {
return -1; return -1;
}; };
parse_buffer buffer;
parse_buffer *p_buffer = &buffer;
memset(&buffer, 0, sizeof(parse_buffer)); memset(&buffer, 0, sizeof(parse_buffer));
buffer.content = (const unsigned char *)lite->value; buffer.content = (const unsigned char *)lite->value;
buffer.length = lite->value_length; buffer.length = lite->value_length;
buffer.offset = 0; buffer.offset = 0;
lite_cjson_t current_item_key;
lite_cjson_t current_item_value;
//int start_pos = p_buffer->offset; //int start_pos = p_buffer->offset;
int index = 0;
if (cannot_access_at_index(p_buffer, 0) || (buffer_at_offset(p_buffer)[0] != '{')) { if (cannot_access_at_index(p_buffer, 0) || (buffer_at_offset(p_buffer)[0] != '{')) {
return -1; /* not an object */ return -1; /* not an object */
...@@ -705,16 +711,16 @@ static int _lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key ...@@ -705,16 +711,16 @@ static int _lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key
static int _lite_cjson_key_array_index(_IN_ const char *key, _IN_ int key_len, static int _lite_cjson_key_array_index(_IN_ const char *key, _IN_ int key_len,
_OU_ int *partial_key_len, _OU_ int *array_key_len, _OU_ int *array_index) _OU_ int *partial_key_len, _OU_ int *array_key_len, _OU_ int *array_index)
{ {
if (!key || !partial_key_len || !array_key_len || !array_index) {
return -1;
}
char *bracket_pre = NULL; char *bracket_pre = NULL;
char *bracket_suf = NULL; char *bracket_suf = NULL;
int index = 0; int index = 0;
int deep = 0; int deep = 0;
char array_index_str[10] = {0}; char array_index_str[10] = {0};
if (!key || !partial_key_len || !array_key_len || !array_index) {
return -1;
}
for (index = 0; index < key_len; index++) { for (index = 0; index < key_len; index++) {
switch (key[index]) { switch (key[index]) {
case '[': { case '[': {
...@@ -762,9 +768,7 @@ static int _lite_cjson_key_array_index(_IN_ const char *key, _IN_ int key_len, ...@@ -762,9 +768,7 @@ static int _lite_cjson_key_array_index(_IN_ const char *key, _IN_ int key_len,
int lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ int key_len, int lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ int key_len,
_OU_ lite_cjson_t *lite_item) _OU_ lite_cjson_t *lite_item)
{ {
if (!lite || lite->type != cJSON_Object || !lite->value || lite->size == 0 || !key || key_len <= 0 || !lite_item) {
return -1;
};
int res = 0; int res = 0;
char *delim = NULL; char *delim = NULL;
...@@ -778,6 +782,9 @@ int lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ i ...@@ -778,6 +782,9 @@ int lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ i
int array_key_len = 0; int array_key_len = 0;
int array_index = 0; int array_index = 0;
if (!lite || lite->type != cJSON_Object || !lite->value || lite->size == 0 || !key || key_len <= 0 || !lite_item) {
return -1;
};
memcpy(&lite_iter, lite, sizeof(lite_cjson_t)); memcpy(&lite_iter, lite, sizeof(lite_cjson_t));
memset(&lite_prev, 0, sizeof(lite_cjson_t)); memset(&lite_prev, 0, sizeof(lite_cjson_t));
...@@ -833,22 +840,25 @@ int lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ i ...@@ -833,22 +840,25 @@ int lite_cjson_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ i
int lite_cjson_object_item_by_index(_IN_ lite_cjson_t *lite, _IN_ int index, _OU_ lite_cjson_t *lite_item_key, int lite_cjson_object_item_by_index(_IN_ lite_cjson_t *lite, _IN_ int index, _OU_ lite_cjson_t *lite_item_key,
_OU_ lite_cjson_t *lite_item_value) _OU_ lite_cjson_t *lite_item_value)
{ {
parse_buffer buffer;
parse_buffer *p_buffer = &buffer;
int item_index = 0;
lite_cjson_t current_item_key;
lite_cjson_t current_item_value;
if (!lite || lite->type != cJSON_Object || !lite->value || lite->size == 0 || index < 0 || index >= lite->size) { if (!lite || lite->type != cJSON_Object || !lite->value || lite->size == 0 || index < 0 || index >= lite->size) {
return -1; return -1;
}; };
parse_buffer buffer;
parse_buffer *p_buffer = &buffer;
memset(&buffer, 0, sizeof(parse_buffer)); memset(&buffer, 0, sizeof(parse_buffer));
buffer.content = (const unsigned char *)lite->value; buffer.content = (const unsigned char *)lite->value;
buffer.length = lite->value_length; buffer.length = lite->value_length;
buffer.offset = 0; buffer.offset = 0;
lite_cjson_t current_item_key;
lite_cjson_t current_item_value;
//int start_pos = p_buffer->offset; //int start_pos = p_buffer->offset;
int item_index = 0;
if (cannot_access_at_index(p_buffer, 0) || (buffer_at_offset(p_buffer)[0] != '{')) { if (cannot_access_at_index(p_buffer, 0) || (buffer_at_offset(p_buffer)[0] != '{')) {
return -1; /* not an object */ return -1; /* not an object */
......
{
"productCode":"3023",
"operateType":"3",
"channel":2,
"newccu":[
{
"identifier":"PowerSwitch_1",
"opcodemap":"SWITCH",
"dataType":"int",
"channel":"1",
"valueRange":[0,1],
"value": 0
},
{
"identifier":"PowerSwitch_2",
"opcodemap":"SWITCH",
"dataType":"int",
"channel":"2",
"valueRange":[0,1],
"value": 0
}
],
"oldccu":[
{
"opcode":"SWITCH",
"identifiermap":"PowerSwitch_1",
"dataType":"string",
"channel":"1",
"valueRange":["OFF","ON"]
},
{
"opcode":"SWITCH",
"identifiermap":"PowerSwitch_2",
"dataType":"string",
"channel":"2",
"valueRange":["OFF","ON"]
}
]
}
\ No newline at end of file
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