Initial commit: CCU621_M firmware project with BLE debug link support.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Vendored
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#if defined(PLATFORM_IS_DEBUG)
|
||||
const char *iotx_ca_crt = \
|
||||
{
|
||||
\
|
||||
"-----BEGIN CERTIFICATE-----\r\n"
|
||||
"MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\r\n" \
|
||||
"A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\r\n" \
|
||||
"b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\r\n" \
|
||||
"MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\r\n" \
|
||||
"YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\r\n" \
|
||||
"aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\r\n" \
|
||||
"jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\r\n" \
|
||||
"xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\r\n" \
|
||||
"1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\r\n" \
|
||||
"snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\r\n" \
|
||||
"U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\r\n" \
|
||||
"9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\r\n" \
|
||||
"BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\r\n" \
|
||||
"AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\r\n" \
|
||||
"yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\r\n" \
|
||||
"38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\r\n" \
|
||||
"AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\r\n" \
|
||||
"DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\r\n" \
|
||||
"HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\r\n" \
|
||||
"-----END CERTIFICATE-----"
|
||||
};
|
||||
#else
|
||||
const char *iotx_ca_crt = \
|
||||
{
|
||||
\
|
||||
"-----BEGIN CERTIFICATE-----\r\n"
|
||||
"MIIDNTCCAh0CFFGhMctUDrA/FfvShIH3r4DzJQ8sMA0GCSqGSIb3DQEBCwUAMFYx\r\n" \
|
||||
"CzAJBgNVBAYTAkNOMREwDwYDVQQIDAhaaGVKaWFuZzERMA8GA1UEBwwISGFuZ1po\r\n" \
|
||||
"b3UxDDAKBgNVBAoMA0lvVDETMBEGA1UEAwwKYWxpeXVuLmNvbTAgFw0yMDA1MjAw\r\n" \
|
||||
"ODUzMjRaGA8yMDgwMDUwNTA4NTMyNFowVjELMAkGA1UEBhMCQ04xETAPBgNVBAgM\r\n" \
|
||||
"CFpoZUppYW5nMREwDwYDVQQHDAhIYW5nWmhvdTEMMAoGA1UECgwDSW9UMRMwEQYD\r\n" \
|
||||
"VQQDDAphbGl5dW4uY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n" \
|
||||
"unSTVZb3FVarrMpy76f+hX7Z05E5l0sL7ukOjYZLgx8kt3RYjnPxIB23wp5kPtNV\r\n" \
|
||||
"/cEHN1dihctqNDMcjXjDXTbf1oFdDWN/0vc8Mz+4hOHiefc+5TRAlmYQ97DN5dKu\r\n" \
|
||||
"8jTndVNNrfoSLdFYpIhufhhWYKM0nb95lVx0YMhbPJAwQbNIhdG2QioFp+oicnjt\r\n" \
|
||||
"A/G8ISNw2oDSAYz0QcUE4Hn1ZsWWHO6VaLwRP+m258cwPKcTmwv+n+kp76b2ZKIj\r\n" \
|
||||
"vtkX1mswdI/geDfMN8rYg6jY9VGgUbd9peLw93EzMQ2QkFPcdsVtJsru8WoU68lr\r\n" \
|
||||
"UASV+JfCmgTSIDnTvOQiCQIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQCalM0n/3pP\r\n" \
|
||||
"qBbQF3YwFqpb3BGAXxk6I8Zk1S2xRXErzbddPWC+f90OMvav6+jpnmvtlQMzFqMn\r\n" \
|
||||
"lGULjwau++G4hcO0gv7zusJf4Gm1M++xPghnrWHvY1DYXbuqoSOVgcY3AE9I/o+X\r\n" \
|
||||
"r2rS4Bo47Yy28hWjyg9M9TCG5Fl77qKqgGuZAQKZTrYNlrC5M1wFgYPtg68wh1yq\r\n" \
|
||||
"pQrLKRrhkKQ91a29/xYLBf1ztNYPzvNOMZViwtiRM/OLV9bqVnfj4nufG0kUmVAA\r\n" \
|
||||
"pMQxvKMTOIvrIqQg1HNrqqKBzW12J3/PZXJyFLtlnWq3n5GDwKjEOlabZRT5vNIa\r\n" \
|
||||
"6FQmo3jbuzIu\r\n" \
|
||||
"-----END CERTIFICATE-----"
|
||||
|
||||
};
|
||||
#endif
|
||||
+205
@@ -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
|
||||
+1991
File diff suppressed because it is too large
Load Diff
+756
@@ -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
@@ -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
@@ -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);
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
|
||||
+2421
File diff suppressed because it is too large
Load Diff
+145
@@ -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
|
||||
+3079
File diff suppressed because it is too large
Load Diff
+204
@@ -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
@@ -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
@@ -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
|
||||
+1051
File diff suppressed because it is too large
Load Diff
+164
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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(¬ify_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, ¬ify_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
|
||||
@@ -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
|
||||
+2535
File diff suppressed because it is too large
Load Diff
+533
@@ -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
|
||||
+1023
File diff suppressed because it is too large
Load Diff
+24
@@ -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
@@ -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
@@ -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
|
||||
+2537
File diff suppressed because it is too large
Load Diff
+62
@@ -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
|
||||
+2079
File diff suppressed because it is too large
Load Diff
+190
@@ -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
@@ -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
|
||||
+1186
File diff suppressed because it is too large
Load Diff
+43
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
|
||||
@@ -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
@@ -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 */
|
||||
@@ -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 */
|
||||
+6671
File diff suppressed because it is too large
Load Diff
+213
@@ -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 */
|
||||
+1643
File diff suppressed because it is too large
Load Diff
+10
@@ -0,0 +1,10 @@
|
||||
#ifndef _DEV_SIGN_H_
|
||||
#define _DEV_SIGN_H_
|
||||
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
|
||||
int32_t IOT_Sign_MQTT(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *meta, iotx_sign_mqtt_t *signout);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef _DEV_SIGN_CONFIG_H_
|
||||
#define _DEV_SIGN_CONFIG_H_
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
#ifndef _DEV_SIGN_INTERNAL_H_
|
||||
#define _DEV_SIGN_INTERNAL_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "infra_config.h"
|
||||
#include "infra_defs.h"
|
||||
#include "dev_sign_api.h"
|
||||
#include "wrappers.h"
|
||||
|
||||
#endif
|
||||
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "infra_state.h"
|
||||
#include "infra_defs.h"
|
||||
#include "infra_config.h"
|
||||
#include "infra_sha256.h"
|
||||
#include "infra_compat.h"
|
||||
#include "dev_sign_api.h"
|
||||
#include "wrappers.h"
|
||||
|
||||
/* all secure mode define */
|
||||
#define MODE_TLS_GUIDER "-1"
|
||||
#define MODE_TLS_DIRECT "2"
|
||||
#define MODE_TCP_DIRECT_PLAIN "3"
|
||||
#define MODE_ITLS_DNS_ID2 "8"
|
||||
|
||||
#if defined(SUPPORT_TLS)
|
||||
#define SECURE_MODE MODE_TLS_DIRECT
|
||||
#else
|
||||
#define SECURE_MODE MODE_TCP_DIRECT_PLAIN
|
||||
#endif
|
||||
|
||||
/* use fixed timestamp */
|
||||
#define TIMESTAMP_VALUE "2524608000000"
|
||||
|
||||
static iotx_mqtt_region_types_t domain_type = IOTX_CLOUD_REGION_SHANGHAI;
|
||||
static char cloud_custom_domain[IOTX_DOMAIN_MAX_LEN + 1] = {0};
|
||||
|
||||
/* clientid key value pair define */
|
||||
const char *clientid_kv[][2] = {
|
||||
{
|
||||
"timestamp", TIMESTAMP_VALUE
|
||||
},
|
||||
{
|
||||
"_v", "sdk-c-"IOTX_SDK_VERSION
|
||||
},
|
||||
{
|
||||
"securemode", SECURE_MODE
|
||||
},
|
||||
{
|
||||
"signmethod", "hmacsha256"
|
||||
},
|
||||
{
|
||||
"lan", "C"
|
||||
},
|
||||
#ifdef MQTT_AUTO_SUBSCRIBE
|
||||
{
|
||||
"_ss", "1"
|
||||
},
|
||||
#endif
|
||||
#if 0
|
||||
{
|
||||
"v", IOTX_ALINK_VERSION
|
||||
},
|
||||
#else
|
||||
{
|
||||
"gw", "0"
|
||||
},
|
||||
{
|
||||
"ext", "0"
|
||||
},
|
||||
#endif
|
||||
};
|
||||
|
||||
static void _hex2str(uint8_t *input, uint16_t input_len, char *output)
|
||||
{
|
||||
char *zEncode = "0123456789ABCDEF";
|
||||
int i = 0, j = 0;
|
||||
|
||||
for (i = 0; i < input_len; i++) {
|
||||
output[j++] = zEncode[(input[i] >> 4) & 0xf];
|
||||
output[j++] = zEncode[(input[i]) & 0xf];
|
||||
}
|
||||
}
|
||||
|
||||
int _sign_get_clientid(char *clientid_string, const char *device_id, const char *custom_kv, uint8_t enable_itls)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
if (clientid_string == NULL || device_id == NULL) {
|
||||
return STATE_USER_INPUT_INVALID;
|
||||
}
|
||||
|
||||
memset(clientid_string, 0, DEV_SIGN_CLIENT_ID_MAXLEN);
|
||||
if (strlen(device_id) + 1 >= DEV_SIGN_CLIENT_ID_MAXLEN) {
|
||||
return STATE_MQTT_SIGN_CLIENTID_BUF_SHORT;
|
||||
}
|
||||
memcpy(clientid_string, device_id, strlen(device_id));
|
||||
memcpy(clientid_string + strlen(clientid_string), "|", 1);
|
||||
|
||||
if (enable_itls > 0) {
|
||||
clientid_kv[2][1] = MODE_ITLS_DNS_ID2;
|
||||
} else {
|
||||
clientid_kv[2][1] = SECURE_MODE;
|
||||
}
|
||||
|
||||
for (i = 0; i < (sizeof(clientid_kv) / (sizeof(clientid_kv[0]))); i++) {
|
||||
if ((strlen(clientid_string) + strlen(clientid_kv[i][0]) + strlen(clientid_kv[i][1]) + 2) >=
|
||||
DEV_SIGN_CLIENT_ID_MAXLEN) {
|
||||
return STATE_MQTT_SIGN_CLIENTID_BUF_SHORT;
|
||||
}
|
||||
|
||||
memcpy(clientid_string + strlen(clientid_string), clientid_kv[i][0], strlen(clientid_kv[i][0]));
|
||||
memcpy(clientid_string + strlen(clientid_string), "=", 1);
|
||||
memcpy(clientid_string + strlen(clientid_string), clientid_kv[i][1], strlen(clientid_kv[i][1]));
|
||||
memcpy(clientid_string + strlen(clientid_string), ",", 1);
|
||||
}
|
||||
|
||||
if (custom_kv != NULL) {
|
||||
if ((strlen(clientid_string) + strlen(custom_kv) + 1) >= DEV_SIGN_CLIENT_ID_MAXLEN) {
|
||||
return STATE_MQTT_SIGN_CLIENTID_BUF_SHORT;
|
||||
}
|
||||
memcpy(clientid_string + strlen(clientid_string), custom_kv, strlen(custom_kv));
|
||||
memcpy(clientid_string + strlen(clientid_string), ",", 1);
|
||||
}
|
||||
|
||||
memcpy(clientid_string + strlen(clientid_string) - 1, "|", 1);
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
#define SIGN_FMT_LEN 50 /* "clientId%sdeviceName%sproductKey%stimestamp%s";*/
|
||||
int _iotx_generate_sign_string(const char *device_id, const char *device_name, const char *product_key,
|
||||
const char *device_secret, char *sign_string)
|
||||
{
|
||||
char signsource[DEV_SIGN_SOURCE_MAXLEN] = {0};
|
||||
uint16_t signsource_len = 0;
|
||||
uint8_t sign_hex[32] = {0};
|
||||
|
||||
signsource_len = SIGN_FMT_LEN + strlen(device_id) + strlen(device_name) + strlen(product_key) + strlen(
|
||||
TIMESTAMP_VALUE);
|
||||
if (signsource_len >= DEV_SIGN_SOURCE_MAXLEN) {
|
||||
return STATE_MQTT_SIGN_SOURCE_BUF_SHORT;
|
||||
}
|
||||
|
||||
memset(signsource, 0, DEV_SIGN_SOURCE_MAXLEN);
|
||||
memcpy(signsource, "clientId", strlen("clientId"));
|
||||
memcpy(signsource + strlen(signsource), device_id, strlen(device_id));
|
||||
memcpy(signsource + strlen(signsource), "deviceName", strlen("deviceName"));
|
||||
memcpy(signsource + strlen(signsource), device_name, strlen(device_name));
|
||||
memcpy(signsource + strlen(signsource), "productKey", strlen("productKey"));
|
||||
memcpy(signsource + strlen(signsource), product_key, strlen(product_key));
|
||||
memcpy(signsource + strlen(signsource), "timestamp", strlen("timestamp"));
|
||||
memcpy(signsource + strlen(signsource), TIMESTAMP_VALUE, strlen(TIMESTAMP_VALUE));
|
||||
|
||||
utils_hmac_sha256((uint8_t *)signsource, strlen(signsource), (uint8_t *)device_secret,
|
||||
strlen(device_secret), sign_hex);
|
||||
|
||||
_hex2str(sign_hex, 32, sign_string);
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t IOT_Sign_MQTT(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *meta, iotx_sign_mqtt_t *signout)
|
||||
{
|
||||
uint16_t length = 0;
|
||||
char device_id[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 1] = {0};
|
||||
int res;
|
||||
|
||||
if (region >= IOTX_MQTT_DOMAIN_NUMBER || meta == NULL) {
|
||||
return STATE_USER_INPUT_INVALID;
|
||||
}
|
||||
|
||||
memset(signout, 0, sizeof(iotx_sign_mqtt_t));
|
||||
|
||||
memcpy(device_id, meta->product_key, strlen(meta->product_key));
|
||||
memcpy(device_id + strlen(device_id), ".", strlen("."));
|
||||
memcpy(device_id + strlen(device_id), meta->device_name, strlen(meta->device_name));
|
||||
|
||||
/* setup clientid */
|
||||
if (_sign_get_clientid(signout->clientid, device_id, NULL, 0) < STATE_SUCCESS) {
|
||||
return STATE_MQTT_SIGN_CLIENTID_BUF_SHORT;
|
||||
}
|
||||
|
||||
/* setup password */
|
||||
memset(signout->password, 0, DEV_SIGN_PASSWORD_MAXLEN);
|
||||
res = _iotx_generate_sign_string(device_id, meta->device_name, meta->product_key, meta->device_secret,
|
||||
signout->password);
|
||||
if (res < STATE_SUCCESS) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* setup hostname */
|
||||
if (IOTX_CLOUD_REGION_CUSTOM == region) {
|
||||
if (g_infra_mqtt_domain[region] == NULL) {
|
||||
return STATE_USER_INPUT_MQTT_DOMAIN;
|
||||
}
|
||||
|
||||
length = strlen(g_infra_mqtt_domain[region]) + 1;
|
||||
if (length >= DEV_SIGN_HOSTNAME_MAXLEN) {
|
||||
return STATE_MQTT_SIGN_HOSTNAME_BUF_SHORT;
|
||||
}
|
||||
|
||||
memset(signout->hostname, 0, DEV_SIGN_HOSTNAME_MAXLEN);
|
||||
memcpy(signout->hostname, g_infra_mqtt_domain[region], strlen(g_infra_mqtt_domain[region]));
|
||||
} else {
|
||||
length = strlen(meta->product_key) + strlen(g_infra_mqtt_domain[region]) + 2;
|
||||
if (length >= DEV_SIGN_HOSTNAME_MAXLEN) {
|
||||
return STATE_MQTT_SIGN_HOSTNAME_BUF_SHORT;
|
||||
}
|
||||
memset(signout->hostname, 0, DEV_SIGN_HOSTNAME_MAXLEN);
|
||||
memcpy(signout->hostname, meta->product_key, strlen(meta->product_key));
|
||||
memcpy(signout->hostname + strlen(signout->hostname), ".", strlen("."));
|
||||
memcpy(signout->hostname + strlen(signout->hostname), g_infra_mqtt_domain[region],
|
||||
strlen(g_infra_mqtt_domain[region]));
|
||||
}
|
||||
|
||||
/* setup username */
|
||||
length = strlen(meta->device_name) + strlen(meta->product_key) + 2;
|
||||
if (length >= DEV_SIGN_USERNAME_MAXLEN) {
|
||||
return STATE_MQTT_SIGN_USERNAME_BUF_SHORT;
|
||||
}
|
||||
memset(signout->username, 0, DEV_SIGN_USERNAME_MAXLEN);
|
||||
memcpy(signout->username, meta->device_name, strlen(meta->device_name));
|
||||
memcpy(signout->username + strlen(signout->username), "&", strlen("&"));
|
||||
memcpy(signout->username + strlen(signout->username), meta->product_key, strlen(meta->product_key));
|
||||
|
||||
/* setup port */
|
||||
signout->port = 443;
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_REGION, void *data)
|
||||
{
|
||||
if(data == NULL) {
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
|
||||
domain_type = *(iotx_mqtt_region_types_t *)data;
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_GET_REGION, void *data)
|
||||
{
|
||||
if(data == NULL) {
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
|
||||
*(iotx_mqtt_region_types_t *)data = domain_type;
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_MQTT_DOMAIN, void *data)
|
||||
{
|
||||
if(data == NULL) {
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
|
||||
domain_type = IOTX_CLOUD_REGION_CUSTOM;
|
||||
|
||||
if (strlen((char *)data) > IOTX_DOMAIN_MAX_LEN) {
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
memset(cloud_custom_domain, 0, strlen((char *)data) + 1);
|
||||
memcpy(cloud_custom_domain, data, strlen((char *)data));
|
||||
g_infra_mqtt_domain[IOTX_CLOUD_REGION_CUSTOM] = (const char *)cloud_custom_domain;
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
+948
@@ -0,0 +1,948 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "string.h"
|
||||
#include "infra_state.h"
|
||||
#include "infra_compat.h"
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
#include "infra_string.h"
|
||||
#include "infra_httpc.h"
|
||||
#include "infra_sha256.h"
|
||||
#include "dynreg_internal.h"
|
||||
#include "dynreg_api.h"
|
||||
#ifdef MQTT_DYNAMIC_REGISTER
|
||||
#include "dev_sign_api.h"
|
||||
#include "mqtt_api.h"
|
||||
#endif
|
||||
|
||||
#define HTTP_RESPONSE_PAYLOAD_LEN (256)
|
||||
|
||||
#define DYNREG_RANDOM_KEY_LENGTH (15)
|
||||
#define DYNREG_SIGN_LENGTH (65)
|
||||
#define DYNREG_SIGN_METHOD_HMACSHA256 "hmacsha256"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *payload;
|
||||
int payload_len;
|
||||
} dynreg_http_response_t;
|
||||
|
||||
static int dynamic_register = 0;
|
||||
static int dynamic_register_region = 0;
|
||||
|
||||
#if !defined(MQTT_DYNAMIC_REGISTER)
|
||||
static int _parse_string_value(char *payload, int *pos, int *start, int *end)
|
||||
{
|
||||
int idx = 0;
|
||||
|
||||
for (idx = *pos + 1; idx < strlen(payload); idx++)
|
||||
{
|
||||
if (payload[idx] == '\"')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
*start = *pos + 1;
|
||||
*end = idx - 1;
|
||||
*pos = idx;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _parse_dynreg_value(char *payload, char *key, int *pos, int *start, int *end)
|
||||
{
|
||||
int idx = 0;
|
||||
/* printf("=====%s\n",&payload[*pos]); */
|
||||
|
||||
if (memcmp(key, "code", strlen("code")) == 0)
|
||||
{
|
||||
for (idx = *pos; idx < strlen(payload); idx++)
|
||||
{
|
||||
if (payload[idx] < '0' || payload[idx] > '9')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
*start = *pos;
|
||||
*end = idx - 1;
|
||||
*pos = *end;
|
||||
return 0;
|
||||
}
|
||||
else if (memcmp(key, "data", strlen("data")) == 0)
|
||||
{
|
||||
int bracket_cnt = 0;
|
||||
if (payload[*pos] != '{')
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
for (idx = *pos; idx < strlen(payload); idx++)
|
||||
{
|
||||
if (payload[idx] == '{')
|
||||
{
|
||||
bracket_cnt++;
|
||||
}
|
||||
else if (payload[idx] == '}')
|
||||
{
|
||||
bracket_cnt--;
|
||||
}
|
||||
if (bracket_cnt == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
*start = *pos;
|
||||
*end = idx;
|
||||
*pos = *end;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (payload[*pos] != '\"')
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return _parse_string_value(payload, pos, start, end);
|
||||
}
|
||||
|
||||
/* return -1; */
|
||||
}
|
||||
|
||||
static int _parse_dynreg_result(char *payload, char *key, int *start, int *end)
|
||||
{
|
||||
int res = 0, idx = 0, pos = 0;
|
||||
|
||||
for (idx = 0; idx < strlen(payload); idx++)
|
||||
{
|
||||
/* printf("loop start: %s\n",&payload[idx]); */
|
||||
if (payload[idx] == '\"')
|
||||
{
|
||||
for (pos = idx + 1; pos < strlen(payload); pos++)
|
||||
{
|
||||
if (payload[pos] == '\"')
|
||||
{
|
||||
/* printf("key: %.*s\n",pos - idx - 1, &payload[idx+1]); */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (pos == strlen(payload) || payload[pos + 1] != ':')
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
pos += 2;
|
||||
res = _parse_dynreg_value(payload, key, &pos, start, end);
|
||||
if (res == 0 && memcmp(key, &payload[idx + 1], strlen(key)) == 0)
|
||||
{
|
||||
/* printf("value: %.*s\n",*end - *start + 1,&payload[*start]); */
|
||||
return 0;
|
||||
}
|
||||
|
||||
idx = pos;
|
||||
}
|
||||
}
|
||||
|
||||
printf("exit 4\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int _calc_dynreg_sign(
|
||||
char product_key[IOTX_PRODUCT_KEY_LEN],
|
||||
char product_secret[IOTX_PRODUCT_SECRET_LEN],
|
||||
char device_name[IOTX_DEVICE_NAME_LEN],
|
||||
char random[DYNREG_RANDOM_KEY_LENGTH + 1],
|
||||
char sign[DYNREG_SIGN_LENGTH])
|
||||
{
|
||||
int sign_source_len = 0;
|
||||
uint8_t signnum[32];
|
||||
uint8_t *sign_source = NULL;
|
||||
const char *dynamic_register_sign_fmt = "deviceName%sproductKey%srandom%s";
|
||||
|
||||
/* Start Dynamic Register */
|
||||
memset(random, 0, DYNREG_RANDOM_KEY_LENGTH + 1);
|
||||
memcpy(random, "8Ygb7ULYh53B6OA", strlen("8Ygb7ULYh53B6OA"));
|
||||
dynreg_info("Random Key: %s", random);
|
||||
|
||||
/* Calculate SHA256 Value */
|
||||
sign_source_len = strlen(dynamic_register_sign_fmt) + strlen(device_name) + strlen(product_key) + strlen(random) + 1;
|
||||
sign_source = dynreg_malloc(sign_source_len);
|
||||
if (sign_source == NULL)
|
||||
{
|
||||
dynreg_err("Memory Not Enough");
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(sign_source, 0, sign_source_len);
|
||||
HAL_Snprintf((char *)sign_source, sign_source_len, dynamic_register_sign_fmt, device_name, product_key, random);
|
||||
|
||||
utils_hmac_sha256(sign_source, strlen((const char *)sign_source), (uint8_t *)product_secret, strlen(product_secret),
|
||||
signnum);
|
||||
infra_hex2str(signnum, 32, sign);
|
||||
dynreg_free(sign_source);
|
||||
dynreg_info("Sign: %s", sign);
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
static int _recv_callback(char *ptr, int length, int total_length, void *userdata)
|
||||
{
|
||||
dynreg_http_response_t *response = (dynreg_http_response_t *)userdata;
|
||||
if (strlen(response->payload) + length > response->payload_len)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
memcpy(response->payload + strlen(response->payload), ptr, length);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
static int _fetch_dynreg_http_resp(char *request_payload, char *response_payload,
|
||||
iotx_http_region_types_t region, char device_secret[IOTX_DEVICE_SECRET_LEN])
|
||||
{
|
||||
int res = 0;
|
||||
const char *domain = NULL;
|
||||
const char *url_format = "http://%s/auth/register/device";
|
||||
char *url = NULL;
|
||||
int url_len = 0;
|
||||
const char *pub_key = NULL;
|
||||
void *http_handle = NULL;
|
||||
int port = 443;
|
||||
iotx_http_method_t method = IOTX_HTTP_POST;
|
||||
int timeout_ms = 10000;
|
||||
char *header = "Accept: text/xml,text/javascript,text/html,application/json\r\n"
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
int http_recv_maxlen = HTTP_RESPONSE_PAYLOAD_LEN;
|
||||
dynreg_http_response_t response;
|
||||
int start = 0, end = 0, data_start = 0, data_end = 0;
|
||||
|
||||
domain = g_infra_http_domain[region];
|
||||
if (NULL == domain)
|
||||
{
|
||||
return STATE_USER_INPUT_HTTP_DOMAIN;
|
||||
}
|
||||
url_len = strlen(url_format) + strlen(domain) + 1;
|
||||
url = (char *)dynreg_malloc(url_len);
|
||||
if (NULL == url)
|
||||
{
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(url, 0, url_len);
|
||||
HAL_Snprintf(url, url_len, url_format, domain);
|
||||
|
||||
memset(&response, 0, sizeof(dynreg_http_response_t));
|
||||
response.payload = response_payload;
|
||||
response.payload_len = HTTP_RESPONSE_PAYLOAD_LEN;
|
||||
#ifdef SUPPORT_TLS
|
||||
{
|
||||
// extern const char *iotx_ca_crt;
|
||||
// pub_key = iotx_ca_crt;
|
||||
}
|
||||
#endif
|
||||
|
||||
http_handle = wrapper_http_init();
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_URL, (void *)url);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_PORT, (void *)&port);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_METHOD, (void *)&method);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_HEADER, (void *)header);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_CERT, (void *)pub_key);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_TIMEOUT, (void *)&timeout_ms);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVCALLBACK, (void *)_recv_callback);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVMAXLEN, (void *)&http_recv_maxlen);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVCONTEXT, (void *)&response);
|
||||
|
||||
res = wrapper_http_perform(http_handle, request_payload, strlen(request_payload));
|
||||
wrapper_http_deinit(&http_handle);
|
||||
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
dynreg_free(url);
|
||||
return res;
|
||||
}
|
||||
dynreg_free(url);
|
||||
dynreg_info("Http Response Payload: %s", response_payload);
|
||||
|
||||
_parse_dynreg_result(response_payload, "errorCode", &start, &end);
|
||||
dynreg_info("Dynamic Register errorcode: %.*s", end - start + 1, &response_payload[start]);
|
||||
|
||||
if (memcmp(&response_payload[start], "200", strlen("200")) != 0)
|
||||
{
|
||||
iotx_state_event(ITE_STATE_HTTP_COMM, STATE_HTTP_DYNREG_FAIL_RESP, &response_payload[start]);
|
||||
return STATE_HTTP_DYNREG_FAIL_RESP;
|
||||
}
|
||||
|
||||
_parse_dynreg_result(response_payload, "data", &data_start, &data_end);
|
||||
/* dynreg_info("value: %.*s\n",data_end - data_start + 1,&response_payload[data_start]); */
|
||||
|
||||
_parse_dynreg_result(&response_payload[data_start + 1], "deviceSecret", &start, &end);
|
||||
dynreg_info("Dynamic Register Device Secret: %.*s", end - start + 1, &response_payload[data_start + 1 + start]);
|
||||
|
||||
if (end - start + 1 > IOTX_DEVICE_SECRET_LEN)
|
||||
{
|
||||
return STATE_HTTP_DYNREG_INVALID_DS;
|
||||
}
|
||||
|
||||
memcpy(device_secret, &response_payload[data_start + 1 + start], end - start + 1);
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
static int _fetch_custom_reg_http_resp(char *request_payload, char *response_payload,
|
||||
iotx_http_region_types_t region, char product_key[IOTX_PRODUCT_KEY_LEN], char device_name[IOTX_DEVICE_NAME_LEN], char device_secret[IOTX_DEVICE_SECRET_LEN])
|
||||
{
|
||||
int res = 0;
|
||||
int port = 0;
|
||||
const char *domain = NULL;
|
||||
char url_format[128] = {0};
|
||||
|
||||
if (dynamic_register_region == ACCESS_IS_FORMAL) // 生产环境
|
||||
{
|
||||
memcpy(url_format, "http://10.111.186.1/asset-web-api/registerService/getCertificateInfo", strlen("http://10.111.186.1/asset-web-api/registerService/getCertificateInfo"));
|
||||
port = 11901;
|
||||
}
|
||||
|
||||
if (dynamic_register_region == ACCESS_IS_DEBUG) // 接入环境
|
||||
{
|
||||
memcpy(url_format, "http://121.196.185.161/api/access/pile/getCertificateInfo", strlen("http://121.196.185.161/api/access/pile/getCertificateInfo"));
|
||||
port = 8081;
|
||||
}
|
||||
|
||||
if (dynamic_register_region == ACCESS_IS_TEST) // 测试环境
|
||||
{
|
||||
memcpy(url_format, "http://124.126.19.5/asset-web-serv-v5.3.0/registerService/getCertificateInfo", strlen("http://124.126.19.5/asset-web-serv-v5.3.0/registerService/getCertificateInfo"));
|
||||
port = 19849;
|
||||
}
|
||||
|
||||
char *url = NULL;
|
||||
int url_len = 0;
|
||||
const char *pub_key = NULL;
|
||||
void *http_handle = NULL;
|
||||
|
||||
iotx_http_method_t method = IOTX_HTTP_POST;
|
||||
int timeout_ms = 10000;
|
||||
char *header = "Accept: text/xml,text/javascript,text/html,application/json\r\n"
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
int http_recv_maxlen = HTTP_RESPONSE_PAYLOAD_LEN;
|
||||
dynreg_http_response_t response;
|
||||
int start = 0, end = 0, data_start = 0, data_end = 0;
|
||||
|
||||
memset(&response, 0, sizeof(dynreg_http_response_t));
|
||||
response.payload = response_payload;
|
||||
response.payload_len = HTTP_RESPONSE_PAYLOAD_LEN;
|
||||
#ifdef SUPPORT_TLS
|
||||
{
|
||||
// extern const char *iotx_ca_crt;
|
||||
// pub_key = iotx_ca_crt;
|
||||
}
|
||||
#endif
|
||||
|
||||
http_handle = wrapper_http_init();
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_URL, (void *)url_format);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_PORT, (void *)&port);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_METHOD, (void *)&method);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_HEADER, (void *)header);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_CERT, (void *)pub_key);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_TIMEOUT, (void *)&timeout_ms);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVCALLBACK, (void *)_recv_callback);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVMAXLEN, (void *)&http_recv_maxlen);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVCONTEXT, (void *)&response);
|
||||
|
||||
res = wrapper_http_perform(http_handle, request_payload, strlen(request_payload));
|
||||
wrapper_http_deinit(&http_handle);
|
||||
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
dynreg_free(url);
|
||||
return res;
|
||||
}
|
||||
dynreg_free(url);
|
||||
dynreg_info("Http Response Payload: %s", response_payload);
|
||||
|
||||
_parse_dynreg_result(response_payload, "code", &start, &end);
|
||||
dynreg_info("Dynamic Register code: %.*s", end - start + 1, &response_payload[start]);
|
||||
|
||||
if (memcmp(&response_payload[start], "200", strlen("200")) != 0)
|
||||
{
|
||||
iotx_state_event(ITE_STATE_HTTP_COMM, STATE_HTTP_DYNREG_FAIL_RESP, &response_payload[start]);
|
||||
return STATE_HTTP_DYNREG_FAIL_RESP;
|
||||
}
|
||||
|
||||
_parse_dynreg_result(response_payload, "data", &data_start, &data_end);
|
||||
/* dynreg_info("value: %.*s\n",data_end - data_start + 1,&response_payload[data_start]); */
|
||||
|
||||
if (_parse_dynreg_result(&response_payload[data_start + 1], "productKey", &start, &end) < 0)
|
||||
return STATE_HTTP_DYNREG_FAIL_RESP;
|
||||
dynreg_info("Dynamic Register Product Key: %.*s", end - start + 1, &response_payload[data_start + 1 + start]);
|
||||
if (end - start + 1 > IOTX_PRODUCT_KEY_LEN)
|
||||
{
|
||||
return STATE_HTTP_DYNREG_INVALID_DS;
|
||||
}
|
||||
memcpy(product_key, &response_payload[data_start + 1 + start], end - start + 1);
|
||||
|
||||
if (_parse_dynreg_result(&response_payload[data_start + 1], "deviceName", &start, &end) < 0)
|
||||
return STATE_HTTP_DYNREG_FAIL_RESP;
|
||||
dynreg_info("Dynamic Register Device Name: %.*s", end - start + 1, &response_payload[data_start + 1 + start]);
|
||||
if (end - start + 1 > IOTX_DEVICE_NAME_LEN)
|
||||
{
|
||||
return STATE_HTTP_DYNREG_INVALID_DS;
|
||||
}
|
||||
memcpy(device_name, &response_payload[data_start + 1 + start], end - start + 1);
|
||||
|
||||
if (_parse_dynreg_result(&response_payload[data_start + 1], "deviceSecret", &start, &end) < 0)
|
||||
return STATE_HTTP_DYNREG_FAIL_RESP;
|
||||
dynreg_info("Dynamic Register Device Secret: %.*s", end - start + 1, &response_payload[data_start + 1 + start]);
|
||||
if (end - start + 1 > IOTX_DEVICE_SECRET_LEN)
|
||||
{
|
||||
return STATE_HTTP_DYNREG_INVALID_DS;
|
||||
}
|
||||
memcpy(device_secret, &response_payload[data_start + 1 + start], end - start + 1);
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
static int _fetch_get_reg_code_http_resp(char *request_payload, char *response_payload,
|
||||
iotx_http_region_types_t region, char reg_code[IOTX_DEVICE_REG_CODE_LEN])
|
||||
{
|
||||
int res = 0;
|
||||
int port = 0;
|
||||
const char *domain = NULL;
|
||||
char url_format[128] = {0};
|
||||
|
||||
if (dynamic_register_region == ACCESS_IS_FORMAL) // 生产环境
|
||||
{
|
||||
memcpy(url_format, "http://10.111.186.1/asset-web-api/registerService/getRegisterCode", strlen("http://10.111.186.1/asset-web-api/registerService/getRegisterCode"));
|
||||
port = 11901;
|
||||
}
|
||||
|
||||
if (dynamic_register_region == ACCESS_IS_DEBUG) // 接入环境
|
||||
{
|
||||
memcpy(url_format, "http://121.196.185.161/api/access/pile/getRegisterCode", strlen("http://121.196.185.161/api/access/pile/getRegisterCode"));
|
||||
port = 8081;
|
||||
}
|
||||
|
||||
if (dynamic_register_region == ACCESS_IS_TEST) // 测试环境
|
||||
{
|
||||
memcpy(url_format, "http://124.126.19.5/asset-web-serv-v5.3.0/registerService/getRegisterCode", strlen("http://124.126.19.5/asset-web-serv-v5.3.0/registerService/getRegisterCode"));
|
||||
port = 19849;
|
||||
}
|
||||
|
||||
char *url = NULL;
|
||||
int url_len = 0;
|
||||
const char *pub_key = NULL;
|
||||
void *http_handle = NULL;
|
||||
|
||||
iotx_http_method_t method = IOTX_HTTP_POST;
|
||||
int timeout_ms = 10000;
|
||||
char *header = "Accept: text/xml,text/javascript,text/html,application/json\r\n"
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
int http_recv_maxlen = HTTP_RESPONSE_PAYLOAD_LEN;
|
||||
dynreg_http_response_t response;
|
||||
int start = 0, end = 0, data_start = 0, data_end = 0;
|
||||
|
||||
memset(&response, 0, sizeof(dynreg_http_response_t));
|
||||
response.payload = response_payload;
|
||||
response.payload_len = HTTP_RESPONSE_PAYLOAD_LEN;
|
||||
#ifdef SUPPORT_TLS
|
||||
{
|
||||
// extern const char *iotx_ca_crt;
|
||||
// pub_key = iotx_ca_crt;
|
||||
}
|
||||
#endif
|
||||
|
||||
http_handle = wrapper_http_init();
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_URL, (void *)url_format);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_PORT, (void *)&port);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_METHOD, (void *)&method);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_HEADER, (void *)header);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_CERT, (void *)pub_key);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_TIMEOUT, (void *)&timeout_ms);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVCALLBACK, (void *)_recv_callback);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVMAXLEN, (void *)&http_recv_maxlen);
|
||||
wrapper_http_setopt(http_handle, IOTX_HTTPOPT_RECVCONTEXT, (void *)&response);
|
||||
|
||||
res = wrapper_http_perform(http_handle, request_payload, strlen(request_payload));
|
||||
wrapper_http_deinit(&http_handle);
|
||||
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
dynreg_free(url);
|
||||
return res;
|
||||
}
|
||||
dynreg_free(url);
|
||||
dynreg_info("Http Response Payload: %s", response_payload);
|
||||
|
||||
_parse_dynreg_result(response_payload, "code", &start, &end);
|
||||
dynreg_info("Dynamic Register code: %.*s", end - start + 1, &response_payload[start]);
|
||||
|
||||
if (memcmp(&response_payload[start], "200", strlen("200")) != 0)
|
||||
{
|
||||
iotx_state_event(ITE_STATE_HTTP_COMM, STATE_HTTP_DYNREG_FAIL_RESP, &response_payload[start]);
|
||||
return STATE_HTTP_DYNREG_FAIL_RESP;
|
||||
}
|
||||
|
||||
_parse_dynreg_result(response_payload, "data", &data_start, &data_end);
|
||||
/* dynreg_info("value: %.*s\n",data_end - data_start + 1,&response_payload[data_start]); */
|
||||
|
||||
if (_parse_dynreg_result(&response_payload[data_start + 1], "registerCode", &start, &end) < 0)
|
||||
return STATE_HTTP_DYNREG_FAIL_RESP;
|
||||
dynreg_info("device code get registerCode is: %.*s", end - start + 1, &response_payload[data_start + 1 + start]);
|
||||
if (end - start + 1 > IOTX_DEVICE_REG_CODE_LEN)
|
||||
{
|
||||
return STATE_HTTP_DYNREG_INVALID_DS;
|
||||
}
|
||||
memcpy(reg_code, &response_payload[data_start + 1 + start], end - start + 1);
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t _http_dynamic_register(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta)
|
||||
{
|
||||
int res = 0, dynamic_register_request_len = 0;
|
||||
char sign[DYNREG_SIGN_LENGTH] = {0};
|
||||
char random[DYNREG_RANDOM_KEY_LENGTH + 1] = {0};
|
||||
const char *dynamic_register_format = "productKey=%s&deviceName=%s&random=%s&sign=%s&signMethod=%s";
|
||||
char *dynamic_register_request = NULL;
|
||||
char *dynamic_register_response = NULL;
|
||||
|
||||
if (strlen(meta->product_key) > IOTX_PRODUCT_KEY_LEN)
|
||||
{
|
||||
return STATE_USER_INPUT_PK;
|
||||
}
|
||||
|
||||
if (strlen(meta->product_secret) > IOTX_PRODUCT_SECRET_LEN)
|
||||
{
|
||||
return STATE_USER_INPUT_PS;
|
||||
}
|
||||
|
||||
if (strlen(meta->device_name) > IOTX_DEVICE_NAME_LEN)
|
||||
{
|
||||
return STATE_USER_INPUT_DN;
|
||||
}
|
||||
|
||||
/* Calcute Signature */
|
||||
res = _calc_dynreg_sign(meta->product_key, meta->product_secret, meta->device_name, random, sign);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Assemble Http Dynamic Register Request Payload */
|
||||
dynamic_register_request_len = strlen(dynamic_register_format) + strlen(meta->product_key) + strlen(meta->device_name) +
|
||||
strlen(random) + strlen(sign) + strlen(DYNREG_SIGN_METHOD_HMACSHA256) + 1;
|
||||
dynamic_register_request = dynreg_malloc(dynamic_register_request_len);
|
||||
if (dynamic_register_request == NULL)
|
||||
{
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(dynamic_register_request, 0, dynamic_register_request_len);
|
||||
HAL_Snprintf(dynamic_register_request, dynamic_register_request_len, dynamic_register_format,
|
||||
meta->product_key, meta->device_name, random, sign, DYNREG_SIGN_METHOD_HMACSHA256);
|
||||
|
||||
dynamic_register_response = dynreg_malloc(HTTP_RESPONSE_PAYLOAD_LEN);
|
||||
if (dynamic_register_response == NULL)
|
||||
{
|
||||
dynreg_free(dynamic_register_request);
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(dynamic_register_response, 0, HTTP_RESPONSE_PAYLOAD_LEN);
|
||||
|
||||
/* Send Http Request For Getting Device Secret */
|
||||
res = _fetch_dynreg_http_resp(dynamic_register_request, dynamic_register_response, region, meta->device_secret);
|
||||
|
||||
#ifdef INFRA_LOG_NETWORK_PAYLOAD
|
||||
dynreg_dbg("Downstream Payload:");
|
||||
iotx_facility_json_print(dynamic_register_response, LOG_DEBUG_LEVEL, '<');
|
||||
#endif
|
||||
|
||||
dynreg_free(dynamic_register_request);
|
||||
dynreg_free(dynamic_register_response);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t _http_custom_register(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta)
|
||||
{
|
||||
int res = 0, custom_register_request_len = 0;
|
||||
// char sign[DYNREG_SIGN_LENGTH] = {0};
|
||||
// char random[DYNREG_RANDOM_KEY_LENGTH + 1] = {0};
|
||||
const char *custom_register_format = "deviceCode=%s®isterCode=%s&gunNo=%s";
|
||||
char *custom_register_request = NULL;
|
||||
char *custom_register_response = NULL;
|
||||
const char *deviceCode = "";
|
||||
const char *gunNo = "";
|
||||
|
||||
if (strlen(meta->device_reg_code) > IOTX_DEVICE_REG_CODE_LEN)
|
||||
{
|
||||
return STATE_USER_INPUT_DR;
|
||||
}
|
||||
custom_register_request_len = strlen(custom_register_format) + strlen(meta->device_reg_code) + strlen(deviceCode) + strlen(gunNo) + 1;
|
||||
custom_register_request = dynreg_malloc(custom_register_request_len);
|
||||
if (custom_register_request == NULL)
|
||||
{
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(custom_register_request, 0, custom_register_request_len);
|
||||
HAL_Snprintf(custom_register_request, custom_register_request_len, custom_register_format, deviceCode,
|
||||
meta->device_reg_code, gunNo);
|
||||
|
||||
custom_register_response = dynreg_malloc(HTTP_RESPONSE_PAYLOAD_LEN);
|
||||
if (custom_register_response == NULL)
|
||||
{
|
||||
dynreg_free(custom_register_request);
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(custom_register_response, 0, HTTP_RESPONSE_PAYLOAD_LEN);
|
||||
|
||||
/* Send Http Request For Getting Device Secret */
|
||||
res = _fetch_custom_reg_http_resp(custom_register_request, custom_register_response, region, meta->product_key, meta->device_name, meta->device_secret);
|
||||
#ifdef INFRA_LOG_NETWORK_PAYLOAD
|
||||
dynreg_dbg("Downstream Payload:");
|
||||
iotx_facility_json_print(custom_register_response, LOG_DEBUG_LEVEL, '<');
|
||||
#endif
|
||||
|
||||
dynreg_free(custom_register_request);
|
||||
dynreg_free(custom_register_response);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t _http_get_regcode(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta)
|
||||
{
|
||||
int res = 0, custom_register_request_len = 0;
|
||||
// char sign[DYNREG_SIGN_LENGTH] = {0};
|
||||
// char random[DYNREG_RANDOM_KEY_LENGTH + 1] = {0};
|
||||
const char *custom_register_format = "deviceCode=%s";
|
||||
char *custom_register_request = NULL;
|
||||
char *custom_register_response = NULL;
|
||||
const char *deviceCode = "1";
|
||||
|
||||
if (strlen(deviceCode) > IOTX_DEVICE_UID_LEN)
|
||||
{
|
||||
return STATE_USER_INPUT_DR;
|
||||
}
|
||||
custom_register_request_len = strlen(custom_register_format) + strlen(meta->device_asset) + 1;
|
||||
custom_register_request = dynreg_malloc(custom_register_request_len);
|
||||
if (custom_register_request == NULL)
|
||||
{
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(custom_register_request, 0, custom_register_request_len);
|
||||
HAL_Snprintf(custom_register_request, custom_register_request_len, custom_register_format, meta->device_asset);
|
||||
|
||||
custom_register_response = dynreg_malloc(HTTP_RESPONSE_PAYLOAD_LEN);
|
||||
if (custom_register_response == NULL)
|
||||
{
|
||||
dynreg_free(custom_register_request);
|
||||
return STATE_SYS_DEPEND_MALLOC;
|
||||
}
|
||||
memset(custom_register_response, 0, HTTP_RESPONSE_PAYLOAD_LEN);
|
||||
|
||||
/* Send Http Request For Getting Device Secret */
|
||||
// res = _fetch_custom_reg_http_resp(custom_register_request, custom_register_response, region, meta->product_key,meta->device_name,meta->device_secret);
|
||||
res = _fetch_get_reg_code_http_resp(custom_register_request, custom_register_response, region, meta->device_reg_code);
|
||||
#ifdef INFRA_LOG_NETWORK_PAYLOAD
|
||||
dynreg_dbg("Downstream Payload:");
|
||||
iotx_facility_json_print(custom_register_response, LOG_DEBUG_LEVEL, '<');
|
||||
#endif
|
||||
|
||||
dynreg_free(custom_register_request);
|
||||
dynreg_free(custom_register_response);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
#else
|
||||
static int _mqtt_dynreg_sign_password(iotx_dev_meta_info_t *meta_info, iotx_sign_mqtt_t *signout, char *rand)
|
||||
{
|
||||
char signsource[DEV_SIGN_SOURCE_MAXLEN] = {0};
|
||||
uint16_t signsource_len = 0;
|
||||
const char sign_fmt[] = "deviceName%sproductKey%srandom%s";
|
||||
uint8_t sign_hex[32] = {0};
|
||||
|
||||
signsource_len = strlen(sign_fmt) + strlen(meta_info->device_name) + 1 + strlen(meta_info->product_key) + strlen(rand) + 1;
|
||||
if (signsource_len >= DEV_SIGN_SOURCE_MAXLEN)
|
||||
{
|
||||
return STATE_MQTT_SIGN_SOURCE_BUF_SHORT;
|
||||
}
|
||||
memset(signsource, 0, signsource_len);
|
||||
memcpy(signsource, "deviceName", strlen("deviceName"));
|
||||
memcpy(signsource + strlen(signsource), meta_info->device_name, strlen(meta_info->device_name));
|
||||
memcpy(signsource + strlen(signsource), "productKey", strlen("productKey"));
|
||||
memcpy(signsource + strlen(signsource), meta_info->product_key, strlen(meta_info->product_key));
|
||||
memcpy(signsource + strlen(signsource), "random", strlen("random"));
|
||||
memcpy(signsource + strlen(signsource), rand, strlen(rand));
|
||||
|
||||
utils_hmac_sha256((const uint8_t *)signsource, strlen(signsource), (uint8_t *)meta_info->product_secret,
|
||||
strlen(meta_info->product_secret), sign_hex);
|
||||
infra_hex2str(sign_hex, 32, signout->password);
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
static int32_t _mqtt_dynreg_sign_clientid(iotx_dev_meta_info_t *meta_info, iotx_sign_mqtt_t *signout, char *rand)
|
||||
{
|
||||
const char *clientid1 = "|authType=register,random=";
|
||||
const char *clientid2 = ",signmethod=hmacsha256,securemode=2|";
|
||||
uint32_t clientid_len = 0;
|
||||
|
||||
clientid_len = strlen(meta_info->product_key) + 1 + strlen(meta_info->device_name) +
|
||||
strlen(clientid1) + strlen(rand) + strlen(clientid2) + 1;
|
||||
if (clientid_len >= DEV_SIGN_CLIENT_ID_MAXLEN)
|
||||
{
|
||||
return ERROR_DEV_SIGN_CLIENT_ID_TOO_SHORT;
|
||||
}
|
||||
memset(signout->clientid, 0, clientid_len);
|
||||
memcpy(signout->clientid, meta_info->product_key, strlen(meta_info->product_key));
|
||||
memcpy(signout->clientid + strlen(signout->clientid), ".", strlen("."));
|
||||
memcpy(signout->clientid + strlen(signout->clientid), meta_info->device_name, strlen(meta_info->device_name));
|
||||
memcpy(signout->clientid + strlen(signout->clientid), clientid1, strlen(clientid1));
|
||||
memcpy(signout->clientid + strlen(signout->clientid), rand, strlen(rand));
|
||||
memcpy(signout->clientid + strlen(signout->clientid), clientid2, strlen(clientid2));
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
void _mqtt_dynreg_topic_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
|
||||
{
|
||||
int32_t res = 0;
|
||||
char *ds = (char *)pcontext;
|
||||
iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
|
||||
const char *asterisk = "**********************";
|
||||
|
||||
switch (msg->event_type)
|
||||
{
|
||||
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
|
||||
{
|
||||
/* print topic name and topic message */
|
||||
char *device_secret = NULL;
|
||||
uint32_t device_secret_len = 0;
|
||||
|
||||
if (memcmp(topic_info->ptopic, "/ext/register", strlen("/ext/register")))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* parse secret */
|
||||
res = infra_json_value((char *)topic_info->payload, topic_info->payload_len, "deviceSecret", strlen("deviceSecret"),
|
||||
&device_secret, &device_secret_len);
|
||||
if (res == STATE_SUCCESS)
|
||||
{
|
||||
memcpy(ds, device_secret + 1, device_secret_len - 2);
|
||||
memcpy(device_secret + 1 + 5, asterisk, strlen(asterisk));
|
||||
dynreg_info("Topic : %.*s", topic_info->topic_len, topic_info->ptopic);
|
||||
dynreg_info("Payload: %.*s", topic_info->payload_len, topic_info->payload);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t _mqtt_dynamic_register(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta)
|
||||
{
|
||||
void *pClient = NULL;
|
||||
iotx_mqtt_param_t mqtt_params;
|
||||
int32_t res = 0;
|
||||
uint32_t length = 0, rand = 0;
|
||||
char rand_str[9] = {0};
|
||||
iotx_sign_mqtt_t signout;
|
||||
uint64_t timestart = 0, timenow = 0;
|
||||
char device_secret[IOTX_DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
memset(&signout, 0, sizeof(iotx_sign_mqtt_t));
|
||||
|
||||
/* setup hostname */
|
||||
if (IOTX_HTTP_REGION_CUSTOM == region)
|
||||
{
|
||||
if (g_infra_mqtt_domain[region] == NULL)
|
||||
{
|
||||
return STATE_USER_INPUT_MQTT_DOMAIN;
|
||||
}
|
||||
|
||||
length = strlen(g_infra_mqtt_domain[region]) + 1;
|
||||
if (length >= DEV_SIGN_HOSTNAME_MAXLEN)
|
||||
{
|
||||
return STATE_MQTT_SIGN_HOSTNAME_BUF_SHORT;
|
||||
}
|
||||
|
||||
memset(signout.hostname, 0, DEV_SIGN_HOSTNAME_MAXLEN);
|
||||
memcpy(signout.hostname, g_infra_mqtt_domain[region], strlen(g_infra_mqtt_domain[region]));
|
||||
}
|
||||
else
|
||||
{
|
||||
length = strlen(meta->product_key) + strlen(g_infra_mqtt_domain[region]) + 2;
|
||||
if (length >= DEV_SIGN_HOSTNAME_MAXLEN)
|
||||
{
|
||||
return STATE_MQTT_SIGN_HOSTNAME_BUF_SHORT;
|
||||
}
|
||||
memset(signout.hostname, 0, DEV_SIGN_HOSTNAME_MAXLEN);
|
||||
memcpy(signout.hostname, meta->product_key, strlen(meta->product_key));
|
||||
memcpy(signout.hostname + strlen(signout.hostname), ".", strlen("."));
|
||||
memcpy(signout.hostname + strlen(signout.hostname), g_infra_mqtt_domain[region],
|
||||
strlen(g_infra_mqtt_domain[region]));
|
||||
}
|
||||
|
||||
/* setup port */
|
||||
signout.port = 443;
|
||||
|
||||
/* setup username */
|
||||
length = strlen(meta->device_name) + strlen(meta->product_key) + 2;
|
||||
if (length >= DEV_SIGN_USERNAME_MAXLEN)
|
||||
{
|
||||
return STATE_MQTT_SIGN_USERNAME_BUF_SHORT;
|
||||
}
|
||||
memset(signout.username, 0, DEV_SIGN_USERNAME_MAXLEN);
|
||||
memcpy(signout.username, meta->device_name, strlen(meta->device_name));
|
||||
memcpy(signout.username + strlen(signout.username), "&", strlen("&"));
|
||||
memcpy(signout.username + strlen(signout.username), meta->product_key, strlen(meta->product_key));
|
||||
|
||||
/* password */
|
||||
rand = HAL_Random(0xffffffff);
|
||||
infra_hex2str((unsigned char *)&rand, 4, rand_str);
|
||||
res = _mqtt_dynreg_sign_password(meta, &signout, rand_str);
|
||||
if (res < 0)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
/* client id */
|
||||
res = _mqtt_dynreg_sign_clientid(meta, &signout, rand_str);
|
||||
if (res < 0)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
memset(&mqtt_params, 0, sizeof(iotx_mqtt_param_t));
|
||||
mqtt_params.host = signout.hostname;
|
||||
mqtt_params.port = signout.port;
|
||||
mqtt_params.username = signout.username;
|
||||
mqtt_params.password = signout.password;
|
||||
mqtt_params.client_id = signout.clientid;
|
||||
mqtt_params.request_timeout_ms = 5000;
|
||||
mqtt_params.clean_session = 1;
|
||||
mqtt_params.keepalive_interval_ms = 60000;
|
||||
mqtt_params.read_buf_size = 1000;
|
||||
mqtt_params.write_buf_size = 1000;
|
||||
mqtt_params.handle_event.h_fp = _mqtt_dynreg_topic_handle;
|
||||
mqtt_params.handle_event.pcontext = device_secret;
|
||||
|
||||
#ifdef SUPPORT_TLS
|
||||
{
|
||||
// extern const char *iotx_ca_crt;
|
||||
// mqtt_params.pub_key = iotx_ca_crt;
|
||||
}
|
||||
#endif
|
||||
|
||||
pClient = wrapper_mqtt_init(&mqtt_params);
|
||||
if (pClient == NULL)
|
||||
{
|
||||
return STATE_MQTT_WRAPPER_INIT_FAIL;
|
||||
}
|
||||
|
||||
res = wrapper_mqtt_connect(pClient);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
wrapper_mqtt_release(&pClient);
|
||||
return res;
|
||||
}
|
||||
|
||||
timestart = HAL_UptimeMs();
|
||||
while (1)
|
||||
{
|
||||
timenow = HAL_UptimeMs();
|
||||
if (timenow < timestart)
|
||||
{
|
||||
timestart = timenow;
|
||||
}
|
||||
|
||||
if (timestart - timenow >= MQTT_DYNREG_TIMEOUT_MS)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
wrapper_mqtt_yield(pClient, 200);
|
||||
|
||||
if (strlen(device_secret) > 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (strlen(device_secret) > 0)
|
||||
{
|
||||
res = STATE_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = STATE_HTTP_DYNREG_INVALID_DS;
|
||||
}
|
||||
|
||||
wrapper_mqtt_release(&pClient);
|
||||
|
||||
memcpy(meta->device_secret, device_secret, strlen(device_secret));
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
int32_t IOT_Dynamic_Register(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta)
|
||||
{
|
||||
#ifdef MQTT_DYNAMIC_REGISTER
|
||||
return _mqtt_dynamic_register(region, meta);
|
||||
#else
|
||||
// return _http_dynamic_register(region, meta);
|
||||
return _http_custom_register(region, meta);
|
||||
#endif
|
||||
}
|
||||
// 设备唯一码换取注册码
|
||||
int32_t IOT_Get_Regcode(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta)
|
||||
{
|
||||
#ifdef MQTT_DYNAMIC_REGISTER
|
||||
return _mqtt_dynamic_register(region, meta);
|
||||
#else
|
||||
// return _http_dynamic_register(region, meta);
|
||||
return _http_get_regcode(region, meta);
|
||||
#endif
|
||||
}
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_DYNAMIC_REGISTER, void *data)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
dynamic_register = *(int *)data;
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_GET_DYNAMIC_REGISTER, void *data)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
|
||||
*(int *)data = dynamic_register;
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_REGISTER_REGION, void *data)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
dynamic_register_region = *(int *)data;
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef _DYNREG_API_H_
|
||||
#define _DYNREG_API_H_
|
||||
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
|
||||
#define MQTT_DYNREG_TIMEOUT_MS (15 * 1000)
|
||||
|
||||
int32_t IOT_Dynamic_Register(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta);
|
||||
int32_t IOT_Get_Regcode(iotx_http_region_types_t region, iotx_dev_meta_info_t *meta);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef _DYNREG_INTERNAL_H_
|
||||
#define _DYNREG_INTERNAL_H_
|
||||
|
||||
#include "wrappers.h"
|
||||
|
||||
#ifdef INFRA_LOG
|
||||
#include "infra_log.h"
|
||||
#define dynreg_info(...) log_info("dynreg", __VA_ARGS__)
|
||||
#define dynreg_err(...) log_err("dynreg", __VA_ARGS__)
|
||||
#define dynreg_dbg(...) log_debug("dynreg", __VA_ARGS__)
|
||||
#else
|
||||
#define dynreg_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define dynreg_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define dynreg_dbg(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#endif
|
||||
|
||||
#ifdef INFRA_MEM_STATS
|
||||
#include "infra_mem_stats.h"
|
||||
#define dynreg_malloc(size) LITE_malloc(size, MEM_MAGIC, "dynreg")
|
||||
#define dynreg_free(ptr) LITE_free(ptr)
|
||||
#else
|
||||
#define dynreg_malloc(size) HAL_Malloc(size)
|
||||
#define dynreg_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef MQTT_DYNAMIC_REGISTER
|
||||
#include "mqtt_api.h"
|
||||
void *wrapper_mqtt_init(iotx_mqtt_param_t *mqtt_params);
|
||||
int wrapper_mqtt_connect(void *client);
|
||||
int wrapper_mqtt_yield(void *client, int timeout_ms);
|
||||
int wrapper_mqtt_subscribe(void *client,
|
||||
const char *topicFilter,
|
||||
iotx_mqtt_qos_t qos,
|
||||
iotx_mqtt_event_handle_func_fpt topic_handle_func,
|
||||
void *pcontext);
|
||||
int wrapper_mqtt_release(void **pclient);
|
||||
#endif
|
||||
Vendored
+1110
File diff suppressed because it is too large
Load Diff
Vendored
+334
@@ -0,0 +1,334 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#ifndef INFRA_AES_H
|
||||
#define INFRA_AES_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(INFRA_AES_BUILDIN)
|
||||
#include "infra_aes_config.h"
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "infra_compat.h"
|
||||
|
||||
|
||||
|
||||
/* padlock.c and aesni.c rely on these values! */
|
||||
#define INFRA_AES_ENCRYPT 1
|
||||
#define INFRA_AES_DECRYPT 0
|
||||
|
||||
#define INFRA_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
|
||||
#define INFRA_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
|
||||
|
||||
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
||||
!defined(inline) && !defined(__cplusplus)
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief AES context structure
|
||||
*
|
||||
* \note buf is able to hold 32 extra bytes, which can be used:
|
||||
* - for alignment purposes if VIA padlock is used, and/or
|
||||
* - to simplify key expansion in the 256-bit case by
|
||||
* generating an extra round key
|
||||
*/
|
||||
typedef struct {
|
||||
int nr; /*!< number of rounds */
|
||||
uint32_t *rk; /*!< AES round keys */
|
||||
uint32_t buf[68]; /*!< unaligned data */
|
||||
}
|
||||
infra_aes_context;
|
||||
|
||||
/**
|
||||
* \brief Initialize AES context
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
*/
|
||||
|
||||
void infra_aes_init(infra_aes_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief Clear AES context
|
||||
*
|
||||
* \param ctx AES context to be cleared
|
||||
*/
|
||||
void infra_aes_free(infra_aes_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (encryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key encryption key
|
||||
* \param keybits must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or INFRA_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int infra_aes_setkey_enc(infra_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits);
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (decryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key decryption key
|
||||
* \param keybits must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or INFRA_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int infra_aes_setkey_dec(infra_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits);
|
||||
|
||||
/**
|
||||
* \brief AES-ECB block encryption/decryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode INFRA_AES_ENCRYPT or INFRA_AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int infra_aes_crypt_ecb(infra_aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
#if defined(INFRA_CIPHER_MODE_CBC)
|
||||
/**
|
||||
* \brief AES-CBC buffer encryption/decryption
|
||||
* Length should be a multiple of the block
|
||||
* size (16 bytes)
|
||||
*
|
||||
* \note Upon exit, the content of the IV is updated so that you can
|
||||
* call the function same function again on the following
|
||||
* block(s) of data and get the same result as if it was
|
||||
* encrypted in one call. This allows a "streaming" usage.
|
||||
* If on the other hand you need to retain the contents of the
|
||||
* IV, you should either save it manually or use the cipher
|
||||
* module instead.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode INFRA_AES_ENCRYPT or INFRA_AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful, or INFRA_ERR_AES_INVALID_INPUT_LENGTH
|
||||
*/
|
||||
int infra_aes_crypt_cbc(infra_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
#endif /* INFRA_CIPHER_MODE_CBC */
|
||||
|
||||
#if defined(INFRA_CIPHER_MODE_CFB)
|
||||
/**
|
||||
* \brief AES-CFB128 buffer encryption/decryption.
|
||||
*
|
||||
* Note: Due to the nature of CFB you should use the same key schedule for
|
||||
* both encryption and decryption. So a context initialized with
|
||||
* infra_aes_setkey_enc() for both INFRA_AES_ENCRYPT and INFRA_AES_DECRYPT.
|
||||
*
|
||||
* \note Upon exit, the content of the IV is updated so that you can
|
||||
* call the function same function again on the following
|
||||
* block(s) of data and get the same result as if it was
|
||||
* encrypted in one call. This allows a "streaming" usage.
|
||||
* If on the other hand you need to retain the contents of the
|
||||
* IV, you should either save it manually or use the cipher
|
||||
* module instead.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode INFRA_AES_ENCRYPT or INFRA_AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv_off offset in IV (updated after use)
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int infra_aes_crypt_cfb128(infra_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
|
||||
/**
|
||||
* \brief AES-CFB8 buffer encryption/decryption.
|
||||
*
|
||||
* Note: Due to the nature of CFB you should use the same key schedule for
|
||||
* both encryption and decryption. So a context initialized with
|
||||
* infra_aes_setkey_enc() for both INFRA_AES_ENCRYPT and INFRA_AES_DECRYPT.
|
||||
*
|
||||
* \note Upon exit, the content of the IV is updated so that you can
|
||||
* call the function same function again on the following
|
||||
* block(s) of data and get the same result as if it was
|
||||
* encrypted in one call. This allows a "streaming" usage.
|
||||
* If on the other hand you need to retain the contents of the
|
||||
* IV, you should either save it manually or use the cipher
|
||||
* module instead.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode INFRA_AES_ENCRYPT or INFRA_AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int infra_aes_crypt_cfb8(infra_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
#endif /*INFRA_CIPHER_MODE_CFB */
|
||||
|
||||
#if defined(INFRA_CIPHER_MODE_CTR)
|
||||
/**
|
||||
* \brief AES-CTR buffer encryption/decryption
|
||||
*
|
||||
* Warning: You have to keep the maximum use of your counter in mind!
|
||||
*
|
||||
* Note: Due to the nature of CTR you should use the same key schedule for
|
||||
* both encryption and decryption. So a context initialized with
|
||||
* infra_aes_setkey_enc() for both INFRA_AES_ENCRYPT and INFRA_AES_DECRYPT.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param length The length of the data
|
||||
* \param nc_off The offset in the current stream_block (for resuming
|
||||
* within current cipher stream). The offset pointer to
|
||||
* should be 0 at the start of a stream.
|
||||
* \param nonce_counter The 128-bit nonce and counter.
|
||||
* \param stream_block The saved stream-block for resuming. Is overwritten
|
||||
* by the function.
|
||||
* \param input The input data stream
|
||||
* \param output The output data stream
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int infra_aes_crypt_ctr(infra_aes_context *ctx,
|
||||
size_t length,
|
||||
size_t *nc_off,
|
||||
unsigned char nonce_counter[16],
|
||||
unsigned char stream_block[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
#endif /* INFRA_CIPHER_MODE_CTR */
|
||||
|
||||
/**
|
||||
* \brief Internal AES block encryption function
|
||||
* (Only exposed to allow overriding it,
|
||||
* see INFRA_AES_ENCRYPT_ALT)
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param input Plaintext block
|
||||
* \param output Output (ciphertext) block
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int infra_internal_aes_encrypt(infra_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
/**
|
||||
* \brief Internal AES block decryption function
|
||||
* (Only exposed to allow overriding it,
|
||||
* see INFRA_AES_DECRYPT_ALT)
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param input Ciphertext block
|
||||
* \param output Output (plaintext) block
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int infra_internal_aes_decrypt(infra_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int infra_aes_self_test(int verbose);
|
||||
|
||||
typedef infra_aes_context mbedtls_aes_context;
|
||||
|
||||
#if !defined(INFRA_AES_BUILDIN)
|
||||
void mbedtls_aes_free( mbedtls_aes_context *ctx );
|
||||
void mbedtls_aes_init(mbedtls_aes_context *ctx);
|
||||
int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits);
|
||||
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits);
|
||||
int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
|
||||
#define MBEDTLS_AES_DECRYPT INFRA_AES_DECRYPT
|
||||
#define MBEDTLS_AES_ENCRYPT INFRA_AES_ENCRYPT
|
||||
#endif
|
||||
|
||||
#define AES_BLOCK_SIZE (16)
|
||||
|
||||
|
||||
|
||||
p_Aes128_t infra_aes128_init(
|
||||
const uint8_t *key,
|
||||
const uint8_t *iv,
|
||||
AES_DIR_t dir);
|
||||
|
||||
int infra_aes128_destroy(p_Aes128_t aes);
|
||||
|
||||
int infra_aes128_cbc_decrypt(
|
||||
p_Aes128_t aes,
|
||||
const void *src,
|
||||
size_t blockNum,
|
||||
void *dst);
|
||||
|
||||
int infra_aes128_cfb_decrypt(
|
||||
p_Aes128_t aes,
|
||||
const void *src,
|
||||
size_t length,
|
||||
void *dst);
|
||||
|
||||
int infra_aes128_cfb_encrypt(
|
||||
p_Aes128_t aes,
|
||||
const void *src,
|
||||
size_t length,
|
||||
void *dst);
|
||||
|
||||
int infra_aes128_cbc_encrypt(
|
||||
p_Aes128_t aes,
|
||||
const void *src,
|
||||
size_t blockNum,
|
||||
void *dst);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* aes.h */
|
||||
|
||||
+1900
File diff suppressed because it is too large
Load Diff
+126
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _INFRA_CJSON_H_
|
||||
#define _INFRA_CJSON_H_
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#define cJSON_False (1 << 0)
|
||||
#define cJSON_True (1 << 1)
|
||||
#define cJSON_NULL (1 << 2)
|
||||
#define cJSON_Number (1 << 3)
|
||||
#define cJSON_String (1 << 4)
|
||||
#define cJSON_Array (1 << 5)
|
||||
#define cJSON_Object (1 << 6)
|
||||
#define cJSON_Raw (1 << 7) /* raw json */
|
||||
|
||||
#ifndef LITE_CJSON_NESTING_LIMIT
|
||||
#define LITE_CJSON_NESTING_LIMIT 1000
|
||||
#endif
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct lite_cjson_st {
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *value;
|
||||
int value_length;
|
||||
|
||||
/* The item's size, if type == cJSON_Array and type == cJSON_Object */
|
||||
int size;
|
||||
|
||||
double value_double;
|
||||
int value_int;
|
||||
} lite_cjson_t;
|
||||
|
||||
int lite_cjson_parse(const char *src, int src_len, lite_cjson_t *lite);
|
||||
|
||||
int lite_cjson_is_false(lite_cjson_t *lite);
|
||||
int lite_cjson_is_true(lite_cjson_t *lite);
|
||||
int lite_cjson_is_null(lite_cjson_t *lite);
|
||||
int lite_cjson_is_number(lite_cjson_t *lite);
|
||||
int lite_cjson_is_string(lite_cjson_t *lite);
|
||||
int lite_cjson_is_array(lite_cjson_t *lite);
|
||||
int lite_cjson_is_object(lite_cjson_t *lite);
|
||||
|
||||
int lite_cjson_array_item(lite_cjson_t *lite, int index, lite_cjson_t *lite_item);
|
||||
int lite_cjson_object_item(
|
||||
lite_cjson_t *lite,
|
||||
const char *key,
|
||||
int key_len,
|
||||
lite_cjson_t *lite_item);
|
||||
int lite_cjson_object_item_by_index(
|
||||
lite_cjson_t *lite,
|
||||
int index,
|
||||
lite_cjson_t *lite_item_key,
|
||||
lite_cjson_t *lite_item_value);
|
||||
|
||||
|
||||
/*** lite_cjson create, add and print ***/
|
||||
typedef int cJSON_bool;
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
#define cJSON_StringIsConst 512
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct lite_cjson_item_t {
|
||||
struct lite_cjson_item_t *next,
|
||||
*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct lite_cjson_item_t
|
||||
*child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
|
||||
int type; /* The type of the item, as above. */
|
||||
|
||||
char *valuestring; /* The item's string, if type==cJSON_String */
|
||||
int valueint; /* The item's number, if type==cJSON_Number */
|
||||
double valuedouble; /* The item's number, if type==cJSON_Number */
|
||||
|
||||
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
} lite_cjson_item_t;
|
||||
|
||||
typedef struct {
|
||||
void *(*malloc_fn)(unsigned int sz);
|
||||
void(*free_fn)(void *ptr);
|
||||
} lite_cjson_hooks;
|
||||
|
||||
void lite_cjson_init_hooks(lite_cjson_hooks *hooks);
|
||||
|
||||
/* Render a lite_cjson_item_t entity to text for transfer/storage. Free the char* when finished. */
|
||||
char *lite_cjson_print(lite_cjson_item_t *item);
|
||||
char *lite_cjson_print_unformatted(lite_cjson_item_t *item);
|
||||
|
||||
/* Delete a lite_cjson_item_t entity and all subentities. */
|
||||
void lite_cjson_delete(lite_cjson_item_t *item);
|
||||
|
||||
/* Append item to specific object */
|
||||
void lite_cjson_add_item_to_array(lite_cjson_item_t *array, lite_cjson_item_t *item);
|
||||
void lite_cjson_add_item_to_object(lite_cjson_item_t *object, const char *string, lite_cjson_item_t *item);
|
||||
|
||||
/* These calls create a lite_cjson_item_t item of the appropriate type. */
|
||||
lite_cjson_item_t *lite_cjson_create_null(void);
|
||||
lite_cjson_item_t *lite_cjson_create_true(void);
|
||||
lite_cjson_item_t *lite_cjson_create_false(void);
|
||||
lite_cjson_item_t *lite_cjson_create_bool(int b);
|
||||
lite_cjson_item_t *lite_cjson_create_number(double num);
|
||||
lite_cjson_item_t *lite_cjson_create_string(const char *string);
|
||||
lite_cjson_item_t *lite_cjson_create_array(void);
|
||||
lite_cjson_item_t *lite_cjson_create_object(void);
|
||||
|
||||
/* These utilities create an Array of count items. */
|
||||
lite_cjson_item_t *lite_cjson_create_intArray(const int *numbers, int count);
|
||||
lite_cjson_item_t *lite_cjson_create_floatArray(const float *numbers, int count);
|
||||
lite_cjson_item_t *lite_cjson_create_doubleArray(const double *numbers, int count);
|
||||
lite_cjson_item_t *lite_cjson_create_stringArray(const char **strings, int count);
|
||||
|
||||
/* Macros for creating things quickly. */
|
||||
#define lite_cjson_add_null_to_object(object,name) lite_cjson_add_item_to_object(object, name, lite_cjson_create_null())
|
||||
#define lite_cjson_add_true_to_object(object,name) lite_cjson_add_item_to_object(object, name, lite_cjson_create_true())
|
||||
#define lite_cjson_add_false_to_object(object,name) lite_cjson_add_item_to_object(object, name, lite_cjson_create_false())
|
||||
#define lite_cjson_add_bool_to_object(object,name,b) lite_cjson_add_item_to_object(object, name, lite_cjson_create_bool(b))
|
||||
#define lite_cjson_add_number_to_object(object,name,n) lite_cjson_add_item_to_object(object, name, lite_cjson_create_number(n))
|
||||
#define lite_cjson_add_string_to_object(object,name,s) lite_cjson_add_item_to_object(object, name, lite_cjson_create_string(s))
|
||||
#endif
|
||||
|
||||
+525
@@ -0,0 +1,525 @@
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_COMPAT
|
||||
#include <string.h>
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
#include "infra_state.h"
|
||||
#include "infra_compat.h"
|
||||
#if defined(INFRA_LOG)
|
||||
#include "infra_log.h"
|
||||
#endif
|
||||
#if defined(INFRA_MEM_STATS)
|
||||
#include "infra_mem_stats.h"
|
||||
#endif
|
||||
|
||||
#include "wrappers.h"
|
||||
|
||||
/*sdk_impl_ctx_t g_sdk_impl_ctx = {0};*/
|
||||
|
||||
#if !defined(INFRA_LOG)
|
||||
void IOT_SetLogLevel(IOT_LogLevel level) {}
|
||||
#endif
|
||||
|
||||
#if defined(WIFI_PROVISION_ENABLED)
|
||||
extern void awss_set_press_timeout_ms(unsigned int timeout_ms);
|
||||
extern void awss_set_channel_scan_interval_ms(uint32_t timeout_ms);
|
||||
#endif
|
||||
|
||||
static char http_custom_domain[IOTX_DOMAIN_MAX_LEN + 1] = {0};
|
||||
static iotx_dev_meta_info_t g_dev_meta_info_t = {0};
|
||||
/* global variable for mqtt construction */
|
||||
static iotx_conn_info_t g_iotx_conn_info = {0};
|
||||
static char g_empty_string[1] = "";
|
||||
|
||||
int IOT_SetupConnInfo(const char *product_key,
|
||||
const char *device_name,
|
||||
const char *device_secret,
|
||||
void **info_ptr)
|
||||
{
|
||||
if (product_key == NULL || device_name == NULL || device_secret == NULL ||
|
||||
strlen(product_key) > IOTX_PRODUCT_KEY_LEN ||
|
||||
strlen(device_name) > IOTX_DEVICE_NAME_LEN ||
|
||||
strlen(device_secret) > IOTX_DEVICE_SECRET_LEN)
|
||||
{
|
||||
return NULL_VALUE_ERROR;
|
||||
}
|
||||
|
||||
if (info_ptr)
|
||||
{
|
||||
memset(&g_iotx_conn_info, 0, sizeof(iotx_conn_info_t));
|
||||
g_iotx_conn_info.host_name = g_empty_string;
|
||||
g_iotx_conn_info.client_id = g_empty_string;
|
||||
g_iotx_conn_info.username = g_empty_string;
|
||||
g_iotx_conn_info.password = g_empty_string;
|
||||
g_iotx_conn_info.pub_key = g_empty_string;
|
||||
|
||||
*info_ptr = &g_iotx_conn_info;
|
||||
}
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_REGION, void *);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_GET_REGION, void *);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_MQTT_DOMAIN, void *data);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_HTTP_DOMAIN, void *data);
|
||||
#if defined(DYNAMIC_REGISTER)
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_DYNAMIC_REGISTER, void *data);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_GET_DYNAMIC_REGISTER, void *);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_REGISTER_REGION, void *data);
|
||||
#endif
|
||||
int IOCTL_FUNC(IOTX_IOCTL_FOTA_TIMEOUT_MS, void *data);
|
||||
#if defined(DEVICE_MODEL_ENABLED)
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_CUSTOMIZE_INFO, void *data);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_PROXY_REGISTER, void *);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_OTA_DEV_ID, void *);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_QUERY_DEVID, void *data);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_MQTT_PORT, void *);
|
||||
#endif
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_AWSS_ENABLE_INTERVAL, void *data);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_AWSS_CHANNEL_SCAN_INTERVAL, void *data);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SUB_USER_TOPIC, void *);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_RECV_PROP_REPLY, void *);
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SEND_PROP_SET_REPLY, void *);
|
||||
|
||||
int IOCTL_FUNC(IOTX_IOCTL_SET_HTTP_DOMAIN, void *data)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
memset(http_custom_domain, 0, strlen((char *)data) + 1);
|
||||
memcpy(http_custom_domain, data, strlen((char *)data));
|
||||
g_infra_http_domain[IOTX_CLOUD_REGION_CUSTOM] = (const char *)http_custom_domain;
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
int IOT_Ioctl(int option, void *data)
|
||||
{
|
||||
int res = FAIL_RETURN;
|
||||
iotx_dev_meta_info_t *meta = NULL;
|
||||
|
||||
meta = &g_dev_meta_info_t;
|
||||
|
||||
if (option < 0 || data == NULL)
|
||||
{
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
switch (option)
|
||||
{
|
||||
case IOTX_IOCTL_GET_MODULE:
|
||||
{
|
||||
if (NULL == data)
|
||||
{
|
||||
res = FAIL_RETURN;
|
||||
break;
|
||||
}
|
||||
memcpy(data, meta->module, strlen(meta->module));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_MODULE:
|
||||
if ((data != NULL) && (strlen(data) <= IOTX_MODULE_LEN))
|
||||
{
|
||||
memset(meta->module, 0, IOTX_MODULE_LEN + 1);
|
||||
memcpy(meta->module, data, strlen(data));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = FAIL_RETURN;
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_REGION:
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_REGION, data);
|
||||
break;
|
||||
case IOTX_IOCTL_GET_REGION:
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_GET_REGION, data);
|
||||
break;
|
||||
|
||||
case IOTX_IOCTL_SET_MQTT_DOMAIN:
|
||||
{
|
||||
int domain_type = IOTX_CLOUD_REGION_CUSTOM;
|
||||
IOCTL_FUNC(IOTX_IOCTL_SET_REGION, &domain_type);
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_MQTT_DOMAIN, data);
|
||||
}
|
||||
break;
|
||||
|
||||
case IOTX_IOCTL_SET_HTTP_DOMAIN:
|
||||
{
|
||||
int region = IOTX_HTTP_REGION_CUSTOM;
|
||||
IOCTL_FUNC(IOTX_IOCTL_SET_REGION, ®ion);
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_HTTP_DOMAIN, data);
|
||||
}
|
||||
break;
|
||||
#if defined(DYNAMIC_REGISTER)
|
||||
case IOTX_IOCTL_SET_DYNAMIC_REGISTER:
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_DYNAMIC_REGISTER, data);
|
||||
break;
|
||||
case IOTX_IOCTL_GET_DYNAMIC_REGISTER:
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_GET_DYNAMIC_REGISTER, data);
|
||||
break;
|
||||
case IOTX_IOCTL_SET_REGISTER_REGION:
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_REGISTER_REGION, data);
|
||||
break;
|
||||
#endif
|
||||
|
||||
break;
|
||||
case IOTX_IOCTL_SET_PRODUCT_KEY:
|
||||
{
|
||||
if ((data != NULL) && (strlen(data) <= IOTX_PRODUCT_KEY_LEN))
|
||||
{
|
||||
memset(meta->product_key, 0, IOTX_PRODUCT_KEY_LEN + 1);
|
||||
memcpy(meta->product_key, data, strlen(data));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = FAIL_RETURN;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_GET_PRODUCT_KEY:
|
||||
{
|
||||
memcpy(data, meta->product_key, strlen(meta->product_key));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_PRODUCT_SECRET:
|
||||
{
|
||||
if ((data != NULL) && (strlen(data) <= IOTX_PRODUCT_SECRET_LEN))
|
||||
{
|
||||
memset(meta->product_secret, 0, IOTX_PRODUCT_SECRET_LEN + 1);
|
||||
memcpy(meta->product_secret, data, strlen(data));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = FAIL_RETURN;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_GET_PRODUCT_SECRET:
|
||||
{
|
||||
memcpy(data, meta->product_secret, strlen(meta->product_secret));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_DEVICE_NAME:
|
||||
{
|
||||
if ((data != NULL) && (strlen(data) <= IOTX_DEVICE_NAME_LEN))
|
||||
{
|
||||
memset(meta->device_name, 0, IOTX_DEVICE_NAME_LEN + 1);
|
||||
memcpy(meta->device_name, data, strlen(data));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = FAIL_RETURN;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_GET_DEVICE_NAME:
|
||||
{
|
||||
memcpy(data, meta->device_name, strlen(meta->device_name));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_DEVICE_SECRET:
|
||||
{
|
||||
if ((data != NULL) && (strlen(data) <= IOTX_DEVICE_SECRET_LEN))
|
||||
{
|
||||
memset(meta->device_secret, 0, IOTX_DEVICE_SECRET_LEN + 1);
|
||||
memcpy(meta->device_secret, data, strlen(data));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = FAIL_RETURN;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_GET_DEVICE_SECRET:
|
||||
{
|
||||
memcpy(data, meta->device_secret, strlen(meta->device_secret));
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
break;
|
||||
#if defined(DEVICE_MODEL_ENABLED) && !defined(DEPRECATED_LINKKIT)
|
||||
#if !defined(DEVICE_MODEL_RAWDATA_SOLO)
|
||||
case IOTX_IOCTL_RECV_EVENT_REPLY:
|
||||
case IOTX_IOCTL_RECV_PROP_REPLY:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_RECV_PROP_REPLY, data);
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SEND_PROP_SET_REPLY:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SEND_PROP_SET_REPLY, data);
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#if defined(DEVICE_MODEL_GATEWAY)
|
||||
#ifdef DEVICE_MODEL_SUBDEV_OTA
|
||||
case IOTX_IOCTL_SET_OTA_DEV_ID:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_OTA_DEV_ID, data);
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
case IOTX_IOCTL_RECV_EVENT_REPLY:
|
||||
case IOTX_IOCTL_RECV_PROP_REPLY:
|
||||
case IOTX_IOCTL_SEND_PROP_SET_REPLY:
|
||||
case IOTX_IOCTL_GET_SUBDEV_LOGIN:
|
||||
{
|
||||
res = SUCCESS_RETURN;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#if defined(DEVICE_MODEL_ENABLED)
|
||||
case IOTX_IOCTL_FOTA_TIMEOUT_MS:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_FOTA_TIMEOUT_MS, data);
|
||||
}
|
||||
break;
|
||||
|
||||
case IOTX_IOCTL_SUB_USER_TOPIC:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SUB_USER_TOPIC, data);
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_CUSTOMIZE_INFO:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_CUSTOMIZE_INFO, data);
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_MQTT_PORT:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_MQTT_PORT, data);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(DEVICE_MODEL_GATEWAY) && !defined(DEPRECATED_LINKKIT)
|
||||
case IOTX_IOCTL_SET_PROXY_REGISTER:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_PROXY_REGISTER, data);
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_QUERY_DEVID:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_QUERY_DEVID, data);
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_SUBDEV_SIGN:
|
||||
{
|
||||
/* todo */
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_GET_SUBDEV_LOGIN:
|
||||
{
|
||||
/* todo */
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(WIFI_PROVISION_ENABLED)
|
||||
case IOTX_IOCTL_SET_AWSS_ENABLE_INTERVAL:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_AWSS_ENABLE_INTERVAL, data);
|
||||
}
|
||||
break;
|
||||
case IOTX_IOCTL_SET_AWSS_CHANNEL_SCAN_INTERVAL:
|
||||
{
|
||||
res = IOCTL_FUNC(IOTX_IOCTL_SET_AWSS_CHANNEL_SCAN_INTERVAL, data);
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
default:
|
||||
{
|
||||
res = FAIL_RETURN;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void IOT_DumpMemoryStats(IOT_LogLevel level)
|
||||
{
|
||||
#ifdef INFRA_MEM_STATS
|
||||
int lvl = (int)level;
|
||||
|
||||
if (lvl > LOG_DEBUG_LEVEL)
|
||||
{
|
||||
lvl = LOG_DEBUG_LEVEL;
|
||||
HAL_Printf("Invalid input level, using default: %d => %d", level, lvl);
|
||||
}
|
||||
|
||||
LITE_dump_malloc_free_stats(lvl);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void *g_event_monitor = NULL;
|
||||
|
||||
int iotx_event_regist_cb(void (*monitor_cb)(int event))
|
||||
{
|
||||
g_event_monitor = (void *)monitor_cb;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iotx_event_post(int event)
|
||||
{
|
||||
if (g_event_monitor == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
((void (*)(int))g_event_monitor)(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int eventid;
|
||||
void *callback;
|
||||
} impl_event_map_t;
|
||||
|
||||
static impl_event_map_t g_impl_event_map[] = {
|
||||
{ITE_AWSS_STATUS, NULL},
|
||||
{ITE_CONNECT_SUCC, NULL},
|
||||
{ITE_CONNECT_FAIL, NULL},
|
||||
{ITE_DISCONNECTED, NULL},
|
||||
{ITE_RAWDATA_ARRIVED, NULL},
|
||||
{ITE_SERVICE_REQUEST, NULL},
|
||||
{ITE_SERVICE_REQUEST_EXT, NULL},
|
||||
{ITE_PROPERTY_SET, NULL},
|
||||
{ITE_PROPERTY_GET, NULL},
|
||||
#ifdef DEVICE_MODEL_SHADOW
|
||||
{ITE_PROPERTY_DESIRED_GET_REPLY, NULL},
|
||||
#endif
|
||||
{ITE_REPORT_REPLY, NULL},
|
||||
{ITE_TRIGGER_EVENT_REPLY, NULL},
|
||||
{ITE_TIMESTAMP_REPLY, NULL},
|
||||
{ITE_TOPOLIST_REPLY, NULL},
|
||||
{ITE_PERMIT_JOIN, NULL},
|
||||
{ITE_INITIALIZE_COMPLETED, NULL},
|
||||
{ITE_FOTA, NULL},
|
||||
{ITE_FOTA_MODULE, NULL},
|
||||
{ITE_COTA, NULL},
|
||||
{ITE_MQTT_CONNECT_SUCC, NULL},
|
||||
{ITE_CLOUD_ERROR, NULL},
|
||||
{ITE_DYNREG_DEVICE_SECRET, NULL},
|
||||
{ITE_IDENTITY_RESPONSE, NULL},
|
||||
{ITE_BIND_EVENT, NULL},
|
||||
{ITE_STATE_EVERYTHING, NULL},
|
||||
{ITE_STATE_USER_INPUT, NULL},
|
||||
{ITE_STATE_SYS_DEPEND, NULL},
|
||||
{ITE_STATE_MQTT_COMM, NULL},
|
||||
{ITE_STATE_WIFI_PROV, NULL},
|
||||
{ITE_STATE_COAP_LOCAL, NULL},
|
||||
{ITE_STATE_HTTP_COMM, NULL},
|
||||
{ITE_STATE_OTA, NULL},
|
||||
{ITE_STATE_DEV_BIND, NULL},
|
||||
{ITE_STATE_DEV_MODEL, NULL}};
|
||||
|
||||
void *iotx_event_callback(int evt)
|
||||
{
|
||||
if (evt < 0 || evt >= sizeof(g_impl_event_map) / sizeof(impl_event_map_t))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return g_impl_event_map[evt].callback;
|
||||
}
|
||||
|
||||
DEFINE_EVENT_CALLBACK(ITE_AWSS_STATUS, int (*callback)(int))
|
||||
DEFINE_EVENT_CALLBACK(ITE_CONNECT_SUCC, int (*callback)(void))
|
||||
DEFINE_EVENT_CALLBACK(ITE_CONNECT_FAIL, int (*callback)(void))
|
||||
DEFINE_EVENT_CALLBACK(ITE_DISCONNECTED, int (*callback)(void))
|
||||
DEFINE_EVENT_CALLBACK(ITE_RAWDATA_ARRIVED, int (*callback)(const int, const unsigned char *, const int))
|
||||
DEFINE_EVENT_CALLBACK(ITE_SERVICE_REQUEST, int (*callback)(const int, const char *, const int, const char *,
|
||||
const int, char **, int *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_SERVICE_REQUEST_EXT, int (*callback)(int, const char *, int, const char *, int, const char *,
|
||||
int, void *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_PROPERTY_SET, int (*callback)(const int, const char *, const int))
|
||||
#ifdef DEVICE_MODEL_SHADOW
|
||||
DEFINE_EVENT_CALLBACK(ITE_PROPERTY_DESIRED_GET_REPLY, int (*callback)(const char *, const int))
|
||||
#endif
|
||||
DEFINE_EVENT_CALLBACK(ITE_PROPERTY_GET, int (*callback)(const int, const char *, const int, char **, int *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_REPORT_REPLY, int (*callback)(const int, const int, const int, const char *,
|
||||
const int))
|
||||
DEFINE_EVENT_CALLBACK(ITE_TRIGGER_EVENT_REPLY, int (*callback)(const int, const int, const int, const char *,
|
||||
const int, const char *, const int))
|
||||
DEFINE_EVENT_CALLBACK(ITE_TIMESTAMP_REPLY, int (*callback)(const char *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_TOPOLIST_REPLY, int (*callback)(const int, const int, const int, const char *,
|
||||
const int))
|
||||
DEFINE_EVENT_CALLBACK(ITE_PERMIT_JOIN, int (*callback)(const char *, int))
|
||||
DEFINE_EVENT_CALLBACK(ITE_INITIALIZE_COMPLETED, int (*callback)(const int))
|
||||
DEFINE_EVENT_CALLBACK(ITE_FOTA, int (*callback)(const int, const char *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_FOTA_MODULE, int (*callback)(const int, const char *, const char *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_COTA, int (*callback)(const int, const char *, int, const char *,
|
||||
const char *, const char *, const char *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_MQTT_CONNECT_SUCC, int (*callback)(void))
|
||||
DEFINE_EVENT_CALLBACK(ITE_CLOUD_ERROR, int (*callback)(const int, const char *, const char *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_DYNREG_DEVICE_SECRET, int (*callback)(const char *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_IDENTITY_RESPONSE, int (*callback)(const char *))
|
||||
DEFINE_EVENT_CALLBACK(ITE_BIND_EVENT, int (*callback)(const char *))
|
||||
int iotx_register_for_ITE_STATE_EVERYTHING(state_handler_t callback)
|
||||
{
|
||||
int idx = 0;
|
||||
|
||||
for (idx = ITE_STATE_EVERYTHING; idx <= ITE_STATE_DEV_MODEL; idx++)
|
||||
{
|
||||
g_impl_event_map[idx].callback = (void *)callback;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_USER_INPUT, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_SYS_DEPEND, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_MQTT_COMM, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_WIFI_PROV, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_COAP_LOCAL, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_HTTP_COMM, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_OTA, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_DEV_BIND, state_handler_t callback)
|
||||
DEFINE_EVENT_CALLBACK(ITE_STATE_DEV_MODEL, state_handler_t callback)
|
||||
|
||||
#if !defined(__APPLE__)
|
||||
extern int vsnprintf(char *str, size_t size, const char *format, va_list ap);
|
||||
#endif
|
||||
|
||||
#define IOTX_STATE_EVENT_MESSAGE_MAXLEN (64)
|
||||
int iotx_state_event(const int event, const int code, const char *msg_format, ...)
|
||||
{
|
||||
char message[IOTX_STATE_EVENT_MESSAGE_MAXLEN + 1] = {0};
|
||||
void *everything_state_handler = iotx_event_callback(ITE_STATE_EVERYTHING);
|
||||
void *state_handler = iotx_event_callback(event);
|
||||
va_list args;
|
||||
|
||||
if (state_handler == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (msg_format != NULL)
|
||||
{
|
||||
memset(message, 0, sizeof(message));
|
||||
va_start(args, msg_format);
|
||||
vsnprintf(message, IOTX_STATE_EVENT_MESSAGE_MAXLEN, msg_format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
((state_handler_t)state_handler)(code, message);
|
||||
|
||||
if (everything_state_handler && everything_state_handler != state_handler)
|
||||
{
|
||||
((state_handler_t)everything_state_handler)(code, message);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
+289
@@ -0,0 +1,289 @@
|
||||
#ifndef _INFRA_COMPAT_H_
|
||||
#define _INFRA_COMPAT_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "infra_defs.h"
|
||||
|
||||
#undef being_deprecated
|
||||
#define being_deprecated
|
||||
|
||||
#define IOCTL_FUNC(evt, params) iotx_ioctl_##evt(params)
|
||||
#define IOT_RegisterCallback(evt, cb) iotx_register_for_##evt(cb)
|
||||
#define DECLARE_EVENT_CALLBACK(evt, cb) int iotx_register_for_##evt(cb);
|
||||
#define DEFINE_EVENT_CALLBACK(evt, cb) \
|
||||
int iotx_register_for_##evt(cb) \
|
||||
{ \
|
||||
if (evt < 0 || evt >= sizeof(g_impl_event_map) / sizeof(impl_event_map_t)) \
|
||||
{ \
|
||||
return -1; \
|
||||
} \
|
||||
g_impl_event_map[evt].callback = (void *)callback; \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
/* compatible for V2.3.0 */
|
||||
#define IOTX_CLOUD_DOMAIN_SH IOTX_CLOUD_REGION_SHANGHAI
|
||||
#define IOTX_CLOUD_DOMAIN_SG IOTX_CLOUD_REGION_SINGAPORE
|
||||
#define IOTX_CLOUD_DOMAIN_JP IOTX_CLOUD_REGION_JAPAN
|
||||
#define IOTX_CLOUD_DOMAIN_US IOTX_CLOUD_REGION_USA_WEST
|
||||
#define IOTX_CLOUD_DOMAIN_GER IOTX_CLOUD_REGION_GERMANY
|
||||
#define IOTX_IOCTL_SET_DOMAIN IOTX_IOCTL_SET_REGION
|
||||
#define IOTX_IOCTL_GET_DOMAIN IOTX_IOCTL_GET_REGION
|
||||
|
||||
#define IOT_OpenLog(arg)
|
||||
#define IOT_CloseLog() IOT_SetLogLevel(IOT_LOG_NONE)
|
||||
#define IOT_LOG_EMERG IOT_LOG_NONE
|
||||
|
||||
#define IOT_Linkkit_Post IOT_Linkkit_Report
|
||||
#define AES_DECRYPTION HAL_AES_DECRYPTION
|
||||
#define AES_ENCRYPTION HAL_AES_ENCRYPTION
|
||||
|
||||
#define NETWORK_ADDR_LEN (16)
|
||||
typedef enum _IOT_LogLevel
|
||||
{
|
||||
IOT_LOG_NONE = 0,
|
||||
IOT_LOG_CRIT,
|
||||
IOT_LOG_ERROR,
|
||||
IOT_LOG_WARNING,
|
||||
IOT_LOG_INFO,
|
||||
IOT_LOG_DEBUG,
|
||||
} IOT_LogLevel;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t port;
|
||||
uint8_t init;
|
||||
char *host_name;
|
||||
char *client_id;
|
||||
char *username;
|
||||
char *password;
|
||||
const char *pub_key;
|
||||
} iotx_conn_info_t, *iotx_conn_info_pt;
|
||||
/**
|
||||
* @brief event list used for iotx_regist_event_monitor_cb
|
||||
*/
|
||||
enum iotx_event_t
|
||||
{
|
||||
IOTX_AWSS_START = 0x1000, /* AWSS start without enbale, just supports device discover */
|
||||
IOTX_AWSS_ENABLE, /* AWSS enable */
|
||||
IOTX_AWSS_LOCK_CHAN, /* AWSS lock channel(Got AWSS sync packet) */
|
||||
IOTX_AWSS_CS_ERR, /* AWSS AWSS checksum is error */
|
||||
IOTX_AWSS_PASSWD_ERR, /* AWSS decrypt passwd error */
|
||||
IOTX_AWSS_GOT_SSID_PASSWD, /* AWSS parse ssid and passwd successfully */
|
||||
IOTX_AWSS_CONNECT_ADHA, /* AWSS try to connnect adha (device discover, router solution) */
|
||||
IOTX_AWSS_CONNECT_ADHA_FAIL, /* AWSS fails to connect adha */
|
||||
IOTX_AWSS_CONNECT_AHA, /* AWSS try to connect aha (AP solution) */
|
||||
IOTX_AWSS_CONNECT_AHA_FAIL, /* AWSS fails to connect aha */
|
||||
IOTX_AWSS_SETUP_NOTIFY, /* AWSS sends out device setup information (AP and router solution) */
|
||||
IOTX_AWSS_CONNECT_ROUTER, /* AWSS try to connect destination router */
|
||||
IOTX_AWSS_CONNECT_ROUTER_FAIL, /* AWSS fails to connect destination router. */
|
||||
IOTX_AWSS_GOT_IP, /* AWSS connects destination successfully and got ip address */
|
||||
IOTX_AWSS_SUC_NOTIFY, /* AWSS sends out success notify (AWSS sucess) */
|
||||
IOTX_AWSS_BIND_NOTIFY, /* AWSS sends out bind notify information to support bind between user and device */
|
||||
IOTX_AWSS_ENABLE_TIMEOUT, /* AWSS enable timeout(user needs to call awss_config_press again to enable awss) */
|
||||
IOTX_CONN_CLOUD = 0x2000, /* Device try to connect cloud */
|
||||
IOTX_CONN_CLOUD_FAIL, /* Device fails to connect cloud, refer to net_sockets.h for error code */
|
||||
IOTX_CONN_CLOUD_SUC, /* Device connects cloud successfully */
|
||||
IOTX_RESET = 0x3000, /* Linkkit reset success (just got reset response from cloud without any other operation) */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief register callback to monitor all event from system.
|
||||
*
|
||||
* @param callback, when some event occurs, the system will trigger callback to user.
|
||||
* refer to enum iotx_event_t for event list supported.
|
||||
*
|
||||
* @return 0 when success, -1 when fail.
|
||||
* @note: user should make sure that callback is not block and runs to complete fast.
|
||||
*/
|
||||
int iotx_event_regist_cb(void (*monitor_cb)(int event));
|
||||
|
||||
/**
|
||||
* @brief post event to trigger callback resitered by iotx_event_regist_cb
|
||||
*
|
||||
* @param event, event id, refer to iotx_event_t
|
||||
*
|
||||
* @return 0 when success, -1 when fail.
|
||||
*/
|
||||
int iotx_event_post(int event);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ITE_AWSS_STATUS,
|
||||
ITE_CONNECT_SUCC,
|
||||
ITE_CONNECT_FAIL,
|
||||
ITE_DISCONNECTED,
|
||||
ITE_RAWDATA_ARRIVED,
|
||||
ITE_SERVICE_REQUEST,
|
||||
ITE_SERVICE_REQUEST_EXT,
|
||||
ITE_PROPERTY_SET,
|
||||
ITE_PROPERTY_GET,
|
||||
#ifdef DEVICE_MODEL_SHADOW
|
||||
ITE_PROPERTY_DESIRED_GET_REPLY,
|
||||
#endif
|
||||
ITE_REPORT_REPLY,
|
||||
ITE_TRIGGER_EVENT_REPLY,
|
||||
ITE_TIMESTAMP_REPLY,
|
||||
ITE_TOPOLIST_REPLY,
|
||||
ITE_PERMIT_JOIN,
|
||||
ITE_INITIALIZE_COMPLETED,
|
||||
ITE_FOTA,
|
||||
ITE_FOTA_MODULE,
|
||||
ITE_COTA,
|
||||
ITE_MQTT_CONNECT_SUCC,
|
||||
ITE_CLOUD_ERROR,
|
||||
ITE_DYNREG_DEVICE_SECRET,
|
||||
ITE_IDENTITY_RESPONSE,
|
||||
ITE_BIND_EVENT,
|
||||
ITE_STATE_EVERYTHING,
|
||||
ITE_STATE_USER_INPUT,
|
||||
ITE_STATE_SYS_DEPEND,
|
||||
ITE_STATE_MQTT_COMM,
|
||||
ITE_STATE_WIFI_PROV,
|
||||
ITE_STATE_COAP_LOCAL,
|
||||
ITE_STATE_HTTP_COMM,
|
||||
ITE_STATE_OTA,
|
||||
ITE_STATE_DEV_BIND,
|
||||
ITE_STATE_DEV_MODEL
|
||||
} iotx_ioctl_event_t;
|
||||
|
||||
DECLARE_EVENT_CALLBACK(ITE_AWSS_STATUS, int (*cb)(int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_CONNECT_SUCC, int (*cb)(void))
|
||||
DECLARE_EVENT_CALLBACK(ITE_CONNECT_FAIL, int (*cb)(void))
|
||||
DECLARE_EVENT_CALLBACK(ITE_DISCONNECTED, int (*cb)(void))
|
||||
DECLARE_EVENT_CALLBACK(ITE_RAWDATA_ARRIVED, int (*cb)(const int, const unsigned char *, const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_SERVICE_REQUEST, int (*cb)(const int, const char *, const int, const char *, const int,
|
||||
char **, int *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_SERVICE_REQUEST_EXT, int (*cb)(int, const char *, int, const char *, int, const char *, int,
|
||||
void *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_PROPERTY_SET, int (*cb)(const int, const char *, const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_PROPERTY_DESIRED_GET_REPLY, int (*cb)(const char *, const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_PROPERTY_GET, int (*cb)(const int, const char *, const int, char **, int *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_REPORT_REPLY, int (*cb)(const int, const int, const int, const char *, const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_TRIGGER_EVENT_REPLY, int (*cb)(const int, const int, const int, const char *, const int,
|
||||
const char *, const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_TIMESTAMP_REPLY, int (*cb)(const char *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_TOPOLIST_REPLY, int (*cb)(const int, const int, const int, const char *, const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_PERMIT_JOIN, int (*cb)(const char *, const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_INITIALIZE_COMPLETED, int (*cb)(const int))
|
||||
DECLARE_EVENT_CALLBACK(ITE_FOTA, int (*cb)(const int, const char *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_FOTA_MODULE, int (*cb)(const int, const char *, const char *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_COTA, int (*cb)(const int, const char *, int, const char *, const char *,
|
||||
const char *, const char *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_MQTT_CONNECT_SUCC, int (*cb)(void))
|
||||
DECLARE_EVENT_CALLBACK(ITE_CLOUD_ERROR, int (*cb)(const int, const char *, const char *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_DYNREG_DEVICE_SECRET, int (*cb)(const char *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_IDENTITY_RESPONSE, int (*cb)(const char *))
|
||||
DECLARE_EVENT_CALLBACK(ITE_BIND_EVENT, int (*cb)(const char *))
|
||||
|
||||
typedef int (*state_handler_t)(const int state_code, const char *state_message);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_EVERYTHING, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_USER_INPUT, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_SYS_DEPEND, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_MQTT_COMM, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_WIFI_PROV, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_COAP_LOCAL, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_HTTP_COMM, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_OTA, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_DEV_BIND, state_handler_t cb);
|
||||
DECLARE_EVENT_CALLBACK(ITE_STATE_DEV_MODEL, state_handler_t cb);
|
||||
|
||||
int iotx_state_event(const int event, const int state_code, const char *state_message_format, ...);
|
||||
|
||||
void *iotx_event_callback(int evt);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IOTX_IOCTL_SET_REGION, /* value(int*): iotx_cloud_region_types_t */
|
||||
IOTX_IOCTL_GET_REGION, /* value(int*) */
|
||||
IOTX_IOCTL_SET_MQTT_DOMAIN, /* value(const char*): point to mqtt domain string */
|
||||
IOTX_IOCTL_SET_HTTP_DOMAIN, /* value(const char*): point to http domain string */
|
||||
IOTX_IOCTL_SET_DYNAMIC_REGISTER, /* value(int*): 0 - Disable Dynamic Register, 1 - Enable Dynamic Register */
|
||||
IOTX_IOCTL_GET_DYNAMIC_REGISTER, /* value(int*) */
|
||||
IOTX_IOCTL_RECV_PROP_REPLY, /* value(int*): 0 - Disable property post reply by cloud; 1 - Enable property post reply by cloud */
|
||||
IOTX_IOCTL_RECV_EVENT_REPLY, /* value(int*): 0 - Disable event post reply by cloud; 1 - Enable event post reply by cloud */
|
||||
IOTX_IOCTL_SEND_PROP_SET_REPLY, /* value(int*): 0 - Disable send post set reply by devid; 1 - Enable property set reply by devid */
|
||||
IOTX_IOCTL_SET_SUBDEV_SIGN, /* value(const char*): only for slave device, set signature of subdevice */
|
||||
IOTX_IOCTL_GET_SUBDEV_LOGIN, /* value(int*): 0 - SubDev is logout; 1 - SubDev is login */
|
||||
IOTX_IOCTL_SET_OTA_DEV_ID, /* value(int*): select the device to do OTA according to devid */
|
||||
IOTX_IOCTL_FOTA_TIMEOUT_MS, /* value(int*): set Firmware OTA max retry timeout */
|
||||
IOTX_IOCTL_SET_CUSTOMIZE_INFO, /* value(char*): set mqtt clientID customize information */
|
||||
IOTX_IOCTL_SET_MQTT_PORT, /* value(uint16_t *) modify mqtt server port number */
|
||||
IOTX_IOCTL_SET_AWSS_ENABLE_INTERVAL, /* value(uint32_t*): - set an interval during which wifi-provision is enabled, unit is Ms*/
|
||||
IOTX_IOCTL_SET_AWSS_CHANNEL_SCAN_INTERVAL, /* value(uint32_t*): - set an interval during which a channel is scanned, unit is Ms*/
|
||||
IOTX_IOCTL_SET_PROXY_REGISTER, /* value(int*): 0 - Disable proxy product register, 1 - Enable proxy product register */
|
||||
IOTX_IOCTL_QUERY_DEVID, /* value(iotx_dev_meta_info_t*): device meta info, only productKey and deviceName is required, ret value is subdev_id or -1 */
|
||||
IOTX_IOCTL_SUB_USER_TOPIC, /* subscribe a topic according to user topic and callback */
|
||||
IOTX_IOCTL_SET_PRODUCT_KEY, /* vale(char *) - set product key */
|
||||
IOTX_IOCTL_GET_PRODUCT_KEY, /* vale(char[IOTX_PRODUCT_KEY_LEN + 1]) - get product key */
|
||||
IOTX_IOCTL_SET_PRODUCT_SECRET, /* vale(char *) - set product secret */
|
||||
IOTX_IOCTL_GET_PRODUCT_SECRET, /* vale(char[IOTX_PRODUCT_SECRET_LEN + 1]) - get product secret */
|
||||
IOTX_IOCTL_SET_DEVICE_NAME, /* vale(char *) - set device name */
|
||||
IOTX_IOCTL_GET_DEVICE_NAME, /* vale(char[IOTX_DEVICE_NAME_LEN + 1]) - get device name */
|
||||
IOTX_IOCTL_SET_DEVICE_SECRET, /* vale(char *) - set device secret */
|
||||
IOTX_IOCTL_GET_DEVICE_SECRET, /* vale(char[IOTX_DEVICE_SECRET_LEN + 1]) - get device secret */
|
||||
IOTX_IOCTL_SET_MODULE, /* vale(char[IOTX_MODULE_LEN + 1]) - set module info*/
|
||||
IOTX_IOCTL_GET_MODULE, /* vale(char[IOTX_MODULE_LEN + 1]) - get module info */
|
||||
IOTX_IOCTL_SET_REGISTER_REGION
|
||||
} iotx_ioctl_option_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ACCESS_IS_FORMAL = 0, // 接入正式平台
|
||||
ACCESS_IS_DEBUG, // 接入调试平台
|
||||
ACCESS_IS_TEST, // 接入测试平台
|
||||
ACCESS_IS_CUSTOM // 自定义
|
||||
} evs_register_region;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IMPL_LINKKIT_IOCTL_SWITCH_PROPERTY_POST_REPLY, /* only for master device, choose whether you need receive property post reply message */
|
||||
IMPL_LINKKIT_IOCTL_SWITCH_EVENT_POST_REPLY, /* only for master device, choose whether you need receive event post reply message */
|
||||
IMPL_LINKKIT_IOCTL_SWITCH_PROPERTY_SET_REPLY, /* only for master device, choose whether you need send property set reply message */
|
||||
IMPL_LINKKIT_IOCTL_MAX
|
||||
} impl_linkkit_ioctl_cmd_t;
|
||||
|
||||
/**
|
||||
* @brief Setup Demain type, should be called before MQTT connection.
|
||||
*
|
||||
* @param [in] option: see iotx_ioctl_option_t.
|
||||
*
|
||||
* @return None.
|
||||
* @see None.
|
||||
*/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
HAL_AES_ENCRYPTION = 0,
|
||||
HAL_AES_DECRYPTION = 1,
|
||||
} AES_DIR_t;
|
||||
|
||||
typedef void *p_Aes128_t;
|
||||
|
||||
typedef struct _network_addr_t
|
||||
{
|
||||
unsigned char
|
||||
addr[NETWORK_ADDR_LEN];
|
||||
unsigned short port;
|
||||
} NetworkAddr;
|
||||
|
||||
int IOT_Ioctl(int option, void *data);
|
||||
|
||||
int IOT_SetupConnInfo(const char *product_key,
|
||||
const char *device_name,
|
||||
const char *device_secret,
|
||||
void **info_ptr);
|
||||
|
||||
void IOT_SetLogLevel(IOT_LogLevel level);
|
||||
|
||||
void IOT_DumpMemoryStats(IOT_LogLevel level);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _INFRA_COMPAT_H_ */
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
#ifndef _INFRA_CONFIG_H_
|
||||
#define _INFRA_CONFIG_H_
|
||||
|
||||
#define PLATFORM_HAS_MBEDTLS
|
||||
#define PLATFORM_HAS_STDINT
|
||||
//#define PLATFORM_IS_DEBUG //测试平台或接入调试平台时打开
|
||||
//#define ACCESS_PLATFORM //接入调试平台时打开
|
||||
//#define PLATFORM_HAS_DYNMEM
|
||||
#define INFRA_STRING
|
||||
#define INFRA_NET
|
||||
#define INFRA_LIST
|
||||
//#define INFRA_LOG_NETWORK_PAYLOAD
|
||||
#define INFRA_LOG
|
||||
#define INFRA_LOG_ALL_MUTED
|
||||
#define INFRA_LOG_MUTE_FLW
|
||||
#define INFRA_LOG_MUTE_DBG
|
||||
#define INFRA_LOG_MUTE_INF
|
||||
#define INFRA_LOG_MUTE_WRN
|
||||
#define INFRA_LOG_MUTE_ERR
|
||||
#define INFRA_LOG_MUTE_CRT
|
||||
#define INFRA_TIMER
|
||||
#define INFRA_JSON_PARSER
|
||||
#define INFRA_CJSON
|
||||
#define INFRA_MD5
|
||||
#define INFRA_SHA256
|
||||
#define INFRA_REPORT
|
||||
#define INFRA_HTTPC
|
||||
#define INFRA_COMPAT
|
||||
#define DEV_SIGN
|
||||
#define MQTT_COMM_ENABLED
|
||||
#define MQTT_DEFAULT_IMPL
|
||||
#define MQTT_DIRECT
|
||||
#define MQTT_AUTO_SUBSCRIBE
|
||||
#define DYNAMIC_REGISTER
|
||||
#define HTTP_DYNAMIC_REGISTER
|
||||
//#define LOG_REPORT_TO_CLOUD
|
||||
#define DEVICE_MODEL_ENABLED
|
||||
#define HAL_KV
|
||||
//#define SUPPORT_TLS
|
||||
#define OTA_ENABLED
|
||||
|
||||
#endif
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
|
||||
#include "infra_config.h"
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
|
||||
const char *g_infra_mqtt_domain[IOTX_MQTT_DOMAIN_NUMBER] = {
|
||||
"iot-as-mqtt.cn-shanghai.aliyuncs.com", /* Shanghai */
|
||||
"iot-as-mqtt.ap-southeast-1.aliyuncs.com", /* Singapore */
|
||||
"iot-as-mqtt.ap-northeast-1.aliyuncs.com", /* Japan */
|
||||
"iot-as-mqtt.us-west-1.aliyuncs.com", /* America */
|
||||
"iot-as-mqtt.eu-central-1.aliyuncs.com", /* Germany */
|
||||
NULL, /* Custom */
|
||||
};
|
||||
#if !defined(PLATFORM_IS_DEBUG)//生产环境
|
||||
const char *g_infra_http_domain[IOTX_HTTP_DOMAIN_NUMBER] = {
|
||||
"iot-auth.cn-shanghai.aliyuncs.com", /* Shanghai */
|
||||
"iot-auth.ap-southeast-1.aliyuncs.com", /* Singapore */
|
||||
"iot-auth.ap-northeast-1.aliyuncs.com", /* Japan */
|
||||
"iot-auth.us-west-1.aliyuncs.com", /* America */
|
||||
"iot-auth.eu-central-1.aliyuncs.com", /* Germany */
|
||||
"10.111.186.1", /* Custom */
|
||||
};
|
||||
#elif defined(ACCESS_PLATFORM)//接入环境
|
||||
const char *g_infra_http_domain[IOTX_HTTP_DOMAIN_NUMBER] = {
|
||||
"iot-auth.cn-shanghai.aliyuncs.com", /* Shanghai */
|
||||
"iot-auth.ap-southeast-1.aliyuncs.com", /* Singapore */
|
||||
"iot-auth.ap-northeast-1.aliyuncs.com", /* Japan */
|
||||
"iot-auth.us-west-1.aliyuncs.com", /* America */
|
||||
"iot-auth.eu-central-1.aliyuncs.com", /* Germany */
|
||||
"121.196.185.161", /* Custom */
|
||||
};
|
||||
#else//测试环境
|
||||
const char *g_infra_http_domain[IOTX_HTTP_DOMAIN_NUMBER] = {
|
||||
"iot-auth.cn-shanghai.aliyuncs.com", /* Shanghai */
|
||||
"iot-auth.ap-southeast-1.aliyuncs.com", /* Singapore */
|
||||
"iot-auth.ap-northeast-1.aliyuncs.com", /* Japan */
|
||||
"iot-auth.us-west-1.aliyuncs.com", /* America */
|
||||
"iot-auth.eu-central-1.aliyuncs.com", /* Germany */
|
||||
"124.126.19.5", /* Custom */
|
||||
};
|
||||
#endif
|
||||
|
||||
+369
@@ -0,0 +1,369 @@
|
||||
#ifndef _INFRA_DEFS_H_
|
||||
#define _INFRA_DEFS_H_
|
||||
|
||||
#include "infra_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#define IOTX_SDK_REVISION "20200603.ota-fix-wrong-device-secret-issue-in-ota-demo"
|
||||
#define IOTX_SDK_VERSION "3.2.0"
|
||||
#define IOTX_ALINK_VERSION "20"
|
||||
#define IOTX_PRODUCT_KEY_LEN (20)
|
||||
#define IOTX_DEVICE_UID_LEN (16)
|
||||
#define IOTX_DEVICE_REG_CODE_LEN (64)
|
||||
#define IOTX_DEVICE_NAME_LEN (32)
|
||||
#define IOTX_DEVICE_SECRET_LEN (64)
|
||||
#define IOTX_MODULE_LEN (64)
|
||||
#define IOTX_DEVICE_ID_LEN (64)
|
||||
#define IOTX_PRODUCT_SECRET_LEN (64)
|
||||
#define IOTX_PARTNER_ID_LEN (64)
|
||||
#define IOTX_MODULE_ID_LEN (64)
|
||||
#define IOTX_NETWORK_IF_LEN (160)
|
||||
#define IOTX_FIRMWARE_VER_LEN (64)
|
||||
#define IOTX_URI_MAX_LEN (135)
|
||||
#define IOTX_DOMAIN_MAX_LEN (64)
|
||||
#define IOTX_CUSTOMIZE_INFO_LEN (80)
|
||||
|
||||
#ifndef _IN_
|
||||
#define _IN_
|
||||
#endif
|
||||
|
||||
#ifndef _OU_
|
||||
#define _OU_
|
||||
#endif
|
||||
|
||||
#ifndef _IN_OPT_
|
||||
#define _IN_OPT_
|
||||
#endif
|
||||
|
||||
#define NETWORK_ADDR_LEN (16)
|
||||
#define HAL_MAC_LEN (17 + 1) /* MAC地址的长度 */
|
||||
#define STR_SHORT_LEN (32)
|
||||
#ifndef ETH_ALEN
|
||||
#define ETH_ALEN (6)
|
||||
#endif
|
||||
#define HAL_MAX_SSID_LEN (32 + 1) /* ssid: 32 octets at most, include the NULL-terminated */
|
||||
#define HAL_MAX_PASSWD_LEN (64 + 1) /* password: 8-63 ascii */
|
||||
#define WLAN_CONNECTION_TIMEOUT_MS (30 * 1000)
|
||||
|
||||
#define DEV_SIGN_SOURCE_MAXLEN (200)
|
||||
#define DEV_SIGN_HOSTNAME_MAXLEN (64)
|
||||
#define DEV_SIGN_CLIENT_ID_MAXLEN (200)
|
||||
#define DEV_SIGN_USERNAME_MAXLEN (64)
|
||||
#define DEV_SIGN_PASSWORD_MAXLEN (65)
|
||||
|
||||
typedef enum IOT_RETURN_CODES
|
||||
{
|
||||
ERROR_DEVICE_NOT_EXIST = -311,
|
||||
ERROR_NET_TIMEOUT = -310,
|
||||
ERROR_CERT_VERIFY_FAIL = -309,
|
||||
ERROR_NET_SETOPT_TIMEOUT = -308,
|
||||
ERROR_NET_SOCKET = -307,
|
||||
ERROR_NET_CONNECT = -306,
|
||||
ERROR_NET_BIND = -305,
|
||||
ERROR_NET_LISTEN = -304,
|
||||
ERROR_NET_RECV = -303,
|
||||
ERROR_NET_SEND = -302,
|
||||
ERROR_NET_CONN = -301,
|
||||
ERROR_NET_UNKNOWN_HOST = -300,
|
||||
|
||||
MQTT_SUBHANDLE_LIST_LEN_TOO_SHORT = -47,
|
||||
MQTT_OFFLINE_LIST_LEN_TOO_SHORT = -46,
|
||||
MQTT_TOPIC_LEN_TOO_SHORT = -45,
|
||||
MQTT_CONNECT_BLOCK = -44,
|
||||
MQTT_SUB_INFO_NOT_FOUND_ERROR = -43,
|
||||
MQTT_PUSH_TO_LIST_ERROR = -42,
|
||||
MQTT_TOPIC_FORMAT_ERROR = -41,
|
||||
NETWORK_RECONNECT_TIMED_OUT_ERROR = -40, /** Returned when the Network is disconnected and the reconnect attempt has timed out */
|
||||
MQTT_CONNACK_UNKNOWN_ERROR = -39, /** Connect request failed with the server returning an unknown error */
|
||||
MQTT_CONANCK_UNACCEPTABLE_PROTOCOL_VERSION_ERROR = -38, /** Connect request failed with the server returning an unacceptable protocol version error */
|
||||
MQTT_CONNACK_IDENTIFIER_REJECTED_ERROR = -37, /** Connect request failed with the server returning an identifier rejected error */
|
||||
MQTT_CONNACK_SERVER_UNAVAILABLE_ERROR = -36, /** Connect request failed with the server returning an unavailable error */
|
||||
MQTT_CONNACK_BAD_USERDATA_ERROR = -35, /** Connect request failed with the server returning a bad userdata error */
|
||||
MQTT_CONNACK_NOT_AUTHORIZED_ERROR = -34, /** Connect request failed with the server failing to authenticate the request */
|
||||
MQTT_CONNECT_ERROR = -33,
|
||||
MQTT_CREATE_THREAD_ERROR = -32,
|
||||
MQTT_PING_PACKET_ERROR = -31,
|
||||
MQTT_CONNECT_PACKET_ERROR = -30,
|
||||
MQTT_CONNECT_ACK_PACKET_ERROR = -29,
|
||||
MQTT_NETWORK_CONNECT_ERROR = -28,
|
||||
MQTT_STATE_ERROR = -27,
|
||||
MQTT_SUBSCRIBE_PACKET_ERROR = -26,
|
||||
MQTT_SUBSCRIBE_ACK_PACKET_ERROR = -25,
|
||||
MQTT_SUBSCRIBE_ACK_FAILURE = -24,
|
||||
MQTT_SUBSCRIBE_QOS_ERROR = -23,
|
||||
MQTT_UNSUBSCRIBE_PACKET_ERROR = -22,
|
||||
MQTT_PUBLISH_PACKET_ERROR = -21,
|
||||
MQTT_PUBLISH_QOS_ERROR = -20,
|
||||
MQTT_PUBLISH_ACK_PACKET_ERROR = -19,
|
||||
MQTT_PUBLISH_COMP_PACKET_ERROR = -18,
|
||||
MQTT_PUBLISH_REC_PACKET_ERROR = -17,
|
||||
MQTT_PUBLISH_REL_PACKET_ERROR = -16,
|
||||
MQTT_UNSUBSCRIBE_ACK_PACKET_ERROR = -15,
|
||||
MQTT_NETWORK_ERROR = -14,
|
||||
MQTT_PUBLISH_ACK_TYPE_ERROR = -13,
|
||||
|
||||
ERROR_SHADOW_NO_METHOD = -2008,
|
||||
ERROR_SHADOW_UNDEF_TYPE = -2007,
|
||||
ERROR_SHADOW_UPDATE_TIMEOUT = -2006,
|
||||
ERROR_SHADOW_UPDATE_NACK = -2005, /**< Negative ACK */
|
||||
ERROR_SHADOW_NO_ATTRIBUTE = -2004,
|
||||
ERROR_SHADOW_ATTR_NO_EXIST = -2003, /**< NO such attribute */
|
||||
ERROR_SHADOW_ATTR_EXIST = -2002, /**< attribute already exists */
|
||||
ERROR_SHADOW_WAIT_LIST_OVERFLOW = -2001,
|
||||
ERROR_SHADOW_INVALID_STATE = -2000,
|
||||
|
||||
ERROR_SUBDEV_NULL_VALUE = -1501, /**< Indicating NULL value*/
|
||||
ERROR_SUBDEV_NOT_NULL_VALUE = -1500, /**< Indicating value not NULL*/
|
||||
ERROR_SUBDEV_STRING_NULL_VALUE = -1499, /**< Indicating NULL value or empty string */
|
||||
ERROR_SUBDEV_INVALID_GATEWAY_HANDLE = -1498, /**< Indicating gateway handle is null or invalid*/
|
||||
ERROR_SUBDEV_SESSION_NOT_FOUND = -1497, /**< Cannot find device session*/
|
||||
ERROR_SUBDEV_RRPC_CB_NOT_NULL = -1496, /**< RRPC callback function has been set,needn't to set again*/
|
||||
ERROR_SUBDEV_REPLY_TYPE_NOT_DEF = -1495, /**< Reply type not defined*/
|
||||
ERROR_SUBDEV_GET_JSON_VAL = -1494, /**< Get value from reply payload fail*/
|
||||
ERROR_SUBDEV_DATA_LEN_OVERFLOW = -1493, /**< Length of 'data' value from reply palyoad is large than limit(1024)*/
|
||||
ERROR_SUBDEV_MSG_LEN = -1492, /**< Indicating msg len is not correct*/
|
||||
ERROR_SUBDEV_REPLY_PROC = -1491, /**< Error occur when process publish reply */
|
||||
ERROR_SUBDEV_REPLY_TOPIC_NOT_MATCH = -1490, /**< Indicating that topic received is unknown*/
|
||||
ERROR_SUBDEV_REPLY_VAL_CHECK = -1489, /**< Indicating that value get from reply checked fail with local*/
|
||||
ERROR_SUBDEV_REGISTER_TYPE_NOT_DEF = -1488, /**< Register type not support*/
|
||||
ERROR_SUBDEV_PACKET_SPLICE_FAIL = -1487, /**< Splice packet error*/
|
||||
ERROR_SUBDEV_MQTT_PUBLISH_FAIL = -1486, /**< MQTT publish fail*/
|
||||
ERROR_SUBDEV_REPLY_PARSE_FAIL = -1485, /**< Parse reply fail*/
|
||||
ERROR_SUBDEV_CREATE_SESSION_FAIL = -1484, /**< Create session fail*/
|
||||
ERROR_SUBDEV_INVALID_CLEAN_SESSION_TYPE = -1483, /**< Clean session not support*/
|
||||
ERROR_SUBDEV_HAS_BEEN_LOGIN = -1482, /**< Device has been login*/
|
||||
ERROR_SUBDEV_SUB_UNSUB_FAIL = -1481, /**< subscribe or unsubscribe fail*/
|
||||
ERROR_SUBDEV_SESSION_STATE_FAIL = -1480, /**< Session state is error,may not login*/
|
||||
ERROR_SUBDEV_MEMORY_NOT_ENOUGH = -1479, /**< Set memory too small*/
|
||||
|
||||
ERROR_REPLY_TIMEOUT = -6099, /**< recieve reply timeout*/
|
||||
ERROR_DEVICE_NOT_FOUND = -6100, /**< device not found*/
|
||||
ERROR_TOO_LARGE_PAGE_SIZE = -6101, /**< page size must less than 200*/
|
||||
ERROR_DEVICE_COUNT_FAULT = -6102, /**< device count query service fault*/
|
||||
ERROR_DEVICE_DETAIL_FAULT = -6103, /**< device detail query service fault*/
|
||||
ERROR_TOO_LARGE_LIST_SIZE = -6104, /**< list size must less than 200*/
|
||||
ERROR_LIST_SIZE_CANNOT_BE_ZERO = -6105, /**< list size must greater than 0*/
|
||||
ERROR_TOO_LARGE_MAP_SIZE = -6106, /**< map size must less than 200*/
|
||||
ERROR_MAP_SIZE_CANNOT_BE_ZERO = -6107, /**< map size must greater than 0*/
|
||||
ERROR_DEVICE_STATUS_FAULT = -6108, /**< device status query service fault*/
|
||||
ERROR_DEVICE_INFO_FAULT = -6109, /**< device info query service fault*/
|
||||
ERROR_SET_THING_PROPERTIES_ERROR = -6150, /**< set thing properties error*/
|
||||
ERROR_INVOKE_THING_SERVICE_ERROR = -6151, /**< invoke thing service error*/
|
||||
ERROR_SCRIPT_REL_NOT_EXIST = -6200, /**< script relation not exist*/
|
||||
ERROR_SCRIPT_CONVERT_DATA_IS_NULL = -6201, /**< script convert data is null*/
|
||||
ERROR_DEVICE_PRODUCT_NOT_EXIST = -6202, /**< product not exist*/
|
||||
ERROR_TOPIC_NOT_EXIST = -6203, /**< topic not exist*/
|
||||
ERROR_DEVICE_IS_DISABLED = -6204, /**< device is disabled*/
|
||||
ERROR_IOT_MESSAGE_ERROR = -6205, /**< iot message service error*/
|
||||
ERROR_PRODUCT_PROPERTY_NOT_EXIST = -6206, /**< product property not exist*/
|
||||
ERROR_DATA_FORMAT_ERROR = -6207, /**< device data format is error*/
|
||||
ERROR_THING_STATUS_PROHIBITED = -6208, /**< thing status is prohibited*/
|
||||
ERROR_THING_STATUS_NOT_ACTIVE = -6209, /**< thing status not active*/
|
||||
/**
|
||||
*
|
||||
* -6250 ~ -6299
|
||||
*/
|
||||
ERROR_PRODUCT_NOT_FOUND = -6250, /**< product not found*/
|
||||
ERROR_DEVICE_EXISTS = -6251, /**< device has existed*/
|
||||
ERROR_JUDGE_DEVICE_EXISTS_ERROR = -6252, /**< judge device exists error*/
|
||||
ERROR_ADD_DEVICE_FAILED = -6253, /**< add device failed*/
|
||||
ERROR_UPDATE_DEVICE_FAILED = -6254, /**< update device failed*/
|
||||
ERROR_INSERT_DGR_FAILED = -6255, /**< insert device group relation failed*/
|
||||
ERROR_SYN_DEVICE_FAILED = -6256, /**< device synchronization failed*/
|
||||
ERROR_PRODUCT_DOMAIN_ILLEGAL = -6257, /**< product domain illegal*/
|
||||
ERROR_TENANID_ILLEGAL = -6258, /**< tenantId illegal*/
|
||||
ERROR_PRODUCT_REGION_ILLEGAL = -6259, /**< product region illegal*/
|
||||
ERROR_PRODUCT_NETTYPE_ILLEGAL = -6260, /**< product nettype illegal*/
|
||||
ERROR_INSERT_DEVICE_APPLY_DETAIL_FAILED = -6261, /**< insert device apply detail failed*/
|
||||
ERROR_UPDATE_DEVICE_APPLY_STATUS_FAILED = -6262, /**< update device apply status failed*/
|
||||
ERROR_DELERE_DGR_FAILED = -6263, /**< delete device group relation status*/
|
||||
ERROR_DELETE_DEVICE_FAILED = -6264, /**< delete device failed*/
|
||||
ERROR_QUERY_DEVICE_DETAIL_FAILED = -6265, /**< query device detail failed*/
|
||||
ERROR_QUERY_DEVICE_COUNT_FAILED = -6266, /**< query device count failed*/
|
||||
ERROR_QUERY_ACTIVE_DEVICE_COUNT_FAILED = -6267, /**< query active device count failed*/
|
||||
ERROR_INSERT_AGR_FAILED = -6268, /**< insert apply group relation failed*/
|
||||
ERROR_QUERY_DEVICE_APPLY_FAILED = -6269, /**< query device apply failed*/
|
||||
ERROR_QUERY_PRODUCT_FAILED = -6270, /**< query product failed*/
|
||||
ERROR_DEVICE_APPLY_NOT_FOUND = -6271, /**< device apply not found*/
|
||||
ERROR_RELEASE_TRIAD_FAILED = -6272, /**< release triad failed*/
|
||||
ERROR_UPDATE_DAD_STATUS_FAILED = -6273, /**< update device apply detail status failed*/
|
||||
ERROR_REG_LORA_DEVICE_FAILED = -6274, /**< register lora device failed*/
|
||||
ERROR_SYN_APPLY_DEVICE_FAILED = -6275, /**< device apply synchronization failed*/
|
||||
ERROR_QUERY_DGR_FAILED = -6276, /**< query device group relation failed*/
|
||||
ERROR_JUDGE_DGR_FAILED = -6277, /**< judge device group relation failed*/
|
||||
ERROR_QUERY_AGR_FAILED = -6278, /**< query apply group relation failed*/
|
||||
ERROR_JUDGE_AGR_FAILED = -6279, /**< judge apply group relation failed*/
|
||||
ERROR_DEVICENAME_NOT_MEET_SPECS = -6280, /**< devicename not meet specs*/
|
||||
ERROR_DELETE_APPLY_DEVICE_FAILED = -6281, /**< delete apply device failed*/
|
||||
ERROR_GEN_DEVICEID_FAILED = -6282, /**< gennerate deviceId failed*/
|
||||
ERROR_APPLY_ILLEGAL = -6283, /**< apply illegal*/
|
||||
ERROR_LORA_DEVICE_METHOD_ERROR = -6284, /**< lora device cannot created by num*/
|
||||
ERROR_APPLY_NOT_READY = -6285, /**< apply not ready*/
|
||||
|
||||
/**
|
||||
* dsl
|
||||
* -6300 ~ -6349
|
||||
*/
|
||||
ERROR_DSL_PARSE_METHOD_NOT_EXIST = -6300, /**< dsl parse: method not exist*/
|
||||
ERROR_DSL_PARSE_PARAMS_FORMAT_ERROR = -6301, /**< dsl parse: params format must be JSONObject/JSONArray*/
|
||||
ERROR_DSL_PARSE_PARAMS_VALUE_EMPTY = -6302, /**< dsl parse: params value empty*/
|
||||
ERROR_DSL_PARSE_PARAMS_NUMBER_ERROR = -6303, /**< dsl parse: params number error*/
|
||||
ERROR_DSL_PARSE_PARAMS_NOT_EXIST = -6304, /**< dsl parse: params not exist*/
|
||||
ERROR_DSL_PARSE_PARAMS_TYPE_ERROR = -6305, /**< dsl parse: params type error*/
|
||||
ERROR_DSL_PARSE_INT_SPECS_ERROR = -6306, /**< dsl parse: int specs error*/
|
||||
ERROR_DSL_PARSE_FLOAT_SPECS_ERROR = -6307, /**< dsl parse: float specs error*/
|
||||
ERROR_DSL_PARSE_BOOL_SPECS_ERROR = -6308, /**< dsl parse: bool specs error*/
|
||||
ERROR_DSL_PARSE_ENUM_SPECS_ERROR = -6309, /**< dsl parse: enum specs error*/
|
||||
ERROR_DSL_PARSE_STRING_SPECS_ERROR = -6310, /**< dsl parse: string specs error*/
|
||||
ERROR_DSL_PARSE_DATE_SPECS_ERROR = -6311, /**< dsl parse: date specs error*/
|
||||
ERROR_DSL_PARSE_STRUCT_SPECS_ERROR = -6312, /**< dsl parse: struct specs error*/
|
||||
ERROR_DSL_SERVICE_NOT_AVAILABLE = -6313, /**< dsl service not available*/
|
||||
ERROR_DSL_PARSE_DATA_TYPE_PARSE_ERROR = -6314, /**< dsl parse: data type parse error*/
|
||||
ERROR_DATA_NOT_SATISFY_DSL = -6315, /**< dsl parse: data not satisfy dsl*/
|
||||
ERROR_DSL_PARSE_SPECS_NUMBER_FORMAT_ERROR = -6316, /**< dsl parse: specs number format error*/
|
||||
ERROR_DSL_PARSE_TEMPLATE_ERROR = -6317, /**< dsl parse: template error*/
|
||||
ERROR_DSL_EXCEPTION = -6318, /**< dsl exception*/
|
||||
ERROR_DSL_PARSE_EVENT_CALL_TYPE_ERROR = -6319, /**< dsl parse: event call type error*/
|
||||
ERROR_DSL_PARSE_NO_PROPERTY = -6320, /**< dsl parse: no property exist in product*/
|
||||
ERROR_DSL_PARSE_IDENTIFIER_IS_NULL = -6321, /**< dsl parse: template property/params idetifier is null*/
|
||||
ERROR_DSL_DEVICE_NOT_EXIST_IN_PRODUCT = -6321, /**< dsl: device not exist in product*/
|
||||
ERROR_DSL_PARSE_DOUBLE_SPECS_ERROR = -6322, /**< dsl parse: double specs error*/
|
||||
|
||||
/**
|
||||
*
|
||||
* -6350 ~ -6399
|
||||
*/
|
||||
ERROR_EVENT_PUT_ERROR = -6350, /**< thing event put error*/
|
||||
ERROR_SERVICE_PUT_ERROR = -6351, /**< thing service put error*/
|
||||
ERROR_DEVICE_GET_EVENT_FAULT = -6352, /**< thing event get error*/
|
||||
ERROR_PRODUCT_KEY_ELEMENT_ALREADY_EXIST = -6353, /**< product key element already exist*/
|
||||
|
||||
/**
|
||||
*
|
||||
* -6400 ~ -6449
|
||||
*/
|
||||
ERROR_TOPO_RELATION_COUNT_EXCEED = -6400, /**< topo relation count exceed*/
|
||||
ERROR_TOPO_RELATION_NOT_EXIST = -6401, /**< topo relation not exist*/
|
||||
ERROR_TOPO_RELATION_CANNOT_ADD_BYSELF = -6402, /**< topo relation cannot add by self*/
|
||||
|
||||
/**
|
||||
* alink
|
||||
* -6450 ~ -6469
|
||||
*/
|
||||
ERROR_ALINK_METHOD_NOT_EXIST = -6450, /**< alink method not exist*/
|
||||
|
||||
/**
|
||||
*
|
||||
* -6550 ~ -6599
|
||||
*/
|
||||
ERROR_DEVICE_GROUP_NOT_FOUND = -6550, /**< device group not found*/
|
||||
|
||||
/**
|
||||
* @brief dev_sign
|
||||
*
|
||||
* -1100 ~ -1200
|
||||
*
|
||||
*/
|
||||
ERROR_DEV_SIGN_CUSTOM_DOMAIN_IS_NULL = -1105,
|
||||
ERROR_DEV_SIGN_SOURCE_TOO_SHORT = -1104,
|
||||
ERROR_DEV_SIGN_PASSWORD_TOO_SHORT = -1103,
|
||||
ERROR_DEV_SIGN_USERNAME_TOO_SHORT = -1102,
|
||||
ERROR_DEV_SIGN_CLIENT_ID_TOO_SHORT = -1101,
|
||||
ERROR_DEV_SIGN_HOST_NAME_TOO_SHORT = -1100,
|
||||
|
||||
ERROR_NO_MEM = -1016,
|
||||
ERROR_CERTIFICATE_EXPIRED = -1015,
|
||||
ERROR_MALLOC = -1014,
|
||||
ERROR_NO_ENOUGH_MEM = -1013, /**< Writes more than size value. */
|
||||
|
||||
ERROR_NO_SUPPORT = -12,
|
||||
ERROR_NO_PERSISTENCE = -11,
|
||||
ERROR_HTTP_BREAK = -10,
|
||||
ERROR_NULL_VALUE = -9,
|
||||
ERROR_HTTP_CONN = -8, /**< Connection failed. */
|
||||
ERROR_HTTP_PARSE = -7, /**< A URL parse error occurred. */
|
||||
ERROR_HTTP_UNRESOLVED_DNS = -6, /**< Could not resolve the hostname. */
|
||||
ERROR_HTTP_PRTCL = -5, /**< A protocol error occurred. */
|
||||
ERROR_HTTP = -4, /**< An unknown error occurred. */
|
||||
ERROR_HTTP_CLOSED = -3, /**< Connection was closed by a remote host. */
|
||||
NULL_VALUE_ERROR = -2,
|
||||
|
||||
FAIL_RETURN = -1, /**< generic error. */
|
||||
SUCCESS_RETURN = 0,
|
||||
|
||||
/* @value > 0, reserved for other usage */
|
||||
|
||||
} iotx_err_t;
|
||||
|
||||
typedef struct _iotx_dev_meta_info
|
||||
{
|
||||
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 module[IOTX_MODULE_LEN + 1];
|
||||
char device_asset[IOTX_DEVICE_UID_LEN + 1];
|
||||
char device_reg_code[IOTX_DEVICE_REG_CODE_LEN + 1];
|
||||
} iotx_dev_meta_info_t;
|
||||
typedef struct
|
||||
{
|
||||
const char *topic;
|
||||
void *callback;
|
||||
} iotx_user_subscribe_context;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *region;
|
||||
uint16_t port;
|
||||
} iotx_region_item_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IOTX_CLOUD_REGION_SHANGHAI, /* Shanghai */
|
||||
IOTX_CLOUD_REGION_SINGAPORE, /* Singapore */
|
||||
IOTX_CLOUD_REGION_JAPAN, /* Japan */
|
||||
IOTX_CLOUD_REGION_USA_WEST, /* America */
|
||||
IOTX_CLOUD_REGION_GERMANY, /* Germany */
|
||||
IOTX_CLOUD_REGION_CUSTOM, /* Custom setting */
|
||||
IOTX_CLOUD_DOMAIN_MAX /* Maximum number of domain */
|
||||
} iotx_mqtt_region_types_t;
|
||||
|
||||
#define IOTX_MQTT_DOMAIN_NUMBER (6)
|
||||
extern const char *g_infra_mqtt_domain[IOTX_MQTT_DOMAIN_NUMBER];
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IOTX_HTTP_REGION_SHANGHAI, /* Shanghai */
|
||||
IOTX_HTTP_REGION_SINGAPORE, /* Singapore */
|
||||
IOTX_HTTP_REGION_JAPAN, /* Japan */
|
||||
IOTX_HTTP_REGION_AMERICA, /* America */
|
||||
IOTX_HTTP_REGION_GERMANY, /* Germany */
|
||||
IOTX_HTTP_REGION_CUSTOM, /* Custom setting */
|
||||
IOTX_HTTP_REGION_MAX /* Maximum number of domain */
|
||||
} iotx_http_region_types_t;
|
||||
|
||||
/**
|
||||
* @brief contains iot_ioctl context
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
char hostname[DEV_SIGN_HOSTNAME_MAXLEN];
|
||||
uint16_t port;
|
||||
char clientid[DEV_SIGN_CLIENT_ID_MAXLEN];
|
||||
char username[DEV_SIGN_USERNAME_MAXLEN];
|
||||
char password[DEV_SIGN_PASSWORD_MAXLEN];
|
||||
} iotx_sign_mqtt_t;
|
||||
|
||||
#define IOTX_HTTP_DOMAIN_NUMBER (6)
|
||||
extern const char *g_infra_http_domain[IOTX_HTTP_DOMAIN_NUMBER];
|
||||
|
||||
extern int iotx_facility_json_print(const char *str, int level, ...);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
+1001
File diff suppressed because it is too large
Load Diff
+139
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef _INFRA_HTTPC_H_
|
||||
#define _INFRA_HTTPC_H_
|
||||
|
||||
#include "infra_net.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup HttpClient
|
||||
* @{
|
||||
* HttpClient API implements the client-side of HTTP/1.1. It provides base interfaces to execute an HTTP request on a given URL. It also supports HTTPS (HTTP over TLS) to provide secure communication.\n
|
||||
* @section HttpClient_Usage_Chapter How to use this module
|
||||
* In this release, MediaTek provides two types of APIs: high level APIs and low level APIs.\n
|
||||
* - \b The \b high \b level \b APIs
|
||||
* - Enables to execute a single HTTP request on a given URL.
|
||||
* - Call #httpclient_get(), #httpclient_post(), #httpclient_put() or #httpclient_delete() to get, post, put or delete and HTTP request.\n
|
||||
* - \b The \b low \b level \b APIs
|
||||
* - Enables to execute more than one HTTP requests during a Keep-Alive connection. Keep-alive is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair.
|
||||
* - Step1: Call #httpclient_connect() to connect to a remote server.
|
||||
* - Step2: Call #httpclient_send_request() to send an HTTP request to the server.
|
||||
* - Step3: Call #httpclient_recv_response() to receive an HTTP response from the server.
|
||||
* - Step4: Repeat Steps 2 and 3 to execute more requests.
|
||||
* - Step5: Call #httpclient_close() to close the connection.
|
||||
* - Sample code: Please refer to the example under <sdk_root>/project/mt7687_hdk/apps/http_client/http_client_keepalive folder.
|
||||
*/
|
||||
|
||||
/** @defgroup httpclient_define Define
|
||||
* @{
|
||||
*/
|
||||
/** @brief This macro defines the HTTP port. */
|
||||
#define HTTP_PORT 80
|
||||
|
||||
/** @brief This macro defines the HTTPS port. */
|
||||
#define HTTPS_PORT 443
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup httpclient_enum Enum
|
||||
* @{
|
||||
*/
|
||||
/** @brief This enumeration defines the HTTP request type. */
|
||||
typedef enum {
|
||||
HTTPCLIENT_GET,
|
||||
HTTPCLIENT_POST,
|
||||
HTTPCLIENT_PUT,
|
||||
HTTPCLIENT_DELETE,
|
||||
HTTPCLIENT_HEAD
|
||||
} HTTPCLIENT_REQUEST_TYPE;
|
||||
|
||||
|
||||
/** @defgroup httpclient_struct Struct
|
||||
* @{
|
||||
*/
|
||||
/** @brief This structure defines the httpclient_t structure. */
|
||||
typedef struct {
|
||||
int remote_port; /**< HTTP or HTTPS port. */
|
||||
utils_network_t net;
|
||||
int response_code; /**< Response code. */
|
||||
char *header; /**< Custom header. */
|
||||
char *auth_user; /**< Username for basic authentication. */
|
||||
char *auth_password; /**< Password for basic authentication. */
|
||||
} httpclient_t;
|
||||
|
||||
/** @brief This structure defines the HTTP data structure. */
|
||||
typedef struct {
|
||||
int is_more; /**< Indicates if more data needs to be retrieved. */
|
||||
int is_chunked; /**< Response data is encoded in portions/chunks.*/
|
||||
int retrieve_len; /**< Content length to be retrieved. */
|
||||
int response_content_len; /**< Response content length. */
|
||||
int response_received_len; /**< Response have received length. */
|
||||
int post_buf_len; /**< Post data length. */
|
||||
int response_buf_len; /**< Response buffer length. */
|
||||
char *post_content_type; /**< Content type of the post data. */
|
||||
char *post_buf; /**< User data to be posted. */
|
||||
char *response_buf; /**< Buffer to store the response data. */
|
||||
} httpclient_data_t;
|
||||
|
||||
int iotx_post(httpclient_t *client,
|
||||
const char *url,
|
||||
int port,
|
||||
const char *ca_crt,
|
||||
httpclient_data_t *client_data);
|
||||
|
||||
int httpclient_recv_response(httpclient_t *client, uint32_t timeout_ms, httpclient_data_t *client_data);
|
||||
|
||||
int httpclient_common(httpclient_t *client, const char *url, int port, const char *ca_crt,
|
||||
HTTPCLIENT_REQUEST_TYPE method, uint32_t timeout_ms, httpclient_data_t *client_data);
|
||||
|
||||
void httpclient_close(httpclient_t *client);
|
||||
|
||||
|
||||
/**********************************************************************************/
|
||||
|
||||
typedef enum {
|
||||
IOTX_HTTP_GET,
|
||||
IOTX_HTTP_POST,
|
||||
IOTX_HTTP_PUT,
|
||||
IOTX_HTTP_DELETE
|
||||
} iotx_http_method_t;
|
||||
|
||||
typedef enum {
|
||||
IOTX_HTTPOPT_URL,
|
||||
IOTX_HTTPOPT_PORT,
|
||||
IOTX_HTTPOPT_METHOD,
|
||||
IOTX_HTTPOPT_HEADER,
|
||||
IOTX_HTTPOPT_CERT,
|
||||
IOTX_HTTPOPT_TIMEOUT,
|
||||
IOTX_HTTPOPT_RECVCALLBACK,
|
||||
IOTX_HTTPOPT_RECVMAXLEN,
|
||||
IOTX_HTTPOPT_RECVCONTEXT
|
||||
} iotx_http_option_t;
|
||||
|
||||
typedef int (*recvcallback)(char *ptr, int length, int total_length, void *userdata);
|
||||
|
||||
void *wrapper_http_init(void);
|
||||
int wrapper_http_setopt(void *handle, iotx_http_option_t option, void *data);
|
||||
int wrapper_http_perform(void *handle, void *data, int length);
|
||||
void wrapper_http_deinit(void **handle);
|
||||
/**********************************************************************************/
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif /* __HTTPCLIENT_H__ */
|
||||
|
||||
|
||||
|
||||
+842
@@ -0,0 +1,842 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_JSON_PARSER
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "infra_types.h"
|
||||
#include "infra_json_parser.h"
|
||||
#include "wrappers.h"
|
||||
|
||||
#ifdef INFRA_LOG
|
||||
#include "infra_log.h"
|
||||
#define jparser_debug(...) log_debug("jparser", __VA_ARGS__)
|
||||
#else
|
||||
#define jparser_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#endif
|
||||
|
||||
#ifdef INFRA_MEM_STATS
|
||||
#include "infra_mem_stats.h"
|
||||
#define jparser_malloc(size) LITE_malloc(size, MEM_MAGIC, "jparser")
|
||||
#define jparser_free(ptr) LITE_free(ptr)
|
||||
#else
|
||||
#define jparser_malloc(size) HAL_Malloc(size)
|
||||
#define jparser_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
|
||||
#endif
|
||||
|
||||
char *json_get_object(int type, char *str, char *str_end)
|
||||
{
|
||||
char *pos = NULL;
|
||||
char ch = (type == JOBJECT) ? '{' : '[';
|
||||
|
||||
if (!str || !str_end) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (str != NULL && *str != 0 && str < str_end) {
|
||||
if (*str == ' ') {
|
||||
str++;
|
||||
continue;
|
||||
}
|
||||
pos = (*str == ch) ? str : NULL;
|
||||
break;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
char *json_get_next_object(int type, char *str, char *str_end, char **key, int *key_len,
|
||||
char **val, int *val_len, int *val_type)
|
||||
{
|
||||
char JsonMark[JTYPEMAX][2] = { { '\"', '\"' }, { '{', '}' }, { '[', ']' }, { '0', ' ' } };
|
||||
int iMarkDepth = 0, iValueType = JNONE, iNameLen = 0, iValueLen = 0, iStringDepth = 0;
|
||||
char *p_cName = 0, *p_cValue = 0, *p_cPos = str;
|
||||
|
||||
if (type == JOBJECT) {
|
||||
/* Get Key */
|
||||
p_cPos = strchr(p_cPos, '\"');
|
||||
if (!p_cPos) {
|
||||
goto do_exit;
|
||||
}
|
||||
p_cName = ++p_cPos;
|
||||
p_cPos = strchr(p_cPos, '\"');
|
||||
if (!p_cPos) {
|
||||
goto do_exit;
|
||||
}
|
||||
iNameLen = p_cPos - p_cName;
|
||||
|
||||
/* Get Value */
|
||||
p_cPos = strchr(p_cPos, ':');
|
||||
}
|
||||
while (p_cPos && *p_cPos && p_cPos < str_end) {
|
||||
if (*p_cPos == '\"') {
|
||||
iValueType = JSTRING;
|
||||
p_cValue = ++p_cPos;
|
||||
break;
|
||||
} else if (*p_cPos == '{') {
|
||||
iValueType = JOBJECT;
|
||||
p_cValue = p_cPos++;
|
||||
break;
|
||||
} else if (*p_cPos == '[') {
|
||||
iValueType = JARRAY;
|
||||
p_cValue = p_cPos++;
|
||||
break;
|
||||
} else if ((*p_cPos == '-') || (*p_cPos >= '0' && *p_cPos <= '9')) {
|
||||
iValueType = JNUMBER;
|
||||
p_cValue = p_cPos++;
|
||||
break;
|
||||
} else if (*p_cPos == 't' || *p_cPos == 'T' || *p_cPos == 'f' || *p_cPos == 'F') {
|
||||
iValueType = JBOOLEAN;
|
||||
p_cValue = p_cPos;
|
||||
break;
|
||||
}
|
||||
p_cPos++;
|
||||
}
|
||||
|
||||
while (p_cPos && *p_cPos && p_cPos < str_end && iValueType > JNONE) {
|
||||
if (iValueType == JBOOLEAN) {
|
||||
int len = strlen(p_cValue);
|
||||
|
||||
if ((*p_cValue == 't' || *p_cValue == 'T') && len >= 4
|
||||
&& (!strncmp(p_cValue, "true", 4)
|
||||
|| !strncmp(p_cValue, "TRUE", 4))) {
|
||||
iValueLen = 4;
|
||||
p_cPos = p_cValue + iValueLen;
|
||||
break;
|
||||
} else if ((*p_cValue == 'f' || *p_cValue == 'F') && len >= 5
|
||||
&& (!strncmp(p_cValue, "false", 5)
|
||||
|| !strncmp(p_cValue, "FALSE", 5))) {
|
||||
iValueLen = 5;
|
||||
p_cPos = p_cValue + iValueLen;
|
||||
break;
|
||||
}
|
||||
} else if (iValueType == JNUMBER) {
|
||||
if ((*p_cPos < '0' || *p_cPos > '9') && (*p_cPos != '.') && (*p_cPos != '+') \
|
||||
&& (*p_cPos != '-') && ((*p_cPos != 'e')) && (*p_cPos != 'E')) {
|
||||
iValueLen = p_cPos - p_cValue;
|
||||
break;
|
||||
}
|
||||
} else if (iValueType == JSTRING) {
|
||||
if (*p_cPos == '\"') {
|
||||
iValueLen = p_cPos - p_cValue;
|
||||
break;
|
||||
}
|
||||
} else if (*p_cPos == JsonMark[iValueType][1]) {
|
||||
if (iStringDepth == 0) {
|
||||
if (iMarkDepth == 0) {
|
||||
iValueLen = p_cPos - p_cValue + 1;
|
||||
p_cPos++;
|
||||
break;
|
||||
} else {
|
||||
iMarkDepth--;
|
||||
}
|
||||
}
|
||||
} else if (*p_cPos == JsonMark[iValueType][0]) {
|
||||
if (iStringDepth == 0) {
|
||||
iMarkDepth++;
|
||||
}
|
||||
} else if (*p_cPos == '\"') {
|
||||
if (iStringDepth) {
|
||||
iStringDepth = 0;
|
||||
} else {
|
||||
iStringDepth = 1;
|
||||
}
|
||||
}
|
||||
p_cPos++;
|
||||
}
|
||||
|
||||
if (type == JOBJECT) {
|
||||
if ((p_cName + iNameLen) > str_end) {
|
||||
goto do_exit;
|
||||
}
|
||||
*key = p_cName;
|
||||
*key_len = iNameLen;
|
||||
}
|
||||
if ((p_cValue + iValueLen) > str_end) {
|
||||
goto do_exit;
|
||||
}
|
||||
|
||||
*val = p_cValue;
|
||||
*val_len = iValueLen;
|
||||
*val_type = iValueType;
|
||||
if (iValueType == JSTRING) {
|
||||
return p_cValue + iValueLen + 1;
|
||||
} else {
|
||||
return p_cValue + iValueLen;
|
||||
}
|
||||
|
||||
do_exit:
|
||||
*val = NULL;
|
||||
*val_len = 0;
|
||||
*key = NULL;
|
||||
*key_len = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int json_parse_name_value(char *p_cJsonStr, int iStrLen, json_parse_cb pfnCB, void *p_CBData)
|
||||
{
|
||||
char *pos = 0, *key = 0, *val = 0;
|
||||
int klen = 0, vlen = 0, vtype = 0;
|
||||
int ret = JSON_RESULT_ERR;
|
||||
|
||||
if (p_cJsonStr == NULL || iStrLen == 0 || pfnCB == NULL) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
json_object_for_each_kv(p_cJsonStr, iStrLen, pos, key, klen, val, vlen, vtype) {
|
||||
if (key && klen && val && vlen) {
|
||||
ret = JSON_RESULT_OK;
|
||||
if (JSON_PARSE_FINISH == pfnCB(key, klen, val, vlen, vtype, p_CBData)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int json_get_value_by_name_cb(char *p_cName, int iNameLen, char *p_cValue, int iValueLen, int iValueType,
|
||||
void *p_CBData)
|
||||
{
|
||||
JSON_NV *p_stNameValue = (JSON_NV *)p_CBData;
|
||||
|
||||
#ifdef JSON_DEBUG
|
||||
int i;
|
||||
|
||||
if (p_cName) {
|
||||
jparser_debug("Name:");
|
||||
for (i = 0; i < iNameLen; i++) {
|
||||
jparser_debug("%c", *(p_cName + i));
|
||||
}
|
||||
}
|
||||
|
||||
if (p_cValue) {
|
||||
jparser_debug("Value:");
|
||||
for (i = 0; i < iValueLen; i++) {
|
||||
jparser_debug("%c", *(p_cValue + i));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((iNameLen == p_stNameValue->nLen) && !strncmp(p_cName, p_stNameValue->pN, p_stNameValue->nLen)) {
|
||||
p_stNameValue->pV = p_cValue;
|
||||
p_stNameValue->vLen = iValueLen;
|
||||
p_stNameValue->vType = iValueType;
|
||||
return JSON_PARSE_FINISH;
|
||||
} else {
|
||||
return JSON_PARSE_OK;
|
||||
}
|
||||
}
|
||||
|
||||
char *json_get_value_by_name(char *p_cJsonStr, int iStrLen, char *p_cName, int *p_iValueLen, int *p_iValueType)
|
||||
{
|
||||
JSON_NV stNV;
|
||||
|
||||
memset(&stNV, 0, sizeof(stNV));
|
||||
stNV.pN = p_cName;
|
||||
stNV.nLen = strlen(p_cName);
|
||||
if (JSON_RESULT_OK == json_parse_name_value(p_cJsonStr, iStrLen, json_get_value_by_name_cb, (void *)&stNV)) {
|
||||
if (p_iValueLen) {
|
||||
*p_iValueLen = stNV.vLen;
|
||||
}
|
||||
if (p_iValueType) {
|
||||
*p_iValueType = stNV.vType;
|
||||
}
|
||||
}
|
||||
return stNV.pV;
|
||||
}
|
||||
|
||||
char *json_get_value_by_name_len(char *p_cJsonStr, int iStrLen, char *p_cName, int p_cNameLen, int *p_iValueLen,
|
||||
int *p_iValueType)
|
||||
{
|
||||
JSON_NV stNV;
|
||||
|
||||
memset(&stNV, 0, sizeof(stNV));
|
||||
stNV.pN = p_cName;
|
||||
stNV.nLen = p_cNameLen;
|
||||
if (JSON_RESULT_OK == json_parse_name_value(p_cJsonStr, iStrLen, json_get_value_by_name_cb, (void *)&stNV)) {
|
||||
if (p_iValueLen) {
|
||||
*p_iValueLen = stNV.vLen;
|
||||
}
|
||||
if (p_iValueType) {
|
||||
*p_iValueType = stNV.vType;
|
||||
}
|
||||
}
|
||||
return stNV.pV;
|
||||
}
|
||||
|
||||
char *LITE_json_value_of(char *key, char *src, ...)
|
||||
{
|
||||
char *value = NULL;
|
||||
char *ret = NULL;
|
||||
char *delim = NULL;
|
||||
char *key_iter;
|
||||
char *key_next;
|
||||
char *src_iter;
|
||||
|
||||
int key_len;
|
||||
int value_len = -1;
|
||||
int src_iter_len;
|
||||
|
||||
if (NULL == key || NULL == src) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WITH_MEM_STATS_PER_MODULE
|
||||
{
|
||||
char *module_name = NULL;
|
||||
int magic = 0;
|
||||
va_list ap;
|
||||
va_start(ap, src);
|
||||
magic = va_arg(ap, int);
|
||||
if (MEM_MAGIC == magic) {
|
||||
module_name = va_arg(ap, char *);
|
||||
}
|
||||
va_end(ap);
|
||||
}
|
||||
#endif
|
||||
|
||||
src_iter = src;
|
||||
src_iter_len = strlen(src_iter);
|
||||
key_iter = key;
|
||||
|
||||
do {
|
||||
if ((delim = strchr(key_iter, '.')) != NULL) {
|
||||
key_len = delim - key_iter;
|
||||
key_next = key_iter;
|
||||
|
||||
value = json_get_value_by_name_len(src_iter, src_iter_len, key_next, key_len, &value_len, 0);
|
||||
if (value == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
src_iter = value;
|
||||
src_iter_len = value_len;
|
||||
key_iter = delim + 1;
|
||||
}
|
||||
} while (delim);
|
||||
|
||||
key_len = strlen(key_iter);
|
||||
key_next = key_iter;
|
||||
value = json_get_value_by_name_len(src_iter, src_iter_len, key_next, key_len, &value_len, 0);
|
||||
if (NULL == value) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = jparser_malloc((value_len + 1) * sizeof(char));
|
||||
|
||||
if (NULL == ret) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HAL_Snprintf(ret, value_len + 1, "%s", value);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if WITH_JSON_KEYS_OF
|
||||
static list_head_t *_LITE_json_keys_of(char *src, int src_len, char *prefix, ...)
|
||||
{
|
||||
static LIST_HEAD(keylist);
|
||||
char *module_name = NULL;
|
||||
char *iter_pre = NULL;
|
||||
char *pos = 0, *key = 0, *val = 0;
|
||||
int klen = 0, vlen = 0, vtype = 0;
|
||||
int magic = 0;
|
||||
unsigned int mlen = 0;
|
||||
|
||||
#if WITH_MEM_STATS_PER_MODULE
|
||||
va_list ap;
|
||||
va_start(ap, prefix);
|
||||
magic = va_arg(ap, int);
|
||||
if (MEM_MAGIC == magic) {
|
||||
module_name = va_arg(ap, char *);
|
||||
}
|
||||
va_end(ap);
|
||||
#endif
|
||||
|
||||
if (!strcmp("", prefix)) {
|
||||
INIT_LIST_HEAD(&keylist);
|
||||
}
|
||||
|
||||
json_object_for_each_kv(src, src_len, pos, key, klen, val, vlen, vtype) {
|
||||
if (key && klen && val && vlen) {
|
||||
|
||||
json_key_t *entry = NULL;
|
||||
|
||||
entry = jparser_malloc(sizeof(json_key_t), magic, module_name);
|
||||
if (NULL == entry) {
|
||||
utils_err("jparser_malloc failed!");
|
||||
return NULL;
|
||||
}
|
||||
memset(entry, 0, sizeof(json_key_t));
|
||||
|
||||
mlen = strlen(prefix) + klen + 1;
|
||||
if (module_name) {
|
||||
entry->key = LITE_format_nstring(mlen, "%s%.*s", magic, module_name, prefix, klen, key);
|
||||
} else {
|
||||
entry->key = LITE_format_nstring(mlen, "%s%.*s", prefix, klen, key);
|
||||
}
|
||||
if (NULL == entry->key) {
|
||||
jparser_free(entry);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_add_tail(&entry->list, &keylist);
|
||||
|
||||
if (JOBJECT == vtype) {
|
||||
mlen = strlen(prefix) + klen + 2;
|
||||
if (module_name) {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s%.*s.", magic, module_name, prefix, klen, key);
|
||||
} else {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s%.*s.", prefix, klen, key);
|
||||
}
|
||||
if (NULL == iter_pre) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_LITE_json_keys_of(val, vlen, iter_pre, magic, module_name);
|
||||
jparser_free(iter_pre);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp("", prefix)) {
|
||||
json_key_t *entry = NULL;
|
||||
|
||||
entry = jparser_malloc(sizeof(json_key_t), magic, module_name);
|
||||
if (NULL == entry) {
|
||||
utils_err("jparser_malloc failed!");
|
||||
return NULL;
|
||||
}
|
||||
memset(entry, 0, sizeof(json_key_t));
|
||||
list_add_tail(&entry->list, &keylist);
|
||||
|
||||
return &keylist;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_head_t *LITE_json_keys_of(char *src, char *prefix, ...)
|
||||
{
|
||||
char *module_name = NULL;
|
||||
int magic = 0;
|
||||
|
||||
if (!src || !prefix) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WITH_MEM_STATS_PER_MODULE
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, prefix);
|
||||
magic = va_arg(ap, int);
|
||||
if (MEM_MAGIC == magic) {
|
||||
module_name = va_arg(ap, char *);
|
||||
}
|
||||
va_end(ap);
|
||||
#endif
|
||||
|
||||
return _LITE_json_keys_of(src, strlen(src), prefix, magic, module_name);
|
||||
|
||||
}
|
||||
|
||||
#if WITH_JSON_TOKEN_EXT
|
||||
static list_head_t *_LITE_json_keys_of_ext(int type, char *src, int src_len, char *prefix, ...)
|
||||
{
|
||||
static LIST_HEAD(keylist);
|
||||
char *module_name = NULL;
|
||||
char *iter_pre = NULL;
|
||||
char *pos = 0, *key = 0, *val = 0;
|
||||
int klen = 0, vlen = 0, vtype = 0;
|
||||
int magic = 0;
|
||||
unsigned int count = 1;
|
||||
unsigned int mlen = 0;
|
||||
|
||||
if (src == NULL || prefix == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WITH_MEM_STATS_PER_MODULE
|
||||
va_list ap;
|
||||
va_start(ap, prefix);
|
||||
magic = va_arg(ap, int);
|
||||
if (MEM_MAGIC == magic) {
|
||||
module_name = va_arg(ap, char *);
|
||||
}
|
||||
va_end(ap);
|
||||
#endif
|
||||
|
||||
if (!strcmp("", prefix)) {
|
||||
INIT_LIST_HEAD(&keylist);
|
||||
}
|
||||
|
||||
if (JOBJECT == type) {
|
||||
json_object_for_each_kv(src, src_len, pos, key, klen, val, vlen, vtype) {
|
||||
if (key && klen && val && vlen) {
|
||||
|
||||
json_key_t *entry = NULL;
|
||||
|
||||
entry = jparser_malloc(sizeof(json_key_t), magic, module_name);
|
||||
|
||||
if (NULL == entry) {
|
||||
utils_err("jparser_malloc failed!");
|
||||
return NULL;
|
||||
}
|
||||
memset(entry, 0, sizeof(json_key_t));
|
||||
|
||||
mlen = strlen(prefix) + klen + 1;
|
||||
if (module_name) {
|
||||
entry->key = LITE_format_nstring(mlen, "%s%.*s", magic, module_name, prefix, klen, key);
|
||||
} else {
|
||||
entry->key = LITE_format_nstring(mlen, "%s%.*s", prefix, klen, key);
|
||||
}
|
||||
|
||||
if (NULL == entry->key) {
|
||||
jparser_free(entry);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_add_tail(&entry->list, &keylist);
|
||||
|
||||
if (JOBJECT == vtype) {
|
||||
mlen = strlen(prefix) + klen + 2;
|
||||
if (module_name) {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s%.*s.", magic, module_name, prefix, klen, key);
|
||||
} else {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s%.*s.", prefix, klen, key);
|
||||
}
|
||||
if (NULL == iter_pre) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_LITE_json_keys_of_ext(vtype, val, vlen, iter_pre, magic, module_name);
|
||||
jparser_free(iter_pre);
|
||||
} else if (JARRAY == vtype) {
|
||||
mlen = strlen(prefix) + klen + 1;
|
||||
if (module_name) {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s%.*s", magic, module_name, prefix, klen, key);
|
||||
} else {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s%.*s", prefix, klen, key);
|
||||
}
|
||||
if (NULL == iter_pre) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_LITE_json_keys_of_ext(vtype, val, vlen, iter_pre, magic, module_name);
|
||||
jparser_free(iter_pre);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (JARRAY == type) {
|
||||
json_array_for_each_entry(src, src_len, pos, val, vlen, vtype) {
|
||||
if (val && vlen) {
|
||||
|
||||
json_key_t *entry = NULL;
|
||||
unsigned int tmp = 0;
|
||||
unsigned int arridxlen = 0;
|
||||
entry = jparser_malloc(sizeof(json_key_t), magic, module_name);
|
||||
if (NULL == entry) {
|
||||
utils_err("jparser_malloc failed!");
|
||||
return NULL;
|
||||
}
|
||||
memset(entry, 0, sizeof(json_key_t));
|
||||
|
||||
tmp = count;
|
||||
do {
|
||||
tmp /= 10;
|
||||
++arridxlen;
|
||||
} while (tmp);
|
||||
mlen = strlen("%s[%d]") + strlen(prefix) + arridxlen;
|
||||
if (module_name) {
|
||||
entry->key = LITE_format_nstring(mlen, "%s[%d]", magic, module_name, prefix, count);
|
||||
} else {
|
||||
entry->key = LITE_format_nstring(mlen, "%s[%d]", prefix, count);
|
||||
}
|
||||
if (NULL == entry->key) {
|
||||
jparser_free(entry);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_add_tail(&entry->list, &keylist);
|
||||
|
||||
if (JOBJECT == vtype) {
|
||||
mlen = strlen("%s[%d].") + strlen(prefix) + arridxlen;
|
||||
if (module_name) {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s[%d].", magic, module_name, prefix, count);
|
||||
} else {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s[%d].", prefix, count);
|
||||
}
|
||||
if (NULL == iter_pre) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_LITE_json_keys_of_ext(vtype, val, vlen, iter_pre, magic, module_name);
|
||||
jparser_free(iter_pre);
|
||||
} else if (JARRAY == vtype) {
|
||||
mlen = strlen("%s[%d]") + strlen(prefix) + arridxlen;
|
||||
if (module_name) {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s[%d]", magic, module_name, prefix, count);
|
||||
} else {
|
||||
iter_pre = LITE_format_nstring(mlen, "%s[%d]", prefix, count);
|
||||
}
|
||||
if (NULL == iter_pre) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_LITE_json_keys_of_ext(vtype, val, vlen, iter_pre, magic, module_name);
|
||||
jparser_free(iter_pre);
|
||||
}
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp("", prefix)) {
|
||||
json_key_t *entry = NULL;
|
||||
|
||||
entry = jparser_malloc(sizeof(json_key_t), magic, module_name);
|
||||
if (NULL == entry) {
|
||||
utils_err("jparser_malloc failed!");
|
||||
return NULL;
|
||||
}
|
||||
memset(entry, 0, sizeof(json_key_t));
|
||||
list_add_tail(&entry->list, &keylist);
|
||||
|
||||
return &keylist;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
int contain_arr(const char *src, int src_len, const char **arr_pre)
|
||||
{
|
||||
int i = 0;
|
||||
int ret = -1;
|
||||
int deep = 0;
|
||||
const char *pre = NULL;
|
||||
|
||||
if (NULL == src || NULL == arr_pre || src_len <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*arr_pre = NULL;
|
||||
for (i = 0; i < src_len; ++i) {
|
||||
switch (src[i]) {
|
||||
case '[': {
|
||||
if (deep != 0) {
|
||||
return ret;
|
||||
}
|
||||
++deep;
|
||||
if (!pre) {
|
||||
pre = &src[i];
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ']': {
|
||||
if (deep != 1) {
|
||||
return ret;
|
||||
}
|
||||
--deep;
|
||||
if ('[' == src[i - 1]) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: {
|
||||
if ((pre != NULL) && (0 == deep)) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((NULL != pre) && (pre < src + src_len) && (pre >= src)) {
|
||||
*arr_pre = pre;
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static char *_json_value_by_arrname(char *src, int src_len, const char *key, int key_len, int *val_len)
|
||||
{
|
||||
char *pos = src;
|
||||
char *entry = NULL;
|
||||
const char *p = NULL;
|
||||
const char *arr_pre = key;
|
||||
const char *arr_suf = NULL;
|
||||
int vtype;
|
||||
int loop;
|
||||
int loop_tmp = 0;
|
||||
do {
|
||||
loop = 0;
|
||||
|
||||
arr_pre = strchr(arr_pre, '[');
|
||||
if (arr_pre && (arr_pre < key + key_len)) {
|
||||
arr_suf = strchr(arr_pre, ']');
|
||||
}
|
||||
if (arr_pre && arr_suf && (arr_suf < key + key_len)) {
|
||||
loop_tmp = 0;
|
||||
for (p = arr_pre + 1; p < arr_suf; ++p) {
|
||||
if (*p > '9' || *p < '0') {
|
||||
return NULL;
|
||||
}
|
||||
loop_tmp *= 10;
|
||||
loop_tmp += *p - '0';
|
||||
|
||||
}
|
||||
|
||||
pos = json_get_object(JARRAY, pos, src + src_len);
|
||||
if (pos != 0 && *pos != 0) {
|
||||
if (*pos == '[' && *(pos + 1) == ']') {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
json_array_for_each_entry(src, src_len, pos, entry, *val_len, vtype) {
|
||||
if (entry && *val_len) {
|
||||
if (++loop >= loop_tmp) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (loop != loop_tmp) {
|
||||
return NULL;
|
||||
}
|
||||
src = entry;
|
||||
arr_pre = arr_suf + 1;
|
||||
}
|
||||
} while (arr_pre && arr_suf && (arr_pre < key + key_len));
|
||||
return entry;
|
||||
}
|
||||
|
||||
void LITE_json_keys_release(list_head_t *keylist)
|
||||
{
|
||||
json_key_t *pos, *tmp;
|
||||
|
||||
if (NULL == keylist) {
|
||||
return;
|
||||
}
|
||||
|
||||
list_for_each_entry_safe(pos, tmp, keylist, list, json_key_t) {
|
||||
if (pos->key) {
|
||||
jparser_free(pos->key);
|
||||
}
|
||||
list_del(&pos->list);
|
||||
jparser_free(pos);
|
||||
}
|
||||
}
|
||||
|
||||
list_head_t *LITE_json_keys_of_ext(char *src, char *prefix, ...)
|
||||
{
|
||||
char *module_name = NULL;
|
||||
int magic = 0;
|
||||
|
||||
#if WITH_MEM_STATS_PER_MODULE
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, prefix);
|
||||
magic = va_arg(ap, int);
|
||||
if (MEM_MAGIC == magic) {
|
||||
module_name = va_arg(ap, char *);
|
||||
}
|
||||
va_end(ap);
|
||||
#endif
|
||||
|
||||
if (!src || !prefix) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return _LITE_json_keys_of_ext(JOBJECT, src, strlen(src), prefix, magic, module_name);
|
||||
}
|
||||
|
||||
static char *_LITE_json_value_of_ext(char *key, char *src, int src_len, int *val_len)
|
||||
{
|
||||
char *value = NULL;
|
||||
char *delim = NULL;
|
||||
char *key_iter;
|
||||
char *key_next;
|
||||
char *src_iter;
|
||||
const char *arr_pre = NULL;
|
||||
|
||||
int value_len;
|
||||
int key_len;
|
||||
int obj_key_len = 0;
|
||||
int key_type;
|
||||
int src_iter_len;
|
||||
|
||||
src_iter = src;
|
||||
src_iter_len = src_len;
|
||||
key_iter = key;
|
||||
value_len = src_iter_len;
|
||||
do {
|
||||
if ((delim = strchr(key_iter, '.')) != NULL) {
|
||||
key_len = delim - key_iter;
|
||||
key_type = JOBJECT;
|
||||
key_next = key_iter;
|
||||
if (0 == contain_arr(key_next, key_len, &arr_pre)) {
|
||||
key_type = JARRAY;
|
||||
obj_key_len = arr_pre - key_iter;
|
||||
if (obj_key_len) {
|
||||
value = json_get_value_by_name_len(src_iter, src_iter_len, key_next, obj_key_len, &value_len, 0);
|
||||
} else {
|
||||
value = src_iter;
|
||||
}
|
||||
} else {
|
||||
value = json_get_value_by_name_len(src_iter, src_iter_len, key_next, key_len, &value_len, 0);
|
||||
}
|
||||
|
||||
if (NULL == value) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (key_type == JARRAY) {
|
||||
if (NULL == (value = _json_value_by_arrname(value, value_len, arr_pre, key_len - obj_key_len, &value_len))) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
src_iter = value;
|
||||
src_iter_len = value_len;
|
||||
key_iter = delim + 1;
|
||||
|
||||
}
|
||||
} while (delim);
|
||||
|
||||
key_len = strlen(key_iter);
|
||||
key_next = key_iter;
|
||||
key_type = JOBJECT;
|
||||
if (0 == contain_arr(key_next, key_len, &arr_pre)) {
|
||||
key_type = JARRAY;
|
||||
obj_key_len = arr_pre - key_iter;
|
||||
if (obj_key_len) {
|
||||
value = json_get_value_by_name_len(src_iter, src_iter_len, key_next, obj_key_len, &value_len, 0);
|
||||
} else {
|
||||
value = src_iter;
|
||||
}
|
||||
} else {
|
||||
value = json_get_value_by_name_len(src_iter, src_iter_len, key_next, key_len, &value_len, 0);
|
||||
}
|
||||
|
||||
if (NULL == value) {
|
||||
return NULL;
|
||||
}
|
||||
if (key_type == JARRAY) {
|
||||
if (NULL == (value = _json_value_by_arrname(value, value_len, arr_pre, key_len - obj_key_len, &value_len))) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
*val_len = value_len;
|
||||
return value;
|
||||
}
|
||||
|
||||
#endif /* #if WITH_JSON_TOKEN_EXT */
|
||||
#endif /* #if WITH_JSON_KEYS_OF */
|
||||
#endif
|
||||
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef _INFRA_JSON_PARSER_H_
|
||||
#define _INFRA_JSON_PARSER_H_
|
||||
|
||||
/* #include "iotx_utils_internal.h" */
|
||||
|
||||
typedef struct JSON_NV {
|
||||
int nLen;
|
||||
int vLen;
|
||||
int vType;
|
||||
char *pN;
|
||||
char *pV;
|
||||
} JSON_NV;
|
||||
|
||||
/**
|
||||
The descriptions of the json value node type
|
||||
**/
|
||||
enum JSONTYPE {
|
||||
JNONE = -1,
|
||||
JSTRING = 0,
|
||||
JOBJECT,
|
||||
JARRAY,
|
||||
JNUMBER,
|
||||
JBOOLEAN,
|
||||
JTYPEMAX
|
||||
};
|
||||
|
||||
/**
|
||||
The error codes produced by the JSON parsers
|
||||
**/
|
||||
enum JSON_PARSE_CODE {
|
||||
JSON_PARSE_ERR,
|
||||
JSON_PARSE_OK,
|
||||
JSON_PARSE_FINISH
|
||||
};
|
||||
|
||||
/**
|
||||
The return codes produced by the JSON parsers
|
||||
**/
|
||||
enum JSON_PARSE_RESULT {
|
||||
JSON_RESULT_ERR = -1,
|
||||
JSON_RESULT_OK
|
||||
};
|
||||
|
||||
typedef int (*json_parse_cb)(char *p_cName, int iNameLen, char *p_cValue, int iValueLen, int iValueType,
|
||||
void *p_Result);
|
||||
|
||||
/**
|
||||
* @brief Parse the JSON string, and iterate through all keys and values,
|
||||
* then handle the keys and values by callback function.
|
||||
*
|
||||
* @param[in] p_cJsonStr @n The JSON string
|
||||
* @param[in] iStrLen @n The JSON string length
|
||||
* @param[in] pfnCB @n Callback function
|
||||
* @param[out] p_CBData @n User data
|
||||
* @return JSON_RESULT_OK success, JSON_RESULT_ERR failed
|
||||
* @see None.
|
||||
* @note None.
|
||||
**/
|
||||
int json_parse_name_value(char *p_cJsonStr, int iStrLen, json_parse_cb pfnCB, void *p_CBData);
|
||||
|
||||
/**
|
||||
* @brief Get the value by a specified key from a json string
|
||||
*
|
||||
* @param[in] p_cJsonStr @n the JSON string
|
||||
* @param[in] iStrLen @n the JSON string length
|
||||
* @param[in] p_cName @n the specified key string
|
||||
* @param[out] p_iValueLen @n the value length
|
||||
* @param[out] p_iValueType @n the value type
|
||||
* @return A pointer to the value
|
||||
* @see None.
|
||||
* @note None.
|
||||
**/
|
||||
char *json_get_value_by_name(char *p_cJsonStr, int iStrLen, char *p_cName, int *p_iValueLen, int *p_iValueType);
|
||||
|
||||
/**
|
||||
* @brief Get the value by a specified key from a json string
|
||||
*
|
||||
* @param[in] p_cJsonStr @n the JSON string
|
||||
* @param[in] iStrLen @n the JSON string length
|
||||
* @param[in] p_cName @n the specified key string
|
||||
* @param[in] p_cNameLen @n the specified key string length
|
||||
* @param[out] p_iValueLen @n the value length
|
||||
* @param[out] p_iValueType @n the value type
|
||||
* @return A pointer to the value
|
||||
* @see None.
|
||||
* @note None.
|
||||
**/
|
||||
char *json_get_value_by_name_len(char *p_cJsonStr, int iStrLen, char *p_cName, int p_cNameLen, int *p_iValueLen,
|
||||
int *p_iValueType);
|
||||
|
||||
/**
|
||||
* @brief Get the JSON object point associate with a given type.
|
||||
*
|
||||
* @param[in] type @n The object type
|
||||
* @param[in] str @n The JSON string
|
||||
* @param[in] str_end @n The end point of Json string
|
||||
* @returns The json object point with the given field type.
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
char *json_get_object(int type, char *str, char *str_end);
|
||||
char *json_get_next_object(int type, char *str, char *str_end, char **key, int *key_len, char **val, int *val_len,
|
||||
int *val_type);
|
||||
|
||||
/**
|
||||
* @brief retrieve each key&value pair from the json string
|
||||
*
|
||||
* @param[in] str @n Json string to revolve
|
||||
* @param[in] slen @n The length of json string
|
||||
* @param[in] pos @n cursor
|
||||
* @param[out] key @n pointer to the next Key object
|
||||
* @param[out] klen @n Key object length
|
||||
* @param[out] val @n pointer to the next Value object
|
||||
* @param[out] vlen @n Value object length
|
||||
* @param[out] vtype @n Value object type(digital, string, object, array)
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
#define json_object_for_each_kv(str, slen, pos, key, klen, val, vlen, vtype) \
|
||||
for (pos = json_get_object(JOBJECT, str, str + slen); \
|
||||
pos != NULL && *pos!= 0 && (pos=json_get_next_object(JOBJECT, pos, str + slen , &key, &klen, &val, &vlen, &vtype))!=0; )
|
||||
|
||||
/**
|
||||
* @brief retrieve each entry from the json array
|
||||
*
|
||||
* @param[in] str @n Json array to revolve
|
||||
* @param[in] slen @n the length of Json array
|
||||
* @param[in] pos @n cursor
|
||||
* @param[out] entry @n pointer to the next entry from the array
|
||||
* @param[out] len @n entry length
|
||||
* @param[out] type @n entry type(digital, string, object, array)
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
#define json_array_for_each_entry(str, slen, pos, entry, len, type) \
|
||||
for (pos = json_get_object(JARRAY, str, str + slen); \
|
||||
pos != NULL && *pos!= 0 && (pos=json_get_next_object(JARRAY, ++pos, str + slen, 0, 0, &entry, &len, &type))!=0; )
|
||||
|
||||
|
||||
/**
|
||||
* @brief backup the last character to register parameters,
|
||||
* and set the end character with '\0'
|
||||
*
|
||||
* @param[in] json_str @n json string
|
||||
* @param[in] str_len @n json string lenth
|
||||
* @param[out] register @n used to backup the last character
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
#define backup_json_str_last_char(json_str, str_len, register) { \
|
||||
register = *((char *)json_str + str_len); \
|
||||
*((char *)json_str + str_len) = '\0'; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief restore the last character from register parameters
|
||||
*
|
||||
* @param[in] json_str @n json string
|
||||
* @param[in] str_len @n json string lenth
|
||||
* @param[in] register @n used to restore the last character
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
#define restore_json_str_last_char(json_str, str_len, register) { \
|
||||
*((char *)json_str + str_len) = register; \
|
||||
}
|
||||
|
||||
char *LITE_json_value_of(char *key, char *src, ...);
|
||||
|
||||
#endif /* __JSON_PARSER_H__ */
|
||||
|
||||
|
||||
+395
@@ -0,0 +1,395 @@
|
||||
|
||||
#ifndef _INFRA_LIST_H_
|
||||
#define _INFRA_LIST_H_
|
||||
|
||||
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) || defined(__GNUC__)) && \
|
||||
!defined(inline) && !defined(__cplusplus)
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Get offset of a member variable.
|
||||
*
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the variable within the struct.
|
||||
*/
|
||||
#define aos_offsetof(type, member) ((size_t)&(((type *)0)->member))
|
||||
|
||||
/*
|
||||
* Get the struct for this entry.
|
||||
*
|
||||
* @param[in] ptr the list head to take the element from.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the variable within the struct.
|
||||
*/
|
||||
#define aos_container_of(ptr, type, member) \
|
||||
((type *) ((char *) (ptr) - aos_offsetof(type, member)))
|
||||
|
||||
/* for double link list */
|
||||
typedef struct dlist_s {
|
||||
struct dlist_s *prev;
|
||||
struct dlist_s *next;
|
||||
} dlist_t;
|
||||
|
||||
static inline void __dlist_add(dlist_t *node, dlist_t *prev, dlist_t *next)
|
||||
{
|
||||
node->next = next;
|
||||
node->prev = prev;
|
||||
|
||||
prev->next = node;
|
||||
next->prev = node;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the struct for this entry.
|
||||
*
|
||||
* @param[in] addr the list head to take the element from.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the dlist_t within the struct.
|
||||
*/
|
||||
#define dlist_entry(addr, type, member) \
|
||||
((type *)((long)addr - aos_offsetof(type, member)))
|
||||
|
||||
|
||||
static inline void dlist_add(dlist_t *node, dlist_t *queue)
|
||||
{
|
||||
__dlist_add(node, queue, queue->next);
|
||||
}
|
||||
|
||||
static inline void dlist_add_tail(dlist_t *node, dlist_t *queue)
|
||||
{
|
||||
__dlist_add(node, queue->prev, queue);
|
||||
}
|
||||
|
||||
static inline void dlist_del(dlist_t *node)
|
||||
{
|
||||
dlist_t *prev = node->prev;
|
||||
dlist_t *next = node->next;
|
||||
|
||||
prev->next = next;
|
||||
next->prev = prev;
|
||||
}
|
||||
|
||||
static inline void dlist_init(dlist_t *node)
|
||||
{
|
||||
node->next = node->prev = node;
|
||||
}
|
||||
|
||||
static inline void INIT_AOS_DLIST_HEAD(dlist_t *list)
|
||||
{
|
||||
list->next = list;
|
||||
list->prev = list;
|
||||
}
|
||||
|
||||
static inline int dlist_empty(const dlist_t *head)
|
||||
{
|
||||
return head->next == head;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialise the list.
|
||||
*
|
||||
* @param[in] list the list to be inited.
|
||||
*/
|
||||
#define AOS_DLIST_INIT(list) {&(list), &(list)}
|
||||
|
||||
/*
|
||||
* Get the first element from a list
|
||||
*
|
||||
* @param[in] ptr the list head to take the element from.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the dlist_t within the struct.
|
||||
*/
|
||||
#define dlist_first_entry(ptr, type, member) \
|
||||
dlist_entry((ptr)->next, type, member)
|
||||
|
||||
/*
|
||||
* Iterate over a list.
|
||||
*
|
||||
* @param[in] pos the &struct dlist_t to use as a loop cursor.
|
||||
* @param[in] head he head for your list.
|
||||
*/
|
||||
#define dlist_for_each(pos, head) \
|
||||
for (pos = (head)->next; pos != (head); pos = pos->next)
|
||||
|
||||
/*
|
||||
* Iterate over a list safe against removal of list entry.
|
||||
*
|
||||
* @param[in] pos the &struct dlist_t to use as a loop cursor.
|
||||
* @param[in] n another &struct dlist_t to use as temporary storage.
|
||||
* @param[in] head he head for your list.
|
||||
*/
|
||||
#define dlist_for_each_safe(pos, n, head) \
|
||||
for (pos = (head)->next, n = pos->next; pos != (head); \
|
||||
pos = n, n = pos->next)
|
||||
|
||||
/*
|
||||
* Iterate over list of given type.
|
||||
*
|
||||
* @param[in] queue he head for your list.
|
||||
* @param[in] node the &struct dlist_t to use as a loop cursor.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the dlist_t within the struct.
|
||||
*/
|
||||
#define dlist_for_each_entry(queue, node, type, member) \
|
||||
for (node = aos_container_of((queue)->next, type, member); \
|
||||
&node->member != (queue); \
|
||||
node = aos_container_of(node->member.next, type, member))
|
||||
|
||||
/*
|
||||
* Iterate over list of given type safe against removal of list entry.
|
||||
*
|
||||
* @param[in] queue the head for your list.
|
||||
* @param[in] n the type * to use as a temp.
|
||||
* @param[in] node the type * to use as a loop cursor.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the dlist_t within the struct.
|
||||
*/
|
||||
#define dlist_for_each_entry_safe(queue, n, node, type, member) \
|
||||
for (node = aos_container_of((queue)->next, type, member), \
|
||||
n = (queue)->next ? (queue)->next->next : NULL; \
|
||||
&node->member != (queue); \
|
||||
node = aos_container_of(n, type, member), n = n ? n->next : NULL)
|
||||
|
||||
/*
|
||||
* Get the struct for this entry.
|
||||
* @param[in] ptr the list head to take the element from.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the variable within the struct.
|
||||
*/
|
||||
#define list_entry(ptr, type, member) \
|
||||
aos_container_of(ptr, type, member)
|
||||
|
||||
|
||||
/*
|
||||
* Iterate backwards over list of given type.
|
||||
*
|
||||
* @param[in] pos the type * to use as a loop cursor.
|
||||
* @param[in] head he head for your list.
|
||||
* @param[in] member the name of the dlist_t within the struct.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
*/
|
||||
#define dlist_for_each_entry_reverse(pos, head, member, type) \
|
||||
for (pos = list_entry((head)->prev, type, member); \
|
||||
&pos->member != (head); \
|
||||
pos = list_entry(pos->member.prev, type, member))
|
||||
|
||||
|
||||
/*
|
||||
* Get the list length.
|
||||
*
|
||||
* @param[in] queue the head for your list.
|
||||
*/
|
||||
static inline int __dlist_entry_number(dlist_t *queue)
|
||||
{
|
||||
int num;
|
||||
dlist_t *cur = queue;
|
||||
for (num = 0; cur->next != queue; cur = cur->next, num++)
|
||||
;
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the list length.
|
||||
*
|
||||
* @param[in] queue the head for your list.
|
||||
*/
|
||||
#define dlist_entry_number(head) \
|
||||
__dlist_entry_number(head)
|
||||
|
||||
/*
|
||||
* Initialise the list.
|
||||
*
|
||||
* @param[in] name the list to be initialized.
|
||||
*/
|
||||
#define AOS_DLIST_HEAD_INIT(name) { &(name), &(name) }
|
||||
|
||||
/*
|
||||
* Initialise the list.
|
||||
*
|
||||
* @param[in] name the list to be initialized.
|
||||
*/
|
||||
#define AOS_DLIST_HEAD(name) \
|
||||
dlist_t name = AOS_DLIST_HEAD_INIT(name)
|
||||
|
||||
/* for single link list */
|
||||
typedef struct slist_s {
|
||||
struct slist_s *next;
|
||||
} slist_t;
|
||||
|
||||
static inline void slist_add(slist_t *node, slist_t *head)
|
||||
{
|
||||
node->next = head->next;
|
||||
head->next = node;
|
||||
}
|
||||
|
||||
static inline void slist_add_tail(slist_t *node, slist_t *head)
|
||||
{
|
||||
while (head->next) {
|
||||
head = head->next;
|
||||
}
|
||||
|
||||
slist_add(node, head);
|
||||
}
|
||||
|
||||
static inline void slist_del(slist_t *node, slist_t *head)
|
||||
{
|
||||
while (head->next) {
|
||||
if (head->next == node) {
|
||||
head->next = node->next;
|
||||
break;
|
||||
}
|
||||
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int slist_empty(const slist_t *head)
|
||||
{
|
||||
return !head->next;
|
||||
}
|
||||
|
||||
static inline void slist_init(slist_t *head)
|
||||
{
|
||||
head->next = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Iterate over list of given type.
|
||||
*
|
||||
* @param[in] queue he head for your list.
|
||||
* @param[in] node the type * to use as a loop cursor.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the slist_t within the struct.
|
||||
*/
|
||||
#define slist_for_each_entry(queue, node, type, member) \
|
||||
for (node = aos_container_of((queue)->next, type, member); \
|
||||
&node->member; \
|
||||
node = aos_container_of(node->member.next, type, member))
|
||||
|
||||
/*
|
||||
* Iterate over list of given type safe against removal of list entry.
|
||||
*
|
||||
* @param[in] queue the head for your list.
|
||||
* @param[in] tmp the type * to use as a temp.
|
||||
* @param[in] node the type * to use as a loop cursor.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the slist_t within the struct.
|
||||
*/
|
||||
#define slist_for_each_entry_safe(queue, tmp, node, type, member) \
|
||||
for (node = aos_container_of((queue)->next, type, member), \
|
||||
tmp = (queue)->next ? (queue)->next->next : NULL; \
|
||||
&node->member; \
|
||||
node = aos_container_of(tmp, type, member), tmp = tmp ? tmp->next : tmp)
|
||||
|
||||
/*
|
||||
* Initialise the list.
|
||||
*
|
||||
* @param[in] name the list to be initialized.
|
||||
*/
|
||||
#define AOS_SLIST_HEAD_INIT(name) {0}
|
||||
|
||||
/*
|
||||
* Initialise the list.
|
||||
*
|
||||
* @param[in] name the list to be initialized.
|
||||
*/
|
||||
#define AOS_SLIST_HEAD(name) \
|
||||
slist_t name = AOS_SLIST_HEAD_INIT(name)
|
||||
|
||||
/*
|
||||
* Get the struct for this entry.
|
||||
* @param[in] addr the list head to take the element from.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the slist_t within the struct.
|
||||
*/
|
||||
#define slist_entry(addr, type, member) ( \
|
||||
addr ? (type *)((long)addr - aos_offsetof(type, member)) : (type *)addr \
|
||||
)
|
||||
|
||||
/*
|
||||
* Get the first element from a list.
|
||||
*
|
||||
* @param[in] ptr the list head to take the element from.
|
||||
* @param[in] type the type of the struct this is embedded in.
|
||||
* @param[in] member the name of the slist_t within the struct.
|
||||
*/
|
||||
#define slist_first_entry(ptr, type, member) \
|
||||
slist_entry((ptr)->next, type, member)
|
||||
|
||||
/*
|
||||
* Get the list length.
|
||||
*
|
||||
* @param[in] queue the head for your list.
|
||||
*/
|
||||
static inline int slist_entry_number(slist_t *queue)
|
||||
{
|
||||
int num;
|
||||
slist_t *cur = queue;
|
||||
for (num = 0; cur->next; cur = cur->next, num++)
|
||||
;
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
#define LIST_HEAD AOS_DLIST_HEAD
|
||||
#define LIST_HEAD_INIT AOS_DLIST_INIT
|
||||
#define INIT_LIST_HEAD INIT_AOS_DLIST_HEAD
|
||||
#define LIST_INIT AOS_DLIST_INIT
|
||||
|
||||
#define list_head dlist_s
|
||||
#define list_head_t dlist_t
|
||||
|
||||
#define list_add dlist_add
|
||||
#define list_add_tail dlist_add_tail
|
||||
#define list_del dlist_del
|
||||
#define list_empty dlist_empty
|
||||
#define list_entry_number dlist_entry_number
|
||||
#define list_first_entry dlist_first_entry
|
||||
#define list_for_each dlist_for_each
|
||||
#define list_for_each_entry_reverse dlist_for_each_entry_reverse
|
||||
#define list_for_each_safe dlist_for_each_safe
|
||||
#define list_init dlist_init
|
||||
|
||||
#ifndef offset_of
|
||||
#define offset_of aos_offsetof
|
||||
#endif
|
||||
#ifndef container_of
|
||||
#define container_of aos_container_of
|
||||
#endif
|
||||
|
||||
#define list_for_each_entry(pos, head, member, type) \
|
||||
dlist_for_each_entry(head, pos, type, member)
|
||||
|
||||
#define list_for_each_entry_safe(pos, n, head, member, type) \
|
||||
for (pos = list_entry((head)->next, type, member), \
|
||||
n = list_entry(pos->member.next, type, member); \
|
||||
&pos->member != (head); \
|
||||
pos = n, n = list_entry(n->member.next, type, member))
|
||||
|
||||
#define list_next_entry(pos, member, type) \
|
||||
list_entry((pos)->member.next, type, member)
|
||||
|
||||
static inline void list_del_init(struct list_head *entry)
|
||||
{
|
||||
list_del(entry);
|
||||
INIT_LIST_HEAD(entry);
|
||||
}
|
||||
|
||||
static inline int list_is_last(const struct list_head *list,
|
||||
const struct list_head *head)
|
||||
{
|
||||
return list->next == head;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* AOS_LIST_H */
|
||||
|
||||
Vendored
+267
@@ -0,0 +1,267 @@
|
||||
#include "infra_config.h"
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include "infra_compat.h"
|
||||
#include "infra_log.h"
|
||||
#include "wrappers.h"
|
||||
#if defined(WITH_MEM_STATS)
|
||||
#include "infra_mem_stats.h"
|
||||
#endif
|
||||
#if defined(INFRA_CJSON)
|
||||
#include "infra_cjson.h"
|
||||
#endif
|
||||
|
||||
#if defined(INFRA_LOG) && !defined(INFRA_LOG_ALL_MUTED)
|
||||
static log_client logcb = {
|
||||
.name = "linkkit",
|
||||
.priority = LOG_DEBUG_LEVEL,
|
||||
.text_buf = {0}
|
||||
};
|
||||
|
||||
static char *lvl_names[] = {
|
||||
"non", "crt", "err", "wrn", "inf", "dbg", "flw"
|
||||
};
|
||||
|
||||
/* 31, red. 32, green. 33, yellow. 34, blue. 35, magenta. 36, cyan. 37, white. */
|
||||
char *lvl_color[] = {
|
||||
"[0m", "[1;31m", "[1;31m", "[1;35m", "[1;33m", "[1;36m", "[1;37m"
|
||||
};
|
||||
|
||||
void LITE_syslog_routine(char *m, const char *f, const int l, const int level, const char *fmt, va_list *params)
|
||||
{
|
||||
char *tmpbuf = logcb.text_buf;
|
||||
char *o = tmpbuf;
|
||||
int truncated = 0;
|
||||
|
||||
if (LITE_get_loglevel() < level || level < LOG_NONE_LEVEL) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if !defined(_WIN32)
|
||||
LITE_printf("%s%s", "\033", lvl_color[level]);
|
||||
LITE_printf(LOG_PREFIX_FMT, lvl_names[level], f, l);
|
||||
#endif /* #if !defined(_WIN32) */
|
||||
|
||||
memset(tmpbuf, 0, sizeof(logcb.text_buf));
|
||||
|
||||
o = tmpbuf;
|
||||
o += LITE_vsnprintf(o, LOG_MSG_MAXLEN + 1, fmt, *params);
|
||||
|
||||
if (o - tmpbuf > LOG_MSG_MAXLEN) {
|
||||
o = tmpbuf + strlen(tmpbuf);
|
||||
truncated = 1;
|
||||
}
|
||||
if (strlen(tmpbuf) == LOG_MSG_MAXLEN) {
|
||||
truncated = 1;
|
||||
}
|
||||
|
||||
LITE_printf("%s", tmpbuf);
|
||||
if (truncated) {
|
||||
LITE_printf(" ...");
|
||||
}
|
||||
|
||||
if (tmpbuf[strlen(tmpbuf) - 1] != '\n') {
|
||||
LITE_printf("\r\n");
|
||||
}
|
||||
|
||||
#if !defined(_WIN32)
|
||||
LITE_printf("%s", "\033[0m");
|
||||
#endif /* #if !defined(_WIN32) */
|
||||
return;
|
||||
}
|
||||
|
||||
void LITE_syslog(char *m, const char *f, const int l, const int level, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
LITE_syslog_routine(m, f, l, level, fmt, &ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
int LITE_get_loglevel(void)
|
||||
{
|
||||
return logcb.priority;
|
||||
}
|
||||
|
||||
void LITE_set_loglevel(int pri)
|
||||
{
|
||||
#if WITH_MEM_STATS
|
||||
void **mutex = NULL;
|
||||
#endif
|
||||
logcb.priority = pri;
|
||||
|
||||
#if WITH_MEM_STATS
|
||||
mutex = LITE_get_mem_mutex();
|
||||
if (pri != LOG_NONE_LEVEL) {
|
||||
if (*mutex == NULL) {
|
||||
*mutex = HAL_MutexCreate();
|
||||
if (*mutex == NULL) {
|
||||
LITE_printf("\nCreate memStats mutex error\n");
|
||||
}
|
||||
}
|
||||
} else if (*mutex != NULL) {
|
||||
HAL_MutexDestroy(*mutex);
|
||||
*mutex = NULL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void LITE_rich_hexdump(const char *f, const int l,
|
||||
const int level,
|
||||
const char *buf_str,
|
||||
const void *buf_ptr,
|
||||
const int buf_len)
|
||||
{
|
||||
if (LITE_get_loglevel() < level) {
|
||||
return;
|
||||
}
|
||||
|
||||
LITE_printf("%s%s", "\033", lvl_color[level]);
|
||||
LITE_printf(LOG_PREFIX_FMT, lvl_names[level], f, l);
|
||||
LITE_printf("HEXDUMP %s @ %p[%d]\r\n", buf_str, buf_ptr, buf_len);
|
||||
LITE_hexdump(buf_str, buf_ptr, buf_len);
|
||||
|
||||
LITE_printf("%s", "\033[0m");
|
||||
return;
|
||||
}
|
||||
|
||||
int log_multi_line_internal(const char *f, const int l,
|
||||
const char *title, int level, char *payload, const char *mark)
|
||||
{
|
||||
const char *pos;
|
||||
const char *endl;
|
||||
int i;
|
||||
|
||||
if (LITE_get_loglevel() < level) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
LITE_printf("[%s] %s(%d): %s (Length: %d Bytes)\r\n",
|
||||
lvl_names[LITE_get_loglevel()], f, l, title, (int)strlen(payload));
|
||||
|
||||
pos = payload;
|
||||
while (pos && *pos) {
|
||||
LITE_printf("%s ", mark);
|
||||
|
||||
if (*pos == '\r') {
|
||||
LITE_printf("\r\n");
|
||||
pos += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
endl = strchr(pos + 1, '\r');
|
||||
if (endl == NULL) {
|
||||
endl = pos;
|
||||
do {
|
||||
++endl;
|
||||
} while (*endl);
|
||||
}
|
||||
|
||||
for (i = 0; i < endl - pos; ++i) {
|
||||
LITE_printf("%c", pos[i]);
|
||||
}
|
||||
LITE_printf("\r\n");
|
||||
|
||||
pos = *endl ? endl + 2 : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define LITE_HEXDUMP_DRAWLINE(start_mark, len, end_mark) \
|
||||
do { \
|
||||
int i; \
|
||||
\
|
||||
LITE_printf("%s", start_mark); \
|
||||
for(i = 0; i < len; ++i) { LITE_printf("-"); } \
|
||||
LITE_printf("%s", end_mark); \
|
||||
LITE_printf("\r\n"); \
|
||||
\
|
||||
} while(0)
|
||||
|
||||
int LITE_hexdump(const char *title, const void *buff, const int len)
|
||||
{
|
||||
int i, j, written;
|
||||
unsigned char ascii[20] = {0};
|
||||
char header[64] = {0};
|
||||
unsigned char *buf = (unsigned char *)buff;
|
||||
|
||||
LITE_snprintf(header, sizeof(header), "| %s: (len=%d) |\r\n", title, (int)len);
|
||||
|
||||
LITE_HEXDUMP_DRAWLINE("+", strlen(header) - 4, "+");
|
||||
LITE_printf("%s", header);
|
||||
LITE_printf("%s\r\n", HEXDUMP_SEP_LINE);
|
||||
|
||||
written = 0;
|
||||
for (i = 0; i < len; ++ i) {
|
||||
if (i % 16 == 0) {
|
||||
LITE_printf("| %08X: ", (unsigned int)(i + (long)buff));
|
||||
written += 8;
|
||||
}
|
||||
|
||||
LITE_printf("%02X", buf[i]);
|
||||
written += 2;
|
||||
|
||||
if (i % 2 == 1) {
|
||||
LITE_printf(" ");
|
||||
written += 1;
|
||||
}
|
||||
LITE_snprintf((char *)ascii + i % 16, (1 + 1), "%c", ((buf[i] >= ' ' && buf[i] <= '~') ? buf[i] : '.'));
|
||||
|
||||
if (((i + 1) % 16 == 0) || (i == len - 1)) {
|
||||
for (j = 0; j < 48 - written; ++j) {
|
||||
LITE_printf(" ");
|
||||
}
|
||||
|
||||
LITE_printf(" %s", ascii);
|
||||
LITE_printf("\r\n");
|
||||
|
||||
written = 0;
|
||||
memset(ascii, 0, sizeof(ascii));
|
||||
}
|
||||
}
|
||||
LITE_printf("%s\r\n", HEXDUMP_SEP_LINE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IOT_SetLogLevel(IOT_LogLevel level)
|
||||
{
|
||||
int lvl = (int)level;
|
||||
|
||||
if (lvl > LOG_DEBUG_LEVEL) {
|
||||
HAL_Printf("Invalid input level: %d out of [%d, %d]", level,
|
||||
LOG_NONE_LEVEL,
|
||||
LOG_DEBUG_LEVEL);
|
||||
return;
|
||||
}
|
||||
|
||||
LITE_set_loglevel(lvl);
|
||||
HAL_Printf("[prt] log level set as: [ %d ]\r\n", lvl);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void IOT_SetLogLevel(IOT_LogLevel level)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int log_multi_line_internal(const char *f, const int l,
|
||||
const char *title, int level, char *payload, const char *mark)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LITE_rich_hexdump(const char *f, const int l,
|
||||
const int level,
|
||||
const char *buf_str,
|
||||
const void *buf_ptr,
|
||||
const int buf_len)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
#endif /* #if defined(INFRA_LOG) && !defined(INFRA_LOG_ALL_MUTED) */
|
||||
|
||||
Vendored
+117
@@ -0,0 +1,117 @@
|
||||
#ifndef _INFRA_LOG_H_
|
||||
#define _INFRA_LOG_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
#include "infra_defs.h"
|
||||
#include "wrappers.h"
|
||||
|
||||
|
||||
#define LITE_printf HAL_Printf
|
||||
#define LITE_snprintf HAL_Snprintf
|
||||
#define LITE_vsnprintf HAL_Vsnprintf
|
||||
#define LITE_LOG_ENABLED
|
||||
|
||||
#define LOG_MSG_MAXLEN (255)
|
||||
#define LOG_MOD_NAME_LEN (7)
|
||||
#define LOG_PREFIX_FMT "[%s] %s(%d): "
|
||||
|
||||
#define HEXDUMP_SEP_LINE "+" \
|
||||
"-----------------------" \
|
||||
"-----------------------" \
|
||||
"-----------------------"
|
||||
|
||||
#if defined(_PLATFORM_IS_LINUX_)
|
||||
#undef LOG_MSG_MAXLEN
|
||||
#define LOG_MSG_MAXLEN (512)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char name[LOG_MOD_NAME_LEN + 1];
|
||||
int priority;
|
||||
char text_buf[LOG_MSG_MAXLEN + 1];
|
||||
} log_client;
|
||||
|
||||
int LITE_get_loglevel(void);
|
||||
void LITE_set_loglevel(int level);
|
||||
int LITE_hexdump(const char *title, const void *buf, const int len);
|
||||
|
||||
void LITE_syslog_routine(char *m, const char *f, const int l, const int level, const char *fmt, va_list *params);
|
||||
void LITE_syslog(char *m, const char *f, const int l, const int level, const char *fmt, ...);
|
||||
|
||||
#define LOG_NONE_LEVEL (0) /* no log printed at all */
|
||||
#define LOG_CRIT_LEVEL (1) /* current application aborting */
|
||||
#define LOG_ERR_LEVEL (2) /* current app-module error */
|
||||
#define LOG_WARNING_LEVEL (3) /* using default parameters */
|
||||
#define LOG_INFO_LEVEL (4) /* running messages */
|
||||
#define LOG_DEBUG_LEVEL (5) /* debugging messages */
|
||||
#define LOG_FLOW_LEVEL (6) /* code/packet flow messages */
|
||||
|
||||
#if defined(INFRA_LOG) && !defined(INFRA_LOG_ALL_MUTED)
|
||||
#if defined(INFRA_LOG_MUTE_FLW)
|
||||
#define log_flow(mod, ...)
|
||||
#else
|
||||
#define log_flow(mod, ...) LITE_syslog(mod, __FUNCTION__, __LINE__, LOG_FLOW_LEVEL, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if defined(INFRA_LOG_MUTE_DBG)
|
||||
#define log_debug(mod, ...)
|
||||
#else
|
||||
#define log_debug(mod, ...) LITE_syslog(mod, __FUNCTION__, __LINE__, LOG_DEBUG_LEVEL, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if defined(INFRA_LOG_MUTE_INF)
|
||||
#define log_info(mod, ...)
|
||||
#else
|
||||
#define log_info(mod, ...) LITE_syslog(mod, __FUNCTION__, __LINE__, LOG_INFO_LEVEL, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if defined(INFRA_LOG_MUTE_WRN)
|
||||
#define log_warning(mod, ...)
|
||||
#else
|
||||
#define log_warning(mod, ...) LITE_syslog(mod, __FUNCTION__, __LINE__, LOG_WARNING_LEVEL, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if defined(INFRA_LOG_MUTE_ERR)
|
||||
#define log_err(mod, ...)
|
||||
#else
|
||||
#define log_err(mod, ...) LITE_syslog(mod, __FUNCTION__, __LINE__, LOG_ERR_LEVEL, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if defined(INFRA_LOG_MUTE_CRT)
|
||||
#define log_crit(mod, ...)
|
||||
#else
|
||||
#define log_crit(mod, ...) LITE_syslog(mod, __FUNCTION__, __LINE__, LOG_CRIT_LEVEL, __VA_ARGS__)
|
||||
#endif
|
||||
#else /* #if defined(INFRA_LOG) */
|
||||
|
||||
#define log_flow(mod, ...)
|
||||
#define log_debug(mod, ...)
|
||||
#define log_info(mod, ...)
|
||||
#define log_warning(mod, ...)
|
||||
#define log_err(mod, ...)
|
||||
#define log_crit(mod, ...)
|
||||
|
||||
#endif
|
||||
|
||||
int log_multi_line_internal(const char *f, const int l,
|
||||
const char *title, int level, char *payload, const char *mark);
|
||||
#define log_multi_line(level, title, fmt, payload, mark) \
|
||||
log_multi_line_internal(__func__, __LINE__, title, level, payload, mark)
|
||||
|
||||
void LITE_rich_hexdump(const char *f, const int l,
|
||||
const int level,
|
||||
const char *buf_str,
|
||||
const void *buf_ptr,
|
||||
const int buf_len
|
||||
);
|
||||
|
||||
#define HEXDUMP_DEBUG(buf, len) \
|
||||
LITE_rich_hexdump(__func__, __LINE__, LOG_DEBUG_LEVEL, #buf, (const void *)buf, (const int)len)
|
||||
|
||||
#define HEXDUMP_INFO(buf, len) \
|
||||
LITE_rich_hexdump(__func__, __LINE__, LOG_INFO_LEVEL, #buf, (const void *)buf, (const int)len)
|
||||
|
||||
int iotx_facility_json_print(const char *str, int level, ...);
|
||||
|
||||
#endif
|
||||
|
||||
Vendored
+351
@@ -0,0 +1,351 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_MD5
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "infra_md5.h"
|
||||
|
||||
#define MD5_KEY_IOPAD_SIZE (64)
|
||||
#define MD5_DIGEST_SIZE (16)
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void utils_md5_zeroize(void *v, size_t n)
|
||||
{
|
||||
volatile unsigned char *p = v;
|
||||
while (n--) {
|
||||
*p++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (little endian)
|
||||
*/
|
||||
#ifndef IOT_MD5_GET_UINT32_LE
|
||||
#define IOT_MD5_GET_UINT32_LE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (uint32_t) (b)[(i) ] ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] << 24 ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef IOT_MD5_PUT_UINT32_LE
|
||||
#define IOT_MD5_PUT_UINT32_LE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
void utils_md5_init(iot_md5_context *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(iot_md5_context));
|
||||
}
|
||||
|
||||
void utils_md5_free(iot_md5_context *ctx)
|
||||
{
|
||||
if (ctx == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
utils_md5_zeroize(ctx, sizeof(iot_md5_context));
|
||||
}
|
||||
|
||||
void utils_md5_clone(iot_md5_context *dst,
|
||||
const iot_md5_context *src)
|
||||
{
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 context setup
|
||||
*/
|
||||
void utils_md5_starts(iot_md5_context *ctx)
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xEFCDAB89;
|
||||
ctx->state[2] = 0x98BADCFE;
|
||||
ctx->state[3] = 0x10325476;
|
||||
}
|
||||
|
||||
void utils_md5_process(iot_md5_context *ctx, const unsigned char data[64])
|
||||
{
|
||||
uint32_t X[16], A, B, C, D;
|
||||
|
||||
IOT_MD5_GET_UINT32_LE(X[ 0], data, 0);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 1], data, 4);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 2], data, 8);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 3], data, 12);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 4], data, 16);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 5], data, 20);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 6], data, 24);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 7], data, 28);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 8], data, 32);
|
||||
IOT_MD5_GET_UINT32_LE(X[ 9], data, 36);
|
||||
IOT_MD5_GET_UINT32_LE(X[10], data, 40);
|
||||
IOT_MD5_GET_UINT32_LE(X[11], data, 44);
|
||||
IOT_MD5_GET_UINT32_LE(X[12], data, 48);
|
||||
IOT_MD5_GET_UINT32_LE(X[13], data, 52);
|
||||
IOT_MD5_GET_UINT32_LE(X[14], data, 56);
|
||||
IOT_MD5_GET_UINT32_LE(X[15], data, 60);
|
||||
|
||||
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
|
||||
|
||||
#define P(a,b,c,d,k,s,t) \
|
||||
{ \
|
||||
a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
|
||||
#define F(x,y,z) (z ^ (x & (y ^ z)))
|
||||
|
||||
P(A, B, C, D, 0, 7, 0xD76AA478);
|
||||
P(D, A, B, C, 1, 12, 0xE8C7B756);
|
||||
P(C, D, A, B, 2, 17, 0x242070DB);
|
||||
P(B, C, D, A, 3, 22, 0xC1BDCEEE);
|
||||
P(A, B, C, D, 4, 7, 0xF57C0FAF);
|
||||
P(D, A, B, C, 5, 12, 0x4787C62A);
|
||||
P(C, D, A, B, 6, 17, 0xA8304613);
|
||||
P(B, C, D, A, 7, 22, 0xFD469501);
|
||||
P(A, B, C, D, 8, 7, 0x698098D8);
|
||||
P(D, A, B, C, 9, 12, 0x8B44F7AF);
|
||||
P(C, D, A, B, 10, 17, 0xFFFF5BB1);
|
||||
P(B, C, D, A, 11, 22, 0x895CD7BE);
|
||||
P(A, B, C, D, 12, 7, 0x6B901122);
|
||||
P(D, A, B, C, 13, 12, 0xFD987193);
|
||||
P(C, D, A, B, 14, 17, 0xA679438E);
|
||||
P(B, C, D, A, 15, 22, 0x49B40821);
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (y ^ (z & (x ^ y)))
|
||||
|
||||
P(A, B, C, D, 1, 5, 0xF61E2562);
|
||||
P(D, A, B, C, 6, 9, 0xC040B340);
|
||||
P(C, D, A, B, 11, 14, 0x265E5A51);
|
||||
P(B, C, D, A, 0, 20, 0xE9B6C7AA);
|
||||
P(A, B, C, D, 5, 5, 0xD62F105D);
|
||||
P(D, A, B, C, 10, 9, 0x02441453);
|
||||
P(C, D, A, B, 15, 14, 0xD8A1E681);
|
||||
P(B, C, D, A, 4, 20, 0xE7D3FBC8);
|
||||
P(A, B, C, D, 9, 5, 0x21E1CDE6);
|
||||
P(D, A, B, C, 14, 9, 0xC33707D6);
|
||||
P(C, D, A, B, 3, 14, 0xF4D50D87);
|
||||
P(B, C, D, A, 8, 20, 0x455A14ED);
|
||||
P(A, B, C, D, 13, 5, 0xA9E3E905);
|
||||
P(D, A, B, C, 2, 9, 0xFCEFA3F8);
|
||||
P(C, D, A, B, 7, 14, 0x676F02D9);
|
||||
P(B, C, D, A, 12, 20, 0x8D2A4C8A);
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
|
||||
P(A, B, C, D, 5, 4, 0xFFFA3942);
|
||||
P(D, A, B, C, 8, 11, 0x8771F681);
|
||||
P(C, D, A, B, 11, 16, 0x6D9D6122);
|
||||
P(B, C, D, A, 14, 23, 0xFDE5380C);
|
||||
P(A, B, C, D, 1, 4, 0xA4BEEA44);
|
||||
P(D, A, B, C, 4, 11, 0x4BDECFA9);
|
||||
P(C, D, A, B, 7, 16, 0xF6BB4B60);
|
||||
P(B, C, D, A, 10, 23, 0xBEBFBC70);
|
||||
P(A, B, C, D, 13, 4, 0x289B7EC6);
|
||||
P(D, A, B, C, 0, 11, 0xEAA127FA);
|
||||
P(C, D, A, B, 3, 16, 0xD4EF3085);
|
||||
P(B, C, D, A, 6, 23, 0x04881D05);
|
||||
P(A, B, C, D, 9, 4, 0xD9D4D039);
|
||||
P(D, A, B, C, 12, 11, 0xE6DB99E5);
|
||||
P(C, D, A, B, 15, 16, 0x1FA27CF8);
|
||||
P(B, C, D, A, 2, 23, 0xC4AC5665);
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (y ^ (x | ~z))
|
||||
|
||||
P(A, B, C, D, 0, 6, 0xF4292244);
|
||||
P(D, A, B, C, 7, 10, 0x432AFF97);
|
||||
P(C, D, A, B, 14, 15, 0xAB9423A7);
|
||||
P(B, C, D, A, 5, 21, 0xFC93A039);
|
||||
P(A, B, C, D, 12, 6, 0x655B59C3);
|
||||
P(D, A, B, C, 3, 10, 0x8F0CCC92);
|
||||
P(C, D, A, B, 10, 15, 0xFFEFF47D);
|
||||
P(B, C, D, A, 1, 21, 0x85845DD1);
|
||||
P(A, B, C, D, 8, 6, 0x6FA87E4F);
|
||||
P(D, A, B, C, 15, 10, 0xFE2CE6E0);
|
||||
P(C, D, A, B, 6, 15, 0xA3014314);
|
||||
P(B, C, D, A, 13, 21, 0x4E0811A1);
|
||||
P(A, B, C, D, 4, 6, 0xF7537E82);
|
||||
P(D, A, B, C, 11, 10, 0xBD3AF235);
|
||||
P(C, D, A, B, 2, 15, 0x2AD7D2BB);
|
||||
P(B, C, D, A, 9, 21, 0xEB86D391);
|
||||
|
||||
#undef F
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 process buffer
|
||||
*/
|
||||
void utils_md5_update(iot_md5_context *ctx, const unsigned char *input, uint32_t ilen)
|
||||
{
|
||||
uint32_t fill;
|
||||
uint32_t left;
|
||||
|
||||
if (ilen == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += (uint32_t) ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if (ctx->total[0] < (uint32_t) ilen) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
if (left && ilen >= fill) {
|
||||
memcpy((void *)(ctx->buffer + left), input, fill);
|
||||
utils_md5_process(ctx, ctx->buffer);
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while (ilen >= 64) {
|
||||
utils_md5_process(ctx, input);
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if (ilen > 0) {
|
||||
memcpy((void *)(ctx->buffer + left), input, ilen);
|
||||
}
|
||||
}
|
||||
|
||||
static const unsigned char iot_md5_padding[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* MD5 final digest
|
||||
*/
|
||||
void utils_md5_finish(iot_md5_context *ctx, unsigned char output[16])
|
||||
{
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = (ctx->total[0] >> 29)
|
||||
| (ctx->total[1] << 3);
|
||||
low = (ctx->total[0] << 3);
|
||||
|
||||
IOT_MD5_PUT_UINT32_LE(low, msglen, 0);
|
||||
IOT_MD5_PUT_UINT32_LE(high, msglen, 4);
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = (last < 56) ? (56 - last) : (120 - last);
|
||||
|
||||
utils_md5_update(ctx, iot_md5_padding, padn);
|
||||
utils_md5_update(ctx, msglen, 8);
|
||||
|
||||
IOT_MD5_PUT_UINT32_LE(ctx->state[0], output, 0);
|
||||
IOT_MD5_PUT_UINT32_LE(ctx->state[1], output, 4);
|
||||
IOT_MD5_PUT_UINT32_LE(ctx->state[2], output, 8);
|
||||
IOT_MD5_PUT_UINT32_LE(ctx->state[3], output, 12);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* output = MD5( input buffer )
|
||||
*/
|
||||
void utils_md5(const unsigned char *input, uint32_t ilen, unsigned char output[16])
|
||||
{
|
||||
iot_md5_context ctx;
|
||||
|
||||
utils_md5_init(&ctx);
|
||||
utils_md5_starts(&ctx);
|
||||
utils_md5_update(&ctx, input, ilen);
|
||||
utils_md5_finish(&ctx, output);
|
||||
utils_md5_free(&ctx);
|
||||
}
|
||||
|
||||
static int8_t utils_hb2hex(uint8_t hb)
|
||||
{
|
||||
hb = hb & 0xF;
|
||||
return (int8_t)(hb < 10 ? '0' + hb : hb - 10 + 'a');
|
||||
}
|
||||
|
||||
void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len)
|
||||
{
|
||||
iot_md5_context context;
|
||||
unsigned char k_ipad[MD5_KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
|
||||
unsigned char k_opad[MD5_KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
|
||||
unsigned char out[MD5_DIGEST_SIZE];
|
||||
int i;
|
||||
|
||||
if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (key_len > MD5_KEY_IOPAD_SIZE) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* start out by storing key in pads */
|
||||
memset(k_ipad, 0, sizeof(k_ipad));
|
||||
memset(k_opad, 0, sizeof(k_opad));
|
||||
memcpy(k_ipad, key, key_len);
|
||||
memcpy(k_opad, key, key_len);
|
||||
|
||||
/* XOR key with ipad and opad values */
|
||||
for (i = 0; i < MD5_KEY_IOPAD_SIZE; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
/* perform inner MD5 */
|
||||
utils_md5_init(&context); /* init context for 1st pass */
|
||||
utils_md5_starts(&context); /* setup context for 1st pass */
|
||||
utils_md5_update(&context, k_ipad, MD5_KEY_IOPAD_SIZE); /* start with inner pad */
|
||||
utils_md5_update(&context, (unsigned char *) msg, msg_len); /* then text of datagram */
|
||||
utils_md5_finish(&context, out); /* finish up 1st pass */
|
||||
|
||||
/* perform outer MD5 */
|
||||
utils_md5_init(&context); /* init context for 2nd pass */
|
||||
utils_md5_starts(&context); /* setup context for 2nd pass */
|
||||
utils_md5_update(&context, k_opad, MD5_KEY_IOPAD_SIZE); /* start with outer pad */
|
||||
utils_md5_update(&context, out, MD5_DIGEST_SIZE); /* then results of 1st hash */
|
||||
utils_md5_finish(&context, out); /* finish up 2nd pass */
|
||||
|
||||
for (i = 0; i < MD5_DIGEST_SIZE; ++i) {
|
||||
digest[i * 2] = utils_hb2hex(out[i] >> 4);
|
||||
digest[i * 2 + 1] = utils_hb2hex(out[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Vendored
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef _INFRA_MD5_H_
|
||||
#define _INFRA_MD5_H_
|
||||
|
||||
#include "infra_types.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t total[2]; /*!< number of bytes processed */
|
||||
uint32_t state[4]; /*!< intermediate digest state */
|
||||
unsigned char buffer[64]; /*!< data block being processed */
|
||||
} iot_md5_context;
|
||||
|
||||
/**
|
||||
* \brief Initialize MD5 context
|
||||
*
|
||||
* \param ctx MD5 context to be initialized
|
||||
*/
|
||||
void utils_md5_init(iot_md5_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief Clear MD5 context
|
||||
*
|
||||
* \param ctx MD5 context to be cleared
|
||||
*/
|
||||
void utils_md5_free(iot_md5_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief Clone (the state of) an MD5 context
|
||||
*
|
||||
* \param dst The destination context
|
||||
* \param src The context to be cloned
|
||||
*/
|
||||
void utils_md5_clone(iot_md5_context *dst,
|
||||
const iot_md5_context *src);
|
||||
|
||||
/**
|
||||
* \brief MD5 context setup
|
||||
*
|
||||
* \param ctx context to be initialized
|
||||
*/
|
||||
void utils_md5_starts(iot_md5_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief MD5 process buffer
|
||||
*
|
||||
* \param ctx MD5 context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void utils_md5_update(iot_md5_context *ctx, const unsigned char *input, uint32_t ilen);
|
||||
|
||||
/**
|
||||
* \brief MD5 final digest
|
||||
*
|
||||
* \param ctx MD5 context
|
||||
* \param output MD5 checksum result
|
||||
*/
|
||||
void utils_md5_finish(iot_md5_context *ctx, unsigned char output[16]);
|
||||
|
||||
/* Internal use */
|
||||
void utils_md5_process(iot_md5_context *ctx, const unsigned char data[64]);
|
||||
|
||||
/**
|
||||
* \brief Output = MD5( input buffer )
|
||||
*
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output MD5 checksum result
|
||||
*/
|
||||
void utils_md5(const unsigned char *input, uint32_t ilen, unsigned char output[16]);
|
||||
|
||||
void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len);
|
||||
|
||||
#endif
|
||||
|
||||
Vendored
+374
@@ -0,0 +1,374 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_NET
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "infra_state.h"
|
||||
#include "infra_defs.h"
|
||||
#include "infra_net.h"
|
||||
#include "wrappers_defs.h"
|
||||
#include "wrappers.h"
|
||||
|
||||
#ifdef INFRA_LOG
|
||||
#include "infra_log.h"
|
||||
#define net_err(...) log_err("infra_net", __VA_ARGS__)
|
||||
#else
|
||||
#define net_err(...)
|
||||
#endif
|
||||
|
||||
/*** SSL connection ***/
|
||||
#if defined(SUPPORT_TLS)
|
||||
#define CERT_HOST_ADDRESS "124.126.19.5"
|
||||
|
||||
static int is_cert_host_addr = 0;
|
||||
#if defined(AT_SSL_ENABLED)
|
||||
#include "at_ssl.h"
|
||||
|
||||
static int read_ssl(utils_network_pt pNetwork, char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return AT_SSL_Read((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int write_ssl(utils_network_pt pNetwork, const char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return AT_SSL_Write((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int disconnect_ssl(utils_network_pt pNetwork)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
AT_SSL_Destroy((uintptr_t)pNetwork->handle);
|
||||
pNetwork->handle = 0;
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
static int connect_ssl(utils_network_pt pNetwork)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
pNetwork->handle = AT_SSL_Establish(
|
||||
pNetwork->pHostAddress,
|
||||
pNetwork->port,
|
||||
pNetwork->ca_crt,
|
||||
pNetwork->ca_crt_len + 1);
|
||||
if (pNetwork->handle == (uintptr_t)(-1))
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
#else /* AT_SSL_ENABLED */
|
||||
#ifdef INFRA_MEM_STATS
|
||||
#include "infra_mem_stats.h"
|
||||
#define NET_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "infra_net")
|
||||
#define NET_FREE(ptr) LITE_free(ptr)
|
||||
extern int ssl_hooks_set(ssl_hooks_t *hooks);
|
||||
#else
|
||||
#define NET_MALLOC(size) HAL_Malloc(size)
|
||||
#define NET_FREE(ptr) HAL_Free(ptr)
|
||||
#endif
|
||||
|
||||
static void *ssl_malloc(uint32_t size)
|
||||
{
|
||||
return NET_MALLOC(size);
|
||||
}
|
||||
static void ssl_free(void *ptr)
|
||||
{
|
||||
NET_FREE(ptr);
|
||||
}
|
||||
|
||||
static int read_ssl(utils_network_pt pNetwork, char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return HAL_SSL_Read((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int write_ssl(utils_network_pt pNetwork, const char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return HAL_SSL_Write((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int disconnect_ssl(utils_network_pt pNetwork)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
HAL_SSL_Destroy((uintptr_t)pNetwork->handle);
|
||||
pNetwork->handle = 0;
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
static int connect_ssl(utils_network_pt pNetwork)
|
||||
{
|
||||
ssl_hooks_t ssl_hooks;
|
||||
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
#ifdef INFRA_MEM_STATS
|
||||
memset(&ssl_hooks, 0, sizeof(ssl_hooks_t));
|
||||
ssl_hooks.malloc = ssl_malloc;
|
||||
ssl_hooks.free = ssl_free;
|
||||
|
||||
ssl_hooks_set(&ssl_hooks);
|
||||
#else
|
||||
(void)ssl_hooks;
|
||||
#endif
|
||||
|
||||
if (0 != (pNetwork->handle = (intptr_t)HAL_SSL_Establish(
|
||||
pNetwork->pHostAddress,
|
||||
pNetwork->port,
|
||||
pNetwork->ca_crt,
|
||||
pNetwork->ca_crt_len + 1)))
|
||||
{
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* TODO SHOLUD not remove this handle space */
|
||||
/* The space will be freed by calling disconnect_ssl() */
|
||||
/* utils_memory_free((void *)pNetwork->handle); */
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
}
|
||||
#endif /* AT_SSL_ENABLED */
|
||||
#elif defined(AT_TCP_ENABLED)
|
||||
#include "at_tcp.h"
|
||||
/*** TCP connection ***/
|
||||
static int read_tcp(utils_network_pt pNetwork, char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
return AT_TCP_Read(pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int write_tcp(utils_network_pt pNetwork, const char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
return AT_TCP_Write(pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int disconnect_tcp(utils_network_pt pNetwork)
|
||||
{
|
||||
if (pNetwork->handle == (uintptr_t)(-1))
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
AT_TCP_Destroy(pNetwork->handle);
|
||||
pNetwork->handle = (uintptr_t)(-1);
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
static int connect_tcp(utils_network_pt pNetwork)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
pNetwork->handle = AT_TCP_Establish(pNetwork->pHostAddress, pNetwork->port);
|
||||
if (pNetwork->handle == (uintptr_t)(-1))
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
#endif /* #ifdef SUPPORT_TLS */
|
||||
/*** TCP connection ***/
|
||||
static int read_tcp(utils_network_pt pNetwork, char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
return HAL_TCP_Read(pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int write_tcp(utils_network_pt pNetwork, const char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
return HAL_TCP_Write(pNetwork->handle, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
static int disconnect_tcp(utils_network_pt pNetwork)
|
||||
{
|
||||
if (pNetwork->handle == (uintptr_t)(-1))
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
HAL_TCP_Destroy(pNetwork->handle);
|
||||
pNetwork->handle = (uintptr_t)(-1);
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
static int connect_tcp(utils_network_pt pNetwork)
|
||||
{
|
||||
if (NULL == pNetwork)
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
pNetwork->handle = HAL_TCP_Establish(pNetwork->pHostAddress, pNetwork->port);
|
||||
if (pNetwork->handle == (uintptr_t)(-1))
|
||||
{
|
||||
return STATE_SYS_DEPEND_NWK_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
|
||||
/****** network interface ******/
|
||||
int utils_net_read(utils_network_pt pNetwork, char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
int ret = 0;
|
||||
#if defined(SUPPORT_TLS)
|
||||
if ((is_cert_host_addr == 0)&&(NULL != pNetwork->ca_crt))
|
||||
{
|
||||
ret = read_ssl(pNetwork, buffer, len, timeout_ms);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = read_tcp(pNetwork, buffer, len, timeout_ms);
|
||||
}
|
||||
#else
|
||||
ret = read_tcp(pNetwork, buffer, len, timeout_ms);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int utils_net_write(utils_network_pt pNetwork, const char *buffer, uint32_t len, uint32_t timeout_ms)
|
||||
{
|
||||
int ret = 0;
|
||||
#if defined(SUPPORT_TLS)
|
||||
if ((is_cert_host_addr == 0)&&(NULL != pNetwork->ca_crt))
|
||||
{
|
||||
ret = write_ssl(pNetwork, buffer, len, timeout_ms);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = write_tcp(pNetwork, buffer, len, timeout_ms);
|
||||
}
|
||||
#else
|
||||
ret = write_tcp(pNetwork, buffer, len, timeout_ms);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int iotx_net_disconnect(utils_network_pt pNetwork)
|
||||
{
|
||||
int ret = 0;
|
||||
#if defined(SUPPORT_TLS)
|
||||
if ((is_cert_host_addr == 0)&&(NULL != pNetwork->ca_crt))
|
||||
{
|
||||
ret = disconnect_ssl(pNetwork);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = disconnect_tcp(pNetwork);
|
||||
}
|
||||
#else
|
||||
ret = disconnect_tcp(pNetwork);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int iotx_net_connect(utils_network_pt pNetwork)
|
||||
{
|
||||
int ret = 0;
|
||||
#if defined(SUPPORT_TLS)
|
||||
if ((is_cert_host_addr == 0)&&(NULL != pNetwork->ca_crt))
|
||||
{
|
||||
ret = connect_ssl(pNetwork);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = connect_tcp(pNetwork);
|
||||
}
|
||||
|
||||
#else
|
||||
ret = connect_tcp(pNetwork);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int iotx_net_init(utils_network_pt pNetwork, const char *host, uint16_t port, const char *ca_crt)
|
||||
{
|
||||
if (!pNetwork || !host)
|
||||
{
|
||||
return STATE_USER_INPUT_INVALID;
|
||||
}
|
||||
pNetwork->pHostAddress = host;
|
||||
pNetwork->port = port;
|
||||
|
||||
#if defined(SUPPORT_TLS)
|
||||
if (memcmp(pNetwork->pHostAddress, CERT_HOST_ADDRESS, strlen(pNetwork->pHostAddress)))
|
||||
{
|
||||
pNetwork->ca_crt = ca_crt;
|
||||
printf("\n\n\nTLS==================================\n\n\n\n");
|
||||
is_cert_host_addr = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
pNetwork->ca_crt = NULL;
|
||||
printf("\n\n\n========TCP==================================\n\n\n\n");
|
||||
is_cert_host_addr = 1;
|
||||
}
|
||||
#else
|
||||
pNetwork->ca_crt = ca_crt;
|
||||
#endif
|
||||
if (NULL == ca_crt)
|
||||
{
|
||||
pNetwork->ca_crt_len = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
pNetwork->ca_crt_len = strlen(ca_crt);
|
||||
}
|
||||
|
||||
pNetwork->handle = 0;
|
||||
pNetwork->read = utils_net_read;
|
||||
pNetwork->write = utils_net_write;
|
||||
pNetwork->disconnect = iotx_net_disconnect;
|
||||
pNetwork->connect = iotx_net_connect;
|
||||
|
||||
return STATE_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _INFRA_NET_H_
|
||||
#define _INFRA_NET_H_
|
||||
|
||||
#include "infra_types.h"
|
||||
|
||||
/**
|
||||
* @brief The structure of network connection(TCP or SSL).
|
||||
* The user has to allocate memory for this structure.
|
||||
*/
|
||||
|
||||
struct utils_network;
|
||||
typedef struct utils_network utils_network_t, *utils_network_pt;
|
||||
|
||||
struct utils_network {
|
||||
const char *pHostAddress;
|
||||
uint16_t port;
|
||||
uint16_t ca_crt_len;
|
||||
|
||||
/**< NULL, TCP connection; NOT NULL, SSL connection */
|
||||
const char *ca_crt;
|
||||
/**< NOT NULL,iTLS connection*/
|
||||
char *product_key;
|
||||
/**< connection handle: 0, NOT connection; NOT 0, handle of the connection */
|
||||
uintptr_t handle;
|
||||
|
||||
/**< Read data from server function pointer. */
|
||||
int (*read)(utils_network_pt, char *, uint32_t, uint32_t);
|
||||
|
||||
/**< Send data to server function pointer. */
|
||||
int (*write)(utils_network_pt, const char *, uint32_t, uint32_t);
|
||||
|
||||
/**< Disconnect the network */
|
||||
int (*disconnect)(utils_network_pt);
|
||||
|
||||
/**< Establish the network */
|
||||
int (*connect)(utils_network_pt);
|
||||
};
|
||||
|
||||
int utils_net_read(utils_network_pt pNetwork, char *buffer, uint32_t len, uint32_t timeout_ms);
|
||||
int utils_net_write(utils_network_pt pNetwork, const char *buffer, uint32_t len, uint32_t timeout_ms);
|
||||
int iotx_net_disconnect(utils_network_pt pNetwork);
|
||||
int iotx_net_connect(utils_network_pt pNetwork);
|
||||
int iotx_net_init(utils_network_pt pNetwork, const char *host, uint16_t port, const char *ca_crt);
|
||||
|
||||
#endif /* IOTX_COMMON_NET_H */
|
||||
|
||||
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_LOG_NETWORK_PAYLOAD
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include "infra_log.h"
|
||||
#if defined(INFRA_CJSON)
|
||||
#include "infra_cjson.h"
|
||||
#endif
|
||||
|
||||
#define JSON_NEWLINE "\r\n"
|
||||
#define JSON_INDENT " "
|
||||
|
||||
#define JSON_PRINT_NEWSTR HAL_Printf("%s", newstr);
|
||||
#define JSON_PRINT_NEWLINE \
|
||||
do { \
|
||||
HAL_Printf("%s", JSON_NEWLINE); \
|
||||
if (mark == '>' || mark == '<' || mark == ':') { \
|
||||
HAL_Printf("%c ", mark); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/* 31, red. 32, green. 33, yellow. 34, blue. 35, magenta. 36, cyan. 37, white. */
|
||||
static char *_color[] = {
|
||||
"[0m", "[1;31m", "[1;31m", "[1;35m", "[1;33m", "[1;36m", "[1;37m"
|
||||
};
|
||||
|
||||
int iotx_facility_json_print(const char *str, int level, ...)
|
||||
{
|
||||
int length = 0;
|
||||
char newstr[2];
|
||||
int quoted = 0;
|
||||
int escaped = 0;
|
||||
int indent = 0;
|
||||
int i = 0, j = 0;
|
||||
#if defined(INFRA_CJSON)
|
||||
int res = -1;
|
||||
lite_cjson_t lite;
|
||||
#endif
|
||||
va_list ap;
|
||||
int mark = ' ';
|
||||
|
||||
newstr[0] = 0x00;
|
||||
newstr[1] = 0x00;
|
||||
|
||||
if (str == NULL || strlen(str) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(INFRA_CJSON)
|
||||
res = lite_cjson_parse(str, strlen(str), &lite);
|
||||
if (res != SUCCESS_RETURN || !lite_cjson_is_object(&lite)) {
|
||||
return -2;
|
||||
}
|
||||
#endif
|
||||
|
||||
length = strlen(str);
|
||||
HAL_Printf("%s%s", "\033", _color[level]);
|
||||
va_start(ap, level);
|
||||
mark = va_arg(ap, int);
|
||||
JSON_PRINT_NEWLINE;
|
||||
va_end(ap);
|
||||
|
||||
for (i = 0 ; i < length ; i++) {
|
||||
char ch = str[i];
|
||||
switch (ch) {
|
||||
case '{':
|
||||
case '[':
|
||||
newstr[0] = ch;
|
||||
JSON_PRINT_NEWSTR;
|
||||
|
||||
if (!quoted) {
|
||||
JSON_PRINT_NEWLINE;
|
||||
|
||||
if (!(str[i + 1] == '}' || str[i + 1] == ']')) {
|
||||
++indent;
|
||||
|
||||
for (j = 0 ; j < indent ; j++) {
|
||||
HAL_Printf("%s", JSON_INDENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case '}':
|
||||
case ']':
|
||||
if (!quoted) {
|
||||
if ((i > 0) && (!(str[i - 1] == '{' || str[i - 1] == '['))) {
|
||||
JSON_PRINT_NEWLINE;
|
||||
--indent;
|
||||
|
||||
for (j = 0 ; j < indent ; j++) {
|
||||
HAL_Printf("%s", JSON_INDENT);
|
||||
}
|
||||
} else if ((i > 0) && ((str[i - 1] == '[' && ch == ']') || (str[i - 1] == '{' && ch == '}'))) {
|
||||
for (j = 0 ; j < indent ; j++) {
|
||||
HAL_Printf("%s", JSON_INDENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newstr[0] = ch;
|
||||
JSON_PRINT_NEWSTR;
|
||||
|
||||
break;
|
||||
|
||||
case '"':
|
||||
newstr[0] = ch;
|
||||
JSON_PRINT_NEWSTR;
|
||||
escaped = 1;
|
||||
|
||||
if (i > 0 && str[i - 1] == '\\') {
|
||||
escaped = !escaped;
|
||||
}
|
||||
|
||||
if (!escaped) {
|
||||
quoted = !quoted;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ',':
|
||||
newstr[0] = ch;
|
||||
JSON_PRINT_NEWSTR;
|
||||
if (!quoted) {
|
||||
JSON_PRINT_NEWLINE;
|
||||
|
||||
for (j = 0 ; j < indent ; j++) {
|
||||
HAL_Printf("%s", JSON_INDENT);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ':':
|
||||
newstr[0] = ch;
|
||||
JSON_PRINT_NEWSTR;
|
||||
if (!quoted) {
|
||||
HAL_Printf("%s", " ");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
newstr[0] = ch;
|
||||
JSON_PRINT_NEWSTR;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_Printf("%s", JSON_NEWLINE JSON_NEWLINE);
|
||||
HAL_Printf("%s", "\033[0m");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* #ifdef INFRA_LOG_NETWORK_PAYLOAD */
|
||||
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_REPORT
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "infra_compat.h"
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
#include "infra_string.h"
|
||||
#include "infra_report.h"
|
||||
#include "wrappers.h"
|
||||
|
||||
#ifdef INFRA_MEM_STATS
|
||||
#include "infra_mem_stats.h"
|
||||
#define SYS_REPORT_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "sys.report")
|
||||
#define SYS_REPORT_FREE(ptr) LITE_free(ptr)
|
||||
#else
|
||||
#define SYS_REPORT_MALLOC(size) HAL_Malloc(size)
|
||||
#define SYS_REPORT_FREE(ptr) HAL_Free(ptr)
|
||||
#endif
|
||||
|
||||
#ifdef INFRA_LOG
|
||||
#include "infra_log.h"
|
||||
#define VERSION_DEBUG(...) log_debug("version", __VA_ARGS__)
|
||||
#define VERSION_ERR(...) log_err("version", __VA_ARGS__)
|
||||
#else
|
||||
#define VERSION_DEBUG(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define VERSION_ERR(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#endif
|
||||
|
||||
static int g_report_id = 0;
|
||||
|
||||
int iotx_report_id(void)
|
||||
{
|
||||
if (++g_report_id < 0) {
|
||||
g_report_id = 0;
|
||||
}
|
||||
return g_report_id;
|
||||
}
|
||||
|
||||
static info_report_func_pt info_report_func = NULL;
|
||||
|
||||
void iotx_set_report_func(info_report_func_pt func)
|
||||
{
|
||||
info_report_func = func;
|
||||
}
|
||||
|
||||
const char *DEVICE_INFO_UPDATE_FMT = "{\"id\":\"%d\",\"version\":\"1.0\",\"params\":["
|
||||
"{\"attrKey\":\"SYS_LP_SDK_VERSION\",\"attrValue\":\"%s\",\"domain\":\"SYSTEM\"},"
|
||||
"{\"attrKey\":\"SYS_SDK_LANGUAGE\",\"attrValue\":\"C\",\"domain\":\"SYSTEM\"}"
|
||||
"],\"method\":\"thing.deviceinfo.update\"}";
|
||||
|
||||
int iotx_report_devinfo(void *pclient)
|
||||
{
|
||||
int ret = 0;
|
||||
char topic_name[IOTX_URI_MAX_LEN + 1] = {0};
|
||||
char product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0};
|
||||
char device_name[IOTX_DEVICE_NAME_LEN + 1] = {0};
|
||||
char *msg = NULL;
|
||||
int msg_len = 0;
|
||||
|
||||
|
||||
if (info_report_func == NULL) {
|
||||
VERSION_ERR("report func not register!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
IOT_Ioctl(IOTX_IOCTL_GET_PRODUCT_KEY, product_key);
|
||||
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_NAME, device_name);
|
||||
VERSION_DEBUG("devinfo report");
|
||||
|
||||
/* devinfo update topic name */
|
||||
ret = HAL_Snprintf(topic_name,
|
||||
IOTX_URI_MAX_LEN,
|
||||
"/sys/%s/%s/thing/deviceinfo/update",
|
||||
product_key,
|
||||
device_name);
|
||||
if (ret <= 0) {
|
||||
VERSION_ERR("topic generate err");
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
VERSION_DEBUG("devinfo report topic: %s", topic_name);
|
||||
|
||||
msg_len = strlen(DEVICE_INFO_UPDATE_FMT) + 10 + strlen(IOTX_SDK_VERSION) + 1;
|
||||
msg = (char *)SYS_REPORT_MALLOC(msg_len);
|
||||
if (msg == NULL) {
|
||||
VERSION_ERR("malloc err");
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
memset(msg, 0, msg_len);
|
||||
|
||||
/* devinfo update message */
|
||||
ret = HAL_Snprintf(msg,
|
||||
msg_len,
|
||||
DEVICE_INFO_UPDATE_FMT,
|
||||
iotx_report_id(),
|
||||
IOTX_SDK_VERSION
|
||||
);
|
||||
if (ret <= 0) {
|
||||
VERSION_ERR("topic msg generate err");
|
||||
SYS_REPORT_FREE(msg);
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
VERSION_DEBUG("devinfo report data: %s", msg);
|
||||
|
||||
if (info_report_func != NULL) {
|
||||
ret = info_report_func(pclient, topic_name, 1, msg, strlen(msg));
|
||||
if (ret < 0) {
|
||||
VERSION_ERR("publish failed, ret = %d", ret);
|
||||
SYS_REPORT_FREE(msg);
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
}
|
||||
|
||||
SYS_REPORT_FREE(msg);
|
||||
VERSION_DEBUG("devinfo report succeed");
|
||||
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
/* report Firmware version */
|
||||
int iotx_report_firmware_version(void *pclient)
|
||||
{
|
||||
int ret;
|
||||
char topic_name[IOTX_URI_MAX_LEN + 1] = {0};
|
||||
char msg[FIRMWARE_VERSION_MSG_LEN] = {0};
|
||||
char version[IOTX_FIRMWARE_VER_LEN + 1] = {0};
|
||||
char product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0};
|
||||
char device_name[IOTX_DEVICE_NAME_LEN + 1] = {0};
|
||||
char module[IOTX_MODULE_LEN] = {0};
|
||||
|
||||
if (info_report_func == NULL) {
|
||||
VERSION_ERR("report func not register!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
IOT_Ioctl(IOTX_IOCTL_GET_PRODUCT_KEY, product_key);
|
||||
IOT_Ioctl(IOTX_IOCTL_GET_DEVICE_NAME, device_name);
|
||||
|
||||
ret = HAL_GetFirmwareVersion(version);
|
||||
if (ret <= 0) {
|
||||
VERSION_ERR("firmware version does not implement");
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
|
||||
VERSION_DEBUG("firmware version report start in MQTT");
|
||||
|
||||
/* firmware report topic name generate */
|
||||
ret = HAL_Snprintf(topic_name,
|
||||
IOTX_URI_MAX_LEN,
|
||||
"/ota/device/inform/%s/%s",
|
||||
product_key,
|
||||
device_name
|
||||
);
|
||||
if (ret <= 0) {
|
||||
VERSION_ERR("firmware report topic generate err");
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
VERSION_DEBUG("firmware report topic: %s", topic_name);
|
||||
|
||||
/* firmware report message json data generate */
|
||||
|
||||
IOT_Ioctl(IOTX_IOCTL_GET_MODULE, module);
|
||||
if (strlen(module) == 0) {
|
||||
ret = HAL_Snprintf(msg,
|
||||
FIRMWARE_VERSION_MSG_LEN,
|
||||
"{\"id\":\"%d\",\"params\":{\"version\":\"%s\"}}",
|
||||
iotx_report_id(),
|
||||
version
|
||||
);
|
||||
} else {
|
||||
ret = HAL_Snprintf(msg,
|
||||
FIRMWARE_VERSION_MSG_LEN,
|
||||
"{\"id\":\"%d\",\"params\":{\"version\":\"%s\", \"module\":\"%s\"}}",
|
||||
iotx_report_id(),
|
||||
version,
|
||||
module
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if (ret <= 0) {
|
||||
VERSION_ERR("firmware report message json data generate err");
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
VERSION_DEBUG("firmware report data: %s", msg);
|
||||
|
||||
ret = info_report_func(pclient, topic_name, 1, msg, strlen(msg));
|
||||
|
||||
if (ret < 0) {
|
||||
VERSION_ERR("publish failed, ret = %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
VERSION_DEBUG("firmware version report finished, iotx_publish() = %d", ret);
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
/* report ModuleID */
|
||||
int iotx_report_mid(void *pclient)
|
||||
{
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include "infra_defs.h"
|
||||
|
||||
#ifndef _INFRA_REPORT_H_
|
||||
#define _INFRA_REPORT_H_
|
||||
|
||||
#ifdef BUILD_AOS
|
||||
#include "activation.h"
|
||||
#endif
|
||||
|
||||
#ifndef VERSION_NUM_SIZE
|
||||
#define VERSION_NUM_SIZE 4
|
||||
#endif
|
||||
|
||||
#ifndef RANDOM_NUM_SIZE
|
||||
#define RANDOM_NUM_SIZE 4
|
||||
#endif
|
||||
|
||||
#ifndef MAC_ADDRESS_SIZE
|
||||
#define MAC_ADDRESS_SIZE 8
|
||||
#endif
|
||||
|
||||
#ifndef CHIP_CODE_SIZE
|
||||
#define CHIP_CODE_SIZE 4
|
||||
#endif
|
||||
|
||||
#define AOS_ACTIVE_INFO_LEN (81)
|
||||
|
||||
/* activation device type */
|
||||
typedef enum {
|
||||
ACTIVE_SUBDEV, /* it's a subDevice */
|
||||
ACTIVE_SINGLE_GW /* it s a single or gateway device */
|
||||
} active_device_type_t;
|
||||
|
||||
/* activation system type */
|
||||
typedef enum {
|
||||
ACTIVE_LINKKIT_ONLY, /* only linkkit implement */
|
||||
ACTIVE_LINKKIT_AOS, /* both linkkit and AOS implement */
|
||||
ACTIVE_LINKKIT_OTHERS /* linkkit and 3-party OS implement */
|
||||
} active_system_type_t;
|
||||
|
||||
typedef int (*info_report_func_pt)(void *handle, const char *topic_name, int req_ack, void *data, int len);
|
||||
|
||||
#define MIDREPORT_PAYLOAD_LEN (62 + IOTX_PARTNER_ID_LEN + IOTX_MODULE_ID_LEN + 32 +1)
|
||||
#define MIDREPORT_REQID_LEN (IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 6)
|
||||
#define AOS_VERSON_MSG_LEN (256)
|
||||
#define LINKKIT_VERSION_MSG_LEN (192)
|
||||
#define FIRMWARE_VERSION_MSG_LEN (64 + IOTX_MODULE_LEN)
|
||||
|
||||
int iotx_report_id(void);
|
||||
int iotx_midreport_reqid(char *requestId, char *product_key, char *device_name);
|
||||
int iotx_midreport_payload(char *msg, char *requestId, char *mid, char *pid);
|
||||
int iotx_midreport_topic(char *topic_name, char *topic_head, char *product_key, char *device_name);
|
||||
|
||||
/* AOS version report API */
|
||||
int iotx_gen_aos_report_topic(char *topic_name, char *product_key, char *device_name);
|
||||
int iotx_gen_aos_report_payload(char *msg, int requestId, char *versionData);
|
||||
|
||||
void aos_get_version_hex(unsigned char version[VERSION_NUM_SIZE]);
|
||||
|
||||
void iotx_set_report_func(info_report_func_pt func);
|
||||
int iotx_report_devinfo(void *pclient);
|
||||
int iotx_report_mid(void *pclient);
|
||||
int iotx_report_firmware_version(void *pclient);
|
||||
|
||||
#endif
|
||||
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_SHA256
|
||||
|
||||
#define INFRA_SHA256_SMALLER
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "infra_sha256.h"
|
||||
|
||||
#define SHA256_KEY_IOPAD_SIZE (64)
|
||||
#define SHA256_DIGEST_SIZE (32)
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_UINT32_BE
|
||||
#define GET_UINT32_BE(n,b,i) \
|
||||
do { \
|
||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||
} while( 0 )
|
||||
#endif
|
||||
|
||||
#ifndef PUT_UINT32_BE
|
||||
#define PUT_UINT32_BE(n,b,i) \
|
||||
do { \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
} while( 0 )
|
||||
#endif
|
||||
|
||||
|
||||
static void utils_sha256_zeroize(void *v, uint32_t n)
|
||||
{
|
||||
volatile unsigned char *p = v;
|
||||
while (n--) {
|
||||
*p++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void utils_sha256_init(iot_sha256_context *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(iot_sha256_context));
|
||||
}
|
||||
|
||||
void utils_sha256_free(iot_sha256_context *ctx)
|
||||
{
|
||||
if (NULL == ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
utils_sha256_zeroize(ctx, sizeof(iot_sha256_context));
|
||||
}
|
||||
|
||||
void utils_sha256_starts(iot_sha256_context *ctx)
|
||||
{
|
||||
int is224 = 0;
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
if (is224 == 0) {
|
||||
/* SHA-256 */
|
||||
ctx->state[0] = 0x6A09E667;
|
||||
ctx->state[1] = 0xBB67AE85;
|
||||
ctx->state[2] = 0x3C6EF372;
|
||||
ctx->state[3] = 0xA54FF53A;
|
||||
ctx->state[4] = 0x510E527F;
|
||||
ctx->state[5] = 0x9B05688C;
|
||||
ctx->state[6] = 0x1F83D9AB;
|
||||
ctx->state[7] = 0x5BE0CD19;
|
||||
}
|
||||
|
||||
ctx->is224 = is224;
|
||||
}
|
||||
|
||||
static const uint32_t K[] = {
|
||||
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
|
||||
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
|
||||
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
|
||||
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
|
||||
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
|
||||
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
|
||||
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
|
||||
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
|
||||
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
|
||||
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
|
||||
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
|
||||
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
|
||||
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
|
||||
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
|
||||
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
|
||||
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
|
||||
};
|
||||
|
||||
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
|
||||
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
|
||||
|
||||
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
|
||||
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
|
||||
|
||||
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
|
||||
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
|
||||
|
||||
#define F0(x,y,z) ((x & y) | (z & (x | y)))
|
||||
#define F1(x,y,z) (z ^ (x & (y ^ z)))
|
||||
|
||||
#define R(t) \
|
||||
( \
|
||||
W[t] = S1(W[t - 2]) + W[t - 7] + \
|
||||
S0(W[t - 15]) + W[t - 16] \
|
||||
)
|
||||
|
||||
#define P(a,b,c,d,e,f,g,h,x,K) \
|
||||
{ \
|
||||
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
|
||||
temp2 = S2(a) + F0(a,b,c); \
|
||||
d += temp1; h = temp1 + temp2; \
|
||||
}
|
||||
|
||||
void utils_sha256_process(iot_sha256_context *ctx, const unsigned char data[64])
|
||||
{
|
||||
uint32_t temp1, temp2, W[64];
|
||||
uint32_t A[8];
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
A[i] = ctx->state[i];
|
||||
}
|
||||
|
||||
#if defined(INFRA_SHA256_SMALLER)
|
||||
for (i = 0; i < 64; i++) {
|
||||
if (i < 16) {
|
||||
GET_UINT32_BE(W[i], data, 4 * i);
|
||||
} else {
|
||||
R(i);
|
||||
}
|
||||
|
||||
P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i]);
|
||||
|
||||
temp1 = A[7];
|
||||
A[7] = A[6];
|
||||
A[6] = A[5];
|
||||
A[5] = A[4];
|
||||
A[4] = A[3];
|
||||
A[3] = A[2];
|
||||
A[2] = A[1];
|
||||
A[1] = A[0];
|
||||
A[0] = temp1;
|
||||
}
|
||||
#else /* INFRA_SHA256_SMALLER */
|
||||
for (i = 0; i < 16; i++) {
|
||||
GET_UINT32_BE(W[i], data, 4 * i);
|
||||
}
|
||||
|
||||
for (i = 0; i < 16; i += 8) {
|
||||
P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i + 0], K[i + 0]);
|
||||
P(A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i + 1], K[i + 1]);
|
||||
P(A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i + 2], K[i + 2]);
|
||||
P(A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i + 3], K[i + 3]);
|
||||
P(A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i + 4], K[i + 4]);
|
||||
P(A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i + 5], K[i + 5]);
|
||||
P(A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i + 6], K[i + 6]);
|
||||
P(A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i + 7], K[i + 7]);
|
||||
}
|
||||
|
||||
for (i = 16; i < 64; i += 8) {
|
||||
P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i + 0), K[i + 0]);
|
||||
P(A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i + 1), K[i + 1]);
|
||||
P(A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i + 2), K[i + 2]);
|
||||
P(A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i + 3), K[i + 3]);
|
||||
P(A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i + 4), K[i + 4]);
|
||||
P(A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i + 5), K[i + 5]);
|
||||
P(A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i + 6), K[i + 6]);
|
||||
P(A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i + 7), K[i + 7]);
|
||||
}
|
||||
#endif /* INFRA_SHA256_SMALLER */
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
ctx->state[i] += A[i];
|
||||
}
|
||||
}
|
||||
void utils_sha256_update(iot_sha256_context *ctx, const unsigned char *input, uint32_t ilen)
|
||||
{
|
||||
size_t fill;
|
||||
uint32_t left;
|
||||
|
||||
if (ilen == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += (uint32_t) ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if (ctx->total[0] < (uint32_t) ilen) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
if (left && ilen >= fill) {
|
||||
memcpy((void *)(ctx->buffer + left), input, fill);
|
||||
utils_sha256_process(ctx, ctx->buffer);
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while (ilen >= 64) {
|
||||
utils_sha256_process(ctx, input);
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if (ilen > 0) {
|
||||
memcpy((void *)(ctx->buffer + left), input, ilen);
|
||||
}
|
||||
}
|
||||
|
||||
static const unsigned char sha256_padding[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
void utils_sha256_finish(iot_sha256_context *ctx, uint8_t output[32])
|
||||
{
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = (ctx->total[0] >> 29)
|
||||
| (ctx->total[1] << 3);
|
||||
low = (ctx->total[0] << 3);
|
||||
|
||||
PUT_UINT32_BE(high, msglen, 0);
|
||||
PUT_UINT32_BE(low, msglen, 4);
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = (last < 56) ? (56 - last) : (120 - last);
|
||||
|
||||
utils_sha256_update(ctx, sha256_padding, padn);
|
||||
utils_sha256_update(ctx, msglen, 8);
|
||||
|
||||
PUT_UINT32_BE(ctx->state[0], output, 0);
|
||||
PUT_UINT32_BE(ctx->state[1], output, 4);
|
||||
PUT_UINT32_BE(ctx->state[2], output, 8);
|
||||
PUT_UINT32_BE(ctx->state[3], output, 12);
|
||||
PUT_UINT32_BE(ctx->state[4], output, 16);
|
||||
PUT_UINT32_BE(ctx->state[5], output, 20);
|
||||
PUT_UINT32_BE(ctx->state[6], output, 24);
|
||||
|
||||
if (ctx->is224 == 0) {
|
||||
PUT_UINT32_BE(ctx->state[7], output, 28);
|
||||
}
|
||||
}
|
||||
|
||||
void utils_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32])
|
||||
{
|
||||
iot_sha256_context ctx;
|
||||
|
||||
utils_sha256_init(&ctx);
|
||||
utils_sha256_starts(&ctx);
|
||||
utils_sha256_update(&ctx, input, ilen);
|
||||
utils_sha256_finish(&ctx, output);
|
||||
utils_sha256_free(&ctx);
|
||||
}
|
||||
|
||||
void utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key, uint32_t key_len, uint8_t output[32])
|
||||
{
|
||||
iot_sha256_context context;
|
||||
uint8_t k_ipad[SHA256_KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
|
||||
uint8_t k_opad[SHA256_KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
|
||||
int32_t i;
|
||||
|
||||
if ((NULL == msg) || (NULL == key) || (NULL == output)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (key_len > SHA256_KEY_IOPAD_SIZE) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* start out by storing key in pads */
|
||||
memset(k_ipad, 0, sizeof(k_ipad));
|
||||
memset(k_opad, 0, sizeof(k_opad));
|
||||
memcpy(k_ipad, key, key_len);
|
||||
memcpy(k_opad, key, key_len);
|
||||
|
||||
/* XOR key with ipad and opad values */
|
||||
for (i = 0; i < SHA256_KEY_IOPAD_SIZE; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
/* perform inner SHA */
|
||||
utils_sha256_init(&context); /* init context for 1st pass */
|
||||
utils_sha256_starts(&context); /* setup context for 1st pass */
|
||||
utils_sha256_update(&context, k_ipad, SHA256_KEY_IOPAD_SIZE); /* start with inner pad */
|
||||
utils_sha256_update(&context, msg, msg_len); /* then text of datagram */
|
||||
utils_sha256_finish(&context, output); /* finish up 1st pass */
|
||||
|
||||
/* perform outer SHA */
|
||||
utils_sha256_init(&context); /* init context for 2nd pass */
|
||||
utils_sha256_starts(&context); /* setup context for 2nd pass */
|
||||
utils_sha256_update(&context, k_opad, SHA256_KEY_IOPAD_SIZE); /* start with outer pad */
|
||||
utils_sha256_update(&context, output, SHA256_DIGEST_SIZE); /* then results of 1st hash */
|
||||
utils_sha256_finish(&context, output); /* finish up 2nd pass */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _IOTX_COMMON_SHA256_H_
|
||||
#define _IOTX_COMMON_SHA256_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define SHA256_DIGEST_LENGTH (32)
|
||||
#define SHA256_BLOCK_LENGTH (64)
|
||||
#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
|
||||
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
|
||||
|
||||
/**
|
||||
* \brief SHA-256 context structure
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t total[2]; /*!< number of bytes processed */
|
||||
uint32_t state[8]; /*!< intermediate digest state */
|
||||
unsigned char buffer[64]; /*!< data block being processed */
|
||||
int is224; /*!< 0 => SHA-256, else SHA-224 */
|
||||
} iot_sha256_context;
|
||||
|
||||
typedef union {
|
||||
char sptr[8];
|
||||
uint64_t lint;
|
||||
} u_retLen;
|
||||
|
||||
/**
|
||||
* \brief Initialize SHA-256 context
|
||||
*
|
||||
* \param ctx SHA-256 context to be initialized
|
||||
*/
|
||||
void utils_sha256_init(iot_sha256_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief Clear SHA-256 context
|
||||
*
|
||||
* \param ctx SHA-256 context to be cleared
|
||||
*/
|
||||
void utils_sha256_free(iot_sha256_context *ctx);
|
||||
|
||||
|
||||
/**
|
||||
* \brief SHA-256 context setup
|
||||
*
|
||||
* \param ctx context to be initialized
|
||||
*/
|
||||
void utils_sha256_starts(iot_sha256_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief SHA-256 process buffer
|
||||
*
|
||||
* \param ctx SHA-256 context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void utils_sha256_update(iot_sha256_context *ctx, const unsigned char *input, uint32_t ilen);
|
||||
|
||||
/**
|
||||
* \brief SHA-256 final digest
|
||||
*
|
||||
* \param ctx SHA-256 context
|
||||
* \param output SHA-256 checksum result
|
||||
*/
|
||||
void utils_sha256_finish(iot_sha256_context *ctx, uint8_t output[32]);
|
||||
|
||||
/* Internal use */
|
||||
void utils_sha256_process(iot_sha256_context *ctx, const unsigned char data[64]);
|
||||
|
||||
/**
|
||||
* \brief Output = SHA-256( input buffer )
|
||||
*
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output SHA-256 checksum result
|
||||
*/
|
||||
void utils_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32]);
|
||||
|
||||
void utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key, uint32_t key_len, uint8_t output[32]);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
+657
@@ -0,0 +1,657 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef __INFRA_STATE_H__
|
||||
#define __INFRA_STATE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define STATE_BASE (0x0000)
|
||||
|
||||
/* General: 0x0000 ~ 0x00FF */
|
||||
/* API works as expected and returns sucess */
|
||||
/* API 接口按预期完成工作, 并返回成功 */
|
||||
#define STATE_SUCCESS (STATE_BASE - 0x0000)
|
||||
|
||||
/* General: 0x0000 ~ 0x00FF */
|
||||
|
||||
/* User Input: 0x0100 ~ 0x01FF */
|
||||
#define STATE_USER_INPUT_BASE (-0x0100)
|
||||
|
||||
/* User input parameters contain unacceptable NULL pointer */
|
||||
/* 用户传递给API的参数中含有不合理的空指针参数 */
|
||||
#define STATE_USER_INPUT_NULL_POINTER (STATE_USER_INPUT_BASE - 0x0001)
|
||||
/* Some user input parameter(s) has value out of acceptable range */
|
||||
/* 用户传递给API的参数中含有超出合理范围的取值 */
|
||||
#define STATE_USER_INPUT_OUT_RANGE (STATE_USER_INPUT_BASE - 0x0002)
|
||||
/* User input parameters contain unacceptable productKey */
|
||||
/* 用户传递给API的参数中含有不合法的产品标识(productKey) */
|
||||
#define STATE_USER_INPUT_PK (STATE_USER_INPUT_BASE - 0x0003)
|
||||
/* User input parameters contain unacceptable productSecret */
|
||||
/* 用户传递给API的参数中含有不合法的产品密钥(productSecret) */
|
||||
#define STATE_USER_INPUT_PS (STATE_USER_INPUT_BASE - 0x0004)
|
||||
/* User input parameters contain unacceptable deviceName */
|
||||
/* 用户传递给API的参数中含有不合法的设备名称(deviceName) */
|
||||
#define STATE_USER_INPUT_DN (STATE_USER_INPUT_BASE - 0x0005)
|
||||
/* User input parameters contain unacceptable deviceSecret */
|
||||
/* 用户传递给API的参数中含有不合法的设备密钥(deviceSecret) */
|
||||
#define STATE_USER_INPUT_DS (STATE_USER_INPUT_BASE - 0x0006)
|
||||
/* User input parameters contain unacceptable HTTP domain name */
|
||||
/* 用户传递给API的参数中含有不合理的域名, SDK无法对其完成HTTP连接 */
|
||||
#define STATE_USER_INPUT_HTTP_DOMAIN (STATE_USER_INPUT_BASE - 0x0007)
|
||||
/* User input parameters contain unacceptable MQTT domain name */
|
||||
/* 用户传递给API的参数中含有不合理的域名, SDK无法对其完成MQTT连接 */
|
||||
#define STATE_USER_INPUT_MQTT_DOMAIN (STATE_USER_INPUT_BASE - 0x0008)
|
||||
/* User input parameters contain unacceptable HTTP port */
|
||||
/* 用户传递给API的参数中含有不合理的HTTP目的端口号 */
|
||||
#define STATE_USER_INPUT_HTTP_PORT (STATE_USER_INPUT_BASE - 0x0009)
|
||||
/* User input parameters contain unacceptable HTTP timeout */
|
||||
/* 用户传递给API的参数中含有不合理的HTTP超时时间 */
|
||||
#define STATE_USER_INPUT_HTTP_TIMEOUT (STATE_USER_INPUT_BASE - 0x000A)
|
||||
/* User input parameters contain unacceptable HTTP options */
|
||||
/* 用户传递给API的参数中含有不合理的HTTP连接选项 */
|
||||
#define STATE_USER_INPUT_HTTP_OPTION (STATE_USER_INPUT_BASE - 0x000B)
|
||||
/* User input parameters contain unacceptable post data */
|
||||
/* 用户传递给API的参数中, 对HTTP POST含有不合理的报文描述, 如长度为负或空指针等 */
|
||||
#define STATE_USER_INPUT_HTTP_POST_DATA (STATE_USER_INPUT_BASE - 0x000C)
|
||||
/* User input parameters contain unacceptable HTTP URL */
|
||||
/* 用户传递给API的参数中含有不合理的HTTP目的URL */
|
||||
#define STATE_USER_INPUT_HTTP_URL (STATE_USER_INPUT_BASE - 0x000D)
|
||||
/* User input parameters contain unacceptable HTTP path */
|
||||
/* 用户传递给API的参数中含有不合理的HTTP资源路径 */
|
||||
#define STATE_USER_INPUT_HTTP_PATH (STATE_USER_INPUT_BASE - 0x000E)
|
||||
/* User input parameters contain unacceptable PK/PS/DN/DS */
|
||||
/* 用户传递给API的参数中含有不合理的产品标识/密钥或设备名称/密钥 */
|
||||
#define STATE_USER_INPUT_META_INFO (STATE_USER_INPUT_BASE - 0x000F)
|
||||
/* User input parameters contain unacceptable deviceID */
|
||||
/* 用户传递给API的参数中含有不合理的 deviceID 设备标识符 */
|
||||
#define STATE_USER_INPUT_DEVID (STATE_USER_INPUT_BASE - 0x0010)
|
||||
/* User input parameters contain unacceptable device type */
|
||||
/* 用户传递给API的参数中含有不合理的设备类型, 既不是master又不是slave */
|
||||
#define STATE_USER_INPUT_DEVICE_TYPE (STATE_USER_INPUT_BASE - 0x0011)
|
||||
/* User input parameters contain unacceptable message type */
|
||||
/* 用户传递给 IOT_Linkkit_Report() 等API的参数中含有不合理的消息类型 */
|
||||
#define STATE_USER_INPUT_MSG_TYPE (STATE_USER_INPUT_BASE - 0x0012)
|
||||
/* User input parameters contain unacceptable value */
|
||||
/* 用户传递给API的参数中含有其它不合理的取值 */
|
||||
#define STATE_USER_INPUT_INVALID (STATE_USER_INPUT_BASE - 0x0013)
|
||||
/* User input parameters contain unacceptable deviceRegistration */
|
||||
/* 用户传递给API的参数中含有不合法的设备注册码(deviceRegistration) */
|
||||
#define STATE_USER_INPUT_DR (STATE_USER_INPUT_BASE - 0x0014)
|
||||
/* User Input: 0x0100 ~ 0x01FF */
|
||||
|
||||
/* System: 0x0200 ~ 0x02FF */
|
||||
#define STATE_SYS_DEPEND_BASE (-0x0200)
|
||||
|
||||
/* SDK run into exception when invoking HAL_Malloc() */
|
||||
/* SDK调用的系统适配接口 HAL_Malloc() 返回异常, 未能成功分配内存 */
|
||||
#define STATE_SYS_DEPEND_MALLOC (STATE_SYS_DEPEND_BASE - 0x0001)
|
||||
/* SDK run into exception when invoking HAL_Kv_Get() */
|
||||
/* SDK调用的系统适配接口 HAL_Kv_Get() 返回异常, 未能成功根据Key获取Value */
|
||||
#define STATE_SYS_DEPEND_KV_GET (STATE_SYS_DEPEND_BASE - 0x0002)
|
||||
/* SDK run into exception when invoking HAL_Kv_Set() */
|
||||
/* SDK调用的系统适配接口 HAL_Kv_Set() 返回异常, 未能成功根据Key写入Value */
|
||||
#define STATE_SYS_DEPEND_KV_SET (STATE_SYS_DEPEND_BASE - 0x0003)
|
||||
/* SDK run into exception when invoking HAL_Kv_Del() */
|
||||
/* SDK调用的系统适配接口 HAL_Kv_Del() 返回异常, 未能成功根据Key删除KV对 */
|
||||
#define STATE_SYS_DEPEND_KV_DELETE (STATE_SYS_DEPEND_BASE - 0x0004)
|
||||
/* SDK run into exception when invoking HAL_MutexCreate() */
|
||||
/* SDK调用的系统适配接口 HAL_MutexCreate() 返回异常, 未能创建一个互斥锁 */
|
||||
#define STATE_SYS_DEPEND_MUTEX_CREATE (STATE_SYS_DEPEND_BASE - 0x0005)
|
||||
/* SDK run into exception when invoking HAL_MutexLock() */
|
||||
/* SDK调用的系统适配接口 HAL_MutexLock() 返回异常, 未能成功申请互斥锁 */
|
||||
#define STATE_SYS_DEPEND_MUTEX_LOCK (STATE_SYS_DEPEND_BASE - 0x0006)
|
||||
/* SDK run into exception when invoking HAL_MutexUnlock() */
|
||||
/* SDK调用的系统适配接口 HAL_MutexUnlock() 返回异常, 未能成功释放互斥锁 */
|
||||
#define STATE_SYS_DEPEND_MUTEX_UNLOCK (STATE_SYS_DEPEND_BASE - 0x0007)
|
||||
/* SDK run into exception when TX or RX through lower network layer */
|
||||
/* SDK调用的系统适配接口 HAL_TCP_Read|Write() 或 HAL_SSL_Read|Write() 返回异常, 底层连接已关闭而未能成功进行网络收发 */
|
||||
#define STATE_SYS_DEPEND_NWK_CLOSE (STATE_SYS_DEPEND_BASE - 0x0008)
|
||||
/* SDK run into timeout when TX or RX through lower network layer */
|
||||
/* SDK调用的系统适配接口 HAL_TCP_Read() 或 HAL_SSL_Read() 未能成功在预期的时间内得到网络回应 */
|
||||
#define STATE_SYS_DEPEND_NWK_TIMEOUT (STATE_SYS_DEPEND_BASE - 0x0009)
|
||||
/* SDK run into invalid handler when lookup network lower layer connection */
|
||||
/* SDK进行底层网络收发通信时, 看到系统对TCP连接或TLS连接的标识句柄是非法的 */
|
||||
#define STATE_SYS_DEPEND_NWK_INVALID_HANDLE (STATE_SYS_DEPEND_BASE - 0x000A)
|
||||
/* SDK run into exception when RX through lower network layer */
|
||||
/* SDK调用的系统适配接口 HAL_TCP_Read() 或 HAL_SSL_Read() 返回异常, 未能成功读取一段内容 */
|
||||
#define STATE_SYS_DEPEND_NWK_READ_ERROR (STATE_SYS_DEPEND_BASE - 0x000B)
|
||||
/* SDK run into exception when invoking HAL_SemaphoreCreate() */
|
||||
/* SDK调用的系统适配接口 HAL_SemaphoreCreate() 返回异常, 未能成功申请信号量 */
|
||||
#define STATE_SYS_DEPEND_SEMAPHORE_CREATE (STATE_SYS_DEPEND_BASE - 0x000C)
|
||||
/* SDK run into exception when invoking HAL_SemaphoreWait() */
|
||||
/* SDK调用的系统适配接口 HAL_SemaphoreWait() 返回异常, 未能成功在信号量上睡眠 */
|
||||
#define STATE_SYS_DEPEND_SEMAPHORE_WAIT (STATE_SYS_DEPEND_BASE - 0x000D)
|
||||
/* SDK run into exception when invoking HAL_Snprintf() */
|
||||
/* SDK调用的系统适配接口 HAL_Snprintf() 返回异常, 未能成功拼接格式化字符串 */
|
||||
#define STATE_SYS_DEPEND_SNPRINTF (STATE_SYS_DEPEND_BASE - 0x000E)
|
||||
/* SDK run into exception when invoking HAL_Firmware_Persistence_Write() */
|
||||
/* SDK调用的系统适配接口 HAL_Firmware_Persistence_Write() 返回异常, 未能成功将固件写入ROM */
|
||||
#define STATE_SYS_DEPEND_FIRMWAIRE_WIRTE (STATE_SYS_DEPEND_BASE - 0x000F)
|
||||
|
||||
/* System: 0x0200 ~ 0x02FF */
|
||||
|
||||
/* MQTT: 0x0300 ~ 0x03FF */
|
||||
#define STATE_MQTT_BASE (-0x0300)
|
||||
|
||||
/* Deserialized CONNACK from MQTT server says protocol version is unacceptable */
|
||||
/* 设备发出MQTT建连请求后, 服务器应答的报文表示不能接受请求中的MQTT协议版本 */
|
||||
#define STATE_MQTT_CONNACK_VERSION_UNACCEPT (STATE_MQTT_BASE - 0x0001)
|
||||
/* Deserialized CONNACK from MQTT server says identifier is rejected */
|
||||
/* 设备发出MQTT建连请求后, 服务器应答的报文表示不能接受请求中的设备标识符 */
|
||||
#define STATE_MQTT_CONNACK_IDENT_REJECT (STATE_MQTT_BASE - 0x0002)
|
||||
/* Deserialized CONNACK from MQTT server says service is not available */
|
||||
/* 设备发出MQTT建连请求后, 服务器应答的报文表示当前MQTT服务不可用 */
|
||||
#define STATE_MQTT_CONNACK_SERVICE_NA (STATE_MQTT_BASE - 0x0003)
|
||||
/* Deserialized CONNACK from MQTT server says it failed to authorize */
|
||||
/* 设备发出MQTT建连请求后, 服务器应答的报文表示当前对请求设备未能完成鉴权 */
|
||||
#define STATE_MQTT_CONNACK_NOT_AUTHORIZED (STATE_MQTT_BASE - 0x0004)
|
||||
/* Deserialized CONNACK from MQTT server says username/password is invalid */
|
||||
/* 设备发出MQTT建连请求后, 服务器应答的报文表示对设备身份鉴权不通过 */
|
||||
#define STATE_MQTT_CONNACK_BAD_USERDATA (STATE_MQTT_BASE - 0x0005)
|
||||
/* Skip current reconnect attemption until next timer expiration */
|
||||
/* 跳过当前的重连动作, 因为上一次的重连过去的时间太短, 稍晚再进行连接 */
|
||||
#define STATE_MQTT_WAIT_RECONN_TIMER (STATE_MQTT_BASE - 0x0006)
|
||||
/* Reserved buffer is too short when generate device signature for hostname */
|
||||
/* 计算设备签名时, 为域名预留的字符串缓冲区太短, 不足容纳输出内容 */
|
||||
#define STATE_MQTT_SIGN_HOSTNAME_BUF_SHORT (STATE_MQTT_BASE - 0x0007)
|
||||
/* Reserved buffer is too short when generate device signature for username */
|
||||
/* 计算设备签名时, 为MQTT的用户名预留的字符串缓冲区太短, 不足容纳输出内容 */
|
||||
#define STATE_MQTT_SIGN_USERNAME_BUF_SHORT (STATE_MQTT_BASE - 0x0008)
|
||||
/* Reserved buffer is too short when generate device signature for clientId */
|
||||
/* 计算设备签名时, 为MQTT的clientId预留的字符串缓冲区太短, 不足容纳输出内容 */
|
||||
#define STATE_MQTT_SIGN_CLIENTID_BUF_SHORT (STATE_MQTT_BASE - 0x0009)
|
||||
/* Reserved buffer is too short for signature generate source */
|
||||
/* 计算设备签名时, 为拼接签名来源内容预留的字符串缓冲区太短, 不足容纳输出内容 */
|
||||
#define STATE_MQTT_SIGN_SOURCE_BUF_SHORT (STATE_MQTT_BASE - 0x000A)
|
||||
/* SDK run into exception when invoking lower layer wrapper_mqtt_init() */
|
||||
/* SDK调用的MQTT建连底层接口 wrapper_mqtt_init() 异常, 初始化失败 */
|
||||
#define STATE_MQTT_WRAPPER_INIT_FAIL (STATE_MQTT_BASE - 0x000B)
|
||||
/* Failed to serialize connect request */
|
||||
/* 序列化MQTT建连请求报文异常 */
|
||||
#define STATE_MQTT_SERIALIZE_CONN_ERROR (STATE_MQTT_BASE - 0x000C)
|
||||
/* Failed to serialize acknowledge message of publish */
|
||||
/* 序列化对服务器下推报文的应答消息异常 */
|
||||
#define STATE_MQTT_SERIALIZE_PUBACK_ERROR (STATE_MQTT_BASE - 0x000D)
|
||||
/* Failed to serialize ping request */
|
||||
/* 序列化MQTT心跳请求报文异常 */
|
||||
#define STATE_MQTT_SERIALIZE_PINGREQ_ERROR (STATE_MQTT_BASE - 0x000E)
|
||||
/* Failed to serialize subscribe request */
|
||||
/* 序列化MQTT订阅请求报文异常 */
|
||||
#define STATE_MQTT_SERIALIZE_SUB_ERROR (STATE_MQTT_BASE - 0x000F)
|
||||
/* Failed to serialize unsubscribe request */
|
||||
/* 序列化MQTT取消订阅请求报文异常 */
|
||||
#define STATE_MQTT_SERIALIZE_UNSUB_ERROR (STATE_MQTT_BASE - 0x0010)
|
||||
/* Failed to serialize publish message */
|
||||
/* 序列化MQTT上报消息报文异常 */
|
||||
#define STATE_MQTT_SERIALIZE_PUB_ERROR (STATE_MQTT_BASE - 0x0011)
|
||||
/* Failed to deserialize connect response */
|
||||
/* 反序列化MQTT服务器返回的建连应答报文异常 */
|
||||
#define STATE_MQTT_DESERIALIZE_CONNACK_ERROR (STATE_MQTT_BASE - 0x0012)
|
||||
/* Failed to deserialize subscribe response */
|
||||
/* 反序列化MQTT服务器返回的订阅应答报文异常 */
|
||||
#define STATE_MQTT_DESERIALIZE_SUBACK_ERROR (STATE_MQTT_BASE - 0x0013)
|
||||
/* Failed to deserialize publish response */
|
||||
/* 反序列化MQTT服务器下推的消息报文异常 */
|
||||
#define STATE_MQTT_DESERIALIZE_PUB_ERROR (STATE_MQTT_BASE - 0x0014)
|
||||
/* Failed to deserialize unsubscribe response */
|
||||
/* 反序列化MQTT服务器返回的取消订阅应答报文异常 */
|
||||
#define STATE_MQTT_DESERIALIZE_UNSUBACK_ERROR (STATE_MQTT_BASE - 0x0015)
|
||||
/* Failed to read MQTT packet from network */
|
||||
/* 未能成功从网络上读取服务器下发的MQTT报文内容 */
|
||||
#define STATE_MQTT_PACKET_READ_ERROR (STATE_MQTT_BASE - 0x0016)
|
||||
/* Failed to interpret CONNACK from MQTT server */
|
||||
/* 未能成功解析从MQTT服务器返回的建连请求应答 */
|
||||
#define STATE_MQTT_CONNACK_UNKNOWN_ERROR (STATE_MQTT_BASE - 0x0017)
|
||||
/* Reserved buffer is too short when retrieve content from network */
|
||||
/* 从网络协议栈读取MQTT下推报文时, 预留的缓冲区太短, 不足容纳待收取内容 */
|
||||
#define STATE_MQTT_RX_BUFFER_TOO_SHORT (STATE_MQTT_BASE - 0x0018)
|
||||
/* Reserved buffer is too short when compose content going to network */
|
||||
/* 组装MQTT上报消息准备发给网络协议栈时, 为报文内容预留的缓冲区太短, 不足容纳待发送内容 */
|
||||
#define STATE_MQTT_TX_BUFFER_TOO_SHORT (STATE_MQTT_BASE - 0x0019)
|
||||
/* Reserved buffer is too short when compose topic for MQTT outgoing message */
|
||||
/* 组装Topic准备进行MQTT发布或者订阅时, 为Topic字段预留的缓冲区太短, 不足容纳组装内容 */
|
||||
#define STATE_MQTT_TOPIC_BUF_TOO_SHORT (STATE_MQTT_BASE - 0x001A)
|
||||
/* Retried time exceeds maximum when perform IOT_MQTT_Construct() */
|
||||
/* 单独的一次 IOT_MQTT_Construct() 接口工作中, 已多次重试仍未连上服务端, 停止尝试返回 */
|
||||
#define STATE_MQTT_CONN_RETRY_EXCEED_MAX (STATE_MQTT_BASE - 0x001B)
|
||||
/* Re-publish QoS1 message exceeds maximum */
|
||||
/* 对QoS1类型的MQTT上报消息已多次重试, 仍未得到服务端收到的回应, 放弃重新发送 */
|
||||
#define STATE_MQTT_QOS1_REPUB_EXCEED_MAX (STATE_MQTT_BASE - 0x001C)
|
||||
/* Invalid QoS input when publish MQTT message */
|
||||
/* 对MQTT消息上报时, 遇到不合理的QoS参数取值 */
|
||||
#define STATE_MQTT_PUB_QOS_INVALID (STATE_MQTT_BASE - 0x001D)
|
||||
/* Skip current action because MQTT connection is break */
|
||||
/* 因为当前MQTT连接已断开, 跳过当前可能的发送或接收动作 */
|
||||
#define STATE_MQTT_IN_OFFLINE_STATUS (STATE_MQTT_BASE - 0x001E)
|
||||
/* Receive MQTT packet from network can not be interpret */
|
||||
/* 从网络上收取到的MQTT报文, 无法解析其报文类型 */
|
||||
#define STATE_MQTT_RECV_UNKNOWN_PACKET (STATE_MQTT_BASE - 0x001F)
|
||||
/* MQTT connection instance exceed allowed maximum */
|
||||
/* 当前已创建的MQTT连接数量超出限制, 已无法再新建连接 */
|
||||
#define STATE_MQTT_CLI_EXCEED_MAX (STATE_MQTT_BASE - 0x0020)
|
||||
/* MQTT subscribe instance exceed allowed maximum */
|
||||
/* 当前已订阅的MQTT主题数量超出限制, 已无法再新建订阅 */
|
||||
#define STATE_MQTT_SUB_EXCEED_MAX (STATE_MQTT_BASE - 0x0021)
|
||||
/* Invalid MQTT topic can not be interpret */
|
||||
/* 遇到不合理的MQTT主题格式, 无法进行订阅或发布等动作 */
|
||||
#define STATE_MQTT_UNEXP_TOPIC_FORMAT (STATE_MQTT_BASE - 0x0022)
|
||||
/* Wait too long time after syncronized subscribe request sent */
|
||||
/* 进行同步的订阅请求时, 已超出限定时长而未得到服务端回应 */
|
||||
#define STATE_MQTT_SYNC_SUB_TIMEOUT (STATE_MQTT_BASE - 0x0023)
|
||||
/* Last connect request has not been responsed in async protocol stack */
|
||||
/* 在异步协议栈上工作时, 上次的连接请求尚未得到服务端回应, 跳过当前连接请求 */
|
||||
#define STATE_MQTT_ASYNC_STACK_CONN_IN_PROG (STATE_MQTT_BASE - 0x0024)
|
||||
/* Currrent SDK configuration does not support working with async protocol stack */
|
||||
/* 当前SDK未被配置为支持在异步协议栈上工作 */
|
||||
#define STATE_MQTT_ASYNC_STACK_NOT_SUPPORT (STATE_MQTT_BASE - 0x0025)
|
||||
/* Got unknown event when work with async protocol stack */
|
||||
/* SDK在异步协议栈上工作时, 接收到来自协议栈的事件, 但不能理解 */
|
||||
#define STATE_MQTT_ASYNC_STACK_UNKNOWN_EVENT (STATE_MQTT_BASE - 0x0026)
|
||||
/* Report connect relative parameters such as username and password */
|
||||
/* MQTT发起连接的过程中, 上报连接相关参数如用户名和密码等 */
|
||||
#define STATE_MQTT_CONN_USER_INFO (STATE_MQTT_BASE - 0x0027)
|
||||
/* Report subscribe relative parameters such as topic string */
|
||||
/* MQTT发起订阅的过程中, 上报订阅相关参数如Topic等 */
|
||||
#define STATE_MQTT_SUB_INFO (STATE_MQTT_BASE - 0x0028)
|
||||
/* Report publish relative parameters such as topic string */
|
||||
/* MQTT发布上报的过程中, 上报发布相关参数如Topic等 */
|
||||
#define STATE_MQTT_PUB_INFO (STATE_MQTT_BASE - 0x0029)
|
||||
|
||||
/* MQTT: 0x0300 ~ 0x03FF */
|
||||
|
||||
/* WiFi Provision: 0x0400 ~ 0x04FF */
|
||||
#define STATE_WIFI_BASE (-0x0400)
|
||||
/* WiFi provision got enabled since user press switch button */
|
||||
/* WiFi配网进入使能状态 */
|
||||
#define STATE_WIFI_ENABLE_AWSS (STATE_WIFI_BASE - 0x0001)
|
||||
/* WiFi L2 frame is being processed */
|
||||
/* WiFi配网正在处理1个2层报文 */
|
||||
#define STATE_WIFI_PROCESS_FRAME (STATE_WIFI_BASE - 0x0002)
|
||||
/* WIFi channel is being operated */
|
||||
/* WiFi配网正在进行对信道进行切换/锁定等操作 */
|
||||
#define STATE_WIFI_CHAN_SCAN (STATE_WIFI_BASE - 0x0003)
|
||||
/* Successfully connect to specified SSID with decrypted password */
|
||||
/* 已使用WiFi配网接收到的密码, 连接WiFi热点成功 */
|
||||
#define STATE_WIFI_CONNECT_AP_SUCCESS (STATE_WIFI_BASE - 0x0004)
|
||||
/* Failed connecting specified SSID with decrypted password */
|
||||
/* 已使用WiFi配网接收到的密码, 连接WiFi热点失败 */
|
||||
#define STATE_WIFI_CONNECT_AP_FAILED (STATE_WIFI_BASE - 0x0005)
|
||||
/* Should not start new round WiFi provision since last round in progress */
|
||||
/* 不应启动新的一轮WiFi配网, 因为上一轮仍未结束 */
|
||||
#define STATE_WIFI_IN_PROGRESS (STATE_WIFI_BASE - 0x0006)
|
||||
/* WiFi provision stopped on request */
|
||||
/* 已按用户调用API的指令, 停止了WiFi配网进程 */
|
||||
#define STATE_WIFI_FORCE_STOPPED (STATE_WIFI_BASE - 0x0007)
|
||||
/* WiFi provision is releasing resources */
|
||||
/* 正在释放WiFi配网所占用的内部资源 */
|
||||
#define STATE_WIFI_DESTROY (STATE_WIFI_BASE - 0x0008)
|
||||
/* WiFi provision successfully detect hint frame */
|
||||
/* WiFi配网功能已成功监听到关键信息起始标志帧 */
|
||||
#define STATE_WIFI_GOT_HINT_FRAME (STATE_WIFI_BASE - 0x0009)
|
||||
/* WiFi provision interpret information encrypt type */
|
||||
/* WiFi配网中发现关键信息的加密方式是一型一密或一机一密等 */
|
||||
#define STATE_WIFI_ENCRYPT_TYPE (STATE_WIFI_BASE - 0x000A)
|
||||
/* WiFi provision ran into mismatch CRC */
|
||||
/* WiFi配网中发现报文的校验和不符合预期 */
|
||||
#define STATE_WIFI_CRC_ERROR (STATE_WIFI_BASE - 0x000B)
|
||||
/* WiFi provision failed to decrypt WiFi password */
|
||||
/* WiFi配网中解密WiFi热点的密码失败 */
|
||||
#define STATE_WIFI_PASSWD_DECODE_FAILED (STATE_WIFI_BASE - 0x000C)
|
||||
/* WiFi provision decrypt WiFi password successfully */
|
||||
/* WiFi配网中解密WiFi热点的密码成功 */
|
||||
#define STATE_WIFI_PASSWD_DECODE_SUCCESS (STATE_WIFI_BASE - 0x000D)
|
||||
/* WiFi configuration statistics report, including overall time, pack number, pack lens */
|
||||
/* WiFi配网中的统计信息, 比如收到包的数量, 配网时间, 收包报文的总长度*/
|
||||
#define STATE_WIFI_STATISTIC (STATE_WIFI_BASE - 0x000E)
|
||||
/* Unexpected version info from cellphone when wifi-provision */
|
||||
/* 手机端发来的配网协议的版本号不符合预期 */
|
||||
#define STATE_WIFI_UNEXP_PROT_VERSION (STATE_WIFI_BASE - 0x000F)
|
||||
/* Debug info during multicast wifi-provision */
|
||||
/* WiFi组播配网中的调试信息 */
|
||||
#define STATE_WIFI_MCAST_DEBUG (STATE_WIFI_BASE - 0x0010)
|
||||
/* Debug info during broadcast wifi-provision */
|
||||
/* WiFi包长编码的一键配网中的调试信息 */
|
||||
#define STATE_WIFI_BCAST_DEBUG (STATE_WIFI_BASE - 0x0011)
|
||||
/* Debug info during zero-config wifi-provision for enrollee */
|
||||
/* WiFi零配过程中的待配方调试信息 */
|
||||
#define STATE_WIFI_ZCONFIG_ENROLLEE_DEBUG (STATE_WIFI_BASE - 0x0012)
|
||||
/* The params from registar in zeroconfig Wifi Configuration is invalid */
|
||||
/* WiFi零配过程中的主配方发来的参数有误 */
|
||||
#define STATE_WIFI_ZCONFIG_REGISTAR_PARAMS_ERROR (STATE_WIFI_BASE - 0x0013)
|
||||
/* Debug info during zero-config wifi-provision for registar */
|
||||
/* WiFi零配过程中的主配方调试信息 */
|
||||
#define STATE_WIFI_ZCONFIG_REGISTAR_DEBUG (STATE_WIFI_BASE - 0x0014)
|
||||
/* Debug info of p2p mode smartconfig */
|
||||
/* WiFi p2p配网过程中的调试信息 */
|
||||
#define STATE_WIFI_P2P_DEBUG (STATE_WIFI_BASE - 0x0015)
|
||||
/* Debug info of dev-ap mode wifi-provision */
|
||||
/* WiFi 设备热点配网过程中的调试信息 */
|
||||
#define STATE_WIFI_DEV_AP_DEBUG (STATE_WIFI_BASE - 0x0016)
|
||||
/* WiFi provision claiming other information */
|
||||
/* WiFi配网中的其它过程信息 */
|
||||
#define STATE_WIFI_OTHERS (STATE_WIFI_BASE - 0x0017)
|
||||
/* MQTT init failed */
|
||||
/* WiFi配网中 MQTT初始化失败, MQTT用于零配主配方 */
|
||||
#define STATE_WIFI_MQTT_INIT_FAILED (STATE_WIFI_BASE - 0x0018)
|
||||
/* CoAP init failed */
|
||||
/* WiFi配网中 CoAP 初始化失败, CoAP用于所有配网模式 */
|
||||
#define STATE_WIFI_COAP_INIT_FAILED (STATE_WIFI_BASE - 0x0019)
|
||||
/* CoAP is not inited */
|
||||
/* WiFi配网中 CoAP Server 未初始化 */
|
||||
#define STATE_WIFI_COAP_UNINIT (STATE_WIFI_BASE - 0x001A)
|
||||
/* Got an invalid coap resp msg */
|
||||
/* WiFi配网中 收到CoAP非法应答消息 */
|
||||
#define STATE_WIFI_COAP_RSP_INVALID (STATE_WIFI_BASE - 0x001B)
|
||||
/* Build CoAP packet failed in wifi-provision */
|
||||
/* WiFi配网中 CoAP 组包失败 */
|
||||
#define STATE_WIFI_COAP_PKTBLD_FAILED (STATE_WIFI_BASE - 0x001C)
|
||||
/* Got invalid MQTT msg in wifi-provision */
|
||||
/* WiFi配网中 收到非法 MQTT 消息 */
|
||||
#define STATE_WIFI_INVALID_MQTT_EVENT (STATE_WIFI_BASE - 0x001D)
|
||||
/* Notify devinfo for phone AP */
|
||||
/* 手机热点配网广播设备信息 */
|
||||
#define STATE_WIFI_SENT_DEVINFO_NOTIFY (STATE_WIFI_BASE - 0x001E)
|
||||
/* Notify connectap event */
|
||||
/* WiFi配网中广播通知连接ap事件 */
|
||||
#define STATE_WIFI_SENT_CONNECTAP_NOTIFY (STATE_WIFI_BASE - 0x001F)
|
||||
/* Recieve connectap state query msg */
|
||||
/* WiFi配网中收到查询连接ap状态的包 */
|
||||
#define STATE_WIFI_GOT_CONNECTAP_QUERY (STATE_WIFI_BASE - 0x0020)
|
||||
/* Recieve devinfo query message */
|
||||
/* WiFi配网中收到查询设备信息的包(手机热点) */
|
||||
#define STATE_WIFI_GOT_DEVINFO_QUERY (STATE_WIFI_BASE - 0x0021)
|
||||
/* Resp Connect AP request */
|
||||
/* WiFi配网中设备回复手机查询connectap事件 */
|
||||
#define STATE_WIFI_SENT_CONNECTAP_RESP (STATE_WIFI_BASE - 0x0022)
|
||||
/* Resp devinfo request */
|
||||
/* WiFi配网中设备回复手机查询设备信息事件 */
|
||||
#define STATE_WIFI_SENT_DEVINFO_RESP (STATE_WIFI_BASE - 0x0023)
|
||||
/* WiFi Provision: 0x0400 ~ 0x04FF */
|
||||
|
||||
|
||||
/* COAP: 0x0500 ~ 0x05FF */
|
||||
#define STATE_COAP_BASE (-0x0500)
|
||||
|
||||
/* COAP: 0x0500 ~ 0x05FF */
|
||||
|
||||
/* HTTP: 0x0600 ~ 0x06FF */
|
||||
#define STATE_HTTP_BASE (-0x0600)
|
||||
/* Got HTTP response from server, says dynamic register request failed */
|
||||
/* 使用动态注册/一型一密功能时, 从服务端返回的报文表示, 申请密钥失败 */
|
||||
#define STATE_HTTP_DYNREG_FAIL_RESP (STATE_HTTP_BASE - 0x0001)
|
||||
/* Got HTTP response from server, which contains invalid deviceSecret */
|
||||
/* 使用动态注册/一型一密功能时, 从服务端返回的报文中, 解析得到的密钥不合理 */
|
||||
#define STATE_HTTP_DYNREG_INVALID_DS (STATE_HTTP_BASE - 0x0002)
|
||||
/* Reserved buffer is too short when compose pre-auth HTTP request */
|
||||
/* 组装预认证消息准备发给服务端时, 为HTTP上行报文预留的缓冲区太短, 不足容纳待发送内容 */
|
||||
#define STATE_HTTP_PREAUTH_REQ_BUF_SHORT (STATE_HTTP_BASE - 0x0003)
|
||||
/* Reserved buffer is too short when retrieve pre-auth HTTP response */
|
||||
/* 收取预认证请求的应答报文时, 为HTTP回应报文预留的缓冲区太短, 不足容纳待接收内容 */
|
||||
#define STATE_HTTP_PREAUTH_INVALID_RESP (STATE_HTTP_BASE - 0x0004)
|
||||
/* Content length field does not exist in HTTP response from server */
|
||||
/* 从HTTP服务器返回的报文中, 没有找到 Content-Length 字段 */
|
||||
#define STATE_HTTP_RESP_MISSING_CONTENT_LEN (STATE_HTTP_BASE - 0x0005)
|
||||
/* Report preauth relative HTTP request parameters */
|
||||
/* 进行HTTP预认证的过程中, 上报预认证请求的相关信息 */
|
||||
#define STATE_HTTP_PREAUTH_REQ (STATE_HTTP_BASE - 0x0006)
|
||||
/* Report preauth relative HTTP respond parameters */
|
||||
/* 进行HTTP预认证的过程中, 上报预认证应答的相关信息 */
|
||||
#define STATE_HTTP_PREAUTH_RSP (STATE_HTTP_BASE - 0x0007)
|
||||
|
||||
/* HTTP: 0x0600 ~ 0x06FF */
|
||||
|
||||
/* OTA: 0x0700 ~ 0x07FF */
|
||||
#define STATE_OTA_BASE (-0x0700)
|
||||
|
||||
/* OTA: 0x0700 ~ 0x07FF */
|
||||
|
||||
/* Bind: 0x0800 ~ 0x08FF */
|
||||
#define STATE_BIND_BASE (-0x0800)
|
||||
/* Got external token configuration */
|
||||
/* 绑定功能模块接收到来自外部的Token传入 */
|
||||
#define STATE_BIND_SET_APP_TOKEN (STATE_BIND_BASE - 0x0001)
|
||||
/* Failed to initialize CoAP server in LAN */
|
||||
/* 绑定功能模块未能成功开启CoAP服务, 该服务用于局域网内的token传递 */
|
||||
#define STATE_BIND_COAP_INIT_FAIL (STATE_BIND_BASE - 0x0002)
|
||||
/* Sending bind token to cloud */
|
||||
/* 正在将绑定token发送给服务端 */
|
||||
#define STATE_BIND_REPORT_TOKEN (STATE_BIND_BASE - 0x0003)
|
||||
/* Sent bind token to cloud gets success response */
|
||||
/* 绑定token发送给服务端后, 已得到服务端成功接收的回应 */
|
||||
#define STATE_BIND_REPORT_TOKEN_SUCCESS (STATE_BIND_BASE - 0x0004)
|
||||
/* Sending bind token in LAN */
|
||||
/* 正在主动将绑定token在局域网内广播 */
|
||||
#define STATE_BIND_NOTIFY_TOKEN_SENT (STATE_BIND_BASE - 0x0005)
|
||||
/* Got token query request from external devices in same LAN */
|
||||
/* 正在接收到同一局域网内来自其它电子设备的token查询请求 */
|
||||
#define STATE_BIND_RECV_TOKEN_QUERY (STATE_BIND_BASE - 0x0006)
|
||||
/* Responding bind token in LAN */
|
||||
/* 正在响应局域网内的token查询请求, 将token发送给其它电子设备 */
|
||||
#define STATE_BIND_SENT_TOKEN_RESP (STATE_BIND_BASE - 0x0007)
|
||||
/* Got invalid MQTT respond from server after msg report */
|
||||
/* mqtt信息上报给服务端后, 接收到的回应报文不合法 */
|
||||
#define STATE_BIND_MQTT_RSP_INVALID (STATE_BIND_BASE - 0x0008)
|
||||
/* Got invalid MQTT msgid from server after token report */
|
||||
/* 将绑定token上报给服务端后, 接收到的回应报文msgid不是最新的*/
|
||||
#define STATE_BIND_MQTT_MSGID_INVALID (STATE_BIND_BASE - 0x0009)
|
||||
/* Got invalid CoAP respond from client after token notify */
|
||||
/* 将绑定token上报给手机后, 接收到的回应报文不合法 */
|
||||
#define STATE_BIND_COAP_RSP_INVALID (STATE_BIND_BASE - 0x000A)
|
||||
/* Got invalid CoAP request from external devices in same LAN */
|
||||
/* 接收到来自同一局域网内其它电子设备的token查询请求, 但请求报文不合法 */
|
||||
#define STATE_BIND_COAP_REQ_INVALID (STATE_BIND_BASE - 0x000B)
|
||||
/* Assemble external token failed */
|
||||
/* app token 组装失败 */
|
||||
#define STATE_BIND_ASSEMBLE_APP_TOKEN_FAILED (STATE_BIND_BASE - 0x000C)
|
||||
/* Register coap topic failed */
|
||||
/* 注册coap topic失败 */
|
||||
#define STATE_BIND_COAP_REGISTER_FAILED (STATE_BIND_BASE - 0x000D)
|
||||
/* Token is expired, need update */
|
||||
/* token过期, 需要更新 */
|
||||
#define STATE_BIND_TOKEN_EXPIRED (STATE_BIND_BASE - 0x000E)
|
||||
/* Sent unbind msg to cloud gets success response */
|
||||
/* 解绑信息发送给服务端后, 已得到服务端成功接收的回应 */
|
||||
#define STATE_BIND_REPORT_RESET_SUCCESS (STATE_BIND_BASE - 0x000F)
|
||||
/* Got notify msg from cloud server */
|
||||
/* 收到云端推送的事件, 如绑定与解绑 */
|
||||
#define STATE_BIND_RECV_CLOUD_NOTIFY (STATE_BIND_BASE - 0x0010)
|
||||
|
||||
|
||||
/* Device Model: 0x0900 ~ 0x09FF */
|
||||
#define STATE_DEV_MODEL_BASE (-0x0900)
|
||||
|
||||
/* Master device already open so skip current open action */
|
||||
/* 主设备已经打开, 不必再次初始化 */
|
||||
#define STATE_DEV_MODEL_MASTER_ALREADY_OPEN (STATE_DEV_MODEL_BASE - 0x0001)
|
||||
/* Master device already connect so skip current connect action */
|
||||
/* 主设备已经连接服务端, 不必再次连接 */
|
||||
#define STATE_DEV_MODEL_MASTER_ALREADY_CONNECT (STATE_DEV_MODEL_BASE - 0x0002)
|
||||
/* Master device not open yet so skip current action */
|
||||
/* 当前动作需要主设备open成功, 但这尚未完成 */
|
||||
#define STATE_DEV_MODEL_MASTER_NOT_OPEN_YET (STATE_DEV_MODEL_BASE - 0x0003)
|
||||
/* Master device not connect yet so skip current action */
|
||||
/* 当前动作需要主设备连接服务端成功, 但这尚未完成 */
|
||||
#define STATE_DEV_MODEL_MASTER_NOT_CONNECT_YET (STATE_DEV_MODEL_BASE - 0x0004)
|
||||
/* Requested device already created */
|
||||
/* 当前请求创建的设备已经存在, 不必再次创建 */
|
||||
#define STATE_DEV_MODEL_DEVICE_ALREADY_EXIST (STATE_DEV_MODEL_BASE - 0x0005)
|
||||
/* Requested device not found in list */
|
||||
/* 当前请求对应的设备未找到, 无法对其操作 */
|
||||
#define STATE_DEV_MODEL_DEVICE_NOT_FOUND (STATE_DEV_MODEL_BASE - 0x0006)
|
||||
/* Requested device can not be deleted */
|
||||
/* 当前请求对应的设备不可删除, 无法对其进行删除操作 */
|
||||
#define STATE_DEV_MODEL_SUBD_NOT_DELETEABLE (STATE_DEV_MODEL_BASE - 0x0007)
|
||||
/* Requested device not in login status so can not logout */
|
||||
/* 当前请求对应的设备不在登入状态, 无法对其进行登出操作 */
|
||||
#define STATE_DEV_MODEL_SUBD_NOT_LOGIN (STATE_DEV_MODEL_BASE - 0x0008)
|
||||
/* Internal error happens in device model function */
|
||||
/* 物模型/子设备管理模块发生内部错误 */
|
||||
#define STATE_DEV_MODEL_INTERNAL_ERROR (STATE_DEV_MODEL_BASE - 0x0009)
|
||||
/* Internal event about MQTT connect happens in device model function */
|
||||
/* 物模型/子设备管理模块发现MQTT连接已经建立, 跳过再次建连动作 */
|
||||
#define STATE_DEV_MODEL_INTERNAL_MQTT_DUP_INIT (STATE_DEV_MODEL_BASE - 0x000A)
|
||||
/* Internal error about MQTT unconnect happens in device model function */
|
||||
/* 物模型/子设备管理模块发生MQTT连接未建立的内部错误 */
|
||||
#define STATE_DEV_MODEL_INTERNAL_MQTT_NOT_INIT_YET (STATE_DEV_MODEL_BASE - 0x000B)
|
||||
/* Failed to open handler for cloud abstract layer */
|
||||
/* 物模型模块中发生连接抽象层初始化失败 */
|
||||
#define STATE_DEV_MODEL_CM_OPEN_FAILED (STATE_DEV_MODEL_BASE - 0x000C)
|
||||
/* Failed to find file descriptor in cloud abstract layer */
|
||||
/* 物模型模块中未找到连接抽象层的fd */
|
||||
#define STATE_DEV_MODEL_CM_FD_NOT_FOUND (STATE_DEV_MODEL_BASE - 0x000D)
|
||||
/* Internal error about file descriptor happens in device model function */
|
||||
/* 物模型/子设备管理模块发生fd有关的内部错误 */
|
||||
#define STATE_DEV_MODEL_CM_FD_ERROR (STATE_DEV_MODEL_BASE - 0x000E)
|
||||
/* Failed to connect MQTT in device model function */
|
||||
/* 物模型模块中发生MQTT连接建立失败 */
|
||||
#define STATE_DEV_MODEL_MQTT_CONNECT_FAILED (STATE_DEV_MODEL_BASE - 0x000F)
|
||||
/* Got unexpected MQTT message from server in device model */
|
||||
/* 物模型模块中接收到来自服务端的MQTT下推消息, 但报文内容不符合预期 */
|
||||
#define STATE_DEV_MODEL_RECV_UNEXP_MQTT_PUB (STATE_DEV_MODEL_BASE - 0x0010)
|
||||
/* Got MQTT message from server but its JSON format is wrong */
|
||||
/* 物模型模块中接收到来自服务端的MQTT下推消息, 但报文内容不是合理的JSON格式 */
|
||||
#define STATE_DEV_MODEL_WRONG_JSON_FORMAT (STATE_DEV_MODEL_BASE - 0x0011)
|
||||
/* Failed to lookup value for specified key when parse JSON */
|
||||
/* 物模型模块中从JSON报文解析KV对失败 */
|
||||
#define STATE_DEV_MODEL_GET_JSON_ITEM_FAILED (STATE_DEV_MODEL_BASE - 0x0012)
|
||||
/* Service respond does not have correct request context in device model */
|
||||
/* 物模型模块中发送服务回应报文给服务端时, 没有找到对应的服务端请求上下文 */
|
||||
#define STATE_DEV_MODEL_SERVICE_CTX_NOT_EXIST (STATE_DEV_MODEL_BASE - 0x0013)
|
||||
/* OTA service is not enabled in device model */
|
||||
/* 物模型模块中发现OTA功能未开启 */
|
||||
#define STATE_DEV_MODEL_OTA_NOT_ENABLED (STATE_DEV_MODEL_BASE - 0x0014)
|
||||
/* OTA service is not initialized correctly in device model */
|
||||
/* 物模型模块中发现OTA功能未能正确初始化 */
|
||||
#define STATE_DEV_MODEL_OTA_NOT_INITED (STATE_DEV_MODEL_BASE - 0x0015)
|
||||
/* OTA service is initialized but failed in device model */
|
||||
/* 物模型模块中发现对OTA功能初始化失败 */
|
||||
#define STATE_DEV_MODEL_OTA_INIT_FAILED (STATE_DEV_MODEL_BASE - 0x0016)
|
||||
/* OTA for some sub-device is not finished yet so skip device switching */
|
||||
/* 子设备OTA时, 由于当前仍有其它子设备OTA尚未完成, 而跳过当前操作 */
|
||||
#define STATE_DEV_MODEL_OTA_STILL_IN_PROGRESS (STATE_DEV_MODEL_BASE - 0x0017)
|
||||
/* OTA firmware downloaded failed to pass integrity check */
|
||||
/* 物模型模块中发现OTA下载的固件, 未能通过完整性校验 */
|
||||
#define STATE_DEV_MODEL_OTA_IMAGE_CHECK_FAILED (STATE_DEV_MODEL_BASE - 0x0018)
|
||||
/* OTA type is neither cota or fota */
|
||||
/* 物模型模块中发现OTA操作的类型既不是FOTA, 又不是COTA */
|
||||
#define STATE_DEV_MODEL_OTA_TYPE_ERROR (STATE_DEV_MODEL_BASE - 0x0019)
|
||||
/* OTA fetching from cloud failed to get expected content */
|
||||
/* 物模型模块中从云端获取下载内容未达预期 */
|
||||
#define STATE_DEV_MODEL_OTA_FETCH_FAILED (STATE_DEV_MODEL_BASE - 0x001A)
|
||||
/* ALCS function failed to initialize itself */
|
||||
/* 本地控制功能初始化失败 */
|
||||
#define STATE_DEV_MODEL_ALCS_CONSTRUCT_FAILED (STATE_DEV_MODEL_BASE - 0x001B)
|
||||
/* Gateway/Sub-device management function is not configured on */
|
||||
/* SDK当前未被配置为打开子设备管理/网关功能 */
|
||||
#define STATE_DEV_MODEL_GATEWAY_NOT_ENABLED (STATE_DEV_MODEL_BASE - 0x001C)
|
||||
/* RRPC message contains too long identifier */
|
||||
/* 物模型模块中发现RRPC请求的ID太长, 超过了可处理的最大长度 */
|
||||
#define STATE_DEV_MODEL_RRPCID_TOO_LONG (STATE_DEV_MODEL_BASE - 0x001D)
|
||||
/* Configured rawdata+solo mode, will not proceed JSON messages */
|
||||
/* 物模型模块已被配置为单品透传模式, 无法处理JSON报文 */
|
||||
#define STATE_DEV_MODEL_IN_RAWDATA_SOLO (STATE_DEV_MODEL_BASE - 0x001E)
|
||||
/* Duplicated upstream device model messages */
|
||||
/* 物模型模块中发现重复的上行报文请求 */
|
||||
#define STATE_DEV_MODEL_DUP_UPSTREAM_MSG (STATE_DEV_MODEL_BASE - 0x001F)
|
||||
/* Corresponding upstream record not found for downstream messages */
|
||||
/* 物模型模块处理下行报文时, 未找到与之对应的上行报文记录 */
|
||||
#define STATE_DEV_MODEL_UPSTREAM_REC_NOT_FOUND (STATE_DEV_MODEL_BASE - 0x0020)
|
||||
/* Got negative respond from cloud in device model */
|
||||
/* 物模型/子设备管理模块中得到来自云端对请求的拒绝报文 */
|
||||
#define STATE_DEV_MODEL_REFUSED_BY_CLOUD (STATE_DEV_MODEL_BASE - 0x0021)
|
||||
/* Encount unexpected option when invoke dm_opt_get() */
|
||||
/* 物模型模块中发现用于获取内部信息的option不合理 */
|
||||
#define STATE_DEV_MODEL_INVALID_DM_OPTION (STATE_DEV_MODEL_BASE - 0x0022)
|
||||
/* Encount parsing failure when process URL in respond from cloud */
|
||||
/* 物模型模块在处理云端回复报文时, 从中解析URL失败 */
|
||||
#define STATE_DEV_MODEL_URL_SPLIT_FAILED (STATE_DEV_MODEL_BASE - 0x0023)
|
||||
/* Failed to parse Alink request or respond messages */
|
||||
/* 解析Alink上行报文或下行报文失败 */
|
||||
#define STATE_DEV_MODEL_ALINK_MSG_PARSE_FAILED (STATE_DEV_MODEL_BASE - 0x0024)
|
||||
/* Failed to upload device log to cloud */
|
||||
/* 上传设备日志到云端日志接口失败 */
|
||||
#define STATE_DEV_MODEL_LOG_REPORT_ERROR (STATE_DEV_MODEL_BASE - 0x0025)
|
||||
/* Bypass current topic subscription since auto subscribed */
|
||||
/* 物模型模块被配置为免订阅模式, 跳过当前的系统Topic订阅 */
|
||||
#define STATE_DEV_MODEL_IN_AUTOSUB_MODE (STATE_DEV_MODEL_BASE - 0x0026)
|
||||
/* Got message from cloud in device model */
|
||||
/* 物模型/子设备管理模块中, 接收到来自云端的下推报文 */
|
||||
#define STATE_DEV_MODEL_RX_CLOUD_MESSAGE (STATE_DEV_MODEL_BASE - 0x0027)
|
||||
/* Sending message to cloud in device model */
|
||||
/* 物模型/子设备管理模块中, 正在发送去往云端的上行报文 */
|
||||
#define STATE_DEV_MODEL_TX_CLOUD_MESSAGE (STATE_DEV_MODEL_BASE - 0x0028)
|
||||
/* Got message from local in device model */
|
||||
/* 物模型/子设备管理模块中, 接收到来自本地控制设备的报文 */
|
||||
#define STATE_DEV_MODLE_RX_LOCAL_MESSAGE (STATE_DEV_MODEL_BASE - 0x0029)
|
||||
/* Sending message to local in device model */
|
||||
/* 物模型/子设备管理模块中, 正在发送去往本地控制设备的报文 */
|
||||
#define STATE_DEV_MODLE_TX_LOCAL_MESSAGE (STATE_DEV_MODEL_BASE - 0x002A)
|
||||
/* Processing control messages in alcs protocol */
|
||||
/* 物模型/子设备管理模块中, 正在处理本地控制协议中的控制类消息 */
|
||||
#define STATE_DEV_MODLE_ALCS_CONTROL (STATE_DEV_MODEL_BASE - 0x002B)
|
||||
/* Processing message queue of device model and external modules */
|
||||
/* 物模型模块中的消息队列正在被插入或删除元素 */
|
||||
#define STATE_DEV_MODEL_MSGQ_OPERATION (STATE_DEV_MODEL_BASE - 0x002C)
|
||||
/* Message queue in device model is full */
|
||||
/* 物模型模块中的消息队列已满 */
|
||||
#define STATE_DEV_MODEL_MSGQ_FULL (STATE_DEV_MODEL_BASE - 0x002D)
|
||||
/* Message queue in device model is empty */
|
||||
/* 物模型模块中的消息队列已空 */
|
||||
#define STATE_DEV_MODEL_MSGQ_EMPTY (STATE_DEV_MODEL_BASE - 0x002E)
|
||||
/* Cache list in device model get inserted new element */
|
||||
/* 物模型模块中的会话上下文链表正在被插入新元素 */
|
||||
#define STATE_DEV_MODEL_CTX_LIST_INSERT (STATE_DEV_MODEL_BASE - 0x002F)
|
||||
/* Cache list in device model get removed element */
|
||||
/* 物模型模块中的会话上下文链表正在被删除某元素 */
|
||||
#define STATE_DEV_MODEL_CTX_LIST_REMOVE (STATE_DEV_MODEL_BASE - 0x0030)
|
||||
/* Element in cache list of device model removed since life-time over */
|
||||
/* 物模型模块中的会话上下文元素因为闲置时间太长而被老化删除 */
|
||||
#define STATE_DEV_MODEL_CTX_LIST_FADEOUT (STATE_DEV_MODEL_BASE - 0x0031)
|
||||
/* Cache list in device model get filled full */
|
||||
/* 物模型模块中的会话上下文链表已满 */
|
||||
#define STATE_DEV_MODEL_CTX_LIST_FULL (STATE_DEV_MODEL_BASE - 0x0032)
|
||||
/* Cache list in device model get consumed empty */
|
||||
/* 物模型模块中的会话上下文链表已空 */
|
||||
#define STATE_DEV_MODEL_CTX_LIST_EMPTY (STATE_DEV_MODEL_BASE - 0x0033)
|
||||
/* Log post to cloud get stopped */
|
||||
/* 物模型模块中的日志上云已被停止 */
|
||||
#define STATE_DEV_MODLE_LOG_REPORT_STOP (STATE_DEV_MODEL_BASE - 0x0034)
|
||||
/* Log post to cloud get switched */
|
||||
/* 物模型模块中的日志上云已被调节 */
|
||||
#define STATE_DEV_MODLE_LOG_REPORT_SWITCH (STATE_DEV_MODEL_BASE - 0x0035)
|
||||
/* Log post to cloud get transmitted */
|
||||
/* 物模型模块中的日志上云消息已被发送 */
|
||||
#define STATE_DEV_MODLE_LOG_REPORT_SEND (STATE_DEV_MODEL_BASE - 0x0036)
|
||||
/* Operating to list of sync request in device model */
|
||||
/* 物模型模块中的同步请求队列正在被操作 */
|
||||
#define STATE_DEV_MODEL_SYNC_REQ_LIST (STATE_DEV_MODEL_BASE - 0x0037)
|
||||
/* Event of alink protocol processing is happenning */
|
||||
/* 物模型模块正在处理Alink协议相关的报文 */
|
||||
#define STATE_DEV_MODEL_ALINK_PROT_EVENT (STATE_DEV_MODEL_BASE - 0x0038)
|
||||
/* Failed to transimit alcs packets into local network */
|
||||
/* 物模型模块中, 在局域网内发送本地控制相关报文失败 */
|
||||
#define STATE_DEV_MODEL_ALCS_SEND_FAILED (STATE_DEV_MODEL_BASE - 0x0039)
|
||||
/* Got invalid alink protocol mqtt packet */
|
||||
/* 接收到非法的alink协议报文 */
|
||||
#define STATE_DEV_MODEL_INVALID_ALINK_PAYLOAD (STATE_DEV_MODEL_BASE - 0x003A)
|
||||
/* Got invalid requested alink protocol mqtt topic */
|
||||
/* 收到不符合当前场景的alink协议topic */
|
||||
#define STATE_DEV_MODEL_INVALID_ALINK_TOPIC (STATE_DEV_MODEL_BASE - 0x003B)
|
||||
/* IOT_Linkkit_Yield already stopped */
|
||||
/* IOT_Linkkit_Yield 已停止运行 */
|
||||
#define STATE_DEV_MODEL_YIELD_STOPPED (STATE_DEV_MODEL_BASE - 0x003C)
|
||||
/* IOT_Linkkit_Yield still running */
|
||||
/* IOT_Linkkit_Yield 仍在运行 */
|
||||
#define STATE_DEV_MODEL_YIELD_RUNNING (STATE_DEV_MODEL_BASE - 0x003D)
|
||||
|
||||
/* Device Model: 0x0900 ~ 0x09FF */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* __INFRA_STATE_H__ */
|
||||
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_STRING
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "infra_types.h"
|
||||
#include "infra_string.h"
|
||||
|
||||
int8_t infra_hex2char(uint8_t hex)
|
||||
{
|
||||
hex = hex & 0xF;
|
||||
return (int8_t)(hex < 10 ? '0' + hex : hex - 10 + 'a');
|
||||
}
|
||||
|
||||
void infra_hex2str(uint8_t *input, uint16_t input_len, char *output)
|
||||
{
|
||||
char *zEncode = "0123456789ABCDEF";
|
||||
int i = 0, j = 0;
|
||||
|
||||
for (i = 0; i < input_len; i++) {
|
||||
output[j++] = zEncode[(input[i] >> 4) & 0xf];
|
||||
output[j++] = zEncode[(input[i]) & 0xf];
|
||||
}
|
||||
}
|
||||
|
||||
void infra_int2str(uint32_t input, char output[10])
|
||||
{
|
||||
uint8_t i = 0, j = 0;
|
||||
char tmp[10] = {0};
|
||||
|
||||
do {
|
||||
tmp[i++] = input%10 + '0';
|
||||
}while((input/=10)>0);
|
||||
|
||||
do {
|
||||
output[--i] = tmp[j++];
|
||||
}while(i > 0);
|
||||
}
|
||||
|
||||
char *infra_strtok(char *str, const char *delim)
|
||||
{
|
||||
int only_delim = 1;
|
||||
static char *pos = NULL;
|
||||
static char *target = NULL;
|
||||
|
||||
pos = (str == NULL)?(pos):(str);
|
||||
|
||||
if (pos == NULL || delim == NULL ||
|
||||
strlen(pos) <= strlen(delim)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
target = pos;
|
||||
while (strlen(pos) >= strlen(delim)) {
|
||||
if (memcmp(pos,delim,strlen(delim)) != 0) {
|
||||
only_delim = 0;
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strlen(pos) == strlen(delim)) {
|
||||
memset(pos,0,strlen(delim));
|
||||
if (only_delim) {
|
||||
return NULL;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
if (target == pos) {
|
||||
pos += strlen(delim);
|
||||
target = pos;
|
||||
}else{
|
||||
memset(pos,0,strlen(delim));
|
||||
pos += strlen(delim);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
#define LITE_isdigit(c) (((c) <= '9' && (c) >= '0') ? (1) : (0))
|
||||
|
||||
static uint8_t _hexval_of_char(char hex)
|
||||
{
|
||||
if (LITE_isdigit(hex)) {
|
||||
return (hex - '0');
|
||||
}
|
||||
if (hex >= 'a' && hex <= 'f') {
|
||||
return (hex - 'a' + 10);
|
||||
}
|
||||
if (hex >= 'A' && hex <= 'F') {
|
||||
return (hex - 'A' + 10);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LITE_hexstr_convert(char *input, int input_len, unsigned char *output, int output_len)
|
||||
{
|
||||
int i = 0;
|
||||
uint8_t ch0, ch1;
|
||||
|
||||
if (input_len % 2 != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (i < input_len / 2 && i < output_len) {
|
||||
ch0 = _hexval_of_char((char)input[2 * i]);
|
||||
ch1 = _hexval_of_char((char)input[2 * i + 1]);
|
||||
output[i] = (ch0 << 4 | ch1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void LITE_hexbuf_convert(unsigned char *digest, char *out, int in_len, int uppercase)
|
||||
{
|
||||
static char *zEncode[] = {"0123456789abcdef", "0123456789ABCDEF"};
|
||||
int j = 0;
|
||||
int i = 0;
|
||||
int idx = uppercase ? 1 : 0;
|
||||
|
||||
for (i = 0; i < in_len; i ++) {
|
||||
int a = digest[i];
|
||||
|
||||
out[j++] = zEncode[idx][(a >> 4) & 0xf];
|
||||
out[j++] = zEncode[idx][a & 0xf];
|
||||
}
|
||||
}
|
||||
|
||||
int infra_str2int(const char *input, int *val)
|
||||
{
|
||||
int sign = 0;
|
||||
int temp = 0;
|
||||
|
||||
if (input == NULL || val == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while(*input == ' ') { /* only support skipping space */
|
||||
input++;
|
||||
}
|
||||
|
||||
if (*input == '+') {
|
||||
input++;
|
||||
}
|
||||
else if (*input == '-') {
|
||||
input++;
|
||||
sign = -1;
|
||||
}
|
||||
|
||||
while (*input != 0) {
|
||||
if (*input < '0' || *input > '9') {
|
||||
break;
|
||||
}
|
||||
|
||||
temp = temp * 10 + (*input - '0');
|
||||
input++;
|
||||
}
|
||||
|
||||
if (sign == -1) {
|
||||
temp = -temp;
|
||||
}
|
||||
|
||||
*val = temp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t utils_str2uint(char *input, uint8_t input_len, uint32_t *output)
|
||||
{
|
||||
uint8_t index = 0;
|
||||
uint32_t temp = 0;
|
||||
|
||||
for (index = 0; index < input_len; index++) {
|
||||
if (input[index] < '0' || input[index] > '9') {
|
||||
return -1;
|
||||
}
|
||||
temp = temp * 10 + input[index] - '0';
|
||||
}
|
||||
*output = temp;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int32_t infra_json_value(const char *input, uint32_t input_len, const char *key, uint32_t key_len, char **value, uint32_t *value_len)
|
||||
{
|
||||
uint32_t idx = 0;
|
||||
|
||||
for (idx = 0;idx < input_len;idx++) {
|
||||
if (idx + key_len >= input_len) {
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(&input[idx], key, key_len) == 0) {
|
||||
idx += key_len;
|
||||
/* shortest ":x, or ":x} or ":x] */
|
||||
if ((idx + 4 >= input_len) ||
|
||||
(input[idx+1] != ':')) {
|
||||
return -1;
|
||||
}
|
||||
idx+=2;
|
||||
*value = (char *)&input[idx];
|
||||
for (;idx < input_len;idx++) {
|
||||
if ((input[idx] == ',') ||
|
||||
(input[idx] == '}') ||
|
||||
(input[idx] == ']')) {
|
||||
*value_len = idx - (*value - input);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
#ifndef _INFRA_STRING_H_
|
||||
#define _INFRA_STRING_H_
|
||||
|
||||
#include "infra_types.h"
|
||||
|
||||
int8_t infra_hex2char(uint8_t hex);
|
||||
void infra_hex2str(uint8_t *input, uint16_t input_len, char *output);
|
||||
void infra_int2str(uint32_t input, char output[10]);
|
||||
char *infra_strtok(char *str, const char *delim);
|
||||
int infra_randstr(char *random, int length);
|
||||
void LITE_hexstr_convert(char *input, int input_len, unsigned char *output, int output_len);
|
||||
int infra_str2int(const char *input, int *val);
|
||||
int32_t utils_str2uint(char *input, uint8_t input_len, uint32_t *output);
|
||||
void LITE_hexbuf_convert(unsigned char *digest, char *out, int in_len, int uppercase);
|
||||
int32_t infra_json_value(const char *input, uint32_t input_len, const char *key, uint32_t key_len, char **value, uint32_t *value_len);
|
||||
#endif
|
||||
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
#include "infra_config.h"
|
||||
|
||||
#ifdef INFRA_TIMER
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include "infra_types.h"
|
||||
#include "infra_timer.h"
|
||||
#include "wrappers.h"
|
||||
|
||||
void iotx_time_start(iotx_time_t *timer)
|
||||
{
|
||||
if (!timer) {
|
||||
return;
|
||||
}
|
||||
|
||||
timer->time = HAL_UptimeMs();
|
||||
}
|
||||
|
||||
uint32_t utils_time_spend(iotx_time_t *start)
|
||||
{
|
||||
uint32_t now, res;
|
||||
|
||||
if (!start) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
now = HAL_UptimeMs();
|
||||
res = now - start->time;
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t iotx_time_left(iotx_time_t *end)
|
||||
{
|
||||
uint32_t now, res;
|
||||
|
||||
if (!end) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (utils_time_is_expired(end)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
now = HAL_UptimeMs();
|
||||
res = end->time - now;
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t utils_time_is_expired(iotx_time_t *timer)
|
||||
{
|
||||
uint32_t cur_time;
|
||||
|
||||
if (!timer) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
cur_time = HAL_UptimeMs();
|
||||
/*
|
||||
* WARNING: Do NOT change the following code until you know exactly what it do!
|
||||
*
|
||||
* check whether it reach destination time or not.
|
||||
*/
|
||||
if ((cur_time - timer->time) < (UINT32_MAX / 2)) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void iotx_time_init(iotx_time_t *timer)
|
||||
{
|
||||
if (!timer) {
|
||||
return;
|
||||
}
|
||||
|
||||
timer->time = 0;
|
||||
}
|
||||
|
||||
void utils_time_countdown_ms(iotx_time_t *timer, uint32_t millisecond)
|
||||
{
|
||||
if (!timer) {
|
||||
return;
|
||||
}
|
||||
|
||||
timer->time = HAL_UptimeMs() + millisecond;
|
||||
}
|
||||
|
||||
uint32_t utils_time_get_ms(void)
|
||||
{
|
||||
return HAL_UptimeMs();
|
||||
}
|
||||
#endif
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef _INFRA_TIMER_H_
|
||||
#define _INFRA_TIMER_H_
|
||||
|
||||
#include "infra_types.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t time;
|
||||
} iotx_time_t;
|
||||
|
||||
|
||||
void iotx_time_start(iotx_time_t *timer);
|
||||
|
||||
uint32_t utils_time_spend(iotx_time_t *start);
|
||||
|
||||
uint32_t iotx_time_left(iotx_time_t *end);
|
||||
|
||||
uint32_t utils_time_is_expired(iotx_time_t *timer);
|
||||
|
||||
void iotx_time_init(iotx_time_t *timer);
|
||||
|
||||
void utils_time_countdown_ms(iotx_time_t *timer, uint32_t millisecond);
|
||||
|
||||
uint32_t utils_time_get_ms(void);
|
||||
|
||||
#endif /* _IOTX_COMMON_TIMER_H_ */
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user