Commit 90005f06 authored by 尹佳钦's avatar 尹佳钦

20210803

parents 570bd950 cc48e429
......@@ -52,7 +52,7 @@ const char DM_MSG_TO_MIDDWARE[] = "{\"msgtype\":\"%s\",\"productCode\":\"%s\",\"
#define KK_POWER_SWITCH_2 "PowerSwitch_2"
#define KK_POWER_SWITCH_3 "PowerSwitch_3"
#define KK_POWER_SWITCH_4 "PowerSwitch_4"
extern char* KK_Make_Topic(cJSON *info);
extern char* KK_Make_Topic(cJSON *info,cJSON *data);
extern int KK_Subdev_UnSubscribe(cJSON *payload);
extern int KK_Subdev_UnSubscribe_By_DeviceCode(const char *deviceCode,const char *productCode );
extern int KK_Subdev_Subscribe(const cJSON *root);
......@@ -214,15 +214,16 @@ int _kk_sendto_cloud(cJSON *root)
if(msgTypeStr == NULL){
return RETURN_FAIL;
}
topic = KK_Make_Topic(info);
if(topic == NULL){
return RETURN_FAIL;
}
pData = cJSON_GetObjectItem(root, MSG_PAYLOAD_STR);
if(pData == NULL){
free(topic);
return RETURN_FAIL;
}
topic = KK_Make_Topic(info,pData);
if(topic == NULL){
return RETURN_FAIL;
}
//If topo delete,need UnSubscribe the topic
if(strstr(msgTypeStr->valuestring,KK_FILTER_DELETE_TOPIC) != NULL){
KK_Subdev_UnSubscribe(pData);
......@@ -258,10 +259,12 @@ int _kk_sendto_cloud(cJSON *root)
free(topic);
return RETURN_SUCCESS;
}
if(kk_check_need_split(info)){
kk_split_send_data(pData,topic);
}
else{
//if(kk_check_need_split(info)){
//kk_split_send_data(pData,topic);
//}
//else
{
#if 0
cJSON *pParam = NULL;
cJSON *PowerSwitch = NULL;
int i = 0;
......@@ -282,6 +285,7 @@ int _kk_sendto_cloud(cJSON *root)
break;
}
}
#endif
char* out=cJSON_Print(pData);
KK_MQTT_SendMsg(topic,(const char*)out);
free(out);
......@@ -511,7 +515,7 @@ static char * _kk_data_create(const char *topic,const char *data)
root=cJSON_CreateObject();
cJSON* infoObj = cJSON_Parse(infoStr);
cJSON* payloadObj = cJSON_Parse(data);
#if 1
#if 0
cJSON * params = cJSON_GetObjectItem(payloadObj,"params");
if(params != NULL){
cJSON * PowerSwitch = cJSON_GetObjectItem(params,"PowerSwitch");
......@@ -564,7 +568,7 @@ int KK_Send_CloudState(int state)
infoStr_len = strlen(DM_MSG_TO_MIDDWARE)+strlen(KK_CLOUDSTATE_MSG_REPLY)+10;
infoStr = malloc(infoStr_len);
if(infoStr == NULL){
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
ERROR_PRINT("infoStr == NULL\n");
return RETURN_FAIL;
}
memset(infoStr,0x0,infoStr_len);
......@@ -573,7 +577,7 @@ int KK_Send_CloudState(int state)
payloadStr_len = strlen(DM_MSG_CLOUDSTATE) + 10;
payloadStr = malloc(payloadStr_len);
if(payloadStr == NULL){
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
ERROR_PRINT("payloadStr == NULL\n");
return RETURN_FAIL;
}
memset(payloadStr,0x0,payloadStr_len);
......@@ -589,7 +593,7 @@ int KK_Send_CloudState(int state)
cJSON_Delete(root);
free(payloadStr);
free(infoStr);
INFO_PRINT("[%s][%d]%s\n",__FUNCTION__,__LINE__,out);
INFO_PRINT("%s\n",out);
free(out);
return RETURN_SUCCESS;
}
......@@ -605,11 +609,11 @@ void KK_Sendto_DevData(const char *topic,const char *data)
{
if(_check_invalid_topic(topic))
{
INFO_PRINT("[%s][%d]ingore the topic:%s\n",__FUNCTION__,__LINE__,topic);
INFO_PRINT("ingore the topic:%s\n",topic);
return;//ingore the message
}
INFO_PRINT("[%s][%d]receive from cloud,topic:%s\n",__FUNCTION__,__LINE__,topic);
INFO_PRINT("[%s][%d]receive from cloud,data:%s\n",__FUNCTION__,__LINE__,data);
INFO_PRINT("receive from cloud,topic:%s\n",topic);
INFO_PRINT("receive from cloud,data:%s\n",data);
char *send_data = _kk_data_create(topic,data);
if(send_data == NULL){
return;
......
......@@ -27,7 +27,32 @@
#include "com_api.h"
#include "kk_product.h"
//#include "kcloud_log.h"
static char s_ccuid[DEVICE_CODE_LEN] = {0};
int KK_Get_ccuid(_OU_ char *device_code)
{
strncpy(device_code, s_ccuid, strlen(s_ccuid));
printf("HAL_Get_ccuid:%s\n",s_ccuid);
return strlen(s_ccuid);
}
static int _setDevice_Code(_IN_ char *device_code,int len)
{
memset(s_ccuid, 0x0, DEVICE_CODE_LEN);
printf("_setDevice_Code:%s\n",device_code);
strncpy(s_ccuid, device_code, len);
return len;
}
static void KK_Ccuid_init(void)
{
uint8_t ccuid[DEVICE_CODE_LEN] = {0};
int ccuid_len = 0;
HAL_Execel_cmd(GET_CCUID_CMD,(char *)ccuid,sizeof(ccuid),&ccuid_len);
printf("GET_CCUID_CMD:%s\n",ccuid);
if(ccuid_len > 0 && ccuid_len <= DEVICE_CODE_LEN){
_setDevice_Code(ccuid,ccuid_len-1);
}else{
_setDevice_Code(KK_CCU_ID,strlen(KK_CCU_ID));
}
}
static int mqtt_start(void)
{
int count = 0;
......@@ -63,8 +88,9 @@ int main(int argc, char* argv[])
int rc = 0;
open("kcloud",LOG_PID,LOG_USER);
/*set the callback to get the device date to cloud*/
KK_Ccuid_init();
kk_ipc_init(IPC_APP2MID,(ipc_cb*)KK_Data_FromDev,NULL,NULL);
//kk_info_report_start();
kk_info_report_start();
again:
rc = kk_start_ccu_register();
if(rc == 0){
......
......@@ -40,10 +40,13 @@ static int kk_info_get(char *str)
int ret = 0;
char version[VERSION_MAXLEN] = {0};
char *str_tmp = NULL;
char ccuid[32] = {0};
if(str == NULL){
return -1;
}
HAL_GetVersion(version);
KK_Get_ccuid(ccuid);
cJSON *root = cJSON_CreateObject();
if (root)
{
......@@ -56,20 +59,25 @@ static int kk_info_get(char *str)
if (ccu)
{
char status[4] = {0};
cJSON_AddItemToObject(args, "ccu", ccu);
cJSON_AddStringToObject(ccu, "id", "12345");
cJSON_AddStringToObject(ccu, "id", &ccuid[4]);
cJSON_AddStringToObject(ccu, "version", version);
cJSON_AddStringToObject(ccu, "name", "kk_new_ccu");
if(kk_get_gw_status() == DEVICE_OFFLINE){
strcpy(status,"2");
}
else if(kk_get_cloud_status() == DEVICE_OFFLINE){
//INFO_PRINT("---222kk_get_cloud_status():%d\n",kk_get_cloud_status());
//if(kk_get_gw_status() == DEVICE_OFFLINE){
//memcpy(status,"2",strlen("2"));
//}
if(kk_get_cloud_status() == DEVICE_OFFLINE)
{
//cJSON_AddStringToObject(ccu, "status", "1");
strcpy(status,"1");
//INFO_PRINT("_-----------------------------------------------------------11\n");
memcpy(status,"1",strlen("1"));
}else{
strcpy(status,"0");
//INFO_PRINT("_-----------------------------------------------------------22\n");
memcpy(status,"0",strlen("0"));
}
//INFO_PRINT("------------------------------------->status :%s\n",status);
cJSON_AddStringToObject(ccu, "status", status);
}
......@@ -91,8 +99,10 @@ static int kk_info_get(char *str)
cJSON *qr_code = cJSON_CreateObject();
if (qr_code)
{
char qrcodeStr[256] = {0};
sprintf(qrcodeStr,"KONKE-CCU:%s:%s:BBE3C5A5FC1FDB6CA1A386AB321D02AB",KK_CCU_PRODUCTID,&ccuid[4]);
cJSON_AddItemToObject(args, "qr_code", qr_code);
cJSON_AddStringToObject(qr_code, "string", "KONKE-CCU:15:95972:E53AC78A40C4EBD4ABFFA1009794641B");
cJSON_AddStringToObject(qr_code, "string", qrcodeStr);
cJSON_AddBoolToObject(qr_code, "show", 1);
}
cJSON *service = cJSON_CreateObject();
......
......@@ -20,27 +20,7 @@
#define BUFSIZE 1024
#define PRODUCTSECRET "123456"
int kk_execel_cmd(char * cmd,char * buf,int buf_len,int* ret_len)
{
if(cmd == NULL || buf == NULL || buf_len == 0)
{
printf("arg error\n");
return -1;
}
memset(buf,0,buf_len);
FILE *fp = NULL;
fp = popen(cmd,"r");
while(fgets(buf,buf_len,fp)!=NULL){
printf("%s return %s",cmd,buf);
}
pclose (fp);
*ret_len = strlen(buf);
return 0;
}
int kk_parse_deviceSecret(char *info){
FILE *fp;
if(info == NULL){
......@@ -205,14 +185,16 @@ static char * kk_create_register_info()
unsigned char sign_hex[32] = {0};
char sign_hex_str[65] = {0};
uint8_t productSecret[64] = {0};
char ccuid[32] = {0};
int productSecret_len = 0;
root=cJSON_CreateObject();
HAL_Get_mac(mac);
HAL_GetTime_s(ramdonId);
cJSON_AddStringToObject(root, "deviceCode", KK_CCU_ID);
KK_Get_ccuid(ccuid);
cJSON_AddStringToObject(root, "deviceCode", ccuid);
cJSON_AddStringToObject(root, "productCode",productCode);
signSource = kk_dictionary_order(KK_CCU_ID,productCode,KK_CCU_RANDOM);
kk_execel_cmd(GET_KEY_CMD,(char *)productSecret,sizeof(productSecret),&productSecret_len);
signSource = kk_dictionary_order(ccuid,productCode,KK_CCU_RANDOM);
HAL_Execel_cmd(GET_KEY_CMD,(char *)productSecret,sizeof(productSecret),&productSecret_len);
if(productSecret_len == 0){
utils_hmac_sha256((const uint8_t *)signSource, strlen(signSource), (const uint8_t *)PRODUCTSECRET, strlen(PRODUCTSECRET), sign_hex);
}else{
......@@ -247,10 +229,12 @@ static char * kk_create_get_jwt_info()
char *out = NULL;
char timestamp[16] = {0};
char deviceSecret[64] = {0};
char ccuid[32] = {0};
HAL_GetTime_s(timestamp);
sprintf(g_clientId, CLIENTID, KK_CCU_ID,timestamp);
KK_Get_ccuid(ccuid);
sprintf(g_clientId, CLIENTID, ccuid,timestamp);
root=cJSON_CreateObject();
cJSON_AddStringToObject(root, "deviceCode", KK_CCU_ID);
cJSON_AddStringToObject(root, "deviceCode", ccuid);
cJSON_AddStringToObject(root, "productCode",KK_CCU_PRODUCTID);
cJSON_AddStringToObject(root, "clientId",g_clientId);
fp = fopen("/usr/kk/kk_deviceSecret.txt", "r");
......
......@@ -83,7 +83,7 @@ static int _kk_ccu_topic_subscribe(char productCode[PRODUCT_CODE_LEN],char devic
int res = 0;
int url_len = 0;
url_len = strlen(KK_URI_SYS_PREFIX_SERVICE) + strlen(productCode)+strlen(deviceCode) + 1;
url_len = strlen(KK_URI_SYS_PREFIX) + strlen(productCode)+strlen(deviceCode) + 1;
char *url = malloc(url_len);
if (url == NULL) {
return -1;
......@@ -95,12 +95,14 @@ static int _kk_ccu_topic_subscribe(char productCode[PRODUCT_CODE_LEN],char devic
//res = KK_MQTT_SubTopic(url);
memset(url, 0, url_len);
snprintf(url, url_len, KK_URI_SYS_PREFIX_SERVICE, productCode,deviceCode);
INFO_PRINT("sys [%s][%d] URL:%s\n",__FUNCTION__,__LINE__,url);
res = KK_MQTT_SubTopic(url);
memset(url, 0, url_len);
snprintf(url, url_len, KK_URI_SYS_PREFIX, productCode, deviceCode);
INFO_PRINT("sys [%s][%d] URL:%s\n",__FUNCTION__,__LINE__,url);
res = KK_MQTT_SubTopic(url);
free(url);
return res;
}
......@@ -113,10 +115,12 @@ int KK_CCU_TOPIC_Subscribe(void)
INFO_PRINT("[%s][%d] already Subscribe\n",__FUNCTION__,__LINE__);
return -1;
}
char ccuid[32] = {0};
KK_Get_ccuid(ccuid);
s_ccu_topic_sub = 1;
HAL_GetProduct_Code(productCode);
HAL_Get_mac(deviceCode);
return _kk_ccu_topic_subscribe(productCode,KK_CCU_ID);
return _kk_ccu_topic_subscribe(productCode,ccuid);
}
static int _kk_utils_topic(_IN_ const char *name, _IN_ char *product_code,
......@@ -212,7 +216,7 @@ errorreturn:
return -1;
}
char* KK_Make_Topic(cJSON *info)
char* KK_Make_Topic(cJSON *info,cJSON *data)
{
cJSON *type,*product_code,*device_code;
char *topic = NULL;
......@@ -236,6 +240,7 @@ char* KK_Make_Topic(cJSON *info)
KK_CCU_TOPIC_Subscribe();
}
if(!strcmp(product_code->valuestring,KK_GW_PRODUCTID)){
//printf("[%s][%d]:%s\n",__FUNCTION__,__LINE__,type->valuestring);
if(strstr(type->valuestring,"/thing/status/offline") != NULL){
kk_set_gw_status(DEVICE_OFFLINE);
}else if(strstr(type->valuestring,"/thing/status/online") != NULL){
......
......@@ -206,7 +206,9 @@ MQTTAsync KK_MQTT_Connect(void)
fclose(fp);
}
HAL_Get_mac(mac);
sprintf(usrname, "%s.%s", KK_CCU_ID,KK_CCU_PRODUCTID);
char ccuid[32] = {0};
KK_Get_ccuid(ccuid);
sprintf(usrname, "%s.%s", ccuid,KK_CCU_PRODUCTID);
printf("cliendid:%s,usrname:%s\n",g_clientId,usrname);
printf("------------>token:%s\n",token);
if ((rc = MQTTAsync_createWithOptions(&s_Client, ADDRESS, g_clientId, MQTTCLIENT_PERSISTENCE_NONE, NULL,&opts)) != MQTTASYNC_SUCCESS)
......
......@@ -158,9 +158,11 @@ static int kk_loginccu_ack(cJSON *arg,int sockfd)
if(arg == NULL){
return -1;
}
char ccuid[32] = {0};
kk_lan_get_ccuid(ccuid);
zkid = cJSON_GetObjectItem(arg, ZKID_STRING);
if(zkid != NULL){
if(strstr(KK_CCU_ID,zkid->valuestring) != NULL){
if(strstr(ccuid,zkid->valuestring) != NULL){
root=cJSON_CreateObject();
if(root){
args = cJSON_CreateObject();
......
......@@ -26,12 +26,14 @@ static int kk_findccu_ack(int sockfd,struct sockaddr_in *addr){
if(addr == NULL || sockfd < 0){
return -1;
}
char ccuid[32] = {0};
kk_lan_get_ccuid(ccuid);
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, "zkid", ccuid);
cJSON_AddStringToObject(args, "zk", KK_CCU_NAME);
HAL_Get_IP(s_IP,NULL);
cJSON_AddStringToObject(args, "ip", s_IP);
......
......@@ -19,10 +19,6 @@
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "com_api.h"
#include "kk_product.h"
#include "kk_findccu_handle.h"
......@@ -31,30 +27,75 @@
//#include "kk_lan_queue.h"
#include "kk_lan_node_db.h"
#include "kk_data_mng.h"
static char s_ccuid[DEVICE_CODE_LEN] = {0};
int kk_lan_get_ccuid(_OU_ char *device_code)
{
strncpy(device_code, s_ccuid, strlen(s_ccuid));
printf("kk_lan_get_ccuid:%s\n",s_ccuid);
return strlen(s_ccuid);
}
static int _setDevice_Code(_IN_ char *device_code,int len)
{
memset(s_ccuid, 0x0, DEVICE_CODE_LEN);
printf("_setDevice_Code:%s\n",device_code);
strncpy(s_ccuid, device_code, len);
return len;
}
static void kk_lan_ccuid_init(void)
{
uint8_t ccuid[DEVICE_CODE_LEN] = {0};
int ccuid_len = 0;
HAL_Execel_cmd(GET_CCUID_CMD,(char *)ccuid,sizeof(ccuid),&ccuid_len);
printf("GET_CCUID_CMD:%s\n",ccuid);
if(ccuid_len > 0 && ccuid_len <= DEVICE_CODE_LEN){
_setDevice_Code(ccuid,ccuid_len-1);
}else{
_setDevice_Code(KK_CCU_ID,strlen(KK_CCU_ID));
}
}
static void sig_handler(int sig)
{
printf("Received signal: %d\n", sig);
abort();
}
extern void ttttt_test();
extern void kk_scene_build_test(void);
int main(int argc, char* argv[])
{
int rc = 0;
char *ppp;
open("kk_lan",LOG_PID,LOG_USER);
kk_scene_build_test();
/*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();
struct sigaction sig = {0};
sig.sa_handler = sig_handler;
sig.sa_flags = 0;
for(int i=0;i<31;i++){
//sigaction(i, &sig, NULL);
}
//lan_queue_init();
kk_lan_db_node_init();
//kk_handle_sync_info();
kk_handle_sync_info();
while(1){
//count++;
//if(count == 10){
//kk_ipc_send(IPC_APP2MID, "wooooooooooooooooooooooooooooooooooollllll!", strlen("wooooooooooooooooooooooooooooooooooollllll!")+1);
//}
sleep(1);
}
return rc;
......
This diff is collapsed.
......@@ -81,10 +81,10 @@ static void watcher_cb (struct ev_loop *loop ,struct ev_io *w, int revents)
//ERROR_PRINT("nn_recv failed with error code %d, %s \n", nn_errno(), nn_strerror(nn_errno ()));
return;
}
//INFO_PRINT("watcher_cb:-------------------------\n");
dat_bak = malloc(bytes+1);
if(dat_bak != NULL){
memset(dat_bak,0x0,bytes);
memset(dat_bak,0x0,bytes+1);
memcpy(dat_bak,dat,bytes);
cJSON_Minify((char *)dat_bak);
INFO_PRINT("watcher_cb:%s\n", (char*)dat_bak);
......@@ -125,7 +125,6 @@ void __loop_init(Bloop_ctrl_t *loop_ctrl, struct ev_loop* loop)
loop_ctrl->watcher.data = loop_ctrl;
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);
}else{
ev_io_init (&(loop_ctrl->watcher), watcher_cb, loop_ctrl->ba.s, EV_READ);
......
#ifndef _LOGDEF_H_
#ifndef _LOGDEF_H_
#define _LOGDEF_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <memory.h>
#include <syslog.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/time.h>
#include <semaphore.h>
#include <errno.h>
#include <assert.h>
#include <net/if.h> // struct ifreq
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/reboot.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
static int g_enable_printf = 1;
#define LOG_STRING(level) ((level == LOG_EMERG) ? "EMERG " : \
(level == LOG_ALERT) ? "ALERT " : \
(level == LOG_CRIT) ? "CRIT " : \
(level == LOG_ERR) ? "ERR " : \
(level == LOG_WARNING) ? "WARNING " : \
(level == LOG_NOTICE) ? "NOTICE " : \
(level == LOG_INFO) ? "INFO " : \
(level == LOG_DEBUG) ? "DEBUG " : "NONE ")
#define KK_LOG(level,fmt,args...) do {\
time_t t; \
t = time(NULL); \
struct tm ptm; \
memset(&ptm, 0, sizeof(ptm)); \
localtime_r(&t, &ptm); \
syslog(level,"%s %4d-%02d-%02d %02d:%02d:%02d [%s:%d]:" fmt ,LOG_STRING(level), \
ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour,ptm.tm_min, ptm.tm_sec,\
__FILE__,__LINE__,##args);\
if(g_enable_printf == 1){\
printf("%s %4d-%02d-%02d %02d:%02d:%02d [%s:%d]:" fmt ,LOG_STRING(level), \
ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour,ptm.tm_min, \
ptm.tm_sec,__FILE__,__LINE__,##args);\
}}while(0)
#define DEBUG_PRINT(info,args...) do { \
syslog(LOG_DEBUG, "[%s][%d]"info,__FUNCTION__,__LINE__ ,##args); \
printf("\n["__FILE__":%d] "info,__LINE__, ##args); \
}while(0)
#define INFO_PRINT(info,args...) do { \
syslog(LOG_INFO, "[%s][%d]"info,__FUNCTION__,__LINE__ , ##args); \
printf("\n["__FILE__":%d] "info,__LINE__, ##args); \
}while(0)
#define ERROR_PRINT(info,args...) do { \
syslog(LOG_ERR, "[%s][%d]"info,__FUNCTION__,__LINE__ , ##args); \
printf("\n["__FILE__":%d] "info,__LINE__, ##args); \
}while(0)
#define WARNING_PRINT(info,args...) do { \
syslog(LOG_WARNING, "[%s][%d]"info,__FUNCTION__,__LINE__ , ##args); \
printf("\n["__FILE__":%d] "info,__LINE__, ##args); \
}while(0)
#define DEBUG_PRINT(fmt, args...) KK_LOG(LOG_DEBUG, fmt, ##args)
#define INFO_PRINT(fmt, args...) KK_LOG(LOG_INFO, fmt, ##args)
#define ERROR_PRINT(fmt, args...) KK_LOG(LOG_ERR, fmt, ##args)
#define WARNING_PRINT(fmt, args...) KK_LOG(LOG_WARNING, fmt, ##args)
#ifdef __cplusplus
}
#endif
#endif
......@@ -32,10 +32,30 @@
#include "com_api.h"
char g_product_type[PRODUCT_TYPE_LEN]={0};
char g_product_code[PRODUCT_CODE_LEN]={0};
char g_device_code[DEVICE_CODE_LEN] = {0};
#define PLATFORM_WAIT_INFINITE (~0)
int HAL_Execel_cmd(char * cmd,char * buf,int buf_len,int* ret_len)
{
if(cmd == NULL || buf == NULL || buf_len == 0)
{
printf("arg error\n");
return -1;
}
memset(buf,0,buf_len);
FILE *fp = NULL;
fp = popen(cmd,"r");
while(fgets(buf,buf_len,fp)!=NULL){
printf("%s return %s",cmd,buf);
}
pclose (fp);
*ret_len = strlen(buf);
return 0;
}
void *HAL_Malloc(_IN_ uint32_t size)
{
return malloc(size);
......@@ -255,20 +275,6 @@ int HAL_SetProduct_Code(_IN_ char *product_code)
}
int HAL_SetDevice_Code(_IN_ char *device_code)
{
int len = strlen(device_code);
if (len > DEVICE_CODE_LEN) {
return -1;
}
memset(g_device_code, 0x0, DEVICE_CODE_LEN);
strncpy(g_device_code, device_code, len);
return len;
}
int HAL_GetProduct_Code(_OU_ char *product_code)
{
strncpy(product_code, KK_CCU_PRODUCTID, strlen(KK_CCU_PRODUCTID));
......@@ -276,12 +282,8 @@ int HAL_GetProduct_Code(_OU_ char *product_code)
return strlen(KK_CCU_PRODUCTID);
}
int HAL_GetDevice_Code(_OU_ char *device_code)
{
strncpy(device_code, KK_CCU_ID, strlen(KK_CCU_ID));
device_code[strlen(KK_CCU_ID)] = '\0';
return strlen(KK_CCU_ID);
}
int HAL_GetVersion(_OU_ char *version)
{
strncpy(version, KK_CCU_VERSION, strlen(KK_CCU_VERSION));
......@@ -747,6 +749,8 @@ uint32_t HAL_Get_IP(char ip_str[NETWORK_ADDR_LEN], const char *ifname)
return ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
}
#if 0
static kv_file_t *kvfile = NULL;
......
......@@ -37,4 +37,9 @@ void HAL_MutexDestroy(_IN_ void *mutex);
int HAL_GetVersion(_OU_ char *version);
int HAL_Get_mac(char* mac);
int HAL_Get_SN(char sn[SN_ADDR_LEN]);
int HAL_Execel_cmd(char * cmd,char * buf,int buf_len,int* ret_len);
int HAL_SetProduct_Type(_IN_ char *product_type);
int HAL_SetProduct_Code(_IN_ char *product_code);
int HAL_GetProduct_Code(_OU_ char *product_code);
#endif
......@@ -37,17 +37,13 @@
#define KK_CCU_RANDOM "0000000000"
#define KK_CCU_NAME "NEW_CCU"
#define KK_DEVICE_MAP_FILE_PATH "/usr/kk/map/device_%s.json"
#define GET_CCUID_CMD "/usr/sbin/fw_printenv uuid -n"
enum {
DEVICE_OFFLINE = 0,
DEVICE_ONLINE,
DEVICE_UNKNOW,
};
int HAL_SetProduct_Type(_IN_ char *product_type);
int HAL_SetProduct_Code(_IN_ char *product_code);
int HAL_SetDevice_Code(_IN_ char *device_code);
int HAL_GetProduct_Code(_OU_ char *product_code);
int HAL_GetDevice_Code(_OU_ char *device_code);
......
......@@ -83,7 +83,7 @@ static int _dm_mgr_search_dev_by_pkdn(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],
}
}
ERROR_PRINT("Device Not Found, Device Code: %s", deviceCode);
ERROR_PRINT("Device Not Found, Device Code: %s\n", deviceCode);
return FAIL_RETURN;
}
static int _dm_mgr_next_devid(void)
......@@ -92,7 +92,7 @@ static int _dm_mgr_next_devid(void)
return ctx->global_devid++;
}
static int _dm_init_tsl_params(kk_tsl_t * dev_shadow)
static int _dm_init_tsl_params(kk_tsl_t * dev_shadow,char *deviceCode)
{
int res = 0;
char version[VERSION_MAXLEN] = {0};
......@@ -150,6 +150,21 @@ static int _dm_init_tsl_params(kk_tsl_t * dev_shadow)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
char currentValue[16] = {0};
kk_property_db_get_value(deviceCode,KK_TSL_CCU_ARMINGSTATE_IDENTIFIER,currentValue);
if(strlen(currentValue) == 0){
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_CCU_ARMINGSTATE_IDENTIFIER,NULL,"1");//默认撤防设置
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
}else{
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_CCU_ARMINGSTATE_IDENTIFIER,NULL,currentValue);//默认撤防设置
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
}
return res;
}
......@@ -282,7 +297,7 @@ int dm_mgr_device_create(_IN_ int dev_type,_IN_ char productCode[PRODUCT_CODE_MA
list_add_tail(&node->linked_list, &ctx->dev_list);
if(dev_type == KK_DM_DEVICE_CCU){
_dm_init_tsl_params(node->dev_shadow);
_dm_init_tsl_params(node->dev_shadow,node->deviceCode);
}
if (devid) {
......@@ -516,6 +531,8 @@ int dm_mgr_init(void)
int res = 0;
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
char mac[DEVICE_MAC_MAXLEN]= {0};
char ccuid[32] = {0};
int devId = 0,heartbeat = 0;
memset(ctx, 0, sizeof(dm_mgr_ctx));
......@@ -527,19 +544,19 @@ int dm_mgr_init(void)
/* Init Device Id*/
ctx->global_devid = 1;
kk_property_db_init();
HAL_Get_ccuid(ccuid);
/* Init Device List */
INIT_LIST_HEAD(&ctx->dev_list);
HAL_Get_mac(mac);
kk_sync_init();
//kk_sync_get_info();
res = dm_mgr_device_create(KK_DM_DEVICE_CCU,KK_DM_CCU_DEVICE_PRODUCT_CODE,KK_CCU_ID,mac,"",KK_DEV_UNKNOW,&devId,&heartbeat);
res = dm_mgr_device_create(KK_DM_DEVICE_CCU,KK_DM_CCU_DEVICE_PRODUCT_CODE,ccuid,mac,"",KK_DEV_UNKNOW,&devId,&heartbeat);
if (res != SUCCESS_RETURN) {
goto ERROR;
}
else{
kk_property_db_update(KK_CCU_ID);
kk_property_db_update(ccuid);
}
......@@ -1090,7 +1107,7 @@ int dm_mgr_upstream_thing_topo_get(void)
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = (char*)DM_URI_THING_TOPO_GET;
HAL_GetDevice_Code(request.deviceCode);
HAL_Get_ccuid(request.deviceCode);
res = _dm_mgr_search_dev_by_pkdn(request.deviceCode, &node);
......
......@@ -41,6 +41,23 @@ typedef struct {
struct list_head dev_list;
} dm_mgr_ctx;
typedef struct kk_dm_property_handle
{
char property[DEVICE_PROPERTY_NAME_MAX];
union{
char *valueStr;
double value;
};
int type;
}kk_dm_property_handle_t;
typedef struct kk_dm_property_e
{
kk_dm_property_handle_t info[DEVICE_PROPERTY_NUM_MAX];
int num;
}kk_dm_property_e_t;
typedef struct {
char msgid[MSG_MAX_LEN];
int devid;
......
......@@ -911,16 +911,40 @@ int dm_msg_ccu_property_post(dm_mgr_dev_node_t *node)
}
return SUCCESS_RETURN;
}
static void kk_split(char *src,const char *separator,char **dest,int *num) {
char *pNext;
int count = 0;
if (src == NULL || strlen(src) == 0)
return;
if (separator == NULL || strlen(separator) == 0)
return;
pNext = strtok(src,separator);
while(pNext != NULL) {
*dest++ = pNext;
++count;
pNext = strtok(NULL,separator);
}
*num = count;
}
int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params)
{
int res = 0;
int nums = 0;
int idx = 0;
int i = 0;
dm_mgr_dev_node_t *node = NULL;
kk_tsl_data_t *property = NULL;
cJSON *propertyItem = NULL;
char tmpValue[20] = {0};
kk_dm_property_e_t propertyInfoBuf[DEVICE_PROPERTY_NUM_MAX];
char propertiesTmp[DEVICE_PROPERTY_NAME_MAX] = {0};
char pFinal[DEVICE_PROPERTY_NAME_MAX] = {0};
int num = 0;
int countPro = 0;
int epNumInt = 1;
int maxEpNum = 1;
if(params == NULL || deviceCode == NULL){
return INVALID_PARAMETER;
}
......@@ -930,13 +954,37 @@ int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params)
return FAIL_RETURN;
}
//printf("dm_msg_thing_property_post_by_identify:%s\n",params->valuestring);
memset(propertyInfoBuf,0x0,sizeof(kk_dm_property_e_t)*16);
nums = node->dev_shadow->property_number;
for(idx = 0; idx < nums; idx++){
property = (kk_tsl_data_t *)(node->dev_shadow->properties + idx);
if(property == NULL){
continue;
}
memset(propertiesTmp,0x0,sizeof(propertiesTmp));
memset(pFinal,0x0,sizeof(pFinal));
memcpy(propertiesTmp,property->identifier,strlen(property->identifier));
//printf("---------------------------------->propertiesTmp:%s\n",propertiesTmp);
if(strstr(propertiesTmp,"_") != NULL){
char *propertiesbuf[DEVICE_PROPERTY_NAME_MAX] = {0};
kk_split(propertiesTmp,"_",propertiesbuf,&num);
if(num == 2){
epNumInt = atoi(propertiesbuf[1]);
}
if(epNumInt < 1 || epNumInt >= 16){
epNumInt = 1;
}
memcpy(pFinal,propertiesbuf[0],strlen(propertiesbuf[0]));
//printf("---------------------------------------->pFinal:%s,num:%d\n",pFinal,epNumInt);
}else{
//printf("---------------------------------->property->identifier:%s\n",property->identifier);
memcpy(pFinal,property->identifier,strlen(property->identifier));
//memset(propertiesbuf[0],0x0,sizeof(propertiesbuf[0]));
//memcpy(propertiesbuf[0],property->identifier,strlen(property->identifier));
//printf("propertiesbuf[0]:%s,num:%d\n",propertiesbuf[0],num);
epNumInt = 1;
}
propertyItem = cJSON_GetObjectItem(params, property->identifier);
if(propertyItem != NULL){
......@@ -946,18 +994,42 @@ int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params)
property->data_value.type == KK_TSL_DATA_TYPE_BOOL){
sprintf(tmpValue,"%d",property->data_value.value_int);
kk_property_db_update_value(deviceCode,property->identifier,tmpValue);
propertyInfoBuf[epNumInt-1].num++;
countPro = propertyInfoBuf[epNumInt-1].num;
propertyInfoBuf[epNumInt-1].info[countPro-1].type = property->data_value.type;
memset(propertyInfoBuf[epNumInt-1].info[countPro-1].property,0x0,sizeof(propertyInfoBuf[epNumInt-1].info[countPro-1].property));
memcpy(propertyInfoBuf[epNumInt-1].info[countPro-1].property,pFinal,strlen(pFinal));
propertyInfoBuf[epNumInt-1].info[countPro-1].value = property->data_value.value_int;
}
else if(property->data_value.type == KK_TSL_DATA_TYPE_FLOAT){
sprintf(tmpValue,"%f",property->data_value.value_float);
kk_property_db_update_value(deviceCode,property->identifier,tmpValue);
propertyInfoBuf[epNumInt-1].num++;
countPro = propertyInfoBuf[epNumInt-1].num;
propertyInfoBuf[epNumInt-1].info[countPro-1].type = property->data_value.type;
memset(propertyInfoBuf[epNumInt-1].info[countPro-1].property,0x0,sizeof(propertyInfoBuf[epNumInt-1].info[countPro-1].property));
memcpy(propertyInfoBuf[epNumInt-1].info[countPro-1].property,pFinal,strlen(pFinal));
propertyInfoBuf[epNumInt-1].info[countPro-1].value = property->data_value.value_float;
}
else if(property->data_value.type == KK_TSL_DATA_TYPE_DOUBLE){
sprintf(tmpValue,"%f",property->data_value.value_double);
kk_property_db_update_value(deviceCode,property->identifier,tmpValue);
propertyInfoBuf[epNumInt-1].num++;
countPro = propertyInfoBuf[epNumInt-1].num;
propertyInfoBuf[epNumInt-1].info[countPro-1].type = property->data_value.type;
memset(propertyInfoBuf[epNumInt-1].info[countPro-1].property,0x0,sizeof(propertyInfoBuf[epNumInt-1].info[countPro-1].property));
memcpy(propertyInfoBuf[epNumInt-1].info[countPro-1].property,pFinal,strlen(pFinal));
propertyInfoBuf[epNumInt-1].info[countPro-1].value = property->data_value.value_double;
}
else if(property->data_value.type == KK_TSL_DATA_TYPE_TEXT||
property->data_value.type == KK_TSL_DATA_TYPE_DATE){
kk_property_db_update_value(deviceCode,property->identifier,property->data_value.value);
propertyInfoBuf[epNumInt-1].num++;
countPro = propertyInfoBuf[epNumInt-1].num;
propertyInfoBuf[epNumInt-1].info[countPro-1].type = property->data_value.type;
memset(propertyInfoBuf[epNumInt-1].info[countPro-1].property,0x0,sizeof(propertyInfoBuf[epNumInt-1].info[countPro-1].property));
memcpy(propertyInfoBuf[epNumInt-1].info[countPro-1].property,pFinal,strlen(pFinal));
propertyInfoBuf[epNumInt-1].info[countPro-1].valueStr = property->data_value.value;
}
else if(property->data_value.type == KK_TSL_DATA_TYPE_STRUCT){
......@@ -991,6 +1063,7 @@ int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params)
current_data->data_value.type == KK_TSL_DATA_TYPE_DATE){
kk_property_db_update_value(deviceCode,tmp_identifiers,current_data->data_value.value);
}
#if 0
else if(current_data->data_value.type == KK_TSL_DATA_TYPE_STRUCT){
kk_tsl_data_value_complex_t *complex_struct_1 = NULL;
complex_struct_1 = current_data->data_value.value;
......@@ -1029,6 +1102,7 @@ int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params)
}
}
#endif
else{
ERROR_PRINT("Unsupport Type\n");
}
......@@ -1039,11 +1113,33 @@ int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params)
else{
ERROR_PRINT("Unkonwn Number Type");
}
//cJSON_DeleteItemFromObject(params,property->identifier);
}
}
int epIdx[4] = {0};
for(idx = 0; idx < epNumInt; idx++){
countPro = propertyInfoBuf[idx].num;
memset(epIdx,0x0,sizeof(epIdx));
sprintf(epIdx,"%d",idx+1);
cJSON * rootData=cJSON_CreateObject();
cJSON_AddStringToObject(rootData,"epNum", epIdx);
for(i = 0; i < countPro; i++){
int vtype = propertyInfoBuf[idx].info[i].type;
if(vtype == KK_TSL_DATA_TYPE_INT || vtype == KK_TSL_DATA_TYPE_ENUM \
||vtype == KK_TSL_DATA_TYPE_BOOL || vtype == KK_TSL_DATA_TYPE_FLOAT \
||vtype == KK_TSL_DATA_TYPE_DOUBLE)
{
cJSON_AddNumberToObject(rootData,propertyInfoBuf[idx].info[i].property, propertyInfoBuf[idx].info[i].value);
}else if(vtype == KK_TSL_DATA_TYPE_TEXT || vtype == KK_TSL_DATA_TYPE_DATE){
cJSON_AddStringToObject(rootData,propertyInfoBuf[idx].info[i].property, propertyInfoBuf[idx].info[i].valueStr);
}
}
char* outstr = cJSON_Print(params);
char* outstr = cJSON_Print(rootData);
dm_mgr_upstream_thing_property_post(node->devid,outstr,strlen(outstr),0);
free(outstr);
cJSON_Delete(rootData);
}
return SUCCESS_RETURN;
}
......
......@@ -930,9 +930,25 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
_iotx_linkkit_upstream_mutex_unlock();
}else if (strstr(typeJson->valuestring,KK_THING_SERVICE_PROPERTY_SET)){
INFO_PRINT("property set \n");
dm_msg_thing_property_set_reply(deviceCode->valuestring,payload_Str, strlen(payload_Str), NULL);
char ccuid[32] = {0};
dm_mgr_dev_node_t *node = NULL;
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
cJSON *state = cJSON_GetObjectItem(paramStr, KK_TSL_CCU_ARMINGSTATE_IDENTIFIER);
if(state != NULL){
dm_mgr_search_dev_by_devid(KK_DM_DEVICE_CCU_DEVICEID,&node);
res = kk_tsl_set_value(kk_tsl_set_property_value,node->dev_shadow,KK_TSL_CCU_ARMINGSTATE_IDENTIFIER,NULL,state->valuestring);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
HAL_Get_ccuid(ccuid);
kk_property_db_update(ccuid);
//dm_msg_thing_property_set_reply(deviceCode->valuestring,payload_Str, strlen(payload_Str), NULL);
dm_msg_ccu_property_post(node);
}
}else if (strstr(typeJson->valuestring,KK_THING_CLOUDSTATE_MSG)){
INFO_PRINT("cloud state notify \n");
char ccuid[32] = {0};
s_CloudStatusRecv = 1;
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
cJSON *state = cJSON_GetObjectItem(paramStr, MSG_IOTClOUDSTATE_STR);
......@@ -951,8 +967,8 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
kk_property_db_update(KK_CCU_ID);
HAL_Get_ccuid(ccuid);
kk_property_db_update(ccuid);
if(s_CloudStatus){
node->isOffline = KK_DEV_ONLINE;
......
......@@ -189,7 +189,12 @@ int kk_property_db_insert(const char *deviceCode,const char *identifier,kk_tsl_d
return SUCCESS_RETURN;
}
_kk_property_db_lock();
//传感器类默认安防等级为离家安防
if(strcmp(identifier,"SensorType") == 0 && devtype == KK_DM_DEVICE_SUBDEV){
sqlCmd = sqlite3_mprintf(insertCmd,deviceCode,identifier,"1",valuetype,devtype);
}else{
sqlCmd = sqlite3_mprintf(insertCmd,deviceCode,identifier,"",valuetype,devtype);
}
rc = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ){
......
......@@ -113,12 +113,12 @@ int kk_history_db_init(void)
/************************************************************
*功能描述:删除历史数据
*输入参数:time:记录时间
*输入参数:itime:记录时间
*输出参数:无
*返 回 值:0:成功;其他:失败
*其他说明:
*************************************************************/
int kk_history_delete_by_recordtime(const char *table,time_t time)
int kk_history_delete_by_recordtime(const char *table,time_t itime)
{
const char *deleteCmd = "delete from '%s' where recordtime = '%d';";
char *sqlCmd = NULL;
......@@ -126,7 +126,7 @@ int kk_history_delete_by_recordtime(const char *table,time_t time)
char *zErrMsg = 0;
kk_history_ctx_t *ctx = _kk_history_get_ctx();
sqlCmd = sqlite3_mprintf(deleteCmd,table,time);
sqlCmd = sqlite3_mprintf(deleteCmd,table,itime);
INFO_PRINT("Table delete data sqlCmd:%s\n",sqlCmd);
rc = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ){
......@@ -179,7 +179,7 @@ err1:
*返 回 值: 0:成功;其他:失败
*其他说明:
*************************************************************/
int kk_history_insert_sensor_info(const char* deviceCode,const char* identifier,const char* valueStr,time_t time)
int kk_history_insert_sensor_info(const char* deviceCode,const char* identifier,const char* valueStr,time_t itime)
{
int res = 0;
kk_history_ctx_t *ctx = _kk_history_get_ctx();
......@@ -194,7 +194,7 @@ int kk_history_insert_sensor_info(const char* deviceCode,const char* identifier,
INFO_PRINT("kk_history_insert_sensor_info,deviceCode:%s,identifier:%s,value:%s\n",deviceCode,identifier,valueStr);
_kk_history_lock();
sqlCmd = sqlite3_mprintf(insertCmd,deviceCode,identifier,valueStr,time);
sqlCmd = sqlite3_mprintf(insertCmd,deviceCode,identifier,valueStr,itime);
res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( res != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
......@@ -235,7 +235,7 @@ int kk_history_insert_sensor_info(const char* deviceCode,const char* identifier,
*返 回 值: 0:成功;其他:失败
*其他说明:
*************************************************************/
int kk_history_insert_Outlet_info(const char* deviceCode,const char* power,const char* metering,time_t time)
int kk_history_insert_Outlet_info(const char* deviceCode,const char* power,const char* metering,time_t itime)
{
int res = 0;
kk_history_ctx_t *ctx = _kk_history_get_ctx();
......@@ -250,7 +250,7 @@ int kk_history_insert_Outlet_info(const char* deviceCode,const char* power,const
INFO_PRINT("kk_history_insert_Outlet_info,deviceCode:%s,power:%s,metering:%s\n",deviceCode,power,metering);
_kk_history_lock();
sqlCmd = sqlite3_mprintf(insertCmd,deviceCode,power,metering,time);
sqlCmd = sqlite3_mprintf(insertCmd,deviceCode,power,metering,itime);
res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( res != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
......
......@@ -15,8 +15,8 @@ enum{
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);
int kk_history_delete_by_recordtime(const char *table,time_t itime);
int kk_history_insert_sensor_info(const char* deviceCode,const char* identifier,const char* valueStr,time_t itime);
int kk_history_insert_Outlet_info(const char* deviceCode,const char* power,const char* metering,time_t itime);
#endif
......@@ -61,6 +61,32 @@ char * g_filerToPlatTable[] =
(char *){KK_THING_SERVICE_NEGATIVE},
(char *){KK_THING_SERVICE_SYNCINFO},
};
static char s_ccuid[DEVICE_CODE_LEN] = {0};
int HAL_Get_ccuid(_OU_ char *device_code)
{
strncpy(device_code, s_ccuid, strlen(s_ccuid));
printf("HAL_Get_ccuid:%s\n",s_ccuid);
return strlen(s_ccuid);
}
static int _setDevice_Code(_IN_ char *device_code,int len)
{
memset(s_ccuid, 0x0, DEVICE_CODE_LEN);
printf("_setDevice_Code:%s\n",device_code);
strncpy(s_ccuid, device_code, len);
return len;
}
static void HAL_Ccuid_init(void)
{
uint8_t ccuid[DEVICE_CODE_LEN] = {0};
int ccuid_len = 0;
HAL_Execel_cmd(GET_CCUID_CMD,(char *)ccuid,sizeof(ccuid),&ccuid_len);
printf("GET_CCUID_CMD:%s\n",ccuid);
if(ccuid_len > 0 && ccuid_len <= DEVICE_CODE_LEN){
_setDevice_Code(ccuid,ccuid_len-1);
}else{
_setDevice_Code(KK_CCU_ID,strlen(KK_CCU_ID));
}
}
/************************************************************
*功能描述:过滤不需要下发给网关的消息
*输入参数:msgtype:消息类型
......@@ -138,7 +164,21 @@ void kk_sendData2gw(void* data, int len, char* chalMark){
kk_print_long_data(data,newLen);
INFO_PRINT("start to send data to gw end\n");
}
static void split(char *src,const char *separator,char **dest,int *num) {
char *pNext;
int count = 0;
if (src == NULL || strlen(src) == 0)
return;
if (separator == NULL || strlen(separator) == 0)
return;
pNext = strtok(src,separator);
while(pNext != NULL) {
*dest++ = pNext;
++count;
pNext = strtok(NULL,separator);
}
*num = count;
}
/************************************************************
*功能描述:属性参数校验
*输入参数:payload:消息内容
......@@ -152,29 +192,44 @@ static int kk_property_set_data_check(cJSON * payload,const char *deviceCode){
dm_mgr_dev_node_t *node = NULL;
kk_tsl_data_t *property = NULL;
cJSON *propertyItem = NULL;
cJSON *epNum = NULL;
int epNumInt = 1;
int num = 0;
int res = 0;
int idx = 0;
int value = 0;
char *propertiesbuf[64] = {0};
char propertiesTmp[64] = {0};
res = dm_mgr_get_device_by_devicecode((char*)deviceCode,&node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
params = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
if(params != NULL){
epNum = cJSON_GetObjectItem(params, "epNum");
if(epNum != NULL){
epNumInt = atoi(epNum->valuestring);
}else{
epNumInt = 1;
}
//printf("----------------->epNumInt:%d\n",epNumInt);
for(idx = 0; idx < node->dev_shadow->property_number; idx++){
property = (kk_tsl_data_t *)(node->dev_shadow->properties + idx);
if(property == NULL){
continue;
}
propertyItem = cJSON_GetObjectItem(params, property->identifier);
//printf("----------------->property->identifier:%s\n",property->identifier);
memcpy(propertiesTmp,property->identifier,strlen(property->identifier));
split(propertiesTmp,"_",propertiesbuf,&num);
if(num == 2){
if(atoi(propertiesbuf[1]) == epNumInt){
//printf("----------------->propertiesbuf[0]:%s\n",propertiesbuf[0]);
propertyItem = cJSON_GetObjectItem(params, propertiesbuf[0]);
if(propertyItem != NULL){
//if(strcmp(property->accessMode,"rw") != 0){
//ERROR_PRINT("ERROR !!!property->accessMode:%s\n",property->accessMode);
//return INVALID_PARAMETER;
//}
//cJSON_AddNumberToObject(params,tmpPS,PowerSwitch->valueint);
//cJSON_DeleteItemFromObject(params, "PowerSwitch");
//printf("----------------->property->data_value.type:%d\n",property->data_value.type);
if(property->data_value.type == KK_TSL_DATA_TYPE_STRUCT){
kk_tsl_data_value_complex_t *complex_struct = NULL;
kk_tsl_data_t *current_data = NULL;
......@@ -234,22 +289,45 @@ static int kk_property_set_data_check(cJSON * payload,const char *deviceCode){
}
}
cJSON_AddItemToObject(params,property->identifier,propertyItem);
cJSON_DeleteItemFromObject(params, propertiesbuf[0]);
}
else if(property->data_value.type == KK_TSL_DATA_TYPE_INT){
else if(property->data_value.type == KK_TSL_DATA_TYPE_INT ||property->data_value.type == KK_TSL_DATA_TYPE_FLOAT \
|| property->data_value.type == KK_TSL_DATA_TYPE_DOUBLE){
value = propertyItem->valueint;
if(value < property->data_value.mix ||value > property->data_value.max ){
ERROR_PRINT("ERROR !!!value:%d,mix:%d,max:%d\n",value,property->data_value.mix,property->data_value.max);
return INVALID_PARAMETER;
}
INFO_PRINT("-----------------KK_TSL_DATA_TYPE_INT-------------------\n");
cJSON_AddNumberToObject(params,property->identifier,propertyItem->valueint);
cJSON_DeleteItemFromObject(params, propertiesbuf[0]);
}
else if(property->data_value.type == KK_TSL_DATA_TYPE_TEXT){
if(strlen(propertyItem->valuestring) > property->data_value.length ){
ERROR_PRINT("ERROR !!!strlen(propertyItem->valuestring):%d,property->data_value.length:%d\n",strlen(propertyItem->valuestring),property->data_value.length );
return INVALID_PARAMETER;
}
INFO_PRINT("-----------------KK_TSL_DATA_TYPE_TEXT-------------------\n");
cJSON_AddStringToObject(params,property->identifier,propertyItem->valuestring);
cJSON_DeleteItemFromObject(params, propertiesbuf[0]);
}
else if(property->data_value.type == KK_TSL_DATA_TYPE_ENUM || property->data_value.type == KK_TSL_DATA_TYPE_BOOL){
INFO_PRINT("-----------------KK_TSL_DATA_TYPE_ENUM-------------------\n");
cJSON_AddNumberToObject(params,property->identifier,propertyItem->valueint);
cJSON_DeleteItemFromObject(params, propertiesbuf[0]);
}
}
}
}
//propertyItem = cJSON_GetObjectItem(params, property->identifier);
//if(propertyItem != NULL){
//if(strcmp(property->accessMode,"rw") != 0){
//ERROR_PRINT("ERROR !!!property->accessMode:%s\n",property->accessMode);
//return INVALID_PARAMETER;
//}
//}
}
}
......@@ -305,6 +383,7 @@ void mid_cb(void* data, int len){
cJSON_Delete(json);
return;
}
}
int devType = 0;
......@@ -331,7 +410,17 @@ void mid_cb(void* data, int len){
cJSON_Delete(json);
return;
}
kk_sendData2gw(data, strlen(data), gw_node->fatherDeviceCode);//send to sub device
cJSON * rootData=cJSON_CreateObject();
char *info_rootS=cJSON_Print(info_root);
char *payloadS = cJSON_Print(payload);
cJSON_AddItemToObject(rootData,MSG_INFO_STR, cJSON_Parse(info_rootS));
cJSON_AddItemToObject(rootData,MSG_PAYLOAD_STR, cJSON_Parse(payloadS));
char *pnewout=cJSON_Print(rootData);
kk_sendData2gw(pnewout, strlen(pnewout), gw_node->fatherDeviceCode);//send to sub device
free(info_rootS);
free(payloadS);
free(pnewout);
cJSON_Delete(rootData);
}
else{
ERROR_PRINT("wrong type\n");
......@@ -457,7 +546,7 @@ void kk_platMsg_handle(void* data, char* chalMark){
//判断网关还是子设备
if (strcmp(info_dcode->valuestring, devCode->valuestring) == 0){
char ccu_deviceCode[DEVICE_CODE_MAXLEN] = {0};
HAL_GetDevice_Code(ccu_deviceCode);
HAL_Get_ccuid(ccu_deviceCode);
kk_mid_subdev_add(KK_DM_DEVICE_GATEWAY,proCode->valuestring,devCode->valuestring, mac->valuestring,ccu_deviceCode);
cJSON * sceneSupportStr = cJSON_GetObjectItem(jsonPay, MSG_SCENE_SUPPORT);
if(sceneSupportStr != NULL&&!strcmp(sceneSupportStr->valuestring,"1")){
......@@ -553,6 +642,7 @@ void kk_platMsg_handle(void* data, char* chalMark){
itemStr = cJSON_GetObjectItem(jsonPay, eventItem->identifier);
itemData = eventItem->output_datas + index;
#if 0
if(itemStr != NULL){
memset(tmpStr,0x0,sizeof(tmpStr));
......@@ -899,7 +989,12 @@ void *udp_dispatch_yield(void *args){
memset(mac, 0, sizeof(mac));
memset(szOut, 0, sizeof(szOut));
HAL_Get_IP(host_ip,NULL);
HAL_GetDevice_Code(device_code);
HAL_Get_ccuid(device_code);
// printf("[%s][%d]inet_ntoa(from.sin_addr):%s\n",__FUNCTION__,__LINE__,inet_ntoa(from.sin_addr));
// printf("[%s][%d]host_ip:%s\n",__FUNCTION__,__LINE__,host_ip);
// if(strcmp(inet_ntoa(from.sin_addr),host_ip) == 0){
sprintf(szOut,"search_kk_ccu_ack|deviceCode=%s;ip=%s;port=%d",device_code,host_ip,16565);
DEBUG_PRINT("szOut:%s\n",szOut);
......@@ -921,6 +1016,7 @@ void *udp_dispatch_yield(void *args){
//{
// DEBUG_PRINT("udp client is not local ip , refused send ack to it\n");
//}
// }
}
}
usleep(100000);
......@@ -1020,7 +1116,7 @@ int main(const int argc, const char **argv)
open("midware",LOG_PID,LOG_USER);
memset(mid_ctx, 0, sizeof(mid_ctx_t));
HAL_Ccuid_init();
kk_sqlite_init();
kk_tsl_api_init();
kk_ipc_init(IPC_MID2APP, mid_cb, NULL, NULL);
......
......@@ -627,7 +627,7 @@ int kk_scene_parse_scene_trigger(const cJSON* str,const char *sceneId)
cJSON *items, *item;
cJSON *type;
cJSON *week;
cJSON *time;
cJSON *timestr;
int res = 0;
int weekflag = 0;
if(str == NULL || sceneId == NULL){
......@@ -653,10 +653,10 @@ int kk_scene_parse_scene_trigger(const cJSON* str,const char *sceneId)
week = cJSON_GetObjectItem(item,MSG_SCENE_WEEK);
if(week == NULL) return FAIL_RETURN;
weekflag = kk_scene_parse_repeatday(week);
time = cJSON_GetObjectItem(item,MSG_SCENE_TIME);
if(time == NULL) return FAIL_RETURN;
timestr = cJSON_GetObjectItem(item,MSG_SCENE_TIME);
if(timestr == NULL) return FAIL_RETURN;
time_t current = HAL_GetTime();
time_t newStart = kk_scene_creat_timer_starttime(weekflag,atoi(time->valuestring),current);
time_t newStart = kk_scene_creat_timer_starttime(weekflag,atoi(timestr->valuestring),current);
kk_scene_insert_scene_timer(newStart,weekflag,sceneId);
kk_scene_push_timer_info(newStart,weekflag,(char*)sceneId);
}
......
......@@ -27,7 +27,7 @@
#define DM_UTILS_UINT64_STRLEN (20)
#define DEVICE_PROPERTY_NAME_MAX (64+1)
#define DEVICE_PROPERTY_VALUE_MAX (64+1)
#define DEVICE_PROPERTY_NUM_MAX (16)
#define KK_TSL_KEY_SCHEMA "schema"
#define KK_TSL_KEY_LINK "link"
......@@ -77,6 +77,7 @@
#define KK_TSL_CCU_WANIP_IDENTIFIER "WanIPAddress"
#define KK_TSL_CCU_WANSTATE_IDENTIFIER "WanState"
#define KK_TSL_CCU_IOTCLOUD_IDENTIFIER "IOTCloudState"
#define KK_TSL_CCU_ARMINGSTATE_IDENTIFIER "ArmingState"
#define KK_TSL_GATAWAY_VERSION_IDENTIFIER "Version"
#define KK_TSL_GATAWAY_IP_IDENTIFIER "IPAddress"
......
......@@ -97,12 +97,9 @@
"identifier": "ArmingState",
"dataType": {
"specs": {
"0": "离家布防",
"1": "撤防",
"2": "在家布防",
"3": "区域布防"
"length": "255"
},
"type": "bool"
"type": "text"
},
"name": "安防状态",
"accessMode": "rw",
......@@ -118,12 +115,9 @@
"identifier": "ArmingState",
"dataType": {
"specs": {
"0": "离家布防",
"1": "撤防",
"2": "在家布防",
"3": "区域布防"
"length": "255"
},
"type": "bool"
"type": "text"
},
"name": "安防状态"
}
......@@ -212,12 +206,9 @@
"identifier": "ArmingState",
"dataType": {
"specs": {
"0": "离家布防",
"1": "撤防",
"2": "在家布防",
"3": "区域布防"
"length": "255"
},
"type": "bool"
"type": "text"
},
"name": "安防状态"
}
......@@ -488,6 +479,16 @@
"type": "text"
},
"name": "版本"
},
{
"identifier": "ArmingState",
"dataType": {
"specs": {
"length": "255"
},
"type": "text"
},
"name": "安防状态"
}
],
"identifier": "property",
......
......@@ -97,12 +97,9 @@
"identifier": "ArmingState",
"dataType": {
"specs": {
"0": "离家布防",
"1": "撤防",
"2": "在家布防",
"3": "区域布防"
"length": "255"
},
"type": "bool"
"type": "text"
},
"name": "安防状态",
"accessMode": "rw",
......@@ -118,12 +115,9 @@
"identifier": "ArmingState",
"dataType": {
"specs": {
"0": "离家布防",
"1": "撤防",
"2": "在家布防",
"3": "区域布防"
"length": "255"
},
"type": "bool"
"type": "text"
},
"name": "安防状态"
}
......@@ -212,12 +206,9 @@
"identifier": "ArmingState",
"dataType": {
"specs": {
"0": "离家布防",
"1": "撤防",
"2": "在家布防",
"3": "区域布防"
"length": "255"
},
"type": "bool"
"type": "text"
},
"name": "安防状态"
}
......@@ -488,6 +479,16 @@
"type": "text"
},
"name": "版本"
},
{
"identifier": "ArmingState",
"dataType": {
"specs": {
"length": "255"
},
"type": "text"
},
"name": "安防状态"
}
],
"identifier": "property",
......
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