rel_1.6.0 init

This commit is contained in:
guocheng.kgc 2020-06-18 20:06:52 +08:00 committed by shengdong.dsd
commit 27b3e2883d
19359 changed files with 8093121 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,199 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __MSDP_H__
#define __MSDP_H__
/******************************************/
#include <stdint.h>
#include <unistd.h>
#include <stdarg.h>
#include "aos/aos.h"
#include "alink_export_internal.h"
#include "alink_protocol.h"
#include "json_parser.h"
//#define __MSDP_UT__ 1
#ifdef __MSDP_UT__
#include "msdp_ut.h"
#endif
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
/******************************************/
#define MODULE_NAME_MSDP "msdp"
#define MAX_PARAMS_LEN 3600
#define MAX_VENDOR_CB_REMALLOC_COUNT 5
#define MAX_UUID_LEN 33
#define MAX_ATTR_NAME_LEN 80
#define MAX_SERVICE_NAME_LEN 80
#define MAX_SERVICE_COUNT 32
#define JSON_KEY_UUID "uuid"
#define JSON_KEY_ATTRSET "attrSet"
#define JSON_KEY_SERVICE "service"
#define JSON_KEY_VALUE "value"
#define JSON_KEY_WHEN "when"
#define JSON_KEY_RESULT "result"
#define JSON_KEY_CODE "code"
#define JSON_KEY_ARGS "args"
#define JSON_KEY_SET "set"
#define JSON_KEY_EXTRA "extra"
#define JSON_KEY_DEVID "devId"
#define REQUEST_SERVICE_STRING_FMT "{\"uuid\":\"%s\",\"service\":\"%s\",\"args\":%s}"
#define RPC_RESULT_STRING_FMT "{\"uuid\":\"%s\",\"service\":\"%s\",\"code\":\"%d\",\"result\":[%s]}"
#define REGISTER_SERVICES_STRING_FMT "[{\"uuid\":\"%s\",\"service\":%s}]"
#define GET_DEVICE_ATTR_STRING_FMT "{\"uuid\":\"%s\",\"attrSet\":%s}"
#define SET_DEVICE_ATTR_STRING_FMT "{\"uuid\":\"%s\",\"attrSet\":%s,%s}"
#define ATTR_NAME_VALUE_STRING_FMT "\"%s\":{\"value\":\"%s\",\"when\":\"%s\"}"
#define ATTR_NAME_OBJECT_STRING_FMT "\"%s\":{\"value\":%s,\"when\":\"%s\"}"
#define POST_ATTR_OBJECT_STRING_FMT "{\"uuid\":\"%s\",\"%s\":{\"value\":%s,\"when\":\"%s\"}}"
#define POST_ATTR_VALUE_STRING_FMT "{\"uuid\":\"%s\",\"%s\":{\"value\":\"%s\",\"when\":\"%s\"}}"
#define METHOD_REGISTER_REMOTE_SERVICE "registerRemoteService"
#define METHOD_GET_DEVICE_STATUS "getDeviceStatus"
#define METHOD_SET_DEVICE_STATUS "setDeviceStatus"
#define METHOD_SET_DEVICE_STATUS_ARRAY "setDeviceStatusArray"
#define METHOD_POST_DEVICE_DATA "postDeviceData"
#define METHOD_POST_DEVICE_DATA_ARRAY "postDeviceDataArray"
#define METHOD_REQUEST_REMOTE_SERVICE "requestRemoteService"
#define METHOD_POST_REMOTE_SERVICE_RSP "postRemoteServieRsp"
/******************************************/
typedef int (*dev_option_cb)(char *params);
typedef int (*get_attr_cb)(char *value_buff, int buff_size);
typedef int (*set_attr_cb)(const char *attr_value);
typedef int (*get_subdev_attr_cb)(const char *mac, char *value_buff,
int buff_size);
typedef int (*set_subdev_attr_cb)(const char *mac, const char *value_buff);
typedef int (*get_dev_attr_cb)(const char *uuid, const char *attr_name,
char **attr_value);
typedef int (*set_dev_attr_cb)(const char *uuid, const char *attr_name,
const char *attr_value);
enum ALINK_COMMAND {
ACB_GET_DEVICE_STATUS = 0,
ACB_SET_DEVICE_STATUS = 1,
ACB_SET_DEVICE_STATUS_ARRAY = 2,
ACB_REQUEST_REMOTE_SERVICE = 3,
ACB_GET_DEVICE_STATUS_BY_RAWDATA = 4,
ACB_SET_DEVICE_STATUS_BY_RAWDATA = 5,
ACB_REQUEST_DEVICE_UPGRADE = 6,
ACB_REQUEST_DEVICE_UNUPGRADE = 7,
MAX_CALLBACK_NUM
};
typedef struct {
dlist_t list_node; //链表节点
char name[MAX_SERVICE_NAME_LEN]; //service方法名称
ALINK_SERVICE_EXECUTE_CB service_cb; //设置属性回调函数
} service_handler_t, *service_handler_ptr;
typedef struct {
dlist_t list_node; //当前属性handler链接节点
char name[MAX_ATTR_NAME_LEN]; //属性名称
void *get_cb; //获取属性回调函数
void *set_cb; //设置属性回调函数
} attr_hanler_t, *attr_hanler_ptr;
typedef struct {
uint8_t dev_type; //设备model
dlist_t list_node; //设备model链表节点(只对subdevice有意义)
dlist_t attr_head; //属性链表头
dlist_t srv_head; //服务链表头
void *mutex_lock; //service和attribute链表读写操作锁
dev_option_cb option_callback[MAX_CALLBACK_NUM];//alink method处理接口
} device_helper_t, *device_helper_ptr;
typedef struct post_node {
dlist_t list_node; /* post_list */
char *params;
} post_node_t;
/******************************************/
int msdp_add_asyncpost_task(char *params);
int msdp_get_all_attrname(const char *uuid, char *attrset, int buff_size);
int msdp_get_all_rpcname(uint8_t dev_type, char *name_array[], int array_size);
int msdp_register_attr_cb(uint8_t dev_type,
const char *name,
void *pf_get,
void *pf_set);
int msdp_register_service_cb(uint8_t dev_type,
const char *name,
void *pf_exec);
int msdp_register_remote_service(char *uuid, const char *service);
void msdp_unregister_service_cb(uint8_t dev_type, const char *name);
int msdp_register_device_helper(uint8_t dev_type,
dev_option_cb callback[MAX_CALLBACK_NUM]);
void msdp_unregister_device_helper(uint8_t dev_type);
int msdp_report_device_status(const char *uuid, const char *attr_name,
const char *attr_value);
int msdp_report_device_attrs(const char *uuid, const char *attrs[]);
int msdp_set_device_status(const char *uuid, const char *attr_name,
const char *attr_value);
int msdp_request_remote_service(const char *uuid, const char *service_name,
const char *args);
int msdp_dispatch_event(const char *uuid, const char *method, char *params);
void msdp_dump_helper();
attr_hanler_ptr msdp_get_attr_handler(const device_helper_ptr helper,
const char *name);
service_handler_ptr msdp_get_service_handler(const device_helper_ptr helper,
const char *name);
device_helper_ptr msdp_get_helper(const char *devid_or_uuid);
int lmns_post_device_data(const char *devid, char *payload);
int lmns_post_remote_service_rsp(const char *devid, char *payload);
int msdp_post_device_data_array(char *params);
int msdp_get_gw_attr(const char *gw_uuid, const char *attr_name,
char *attr_value_buff, int buff_size);
void *msdp_dup_string(const char *src);
void *msdp_dup_buff(const void *buff, unsigned buff_size);
void *msdp_new_buff(unsigned int buff_size);
void msdp_free_buff(void *buff);
int msdp_gw_init();
int msdp_zbnet_init();
int msdp_init();
void msdp_exit();
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View file

