Commit bc05e79b authored by whmaizmy's avatar whmaizmy Committed by 黄振令

【修改内容】1,子设备注册,添加topo,login 到云端

parent 0db490ba
......@@ -4,7 +4,7 @@
#define ADDRESS "tcp://172.25.240.31:1983"
#define CLIENTID "1234"
#define TOPIC "/sys/a1OYuSBt23u/aIqEbWno8yDdsjCX15iq/thing/service/property/set"
#define TOPIC "/sys/a1OYuSBt23u/aIqEbWno8yDdsjCX15iq/#"
#define PAYLOAD "Hello cwc World!"
#define QOS 2
......
......@@ -74,7 +74,7 @@ static int _check_invalid_topic(char* topic)
}
void KK_Sendto_DevData(const char *topic,const char *data)
{
if(_check_invalid_topic())
if(_check_invalid_topic(topic))
{
return;//ingore the message
}
......
......@@ -30,9 +30,9 @@
#include "kk_product.h"
//#include "iot_import.h"
char g_product_type[PRODUCT_TYPE_LEN];
char g_product_code[PRODUCT_CODE_LEN];
char g_device_code[DEVICE_CODE_LEN];
volatile char g_product_type[PRODUCT_TYPE_LEN]={"a1OYuSBt23u"};
volatile char g_product_code[PRODUCT_CODE_LEN] = {"aIqEbWno8yDdsjCX15iq"};
volatile char g_device_code[DEVICE_CODE_LEN];
#define PLATFORM_WAIT_INFINITE (~0)
......@@ -185,8 +185,10 @@ int HAL_SetProduct_Type(_IN_ char *product_type)
if (len > PRODUCT_TYPE_LEN) {
return -1;
}
printf("HAL_SetProduct_Type [%d] [%s] \n", len, g_product_type);
memset(g_product_type, 0x0, PRODUCT_TYPE_LEN);
strncpy(g_product_type, product_type, len);
printf("HAL_SetProduct_Type end [%d] [%s] \n", len, g_product_type);
return len;
}
......@@ -219,7 +221,9 @@ int HAL_GetProduct_Type(_OU_ char *product_type)
{
int len = strlen(g_product_type);
memset(product_type, 0x0, PRODUCT_TYPE_LEN);
printf("HAL_GetProduct_Type [%d] [%s] \n", len, g_product_type);
strncpy(product_type, g_product_type, len);
printf("HAL_GetProduct_Type product_type [%d] [%s] \n", len, product_type);
return len;
}
......
/*
* Copyright (C) 2020-2020 ikonke
*/
#ifndef _IOT_EXPORT_LINKKIT_H_
#define _IOT_EXPORT_LINKKIT_H_
#if defined(__cplusplus)
extern "C" {
#endif
/*#include "dm_wrapper.h"*/
typedef enum {
IOTX_LINKKIT_DEV_TYPE_MASTER,
IOTX_LINKKIT_DEV_TYPE_SLAVE,
IOTX_LINKKIT_DEV_TYPE_MAX
} iotx_linkkit_dev_type_t;
/*typedef struct {
char product_key[IOTX_PRODUCT_KEY_LEN + 1];
char product_secret[IOTX_PRODUCT_SECRET_LEN + 1];
char device_name[IOTX_DEVICE_NAME_LEN + 1];
char device_secret[IOTX_DEVICE_SECRET_LEN + 1];
} iotx_linkkit_dev_meta_info_t;*/
typedef enum {
/* post property value to cloud */
ITM_MSG_POST_PROPERTY,
/* post device info update message to cloud */
ITM_MSG_DEVICEINFO_UPDATE,
/* post device info delete message to cloud */
ITM_MSG_DEVICEINFO_DELETE,
/* post raw data to cloud */
ITM_MSG_POST_RAW_DATA,
/* only for slave device, send login request to cloud */
ITM_MSG_LOGIN,
/* only for slave device, send logout request to cloud */
ITM_MSG_LOGOUT,
/* only for slave device, send delete topo request to cloud */
ITM_MSG_DELETE_TOPO,
/* query ntp time from cloud */
ITM_MSG_QUERY_TIMESTAMP,
/* only for master device, query topo list */
ITM_MSG_QUERY_TOPOLIST,
/* only for master device, qurey firmware ota data */
ITM_MSG_QUERY_FOTA_DATA,
/* only for master device, qurey config ota data */
ITM_MSG_QUERY_COTA_DATA,
/* only for master device, request config ota data from cloud */
ITM_MSG_REQUEST_COTA,
/* only for master device, request fota image from cloud */
ITM_MSG_REQUEST_FOTA_IMAGE,
/* report subdev's firmware version */
ITM_MSG_REPORT_SUBDEV_FIRMWARE_VERSION,
/* get a device's desired property */
ITM_MSG_PROPERTY_DESIRED_GET,
/* delete a device's desired property */
ITM_MSG_PROPERTY_DESIRED_DELETE,
IOTX_LINKKIT_MSG_MAX
} iotx_linkkit_msg_type_t;
#if 0
/**
* @brief create a new device
*
* @param dev_type. type of device which will be created. see iotx_linkkit_dev_type_t
* @param meta_info. The product key, product secret, device name and device secret of new device.
*
* @return success: device id (>=0), fail: -1.
*
*/
int IOT_Linkkit_Open(iotx_linkkit_dev_type_t dev_type, iotx_linkkit_dev_meta_info_t *meta_info);
/**
* @brief start device network connection.
* for master device, start to connect aliyun server.
* for slave device, send message to cloud for register new device and add topo with master device
*
* @param devid. device identifier.
*
* @return success: device id (>=0), fail: -1.
*
*/
int IOT_Linkkit_Connect(int devid);
/**
* @brief try to receive message from cloud and dispatch these message to user event callback
*
* @param timeout_ms. timeout for waiting new message arrived
*
* @return void.
*
*/
void IOT_Linkkit_Yield(int timeout_ms);
/**
* @brief close device network connection and release resources.
* for master device, disconnect with aliyun server and release all local resources.
* for slave device, send message to cloud for delete topo with master device and unregister itself, then release device's resources.
*
* @param devid. device identifier.
*
* @return success: 0, fail: -1.
*
*/
int IOT_Linkkit_Close(int devid);
/**
* @brief Report message to cloud
*
* @param devid. device identifier.
* @param msg_type. message type. see iotx_linkkit_msg_type_t, as follows:
* ITM_MSG_POST_PROPERTY
* ITM_MSG_DEVICEINFO_UPDATE
* ITM_MSG_DEVICEINFO_DELETE
* ITM_MSG_POST_RAW_DATA
* ITM_MSG_LOGIN
* ITM_MSG_LOGOUT
*
* @param payload. message payload.
* @param payload_len. message payload length.
*
* @return success: 0 or message id (>=1), fail: -1.
*
*/
int IOT_Linkkit_Report(int devid, iotx_linkkit_msg_type_t msg_type, unsigned char *payload,
int payload_len);
/**
* @brief post message to cloud
*
* @param devid. device identifier.
* @param msg_type. message type. see iotx_linkkit_msg_type_t, as follows:
* ITM_MSG_QUERY_TIMESTAMP
* ITM_MSG_QUERY_TOPOLIST
* ITM_MSG_QUERY_FOTA_DATA
* ITM_MSG_QUERY_COTA_DATA
* ITM_MSG_REQUEST_COTA
* ITM_MSG_REQUEST_FOTA_IMAGE
*
* @param payload. message payload.
* @param payload_len. message payload length.
*
* @return success: 0 or message id (>=1), fail: -1.
*
*/
int IOT_Linkkit_Query(int devid, iotx_linkkit_msg_type_t msg_type, unsigned char *payload,
int payload_len);
/**
* @brief post event to cloud
*
* @param devid. device identifier.
* @param eventid. tsl event id.
* @param eventid_len. length of tsl event id.
* @param payload. event payload.
* @param payload_len. event payload length.
*
* @return success: message id (>=1), fail: -1.
*
*/
int IOT_Linkkit_TriggerEvent(int devid, char *eventid, int eventid_len, char *payload, int payload_len);
#endif
#if defined(__cplusplus)
}
#endif
#endif
/*
* Copyright (C) 2020-2020 ikonke
*/
#ifndef _IOT_EXPORT_DM_H_
#define _IOT_EXPORT_DM_H_
#ifndef _IN_
#define _IN_
#endif
#ifndef _OU_
#define _OU_
#endif
#ifdef DEVICE_MODEL_GATEWAY
#define IOTX_DM_DEVICE_TYPE IOTX_DM_DEVICE_GATEWAY
#else
#define IOTX_DM_DEVICE_TYPE IOTX_DM_DEVICE_SINGLE
#endif
#define IOTX_DM_LOCAL_NODE_DEVID (0)
#define IOTX_DM_DEVICE_SINGLE (0x01)
#define IOTX_DM_DEVICE_SUBDEV (0x02)
#define IOTX_DM_DEVICE_GATEWAY (0x04)
#define IOTX_DM_DEVICE_MAIN (IOTX_DM_DEVICE_SINGLE|IOTX_DM_DEVICE_GATEWAY)
#define IOTX_DM_DEVICE_ALL (IOTX_DM_DEVICE_SINGLE|IOTX_DM_DEVICE_SUBDEV|IOTX_DM_DEVICE_GATEWAY)
/* Service Type 0~7bit: type, 8~15bit: extended*/
#define IOTX_DM_SERVICE_CLOUD (0x0001)
#define IOTX_DM_SERVICE_LOCAL (0x0002)
#define IOTX_DM_SERVICE_LOCAL_NO_AUTH (0x0000)
#define IOTX_DM_SERVICE_LOCAL_AUTH (0x0100)
#define IOTX_DM_LOCAL_AUTH (IOTX_DM_SERVICE_LOCAL|IOTX_DM_SERVICE_LOCAL_AUTH)
#define IOTX_DM_LOCAL_NO_AUTH (IOTX_DM_SERVICE_LOCAL|IOTX_DM_SERVICE_LOCAL_NO_AUTH)
#define IOTX_DM_SERVICE_ALL (IOTX_DM_SERVICE_CLOUD|IOTX_DM_LOCAL_AUTH)
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 enum {
IOTX_DM_EVENT_CLOUD_CONNECTED = 0,
IOTX_DM_EVENT_CLOUD_DISCONNECT,
IOTX_DM_EVENT_CLOUD_RECONNECT,
IOTX_DM_EVENT_LOCAL_CONNECTED,
IOTX_DM_EVENT_LOCAL_DISCONNECT,
IOTX_DM_EVENT_LOCAL_RECONNECT,
IOTX_DM_EVENT_FOUND_DEVICE,
IOTX_DM_EVENT_REMOVE_DEVICE,
IOTX_DM_EVENT_REGISTER_RESULT,
IOTX_DM_EVENT_UNREGISTER_RESULT,
IOTX_DM_EVENT_INITIALIZED,
IOTX_DM_EVENT_SEND_RESULT,
IOTX_DM_EVENT_ADD_SERVICE_RESULT,
IOTX_DM_EVENT_REMOVE_SERVICE_RESULT,
IOTX_DM_EVENT_NEW_DATA_RECEIVED,
IOTX_DM_EVENT_PROPERTY_SET,
IOTX_DM_EVENT_PROPERTY_GET,
IOTX_DM_EVENT_PROPERTY_DESIRED_GET_REPLY,
IOTX_DM_EVENT_PROPERTY_DESIRED_DELETE_REPLY,
IOTX_DM_EVENT_TOPO_ADD_NOTIFY,
IOTX_DM_EVENT_THING_SERVICE_REQUEST,
IOTX_DM_EVENT_THING_DISABLE,
IOTX_DM_EVENT_THING_ENABLE,
IOTX_DM_EVENT_THING_DELETE,
IOTX_DM_EVENT_MODEL_DOWN_RAW,
IOTX_DM_EVENT_GATEWAY_PERMIT,
IOTX_DM_EVENT_SUBDEV_REGISTER_REPLY,
IOTX_DM_EVENT_SUBDEV_UNREGISTER_REPLY,
IOTX_DM_EVENT_TOPO_ADD_REPLY,
IOTX_DM_EVENT_TOPO_DELETE_REPLY,
IOTX_DM_EVENT_TOPO_GET_REPLY,
IOTX_DM_EVENT_TOPO_ADD_NOTIFY_REPLY,
IOTX_DM_EVENT_EVENT_PROPERTY_POST_REPLY,
IOTX_DM_EVENT_EVENT_SPECIFIC_POST_REPLY,
IOTX_DM_EVENT_DEVICEINFO_UPDATE_REPLY,
IOTX_DM_EVENT_DEVICEINFO_DELETE_REPLY,
IOTX_DM_EVENT_DSLTEMPLATE_GET_REPLY,
IOTX_DM_EVENT_COMBINE_LOGIN_REPLY,
IOTX_DM_EVENT_COMBINE_LOGOUT_REPLY,
IOTX_DM_EVENT_MODEL_UP_RAW_REPLY,
IOTX_DM_EVENT_LEGACY_THING_CREATED,
IOTX_DM_EVENT_COTA_NEW_CONFIG,
IOTX_DM_EVENT_FOTA_NEW_FIRMWARE,
IOTX_DM_EVENT_NTP_RESPONSE,
IOTX_DM_EVENT_RRPC_REQUEST,
IOTX_DM_EVENT_MAX
} iotx_dm_event_types_t;
typedef void (*iotx_dm_event_callback)(iotx_dm_event_types_t type, char *payload);
typedef enum {
IOTX_DM_DEVICE_SECRET_PRODUCT,
IOTX_DM_DEVICE_SECRET_DEVICE,
IOTX_DM_DEVICE_SECRET_TYPES_MAX
} iotx_dm_device_secret_types_t;
typedef enum {
IOTX_DM_CLOUD_DOMAIN_SHANGHAI,
IOTX_DM_CLOUD_DOMAIN_SINGAPORE,
IOTX_DM_CLOUD_DOMAIN_JAPAN,
IOTX_DM_CLOUD_DOMAIN_AMERICA,
IOTX_DM_CLOUD_DOMAIN_GERMANY,
IOTX_DM_CLOUD_DOMAIN_MAX
} iotx_dm_cloud_domain_types_t;
typedef enum {
IOTX_DM_MESSAGE_NO_AUTH,
IOTX_DM_MESSAGE_AUTH,
IOTX_DM_MESSAGE_AUTH_MAX
} iotx_dm_message_auth_types_t;
typedef enum {
IOTX_DM_TSL_SOURCE_LOCAL,
IOTX_DM_TSL_SOURCE_CLOUD
} iotx_dm_tsl_source_t;
typedef enum {
IOTX_DM_TSL_TYPE_ALINK,
IOTX_DM_TSL_TYPE_TLV
} iotx_dm_tsl_type_t;
typedef struct {
iotx_dm_device_secret_types_t secret_type;
iotx_dm_cloud_domain_types_t domain_type;
iotx_dm_event_callback event_callback;
} iotx_dm_init_params_t;
typedef enum {
IOTX_DM_DEV_AVAIL_ENABLE,
IOTX_DM_DEV_AVAIL_DISABLE
} iotx_dm_dev_avail_t;
typedef enum {
IOTX_DM_DEV_STATUS_UNAUTHORIZED, /* Subdev Created */
IOTX_DM_DEV_STATUS_AUTHORIZED, /* Receive Topo Add Notify */
IOTX_DM_DEV_STATUS_REGISTERED, /* Receive Subdev Registered */
IOTX_DM_DEV_STATUS_ATTACHED, /* Receive Subdev Topo Add Reply */
IOTX_DM_DEV_STATUS_LOGINED, /* Receive Subdev Login Reply */
IOTX_DM_DEV_STATUS_ONLINE /* After All Topic Subscribed */
} iotx_dm_dev_status_t;
typedef enum {
DM_TSL_SERVICE_GET_FAILED = -13,
DM_TSL_SERVICE_SET_FAILED = -12,
DM_TSL_EVENT_GET_FAILED = -11,
DM_TSL_EVENT_SET_FAILED = -10,
DM_TSL_PROPERTY_GET_FAILED = -9,
DM_TSL_PROPERTY_SET_FAILED = -8,
DM_TSL_EVENT_NOT_EXIST = -7,
DM_TSL_PROPERTY_NOT_EXIST = -6,
DM_TSL_SERVICE_NOT_EXIST = -5,
DM_JSON_PARSE_FAILED = -4,
DM_MEMORY_NOT_ENOUGH = -3,
DM_INVALID_PARAMETER = -2
} dm_error_code_t;
#define IOTX_DM_POST_PROPERTY_ALL (NULL)
int iotx_dm_open(void);
int iotx_dm_connect(_IN_ iotx_dm_event_callback cb);
int iotx_dm_subscribe(_IN_ int devid);
int iotx_dm_close(void);
int iotx_dm_yield(int timeout_ms);
void iotx_dm_dispatch(void);
int iotx_dm_post_rawdata(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
int iotx_dm_set_opt(int opt, void *data);
int iotx_dm_get_opt(int opt, void *data);
#ifdef LOG_REPORT_TO_CLOUD
int iotx_dm_log_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
#endif
int iotx_dm_post_property(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int iotx_dm_property_desired_get(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int iotx_dm_property_desired_delete(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int iotx_dm_post_event(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *payload,
_IN_ int payload_len);
int iotx_dm_send_service_response(_IN_ int devid, _IN_ char *msgid, _IN_ int msgid_len, _IN_ iotx_dm_error_code_t code,
_IN_ char *identifier,
_IN_ int identifier_len, _IN_ char *payload, _IN_ int payload_len, void *ctx);
int iotx_dm_send_property_get_response(_IN_ int devid, _IN_ char *msgid, _IN_ int msgid_len,
_IN_ iotx_dm_error_code_t code,
_IN_ char *payload, _IN_ int payload_len, _IN_ void *ctx);
int iotx_dm_send_rrpc_response(_IN_ int devid, _IN_ char *msgid, _IN_ int msgid_len, _IN_ iotx_dm_error_code_t code,
_IN_ char *rrpcid, _IN_ int rrpcid_len, _IN_ char *payload, _IN_ int payload_len);
int iotx_dm_deviceinfo_update(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int iotx_dm_deviceinfo_delete(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int iotx_dm_qurey_ntp(void);
int iotx_dm_send_aos_active(int devid);
#endif
int iotx_dm_cota_perform_sync(_OU_ char *buffer, _IN_ int buffer_len);
int iotx_dm_cota_get_config(_IN_ const char *config_scope, const char *get_type, const char *attribute_keys);
int iotx_dm_fota_perform_sync(_OU_ char *buffer, _IN_ int buffer_len);
int iotx_dm_fota_request_image(_IN_ const char *version, _IN_ int buffer_len);
#ifdef DEVICE_MODEL_GATEWAY
int iotx_dm_query_topo_list(void);
int iotx_dm_subdev_query(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ int *devid);
int iotx_dm_subdev_create(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1], _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1], _OU_ int *devid);
int iotx_dm_subdev_destroy(_IN_ int devid);
int iotx_dm_subdev_number(void);
int iotx_dm_subdev_register(_IN_ int devid);
int iotx_dm_subdev_unregister(_IN_ int devid);
int iotx_dm_subdev_topo_add(_IN_ int devid);
int iotx_dm_subdev_topo_del(_IN_ int devid);
int iotx_dm_subdev_login(_IN_ int devid);
int iotx_dm_subdev_logout(_IN_ int devid);
int iotx_dm_get_device_type(_IN_ int devid, _OU_ int *type);
int iotx_dm_get_device_avail_status(_IN_ int devid, _OU_ iotx_dm_dev_avail_t *status);
int iotx_dm_get_device_status(_IN_ int devid, _OU_ iotx_dm_dev_status_t *status);
#ifdef DEVICE_MODEL_SUBDEV_OTA
int iotx_dm_send_firmware_version(int devid, const char *firmware_version);
int iotx_dm_ota_switch_device(_IN_ int devid);
#endif
#endif
#ifdef DEPRECATED_LINKKIT
int iotx_dm_deprecated_subdev_register(_IN_ int devid, _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
int iotx_dm_deprecated_set_tsl(_IN_ int devid, _IN_ iotx_dm_tsl_source_t source, _IN_ const char *tsl,
_IN_ int tsl_len);
int iotx_dm_deprecated_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int iotx_dm_deprecated_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int iotx_dm_deprecated_set_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int iotx_dm_deprecated_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int iotx_dm_deprecated_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int iotx_dm_deprecated_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int iotx_dm_deprecated_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int iotx_dm_deprecated_post_property_start(_IN_ int devid, _OU_ void **handle);
int iotx_dm_deprecated_post_property_add(_IN_ void *handle, _IN_ char *identifier, _IN_ int identifier_len);
int iotx_dm_deprecated_post_property_end(_IN_ void **handle);
int iotx_dm_deprecated_post_event(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len);
int iotx_dm_deprecated_send_service_response(_IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
_IN_ char *identifier,
_IN_ int identifier_len);
int iotx_dm_deprecated_legacy_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char *value_str);
int iotx_dm_deprecated_legacy_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char **value_str);
int iotx_dm_deprecated_legacy_set_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char *value_str);
int iotx_dm_deprecated_legacy_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ char **value_str);
int iotx_dm_deprecated_legacy_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
_IN_ void *value,
_IN_ char **value_str);
int iotx_dm_deprecated_legacy_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
_IN_ void *value,
_IN_ char *value_str);
int iotx_dm_deprecated_legacy_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len,
_IN_ void *value,
_IN_ char **value_str);
int iotx_dm_deprecated_legacy_get_pkdn_by_devid(_IN_ int devid, _OU_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_OU_ char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int iotx_dm_deprecated_legacy_get_devid_by_pkdn(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ int *devid);
int iotx_dm_deprecated_legacy_get_thingid_by_devid(_IN_ int devid, _OU_ void **thing_id);
int iotx_dm_deprecated_legacy_get_devid_by_thingid(_IN_ void *thing_id, _OU_ int *devid);
int iotx_dm_deprecated_legacy_get_pkdn_ptr_by_devid(_IN_ int devid, _OU_ char **product_key, _OU_ char **device_name);
int iotx_dm_deprecated_legacy_send_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);
#ifdef DEVICE_MODEL_GATEWAY
int iotx_dm_deprecated_subdev_register(_IN_ int devid, _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
#endif
#endif
#endif
/*
* Copyright (C) 2020-2020 ikonke
*/
#include "kk_dm_api.h"
#include "kk_tsl_common.h"
#include "kk_dm_mng.h"
static dm_api_ctx_t g_dm_api_ctx;
static dm_api_ctx_t *_dm_api_get_ctx(void)
{
return &g_dm_api_ctx;
}
static void _dm_api_lock(void)
{
dm_api_ctx_t *ctx = _dm_api_get_ctx();
if (ctx->mutex) {
HAL_MutexLock(ctx->mutex);
}
}
static void _dm_api_unlock(void)
{
dm_api_ctx_t *ctx = _dm_api_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
}
int iotx_dm_post_property(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len)
{
int res = 0;
_dm_api_lock();
res = dm_mgr_upstream_thing_property_post(devid, payload, payload_len);
if (res < SUCCESS_RETURN) {
_dm_api_unlock();
return FAIL_RETURN;
}
_dm_api_unlock();
return res;
}
int iotx_dm_subdev_create(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN],
_IN_ char device_secret[DEVICE_SECRET_MAXLEN], _OU_ int *devid)
{
int res = 0;
if (product_key == NULL || device_name == NULL ||
(strlen(product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(device_name) >= DEVICE_NAME_MAXLEN) ||
devid == NULL) {
return INVALID_PARAMETER;
}
if (device_secret != NULL && strlen(device_secret) >= DEVICE_SECRET_MAXLEN) {
return INVALID_PARAMETER;
}
_dm_api_lock();
res = dm_mgr_device_create(IOTX_DM_DEVICE_SUBDEV, product_key, device_name, device_secret, devid);
if (res != SUCCESS_RETURN) {
_dm_api_unlock();
return FAIL_RETURN;
}
_dm_api_unlock();
return SUCCESS_RETURN;
}
int kk_dm_subdev_register(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *search_node = NULL;
if (devid < 0) {
return INVALID_PARAMETER;
}
_dm_api_lock();
res = dm_mgr_search_dev_by_devid(devid, (void **)&search_node);
if (res != SUCCESS_RETURN) {
_dm_api_unlock();
return FAIL_RETURN;
}
if ((strlen(search_node->device_secret) > 0) && (strlen(search_node->device_secret) < DEVICE_SECRET_MAXLEN)) {
_dm_api_unlock();
return SUCCESS_RETURN;
}
res = dm_mgr_upstream_thing_sub_register(devid);
_dm_api_unlock();
return res;
}
int kk_dm_subdev_unregister(_IN_ int devid)
{
int res = 0;
if (devid < 0) {
return INVALID_PARAMETER;
}
_dm_api_lock();
res = dm_mgr_upstream_thing_sub_unregister(devid);
_dm_api_unlock();
return res;
}
int kk_dm_subdev_topo_add(_IN_ int devid)
{
int res = 0;
if (devid < 0) {
return INVALID_PARAMETER;
}
_dm_api_lock();
res = dm_mgr_upstream_thing_topo_add(devid);
_dm_api_unlock();
return res;
}
int iotx_dm_subdev_topo_del(_IN_ int devid)
{
int res = 0;
if (devid < 0) {
return DM_INVALID_PARAMETER;
}
_dm_api_lock();
res = dm_mgr_upstream_thing_topo_delete(devid);
_dm_api_unlock();
return res;
}
int iotx_dm_subdev_login(_IN_ int devid)
{
int res = 0;
if (devid < 0) {
return INVALID_PARAMETER;
}
_dm_api_lock();
res = dm_mgr_upstream_combine_login(devid);
_dm_api_unlock();
return res;
}
int iotx_dm_subdev_logout(_IN_ int devid)
{
int res = 0;
if (devid < 0) {
return DM_INVALID_PARAMETER;
}
_dm_api_lock();
res = dm_mgr_upstream_combine_logout(devid);
_dm_api_unlock();
return res;
}
void iotx_dm_dispatch(void)
{
int count = 0;
void *data = NULL;
dm_api_ctx_t *ctx = _dm_api_get_ctx();
#if defined(OTA_ENABLED) && !defined(BUILD_AOS)
dm_cota_status_check();
dm_fota_status_check();
#endif
while (CONFIG_DISPATCH_QUEUE_MAXLEN == 0 || count++ < CONFIG_DISPATCH_QUEUE_MAXLEN) {
//printf("iotx_dm_dispatch time=%d \n", HAL_UptimeMs());
if (dm_queue_msg_next(&data) == SUCCESS_RETURN) {
//dm_queue_msg_t *msg = (dm_queue_msg_t *)data;
printf("dm_queue_msg_next call \n");
if (ctx->event_callback) {
ctx->event_callback(1, data);
}
free(data);
data = NULL;
} else {
break;
}
}
}
int iotx_dm_connect(_IN_ iotx_dm_event_callback cb)
{
int res = 0;
dm_api_ctx_t *ctx = _dm_api_get_ctx();
if (cb == NULL) {
return INVALID_PARAMETER;
}
/* DM Event Callback */
ctx->event_callback = cb;
/*res = dm_client_connect(IOTX_DM_CLIENT_CONNECT_TIMEOUT_MS);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
#ifdef ALCS_ENABLED
res = dm_server_connect();
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
#endif*/
return SUCCESS_RETURN;
}
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _KK_DM_API_H_
#define _KK_DM_API_H_
#include "iotx_dm.h"
#define CONFIG_DISPATCH_QUEUE_MAXLEN (50)
typedef struct {
void *mutex;
void *cloud_connectivity;
void *local_connectivity;
iotx_dm_event_callback event_callback;
} dm_api_ctx_t;
#if defined(DEPRECATED_LINKKIT)
typedef struct {
void *mutex;
int devid;
lite_cjson_item_t *lite;
} dm_api_property_t;
#endif
#endif
......@@ -5,6 +5,46 @@
#include "kk_dm_msg.h"
#include "kk_tsl_api.h"
#include "kk_tsl_load.h"
#include "iot_export_linkkit.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";
/* From Cloud To Local Request And Response*/
const char DM_URI_THING_TOPO_ADD_NOTIFY[] DM_READ_ONLY = "thing/topo/add/notify";
const char DM_URI_THING_TOPO_ADD_NOTIFY_REPLY[] DM_READ_ONLY = "thing/topo/add/notify_reply";
const char DM_URI_THING_DELETE[] DM_READ_ONLY = "thing/delete";
const char DM_URI_THING_DELETE_REPLY[] DM_READ_ONLY = "thing/delete_reply";
const char DM_URI_THING_DISABLE[] DM_READ_ONLY = "thing/disable";
const char DM_URI_THING_DISABLE_REPLY[] DM_READ_ONLY = "thing/disable_reply";
const char DM_URI_THING_ENABLE[] DM_READ_ONLY = "thing/enable";
const char DM_URI_THING_ENABLE_REPLY[] DM_READ_ONLY = "thing/enable_reply";
const char DM_URI_THING_GATEWAY_PERMIT[] DM_READ_ONLY = "thing/gateway/permit";
const char DM_URI_THING_GATEWAY_PERMIT_REPLY[] DM_READ_ONLY = "thing/gateway/permit_reply";
/* From Local To Cloud Request And Response*/
const char DM_URI_THING_SUB_REGISTER[] DM_READ_ONLY = "thing/sub/register";
const char DM_URI_THING_SUB_REGISTER_REPLY[] DM_READ_ONLY = "thing/sub/register_reply";
const char DM_URI_THING_SUB_UNREGISTER[] DM_READ_ONLY = "thing/sub/unregister";
const char DM_URI_THING_SUB_UNREGISTER_REPLY[] DM_READ_ONLY = "thing/sub/unregister_reply";
const char DM_URI_THING_TOPO_ADD[] DM_READ_ONLY = "thing/topo/add";
const char DM_URI_THING_TOPO_ADD_REPLY[] DM_READ_ONLY = "thing/topo/add_reply";
const char DM_URI_THING_TOPO_DELETE[] DM_READ_ONLY = "thing/topo/delete";
const char DM_URI_THING_TOPO_DELETE_REPLY[] DM_READ_ONLY = "thing/topo/delete_reply";
const char DM_URI_THING_TOPO_GET[] DM_READ_ONLY = "thing/topo/get";
const char DM_URI_THING_TOPO_GET_REPLY[] DM_READ_ONLY = "thing/topo/get_reply";
const char DM_URI_THING_LIST_FOUND[] DM_READ_ONLY = "thing/list/found";
const char DM_URI_THING_LIST_FOUND_REPLY[] DM_READ_ONLY = "thing/list/found_reply";
const char DM_URI_COMBINE_LOGIN[] DM_READ_ONLY = "combine/login";
const char DM_URI_COMBINE_LOGIN_REPLY[] DM_READ_ONLY = "combine/login_reply";
const char DM_URI_COMBINE_LOGOUT[] DM_READ_ONLY = "combine/logout";
const char DM_URI_COMBINE_LOGOUT_REPLY[] DM_READ_ONLY = "combine/logout_reply";
static dm_mgr_ctx g_dm_mgr = {0};
static dm_mgr_ctx *_dm_mgr_get_ctx(void)
......@@ -226,7 +266,7 @@ static int _dm_mgr_upstream_request_assemble(_IN_ int msgid, _IN_ int devid, _IN
return SUCCESS_RETURN;
}
static unsigned int g_report_id = 0;
static unsigned int g_report_id = 1;
int iotx_report_id(void)
{
......@@ -366,3 +406,308 @@ int dm_mgr_deprecated_upstream_thing_service_response(_IN_ int devid, _IN_ int m
return SUCCESS_RETURN;
}
int dm_mgr_upstream_thing_sub_register(_IN_ int devid)
{
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.service_prefix = DM_URI_SYS_PREFIX;
request.service_name = DM_URI_THING_SUB_REGISTER;
HAL_GetProduct_Type(request.product_key);
HAL_GetProduct_Code(request.device_name);
/* Get Params And Method */
res = dm_msg_thing_sub_register(node->product_key, node->device_name, &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);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
int dm_mgr_upstream_thing_sub_unregister(_IN_ int devid)
{
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.service_prefix = DM_URI_SYS_PREFIX;
request.service_name = DM_URI_THING_SUB_UNREGISTER;
HAL_GetProduct_Type(request.product_key);
HAL_GetProduct_Code(request.device_name);
/* Get Params And Method */
res = dm_msg_thing_sub_unregister(node->product_key, node->device_name, &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);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
int dm_mgr_upstream_thing_topo_add(_IN_ int devid)
{
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.service_prefix = DM_URI_SYS_PREFIX;
request.service_name = DM_URI_THING_TOPO_ADD;
HAL_GetProduct_Type(request.product_key);
HAL_GetProduct_Code(request.device_name);
/* Get Params And Method */
res = dm_msg_thing_topo_add(node->product_key, node->device_name, node->device_secret, &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);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
int dm_mgr_upstream_combine_login(_IN_ int devid)
{
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.service_prefix = DM_URI_EXT_SESSION_PREFIX;
request.service_name = DM_URI_COMBINE_LOGIN;
HAL_GetProduct_Type(request.product_key);
HAL_GetProduct_Code(request.device_name);
/* Get Params And Method */
res = dm_msg_combine_login(node->product_key, node->device_name, node->device_secret, &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);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
int dm_mgr_upstream_combine_logout(_IN_ int devid)
{
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;
}
if (node->dev_status < IOTX_DM_DEV_STATUS_LOGINED) {
return FAIL_RETURN;
}
memset(&request, 0, sizeof(dm_msg_request_t));
request.service_prefix = DM_URI_EXT_SESSION_PREFIX;
request.service_name = DM_URI_COMBINE_LOGOUT;
HAL_GetProduct_Type(request.product_key);
HAL_GetProduct_Code(request.device_name);
/* Get Params And Method */
res = dm_msg_combine_logout(node->product_key, node->device_name, &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);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
int dm_mgr_upstream_thing_topo_delete(_IN_ int devid)
{
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.service_prefix = DM_URI_SYS_PREFIX;
request.service_name = DM_URI_THING_TOPO_DELETE;
HAL_GetProduct_Type(request.product_key);
HAL_GetProduct_Code(request.device_name);
/* Get Params And Method */
res = dm_msg_thing_topo_delete(node->product_key, node->device_name, &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);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
int dm_mgr_subdev_create(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN], _IN_ char device_secret[DEVICE_SECRET_MAXLEN], _OU_ int *devid){
int res = 0;
res = dm_mgr_device_create(KK_DM_DEVICE_SUBDEV,product_key,device_name,device_secret, devid);
if (res != SUCCESS_RETURN) {
printf("subdev open Failed\n");
return FAIL_RETURN;
}
return res;
}
\ No newline at end of file
......@@ -3,9 +3,12 @@
#include "kk_tsl_common.h"
#include "klist.h"
#include "iotx_dm.h"
#define KK_DM_DEVICE_SINGLE (0x01)
#define KK_DM_DEVICE_SUBDEV (0x02)
#define KK_DM_DEVICE_GATEWAY (0x04)
#define KK_MID_SYNC_DEFAULT_TIMEOUT_MS (200)
typedef struct {
int devid;
......@@ -14,8 +17,8 @@ typedef struct {
char product_key[PRODUCT_KEY_MAXLEN];
char device_name[DEVICE_NAME_MAXLEN];
char device_secret[DEVICE_SECRET_MAXLEN];
//iotx_dm_dev_avail_t status;
//iotx_dm_dev_status_t dev_status;
iotx_dm_dev_avail_t status;
iotx_dm_dev_status_t dev_status;
struct list_head linked_list;
} dm_mgr_dev_node_t;
......
......@@ -29,6 +29,315 @@ void kk_sendData2app(void *uri, void *payload){
}
const char DM_MSG_THING_SUB_REGISTER_METHOD[] = "thing.sub.register";
const char DM_MSG_THING_SUB_REGISTER_PARAMS[] = "[{\"productKey\":\"%s\",\"deviceName\":\"%s\"}]";
int dm_msg_thing_sub_register(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN],
_OU_ dm_msg_request_t *request)
{
int params_len = 0;
char *params = NULL;
if (request == NULL || product_key == NULL || device_name == NULL ||
(strlen(product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(device_name) >= DEVICE_NAME_MAXLEN) ||
(strlen(request->product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(request->device_name) >= DEVICE_NAME_MAXLEN)) {
return INVALID_PARAMETER;
}
params_len = strlen(DM_MSG_THING_SUB_REGISTER_PARAMS) + strlen(product_key) + strlen(device_name) + 1;
params = malloc(params_len);
if (params == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(params, 0, params_len);
snprintf(params, params_len, DM_MSG_THING_SUB_REGISTER_PARAMS, product_key, device_name);
/* Get Params */
request->params = params;
request->params_len = strlen(request->params);
/* Get Method */
request->method = (char *)DM_MSG_THING_SUB_REGISTER_METHOD;
return SUCCESS_RETURN;
}
const char DM_MSG_THING_SUB_UNREGISTER_METHOD[] DM_READ_ONLY = "thing.sub.unregister";
const char DM_MSG_THING_SUB_UNREGISTER_PARAMS[] DM_READ_ONLY = "[{\"productKey\":\"%s\",\"deviceName\":\"%s\"}]";
int dm_msg_thing_sub_unregister(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN],
_OU_ dm_msg_request_t *request)
{
int params_len = 0;
char *params = NULL;
if (request == NULL || product_key == NULL || device_name == NULL ||
(strlen(product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(device_name) >= DEVICE_NAME_MAXLEN) ||
(strlen(request->product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(request->device_name) >= DEVICE_NAME_MAXLEN)) {
return INVALID_PARAMETER;
}
params_len = strlen(DM_MSG_THING_SUB_UNREGISTER_PARAMS) + strlen(product_key) + strlen(device_name) + 1;
params = malloc(params_len);
if (params == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(params, 0, params_len);
snprintf(params, params_len, DM_MSG_THING_SUB_UNREGISTER_PARAMS, product_key, device_name);
/* Get Params */
request->params = params;
request->params_len = strlen(request->params);
/* Get Method */
request->method = (char *)DM_MSG_THING_SUB_UNREGISTER_METHOD;
return SUCCESS_RETURN;
}
const char DM_MSG_THING_TOPO_ADD_SIGN_SOURCE[] DM_READ_ONLY = "clientId%sdeviceName%sproductKey%stimestamp%s";
const char DM_MSG_THING_TOPO_ADD_METHOD[] DM_READ_ONLY = "thing.topo.add";
const char DM_MSG_THING_TOPO_ADD_PARAMS[] DM_READ_ONLY =
"[{\"productKey\":\"%s\",\"deviceName\":\"%s\",\"signmethod\":\"%s\",\"sign\":\"%s\",\"timestamp\":\"%s\",\"clientId\":\"%s\"}]";
int dm_msg_thing_topo_add(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN],
_IN_ char device_secret[DEVICE_SECRET_MAXLEN], _OU_ dm_msg_request_t *request)
{
char *params = NULL;
int params_len = 0;
char timestamp[DM_UTILS_UINT64_STRLEN] = {0};
char client_id[PRODUCT_KEY_MAXLEN + DEVICE_NAME_MAXLEN + 1] = {0};
char *sign_source = NULL;
int sign_source_len = 0;
char *sign_method = DM_MSG_SIGN_METHOD_HMACSHA1;
char sign[65] = {0};
if (request == NULL || product_key == NULL ||
device_name == NULL || device_secret == NULL ||
(strlen(product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(device_name) >= DEVICE_NAME_MAXLEN) ||
(strlen(device_secret) >= DEVICE_SECRET_MAXLEN) ||
(strlen(request->product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(request->device_name) >= DEVICE_NAME_MAXLEN)) {
return INVALID_PARAMETER;
}
/* TimeStamp */
HAL_Snprintf(timestamp, DM_UTILS_UINT64_STRLEN, "%llu", (unsigned long long)HAL_UptimeMs());
/* dm_log_debug("Time Stamp: %s", timestamp); */
/* Client ID */
HAL_Snprintf(client_id, PRODUCT_KEY_MAXLEN + DEVICE_NAME_MAXLEN + 1, "%s.%s", product_key, device_name);
/* Sign */
sign_source_len = strlen(DM_MSG_THING_TOPO_ADD_SIGN_SOURCE) + strlen(client_id) +
strlen(device_name) + strlen(product_key) + strlen(timestamp) + 1;
sign_source = malloc(sign_source_len);
if (sign_source == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(sign_source, 0, sign_source_len);
HAL_Snprintf(sign_source, sign_source_len, DM_MSG_THING_TOPO_ADD_SIGN_SOURCE, client_id,
device_name, product_key, timestamp);
/* dm_log_debug("Sign Srouce: %s", sign_source); */
#if 0
if (strcmp(sign_method, DM_MSG_SIGN_METHOD_HMACMD5) == 0) {
utils_hmac_md5(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
} else if (strcmp(sign_method, DM_MSG_SIGN_METHOD_HMACSHA1) == 0) {
utils_hmac_sha1(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
} else if (strcmp(sign_method, DM_MSG_SIGN_METHOD_HMACSHA256) == 0) {
utils_hmac_sha256(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
} else {
DM_free(sign_source);
return FAIL_RETURN;
}
#else
//utils_hmac_sha1(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
#endif
free(sign_source);
/* dm_log_debug("Sign : %s", sign); */
/* Params */
request->method = (char *)DM_MSG_THING_TOPO_ADD_METHOD;
params_len = strlen(DM_MSG_THING_TOPO_ADD_PARAMS) + strlen(product_key) + strlen(device_name) +
strlen(sign_method) + strlen(sign) + strlen(timestamp) + strlen(client_id) + 1;
params = malloc(params_len);
if (params == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(params, 0, params_len);
HAL_Snprintf(params, params_len, DM_MSG_THING_TOPO_ADD_PARAMS, product_key, device_name,
sign_method, sign, timestamp, client_id);
request->params = params;
request->params_len = strlen(request->params);
return SUCCESS_RETURN;
}
const char DM_MSG_COMBINE_LOGIN_SIGN_SOURCE[] DM_READ_ONLY = "clientId%sdeviceName%sproductKey%stimestamp%s";
const char DM_MSG_COMBINE_LOGIN_METHOD[] DM_READ_ONLY = "combine.login";
const char DM_MSG_COMBINE_LOGIN_PARAMS[] DM_READ_ONLY =
"{\"productKey\":\"%s\",\"deviceName\":\"%s\",\"clientId\":\"%s\",\"timestamp\":\"%s\",\"signMethod\":\"%s\",\"sign\":\"%s\",\"cleanSession\":\"%s\"}";
int dm_msg_combine_login(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN],
_IN_ char device_secret[DEVICE_SECRET_MAXLEN], _OU_ dm_msg_request_t *request)
{
char *params = NULL;
int params_len = 0;
char timestamp[DM_UTILS_UINT64_STRLEN] = {0};
char client_id[PRODUCT_KEY_MAXLEN + DEVICE_NAME_MAXLEN + 20] = {0};
char *sign_source = NULL;
int sign_source_len = 0;
char *sign_method = DM_MSG_SIGN_METHOD_HMACSHA1;
char sign[64] = {0};
if (request == NULL || product_key == NULL ||
device_name == NULL || device_secret == NULL ||
(strlen(product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(device_name) >= DEVICE_NAME_MAXLEN) ||
(strlen(device_secret) >= DEVICE_SECRET_MAXLEN) ||
(strlen(request->product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(request->device_name) >= DEVICE_NAME_MAXLEN)) {
return INVALID_PARAMETER;
}
/* TimeStamp */
HAL_Snprintf(timestamp, DM_UTILS_UINT64_STRLEN, "%llu", (unsigned long long)HAL_UptimeMs());
/* dm_log_debug("Time Stamp: %s", timestamp); */
/* Client ID */
HAL_Snprintf(client_id, PRODUCT_KEY_MAXLEN + DEVICE_NAME_MAXLEN + 20, "%s.%s|_v=sdk-c-3.0.1|", product_key, device_name);
/* Sign */
sign_source_len = strlen(DM_MSG_COMBINE_LOGIN_SIGN_SOURCE) + strlen(client_id) +
strlen(device_name) + strlen(product_key) + strlen(timestamp) + 1;
sign_source = malloc(sign_source_len);
if (sign_source == NULL) {
return DM_MEMORY_NOT_ENOUGH;
}
memset(sign_source, 0, sign_source_len);
HAL_Snprintf(sign_source, sign_source_len, DM_MSG_COMBINE_LOGIN_SIGN_SOURCE, client_id,
device_name, product_key, timestamp);
/* dm_log_debug("Sign Srouce: %s", sign_source); */
#if 0
if (strcmp(sign_method, DM_MSG_SIGN_METHOD_HMACMD5) == 0) {
utils_hmac_md5(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
} else if (strcmp(sign_method, DM_MSG_SIGN_METHOD_HMACSHA1) == 0) {
utils_hmac_sha1(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
} else if (strcmp(sign_method, DM_MSG_SIGN_METHOD_HMACSHA256) == 0) {
utils_hmac_sha256(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
} else {
DM_free(sign_source);
return FAIL_RETURN;
}
#else
//utils_hmac_sha1(sign_source, strlen(sign_source), sign, device_secret, strlen(device_secret));
#endif
free(sign_source);
/* dm_log_debug("Sign : %s", sign); */
/* Params */
request->method = (char *)DM_MSG_COMBINE_LOGIN_METHOD;
params_len = strlen(DM_MSG_COMBINE_LOGIN_PARAMS) + strlen(product_key) + strlen(device_name) +
strlen(sign_method) + strlen(sign) + strlen(timestamp) + strlen(client_id) + 1;
params = malloc(params_len);
if (params == NULL) {
return DM_MEMORY_NOT_ENOUGH;
}
memset(params, 0, params_len);
HAL_Snprintf(params, params_len, DM_MSG_COMBINE_LOGIN_PARAMS, product_key, device_name,
client_id, timestamp, sign_method, sign, "true");
request->params = params;
request->params_len = strlen(request->params);
return SUCCESS_RETURN;
}
const char DM_MSG_COMBINE_LOGOUT_METHOD[] DM_READ_ONLY = "combine.logout";
const char DM_MSG_COMBINE_LOGOUT_PARAMS[] DM_READ_ONLY = "{\"productKey\":\"%s\",\"deviceName\":\"%s\"}";
int dm_msg_combine_logout(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN],
_OU_ dm_msg_request_t *request)
{
char *params = NULL;
int params_len = 0;
if (product_key == NULL || device_name == NULL ||
(strlen(product_key) >= PRODUCT_KEY_MAXLEN ||
(strlen(device_name) >= DEVICE_NAME_MAXLEN) ||
request == NULL) ){
return INVALID_PARAMETER;
}
/* Params */
request->method = (char *)DM_MSG_COMBINE_LOGOUT_METHOD;
params_len = strlen(DM_MSG_COMBINE_LOGOUT_PARAMS) + strlen(product_key) + strlen(device_name) + 1;
params = malloc(params_len);
if (params == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(params, 0, params_len);
HAL_Snprintf(params, params_len, DM_MSG_COMBINE_LOGOUT_PARAMS, product_key, device_name);
request->params = params;
request->params_len = strlen(request->params);
return SUCCESS_RETURN;
}
const char DM_MSG_THING_TOPO_DELETE_METHOD[] DM_READ_ONLY = "thing.topo.delete";
const char DM_MSG_THING_TOPO_DELETE_PARAMS[] DM_READ_ONLY = "[{\"productKey\":\"%s\",\"deviceName\":\"%s\"}]";
int dm_msg_thing_topo_delete(_IN_ char product_key[PRODUCT_KEY_MAXLEN],
_IN_ char device_name[DEVICE_NAME_MAXLEN],
_OU_ dm_msg_request_t *request)
{
char *params = NULL;
int params_len = 0;
if (request == NULL || product_key == NULL ||
device_name == NULL ||
(strlen(product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(device_name) >= DEVICE_NAME_MAXLEN) ||
(strlen(request->product_key) >= PRODUCT_KEY_MAXLEN) ||
(strlen(request->device_name) >= DEVICE_NAME_MAXLEN)) {
return INVALID_PARAMETER;
}
/* Params */
request->method = (char *)DM_MSG_THING_TOPO_DELETE_METHOD;
params_len = strlen(DM_MSG_THING_TOPO_DELETE_PARAMS) + strlen(product_key) + strlen(device_name) + 1;
params = malloc(params_len);
if (params == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(params, 0, params_len);
HAL_Snprintf(params, params_len, DM_MSG_THING_TOPO_DELETE_PARAMS, product_key, device_name);
request->params = params;
request->params_len = strlen(request->params);
return SUCCESS_RETURN;
}
int dm_msg_request (_IN_ dm_msg_request_t *request)
{
......@@ -67,7 +376,7 @@ int dm_msg_request (_IN_ dm_msg_request_t *request)
return FAIL_RETURN;
}
printf("DM Send Message, URI: %s, Payload: %s", uri, payload);
printf("DM Send Message, URI: %s, Payload: %s \n", uri, payload);
//if (type & DM_MSG_DEST_CLOUD) {
// dm_client_publish(uri, (unsigned char *)payload, strlen(payload), request->callback);
......@@ -129,3 +438,35 @@ int dm_msg_response(_IN_ kk_msg_request_payload_t *request, _IN_ kk_msg_response
return SUCCESS_RETURN;
}
int dm_msg_response_parse(_IN_ char *payload, _IN_ int payload_len, _OU_ dm_msg_response_payload_t *response)
{
lite_cjson_t lite, lite_message;
if (payload == NULL || payload_len <= 0 || response == NULL) {
return INVALID_PARAMETER;
}
if (kk_utils_json_parse(payload, payload_len, cJSON_Invalid, &lite) != SUCCESS_RETURN ||
kk_utils_json_object_item(&lite, DM_MSG_KEY_ID, strlen(DM_MSG_KEY_ID), cJSON_Invalid, &response->id) != SUCCESS_RETURN ||
kk_utils_json_object_item(&lite, DM_MSG_KEY_CODE, strlen(DM_MSG_KEY_CODE), cJSON_Invalid,
&response->code) != SUCCESS_RETURN ||
kk_utils_json_object_item(&lite, DM_MSG_KEY_DATA, strlen(DM_MSG_KEY_DATA), cJSON_Invalid,
&response->data) != SUCCESS_RETURN) {
printf("Current Request parse faild \n");
return FAIL_RETURN;
}
printf("Current Request Message ID: %.*s", response->id.value_length, response->id.value);
printf("Current Request Message Code: %d", response->code.value_int);
printf("Current Request Message Data: %.*s", response->data.value_length, response->data.value);
memset(&lite_message, 0, sizeof(lite_cjson_t));
if (kk_utils_json_object_item(&lite, DM_MSG_KEY_MESSAGE, strlen(DM_MSG_KEY_MESSAGE), cJSON_Invalid,
&response->message) == SUCCESS_RETURN) {
printf("Current Request Message Desc: %.*s", response->message.value_length, response->message.value);
}
return SUCCESS_RETURN;
}
#ifndef __KK_DM_MSG__
#define __KK_DM_MSG__
#include "kk_tsl_common.h"
#include "../tsl_handle/lite-cjson.h"
typedef enum {
#define DM_MSG_KEY_ID "id"
#define DM_MSG_KEY_VERSION "version"
#define DM_MSG_KEY_METHOD "method"
#define DM_MSG_KEY_PARAMS "params"
#define DM_MSG_KEY_CODE "code"
#define DM_MSG_KEY_DATA "data"
#define DM_MSG_KEY_MESSAGE "message"
#define DM_MSG_VERSION "1.0"
#define DM_MSG_KEY_PRODUCT_KEY "productKey"
#define DM_MSG_KEY_DEVICE_NAME "deviceName"
#define DM_MSG_KEY_DEVICE_SECRET "deviceSecret"
#define DM_MSG_KEY_TIME "time"
#define DM_MSG_SIGN_METHOD_SHA256 "Sha256"
#define DM_MSG_SIGN_METHOD_HMACMD5 "hmacMd5"
#define DM_MSG_SIGN_METHOD_HMACSHA1 "hmacSha1"
#define DM_MSG_SIGN_METHOD_HMACSHA256 "hmacSha256"
/*typedef enum {
IOTX_DM_EVENT_CLOUD_CONNECTED = 0,
IOTX_DM_EVENT_CLOUD_DISCONNECT,
IOTX_DM_EVENT_CLOUD_RECONNECT,
......@@ -47,7 +71,15 @@ typedef enum {
IOTX_DM_EVENT_NTP_RESPONSE,
IOTX_DM_EVENT_RRPC_REQUEST,
IOTX_DM_EVENT_MAX
} iotx_dm_event_types_t;
} iotx_dm_event_types_t;*/
typedef struct {
lite_cjson_t id;
lite_cjson_t code;
lite_cjson_t data;
lite_cjson_t message;
} dm_msg_response_payload_t;
#ifndef DM_READ_ONLY
#define DM_READ_ONLY
......
/*
* Copyright (C) 2020-2020 ikonke
*/
#include "kk_dm_queue.h"
#include "kk_tsl_common.h"
dm_queue_t g_dm_queue;
static dm_queue_t *_dm_queue_get_ctx(void)
{
return &g_dm_queue;
}
static void _dm_queue_lock(void)
{
dm_queue_t *ctx = _dm_queue_get_ctx();
if (ctx->mutex) {
HAL_MutexLock(ctx->mutex);
}
}
static void _dm_queue_unlock(void)
{
dm_queue_t *ctx = _dm_queue_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
}
int dm_queue_init(int max_size)
{
dm_queue_t *ctx = _dm_queue_get_ctx();
memset(ctx, 0, sizeof(dm_queue_t));
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
return INVALID_PARAMETER;
}
/* Init List */
ctx->msg_list.max_size = max_size;
INIT_LIST_HEAD(&ctx->msg_list.message_list);
return SUCCESS_RETURN;
}
void dm_queue_deinit(void)
{
dm_queue_t *ctx = _dm_queue_get_ctx();
dm_queue_msg_node_t *del_node = NULL;
dm_queue_msg_node_t *next_node = NULL;
dm_queue_msg_t *del_msg = NULL;
if (ctx->mutex) {
HAL_MutexDestroy(ctx->mutex);
}
list_for_each_entry_safe(del_node, next_node, &ctx->msg_list.message_list, linked_list, dm_queue_msg_node_t) {
/* Free Message */
del_msg = (dm_queue_msg_t *)del_node->data;
if (del_msg->data) {
free(del_msg->data);
}
free(del_msg);
del_msg = NULL;
/* Free Node */
list_del(&del_node->linked_list);
free(del_node);
}
}
int dm_queue_msg_insert(void *data)
{
dm_queue_t *ctx = _dm_queue_get_ctx();
dm_queue_msg_node_t *node = NULL;
if (data == NULL) {
return INVALID_PARAMETER;
}
_dm_queue_lock();
printf("dm msg list size: %d, max size: %d", ctx->msg_list.size, ctx->msg_list.max_size);
if (ctx->msg_list.size >= ctx->msg_list.max_size) {
printf("dm queue list full");
_dm_queue_unlock();
return FAIL_RETURN;
}
node = malloc(sizeof(dm_queue_msg_node_t));
if (node == NULL) {
_dm_queue_unlock();
return DM_MEMORY_NOT_ENOUGH;
}
memset(node, 0, sizeof(dm_queue_msg_node_t));
node->data = data;
INIT_LIST_HEAD(&node->linked_list);
ctx->msg_list.size++;
list_add_tail(&node->linked_list, &ctx->msg_list.message_list);
_dm_queue_unlock();
return SUCCESS_RETURN;
}
int dm_queue_msg_next(void **data)
{
dm_queue_t *ctx = _dm_queue_get_ctx();
dm_queue_msg_node_t *node = NULL;
if (data == NULL || *data != NULL) {
return INVALID_PARAMETER;
}
_dm_queue_lock();
if (list_empty(&ctx->msg_list.message_list)) {
_dm_queue_unlock();
return FAIL_RETURN;
}
node = list_first_entry(&ctx->msg_list.message_list, dm_queue_msg_node_t, linked_list);
list_del(&node->linked_list);
ctx->msg_list.size--;
*data = node->data;
free(node);
_dm_queue_unlock();
return SUCCESS_RETURN;
}
/*
* Copyright (C) 2020-2020 ikonke
*/
#ifndef _DM_QUEUE_H_
#define _DM_QUEUE_H_
#include "iotx_dm.h"
#include "klist.h"
typedef struct {
iotx_dm_event_types_t type;
char *data;
} dm_queue_msg_t;
typedef struct {
void *data;
struct list_head linked_list;
} dm_queue_msg_node_t;
typedef struct {
int max_size;
int size;
struct list_head message_list;
} dm_queue_msg_list_t;
typedef struct {
void *mutex;
dm_queue_msg_list_t msg_list;
} dm_queue_t;
int dm_queue_init(int max_size);
void dm_queue_deinit(void);
int dm_queue_msg_insert(void *data);
int dm_queue_msg_next(void **data);
#endif
/*
* Copyright (C) 2020-2020 ikonke
*/
//#include "iotx_dm_internal.h"
#include "kk_tsl_common.h"
#include "klist.h"
#include "cJSON.h"
#include "kk_dm_msg.h"
#include "kk_dm_api.h"
#include "com_api.h"
#define IOTX_LINKKIT_KEY_ID "id"
#define IOTX_LINKKIT_KEY_CODE "code"
#define IOTX_LINKKIT_KEY_DEVID "devid"
#define IOTX_LINKKIT_KEY_SERVICEID "serviceid"
#define IOTX_LINKKIT_KEY_PROPERTYID "propertyid"
#define IOTX_LINKKIT_KEY_EVENTID "eventid"
#define IOTX_LINKKIT_KEY_PAYLOAD "payload"
#define IOTX_LINKKIT_KEY_CONFIG_ID "configId"
#define IOTX_LINKKIT_KEY_CONFIG_SIZE "configSize"
#define IOTX_LINKKIT_KEY_GET_TYPE "getType"
#define IOTX_LINKKIT_KEY_SIGN "sign"
#define IOTX_LINKKIT_KEY_SIGN_METHOD "signMethod"
#define IOTX_LINKKIT_KEY_URL "url"
#define IOTX_LINKKIT_KEY_VERSION "version"
#define IOTX_LINKKIT_KEY_UTC "utc"
#define IOTX_LINKKIT_KEY_RRPCID "rrpcid"
#define IOTX_LINKKIT_KEY_CTX "ctx"
#define IOTX_LINKKIT_KEY_TOPO "topo"
#define IOTX_LINKKIT_KEY_PRODUCT_KEY "productKey"
#define IOTX_LINKKIT_KEY_TIME "time"
#define IOTX_LINKKIT_KEY_DATA "data"
#define IOTX_LINKKIT_SYNC_DEFAULT_TIMEOUT_MS 10000
#define dm_log_err printf
#define dm_log_debug printf
typedef struct {
int msgid;
void *semaphore;
int code;
struct list_head linked_list;
} iotx_linkkit_upstream_sync_callback_node_t;
typedef struct {
void *mutex;
void *upstream_mutex;
int is_opened;
int is_connected;
struct list_head upstream_sync_callback_list;
} iotx_linkkit_ctx_t;
static iotx_linkkit_ctx_t g_iotx_linkkit_ctx = {0};
static iotx_linkkit_ctx_t *_iotx_linkkit_get_ctx(void)
{
return &g_iotx_linkkit_ctx;
}
static void _iotx_linkkit_mutex_lock(void)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (ctx->mutex) {
HAL_MutexLock(ctx->mutex);
}
}
static void _iotx_linkkit_mutex_unlock(void)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
}
static int _impl_copy(_IN_ void *input, _IN_ int input_len, _OU_ void **output, _IN_ int output_len)
{
if (input == NULL || output == NULL || *output != NULL) {
return INVALID_PARAMETER;
}
*output = malloc(output_len);
if (*output == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(*output, 0, output_len);
memcpy(*output, input, input_len);
return SUCCESS_RETURN;
}
#if 1
static void _iotx_linkkit_upstream_mutex_lock(void)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (ctx->upstream_mutex) {
HAL_MutexLock(ctx->upstream_mutex);
}
}
static void _iotx_linkkit_upstream_mutex_unlock(void)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (ctx->upstream_mutex) {
HAL_MutexUnlock(ctx->upstream_mutex);
}
}
static int _iotx_linkkit_upstream_sync_callback_list_insert(int msgid, void *semaphore,
iotx_linkkit_upstream_sync_callback_node_t **node)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
iotx_linkkit_upstream_sync_callback_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->upstream_sync_callback_list, linked_list,
iotx_linkkit_upstream_sync_callback_node_t) {
if (search_node->msgid == msgid) {
printf("Message Already Exist: %d", msgid);
return FAIL_RETURN;
}
}
search_node = malloc(sizeof(iotx_linkkit_upstream_sync_callback_node_t));
if (search_node == NULL) {
dm_log_debug("malloc error");
return FAIL_RETURN;
}
memset(search_node, 0, sizeof(iotx_linkkit_upstream_sync_callback_node_t));
search_node->msgid = msgid;
search_node->semaphore = semaphore;
INIT_LIST_HEAD(&search_node->linked_list);
list_add(&search_node->linked_list, &ctx->upstream_sync_callback_list);
printf("New Message, msgid: %d", msgid);
*node = search_node;
return SUCCESS_RETURN;
}
static int _iotx_linkkit_upstream_sync_callback_list_remove(int msgid)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
iotx_linkkit_upstream_sync_callback_node_t *search_node = NULL;
list_for_each_entry(search_node, &ctx->upstream_sync_callback_list, linked_list,
iotx_linkkit_upstream_sync_callback_node_t) {
if (search_node->msgid == msgid) {
printf("Message Found: %d, Delete It \n", msgid);
HAL_SemaphoreDestroy(search_node->semaphore);
list_del(&search_node->linked_list);
free(search_node);
return SUCCESS_RETURN;
}
}
return FAIL_RETURN;
}
int _iotx_linkkit_upstream_sync_callback_list_search(int msgid,
iotx_linkkit_upstream_sync_callback_node_t **node)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
iotx_linkkit_upstream_sync_callback_node_t *search_node = NULL;
if (node == NULL || *node != NULL) {
dm_log_debug("invalid param");
return FAIL_RETURN;
}
list_for_each_entry(search_node, &ctx->upstream_sync_callback_list, linked_list,
iotx_linkkit_upstream_sync_callback_node_t) {
printf("allan search_node->msgid=%d \n",search_node->msgid);
if (search_node->msgid == msgid) {
dm_log_debug("Sync Message Found: %d", msgid);
*node = search_node;
return SUCCESS_RETURN;
}
}
printf("----------------------allan search_node->msgid=%d \n",search_node->msgid);
return FAIL_RETURN;
}
static void _iotx_linkkit_upstream_sync_callback_list_destroy(void)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
iotx_linkkit_upstream_sync_callback_node_t *search_node = NULL, *next_node = NULL;
list_for_each_entry_safe(search_node, next_node, &ctx->upstream_sync_callback_list, linked_list,
iotx_linkkit_upstream_sync_callback_node_t) {
list_del(&search_node->linked_list);
HAL_SemaphoreDestroy(search_node->semaphore);
free(search_node);
}
}
static void _iotx_linkkit_upstream_callback_remove(int msgid, int code)
{
printf("_iotx_linkkit_upstream_callback_remove : [%d] ,code= %d \n",msgid, code );
int res = 0;
iotx_linkkit_upstream_sync_callback_node_t *sync_node = NULL;
res = _iotx_linkkit_upstream_sync_callback_list_search(msgid, &sync_node);
if (res == SUCCESS_RETURN) {
sync_node->code = (code == IOTX_DM_ERR_CODE_SUCCESS) ? (SUCCESS_RETURN) : (FAIL_RETURN);
printf("Sync Message %d Result: %d", msgid, sync_node->code);
HAL_SemaphorePost(sync_node->semaphore);
}
}
#endif
#ifdef LOG_REPORT_TO_CLOUD
int report_sample = 0;
#endif
#ifdef ALCS_ENABLED
extern void dm_server_free_context(_IN_ void *ctx);
#endif
static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
{
printf("_iotx_linkkit_event_callback ================== [%s]\n",data);
char *out;
int res = 0;
cJSON *json, *topic, *payload;
json=cJSON_Parse(data);
if (!json) {
printf("Error before: [%s]\n","cJSON_Parse");
}
else{
topic = cJSON_GetObjectItem(json, "topic");
payload = cJSON_GetObjectItem(json, "payload");
printf("_iotx_linkkit_event_callback topic: [%s] ,payload= %s \n",topic->valuestring,payload->valuestring );
if (strcmp(payload->valuestring, "addsub")==0){
kk_mid_subdev_add("a1OYuSallan","allanWno8yDdsjCX15iq","");
}else{
printf("rrr topic: [%s] ,payload= %s \n",topic->valuestring,payload->valuestring );
if (strpbrk(payload->valuestring, "register_reply") != NULL){
//====todo======
//get devicececret and save it
dm_msg_response_payload_t response;
res = dm_msg_response_parse((char *)payload->valuestring, strlen(payload->valuestring)+1, &response);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_callback_remove(atoi(response.id.value), response.code.value_int);
_iotx_linkkit_upstream_mutex_unlock();
}else if (strpbrk(topic, "add_reply") != NULL){
//====todo======
//
dm_msg_response_payload_t response;
res = dm_msg_response_parse((char *)payload->valuestring, strlen(payload->valuestring)+1, &response);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_callback_remove(atoi(response.id.value), response.code.value_int);
_iotx_linkkit_upstream_mutex_unlock();
}else if (strpbrk(topic, "login_reply") != NULL){
//====todo======
//
dm_msg_response_payload_t response;
res = dm_msg_response_parse((char *)payload->valuestring, strlen(payload->valuestring)+1, &response);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_callback_remove(atoi(response.id.value), response.code.value_int);
_iotx_linkkit_upstream_mutex_unlock();
}else{
printf("Error 222222222222222 \n");
//kk_tsl_service_property_set(topic->valuestring, payload->valuestring, strlen(payload->valuestring), NULL);
}
}
}
#if 0
int res = 0;
void *callback;
#ifdef LOG_REPORT_TO_CLOUD
lite_cjson_t msg_id;
#endif
lite_cjson_t lite, lite_item_id, lite_item_devid, lite_item_serviceid, lite_item_payload, lite_item_ctx;
lite_cjson_t lite_item_code, lite_item_eventid, lite_item_utc, lite_item_rrpcid, lite_item_topo;
lite_cjson_t lite_item_pk, lite_item_time;
lite_cjson_t lite_item_version, lite_item_configid, lite_item_configsize, lite_item_gettype, lite_item_sign,
lite_item_signmethod, lite_item_url;
dm_log_info("Receive Message Type: %d", type);
if (payload) {
dm_log_info("Receive Message: %s", payload);
res = dm_utils_json_parse(payload, strlen(payload), cJSON_Invalid, &lite);
if (res != SUCCESS_RETURN) {
return;
}
#ifdef LOG_REPORT_TO_CLOUD
dm_utils_json_object_item(&lite, "msgid", 5, cJSON_Invalid, &msg_id);
#endif
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_ID, strlen(IOTX_LINKKIT_KEY_ID), cJSON_Invalid, &lite_item_id);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_DEVID, strlen(IOTX_LINKKIT_KEY_DEVID), cJSON_Invalid,
&lite_item_devid);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_SERVICEID, strlen(IOTX_LINKKIT_KEY_SERVICEID), cJSON_Invalid,
&lite_item_serviceid);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_PAYLOAD, strlen(IOTX_LINKKIT_KEY_PAYLOAD), cJSON_Invalid,
&lite_item_payload);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_CTX, strlen(IOTX_LINKKIT_KEY_CTX), cJSON_Invalid, &lite_item_ctx);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_CODE, strlen(IOTX_LINKKIT_KEY_CODE), cJSON_Invalid, &lite_item_code);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_EVENTID, strlen(IOTX_LINKKIT_KEY_EVENTID), cJSON_Invalid,
&lite_item_eventid);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_UTC, strlen(IOTX_LINKKIT_KEY_UTC), cJSON_Invalid, &lite_item_utc);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_RRPCID, strlen(IOTX_LINKKIT_KEY_RRPCID), cJSON_Invalid,
&lite_item_rrpcid);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_TOPO, strlen(IOTX_LINKKIT_KEY_TOPO), cJSON_Invalid,
&lite_item_topo);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_PRODUCT_KEY, strlen(IOTX_LINKKIT_KEY_PRODUCT_KEY), cJSON_Invalid,
&lite_item_pk);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_TIME, strlen(IOTX_LINKKIT_KEY_TIME), cJSON_Invalid,
&lite_item_time);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_VERSION, strlen(IOTX_LINKKIT_KEY_VERSION), cJSON_Invalid,
&lite_item_version);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_CONFIG_ID, strlen(IOTX_LINKKIT_KEY_CONFIG_ID), cJSON_Invalid,
&lite_item_configid);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_CONFIG_SIZE, strlen(IOTX_LINKKIT_KEY_CONFIG_SIZE), cJSON_Invalid,
&lite_item_configsize);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_GET_TYPE, strlen(IOTX_LINKKIT_KEY_GET_TYPE), cJSON_Invalid,
&lite_item_gettype);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_SIGN, strlen(IOTX_LINKKIT_KEY_SIGN), cJSON_Invalid,
&lite_item_sign);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_SIGN_METHOD, strlen(IOTX_LINKKIT_KEY_SIGN_METHOD), cJSON_Invalid,
&lite_item_signmethod);
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_URL, strlen(IOTX_LINKKIT_KEY_URL), cJSON_Invalid,
&lite_item_url);
}
switch (type) {
case IOTX_DM_EVENT_CLOUD_CONNECTED: {
callback = iotx_event_callback(ITE_CONNECT_SUCC);
if (callback) {
((int (*)(void))callback)();
}
}
break;
case IOTX_DM_EVENT_CLOUD_DISCONNECT: {
//callback = iotx_event_callback(ITE_DISCONNECTED);
if (callback) {
((int (*)(void))callback)();
}
}
break;
case IOTX_DM_EVENT_INITIALIZED: {
if (payload == NULL || lite_item_devid.type != cJSON_Number) {
return;
}
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
//callback = iotx_event_callback(ITE_INITIALIZE_COMPLETED);
if (callback) {
((int (*)(const int))callback)(lite_item_devid.value_int);
}
}
break;
case IOTX_DM_EVENT_MODEL_DOWN_RAW: {
int raw_data_len = 0;
unsigned char *raw_data = NULL;
if (payload == NULL || lite_item_devid.type != cJSON_Number || lite_item_payload.type != cJSON_String) {
return;
}
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current Raw Data: %.*s", lite_item_payload.value_length, lite_item_payload.value);
raw_data_len = lite_item_payload.value_length / 2;
raw_data = IMPL_LINKKIT_MALLOC(raw_data_len);
if (raw_data == NULL) {
dm_log_err("No Enough Memory");
return;
}
LITE_hexstr_convert(lite_item_payload.value, lite_item_payload.value_length, raw_data, raw_data_len);
HEXDUMP_DEBUG(raw_data, raw_data_len);
//callback = iotx_event_callback(ITE_RAWDATA_ARRIVED);
if (callback) {
((int (*)(const int, const unsigned char *, const int))callback)(lite_item_devid.value_int, raw_data, raw_data_len);
}
IMPL_LINKKIT_FREE(raw_data);
}
break;
case IOTX_DM_EVENT_MODEL_UP_RAW_REPLY: {
int raw_data_len = 0;
unsigned char *raw_data = NULL;
if (payload == NULL || lite_item_devid.type != cJSON_Number || lite_item_payload.type != cJSON_String) {
return;
}
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current Raw Data: %.*s", lite_item_payload.value_length, lite_item_payload.value);
raw_data_len = lite_item_payload.value_length / 2;
raw_data = IMPL_LINKKIT_MALLOC(raw_data_len);
if (raw_data == NULL) {
dm_log_err("No Enough Memory");
return;
}
memset(raw_data, 0, raw_data_len);
LITE_hexstr_convert(lite_item_payload.value, lite_item_payload.value_length, raw_data, raw_data_len);
HEXDUMP_DEBUG(raw_data, raw_data_len);
//callback = iotx_event_callback(ITE_RAWDATA_ARRIVED);
if (callback) {
((int (*)(const int, const unsigned char *, const int))callback)(lite_item_devid.value_int, raw_data, raw_data_len);
}
IMPL_LINKKIT_FREE(raw_data);
}
break;
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
case IOTX_DM_EVENT_THING_SERVICE_REQUEST: {
int response_len = 0;
char *request = NULL, *response = NULL;
uintptr_t property_get_ctx_num = 0;
void *property_get_ctx = NULL;
if (payload == NULL || lite_item_id.type != cJSON_String || lite_item_devid.type != cJSON_Number ||
lite_item_serviceid.type != cJSON_String || lite_item_payload.type != cJSON_Object) {
return;
}
dm_log_debug("Current Id: %.*s", lite_item_id.value_length, lite_item_id.value);
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current ServiceID: %.*s", lite_item_serviceid.value_length, lite_item_serviceid.value);
dm_log_debug("Current Payload: %.*s", lite_item_payload.value_length, lite_item_payload.value);
dm_log_debug("Current Ctx: %.*s", lite_item_ctx.value_length, lite_item_ctx.value);
LITE_hexstr_convert(lite_item_ctx.value, lite_item_ctx.value_length, (unsigned char *)&property_get_ctx_num,
sizeof(uintptr_t));
property_get_ctx = (void *)property_get_ctx_num;
request = IMPL_LINKKIT_MALLOC(lite_item_payload.value_length + 1);
if (request == NULL) {
dm_log_err("Not Enough Memory");
return;
}
memset(request, 0, lite_item_payload.value_length + 1);
memcpy(request, lite_item_payload.value, lite_item_payload.value_length);
//callback = iotx_event_callback(ITE_SERVICE_REQUEST);
if (callback) {
res = ((int (*)(const int, const char *, const int, const char *, const int, char **,
int *))callback)(lite_item_devid.value_int, lite_item_serviceid.value,
lite_item_serviceid.value_length, request, lite_item_payload.value_length, &response, &response_len);
if (response != NULL && response_len > 0) {
/* service response exist */
iotx_dm_error_code_t code = (res == 0) ? (IOTX_DM_ERR_CODE_SUCCESS) : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
iotx_dm_send_service_response(lite_item_devid.value_int, lite_item_id.value, lite_item_id.value_length, code,
lite_item_serviceid.value,
lite_item_serviceid.value_length,
response, response_len, property_get_ctx);
HAL_Free(response);
}
}
#ifdef ALCS_ENABLED
if (property_get_ctx) {
dm_server_free_context(property_get_ctx);
}
#endif
IMPL_LINKKIT_FREE(request);
}
break;
case IOTX_DM_EVENT_PROPERTY_SET: {
char *property_payload = NULL;
if (payload == NULL || lite_item_devid.type != cJSON_Number || lite_item_payload.type != cJSON_Object) {
return;
}
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current Payload: %.*s", lite_item_payload.value_length, lite_item_payload.value);
property_payload = IMPL_LINKKIT_MALLOC(lite_item_payload.value_length + 1);
if (property_payload == NULL) {
dm_log_err("No Enough Memory");
return;
}
memset(property_payload, 0, lite_item_payload.value_length + 1);
memcpy(property_payload, lite_item_payload.value, lite_item_payload.value_length);
#ifdef LOG_REPORT_TO_CLOUD
if (SUCCESS_RETURN == check_target_msg(msg_id.value, msg_id.value_length)) {
report_sample = 1;
send_permance_info(msg_id.value, msg_id.value_length, "3", 1);
}
#endif
//callback = iotx_event_callback(ITE_PROPERTY_SET);
if (callback) {
((int (*)(const int, const char *, const int))callback)(lite_item_devid.value_int, property_payload,
lite_item_payload.value_length);
}
#ifdef LOG_REPORT_TO_CLOUD
if (1 == report_sample) {
send_permance_info(NULL, 0, "5", 2);
report_sample = 0;
}
#endif
IMPL_LINKKIT_FREE(property_payload);
}
break;
#ifdef DEVICE_MODEL_SHADOW
case IOTX_DM_EVENT_PROPERTY_DESIRED_GET_REPLY: {
char *property_data = NULL;
lite_cjson_t lite_item_data;
memset(&lite_item_data, 0, sizeof(lite_cjson_t));
dm_utils_json_object_item(&lite, IOTX_LINKKIT_KEY_DATA, strlen(IOTX_LINKKIT_KEY_DATA), cJSON_Invalid,
&lite_item_data);
if (payload == NULL || lite_item_data.type != cJSON_Object) {
return;
}
dm_log_debug("Current Data: %.*s", lite_item_data.value_length, lite_item_data.value);
property_data = IMPL_LINKKIT_MALLOC(lite_item_data.value_length + 1);
if (property_data == NULL) {
dm_log_err("No Enough Memory");
return;
}
memset(property_data, 0, lite_item_data.value_length + 1);
memcpy(property_data, lite_item_data.value, lite_item_data.value_length);
//callback = iotx_event_callback(ITE_PROPERTY_DESIRED_GET_REPLY);
if (callback) {
((int (*)(const char *, const int))callback)(property_data,
lite_item_data.value_length);
}
IMPL_LINKKIT_FREE(property_data);
}
break;
#endif
case IOTX_DM_EVENT_PROPERTY_GET: {
int response_len = 0;
char *request = NULL, *response = NULL;
uintptr_t property_get_ctx_num = 0;
void *property_get_ctx = NULL;
if (payload == NULL || lite_item_id.type != cJSON_String || lite_item_devid.type != cJSON_Number ||
lite_item_payload.type != cJSON_Array) {
return;
}
dm_log_debug("Current Id: %.*s", lite_item_id.value_length, lite_item_id.value);
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current Payload: %.*s", lite_item_payload.value_length, lite_item_payload.value);
dm_log_debug("Current Ctx: %.*s", lite_item_ctx.value_length, lite_item_ctx.value);
LITE_hexstr_convert(lite_item_ctx.value, lite_item_ctx.value_length, (unsigned char *)&property_get_ctx_num,
sizeof(uintptr_t));
property_get_ctx = (void *)property_get_ctx_num;
dm_log_debug("property_get_ctx_num: %0x016llX", (unsigned int)property_get_ctx_num);
dm_log_debug("property_get_ctx: %p", property_get_ctx);
request = IMPL_LINKKIT_MALLOC(lite_item_payload.value_length + 1);
if (request == NULL) {
dm_log_err("No Enough Memory");
return;
}
memset(request, 0, lite_item_payload.value_length + 1);
memcpy(request, lite_item_payload.value, lite_item_payload.value_length);
//callback = iotx_event_callback(ITE_PROPERTY_GET);
if (callback) {
res = ((int (*)(const int, const char *, const int, char **, int *))callback)(lite_item_devid.value_int, request,
lite_item_payload.value_length, &response, &response_len);
if (response != NULL && response_len > 0) {
/* property get response exist */
iotx_dm_error_code_t code = (res == 0) ? (IOTX_DM_ERR_CODE_SUCCESS) : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
iotx_dm_send_property_get_response(lite_item_devid.value_int, lite_item_id.value, lite_item_id.value_length, code,
response, response_len, property_get_ctx);
HAL_Free(response);
}
}
IMPL_LINKKIT_FREE(request);
}
break;
case IOTX_DM_EVENT_EVENT_PROPERTY_POST_REPLY:
case IOTX_DM_EVENT_DEVICEINFO_UPDATE_REPLY:
#ifdef DEVICE_MODEL_SHADOW
case IOTX_DM_EVENT_PROPERTY_DESIRED_DELETE_REPLY:
#endif
case IOTX_DM_EVENT_DEVICEINFO_DELETE_REPLY: {
char *user_payload = NULL;
int user_payload_length = 0;
if (payload == NULL || lite_item_id.type != cJSON_Number || lite_item_code.type != cJSON_Number
|| lite_item_devid.type != cJSON_Number) {
return;
}
dm_log_debug("Current Id: %d", lite_item_id.value_int);
dm_log_debug("Current Code: %d", lite_item_code.value_int);
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
if (lite_item_payload.type == cJSON_Object && lite_item_payload.value_length > 0) {
user_payload = IMPL_LINKKIT_MALLOC(lite_item_payload.value_length + 1);
if (user_payload == NULL) {
dm_log_err("No Enough Memory");
return;
}
memset(user_payload, 0, lite_item_payload.value_length + 1);
memcpy(user_payload, lite_item_payload.value, lite_item_payload.value_length);
user_payload_length = lite_item_payload.value_length;
}
//callback = iotx_event_callback(ITE_REPORT_REPLY);
if (callback) {
((int (*)(const int, const int, const int, const char *, const int))callback)(lite_item_devid.value_int,
lite_item_id.value_int, lite_item_code.value_int, user_payload,
user_payload_length);
}
if (user_payload) {
IMPL_LINKKIT_FREE(user_payload);
}
}
break;
case IOTX_DM_EVENT_EVENT_SPECIFIC_POST_REPLY: {
char *user_eventid = NULL;
char *user_payload = NULL;
if (payload == NULL || lite_item_id.type != cJSON_Number || lite_item_code.type != cJSON_Number ||
lite_item_devid.type != cJSON_Number || lite_item_eventid.type != cJSON_String
|| lite_item_payload.type != cJSON_String) {
return;
}
dm_log_debug("Current Id: %d", lite_item_id.value_int);
dm_log_debug("Current Code: %d", lite_item_code.value_int);
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current EventID: %.*s", lite_item_eventid.value_length, lite_item_eventid.value);
dm_log_debug("Current Message: %.*s", lite_item_payload.value_length, lite_item_payload.value);
user_eventid = IMPL_LINKKIT_MALLOC(lite_item_eventid.value_length + 1);
if (user_eventid == NULL) {
dm_log_err("Not Enough Memory");
return;
}
memset(user_eventid, 0, lite_item_eventid.value_length + 1);
memcpy(user_eventid, lite_item_eventid.value, lite_item_eventid.value_length);
user_payload = IMPL_LINKKIT_MALLOC(lite_item_payload.value_length + 1);
if (user_payload == NULL) {
dm_log_err("Not Enough Memory");
IMPL_LINKKIT_FREE(user_eventid);
return;
}
memset(user_payload, 0, lite_item_payload.value_length + 1);
memcpy(user_payload, lite_item_payload.value, lite_item_payload.value_length);
//callback = iotx_event_callback(ITE_TRIGGER_EVENT_REPLY);
if (callback) {
((int (*)(const int, const int, const int, const char *, const int, const char *,
const int))callback)(lite_item_devid.value_int,
lite_item_id.value_int, lite_item_code.value_int,
user_eventid, lite_item_eventid.value_length, user_payload, lite_item_payload.value_length);
}
IMPL_LINKKIT_FREE(user_eventid);
IMPL_LINKKIT_FREE(user_payload);
}
break;
case IOTX_DM_EVENT_NTP_RESPONSE: {
char *utc_payload = NULL;
if (payload == NULL || lite_item_utc.type != cJSON_String) {
return;
}
dm_log_debug("Current UTC: %.*s", lite_item_utc.value_length, lite_item_utc.value);
utc_payload = IMPL_LINKKIT_MALLOC(lite_item_utc.value_length + 1);
if (utc_payload == NULL) {
dm_log_err("Not Enough Memory");
return;
}
memset(utc_payload, 0, lite_item_utc.value_length + 1);
memcpy(utc_payload, lite_item_utc.value, lite_item_utc.value_length);
//callback = iotx_event_callback(ITE_TIMESTAMP_REPLY);
if (callback) {
((int (*)(const char *))callback)(utc_payload);
}
IMPL_LINKKIT_FREE(utc_payload);
}
break;
case IOTX_DM_EVENT_RRPC_REQUEST: {
int rrpc_response_len = 0;
char *rrpc_request = NULL, *rrpc_response = NULL;
if (payload == NULL || lite_item_id.type != cJSON_String || lite_item_devid.type != cJSON_Number ||
lite_item_serviceid.type != cJSON_String || lite_item_rrpcid.type != cJSON_String
|| lite_item_payload.type != cJSON_Object) {
return;
}
dm_log_debug("Current Id: %.*s", lite_item_id.value_length, lite_item_id.value);
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current ServiceID: %.*s", lite_item_serviceid.value_length, lite_item_serviceid.value);
dm_log_debug("Current RRPC ID: %.*s", lite_item_rrpcid.value_length, lite_item_rrpcid.value);
dm_log_debug("Current Payload: %.*s", lite_item_payload.value_length, lite_item_payload.value);
rrpc_request = IMPL_LINKKIT_MALLOC(lite_item_payload.value_length + 1);
if (rrpc_request == NULL) {
dm_log_err("Not Enough Memory");
return;
}
memset(rrpc_request, 0, lite_item_payload.value_length + 1);
memcpy(rrpc_request, lite_item_payload.value, lite_item_payload.value_length);
//callback = iotx_event_callback(ITE_SERVICE_REQUEST);
if (callback) {
res = ((int (*)(const int, const char *, const int, const char *, const int, char **,
int *))callback)(lite_item_devid.value_int, lite_item_serviceid.value,
lite_item_serviceid.value_length,
rrpc_request, lite_item_payload.value_length, &rrpc_response, &rrpc_response_len);
if (rrpc_response != NULL && rrpc_response_len > 0) {
iotx_dm_error_code_t code = (res == 0) ? (IOTX_DM_ERR_CODE_SUCCESS) : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
iotx_dm_send_rrpc_response(lite_item_devid.value_int, lite_item_id.value, lite_item_id.value_length, code,
lite_item_rrpcid.value,
lite_item_rrpcid.value_length,
rrpc_response, rrpc_response_len);
HAL_Free(rrpc_response);
}
}
IMPL_LINKKIT_FREE(rrpc_request);
}
break;
#endif
case IOTX_DM_EVENT_FOTA_NEW_FIRMWARE: {
char *version = NULL;
if (payload == NULL || lite_item_version.type != cJSON_String) {
return;
}
dm_log_debug("Current Firmware Version: %.*s", lite_item_version.value_length, lite_item_version.value);
version = IMPL_LINKKIT_MALLOC(lite_item_version.value_length + 1);
if (version == NULL) {
return;
}
memset(version, 0, lite_item_version.value_length + 1);
memcpy(version, lite_item_version.value, lite_item_version.value_length);
//callback = iotx_event_callback(ITE_FOTA);
if (callback) {
((int (*)(const int, const char *))callback)(0, version);
}
if (version) {
IMPL_LINKKIT_FREE(version);
}
}
break;
case IOTX_DM_EVENT_COTA_NEW_CONFIG: {
char *config_id = NULL, *get_type = NULL, *sign = NULL, *sign_method = NULL, *url = NULL;
if (payload == NULL || lite_item_configid.type != cJSON_String || lite_item_configsize.type != cJSON_Number ||
lite_item_gettype.type != cJSON_String || lite_item_sign.type != cJSON_String
|| lite_item_signmethod.type != cJSON_String ||
lite_item_url.type != cJSON_String) {
return;
}
dm_log_debug("Current Config ID: %.*s", lite_item_configid.value_length, lite_item_configid.value);
dm_log_debug("Current Config Size: %d", lite_item_configsize.value_int);
dm_log_debug("Current Get Type: %.*s", lite_item_gettype.value_length, lite_item_gettype.value);
dm_log_debug("Current Sign: %.*s", lite_item_sign.value_length, lite_item_sign.value);
dm_log_debug("Current Sign Method: %.*s", lite_item_signmethod.value_length, lite_item_signmethod.value);
dm_log_debug("Current URL: %.*s", lite_item_url.value_length, lite_item_url.value);
_impl_copy(lite_item_configid.value, lite_item_configid.value_length, (void **)&config_id,
lite_item_configid.value_length + 1);
_impl_copy(lite_item_gettype.value, lite_item_gettype.value_length, (void **)&get_type,
lite_item_gettype.value_length + 1);
_impl_copy(lite_item_sign.value, lite_item_sign.value_length, (void **)&sign, lite_item_sign.value_length + 1);
_impl_copy(lite_item_signmethod.value, lite_item_signmethod.value_length, (void **)&sign_method,
lite_item_signmethod.value_length + 1);
_impl_copy(lite_item_url.value, lite_item_url.value_length, (void **)&url, lite_item_url.value_length + 1);
if (config_id == NULL || get_type == NULL || sign == NULL || sign_method == NULL || url == NULL) {
if (config_id) {
IMPL_LINKKIT_FREE(config_id);
}
if (get_type) {
IMPL_LINKKIT_FREE(get_type);
}
if (sign) {
IMPL_LINKKIT_FREE(sign);
}
if (sign_method) {
IMPL_LINKKIT_FREE(sign_method);
}
if (url) {
IMPL_LINKKIT_FREE(url);
}
return;
}
//callback = iotx_event_callback(ITE_COTA);
if (callback) {
((int (*)(const int, const char *, int, const char *, const char *, const char *, const char *))callback)(0, config_id,
lite_item_configsize.value_int, get_type, sign, sign_method, url);
}
if (config_id) {
IMPL_LINKKIT_FREE(config_id);
}
if (get_type) {
IMPL_LINKKIT_FREE(get_type);
}
if (sign) {
IMPL_LINKKIT_FREE(sign);
}
if (sign_method) {
IMPL_LINKKIT_FREE(sign_method);
}
if (url) {
IMPL_LINKKIT_FREE(url);
}
}
break;
#ifdef 1//DEVICE_MODEL_GATEWAY
case IOTX_DM_EVENT_TOPO_GET_REPLY: {
char *topo_list = NULL;
if (payload == NULL || lite_item_id.type != cJSON_Number || lite_item_devid.type != cJSON_Number ||
lite_item_code.type != cJSON_Number || lite_item_topo.type != cJSON_Array) {
return;
}
dm_log_debug("Current Id: %d", lite_item_id.value_int);
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
dm_log_debug("Current Code: %d", lite_item_code.value_int);
dm_log_debug("Current Topo List: %.*s", lite_item_topo.value_length, lite_item_topo.value);
topo_list = IMPL_LINKKIT_MALLOC(lite_item_topo.value_length + 1);
if (topo_list == NULL) {
dm_log_err("Not Enough Memory");
return;
}
memset(topo_list, 0, lite_item_topo.value_length + 1);
memcpy(topo_list, lite_item_topo.value, lite_item_topo.value_length);
//callback = iotx_event_callback(ITE_TOPOLIST_REPLY);
if (callback) {
((int (*)(const int, const int, const int, const char *, const int))callback)(lite_item_devid.value_int,
lite_item_id.value_int,
lite_item_code.value_int, topo_list, lite_item_topo.value_length);
}
IMPL_LINKKIT_FREE(topo_list);
}
break;
case IOTX_DM_EVENT_TOPO_DELETE_REPLY:
case IOTX_DM_EVENT_TOPO_ADD_REPLY:
case IOTX_DM_EVENT_SUBDEV_REGISTER_REPLY:
case IOTX_DM_EVENT_COMBINE_LOGIN_REPLY:
case IOTX_DM_EVENT_COMBINE_LOGOUT_REPLY: {
if (payload == NULL || lite_item_id.type != cJSON_Number || lite_item_devid.type != cJSON_Number ||
lite_item_code.type != cJSON_Number) {
return;
}
dm_log_debug("Current Id: %d", lite_item_id.value_int);
dm_log_debug("Current Code: %d", lite_item_code.value_int);
dm_log_debug("Current Devid: %d", lite_item_devid.value_int);
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_callback_remove(lite_item_id.value_int, lite_item_code.value_int);
_iotx_linkkit_upstream_mutex_unlock();
}
break;
case IOTX_DM_EVENT_GATEWAY_PERMIT: {
char *product_key = "";
if (payload == NULL || lite_item_time.type != cJSON_Number) {
return;
}
dm_log_debug("Current Time: %d", lite_item_time.value_int);
if (lite_item_pk.type == cJSON_String) {
dm_log_debug("Current Product Key: %.*s", lite_item_pk.value_length, lite_item_pk.value);
product_key = IMPL_LINKKIT_MALLOC(lite_item_pk.value_length + 1);
if (product_key == NULL) {
dm_log_err("Not Enough Memory");
return;
}
memset(product_key, 0, lite_item_pk.value_length + 1);
memcpy(product_key, lite_item_pk.value, lite_item_pk.value_length);
}
//callback = iotx_event_callback(ITE_PERMIT_JOIN);
if (callback) {
((int (*)(const char *, int))callback)((const char *)product_key, (const int)lite_item_time.value_int);
}
if (lite_item_pk.type == cJSON_String) {
IMPL_LINKKIT_FREE(product_key);
}
}
break;
#endif
default: {
}
break;
}
#endif
}
int kk_init_dmproc(){
int res = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
/* DM QUEUE Module Init */
res = dm_queue_init(CONFIG_DISPATCH_QUEUE_MAXLEN);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
res = iotx_dm_connect(_iotx_linkkit_event_callback);
if (res != SUCCESS_RETURN) {
dm_log_err("DM Start Failed");
//ctx->is_connected = 0;
return FAIL_RETURN;
}
if (ctx->is_opened) {
return FAIL_RETURN;
}
ctx->is_opened = 1;
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
dm_log_err("Not Enough Memory");
ctx->is_opened = 0;
return FAIL_RETURN;
}
ctx->upstream_mutex = HAL_MutexCreate();
if (ctx->upstream_mutex == NULL) {
HAL_MutexDestroy(ctx->mutex);
dm_log_err("Not Enough Memory");
ctx->is_opened = 0;
return FAIL_RETURN;
}
INIT_LIST_HEAD(&ctx->upstream_sync_callback_list);
return SUCCESS_RETURN;
}
#if 0
static int _iotx_linkkit_master_open(iotx_linkkit_dev_meta_info_t *meta_info)
{
int res = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (ctx->is_opened) {
return FAIL_RETURN;
}
ctx->is_opened = 1;
HAL_SetProductKey(meta_info->product_key);
HAL_SetProductSecret(meta_info->product_secret);
HAL_SetDeviceName(meta_info->device_name);
HAL_SetDeviceSecret(meta_info->device_secret);
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
dm_log_err("Not Enough Memory");
ctx->is_opened = 0;
return FAIL_RETURN;
}
ctx->upstream_mutex = HAL_MutexCreate();
if (ctx->upstream_mutex == NULL) {
HAL_MutexDestroy(ctx->mutex);
dm_log_err("Not Enough Memory");
ctx->is_opened = 0;
return FAIL_RETURN;
}
res = iotx_dm_open();
if (res != SUCCESS_RETURN) {
HAL_MutexDestroy(ctx->upstream_mutex);
HAL_MutexDestroy(ctx->mutex);
ctx->is_opened = 0;
return FAIL_RETURN;
}
INIT_LIST_HEAD(&ctx->upstream_sync_callback_list);
return SUCCESS_RETURN;
}
static int _iotx_linkkit_slave_open(iotx_linkkit_dev_meta_info_t *meta_info)
{
int res = 0, devid;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (!ctx->is_opened) {
return FAIL_RETURN;
}
res = iotx_dm_subdev_create(meta_info->product_key, meta_info->device_name, meta_info->device_secret, &devid);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
return devid;
}
static int _iotx_linkkit_master_connect(void)
{
int res = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
iotx_dm_init_params_t dm_init_params;
iotx_dm_event_types_t type;
if (ctx->is_connected) {
return FAIL_RETURN;
}
ctx->is_connected = 1;
memset(&dm_init_params, 0, sizeof(iotx_dm_init_params_t));
dm_init_params.event_callback = _iotx_linkkit_event_callback;
res = iotx_dm_connect(&dm_init_params);
if (res != SUCCESS_RETURN) {
dm_log_err("DM Start Failed");
ctx->is_connected = 0;
return FAIL_RETURN;
}
res = iotx_dm_subscribe(IOTX_DM_LOCAL_NODE_DEVID);
if (res != SUCCESS_RETURN) {
dm_log_err("DM Subscribe Failed");
ctx->is_connected = 0;
return FAIL_RETURN;
}
type = IOTX_DM_EVENT_INITIALIZED;
_iotx_linkkit_event_callback(type, "{\"devid\":0}");
return SUCCESS_RETURN;
}
#endif
int _iotx_linkkit_slave_connect(int devid)
{
int res = 0, msgid = 0, code = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
iotx_linkkit_upstream_sync_callback_node_t *node = NULL;
void *semaphore = NULL;
/*if (ctx->is_connected == 0) {
dm_log_err("master isn't start");
return FAIL_RETURN;
}*/
if (devid <= 0) {
dm_log_err("devid invalid");
return FAIL_RETURN;
}
/* Subdev Register */
res = kk_dm_subdev_register(devid);
if (res < SUCCESS_RETURN) {
return FAIL_RETURN;
}
if (res > SUCCESS_RETURN) {
semaphore = HAL_SemaphoreCreate();
if (semaphore == NULL) {
return FAIL_RETURN;
}
msgid = res;
_iotx_linkkit_upstream_mutex_lock();
res = _iotx_linkkit_upstream_sync_callback_list_insert(msgid, semaphore, &node);
if (res != SUCCESS_RETURN) {
HAL_SemaphoreDestroy(semaphore);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
res = HAL_SemaphoreWait(semaphore, IOTX_LINKKIT_SYNC_DEFAULT_TIMEOUT_MS);
if (res < SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
code = node->code;
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
if (code != SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
}
/* Subdev Add Topo */
res = kk_dm_subdev_topo_add(devid);
if (res < SUCCESS_RETURN) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
semaphore = HAL_SemaphoreCreate();
if (semaphore == NULL) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
msgid = res;
_iotx_linkkit_upstream_mutex_lock();
res = _iotx_linkkit_upstream_sync_callback_list_insert(msgid, semaphore, &node);
if (res != SUCCESS_RETURN) {
HAL_SemaphoreDestroy(semaphore);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
res = HAL_SemaphoreWait(semaphore, IOTX_LINKKIT_SYNC_DEFAULT_TIMEOUT_MS);
if (res < SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
code = node->code;
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
if (code != SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
return SUCCESS_RETURN;
}
static int _iotx_linkkit_subdev_delete_topo(int devid)
{
int res = 0, msgid = 0, code = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
iotx_linkkit_upstream_sync_callback_node_t *node = NULL;
void *semaphore = NULL;
if (ctx->is_connected == 0) {
dm_log_err("master isn't start");
return FAIL_RETURN;
}
if (devid <= 0) {
dm_log_err("devid invalid");
return FAIL_RETURN;
}
/* Subdev Delete Topo */
res = iotx_dm_subdev_topo_del(devid);
if (res < SUCCESS_RETURN) {
return FAIL_RETURN;
}
msgid = res;
semaphore = HAL_SemaphoreCreate();
if (semaphore == NULL) {
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
res = _iotx_linkkit_upstream_sync_callback_list_insert(msgid, semaphore, &node);
if (res != SUCCESS_RETURN) {
HAL_SemaphoreDestroy(semaphore);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
res = HAL_SemaphoreWait(semaphore, IOTX_LINKKIT_SYNC_DEFAULT_TIMEOUT_MS);
if (res < SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
code = node->code;
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
if (code != SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
return SUCCESS_RETURN;
}
static int _iotx_linkkit_master_close(void)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
_iotx_linkkit_mutex_lock();
if (ctx->is_opened == 0) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
ctx->is_opened = 0;
//iotx_dm_close();
#if 1//DEVICE_MODEL_GATEWAY
_iotx_linkkit_upstream_sync_callback_list_destroy();
HAL_MutexDestroy(ctx->upstream_mutex);
#endif
_iotx_linkkit_mutex_unlock();
HAL_MutexDestroy(ctx->mutex);
memset(ctx, 0, sizeof(iotx_linkkit_ctx_t));
return SUCCESS_RETURN;
}
#if 1//DEVICE_MODEL_GATEWAY
static int _iotx_linkkit_slave_close(int devid)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
_iotx_linkkit_mutex_lock();
if (ctx->is_opened == 0) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
/* Release Subdev Resources */
//iotx_dm_subdev_destroy(devid);
_iotx_linkkit_mutex_unlock();
return SUCCESS_RETURN;
}
#endif
/*int IOT_Linkkit_Open(iotx_linkkit_dev_type_t dev_type, iotx_linkkit_dev_meta_info_t *meta_info)
{
int res = 0;
if (dev_type < 0 || dev_type >= IOTX_LINKKIT_DEV_TYPE_MAX || meta_info == NULL) {
dm_log_err("Invalid Parameter");
return FAIL_RETURN;
}
switch (dev_type) {
case IOTX_LINKKIT_DEV_TYPE_MASTER: {
res = _iotx_linkkit_master_open(meta_info);
if (res == SUCCESS_RETURN) {
res = IOTX_DM_LOCAL_NODE_DEVID;
}
}
break;
case IOTX_LINKKIT_DEV_TYPE_SLAVE: {
#if 1//DEVICE_MODEL_GATEWAY
res = _iotx_linkkit_slave_open(meta_info);
#else
res = FAIL_RETURN;
#endif
}
break;
default: {
dm_log_err("Unknown Device Type");
res = FAIL_RETURN;
}
break;
}
return res;
}*/
/*int IOT_Linkkit_Connect(int devid)
{
int res = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (devid < 0) {
dm_log_err("Invalid Parameter");
return FAIL_RETURN;
}
if (ctx->is_opened == 0) {
return FAIL_RETURN;
}
_iotx_linkkit_mutex_lock();
if (devid == IOTX_DM_LOCAL_NODE_DEVID) {
res = _iotx_linkkit_master_connect();
} else {
res = _iotx_linkkit_slave_connect(devid);
}
_iotx_linkkit_mutex_unlock();
return res;
}*/
void IOT_Linkkit_Yield(int timeout_ms)
{
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (timeout_ms <= 0) {
dm_log_err("Invalid Parameter");
return;
}
//if (ctx->is_opened == 0 || ctx->is_connected == 0) {
// return;
//}
//iotx_dm_yield(timeout_ms);
iotx_dm_dispatch();
#if 1//DEVICE_MODEL_GATEWAY
HAL_SleepMs(timeout_ms);
#endif
}
int IOT_Linkkit_Close(int devid)
{
int res = 0;
if (devid < 0) {
dm_log_err("Invalid Parameter");
return FAIL_RETURN;
}
if (devid == IOTX_DM_LOCAL_NODE_DEVID) {
res = _iotx_linkkit_master_close();
#ifdef DEV_BIND_ENABLED
awss_bind_deinit();
#endif
} else {
#if 1//DEVICE_MODEL_GATEWAY
res = _iotx_linkkit_slave_close(devid);
#else
res = FAIL_RETURN;
#endif
}
return res;
}
#if 1//DEVICE_MODEL_GATEWAY
static int _iotx_linkkit_subdev_login(int devid)
{
int res = 0, msgid = 0, code = 0;
iotx_linkkit_upstream_sync_callback_node_t *node = NULL;
void *semaphore = NULL;
void *callback = NULL;
res = iotx_dm_subdev_login(devid);
if (res < SUCCESS_RETURN) {
return FAIL_RETURN;
}
msgid = res;
semaphore = HAL_SemaphoreCreate();
if (semaphore == NULL) {
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
res = _iotx_linkkit_upstream_sync_callback_list_insert(msgid, semaphore, &node);
if (res != SUCCESS_RETURN) {
HAL_SemaphoreDestroy(semaphore);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
res = HAL_SemaphoreWait(semaphore, IOTX_LINKKIT_SYNC_DEFAULT_TIMEOUT_MS);
if (res < SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
code = node->code;
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
if (code != SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
//send app to subscribe mqtt
const char subscribe_cmd[] =
"{\"cmd\":\"subscribe\",\"productKey\":\"%s\",\"deviceName\":\"%s\",\"clientId\":\"%s\",\"timestamp\":\"%s\",\"signMethod\":\"%s\",\"sign\":\"%s\",\"cleanSession\":\"%s\"}";
kk_ipc_send(IPC_MID2APP, subscribe_cmd, strlen(subscribe_cmd) + 1);
printf("\n [%d][%s] [%d] allan kk_ipc_send \n", __LINE__, __FUNCTION__, HAL_UptimeMs());
/*res = iotx_dm_subscribe(devid);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
iotx_dm_send_aos_active(devid);
callback = iotx_event_callback(ITE_INITIALIZE_COMPLETED);
if (callback) {
((int (*)(const int))callback)(devid);
}*/
return res;
}
static int _iotx_linkkit_subdev_logout(int devid)
{
int res = 0, msgid = 0, code = 0;
iotx_linkkit_upstream_sync_callback_node_t *node = NULL;
void *semaphore = NULL;
res = iotx_dm_subdev_logout(devid);
if (res < SUCCESS_RETURN) {
return FAIL_RETURN;
}
msgid = res;
semaphore = HAL_SemaphoreCreate();
if (semaphore == NULL) {
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
res = _iotx_linkkit_upstream_sync_callback_list_insert(msgid, semaphore, &node);
if (res != SUCCESS_RETURN) {
HAL_SemaphoreDestroy(semaphore);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
res = HAL_SemaphoreWait(semaphore, IOTX_LINKKIT_SYNC_DEFAULT_TIMEOUT_MS);
if (res < SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_lock();
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_lock();
code = node->code;
_iotx_linkkit_upstream_sync_callback_list_remove(msgid);
if (code != SUCCESS_RETURN) {
_iotx_linkkit_upstream_mutex_unlock();
return FAIL_RETURN;
}
_iotx_linkkit_upstream_mutex_unlock();
return res;
}
#endif
/*int IOT_Linkkit_Report(int devid, iotx_linkkit_msg_type_t msg_type, unsigned char *payload, int payload_len)
{
int res = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (devid < 0 || msg_type < 0 || msg_type >= IOTX_LINKKIT_MSG_MAX) {
dm_log_err("Invalid Parameter");
return FAIL_RETURN;
}
if (ctx->is_opened == 0 || ctx->is_connected == 0) {
return FAIL_RETURN;
}
_iotx_linkkit_mutex_lock();
switch (msg_type) {
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
case ITM_MSG_POST_PROPERTY: {
if (payload == NULL || payload_len <= 0) {
dm_log_err("Invalid Parameter");
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
res = iotx_dm_post_property(devid, (char *)payload, payload_len);
#ifdef LOG_REPORT_TO_CLOUD
if (1 == report_sample) {
send_permance_info(NULL, 0, "4", 1);
}
#endif
}
break;
#ifdef DEVICE_MODEL_SHADOW
case ITM_MSG_PROPERTY_DESIRED_GET: {
if (payload == NULL || payload_len <= 0) {
dm_log_err("Invalid Parameter");
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
res = iotx_dm_property_desired_get(devid, (char *)payload, payload_len);
}
break;
case ITM_MSG_PROPERTY_DESIRED_DELETE: {
if (payload == NULL || payload_len <= 0) {
dm_log_err("Invalid Parameter");
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
res = iotx_dm_property_desired_delete(devid, (char *)payload, payload_len);
}
break;
#endif
case ITM_MSG_DEVICEINFO_UPDATE: {
if (payload == NULL || payload_len <= 0) {
dm_log_err("Invalid Parameter");
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
res = iotx_dm_deviceinfo_update(devid, (char *)payload, payload_len);
}
break;
case ITM_MSG_DEVICEINFO_DELETE: {
if (payload == NULL || payload_len <= 0) {
dm_log_err("Invalid Parameter");
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
res = iotx_dm_deviceinfo_delete(devid, (char *)payload, payload_len);
}
break;
#endif
case ITM_MSG_POST_RAW_DATA: {
if (payload == NULL || payload_len <= 0) {
dm_log_err("Invalid Parameter");
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
res = iotx_dm_post_rawdata(devid, (char *)payload, payload_len);
}
break;
case ITM_MSG_LOGIN: {
#if 1//DEVICE_MODEL_GATEWAY
res = _iotx_linkkit_subdev_login(devid);
if (res != SUCCESS_RETURN) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
#else
res = FAIL_RETURN;
#endif
}
break;
case ITM_MSG_LOGOUT: {
#if 1//DEVICE_MODEL_GATEWAY
res = _iotx_linkkit_subdev_logout(devid);
if (res != SUCCESS_RETURN) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
#else
res = FAIL_RETURN;
#endif
}
break;
case ITM_MSG_DELETE_TOPO: {
#if 1//DEVICE_MODEL_GATEWAY
res = _iotx_linkkit_subdev_delete_topo(devid);
if (res != SUCCESS_RETURN) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
#else
res = FAIL_RETURN;
#endif
}
break;
#if 1//DEVICE_MODEL_GATEWAY
#ifdef DEVICE_MODEL_SUBDEV_OTA
case ITM_MSG_REPORT_SUBDEV_FIRMWARE_VERSION: {
res = iotx_dm_send_firmware_version(devid, (const char *)payload);
}
break;
#endif
#endif
default: {
dm_log_err("Unknown Message Type");
res = FAIL_RETURN;
}
break;
}
_iotx_linkkit_mutex_unlock();
return res;
}
int IOT_Linkkit_Query(int devid, iotx_linkkit_msg_type_t msg_type, unsigned char *payload, int payload_len)
{
int res = 0;
#if 1
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (devid < 0 || msg_type < 0 || msg_type >= IOTX_LINKKIT_MSG_MAX) {
dm_log_err("Invalid Parameter");
return FAIL_RETURN;
}
if (ctx->is_opened == 0 || ctx->is_connected == 0) {
return FAIL_RETURN;
}
_iotx_linkkit_mutex_lock();
switch (msg_type) {
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
case ITM_MSG_QUERY_TIMESTAMP: {
res = iotx_dm_qurey_ntp();
}
break;
#endif
case ITM_MSG_QUERY_TOPOLIST: {
#if 1//DEVICE_MODEL_GATEWAY
res = iotx_dm_query_topo_list();
#else
res = FAIL_RETURN;
#endif
}
break;
case ITM_MSG_QUERY_FOTA_DATA: {
res = iotx_dm_fota_perform_sync((char *)payload, payload_len);
}
break;
case ITM_MSG_QUERY_COTA_DATA: {
res = iotx_dm_cota_perform_sync((char *)payload, payload_len);
}
break;
case ITM_MSG_REQUEST_COTA: {
res = iotx_dm_cota_get_config("product", "file", "");
}
break;
case ITM_MSG_REQUEST_FOTA_IMAGE: {
res = iotx_dm_fota_request_image((const char *)payload, payload_len);
}
break;
default: {
dm_log_err("Unknown Message Type");
res = FAIL_RETURN;
}
break;
}
_iotx_linkkit_mutex_unlock();
return res;
}
int IOT_Linkkit_TriggerEvent(int devid, char *eventid, int eventid_len, char *payload, int payload_len)
{
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
int res = 0;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (devid < 0 || eventid == NULL || eventid_len <= 0 || payload == NULL || payload_len <= 0) {
dm_log_err("Invalid Parameter");
return FAIL_RETURN;
}
if (ctx->is_opened == 0 || ctx->is_connected == 0) {
return FAIL_RETURN;
}
_iotx_linkkit_mutex_lock();
res = iotx_dm_post_event(devid, eventid, eventid_len, payload, payload_len);
_iotx_linkkit_mutex_unlock();
return res;
#else
return -1;
#endif
#endif
return -1;
}*/
#if 0//DEVICE_MODEL_GATEWAY
int iot_linkkit_subdev_query_id(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1])
{
int res = -1;
iotx_linkkit_ctx_t *ctx = _iotx_linkkit_get_ctx();
if (ctx->is_opened == 0) {
return res;
}
iotx_dm_subdev_query(product_key, device_name, &res);
return res;
}
#endif /* #ifdef DEVICE_MODEL_GATEWAY */
int kk_mid_subdev_add(char product_key[PRODUCT_KEY_MAXLEN], char device_name[DEVICE_NAME_MAXLEN], char device_secret[DEVICE_SECRET_MAXLEN]){
int res = 0;
int devid = 0;
res = dm_mgr_subdev_create(product_key,device_name,device_secret,&devid);
if (res != SUCCESS_RETURN) {
printf("subdev create Failed\n");
return FAIL_RETURN;
}
printf("subdev open susseed, devid = %d\n", devid);
res = _iotx_linkkit_slave_connect(devid);
if (res != SUCCESS_RETURN) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
res = _iotx_linkkit_subdev_login(devid);
if (res != SUCCESS_RETURN) {
_iotx_linkkit_mutex_unlock();
return FAIL_RETURN;
}
return SUCCESS_RETURN;
}
#ifndef _INFRA_LIST_H_
#define _INFRA_LIST_H_
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) || defined(__GNUC__)) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
* Get offset of a member variable.
*
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the variable within the struct.
*/
#define aos_offsetof(type, member) ((int)&(((type *)0)->member))
/*
* Get the struct for this entry.
*
* @param[in] ptr the list head to take the element from.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the variable within the struct.
*/
#define aos_container_of(ptr, type, member) \
((type *) ((char *) (ptr) - aos_offsetof(type, member)))
/* for double link list */
typedef struct dlist_s {
struct dlist_s *prev;
struct dlist_s *next;
} dlist_t;
static inline void __dlist_add(dlist_t *node, dlist_t *prev, dlist_t *next)
{
node->next = next;
node->prev = prev;
//printf("[%s][%d]node->name:%d\n",__FUNCTION__,__LINE__);
prev->next = node;
next->prev = node;
}
/*
* Get the struct for this entry.
*
* @param[in] addr the list head to take the element from.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the dlist_t within the struct.
*/
#define dlist_entry(addr, type, member) \
((type *)((long)addr - aos_offsetof(type, member)))
static inline void dlist_add(dlist_t *node, dlist_t *queue)
{
__dlist_add(node, queue, queue->next);
}
static inline void dlist_add_tail(dlist_t *node, dlist_t *queue)
{
__dlist_add(node, queue->prev, queue);
}
static inline void dlist_del(dlist_t *node)
{
dlist_t *prev = node->prev;
dlist_t *next = node->next;
prev->next = next;
next->prev = prev;
}
static inline void dlist_init(dlist_t *node)
{
node->next = node->prev = node;
}
static inline void INIT_AOS_DLIST_HEAD(dlist_t *list)
{
list->next = list;
list->prev = list;
}
static inline int dlist_empty(const dlist_t *head)
{
return head->next == head;
}
/*
* Initialise the list.
*
* @param[in] list the list to be inited.
*/
#define AOS_DLIST_INIT(list) {&(list), &(list)}
/*
* Get the first element from a list
*
* @param[in] ptr the list head to take the element from.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the dlist_t within the struct.
*/
#define dlist_first_entry(ptr, type, member) \
dlist_entry((ptr)->next, type, member)
/*
* Iterate over a list.
*
* @param[in] pos the &struct dlist_t to use as a loop cursor.
* @param[in] head he head for your list.
*/
#define dlist_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/*
* Iterate over a list safe against removal of list entry.
*
* @param[in] pos the &struct dlist_t to use as a loop cursor.
* @param[in] n another &struct dlist_t to use as temporary storage.
* @param[in] head he head for your list.
*/
#define dlist_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/*
* Iterate over list of given type.
*
* @param[in] queue he head for your list.
* @param[in] node the &struct dlist_t to use as a loop cursor.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the dlist_t within the struct.
*/
#define dlist_for_each_entry(queue, node, type, member) \
for (node = aos_container_of((queue)->next, type, member); \
&node->member != (queue); \
node = aos_container_of(node->member.next, type, member))
/*
* Iterate over list of given type safe against removal of list entry.
*
* @param[in] queue the head for your list.
* @param[in] n the type * to use as a temp.
* @param[in] node the type * to use as a loop cursor.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the dlist_t within the struct.
*/
#define dlist_for_each_entry_safe(queue, n, node, type, member) \
for (node = aos_container_of((queue)->next, type, member), \
n = (queue)->next ? (queue)->next->next : NULL; \
&node->member != (queue); \
node = aos_container_of(n, type, member), n = n ? n->next : NULL)
/*
* Get the struct for this entry.
* @param[in] ptr the list head to take the element from.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the variable within the struct.
*/
#define list_entry(ptr, type, member) \
aos_container_of(ptr, type, member)
/*
* Iterate backwards over list of given type.
*
* @param[in] pos the type * to use as a loop cursor.
* @param[in] head he head for your list.
* @param[in] member the name of the dlist_t within the struct.
* @param[in] type the type of the struct this is embedded in.
*/
#define dlist_for_each_entry_reverse(pos, head, member, type) \
for (pos = list_entry((head)->prev, type, member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, type, member))
/*
* Get the list length.
*
* @param[in] queue the head for your list.
*/
static inline int __dlist_entry_number(dlist_t *queue)
{
int num;
dlist_t *cur = queue;
for (num = 0; cur->next != queue; cur = cur->next, num++)
;
return num;
}
/*
* Get the list length.
*
* @param[in] queue the head for your list.
*/
#define dlist_entry_number(head) \
__dlist_entry_number(head)
/*
* Initialise the list.
*
* @param[in] name the list to be initialized.
*/
#define AOS_DLIST_HEAD_INIT(name) { &(name), &(name) }
/*
* Initialise the list.
*
* @param[in] name the list to be initialized.
*/
#define AOS_DLIST_HEAD(name) \
dlist_t name = AOS_DLIST_HEAD_INIT(name)
/* for single link list */
typedef struct slist_s {
struct slist_s *next;
} slist_t;
static inline void slist_add(slist_t *node, slist_t *head)
{
node->next = head->next;
head->next = node;
}
static inline void slist_add_tail(slist_t *node, slist_t *head)
{
while (head->next) {
head = head->next;
}
slist_add(node, head);
}
static inline void slist_del(slist_t *node, slist_t *head)
{
while (head->next) {
if (head->next == node) {
head->next = node->next;
break;
}
head = head->next;
}
}
static inline int slist_empty(const slist_t *head)
{
return !head->next;
}
static inline void slist_init(slist_t *head)
{
head->next = 0;
}
/*
* Iterate over list of given type.
*
* @param[in] queue he head for your list.
* @param[in] node the type * to use as a loop cursor.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the slist_t within the struct.
*/
#define slist_for_each_entry(queue, node, type, member) \
for (node = aos_container_of((queue)->next, type, member); \
&node->member; \
node = aos_container_of(node->member.next, type, member))
/*
* Iterate over list of given type safe against removal of list entry.
*
* @param[in] queue the head for your list.
* @param[in] tmp the type * to use as a temp.
* @param[in] node the type * to use as a loop cursor.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the slist_t within the struct.
*/
#define slist_for_each_entry_safe(queue, tmp, node, type, member) \
for (node = aos_container_of((queue)->next, type, member), \
tmp = (queue)->next ? (queue)->next->next : NULL; \
&node->member; \
node = aos_container_of(tmp, type, member), tmp = tmp ? tmp->next : tmp)
/*
* Initialise the list.
*
* @param[in] name the list to be initialized.
*/
#define AOS_SLIST_HEAD_INIT(name) {0}
/*
* Initialise the list.
*
* @param[in] name the list to be initialized.
*/
#define AOS_SLIST_HEAD(name) \
slist_t name = AOS_SLIST_HEAD_INIT(name)
/*
* Get the struct for this entry.
* @param[in] addr the list head to take the element from.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the slist_t within the struct.
*/
#define slist_entry(addr, type, member) ( \
addr ? (type *)((long)addr - aos_offsetof(type, member)) : (type *)addr \
)
/*
* Get the first element from a list.
*
* @param[in] ptr the list head to take the element from.
* @param[in] type the type of the struct this is embedded in.
* @param[in] member the name of the slist_t within the struct.
*/
#define slist_first_entry(ptr, type, member) \
slist_entry((ptr)->next, type, member)
/*
* Get the list length.
*
* @param[in] queue the head for your list.
*/
static inline int slist_entry_number(slist_t *queue)
{
int num;
slist_t *cur = queue;
for (num = 0; cur->next; cur = cur->next, num++)
;
return num;
}
#ifndef offset_of
#define offset_of aos_offsetof
#endif
#ifndef container_of
#define container_of aos_container_of
#endif
#define LIST_HEAD AOS_DLIST_HEAD
#define LIST_HEAD_INIT AOS_DLIST_INIT
#define INIT_LIST_HEAD INIT_AOS_DLIST_HEAD
#define LIST_INIT AOS_DLIST_INIT
#define list_head dlist_s
#define list_head_t dlist_t
#define list_add dlist_add
#define list_add_tail dlist_add_tail
#define list_del dlist_del
#define list_empty dlist_empty
#define list_entry_number dlist_entry_number
#define list_first_entry dlist_first_entry
#define list_for_each dlist_for_each
#define list_for_each_entry_reverse dlist_for_each_entry_reverse
#define list_for_each_safe dlist_for_each_safe
#define list_init dlist_init
#define list_for_each_entry(pos, head, member, type) \
dlist_for_each_entry(head, pos, type, member)
#define list_for_each_entry_safe(pos, n, head, member, type) \
for (pos = list_entry((head)->next, type, member), \
n = list_entry(pos->member.next, type, member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, type, member))
#define list_next_entry(pos, member, type) \
list_entry((pos)->member.next, type, member)
static inline void list_del_init(struct list_head *entry)
{
list_del(entry);
INIT_LIST_HEAD(entry);
}
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
typedef struct propertyInfo
{
char name[50];
char identifier[50];
struct list_head list;
}propertyInfo_t;
#ifdef __cplusplus
}
#endif
#endif /* AOS_LIST_H */
......@@ -13,6 +13,7 @@
#include <sys/shm.h>
#include "cJSON.h"
#include "kk_product.h"
#include "kk_tsl_common.h"
......@@ -29,7 +30,21 @@ void mid_cb(void* data, int len){
{
topic = cJSON_GetObjectItem(json, "topic");
payload = cJSON_GetObjectItem(json, "payload");
kk_tsl_service_property_set(topic->valuestring, payload->valuestring,strlen(payload->valuestring),NULL);
printf("mid_cb topic: [%s] ,payload= %s \n",topic->valuestring,payload->valuestring );
if (strcmp(payload->valuestring, "addsub")==0){
kk_mid_subdev_add("a1OYuSallan","allanWno8yDdsjCX15iq","");
}else{
void* buf = malloc(len);
memcpy(buf, data, len);
int res = dm_queue_msg_insert((void *)buf);
if (res != SUCCESS_RETURN) {
free(buf);
return FAIL_RETURN;
}
//kk_tsl_service_property_set(topic->valuestring, payload->valuestring, strlen(payload->valuestring), NULL);
}
}
kk_ipc_send(IPC_MID2PLAT, data, len);
}
......@@ -94,6 +109,38 @@ time_t getSysTime(){
return t;
}
typedef struct {
int auto_add_subdev;
int master_devid;
int cloud_connected;
int master_initialized;
int subdev_index;
int permit_join;
void *g_mid_dispatch_thread;
int g_mid_dispatch_thread_running;
} mid_ctx_t;
#define MID_YIELD_TIMEOUT_MS (200)
static mid_ctx_t g_mid_ctx;
static mid_ctx_t *kk_mid_get_ctx(void)
{
return &g_mid_ctx;
}
extern void IOT_Linkkit_Yield(int timeout_ms);
void *mid_dispatch_yield(void *args)
{
mid_ctx_t *mid_ctx = kk_mid_get_ctx();
while (mid_ctx->g_mid_dispatch_thread_running) {
IOT_Linkkit_Yield(MID_YIELD_TIMEOUT_MS);
}
return NULL;
}
static int kk_set_product_info(void)
{
HAL_SetProduct_Type(PRODUCT_TPYE);
......@@ -104,6 +151,13 @@ static int kk_set_product_info(void)
int main(const int argc, const char **argv)
{
int res = 0;
mid_ctx_t *mid_ctx = kk_mid_get_ctx();
memset(mid_ctx, 0, sizeof(mid_ctx_t));
kk_set_product_info();
kk_tsl_api_init();
kk_ipc_init(IPC_MID2APP, mid_cb);
......@@ -113,9 +167,33 @@ int main(const int argc, const char **argv)
//DB_Init();
//test_tcp();
/* when Connect to app and platfrom */
/*do {
res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
if (res < 0) {
EXAMPLE_TRACE("IOT_Linkkit_Connect failed, retry after 5s...\n");
HAL_SleepMs(5000);
}
} while (res < 0);*/
kk_init_dmproc();
mid_ctx->g_mid_dispatch_thread_running = 1;
res = pthread_create(&mid_ctx->g_mid_dispatch_thread, NULL, mid_dispatch_yield, NULL);
if (res < 0) {
printf("HAL_ThreadCreate Failed\n");
IOT_Linkkit_Close(mid_ctx->master_devid);
return -1;
}
char buf[100] = {0};
int ct = 0;
for (;;) {
sleep(5);
usleep(4000000);
if (ct == 0){
ct =1;
kk_set_tsl_by_productKey("a1OYuSallan","model.json");
kk_mid_subdev_add("a1OYuSallan","allanWno8yDdsjCX15iq","");
}
/*memset(buf, 0, 100);
sprintf(buf , "send data app time = %d\n", getSysTime());
printf("M2A: %s", buf);
......
......@@ -2,6 +2,7 @@
#define _TSL_API_H__
#include "lite-cjson.h"
#include "kk_tsl_common.h"
#include "../../midware/dm/iotx_dm.h"
......@@ -14,14 +15,14 @@
#define KK_MSG_KEY_MESSAGE "message"
typedef enum {
/*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;
} iotx_dm_error_code_t;*/
typedef struct {
const char *uri;
......
#include "kk_utils.h"
#include "lite-cjson.h"
int kk_utils_copy(_IN_ void *input, _IN_ int input_len, _OU_ void **output, _IN_ int output_len)
{
......@@ -262,4 +264,55 @@ int kk_utils_ftoa_direct(_IN_ double input, _OU_ char **output)
return SUCCESS_RETURN;
}
int kk_utils_json_parse(_IN_ const char *payload, _IN_ int payload_len, _IN_ int type, _OU_ lite_cjson_t *lite)
{
int res = 0;
if (payload == NULL || payload_len <= 0 || type < 0 || lite == NULL) {
return INVALID_PARAMETER;
}
memset(lite, 0, sizeof(lite_cjson_t));
res = lite_cjson_parse(payload, payload_len, lite);
if (res != SUCCESS_RETURN) {
memset(lite, 0, sizeof(lite_cjson_t));
return FAIL_RETURN;
}
//printf (" lite->type= %d [%d] \n", lite->type, type);
if (type != cJSON_Invalid && lite->type != type) {
memset(lite, 0, sizeof(lite_cjson_t));
return FAIL_RETURN;
}
return SUCCESS_RETURN;
}
int kk_utils_json_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ int key_len, _IN_ int type,
_OU_ lite_cjson_t *lite_item)
{
int res = 0;
if (lite == NULL || lite->type != cJSON_Object || key == NULL || key_len <= 0 || type < 0 || lite_item == NULL) {
return INVALID_PARAMETER;
}
if (lite->type != cJSON_Object) {
dm_log_err("lite->type != cJSON_Object, %d", lite->type);
}
memset(lite_item, 0, sizeof(lite_cjson_t));
res = lite_cjson_object_item(lite, key, key_len, lite_item);
if (res != SUCCESS_RETURN) {
/* dm_log_err(DM_UTILS_LOG_JSON_PARSE_FAILED, lite->value_length, lite->value); */
memset(lite_item, 0, sizeof(lite_cjson_t));
return FAIL_RETURN;
}
if (type != cJSON_Invalid && lite_item->type != type) {
memset(lite_item, 0, sizeof(lite_cjson_t));
return FAIL_RETURN;
}
return SUCCESS_RETURN;
}
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