Initial commit: CCU621_M firmware project with BLE debug link support.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-08 17:28:36 +08:00
commit 9ceb218f80
1597 changed files with 724159 additions and 0 deletions
+205
View File
@@ -0,0 +1,205 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOT_EXPORT_LINKKIT_H_
#define _IOT_EXPORT_LINKKIT_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "infra_types.h"
#include "infra_defs.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];
char product_Registration[IOTX_DEVICE_REG_CODE_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,
/* post historical property or event to cloud */
ITM_MSG_POST_HISTORY_DATA,
IOTX_LINKKIT_MSG_MAX
} iotx_linkkit_msg_type_t;
/**
* @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);
void v_connectde_init(void);
/**
* @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 state code.
*
*/
int 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);
/**
* @brief post service response to cloud
*
* @param devid. device identifier.
* @param serviceid. tsl service id.
* @param serviceid_len. length of tsl service id.
* @param payload. service response payload.
* @param payload_len. service response payload length.
*
* @return success: 0, fail: -1.
*
*/
int IOT_Linkkit_AnswerService(int devid, char *serviceid, int serviceid_len, char *payload, int payload_len,
void *p_service_ctx);
#if defined(__cplusplus)
}
#endif
#endif
File diff suppressed because it is too large Load Diff
+756
View File
@@ -0,0 +1,756 @@
#include "iotx_dm_internal.h"
#ifdef LOG_REPORT_TO_CLOUD
#include "iotx_log_report.h"
#endif
static dm_client_uri_map_t g_dm_client_uri_map[] = {
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
{DM_URI_THING_EVENT_POST_REPLY_WILDCARD, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_event_post_reply},
#ifdef DEVICE_MODEL_SHADOW
{DM_URI_THING_PROPERTY_DESIRED_DELETE_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_property_desired_delete_reply},
{DM_URI_THING_PROPERTY_DESIRED_GET_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_property_desired_get_reply},
{DM_URI_THING_SERVICE_PROPERTY_GET, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_service_property_get},
#endif
#ifdef DEVICE_HISTORY_POST
{DM_URI_THING_EVENT_HISTORY_POST_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_event_post_reply},
#endif
{DM_URI_THING_SERVICE_PROPERTY_SET, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_service_property_set},
{DM_URI_THING_SERVICE_REQUEST_WILDCARD, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_service_request},
{DM_URI_THING_DEVICEINFO_UPDATE_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_deviceinfo_update_reply},
{DM_URI_THING_DEVICEINFO_DELETE_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_deviceinfo_delete_reply},
{DM_URI_THING_DYNAMICTSL_GET_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_dynamictsl_get_reply},
{DM_URI_RRPC_REQUEST_WILDCARD, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_rrpc_request_wildcard},
{DM_URI_NTP_RESPONSE, DM_URI_EXT_NTP_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_ntp_response},
{NULL, DM_URI_EXT_ERROR_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_ext_error},
#endif
{DM_URI_THING_MODEL_DOWN_RAW, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_model_down_raw},
{DM_URI_THING_MODEL_UP_RAW_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_ALL, (void *)dm_client_thing_model_up_raw_reply},
#ifdef DEVICE_MODEL_GATEWAY
{DM_URI_THING_TOPO_ADD_NOTIFY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_topo_add_notify},
{DM_URI_THING_GATEWAY_PERMIT, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_gateway_permit},
{DM_URI_THING_SUB_REGISTER_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_sub_register_reply},
{DM_URI_THING_PROXY_PRODUCT_REGISTER_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_proxy_product_register_reply},
{DM_URI_THING_SUB_UNREGISTER_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_sub_unregister_reply},
{DM_URI_THING_TOPO_ADD_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_topo_add_reply},
{DM_URI_THING_TOPO_DELETE_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_topo_delete_reply},
{DM_URI_THING_TOPO_GET_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_topo_get_reply},
{DM_URI_THING_LIST_FOUND_REPLY, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_list_found_reply},
{DM_URI_COMBINE_LOGIN_REPLY, DM_URI_EXT_SESSION_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_combine_login_reply},
{DM_URI_COMBINE_LOGOUT_REPLY, DM_URI_EXT_SESSION_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_combine_logout_reply},
{DM_URI_THING_DISABLE, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_disable},
{DM_URI_THING_ENABLE, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_enable},
{DM_URI_THING_DELETE, DM_URI_SYS_PREFIX, IOTX_DM_DEVICE_GATEWAY, (void *)dm_client_thing_delete},
#endif
};
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
/* property/event post reply filter */
static int _dm_client_subscribe_filter(char *uri, iotx_cm_data_handle_cb cb)
{
int res = 0;
int event_post_reply_opt = 0;
int retry_cnt = IOTX_DM_CLIENT_SUB_RETRY_MAX_COUNTS;
res = dm_opt_get(DM_OPT_DOWNSTREAM_EVENT_POST_REPLY, &event_post_reply_opt);
if (res != SUCCESS_RETURN)
{
return res;
}
if (event_post_reply_opt == 0)
{
res = dm_client_unsubscribe(uri);
return res;
}
else
{
res = -1;
while (res < SUCCESS_RETURN && retry_cnt--)
{
res = dm_client_subscribe(uri, cb, 0);
}
return res;
}
}
#endif /* #if !defined(DEVICE_MODEL_RAWDATA_SOLO) */
int dm_client_subscribe_all(int devid, char product_key[IOTX_PRODUCT_KEY_LEN + 1],
char device_name[IOTX_DEVICE_NAME_LEN + 1],
int dev_type)
{
int res = 0, index = 0;
int number = sizeof(g_dm_client_uri_map) / sizeof(dm_client_uri_map_t);
char *uri = NULL;
uint8_t local_sub = 0;
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
/* index 0 must be DM_URI_THING_EVENT_POST_REPLY_WILDCARD */
res = dm_utils_service_name((char *)g_dm_client_uri_map[0].uri_prefix, (char *)g_dm_client_uri_map[0].uri_name,
product_key, device_name, &uri);
if (res == SUCCESS_RETURN)
{
_dm_client_subscribe_filter(uri, (iotx_cm_data_handle_cb)g_dm_client_uri_map[0].callback);
DM_free(uri);
}
index = 1;
#else
index = 0;
#endif
#ifdef MQTT_AUTO_SUBSCRIBE
if (devid != 0)
{
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_IN_AUTOSUB_MODE, "subdev subscribe bypass");
return SUCCESS_RETURN;
}
#else
(void)devid;
#endif /* #ifdef MQTT_AUTO_SUBSCRIBE */
for (; index < number; index++)
{
if ((g_dm_client_uri_map[index].dev_type & dev_type) == 0)
{
continue;
}
#ifdef MQTT_AUTO_SUBSCRIBE
res = dm_utils_service_name((char *)g_dm_client_uri_map[index].uri_prefix, (char *)g_dm_client_uri_map[index].uri_name,
"+", "+", &uri); /* plus sign wildcards used */
if (res < SUCCESS_RETURN)
{
continue;
}
local_sub = 1;
res = dm_client_subscribe(uri, (iotx_cm_data_handle_cb)g_dm_client_uri_map[index].callback, &local_sub);
DM_free(uri);
#else
res = dm_utils_service_name((char *)g_dm_client_uri_map[index].uri_prefix, (char *)g_dm_client_uri_map[index].uri_name,
product_key, device_name, &uri);
if (res < SUCCESS_RETURN)
{
continue;
}
{
int retry_cnt = IOTX_DM_CLIENT_SUB_RETRY_MAX_COUNTS;
local_sub = 0;
do
{
res = dm_client_subscribe(uri, (iotx_cm_data_handle_cb)g_dm_client_uri_map[index].callback, &local_sub);
} while (res < SUCCESS_RETURN && --retry_cnt);
DM_free(uri);
}
#endif /* MQTT_AUTO_SUBSCRIBE */
}
return SUCCESS_RETURN;
}
static void _dm_client_event_cloud_connected_handle(void)
{
dm_msg_cloud_connected();
}
static void _dm_client_event_cloud_disconnect_handle(void)
{
dm_msg_cloud_disconnect();
}
void dm_client_event_handle(int fd, iotx_cm_event_msg_t *event, void *context)
{
switch (event->type)
{
case IOTX_CM_EVENT_CLOUD_CONNECTED:
{
_dm_client_event_cloud_connected_handle();
}
break;
case IOTX_CM_EVENT_CLOUD_CONNECT_FAILED:
{
}
break;
case IOTX_CM_EVENT_CLOUD_DISCONNECT:
{
_dm_client_event_cloud_disconnect_handle();
}
break;
default:
break;
}
}
void dm_client_thing_model_down_raw(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_model_down_raw(&source);
}
void dm_client_thing_model_up_raw_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_model_up_raw_reply(&source);
}
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
void dm_client_thing_service_property_set(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
int prop_set_reply_opt = 0;
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dest.uri_name = DM_URI_THING_SERVICE_PROPERTY_SET_REPLY;
res = dm_msg_proc_thing_service_property_set(&source, &dest, &request, &response);
if (res < SUCCESS_RETURN)
{
return;
}
prop_set_reply_opt = 0;
res = dm_opt_get(DM_OPT_UPSTREAM_PROPERTY_SET_REPLY, &prop_set_reply_opt);
if (res == SUCCESS_RETURN)
{
if (prop_set_reply_opt)
{
dm_msg_response(DM_MSG_DEST_CLOUD, &request, &response, "{}", strlen("{}"), NULL);
#ifdef LOG_REPORT_TO_CLOUD
if (SUCCESS_RETURN == check_target_msg(request.id.value, request.id.value_length))
{
send_permance_info(request.id.value, request.id.value_length, "2", 1);
}
#endif
}
}
}
#ifdef DEVICE_MODEL_SHADOW
void dm_client_thing_service_property_get(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
unsigned char *data = NULL;
int data_len = 0;
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dest.uri_name = DM_URI_THING_SERVICE_PROPERTY_GET_REPLY;
res = dm_msg_proc_thing_service_property_get(&source, &dest, &request, &response, &data, &data_len);
if (res < SUCCESS_RETURN)
{
return;
}
}
#endif
void dm_client_thing_service_request(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_service_request(&source);
}
void dm_client_thing_event_post_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_event_post_reply(&source);
}
#ifdef DEVICE_MODEL_SHADOW
void dm_client_thing_property_desired_get_reply(int fd, const char *topic, const char *payload,
unsigned int payload_len, void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_property_desired_get_reply(&source);
}
void dm_client_thing_property_desired_delete_reply(int fd, const char *topic, const char *payload,
unsigned int payload_len, void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_property_desired_delete_reply(&source);
}
#endif
void dm_client_thing_deviceinfo_update_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_deviceinfo_update_reply(&source);
}
void dm_client_thing_deviceinfo_delete_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_deviceinfo_delete_reply(&source);
}
void dm_client_thing_dynamictsl_get_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_dynamictsl_get_reply(&source);
}
void dm_client_rrpc_request_wildcard(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_rrpc_request(&source);
}
void dm_client_ntp_response(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_disp_ntp_response(&source);
}
void dm_client_ext_error(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_disp_ext_error_response(&source);
}
#endif
#ifdef DEVICE_MODEL_GATEWAY
int dm_client_subdev_unsubscribe(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1])
{
int res = 0, index = 0;
int number = sizeof(g_dm_client_uri_map) / sizeof(dm_client_uri_map_t);
char *uri = NULL;
for (index = 0; index < number; index++)
{
if ((g_dm_client_uri_map[index].dev_type & IOTX_DM_DEVICE_SUBDEV) == 0)
{
continue;
}
res = dm_utils_service_name((char *)g_dm_client_uri_map[index].uri_prefix, (char *)g_dm_client_uri_map[index].uri_name,
product_key, device_name, &uri);
if (res < SUCCESS_RETURN)
{
index--;
continue;
}
dm_client_unsubscribe(uri);
DM_free(uri);
}
return SUCCESS_RETURN;
}
void dm_client_thing_topo_add_notify(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dest.uri_name = DM_URI_THING_TOPO_ADD_NOTIFY_REPLY;
res = dm_msg_proc_thing_topo_add_notify(&source, &dest, &request, &response);
if (res < SUCCESS_RETURN)
{
return;
}
dm_msg_response(DM_MSG_DEST_CLOUD, &request, &response, "{}", strlen("{}"), NULL);
}
void dm_client_thing_disable(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dest.uri_name = DM_URI_THING_DISABLE_REPLY;
res = dm_msg_proc_thing_disable(&source, &dest, &request, &response);
if (res < SUCCESS_RETURN)
{
return;
}
dm_msg_response(DM_MSG_DEST_CLOUD, &request, &response, "{}", strlen("{}"), NULL);
}
void dm_client_thing_enable(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dest.uri_name = DM_URI_THING_ENABLE_REPLY;
res = dm_msg_proc_thing_enable(&source, &dest, &request, &response);
if (res < SUCCESS_RETURN)
{
return;
}
dm_msg_response(DM_MSG_DEST_CLOUD, &request, &response, "{}", strlen("{}"), NULL);
}
void dm_client_thing_delete(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dest.uri_name = DM_URI_THING_DELETE_REPLY;
res = dm_msg_proc_thing_delete(&source, &dest, &request, &response);
if (res < SUCCESS_RETURN)
{
return;
}
dm_msg_response(DM_MSG_DEST_CLOUD, &request, &response, "{}", strlen("{}"), NULL);
}
void dm_client_thing_gateway_permit(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dest.uri_name = DM_URI_THING_GATEWAY_PERMIT_REPLY;
res = dm_msg_proc_thing_gateway_permit(&source, &dest, &request, &response);
if (res < SUCCESS_RETURN)
{
return;
}
dm_msg_response(DM_MSG_DEST_CLOUD, &request, &response, "{}", strlen("{}"), NULL);
}
void dm_client_thing_sub_register_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_sub_register_reply(&source);
}
void dm_client_thing_proxy_product_register_reply(int fd, const char *topic, const char *payload,
unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_proxy_product_register_reply(&source);
}
void dm_client_thing_sub_unregister_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_sub_unregister_reply(&source);
}
void dm_client_thing_topo_add_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_topo_add_reply(&source);
}
void dm_client_thing_topo_delete_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_topo_delete_reply(&source);
}
void dm_client_thing_topo_get_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_topo_get_reply(&source);
}
void dm_client_thing_list_found_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_list_found_reply(&source);
}
void dm_client_combine_login_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_combine_login_reply(&source);
}
void dm_client_combine_logout_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_combine_logout_reply(&source);
}
#endif
+80
View File
@@ -0,0 +1,80 @@
#ifndef _DM_CLIENT_H_
#define _DM_CLIENT_H_
typedef struct {
const char *uri_name;
const char *uri_prefix;
int dev_type;
void *callback;
} dm_client_uri_map_t;
void dm_client_event_handle(int fd, iotx_cm_event_msg_t *event, void *context);
int dm_client_subscribe_all(int devid, char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1],
int dev_type);
void dm_client_thing_model_down_raw(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_model_up_raw_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
void dm_client_thing_property_desired_get_reply(int fd, const char *topic, const char *payload,
unsigned int payload_len, void *context);
void dm_client_thing_property_desired_delete_reply(int fd, const char *topic, const char *payload,
unsigned int payload_len, void *context);
void dm_client_thing_service_property_set(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_service_property_get(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_service_property_post(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_event_property_post_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_deviceinfo_update_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_deviceinfo_delete_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_dynamictsl_get_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_service_request(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_event_post_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_rrpc_request_wildcard(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_ntp_response(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context);
void dm_client_ext_error(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context);
#endif
#ifdef DEVICE_MODEL_GATEWAY
int dm_client_subdev_unsubscribe(char product_key[IOTX_PRODUCT_KEY_LEN + 1],
char device_name[IOTX_DEVICE_NAME_LEN + 1]);
void dm_client_thing_topo_add_notify(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_disable(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context);
void dm_client_thing_enable(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context);
void dm_client_thing_delete(int fd, const char *topic, const char *payload, unsigned int payload_len, void *context);
void dm_client_thing_gateway_permit(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_sub_register_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_proxy_product_register_reply(int fd, const char *topic, const char *payload,
unsigned int payload_len,
void *context);
void dm_client_thing_sub_unregister_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_topo_add_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_topo_delete_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_topo_get_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_thing_list_found_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_combine_login_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
void dm_client_combine_logout_reply(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
#endif
#endif
+139
View File
@@ -0,0 +1,139 @@
#include "iotx_dm_internal.h"
static dm_client_ctx_t g_dm_client_ctx = {0};
static dm_client_ctx_t *dm_client_get_ctx(void)
{
return &g_dm_client_ctx;
}
int dm_client_open(void)
{
int res = 0;
dm_client_ctx_t *ctx = dm_client_get_ctx();
iotx_cm_init_param_t cm_param;
memset(ctx, 0, sizeof(dm_client_ctx_t));
memset(&cm_param, 0, sizeof(iotx_cm_init_param_t));
cm_param.request_timeout_ms = IOTX_DM_CLIENT_REQUEST_TIMEOUT_MS;
cm_param.keepalive_interval_ms = IOTX_DM_CLIENT_KEEPALIVE_INTERVAL_MS;
cm_param.write_buf_size = CONFIG_MQTT_TX_MAXLEN;
cm_param.read_buf_size = CONFIG_MQTT_RX_MAXLEN;
#if defined(COAP_COMM_ENABLED) && !defined(MQTT_COMM_ENABLED)
cm_param.protocol_type = IOTX_CM_PROTOCOL_TYPE_COAP;
#else
cm_param.protocol_type = IOTX_CM_PROTOCOL_TYPE_MQTT;
#endif
cm_param.handle_event = dm_client_event_handle;
res = iotx_cm_open(&cm_param);
ctx->fd = res;
if (res < SUCCESS_RETURN) {
return res;
}
return SUCCESS_RETURN;
}
int dm_client_connect(int timeout_ms)
{
dm_client_ctx_t *ctx = dm_client_get_ctx();
return iotx_cm_connect(ctx->fd, timeout_ms);
}
int dm_client_close(void)
{
dm_client_ctx_t *ctx = dm_client_get_ctx();
return iotx_cm_close(ctx->fd);
}
int dm_client_subscribe(char *uri, iotx_cm_data_handle_cb callback, void *context)
{
uint8_t local_sub = 0;
dm_client_ctx_t *ctx = dm_client_get_ctx();
iotx_cm_ext_params_t sub_params;
memset(&sub_params, 0, sizeof(iotx_cm_ext_params_t));
if (context != NULL) {
local_sub = *((uint8_t *)context);
}
if (local_sub == 1) {
sub_params.ack_type = IOTX_CM_MESSAGE_SUB_LOCAL;
sub_params.sync_mode = IOTX_CM_ASYNC;
} else {
sub_params.ack_type = IOTX_CM_MESSAGE_NO_ACK;
sub_params.sync_mode = IOTX_CM_SYNC;
}
sub_params.sync_timeout = IOTX_DM_CLIENT_SUB_TIMEOUT_MS;
sub_params.ack_cb = NULL;
return iotx_cm_sub(ctx->fd, &sub_params, (const char *)uri, callback, NULL);
}
int dm_client_unsubscribe(char *uri)
{
dm_client_ctx_t *ctx = dm_client_get_ctx();
return iotx_cm_unsub(ctx->fd, uri);
}
int dm_client_publish(char *uri, unsigned char *payload, int payload_len, iotx_cm_data_handle_cb callback)
{
int res = 0;
char *pub_uri = NULL;
dm_client_ctx_t *ctx = dm_client_get_ctx();
iotx_cm_ext_params_t pub_param;
memset(&pub_param, 0, sizeof(iotx_cm_ext_params_t));
pub_param.ack_type = IOTX_CM_MESSAGE_NO_ACK;
pub_param.sync_mode = IOTX_CM_ASYNC;
pub_param.sync_timeout = 0;
pub_param.ack_cb = NULL;
#if defined(COAP_COMM_ENABLED) && !defined(MQTT_COMM_ENABLED)
pub_param.ack_cb = callback;
res = dm_utils_uri_add_prefix("/topic", uri, &pub_uri);
if (res < SUCCESS_RETURN) {
return res;
}
#else
pub_uri = uri;
#endif
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_TX_CLOUD_MESSAGE, "pub-uri: %s", pub_uri);
res = iotx_cm_pub(ctx->fd, &pub_param, (const char *)pub_uri, (const char *)payload, (unsigned int)payload_len);
#if defined(COAP_COMM_ENABLED) && !defined(MQTT_COMM_ENABLED)
DM_free(pub_uri);
#endif
return res;
}
int dm_client_yield(unsigned int timeout)
{
dm_client_ctx_t *ctx = dm_client_get_ctx();
return iotx_cm_yield(ctx->fd, timeout);
}
void dm_client_user_sub_request(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context)
{
dm_msg_source_t source;
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = topic;
source.payload = (unsigned char *)payload;
source.payload_len = payload_len;
source.context = NULL;
dm_msg_proc_thing_model_user_sub(&source);
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef _DM_CLIENT_ADAPTER_H_
#define _DM_CLIENT_ADAPTER_H_
typedef struct {
int fd;
void *callback;
} dm_client_ctx_t;
int dm_client_open(void);
int dm_client_connect(int timeout_ms);
int dm_client_close(void);
int dm_client_subscribe(char *uri, iotx_cm_data_handle_cb callback, void *context);
int dm_client_unsubscribe(char *uri);
int dm_client_publish(char *uri, unsigned char *payload, int payload_len, iotx_cm_data_handle_cb callback);
int dm_client_yield(unsigned int timeout);
void dm_client_user_sub_request(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
#endif
+229
View File
@@ -0,0 +1,229 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
#if defined(OTA_ENABLED) && !defined(BUILD_AOS)
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#define DM_COTA_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "dm.cota")
#define DM_COTA_FREE(ptr) LITE_free(ptr)
#else
#define DM_COTA_MALLOC(size) HAL_Malloc(size)
#define DM_COTA_FREE(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
#endif
static dm_cota_ctx_t g_dm_cota_ctx;
dm_cota_ctx_t *dm_cota_get_ctx(void)
{
return &g_dm_cota_ctx;
}
int dm_cota_init(void)
{
dm_cota_ctx_t *ctx = dm_cota_get_ctx();
memset(ctx, 0, sizeof(dm_cota_ctx_t));
return SUCCESS_RETURN;
}
int dm_cota_deinit(void)
{
dm_cota_ctx_t *ctx = dm_cota_get_ctx();
memset(ctx, 0, sizeof(dm_cota_ctx_t));
return SUCCESS_RETURN;
}
static int _dm_cota_send_new_config_to_user(void *ota_handle)
{
int res = 0, message_len = 0;
char *message = NULL;
uint32_t config_size = 0;
char *config_id = NULL, *sign = NULL, *sign_method = NULL, *url = NULL, *get_type = NULL;
const char *cota_new_config_fmt =
"{\"configId\":\"%s\",\"configSize\":%d,\"getType\":\"%s\",\"sign\":\"%s\",\"signMethod\":\"%s\",\"url\":\"%s\"}";
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_COTA_CONFIG_ID, (void *)&config_id, 1);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_COTA_CONFIG_SIZE, &config_size, 4);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_COTA_SIGN, (void *)&sign, 1);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_COTA_SIGN_METHOD, (void *)&sign_method, 1);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_COTA_URL, (void *)&url, 1);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_COTA_GETTYPE, (void *)&get_type, 1);
if (config_id == NULL || sign == NULL || sign_method == NULL || url == NULL || get_type == NULL) {
res = STATE_USER_INPUT_INVALID;
goto ERROR;
}
message_len = strlen(cota_new_config_fmt) + strlen(config_id) + DM_UTILS_UINT32_STRLEN + strlen(get_type) +
strlen(sign) + strlen(sign_method) + strlen(url) + 1;
message = DM_malloc(message_len);
if (message == NULL) {
res = STATE_SYS_DEPEND_MALLOC;
goto ERROR;
}
memset(message, 0, message_len);
HAL_Snprintf(message, message_len, cota_new_config_fmt, config_id, config_size, get_type, sign, sign_method, url);
res = _dm_msg_send_to_user(IOTX_DM_EVENT_COTA_NEW_CONFIG, message);
if (res != SUCCESS_RETURN) {
if (message) {
DM_free(message);
}
goto ERROR;
}
res = SUCCESS_RETURN;
ERROR:
if (config_id) {
DM_COTA_FREE(config_id);
}
if (sign) {
DM_COTA_FREE(sign);
}
if (sign_method) {
DM_COTA_FREE(sign_method);
}
if (url) {
DM_COTA_FREE(url);
}
if (get_type) {
DM_COTA_FREE(get_type);
}
return res;
}
int dm_cota_perform_sync(_OU_ char *output, _IN_ int output_len)
{
int res = 0, file_download = 0;
uint64_t file_size = 0, file_downloaded = 0;
uint32_t percent_pre = 0, percent_now = 0;
unsigned long long report_pre = 0, report_now = 0;
dm_cota_ctx_t *ctx = dm_cota_get_ctx();
void *ota_handle = NULL;
uint32_t ota_type = IOT_OTAT_NONE;
if (output == NULL || output_len <= 0) {
return STATE_USER_INPUT_INVALID;
}
/* Get Ota Handle */
res = dm_ota_get_ota_handle(&ota_handle);
if (res < 0) {
return res;
}
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_OTA_TYPE, &ota_type, 4);
if (ota_type != IOT_OTAT_COTA) {
return STATE_DEV_MODEL_OTA_TYPE_ERROR;
}
/* reset the size_fetched in ota_handle to be 0 */
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_RESET_FETCHED_SIZE, ota_handle, 4);
/* Prepare Write Data To Storage */
HAL_Firmware_Persistence_Start();
while (1) {
file_download = IOT_OTA_FetchYield(ota_handle, output, output_len, 1);
if (file_download < 0) {
IOT_OTA_ReportProgress(ota_handle, IOT_OTAP_FETCH_FAILED, NULL);
HAL_Firmware_Persistence_Stop();
ctx->is_report_new_config = 0;
return STATE_DEV_MODEL_OTA_FETCH_FAILED;
}
/* Write Config File Into Stroage */
HAL_Firmware_Persistence_Write(output, file_download);
/* Get OTA information */
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_FETCHED_SIZE, &file_downloaded, 4);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_FILE_SIZE, &file_size, 4);
/* Calculate Download Percent And Update Report Timestamp*/
percent_now = (file_downloaded * 100) / file_size;
report_now = HAL_UptimeMs();
/* Report Download Process To Cloud */
if (report_now < report_pre) {
report_pre = report_now;
}
if ((((percent_now - percent_pre) > 5) &&
((report_now - report_pre) > 50)) || (percent_now >= IOT_OTAP_FETCH_PERCENTAGE_MAX)) {
IOT_OTA_ReportProgress(ota_handle, percent_now, NULL);
percent_pre = percent_now;
report_pre = report_now;
}
/* Check If OTA Finished */
if (IOT_OTA_IsFetchFinish(ota_handle)) {
uint32_t file_isvalid = 0;
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_CHECK_CONFIG, &file_isvalid, 4);
if (file_isvalid == 0) {
HAL_Firmware_Persistence_Stop();
ctx->is_report_new_config = 0;
return STATE_DEV_MODEL_OTA_IMAGE_CHECK_FAILED;
} else {
break;
}
}
}
HAL_Firmware_Persistence_Stop();
ctx->is_report_new_config = 0;
return SUCCESS_RETURN;
}
int dm_cota_get_config(const char *config_scope, const char *get_type, const char *attribute_keys)
{
int res = 0;
void *ota_handle = NULL;
/* Get Ota Handle */
res = dm_ota_get_ota_handle(&ota_handle);
if (res != SUCCESS_RETURN) {
return res;
}
return iotx_ota_get_config(ota_handle, config_scope, get_type, attribute_keys);
}
int dm_cota_status_check(void)
{
int res = 0;
dm_cota_ctx_t *ctx = dm_cota_get_ctx();
void *ota_handle = NULL;
/* Get Ota Handle */
res = dm_ota_get_ota_handle(&ota_handle);
if (res != SUCCESS_RETURN) {
return res;
}
if (IOT_OTA_IsFetching(ota_handle)) {
uint32_t ota_type = IOT_OTAT_NONE;
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_OTA_TYPE, &ota_type, 4);
if (ota_type == IOT_OTAT_COTA) {
/* Send New Config Information To User */
if (ctx->is_report_new_config == 0) {
res = _dm_cota_send_new_config_to_user(ota_handle);
if (res == SUCCESS_RETURN) {
ctx->is_report_new_config = 1;
}
}
}
}
return SUCCESS_RETURN;
}
#endif
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_COTA_H_
#define _DM_COTA_H_
typedef struct {
int is_report_new_config;
} dm_cota_ctx_t;
int dm_cota_init(void);
int dm_cota_deinit(void);
int dm_cota_perform_sync(_OU_ char *output, _IN_ int output_len);
int dm_cota_get_config(const char *config_scope, const char *get_type, const char *attribute_keys);
int dm_cota_status_check(void);
dm_cota_ctx_t *dm_cota_get_ctx(void);
#endif
+266
View File
@@ -0,0 +1,266 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
#if defined(OTA_ENABLED) && !defined(BUILD_AOS)
static dm_fota_ctx_t g_dm_fota_ctx;
dm_fota_ctx_t *dm_fota_get_ctx(void)
{
return &g_dm_fota_ctx;
}
int dm_fota_init(void)
{
dm_fota_ctx_t *ctx = dm_fota_get_ctx();
memset(ctx, 0, sizeof(dm_fota_ctx_t));
return SUCCESS_RETURN;
}
int dm_fota_deinit(void)
{
dm_fota_ctx_t *ctx = dm_fota_get_ctx();
memset(ctx, 0, sizeof(dm_fota_ctx_t));
return SUCCESS_RETURN;
}
static int _dm_fota_send_new_config_to_user(void *ota_handle)
{
int res = 0, message_len = 0;
char *message = NULL;
char version[128] = {0};
const char *fota_new_config_fmt = "{\"version\":\"%s\"}";
char module[128] = {0};
const char *fota_new_config_with_module_fmt = "{\"version\":\"%s\",\"module\":\"%s\"}";
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_VERSION, version, 128);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_MODULE, module, 128);
if (strlen(module) == 0)
{
message_len = strlen(fota_new_config_fmt) + strlen(version) + 1;
}
else
{
message_len = strlen(fota_new_config_with_module_fmt) + strlen(version) + strlen(module) + 1;
}
message = DM_malloc(message_len);
if (message == NULL)
{
return STATE_SYS_DEPEND_MALLOC;
}
memset(message, 0, message_len);
if (strlen(module) == 0)
{
HAL_Snprintf(message, message_len, fota_new_config_fmt, version);
}
else
{
HAL_Snprintf(message, message_len, fota_new_config_with_module_fmt, version, module);
}
res = _dm_msg_send_to_user(IOTX_DM_EVENT_FOTA_NEW_FIRMWARE, message);
if (res != SUCCESS_RETURN)
{
if (message)
{
DM_free(message);
}
return res;
}
return SUCCESS_RETURN;
}
int dm_fota_perform_sync(_OU_ char *output, _IN_ int output_len)
{
int res = 0, file_download = 0, retry_timeout = 0, retry_max_timeout = 0;
uint64_t file_size = 0, file_downloaded = 0;
uint32_t percent_pre = 0, percent_now = 0;
unsigned long long report_pre = 0, report_now = 0;
dm_fota_ctx_t *ctx = dm_fota_get_ctx();
void *ota_handle = NULL;
uint32_t ota_type = IOT_OTAT_NONE;
int ret = 0;
if (output == NULL || output_len <= 0)
{
return STATE_USER_INPUT_INVALID;
}
/* Get Ota Handle */
res = dm_ota_get_ota_handle(&ota_handle);
if (res != SUCCESS_RETURN)
{
return res;
}
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_OTA_TYPE, &ota_type, 4);
if (ota_type != IOT_OTAT_FOTA)
{
return STATE_DEV_MODEL_OTA_TYPE_ERROR;
}
/* reset the size_fetched in ota_handle to be 0 */
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_RESET_FETCHED_SIZE, ota_handle, 4);
/* Prepare Write Data To Storage */
HAL_Firmware_Persistence_Start();
while (1)
{
file_download = IOT_OTA_FetchYield(ota_handle, output, output_len, 3);
if (file_download < 0)
{
res = dm_opt_get(DM_OPT_FOTA_RETRY_TIMEOUT_MS, &retry_max_timeout);
if (res == SUCCESS_RETURN && retry_timeout >= retry_max_timeout)
{
IOT_OTA_ReportProgress(ota_handle, IOT_OTAP_FETCH_FAILED, NULL);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_RESET_STATE, NULL, 0);
HAL_Firmware_Persistence_Stop();
ctx->is_report_new_config = 0;
return STATE_DEV_MODEL_OTA_FETCH_FAILED;
}
retry_timeout += CONFIG_FOTA_RETRY_INTERNAL_MS;
HAL_SleepMs(CONFIG_FOTA_RETRY_INTERNAL_MS);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_OTA_FETCH_FAILED, "IOT_OTA_FetchYield() got %d", file_download);
continue;
}
retry_timeout = 0;
/* Write Config File Into Stroage */
ret = HAL_Firmware_Persistence_Write(output, file_download);
if (ret < 0)
{
IOT_OTA_ReportProgress(ota_handle, IOT_OTAP_BURN_FAILED, NULL);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_RESET_STATE, NULL, 0);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_SYS_DEPEND_FIRMWAIRE_WIRTE, "write f/w ran into %d", ret);
HAL_Firmware_Persistence_Stop();
ctx->is_report_new_config = 0;
return STATE_SYS_DEPEND_FIRMWAIRE_WIRTE;
}
/* Get OTA information */
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_FETCHED_SIZE, &file_downloaded, 4);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_FILE_SIZE, &file_size, 4);
/* Calculate Download Percent And Update Report Timestamp*/
percent_now = (file_downloaded * 100) / file_size;
report_now = HAL_UptimeMs();
/* Report Download Process To Cloud */
if (report_now < report_pre)
{
report_pre = report_now;
}
if ((((percent_now - percent_pre) > 5) &&
((report_now - report_pre) > 50)) ||
(percent_now >= IOT_OTAP_FETCH_PERCENTAGE_MAX))
{
IOT_OTA_ReportProgress(ota_handle, percent_now, NULL);
percent_pre = percent_now;
report_pre = report_now;
}
/* Check If OTA Finished */
if (IOT_OTA_IsFetchFinish(ota_handle))
{
uint32_t file_isvalid = 0;
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_CHECK_FIRMWARE, &file_isvalid, 4);
if (file_isvalid == 0)
{
IOT_OTA_ReportProgress(ota_handle, IOT_OTAP_CHECK_FALIED, NULL);
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_RESET_STATE, NULL, 0);
HAL_Firmware_Persistence_Stop();
ctx->is_report_new_config = 0;
return STATE_DEV_MODEL_OTA_IMAGE_CHECK_FAILED;
}
else
{
break;
}
}
HAL_SleepMs(50);
}
HAL_Firmware_Persistence_Stop();
ctx->is_report_new_config = 0;
return SUCCESS_RETURN;
}
int dm_fota_status_check(void)
{
int res = 0;
dm_fota_ctx_t *ctx = dm_fota_get_ctx();
void *ota_handle = NULL;
/* Get Ota Handle */
res = dm_ota_get_ota_handle(&ota_handle);
if (res != SUCCESS_RETURN)
{
return STATE_DEV_MODEL_OTA_NOT_INITED;
}
if (IOT_OTA_IsFetching(ota_handle))
{
uint32_t ota_type = IOT_OTAT_NONE;
IOT_OTA_Ioctl(ota_handle, IOT_OTAG_OTA_TYPE, &ota_type, 4);
if (ota_type == IOT_OTAT_FOTA)
{
/* Send New Config Information To User */
if (ctx->is_report_new_config == 0)
{
res = _dm_fota_send_new_config_to_user(ota_handle);
if (res == SUCCESS_RETURN)
{
ctx->is_report_new_config = 1;
}
}
}
}
return SUCCESS_RETURN;
}
int dm_fota_request_image(const char *version, int buffer_len)
{
int res = 0;
void *ota_handle = NULL;
char *version_str = NULL;
if (NULL == version || buffer_len <= 0)
{
return STATE_USER_INPUT_INVALID;
}
/* Get Ota Handle */
res = dm_ota_get_ota_handle(&ota_handle);
if (res != SUCCESS_RETURN)
{
return STATE_DEV_MODEL_OTA_NOT_INITED;
}
version_str = DM_malloc(buffer_len + 1);
if (NULL == version_str)
{
return STATE_SYS_DEPEND_MALLOC;
}
memset(version_str, 0, buffer_len + 1);
memcpy(version_str, version, buffer_len);
res = iotx_req_image(ota_handle, version_str);
DM_free(version_str);
return res;
}
#endif
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_FOTA_H_
#define _DM_FOTA_H_
typedef struct {
int is_report_new_config;
} dm_fota_ctx_t;
int dm_fota_init(void);
int dm_fota_deinit(void);
int dm_fota_perform_sync(_OU_ char *output, _IN_ int output_len);
int dm_fota_status_check(void);
int dm_fota_request_image(_IN_ const char *version, _IN_ int buffer_len);
dm_fota_ctx_t *dm_fota_get_ctx(void);
#endif
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOT_DM_API_H_
#define _IOT_DM_API_H_
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
+138
View File
@@ -0,0 +1,138 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
dm_ipc_t g_dm_ipc;
static dm_ipc_t *_dm_ipc_get_ctx(void)
{
return &g_dm_ipc;
}
static void _dm_ipc_lock(void)
{
dm_ipc_t *ctx = _dm_ipc_get_ctx();
if (ctx->mutex) {
HAL_MutexLock(ctx->mutex);
}
}
static void _dm_ipc_unlock(void)
{
dm_ipc_t *ctx = _dm_ipc_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
}
int dm_ipc_init(int max_size)
{
dm_ipc_t *ctx = _dm_ipc_get_ctx();
memset(ctx, 0, sizeof(dm_ipc_t));
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
return STATE_SYS_DEPEND_MUTEX_CREATE;
}
/* Init List */
ctx->msg_list.max_size = max_size;
INIT_LIST_HEAD(&ctx->msg_list.message_list);
return SUCCESS_RETURN;
}
void dm_ipc_deinit(void)
{
dm_ipc_t *ctx = _dm_ipc_get_ctx();
dm_ipc_msg_node_t *del_node = NULL;
dm_ipc_msg_node_t *next_node = NULL;
dm_ipc_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_ipc_msg_node_t) {
/* Free Message */
del_msg = (dm_ipc_msg_t *)del_node->data;
if (del_msg->data) {
DM_free(del_msg->data);
}
DM_free(del_msg);
del_msg = NULL;
/* Free Node */
list_del(&del_node->linked_list);
DM_free(del_node);
}
}
int dm_ipc_msg_insert(void *data)
{
dm_ipc_t *ctx = _dm_ipc_get_ctx();
dm_ipc_msg_node_t *node = NULL;
if (data == NULL) {
return STATE_USER_INPUT_INVALID;
}
_dm_ipc_lock();
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_MSGQ_OPERATION, "msg queue size: %d, max size: %d",
ctx->msg_list.size, ctx->msg_list.max_size);
if (ctx->msg_list.size >= ctx->msg_list.max_size) {
_dm_ipc_unlock();
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_MSGQ_FULL, NULL);
return STATE_DEV_MODEL_MSGQ_FULL;
}
node = DM_malloc(sizeof(dm_ipc_msg_node_t));
if (node == NULL) {
_dm_ipc_unlock();
return STATE_SYS_DEPEND_MALLOC;
}
memset(node, 0, sizeof(dm_ipc_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_ipc_unlock();
return SUCCESS_RETURN;
}
int dm_ipc_msg_next(void **data)
{
dm_ipc_t *ctx = _dm_ipc_get_ctx();
dm_ipc_msg_node_t *node = NULL;
if (data == NULL || *data != NULL) {
return STATE_USER_INPUT_INVALID;
}
_dm_ipc_lock();
if (list_empty(&ctx->msg_list.message_list)) {
_dm_ipc_unlock();
return STATE_DEV_MODEL_MSGQ_EMPTY;
}
node = list_first_entry(&ctx->msg_list.message_list, dm_ipc_msg_node_t, linked_list);
list_del(&node->linked_list);
ctx->msg_list.size--;
*data = node->data;
DM_free(node);
_dm_ipc_unlock();
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_MSGQ_OPERATION, "msg dequeue");
return SUCCESS_RETURN;
}
+38
View File
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_IPC_H_
#define _DM_IPC_H_
#include "iotx_dm_internal.h"
typedef struct {
iotx_dm_event_types_t type;
char *data;
} dm_ipc_msg_t;
typedef struct {
void *data;
struct list_head linked_list;
} dm_ipc_msg_node_t;
typedef struct {
int max_size;
int size;
struct list_head message_list;
} dm_ipc_msg_list_t;
typedef struct {
void *mutex;
dm_ipc_msg_list_t msg_list;
} dm_ipc_t;
int dm_ipc_init(int max_size);
void dm_ipc_deinit(void);
int dm_ipc_msg_insert(void *data);
int dm_ipc_msg_next(void **data);
#endif
+269
View File
@@ -0,0 +1,269 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
#if defined(LOG_REPORT_TO_CLOUD) && !defined(DEVICE_MODEL_RAWDATA_SOLO)
#include "dev_model_api.h"
const char THING_LOG_POST_PARAMS_HEAD[] =
"\"%.*s %s %ld ";
const char THING_LOG_POST_PARAMS_BODY[] =
"%s %ld ";
const char THING_LOG_POST_PARAMS_END[] =
"%s %ld\",";
char *g_log_poll = NULL;
static char *current_log_pos = NULL;
int remove_log_poll()
{
if (NULL != g_log_poll) {
HAL_Free(g_log_poll);
g_log_poll = NULL;
current_log_pos = NULL;
}
return 0;
}
unsigned int push_log(const char *input_log, int input_log_size)
{
if (NULL == current_log_pos || NULL == input_log || input_log_size <= 0) {
return -1;
}
memcpy(current_log_pos, input_log, input_log_size);
current_log_pos += input_log_size;
return (current_log_pos - g_log_poll);
}
unsigned int add_tail()
{
const char *tail = "]}";
current_log_pos -= 1;
return push_log(tail, strlen(tail));
}
void add_log_header()
{
const char *subprefix = "{\"template\": \"traceContext logContent\",\"contents\":[";
int sublen = strlen(subprefix);
push_log(subprefix, sublen);
}
int reset_log_poll()
{
if (NULL == g_log_poll) {
return -1;
}
memset(g_log_poll, 0, LOG_POLL_SIZE);
current_log_pos = g_log_poll;
add_log_header();
return 0;
}
int create_log_poll()
{
int ret;
remove_log_poll();
g_log_poll = HAL_Malloc(LOG_POLL_SIZE);
ret = reset_log_poll();
return ret;
}
static int switch_status = 0; /* 0 for switch off; 1 for switch on */
static unsigned int sample_interval = 5;
static unsigned int sample_count = 1000;
#define MSG_ID_LEN (64)
char msg_array[MSG_ID_LEN] = {0};
int check_target_msg(const char *input, int len)
{
/* do not upload log when swith is off */
if (0 == switch_status) {
return -1;
}
if (NULL == input || len <= 0) {
return -1;
}
return strncmp(input, msg_array, len);
}
static unsigned int msg_num = 0;
/* return 0 for success; -1 for failure */
int set_target_msg(const char *input, int len)
{
if (0 == switch_status) {
return -1;
}
if ((msg_num % sample_interval == 0) && (msg_num < sample_count)) {
if (NULL == input || len <= 0) {
return -1;
}
strncpy(msg_array, input, len);
return 0;
}
return -1;
}
void parse_msg_id(_IN_ char *payload, _IN_ int payload_len, _OU_ dm_msg_request_payload_t *request)
{
lite_cjson_t lite;
if (payload == NULL || payload_len <= 0 || request == NULL) {
return;
}
dm_utils_json_parse(payload, payload_len, cJSON_Object, &lite);
dm_utils_json_object_item(&lite, DM_MSG_KEY_ID, strlen(DM_MSG_KEY_ID), cJSON_String, &request->id);
}
int stop_sample()
{
if (current_log_pos > g_log_poll) {
dm_mgr_upstream_thing_log_post(0, NULL, 0, 1);
}
switch_status = 0;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_LOG_REPORT_STOP, "log report stop");
return remove_log_poll();
}
void parse_switch_info(_IN_ char *payload, _IN_ int payload_len)
{
lite_cjson_t lite, lite_sample_count, lite_sample_interval, lite_sample_target;
const char *c1 = "Count";
const char *c2 = "Interval";
const char *c3 = "ProfileTarget";
char *sample_target;
int sample_target_len;
const char *target = "propSet";
int ret = -1;
if (payload == NULL || payload_len <= 0) {
return;
}
dm_utils_json_parse(payload, payload_len, cJSON_Object, &lite);
ret = lite_cjson_object_item(&lite, c1, strlen(c1), &lite_sample_count);
if (ret < SUCCESS_RETURN) {
return;
}
ret = lite_cjson_object_item(&lite, c2, strlen(c2), &lite_sample_interval);
if (ret < SUCCESS_RETURN) {
return;
}
ret = lite_cjson_object_item(&lite, c3, strlen(c3), &lite_sample_target);
if (ret < SUCCESS_RETURN) {
return;
}
sample_count = lite_sample_count.value_int;
sample_interval = lite_sample_interval.value_int;
sample_target = lite_sample_target.value;
sample_target_len = lite_sample_target.value_length;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_LOG_REPORT_SWITCH, "switch count is %d, interval is %d, target is %.*s\n",
sample_count, sample_interval, sample_target_len, sample_target);
/* if the target is not property set, return */
if (0 != strncmp(sample_target, target, sample_target_len)) {
return;
}
if (sample_interval <= 0) {
return;
}
msg_num = 0;
/* when it switch off, force upload the remaining log */
if (0 == sample_count) {
ret = stop_sample();
} else {
switch_status = 1;
ret = create_log_poll();
}
}
REPORT_STATE g_report_status = READY;
void send_permance_info(char *input, int input_len, char *comments, int report_format)
{
#define LOCAL_POST_LEN (150)
char data[LOCAL_POST_LEN] = {0};
const char *format = NULL;
if (0 == switch_status) {
return;
}
switch (report_format) {
case 0:
if (NULL == input || input_len <= 0) {
return;
}
format = THING_LOG_POST_PARAMS_HEAD;
HAL_Snprintf(data, sizeof(data), format, input_len, input,
comments, (long)HAL_UptimeMs());
break;
case 1:
format = THING_LOG_POST_PARAMS_BODY;
HAL_Snprintf(data, sizeof(data), format,
comments, (long)HAL_UptimeMs());
break;
case 2:
format = THING_LOG_POST_PARAMS_END;
HAL_Snprintf(data, sizeof(data), format,
comments, (long)HAL_UptimeMs());
g_report_status = DONE;
break;
default:
return;
}
iotx_dm_log_post(0, data, strlen((const char *)data));
if (2 == report_format) {
g_report_status = READY;
}
}
void get_msgid(char *payload, int is_cloud)
{
const char *interest = "\"method\":\"thing.service.property.set";
char *found;
dm_msg_request_payload_t request;
if (0 == switch_status || NULL == payload) {
return;
}
found = strstr(payload, interest);
if (NULL == found) {
return;
}
found = strstr(payload, "{");
if (NULL == found) {
return;
}
msg_num++;
parse_msg_id(found, strlen(found), &request);
if (RUNNING == g_report_status) {
return;
}
if (sample_count <= msg_num) {
stop_sample();
return;
}
/* if it does not meet the sample conditions, do NOT take sample */
if (SUCCESS_RETURN != set_target_msg(request.id.value, request.id.value_length)) {
return;
}
g_report_status = RUNNING;
if (1 == is_cloud) {
send_permance_info(request.id.value, request.id.value_length, "1_cloud", 0);
} else if (0 == is_cloud) {
send_permance_info(request.id.value, request.id.value_length, "1_alcs", 0);
}
}
#endif
File diff suppressed because it is too large Load Diff
+145
View File
@@ -0,0 +1,145 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_MANAGER_H_
#define _DM_MANAGER_H_
#include "iotx_dm_internal.h"
typedef struct {
int devid;
int dev_type;
#if defined(DEPRECATED_LINKKIT)
dm_shw_t *dev_shadow;
iotx_dm_tsl_source_t tsl_source;
#endif
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_dm_dev_avail_t status;
iotx_dm_dev_status_t dev_status;
struct list_head linked_list;
} dm_mgr_dev_node_t;
typedef struct {
void *mutex;
int global_devid;
struct list_head dev_list;
} dm_mgr_ctx;
int dm_mgr_init(void);
int dm_mgr_deinit(void);
int dm_mgr_device_query(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1], _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ int *devid);
int dm_mgr_device_create(_IN_ int dev_type, _IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char product_secret[IOTX_PRODUCT_SECRET_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1], _OU_ int *devid);
int dm_mgr_device_destroy(_IN_ int devid);
int dm_mgr_device_number(void);
int dm_mgr_get_devid_by_index(_IN_ int index, _OU_ int *devid);
int dm_mgr_get_next_devid(_IN_ int devid, _OU_ int *devid_next);
int dm_mgr_search_device_by_devid(_IN_ int devid, _OU_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_OU_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
int dm_mgr_search_device_by_pkdn(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ int *devid);
int dm_mgr_search_device_node_by_devid(_IN_ int devid, _OU_ void **node);
int dm_mgr_get_dev_type(_IN_ int devid, _OU_ int *dev_type);
int dm_mgr_set_dev_enable(_IN_ int devid);
int dm_mgr_set_dev_disable(_IN_ int devid);
int dm_mgr_get_dev_avail(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ iotx_dm_dev_avail_t *status);
int dm_mgr_set_dev_status(_IN_ int devid, _IN_ iotx_dm_dev_status_t status);
int dm_mgr_get_dev_status(_IN_ int devid, _OU_ iotx_dm_dev_status_t *status);
int dm_mgr_set_device_secret(_IN_ int devid, _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
int dm_mgr_dev_initialized(int devid);
int dm_mgr_upstream_thing_property_desired_get(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int dm_mgr_upstream_thing_property_desired_delete(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
#ifdef DEVICE_MODEL_GATEWAY
int dm_mgr_upstream_thing_sub_register(_IN_ int devid);
int dm_mgr_upstream_thing_proxy_product_register(_IN_ int devid);
int dm_mgr_upstream_thing_sub_unregister(_IN_ int devid);
int dm_mgr_upstream_thing_topo_add(_IN_ int devid);
int dm_mgr_upstream_thing_topo_delete(_IN_ int devid);
int dm_mgr_upstream_thing_topo_get(void);
int dm_mgr_upstream_thing_list_found(_IN_ int devid);
int dm_mgr_upstream_combine_login(_IN_ int devid);
int dm_mgr_upstream_combine_logout(_IN_ int devid);
#endif
int dm_mgr_upstream_thing_model_up_raw(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
#ifdef LOG_REPORT_TO_CLOUD
int dm_mgr_upstream_thing_log_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len, int force_update);
#endif
#ifdef DEVICE_HISTORY_POST
int dm_mgr_upstream_thing_history_post(int devid, char *payload, int payload_len);
#endif /* #ifdef DEVICE_HISTORY_POST */
int dm_mgr_upstream_thing_event_post(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *method,
_IN_ char *payload, _IN_ int payload_len);
int dm_mgr_upstream_thing_deviceinfo_update(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int dm_mgr_upstream_thing_deviceinfo_delete(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
int dm_mgr_upstream_thing_dsltemplate_get(_IN_ int devid);
int dm_mgr_upstream_thing_dynamictsl_get(_IN_ int devid);
int dm_mgr_upstream_ntp_request(void);
int dm_mgr_upstream_thing_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 dm_mgr_upstream_thing_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 dm_mgr_upstream_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);
#ifdef DEVICE_MODEL_SUBDEV_OTA
int dm_mgr_upstream_thing_firmware_version_update(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
#endif
#endif
#ifdef DEPRECATED_LINKKIT
int dm_mgr_deprecated_set_tsl_source(_IN_ int devid, _IN_ iotx_dm_tsl_source_t tsl_source);
int dm_mgr_deprecated_get_tsl_source(_IN_ int devid, _IN_ iotx_dm_tsl_source_t *tsl_source);
int dm_mgr_deprecated_search_devid_by_device_node(_IN_ void *node, _OU_ int *devid);
int dm_mgr_deprecated_set_tsl(int devid, iotx_dm_tsl_type_t tsl_type, const char *tsl, int tsl_len);
int dm_mgr_deprecated_get_property_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int dm_mgr_deprecated_get_service_input_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int dm_mgr_deprecated_get_service_output_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int dm_mgr_deprecated_get_event_output_data(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int dm_mgr_deprecated_get_data_type(_IN_ void *property, _OU_ dm_shw_data_type_e *type);
int dm_mgr_deprecated_get_property_number(_IN_ int devid, _OU_ int *number);
int dm_mgr_deprecated_get_service_number(_IN_ int devid, _OU_ int *number);
int dm_mgr_deprecated_get_event_number(_IN_ int devid, _OU_ int *number);
int dm_mgr_deprecated_get_property_by_index(_IN_ int devid, _IN_ int index, _OU_ void **property);
int dm_mgr_deprecated_get_service_by_index(_IN_ int devid, _IN_ int index, _OU_ void **service);
int dm_mgr_deprecated_get_event_by_index(_IN_ int devid, _IN_ int index, _OU_ void **event);
int dm_mgr_deprecated_get_service_by_identifier(_IN_ int devid, _IN_ char *identifier, _OU_ void **service);
int dm_mgr_deprecated_get_event_by_identifier(_IN_ int devid, _IN_ char *identifier, _OU_ void **event);
int dm_mgr_deprecated_get_property_identifier(_IN_ void *property, _OU_ char **identifier);
int dm_mgr_deprecated_get_service_method(_IN_ void *service, _OU_ char **method);
int dm_mgr_deprecated_get_event_method(_IN_ void *event, _OU_ char **method);
int dm_mgr_deprecated_set_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int dm_mgr_deprecated_get_property_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int dm_mgr_deprecated_set_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int dm_mgr_deprecated_get_event_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int dm_mgr_deprecated_set_service_input_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int dm_mgr_deprecated_get_service_input_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int dm_mgr_deprecated_set_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
int dm_mgr_deprecated_get_service_output_value(_IN_ int devid, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int dm_mgr_deprecated_assemble_property(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
int dm_mgr_deprecated_assemble_event_output(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
int dm_mgr_deprecated_assemble_service_output(_IN_ int devid, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
int dm_mgr_deprecated_upstream_thing_service_response(_IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
_IN_ char *identifier, _IN_ int identifier_len, _IN_ char *payload, _IN_ int payload_len);
#endif
#endif
File diff suppressed because it is too large Load Diff
+204
View File
@@ -0,0 +1,204 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_MESSAGE_H_
#define _DM_MESSAGE_H_
#include "iotx_dm_internal.h"
#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_FAILURES "failures"
#define DM_MSG_KEY_SUCCESSES "successes"
#define DM_MSG_KEY_ERRORDETAIL "errorDetail"
#define DM_MSG_KEY_CODE "code"
#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 {
DM_MSG_DEST_CLOUD = 0x01,
DM_MSG_DEST_LOCAL = 0x02,
DM_MSG_DEST_ALL = 0x03
} dm_msg_dest_type_t;
typedef struct {
const char *uri;
unsigned char *payload;
unsigned int payload_len;
void *context;
} dm_msg_source_t;
typedef struct {
const char *uri_name;
} dm_msg_dest_t;
typedef struct {
lite_cjson_t id;
lite_cjson_t version;
lite_cjson_t method;
lite_cjson_t params;
} dm_msg_request_payload_t;
typedef struct {
lite_cjson_t id;
lite_cjson_t code;
lite_cjson_t data;
lite_cjson_t message;
} dm_msg_response_payload_t;
typedef struct {
int msgid;
int devid;
const char *service_prefix;
const char *service_name;
char product_key[IOTX_PRODUCT_KEY_LEN + 1];
char device_name[IOTX_DEVICE_NAME_LEN + 1];
char *params;
int params_len;
char *method;
iotx_cm_data_handle_cb callback;
} dm_msg_request_t;
typedef struct {
const char *service_prefix;
const char *service_name;
char product_key[IOTX_PRODUCT_KEY_LEN + 1];
char device_name[IOTX_DEVICE_NAME_LEN + 1];
iotx_dm_error_code_t code;
} dm_msg_response_t;
typedef struct {
int id;
} dm_msg_ctx_t;
int dm_msg_init(void);
int dm_msg_deinit(void);
int _dm_msg_send_to_user(iotx_dm_event_types_t type, char *message);
int dm_msg_send_msg_timeout_to_user(int msg_id, int devid, iotx_dm_event_types_t type);
int dm_msg_uri_parse_pkdn(_IN_ char *uri, _IN_ int uri_len, _IN_ int start_deli, _IN_ int end_deli,
_OU_ char product_key[IOTX_PRODUCT_KEY_LEN + 1], _OU_ char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_msg_request_parse(_IN_ char *payload, _IN_ int payload_len, _OU_ dm_msg_request_payload_t *request);
int dm_msg_response_parse(_IN_ char *payload, _IN_ int payload_len, _OU_ dm_msg_response_payload_t *response);
int dm_msg_request(dm_msg_dest_type_t type, _IN_ dm_msg_request_t *request);
int dm_msg_response(dm_msg_dest_type_t type, _IN_ dm_msg_request_payload_t *request, _IN_ dm_msg_response_t *response,
_IN_ char *data, _IN_ int data_len, _IN_ void *user_data);
int dm_msg_thing_model_down_raw(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_IN_ char *payload, _IN_ int payload_len);
int dm_msg_thing_model_up_raw_reply(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], char *payload, int payload_len);
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
int dm_msg_property_set(int devid, dm_msg_request_payload_t *request);
#ifndef DEPRECATED_LINKKIT
int dm_msg_property_get(_IN_ int devid, _IN_ dm_msg_request_payload_t *request, _IN_ void *ctx);
#else
int dm_msg_property_get(_IN_ int devid, _IN_ dm_msg_request_payload_t *request, _IN_ char **payload,
_IN_ int *payload_len);
#endif
int dm_msg_thing_service_request(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
char *identifier, int identifier_len, dm_msg_request_payload_t *request, _IN_ void *ctx);
int dm_msg_rrpc_request(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
char *messageid, int messageid_len, dm_msg_request_payload_t *request);
int dm_msg_thing_event_property_post_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_event_post_reply(_IN_ char *identifier, _IN_ int identifier_len,
_IN_ dm_msg_response_payload_t *response);
int dm_msg_thing_deviceinfo_update_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_property_desired_get_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_property_desired_delete_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_deviceinfo_delete_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_dsltemplate_get_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_dynamictsl_get_reply(dm_msg_response_payload_t *response);
int dm_msg_ntp_response(char *payload, int payload_len);
int dm_msg_ext_error_response(char *payload, int payload_len);
#endif
#ifdef DEVICE_MODEL_GATEWAY
int dm_msg_topo_add_notify(_IN_ char *payload, _IN_ int payload_len);
int dm_msg_thing_disable(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_msg_thing_enable(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_msg_thing_delete(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_msg_thing_gateway_permit(_IN_ char *payload, _IN_ int payload_len);
int dm_msg_thing_sub_register_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_proxy_product_register_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_sub_unregister_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_topo_add_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_topo_delete_reply(dm_msg_response_payload_t *response);
int dm_msg_topo_get_reply(dm_msg_response_payload_t *response);
int dm_msg_thing_list_found_reply(dm_msg_response_payload_t *response);
int dm_msg_combine_login_reply(dm_msg_response_payload_t *response);
int dm_msg_combine_logout_reply(dm_msg_response_payload_t *response);
#endif
#ifdef ALCS_ENABLED
int dm_msg_dev_core_service_dev(char **payload, int *payload_len);
#endif
int dm_msg_cloud_connected(void);
int dm_msg_cloud_disconnect(void);
int dm_msg_cloud_reconnect(void);
#if 0
int dm_msg_found_device(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_msg_remove_device(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_msg_unregister_result(_IN_ char *uri, _IN_ int result);
int dm_msg_send_result(_IN_ char *uri, _IN_ int result);
int dm_msg_add_service_result(_IN_ char *uri, _IN_ int result);
int dm_msg_remove_service_result(_IN_ char *uri, _IN_ int result);
#endif
int dm_msg_register_result(_IN_ char *uri, _IN_ int result);
#ifdef DEVICE_MODEL_GATEWAY
int dm_msg_thing_sub_register(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ dm_msg_request_t *request);
int dm_msg_thing_proxy_product_register(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char product_secret[IOTX_PRODUCT_SECRET_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ dm_msg_request_t *request);
int dm_msg_thing_sub_unregister(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ dm_msg_request_t *request);
int dm_msg_thing_topo_add(_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_ dm_msg_request_t *request);
int dm_msg_thing_topo_delete(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ dm_msg_request_t *request);
int dm_msg_thing_topo_get(_OU_ dm_msg_request_t *request);
int dm_msg_thing_list_found(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ dm_msg_request_t *request);
int dm_msg_combine_login(_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_ dm_msg_request_t *request);
int dm_msg_combine_logout(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
_OU_ dm_msg_request_t *request);
#endif
int dm_msg_thing_model_user_sub(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN],
_IN_ char *payload, _IN_ int payload_len);
#endif
+177
View File
@@ -0,0 +1,177 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
#if !defined(DM_MESSAGE_CACHE_DISABLED)
dm_msg_cache_ctx_t g_dm_msg_cache_ctx;
dm_msg_cache_ctx_t *_dm_msg_cache_get_ctx(void)
{
return &g_dm_msg_cache_ctx;
}
static void _dm_msg_cache_mutex_lock(void)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
if (ctx->mutex) {
HAL_MutexLock(ctx->mutex);
}
}
static void _dm_msg_cache_mutex_unlock(void)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
}
int dm_msg_cache_init(void)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
memset(ctx, 0, sizeof(dm_msg_cache_ctx_t));
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
return STATE_SYS_DEPEND_MUTEX_CREATE;
}
/* Init Message Cache List */
INIT_LIST_HEAD(&ctx->dmc_list);
return SUCCESS_RETURN;
}
int dm_msg_cache_deinit(void)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
dm_msg_cache_node_t *node = NULL;
dm_msg_cache_node_t *next = NULL;
_dm_msg_cache_mutex_lock();
list_for_each_entry_safe(node, next, &ctx->dmc_list, linked_list, dm_msg_cache_node_t) {
list_del(&node->linked_list);
if (node->data) {
DM_free(node->data);
}
DM_free(node);
_dm_msg_cache_mutex_unlock();
}
_dm_msg_cache_mutex_unlock();
if (ctx->mutex) {
HAL_MutexDestroy(ctx->mutex);
}
return SUCCESS_RETURN;
}
int dm_msg_cache_insert(int msgid, int devid, iotx_dm_event_types_t type, char *data)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
dm_msg_cache_node_t *node = NULL;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_CTX_LIST_INSERT, "context list size: %d", ctx->dmc_list_size);
if (ctx->dmc_list_size >= CONFIG_MSGCACHE_QUEUE_MAXLEN) {
return STATE_DEV_MODEL_CTX_LIST_FULL;
}
node = DM_malloc(sizeof(dm_msg_cache_node_t));
if (node == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(node, 0, sizeof(dm_msg_cache_node_t));
node->msgid = msgid;
node->devid = devid;
node->response_type = type;
node->data = data;
node->ctime = HAL_UptimeMs();
INIT_LIST_HEAD(&node->linked_list);
_dm_msg_cache_mutex_lock();
list_add_tail(&node->linked_list, &ctx->dmc_list);
ctx->dmc_list_size++;
_dm_msg_cache_mutex_unlock();
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_CTX_LIST_INSERT, "context elem inserted, msgid: %d", msgid);
return SUCCESS_RETURN;
}
int dm_msg_cache_search(_IN_ int msgid, _OU_ dm_msg_cache_node_t **node)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
dm_msg_cache_node_t *search_node = NULL;
if (msgid < 0 || node == NULL || *node != NULL) {
return STATE_USER_INPUT_INVALID;
}
_dm_msg_cache_mutex_lock();
list_for_each_entry(search_node, &ctx->dmc_list, linked_list, dm_msg_cache_node_t) {
if (search_node->msgid == msgid) {
*node = search_node;
_dm_msg_cache_mutex_unlock();
return SUCCESS_RETURN;
}
}
_dm_msg_cache_mutex_unlock();
return STATE_DEV_MODEL_CTX_LIST_EMPTY;
}
int dm_msg_cache_remove(int msgid)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
dm_msg_cache_node_t *node = NULL;
dm_msg_cache_node_t *next = NULL;
_dm_msg_cache_mutex_lock();
list_for_each_entry_safe(node, next, &ctx->dmc_list, linked_list, dm_msg_cache_node_t) {
if (node->msgid == msgid) {
list_del(&node->linked_list);
if (node->data) {
DM_free(node->data);
}
ctx->dmc_list_size--;
DM_free(node);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_CTX_LIST_REMOVE, "context elem remove, msgid: %d", msgid);
_dm_msg_cache_mutex_unlock();
return SUCCESS_RETURN;
}
}
_dm_msg_cache_mutex_unlock();
return STATE_DEV_MODEL_CTX_LIST_EMPTY;
}
void dm_msg_cache_tick(void)
{
dm_msg_cache_ctx_t *ctx = _dm_msg_cache_get_ctx();
dm_msg_cache_node_t *node = NULL;
dm_msg_cache_node_t *next = NULL;
uint64_t current_time = HAL_UptimeMs();
_dm_msg_cache_mutex_lock();
list_for_each_entry_safe(node, next, &ctx->dmc_list, linked_list, dm_msg_cache_node_t) {
if (current_time < node->ctime) {
node->ctime = current_time;
}
if (current_time - node->ctime >= DM_MSG_CACHE_TIMEOUT_MS_DEFAULT) {
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_CTX_LIST_FADEOUT, "context elem timeout, msgid: %d", node->msgid);
/* Send Timeout Message To User */
dm_msg_send_msg_timeout_to_user(node->msgid, node->devid, node->response_type);
list_del(&node->linked_list);
if (node->data) {
DM_free(node->data);
}
DM_free(node);
}
}
_dm_msg_cache_mutex_unlock();
}
#endif
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#if !defined(DM_MESSAGE_CACHE_DISABLED)
#ifndef _DM_MESSAGE_CACHE_H_
#define _DM_MESSAGE_CACHE_H_
#include "iotx_dm_internal.h"
#define DM_MSG_CACHE_TIMEOUT_MS_DEFAULT (10000)
typedef struct {
int msgid;
int devid;
iotx_dm_event_types_t response_type;
char *data;
uint64_t ctime;
struct list_head linked_list;
} dm_msg_cache_node_t;
typedef struct {
void *mutex;
int dmc_list_size;
struct list_head dmc_list;
} dm_msg_cache_ctx_t;
int dm_msg_cache_init(void);
int dm_msg_cache_deinit(void);
int dm_msg_cache_insert(int msg_id, int devid, iotx_dm_event_types_t type, char *data);
int dm_msg_cache_search(_IN_ int msg_id, _OU_ dm_msg_cache_node_t **node);
int dm_msg_cache_remove(int msg_id);
void dm_msg_cache_tick(void);
#endif
#endif
File diff suppressed because it is too large Load Diff
+164
View File
@@ -0,0 +1,164 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_MSG_PROCESS_H_
#define _DM_MSG_PROCESS_H_
#define DM_URI_SERVICE_DELIMITER '/'
extern const char DM_URI_SYS_PREFIX[] DM_READ_ONLY;
extern const char DM_URI_EXT_SESSION_PREFIX[] DM_READ_ONLY;
extern const char DM_URI_EXT_NTP_PREFIX[] DM_READ_ONLY;
extern const char DM_URI_EXT_ERROR_PREFIX[] DM_READ_ONLY;
extern const char DM_URI_REPLY_SUFFIX[] DM_READ_ONLY;
extern const char DM_URI_OTA_DEVICE_INFORM[] DM_READ_ONLY;
extern const char DM_URI_THING_PROPERTY_DESIRED_GET[] DM_READ_ONLY;
extern const char DM_URI_THING_PROPERTY_DESIRED_DELETE[] DM_READ_ONLY;
extern const char DM_URI_THING_PROPERTY_DESIRED_GET_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_PROPERTY_DESIRED_DELETE_REPLY[] DM_READ_ONLY;
/* From Cloud To Local Request And Response*/
extern const char DM_URI_THING_MODEL_DOWN_RAW[] DM_READ_ONLY;
extern const char DM_URI_THING_MODEL_DOWN_RAW_REPLY[] DM_READ_ONLY;
/* From Local To Cloud Request And Response*/
extern const char DM_URI_THING_MODEL_UP_RAW[] DM_READ_ONLY;
extern const char DM_URI_THING_MODEL_UP_RAW_REPLY[] DM_READ_ONLY;
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
extern const char DM_URI_RRPC_REQUEST_WILDCARD[] DM_READ_ONLY;
/* From Cloud To Local Request And Response*/
extern const char DM_URI_THING_SERVICE_PROPERTY_SET[] DM_READ_ONLY;
extern const char DM_URI_THING_SERVICE_PROPERTY_SET_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_SERVICE_PROPERTY_GET[] DM_READ_ONLY;
extern const char DM_URI_THING_SERVICE_PROPERTY_GET_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_SERVICE_REQUEST_WILDCARD[] DM_READ_ONLY;
extern const char DM_URI_THING_SERVICE_REQUEST_WILDCARD2[] DM_READ_ONLY;
extern const char DM_URI_THING_SERVICE_REQUEST[] DM_READ_ONLY;
extern const char DM_URI_THING_SERVICE_RESPONSE[] DM_READ_ONLY;
/* From Local To Cloud Request And Response*/
extern const char DM_URI_THING_EVENT_PROPERTY_POST[] DM_READ_ONLY;
extern const char DM_URI_THING_EVENT_PROPERTY_POST_REPLY[] DM_READ_ONLY;
#ifdef DEVICE_HISTORY_POST
extern const char DM_URI_THING_EVENT_HISTORY_POST[] DM_READ_ONLY;
extern const char DM_URI_THING_EVENT_HISTORY_POST_REPLY[] DM_READ_ONLY;
#endif /* #ifdef DEVICE_HISTORY_POST */
#ifdef LOG_REPORT_TO_CLOUD
extern const char DM_URI_THING_LOG_POST[] DM_READ_ONLY;
#endif
extern const char DM_URI_THING_EVENT_POST[] DM_READ_ONLY;
extern const char DM_URI_THING_EVENT_POST_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_EVENT_POST_REPLY_WILDCARD[] DM_READ_ONLY;
extern const char DM_URI_THING_DEVICEINFO_UPDATE[] DM_READ_ONLY;
extern const char DM_URI_THING_DEVICEINFO_UPDATE_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_DEVICEINFO_DELETE[] DM_READ_ONLY;
extern const char DM_URI_THING_DEVICEINFO_DELETE_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_DSLTEMPLATE_GET[] DM_READ_ONLY;
extern const char DM_URI_THING_DSLTEMPLATE_GET_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_DYNAMICTSL_GET[] DM_READ_ONLY;
extern const char DM_URI_THING_DYNAMICTSL_GET_REPLY[] DM_READ_ONLY;
extern const char DM_URI_NTP_REQUEST[] DM_READ_ONLY;
extern const char DM_URI_NTP_RESPONSE[] DM_READ_ONLY;
#endif
extern const char DM_URI_DEV_CORE_SERVICE_DEV[] DM_READ_ONLY;
#ifdef DEVICE_MODEL_GATEWAY
/* From Cloud To Local Request And Response*/
extern const char DM_URI_THING_TOPO_ADD_NOTIFY[] DM_READ_ONLY;
extern const char DM_URI_THING_TOPO_ADD_NOTIFY_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_DISABLE[] DM_READ_ONLY;
extern const char DM_URI_THING_DISABLE_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_ENABLE[] DM_READ_ONLY;
extern const char DM_URI_THING_ENABLE_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_DELETE[] DM_READ_ONLY;
extern const char DM_URI_THING_DELETE_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_GATEWAY_PERMIT[] DM_READ_ONLY;
extern const char DM_URI_THING_GATEWAY_PERMIT_REPLY[] DM_READ_ONLY;
/* From Local To Cloud Request And Response*/
extern const char DM_URI_THING_SUB_REGISTER[] DM_READ_ONLY;
extern const char DM_URI_THING_SUB_REGISTER_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_PROXY_PRODUCT_REGISTER[] DM_READ_ONLY;
extern const char DM_URI_THING_PROXY_PRODUCT_REGISTER_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_SUB_REGISTER_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_SUB_UNREGISTER[] DM_READ_ONLY;
extern const char DM_URI_THING_SUB_UNREGISTER_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_TOPO_ADD[] DM_READ_ONLY;
extern const char DM_URI_THING_TOPO_ADD_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_TOPO_DELETE[] DM_READ_ONLY;
extern const char DM_URI_THING_TOPO_DELETE_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_TOPO_GET[] DM_READ_ONLY;
extern const char DM_URI_THING_TOPO_GET_REPLY[] DM_READ_ONLY;
extern const char DM_URI_THING_LIST_FOUND[] DM_READ_ONLY;
extern const char DM_URI_THING_LIST_FOUND_REPLY[] DM_READ_ONLY;
extern const char DM_URI_COMBINE_LOGIN[] DM_READ_ONLY;
extern const char DM_URI_COMBINE_LOGIN_REPLY[] DM_READ_ONLY;
extern const char DM_URI_COMBINE_LOGOUT[] DM_READ_ONLY;
extern const char DM_URI_COMBINE_LOGOUT_REPLY[] DM_READ_ONLY;
#endif
int dm_disp_uri_prefix_split(_IN_ const char *prefix, _IN_ char *uri, _IN_ int uri_len, _OU_ int *start, _OU_ int *end);
int dm_disp_uri_pkdn_split(_IN_ char *uri, _IN_ int uri_len, _OU_ int *start, _OU_ int *end);
int dm_disp_uri_service_specific_split(_IN_ char *uri, _IN_ int uri_len, _OU_ int *start, _OU_ int *end);
int dm_disp_uri_rrpc_request_split(_IN_ char *uri, _IN_ int uri_len, _OU_ int *start, _OU_ int *end);
int dm_disp_uri_event_specific_split(_IN_ char *uri, _IN_ int uri_len, _OU_ int *start, _OU_ int *end);
int dm_msg_proc_thing_model_down_raw(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_model_up_raw_reply(_IN_ dm_msg_source_t *source);
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
int dm_msg_proc_thing_service_property_set(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response);
int dm_msg_proc_thing_service_property_get(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response,
_OU_ unsigned char **data, int *data_len);
int dm_msg_proc_thing_property_desired_get_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_property_desired_delete_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_service_property_post(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response);
int dm_msg_proc_thing_service_request(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_event_post_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_deviceinfo_update_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_deviceinfo_delete_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_dynamictsl_get_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_rrpc_request(_IN_ dm_msg_source_t *source);
int dm_disp_ntp_response(_IN_ dm_msg_source_t *source);
int dm_disp_ext_error_response(_IN_ dm_msg_source_t *source);
#endif
#ifdef DEVICE_MODEL_GATEWAY
int dm_msg_proc_thing_topo_add_notify(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response);
int dm_msg_proc_thing_disable(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response);
int dm_msg_proc_thing_enable(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response);
int dm_msg_proc_thing_delete(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response);
int dm_msg_proc_thing_gateway_permit(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response);
int dm_msg_proc_thing_sub_register_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_proxy_product_register_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_sub_unregister_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_topo_add_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_topo_delete_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_topo_get_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_thing_list_found_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_combine_login_reply(_IN_ dm_msg_source_t *source);
int dm_msg_proc_combine_logout_reply(_IN_ dm_msg_source_t *source);
#endif
#ifdef ALCS_ENABLED
int dm_msg_proc_thing_dev_core_service_dev(_IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
_OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response,
_OU_ unsigned char **data, int *data_len);
#endif
int dm_msg_proc_thing_model_user_sub(_IN_ dm_msg_source_t *source);
#endif
+118
View File
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
#ifdef DEVICE_MODEL_ENABLED
static dm_opt_ctx g_dm_opt = {
0, 0, 1, 1, 1, 60 * 1000, 0
};
int dm_opt_set(dm_opt_t opt, void *data)
{
int res = SUCCESS_RETURN;
if (data == NULL) {
return STATE_USER_INPUT_INVALID;
}
switch (opt) {
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
case DM_OPT_DOWNSTREAM_PROPERTY_POST_REPLY: {
int opt = *(int *)(data);
g_dm_opt.prop_post_reply_opt = opt;
}
break;
case DM_OPT_DOWNSTREAM_EVENT_POST_REPLY: {
int opt = *(int *)(data);
g_dm_opt.event_post_reply_opt = opt;
}
break;
case DM_OPT_UPSTREAM_PROPERTY_SET_REPLY: {
int opt = *(int *)(data);
g_dm_opt.prop_set_reply_opt = opt;
}
break;
#endif
#ifdef DEVICE_MODEL_SHADOW
case DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_GET_REPLY: {
int opt = *(int *)(data);
g_dm_opt.prop_desired_get_reply_opt = opt;
}
break;
case DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_DELETE_REPLY: {
int opt = *(int *)(data);
g_dm_opt.prop_desired_delete_reply_opt = opt;
}
break;
#endif
case DM_OPT_FOTA_RETRY_TIMEOUT_MS: {
int opt = *(int *)(data);
g_dm_opt.fota_retry_timeout_ms = opt;
}
break;
case DM_OPT_PROXY_PRODUCT_REGISTER: {
int opt = *(int *)(data);
g_dm_opt.proxy_product_register = opt;
}
break;
default: {
res = STATE_USER_INPUT_INVALID;
}
break;
}
return res;
}
int dm_opt_get(dm_opt_t opt, void *data)
{
int res = SUCCESS_RETURN;
if (data == NULL) {
return STATE_USER_INPUT_INVALID;
}
switch (opt) {
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
case DM_OPT_DOWNSTREAM_PROPERTY_POST_REPLY: {
*(int *)(data) = g_dm_opt.prop_post_reply_opt;
}
break;
case DM_OPT_DOWNSTREAM_EVENT_POST_REPLY: {
*(int *)(data) = g_dm_opt.event_post_reply_opt;
}
break;
case DM_OPT_UPSTREAM_PROPERTY_SET_REPLY: {
*(int *)(data) = g_dm_opt.prop_set_reply_opt;
}
break;
#endif
#ifdef DEVICE_MODEL_SHADOW
case DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_DELETE_REPLY: {
*(int *)(data) = g_dm_opt.prop_desired_delete_reply_opt;
}
break;
case DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_GET_REPLY: {
*(int *)(data) = g_dm_opt.prop_desired_get_reply_opt;
}
break;
#endif
case DM_OPT_FOTA_RETRY_TIMEOUT_MS: {
*(int *)(data) = g_dm_opt.fota_retry_timeout_ms;
}
break;
case DM_OPT_PROXY_PRODUCT_REGISTER: {
*(int *)(data) = g_dm_opt.proxy_product_register;
}
break;
default: {
res = STATE_DEV_MODEL_INVALID_DM_OPTION;
}
break;
}
return res;
}
#endif
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifdef DEVICE_MODEL_ENABLED
#ifndef _DM_OPT_H
#define _DM_OPT_H
typedef enum {
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
DM_OPT_DOWNSTREAM_PROPERTY_POST_REPLY,
DM_OPT_DOWNSTREAM_EVENT_POST_REPLY,
DM_OPT_UPSTREAM_PROPERTY_SET_REPLY,
#endif
DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_DELETE_REPLY,
DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_GET_REPLY,
DM_OPT_FOTA_RETRY_TIMEOUT_MS,
DM_OPT_PROXY_PRODUCT_REGISTER
} dm_opt_t;
typedef struct {
int prop_post_reply_opt;
int event_post_reply_opt;
int prop_set_reply_opt;
int prop_desired_get_reply_opt;
int prop_desired_delete_reply_opt;
int fota_retry_timeout_ms;
int proxy_product_register;
} dm_opt_ctx;
int dm_opt_set(dm_opt_t opt, void *data);
int dm_opt_get(dm_opt_t opt, void *data);
#endif
#endif
+120
View File
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
#if defined(OTA_ENABLED) && !defined(BUILD_AOS)
static dm_ota_ctx_t g_dm_ota_ctx;
static dm_ota_ctx_t *_dm_ota_get_ctx(void)
{
return &g_dm_ota_ctx;
}
static int _dm_ota_clean_state(void *context)
{
dm_cota_ctx_t *cota = dm_cota_get_ctx();
dm_fota_ctx_t *fota = dm_fota_get_ctx();
if (cota != NULL) {
cota->is_report_new_config = 0;
}
if (fota != NULL) {
fota->is_report_new_config = 0;
}
return 0;
}
int dm_ota_init(void)
{
dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
memset(ctx, 0, sizeof(dm_ota_ctx_t));
IOT_Ioctl(IOTX_IOCTL_GET_PRODUCT_KEY, ctx->product_key);
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_NAME, ctx->device_name);
return SUCCESS_RETURN;
}
int dm_ota_sub(void)
{
dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
void *handle = NULL;
/* Init OTA Handle */
handle = IOT_OTA_Init(ctx->product_key, ctx->device_name, NULL);
if (handle == NULL) {
return STATE_DEV_MODEL_OTA_INIT_FAILED;
}
IOT_OTA_SetOnPushedCallback(handle, _dm_ota_clean_state);
ctx->ota_handle = handle;
return SUCCESS_RETURN;
}
int dm_ota_deinit(void)
{
dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
if (ctx->ota_handle) {
IOT_OTA_Deinit(ctx->ota_handle);
ctx->ota_handle = NULL;
}
return SUCCESS_RETURN;
}
#ifdef DEVICE_MODEL_GATEWAY
#ifdef DEVICE_MODEL_SUBDEV_OTA
int dm_ota_switch_device(int devid)
{
char pk[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char dn[IOTX_DEVICE_NAME_LEN + 1] = {0};
char ds[IOTX_DEVICE_SECRET_LEN + 1] = {0};
void *ota_handle = NULL;
int res = -1;
dm_ota_ctx_t *ctx = NULL;
res = dm_mgr_search_device_by_devid(devid, pk, dn, ds);
if (SUCCESS_RETURN != res) {
return res;
}
res = dm_ota_get_ota_handle(&ota_handle);
if (res != SUCCESS_RETURN) {
return res;
}
/* if currently a device is doing OTA, do not interrupt */
if (IOT_OTA_IsFetching(ota_handle)) {
return STATE_DEV_MODEL_OTA_STILL_IN_PROGRESS;
}
dm_ota_deinit();
ctx = _dm_ota_get_ctx();
memset(ctx, 0, sizeof(dm_ota_ctx_t));
memcpy(ctx->product_key, pk, strlen(pk) + 1);
memcpy(ctx->device_name, dn, strlen(dn) + 1);
res = dm_ota_sub();
return res;
}
#endif
#endif
int dm_ota_get_ota_handle(void **handle)
{
dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
if (handle == NULL || *handle != NULL) {
return STATE_DEV_MODEL_INTERNAL_ERROR;
}
if (ctx->ota_handle == NULL) {
return STATE_DEV_MODEL_OTA_NOT_INITED;
}
*handle = ctx->ota_handle;
return SUCCESS_RETURN;
}
#endif
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_OTA_H_
#define _DM_OTA_H_
typedef struct {
void *ota_handle;
char product_key[IOTX_PRODUCT_KEY_LEN + 1];
char device_name[IOTX_DEVICE_NAME_LEN + 1];
} dm_ota_ctx_t;
int dm_ota_init(void);
int dm_ota_sub(void);
int dm_ota_deinit(void);
int dm_ota_get_ota_handle(void **handle);
#ifdef DEVICE_MODEL_GATEWAY
#ifdef DEVICE_MODEL_SUBDEV_OTA
int dm_ota_switch_device(int devid);
#endif
#endif
#endif
+292
View File
@@ -0,0 +1,292 @@
#include "iotx_dm_internal.h"
#ifdef ALCS_ENABLED
#ifdef LOG_REPORT_TO_CLOUD
#include "iotx_log_report.h"
#endif
static int _dm_server_malloc_context(_IN_ NetworkAddr *remote, _IN_ CoAPMessage *message,
_OU_ dm_server_alcs_context_t **context)
{
dm_server_alcs_context_t *alcs_context = NULL;
alcs_context = DM_malloc(sizeof(dm_server_alcs_context_t));
if (alcs_context == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(alcs_context, 0, sizeof(dm_server_alcs_context_t));
alcs_context->ip = DM_malloc(strlen((char *)remote->addr) + 1);
if (alcs_context->ip == NULL) {
DM_free(alcs_context);
return STATE_SYS_DEPEND_MALLOC;
}
memset(alcs_context->ip, 0, strlen((char *)remote->addr) + 1);
memcpy(alcs_context->ip, (char *)remote->addr, strlen((char *)remote->addr) + 1);
alcs_context->port = remote->port;
alcs_context->token = DM_malloc(message->header.tokenlen);
if (alcs_context->token == NULL) {
DM_free(alcs_context->ip);
DM_free(alcs_context);
return STATE_SYS_DEPEND_MALLOC;
}
memset(alcs_context->token, 0, message->header.tokenlen);
memcpy(alcs_context->token, message->token, message->header.tokenlen);
alcs_context->token_len = message->header.tokenlen;
*context = alcs_context;
return SUCCESS_RETURN;
}
void dm_server_free_context(_IN_ void *ctx)
{
dm_server_alcs_context_t *context = (dm_server_alcs_context_t *)ctx;
DM_free(context->ip);
DM_free(context->token);
DM_free(context);
}
static dm_server_uri_map_t g_dm_server_uri_map[] = {
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
{DM_URI_THING_SERVICE_PROPERTY_SET, DM_URI_SYS_PREFIX, IOTX_DM_LOCAL_AUTH, dm_server_thing_service_property_set },
{DM_URI_THING_SERVICE_PROPERTY_GET, DM_URI_SYS_PREFIX, IOTX_DM_LOCAL_AUTH, dm_server_thing_service_property_get },
{DM_URI_THING_EVENT_PROPERTY_POST, DM_URI_SYS_PREFIX, IOTX_DM_LOCAL_AUTH, dm_server_thing_service_property_post },
{DM_URI_THING_SERVICE_REQUEST_WILDCARD2, DM_URI_SYS_PREFIX, IOTX_DM_LOCAL_AUTH, dm_server_thing_service_request },
#endif
{DM_URI_DEV_CORE_SERVICE_DEV, NULL, IOTX_DM_LOCAL_NO_AUTH, dm_server_thing_dev_core_service_dev },
};
int dm_server_subscribe_all(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1])
{
int res = 0, index = 0, auth = 0;
int number = sizeof(g_dm_server_uri_map) / sizeof(dm_server_uri_map_t);
char *uri = NULL;
for (index = 0; index < number; index++) {
res = dm_utils_service_name((char *)g_dm_server_uri_map[index].uri_prefix, (char *)g_dm_server_uri_map[index].uri_name,
product_key, device_name, &uri);
if (res < SUCCESS_RETURN) {
index--;
continue;
}
auth = (g_dm_server_uri_map[index].auth_type & IOTX_DM_SERVICE_LOCAL_AUTH) ? (IOTX_DM_MESSAGE_AUTH) :
(IOTX_DM_MESSAGE_NO_AUTH);
res = dm_server_subscribe(uri, (void *)g_dm_server_uri_map[index].callback, auth);
if (res < SUCCESS_RETURN) {
index--;
DM_free(uri);
continue;
}
DM_free(uri);
}
return SUCCESS_RETURN;
}
void dm_server_alcs_event_handler(void *pcontext, void *phandle, iotx_alcs_event_msg_t *msg)
{
}
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
void dm_server_thing_service_property_set(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message)
{
int res = 0;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
dm_server_alcs_context_t *alcs_context = NULL;
res = _dm_server_malloc_context(remote, message, &alcs_context);
if (res != SUCCESS_RETURN) {
return;
}
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = paths;
source.payload = (unsigned char *)message->payload;
source.payload_len = message->payloadlen;
source.context = alcs_context;
dest.uri_name = DM_URI_THING_SERVICE_PROPERTY_SET;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_RX_LOCAL_MESSAGE, "recv property set from %s", remote->addr);
res = dm_msg_proc_thing_service_property_set(&source, &dest, &request, &response);
if (res < SUCCESS_RETURN) {
dm_server_free_context(alcs_context);
return;
}
#ifdef LOG_REPORT_TO_CLOUD
{
extern void send_permance_info(char *input, int input_len, char *comments, int report_format);
if (SUCCESS_RETURN == check_target_msg(request.id.value, request.id.value_length)) {
send_permance_info(request.id.value, request.id.value_length, "2", 1);
}
}
#endif
dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, "{}", strlen("{}"), (void *)alcs_context);
dm_server_free_context(alcs_context);
}
void dm_server_thing_service_request(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message)
{
int res = 0;
dm_msg_source_t source;
dm_server_alcs_context_t *alcs_context = NULL;
res = _dm_server_malloc_context(remote, message, &alcs_context);
if (res != SUCCESS_RETURN) {
return;
}
memset(&source, 0, sizeof(dm_msg_source_t));
source.uri = paths;
source.payload = (unsigned char *)message->payload;
source.payload_len = message->payloadlen;
source.context = alcs_context;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_RX_LOCAL_MESSAGE, "recv service request from %s", remote->addr);
if (dm_msg_proc_thing_service_request(&source) < 0) {
dm_server_free_context(alcs_context);
}
}
void dm_server_thing_service_property_get(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message)
{
int res = 0;
dm_server_alcs_context_t *alcs_context = NULL;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
unsigned char *data = NULL;
int data_len = 0;
res = _dm_server_malloc_context(remote, message, &alcs_context);
if (res != SUCCESS_RETURN) {
return;
}
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = paths;
source.payload = (unsigned char *)message->payload;
source.payload_len = message->payloadlen;
source.context = alcs_context;
dest.uri_name = DM_URI_THING_SERVICE_PROPERTY_GET;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_RX_LOCAL_MESSAGE, "recv property get from %s", remote->addr);
dm_msg_proc_thing_service_property_get(&source, &dest, &request, &response, &data, &data_len);
#ifdef DEPRECATED_LINKKIT
dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, (char *)data, data_len, alcs_context);
DM_free(data);
dm_server_free_context(alcs_context);
#endif
}
void dm_server_thing_service_property_post(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message)
{
int res = 0;
dm_server_alcs_context_t *alcs_context = NULL;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
res = _dm_server_malloc_context(remote, message, &alcs_context);
if (res != SUCCESS_RETURN) {
return;
}
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = paths;
source.payload = (unsigned char *)message->payload;
source.payload_len = message->payloadlen;
source.context = alcs_context;
dest.uri_name = DM_URI_THING_EVENT_PROPERTY_POST;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_RX_LOCAL_MESSAGE, "recv property post from %s", remote->addr);
dm_msg_proc_thing_service_property_post(&source, &dest, &request, &response);
dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, "{}", strlen("{}"), alcs_context);
dm_server_free_context(alcs_context);
}
#endif
void dm_server_thing_dev_core_service_dev(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message)
{
int res = 0;
dm_server_alcs_context_t *alcs_context = NULL;
dm_msg_source_t source;
dm_msg_dest_t dest;
dm_msg_request_payload_t request;
dm_msg_response_t response;
unsigned char *data = NULL;
int data_len = 0;
res = _dm_server_malloc_context(remote, message, &alcs_context);
if (res != SUCCESS_RETURN) {
return;
}
memset(&source, 0, sizeof(dm_msg_source_t));
memset(&dest, 0, sizeof(dm_msg_dest_t));
memset(&request, 0, sizeof(dm_msg_request_payload_t));
memset(&response, 0, sizeof(dm_msg_response_t));
source.uri = paths;
source.payload = (unsigned char *)message->payload;
source.payload_len = message->payloadlen;
source.context = alcs_context;
dest.uri_name = DM_URI_DEV_CORE_SERVICE_DEV;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_RX_LOCAL_MESSAGE, "recv core service dev from %s", remote->addr);
res = dm_msg_proc_thing_dev_core_service_dev(&source, &dest, &request, &response, &data, &data_len);
if (res < SUCCESS_RETURN) {
dm_server_free_context(alcs_context);
return;
}
dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, (char *)data, data_len, alcs_context);
if (response.code == IOTX_DM_ERR_CODE_SUCCESS) {
DM_free(data);
}
dm_server_free_context(alcs_context);
}
#endif
+29
View File
@@ -0,0 +1,29 @@
#ifndef _DM_SERVER_H_
#define _DM_SERVER_H_
#ifdef ALCS_ENABLED
typedef struct {
const char *uri_name;
const char *uri_prefix;
int auth_type;
CoAPRecvMsgHandler callback;
} dm_server_uri_map_t;
#define DM_SERVER_ALCS_NO_AUTH (0)
#define DM_SERVER_ALCS_AUTH (1)
void dm_server_alcs_event_handler(void *pcontext, void *phandle, iotx_alcs_event_msg_t *msg);
int dm_server_subscribe_all(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1]);
void dm_server_thing_service_property_set(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message);
void dm_server_thing_service_property_get(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message);
void dm_server_thing_service_property_post(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message);
void dm_server_thing_dev_core_service_dev(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message);
void dm_server_thing_service_request(CoAPContext *context, const char *paths, NetworkAddr *remote,
CoAPMessage *message);
#endif
#endif
+190
View File
@@ -0,0 +1,190 @@
#include "iotx_dm_internal.h"
#ifdef ALCS_ENABLED
#include "CoAPServer.h"
#define ALCS_NOTIFY_PORT (5683)
#define ALCS_NOTIFY_HOST "255.255.255.255"
#define ALCS_NOTIFY_METHOD "core.service.dev.notify"
const char DM_URI_DEV_CORE_SERVICE_DEV_NOTIFY[] DM_READ_ONLY = "/dev/core/service/dev/notify";
extern const char DM_MSG_REQUEST[];
static dm_server_ctx_t g_dm_server_ctx = {0};
static dm_server_ctx_t *dm_server_get_ctx(void)
{
return &g_dm_server_ctx;
}
static int _dm_server_dev_notify(void *handle)
{
int ret, i;
char *data = NULL;
char *payload = NULL;
int data_len = 0;
int payload_len = 0;
NetworkAddr notify_sa;
CoAPContext *g_coap_ctx = CoAPServer_init();
dm_msg_dev_core_service_dev(&data, &data_len);
payload_len = strlen(DM_MSG_REQUEST) + 10 + strlen(DM_MSG_VERSION) + data_len + strlen(
ALCS_NOTIFY_METHOD) + 1;
payload = DM_malloc(payload_len);
if (payload == NULL) {
DM_free(data);
return STATE_SYS_DEPEND_MALLOC;
}
memset(payload, 0, payload_len);
HAL_Snprintf(payload, payload_len, DM_MSG_REQUEST, iotx_report_id(),
DM_MSG_VERSION, data_len, data, ALCS_NOTIFY_METHOD);
memset(&notify_sa, 0, sizeof(notify_sa));
memcpy(notify_sa.addr, ALCS_NOTIFY_HOST, strlen(ALCS_NOTIFY_HOST));
notify_sa.port = ALCS_NOTIFY_PORT;
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_ALCS_CONTROL, "core/service/dev notify, payload: %s", payload);
for (i = 0; i < 2; i++) {
ret = CoAPServerMultiCast_send(g_coap_ctx, &notify_sa, DM_URI_DEV_CORE_SERVICE_DEV_NOTIFY, (uint8_t *)payload,
(uint16_t)payload_len, NULL, NULL);
}
DM_free(payload);
DM_free(data);
return ret;
}
int dm_server_open(void)
{
dm_server_ctx_t *ctx = dm_server_get_ctx();
iotx_alcs_param_t alcs_param;
iotx_alcs_event_handle_t event_handle;
memset(&alcs_param, 0x0, sizeof(iotx_alcs_param_t));
memset(&event_handle, 0x0, sizeof(iotx_alcs_event_handle_t));
alcs_param.group = (char *)DM_SERVER_ALCS_ADDR;
alcs_param.port = DM_SERVER_ALCS_PORT;
alcs_param.send_maxcount = DM_SERVER_ALCS_SEND_MAXCOUNT;
alcs_param.waittime = DM_SERVER_ALCS_WAITTIME;
alcs_param.obs_maxcount = DM_SERVER_ALCS_OBS_MAXCOUNT;
alcs_param.res_maxcount = DM_SERVER_ALCS_RES_MAXCOUNT;
alcs_param.role = IOTX_ALCS_ROLE_CLIENT | IOTX_ALCS_ROLE_SERVER;
event_handle.h_fp = dm_server_alcs_event_handler;
event_handle.pcontext = NULL;
alcs_param.handle_event = &event_handle;
ctx->conn_handle = iotx_alcs_construct(&alcs_param);
if (ctx->conn_handle == NULL) {
return STATE_DEV_MODEL_ALCS_CONSTRUCT_FAILED;
}
/* dev/core/service/dev message notify */
_dm_server_dev_notify(ctx->conn_handle);
return SUCCESS_RETURN;
}
int dm_server_connect(void)
{
dm_server_ctx_t *ctx = dm_server_get_ctx();
return iotx_alcs_cloud_init(ctx->conn_handle);
}
int dm_server_close(void)
{
dm_server_ctx_t *ctx = dm_server_get_ctx();
return iotx_alcs_destroy(&ctx->conn_handle);
}
int dm_server_send(char *uri, unsigned char *payload, int payload_len, void *context)
{
int res = 0;
dm_server_ctx_t *ctx = dm_server_get_ctx();
iotx_alcs_msg_t alcs_msg;
dm_server_alcs_context_t *alcs_context = (dm_server_alcs_context_t *)context;
memset(&alcs_msg, 0, sizeof(iotx_alcs_msg_t));
alcs_msg.group_id = 0;
alcs_msg.ip = alcs_context ? alcs_context->ip : NULL;
alcs_msg.port = alcs_context ? alcs_context->port : 0;
alcs_msg.msg_code = (alcs_context && alcs_context->token_len
&& alcs_context->token) ? ITOX_ALCS_COAP_MSG_CODE_205_CONTENT : ITOX_ALCS_COAP_MSG_CODE_GET;
alcs_msg.msg_type = IOTX_ALCS_MESSAGE_TYPE_CON;
alcs_msg.uri = uri;
alcs_msg.payload = payload;
alcs_msg.payload_len = payload_len;
if (alcs_context == NULL) {
res = iotx_alcs_observe_notify(ctx->conn_handle, alcs_msg.uri, alcs_msg.payload_len, alcs_msg.payload);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_TX_LOCAL_MESSAGE,
"alcs send msg through observe notify, result: %d", res);
} else if (alcs_context->ip && alcs_context->port && NULL == alcs_context->token) {
res = iotx_alcs_send(ctx->conn_handle, &alcs_msg);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_TX_LOCAL_MESSAGE, "alcs send msg through alcs send, result: %d",
res);
} else if (alcs_context->ip && alcs_context->port && alcs_context->token_len && alcs_context->token) {
res = iotx_alcs_send_Response(ctx->conn_handle, &alcs_msg, (uint8_t)alcs_context->token_len,
(uint8_t *)alcs_context->token);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_TX_LOCAL_MESSAGE,
"alcs send msg through alcs response, result: %d", res);
}
return res;
}
int dm_server_subscribe(char *uri, CoAPRecvMsgHandler callback, int auth_type)
{
int res = 0;
dm_server_ctx_t *ctx = dm_server_get_ctx();
iotx_alcs_res_t alcs_res;
memset(&alcs_res, 0, sizeof(iotx_alcs_res_t));
alcs_res.uri = uri;
alcs_res.msg_ct = IOTX_ALCS_MESSAGE_CT_APP_JSON;
alcs_res.msg_perm = IOTX_ALCS_MESSAGE_PERM_GET;
alcs_res.maxage = 60;
alcs_res.need_auth = auth_type;
alcs_res.callback = callback;
return iotx_alcs_register_resource(ctx->conn_handle, &alcs_res);
}
int dm_server_add_device(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1])
{
int res = 0;
dm_server_ctx_t *ctx = dm_server_get_ctx();
res = iotx_alcs_add_sub_device(ctx->conn_handle, (const char *)product_key, (const char *)device_name);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_ALCS_CONTROL, "alcs add subdev %s.%s return %d",
product_key, device_name, res);
return res;
}
int dm_server_del_device(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1])
{
int res = 0;
dm_server_ctx_t *ctx = dm_server_get_ctx();
res = iotx_alcs_remove_sub_device(ctx->conn_handle, (const char *)product_key, (const char *)device_name);
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODLE_ALCS_CONTROL, "alcs delete subdev %s.%s return %d",
product_key, device_name, res);
return res;
}
int dm_server_yield(void)
{
dm_server_ctx_t *ctx = dm_server_get_ctx();
return iotx_alcs_yield(ctx->conn_handle);
}
#endif
+34
View File
@@ -0,0 +1,34 @@
#ifndef _DM_SERVER_ADAPTER_H_
#define _DM_SERVER_ADAPTER_H_
#ifdef ALCS_ENABLED
#define DM_SERVER_ALCS_ADDR "224.0.1.187"
#define DM_SERVER_ALCS_PORT (5863)
#define DM_SERVER_ALCS_SEND_MAXCOUNT (16)
#define DM_SERVER_ALCS_WAITTIME (200)
#define DM_SERVER_ALCS_OBS_MAXCOUNT (16)
#define DM_SERVER_ALCS_RES_MAXCOUNT (255)
typedef struct {
void *conn_handle;
} dm_server_ctx_t;
typedef struct {
char *ip;
uint16_t port;
char *token;
int token_len;
} dm_server_alcs_context_t;
int dm_server_open(void);
int dm_server_connect(void);
int dm_server_close(void);
int dm_server_send(char *uri, unsigned char *payload, int payload_len, void *context);
int dm_server_subscribe(char *uri, CoAPRecvMsgHandler callback, int auth_type);
int dm_server_add_device(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_server_del_device(char product_key[IOTX_PRODUCT_KEY_LEN + 1], char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int dm_server_yield(void);
#endif
#endif
File diff suppressed because it is too large Load Diff
+533
View File
@@ -0,0 +1,533 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#if defined(DEPRECATED_LINKKIT)
#ifndef _DM_SHADOW_H_
#define _DM_SHADOW_H_
#include "iotx_dm_internal.h"
#define DM_SHW_KEY_SCHEMA "schema"
#define DM_SHW_KEY_LINK "link"
#define DM_SHW_KEY_PROFILE "profile"
#define DM_SHW_KEY_PROPERTIES "properties"
#define DM_SHW_KEY_EVENTS "events"
#define DM_SHW_KEY_SERVICES "services"
#define DM_SHW_KEY_PROFILE_PK "productKey"
#define DM_SHW_KEY_PROFILE_DN "deviceName"
#define DM_SHW_KEY_IDENTIFIER "identifier"
#define DM_SHW_KEY_NAME "name"
#define DM_SHW_KEY_DESC "desc"
#define DM_SHW_KEY_ACCESS_MODE "accessMode"
#define DM_SHW_KEY_REQUIRED "required"
#define DM_SHW_KEY_METHOD "method"
#define DM_SHW_KEY_CALLTYPE "callType"
#define DM_SHW_KEY_OUTPUTDATA "outputData"
#define DM_SHW_KEY_INPUTDATA "inputData"
#define DM_SHW_KEY_DATATYPE "dataType"
#define DM_SHW_KEY_TYPE "type"
#define DM_SHW_KEY_SPECS "specs"
#define DM_SHW_KEY_UNIT "unit"
#define DM_SHW_KEY_UNITNAME "unitName"
#define DM_SHW_KEY_MIN "min"
#define DM_SHW_KEY_MAX "max"
#define DM_SHW_KEY_LENGTH "length"
#define DM_SHW_KEY_SIZE "size"
#define DM_SHW_KEY_ITEM "item"
/* Special Service And Event */
#define DM_SHW_SPECIAL_SERVICE_SET_IDENTIFIER "set"
#define DM_SHW_SPECIAL_SERVICE_SET_METHOD "thing.service.property.set"
#define DM_SHW_SPECIAL_SERVICE_GET_IDENTIFIER "get"
#define DM_SHW_SPECIAL_SERVICE_GET_METHOD "thing.service.property.get"
#define DM_SHW_SPECIAL_EVENT_POST_IDENTIFIER "post"
#define DM_SHW_SPECIAL_EVENT_POST_METHOD "thing.event.property.post"
#define DM_SHW_KEY_DELIMITER '.'
typedef enum {
DM_SHW_DATA_TYPE_NONE, /* none */
DM_SHW_DATA_TYPE_INT, /* int */
DM_SHW_DATA_TYPE_FLOAT, /* float */
DM_SHW_DATA_TYPE_DOUBLE, /* double */
DM_SHW_DATA_TYPE_TEXT, /* string */
DM_SHW_DATA_TYPE_ENUM, /* int */
DM_SHW_DATA_TYPE_DATE, /* string */
DM_SHW_DATA_TYPE_BOOL, /* bool,0 or 1 */
DM_SHW_DATA_TYPE_ARRAY, /* support int, float, double, text */
DM_SHW_DATA_TYPE_STRUCT, /* support above 8 data types */
} dm_shw_data_type_e;
typedef enum {
DM_SHW_DATA_TARGET_SERVICE_INPUT_DATA,
DM_SHW_DATA_TARGET_SERVICE_OUTPUT_DATA
} dm_shw_data_target_e;
typedef struct {
dm_shw_data_type_e type;
int size;
void *value;
} dm_shw_data_value_complex_t;
typedef struct {
dm_shw_data_type_e type;
union {
int value_int;
float value_float;
double value_double;
void *value; /* string or complex type accroding to data type */
};
} dm_shw_data_value_t;
typedef struct {
dm_shw_data_type_e type;
int specs_number; /* used when type is enum and struct */
void *specs; /* nerver be used by struct */
} dm_shw_data_type_t;
typedef struct {
char *identifier;
dm_shw_data_value_t data_value;
} dm_shw_data_t;
typedef struct {
char *identifier;
int input_data_number; /* input_data Number */
dm_shw_data_t *input_datas; /* input_data array, type is dm_shw_data_t */
int output_data_number; /* ouput_data Number */
dm_shw_data_t *output_datas; /* output_data array, type is dm_shw_data_t */
} dm_shw_event_t;
typedef struct {
char *identifier; /* synchronized or asynchronized */
int input_data_number; /* input_data_number */
dm_shw_data_t *input_datas; /* input_data array, type is dm_shw_data_t */
int output_data_number; /* ouput_data Number */
dm_shw_data_t *output_datas; /* output_data array, type is dm_shw_data_t */
} dm_shw_service_t;
typedef struct {
int property_number;
dm_shw_data_t *properties; /* property array, type is dm_shw_data_t */
int event_number;
dm_shw_event_t *events; /* event array, type is dm_shw_event_t */
int service_number;
dm_shw_service_t *services; /* service array, type is dm_shw_service_t */
} dm_shw_t;
/**
* @brief Create TSL struct from TSL string.
* This function used to parse TSL string into TSL struct.
*
* @param tsl. The TSL string in JSON format.
* @param tsl_len. The length of tsl
* @param shadow. The pointer of TSL Struct pointer, will be malloc memory.
* This memory should be free by dm_shw_destroy.
*
* @return success or fail.
*
*/
int dm_shw_create(_IN_ iotx_dm_tsl_type_t type, _IN_ const char *tsl, _IN_ int tsl_len, _OU_ dm_shw_t **shadow);
/**
* @brief Get property from TSL struct.
* This function used to get property from TSL struct.
*
* @param shadow. The pointer of TSL Struct.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: property_id
* array type: property_id(array)[index]
* struct type: property_id(struct).property_id or property_id(struct).property_id[index]
*
* @param key_len. The length of key.
* @param property. The property in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_property_data(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
int dm_shw_get_service_input_output_data(_IN_ dm_shw_data_target_e type, _IN_ dm_shw_t *shadow, _IN_ char *key,
_IN_ int key_len, _OU_ void **data);
/**
* @brief Get event output data from TSL struct.
* This function used to get event output data from TSL struct.
*
* @param shadow. The pointer of TSL Struct.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: property_id
* array type: property_id(array)[index]
* struct type: property_id(struct).property_id or property_id(struct).property_id[index]
*
* @param key_len. The length of key.
* @param property. The property in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_event_output_data(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **data);
/**
* @brief Get property type from TSL struct.
* This function used to get property type from TSL struct.
*
* @param property. The handle of property.
* @param type. The data type of property
*
*
* @return success or fail.
*
*/
int dm_shw_get_data_type(_IN_ void *data, _OU_ dm_shw_data_type_e *type);
/**
* @brief Get event from TSL struct.
* This function used to get property from TSL struct.
*
* @param service. The handle of event.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: event_id
*
* @param key_len. The length of key.
* @param property. The event in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_event(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **event);
/**
* @brief Get service from TSL struct.
* This function used to get property from TSL struct.
*
* @param shadow. The pointer of TSL Struct.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: service_id
*
* @param key_len. The length of key.
* @param property. The service in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_service(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void **service);
/**
* @brief Get property number from TSL struct.
* This function used to get property number from TSL struct.
*
* @param shadow. The pointer of TSL Struct.
* @param number. The property number in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_property_number(_IN_ dm_shw_t *shadow, _OU_ int *number);
/**
* @brief Get service number from TSL struct.
* This function used to get property number from TSL struct.
*
* @param shadow. The pointer of TSL Struct.
* @param number. The service number in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_service_number(_IN_ dm_shw_t *shadow, _OU_ int *number);
/**
* @brief Get event number from TSL struct.
* This function used to get property number from TSL struct.
*
* @param shadow. The pointer of TSL Struct.
* @param number. The event number in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_event_number(_IN_ dm_shw_t *shadow, _OU_ int *number);
/**
* @brief Get property reference from TSL struct by index.
* This function used to get property reference from TSL struct by index.
*
* @param shadow. The pointer of TSL Struct.
* @param index. The index of property
* @param property. The property reference in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_property_by_index(_IN_ dm_shw_t *shadow, _IN_ int index, _OU_ void **property);
/**
* @brief Get service reference from TSL struct by index.
* This function used to get service reference from TSL struct by index.
*
* @param shadow. The pointer of TSL Struct.
* @param index. The index of service
* @param service. The service reference in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_service_by_index(_IN_ dm_shw_t *shadow, _IN_ int index, _OU_ void **service);
/**
* @brief Get event reference from TSL struct by index.
* This function used to get event reference from TSL struct by index.
*
* @param shadow. The pointer of TSL Struct.
* @param index. The index of event
* @param event. The event reference in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_event_by_index(_IN_ dm_shw_t *shadow, _IN_ int index, _OU_ void **event);
/**
* @brief Get service reference from TSL struct by identifier.
* This function used to get service reference from TSL struct by identifier.
*
* @param shadow. The pointer of TSL Struct.
* @param identifier. The identifier of event
* @param service. The service reference in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_service_by_identifier(_IN_ dm_shw_t *shadow, _IN_ char *identifier, _OU_ void **service);
/**
* @brief Get event reference from TSL struct by identifier.
* This function used to get event reference from TSL struct by identifier.
*
* @param shadow. The pointer of TSL Struct.
* @param identifier. The identifier of event
* @param event. The event reference in TSL Struct.
*
* @return success or fail.
*
*/
int dm_shw_get_event_by_identifier(_IN_ dm_shw_t *shadow, _IN_ char *identifier, _OU_ void **event);
/**
* @brief Get property identifier from TSL struct by service handle.
* This function used to get property identifier from TSL struct by service handle.
*
* @param service. The handle of property.
* @param method. The reference to property identifier in TSL Struct
*
* @return success or fail.
*
*/
int dm_shw_get_property_identifier(_IN_ void *property, _OU_ char **identifier);
/**
* @brief Get service method from TSL struct by service handle.
* This function used to get service method from TSL struct by service handle.
*
* @param service. The handle of service.
* @param method. Generate method from service identifier
*
* @return success or fail.
*
*/
int dm_shw_get_service_method(_IN_ void *service, _OU_ char **method);
/**
* @brief Get event method from TSL struct by event handle.
* This function used to get event method from TSL struct by event handle.
*
* @param service. The handle of event.
* @param method. Generate method from event identifier
*
* @return success or fail.
*
*/
int dm_shw_get_event_method(_IN_ void *event, _OU_ char **method);
/**
* @brief Set Property Value Into TSL Struct.
* This function used to set property value into TSL Struct.
*
* @param tsl. The pointer of TSL Struct.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: property_id
* array type: property_id(array)[index]
* struct type: property_id(struct).property_id or property_id(struct).property_id[index]
*
* @param key_len. The length of key
* @param value. The value to be set, data type decided by data type of property as follows:
* int: int*, float: float*, double: double*,
* text: char*, enum: int*, date: char*, bool: int*
* attention! value can be NULL to clear property value
* @param value_len. The length of value, only be used when type is text or data
*
* @return success or fail.
*
*/
int dm_shw_set_property_value(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
/**
* @brief Get Property Value From TSL Struct.
* This function used to get property value from TSL Struct.
*
* @param tsl. The pointer of TSL Struct.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: property_id
* array type: property_id(array)[index]
* struct type: property_id(struct).property_id or property_id(struct).property_id[index]
*
* @param key_len. The length of key
* @param value. The variable to store value, data type decided by data type of property as follows:
* int: int*, float: float*, double: double*,
* text: char**, enum: int*, date: char**, bool: int*
* attention! value can not be NULL
*
* @warning if data type is text or date, *value well be end with '\0'.
* the memory allocated to *value must be free by user.
*
* @return success or fail.
*
*/
int dm_shw_get_property_value(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _OU_ void *value);
/**
* @brief Set event output value into TSL struct.
* This function used to set event output value into TSL struct.
*
* @param tsl. The pointer of TSL Struct.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: event_id.event_data_id
* array type: event_id.event_data_id(array)[index]
* struct type: event_id.event_data_id(struct).property_id
* or event_id.event_data_id(struct).property_id[index]
*
* @param key_len. The length of key
* @param value. The value to be set, data type decided by data type of property as follows:
* int: int*, float: float*, double: double*,
* text: char*, enum: int*, date: char*, bool: int*
* attention! value can be NULL to clear property value
* @param value_len. The length of value, only be used when type is text or data
*
* @return success or fail.
*
*/
int dm_shw_set_event_output_value(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value,
_IN_ int value_len);
/**
* @brief Get event output value from TSL struct.
* This function used to get event output value from TSL struct.
*
* @param tsl. The pointer of TSL Struct.
* @param key. The property compound string, format decided by data type of property as follows:
* int,float,double,text,enum,date,bool type: event_id.event_data_id
* array type: event_id.event_data_id(array)[index]
* struct type: event_id.event_data_id(struct).property_id
* or event_id.event_data_id(struct).property_id[index]
*
* @param key_len. The length of key
* @param value. The variable to store value, data type decided by data type of property as follows:
* int: int*, float: float*, double: double*,
* text: char**, enum: int*, date: char**, bool: int*
* attention! value can not be NULL
*
* @warning if data type is text or date, *value well be end with '\0'.
* the memory allocated to *value must be free by user.
*
* @return success or fail.
*
*/
int dm_shw_get_event_output_value(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len, _IN_ void *value);
int dm_shw_set_service_input_output_value(_IN_ dm_shw_data_target_e type, _IN_ dm_shw_t *shadow, _IN_ char *key,
_IN_ int key_len, _IN_ void *value, _IN_ int value_len);
int dm_shw_get_service_input_output_value(_IN_ dm_shw_data_target_e type, _IN_ dm_shw_t *shadow, _IN_ char *key,
_IN_ int key_len, _IN_ void *value);
/**
* @brief Get property payload from TSL struct.
* This function used to get property payload from TSL struct.
*
* @param shadow. The pointer of TSL Struct
* @param identifier. The Property Identifier
* @param identifier_len. The Property Identifier Length
* @param lite. The pointer to json array where to store property value
*
* @warning The payload malloc by this function and need to be free manully.
*
* @return success or fail.
*
*/
int dm_shw_assemble_property(_IN_ dm_shw_t *shadow, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
/**
* @brief Get event output payload from TSL struct.
* This function used to get event output payload from TSL struct.
*
* @param shadow. The pointer of TSL Struct
* @param identifier. The Event Identifier
* @param identifier_len. The Event Identifier Length
* @param lite. The pointer to json array where to store event output value
*
* @warning The payload malloc by this function and need to be free manully.
*
* @return success or fail.
*
*/
int dm_shw_assemble_event_output(_IN_ dm_shw_t *shadow, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
/**
* @brief Get service output payload from TSL struct.
* This function used to get service output payload from TSL struct.
*
* @param shadow. The pointer of TSL Struct
* @param identifier. The Service Identifier
* @param identifier_len. The Service Identifier Length
* @param lite. The pointer to json array where to store service output value
*
* @warning The payload malloc by this function and need to be free manully.
*
* @return success or fail.
*
*/
int dm_shw_assemble_service_output(_IN_ dm_shw_t *shadow, _IN_ char *identifier, _IN_ int identifier_len,
_IN_ lite_cjson_item_t *lite);
/**
* @brief Free TSL struct.
* This function used to free TSL struct.
*
* @param shadow. The pointer of TSL Struct.
*
* @return success or fail.
*
*/
void dm_shw_destroy(_IN_ dm_shw_t **shadow);
#if 0
/**
* @brief Print detailed information of TSL struct.
* This function used to print detailed information of TSL struct.
*
* @param shadow. The pointer of TSL Struct.
*
* @return success or fail.
*
*/
void dm_shw_print(_IN_ dm_shw_t *shadow);
#endif
#endif
#endif
File diff suppressed because it is too large Load Diff
+24
View File
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#if defined(DEPRECATED_LINKKIT)
#ifndef _DM_TSL_ALINK_H_
#define _DM_TSL_ALINK_H_
/**
* @brief Create TSL struct from TSL string.
* This function used to parse TSL string into TSL struct.
*
* @param tsl. The TSL string in JSON format.
* @param tsl_len. The length of tsl
* @param shadow. The pointer of TSL Struct pointer, will be malloc memory.
* This memory should be free by dm_shw_destroy.
*
* @return success or fail.
*
*/
int dm_tsl_alink_create(_IN_ const char *tsl, _IN_ int tsl_len, _OU_ dm_shw_t **shadow);
#endif
#endif
+430
View File
@@ -0,0 +1,430 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
int dm_utils_copy_direct(_IN_ void *input, _IN_ int input_len, _OU_ void **output, _IN_ int output_len)
{
if (input == NULL || output == NULL || *output != NULL) {
return STATE_USER_INPUT_INVALID;
}
*output = HAL_Malloc(output_len);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, output_len);
memcpy(*output, input, input_len);
return SUCCESS_RETURN;
}
int dm_utils_copy(_IN_ void *input, _IN_ int input_len, _OU_ void **output, _IN_ int output_len)
{
if (input == NULL || output == NULL || *output != NULL) {
return STATE_USER_INPUT_INVALID;
}
*output = DM_malloc(output_len);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, output_len);
memcpy(*output, input, input_len);
return SUCCESS_RETURN;
}
int dm_utils_strarr_index(_IN_ char *input, _IN_ int input_len,
_OU_ int *partial_input_len, _OU_ int *array_input_len, _OU_ int *array_index)
{
int index = 0;
int deep = 0;
char *bracket_pre = NULL;
char *bracket_suf = NULL;
char array_index_str[11] = {0};
if (input == NULL || input_len <= 1 || array_index == NULL) {
return STATE_USER_INPUT_INVALID;
}
for (index = 0; index < input_len; index++) {
switch (input[index]) {
case '[': {
if (deep != 0) {
return FAIL_RETURN;
}
deep++;
if (!bracket_pre) {
bracket_pre = (char *)&input[index];
}
}
break;
case ']': {
if (deep != 1) {
return FAIL_RETURN;
}
deep--;
if (input[index - 1] == '[') {
return FAIL_RETURN;
}
if (!bracket_suf) {
bracket_suf = (char *)&input[index];
}
}
break;
default:
break;
}
}
if (bracket_pre && bracket_suf && ((bracket_suf - input + 1) == input_len)) {
if (partial_input_len) {
*partial_input_len = bracket_pre - input;
}
if (array_input_len) {
*array_input_len = bracket_suf - input + 1;
}
/* Get Index */
if(bracket_suf - bracket_pre - 1 > 10) {
return FAIL_RETURN;
}
memcpy(array_index_str, bracket_pre + 1, bracket_suf - bracket_pre - 1);
*array_index = atoi(array_index_str);
return SUCCESS_RETURN;
}
return FAIL_RETURN;
}
int dm_utils_itoa_direct(_IN_ int input, _OU_ char **output)
{
int res = 0;
char temp_output[10 + 1] = {0};
if (output == NULL || *output != NULL) {
return STATE_USER_INPUT_INVALID;
}
res = HAL_Snprintf(temp_output, 10, "%d", input);
if (res < 0) {
return STATE_SYS_DEPEND_SNPRINTF;
}
*output = HAL_Malloc(strlen(temp_output) + 1);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, strlen(temp_output) + 1);
memcpy(*output, temp_output, strlen(temp_output));
return SUCCESS_RETURN;
}
int dm_utils_itoa(_IN_ int input, _OU_ char **output)
{
int res = 0;
char temp_output[10 + 1] = {0};
if (output == NULL || *output != NULL) {
return STATE_USER_INPUT_INVALID;
}
res = HAL_Snprintf(temp_output, 10, "%d", input);
if (res < 0) {
return STATE_SYS_DEPEND_SNPRINTF;
}
*output = DM_malloc(strlen(temp_output) + 1);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, strlen(temp_output) + 1);
memcpy(*output, temp_output, strlen(temp_output));
return SUCCESS_RETURN;
}
int dm_utils_ftoa_direct(_IN_ double input, _OU_ char **output)
{
int res = 0;
char temp_output[30 + 1] = {0};
if (output == NULL || *output != NULL) {
return STATE_USER_INPUT_INVALID;
}
res = HAL_Snprintf(temp_output, 30, "%f", input);
if (res < 0) {
return STATE_SYS_DEPEND_SNPRINTF;
}
*output = HAL_Malloc(strlen(temp_output) + 1);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, strlen(temp_output) + 1);
memcpy(*output, temp_output, strlen(temp_output));
return SUCCESS_RETURN;
}
int dm_utils_ftoa(_IN_ double input, _OU_ char **output)
{
int res = 0;
char temp_output[30 + 1] = {0};
if (output == NULL || *output != NULL) {
return STATE_USER_INPUT_INVALID;
}
res = HAL_Snprintf(temp_output, 30, "%f", input);
if (res < 0) {
return STATE_SYS_DEPEND_SNPRINTF;
}
*output = DM_malloc(strlen(temp_output) + 1);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, strlen(temp_output) + 1);
memcpy(*output, temp_output, strlen(temp_output));
return SUCCESS_RETURN;
}
int dm_utils_hex_to_str(_IN_ unsigned char *input, _IN_ int input_len, _OU_ char **output)
{
int index = 0, output_len = 0;
unsigned char iter_char = 0;
if (input == NULL || input_len <= 0 || output == NULL || *output != NULL) {
return STATE_USER_INPUT_INVALID;
}
output_len = input_len * 2;
*output = DM_malloc(output_len + 1);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, output_len + 1);
for (index = 0; index < input_len; index++) {
iter_char = (input[index] >> 4) & 0x0F;
if (iter_char <= 0x09) {
iter_char += '0';
} else if (iter_char >= 0x0A && iter_char <= 0x0F) {
iter_char += 'A' - 0x0A;
}
(*output)[index * 2] = iter_char;
iter_char = (input[index]) & 0x0F;
if (iter_char <= 0x09) {
iter_char += '0';
} else if (iter_char >= 0x0A && iter_char <= 0x0F) {
iter_char += 'A' - 0x0A;
}
(*output)[index * 2 + 1] = iter_char;
}
return SUCCESS_RETURN;
}
int dm_utils_str_to_hex(_IN_ char *input, _IN_ int input_len, _OU_ unsigned char **output, _OU_ int *output_len)
{
int index = 0;
char iter_char = 0;
if (input == NULL || input_len <= 0 || input_len % 2 != 0 ||
output == NULL || *output != NULL || output_len == NULL) {
return STATE_USER_INPUT_INVALID;
}
*output_len = input_len / 2;
*output = DM_malloc(*output_len);
if (*output == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*output, 0, *output_len);
for (index = 0; index < input_len; index += 2) {
if (input[index] >= '0' && input[index] <= '9') {
iter_char = input[index] - '0';
} else if (input[index] >= 'A' && input[index] <= 'F') {
iter_char = input[index] - 'A' + 0x0A;
}
(*output)[index / 2] |= (iter_char << 4) & 0xF0;
if (input[index + 1] >= '0' && input[index + 1] <= '9') {
iter_char = input[index + 1] - '0';
} else if (input[index + 1] >= 'A' && input[index + 1] <= 'F') {
iter_char = input[index + 1] - 'A' + 0x0A;
}
(*output)[index / 2] |= (iter_char) & 0x0F;
}
return SUCCESS_RETURN;
}
int dm_utils_memtok(_IN_ char *input, _IN_ int input_len, _IN_ char delimiter, _IN_ int index, _OU_ int *offset)
{
int item_index = 0;
int count = 0;
if (input == NULL || input_len <= 0 || offset == NULL) {
return STATE_USER_INPUT_INVALID;
}
for (item_index = 0; item_index < input_len; item_index++) {
if (input[item_index] == delimiter && (item_index + 1) < input_len) {
count++;
if (count == index) {
*offset = item_index;
return SUCCESS_RETURN;
}
}
}
return STATE_DEV_MODEL_URL_SPLIT_FAILED;
}
int dm_utils_replace_char(_IN_ char *input, _IN_ int input_len, _IN_ char src, _IN_ char dest)
{
int index = 0;
if (input == NULL || input_len <= 0) {
return STATE_USER_INPUT_INVALID;
}
for (index = 0; index < input_len; index++) {
if (input[index] == src) {
input[index] = dest;
}
}
return SUCCESS_RETURN;
}
int dm_utils_service_name(_IN_ const char *prefix, _IN_ const char *name, _IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
_IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ char **service_name)
{
int prefix_len = (prefix == NULL) ? (0) : (strlen(prefix));
int name_len = (name == NULL) ? (0) : (strlen(name));
int service_name_len = 0;
if ((prefix == NULL && name == NULL) || product_key == NULL || device_name == NULL ||
service_name == NULL || *service_name != NULL) {
return STATE_USER_INPUT_INVALID;
}
service_name_len = prefix_len + name_len + strlen(product_key) + strlen(device_name) + 1;
*service_name = DM_malloc(service_name_len);
if (*service_name == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*service_name, 0, service_name_len);
if (prefix != NULL) {
HAL_Snprintf(*service_name, service_name_len, prefix, product_key, device_name);
}
if (name != NULL) {
memcpy(*service_name + strlen(*service_name), name, name_len);
}
return SUCCESS_RETURN;
}
int dm_utils_uri_add_prefix(_IN_ const char *prefix, _IN_ char *uri, _OU_ char **new_uri)
{
int new_uri_len = 0;
if (prefix == NULL || uri == NULL || new_uri == NULL || *new_uri != NULL) {
return STATE_USER_INPUT_INVALID;
}
new_uri_len = strlen(prefix) + strlen(uri) + 1;
*new_uri = DM_malloc(new_uri_len);
if (*new_uri == NULL) {
return STATE_SYS_DEPEND_MALLOC;
}
memset(*new_uri, 0, new_uri_len);
memcpy(*new_uri, prefix, strlen(prefix));
memcpy(*new_uri + strlen(*new_uri), uri, strlen(uri));
return SUCCESS_RETURN;
}
int dm_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 STATE_USER_INPUT_INVALID;
}
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 STATE_DEV_MODEL_WRONG_JSON_FORMAT;
}
if (type != cJSON_Invalid && lite->type != type) {
memset(lite, 0, sizeof(lite_cjson_t));
return STATE_DEV_MODEL_WRONG_JSON_FORMAT;
}
return SUCCESS_RETURN;
}
int dm_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 STATE_USER_INPUT_INVALID;
}
if (lite->type != cJSON_Object) {
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_WRONG_JSON_FORMAT, "parse object item");
}
memset(lite_item, 0, sizeof(lite_cjson_t));
res = lite_cjson_object_item(lite, key, key_len, lite_item);
if (res != SUCCESS_RETURN) {
memset(lite_item, 0, sizeof(lite_cjson_t));
return STATE_DEV_MODEL_GET_JSON_ITEM_FAILED;
}
if (type != cJSON_Invalid && lite_item->type != type) {
memset(lite_item, 0, sizeof(lite_cjson_t));
return STATE_DEV_MODEL_GET_JSON_ITEM_FAILED;
}
return SUCCESS_RETURN;
}
void *dm_utils_malloc(unsigned int size)
{
#ifdef INFRA_MEM_STATS
return LITE_malloc(size, MEM_MAGIC, "lite_cjson");
#else
return HAL_Malloc(size);
#endif
}
void dm_utils_free(void *ptr)
{
#ifdef INFRA_MEM_STATS
LITE_free(ptr);
#else
HAL_Free((void *)ptr);
#endif
}
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _DM_UTILS_H_
#define _DM_UTILS_H_
#define DM_UTILS_UINT16_STRLEN (5)
#define DM_UTILS_UINT32_STRLEN (10)
#define DM_UTILS_UINT64_STRLEN (20)
int dm_utils_copy_direct(void *input, int input_len, void **output, int output_len);
int dm_utils_copy(void *input, int input_len, void **output, int output_len);
/**
* @brief search array index in a string.
* This function used to search array index in a string.
*
* @param input. The string to be searched
* @param input_len. The length of input
* @param partial_input_len. The length of input except [xx]
* @param array_input_len. The length of input include [xx]
* @param array_index. The array index in [xx]
*
* @warning input must be type of "xxxxx[xx]"
* @return success or fail.
*
*/
int dm_utils_strarr_index(char *input, int input_len,
int *partial_input_len, int *array_input_len, int *array_index);
int dm_utils_itoa_direct(int input, char **output);
int dm_utils_itoa(int input, char **output);
int dm_utils_ftoa_direct(double input, char **output);
int dm_utils_ftoa(double input, char **output);
int dm_utils_hex_to_str(unsigned char *input, int input_len, char **output);
int dm_utils_str_to_hex(char *input, int input_len, unsigned char **output, int *output_len);
int dm_utils_memtok(char *input, int input_len, char delimiter, int index, int *offset);
int dm_utils_replace_char(char *input, int input_len, char src, char dest);
int dm_utils_service_name(const char *prefix, const char *name, char product_key[IOTX_PRODUCT_KEY_LEN + 1],
char device_name[IOTX_DEVICE_NAME_LEN + 1], char **service_name);
int dm_utils_uri_add_prefix(const char *prefix, char *uri, char **new_uri);
int dm_utils_json_parse(const char *payload, int payload_len, int type, lite_cjson_t *lite);
int dm_utils_json_object_item(lite_cjson_t *lite, const char *key, int key_len, int type,
lite_cjson_t *lite_item);
void *dm_utils_malloc(unsigned int size);
void dm_utils_free(void *ptr);
#endif
File diff suppressed because it is too large Load Diff
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _LINKKIT_GATEWAY_LEGACY_H_
#define _LINKKIT_GATEWAY_LEGACY_H_
#include "infra_list.h"
#include "linkkit_gateway_export.h"
#define LINKKIT_GATEWAY_LEGACY_KEY_ID "id"
#define LINKKIT_GATEWAY_LEGACY_KEY_CODE "code"
#define LINKKIT_GATEWAY_LEGACY_KEY_DEVID "devid"
#define LINKKIT_GATEWAY_LEGACY_KEY_SERVICEID "serviceid"
#define LINKKIT_GATEWAY_LEGACY_KEY_PROPERTYID "propertyid"
#define LINKKIT_GATEWAY_LEGACY_KEY_EVENTID "eventid"
#define LINKKIT_GATEWAY_LEGACY_KEY_PAYLOAD "payload"
#define LINKKIT_GATEWAY_LEGACY_KEY_PRODUCT_KEY "productKey"
#define LINKKIT_GATEWAY_LEGACY_KEY_TIME "time"
#define LINKKIT_GATEWAY_LEGACY_KEY_VERSION "version"
#define LINKKIT_GATEWAY_LEGACY_SYNC_DEFAULT_TIMEOUT_MS (10000)
typedef struct {
int devid;
linkkit_cbs_t *callback;
void *callback_ctx;
struct list_head linked_list;
} linkkit_gateway_dev_callback_node_t;
typedef void (*linkkit_gateway_upstream_async_callback)(int retval, void *ctx);
typedef struct {
int msgid;
void *semaphore;
int code;
struct list_head linked_list;
} linkkit_gateway_upstream_sync_callback_node_t;
typedef struct {
int msgid;
int timeout_ms;
uint64_t timestamp_ms;
linkkit_gateway_upstream_async_callback callback;
void *callback_ctx;
struct list_head linked_list;
} linkkit_gateway_upstream_async_callback_node_t;
typedef struct {
void *mutex;
void *upstream_mutex;
int is_inited;
int is_started;
linkkit_params_t init_params;
void *dispatch_thread;
handle_service_fota_callback_fp_t fota_callback;
struct list_head dev_callback_list;
struct list_head upstream_sync_callback_list;
struct list_head upstream_async_callback_list;
} linkkit_gateway_legacy_ctx_t;
#endif
File diff suppressed because it is too large Load Diff
+190
View File
@@ -0,0 +1,190 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "iotx_dm_internal.h"
#ifdef DEPRECATED_LINKKIT
#include "infra_json_parser.h"
#include "mqtt_api.h"
#include "impl_ntp.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#define IMPL_NTP_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "impl.ntp")
#define IMPL_NTP_FREE(ptr) LITE_free(ptr)
#else
#define IMPL_NTP_MALLOC(size) HAL_Malloc(size)
#define IMPL_NTP_FREE(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
#endif
typedef void (*ntp_reply_cb_t)(const char *);
static ntp_reply_cb_t g_ntp_reply_cb = NULL;
static char g_ntp_time[NTP_TIME_STR_MAX_LEN + 1] = {0};
static void linkkit_ntp_time_reply(void *pcontext, void *pclient, void *mesg)
{
#define DEV_TX_TIME "deviceSendTime"
#define SERVER_RX_TIME "serverRecvTime"
#define SERVER_TX_TIME "serverSendTime"
int len = 0;
char *elem = NULL;
char server_rx_time[NTP_TIME_STR_MAX_LEN + 1] = {0};
char server_tx_time[NTP_TIME_STR_MAX_LEN + 1] = {0};
uint32_t payload_len;
char *payload;
uint32_t tx = 0;
uint32_t rx = 0;
uint32_t diff = 0;
iotx_mqtt_event_msg_pt msg = (iotx_mqtt_event_msg_pt)mesg;
iotx_mqtt_topic_info_pt ptopic_info = (iotx_mqtt_topic_info_pt) msg->msg;
switch (msg->event_type) {
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
payload_len = ptopic_info->payload_len;
payload = (char *)ptopic_info->payload;
break;
default:
goto NTP_FAIL;
}
if (payload == NULL || payload_len == 0) {
goto NTP_FAIL;
}
memset(g_ntp_time, 0, sizeof(g_ntp_time));
log_debug("[ntp]", "ntp reply len:%u, payload:%s\r\n", payload_len, payload);
/*
* get deviceSendTime, serverRecvTime, serverSendTime
*/
elem = json_get_value_by_name(payload, payload_len, SERVER_TX_TIME, &len, NULL);
if (elem == NULL || len <= 0 || len > NTP_TIME_STR_MAX_LEN) {
goto NTP_FAIL;
}
memcpy(server_tx_time, elem, len);
elem = json_get_value_by_name(payload, payload_len, SERVER_RX_TIME, &len, NULL);
if (elem == NULL || len <= 0 || len > NTP_TIME_STR_MAX_LEN) {
goto NTP_FAIL;
}
memcpy(server_rx_time, elem, len);
elem = json_get_value_by_name(payload, payload_len, DEV_TX_TIME, &len, NULL);
if (elem == NULL || len <= 0 || len > NTP_TIME_STR_MAX_LEN) {
goto NTP_FAIL;
}
/*
* atoi fails to convert string to integer
* so we convert manully
*/
while (len -- > 0) {
tx *= 10;
tx += elem[0] - '0';
elem ++;
}
rx = HAL_UptimeMs();
diff = (rx - tx) >> 1;
if (diff >= 1000000) {
goto NTP_FAIL;
}
len = strlen(server_tx_time);
elem = &server_tx_time[len > 9 ? len - 9 : 0];
tx = atoi(elem);
tx += diff;
if (tx > 999999999) {
tx = 999999999;
}
if (len > 9) {
sprintf(elem, "%09u", (unsigned int)tx);
} else {
sprintf(elem, "%u", (unsigned int)tx);
}
strncpy(g_ntp_time, (const char *)server_tx_time, sizeof(g_ntp_time) - 1);
NTP_FAIL:
if (g_ntp_reply_cb != NULL) {
g_ntp_reply_cb(g_ntp_time);
}
return;
}
int linkkit_ntp_time_request(void (*ntp_reply)(const char *ntp_offset_time_ms))
{
int ret = -1;
int final_len = 0;
int packet_len = 64;
int topic_len = 128;
char *packet = NULL;
char *topic = NULL;
do {
char pk[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char dn[IOTX_DEVICE_NAME_LEN + 1] = {0};
IOT_Ioctl(IOTX_IOCTL_GET_PRODUCT_KEY, pk);
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_NAME, dn);
topic = (char *)IMPL_NTP_MALLOC(topic_len + 1);
if (topic == NULL) {
goto NTP_REQ_ERR;
}
memset(topic, 0, topic_len + 1);
HAL_Snprintf(topic, topic_len, TOPIC_NTP_REPLY, pk, dn);
#ifdef MQTT_AUTO_SUBSCRIBE
ret = IOT_MQTT_Subscribe_Sync(NULL, topic, IOTX_MQTT_QOS3_SUB_LOCAL,
(iotx_mqtt_event_handle_func_fpt)linkkit_ntp_time_reply, NULL, 1000);
#else
ret = IOT_MQTT_Subscribe_Sync(NULL, topic, IOTX_MQTT_QOS0,
(iotx_mqtt_event_handle_func_fpt)linkkit_ntp_time_reply, NULL, 1000);
#endif
if (ret < 0) {
goto NTP_REQ_ERR;
}
memset(topic, 0, topic_len + 1);
HAL_Snprintf(topic, topic_len, TOPIC_NTP, pk, dn);
} while (0);
packet = (char *)IMPL_NTP_MALLOC(packet_len + 1);
if (packet == NULL) {
ret = -1;
goto NTP_REQ_ERR;
}
memset(packet, 0, packet_len + 1);
g_ntp_reply_cb = ntp_reply;
final_len = HAL_Snprintf(packet, packet_len, "{\"deviceSendTime\":\"%u\"}", (unsigned int)(HAL_UptimeMs()));
log_debug("[ntp]", "report ntp:%s\r\n", packet);
ret = IOT_MQTT_Publish_Simple(NULL, topic, IOTX_MQTT_QOS0, packet, final_len);
NTP_REQ_ERR:
if (topic) {
IMPL_NTP_FREE(topic);
}
if (packet) {
IMPL_NTP_FREE(packet);
}
return ret;
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef NTP_H
#define NTP_H
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#define TOPIC_NTP "/ext/ntp/%s/%s/request"
#define TOPIC_NTP_REPLY "/ext/ntp/%s/%s/response"
#define NTP_TIME_STR_MAX_LEN (20)
int linkkit_ntp_time_request(void (*)(const char *ntp_offset_time_ms));
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif
File diff suppressed because it is too large Load Diff
+43
View File
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _LINKKIT_SOLO_LEGACY_H_
#define _LINKKIT_SOLO_LEGACY_H_
#include "linkkit_export.h"
#define LINKKIT_SOLO_LEGACY_KEY_ID "id"
#define LINKKIT_SOLO_LEGACY_KEY_CODE "code"
#define LINKKIT_SOLO_LEGACY_KEY_DEVID "devid"
#define LINKKIT_SOLO_LEGACY_KEY_SERVICEID "serviceid"
#define LINKKIT_SOLO_LEGACY_KEY_PROPERTYID "propertyid"
#define LINKKIT_SOLO_LEGACY_KEY_EVENTID "eventid"
#define LINKKIT_SOLO_LEGACY_KEY_PAYLOAD "payload"
#define LINKKIT_SOLO_LEGACY_KEY_CONFIG_ID "configId"
#define LINKKIT_SOLO_LEGACY_KEY_CONFIG_SIZE "configSize"
#define LINKKIT_SOLO_LEGACY_KEY_GET_TYPE "getType"
#define LINKKIT_SOLO_LEGACY_KEY_SIGN "sign"
#define LINKKIT_SOLO_LEGACY_KEY_SIGN_METHOD "signMethod"
#define LINKKIT_SOLO_LEGACY_KEY_URL "url"
#define LINKKIT_SOLO_LEGACY_KEY_VERSION "version"
typedef struct {
int msgid;
handle_post_cb_fp_t callback;
struct list_head linked_list;
} linkkit_solo_upstream_callback_node_t;
typedef struct {
void *mutex;
void *upstream_mutex;
int is_started;
int is_leaved;
linkkit_ops_t *user_callback;
void *user_context;
handle_service_cota_callback_fp_t cota_callback;
handle_service_fota_callback_fp_t fota_callback;
struct list_head callback_list;
} linkkit_solo_legacy_ctx_t;
#endif
+116
View File
@@ -0,0 +1,116 @@
#include "interface.h"
/////////////////////////////////////////////////////////////////////////////////////////
#ifndef NULL
#define NULL (void *)0
#endif
typedef struct
{
int service_id;
void *callback;
} evs_service_map_t;
static evs_service_map_t g_evs_service_map[] = {
/*服务回调*/
{EVS_CONF_GET_SRV, NULL}, // 配置获取服务
{EVS_CONF_UPDATE_SRV, NULL}, // 配置更新服务
{EVS_QUE_DATA_SRV, NULL}, // 数据查询服务
{EVS_DEV_MAINTAIN_SRV, NULL}, // 设备维护服务
{EVS_CTRL_LOCK_SRV, NULL}, // 电子锁控制服务
{EVS_FEE_MODEL_UPDATE_SRV, NULL}, // 计量计费模型更新服务
{EVS_START_CHARGE_SRV, NULL}, // 平台远程启动服务
{EVS_AUTH_RESULT_SRV, NULL}, // 启动充电鉴权结果
{EVS_STOP_CHARGE_SRV, NULL}, // 平台停止充电
{EVS_ORDER_CHECK_SRV, NULL}, // 交易记录确认服务
{EVS_RSV_CHARGE_SRV, NULL}, // 预约充电服务
{EVS_GROUND_LOCK_SRV, NULL}, // 地锁控制服务
{EVS_GATE_LOCK_SRV, NULL}, // 智能门锁控制服务
{EVS_ORDERLY_CHARGE_SRV, NULL}, // 有序充电策略下发
{EVS_MAINTAIN_RESULT_SRV, NULL}, // 维护状态查询结果
{EVS_FUN_CONF_UPDATE_SRV, NULL}, // 功能配置更新服务
{EVS_FUN_CONF_GET_SRV, NULL}, // 功能配置获取服务
{EVS_FEE_MODEL_QUERY_SRV, NULL}, // 计量计费模型查询服务
{EVS_BLE_SECRET_UPDATE_SRV, NULL}, // 蓝牙秘钥更新服务
{EVS_BLE_PLUG_CHG_INFO_GET_SRV, NULL}, // 蓝牙即插即充信息查询服务
{EVS_BLE_PLUG_CHG_INFO_CONF_SRV, NULL}, // 蓝牙即插即充信息确认服务
{EVS_BLE_AUTH_LIST_CLEAN_SRV, NULL}, // 蓝牙鉴权列表清除服务
{EVS_BLE_RESET_SRV, NULL}, // 蓝牙即插即充重置服务
{EVS_PILE_PARTS_CONF_SRV, NULL}, // 充电桩部件配置服务
{EVS_PILE_PARTS_CONF_GET_SRV, NULL}, // 充电桩部件配置查询服务
{EVS_VIN_LIST_UPDATE_SRV, NULL}, // VIN 白名单更新服务
{EVS_ORDER_ASK_SRV, NULL}, // 交易记录召测服务
{EVS_Meter_ASK_SRV, NULL}, // 电表底值召测服务
/*系统回调*/
{EVS_STATE_EVERYTHING, NULL}, // SDK内部打印回调
{EVS_CONNECT_SUCC, NULL}, // SDK连接平台成功回调
{EVS_DISCONNECTED, NULL}, // SDK与平台断开连接回调
{EVS_REPORT_REPLY, NULL}, // 属性上报回复回调
{EVS_TRIGGER_EVENT_REPLY, NULL}, // 事件上报回复回调
{EVS_CERT_GET, NULL}, // 证书获取回调
{EVS_CERT_SET, NULL}, // 证书设置回调
{EVS_DEVICE_REG_CODE_GET, NULL}, // 注册码获取回调
{EVS_DEVICE_UID_GET, NULL}, // 设备唯一编码获取回调
{EVS_TIME_SYNC, NULL}, // 时钟同步回调
{EVS_OTA_UPDATE, NULL}, // 远程升级回调
};
void *evs_service_callback(int service)
{
if (service < 0 || service >= sizeof(g_evs_service_map) / sizeof(evs_service_map_t))
{
return NULL;
}
return g_evs_service_map[service].callback;
}
/*服务回调函数定义*/
DEFINE_SEVICE_CALLBACK(EVS_DEV_MAINTAIN_SRV, int (*callback)(evs_service_dev_maintain *request))
DEFINE_SEVICE_CALLBACK(EVS_CTRL_LOCK_SRV, int (*callback)(evs_service_lockCtrl *request))
DEFINE_SEVICE_CALLBACK(EVS_FEE_MODEL_UPDATE_SRV, int (*callback)(evs_service_issue_feeModel *request, evs_service_feedback_feeModel *feedback))
DEFINE_SEVICE_CALLBACK(EVS_START_CHARGE_SRV, int (*callback)(evs_service_startCharge *request, evs_service_feedback_startCharge *feedback))
DEFINE_SEVICE_CALLBACK(EVS_AUTH_RESULT_SRV, int (*callback)(evs_service_authCharge *request, evs_service_feedback_authCharge *feedback))
DEFINE_SEVICE_CALLBACK(EVS_STOP_CHARGE_SRV, int (*callback)(evs_service_stopCharge *request, evs_service_feedback_stopCharge *feedback))
DEFINE_SEVICE_CALLBACK(EVS_ORDER_CHECK_SRV, int (*callback)(evs_service_confirmTrade *request, void *feedback))
DEFINE_SEVICE_CALLBACK(EVS_RSV_CHARGE_SRV, int (*callback)(evs_service_rsvCharge *request, evs_service_feedback_rsvCharge *feedback))
DEFINE_SEVICE_CALLBACK(EVS_GROUND_LOCK_SRV, int (*callback)(evs_service_groundLock_ctrl *request, evs_service_feedback_groundLock_ctrl *feedback))
DEFINE_SEVICE_CALLBACK(EVS_GATE_LOCK_SRV, int (*callback)(evs_service_gateLock_ctrl *request, evs_service_feedback_gateLock_ctrl *feedback))
DEFINE_SEVICE_CALLBACK(EVS_CONF_UPDATE_SRV, int (*callback)(evs_data_dev_config *request, int *feedback))
DEFINE_SEVICE_CALLBACK(EVS_CONF_GET_SRV, int (*callback)(evs_data_dev_config *feedback))
DEFINE_SEVICE_CALLBACK(EVS_MAINTAIN_RESULT_SRV, int (*callback)(evs_service_feedback_maintain_query *feedback))
DEFINE_SEVICE_CALLBACK(EVS_FUN_CONF_UPDATE_SRV, int (*callback)(evs_service_dev_fun_config *request, evs_service_feedback_dev_fun_config *feedback))
DEFINE_SEVICE_CALLBACK(EVS_FUN_CONF_GET_SRV, int (*callback)(evs_service_get_dev_fun_config *request, evs_service_dev_fun_config *feedback))
DEFINE_SEVICE_CALLBACK(EVS_FEE_MODEL_QUERY_SRV, int (*callback)(evs_service_feedback_feeModel_qurey *feedback))
DEFINE_SEVICE_CALLBACK(EVS_BLE_SECRET_UPDATE_SRV, int (*callback)(evs_service_blesecret_update *request, evs_service_feedback_blesecret_update *feedback))
DEFINE_SEVICE_CALLBACK(EVS_BLE_PLUG_CHG_INFO_GET_SRV, int (*callback)(evs_event_ble_plug_charge_info *feedback))
DEFINE_SEVICE_CALLBACK(EVS_BLE_PLUG_CHG_INFO_CONF_SRV, int (*callback)(evs_service_ble_plug_charge_info_conf *request))
DEFINE_SEVICE_CALLBACK(EVS_BLE_AUTH_LIST_CLEAN_SRV, int (*callback)(evs_service_blelist_clean *request, evs_service_feedback_blelist_clean *feedback))
DEFINE_SEVICE_CALLBACK(EVS_BLE_RESET_SRV, int (*callback)(evs_service_feedback_ble_reset *feedback))
DEFINE_SEVICE_CALLBACK(EVS_PILE_PARTS_CONF_SRV, int (*callback)(evs_service_config_parts *request, evs_service_feedback_config_parts *feedback))
DEFINE_SEVICE_CALLBACK(EVS_PILE_PARTS_CONF_GET_SRV, int (*callback)(evs_service_parts_config_get *request, evs_service_feedback_config_parts_get *feedback))
DEFINE_SEVICE_CALLBACK(EVS_VIN_LIST_UPDATE_SRV, int (*callback)(evs_service_vinList_update *request, evs_service_feedback_vinList_update *feedback))
DEFINE_SEVICE_CALLBACK(EVS_ORDER_ASK_SRV, int (*callback)(evs_service_trade_get *request, evs_service_feedback_trade_get *feedback))
DEFINE_SEVICE_CALLBACK(EVS_Meter_ASK_SRV, int (*callback)(evs_service_meter_get *request, evs_service_feedback_meter_get *feedback))
/************直流功率调节控制与交流有序充电共用回调****************/
DEFINE_SEVICE_CALLBACK(EVS_ORDERLY_CHARGE_SRV, int (*callback)(evs_service_orderCharge *request, evs_service_feedback_orderCharge *feedback))
DEFINE_SEVICE_CALLBACK(EVS_QUE_DATA_SRV, int (*callback)(evs_service_query_log *request, evs_service_feedback_query_log *feedback))
/*系统回调函数定义*/
DEFINE_SEVICE_CALLBACK(EVS_OTA_UPDATE, int (*callback)(const char *))
DEFINE_SEVICE_CALLBACK(EVS_TIME_SYNC, int (*callback)(const unsigned int))
DEFINE_SEVICE_CALLBACK(EVS_CONNECT_SUCC, int (*callback)(void))
DEFINE_SEVICE_CALLBACK(EVS_DISCONNECTED, int (*callback)(void))
DEFINE_SEVICE_CALLBACK(EVS_REPORT_REPLY, int (*callback)(const int, const int, const char *, const int))
DEFINE_SEVICE_CALLBACK(EVS_TRIGGER_EVENT_REPLY, int (*callback)(const int, const int, const char *, const int,
const char *, const int))
DEFINE_SEVICE_CALLBACK(EVS_STATE_EVERYTHING, int (*callback)(int ev, const char *msg))
DEFINE_SEVICE_CALLBACK(EVS_CERT_GET, int (*callback)(evs_device_meta *meta))
DEFINE_SEVICE_CALLBACK(EVS_CERT_SET, int (*callback)(const evs_device_meta meta))
DEFINE_SEVICE_CALLBACK(EVS_DEVICE_REG_CODE_GET, int (*callback)(char *device_reg_code))
DEFINE_SEVICE_CALLBACK(EVS_DEVICE_UID_GET, int (*callback)(char *device_uid))
+127
View File
@@ -0,0 +1,127 @@
#ifndef INTERFACE_H
#define INTERFACE_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "protocol_data_def.h"
/*服务类型定义*/
typedef enum
{
/*服务回调*/
EVS_CONF_GET_SRV, // 配置获取服务
EVS_CONF_UPDATE_SRV, // 配置更新服务
EVS_QUE_DATA_SRV, // 设备日志查询
EVS_DEV_MAINTAIN_SRV, // 设备维护服务
EVS_CTRL_LOCK_SRV, // 电子锁控制服务
EVS_FEE_MODEL_UPDATE_SRV, // 计量计费模型更新服务
EVS_START_CHARGE_SRV, // 平台远程启动服务
EVS_AUTH_RESULT_SRV, // 启动充电鉴权结果
EVS_STOP_CHARGE_SRV, // 平台停止充电
EVS_ORDER_CHECK_SRV, // 交易记录确认服务
EVS_RSV_CHARGE_SRV, // 预约充电服务
EVS_GROUND_LOCK_SRV, // 地锁控制服务
EVS_GATE_LOCK_SRV, // 智能门锁控制服务
EVS_ORDERLY_CHARGE_SRV, // 有序充电策略下发
EVS_MAINTAIN_RESULT_SRV, // 维护状态查询结果
EVS_FUN_CONF_UPDATE_SRV, // 功能配置更新服务
EVS_FUN_CONF_GET_SRV, // 功能配置获取服务
EVS_FEE_MODEL_QUERY_SRV, // 计量计费模型查询服务
EVS_BLE_SECRET_UPDATE_SRV, // 蓝牙秘钥更新服务
EVS_BLE_PLUG_CHG_INFO_GET_SRV, // 蓝牙即插即充信息查询服务
EVS_BLE_PLUG_CHG_INFO_CONF_SRV, // 蓝牙即插即充信息确认服务
EVS_BLE_AUTH_LIST_CLEAN_SRV, // 蓝牙鉴权列表清除服务
EVS_BLE_RESET_SRV, // 蓝牙即插即充重置服务
EVS_PILE_PARTS_CONF_SRV, // 充电桩部件配置服务
EVS_PILE_PARTS_CONF_GET_SRV, // 充电桩部件配置查询服务
EVS_VIN_LIST_UPDATE_SRV, // VIN白名单更新服务
EVS_ORDER_ASK_SRV, // 交易记录召测服务
EVS_Meter_ASK_SRV, // 电表底值召测服务
/*系统回调*/
EVS_STATE_EVERYTHING, // sdk状态变化回调
EVS_CONNECT_SUCC, // SDK连接平台成功回调
EVS_DISCONNECTED, // SDK与平台断开连接回调
EVS_REPORT_REPLY, // 属性上报回复回调
EVS_TRIGGER_EVENT_REPLY, // 事件上报回复回调
EVS_CERT_GET, // 证书获取回调
EVS_CERT_SET, // 证书设置回调
EVS_DEVICE_REG_CODE_GET, // 注册码获取回调
EVS_DEVICE_UID_GET, // 设备唯一编码获取回调
EVS_TIME_SYNC, // 时钟同步
EVS_OTA_UPDATE // ota升级
} evs_service_type_t;
#define EVS_RegisterCallback(service, cb) evs_register_for_##service(cb)
#define DECLARE_SEVICE_CALLBACK(service, cb) int evs_register_for_##service(cb);
#define DEFINE_SEVICE_CALLBACK(service, cb) \
int evs_register_for_##service(cb) \
{ \
if (service < 0 || service >= sizeof(g_evs_service_map) / sizeof(evs_service_map_t)) \
{ \
return -1; \
} \
g_evs_service_map[service].callback = (void *)callback; \
return 0; \
}
/*服务回调函数声明*/
DECLARE_SEVICE_CALLBACK(EVS_DEV_MAINTAIN_SRV, int (*cb)(evs_service_dev_maintain *request))
DECLARE_SEVICE_CALLBACK(EVS_CTRL_LOCK_SRV, int (*cb)(evs_service_lockCtrl *request))
DECLARE_SEVICE_CALLBACK(EVS_FEE_MODEL_UPDATE_SRV, int (*cb)(evs_service_issue_feeModel *request, evs_service_feedback_feeModel *feedback))
DECLARE_SEVICE_CALLBACK(EVS_START_CHARGE_SRV, int (*cb)(evs_service_startCharge *request, evs_service_feedback_startCharge *feedback))
DECLARE_SEVICE_CALLBACK(EVS_AUTH_RESULT_SRV, int (*cb)(evs_service_authCharge *request, evs_service_feedback_authCharge *feedback))
DECLARE_SEVICE_CALLBACK(EVS_STOP_CHARGE_SRV, int (*cb)(evs_service_stopCharge *request, evs_service_feedback_stopCharge *feedback))
DECLARE_SEVICE_CALLBACK(EVS_ORDER_CHECK_SRV, int (*cb)(evs_service_confirmTrade *request, void *feedback))
DECLARE_SEVICE_CALLBACK(EVS_RSV_CHARGE_SRV, int (*cb)(evs_service_rsvCharge *request, evs_service_feedback_rsvCharge *feedback))
DECLARE_SEVICE_CALLBACK(EVS_GROUND_LOCK_SRV, int (*cb)(evs_service_groundLock_ctrl *request, evs_service_feedback_groundLock_ctrl *feedback))
DECLARE_SEVICE_CALLBACK(EVS_GATE_LOCK_SRV, int (*cb)(evs_service_gateLock_ctrl *request, evs_service_feedback_gateLock_ctrl *feedback))
DECLARE_SEVICE_CALLBACK(EVS_CONF_UPDATE_SRV, int (*cb)(evs_data_dev_config *request, int *feedback))
DECLARE_SEVICE_CALLBACK(EVS_CONF_GET_SRV, int (*cb)(evs_data_dev_config *feedback))
DECLARE_SEVICE_CALLBACK(EVS_MAINTAIN_RESULT_SRV, int (*cb)(evs_service_feedback_maintain_query *feedback))
DECLARE_SEVICE_CALLBACK(EVS_FUN_CONF_UPDATE_SRV, int (*cb)(evs_service_dev_fun_config *request, evs_service_feedback_dev_fun_config *feedback))
DECLARE_SEVICE_CALLBACK(EVS_FUN_CONF_GET_SRV, int (*cb)(evs_service_get_dev_fun_config *request, evs_service_dev_fun_config *feedback))
DECLARE_SEVICE_CALLBACK(EVS_FEE_MODEL_QUERY_SRV, int (*cb)(evs_service_feedback_feeModel_qurey *feedback))
DECLARE_SEVICE_CALLBACK(EVS_BLE_SECRET_UPDATE_SRV, int (*cb)(evs_service_blesecret_update *request, evs_service_feedback_blesecret_update *feedback))
DECLARE_SEVICE_CALLBACK(EVS_BLE_PLUG_CHG_INFO_GET_SRV, int (*cb)(evs_event_ble_plug_charge_info *feedback))
DECLARE_SEVICE_CALLBACK(EVS_BLE_PLUG_CHG_INFO_CONF_SRV, int (*cb)(evs_service_ble_plug_charge_info_conf *request))
DECLARE_SEVICE_CALLBACK(EVS_BLE_AUTH_LIST_CLEAN_SRV, int (*cb)(evs_service_blelist_clean *request, evs_service_feedback_blelist_clean *feedback))
DECLARE_SEVICE_CALLBACK(EVS_BLE_RESET_SRV, int (*cb)(evs_service_feedback_ble_reset *feedback))
DECLARE_SEVICE_CALLBACK(EVS_PILE_PARTS_CONF_SRV, int (*cb)(evs_service_config_parts *request, evs_service_feedback_config_parts *feedback))
DECLARE_SEVICE_CALLBACK(EVS_PILE_PARTS_CONF_GET_SRV, int (*cb)(evs_service_parts_config_get *request, evs_service_feedback_config_parts_get *feedback))
DECLARE_SEVICE_CALLBACK(EVS_VIN_LIST_UPDATE_SRV, int (*cb)(evs_service_vinList_update *request, evs_service_feedback_vinList_update *feedback))
DECLARE_SEVICE_CALLBACK(EVS_ORDER_ASK_SRV, int (*cb)(evs_service_trade_get *request, evs_service_feedback_trade_get *feedback))
DECLARE_SEVICE_CALLBACK(EVS_Meter_ASK_SRV, int (*cb)(evs_service_meter_get *request, evs_service_feedback_meter_get *feedback))
/************直流功率调节控制与交流有序充电共用回调****************/
DECLARE_SEVICE_CALLBACK(EVS_ORDERLY_CHARGE_SRV, int (*cb)(evs_service_orderCharge *request, evs_service_feedback_orderCharge *feedback))
DECLARE_SEVICE_CALLBACK(EVS_QUE_DATA_SRV, int (*cb)(evs_service_query_log *request, evs_service_feedback_query_log *feedback))
/*系统回调函数声明*/
DECLARE_SEVICE_CALLBACK(EVS_OTA_UPDATE, int (*cb)(const char *))
DECLARE_SEVICE_CALLBACK(EVS_TIME_SYNC, int (*cb)(const unsigned int))
DECLARE_SEVICE_CALLBACK(EVS_CONNECT_SUCC, int (*cb)(void))
DECLARE_SEVICE_CALLBACK(EVS_DISCONNECTED, int (*cb)(void))
DECLARE_SEVICE_CALLBACK(EVS_REPORT_REPLY, int (*cb)(const int, const int, const char *, const int))
DECLARE_SEVICE_CALLBACK(EVS_TRIGGER_EVENT_REPLY, int (*cb)(const int, const int, const char *, const int,
const char *, const int))
DECLARE_SEVICE_CALLBACK(EVS_STATE_EVERYTHING, int (*cb)(int ev, const char *msg))
DECLARE_SEVICE_CALLBACK(EVS_CERT_GET, int (*cb)(evs_device_meta *meta))
DECLARE_SEVICE_CALLBACK(EVS_CERT_SET, int (*cb)(const evs_device_meta meta))
DECLARE_SEVICE_CALLBACK(EVS_DEVICE_REG_CODE_GET, int (*cb)(char *device_reg_code))
DECLARE_SEVICE_CALLBACK(EVS_DEVICE_UID_GET, int (*cb)(char *device_uid))
void *evs_service_callback(int service);
/*设备端接口*/
#ifdef __cplusplus
}
#endif
#endif /* protocol.h */
+183
View File
@@ -0,0 +1,183 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOT_EXPORT_LINKKIT_H_
#define _IOT_EXPORT_LINKKIT_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "wrappers.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;
/**
* @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 state code.
*
*/
int 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);
#if defined(__cplusplus)
}
#endif
#endif
+337
View File
@@ -0,0 +1,337 @@
#include "iotx_cm_internal.h"
#if defined(MQTT_COMM_ENABLED) || defined(MAL_ENABLED)
#include "iotx_cm_mqtt.h"
#endif
#ifdef COAP_COMM_ENABLED
#include "iotx_cm_coap.h"
#endif
static void *fd_lock = NULL;
static iotx_cm_connection_t *_cm_fd[CM_MAX_FD_NUM] = {NULL};
static int _get_fd(iotx_cm_connection_t *handle);
static int _recycle_fd(int fd);
static int inline _fd_is_valid(int fd);
static int inited_conn_num = 0;
#ifdef DEVICE_MODEL_MULTI_THREAD
static void *_iotx_cm_yield_thread_func(void *params);
static void *yield_thread = NULL;
static int yield_task_leave = 1;
#endif
const char ERR_INVALID_PARAMS[] = "invalid parameter";
int iotx_cm_open(iotx_cm_init_param_t *params)
{
int fd;
iotx_cm_connection_t *connection = NULL;
switch (params->protocol_type)
{
case IOTX_CM_PROTOCOL_TYPE_MQTT:
#if defined(MQTT_COMM_ENABLED) || defined(MAL_ENABLED)
connection = iotx_cm_open_mqtt(params);
#endif
break;
case IOTX_CM_PROTOCOL_TYPE_COAP:
#ifdef COAP_COMM_ENABLED
connection = iotx_cm_open_coap(params);
#endif
break;
default:
break;
}
if (connection == NULL)
{
return STATE_DEV_MODEL_CM_OPEN_FAILED;
}
fd = _get_fd(connection);
if (fd < 0)
{
connection->close_func();
return fd;
}
connection->fd = fd;
return fd;
}
int iotx_cm_connect(int fd, uint32_t timeout)
{
iotx_cm_connect_fp connect_func;
int ret;
if (_fd_is_valid(fd) < 0)
{
return STATE_DEV_MODEL_CM_FD_ERROR;
}
HAL_MutexLock(fd_lock);
connect_func = _cm_fd[fd]->connect_func;
HAL_MutexUnlock(fd_lock);
iotx_event_post(IOTX_CONN_CLOUD);
ret = connect_func(timeout);
if (ret == 0)
{
inited_conn_num++;
if (inited_conn_num == 1)
{
#ifdef DEVICE_MODEL_MULTI_THREAD
int stack_used;
hal_os_thread_param_t task_parms = {0};
task_parms.stack_size = 6144;
task_parms.name = "cm_yield";
ret = HAL_ThreadCreate(&yield_thread, _iotx_cm_yield_thread_func, NULL,
&task_parms, &stack_used);
if (ret < 0)
{
inited_conn_num--;
}
#endif
}
iotx_event_post(IOTX_CONN_CLOUD_SUC);
}
else
{
iotx_event_post(IOTX_CONN_CLOUD_FAIL);
}
return ret;
}
static int _iotx_cm_yield(int fd, unsigned int timeout)
{
iotx_cm_yield_fp yield_func;
if (fd_lock == NULL)
{
return STATE_DEV_MODEL_INTERNAL_ERROR;
}
if (fd == -1)
{
int i;
for (i = 0; i < CM_MAX_FD_NUM; i++)
{
yield_func = NULL;
HAL_MutexLock(fd_lock);
if (_cm_fd[i] != NULL)
{
yield_func = _cm_fd[i]->yield_func;
}
HAL_MutexUnlock(fd_lock);
if (yield_func != NULL)
{
yield_func(timeout);
}
}
return STATE_SUCCESS;
}
if (_fd_is_valid(fd) < 0)
{
return STATE_DEV_MODEL_CM_FD_ERROR;
}
HAL_MutexLock(fd_lock);
yield_func = _cm_fd[fd]->yield_func;
HAL_MutexUnlock(fd_lock);
return yield_func(timeout);
}
#ifdef DEVICE_MODEL_MULTI_THREAD
static void *_iotx_cm_yield_thread_func(void *params)
{
yield_task_leave = 0;
while (inited_conn_num > 0)
{
_iotx_cm_yield(-1, CM_DEFAULT_YIELD_TIMEOUT);
}
yield_task_leave = 1;
return NULL;
}
#endif
int iotx_cm_yield(int fd, unsigned int timeout)
{
#ifdef DEVICE_MODEL_MULTI_THREAD
HAL_SleepMs(timeout);
return STATE_SUCCESS;
#else
return _iotx_cm_yield(fd, timeout);
#endif
}
int iotx_cm_sub(int fd, iotx_cm_ext_params_t *ext, const char *topic,
iotx_cm_data_handle_cb topic_handle_func, void *pcontext)
{
iotx_cm_sub_fp sub_func;
if (_fd_is_valid(fd) < 0)
{
return STATE_DEV_MODEL_CM_FD_ERROR;
}
HAL_MutexLock(fd_lock);
sub_func = _cm_fd[fd]->sub_func;
HAL_MutexUnlock(fd_lock);
return sub_func(ext, topic, topic_handle_func, pcontext);
}
int iotx_cm_unsub(int fd, const char *topic)
{
iotx_cm_unsub_fp unsub_func;
if (_fd_is_valid(fd) < 0)
{
return STATE_DEV_MODEL_CM_FD_ERROR;
}
HAL_MutexLock(fd_lock);
unsub_func = _cm_fd[fd]->unsub_func;
HAL_MutexUnlock(fd_lock);
return unsub_func(topic);
}
int iotx_cm_pub(int fd, iotx_cm_ext_params_t *ext, const char *topic, const char *payload, unsigned int payload_len)
{
iotx_cm_pub_fp pub_func;
if (_fd_is_valid(fd) < 0)
{
return STATE_DEV_MODEL_CM_FD_ERROR;
}
HAL_MutexLock(fd_lock);
pub_func = _cm_fd[fd]->pub_func;
HAL_MutexUnlock(fd_lock);
return pub_func(ext, topic, payload, payload_len);
}
int iotx_cm_close(int fd)
{
iotx_cm_close_fp close_func;
if (_fd_is_valid(fd) < 0)
{
return STATE_DEV_MODEL_CM_FD_ERROR;
}
if (inited_conn_num > 0)
{
inited_conn_num--;
}
if (inited_conn_num == 0)
{
#ifdef DEVICE_MODEL_MULTI_THREAD
while (!yield_task_leave)
{
HAL_SleepMs(10);
}
yield_thread = NULL;
#endif
}
HAL_MutexLock(fd_lock);
close_func = _cm_fd[fd]->close_func;
HAL_MutexUnlock(fd_lock);
if (close_func == NULL)
{
return FAIL_RETURN;
}
if (close_func() != 0)
{
return FAIL_RETURN;
}
if (_recycle_fd(fd) != 0)
{
return FAIL_RETURN;
}
if (inited_conn_num == 0)
{
if (fd_lock != NULL)
{
HAL_MutexDestroy(fd_lock);
fd_lock = NULL;
}
}
return 0;
}
static int inline _fd_is_valid(int fd)
{
int ret;
if (fd_lock == NULL)
{
return NULL_VALUE_ERROR;
}
HAL_MutexLock(fd_lock);
ret = (fd >= 0 && fd < CM_MAX_FD_NUM && _cm_fd[fd] != NULL) ? 0 : -1;
HAL_MutexUnlock(fd_lock);
return ret;
}
static int _recycle_fd(int fd)
{
if (fd_lock == NULL)
{
fd_lock = HAL_MutexCreate();
if (fd_lock == NULL)
{
return -1;
}
}
if (fd < 0 || fd > CM_MAX_FD_NUM - 1)
{
return -1;
}
HAL_MutexLock(fd_lock);
_cm_fd[fd] = NULL;
HAL_MutexUnlock(fd_lock);
return 0;
}
static int _get_fd(iotx_cm_connection_t *handle)
{
int i;
if (handle == NULL)
{
return NULL_VALUE_ERROR;
}
if (fd_lock == NULL)
{
fd_lock = HAL_MutexCreate();
if (fd_lock == NULL)
{
return STATE_SYS_DEPEND_MUTEX_CREATE;
}
}
HAL_MutexLock(fd_lock);
for (i = 0; i < CM_MAX_FD_NUM; i++)
{
if (_cm_fd[i] == NULL)
{
_cm_fd[i] = handle;
HAL_MutexUnlock(fd_lock);
return i;
}
}
HAL_MutexUnlock(fd_lock);
return STATE_DEV_MODEL_CM_FD_NOT_FOUND;
}
+136
View File
@@ -0,0 +1,136 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOTX_CM_H_
#define _IOTX_CM_H_
#include "infra_types.h"
#define CM_MAX_FD_NUM 3
#define CM_DEFAULT_YIELD_TIMEOUT 200
/* message confirmation type */
typedef enum {
/* non ACK */
/* MQTT: QoS is 0 */
/* CoAP: NON */
/* default */
IOTX_CM_MESSAGE_NO_ACK,
/* need ACK */
/* MQTT: QoS is 1 */
/* CoAP: CON */
IOTX_CM_MESSAGE_NEED_ACK,
/* non ACK */
/* MQTT: QoS is 3 */
/* CoAP: NONE*/
IOTX_CM_MESSAGE_SUB_LOCAL,
/* Maximum number of ack type */
IOTX_CM_MESSAGE_ACK_MAX
} iotx_cm_ack_types_t;
/* message confirmation type */
typedef enum {
/* non ACK */
/* MQTT: QoS is 0 */
/* CoAP: NON */
/* default */
IOTX_CM_ASYNC,
/* need ACK */
/* MQTT: QoS is 1 */
/* CoAP: CON */
IOTX_CM_SYNC,
/* Maximum number of ack type */
IOTX_CM_SYNC_MAX
} iotx_cm_sync_mode_types_t;
/* protocol type */
typedef enum IOTX_CM_PROTOCOL_TYPES {
/* MQTT */
IOTX_CM_PROTOCOL_TYPE_MQTT = 1,
/* COAP */
IOTX_CM_PROTOCOL_TYPE_COAP = 2,
/* HTTP */
IOTX_CM_PROTOCOL_TYPE_HTTP = 3,
/* HTTP2 */
IOTX_CM_PROTOCOL_TYPE_HTTP2 = 4,
/* Maximum number of protocol type */
IOTX_CM_PROTOCOL_TYPE_MAX
} iotx_cm_protocol_types_t;
/* event type */
typedef enum IOTX_CM_EVENT_TYPES {
/* cloud connected */
IOTX_CM_EVENT_CLOUD_CONNECTED = 0,
/* cloud: disconnect */
/* event_msg is null */
IOTX_CM_EVENT_CLOUD_CONNECT_FAILED,
/* cloud: disconnect */
/* event_msg is null */
IOTX_CM_EVENT_CLOUD_DISCONNECT,
/* event_msg is iotx_cm_event_result_pt */
IOTX_CM_EVENT_SUBCRIBE_SUCCESS,
IOTX_CM_EVENT_SUBCRIBE_FAILED,
IOTX_CM_EVENT_UNSUB_SUCCESS,
IOTX_CM_EVENT_UNSUB_FAILED,
IOTX_CM_EVENT_PUBLISH_SUCCESS,
IOTX_CM_EVENT_PUBLISH_FAILED,
/* Maximum number of event */
IOTX_CM_EVENT_MAX
} iotx_cm_event_types_t;
/* The structure of cloud Connection event struct */
typedef struct {
iotx_cm_event_types_t type;
void *msg;
} iotx_cm_event_msg_t;
typedef struct {
char *topic;
uint8_t *payload;
uint32_t payload_len;
} event_msg_data_t;
#ifdef DEVICE_MODEL_ALINK2
typedef void (*iotx_cm_data_handle_cb)(int fd, const char *topic, uint32_t topic_len, const char *payload, unsigned int payload_len, void *context);
#else
typedef void (*iotx_cm_data_handle_cb)(int fd, const char *topic, const char *payload, unsigned int payload_len,
void *context);
#endif
typedef void (*iotx_cm_event_handle_cb)(int fd, iotx_cm_event_msg_t *event, void *context);
/* IoTx initializa parameters */
typedef struct {
uint32_t request_timeout_ms;
uint32_t keepalive_interval_ms;
uint32_t write_buf_size;
uint32_t read_buf_size;
iotx_cm_protocol_types_t protocol_type;
iotx_cm_event_handle_cb handle_event; /* Specify MQTT event handle */
void *context;
#ifdef DEVICE_MODEL_ALINK2
iotx_dev_meta_info_t *dev_info;
iotx_mqtt_region_types_t region;
#endif
} iotx_cm_init_param_t;
typedef struct {
iotx_cm_ack_types_t ack_type;
iotx_cm_sync_mode_types_t sync_mode;
uint32_t sync_timeout;
iotx_cm_data_handle_cb ack_cb;
void *cb_context;
} iotx_cm_ext_params_t;
int iotx_cm_open(iotx_cm_init_param_t *params);
int iotx_cm_connect(int fd, uint32_t timeout);
int iotx_cm_yield(int fd, unsigned int timeout);
int iotx_cm_sub(int fd, iotx_cm_ext_params_t *ext, const char *topic,
iotx_cm_data_handle_cb topic_handle_func, void *pcontext);
int iotx_cm_unsub(int fd, const char *topic);
int iotx_cm_pub(int fd, iotx_cm_ext_params_t *ext, const char *topic, const char *payload, unsigned int payload_len);
int iotx_cm_close(int fd);
#endif /* _LINKKIT_CM_H_ */
+408
View File
@@ -0,0 +1,408 @@
#include "iotx_cm_internal.h"
#ifdef COAP_COMM_ENABLED
#include "iotx_cm.h"
#include "iotx_cm_coap.h"
#include "infra_timer.h"
#ifdef COAP_DTLS_SUPPORT /* DTLS */
#ifdef ON_DAILY
#define IOTX_COAP_SERVER_URI "coaps://11.239.164.238:5684"
#else
#ifdef ON_PRE
#define IOTX_COAP_SERVER_URI "coaps://pre.coap.cn-shanghai.link.aliyuncs.com:5684"
#else /* online */
#define IOTX_COAP_SERVER_URI "coaps://%s.coap.cn-shanghai.link.aliyuncs.com:5684"
#endif
#endif
#else
#ifdef COAP_PSK_SUPPORT /* PSK */
#ifdef ON_DAILY
#define IOTX_COAP_SERVER_URI "coap-psk://10.101.83.159:5682"
#else
#ifdef ON_PRE
#define IOTX_COAP_SERVER_URI "coap-psk://pre.coap.cn-shanghai.link.aliyuncs.com:5682"
#else /* online */
#define IOTX_COAP_SERVER_URI "coap-psk://%s.coap.cn-shanghai.link.aliyuncs.com:5682"
#endif
#endif
#else /* UDP */
#ifdef ON_DAILY
#define IOTX_COAP_SERVER_URI ""
#else
#ifdef ON_PRE
#define IOTX_COAP_SERVER_URI "coap://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5683"
#else /* online */
#define IOTX_COAP_SERVER_URI "coap://%s.coap.cn-shanghai.link.aliyuncs.com:5683"
#endif
#endif
#endif
#endif
extern uint32_t IOT_CoAP_GetCurToken(iotx_coap_context_t *p_context);
int IOT_CoAP_GetMessageToken(void *p_message, unsigned int *token);
static struct list_head g_coap_response_list = LIST_HEAD_INIT(g_coap_response_list);
static iotx_cm_connection_t *_coap_conncection = NULL;
static int iotx_set_devinfo(iotx_coap_device_info_t *p_devinfo);
static int _coap_connect(uint32_t timeout);
static int _coap_publish(iotx_cm_ext_params_t *params, const char *topic, const char *payload,
unsigned int payload_len);
static int _coap_sub(iotx_cm_ext_params_t *params, const char *topic,
iotx_cm_data_handle_cb topic_handle_func, void *pcontext);
static iotx_msg_type_t _get_coap_qos(iotx_cm_ack_types_t ack_type);
static int _coap_unsub(const char *topic);
static int _coap_close();
static void _set_common_handlers();
iotx_cm_connection_t *iotx_cm_open_coap(iotx_cm_init_param_t *params)
{
iotx_coap_config_t *coap_config = NULL;
iotx_coap_device_info_t *deviceinfo = NULL;
if (_coap_conncection != NULL) {
cm_warning("mqtt connection is opened already,return it");
return _coap_conncection;
}
_coap_conncection = (iotx_cm_connection_t *)cm_malloc(sizeof(iotx_cm_connection_t));
if (_coap_conncection == NULL) {
cm_err("_coap_conncection malloc failed!");
goto failed;
}
_coap_conncection->list_lock = HAL_MutexCreate();
if (_coap_conncection->list_lock == NULL) {
cm_err("list_lock create failed!");
goto failed;
}
coap_config = (iotx_coap_config_t *)cm_malloc(sizeof(iotx_coap_config_t));
if (coap_config == NULL) {
cm_err("coap_config malloc failed!");
goto failed;
}
memset(coap_config, 0, sizeof(iotx_coap_config_t));
deviceinfo = (iotx_coap_device_info_t *)cm_malloc(sizeof(iotx_coap_device_info_t));
if (deviceinfo == NULL) {
cm_err("deviceinfo malloc failed!");
goto failed;
}
_coap_conncection->open_params = coap_config;
memset(deviceinfo, 0, sizeof(iotx_coap_device_info_t));
iotx_set_devinfo(deviceinfo);
coap_config->wait_time_ms = params->request_timeout_ms;
coap_config->p_devinfo = deviceinfo;
/* coap_config->p_url = IOTX_COAP_SERVER_URI; */
_coap_conncection->event_handler = params->handle_event;
_set_common_handlers();
return _coap_conncection;
failed:
if (_coap_conncection != NULL) {
if (_coap_conncection->list_lock != NULL) {
HAL_MutexDestroy(_coap_conncection->list_lock);
}
cm_free(_coap_conncection);
_coap_conncection = NULL;
}
if (coap_config != NULL) {
cm_free(coap_config);
}
if (deviceinfo != NULL) {
cm_free(deviceinfo);
}
return NULL;
}
static int iotx_set_devinfo(iotx_coap_device_info_t *p_devinfo)
{
if (NULL == p_devinfo) {
return IOTX_ERR_INVALID_PARAM;
}
memset(p_devinfo, 0x00, sizeof(iotx_coap_device_info_t));
/**< get device info*/
IOT_Ioctl(IOTX_IOCTL_GET_PRODUCT_KEY, p_devinfo->product_key);
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_NAME, p_devinfo->device_name);
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_SECRET, p_devinfo->device_secret);
HAL_Snprintf(p_devinfo->device_id, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 2, "%s.%s", p_devinfo->product_key, p_devinfo->device_name);
p_devinfo->device_id[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 1] = '\0';
/**< end*/
cm_info("*****The Product Key : %s *****\r\n", p_devinfo->product_key);
cm_info("*****The Device Name : %s *****\r\n", p_devinfo->device_name);
cm_info("*****The Device Secret: %s *****\r\n", p_devinfo->device_secret);
cm_info("*****The Device ID : %s *****\r\n", p_devinfo->device_id);
return IOTX_SUCCESS;
}
static int _coap_connect(uint32_t timeout)
{
int ret;
char url[100] = {0};
iotx_time_t timer;
iotx_coap_config_t *config = NULL;
iotx_coap_context_t *p_ctx = NULL;
char product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0};
if (_coap_conncection == NULL) {
return NULL_VALUE_ERROR;
}
IOT_Ioctl(IOTX_IOCTL_GET_PRODUCT_KEY, product_key);
config = _coap_conncection->open_params;
if (config == NULL) {
return NULL_VALUE_ERROR;
}
HAL_Snprintf(url, 100, IOTX_COAP_SERVER_URI, product_key);
config->p_url = url;
iotx_time_init(&timer);
utils_time_countdown_ms(&timer, timeout);
do {
if (p_ctx == NULL) {
p_ctx = IOT_CoAP_Init(config);
if (NULL == p_ctx) {
continue;
}
}
ret = IOT_CoAP_DeviceNameAuth(p_ctx);
if (ret == 0) {
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_CLOUD_CONNECTED;
event.msg = NULL;
_coap_conncection->context = p_ctx;
if (_coap_conncection->event_handler) {
_coap_conncection->event_handler(_coap_conncection->fd, &event, (void *)_coap_conncection);
}
return 0;
}
} while (!utils_time_is_expired(&timer));
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_CLOUD_CONNECT_FAILED;
event.msg = NULL;
if (_coap_conncection->event_handler) {
_coap_conncection->event_handler(_coap_conncection->fd, &event, (void *)_coap_conncection);
}
}
cm_err("mqtt connect failed");
return -1;
}
static void _coap_response_default(void *p_arg, void *p_message)
{
int ret;
int len = 0;
unsigned char *p_payload = NULL;
unsigned int token;
iotx_coap_resp_code_t resp_code;
coap_response_node_t *node = NULL;
coap_response_node_t *next = NULL;
if (_coap_conncection == NULL || p_message == NULL) {
cm_err("paras err");
return;
}
ret = IOT_CoAP_GetMessageCode(p_message, &resp_code);
if (ret < 0) {
cm_err("get msg code err");
return;
}
cm_info("resp_code = %d", resp_code);
ret = IOT_CoAP_GetMessagePayload(p_message, &p_payload, &len);
if (ret < 0) {
cm_err("get msg payload err");
return;
}
ret = IOT_CoAP_GetMessageToken(p_message, &token);
if (ret < 0) {
cm_err("get msg token err");
return;
}
HAL_MutexLock(_coap_conncection->list_lock);
list_for_each_entry_safe(node, next, &g_coap_response_list, linked_list, coap_response_node_t) {
if (node->token_num == token) {
iotx_cm_data_handle_cb recieve_cb = node->responce_cb;
void *context = node->context;
unsigned int topic_len = strlen(node->topic) + 1;
char *topic = cm_malloc(topic_len);
if (topic == NULL) {
cm_err("topic malloc failed");
continue;
}
memset(topic, 0, topic_len);
strncpy(topic, node->topic, topic_len);
list_del(&node->linked_list);
cm_free(node->topic);
cm_free(node);
HAL_MutexUnlock(_coap_conncection->list_lock); /* do not lock while callback */
recieve_cb(_coap_conncection->fd, topic, (const char *)p_payload, len, context);
/* recieve_cb(_coap_conncection->fd, &msg, context); */
cm_free(topic);
HAL_MutexLock(_coap_conncection->list_lock);
}
}
HAL_MutexUnlock(_coap_conncection->list_lock);
}
static int _coap_publish(iotx_cm_ext_params_t *ext, const char *topic, const char *payload, unsigned int payload_len)
{
iotx_msg_type_t qos = 0;
iotx_message_t message;
uint32_t token;
int topic_len;
int ret;
if (_coap_conncection == NULL) {
return NULL_VALUE_ERROR;
}
if (ext != NULL) {
qos = _get_coap_qos(ext->ack_type);
}
memset(&message, 0, sizeof(iotx_message_t));
message.p_payload = (unsigned char *)payload;
message.payload_len = payload_len;
message.resp_callback = _coap_response_default;
message.msg_type = qos;
message.content_type = IOTX_CONTENT_TYPE_JSON;
token = IOT_CoAP_GetCurToken((iotx_coap_context_t *)_coap_conncection->context);
ret = IOT_CoAP_SendMessage((iotx_coap_context_t *)_coap_conncection->context, (char *)topic, &message);
if (ret < 0) {
return -1;
}
if (ext != NULL && ext->ack_cb != NULL) {
coap_response_node_t *node;
node = (coap_response_node_t *)cm_malloc(sizeof(coap_response_node_t));
if (node == NULL) {
return -1;
}
memset(node, 0, sizeof(coap_response_node_t));
topic_len = strlen(topic) + 1;
node->topic = (char *)cm_malloc(topic_len);
if (node->topic == NULL) {
cm_free(node);
return -1;
}
memset(node->topic, 0, topic_len);
strncpy(node->topic, topic, topic_len);
node->user_data = _coap_conncection;
node->responce_cb = ext->ack_cb;
node->context = ext->cb_context;
node->token_num = token;
HAL_MutexLock(_coap_conncection->list_lock);
list_add_tail(&node->linked_list, &g_coap_response_list);
HAL_MutexUnlock(_coap_conncection->list_lock);
}
return 0;
}
static int _coap_yield(uint32_t timeout)
{
if (_coap_conncection == NULL) {
return NULL_VALUE_ERROR;
}
return IOT_CoAP_Yield((iotx_coap_context_t *)_coap_conncection->context);
}
static int _coap_sub(iotx_cm_ext_params_t *ext, const char *topic,
iotx_cm_data_handle_cb topic_handle_func, void *pcontext)
{
return 0;
}
static int _coap_unsub(const char *topic)
{
return 0;
}
static int _coap_close()
{
coap_response_node_t *node = NULL;
coap_response_node_t *next = NULL;
iotx_coap_config_t *coap_config = NULL;
if (_coap_conncection == NULL) {
return NULL_VALUE_ERROR;
}
coap_config = (iotx_coap_config_t *)_coap_conncection->open_params;
HAL_MutexLock(_coap_conncection->list_lock);
list_for_each_entry_safe(node, next, &g_coap_response_list, linked_list, coap_response_node_t) {
cm_free(node->topic);
list_del(&node->linked_list);
cm_free(node);
}
HAL_MutexUnlock(_coap_conncection->list_lock);
if (_coap_conncection->list_lock != NULL) {
HAL_MutexDestroy(_coap_conncection->list_lock);
}
cm_free(coap_config->p_devinfo);
cm_free(coap_config);
IOT_CoAP_Deinit(&_coap_conncection->context);
cm_free(_coap_conncection);
_coap_conncection = NULL;
return 0;
}
static iotx_msg_type_t _get_coap_qos(iotx_cm_ack_types_t ack_type)
{
switch (ack_type) {
case IOTX_CM_MESSAGE_NO_ACK:
return IOTX_MESSAGE_NON;
case IOTX_CM_MESSAGE_NEED_ACK:
return IOTX_MESSAGE_CON;
default:
return IOTX_MESSAGE_CON;
}
}
static void _set_common_handlers()
{
if (_coap_conncection != NULL) {
_coap_conncection->connect_func = _coap_connect;
_coap_conncection->sub_func = _coap_sub;
_coap_conncection->unsub_func = _coap_unsub;
_coap_conncection->pub_func = _coap_publish;
_coap_conncection->yield_func = _coap_yield;
_coap_conncection->close_func = _coap_close;
}
}
#endif
+24
View File
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOTX_CM_COAP_H_
#define _IOTX_CM_COAP_H_
#include "iotx_cm.h"
#include "iotx_cm_internal.h"
#include "coap_api.h"
typedef struct {
uint32_t token_num;
void *user_data;
char *topic;
iotx_cm_data_handle_cb responce_cb;
void *context;
dlist_t linked_list;
} coap_response_node_t;
iotx_cm_connection_t *iotx_cm_open_coap(iotx_cm_init_param_t *params);
#endif /* _LINKKIT_CM_H_ */
+75
View File
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOTX_CM_INTERNAL_H_
#define _IOTX_CM_INTERNAL_H_
#include <string.h>
#include "infra_config.h"
#include "infra_types.h"
#include "infra_defs.h"
#include "infra_list.h"
#include "infra_compat.h"
#include "infra_timer.h"
#include "infra_state.h"
#include "wrappers.h"
#include "mqtt_api.h"
#include "iotx_cm.h"
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#define cm_malloc(size) LITE_malloc(size, MEM_MAGIC, "cm")
#define cm_free(ptr) LITE_free(ptr)
#else
#define cm_malloc(size) HAL_Malloc(size)
#define cm_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
#endif
#ifdef INFRA_LOG
#include "infra_log.h"
#define cm_debug(...) log_debug("CM", __VA_ARGS__)
#define cm_info(...) log_info("CM", __VA_ARGS__)
#define cm_warning(...) log_warning("CM", __VA_ARGS__)
#define cm_err(...) log_err("CM", __VA_ARGS__)
#else
#define cm_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define cm_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define cm_warning(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define cm_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#endif
typedef int (*iotx_cm_connect_fp)(uint32_t timeout);
typedef int (*iotx_cm_yield_fp)(unsigned int timeout);
typedef int (*iotx_cm_sub_fp)(iotx_cm_ext_params_t *params, const char *topic,
iotx_cm_data_handle_cb topic_handle_func, void *pcontext);
typedef int (*iotx_cm_unsub_fp)(const char *topic);
typedef int (*iotx_cm_pub_fp)(iotx_cm_ext_params_t *params, const char *topic, const char *payload,
unsigned int payload_len);
typedef int (*iotx_cm_close_fp)();
typedef struct iotx_connection_st {
int fd;
void *open_params;
void *context;
void *list_lock;
iotx_cm_protocol_types_t protocol_type;
iotx_cm_connect_fp connect_func;
iotx_cm_sub_fp sub_func;
iotx_cm_unsub_fp unsub_func;
iotx_cm_pub_fp pub_func;
iotx_cm_yield_fp yield_func;
iotx_cm_close_fp close_func;
iotx_cm_event_handle_cb event_handler;
void *cb_data;
} iotx_cm_connection_t;
#include "iotx_cm_mqtt.h"
extern const char ERR_INVALID_PARAMS[];
#endif /* _LINKKIT_CM_H_ */
+458
View File
@@ -0,0 +1,458 @@
#include "iotx_cm_internal.h"
#if defined(MQTT_COMM_ENABLED) || defined(MAL_ENABLED)
static iotx_cm_connection_t *_mqtt_conncection = NULL;
static char mqtt_customzie_info[IOTX_CUSTOMIZE_INFO_LEN + 1] = {0};
static uint16_t mqtt_customzie_port = 0;
static void iotx_cloud_conn_mqtt_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg);
static int _mqtt_connect(uint32_t timeout);
static int _mqtt_publish(iotx_cm_ext_params_t *params, const char *topic, const char *payload,
unsigned int payload_len);
static int _mqtt_sub(iotx_cm_ext_params_t *params, const char *topic,
iotx_cm_data_handle_cb topic_handle_func, void *pcontext);
static iotx_mqtt_qos_t _get_mqtt_qos(iotx_cm_ack_types_t ack_type);
static int _mqtt_unsub(const char *topic);
static int _mqtt_close(void);
static void _set_common_handlers(void);
int IOCTL_FUNC(IOTX_IOCTL_SET_CUSTOMIZE_INFO, void *data)
{
int res;
if (data == NULL)
{
return FAIL_RETURN;
}
if (strlen((char *)data) > IOTX_CUSTOMIZE_INFO_LEN)
{
return FAIL_RETURN;
}
memset(mqtt_customzie_info, 0, strlen((char *)data) + 1);
memcpy(mqtt_customzie_info, data, strlen((char *)data));
res = SUCCESS_RETURN;
return res;
}
int IOCTL_FUNC(IOTX_IOCTL_SET_MQTT_PORT, void *data)
{
int res;
if (data == NULL)
{
return FAIL_RETURN;
}
mqtt_customzie_port = *(uint16_t *)data;
res = SUCCESS_RETURN;
return res;
}
iotx_cm_connection_t *iotx_cm_open_mqtt(iotx_cm_init_param_t *params)
{
iotx_mqtt_param_t *mqtt_param = NULL;
if (_mqtt_conncection != NULL)
{
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_INTERNAL_MQTT_DUP_INIT, NULL);
return _mqtt_conncection;
}
_mqtt_conncection = (iotx_cm_connection_t *)cm_malloc(sizeof(iotx_cm_connection_t));
if (_mqtt_conncection == NULL)
{
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_SYS_DEPEND_MALLOC, NULL);
goto failed;
}
memset(_mqtt_conncection, 0, sizeof(iotx_cm_connection_t));
mqtt_param = (iotx_mqtt_param_t *)cm_malloc(sizeof(iotx_mqtt_param_t));
if (mqtt_param == NULL)
{
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_SYS_DEPEND_MALLOC, NULL);
goto failed;
}
memset(mqtt_param, 0, sizeof(iotx_mqtt_param_t));
mqtt_param->request_timeout_ms = params->request_timeout_ms;
mqtt_param->clean_session = 0;
mqtt_param->keepalive_interval_ms = params->keepalive_interval_ms;
mqtt_param->read_buf_size = params->read_buf_size;
mqtt_param->write_buf_size = params->write_buf_size;
mqtt_param->handle_event.h_fp = iotx_cloud_conn_mqtt_event_handle;
mqtt_param->handle_event.pcontext = NULL;
_mqtt_conncection->open_params = mqtt_param;
_mqtt_conncection->event_handler = params->handle_event;
_mqtt_conncection->cb_data = params->context;
_set_common_handlers();
return _mqtt_conncection;
failed:
if (_mqtt_conncection != NULL)
{
cm_free(_mqtt_conncection);
_mqtt_conncection = NULL;
}
if (mqtt_param != NULL)
{
cm_free(mqtt_param);
}
return NULL;
}
static void iotx_cloud_conn_mqtt_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
uintptr_t packet_id = (uintptr_t)msg->msg;
if (_mqtt_conncection == NULL)
{
return;
}
switch (msg->event_type)
{
case IOTX_MQTT_EVENT_DISCONNECT:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_CLOUD_DISCONNECT;
event.msg = NULL;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_RECONNECT:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_CLOUD_CONNECTED;
event.msg = NULL;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_SUBCRIBE_SUCCESS;
event.msg = (void *)packet_id;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_SUBCRIBE_NACK:
case IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_SUBCRIBE_FAILED;
event.msg = (void *)packet_id;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_UNSUB_SUCCESS;
event.msg = (void *)packet_id;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_UNSUBCRIBE_NACK:
case IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_UNSUB_FAILED;
event.msg = (void *)packet_id;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_PUBLISH_SUCCESS:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_PUBLISH_SUCCESS;
event.msg = (void *)packet_id;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_PUBLISH_NACK:
case IOTX_MQTT_EVENT_PUBLISH_TIMEOUT:
{
iotx_cm_event_msg_t event;
event.type = IOTX_CM_EVENT_PUBLISH_FAILED;
event.msg = (void *)packet_id;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, _mqtt_conncection->cb_data);
}
}
break;
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
{
iotx_mqtt_topic_info_pt topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
iotx_cm_data_handle_cb topic_handle_func = (iotx_cm_data_handle_cb)pcontext;
char *topic = NULL;
if (topic_handle_func == NULL)
{
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_RECV_UNEXP_MQTT_PUB, "unexpected pub message received");
return;
}
topic = cm_malloc(topic_info->topic_len + 1);
if (topic == NULL)
{
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_SYS_DEPEND_MALLOC, NULL);
return;
}
memset(topic, 0, topic_info->topic_len + 1);
memcpy(topic, topic_info->ptopic, topic_info->topic_len);
topic_handle_func(_mqtt_conncection->fd, topic, topic_info->payload, topic_info->payload_len, NULL);
cm_free(topic);
}
break;
case IOTX_MQTT_EVENT_BUFFER_OVERFLOW:
iotx_state_event(ITE_STATE_DEV_MODEL, STATE_DEV_MODEL_INTERNAL_ERROR, "mqtt read buffer too small");
break;
default:
break;
}
}
static int _mqtt_connect(uint32_t timeout)
{
void *pclient;
iotx_time_t timer;
iotx_cm_event_msg_t event;
char product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char device_name[IOTX_DEVICE_NAME_LEN + 1] = {0};
char device_secret[IOTX_DEVICE_SECRET_LEN + 1] = {0};
if (_mqtt_conncection == NULL)
{
return STATE_DEV_MODEL_INTERNAL_MQTT_NOT_INIT_YET;
}
IOT_Ioctl(IOTX_IOCTL_GET_PRODUCT_KEY, product_key);
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_NAME, device_name);
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_SECRET, device_secret);
if (strlen(product_key) == 0)
{
return STATE_USER_INPUT_PK;
}
if (strlen(device_name) == 0)
{
return STATE_USER_INPUT_DN;
}
iotx_time_init(&timer);
utils_time_countdown_ms(&timer, timeout);
if (mqtt_customzie_info[0] != '\0')
{
((iotx_mqtt_param_t *)_mqtt_conncection->open_params)->customize_info = mqtt_customzie_info;
}
if (mqtt_customzie_port != 0)
{
((iotx_mqtt_param_t *)_mqtt_conncection->open_params)->port = mqtt_customzie_port;
}
do
{
pclient = IOT_MQTT_Construct((iotx_mqtt_param_t *)_mqtt_conncection->open_params);
if (pclient != NULL)
{
iotx_cm_event_msg_t event;
_mqtt_conncection->context = pclient;
event.type = IOTX_CM_EVENT_CLOUD_CONNECTED;
event.msg = NULL;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, (void *)_mqtt_conncection);
}
HAL_Printf("mqtt connect success!!!!\n");
return STATE_SUCCESS;
}
HAL_SleepMs(500);
} while (!utils_time_is_expired(&timer));
event.type = IOTX_CM_EVENT_CLOUD_CONNECT_FAILED;
event.msg = NULL;
if (_mqtt_conncection->event_handler)
{
_mqtt_conncection->event_handler(_mqtt_conncection->fd, &event, (void *)_mqtt_conncection);
}
HAL_Printf("mqtt comm timeout\n");
return STATE_DEV_MODEL_MQTT_CONNECT_FAILED;
}
static int _mqtt_publish(iotx_cm_ext_params_t *ext, const char *topic, const char *payload, unsigned int payload_len)
{
int qos = 0;
if (_mqtt_conncection == NULL)
{
return STATE_DEV_MODEL_INTERNAL_MQTT_NOT_INIT_YET;
}
if (ext != NULL)
{
qos = (int)_get_mqtt_qos(ext->ack_type);
}
return IOT_MQTT_Publish_Simple(_mqtt_conncection->context, topic, qos, (void *)payload, payload_len);
}
static int _mqtt_yield(uint32_t timeout)
{
if (_mqtt_conncection == NULL)
{
return STATE_DEV_MODEL_INTERNAL_MQTT_NOT_INIT_YET;
}
return IOT_MQTT_Yield(_mqtt_conncection->context, timeout);
}
static int _mqtt_sub(iotx_cm_ext_params_t *ext, const char *topic,
iotx_cm_data_handle_cb topic_handle_func, void *pcontext)
{
int sync = 0;
iotx_mqtt_qos_t qos = IOTX_MQTT_QOS0;
int timeout = 0;
int ret;
if (_mqtt_conncection == NULL)
{
return STATE_DEV_MODEL_INTERNAL_MQTT_NOT_INIT_YET;
}
if (ext != NULL)
{
if (ext->sync_mode == IOTX_CM_ASYNC)
{
sync = 0;
}
else
{
sync = 1;
timeout = ext->sync_timeout;
}
qos = (int)_get_mqtt_qos(ext->ack_type);
}
if (sync != 0)
{
ret = IOT_MQTT_Subscribe_Sync(_mqtt_conncection->context,
topic,
qos,
iotx_cloud_conn_mqtt_event_handle,
(void *)topic_handle_func,
timeout);
}
else
{
ret = IOT_MQTT_Subscribe(_mqtt_conncection->context,
topic,
qos,
iotx_cloud_conn_mqtt_event_handle,
(void *)topic_handle_func);
}
return ret;
}
static int _mqtt_unsub(const char *topic)
{
if (_mqtt_conncection == NULL)
{
return STATE_DEV_MODEL_INTERNAL_MQTT_NOT_INIT_YET;
}
return IOT_MQTT_Unsubscribe(_mqtt_conncection->context, topic);
}
static int _mqtt_close()
{
if (_mqtt_conncection == NULL)
{
return STATE_DEV_MODEL_INTERNAL_MQTT_NOT_INIT_YET;
}
cm_free(_mqtt_conncection->open_params);
IOT_MQTT_Destroy(&_mqtt_conncection->context);
cm_free(_mqtt_conncection);
_mqtt_conncection = NULL;
return STATE_SUCCESS;
}
static iotx_mqtt_qos_t _get_mqtt_qos(iotx_cm_ack_types_t ack_type)
{
switch (ack_type)
{
case IOTX_CM_MESSAGE_NO_ACK:
return IOTX_MQTT_QOS0;
case IOTX_CM_MESSAGE_NEED_ACK:
return IOTX_MQTT_QOS1;
case IOTX_CM_MESSAGE_SUB_LOCAL:
return IOTX_MQTT_QOS3_SUB_LOCAL;
default:
return IOTX_MQTT_QOS0;
}
}
static void _set_common_handlers()
{
if (_mqtt_conncection != NULL)
{
_mqtt_conncection->connect_func = _mqtt_connect;
_mqtt_conncection->sub_func = _mqtt_sub;
_mqtt_conncection->unsub_func = _mqtt_unsub;
_mqtt_conncection->pub_func = _mqtt_publish;
_mqtt_conncection->yield_func = (iotx_cm_yield_fp)_mqtt_yield;
_mqtt_conncection->close_func = _mqtt_close;
}
}
#endif
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOTX_CM_MQTT_H_
#define _IOTX_CM_MQTT_H_
#include "iotx_cm.h"
#include "iotx_cm_internal.h"
typedef struct {
uintptr_t packet_id;
char * topic;
void * user_data;
iotx_mqtt_event_handle_func_fpt sub_state_cb;
iotx_cm_data_handle_cb sub_recieve_cb;
dlist_t linked_list;
} mqtt_sub_node_t;
iotx_cm_connection_t *iotx_cm_open_mqtt(iotx_cm_init_param_t *params);
#endif /* _LINKKIT_CM_H_ */
+296
View File
@@ -0,0 +1,296 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#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_PROXY_PRODUCT_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_CLOUD_ERROR,
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_error_code_t;
#define IOTX_DM_POST_PROPERTY_ALL (NULL)
int iotx_dm_open(void);
int iotx_dm_connect(_IN_ iotx_dm_init_params_t *init_params);
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);
int iotx_dm_set_opt(int opt, void *data);
int iotx_dm_get_opt(int opt, void *data);
int iotx_dm_subscribe_user_topic(char *topic, void *user_callback);
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
#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);
#ifdef DEVICE_HISTORY_POST
int iotx_dm_post_history(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len);
#endif /* #ifdef DEVICE_HISTORY_POST */
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);
#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 product_secret[IOTX_PRODUCT_SECRET_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_proxy_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
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOTX_DM_CONFIG_H_
#define _IOTX_DM_CONFIG_H_
#define IOTX_DM_CLIENT_CONNECT_TIMEOUT_MS (10000)
#define IOTX_DM_CLIENT_SUB_RETRY_MAX_COUNTS (3)
#define IOTX_DM_CLIENT_SUB_TIMEOUT_MS (5000)
#define IOTX_DM_CLIENT_REQUEST_TIMEOUT_MS (5000)
#define IOTX_DM_CLIENT_KEEPALIVE_INTERVAL_MS (30000)
#define CONFIG_SERVICE_LIST_MAXLEN (30)
#define CONFIG_SERVICE_REQUEST_TIMEOUT (20000)
#ifndef CONFIG_MQTT_TX_MAXLEN
#define CONFIG_MQTT_TX_MAXLEN (3072)
#endif
#ifndef CONFIG_MQTT_RX_MAXLEN
#define CONFIG_MQTT_RX_MAXLEN (3072)
#endif
#ifndef CONFIG_DISPATCH_QUEUE_MAXLEN
#define CONFIG_DISPATCH_QUEUE_MAXLEN (50)
#endif
#ifndef CONFIG_DISPATCH_PACKET_MAXCOUNT
#define CONFIG_DISPATCH_PACKET_MAXCOUNT (0)
#endif
#ifndef CONFIG_MSGCACHE_QUEUE_MAXLEN
#define CONFIG_MSGCACHE_QUEUE_MAXLEN (50)
#endif
#ifndef CONFIG_FOTA_RETRY_INTERNAL_MS
#define CONFIG_FOTA_RETRY_INTERNAL_MS (100)
#endif
#endif
+129
View File
@@ -0,0 +1,129 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef _IOTX_DM_INTERNAL_H_
#define _IOTX_DM_INTERNAL_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "infra_compat.h"
#include "infra_config.h"
#include "infra_types.h"
#include "infra_defs.h"
#include "infra_list.h"
#include "infra_cjson.h"
#include "infra_report.h"
#include "infra_string.h"
#include "infra_state.h"
#if defined(DEVICE_MODEL_GATEWAY)
#include "infra_sha256.h"
#endif
#ifndef _IN_
#define _IN_
#endif
#ifndef _OU_
#define _OU_
#endif
#ifndef DM_READ_ONLY
#define DM_READ_ONLY
#endif
#include <stdlib.h>
#if defined(OTA_ENABLED) && !defined(BUILD_AOS)
#include "iotx_ota.h"
#include "ota_api.h"
#endif
/* CM Header File */
#include "iotx_cm.h"
/* ALCS Header File */
#ifdef ALCS_ENABLED
#include "CoAPExport.h"
#include "iotx_alcs.h"
#include "dm_server_adapter.h"
#include "dm_server.h"
#endif
/* DM Header File */
#include "wrappers.h"
#include "iotx_dm_config.h"
#include "iotx_dm.h"
#include "dm_utils.h"
#include "dm_shadow.h"
#include "dm_tsl_alink.h"
#include "dm_message_cache.h"
#include "dm_opt.h"
#include "dm_ota.h"
#include "dm_cota.h"
#include "dm_fota.h"
#include "dm_ipc.h"
#include "dm_message.h"
#include "dm_msg_process.h"
#include "dm_manager.h"
#include "dm_client_adapter.h"
#include "dm_client.h"
#include "dm_intf.h"
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#define DM_malloc(size) LITE_malloc(size, MEM_MAGIC, "dm")
#define DM_free(ptr) LITE_free(ptr)
#else
#define DM_malloc(size) HAL_Malloc(size)
#define DM_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
#endif
#if defined(COAP_COMM_ENABLED) && !defined(MQTT_COMM_ENABLED)
#define DM_URI_OFFSET 1
#else
#define DM_URI_OFFSET 0
#endif
#ifdef INFRA_LOG
#include "infra_log.h"
#define dm_log_emerg(...) log_emerg("DM", __VA_ARGS__)
#define dm_log_crit(...) log_crit("DM", __VA_ARGS__)
#define dm_log_err(...) log_err("DM", __VA_ARGS__)
#define dm_log_warning(...) log_warning("DM", __VA_ARGS__)
#define dm_log_info(...) log_info("DM", __VA_ARGS__)
#define dm_log_debug(...) log_debug("DM", __VA_ARGS__)
#else
#define dm_log_emerg(...)
#define dm_log_crit(...)
#define dm_log_err(...)
#define dm_log_warning(...)
#define dm_log_info(...)
#define dm_log_debug(...)
#define HEXDUMP_INFO(...)
#define HEXDUMP_DEBUG(...)
#endif
#ifdef LOG_REPORT_TO_CLOUD
#define LOG_POLL_SIZE (CONFIG_MQTT_TX_MAXLEN - 174)
#define REPORT_LEN (LOG_POLL_SIZE - 110)
#define OVERFLOW_LEN (LOG_POLL_SIZE - 10)
typedef enum {
READY,
RUNNING,
DONE
} REPORT_STATE;
unsigned int add_tail();
int reset_log_poll();
int remove_log_poll();
unsigned int push_log(const char *perform_data, int perform_data_size);
#endif
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
void iotx_linkkit_service_list_overtime_handle(void);
#endif
#endif
+3
View File
@@ -0,0 +1,3 @@
void get_msgid(char *payload, int is_cloud);
int check_target_msg(const char *input, int len);
void send_permance_info(char *input, int input_len, char *comments, int report_format);
+370
View File
@@ -0,0 +1,370 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef LINKKIT_EXPORT_H
#define LINKKIT_EXPORT_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <stdint.h>
typedef void (*handle_post_cb_fp_t)(const void *thing_id, int respons_id, int code, const char *response_message,
void *ctx);
typedef void (*handle_subdev_cb_fp_t)(const void *thing_id, int code, const char *response_message, int success,
void *ctx);
typedef struct _linkkit_ops {
#ifdef LOCAL_CONN_ENABLE
int (*on_connect)(void *ctx, int cloud); /* true: cloud connection; false: local connection */
int (*on_disconnect)(void *ctx, int cloud); /* true: cloud connection; false: local connection */
#else
int (*on_connect)(void *ctx); /* true: cloud connection; false: local connection */
int (*on_disconnect)(void *ctx); /* true: cloud connection; false: local connection */
#endif
int (*raw_data_arrived)(const void *thing_id, const void *data, int len, void *ctx);
int (*thing_create)(const void *thing_id, void *ctx);
int (*thing_enable)(const void *thing_id, void *ctx);
int (*thing_disable)(const void *thing_id, void *ctx);
int (*thing_call_service)(const void *thing_id, const char *service, int request_id, void *ctx);
int (*thing_prop_changed)(const void *thing_id, const char *property, void *ctx);
int (*linkit_data_arrived)(const void *thing_id, const void *data, int len, void *ctx);
} linkkit_ops_t;
typedef enum _linkkit_loglevel {
linkkit_loglevel_emerg = 0,
linkkit_loglevel_crit,
linkkit_loglevel_error,
linkkit_loglevel_warning,
linkkit_loglevel_info,
linkkit_loglevel_debug,
} linkkit_loglevel_t;
/* domain type */
/* please sync with dm_cloud_domain_type_t */
typedef enum {
/* shanghai */
linkkit_cloud_domain_shanghai,
/* singapore */
linkkit_cloud_domain_singapore,
/* japan */
linkkit_cloud_domain_japan,
/* america */
linkkit_cloud_domain_america,
/* germany */
linkkit_cloud_domain_germany,
linkkit_cloud_domain_max,
} linkkit_cloud_domain_type_t;
/* device info related operation */
typedef enum {
linkkit_extended_info_operate_update,
linkkit_extended_info_operate_delete,
linkkit_deviceinfo_operate_max,
} linkkit_extended_info_operate_t;
/**
* @brief dispatch message of queue for further process.
*
* @return void*
*/
void *linkkit_dispatch(void);
typedef enum {
linkkit_opt_property_post_reply, /* data type: int */
linkkit_opt_event_post_reply, /* data type: int */
linkkit_opt_property_set_reply /* data type: int */
} linkkit_opt_t;
/**
* @brief get leave signal.
*
*
* @return int,0 no leave signal, 1 get a leave signal
*/
int being_deprecated linkkit_is_try_leave();
/**
* @brief start linkkit routines, and install callback funstions(async type for cloud connecting).
*
* @param opt, specify the option need to be set.
* @param data, specify the option value.
*
* @return int, 0 when success, -1 when fail.
*/
int being_deprecated linkkit_set_opt(linkkit_opt_t opt, void *data);
/**
* @brief start linkkit routines, and install callback funstions(async type for cloud connecting).
*
* @param max_buffered_msg, specify max buffered message size.
* @param ops, callback function struct to be installed.
* @param get_tsl_from_cloud, config if device need to get tsl from cloud(!0) or local(0), if local selected, must invoke linkkit_set_tsl to tell tsl to dm after start complete.
* @param log_level, config log level.
* @param user_context, user context pointer.
* @param domain_type, specify the could server domain.
*
* @return int, 0 when success, -1 when fail.
*/
int being_deprecated linkkit_start(int max_buffered_msg, int get_tsl_from_cloud,
linkkit_loglevel_t log_level,
linkkit_ops_t *ops,
linkkit_cloud_domain_type_t domain_type, void *user_context);
/**
* @brief stop linkkit routines.
*
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_end(void);
/**
* @brief install user tsl.
*
* @param tsl, tsl string that contains json description for thing object.
* @param tsl_len, tsl string length.
*
* @return pointer to thing object, NULL when fails.
*/
void *linkkit_set_tsl(const char *tsl, int tsl_len);
/* patterns: */
/* method:
* set_property_/event_output_/service_output_value:
* method_set, thing_id, identifier, value */
typedef enum {
linkkit_method_set_property_value = 0,
linkkit_method_set_event_output_value,
linkkit_method_set_service_output_value,
linkkit_method_set_number,
} linkkit_method_set_t;
/**
* @brief set value to property, event output, service output items.
* if identifier is struct type or service output type or event output type, use '.' as delimeter like "identifier1.ientifier2"
* to point to specific item.
* value and value_str could not be NULL at the same time;
* if value and value_str both as not NULL, value shall be used and value_str will be ignored.
* if value is NULL, value_str not NULL, value_str will be used.
* in brief, value will be used if not NULL, value_str will be used only if value is NULL.
*
* @param method_set, specify set value type.
* @param thing_id, pointer to thing object, specify which thing to set.
* @param identifier, property, event output, service output identifier.
* @param value. The value to be set, data type decided by data type of property as follows:
* int: int*, float: float*, double: double*,
* text: char*, enum: int*, date: char*, bool: int*
*
* @param value_str, value to set in string format if value is null.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_set_value(linkkit_method_set_t method_set, const void *thing_id,
const char *identifier,
const void *value, const char *value_str);
typedef enum {
linkkit_method_get_property_value = 0,
linkkit_method_get_event_output_value,
linkkit_method_get_service_input_value,
linkkit_method_get_service_output_value,
linkkit_method_get_number,
} linkkit_method_get_t;
/**
* @brief get value from property, event output, service input/output items.
* if identifier is struct type or service input/output type or event output type, use '.' as delimeter like "identifier1.ientifier2"
* to point to specific item.
* value and value_str could not be NULL at the same time;
* if value and value_str both as not NULL, value shall be used and value_str will be ignored.
* if value is NULL, value_str not NULL, value_str will be used.
* in brief, value will be used if not NULL, value_str will be used only if value is NULL.
* @param method_get, specify get value type.
* @param thing_id, pointer to thing object, specify which thing to get.
* @param identifier, property, event output, service input/output identifier.
* @param value. The variable to store value, data type decided by data type of property as follows:
* int: int*, float: float*, double: double*,
* text: char**, enum: int*, date: char**, bool: int*
*
* @param value_str, value to get in string format. If success, memory of *value_str will be allocated,
* user should free the memory.
*
* @warning if data type is text or date, *value well be end with '\0'.
* the memory allocated to *value must be free by user.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_get_value(linkkit_method_get_t method_get, const void *thing_id,
const char *identifier,
void *value, char **value_str);
/**
* @brief answer to a service when a service requested by cloud.
*
* @param thing_id, pointer to thing object.
* @param service_identifier, service identifier to answer, user should get this identifier from handle_dm_callback_fp_t type callback
* report that "dm_callback_type_service_requested" happened, use this function to generate response to the service sender.
* @param response_id, id value in response payload. its value is from "dm_callback_type_service_requested" type callback function.
* use the same id as the request to send response as the same communication session.
* @param code, code value in response payload. for example, 200 when service successfully executed, 400 when not successfully executed.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_answer_service(const void *thing_id, const char *service_identifier,
int response_id, int code);
/**
* @brief answer a down raw service when a raw service requested by cloud, or invoke a up raw service to cloud.
*
* @param thing_id, pointer to thing object.
* @param is_up_raw, specify up raw(not 0) or down raw reply(0).
* @param raw_data, raw data that sent to cloud.
* @param raw_data_length, raw data length that sent to cloud.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_invoke_raw_service(const void *thing_id, int is_up_raw, void *raw_data,
int raw_data_length);
/**
* @brief trigger extended info update procedure.
*
* @param thing_id, pointer to thing object.
* @param params, json type string that user to send to cloud.
* @param linkkit_extended_info_operation, specify update type or delete type.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_trigger_extended_info_operate(const void *thing_id, const char *params,
linkkit_extended_info_operate_t linkkit_extended_info_operation);
/**
* @brief trigger a event to post to cloud.
*
* @param thing_id, pointer to thing object.
* @param event_identifier, event identifier to trigger.
* @param cb, callback function of event post.
*
* @return >=0 when success, -1 when fail.
*/
int being_deprecated linkkit_trigger_event(const void *thing_id, const char *event_identifier,
handle_post_cb_fp_t cb);
/**
* @brief post property to cloud.
*
* @param thing_id, pointer to thing object.
* @param property_identifier, used when trigger event with method "event.property.post", if set, post specified property, if NULL, post all.
* @param cb, callback function of property post.
*
* @return >=0 when success, -1 when fail.
*/
int being_deprecated linkkit_post_property(const void *thing_id, const char *property_identifier,
handle_post_cb_fp_t cb);
/**
* @brief this function used to yield when want to receive or send data.
* if multi-thread enabled, user should NOT call this function.
*
* @param timeout_ms, timeout value in ms.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_yield(int timeout_ms);
typedef enum {
service_cota_callback_type_new_version_detected = 10,
service_cota_callback_type_number,
} service_cota_callback_type_t;
typedef void (*handle_service_cota_callback_fp_t)(service_cota_callback_type_t callback_type, const char *configid,
uint32_t configsize,
const char *gettype,
const char *sign,
const char *signmethod,
const char *cota_url);
/**
* @brief this function used to register callback for config ota.
*
* @param callback_fp, user callback which register to cota.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_cota_init(handle_service_cota_callback_fp_t callback_fp);
/**
* @brief this function used to execute cota process.
*
* @param data_buf, data buf that used to do ota. ota service will use this buffer to download bin.
* @param data_buf_length, data buf length that used to do ota.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_invoke_cota_service(void *data_buf, int data_buf_length);
/**
* @brief this function used to trigger cota process.
*
* @param config_scope, remote config scope, should be "product".
* @param get_type, remote config file type, should be "file".
* @param attribute_Keys, reserved.
* @param option, reserved.
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_invoke_cota_get_config(const char *config_scope, const char *get_type,
const char *attribute_Keys, void *option);
typedef enum {
service_fota_callback_type_new_version_detected = 10,
service_fota_callback_type_number,
} service_fota_callback_type_t;
typedef void (*handle_service_fota_callback_fp_t)(service_fota_callback_type_t callback_type, const char *version);
/**
* @brief this function used to register callback for firmware ota.
*
* @param callback_fp, user callback which register to fota.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_fota_init(handle_service_fota_callback_fp_t callback_fp);
/**
* @brief this function used to execute fota process.
*
* @param data_buf, data buf that used to do ota. ota service will use this buffer to download bin.
* @param data_buf_length, data buf length that used to do ota.
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_invoke_fota_service(void *data_buf, int data_buf_length);
/**
* @brief this function used to get NTP time from cloud.
*
* @param ntp_reply_cb, user callback which register to ntp request.
* when cloud returns ntp reply, sdk would trigger the callback function
*
* @return 0 when success, -1 when fail.
*/
int being_deprecated linkkit_ntp_time_request(void (*ntp_reply_cb)(const char *ntp_offset_time_ms));
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LINKKIT_EXPORT_H */
+434
View File
@@ -0,0 +1,434 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef LINKKIT_GATEWAY_EXPORT_H
#define LINKKIT_GATEWAY_EXPORT_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#if defined (__CC_ARM)
#define ssize_t int
#elif defined (__ICCARM__)
#define ssize_t int
#endif
/***************************Gateway Interface***************************/
enum {
LINKKIT_EVENT_CLOUD_DISCONNECTED = 0, /* cloud disconnected */
LINKKIT_EVENT_CLOUD_CONNECTED = 1, /* cloud connected */
LINKKIT_EVENT_SUBDEV_DELETED = 2, /* subdev deleted */
LINKKIT_EVENT_SUBDEV_PERMITED = 3, /* subdev permit join */
LINKKIT_EVENT_SUBDEV_SETUP = 4, /* subdev install */
};
/*
* option | default | minimum | maximum
*--------------------------------|---------|---------|---------
* LINKKIT_OPT_MAX_MSG_SIZE | 20480 | 512 | 51200
* LINKKIT_OPT_MAX_MSG_QUEUE_SIZE | 16 | 2 | 32
* LINKKIT_OPT_THREAD_POOL_SIZE | 4 | 1 | 16
* LINKKIT_OPT_THREAD_STACK_SIZE | 8192 | 1024 | 8388608
* LINKKIT_OPT_LOG_LEVEL | 3 | 0 | 5
*/
enum {
LINKKIT_OPT_MAX_MSG_SIZE = 1,
LINKKIT_OPT_MAX_MSG_QUEUE_SIZE = 2,
LINKKIT_OPT_THREAD_POOL_SIZE = 3,
LINKKIT_OPT_THREAD_STACK_SIZE = 4,
LINKKIT_OPT_PROPERTY_POST_REPLY = 5, /* data type: int */
LINKKIT_OPT_EVENT_POST_REPLY = 6, /* data type: int */
LINKKIT_OPT_PROPERTY_SET_REPLY = 7 /* data type: int */
};
typedef struct {
int event_type; /* see LINKKIT_EVENT_XXX for more details */
union {
struct {
char *productKey;
char *deviceName;
} subdev_deleted;
struct {
char *productKey;
int timeoutSec;
} subdev_permited;
struct {
char *subdevList; /* json format:[{"productKey":"","deviceName":""},...] */
} subdev_install;
} event_data;
} linkkit_event_t;
typedef struct linkkit_params_s {
int maxMsgSize; /* max message size */
int maxMsgQueueSize; /* max message queue size */
int threadPoolSize; /* number threads in pool */
int threadStackSize; /* default thread stack size */
int (*event_cb)(linkkit_event_t *ev, void *ctx);
/* user private data */
void *ctx;
} linkkit_params_t;
/**
* @brief get default initialize parameters
*
* @return linkkit default parameters.
*/
linkkit_params_t *linkkit_gateway_get_default_params(void);
/**
* @brief set option in paremeters
*
* @param params, linkkit initialize parameters, return from linkkit_gateway_get_default_params().
* @param option, see LINKKIT_OPT_XXX for more detail.
* @param value, value of option.
* @param value_len, value length.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_setopt(linkkit_params_t *params, int option, void *value, int value_len);
/**
* @brief set event callback
*
* @param params, linkkit initialize parameters, return from linkkit_gateway_get_default_params().
* @param event_cb, event callback.
* @param ctx, user private data.
*
* @return 0 when success, < 0 when fail.
*/
int linkkit_gateway_set_event_callback(linkkit_params_t *params, int (*event_cb)(linkkit_event_t *ev,
void *ctx),
void *ctx);
/**
* @brief linkkit initialization.
*
* @param initParams, linkkit initialize parameters, see linkkit_params_t for more detail.
*
* @return 0 when success, < 0 when fail.
*/
int linkkit_gateway_init(linkkit_params_t *initParams);
/**
* @brief linkkit deinitialization.
*
* @return 0 when success, < 0 when fail.
*/
int linkkit_gateway_exit(void);
typedef struct {
int (*register_complete)(void *ctx);
/**
* @brief get property callback.
*
* @param in, properties to be get, in JSON array format, terminated by NULL.
* @param out, output buffer fill by user, in json format, terminated by NULL.
* @param out_len, out buffer length.
* @param ctx, user private data passed by linkkit_gateway_start() or linkkit_gateway_subdev_create()
*
* @return 0 when success, -1 when fail.
*/
int (*get_property)(char *in, char *out, int out_len, void *ctx);
/**
* @brief set property callback.
*
* @param in, properties to be set, in JSON object format, terminated by NULL.
* @param ctx, user private data passed by linkkit_gateway_start() or linkkit_gateway_subdev_create()
*
* @return 0 when success, -1 when fail.
*/
int (*set_property)(char *in, void *ctx);
/**
* @brief call service callback.
*
* @param identifier, service identifier, available services define in TSL file.
* @param in, service input data, in JSON object format, terminated by NULL.
* @param out, service output, this buffer will be filled by user, in json format, terminated by NULL.
* @param out_len, out buffer length.
* @param ctx, user private data passed by linkkit_gateway_start() or linkkit_gateway_subdev_create().
*
* @return 0 when success, -1 when fail.
*/
int (*call_service)(char *identifier, char *in, char *out, int out_len, void *ctx);
/**
* @brief raw data from cloud.
*
* @param in, input data from cloud.
* @param in_len, input data length.
* @param out, output data to cloud, allocated by linkkit fill by user, no need to be free.
* @param out_len, out buffer length.
* @param ctx, user private data passed by linkkit_gateway_start() or linkkit_gateway_subdev_create().
*
* @return output data size. < 0 when fail.
*/
int (*down_rawdata)(const void *in, int in_len, void *out, int out_len, void *ctx);
/**
* @brief return data from cloud when calling linkkit_gateway_post_rawdata().
*
* @param data, return raw data from cloud.
* @param len, data length.
* @param ctx, user private data passed by linkkit_gateway_start() or linkkit_gateway_subdev_create().
*
* @return 0 when success, -1 when fail.
*/
int (*post_rawdata_reply)(const void *data, int len, void *ctx);
} linkkit_cbs_t;
/**
* @brief start linkkit gateway routines and install callback funstions.
*
* @param cbs, callback function struct to be installed.
* @param ctx, user context pointer.
*
* @return device id, 0 > when success, < 0 when fail.
*/
int linkkit_gateway_start(linkkit_cbs_t *cbs, void *ctx);
/**
* @brief stop linkkit gateway.
* @param devid, device id return from linkkit_gateway_start().
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_stop(int devid);
/**
* @brief register subdev to gateway.
* @param productKey, subdev's product key.
* @param deviceName, subdev's device name.
* @param deviceSecret, subdev's device secret.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_subdev_register(char *productKey, char *deviceName, char *deviceSecret);
/**
* @brief deregister subdev from gateway.
* @param productKey, subdev's product key.
* @param deviceName, subdev's device name.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_subdev_unregister(char *productKey, char *deviceName);
/**
* @brief create subdev and install callback funstions.
*
* @param productKey, subdev's product key.
* @param deviceName, subdev's device name.
* @param cbs, callback function struct to be installed.
* @param ctx, user context pointer.
*
* @return device id, 0 > when success, < 0 when fail.
*/
int linkkit_gateway_subdev_create(char *productKey, char *deviceName, linkkit_cbs_t *cbs, void *ctx);
/**
* @brief destroy subdev by device id.
* @param devid, device id return from linkkit_gateway_subdev_create().
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_subdev_destroy(int devid);
/**
* @brief make subdev accessible from cloud.
* @param devid, device id return from linkkit_gateway_subdev_create().
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_subdev_login(int devid);
/**
* @brief make subdev inaccessible on cloud.
* @param devid, device id return from linkkit_gateway_subdev_create().
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_subdev_logout(int devid);
enum {
LINKKIT_STATE_ENABLED = 0, /* device is enabled by cloud */
LINKKIT_STATE_DISABLED, /* device is disabled by cloud */
LINKKIT_STATE_REMOVED, /* device is deleted by cloud */
};
typedef struct {
char *productKey; /* device's product key */
char *deviceName; /* device's device name */
int devtype; /* Device Type: 0 - gateway, 1 - subdev */
int login; /* Login State: 0 - logout, 1 - login */
int state; /* Device State: see LINKKIT_STATE_xxx */
int online; /* 0 - offline, 1 - online */
} linkkit_devinfo_t;
/**
* @brief get device infomation specific by devid.
*
* @param devinfo, device information, see linkkit_devinfo_t for more detail.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_get_devinfo(int devid, linkkit_devinfo_t *devinfo);
/**
* @brief post event to cloud.
*
* @param devid, device id return from linkkit_gateway_start() or linkkit_gateway_subdev_create().
* @param identifier, event identifier, see tsl file for more detail.
* @param event, event data, in JSON format.
* @param timeout_ms, transmission timeout, in milliseconds. when timeout_ms is 0, wait no response.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_trigger_event_json_sync(int devid, char *identifier, char *event, int timeout_ms);
/**
* @brief post event to cloud asynchronously.
*
* @param devid, device id return from linkkit_gateway_start() or linkkit_gateway_subdev_create().
* @param identifier, event identifier, see tsl file for more detail.
* @param event, event data, in JSON format.
* @param timeout_ms, transmission timeout, in milliseconds. when timeout_ms is 0, wait no response.
* @param func, callback function when success(retval > 0), timeout(retval = 0) or failed(retval < 0).
* @param ctx, user data passed to 'func'.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_trigger_event_json(int devid, char *identifier, char *event, int timeout_ms,
void (*func)(int retval, void *ctx), void *ctx);
/**
* @brief post property to cloud.
*
* @param devid, device id return from linkkit_gateway_start() or linkkit_gateway_subdev_create().
* @param property, property data, in JSON format.
* @param timeout_ms, transmission timeout, in milliseconds. when timeout_ms is 0, wait no response.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_post_property_json_sync(int devid, char *property, int timeout_ms);
/**
* @brief post property to cloud asynchronously.
*
* @param devid, device id return from linkkit_gateway_start() or linkkit_gateway_subdev_create().
* @param property, property data, in JSON format.
* @param timeout_ms, transmission timeout, in milliseconds. when timeout_ms is 0, wait no response.
* @param func, callback function when success(retval > 0), timeout(retval = 0) or failed(retval < 0).
* @param ctx, user data passed to 'func'.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_post_property_json(int devid, char *property, int timeout_ms,
void (*func)(int retval, void *ctx),
void *ctx);
/**
* @brief post raw data to cloud.
*
* @param devid, device id return from linkkit_gateway_start() or linkkit_gateway_subdev_create().
* @param data, raw data buffer pointer.
* @param len, raw data length.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_post_rawdata(int devid, void *data, int len);
typedef enum {
LINKKIT_OTA_EVENT_NEW_VERSION_DETECTED = 1,
} linkkit_ota_event_t;
typedef enum {
service_fota_callback_type_new_version_detected = 10,
service_fota_callback_type_number,
} service_fota_callback_type_t;
typedef void (*handle_service_fota_callback_fp_t)(service_fota_callback_type_t callback_type, const char *version);
/**
* @brief this function used to register callback for firmware ota.
*
* @param callback_fp, user callback which register to fota. (NULL for unregister)
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_fota_init(handle_service_fota_callback_fp_t callback_fp);
/**
* @brief this function used to execute fota process.
*
* @param data_buf, data buf that used to do ota. ota service will use this buffer to download bin.
* @param data_buf_length, data buf length that used to do ota.
*
* @return 0 when success, -1 when fail.
*/
int linkkit_gateway_invoke_fota_service(void *data_buf, int data_buf_length);
typedef struct {
char *attrKey; /* the key of extend info. */
char *attrValue; /* the value of extend info. */
} linkkit_extinfo_t;
/**
* @brief post group of extend info to cloud
*
* @param devid, device id return from linkkit_gateway_start() or linkkit_gateway_subdev_create().
* @param extinfos, group of extend info to be post.
* @param nb_extinfos, number of extend infos in extinfos.
* @param timeout_ms, transmission timeout, in milliseconds. when timeout_ms is 0, wait no response.
*
* @return 0 when success, < 0 when fail.
*/
int linkkit_gateway_post_extinfos(int devid, linkkit_extinfo_t *extinfos, int nb_extinfos,
int timeout_ms);
/**
* @brief delete extend info specific by key
*
* @param devid, device id return from linkkit_gateway_start() or linkkit_gateway_subdev_create().
* @param extinfos, group of extend info to be deleted, attrValue in linkkit_extinfo_t will be ignore.
* @param nb_extinfos, number of extend infos in extinfos.
* @param timeout_ms, transmission timeout, in milliseconds. when timeout_ms is 0, wait no response.
*
* @return 0 when success, < 0 when fail.
*/
int linkkit_gateway_delete_extinfos(int devid, linkkit_extinfo_t *extinfos, int nb_extinfos,
int timeout_ms);
/**
* @brief get number devices currently in gateway
*
* @return number devinfos.
*/
int linkkit_gateway_get_num_devices(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LINKKIT_GATEWAY_EXPORT_H */
File diff suppressed because it is too large Load Diff
+213
View File
@@ -0,0 +1,213 @@
#ifndef PROTOCOL_H
#define PROTOCOL_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "protocol_data_def.h"
#define EVS_LINKKIT_OPEN 0
#define EVS_LINKKIT_CONNECT 1
#define EVS_LINKKIT_POLL 2
#define EVS_LINKKIT_OPEN_FAULT -1 // 设备linkkit打开失败
#define EVS_LINKKIT_OPEN_WAIT -2 // 设备linkkit打开重试等待
#define EVS_LINKKIT_CONNECT_FAULT -3 // 设备linkkit连接失败
#define EVS_LINKKIT_CONNECT_WAIT -4 // 设备linkkit连接重试等待
#define EVS_IS_NOT_READY -1 // 设备未准备就绪
#define EVS_GET_REG_CODE_FAULT -2 // 获取设备注册码失败
#define EVS_SET_CERT_FAULT -3 // 设置设备证书失败
/*****************************智能枪读卡器相关********************/
#define EVS_OPT_READER_SUCCESS 0 // 读卡器操作成功
#define EVS_SET_READER_BAUD_FAIL -1 // 设置读卡器波特率失败
#define EVS_CHECK_READER_FAIL -2 // 获取读卡器版本失败
#define EVS_CTRL_BEEMER_FAIL -3 // 设置蜂鸣器失败
#define EVS_OPEN_RF_FAIL -4 // 打开射频失败
#define EVS_CLOSE_RF_FAIL -5 // 关闭射频失败
#define EVS_HANDLE_AUTHING 0 // 正在鉴权
#define EVS_HANDLE_AUTH_SUCCESS 1 // 鉴权成功
#define EVS_HANDLE_CARD_FAILED -1 // 读卡失败
#define EVS_HANDLE_TERMINAL_TIMEOUT -2 // 获取终端机编号服务超时
#define EVS_HANDLE_GET_MAC1 -3 // 读取卡内MAC1失败
#define EVS_HANDLE_AUTHCODE_TIMEOUT -4 // 获取加密机鉴权码服务超时
#define EVS_HANDLE_CHECK_MAC2 -5 // 验证MAC2失败
#define EVS_HANDLE_AUTH_RESULT_TIMEOUT -6 // 获取鉴权结果服务超时
#define EVS_HANDLE_GUN_NUM_WRONG -7 // 枪编号错误
#define EVS_HANDLE_GUN_PULL_OUT -8 // 鉴权中拔枪
#define EVS_CARD_ERROR_READER_COMM 10 // 读卡器通信异常
#define EVS_CARD_ERROR_OPEN_RF 11 // 开射频失败
#define EVS_CARD_ERROR_FOUND_CARD 12 // 寻卡失败
#define EVS_CARD_ERROR_GET_USER_ID 13 // 获取用户ID失败
#define EVS_CARD_ERROR_GET_PSY_ID 14 // 获取物理卡号失败
#define EVS_CARD_ERROR_CHECK_PIN 15 // 验密失败
#define EVS_CARD_ERROR_GET_KEY_VER 16 // 获取秘钥版本失败
#define EVS_CARD_ERROR_GET_MAC1 17 // 获取MAC1失败
#define EVS_CARD_ERROR_CHECK_MAC2 18 // 获取MAC1失败
#define EVS_CARD_ERROR_CLOSE_RF 19 // 关射频失败
#define EVS_CARD_ERROR_TERMINAL_FAULT 20 // 获取终端机编号数据异常
#define EVS_CARD_ERROR_TERMINAL_TIMEOUT 21 // 获取终端机编号超时
#define EVS_CARD_ERROR_CHECK_MAC1_FAILED 22 // 平台验证MAC1失败
#define EVS_CARD_ERROR_CHECK_MAC1_TIMEOUT 23 // 平台验证MAC1超时
#define EVS_CARD_ERROR_AUTH_TIMEOUT 24 // 接收启动鉴权结果超时
#define EVS_CARD_ERROR_AUTH_FAILED 25 // 鉴权结果不允许启动
#define EVS_CARD_ERROR_GUN_PULL_OUT 26 // 鉴权过程中用户拔枪
extern char new_firmware_version[IOTX_FIRMWARE_VER_LEN];
/*************************************************************/
/**
* int evs_card_ctrl_init(int fd)
*
*
*
* fd
*
*:
*
*
*EVS_OPT_READER_SUCCESS 0 //读卡器操作成功
*EVS_SET_READER_BAUD_FAIL -1 //设置读卡器波特率失败
*EVS_CHECK_READER_FAIL -2 //获取读卡器版本失败
*EVS_CTRL_BEEMER_FAIL -3 //设置蜂鸣器失败
*EVS_OPEN_RF_FAIL -4 //打开射频失败
*EVS_CLOSE_RF_FAIL -5 //关闭射频失败
**/
int evs_card_ctrl_init(int fd);
/**
* int evs_card_handle(int fd, evs_smart_gun_auth_param *param)
*
*
*
* fd
*
*:
*
*
*EVS_OPT_READER_SUCCESS 0 //读卡器操作成功
*EVS_SET_READER_BAUD_FAIL -1 //设置读卡器波特率失败
*EVS_CHECK_READER_FAIL -2 //获取读卡器版本失败
*EVS_CTRL_BEEMER_FAIL -3 //设置蜂鸣器失败
*EVS_OPEN_RF_FAIL -4 //打开射频失败
*EVS_CLOSE_RF_FAIL -5 //关闭射频失败
**/
int evs_card_handle(int fd, evs_smart_gun_auth_param *param);
/**
* int evs_linkkit_file_upload(int fd, evs_smart_gun_auth_param *param)
* upload file to IOT
*
*
* filePath
*file path
*:
*
*
*EVS_UPLOAD_SUCCESS 0 //upload file success
*EVS_UPLOAD_FAIL -1 //upload file failed
**/
int evs_linkkit_file_upload(char *filePath);
/*
int evs_linkkit_new(const int evs_access,const int is_device_uid)
SDK套件
evs_access
ACCESS_IS_CUSTOM 3
ACCESS_IS_TEST 2
ACCESS_IS_DEBUG 1
ACCESS_IS_FORMAL 0:
is_device_uid
0:使
1使
:
0
EVS_IS_NOT_READY
EVS_GET_REG_CODE_FAULT
EVS_SET_CERT_FAULT
*/
int evs_linkkit_new(const int evs_access, const int is_device_uid);
/*
int evs_linkkit_time_sync(void)
iot平台连接成功后
:
0
1
*/
int evs_linkkit_time_sync(void);
/*
int evs_linkkit_fota(unsigned char *buffer, int buffer_length)
iot平台连下发固件升级时ota固件包下载
1 buffer固件缓存区
2 buffer_length
:
0 ota固件下载成功
1 ota固件下载失败
*/
int evs_linkkit_fota(unsigned char *buffer, int buffer_length);
int evs_linkkit_free(void);
/*
void evs_set_firmware_version(const char *verion)
verion固件版本信息
:
*/
void evs_set_firmware_version(const char *verion);
/*
int evs_mainloop()
SDK功能运行函数
:
EVS_LINKKIT_OPEN_FAULT linkkit打开失败
EVS_LINKKIT_OPEN_WAIT linkkit打开重试等待
EVS_LINKKIT_CONNECT_FAULT linkkit连接失败
EVS_LINKKIT_CONNECT_WAIT linkkit连接重试等待
*/
int evs_mainloop(void);
// int evs_send_event();
// int evs_post_property();
/**
* @brief Send pile event data to SDK.
*---
* @param [in] event_type: @n the event you want to send.
* @param [in] param: @n the event data will be written.
* @return failed -1 success 0.
* @see None.
* @note None.
*/
void evs_send_event(evs_cmd_event_enum event_type, void *param);
/**
 *
 * evs_send_property() SDK内部实现的事件发送接口, 使
 * ---
 * Interface of evs_send_property() implemented by SDK provide for user of SDK.
 *
 * 
 */
/**
* @brief Send pile porperty data to SDK.
*---
* @param [in] event_type: @n the property you want to send.
* @param [in] param: @n the property data will be written.
* @return failed -1 success 0.
* @see None.
* @note None.
*/
void evs_send_property(evs_cmd_property_enum property_type, void *param);
#ifdef __cplusplus
}
#endif
#endif /* interface.h */
File diff suppressed because it is too large Load Diff