@ -0,0 +1,21 @@
NAME := msdp
$(NAME)_TYPE := framework
GLOBAL_INCLUDES += .
$(NAME)_SOURCES := msdp.c msdp_common.c msdp_zigbee.c msdp_gateway.c
ifeq ($(COMPILER),)
$(NAME)_CFLAGS += -Wall -Werror -Wno-unused-variable -Wno-unused-parameter -Wno-implicit-function-declaration
$(NAME)_CFLAGS += -Wno-type-limits -Wno-sign-compare -Wno-pointer-sign -Wno-uninitialized
$(NAME)_CFLAGS += -Wno-return-type -Wno-unused-function -Wno-unused-but-set-variable
$(NAME)_CFLAGS += -Wno-unused-value -Wno-strict-aliasing
else ifeq ($(COMPILER),gcc)
$(NAME)_CFLAGS += -Wall -Werror -Wno-unused-variable -Wno-unused-parameter -Wno-implicit-function-declaration
$(NAME)_CFLAGS += -Wno-type-limits -Wno-sign-compare -Wno-pointer-sign -Wno-uninitialized
$(NAME)_CFLAGS += -Wno-return-type -Wno-unused-function -Wno-unused-but-set-variable
$(NAME)_CFLAGS += -Wno-unused-value -Wno-strict-aliasing
endif
$(NAME)_CFLAGS += -DGATEWAY_SDK

View file

