1 /* 2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited 3 */ 4 5 #ifndef _IOTX_CM_H_ 6 #define _IOTX_CM_H_ 7 8 #include "linkkit/infra/infra_types.h" 9 10 #define CM_MAX_FD_NUM 3 11 #define CM_DEFAULT_YIELD_TIMEOUT 200 12 /* message confirmation type */ 13 typedef enum { 14 /* non ACK */ 15 /* MQTT: QoS is 0 */ 16 /* CoAP: NON */ 17 /* default */ 18 IOTX_CM_MESSAGE_NO_ACK, 19 /* need ACK */ 20 /* MQTT: QoS is 1 */ 21 /* CoAP: CON */ 22 IOTX_CM_MESSAGE_NEED_ACK, 23 /* non ACK */ 24 /* MQTT: QoS is 3 */ 25 /* CoAP: NONE*/ 26 IOTX_CM_MESSAGE_SUB_LOCAL, 27 /* Maximum number of ack type */ 28 IOTX_CM_MESSAGE_ACK_MAX 29 } iotx_cm_ack_types_t; 30 31 /* message confirmation type */ 32 typedef enum { 33 /* non ACK */ 34 /* MQTT: QoS is 0 */ 35 /* CoAP: NON */ 36 /* default */ 37 IOTX_CM_ASYNC, 38 /* need ACK */ 39 /* MQTT: QoS is 1 */ 40 /* CoAP: CON */ 41 IOTX_CM_SYNC, 42 /* Maximum number of ack type */ 43 IOTX_CM_SYNC_MAX 44 } iotx_cm_sync_mode_types_t; 45 46 /* protocol type */ 47 typedef enum IOTX_CM_PROTOCOL_TYPES { 48 /* MQTT */ 49 IOTX_CM_PROTOCOL_TYPE_MQTT = 1, 50 /* COAP */ 51 IOTX_CM_PROTOCOL_TYPE_COAP = 2, 52 /* HTTP */ 53 IOTX_CM_PROTOCOL_TYPE_HTTP = 3, 54 /* HTTP2 */ 55 IOTX_CM_PROTOCOL_TYPE_HTTP2 = 4, 56 /* Maximum number of protocol type */ 57 IOTX_CM_PROTOCOL_TYPE_MAX 58 } iotx_cm_protocol_types_t; 59 60 /* event type */ 61 typedef enum IOTX_CM_EVENT_TYPES { 62 /* cloud connected */ 63 IOTX_CM_EVENT_CLOUD_CONNECTED = 0, 64 /* cloud: disconnect */ 65 /* event_msg is null */ 66 IOTX_CM_EVENT_CLOUD_CONNECT_FAILED, 67 /* cloud: disconnect */ 68 /* event_msg is null */ 69 IOTX_CM_EVENT_CLOUD_DISCONNECT, 70 /* event_msg is iotx_cm_event_result_pt */ 71 IOTX_CM_EVENT_SUBCRIBE_SUCCESS, 72 IOTX_CM_EVENT_SUBCRIBE_FAILED, 73 IOTX_CM_EVENT_UNSUB_SUCCESS, 74 IOTX_CM_EVENT_UNSUB_FAILED, 75 IOTX_CM_EVENT_PUBLISH_SUCCESS, 76 IOTX_CM_EVENT_PUBLISH_FAILED, 77 /* Maximum number of event */ 78 IOTX_CM_EVENT_MAX 79 } iotx_cm_event_types_t; 80 81 /* The structure of cloud Connection event struct */ 82 typedef struct { 83 iotx_cm_event_types_t type; 84 void *msg; 85 } iotx_cm_event_msg_t; 86 87 typedef struct { 88 char *topic; 89 uint8_t *payload; 90 uint32_t payload_len; 91 } event_msg_data_t; 92 93 #ifdef DEVICE_MODEL_ALINK2 94 typedef void (*iotx_cm_data_handle_cb)(int fd, const char *topic, 95 uint32_t topic_len, const char *payload, 96 unsigned int payload_len, void *context); 97 #else 98 typedef void (*iotx_cm_data_handle_cb)(int fd, const char *topic, 99 const char *payload, 100 unsigned int payload_len, void *context); 101 #endif 102 103 typedef void (*iotx_cm_event_handle_cb)(int fd, iotx_cm_event_msg_t *event, 104 void *context); 105 106 /* IoTx initializa parameters */ 107 typedef struct { 108 uint32_t request_timeout_ms; 109 uint32_t keepalive_interval_ms; 110 uint32_t write_buf_size; 111 uint32_t read_buf_size; 112 iotx_cm_protocol_types_t protocol_type; 113 iotx_cm_event_handle_cb handle_event; /* Specify MQTT event handle */ 114 void *context; 115 #ifdef DEVICE_MODEL_ALINK2 116 iotx_dev_meta_info_t *dev_info; 117 iotx_mqtt_region_types_t region; 118 #endif 119 } iotx_cm_init_param_t; 120 121 typedef struct { 122 iotx_cm_ack_types_t ack_type; 123 iotx_cm_sync_mode_types_t sync_mode; 124 uint32_t sync_timeout; 125 iotx_cm_data_handle_cb ack_cb; 126 void *cb_context; 127 } iotx_cm_ext_params_t; 128 129 int iotx_cm_open(iotx_cm_init_param_t *params); 130 int iotx_cm_connect(int fd, uint32_t timeout); 131 int iotx_cm_yield(int fd, unsigned int timeout); 132 int iotx_cm_sub(int fd, iotx_cm_ext_params_t *ext, const char *topic, 133 iotx_cm_data_handle_cb topic_handle_func, void *pcontext); 134 int iotx_cm_unsub(int fd, const char *topic); 135 int iotx_cm_pub(int fd, iotx_cm_ext_params_t *ext, const char *topic, 136 const char *payload, unsigned int payload_len); 137 int iotx_cm_close(int fd); 138 #endif /* _LINKKIT_CM_H_ */ 139