Commit fc3fe0ee authored by 黄振令's avatar 黄振令
parents dbaa62f2 4edd1128
......@@ -7,7 +7,7 @@
#define TOPIC "/sys/a1OYuSBt23u/aIqEbWno8yDdsjCX15iq/thing/service/property/set"
#define PAYLOAD "Hello cwc World!"
#define QOS 2
#define QOS 0
#define TIMEOUT 10000L
#define USRNAME "hangzhou_gateway "
#define PASSWORD "2ca1442865ff4cb99870f60f2c646190"
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kk_dm_mng.h"
#include "kk_dm_msg.h"
#include "kk_tsl_api.h"
#include "kk_tsl_load.h"
#include "iot_export_linkkit.h"
#include "kk_product.h"
#include "kk_log.h"
#include "kk_property_db.h"
//const char DM_URI_SYS_PREFIX[] DM_READ_ONLY = "/sys/%s/%s/";
const char DM_URI_EXT_SESSION_PREFIX[] DM_READ_ONLY = "/ext/session/%s/%s/";
const char DM_URI_EXT_NTP_PREFIX[] DM_READ_ONLY = "/ext/ntp/%s/%s/";
const char DM_URI_EXT_ERROR_PREFIX[] DM_READ_ONLY = "/ext/error/%s/%s";
const char DM_URI_REPLY_SUFFIX[] DM_READ_ONLY = "_reply";
const char DM_URI_OTA_DEVICE_INFORM[] DM_READ_ONLY = "/ota/device/inform/%s/%s";
const char DM_URI_THING_EVENT_PROPERTY_POST_METHOD[] DM_READ_ONLY = "thing.event.property.post";
static dm_mgr_ctx g_dm_mgr = {0};
static dm_mgr_ctx *_dm_mgr_get_ctx(void)
{
return &g_dm_mgr;
}
static void _dm_mgr_mutex_lock(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
if (ctx->mutex) {
kk_MutexLock(ctx->mutex);
}
}
static void _dm_mgr_mutex_unlock(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
if (ctx->mutex) {
kk_MutexUnLock(ctx->mutex);
}
}
int dm_mgr_search_dev_by_devid(_IN_ int devid, _OU_ dm_mgr_dev_node_t **node)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if (search_node->devid == devid) {
/* dm_log_debug("Device Found, devid: %d", devid); */
if (node) {
*node = search_node;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, devid: %d", devid);
return FAIL_RETURN;
}
static int _dm_mgr_search_dev_by_pkdn(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_mgr_dev_node_t **node)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ( (strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (node) {
*node = search_node;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, Device Code: %s", deviceCode);
return FAIL_RETURN;
}
static int _dm_mgr_next_devid(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
return ctx->global_devid++;
}
static int _dm_init_tsl_params(int devId)
{
int res = 0;
char version[VERSION_MAXLEN] = {0};
char s_IP[NETWORK_ADDR_LEN] = {0};
char s_mac[MAC_ADDR_LEN] = {0};
char s_SN[SN_ADDR_LEN] = {0};
int port = 0;
/*******set version***********/
HAL_GetVersion(version);
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,KK_TSL_VERSION_IDENTIFIER,NULL,version);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
/*******set IP*************/
//HAL_Get_IP(s_IP,NULL);
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,KK_TSL_CCU_LANIP_IDENTIFIER,NULL,"0.0.0.0");
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,KK_TSL_CCU_WANIP_IDENTIFIER,NULL,"0.0.0.0");
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
/*******set MAC*************/
HAL_Get_mac(s_mac);
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,KK_TSL_GATAWAY_MAC_IDENTIFIER,NULL,s_mac);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
int wanState = 0;
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,KK_TSL_CCU_WANSTATE_IDENTIFIER,&wanState,NULL);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
int IOTCloudState = 0;
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,KK_TSL_CCU_IOTCLOUD_IDENTIFIER,&IOTCloudState,NULL);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
/*******set sn*************/
HAL_Get_SN(s_SN);
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,KK_TSL_GATAWAY_SN_IDENTIFIER,NULL,s_SN);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
return res;
}
int dm_mgr_properities_db_create(kk_tsl_t* dev_shadow,char *deviceCode,int devType )
{
int num = 0,idx = 0;
kk_tsl_data_t *pProperty = NULL;
if(dev_shadow == NULL || deviceCode == NULL){
ERROR_PRINT("dm_mgr_properities_db_create fail!!\n");
return INVALID_PARAMETER;
}
num = dev_shadow->property_number;
INFO_PRINT("dm_mgr_properities_db_create num:%d!!\n",num);
for(idx = 0; idx < num; idx++){
pProperty = dev_shadow->properties+idx;
INFO_PRINT("dm_mgr_properities_db_create pProperty->identifier:%s!!\n",pProperty->identifier);
if(pProperty){
kk_property_db_insert(deviceCode,pProperty->identifier,pProperty->data_value.type,devType);
}
}
}
int dm_mgr_device_create(_IN_ int dev_type,_IN_ char productCode[PRODUCT_CODE_MAXLEN], _IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_IN_ char mac[DEVICE_MAC_MAXLEN],_IN_ char fatherDeviceCode[DEVICE_CODE_MAXLEN], _OU_ int *devid)
{
int res = 0;
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *node = NULL;
char *tsl_str = NULL;
int idx = 0;
if (deviceCode != NULL && strlen(deviceCode) >= DEVICE_CODE_MAXLEN) {
return INVALID_PARAMETER;
}
res = _dm_mgr_search_dev_by_pkdn(deviceCode, &node);
if (res == SUCCESS_RETURN) {
if (devid) {
*devid = node->devid;
}
return TSL_ALREADY_EXIST;
}
node = malloc(sizeof(dm_mgr_dev_node_t));
if (node == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(node, 0, sizeof(dm_mgr_dev_node_t));
if(dev_type == KK_DM_DEVICE_CCU)
node->devid = KK_DM_DEVICE_CCU_DEVICEID;
else
node->devid = _dm_mgr_next_devid();
node->dev_type = dev_type;
node->dev_shadow = NULL;
if (productCode != NULL) {
memcpy(node->productCode, productCode, strlen(productCode));
}
if (deviceCode != NULL) {
memcpy(node->deviceCode, deviceCode, strlen(deviceCode));
}
if (mac != NULL) {
memcpy(node->mac, mac, strlen(mac));
}
if (dev_type != KK_DM_DEVICE_CCU && fatherDeviceCode != NULL) {
memcpy(node->fatherDeviceCode, fatherDeviceCode, strlen(fatherDeviceCode));
}
node->timestamp = HAL_UptimeMs();
//node->dev_status = IOTX_DM_DEV_STATUS_AUTHORIZED;
tsl_str = kk_load_json(productCode, dev_type);
if(tsl_str != NULL)
{
res = kk_tsl_create(tsl_str,strlen(tsl_str),&node->dev_shadow);
free(tsl_str);
if(res != 0){
return FAIL_RETURN;
}
}
dm_mgr_properities_db_create(node->dev_shadow,deviceCode,dev_type);
INIT_LIST_HEAD(&node->linked_list);
list_add_tail(&node->linked_list, &ctx->dev_list);
if(dev_type == KK_DM_DEVICE_CCU){
//_dm_init_tsl_params(node->devid);
}
if (devid) {
*devid = node->devid;
}
return SUCCESS_RETURN;
}
int dm_mgr_search_device_by_pkdn( _IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ int *devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (deviceCode == NULL) {
return INVALID_PARAMETER;
}
res = _dm_mgr_search_dev_by_pkdn(deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if (devid) {
*devid = node->devid;
}
return SUCCESS_RETURN;
}
int dm_mgr_get_devicetype_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ int* deviceType)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *node = NULL;
list_for_each_entry(node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ((strlen(node->deviceCode) == strlen(deviceCode)) &&
(memcmp(node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (node) {
*deviceType = node->dev_type;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, deviceCode: %s\n", deviceCode);
return FAIL_RETURN;
}
int dm_mgr_get_device_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_mgr_dev_node_t **node)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ((strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (node) {
*node = search_node;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, deviceCode: %s\n", deviceCode);
return FAIL_RETURN;
}
int dm_mgr_update_timestamp_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],uint64_t timestamp)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
_dm_mgr_mutex_lock();
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ((strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
search_node->timestamp = timestamp;
_dm_mgr_mutex_unlock();
return SUCCESS_RETURN;
}
}
_dm_mgr_mutex_unlock();
ERROR_PRINT("Device Not Found, deviceCode: %s\n", deviceCode);
return FAIL_RETURN;
}
#define TEST_TIMEOUT 600000 // ten minutes
int dm_mgr_check_heartbeat_timeout(uint64_t timestamp)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
_dm_mgr_mutex_lock();
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if(search_node->dev_type == KK_DM_DEVICE_CCU){
continue;
}
if((timestamp - search_node->timestamp) >= TEST_TIMEOUT/*search_node->hb_timeout*/){
if(search_node->isOffline == 0){
INFO_PRINT("---------->dev timeout,send offline\n");
search_node->isOffline = 1;
iotx_dm_dev_offline(search_node->devid);
kk_subDev_update_offline(search_node->isOffline,search_node->deviceCode);
}
//_dm_mgr_mutex_unlock();
//return DEVICE_HEARTBEAT_TIMEOUT;
}
else{
if(search_node->isOffline == 1){//need send online
INFO_PRINT("---------->dev online again,send online\n");
search_node->isOffline = 0;
iotx_dm_dev_online(search_node->devid);
kk_subDev_update_offline(search_node->isOffline,search_node->deviceCode);
}
}
}
_dm_mgr_mutex_unlock();
return SUCCESS_RETURN;
}
int dm_mgr_get_devId_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ int *devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (deviceCode == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_get_device_by_devicecode(deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if (devid) {
*devid = node->devid;
}
return SUCCESS_RETURN;
}
static void _dm_mgr_destroy_devlist(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *del_node = NULL;
dm_mgr_dev_node_t *next_node = NULL;
list_for_each_entry_safe(del_node, next_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
list_del(&del_node->linked_list);
//dm_shw_destroy(&del_node->dev_shadow);
free(del_node);
}
}
int dm_mgr_init(void)
{
int res = 0;
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
char productCode[PRODUCT_CODE_MAXLEN] = {0};
char deviceCode[DEVICE_CODE_MAXLEN]= {0};
char mac[DEVICE_MAC_MAXLEN]= {0};
int devId = 0;
memset(ctx, 0, sizeof(dm_mgr_ctx));
/* Create Mutex */
ctx->mutex = kk_MutexCreate();
if (ctx->mutex == NULL) {
goto ERROR;
}
/* Init Device Id*/
ctx->global_devid = 1;
kk_property_db_init();
/* Init Device List */
INIT_LIST_HEAD(&ctx->dev_list);
//HAL_GetProduct_Type(product_key);
//HAL_GetProduct_Code(device_name);
HAL_Get_mac(mac);
res = dm_mgr_device_create(KK_DM_DEVICE_CCU,KK_DM_CCU_DEVICE_PRODUCT_CODE,"CCU_66666",mac,"",&devId);
if (res != SUCCESS_RETURN) {
goto ERROR;
}
else{
_dm_init_tsl_params(devId);
kk_property_db_update("CCU_66666");
}
return SUCCESS_RETURN;
ERROR:
if (ctx->mutex) {
kk_MutexDestroy(ctx->mutex);
}
memset(ctx, 0, sizeof(dm_mgr_ctx));
return FAIL_RETURN;
}
int dm_mgr_deinit(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
_dm_mgr_mutex_lock();
_dm_mgr_destroy_devlist();
_dm_mgr_mutex_unlock();
if (ctx->mutex) {
HAL_MutexDestroy(ctx->mutex);
}
return SUCCESS_RETURN;
}
const char DM_URI_THING_EVENT_POST[] = "/thing/event/%s/post";
const char DM_MSG_THING_UPSTREAM_REQUEST_PARAMS[] DM_READ_ONLY = "%s";
static int _dm_mgr_upstream_request_assemble(_IN_ int msgid, _IN_ int devid,
_IN_ char *identify,
_IN_ char *params, _IN_ int params_len, _IN_ char *method, _OU_ dm_msg_request_t *request)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
int paramLen = 0;
int msgTypeLen = 0;
char *payload_param = NULL;
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* dm_log_debug("Time Stamp: %s", timestamp); */
paramLen = strlen(DM_MSG_THING_UPSTREAM_REQUEST_PARAMS) +
params_len + 1;
payload_param = malloc(paramLen);
if (payload_param == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(payload_param, 0, paramLen);
HAL_Snprintf(payload_param, paramLen, DM_MSG_THING_UPSTREAM_REQUEST_PARAMS, params);
request->msgid = msgid;
request->devid = devid;
msgTypeLen = strlen(DM_URI_THING_EVENT_POST) + strlen(identify) + 1;
request->msgTypeStr = malloc(msgTypeLen);
if(request->msgTypeStr == NULL){
return MEMORY_NOT_ENOUGH;
}
memset(request->msgTypeStr,0x0,msgTypeLen);
snprintf(request->msgTypeStr,msgTypeLen,DM_URI_THING_EVENT_POST,identify);
memcpy(request->productCode, node->productCode, strlen(node->productCode));
memcpy(request->deviceCode, node->deviceCode, strlen(node->deviceCode));
request->params = payload_param;
request->params_len = paramLen;
request->method = method;
return SUCCESS_RETURN;
}
static unsigned int g_report_id = 1;
int iotx_report_id(void)
{
return g_report_id++;
}
int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len,_IN_ int isAsync)
{
int res = 0;
dm_msg_request_t request;
if (devid < 0 || payload == NULL || payload_len <= 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = _dm_mgr_upstream_request_assemble(iotx_report_id(), devid, "property",
payload, payload_len, DM_URI_THING_EVENT_PROPERTY_POST_METHOD, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Callback */
//request.callback = dm_client_thing_event_post_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,isAsync);
free(request.msgTypeStr);
free(request.params);
return res;
}
int dm_mgr_upstream_thing_event_post(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *method,
_IN_ char *payload, _IN_ int payload_len)
{
int res = 0;
dm_msg_request_t request;
if (devid < 0 || identifier == NULL || identifier_len <= 0 ||
method == NULL || payload == NULL || payload_len <= 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = _dm_mgr_upstream_request_assemble(iotx_report_id(), devid,identifier,
payload, payload_len, method, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Callback */
//request.callback = dm_client_thing_event_post_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
free(request.msgTypeStr);
free(request.params);
return res;
}
const char DM_URI_THING_SERVICE_RESPONSE[] = "/thing/service/%.*s_reply";
static int _kk_mgr_upstream_response_assemble(_IN_ int devid, _IN_ char *msgid, _IN_ int msgid_len,
_IN_ const char *identfy, _IN_ int code, _OU_ kk_msg_request_payload_t *request, _OU_ kk_msg_response_t *response)
{
int res = 0;
int msgTypeLen = 0;
dm_mgr_dev_node_t *node = NULL;
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
request->id.value = msgid;
request->id.value_length = msgid_len;
msgTypeLen = strlen(DM_URI_THING_SERVICE_RESPONSE) + strlen(identfy) + 1;
response->msgTypeStr = malloc(msgTypeLen);
if(response->msgTypeStr == NULL){
return MEMORY_NOT_ENOUGH;
}
memset(response->msgTypeStr,0x0,msgTypeLen);
snprintf(response->msgTypeStr,msgTypeLen,DM_URI_THING_SERVICE_RESPONSE,identfy);
memcpy(response->productCode, node->productCode, strlen(node->productCode));
memcpy(response->deviceCode, node->deviceCode, strlen(node->deviceCode));
response->code = code;
return SUCCESS_RETURN;
}
int dm_mgr_deprecated_upstream_thing_service_response(_IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
_IN_ char *identifier, _IN_ int identifier_len, _IN_ char *payload, _IN_ int payload_len)
{
int res = 0, service_name_len = 0;
char *msgid_str = NULL;
kk_msg_request_payload_t request;
kk_msg_response_t response;
memset(&request, 0, sizeof(kk_msg_request_payload_t));
memset(&response, 0, sizeof(kk_msg_response_t));
if (devid < 0 || msgid < 0 || identifier == NULL || identifier_len <= 0 ||
payload == NULL || payload_len <= 0) {
return INVALID_PARAMETER;
}
/* Response Msg ID */
res = kk_utils_itoa(msgid, &msgid_str);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
request.id.value = msgid_str;
request.id.value_length = strlen(msgid_str);
/* Service Name */
res = _kk_mgr_upstream_response_assemble(devid, msgid_str, strlen(msgid_str), identifier, code,
&request,
&response);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
dm_msg_response(&request, &response, payload, payload_len, NULL);
free(response.msgTypeStr);
free(msgid_str);
return SUCCESS_RETURN;
}
const char DM_URI_THING_SUB_REGISTER[] = "/thing/sub/register";
int dm_mgr_upstream_thing_sub_register(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_SUB_REGISTER;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_sub_register( node->productCode,node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_sub_register_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_SUB_UNREGISTER[] = "/thing/sub/unregister";
int dm_mgr_upstream_thing_sub_unregister(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_SUB_UNREGISTER;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_sub_unregister(node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_sub_unregister_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_TOPO_ADD[] = "/thing/topo/add";
int dm_mgr_upstream_thing_topo_add(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR res:%d\n",res);
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_TOPO_ADD;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_topo_add( node->productCode, node->deviceCode, node->mac,&request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_topo_add_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_TOPO_DELETE[] = "/thing/topo/delete";
int dm_mgr_upstream_thing_topo_delete(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return DM_INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_TOPO_DELETE;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_topo_delete( node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_topo_delete_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_TOPO_GET[] = "/thing/topo/get";
int dm_mgr_upstream_thing_topo_get(void)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_msg_request_t request;
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_TOPO_GET;
HAL_GetProduct_Code(request.deviceCode);
res = _dm_mgr_search_dev_by_pkdn(request.deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Params And Method */
res = dm_msg_thing_topo_get(&request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = node->devid;
/* Callback */
//request.callback = dm_client_thing_topo_get_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_LIST_FOUND[] = "/thing/list/found";
int dm_mgr_upstream_thing_list_found(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_LIST_FOUND;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_list_found( node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_list_found_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_STATUS_CLOUD[] = "/thing/ccu/cloudstate";
int dm_mgr_ccu_status_cloud(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
memcpy(request.productCode,node->productCode,strlen(node->productCode));
memcpy(request.deviceCode,node->deviceCode,strlen(node->deviceCode));
request.msgTypeStr = DM_URI_STATUS_CLOUD;
/* Get Params And Method */
res = dm_msg_status_cloud( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_STATUS_ONLINE[] = "/thing/status/online";
int dm_mgr_upstream_status_online(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if(strlen(node->fatherDeviceCode) > 0){
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
}else{
memcpy(request.productCode,node->productCode,strlen(node->productCode));
memcpy(request.deviceCode,node->deviceCode,strlen(node->deviceCode));
}
request.msgTypeStr = DM_URI_STATUS_ONLINE;
/* Get Params And Method */
res = dm_msg_status_online( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_STATUS_OFFLINE[] = "/thing/status/offline";
int dm_mgr_upstream_status_offline(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if(strlen(node->fatherDeviceCode) > 0){
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
}else{
memcpy(request.productCode,node->productCode,strlen(node->productCode));
memcpy(request.deviceCode,node->deviceCode,strlen(node->deviceCode));
}
request.msgTypeStr = DM_URI_STATUS_OFFLINE;
/* Get Params And Method */
res = dm_msg_status_offline( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_COMBINE_LOGIN[] = "/thing/combine/login";
int dm_mgr_upstream_combine_login(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_COMBINE_LOGIN;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_combine_login( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_COMBINE_LOGOUT[] = "/thing/combine/logout";
int dm_mgr_upstream_combine_logout(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_COMBINE_LOGOUT;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_combine_logout( node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_logout_reply;
/* Send Message To Cloud */
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char KK_URI_OTA_INFORM[] = "/ota/device/inform/%s/%s";
int dm_mgr_ota_report_version(_IN_ int devid, char *version)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = malloc(strlen(KK_URI_OTA_INFORM)+strlen(node->productCode)+strlen(node->deviceCode)+1);
if(request.msgTypeStr == NULL){
return MEMORY_NOT_ENOUGH;
}
sprintf(request.msgTypeStr,KK_URI_OTA_INFORM,node->productCode,node->deviceCode);
memcpy(request.productCode, node->productCode, PRODUCT_CODE_MAXLEN);
memcpy(request.deviceCode, node->deviceCode, DEVICE_CODE_MAXLEN);
/* Get Params And Method */
res = dm_msg_ota_report_version(version, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_logout_reply;
/* Send Message To Cloud */
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
free(request.msgTypeStr);
return res;
}
int dm_mgr_subdev_create(int devtype,_IN_ char productCode[PRODUCT_CODE_MAXLEN], _IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_IN_ char mac[DEVICE_MAC_MAXLEN],_IN_ char fatherDeviceCode[DEVICE_CODE_MAXLEN], _OU_ int *devid){
int res = 0;
res = dm_mgr_device_create(devtype,productCode,deviceCode,mac,fatherDeviceCode, devid);
if(TSL_ALREADY_EXIST == res)
{
ERROR_PRINT("SUBDEV ALREADY EXIST!!!\n");
}
return res;
}
int dm_mgr_subdev_delete(_IN_ char deviceCode[DEVICE_CODE_MAXLEN])
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if(deviceCode == NULL || strlen(deviceCode) <=0){
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
INFO_PRINT("dm_mgr_subdev_delete deviceCode:%s\n",deviceCode);
res = kk_subDev_delete_by_dcode(deviceCode);//delete sub db data
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = kk_property_delete_by_dcode(deviceCode);//delete properties db data
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(deviceCode,&node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
if(node->dev_type == KK_DM_DEVICE_CCU){
ERROR_PRINT("ERROR [%s][%d]\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
list_del(&node->linked_list);
if (node->dev_shadow) {
kk_tsl_destroy(&node->dev_shadow);
}
free(node);
node = NULL;
return SUCCESS_RETURN;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kk_dm_mng.h"
#include "kk_dm_msg.h"
#include "kk_tsl_api.h"
#include "kk_tsl_load.h"
#include "iot_export_linkkit.h"
#include "kk_product.h"
#include "kk_log.h"
#include "kk_property_db.h"
//const char DM_URI_SYS_PREFIX[] DM_READ_ONLY = "/sys/%s/%s/";
const char DM_URI_EXT_SESSION_PREFIX[] DM_READ_ONLY = "/ext/session/%s/%s/";
const char DM_URI_EXT_NTP_PREFIX[] DM_READ_ONLY = "/ext/ntp/%s/%s/";
const char DM_URI_EXT_ERROR_PREFIX[] DM_READ_ONLY = "/ext/error/%s/%s";
const char DM_URI_REPLY_SUFFIX[] DM_READ_ONLY = "_reply";
const char DM_URI_OTA_DEVICE_INFORM[] DM_READ_ONLY = "/ota/device/inform/%s/%s";
const char DM_URI_THING_EVENT_PROPERTY_POST_METHOD[] DM_READ_ONLY = "thing.event.property.post";
static dm_mgr_ctx g_dm_mgr = {0};
static dm_mgr_ctx *_dm_mgr_get_ctx(void)
{
return &g_dm_mgr;
}
static void _dm_mgr_mutex_lock(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
if (ctx->mutex) {
kk_MutexLock(ctx->mutex);
}
}
static void _dm_mgr_mutex_unlock(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
if (ctx->mutex) {
kk_MutexUnLock(ctx->mutex);
}
}
int dm_mgr_search_dev_by_devid(_IN_ int devid, _OU_ dm_mgr_dev_node_t **node)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if (search_node->devid == devid) {
/* dm_log_debug("Device Found, devid: %d", devid); */
if (node) {
*node = search_node;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, devid: %d", devid);
return FAIL_RETURN;
}
static int _dm_mgr_search_dev_by_pkdn(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_mgr_dev_node_t **node)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ( (strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (node) {
*node = search_node;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, Device Code: %s", deviceCode);
return FAIL_RETURN;
}
static int _dm_mgr_next_devid(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
return ctx->global_devid++;
}
static int _dm_init_tsl_params(kk_tsl_t * dev_shadow)
{
int res = 0;
char version[VERSION_MAXLEN] = {0};
char s_IP[NETWORK_ADDR_LEN] = {0};
char s_mac[MAC_ADDR_LEN] = {0};
char s_SN[SN_ADDR_LEN] = {0};
int port = 0;
/*******set version***********/
HAL_GetVersion(version);
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_VERSION_IDENTIFIER,NULL,version);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
/*******set IP*************/
//HAL_Get_IP(s_IP,NULL);
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_CCU_LANIP_IDENTIFIER,NULL,"0.0.0.0");
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_CCU_WANIP_IDENTIFIER,NULL,"0.0.0.0");
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
/*******set MAC*************/
HAL_Get_mac(s_mac);
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_GATAWAY_MAC_IDENTIFIER,NULL,s_mac);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
int wanState = 0;
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_CCU_WANSTATE_IDENTIFIER,&wanState,NULL);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
int IOTCloudState = 0;
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_CCU_IOTCLOUD_IDENTIFIER,&IOTCloudState,NULL);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
/*******set sn*************/
HAL_Get_SN(s_SN);
res = kk_tsl_set_value(kk_tsl_set_property_value,dev_shadow,KK_TSL_GATAWAY_SN_IDENTIFIER,NULL,s_SN);
if(res != SUCCESS_RETURN)
{
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
}
return res;
}
int dm_mgr_properities_db_create(kk_tsl_t* dev_shadow,char *deviceCode,int devType )
{
int num = 0,idx = 0;
kk_tsl_data_t *pProperty = NULL;
if(dev_shadow == NULL || deviceCode == NULL){
ERROR_PRINT("dm_mgr_properities_db_create fail!!\n");
return INVALID_PARAMETER;
}
num = dev_shadow->property_number;
INFO_PRINT("dm_mgr_properities_db_create num:%d!!\n",num);
for(idx = 0; idx < num; idx++){
pProperty = dev_shadow->properties+idx;
INFO_PRINT("dm_mgr_properities_db_create pProperty->identifier:%s!!\n",pProperty->identifier);
if(pProperty){
kk_property_db_insert(deviceCode,pProperty->identifier,pProperty->data_value.type,devType);
}
}
}
int dm_mgr_device_create(_IN_ int dev_type,_IN_ char productCode[PRODUCT_CODE_MAXLEN], _IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_IN_ char mac[DEVICE_MAC_MAXLEN],_IN_ char fatherDeviceCode[DEVICE_CODE_MAXLEN], _OU_ int *devid)
{
int res = 0;
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *node = NULL;
char *tsl_str = NULL;
int idx = 0;
if (deviceCode != NULL && strlen(deviceCode) >= DEVICE_CODE_MAXLEN) {
return INVALID_PARAMETER;
}
res = _dm_mgr_search_dev_by_pkdn(deviceCode, &node);
if (res == SUCCESS_RETURN) {
if (devid) {
*devid = node->devid;
}
return TSL_ALREADY_EXIST;
}
node = malloc(sizeof(dm_mgr_dev_node_t));
if (node == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(node, 0, sizeof(dm_mgr_dev_node_t));
if(dev_type == KK_DM_DEVICE_CCU)
node->devid = KK_DM_DEVICE_CCU_DEVICEID;
else
node->devid = _dm_mgr_next_devid();
node->dev_type = dev_type;
node->dev_shadow = NULL;
if (productCode != NULL) {
memcpy(node->productCode, productCode, strlen(productCode));
}
if (deviceCode != NULL) {
memcpy(node->deviceCode, deviceCode, strlen(deviceCode));
}
if (mac != NULL) {
memcpy(node->mac, mac, strlen(mac));
}
if (dev_type != KK_DM_DEVICE_CCU && fatherDeviceCode != NULL) {
memcpy(node->fatherDeviceCode, fatherDeviceCode, strlen(fatherDeviceCode));
}
node->timestamp = HAL_UptimeMs();
//node->dev_status = IOTX_DM_DEV_STATUS_AUTHORIZED;
tsl_str = kk_load_json(productCode, dev_type);
if(tsl_str != NULL)
{
res = kk_tsl_create(tsl_str,strlen(tsl_str),&node->dev_shadow);
free(tsl_str);
if(res != 0){
return FAIL_RETURN;
}
}
dm_mgr_properities_db_create(node->dev_shadow,deviceCode,dev_type);
INIT_LIST_HEAD(&node->linked_list);
list_add_tail(&node->linked_list, &ctx->dev_list);
if(dev_type == KK_DM_DEVICE_CCU){
_dm_init_tsl_params(node->dev_shadow);
}
if (devid) {
*devid = node->devid;
}
return SUCCESS_RETURN;
}
int dm_mgr_search_device_by_pkdn( _IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ int *devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (deviceCode == NULL) {
return INVALID_PARAMETER;
}
res = _dm_mgr_search_dev_by_pkdn(deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if (devid) {
*devid = node->devid;
}
return SUCCESS_RETURN;
}
int dm_mgr_get_devicetype_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ int* deviceType)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *node = NULL;
list_for_each_entry(node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ((strlen(node->deviceCode) == strlen(deviceCode)) &&
(memcmp(node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (node) {
*deviceType = node->dev_type;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, deviceCode: %s\n", deviceCode);
return FAIL_RETURN;
}
int dm_mgr_get_device_shadow_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ kk_tsl_t **shadow)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ((strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (shadow) {
*shadow = search_node->dev_shadow;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, deviceCode: %s\n", deviceCode);
return FAIL_RETURN;
}
int dm_mgr_get_device_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_mgr_dev_node_t **node)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ((strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
if (node) {
*node = search_node;
}
return SUCCESS_RETURN;
}
}
ERROR_PRINT("Device Not Found, deviceCode: %s\n", deviceCode);
return FAIL_RETURN;
}
int dm_mgr_update_timestamp_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],uint64_t timestamp)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
_dm_mgr_mutex_lock();
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if ((strlen(search_node->deviceCode) == strlen(deviceCode)) &&
(memcmp(search_node->deviceCode, deviceCode, strlen(deviceCode)) == 0)) {
/* dm_log_debug("Device Found, Product Key: %s, Device Name: %s", product_key, device_name); */
search_node->timestamp = timestamp;
_dm_mgr_mutex_unlock();
return SUCCESS_RETURN;
}
}
_dm_mgr_mutex_unlock();
ERROR_PRINT("Device Not Found, deviceCode: %s\n", deviceCode);
return FAIL_RETURN;
}
#define TEST_TIMEOUT 600000 // ten minutes
int dm_mgr_check_heartbeat_timeout(uint64_t timestamp)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *search_node = NULL;
_dm_mgr_mutex_lock();
list_for_each_entry(search_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
if(search_node->dev_type == KK_DM_DEVICE_CCU){
continue;
}
if((timestamp - search_node->timestamp) >= TEST_TIMEOUT/*search_node->hb_timeout*/){
if(search_node->isOffline == 0){
INFO_PRINT("---------->dev timeout,send offline\n");
search_node->isOffline = 1;
iotx_dm_dev_offline(search_node->devid);
kk_subDev_update_offline(search_node->isOffline,search_node->deviceCode);
}
//_dm_mgr_mutex_unlock();
//return DEVICE_HEARTBEAT_TIMEOUT;
}
else{
if(search_node->isOffline == 1){//need send online
INFO_PRINT("---------->dev online again,send online\n");
search_node->isOffline = 0;
iotx_dm_dev_online(search_node->devid);
kk_subDev_update_offline(search_node->isOffline,search_node->deviceCode);
}
}
}
_dm_mgr_mutex_unlock();
return SUCCESS_RETURN;
}
int dm_mgr_get_devId_by_devicecode(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ int *devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (deviceCode == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_get_device_by_devicecode(deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if (devid) {
*devid = node->devid;
}
return SUCCESS_RETURN;
}
static void _dm_mgr_destroy_devlist(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
dm_mgr_dev_node_t *del_node = NULL;
dm_mgr_dev_node_t *next_node = NULL;
list_for_each_entry_safe(del_node, next_node, &ctx->dev_list, linked_list, dm_mgr_dev_node_t) {
list_del(&del_node->linked_list);
//dm_shw_destroy(&del_node->dev_shadow);
free(del_node);
}
}
int dm_mgr_init(void)
{
int res = 0;
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
char productCode[PRODUCT_CODE_MAXLEN] = {0};
char deviceCode[DEVICE_CODE_MAXLEN]= {0};
char mac[DEVICE_MAC_MAXLEN]= {0};
int devId = 0;
memset(ctx, 0, sizeof(dm_mgr_ctx));
/* Create Mutex */
ctx->mutex = kk_MutexCreate();
if (ctx->mutex == NULL) {
goto ERROR;
}
/* Init Device Id*/
ctx->global_devid = 1;
kk_property_db_init();
/* Init Device List */
INIT_LIST_HEAD(&ctx->dev_list);
//HAL_GetProduct_Type(product_key);
//HAL_GetProduct_Code(device_name);
HAL_Get_mac(mac);
res = dm_mgr_device_create(KK_DM_DEVICE_CCU,KK_DM_CCU_DEVICE_PRODUCT_CODE,"CCU_66666",mac,"",&devId);
if (res != SUCCESS_RETURN) {
goto ERROR;
}
else{
kk_property_db_update("CCU_66666");
}
return SUCCESS_RETURN;
ERROR:
if (ctx->mutex) {
kk_MutexDestroy(ctx->mutex);
}
memset(ctx, 0, sizeof(dm_mgr_ctx));
return FAIL_RETURN;
}
int dm_mgr_deinit(void)
{
dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
_dm_mgr_mutex_lock();
_dm_mgr_destroy_devlist();
_dm_mgr_mutex_unlock();
if (ctx->mutex) {
HAL_MutexDestroy(ctx->mutex);
}
return SUCCESS_RETURN;
}
const char DM_URI_THING_EVENT_POST[] = "/thing/event/%s/post";
const char DM_MSG_THING_UPSTREAM_REQUEST_PARAMS[] DM_READ_ONLY = "%s";
static int _dm_mgr_upstream_request_assemble(_IN_ int msgid, _IN_ int devid,
_IN_ char *identify,
_IN_ char *params, _IN_ int params_len, _IN_ char *method, _OU_ dm_msg_request_t *request)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
int paramLen = 0;
int msgTypeLen = 0;
char *payload_param = NULL;
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* dm_log_debug("Time Stamp: %s", timestamp); */
paramLen = strlen(DM_MSG_THING_UPSTREAM_REQUEST_PARAMS) +
params_len + 1;
payload_param = malloc(paramLen);
if (payload_param == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(payload_param, 0, paramLen);
HAL_Snprintf(payload_param, paramLen, DM_MSG_THING_UPSTREAM_REQUEST_PARAMS, params);
request->msgid = msgid;
request->devid = devid;
msgTypeLen = strlen(DM_URI_THING_EVENT_POST) + strlen(identify) + 1;
request->msgTypeStr = malloc(msgTypeLen);
if(request->msgTypeStr == NULL){
return MEMORY_NOT_ENOUGH;
}
memset(request->msgTypeStr,0x0,msgTypeLen);
snprintf(request->msgTypeStr,msgTypeLen,DM_URI_THING_EVENT_POST,identify);
memcpy(request->productCode, node->productCode, strlen(node->productCode));
memcpy(request->deviceCode, node->deviceCode, strlen(node->deviceCode));
request->params = payload_param;
request->params_len = paramLen;
request->method = method;
return SUCCESS_RETURN;
}
static unsigned int g_report_id = 1;
int iotx_report_id(void)
{
return g_report_id++;
}
int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len,_IN_ int isAsync)
{
int res = 0;
dm_msg_request_t request;
if (devid < 0 || payload == NULL || payload_len <= 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = _dm_mgr_upstream_request_assemble(iotx_report_id(), devid, "property",
payload, payload_len, DM_URI_THING_EVENT_PROPERTY_POST_METHOD, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Callback */
//request.callback = dm_client_thing_event_post_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,isAsync);
free(request.msgTypeStr);
free(request.params);
return res;
}
int dm_mgr_upstream_thing_event_post(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *method,
_IN_ char *payload, _IN_ int payload_len)
{
int res = 0;
dm_msg_request_t request;
if (devid < 0 || identifier == NULL || identifier_len <= 0 ||
method == NULL || payload == NULL || payload_len <= 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = _dm_mgr_upstream_request_assemble(iotx_report_id(), devid,identifier,
payload, payload_len, method, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Callback */
//request.callback = dm_client_thing_event_post_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
free(request.msgTypeStr);
free(request.params);
return res;
}
const char DM_URI_THING_SERVICE_RESPONSE[] = "/thing/service/%.*s_reply";
static int _kk_mgr_upstream_response_assemble(_IN_ int devid, _IN_ char *msgid, _IN_ int msgid_len,
_IN_ const char *identfy, _IN_ int code, _OU_ kk_msg_request_payload_t *request, _OU_ kk_msg_response_t *response)
{
int res = 0;
int msgTypeLen = 0;
dm_mgr_dev_node_t *node = NULL;
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
request->id.value = msgid;
request->id.value_length = msgid_len;
msgTypeLen = strlen(DM_URI_THING_SERVICE_RESPONSE) + strlen(identfy) + 1;
response->msgTypeStr = malloc(msgTypeLen);
if(response->msgTypeStr == NULL){
return MEMORY_NOT_ENOUGH;
}
memset(response->msgTypeStr,0x0,msgTypeLen);
snprintf(response->msgTypeStr,msgTypeLen,DM_URI_THING_SERVICE_RESPONSE,identfy);
memcpy(response->productCode, node->productCode, strlen(node->productCode));
memcpy(response->deviceCode, node->deviceCode, strlen(node->deviceCode));
response->code = code;
return SUCCESS_RETURN;
}
int dm_mgr_deprecated_upstream_thing_service_response(_IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
_IN_ char *identifier, _IN_ int identifier_len, _IN_ char *payload, _IN_ int payload_len)
{
int res = 0, service_name_len = 0;
char *msgid_str = NULL;
kk_msg_request_payload_t request;
kk_msg_response_t response;
memset(&request, 0, sizeof(kk_msg_request_payload_t));
memset(&response, 0, sizeof(kk_msg_response_t));
if (devid < 0 || msgid < 0 || identifier == NULL || identifier_len <= 0 ||
payload == NULL || payload_len <= 0) {
return INVALID_PARAMETER;
}
/* Response Msg ID */
res = kk_utils_itoa(msgid, &msgid_str);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
request.id.value = msgid_str;
request.id.value_length = strlen(msgid_str);
/* Service Name */
res = _kk_mgr_upstream_response_assemble(devid, msgid_str, strlen(msgid_str), identifier, code,
&request,
&response);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
dm_msg_response(&request, &response, payload, payload_len, NULL);
free(response.msgTypeStr);
free(msgid_str);
return SUCCESS_RETURN;
}
const char DM_URI_THING_SUB_REGISTER[] = "/thing/sub/register";
int dm_mgr_upstream_thing_sub_register(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_SUB_REGISTER;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_sub_register( node->productCode,node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_sub_register_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_SUB_UNREGISTER[] = "/thing/sub/unregister";
int dm_mgr_upstream_thing_sub_unregister(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_SUB_UNREGISTER;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_sub_unregister(node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_sub_unregister_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_TOPO_ADD[] = "/thing/topo/add";
int dm_mgr_upstream_thing_topo_add(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR res:%d\n",res);
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_TOPO_ADD;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_topo_add( node->productCode, node->deviceCode, node->mac,&request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_topo_add_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_TOPO_DELETE[] = "/thing/topo/delete";
int dm_mgr_upstream_thing_topo_delete(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return DM_INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_TOPO_DELETE;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_topo_delete( node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_topo_delete_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_TOPO_GET[] = "/thing/topo/get";
int dm_mgr_upstream_thing_topo_get(void)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_msg_request_t request;
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_TOPO_GET;
HAL_GetProduct_Code(request.deviceCode);
res = _dm_mgr_search_dev_by_pkdn(request.deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Params And Method */
res = dm_msg_thing_topo_get(&request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = node->devid;
/* Callback */
//request.callback = dm_client_thing_topo_get_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_THING_LIST_FOUND[] = "/thing/list/found";
int dm_mgr_upstream_thing_list_found(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_THING_LIST_FOUND;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_thing_list_found( node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_thing_list_found_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_STATUS_CLOUD[] = "/thing/ccu/cloudstate";
int dm_mgr_ccu_status_cloud(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
memcpy(request.productCode,node->productCode,strlen(node->productCode));
memcpy(request.deviceCode,node->deviceCode,strlen(node->deviceCode));
request.msgTypeStr = DM_URI_STATUS_CLOUD;
/* Get Params And Method */
res = dm_msg_status_cloud( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_STATUS_ONLINE[] = "/thing/status/online";
int dm_mgr_upstream_status_online(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if(strlen(node->fatherDeviceCode) > 0){
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
}else{
memcpy(request.productCode,node->productCode,strlen(node->productCode));
memcpy(request.deviceCode,node->deviceCode,strlen(node->deviceCode));
}
request.msgTypeStr = DM_URI_STATUS_ONLINE;
/* Get Params And Method */
res = dm_msg_status_online( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_STATUS_OFFLINE[] = "/thing/status/offline";
int dm_mgr_upstream_status_offline(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if(strlen(node->fatherDeviceCode) > 0){
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
}else{
memcpy(request.productCode,node->productCode,strlen(node->productCode));
memcpy(request.deviceCode,node->deviceCode,strlen(node->deviceCode));
}
request.msgTypeStr = DM_URI_STATUS_OFFLINE;
/* Get Params And Method */
res = dm_msg_status_offline( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_COMBINE_LOGIN[] = "/thing/combine/login";
int dm_mgr_upstream_combine_login(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_COMBINE_LOGIN;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_combine_login( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_COMBINE_LOGOUT[] = "/thing/combine/logout";
int dm_mgr_upstream_combine_logout(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(node->fatherDeviceCode,&gw_node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = DM_URI_COMBINE_LOGOUT;
memcpy(request.productCode,gw_node->productCode,strlen(gw_node->productCode));
memcpy(request.deviceCode,gw_node->deviceCode,strlen(gw_node->deviceCode));
/* Get Params And Method */
res = dm_msg_combine_logout( node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_logout_reply;
/* Send Message To Cloud */
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char KK_URI_OTA_INFORM[] = "/ota/device/inform/%s/%s";
int dm_mgr_ota_report_version(_IN_ int devid, char *version)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.msgTypeStr = malloc(strlen(KK_URI_OTA_INFORM)+strlen(node->productCode)+strlen(node->deviceCode)+1);
if(request.msgTypeStr == NULL){
return MEMORY_NOT_ENOUGH;
}
sprintf(request.msgTypeStr,KK_URI_OTA_INFORM,node->productCode,node->deviceCode);
memcpy(request.productCode, node->productCode, PRODUCT_CODE_MAXLEN);
memcpy(request.deviceCode, node->deviceCode, DEVICE_CODE_MAXLEN);
/* Get Params And Method */
res = dm_msg_ota_report_version(version, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_logout_reply;
/* Send Message To Cloud */
/* Send Message To Cloud */
res = dm_msg_request(&request,0);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
free(request.msgTypeStr);
return res;
}
int dm_mgr_subdev_create(int devtype,_IN_ char productCode[PRODUCT_CODE_MAXLEN], _IN_ char deviceCode[DEVICE_CODE_MAXLEN],
_IN_ char mac[DEVICE_MAC_MAXLEN],_IN_ char fatherDeviceCode[DEVICE_CODE_MAXLEN], _OU_ int *devid){
int res = 0;
res = dm_mgr_device_create(devtype,productCode,deviceCode,mac,fatherDeviceCode, devid);
if(TSL_ALREADY_EXIST == res)
{
ERROR_PRINT("SUBDEV ALREADY EXIST!!!\n");
}
return res;
}
int dm_mgr_subdev_delete(_IN_ char deviceCode[DEVICE_CODE_MAXLEN])
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if(deviceCode == NULL || strlen(deviceCode) <=0){
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
INFO_PRINT("dm_mgr_subdev_delete deviceCode:%s\n",deviceCode);
res = kk_subDev_delete_by_dcode(deviceCode);//delete sub db data
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = kk_property_delete_by_dcode(deviceCode);//delete properties db data
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(deviceCode,&node);
if (res != SUCCESS_RETURN) {
ERROR_PRINT("ERROR [%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
if(node->dev_type == KK_DM_DEVICE_CCU){
ERROR_PRINT("ERROR [%s][%d]\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
list_del(&node->linked_list);
if (node->dev_shadow) {
kk_tsl_destroy(&node->dev_shadow);
}
free(node);
node = NULL;
return SUCCESS_RETURN;
}
......@@ -738,11 +738,78 @@ int dm_msg_thing_property_set_reply(char deviceCode[DEVICE_CODE_MAXLEN],char *pa
dm_msg_response(&request, &response, "{}", strlen("{}"), NULL);
return SUCCESS_RETURN;
}
int dm_msg_thing_event_post(const char *deviceCode, const char *identifier)
{
int res = 0;
int nums = 0;
int idx = 0;
char *payload = NULL;
dm_mgr_dev_node_t *node = NULL;
void *event = NULL;
char *method = NULL;
if(deviceCode == NULL){
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
payload = kk_tsl_get_post_event_str(node->dev_shadow,identifier,strlen(identifier));
if(payload != NULL){
res = kk_tsl_get_event_by_identifier(node->dev_shadow, identifier, &event);
if (res != SUCCESS_RETURN) {
free(payload);
return FAIL_RETURN;
}
res = kk_tsl_get_event_method(event, &method);
if (res != SUCCESS_RETURN) {
free(payload);
return FAIL_RETURN;
}
dm_mgr_upstream_thing_event_post(node->devid, identifier, strlen(identifier), method, payload, strlen(payload));
free(method);
method = NULL;
free(payload);
payload = NULL;
}
return SUCCESS_RETURN;
}
int dm_msg_thing_service_post(const char *deviceCode, const char *identifier,int response_id,int code)
{
int res = 0;
int nums = 0;
int idx = 0;
char *payload = NULL;
dm_mgr_dev_node_t *node = NULL;
if(deviceCode == NULL){
return FAIL_RETURN;
}
res = dm_mgr_get_device_by_devicecode(deviceCode, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
payload = kk_tsl_get_post_service_str(node->dev_shadow,identifier);
if(payload != NULL){
dm_mgr_deprecated_upstream_thing_service_response(node->devid,response_id,code, identifier, strlen(identifier), payload, strlen(payload));
free(payload);
payload = NULL;
}
return SUCCESS_RETURN;
}
int dm_msg_thing_property_post_all(char *deviceCode)
{
int res = 0;
int nums = 0;
int idx = 0;
char *payload = NULL;
dm_mgr_dev_node_t *node = NULL;
if(deviceCode == NULL){
......@@ -753,7 +820,12 @@ int dm_msg_thing_property_post_all(char *deviceCode)
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
kk_tsl_post_property(node->devid,NULL,1);//here need async post
payload = kk_tsl_get_post_property_str(node->dev_shadow,NULL);//here need async post
if(payload != NULL){
dm_mgr_upstream_thing_property_post(node->devid, payload, strlen(payload),1);
free(payload);
payload = NULL;
}
return SUCCESS_RETURN;
}
......@@ -784,7 +856,6 @@ int dm_msg_thing_property_post_by_identify(char *deviceCode,cJSON *params)
}
propertyItem = cJSON_GetObjectItem(params, property->identifier);
if(propertyItem != NULL){
//kk_tsl_post_property(node->devid,property->identifier,0);
/*update the db data */
if(property->data_value.type == KK_TSL_DATA_TYPE_INT ||
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -328,11 +328,12 @@ int kk_property_sync_values(const char *deviceCode)
char *pIdentifier = NULL;
char *valueStr = NULL;
int valueType = 0;
int devId = 0;
kk_property_db_ctx_t *ctx = _kk_property_db_get_ctx();
char *searchCmd = "select * from PropertiesInfo;";
dm_mgr_dev_node_t *node = NULL;
dm_mgr_get_devId_by_devicecode(deviceCode,&devId);
dm_mgr_get_device_by_devicecode(deviceCode,&node);
sqlCmd = sqlite3_mprintf(searchCmd,deviceCode);
......@@ -351,18 +352,18 @@ int kk_property_sync_values(const char *deviceCode)
valueType == KK_TSL_DATA_TYPE_ENUM||
valueType == KK_TSL_DATA_TYPE_BOOL){
int value_int = atoi(valueStr);
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,pIdentifier,&value_int,NULL);
res = kk_tsl_set_value(kk_tsl_set_property_value,node->dev_shadow,pIdentifier,&value_int,NULL);
}
else if(valueType == KK_TSL_DATA_TYPE_FLOAT){
float value_float = atoi(valueStr);
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,pIdentifier,&value_float,NULL);
res = kk_tsl_set_value(kk_tsl_set_property_value,node->dev_shadow,pIdentifier,&value_float,NULL);
}
else if(valueType == KK_TSL_DATA_TYPE_DOUBLE){
double value_double = atoi(valueStr);
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,pIdentifier,&value_double,NULL);
res = kk_tsl_set_value(kk_tsl_set_property_value,node->dev_shadow,pIdentifier,&value_double,NULL);
}
else{
res = kk_tsl_set_value(kk_tsl_set_property_value,devId,pIdentifier,NULL,valueStr);
res = kk_tsl_set_value(kk_tsl_set_property_value,node->dev_shadow,pIdentifier,NULL,valueStr);
}
if(res != SUCCESS_RETURN)
{
......
#include <stdio.h>
#include "kk_tsl_api.h"
#include "kk_wlist_mng.h"
#include "sqlite3.h"
#include "kk_log.h"
#define KK_TSL_GATAWAY_ADDWHITELIST_IDENTIFIER "addWhiteList"
#define KK_TSL_GATAWAY_WHITELISTDEVICE_IDENTIFIER "Devices"
#define KK_TSL_GATAWAY_WHITELISTMAC_IDENTIFIER "getWhiteListNotification.Devices[%d].MACAddress"
#define KK_TSL_GATAWAY_WHITELISTPRODUCTID_IDENTIFIER "getWhiteListNotification.Devices[%d].ProductId"
#define KK_TSL_GATAWAY_WHITELISTSN_IDENTIFIER "getWhiteListNotification.Devices[%d].SN"
#define KK_TSL_GATAWAY_WHITELISTDEVICEID_IDENTIFIER "getWhiteListNotification.Devices[%d].deviceId"
#define KK_WLIST_DB_FILE "wlist.db"
typedef struct {
void *mutex;
int isOpened;
int isInited;
int wlistNum;
sqlite3 *pDb;
} kk_wlist_ctx_t;
static kk_wlist_ctx_t s_kk_wlist_ctx = {NULL,0,0,NULL};
static kk_wlist_ctx_t *_kk_wlist_get_ctx(void)
{
return &s_kk_wlist_ctx;
}
static void _kk_wlist_lock(void)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
if (ctx->mutex) {
HAL_MutexLock(ctx->mutex);
}
}
static void _kk_wlist_unlock(void)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
}
static int kk_wlist_db_Init(void)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
//eUtils_LockLock(&sLock);
_kk_wlist_lock();
if (sqlite3_open_v2(KK_WLIST_DB_FILE, &ctx->pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) != SQLITE_OK)
{
ERROR_PRINT("Error initialising linkage database (%s)", sqlite3_errmsg(ctx->pDb));
_kk_wlist_unlock();
return FAIL_RETURN;
}
INFO_PRINT("wlist Database opened\n");
{
const char *pwListTable = "CREATE TABLE IF NOT EXISTS WhiteList(idx INTEGER, deviceMac varchar(17),productId varchar(33), deviceSN varchar(33), deviceId varchar(33))";
char *pcErr;
// DBG_vPrintf(DBG_SQL, "Execute SQL: '%s'\n", pConditionTableDef);
if (sqlite3_exec(ctx->pDb, pwListTable, NULL, NULL, &pcErr) != SQLITE_OK)
{
ERROR_PRINT("Error creating table (%s)\n", pcErr);
sqlite3_free(pcErr);
//eUtils_LockUnlock(&sLock);
_kk_wlist_unlock();
return FAIL_RETURN;
}
}
//eUtils_LockUnlock(&sLock);
_kk_wlist_unlock();
return SUCCESS_RETURN;
}
static int _kk_check_exist(const char* device_mac)
{
int isExist = 0;
sqlite3_stmt *stmt;
char *pmac = NULL;
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
const char *searchCmd = "select * from WhiteList;";
sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);
INFO_PRINT("total_column = %d\n", sqlite3_column_count(stmt));
while(sqlite3_step(stmt) == SQLITE_ROW){
pmac = sqlite3_column_text(stmt, 1);
if(!strcmp(device_mac,pmac))
{
isExist = 1;
break;
}
}
INFO_PRINT("\n");
sqlite3_finalize(stmt);
return isExist;
}
static int kk_wlist_insert_db(int index,char device_mac[DEVICE_MAC_MAXLEN],char product_Id[PRODUCT_CODE_MAXLEN], \
char device_SN[DEVICE_SN_MAXLEN],char device_Id[DEVICE_CODE_MAXLEN])
{
const char *insertCmd = "insert into WhiteList (idx, deviceMac, productId,deviceSN,deviceId) values ('%d', '%s', '%s','%s','%s');";
char *sqlCmd = NULL;
int rc = 0;
char *zErrMsg = 0;
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
_kk_wlist_lock();
sqlCmd = sqlite3_mprintf(insertCmd, index,device_mac,product_Id,device_SN,device_Id);
rc = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
INFO_PRINT("Table insert data successfully\n");
}
sqlite3_free(sqlCmd);
_kk_wlist_unlock();
return SUCCESS_RETURN;
}
static int _kk_wlist_delete_db_byMac(char device_mac[DEVICE_MAC_MAXLEN])
{
const char *deleteCmd = "delete from WhiteList where deviceMac = %s;";
char *sqlCmd = NULL;
int rc = 0;
char *zErrMsg = 0;
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
_kk_wlist_lock();
sqlCmd = sqlite3_mprintf( deleteCmd,device_mac);
INFO_PRINT("Table delete data sqlCmd:%s\n",sqlCmd);
rc = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
INFO_PRINT("Table delete data successfully\n");
}
sqlite3_free(sqlCmd);
_kk_wlist_unlock();
return SUCCESS_RETURN;
}
int kk_wlist_status_open(int isOpen)
{
int res = 0;
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
if(ctx->isOpened)
{
printf("[%s][%d] ALREADY OPENED\n",__FUNCTION__,__LINE__,res);
return SUCCESS_RETURN;
}
res = kk_tsl_set_value(kk_tsl_set_property_value,0,KK_TSL_GATAWAY_WHITELIST_IDENTIFIER,&isOpen,NULL);
if(res != SUCCESS_RETURN){
ERROR_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
ctx->isOpened = isOpen;
if(ctx->isOpened){
kk_wlist_init();
}
return SUCCESS_RETURN;
}
int kk_load_wlist(void)
{
const char *searchCmd = "select * from WhiteList;";
sqlite3_stmt *stmt;
char *pmac = NULL,*pPID = NULL,*pSN = NULL,pDID = NULL;
kk_wlist_dev_t wlist = {0};
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
if(ctx->isOpened != 1){
return FAIL_RETURN;
}
sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);
INFO_PRINT("kk_load_wlist total_column = %d\n", sqlite3_column_count(stmt));
while(sqlite3_step(stmt) == SQLITE_ROW){
memset(&wlist,0x0,sizeof(kk_wlist_dev_t));
strcpy(wlist.device_mac,sqlite3_column_text(stmt, 1));
strcpy(wlist.product_id,sqlite3_column_text(stmt, 2));
strcpy(wlist.device_sn,sqlite3_column_text(stmt, 3));
strcpy(wlist.device_id,sqlite3_column_text(stmt, 4));
// kk_add_wlist(&wlist);
kk_set_wlist_byIdx(ctx->wlistNum,&wlist);
ctx->wlistNum++;
}
sqlite3_finalize(stmt);
return SUCCESS_RETURN;
}
int kk_wlist_init(void)
{
int res = 0;
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
if(ctx->isInited)
{
return SUCCESS_RETURN;
}
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
return FAIL_RETURN;
}
res = kk_wlist_db_Init();
if(res != SUCCESS_RETURN){
ERROR_PRINT("[%s][%d]kk_wlist_db_Init FAIL!!!\n",__FUNCTION__,__LINE__);
}
kk_load_wlist();
ctx->isInited = 1;
return SUCCESS_RETURN;
}
int kk_set_wlist_byIdx(int idx,kk_wlist_dev_t * wlist_dev)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
int res = 0;
char identifier_name[64] = {0};
if(wlist_dev == NULL)
{
ERROR_PRINT("[%s][%d] wlist_dev == NULL!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
_kk_wlist_lock();
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTMAC_IDENTIFIER,idx);
res = kk_tsl_set_value(kk_tsl_set_event_output_value,0,identifier_name,NULL,wlist_dev->device_mac);
if(res != SUCCESS_RETURN){
goto fail_return;
}
memset(identifier_name,0x0,sizeof(identifier_name));
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTPRODUCTID_IDENTIFIER,idx);
res = kk_tsl_set_value(kk_tsl_set_event_output_value,0,identifier_name,NULL,wlist_dev->product_id);
if(res != SUCCESS_RETURN){
goto fail_return;
}
memset(identifier_name,0x0,sizeof(identifier_name));
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTSN_IDENTIFIER,idx);
res = kk_tsl_set_value(kk_tsl_set_event_output_value,0,identifier_name,NULL,wlist_dev->device_sn);
if(res != SUCCESS_RETURN){
goto fail_return;
}
memset(identifier_name,0x0,sizeof(identifier_name));
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTDEVICEID_IDENTIFIER,idx);
res = kk_tsl_set_value(kk_tsl_set_event_output_value,0,identifier_name,NULL,wlist_dev->device_id);
if(res != SUCCESS_RETURN){
goto fail_return;
}
_kk_wlist_unlock();
return SUCCESS_RETURN;
fail_return:
_kk_wlist_unlock();
INFO_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
int kk_add_wlist(kk_wlist_dev_t *wlist_dev)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
int res = 0;
if(ctx->isOpened != 1){
ERROR_PRINT("[%s][%d] PLEASE OPNE THE WHITELIST FUNCTION FIRST!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
if(ctx->wlistNum >= 512){
ERROR_PRINT("[%s][%d] WHITELIST FULL!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
if(wlist_dev == NULL){
ERROR_PRINT("[%s][%d] wlist_dev == NULL!!!\n",__FUNCTION__,__LINE__);
return INVALID_PARAMETER;
}
if(_kk_check_exist(wlist_dev->device_mac) == 1)
{
ERROR_PRINT("[%s][%d] DATA ALREADY EXIST!!!\n",__FUNCTION__,__LINE__);
return SUCCESS_RETURN;
}
res = kk_wlist_insert_db(ctx->wlistNum,wlist_dev->device_mac,wlist_dev->product_id,wlist_dev->device_sn,wlist_dev->device_id);
res |= kk_set_wlist_byIdx(ctx->wlistNum,wlist_dev);
ctx->wlistNum++;
return res;
}
int kk_get_wlist_num(void)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
if(ctx->isOpened != 1)
{
ERROR_PRINT("[%s][%d] PLEASE OPNE THE WHITELIST FUNCTION FIRST!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
return ctx->wlistNum;
}
int kk_getwlist_ByIdx(int idx,kk_wlist_dev_t* wlist_dev)
{
char macs[DEVICE_MAC_MAXLEN] = {0};
char PID[PRODUCT_CODE_MAXLEN] = {0};
char DID[DEVICE_CODE_MAXLEN] = {0};
char SN[PRODUCT_CODE_MAXLEN] = {0};
char identifier_name[64] = {0};
int res = 0;
_kk_wlist_lock();
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTMAC_IDENTIFIER,idx);
res = kk_tsl_get_value(kk_tsl_get_event_output_value,0,identifier_name,macs,NULL);
if(res != SUCCESS_RETURN){
goto fail_return;
}
strcpy(wlist_dev->device_mac,macs);
memset(identifier_name,0x0,sizeof(identifier_name));
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTPRODUCTID_IDENTIFIER,idx);
res = kk_tsl_get_value(kk_tsl_get_event_output_value,0,identifier_name,PID,NULL);
if(res != SUCCESS_RETURN){
goto fail_return;
}
strcpy(wlist_dev->product_id,PID);
memset(identifier_name,0x0,sizeof(identifier_name));
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTSN_IDENTIFIER,idx);
res = kk_tsl_get_value(kk_tsl_get_event_output_value,0,identifier_name,SN,NULL);
if(res != SUCCESS_RETURN){
goto fail_return;
}
strcpy(wlist_dev->device_sn,SN);
memset(identifier_name,0x0,sizeof(identifier_name));
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTDEVICEID_IDENTIFIER,idx);
res = kk_tsl_get_value(kk_tsl_get_event_output_value,0,identifier_name,DID,NULL);
if(res != SUCCESS_RETURN){
goto fail_return;
}
strcpy(wlist_dev->device_id,DID);
_kk_wlist_unlock();
return SUCCESS_RETURN;
fail_return:
_kk_wlist_unlock();
INFO_PRINT("[%s][%d] res:%d\n",__FUNCTION__,__LINE__,res);
return FAIL_RETURN;
}
int kk_get_wlistIdx_byMac(const char* device_mac)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
int res = 0;
char pmac[DEVICE_MAC_MAXLEN] = {0};
char identifier_name[64] = {0};
int idx = 0,findIdx = 0;
if(ctx->isOpened != 1)
{
ERROR_PRINT("[%s][%d] PLEASE OPNE THE WHITELIST FUNCTION FIRST!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
if(idx >= 512)
{
ERROR_PRINT("[%s][%d]Invalid idx!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
_kk_wlist_lock();
for(idx = 0; idx < ctx->wlistNum;idx++)
{
memset(identifier_name,0x0,sizeof(identifier_name));
sprintf(identifier_name,KK_TSL_GATAWAY_WHITELISTMAC_IDENTIFIER,idx);
res = kk_tsl_get_value(kk_tsl_get_event_output_value,0,identifier_name,pmac,NULL);
if(res != SUCCESS_RETURN){
continue;
}
if(!strcmp(device_mac,pmac))
{
findIdx = idx;
ERROR_PRINT("[%s][%d]find the delete index\n",__FUNCTION__,__LINE__);
_kk_wlist_unlock();
return findIdx;
}
}
if(idx == ctx->wlistNum)
{
_kk_wlist_unlock();
return TSL_EVENT_NOT_EXIST;
}
_kk_wlist_unlock();
return FAIL_RETURN;
}
static int _kk_delete_wlist_byIdx(int idx)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
int res = 0;
kk_wlist_dev_t dWlist = {0};
if(idx >= 512)
{
ERROR_PRINT("[%s][%d]Invalid idx!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
strcpy(dWlist.product_id,"");
strcpy(dWlist.device_mac,"");
strcpy(dWlist.device_sn,"");
strcpy(dWlist.device_id,"");
res = kk_set_wlist_byIdx(idx,&dWlist);
return res;
}
int kk_delete_wlist_byMac(const char* device_mac)
{
kk_wlist_ctx_t *ctx = _kk_wlist_get_ctx();
char identifier_name[64] = {0};
int res;
int idx = 0;
char *pmac = NULL;
int findIdx = 0;
if(ctx->isOpened != 1)
{
ERROR_PRINT("[%s][%d] PLEASE OPNE THE WHITELIST FUNCTION FIRST!!!\n",__FUNCTION__,__LINE__);
return FAIL_RETURN;
}
findIdx = kk_get_wlistIdx_byMac(device_mac);
if(findIdx < 0)
{
return FAIL_RETURN;
}
res = _kk_delete_wlist_byIdx(findIdx);
res |= _kk_wlist_delete_db_byMac(device_mac);
if(res != SUCCESS_RETURN)
{
return FAIL_RETURN;
}
ctx->wlistNum--;
return SUCCESS_RETURN;
}
#ifndef __KK_WHITELIST_MNG_H__
#define __KK_WHITELIST_MNG_H__
#include "kk_tsl_common.h"
typedef struct {
char device_mac[DEVICE_MAC_MAXLEN];
char product_id[PRODUCT_TYPE_MAXLEN];
char device_sn[DEVICE_SN_MAXLEN];
char device_id[DEVICE_CODE_MAXLEN];
int index;
} kk_wlist_dev_t;
int kk_wlist_init(void);
int kk_wlist_status_open(int isOpen);
int kk_add_wlist(kk_wlist_dev_t *wlist_dev);
int kk_get_wlist_num(void);
int kk_delete_wlist_byMac(const char* device_mac);
#endif
\ No newline at end of file
......@@ -223,7 +223,9 @@ void kk_platMsg_handle(void* data, char* chalMark){
}else if (strstr(msgType->valuestring, KK_THING_PROPERTY_POST) != NULL){
INFO_PRINT("save property and send to cloud \n");
char* outstr = cJSON_Print(payload);
kk_tsl_property_set_by_devicecode(info_dcode->valuestring, outstr, strlen(outstr)+1);
kk_tsl_t *dev_shadow = NULL;
dm_mgr_get_device_shadow_by_devicecode(info_dcode->valuestring,&dev_shadow);
kk_tsl_property_set_by_shadow(dev_shadow, outstr, strlen(outstr)+1);
dm_msg_thing_property_post_by_identify(info_dcode->valuestring,jsonPay);
free(outstr);
......@@ -236,7 +238,6 @@ void kk_platMsg_handle(void* data, char* chalMark){
}else{
INFO_PRINT("kk_platMsg_handle data: don't handle it [%s]\n",data);
//kk_tsl_service_property_set(topic->valuestring, payload->valuestring, strlen(payload->valuestring), NULL);
}
error:
cJSON_Delete(json);
......@@ -580,6 +581,8 @@ void *ccu_property_monitor(void *args)
int res = 0;
int needReport = 0;
int time_second = 60;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_search_dev_by_devid(KK_DM_DEVICE_CCU_DEVICEID,&node);
while (mid_ctx->g_ccuProChg_dispatch_thread_running) {
if(kk_get_cloud_recv_status() == 0){
......@@ -587,15 +590,15 @@ void *ccu_property_monitor(void *args)
sleep(10);
continue;
}
//dm_ota_yield(MID_YIELD_TIMEOUT_MS);
HAL_Get_IP(s_IP,NULL);
res = kk_tsl_get_value(kk_tsl_get_property_value,0,KK_TSL_CCU_WANIP_IDENTIFIER,s_IP_TSL,NULL);
res = kk_tsl_get_value(kk_tsl_get_property_value,node->dev_shadow,KK_TSL_CCU_WANIP_IDENTIFIER,s_IP_TSL,NULL);
if(res != SUCCESS_RETURN){
ERROR_PRINT("kk_tsl_get_value Failed\n");
}
else{
if(strcmp(s_IP,s_IP_TSL)){
kk_tsl_set_value(kk_tsl_set_property_value,0,KK_TSL_CCU_WANIP_IDENTIFIER,NULL,s_IP);
kk_tsl_set_value(kk_tsl_set_property_value,node->dev_shadow,KK_TSL_CCU_WANIP_IDENTIFIER,NULL,s_IP);
INFO_PRINT("current ip:%s,before ip:%s\n",s_IP,s_IP_TSL);
kk_property_db_update("CCU_66666");
needReport = 1;
......@@ -604,7 +607,12 @@ void *ccu_property_monitor(void *args)
if(needReport&&(kk_get_cloudstatus() == 1)){
needReport = 0;
kk_tsl_post_property(KK_DM_DEVICE_CCU_DEVICEID,NULL,0);
char *postStr = kk_tsl_get_post_property_str(node->dev_shadow,NULL);
if(postStr != NULL){
dm_mgr_upstream_thing_property_post(node->devid, postStr, strlen(postStr),0);
free(postStr);
postStr = NULL;
}
}
sleep(time_second);
}
......
......@@ -5,32 +5,37 @@
#include "kk_dm_msg.h"
#include "kk_log.h"
#ifndef _ZLOG_
#undef INFO_PRINT
#undef WARNING_PRINT
#undef ERROR_PRINT
#define INFO_PRINT printf
#define WARNING_PRINT printf
#define ERROR_PRINT printf
#else
#include "kk_log.h"
#endif
typedef enum {
KK_MSG_PROPERTY_SET,
KK_MSG_SERVICE_SET
} kk_msg_set_type_t;
typedef int (*dm_get_shadow_data)(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
typedef int (*dm_set_shadow_data)(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
typedef int (*dm_get_shadow_data)(_IN_ kk_tsl_t * dev_shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
typedef int (*dm_set_shadow_data)(_IN_ kk_tsl_t * dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
static int _kk_msg_set_object(kk_msg_set_type_t type, int devid, char *key, lite_cjson_t *root);
static int _kk_msg_get_property_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
static int _kk_msg_set_object(kk_msg_set_type_t type, kk_tsl_t *dev_shadow, char *key, lite_cjson_t *root);
static int _kk_msg_get_property_data(_IN_ kk_tsl_t * dev_shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = kk_tsl_get_property_data(node->dev_shadow, key, key_len, data);
res = kk_tsl_get_property_data(dev_shadow, key, key_len, data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -38,21 +43,15 @@ static int _kk_msg_get_property_data(_IN_ int devid, _IN_ char *key, _IN_ int ke
return SUCCESS_RETURN;
}
#if 1
int kk_msg_get_event_output_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
int kk_msg_get_event_output_data(_IN_ kk_tsl_t * dev_shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tsl_get_event_output_data(node->dev_shadow, key, key_len, data);
res = dm_tsl_get_event_output_data(dev_shadow, key, key_len, data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -60,66 +59,34 @@ int kk_msg_get_event_output_data(_IN_ int devid, _IN_ char *key, _IN_ int key_le
return SUCCESS_RETURN;
}
int kk_msg_set_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tls_set_event_output_value(node->dev_shadow, key, key_len, value, value_len);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
return SUCCESS_RETURN;
}
#endif
static int _kk_msg_get_service_input_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
static int _kk_msg_get_service_input_data(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tsl_get_service_input_output_data(KK_TSL_DATA_TARGET_SERVICE_INPUT_DATA, node->dev_shadow, key, key_len, data);
res = dm_tsl_get_service_input_output_data(KK_TSL_DATA_TARGET_SERVICE_INPUT_DATA,dev_shadow, key, key_len, data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
return SUCCESS_RETURN;
}
static int _kk_msg_get_service_output_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
static int _kk_msg_get_service_output_data(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tsl_get_service_input_output_data(KK_TSL_DATA_TARGET_SERVICE_OUTPUT_DATA, node->dev_shadow, key, key_len,
res = dm_tsl_get_service_input_output_data(KK_TSL_DATA_TARGET_SERVICE_OUTPUT_DATA,dev_shadow, key, key_len,
data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
......@@ -128,21 +95,16 @@ static int _kk_msg_get_service_output_data(_IN_ int devid, _IN_ char *key, _IN_
return SUCCESS_RETURN;
}
static int _kk_msg_set_service_input_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
static int _kk_msg_set_service_input_value(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tsl_set_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_INPUT_DATA, node->dev_shadow, key, key_len,
res = dm_tsl_set_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_INPUT_DATA, dev_shadow, key, key_len,
value, value_len);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
......@@ -150,22 +112,16 @@ static int _kk_msg_set_service_input_value(_IN_ int devid, _IN_ char *key, _IN_
return SUCCESS_RETURN;
}
static int _kk_msg_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
static int _kk_msg_set_service_output_value(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tsl_set_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_OUTPUT_DATA, node->dev_shadow, key, key_len,
res = dm_tsl_set_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_OUTPUT_DATA, dev_shadow, key, key_len,
value, value_len);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
......@@ -176,22 +132,16 @@ static int _kk_msg_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_
static int _kk_msg_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
static int _kk_msg_set_property_value(_IN_ kk_tsl_t *dev_shadow , _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tsl_set_property_value(node->dev_shadow, key, key_len, value, value_len);
res = dm_tsl_set_property_value(dev_shadow, key, key_len, value, value_len);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -199,18 +149,18 @@ static int _kk_msg_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int k
return SUCCESS_RETURN;
}
int _kk_tsl_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
int _kk_tsl_set_property_value(_IN_ kk_tsl_t * dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char *value_str)
{
int res = 0;
void *data = NULL;
kk_tsl_data_type_e type;
if (devid < 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
if (dev_shadow == NULL|| key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
return INVALID_PARAMETER;
}
res = _kk_msg_get_property_data(devid, key, key_len, &data);
res = _kk_msg_get_property_data(dev_shadow, key, key_len, &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -225,23 +175,23 @@ int _kk_tsl_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: {
int value_int = (value == NULL) ? (atoi(value_str)) : (*(int *)value);
res = _kk_msg_set_property_value(devid, key, key_len, &value_int, sizeof(int));
res = _kk_msg_set_property_value(dev_shadow, key, key_len, &value_int, sizeof(int));
}
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = (value == NULL) ? (atof(value_str)) : (*(float *)value);
res = _kk_msg_set_property_value(devid, key, key_len, &value_float, sizeof(float));
res = _kk_msg_set_property_value(dev_shadow, key, key_len, &value_float, sizeof(float));
}
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
double value_double = (value == NULL) ? (atof(value_str)) : (*(double *)value);
res = _kk_msg_set_property_value(devid, key, key_len, &value_double, sizeof(double));
res = _kk_msg_set_property_value(dev_shadow, key, key_len, &value_double, sizeof(double));
}
break;
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
char *value_string = (value == NULL) ? (value_str) : (value);
res = _kk_msg_set_property_value(devid, key, key_len, value_string, strlen(value_string));
res = _kk_msg_set_property_value(dev_shadow, key, key_len, value_string, strlen(value_string));
}
break;
default: {
......@@ -256,7 +206,7 @@ int _kk_tsl_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
return SUCCESS_RETURN;
}
static int _kk_msg_set_numberOrStr(kk_msg_set_type_t type, int devid, char *key, lite_cjson_t *root)
static int _kk_msg_set_numberOrStr(kk_msg_set_type_t type, kk_tsl_t *dev_shadow, char *key, lite_cjson_t *root)
{
int res = 0;
void *data = NULL;
......@@ -268,7 +218,7 @@ static int _kk_msg_set_numberOrStr(kk_msg_set_type_t type, int devid, char *key,
/* dm_log_debug("Current Key: %s", key); */
res = get_shadow_data_func(devid, key, strlen(key), &data);
res = get_shadow_data_func(dev_shadow, key, strlen(key), &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -286,21 +236,21 @@ static int _kk_msg_set_numberOrStr(kk_msg_set_type_t type, int devid, char *key,
if (root->type == cJSON_String && root->value_length > 0){
root->value_int = atoi(root->value);
}
res = set_shadow_data_func(devid, key, strlen(key), &root->value_int, 0);
res = set_shadow_data_func(dev_shadow, key, strlen(key), &root->value_int, 0);
}
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = (float)root->value_double;
res = set_shadow_data_func(devid, key, strlen(key), &value_float, 0);
res = set_shadow_data_func(dev_shadow, key, strlen(key), &value_float, 0);
}
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
res = set_shadow_data_func(devid, key, strlen(key), &root->value_double, 0);
res = set_shadow_data_func(dev_shadow, key, strlen(key), &root->value_double, 0);
}
break;
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
res = set_shadow_data_func(devid, key, strlen(key), root->value, root->value_length);
res = set_shadow_data_func(dev_shadow, key, strlen(key), root->value, root->value_length);
}
break;
default:
......@@ -311,7 +261,7 @@ static int _kk_msg_set_numberOrStr(kk_msg_set_type_t type, int devid, char *key,
return res;
}
static int _kk_msg_set_array(kk_msg_set_type_t type, int devid, char *key, lite_cjson_t *root)
static int _kk_msg_set_array(kk_msg_set_type_t type, kk_tsl_t *dev_shadow, char *key, lite_cjson_t *root)
{
int res = 0, index = 0;
lite_cjson_t lite_item_value;
......@@ -351,16 +301,16 @@ static int _kk_msg_set_array(kk_msg_set_type_t type, int devid, char *key, lite_
free(ascii_index);
if (lite_cjson_is_object(&lite_item_value)) {
res = _kk_msg_set_object(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_object(type, dev_shadow, new_key, &lite_item_value);
}
if (lite_cjson_is_array(&lite_item_value)) {
res = _kk_msg_set_array(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_array(type, dev_shadow, new_key, &lite_item_value);
}
if (lite_cjson_is_number(&lite_item_value)) {
res = _kk_msg_set_numberOrStr(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_numberOrStr(type, dev_shadow, new_key, &lite_item_value);
}
if (lite_cjson_is_string(&lite_item_value)) {
res = _kk_msg_set_numberOrStr(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_numberOrStr(type, dev_shadow, new_key, &lite_item_value);
}
free(new_key);
......@@ -372,7 +322,7 @@ static int _kk_msg_set_array(kk_msg_set_type_t type, int devid, char *key, lite_
return SUCCESS_RETURN;
}
static int _kk_msg_set_object(kk_msg_set_type_t type, int devid, char *key, lite_cjson_t *root)
static int _kk_msg_set_object(kk_msg_set_type_t type, kk_tsl_t *dev_shadow, char *key, lite_cjson_t *root)
{
int res = 0, index = 0;
lite_cjson_t lite_item_key;
......@@ -405,16 +355,16 @@ static int _kk_msg_set_object(kk_msg_set_type_t type, int devid, char *key, lite
/* dm_log_debug("New Key: %s", new_key); */
if (lite_cjson_is_object(&lite_item_value)) {
res = _kk_msg_set_object(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_object(type, dev_shadow, new_key, &lite_item_value);
}
if (lite_cjson_is_array(&lite_item_value)) {
res = _kk_msg_set_array(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_array(type, dev_shadow, new_key, &lite_item_value);
}
if (lite_cjson_is_number(&lite_item_value)) {
res = _kk_msg_set_numberOrStr(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_numberOrStr(type, dev_shadow, new_key, &lite_item_value);
}
if (lite_cjson_is_string(&lite_item_value)) {
res = _kk_msg_set_numberOrStr(type, devid, new_key, &lite_item_value);
res = _kk_msg_set_numberOrStr(type, dev_shadow, new_key, &lite_item_value);
}
free(new_key);
......@@ -425,7 +375,7 @@ static int _kk_msg_set_object(kk_msg_set_type_t type, int devid, char *key, lite
return SUCCESS_RETURN;
}
int _kk_msg_property_set(int devid, kk_msg_request_payload_t *request)
int _kk_msg_property_set(kk_tsl_t *dev_shadow, kk_msg_request_payload_t *request)
{
int res = 0, message_len = 0;
char *message = NULL;
......@@ -444,28 +394,12 @@ int _kk_msg_property_set(int devid, kk_msg_request_payload_t *request)
/* dm_log_info("Property Set, Size: %d", lite.size); */
if (lite_cjson_is_object(&lite)) {
res = _kk_msg_set_object(KK_MSG_PROPERTY_SET, devid, NULL, &lite);
res = _kk_msg_set_object(KK_MSG_PROPERTY_SET, dev_shadow, NULL, &lite);
}
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
#if 0
message_len = strlen(DM_MSG_PROPERTY_SET_FMT) + DM_UTILS_UINT32_STRLEN + request->params.value_length + 1;
message = DM_malloc(message_len);
if (message == NULL) {
return DM_MEMORY_NOT_ENOUGH;
}
memset(message, 0, message_len);
HAL_Snprintf(message, message_len, DM_MSG_PROPERTY_SET_FMT, devid, request->params.value_length, request->params.value);
res = _dm_msg_send_to_user(IOTX_DM_EVENT_PROPERTY_SET, message);
if (res != SUCCESS_RETURN) {
DM_free(message);
}
#endif
return SUCCESS_RETURN;
}
......@@ -496,24 +430,19 @@ static int _kk_msg_request_parse(_IN_ char *payload, _IN_ int payload_len, _OU_
return SUCCESS_RETURN;
}
static int _kk_set_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
static int _kk_set_event_output_value(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char *value_str)
{
int res = 0;
void *data = NULL;
kk_tsl_data_type_e type;
dm_mgr_dev_node_t *node = NULL;
if (devid < 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
if (dev_shadow == NULL || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = dm_tsl_get_event_output_data(node->dev_shadow, key, key_len, &data);
res = dm_tsl_get_event_output_data(dev_shadow, key, key_len, &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -528,23 +457,23 @@ static int _kk_set_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int k
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: {
int value_int = (value == NULL) ? (atoi(value_str)) : (*(int *)value);
res = dm_tls_set_event_output_value(node->dev_shadow, key, key_len, &value_int, sizeof(int));
res = dm_tls_set_event_output_value(dev_shadow, key, key_len, &value_int, sizeof(int));
}
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = (value == NULL) ? (atof(value_str)) : (*(float *)value);
res = dm_tls_set_event_output_value(node->dev_shadow, key, key_len, &value_float, sizeof(float));
res = dm_tls_set_event_output_value(dev_shadow, key, key_len, &value_float, sizeof(float));
}
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
double value_double = (value == NULL) ? (atof(value_str)) : (*(double *)value);
res = dm_tls_set_event_output_value(node->dev_shadow, key, key_len, &value_double, sizeof(double));
res = dm_tls_set_event_output_value(dev_shadow, key, key_len, &value_double, sizeof(double));
}
break;
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
char *value_string = (value == NULL) ? (value_str) : (value);
res = dm_tls_set_event_output_value(node->dev_shadow, key, key_len, value_string, strlen(value_string));
res = dm_tls_set_event_output_value(dev_shadow, key, key_len, value_string, strlen(value_string));
}
break;
default: {
......@@ -559,7 +488,7 @@ static int _kk_set_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int k
return SUCCESS_RETURN;
}
static int _kk_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
static int _kk_set_service_output_value(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len,
_IN_ void *value,
_IN_ char *value_str)
{
......@@ -567,11 +496,11 @@ static int _kk_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int
void *data = NULL;
kk_tsl_data_type_e type;
if (devid < 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
if (dev_shadow == 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
return INVALID_PARAMETER;
}
res = _kk_msg_get_service_output_data(devid, key, key_len, &data);
res = _kk_msg_get_service_output_data(dev_shadow, key, key_len, &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -586,23 +515,23 @@ static int _kk_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: {
int value_int = (value == NULL) ? (atoi(value_str)) : (*(int *)value);
res = _kk_msg_set_service_output_value(devid, key, key_len, &value_int, sizeof(int));
res = _kk_msg_set_service_output_value(dev_shadow, key, key_len, &value_int, sizeof(int));
}
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = (value == NULL) ? (atof(value_str)) : (*(float *)value);
res = _kk_msg_set_service_output_value(devid, key, key_len, &value_float, sizeof(float));
res = _kk_msg_set_service_output_value(dev_shadow, key, key_len, &value_float, sizeof(float));
}
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
double value_double = (value == NULL) ? (atof(value_str)) : (*(double *)value);
res = _kk_msg_set_service_output_value(devid, key, key_len, &value_double, sizeof(double));
res = _kk_msg_set_service_output_value(dev_shadow, key, key_len, &value_double, sizeof(double));
}
break;
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
char *value_string = (value == NULL) ? (value_str) : (value);
res = _kk_msg_set_service_output_value(devid, key, key_len, value_string, strlen(value_string));
res = _kk_msg_set_service_output_value(dev_shadow, key, key_len, value_string, strlen(value_string));
}
break;
default: {
......@@ -659,40 +588,18 @@ int kk_tsl_api_init(void)
return SUCCESS_RETURN;
}
static int _kk_tsl_get_property_value_byId(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = kk_tsl_get_property_value_ex(node->dev_shadow, key, key_len, value);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
return SUCCESS_RETURN;
}
static int _kk_tsl_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
static int _kk_tsl_get_property_value(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char **value_str)
{
int res = 0;
void *data = NULL;
kk_tsl_data_type_e type;
if (devid < 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
if (dev_shadow == NULL || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
return INVALID_PARAMETER;
}
res = _kk_msg_get_property_data(devid, key, key_len, &data);
res = _kk_msg_get_property_data(dev_shadow, key, key_len, &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -707,7 +614,7 @@ static int _kk_tsl_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int k
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: {
int value_int = 0;
res = _kk_tsl_get_property_value_byId(devid, key, key_len, (void *)&value_int);
res = kk_tsl_get_property_value_ex(dev_shadow, key, key_len, (void *)&value_int);
if (res == SUCCESS_RETURN) {
if (value) {
*(int *)value = value_int;
......@@ -720,7 +627,7 @@ static int _kk_tsl_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int k
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = 0;
res = _kk_tsl_get_property_value_byId(devid, key, key_len, (void *)&value_float);
res = kk_tsl_get_property_value_ex(dev_shadow, key, key_len, (void *)&value_float);
if (res == SUCCESS_RETURN) {
if (value) {
*(float *)value = value_float;
......@@ -733,7 +640,7 @@ static int _kk_tsl_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int k
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
double value_double = 0;
res = _kk_tsl_get_property_value_byId(devid, key, key_len, (void *)&value_double);
res = kk_tsl_get_property_value_ex(dev_shadow, key, key_len, (void *)&value_double);
if (res == SUCCESS_RETURN) {
if (value) {
*(double *)value = value_double;
......@@ -747,7 +654,7 @@ static int _kk_tsl_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int k
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
char *value_string = NULL;
res = _kk_tsl_get_property_value_byId(devid, key, key_len, (void *)&value_string);
res = kk_tsl_get_property_value_ex(dev_shadow, key, key_len, (void *)&value_string);
if (res == SUCCESS_RETURN) {
if (value) {
memcpy(value, value_string, strlen(value_string));
......@@ -775,39 +682,18 @@ static int _kk_tsl_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int k
return SUCCESS_RETURN;
}
static int _kk_tsl_get_event_output_value_byId(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = kk_tsl_get_event_oput_value(node->dev_shadow, key, key_len, value);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
return SUCCESS_RETURN;
}
static int _kk_tsl_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
static int _kk_tsl_get_event_output_value(_IN_ kk_tsl_t * dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char **value_str)
{
int res = 0;
void *data = NULL;
kk_tsl_data_type_e type;
if (devid < 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
if (dev_shadow == NULL || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
return INVALID_PARAMETER;
}
res = kk_msg_get_event_output_data(devid, key, key_len, &data);
res = kk_msg_get_event_output_data(dev_shadow, key, key_len, &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -822,7 +708,7 @@ static int _kk_tsl_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ i
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: {
int value_int = 0;
res = _kk_tsl_get_event_output_value_byId(devid, key, key_len, (void *)&value_int);
res = kk_tsl_get_event_oput_value(dev_shadow, key, key_len, (void *)&value_int);
if (res == SUCCESS_RETURN) {
if (value) {
*(int *)value = value_int;
......@@ -835,7 +721,7 @@ static int _kk_tsl_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ i
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = 0;
res = _kk_tsl_get_event_output_value_byId(devid, key, key_len, (void *)&value_float);
res = kk_tsl_get_event_oput_value(dev_shadow, key, key_len, (void *)&value_float);
if (res == SUCCESS_RETURN) {
if (value) {
*(float *)value = value_float;
......@@ -848,7 +734,7 @@ static int _kk_tsl_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ i
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
double value_double = 0;
res = _kk_tsl_get_event_output_value_byId(devid, key, key_len, (void *)&value_double);
res = kk_tsl_get_event_oput_value(dev_shadow, key, key_len, (void *)&value_double);
if (res == SUCCESS_RETURN) {
if (value) {
*(double *)value = value_double;
......@@ -862,7 +748,7 @@ static int _kk_tsl_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ i
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
char *value_string = NULL;
res = _kk_tsl_get_event_output_value_byId(devid, key, key_len, (void *)&value_string);
res = kk_tsl_get_event_oput_value(dev_shadow, key, key_len, (void *)&value_string);
if (res == SUCCESS_RETURN) {
if (value) {
memcpy(value, value_string, strlen(value_string));
......@@ -887,21 +773,15 @@ static int _kk_tsl_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ i
return SUCCESS_RETURN;
}
static int _kk_tsl_get_service_input_value_byId(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value)
static int _kk_tsl_get_service_input_value_byId(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = kk_tsl_get_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_INPUT_DATA, node->dev_shadow, key, key_len,
res = kk_tsl_get_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_INPUT_DATA, dev_shadow, key, key_len,
value);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
......@@ -909,7 +789,7 @@ static int _kk_tsl_get_service_input_value_byId(_IN_ int devid, _IN_ char *key,
return SUCCESS_RETURN;
}
static int _kk_tsl_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
static int _kk_tsl_get_service_input_value(_IN_ kk_tsl_t * dev_shadow, _IN_ char *key, _IN_ int key_len,
_IN_ void *value,
_IN_ char **value_str)
{
......@@ -917,11 +797,11 @@ static int _kk_tsl_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_
void *data = NULL;
kk_tsl_data_type_e type;
if (devid < 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
if (dev_shadow == NULL || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
return INVALID_PARAMETER;
}
res = _kk_msg_get_service_input_data(devid, key, key_len, &data);
res = _kk_msg_get_service_input_data(dev_shadow, key, key_len, &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -936,7 +816,7 @@ static int _kk_tsl_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: {
int value_int = 0;
res = _kk_tsl_get_service_input_value_byId(devid, key, key_len, (void *)&value_int);
res = _kk_tsl_get_service_input_value_byId(dev_shadow, key, key_len, (void *)&value_int);
if (res == SUCCESS_RETURN) {
if (value) {
*(int *)value = value_int;
......@@ -949,7 +829,7 @@ static int _kk_tsl_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = 0;
res = _kk_tsl_get_service_input_value_byId(devid, key, key_len, (void *)&value_float);
res = _kk_tsl_get_service_input_value_byId(dev_shadow, key, key_len, (void *)&value_float);
if (res == SUCCESS_RETURN) {
if (value) {
*(float *)value = value_float;
......@@ -962,7 +842,7 @@ static int _kk_tsl_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
double value_double = 0;
res = _kk_tsl_get_service_input_value_byId(devid, key, key_len, (void *)&value_double);
res = _kk_tsl_get_service_input_value_byId(dev_shadow, key, key_len, (void *)&value_double);
if (res == SUCCESS_RETURN) {
if (value) {
*(double *)value = value_double;
......@@ -976,7 +856,7 @@ static int _kk_tsl_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
char *value_string = NULL;
res = _kk_tsl_get_service_input_value_byId(devid, key, key_len, (void *)&value_string);
res = _kk_tsl_get_service_input_value_byId(dev_shadow, key, key_len, (void *)&value_string);
if (res == SUCCESS_RETURN) {
if (value) {
memcpy(value, value_string, strlen(value_string));
......@@ -1000,21 +880,15 @@ static int _kk_tsl_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_
}
return SUCCESS_RETURN;
}
static int _kk_tsl_get_service_output_value_byId(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value)
static int _kk_tsl_get_service_output_value_byId(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
if (dev_shadow == NULL || key == NULL || key_len <= 0 || value == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = kk_tsl_get_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_OUTPUT_DATA, node->dev_shadow, key, key_len,
res = kk_tsl_get_service_input_output_value(KK_TSL_DATA_TARGET_SERVICE_OUTPUT_DATA, dev_shadow, key, key_len,
value);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
......@@ -1022,7 +896,7 @@ static int _kk_tsl_get_service_output_value_byId(_IN_ int devid, _IN_ char *key,
return SUCCESS_RETURN;
}
static int _kk_tsl_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
static int _kk_tsl_get_service_output_value(_IN_ kk_tsl_t *dev_shadow, _IN_ char *key, _IN_ int key_len,
_IN_ void *value,
_IN_ char **value_str)
{
......@@ -1030,11 +904,11 @@ static int _kk_tsl_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_
void *data = NULL;
kk_tsl_data_type_e type;
if (devid < 0 || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
if (dev_shadow == NULL || key == NULL || key_len <= 0 || ((value == NULL) && (value_str == NULL))) {
return INVALID_PARAMETER;
}
res = _kk_msg_get_service_output_data(devid, key, key_len, &data);
res = _kk_msg_get_service_output_data(dev_shadow, key, key_len, &data);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -1049,7 +923,7 @@ static int _kk_tsl_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: {
int value_int = 0;
res = _kk_tsl_get_service_output_value_byId(devid, key, key_len, (void *)&value_int);
res = _kk_tsl_get_service_output_value_byId(dev_shadow, key, key_len, (void *)&value_int);
if (res == SUCCESS_RETURN) {
if (value) {
*(int *)value = value_int;
......@@ -1062,7 +936,7 @@ static int _kk_tsl_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_
break;
case KK_TSL_DATA_TYPE_FLOAT: {
float value_float = 0;
res = _kk_tsl_get_service_output_value_byId(devid, key, key_len, (void *)&value_float);
res = _kk_tsl_get_service_output_value_byId(dev_shadow, key, key_len, (void *)&value_float);
if (res == SUCCESS_RETURN) {
if (value) {
*(float *)value = value_float;
......@@ -1075,7 +949,7 @@ static int _kk_tsl_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_
break;
case KK_TSL_DATA_TYPE_DOUBLE: {
double value_double = 0;
res = _kk_tsl_get_service_output_value_byId(devid, key, key_len, (void *)&value_double);
res = _kk_tsl_get_service_output_value_byId(dev_shadow, key, key_len, (void *)&value_double);
if (res == SUCCESS_RETURN) {
if (value) {
*(double *)value = value_double;
......@@ -1089,7 +963,7 @@ static int _kk_tsl_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_
case KK_TSL_DATA_TYPE_TEXT:
case KK_TSL_DATA_TYPE_DATE: {
char *value_string = NULL;
res = _kk_tsl_get_service_output_value_byId(devid, key, key_len, (void *)&value_string);
res = _kk_tsl_get_service_output_value_byId(dev_shadow, key, key_len, (void *)&value_string);
if (res == SUCCESS_RETURN) {
if (value) {
memcpy(value, value_string, strlen(value_string));
......@@ -1114,7 +988,7 @@ static int _kk_tsl_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_
return SUCCESS_RETURN;
}
int kk_tsl_get_value(kk_tsl_get_t method_get, int devId, const char *identifier,
int kk_tsl_get_value(kk_tsl_get_t method_get, kk_tsl_t * dev_shadow, const char *identifier,
void *value,
char **value_str)
{
......@@ -1130,20 +1004,20 @@ int kk_tsl_get_value(kk_tsl_get_t method_get, int devId, const char *identifier,
switch (method_get) {
case kk_tsl_get_property_value: {
res = _kk_tsl_get_property_value(devId, (char *)identifier, strlen(identifier), value, value_str);
res = _kk_tsl_get_property_value(dev_shadow, (char *)identifier, strlen(identifier), value, value_str);
}
break;
case kk_tsl_get_event_output_value: {
res = _kk_tsl_get_event_output_value(devId, (char *)identifier, strlen(identifier), value, value_str);
res = _kk_tsl_get_event_output_value(dev_shadow, (char *)identifier, strlen(identifier), value, value_str);
}
break;
case kk_tsl_get_service_input_value: {
res = _kk_tsl_get_service_input_value(devId, (char *)identifier, strlen(identifier), value,
res = _kk_tsl_get_service_input_value(dev_shadow, (char *)identifier, strlen(identifier), value,
value_str);
}
break;
case kk_tsl_get_service_output_value: {
res = _kk_tsl_get_service_output_value(devId, (char *)identifier, strlen(identifier), value,
res = _kk_tsl_get_service_output_value(dev_shadow, (char *)identifier, strlen(identifier), value,
value_str);
}
break;
......@@ -1164,7 +1038,7 @@ int kk_tsl_get_value(kk_tsl_get_t method_get, int devId, const char *identifier,
return SUCCESS_RETURN;
}
int kk_tsl_set_value(kk_tsl_set_t set, int devId, const char *identifier,
int kk_tsl_set_value(kk_tsl_set_t set, kk_tsl_t * dev_shadow, const char *identifier,
const void *value,
const char *value_str)
{
......@@ -1179,17 +1053,17 @@ int kk_tsl_set_value(kk_tsl_set_t set, int devId, const char *identifier,
_kk_tsl_api_lock();
switch (set) {
case kk_tsl_set_property_value: {
res = _kk_tsl_set_property_value(devId, (char *)identifier, strlen(identifier), (void *)value,
res = _kk_tsl_set_property_value(dev_shadow, (char *)identifier, strlen(identifier), (void *)value,
(char*)value_str);
}
break;
case kk_tsl_set_event_output_value: {
res = _kk_set_event_output_value(devId, (char *)identifier, strlen(identifier), (void *)value,
res = _kk_set_event_output_value(dev_shadow, (char *)identifier, strlen(identifier), (void *)value,
(char *)value_str);
}
break;
case kk_tsl_set_service_output_value: {
res = _kk_set_service_output_value(devId, (char *)identifier, strlen(identifier), (void *)value,
res = _kk_set_service_output_value(dev_shadow, (char *)identifier, strlen(identifier), (void *)value,
(char *)value_str);
}
break;
......@@ -1243,47 +1117,24 @@ int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int start_deli,
return SUCCESS_RETURN;
}
int kk_tsl_property_set_by_devicecode(const char deviceCode[DEVICE_CODE_MAXLEN], const char *payload, unsigned int payload_len)
int kk_tsl_property_set_by_shadow(kk_tsl_t * dev_shadow, const char *payload, unsigned int payload_len)
{
kk_msg_request_payload_t request;
int res = 0, devid = 0;
memset(&request, 0, sizeof(kk_msg_request_payload_t));
res = dm_mgr_get_devId_by_devicecode(deviceCode, &devid);
if(res != SUCCESS_RETURN){
return FAIL_RETURN;
}
res = _kk_msg_request_parse((char *)payload, payload_len, &request);
if (res < SUCCESS_RETURN) {
return res ;
}
/* Operation */
res = _kk_msg_property_set(devid, &request);
/* Response */
//kk_tsl_post_property(devid,NULL);
res = _kk_msg_property_set(dev_shadow, &request);
#if 0
#define EVENT_ERROR_IDENTIFIER "Error"
#define EVENT_ERROR_OUTPUT_INFO_IDENTIFIER "ErrorCode"
//kk_tsl_post_property(devid,NULL);
char event_output_identifier[64];
snprintf(event_output_identifier, sizeof(event_output_identifier), "%s.%s",
EVENT_ERROR_IDENTIFIER, EVENT_ERROR_OUTPUT_INFO_IDENTIFIER);
int errorCode = 0;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_tsl_set_value(kk_tsl_set_event_output_value, devid,
event_output_identifier, &errorCode, NULL);
kk_tsl_post_event(devid,EVENT_ERROR_IDENTIFIER);
#endif
return SUCCESS_RETURN;
}
#if 0
int kk_tsl_service_property_set(const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
......@@ -1293,6 +1144,7 @@ int kk_tsl_service_property_set(const char *topic, const char *payload, unsigned
int res = 0, devid = 0;
char productType[PRODUCT_TYPE_MAXLEN] = {0};
char deviceCode[DEVICE_CODE_MAXLEN] = {0};
kk_tsl_t *dev_shadow = NULL;
memset(&request, 0, sizeof(kk_msg_request_payload_t));
......@@ -1308,7 +1160,7 @@ int kk_tsl_service_property_set(const char *topic, const char *payload, unsigned
deviceCode);
res = dm_mgr_search_device_by_pkdn(productType, deviceCode, &devid);
res = dm_mgr_get_device_shadow_by_devicecode(deviceCode, &dev_shadow);
if (res < SUCCESS_RETURN) {
return res;
}
......@@ -1318,34 +1170,19 @@ int kk_tsl_service_property_set(const char *topic, const char *payload, unsigned
}
/* Operation */
res = _kk_msg_property_set(devid, &request);
res = _kk_msg_property_set(dev_shadow, &request);
/* Response */
kk_tsl_post_property(devid,NULL,0);
kk_tsl_post_property(dev_shadow,NULL,0);
#if 0
#define EVENT_ERROR_IDENTIFIER "Error"
#define EVENT_ERROR_OUTPUT_INFO_IDENTIFIER "ErrorCode"
//kk_tsl_post_property(devid,NULL);
char event_output_identifier[64];
snprintf(event_output_identifier, sizeof(event_output_identifier), "%s.%s",
EVENT_ERROR_IDENTIFIER, EVENT_ERROR_OUTPUT_INFO_IDENTIFIER);
int errorCode = 0;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_tsl_set_value(kk_tsl_set_event_output_value, devid,
event_output_identifier, &errorCode, NULL);
kk_tsl_post_event(devid,EVENT_ERROR_IDENTIFIER);
#endif
return SUCCESS_RETURN;
}
static int kk_tsl_post_property_start(_IN_ int devid, _OU_ void **handle)
#endif
static int kk_tsl_post_property_start(_IN_ kk_tsl_t * dev_shadow, _OU_ void **handle)
{
dm_api_property_t *dapi_property = NULL;
if (devid < 0 || handle == NULL || *handle != NULL) {
if (dev_shadow == NULL || handle == NULL || *handle != NULL) {
return INVALID_PARAMETER;
}
......@@ -1355,7 +1192,7 @@ static int kk_tsl_post_property_start(_IN_ int devid, _OU_ void **handle)
}
memset(dapi_property, 0, sizeof(dm_api_property_t));
/* Set Devid */
dapi_property->devid = devid;
dapi_property->shadow = dev_shadow;
/* Init Json Object */
dapi_property->lite = lite_cjson_create_object();
if (dapi_property->lite == NULL) {
......@@ -1365,41 +1202,26 @@ static int kk_tsl_post_property_start(_IN_ int devid, _OU_ void **handle)
*handle = (void *)dapi_property;
return SUCCESS_RETURN;
}
static int _kk_tsl_get_property_by_index(_IN_ int devid, _IN_ int index, _OU_ kk_tsl_data_t **property)
static int _kk_tsl_get_property_by_index(_IN_ kk_tsl_t *dev_shadow, _IN_ int index, _OU_ kk_tsl_data_t **property)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (devid < 0 || index < 0) {
if (dev_shadow == NULL || index < 0) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
*property = (kk_tsl_data_t *)(node->dev_shadow->properties + index);
*property = (kk_tsl_data_t *)(dev_shadow->properties + index);
return SUCCESS_RETURN;
}
int kk_tsl_get_property_number(_IN_ int devid, _OU_ int *number)
int kk_tsl_get_property_number(_IN_ kk_tsl_t *shadow, _OU_ int *number)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (devid < 0 || number == NULL) {
if (shadow == NULL || number == NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
if(node->dev_shadow == NULL)
{
return FAIL_RETURN;
}
*number = node->dev_shadow->property_number;
*number = shadow->property_number;
return SUCCESS_RETURN;
}
......@@ -1414,13 +1236,8 @@ static int _kk_tsl_post_property_add(_IN_ void *handle, _IN_ char *identifier, _
}
dapi_property = (dm_api_property_t *)handle;
/* Assemble Property Payload */
res = dm_mgr_search_dev_by_devid(dapi_property->devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = kk_tsl_assemble_property(node->dev_shadow, identifier, identifier_len, dapi_property->lite);
res = kk_tsl_assemble_property(dapi_property->shadow, identifier, identifier_len, dapi_property->lite);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -1449,7 +1266,7 @@ int kk_tsl_post_property_add(_IN_ void *handle, _IN_ char *identifier, _IN_ int
return ret;
}
res = kk_tsl_get_property_number(dapi_property->devid, &number);
res = kk_tsl_get_property_number(dapi_property->shadow, &number);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -1458,7 +1275,7 @@ int kk_tsl_post_property_add(_IN_ void *handle, _IN_ char *identifier, _IN_ int
property_refer = NULL;
identifier_refer = NULL;
res = _kk_tsl_get_property_by_index(dapi_property->devid, index, &property_refer);
res = _kk_tsl_get_property_by_index(dapi_property->shadow, index, &property_refer);
if (res != SUCCESS_RETURN) {
continue;
}
......@@ -1472,75 +1289,60 @@ int kk_tsl_post_property_add(_IN_ void *handle, _IN_ char *identifier, _IN_ int
}
return ret;
}
static int kk_tsl_post_property_end(_IN_ void *handle,_IN_ int isAsync)
static char* kk_tsl_post_property_end(_IN_ void *handle)
{
int res = 0;
char *payload = NULL;
dm_api_property_t *dapi_property = NULL;
if (handle == NULL) {
return INVALID_PARAMETER;
return NULL;
}
dapi_property = (dm_api_property_t *)handle;
payload = lite_cjson_print_unformatted(dapi_property->lite);
if (payload == NULL) {
lite_cjson_delete(dapi_property->lite);
free(dapi_property);
return MEMORY_NOT_ENOUGH;
return NULL;
}
INFO_PRINT("Post Payload, Length: %d, Payload: %s\n", strlen(payload), payload);
res = dm_mgr_upstream_thing_property_post(dapi_property->devid, payload, strlen(payload),isAsync);
//res = dm_mgr_upstream_thing_property_post(dapi_property->shadow, payload, strlen(payload),isAsync);
lite_cjson_delete(dapi_property->lite);
free(dapi_property);
free(payload);
//free(payload);
dapi_property = NULL;
return res;
return payload;
}
int kk_tsl_post_property(int devId, const char *property_identifier,int isAsync)
char* kk_tsl_get_post_property_str(kk_tsl_t *dev_shadow, const char *property_identifier)
{
int res = 0, msgid = 0, property_identifier_len = 0, post_property_reply = 0;
void *property_handle = NULL;
kk_tsl_api_ctx_t *kk_tsl_api_ctx = _kk_tsl_api_get_ctx();
_kk_tsl_api_lock();
res = kk_tsl_post_property_start(devId, &property_handle);
res = kk_tsl_post_property_start(dev_shadow, &property_handle);
if (res != SUCCESS_RETURN) {
_kk_tsl_api_unlock();
return FAIL_RETURN;
return NULL;
}
property_identifier_len = (property_identifier) ? (strlen((char *)property_identifier)) : (0);
res = kk_tsl_post_property_add(property_handle, (char *)property_identifier, property_identifier_len);
if (res != SUCCESS_RETURN) {
kk_tsl_post_property_end(property_handle,isAsync);
_kk_tsl_api_unlock();
return FAIL_RETURN;
}
res = kk_tsl_post_property_end(property_handle,isAsync);
if (res < SUCCESS_RETURN) {
_kk_tsl_api_unlock();
return FAIL_RETURN;
}
kk_tsl_post_property_add(property_handle, (char *)property_identifier, property_identifier_len);
_kk_tsl_api_unlock();
return kk_tsl_post_property_end(property_handle);
_kk_tsl_api_unlock();
return SUCCESS_RETURN;
}
static int kk_tsl_event_output(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len,
static int kk_tsl_event_output(_IN_ kk_tsl_t *dev_shadow, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (devid < 0 || identifier == NULL || identifier_len <= 0 || lite == NULL || lite->type != cJSON_Object) {
if (dev_shadow == NULL || identifier == NULL || identifier_len <= 0 || lite == NULL || lite->type != cJSON_Object) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = kk_tsl_assemble_event_output(node->dev_shadow, identifier, identifier_len, lite);
res = kk_tsl_assemble_event_output(dev_shadow, identifier, identifier_len, lite);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
......@@ -1569,21 +1371,15 @@ static int _kk_tsl_get_event_by_identifier(_IN_ kk_tsl_t *shadow, _IN_ char *ide
return FAIL_RETURN;
}
int kk_tsl_get_event_by_identifier(_IN_ int devid, _IN_ char *identifier, _OU_ void **event)
int kk_tsl_get_event_by_identifier(_IN_ kk_tsl_t *dev_shadow, _IN_ char *identifier, _OU_ void **event)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if (devid < 0 || identifier == NULL || event == NULL || *event != NULL) {
if (dev_shadow == NULL || identifier == NULL || event == NULL || *event != NULL) {
return INVALID_PARAMETER;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
return _kk_tsl_get_event_by_identifier(node->dev_shadow, identifier, event);
return _kk_tsl_get_event_by_identifier(dev_shadow, identifier, event);
}
int kk_tsl_get_event_method(_IN_ void *event, _OU_ char **method)
{
......@@ -1617,143 +1413,97 @@ int kk_tsl_get_event_method(_IN_ void *event, _OU_ char **method)
return SUCCESS_RETURN;
}
static int _kk_tsl_post_event(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len)
char* kk_tsl_get_post_event_str(_IN_ kk_tsl_t *dev_shadow, _IN_ char *identifier, _IN_ int identifier_len)
{
int res = 0;
void *event = NULL;
lite_cjson_item_t *lite = NULL;
char *method = NULL, *payload = NULL;
char *payload = NULL;
if (devid < 0 || identifier == NULL || identifier_len <= 0) {
return INVALID_PARAMETER;
if (dev_shadow == NULL || identifier == NULL || identifier_len <= 0) {
return NULL;
}
lite = lite_cjson_create_object();
if (lite == NULL) {
return MEMORY_NOT_ENOUGH;
return NULL;
}
_kk_tsl_api_lock();
res = kk_tsl_event_output(devid, identifier, identifier_len, lite);
res = kk_tsl_event_output(dev_shadow, identifier, identifier_len, lite);
if (res != SUCCESS_RETURN) {
lite_cjson_delete(lite);
return FAIL_RETURN;
_kk_tsl_api_unlock();
return NULL;
}
payload = lite_cjson_print_unformatted(lite);
lite_cjson_delete(lite);
if (payload == NULL) {
return MEMORY_NOT_ENOUGH;
_kk_tsl_api_unlock();
return NULL;
}
INFO_PRINT("\nCurrent Event Post Payload, Length: %d, Payload: %s\n", strlen(payload), payload);
_kk_tsl_api_unlock();
res = kk_tsl_get_event_by_identifier(devid, identifier, &event);
if (res != SUCCESS_RETURN) {
free(payload);
return FAIL_RETURN;
}
res = kk_tsl_get_event_method(event, &method);
if (res != SUCCESS_RETURN) {
free(payload);
return FAIL_RETURN;
}
INFO_PRINT("Current Event Method: %s", method);
res = dm_mgr_upstream_thing_event_post(devid, identifier, identifier_len, method, payload, strlen(payload));
//res = dm_mgr_upstream_thing_event_post(devid, identifier, identifier_len, method, payload, strlen(payload));
free(payload);
free(method);
return res;
//free(payload);
return payload;
}
int kk_tsl_post_event(int devId, const char *event_identifier)
{
int res = 0, msgid = 0, post_event_reply = 0;
kk_tsl_api_ctx_t *kk_tsl_api_ctx = _kk_tsl_api_get_ctx();
if (event_identifier == NULL) {
return FAIL_RETURN;
}
_kk_tsl_api_lock();
res = _kk_tsl_post_event(devId, (char *)event_identifier, strlen((char *)event_identifier));
if (res < SUCCESS_RETURN) {
_kk_tsl_api_unlock();
return FAIL_RETURN;
}
_kk_tsl_api_unlock();
return SUCCESS_RETURN;
}
static int _kk_tsl_send_service_response(_IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
static char* _kk_tsl_send_service_response(_IN_ kk_tsl_t *dev_shadow,
_IN_ char *identifier,
_IN_ int identifier_len)
{
int res = 0;
lite_cjson_item_t *lite = NULL;
char *payload = NULL;
dm_mgr_dev_node_t *node = NULL;
if (devid < 0 || msgid < 0 || identifier == NULL || identifier_len <= 0) {
return INVALID_PARAMETER;
if (dev_shadow == NULL || identifier == NULL || identifier_len <= 0) {
return NULL;
}
lite = lite_cjson_create_object();
if (lite == NULL) {
return MEMORY_NOT_ENOUGH;
}
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
return NULL;
}
_kk_tsl_api_lock();
res = kk_tsl_assemble_service_output(node->dev_shadow, identifier, identifier_len, lite);
res = kk_tsl_assemble_service_output(dev_shadow, identifier, identifier_len, lite);
if (res != SUCCESS_RETURN) {
lite_cjson_delete(lite);
return FAIL_RETURN;
_kk_tsl_api_unlock();
return NULL;
}
_kk_tsl_api_unlock();
payload = lite_cjson_print_unformatted(lite);
lite_cjson_delete(lite);
if (payload == NULL) {
return MEMORY_NOT_ENOUGH;
return NULL;
}
INFO_PRINT("Current Service Response Payload, Length: %d, Payload: %s", strlen(payload), payload);
res = dm_mgr_deprecated_upstream_thing_service_response(devid, msgid, code, identifier, identifier_len, payload,
strlen(payload));
// res = dm_mgr_deprecated_upstream_thing_service_response(devid, msgid, code, identifier, identifier_len, payload,
//strlen(payload));
free(payload);
return res;
//free(payload);
return payload;
}
int kk_tsl_post_service(int devId, const char *service_identifier, int response_id,
int code)
char * kk_tsl_get_post_service_str(kk_tsl_t *dev_shadow, const char *service_identifier)
{
int res = 0;
kk_tsl_api_ctx_t *kk_tsl_api_ctx = _kk_tsl_api_get_ctx();
if (service_identifier == NULL) {
ERROR_PRINT("Invalid Parameter");
return FAIL_RETURN;
return NULL;
}
_kk_tsl_api_lock();
res = _kk_tsl_send_service_response(devId, response_id, (iotx_dm_error_code_t)code,
(char *)service_identifier, strlen(service_identifier));
if (res < SUCCESS_RETURN) {
_kk_tsl_api_unlock();
return FAIL_RETURN;
}
_kk_tsl_api_unlock();
return SUCCESS_RETURN;
return _kk_tsl_send_service_response(dev_shadow,(char *)service_identifier, strlen(service_identifier));
}
......
#ifndef _TSL_API_H__
#define _TSL_API_H__
#include "lite-cjson.h"
#include "kk_tsl_common.h"
#include "../../midware/dm/iotx_dm.h"
#define KK_MSG_KEY_ID "msgId"
#define KK_MSG_KEY_VERSION "version"
#define KK_MSG_KEY_METHOD "method"
#define KK_MSG_KEY_PARAMS "params"
#define KK_MSG_KEY_CODE "code"
#define KK_MSG_KEY_DATA "data"
#define KK_MSG_KEY_MESSAGE "message"
/*typedef enum {
IOTX_DM_ERR_CODE_SUCCESS = 200,
IOTX_DM_ERR_CODE_REQUEST_ERROR = 400,
IOTX_DM_ERR_CODE_REQUEST_PARAMS_ERROR = 460,
IOTX_DM_ERR_CODE_REQUEST_TOO_MANY = 429,
IOTX_DM_ERR_CODE_NO_ACTIVE_SESSION = 520,
IOTX_DM_ERR_CODE_TIMEOUT = 100000
} iotx_dm_error_code_t;*/
typedef struct {
const char *uri;
unsigned char *payload;
unsigned int payload_len;
void *context;
} kk_msg_source_t;
typedef struct {
const char *uri_name;
} kk_msg_dest_t;
typedef struct {
lite_cjson_t id;
lite_cjson_t version;
lite_cjson_t method;
lite_cjson_t params;
} kk_msg_request_payload_t;
typedef struct {
lite_cjson_t id;
lite_cjson_t code;
lite_cjson_t data;
lite_cjson_t message;
} kk_msg_response_payload_t;
typedef enum {
kk_tsl_set_property_value = 0,
kk_tsl_set_event_output_value,
kk_tsl_set_service_output_value,
kk_tsl_set_number,
} kk_tsl_set_t;
typedef enum {
kk_tsl_get_property_value = 0,
kk_tsl_get_event_output_value,
kk_tsl_get_service_input_value,
kk_tsl_get_service_output_value,
kk_tsl_get_number,
} kk_tsl_get_t;
typedef struct {
int devid;
lite_cjson_item_t *lite;
} dm_api_property_t;
extern int kk_tsl_api_init(void);
extern int kk_tsl_set_value(kk_tsl_set_t set, int devId, const char *identifier, \
const void *value, \
const char *value_str);
extern int kk_tsl_get_value(kk_tsl_get_t method_get, int devId, const char *identifier, \
void *value, \
char **value_str);
extern int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int start_deli, _IN_ int end_deli, \
_OU_ char productType[PRODUCT_TYPE_MAXLEN], _OU_ char deviceCode[DEVICE_CODE_MAXLEN]);
extern int kk_tsl_service_property_set(const char *topic, const char *payload, unsigned int payload_len,
void *context);
extern int kk_tsl_post_property(int devId, const char *property_identifier,int isAsync);
extern int kk_tsl_post_event(int devId, const char *event_identifier);
extern int kk_tsl_post_service(int devId, const char *service_identifier, int response_id,int code);
#endif
#ifndef _TSL_API_H__
#define _TSL_API_H__
#include "lite-cjson.h"
#include "kk_tsl_common.h"
#include "../../midware/dm/iotx_dm.h"
#define KK_MSG_KEY_ID "msgId"
#define KK_MSG_KEY_VERSION "version"
#define KK_MSG_KEY_METHOD "method"
#define KK_MSG_KEY_PARAMS "params"
#define KK_MSG_KEY_CODE "code"
#define KK_MSG_KEY_DATA "data"
#define KK_MSG_KEY_MESSAGE "message"
/*typedef enum {
IOTX_DM_ERR_CODE_SUCCESS = 200,
IOTX_DM_ERR_CODE_REQUEST_ERROR = 400,
IOTX_DM_ERR_CODE_REQUEST_PARAMS_ERROR = 460,
IOTX_DM_ERR_CODE_REQUEST_TOO_MANY = 429,
IOTX_DM_ERR_CODE_NO_ACTIVE_SESSION = 520,
IOTX_DM_ERR_CODE_TIMEOUT = 100000
} iotx_dm_error_code_t;*/
typedef struct {
const char *uri;
unsigned char *payload;
unsigned int payload_len;
void *context;
} kk_msg_source_t;
typedef struct {
const char *uri_name;
} kk_msg_dest_t;
typedef struct {
lite_cjson_t id;
lite_cjson_t version;
lite_cjson_t method;
lite_cjson_t params;
} kk_msg_request_payload_t;
typedef struct {
lite_cjson_t id;
lite_cjson_t code;
lite_cjson_t data;
lite_cjson_t message;
} kk_msg_response_payload_t;
typedef enum {
kk_tsl_set_property_value = 0,
kk_tsl_set_event_output_value,
kk_tsl_set_service_output_value,
kk_tsl_set_number,
} kk_tsl_set_t;
typedef enum {
kk_tsl_get_property_value = 0,
kk_tsl_get_event_output_value,
kk_tsl_get_service_input_value,
kk_tsl_get_service_output_value,
kk_tsl_get_number,
} kk_tsl_get_t;
typedef struct {
kk_tsl_t * shadow;
lite_cjson_item_t *lite;
} dm_api_property_t;
extern int kk_tsl_api_init(void);
extern int kk_tsl_set_value(kk_tsl_set_t set, kk_tsl_t *dev_shadow, const char *identifier, \
const void *value, \
const char *value_str);
extern int kk_tsl_get_value(kk_tsl_get_t method_get, kk_tsl_t * dev_shadow, const char *identifier, \
void *value, \
char **value_str);
extern int kk_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int start_deli, _IN_ int end_deli, \
_OU_ char productType[PRODUCT_TYPE_MAXLEN], _OU_ char deviceCode[DEVICE_CODE_MAXLEN]);
extern char* kk_tsl_get_post_property_str(kk_tsl_t *dev_shadow, const char *property_identifier);
extern char* kk_tsl_get_post_event_str(_IN_ kk_tsl_t *dev_shadow, _IN_ char *identifier, _IN_ int identifier_len);
extern char * kk_tsl_get_post_service_str(kk_tsl_t *dev_shadow, const char *service_identifier);
#endif
......@@ -3,7 +3,18 @@
#include <stdio.h>
#include "kk_tsl_common.h"
#include "kk_dm_mng.h"
#ifndef _ZLOG_
#undef INFO_PRINT
#undef WARNING_PRINT
#undef ERROR_PRINT
#define INFO_PRINT printf
#define WARNING_PRINT printf
#define ERROR_PRINT printf
#else
#include "kk_log.h"
#endif
#define TSL_SUBDEVICE_PATH_FILE "/home/tsl/product_%s.json"
......
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