@ -0,0 +1,359 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "aos/aos.h"
#include "json_parser.h"
#include "service.h"
#include "msdp.h"
#define MODULE_NAME MODULE_NAME_MSDP
typedef int (*status_array_cb)(char *entry, int entry_len, int entry_type,
const char *method_name);
typedef int (*get_status_cb)(char *entry, int entry_len, int entry_type,
const char *params, dlist_t *head, void *exec_cb);
typedef int (*set_status_cb)(char *entry, int entry_len, int entry_type,
const char *params, void *exec_cb);
typedef struct st_attribute_info {
dlist_t list_node;
char *attr_name;
char *attr_value;
} attribute_info;
static const char *msdp_get_timestamp_string()
{
static char time_string[32] = {0};
snprintf(time_string, sizeof(time_string) - 1, "%d", (unsigned int)time(NULL));
return time_string;
}
static int msdp_set_attr_array_each_cb(char *params, int str_len,
const char *method_name)//va_list *ap)
{
int uuid_len, ret = SERVICE_RESULT_ERR;
char *str_pos;
char uuid[MAX_UUID_LEN] = {0};
str_pos = json_get_value_by_name(params, str_len, JSON_KEY_UUID, &uuid_len,
NULL);
PTR_RETURN(str_pos, SERVICE_RESULT_ERR, "get value fail, name:%s, params:%s",
JSON_KEY_UUID, params);
strncpy(uuid, str_pos, uuid_len);
char *params_buff = msdp_dup_buff(str_pos, str_len + 1);
PTR_RETURN(params_buff, ret, "pmalloc fail");
params_buff[str_len] = '\0';
log_trace("params = %s\n", params_buff);
/*method转换为setDeviceStatus*/
ret = msdp_dispatch_event(uuid, method_name, params_buff);
RET_LOG(ret, "msdp_dispatch_event, params:%s", params_buff);
msdp_free_buff(params_buff);
return ret;
}
static int msdp_status_array_handler(char *params, void *cb,
const char *method_name)
{
int ret = SERVICE_RESULT_OK;
//遍历设置设备状态数组
char *entry, *pos;
int entry_len, type;
json_array_for_each_entry(params, pos, entry, entry_len, type) {
ret = ((status_array_cb)cb)(entry, entry_len, type, method_name);
RET_LOG(ret, CALL_FUCTION_FAILED, "json_array_for_each_entry");
}
return ret;
}
int msdp_set_attr_each_cb(const char *attr_name, int name_len, int type,
char *params, set_dev_attr_cb exec_cb)//va_list *ap)
{
int str_len, ret = SERVICE_RESULT_ERR;
char *str_pos, *attr_value = NULL;
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
&str_len, NULL);
PTR_RETURN(str_pos, SERVICE_RESULT_ERR, "get uuid fail, params:%s", params);
char uuid[MAX_UUID_LEN] = {0};
strncpy(uuid, str_pos, str_len);
char *name_str = msdp_dup_buff(attr_name, name_len + 1);
PTR_GOTO(name_str, out, CALL_FUCTION_FAILED, "msdp_dup_buff");
name_str[name_len] = '\0';
str_pos = json_get_value_by_name(params, strlen(params), name_str, &str_len, NULL);
PTR_GOTO(str_pos, out, "get %s value fail, params:%s", name_str, params);
int value_len = 0;
char *value_str = NULL;
char *value_pos = json_get_value_by_name(str_pos, str_len, JSON_KEY_VALUE,
&value_len, NULL);
if (NULL != value_pos) {
value_str = msdp_dup_buff(value_pos, value_len + 1);
} else {
value_len = str_len;
value_str = msdp_dup_buff(str_pos, value_len + 1);
}
value_str[value_len] = '\0';
log_trace("attr_name:%s, attr_value:%s", name_str, value_str);
ret = exec_cb(uuid, name_str, value_str);
RET_GOTO(ret, out, CALL_FUCTION_FAILED, "exec_cb");
log_trace("set attibute \"%s\" = \"%s\" success", name_str, value_str);
out:
if (name_str) {
msdp_free_buff(name_str);
}
if (value_str) {
msdp_free_buff(value_str);
}
return ret;
}
int msdp_get_attr_each_cb(const char *attr_name, int name_len, int type,
char *params,
dlist_t *head, get_dev_attr_cb exec_cb)// va_list *ap)
{
int str_len, ret = SERVICE_RESULT_ERR;
attribute_info *pnode = NULL;
char *str_pos, *buff = NULL;
char name_buff[MAX_UUID_LEN] = {0};
strncpy(name_buff, attr_name, name_len);
//log_trace("attr_name:%s, params:%s", name_buff, params);
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
&str_len, NULL);
PTR_RETURN(str_pos, SERVICE_RESULT_ERR, "get uuid fail, params:%s", params);
char uuid[MAX_UUID_LEN] = {0};
strncpy(uuid, str_pos, str_len);
log_trace("uuid:%s, attrname:%s", uuid, name_buff);
ret = exec_cb(uuid, name_buff, &buff);
RET_RETURN(ret, CALL_FUCTION_FAILED, "exec_cb");
log_trace("get attibute \"%s\" handler output = %s", name_buff, buff);
if (head && buff) {
pnode = (attribute_info *)msdp_new_buff( sizeof(attribute_info));
PTR_GOTO(pnode, err, "malloc failed\n");
memset(pnode, 0, sizeof(attribute_info));
//attr name
pnode->attr_name = msdp_dup_buff(attr_name, name_len + 1);
PTR_GOTO(pnode->attr_name, err, "pbufdup failed");
pnode->attr_name[name_len] = '\0';
//attr value
pnode->attr_value = msdp_dup_string(buff);
PTR_GOTO(pnode->attr_value, err, "pstrdup failed");
dlist_add_tail(&pnode->list_node, head);
}
if (buff) {
msdp_free_buff(buff);
}
return SERVICE_RESULT_OK;
err:
if (pnode) {
if (pnode->attr_name) {
msdp_free_buff(pnode->attr_name);
}
if (pnode->attr_value) {
msdp_free_buff(pnode->attr_value);
}
msdp_free_buff((void *)pnode);
}
if (buff) {
msdp_free_buff(buff);
}
return SERVICE_RESULT_ERR;
}
/*
--> [{"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}]
*/
int msdp_set_status_array(char *params)
{
int ret = SERVICE_RESULT_OK;
//遍历各设备,分别设置属性并返会新的属性值
ret = msdp_status_array_handler(params, msdp_set_attr_array_each_cb,
METHOD_SET_DEVICE_STATUS_ARRAY);
RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_status_array_handler");
return ret;
}
int msdp_set_device_status_handler(char *params, void *cb, void *exec_cb)
{
int ret = SERVICE_RESULT_OK;
char *str_pos, *attr_set = NULL;
char uuid[MAX_UUID_LEN] = {0};
int str_len = 0;
attr_set = json_get_value_by_name(params, strlen(params), JSON_KEY_ATTRSET,
&str_len, NULL);
PTR_RETURN(attr_set, SERVICE_RESULT_ERR, "get arrSet fail");
log_trace("attr_set = %s, len:%d", attr_set, str_len);
char *attr_set_dup = msdp_dup_buff(attr_set, str_len + 1);
attr_set_dup[str_len] = '\0';
char *entry, *pos;
int entry_len, type;
json_array_for_each_entry(attr_set_dup, pos, entry, entry_len, type) {
ret = ((set_status_cb)cb)(entry, entry_len, type, params, exec_cb);
RET_LOG(ret, CALL_FUCTION_FAILED, "json_array_for_each_entry");
}
msdp_free_buff(attr_set_dup);
return ret;
}
int msdp_get_device_status_handler(char *params, void *cb, void *exec_cb,
char **json_out)
{
int str_len = 0;
int ret = SERVICE_RESULT_OK;
int uuid_len = 0;
char *str_pos = NULL;
char *report_params = NULL;
attribute_info *pnode;
dlist_t *pnode_next = NULL;
AOS_DLIST_HEAD(head);
char uuid[MAX_UUID_LEN] = {0};
char *entry, *pos;
int entry_len, type;
char *attr_set_dup = NULL;
char *attr_set = json_get_value_by_name(params, strlen(params),
JSON_KEY_ATTRSET, &str_len, NULL);
PTR_RETURN(attr_set, SERVICE_RESULT_ERR, "get arrSet fail");
log_trace("attr_set = %s", attr_set);
/*无需同步返回结果*/
if (NULL == json_out) {
attr_set_dup = msdp_dup_buff(attr_set, str_len + 1);
attr_set_dup[str_len] = '\0';
json_array_for_each_entry(attr_set_dup, pos, entry, entry_len, type) {
ret = ((get_status_cb)cb)(entry, entry_len, type, params, NULL, exec_cb);
RET_LOG(ret, CALL_FUCTION_FAILED, "json_array_for_each_entry");
}
msdp_free_buff(attr_set_dup);
attr_set_dup = NULL;
return ret;
}
attr_set_dup = msdp_dup_buff(attr_set, str_len + 1);
attr_set_dup[str_len] = '\0';
json_array_for_each_entry(attr_set_dup, pos, entry, entry_len, type) {
ret = ((get_status_cb)cb)(entry, entry_len, type, params, &head, exec_cb);
RET_LOG(ret, "get device status fail, params:%s", params);
}
msdp_free_buff(attr_set_dup);
attr_set_dup = NULL;
if (dlist_empty(&head)) {
log_warn("attribute value list is empty, nothing todo");
//输出串为空也返回OK避免未注册get_callback的属性设置失败
ret = SERVICE_RESULT_OK;
//return SERVICE_RESULT_ERR;
goto out;
}
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
&str_len, NULL);
PTR_RETURN(str_pos, SERVICE_RESULT_ERR, "get uuid fail");
strncpy(uuid, str_pos, str_len);
/*{"uuid":"%s"}*/
unsigned int params_length = strlen(uuid) + strlen(JSON_KEY_UUID) + 20;
dlist_for_each_entry(&head, pnode, attribute_info, list_node) {
/*计算单个属性字符串长度:"attr_name":{"value":%s,"when":""}*/
params_length += strlen(pnode->attr_name) + strlen(pnode->attr_value) + 30;
}
report_params = msdp_new_buff( params_length);
PTR_GOTO(report_params, out, "pmalloc fail");
memset(report_params, 0, params_length);
int len = snprintf(report_params, params_length, "{\"uuid\":\"%s\"", uuid);
dlist_for_each_entry_safe(&head, pnode_next, pnode, attribute_info, list_node) {
if (pnode->attr_value[0] != '{' && pnode->attr_value[0] != '[')
len += snprintf(report_params + len, params_length - len,
",\"%s\":{\"value\":\"%s\"}",
pnode->attr_name, pnode->attr_value);
else
len += snprintf(report_params + len, params_length - len,
",\"%s\":{\"value\":%s}",
pnode->attr_name, pnode->attr_value);
if (len == params_length) {
goto out;
log_error("memory overflow");
}
/*移除节点*/
dlist_del(&pnode->list_node);
msdp_free_buff(pnode->attr_name);
msdp_free_buff(pnode->attr_value);
msdp_free_buff((void *)pnode);
}
report_params[len++] = '}';
//#ifdef CONFIG_METHOD_POSTDEVICEDATAARRAY
memmove(report_params + 1, report_params, len);
report_params[0] = '[';
report_params[len + 1] = ']';
//#endif
if (json_out) {
*json_out = report_params;
report_params = NULL;
}
ret = SERVICE_RESULT_OK;
log_trace("json_out = %s", json_out ? *json_out : "NULL");
out:
dlist_for_each_entry_safe(&head, pnode_next, pnode, attribute_info, list_node) {
dlist_del(&pnode->list_node);
msdp_free_buff(pnode->attr_name);
msdp_free_buff(pnode->attr_value);
msdp_free_buff((void *)pnode);
}
if (report_params) {
msdp_free_buff(report_params);
}
return ret;
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __MSDP_COMMON_H__
#define __MSDP_COMMON_H__
#include <stdint.h>
#include <unistd.h>
#include <stdarg.h>
#include "msdp.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
int msdp_set_attr_each_cb(const char *attr_name, int name_len, int type,
va_list *ap);
int msdp_get_attr_each_cb(const char *attr_name, int name_len, int type,
va_list *ap);
int msdp_set_status_array(char *params);
int msdp_set_device_status_handler(char *params, void *cb, void *exec_cb);
int msdp_get_device_status_handler(char *params, void *cb, void *exec_cb,
char **json_out);
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View file

