Commit 784fe296 authored by whmaizmy's avatar whmaizmy

【修改内容】增加zigbee端物模型处理

【提交人】陈伟灿
parent 47872e3f
......@@ -294,6 +294,8 @@ APPLICATION_FILES= \
./jsonrpc/rpccJSON.c\
./yjq_ezsp.c\
./kk_test.c\
./kk_sub_tsl.c\
./kk_tsl_zigbee_map.c\
./rpc_api/src/rpc_common.c\
./rpc_api/src/rpc_onoff.c\
./rpc_api/src/rpc_global_cmd.c\
......@@ -375,7 +377,7 @@ $(TARGET_FILE): $(APPLICATION_OBJECTS) $(LIBRARIES)
@echo -e '\n$@ build success'
else
$(TARGET_FILE): $(APPLICATION_OBJECTS) $(LIBRARIES)
$(LD) $^ $(LINKER_FLAGS) -lm -L. -static -lapi_com -static -lnanomsg -lanl -pthread -static -lev -ltinfo -o $(TARGET_FILE)
$(LD) $^ $(LINKER_FLAGS) -lm -L. -static -lapi_com -static -lnanomsg -lkk_tsl -lanl -pthread -static -lev -ltinfo -o $(TARGET_FILE)
@echo -e '\n$@ build success'
endif
......
{
"schema":"https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
"profile":{
"productKey":"a1h88DsZIaY"
},
"services":[
{
"outputData":[
],
"identifier":"set",
"inputData":[
{
"identifier":"LightStatus",
"dataType":{
"specs":{
"0":"关闭",
"1":"打开"
},
"type":"bool"
},
"name":"工作状态"
},
{
"identifier":"LightAdjustLevel",
"dataType":{
"specs":{
"unit":"%",
"min":"0",
"max":"100",
"step":"1"
},
"type":"int"
},
"name":"调光等级"
},
{
"identifier":"LightAlias",
"dataType":{
"specs":{
"length":"32"
},
"type":"text"
},
"name":"别名"
}
],
"method":"thing.service.property.set",
"name":"set",
"required":true,
"callType":"async",
"desc":"属性设置"
},
{
"outputData":[
{
"identifier":"LightStatus",
"dataType":{
"specs":{
"0":"关闭",
"1":"打开"
},
"type":"bool"
},
"name":"工作状态"
},
{
"identifier":"LightAdjustLevel",
"dataType":{
"specs":{
"unit":"%",
"min":"0",
"max":"100",
"step":"1"
},
"type":"int"
},
"name":"调光等级"
},
{
"identifier":"LightAlias",
"dataType":{
"specs":{
"length":"32"
},
"type":"text"
},
"name":"别名"
}
],
"identifier":"get",
"inputData":[
"LightStatus",
"LightAdjustLevel",
"LightAlias"
],
"method":"thing.service.property.get",
"name":"get",
"required":true,
"callType":"async",
"desc":"属性获取"
}
],
"properties":[
{
"identifier":"LightStatus",
"dataType":{
"specs":{
"0":"关闭",
"1":"打开"
},
"type":"bool"
},
"name":"工作状态",
"accessMode":"rw",
"required":false
},
{
"identifier":"LightAdjustLevel",
"dataType":{
"specs":{
"unit":"%",
"min":"0",
"max":"100",
"step":"1"
},
"type":"int"
},
"name":"调光等级",
"accessMode":"rw",
"required":false,
"desc":"调光等级采用百分比表示"
},
{
"identifier":"LightAlias",
"dataType":{
"specs":{
"length":"32"
},
"type":"text"
},
"name":"别名",
"accessMode":"rw",
"required":false
}
],
"events":[
{
"outputData":[
{
"identifier":"LightStatus",
"dataType":{
"specs":{
"0":"关闭",
"1":"打开"
},
"type":"bool"
},
"name":"工作状态"
},
{
"identifier":"LightAdjustLevel",
"dataType":{
"specs":{
"unit":"%",
"min":"0",
"max":"100",
"step":"1"
},
"type":"int"
},
"name":"调光等级"
},
{
"identifier":"LightAlias",
"dataType":{
"specs":{
"length":"32"
},
"type":"text"
},
"name":"别名"
}
],
"identifier":"post",
"method":"thing.event.property.post",
"name":"post",
"type":"info",
"required":true,
"desc":"属性上报"
},
{
"outputData":[
{
"identifier":"ErrorCode",
"dataType":{
"specs":{
"0":"恢复正常"
},
"type":"enum"
},
"name":"故障代码"
}
],
"identifier":"Error",
"method":"thing.event.Error.post",
"name":"故障上报",
"type":"alert",
"required":false
},
{
"outputData":[
],
"identifier":"TamperAlarm",
"method":"thing.event.TamperAlarm.post",
"name":"防撬报警",
"type":"alert",
"required":false
}
]
}
#include "kk_sub_tsl.h"
typedef struct {
void *mutex;
int sub_devid;
struct list_head dev_list;
} kk_sub_dev_ctx_t;
static kk_sub_dev_ctx_t g_sub_dev_mgr = {0};
static kk_sub_dev_ctx_t * _sub_dev_get_ctx(void)
{
return &g_sub_dev_mgr;
}
static int _sub_tsl_next_devid(void)
{
kk_sub_dev_ctx_t *ctx = _sub_dev_get_ctx();
return ctx->sub_devid++;
}
static void _sub_dev_mutex_lock(void)
{
kk_sub_dev_ctx_t *ctx = _sub_dev_get_ctx();
if (ctx->mutex) {
kk_MutexLock(ctx->mutex);
}
}
static void _sub_dev_mutex_unlock(void)
{
kk_sub_dev_ctx_t *ctx = _sub_dev_get_ctx();
if (ctx->mutex) {
kk_MutexUnLock(ctx->mutex);
}
}
int kk_tsl_init(void)
{
char *tsl_str = NULL;
int res = 0;
kk_tsl_t *dev_shadow;
sub_dev_node_t *node = NULL;
kk_sub_dev_ctx_t *ctx = _sub_dev_get_ctx();
ctx->mutex = kk_MutexCreate();
if (ctx->mutex == NULL) {
return FAIL_RETURN;
}
ctx->sub_devid = 1;
INIT_LIST_HEAD(&ctx->dev_list);
kk_sub_tsl_add("112233445566","a1h88DsZIaY");
usleep(1000000);
kk_test();
return SUCCESS_RETURN;
}
int kk_sub_tsl_add(const char *device_mac,const char *product_type)
{
sub_dev_node_t *node = NULL;
kk_sub_dev_ctx_t *ctx = _sub_dev_get_ctx();
char name[TSL_PATH_MAXLEN] = {0};
char *tsl_str = NULL;
int res = 0;
if(device_mac == NULL||product_type == NULL)
{
return INVALID_PARAMETER;
}
node = malloc(sizeof(sub_dev_node_t));
if (node == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(node->product_type,0x0,sizeof(node->product_type));
memcpy(node->device_mac, device_mac, strlen(device_mac));
memcpy(node->product_type, product_type, strlen(product_type));
node->devid = _sub_tsl_next_devid();
node->dev_shadow = NULL;
memset(name,0x0,sizeof(name));
sprintf(name,"%s.json",product_type);
tsl_str = kk_load_json(name);
if(tsl_str != NULL)
{
res = kk_tsl_create(tsl_str,strlen(tsl_str),&node->dev_shadow);
free(tsl_str);
if(res != 0){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
}
INIT_LIST_HEAD(&node->linked_list);
list_add_tail(&node->linked_list, &ctx->dev_list);
return SUCCESS_RETURN;
}
int kk_sub_tsl_get_device_by_mac(_IN_ char device_mac[DEVICE_MAC_MAXLEN], _OU_ sub_dev_node_t **node)
{
kk_sub_dev_ctx_t *ctx = _sub_dev_get_ctx();
sub_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, sub_dev_node_t) {
if ((strlen(search_node->device_mac) == strlen(device_mac)) &&
(memcmp(search_node->device_mac, device_mac, strlen(device_mac)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (node) {
*node = search_node;
}
return SUCCESS_RETURN;
}
}
printf("Device Not Found, device_mac: %s\n", device_mac);
return FAIL_RETURN;
}
int kk_sub_tsl_get_Identifier_by_index(_IN_ char device_mac[DEVICE_MAC_MAXLEN], int idx,_OU_ char **identifiers)
{
int res = 0;
sub_dev_node_t *node = NULL;
int index = 0;
kk_tsl_t *dev_shadow = NULL;
int propertiesNum = 0;
kk_tsl_data_t *pProperty = NULL;
char *identifier = NULL;
res = kk_sub_tsl_get_device_by_mac(device_mac, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
dev_shadow = node->dev_shadow;
propertiesNum = dev_shadow->property_number;
if(idx >= propertiesNum){
return FAIL_RETURN;
}
pProperty = (kk_tsl_data_t *)(dev_shadow->properties+idx);
identifier = (char *)malloc(strlen(pProperty->identifier)+1);
memset(identifier,0x0,strlen(pProperty->identifier)+1);
memcpy(identifier,pProperty->identifier,strlen(pProperty->identifier));
*identifiers = identifier;
return SUCCESS_RETURN;
}
int kk_sub_tsl_get_Identifiers_Num(_IN_ char device_mac[DEVICE_MAC_MAXLEN],_OU_ int *num)
{
int res = 0;
sub_dev_node_t *node = NULL;
kk_tsl_t *dev_shadow = NULL;
res = kk_sub_tsl_get_device_by_mac(device_mac, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
dev_shadow = node->dev_shadow;
*num = dev_shadow->property_number;
return SUCCESS_RETURN;
}
const char DM_MSG_REQUEST[] = "{\"LightStatus123\":\"1\"}";
int kk_test(void)
{
int num;
int i = 0;
char *identifier = NULL;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_test_fuc("112233445566",DM_MSG_REQUEST);
return 0;
}
#ifndef _KK_SUB_TSL_H_
#define _KK_SUB_TSL_H_
#include "kk_tsl_common.h"
#include "klist.h"
typedef struct {
int devid;
kk_tsl_t *dev_shadow;
char product_type[PRODUCT_KEY_MAXLEN];
char device_mac[DEVICE_MAC_MAXLEN];
struct list_head linked_list;
} sub_dev_node_t;
#endif
......@@ -13,6 +13,7 @@
cJSON *rpc_Control(jrpc_context * ctx, cJSON * params, cJSON *id,cJSON *mac);
cJSON *rpc_read_attribue(jrpc_context * ctx, cJSON * params, cJSON *id,cJSON *mac);
int lightStatusCtrl(jrpc_context * ctx,const char *mac,void* data);
......
#ifndef __KK_TSL_COM__
#define __KK_TSL_COM__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _IN_
#define _IN_
#endif
#ifndef _OU_
#define _OU_
#endif
#define PRODUCT_KEY_MAXLEN (32 + 1)
#define DEVICE_NAME_MAXLEN (32 + 1)
#define DEVICE_SECRET_MAXLEN (64 + 1)
#define DEVICE_MAC_MAXLEN (16 + 1)
#define DEVICE_SN_MAXLEN (32 + 1)
#define TSL_PATH_MAXLEN (64 + 1)
#define DM_UTILS_UINT16_STRLEN (5)
#define DM_UTILS_UINT32_STRLEN (10)
#define DM_UTILS_UINT64_STRLEN (20)
#define KK_TSL_KEY_SCHEMA "schema"
#define KK_TSL_KEY_LINK "link"
#define KK_TSL_KEY_PROFILE "profile"
#define KK_TSL_KEY_PROPERTIES "properties"
#define KK_TSL_KEY_EVENTS "events"
#define KK_TSL_KEY_SERVICES "services"
#define KK_TSL_KEY_PROFILE_PK "productKey"
#define KK_TSL_KEY_PROFILE_DN "deviceName"
#define KK_TSL_KEY_IDENTIFIER "identifier"
#define KK_TSL_KEY_NAME "name"
#define KK_TSL_KEY_DESC "desc"
#define KK_TSL_KEY_ACCESS_MODE "accessMode"
#define KK_TSL_KEY_REQUIRED "required"
#define KK_TSL_KEY_METHOD "method"
#define KK_TSL_KEY_CALLTYPE "callType"
#define KK_TSL_KEY_OUTPUTDATA "outputData"
#define KK_TSL_KEY_INPUTDATA "inputData"
#define KK_TSL_KEY_DATATYPE "dataType"
#define KK_TSL_KEY_TYPE "type"
#define KK_TSL_KEY_SPECS "specs"
#define KK_TSL_KEY_UNIT "unit"
#define KK_TSL_KEY_UNITNAME "unitName"
#define KK_TSL_KEY_MIN "min"
#define KK_TSL_KEY_MAX "max"
#define KK_TSL_KEY_LENGTH "length"
#define KK_TSL_KEY_SIZE "size"
#define KK_TSL_KEY_ITEM "item"
#define KK_MSG_KEY_DELIMITER '.'
#define KK_URI_SERVICE_DELIMITER '/'
#define KK_URI_OFFSET 0
//Special Service And Event
#define KK_TSL_SPECIAL_SERVICE_SET_IDENTIFIER "set"
#define KK_TSL_SPECIAL_SERVICE_SET_METHOD "thing.service.property.set"
#define KK_TSL_SPECIAL_SERVICE_GET_IDENTIFIER "get"
#define KK_TSL_SPECIAL_SERVICE_GET_METHOD "thing.service.property.get"
#define KK_TSL_SPECIAL_EVENT_POST_IDENTIFIER "post"
#define KK_TSL_SPECIAL_EVENT_POST_METHOD "thing.event.property.post"
#define KK_TSL_GATAWAY_VERSION_IDENTIFIER "Version"
#define KK_TSL_GATAWAY_IP_IDENTIFIER "IPAddress"
#define KK_TSL_GATAWAY_MAC_IDENTIFIER "MACAddress"
#define KK_TSL_GATAWAY_PORT_IDENTIFIER "Port"
#define KK_TSL_GATAWAY_SN_IDENTIFIER "SN"
#define KK_TSL_GATAWAY_WHITELIST_IDENTIFIER "WhiteListState"
typedef enum {
KK_TSL_DATA_TARGET_SERVICE_INPUT_DATA,
KK_TSL_DATA_TARGET_SERVICE_OUTPUT_DATA
} kk_tsl_data_target_e;
typedef enum {
TSL_SERVICE_GET_FAILED = -13,
TSL_SERVICE_SET_FAILED = -12,
TSL_EVENT_GET_FAILED = -11,
TSL_EVENT_SET_FAILED = -10,
TSL_PROPERTY_GET_FAILED = -9,
TSL_PROPERTY_SET_FAILED = -8,
TSL_EVENT_NOT_EXIST = -7,
TSL_PROPERTY_NOT_EXIST = -6,
TSL_SERVICE_NOT_EXIST = -5,
JSON_PARSE_FAILED = -4,
MEMORY_NOT_ENOUGH = -3,
INVALID_PARAMETER = -2,
FAIL_RETURN = -1,
SUCCESS_RETURN = 0,
} kk_tsl_error_code_t;
typedef enum {
KK_TSL_DATA_TYPE_NONE, //none
KK_TSL_DATA_TYPE_INT, //int
KK_TSL_DATA_TYPE_FLOAT, //float
KK_TSL_DATA_TYPE_DOUBLE, //double
KK_TSL_DATA_TYPE_TEXT, //string
KK_TSL_DATA_TYPE_ENUM, //int
KK_TSL_DATA_TYPE_DATE, //string
KK_TSL_DATA_TYPE_BOOL, //bool,0 or 1
KK_TSL_DATA_TYPE_ARRAY, //support int, float, double, text
KK_TSL_DATA_TYPE_STRUCT, //support above 8 data types
} kk_tsl_data_type_e;
typedef struct {
kk_tsl_data_type_e type;
int size;
void *value;
} kk_tsl_data_value_complex_t;
typedef struct {
kk_tsl_data_type_e type;
union {
int value_int;
float value_float;
double value_double;
void *value; //string or complex type accroding to data type
};
} kk_tsl_data_value_t;
typedef struct {
char *identifier;
kk_tsl_data_value_t data_value;
} kk_tsl_data_t;
typedef struct {
char *identifier;
int input_data_number; //input_data Number
kk_tsl_data_t *input_datas; //input_data array, type is dm_shw_data_t
int output_data_number; //ouput_data Number
kk_tsl_data_t *output_datas; //output_data array, type is dm_shw_data_t
} kk_tsl_event_t;
typedef struct {
char *identifier; //synchronized or asynchronized
int input_data_number; //input_data_number
kk_tsl_data_t *input_datas; //input_data array, type is dm_shw_data_t
int output_data_number; //ouput_data Number
kk_tsl_data_t *output_datas; //output_data array, type is dm_shw_data_t
} kk_tsl_service_t;
typedef struct {
int property_number;
kk_tsl_data_t *properties; //property array, type is dm_shw_data_t
int event_number;
kk_tsl_event_t *events; //event array, type is dm_shw_event_t
int service_number;
kk_tsl_service_t *services; //service array, type is dm_shw_service_t
} kk_tsl_t;
#endif
#include "kk_tsl_zigbee_map.h"
#include "kk_test.h"
kk_tsl_zigbee_map_t g_tsl_zigbee_map [] = {
{
"a1h88DsZIaY",
{
{KK_TSL_DATA_TYPE_TEXT,"LightStatus",lightStatusCtrl},
{KK_TSL_DATA_TYPE_TEXT,"LightAdjustLevel",lightStatusCtrl},
{KK_TSL_DATA_TYPE_TEXT,"LightAlias",lightStatusCtrl},
}
},
{
"testtype",
{
{KK_TSL_DATA_TYPE_TEXT,"test1",lightStatusCtrl},
{KK_TSL_DATA_TYPE_TEXT,"test12",lightStatusCtrl},
{KK_TSL_DATA_TYPE_TEXT,"test123",lightStatusCtrl},
{KK_TSL_DATA_TYPE_TEXT,"test1234",lightStatusCtrl},
}
},
};
int kk_find_ctrl_obj(const char *product_type)
{
int map_num = sizeof(g_tsl_zigbee_map) / sizeof(kk_tsl_zigbee_map_t);
int index = 0;
for(index = 0;index < map_num; index++)
{
if(!strcmp(product_type,g_tsl_zigbee_map[index].product_type))
{
return index;
}
}
return -1;
}
#ifndef _KK_ZIGBEE_MAP_H_
#define _KK_ZIGBEE_MAP_H_
#include "RPC_API.h"
#include "kk_tsl_common.h"
typedef int (*zigbee_ctrl_cb)(jrpc_context * ctx,const char *mac,void* data);
typedef struct{
int type;
char* Identity;
zigbee_ctrl_cb zigbee_cb;
}kk_zigbee_ctrl_map_t;
typedef struct{
char *product_type;
kk_zigbee_ctrl_map_t zigbee_ctrl[20];
}kk_tsl_zigbee_map_t;
#endif
This diff is collapsed.
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