@ -0,0 +1,386 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "aos/aos.h"
#include "msdp_common.h"
#include "json_parser.h"
#include "service.h"
#include "devmgr.h"
#include "msdp.h"
#define MODULE_NAME MODULE_NAME_MSDP
static int __get_attribute(const char *uuid, const char *attr_name,
char **ppbuf)
{
int ret = SERVICE_RESULT_ERR;
attr_hanler_ptr attr_handler = NULL;
int buff_size, count = 0;
log_trace("uuid = %s, attr_name = %s", uuid, attr_name);
*ppbuf = NULL;
buff_size = MAX_PARAMS_LEN / (1 << MAX_VENDOR_CB_REMALLOC_COUNT);
device_helper_ptr helper = NULL;
helper = msdp_get_helper(uuid);
PTR_RETURN(helper, ret, "no exist model helper, uuid = %s", uuid)
os_mutex_lock((helper->mutex_lock));
attr_handler = msdp_get_attr_handler(helper, attr_name);
if (attr_handler) {
log_debug("exist attribute(%s) handler", attr_name);
void *get_cb = NULL;
get_cb = attr_handler->get_cb;
PTR_GOTO(get_cb, unlock_out, "attribute %s's get_cb is NULL", attr_name);
while (count < MAX_VENDOR_CB_REMALLOC_COUNT) {
buff_size = MAX_PARAMS_LEN / (1 << (MAX_VENDOR_CB_REMALLOC_COUNT - count));
*ppbuf = msdp_new_buff( buff_size);
PTR_GOTO(*ppbuf, unlock_out, "malloc failed");
memset(*ppbuf, 0, buff_size);
if (helper->dev_type == DEV_TYPE_GATEWAY) {
ret = ((get_attr_cb)attr_handler->get_cb)(*ppbuf, buff_size);
} else {
// modify by wukong 2017-4-17
char mac[ETHER_ADDR_BYTES] = {0};
get_mac_by_uuid(uuid, mac);
//modify by wukong end 2017-4-17
ret = ((get_subdev_attr_cb)attr_handler->get_cb)(mac, *ppbuf, buff_size);
}
if (ret != SERVICE_BUFFER_INSUFFICENT) {
break;
}
log_error("buff too short,buff_size = %d, remalloc", buff_size);
msdp_free_buff(*ppbuf);
*ppbuf = NULL;
count++;
}
//检查attribute调用返回值
RET_GOTO(ret, unlock_out, "get attibute \"%s\" handler faild", attr_name);
if (NULL == *ppbuf) { // || strlen(*ppbuf) == 0)
log_error("get attibute \"%s\" handler error, output_len == 0", attr_name);
ret = SERVICE_RESULT_ERR;
goto unlock_out;
}
log_debug("get attibute \"%s\" handler output = %s", attr_name, *ppbuf);
} else {
log_error("unsupport attribute \"%s\"", attr_name);
}
os_mutex_unlock((helper->mutex_lock));
return ret;
unlock_out:
os_mutex_unlock((helper->mutex_lock));
if (*ppbuf) {
msdp_free_buff(*ppbuf);
*ppbuf = NULL;
}
return ret;
}
static int __set_attribute(const char *uuid, const char *attr_name,
char *attr_value)
{
int ret = SERVICE_RESULT_ERR;
const char *mac = NULL;
attr_hanler_ptr attr_handler = NULL;
log_trace("uuid = %s, attr_name = %s, attr_value = %s", uuid, attr_name,
attr_value);
device_helper_ptr helper = NULL;
helper = msdp_get_helper(uuid);
PTR_RETURN(helper, ret, "no exist model helper, uuid = %s", uuid)
os_mutex_lock(helper->mutex_lock);
attr_handler = msdp_get_attr_handler(helper, attr_name);
if (attr_handler) {
log_trace("Exist attribute(%s) handler on devtype:%02x", attr_name,
helper->dev_type);
if (helper->dev_type == DEV_TYPE_GATEWAY) {
PTR_GOTO((attr_handler)->set_cb,
unlock_out, "attribute %s's get_cb is NULL", attr_name);
ret = ((set_attr_cb)attr_handler->set_cb)(attr_value);
log_trace("set attibute \"%s\" handler ret = %d", attr_name, ret);
} else {
// modify by wukong 2017-4-17
char mac[ETHER_ADDR_BYTES] = {0};
get_mac_by_uuid(uuid, mac);
//modify by wukong end 2017-4-17
PTR_GOTO((attr_handler->set_cb),
unlock_out, "subdevice attribute %s's set_cb is NULL", attr_name);
ret = ((set_subdev_attr_cb)attr_handler->set_cb)(mac, attr_value);
log_trace("set attibute \"%s\" handler ret = %d\n", attr_name, ret);
}
} else {
log_error("unsupport attribute \"%s\"", attr_name);
}
unlock_out:
os_mutex_unlock((helper->mutex_lock));
return ret;
}
#define RPC_RESULT_FMT "{\"set\":%s,\"when\":\"%s\",\"extra\":\"\"}"
static int msdp_rpc_handler(const device_helper_ptr helper,
const char *rpc_name,
const char *rpc_args)
{
int ret = SERVICE_RESULT_ERR;
service_handler_ptr phandler = NULL;
int buff_size, count = 0;
char *rpc_result = NULL;
os_mutex_lock((helper->mutex_lock));
phandler = msdp_get_service_handler(helper, rpc_name);
if (phandler) {
log_trace("Exist service(%s) handler", rpc_name);
ret = phandler->service_cb((char *)rpc_args, NULL, 0);
RET_LOG(ret, "excute rpc fail, rpcname:%s", rpc_name);
} else {
log_error("unsupport service \"%s\"", rpc_name);
}
os_mutex_unlock((helper->mutex_lock));
return ret;
}
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState", "lightSwitchState"]}
<-- {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}
*/
static int msdp_get_status(char *params)
{
int ret = SERVICE_RESULT_ERR;
char *attrset;
char *json_out = NULL;
char params_in[512] = {0};
int attrset_size, attrset_len = 0;
//char szPostString[1024] = "{\"uuid\":\"2427287C75CD579897D3DB5854E46A2F\",\"wlanSwitchState\":{\"value\":\"1\",\"when\":\"1404443369\"}}";
attrset = json_get_value_by_name(params, strlen(params), JSON_KEY_ATTRSET,
&attrset_len, NULL);
if (attrset) {
attrset_size = json_get_array_size(attrset, attrset_len);
}
if (NULL == attrset || attrset_size == 0) {
char uuid[MAX_UUID_LEN] = {0};
int uuid_len = 0;
char *uuid_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
&uuid_len, NULL);
PTR_RETURN(uuid_ptr, SERVICE_RESULT_ERR, CALL_FUCTION_FAILED,
"json_get_value_by_name(uuid)");
strncpy(uuid, uuid_ptr, sizeof(uuid) > uuid_len ? uuid_len : sizeof(uuid) - 1);
char attrset[512] = {0};
ret = msdp_get_all_attrname(uuid, attrset, sizeof(attrset));
RET_RETURN(ret, "get all attribute name fail, uuid:%s", uuid);
log_trace("attrset = %s\n", attrset);
snprintf(params_in, sizeof(params_in) - 1, GET_DEVICE_ATTR_STRING_FMT, uuid,
attrset);
params = params_in;
}
//遍历attrset并调用厂商注册的接口返回属性json串
ret = msdp_get_device_status_handler(params, msdp_get_attr_each_cb,
__get_attribute, &json_out);
RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_status_handler");
log_trace("json_out = %s", json_out ? (json_out) : "NULL");
//todo: postDeviceData/deviceDataChange
if (json_out) {
int str_len, post_ret = SERVICE_RESULT_ERR;
char devid_str[18] = {0};
char *dev_id = json_get_value_by_name(params, strlen(params), JSON_KEY_DEVID,
&str_len, NULL);
if (dev_id != NULL) {
post_ret = lmns_post_device_data(devid_str, json_out);
RET_GOTO(post_ret, end, CALL_FUCTION_FAILED, "lmns_post_device_data");
} else {
int post_ret = msdp_add_asyncpost_task(json_out);
RET_GOTO(post_ret, end, "add async post task fail, params:%s", json_out);
json_out = NULL;
}
}
end:
if (json_out) {
msdp_free_buff(json_out);
}
return ret;
}
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}
*/
static int msdp_set_status(char *params)
{
int ret = SERVICE_RESULT_OK;
char *json_out = NULL;
//log_trace("json_in = %s\n", json_in);
//遍历attrset设置属性值
ret = msdp_set_device_status_handler(params, msdp_set_attr_each_cb,
__set_attribute);
RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_set_attr_each_cb");
//遍历attrset获取属性值
ret = msdp_get_device_status_handler(params, msdp_get_attr_each_cb,
__get_attribute, &json_out);
RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_get_attr_each_cb");
log_trace("json_out = %s", json_out ? (json_out) : "NULL");
//todo: postDeviceData/deviceDataChange
if (json_out) {
int str_len, post_ret = SERVICE_RESULT_ERR;
char *dev_id = json_get_value_by_name(params, strlen(params), JSON_KEY_DEVID,
&str_len, NULL);
if (dev_id != NULL) {
char devid_str[18] = {0};
strncpy(devid_str, dev_id, str_len);
post_ret = lmns_post_device_data(devid_str, json_out);
RET_GOTO(post_ret, end, CALL_FUCTION_FAILED, "lmns_post_device_data");
} else {
int post_ret = msdp_add_asyncpost_task(json_out);
RET_GOTO(post_ret, end, "add async post task fail, params:%s", json_out);
json_out = NULL;
}
}
end:
if (json_out) {
msdp_free_buff(json_out);
}
return ret;
}
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","service\":"startBwCheck","args":{}}
<-- {"uuid": "42E6E69DAEF6483FBBA412A28AF7CD76", "service": "stopBwCheck", "code": "1", "result": [ ] }
*/
static int msdp_rpc(char *params)
{
int ret = SERVICE_RESULT_ERR;
char *json_ptr, *rpc_name, *rpc_args;
json_ptr = rpc_name = rpc_args = NULL;
int len = 0;
log_trace("params = %s", params);
json_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_SERVICE,
&len, NULL);
PTR_GOTO(json_ptr, end, CALL_FUCTION_FAILED, "json_get_value_by_name");
rpc_name = msdp_dup_buff(json_ptr, len + 1);
PTR_GOTO(rpc_name, end, "pmalloc fail");
rpc_name[len] = '\0';
json_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_ARGS, &len,
NULL);
PTR_GOTO(json_ptr, end, CALL_FUCTION_FAILED, "json_get_value_by_name");
rpc_args = msdp_dup_buff(json_ptr, len + 1);
PTR_GOTO(rpc_args, end, "pmalloc fail");
rpc_args[len] = '\0';
char uuid[MAX_UUID_LEN] = {0};
json_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID, &len,
NULL);
PTR_GOTO(json_ptr, end, CALL_FUCTION_FAILED, "json_get_value_by_name");
strncpy(uuid, json_ptr, len);
device_helper_ptr helper = msdp_get_helper(uuid);
PTR_GOTO(helper, end, "no exist model helper, uuid = %s", uuid);
ret = msdp_rpc_handler(helper, rpc_name, rpc_args);
RET_GOTO(ret, end, CALL_FUCTION_FAILED, "msdp_service_handler");
ret = SERVICE_RESULT_OK;
end:
if (rpc_name) {
msdp_free_buff(rpc_name);
}
if (rpc_args) {
msdp_free_buff(rpc_args);
}
return ret;
}
int msdp_get_gw_attr(const char *gw_uuid, const char *attr_name,
char *attr_value_buff, int buff_size)
{
int ret = SERVICE_RESULT_ERR;
char *output = NULL;
ret = __get_attribute(gw_uuid, attr_name, &output);
RET_RETURN(ret, "get attr fail, attr_name:%s", attr_name);
if (output != NULL) {
if (strlen(output) >= buff_size) {
ret = SERVICE_RESULT_ERR;
log_error("the buffer size is too small");
goto out;
}
strncpy(attr_value_buff, output, buff_size - 1);
} else {
ret = SERVICE_RESULT_ERR;
}
out:
if (output) {
msdp_free_buff(output);
}
return ret;
}
int msdp_gw_init()
{
dev_option_cb callback[MAX_CALLBACK_NUM] = {NULL};
/*默认cmd callback*/
callback[ACB_GET_DEVICE_STATUS] = msdp_get_status;
callback[ACB_SET_DEVICE_STATUS] = msdp_set_status;
callback[ACB_SET_DEVICE_STATUS_ARRAY] = msdp_set_status_array;
callback[ACB_REQUEST_REMOTE_SERVICE] = msdp_rpc;
/*注册网关设备事件处理接口*/
return msdp_register_device_helper(DEV_TYPE_GATEWAY, callback);
#ifdef ROUTER_SDK
/*注册WIFI下联子设备model*/
return msdp_register_device_helper(DEV_TYPE_WIFI_SUB, callback);
#endif
return 0;
}

View file

@ -0,0 +1,204 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <aos/errno.h>
#include "msdp_ut.h"
#include "msdp.h"
#define GW_UUID "123412412312312312312sdfsdgdgdew"
#define ZBDEV_UUID "5465465465465464865465asdfa12312"
#define ATTR1_NAME "attr1"
#define ATTR2_NAME "attr2"
//static char attr1_value[32] = "attrvalue1";
//static char attr2_value[32] = "attrvalue2";
static char attr1_value[128] = "{\"element\":\"value1\"}";
static char attr2_value[128] = "[\"element1\",\"element2\"]";
void dump_all_type_handler();
int stdd_zbnet_supervision_interval_cb(uint8_t ieee_addr[8], uint8_t interval)
{
return 0;
}
int devmgr_read_attr_cache(const char *devid_or_uuid, const char *attr_name,
char **attr_value)
{
log_trace("read attr cache, devid:%s, attr_name:%s", devid_or_uuid, attr_name);
char *value_str = NULL;
if (strcmp(attr_name, ATTR1_NAME) == 0) {
value_str = (char *)attr1_value;
} else if (strcmp(attr_name, ATTR2_NAME) == 0) {
value_str = (char *)attr2_value;
} else {
return -1;
}
*attr_value = malloc(strlen(value_str) + 1);
memset(*attr_value, 0, strlen(value_str) + 1);
strcpy(*attr_value, value_str);
return 0;
}
int devmgr_get_dev_type(const char *devid_or_uuid, uint8_t *dev_type)
{
*dev_type = DEV_TYPE_ZIGBEE;
return 0;
}
int32_t alink_post(const char *method, char *buff)
{
log_trace("alink post, mehtod:%s, buff:%s", method, buff);
return 0;
}
int stdd_zbnet_get_attr(const char *devid_or_uuid, const char *attr_name)
{
log_trace("get attibute, uuid:%s, attrname:%s", devid_or_uuid, attr_name);
return 0;
}
int stdd_zbnet_set_attr(const char *devid_or_uuid, const char *attr_name,
const char *attr_value)
{
log_trace("set attribute, uuid:%s, attrname:%s, attrvalue:%s", devid_or_uuid,
attr_name, attr_value);
if (strcmp(attr_name, ATTR1_NAME) == 0) {
strcpy(attr1_value, attr_value);
} else if (strcmp(attr_name, ATTR2_NAME) == 0) {
strcpy(attr2_value, attr_value);
} else {
return -1;
}
return 0;
}
int stdd_zbnet_exec_rpc(const char *devid_or_uuid, const char *rpc_name,
const char *rpc_args)
{
log_trace("exec_rpc success, uuid:%s", devid_or_uuid);
return 0;
}
int stdd_get_device_attrset(const char *devid_or_uuid, char *attrset_buff,
int buff_size)
{
log_trace("get device attrset, uuid:%s", devid_or_uuid);
snprintf(attrset_buff, buff_size, "[\"attr1\",\"attr2\"]");
return 0;
}
const char *config_get_main_uuid(void)
{
return GW_UUID;
}
int gw_get_attr1_cb(char *attr_value_buff, int buff_size)
{
log_trace("get attr1, value:%s", attr1_value);
strcpy(attr_value_buff, attr1_value);
return 0;
}
int gw_set_attr1_cb(char *attr_value)
{
log_trace("set attr1, value:%s", attr_value);
strcpy(attr1_value, attr_value);
return 0;
}
int gw_get_attr2_cb(char *attr_value_buff, int buff_size)
{
log_trace("get attr2, value:%s", attr2_value);
strcpy(attr_value_buff, attr2_value);
return 0;
}
int gw_set_attr2_cb(char *attr_value)
{
log_trace("set attr2, value:%s", attr_value);
strcpy(attr2_value, attr_value);
}
int gw_rpc_reboot_cb(char *args, char *rpc_result, int buff_size)
{
log_trace("rpc reboot, args:%s", args);
return 0;
}
void dev_attr_init()
{
msdp_register_attr_cb(DEV_TYPE_GATEWAY, ATTR1_NAME, gw_get_attr1_cb,
gw_set_attr1_cb);
msdp_register_attr_cb(DEV_TYPE_GATEWAY, ATTR2_NAME, gw_get_attr2_cb,
gw_set_attr2_cb);
msdp_register_service_cb(DEV_TYPE_GATEWAY, "reboot", gw_rpc_reboot_cb);
}
int get_attr_ut()
{
int ret = SERVICE_RESULT_ERR;
char params[1024] = {0};
sprintf(params, GET_DEVICE_ATTR_STRING_FMT, GW_UUID, "[]");
log_trace("params:%s", params);
ret = msdp_dispatch_event(GW_UUID, METHOD_GET_DEVICE_STATUS, params);
RET_RETURN(ret, "get device status fail, params:%s", params);
log_trace("get device status success\n");
return ret;
}
int set_attr_ut()
{
int ret = SERVICE_RESULT_ERR;
char params[1024] = {0};
sprintf(params, SET_DEVICE_ATTR_STRING_FMT, GW_UUID, "[\"attr1\",\"attr2\"]",
"\"attr1\":{\"value\":[\"element1\",\"element2\"]},\"attr2\":{\"value\":{\"element\":\"elementvalue\"}}");
log_trace("params:%s", params);
ret = msdp_dispatch_event(GW_UUID, METHOD_SET_DEVICE_STATUS, params);
RET_RETURN(ret, "set device status fail, params:%s", params);
ret = msdp_set_device_status(ZBDEV_UUID, ATTR1_NAME, "attr_value");
RET_RETURN(ret, "set device status fail");
ret = msdp_request_remote_service(ZBDEV_UUID, "reboot", "{\"args\":\"test\"");
RET_RETURN(ret, "request remote service fail");
log_trace("set device status success, params:%s\n", params);
return ret;
}
int main(int argc, char *argv[])
{
int ret = msdp_init();
RET_RETURN(ret, "msdp_init fail");
dev_attr_init();
dump_all_type_handler();
//get_attr_ut();
set_attr_ut();
//get_attr_ut();
return 0;
}

View file

@ -0,0 +1,227 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __MSDP_UT__H__
#define __MSDP_UT__H__
#include <stdint.h>
#include <unistd.h>
#ifdef __cplusplus
extern "C" {
#endif
#define os_mutex_init() \
((void *)0x010101010)
#define os_mutex_lock(mutex)
#define os_mutex_unlock(mutex)
#define os_mutex_destroy(mutex) (mutex = NULL)
#define CALL_FUCTION_FAILED "Call function \"%s\" failed\n"
#define MAX_UUID_LEN 33
#define MAX_ATTR_NAME_LEN 80
#define MAX_SERVICE_NAME_LEN 80
typedef int (*ALINK_SERVICE_EXECUTE_CB)(const char *args,
char *json_out_buf, unsigned int buf_sz);
#define log_print(fmt, args...) \
do { \
printf("[MSDP] %s:%4d %20s: " fmt "\n", __FILE__, __LINE__, __func__, ##args); \
} while(0)
#define log_fatal(FMT, args...) log_print(FMT, ##args)
#define log_error(FMT, args...) log_print(FMT, ##args)
#define log_info(FMT, args...) log_print(FMT, ##args)
#define log_dump(FMT, args...) log_print(FMT, ##args)
#define log_debug(FMT, args...) log_print(FMT, ##args)
#define log_trace(FMT, args...) log_print(FMT, ##args)
#define RET_FAILED(ret) (ret != SERVICE_RESULT_OK)
#define RET_GOTO(Ret,gotoTag,strError, args...) \
{\
if ( RET_FAILED(Ret) ) \
{ \
log_trace(strError, ##args); \
goto gotoTag; \
}\
}
#define RET_FALSE(Ret,strError,args...) \
{\
if ( RET_FAILED(Ret) ) \
{ \
log_trace(strError, ##args); \
return false; \
}\
}
#define RET_RETURN(Ret,strError,args...) \
{\
if ( RET_FAILED(Ret) ) \
{ \
log_trace(strError, ##args); \
return Ret; \
}\
}
#define RET_LOG(Ret,strError,args...) \
{\
if ( RET_FAILED(Ret) ) \
{ \
log_error(strError, ##args); \
}\
}
#define PTR_RETURN(Pointer,Ret,strError,args...) \
{\
if ( !Pointer) \
{ \
log_trace(strError, ##args); \
return Ret; \
}\
}
#define PTR_FALSE(Pointer,strError,args...) \
{\
if ( !Pointer) \
{ \
log_trace(strError, ##args); \
return FALSE; \
}\
}
#define PTR_LOG(Pointer,strError,args...) \
{\
if ( !Pointer) \
{ \
log_error(strError, ##args); \
}\
}
#define PTR_GOTO(Pointer, gotoTag, strError, args...) \
{\
if ( !Pointer) \
{ \
log_trace(strError, ##args); \
goto gotoTag; \
}\
}
#define POINTER_RETURN(Pointer,strError,args...) \
{\
if ( !Pointer) \
{ \
log_trace(strError, ##args); \
return Pointer; \
}\
}
enum SERVICE_CODE {
SERVICE_BUFFER_INSUFFICENT = -3,
SERVICE_CODE_UNKNOWN = -2,
SERVICE_RESULT_ERR = -1,
SERVICE_RESULT_OK = 0,
SERVICE_CODE_BEGIN = 0x0100,
SERVICE_EVENT,
SERVICE_DATA,
SERVICE_CONN_INIT,
SERVICE_CONN_READY,
SERVICE_CONN_CLOSE,
SERVICE_ATTACH,
SERVICE_DETACH,
SERVICE_STATE_INIT,
SERVICE_STATE_PREPARE,
SERVICE_STATE_READY,
SERVICE_STATE_BUSY,
SERVICE_STATE_STOP,
SERVICE_CODE_END = 0x01FF,
};
/**
* command argument
*/
typedef struct cmd_argument {
char arg_name[32];
unsigned char data_type;
struct cmd_argument *next_arg;
} cmd_argument_t;
/**
* attr element
*/
typedef struct attr_element {
struct attr_element *next_element;
unsigned char data_type;/**< defined by zigbee protocol */
char element_name[32];
struct attr_element *child_element;
} attr_element_t;
/**
* alink attribute profile description
*/
typedef struct attr_profile {
char attr_name[32];
unsigned char data_type;
unsigned short cluster_id;
unsigned short attribute_id;
attr_element_t *attr_element;
unsigned char reserved[4];
} attr_profile_t;
/**
* alink command profile description
*/
typedef struct cmd_profile {
char cmd_name[32];
unsigned short cluster_id;
unsigned char cmd_id;
unsigned char direction; /**< 0:c to s; 1:s to c */
cmd_argument_t *args;
unsigned char reserved[4];
} cmd_profile_t;
int devmgr_get_dev_type(const char *devid_or_uuid, uint8_t *dev_type);
int32_t alink_post(const char *method, char *buff);
int devmgr_read_attr_cache(const char *devid_or_uuid, const char *attr_name,
char **attr_value);
const char *config_get_main_uuid(void);
int stdd_zbnet_update_attr_profile_cb(attr_profile_t *profile[]);
int stdd_zbnet_update_cmd_profile_cb(cmd_profile_t *profile[]);
int stdd_zbnet_get_attr_cb(uint8_t ieee_addr[8], \
uint8_t endpoint_id, const char *attr_set[]);
int stdd_zbnet_set_attr_cb(uint8_t ieee_addr[8], uint8_t endpoint_id, \
const char *attr_name, const char *attr_value);
int stdd_zbnet_exec_cmd_cb(uint8_t ieee_addr[8], uint8_t endpoint_id, \
const char *cmd_name, const char *cmd_args);
int stdd_zbnet_get_attr(const char *devid_or_uuid, const char *attr_name);
int stdd_zbnet_set_attr(const char *devid_or_uuid, const char *attr_name,
const char *attr_value);
int stdd_zbnet_exec_rpc(const char *devid_or_uuid, const char *rpc_name,
const char *rpc_args);
int stdd_get_device_attrset(const char *devid_or_uuid, char *attrset_buff,
int buff_size);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,228 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "aos/aos.h"
#include "alink_export_internal.h"
#include "json_parser.h"
#include "service.h"
#include "devmgr_cache.h"
#include "msdp_common.h"
#define MODULE_NAME MODULE_NAME_MSDP
static int stdd_zbnet_get_attr(const char *devid_or_uuid, const char *attr_name)
{
log_info("zbnet_get_attr, uuid:%s attr_name:%s\n", devid_or_uuid, attr_name);
return 0;
}
static int stdd_zbnet_set_attr(const char *devid_or_uuid, const char *attr_name,
const char *attr_value)
{
log_info("zbnet_set_attr, uuid:%s attr_name:%s attr_value:%s\n", devid_or_uuid,
attr_name, attr_value);
return 0;
}
#define DEV_ATTR_SET "[\"BackLightMode\",\"BatteryPercentage\",\"Rssi\",\"Switch\"]"
static int stdd_get_device_attrset(const char *devid_or_uuid,
char *attrset_buff, int buff_size)
{
strncpy(attrset_buff, DEV_ATTR_SET, buff_size);
log_info("zbnet get device attrset, uuid:%s\n", devid_or_uuid);
return 0;
}
static int stdd_zbnet_exec_rpc(const char *devid_or_uuid, const char *rpc_name,
const char *rpc_args)
{
return 0;
}
static int __get_attribute(const char *uuid, const char *attr_name,
char **attr_value)
{
int ret = SERVICE_RESULT_ERR;
char *attr_cache = NULL;
log_trace("uuid:%s, attr_name:%s", uuid, attr_name);
ret = stdd_zbnet_get_attr(uuid, attr_name);
RET_LOG(ret, "get attribute fail, attrname:%s", attr_name);
return ret;
}
static int __set_attribute(const char *uuid, const char *attr_name,
char *attr_value)
{
int ret = SERVICE_RESULT_ERR;
ret = stdd_zbnet_set_attr(uuid, attr_name, attr_value);
RET_RETURN(ret, CALL_FUCTION_FAILED, "stdd_zbnet_set_attr");
return ret;
}
/*获取指定设备所有属性*/
static int __get_all_attrname(const char *uuid, char *attr_set, int buff_size)
{
int count, ret = SERVICE_RESULT_ERR;
ret = stdd_get_device_attrset(uuid, attr_set, buff_size);
RET_RETURN(ret, "get device all attribute name fail, uuid:%s", uuid);
return ret;
}
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState", "lightSwitchState"]}
<-- {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}
*/
static int msdp_get_status(char *params)
{
#if 0
int ret = SERVICE_RESULT_ERR;
char *str_pos, *params_ptr = params;
int str_len = 0;
char params_buff[512] = {0};
int attrset_size = 0;
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_ATTRSET,
&str_len, NULL);
if (str_pos) {
log_trace("attrset:%s", str_pos);
attrset_size = json_get_array_size(str_pos, str_len);
}
if (NULL == str_pos || attrset_size == 0) {
//uuid
char uuid[MAX_UUID_LEN] = {0};
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
&str_len, NULL);
PTR_RETURN(str_pos, SERVICE_RESULT_ERR, "get uuid fail, params:%s", params);
strncpy(uuid, str_pos, str_len);
//else,todo:get devid
char all_attr[256] = {0};
ret = __get_all_attrname(uuid, all_attr, sizeof(all_attr));
RET_RETURN(ret, "__get_all_attrname(%s)", uuid);
log_trace("attrset:%s", all_attr);
snprintf(params_buff, sizeof(params_buff) - 1, GET_DEVICE_ATTR_STRING_FMT, uuid,
all_attr);
params_ptr = params_buff;
}
log_trace("params_ptr:%s", params_ptr);
char *json_out = NULL;
ret = msdp_get_device_status_handler(params_ptr, msdp_get_attr_each_cb,
__get_attribute, &json_out);
RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_status_handler");
if (json_out) {
log_trace("post device data: params:%s", json_out);
if (SERVICE_RESULT_OK != msdp_add_asyncpost_task(json_out)) {
log_error("add async post task fail, params:%s", json_out);
msdp_free_buff(json_out);
}
json_out = NULL;
}
return ret;
#else
aos_cloud_trigger(GET_SUB_DEVICE_STATUS, params);
return SERVICE_RESULT_OK;
#endif
}
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}
*/
static int msdp_set_status(char *params)
{
#if 0
int ret = SERVICE_RESULT_ERR;
//log_trace("params:%s\n", params);
//遍历attrset设置属性
ret = msdp_set_device_status_handler(params, msdp_set_attr_each_cb,
__set_attribute);
RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_set_attr_each_cb");
return ret;
#else
aos_cloud_trigger(SET_SUB_DEVICE_STATUS, params);
return SERVICE_RESULT_OK;
#endif
}
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","service\":"startBwCheck","args":{}}
<-- {"uuid": "42E6E69DAEF6483FBBA412A28AF7CD76", "service": "stopBwCheck", "code": "1", "result": [ ] }
*/
static int msdp_rpc(char *params)
{
int ret = SERVICE_RESULT_ERR;
char rpc_name[MAX_SERVICE_NAME_LEN] = {0};
char uuid[MAX_UUID_LEN] = {0};
char rpc_args[256] = {0};
char *str_pos = NULL;
int str_len = 0;
log_trace("params:%s", params);
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_SERVICE,
&str_len, NULL);
PTR_RETURN(str_pos, ret, "get service name fail, params:%s", params);
strncpy(rpc_name, str_pos, str_len);
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_ARGS,
&str_len, NULL);
PTR_RETURN(str_pos, ret, "get service args fail, params:%s", params);
strncpy(rpc_args, str_pos, str_len);
str_pos = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
&str_len, NULL);
PTR_RETURN(str_pos, ret, "get uuid fail, params:%s", params);
strncpy(uuid, str_pos, str_len);
ret = stdd_zbnet_exec_rpc(uuid, rpc_name, rpc_args);
RET_RETURN(ret, "exeute rpc fail, uuid:%s, rpc_name:%s, args:%s", uuid,
rpc_name, rpc_args);
return ret;
}
int msdp_zbnet_init()
{
dev_option_cb callback[MAX_CALLBACK_NUM] = {NULL};
/*默认cmd callback*/
callback[ACB_GET_DEVICE_STATUS] = msdp_get_status;
callback[ACB_SET_DEVICE_STATUS] = msdp_set_status;
callback[ACB_REQUEST_REMOTE_SERVICE] = msdp_rpc;
/*注册zigbee设备事件处理接口*/
return msdp_register_device_helper(DEV_TYPE_ZIGBEE, callback);
}
void mdsp_zbnet_exit()
{
msdp_unregister_device_helper(DEV_TYPE_ZIGBEE);
}

View file

@ -0,0 +1,8 @@
src = Split('''
msdp.c
msdp_common.c
msdp_zigbee.c
msdp_gateway.c
''')
component = aos_component('msdp', src)