1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _IOT_EXPORT_LINKKIT_H_
6 #define _IOT_EXPORT_LINKKIT_H_
7 
8 #if defined(__cplusplus)
9 extern "C" {
10 #endif
11 
12 #include "linkkit/infra/infra_types.h"
13 #include "linkkit/infra/infra_defs.h"
14 
15 typedef enum {
16     IOTX_LINKKIT_DEV_TYPE_MASTER,
17     IOTX_LINKKIT_DEV_TYPE_SLAVE,
18     IOTX_LINKKIT_DEV_TYPE_MAX
19 } iotx_linkkit_dev_type_t;
20 
21 typedef struct {
22     char product_key[IOTX_PRODUCT_KEY_LEN + 1];
23     char product_secret[IOTX_PRODUCT_SECRET_LEN + 1];
24     char device_name[IOTX_DEVICE_NAME_LEN + 1];
25     char device_secret[IOTX_DEVICE_SECRET_LEN + 1];
26 } iotx_linkkit_dev_meta_info_t;
27 
28 typedef enum {
29     /* post property value to cloud */
30     ITM_MSG_POST_PROPERTY,
31 
32     /* post device info update message to cloud */
33     ITM_MSG_DEVICEINFO_UPDATE,
34 
35     /* post device info delete message to cloud */
36     ITM_MSG_DEVICEINFO_DELETE,
37 
38     /* post raw data to cloud */
39     ITM_MSG_POST_RAW_DATA,
40 
41     /* only for slave device, send login request to cloud */
42     ITM_MSG_LOGIN,
43 
44     /* only for slave device, send logout request to cloud */
45     ITM_MSG_LOGOUT,
46 
47     /* only for slave device, send delete topo request to cloud */
48     ITM_MSG_DELETE_TOPO,
49 
50     /* query ntp time from cloud */
51     ITM_MSG_QUERY_TIMESTAMP,
52 
53     /* only for master device, query topo list */
54     ITM_MSG_QUERY_TOPOLIST,
55 
56     /* only for master device, qurey firmware ota data */
57     ITM_MSG_QUERY_FOTA_DATA,
58 
59     /* only for master device, qurey config ota data */
60     ITM_MSG_QUERY_COTA_DATA,
61 
62     /* only for master device, request config ota data from cloud */
63     ITM_MSG_REQUEST_COTA,
64 
65     /* only for master device, request fota image from cloud */
66     ITM_MSG_REQUEST_FOTA_IMAGE,
67 
68     /* report subdev's firmware version */
69     ITM_MSG_REPORT_SUBDEV_FIRMWARE_VERSION,
70 
71     /* get a device's desired property */
72     ITM_MSG_PROPERTY_DESIRED_GET,
73 
74     /* delete a device's desired property */
75     ITM_MSG_PROPERTY_DESIRED_DELETE,
76 
77     IOTX_LINKKIT_MSG_MAX
78 } iotx_linkkit_msg_type_t;
79 
80 /**
81  * @brief create a new device
82  *
83  * @param dev_type. type of device which will be created. see
84  * iotx_linkkit_dev_type_t
85  * @param meta_info. The product key, product secret, device name and device
86  * secret of new device.
87  *
88  * @return success: device id (>=0), fail: -1.
89  *
90  */
91 int IOT_Linkkit_Open(iotx_linkkit_dev_type_t dev_type,
92                      iotx_linkkit_dev_meta_info_t *meta_info);
93 
94 /**
95  * @brief start device network connection.
96  *        for master device, start to connect aliyun server.
97  *        for slave device, send message to cloud for register new device and
98  * add topo with master device
99  *
100  * @param devid. device identifier.
101  *
102  * @return success: device id (>=0), fail: -1.
103  *
104  */
105 int IOT_Linkkit_Connect(int devid);
106 
107 /**
108  * @brief try to receive message from cloud and dispatch these message to user
109  * event callback
110  *
111  * @param timeout_ms. timeout for waiting new message arrived
112  *
113  * @return void.
114  *
115  */
116 void IOT_Linkkit_Yield(int timeout_ms);
117 
118 /**
119  * @brief close device network connection and release resources.
120  *        for master device, disconnect with aliyun server and release all local
121  * resources. for slave device, send message to cloud for delete topo with
122  * master device and unregister itself, then release device's resources.
123  *
124  * @param devid. device identifier.
125  *
126  * @return success: 0, fail: -1.
127  *
128  */
129 int IOT_Linkkit_Close(int devid);
130 
131 /**
132  * @brief Report message to cloud
133  *
134  * @param devid. device identifier.
135  * @param msg_type. message type. see iotx_linkkit_msg_type_t, as follows:
136  *        ITM_MSG_POST_PROPERTY
137  *        ITM_MSG_DEVICEINFO_UPDATE
138  *        ITM_MSG_DEVICEINFO_DELETE
139  *        ITM_MSG_POST_RAW_DATA
140  *        ITM_MSG_LOGIN
141  *        ITM_MSG_LOGOUT
142  *
143  * @param payload. message payload.
144  * @param payload_len. message payload length.
145  *
146  * @return success: 0 or message id (>=1), fail: -1.
147  *
148  */
149 int IOT_Linkkit_Report(int devid, iotx_linkkit_msg_type_t msg_type,
150                        unsigned char *payload, int payload_len);
151 
152 /**
153  * @brief post message to cloud
154  *
155  * @param devid. device identifier.
156  * @param msg_type. message type. see iotx_linkkit_msg_type_t, as follows:
157  *        ITM_MSG_QUERY_TIMESTAMP
158  *        ITM_MSG_QUERY_TOPOLIST
159  *        ITM_MSG_QUERY_FOTA_DATA
160  *        ITM_MSG_QUERY_COTA_DATA
161  *        ITM_MSG_REQUEST_COTA
162  *        ITM_MSG_REQUEST_FOTA_IMAGE
163  *
164  * @param payload. message payload.
165  * @param payload_len. message payload length.
166  *
167  * @return success: 0 or message id (>=1), fail: -1.
168  *
169  */
170 int IOT_Linkkit_Query(int devid, iotx_linkkit_msg_type_t msg_type,
171                       unsigned char *payload, int payload_len);
172 
173 /**
174  * @brief post event to cloud
175  *
176  * @param devid. device identifier.
177  * @param eventid. tsl event id.
178  * @param eventid_len. length of tsl event id.
179  * @param payload. event payload.
180  * @param payload_len. event payload length.
181  *
182  * @return success: message id (>=1), fail: -1.
183  *
184  */
185 int IOT_Linkkit_TriggerEvent(int devid, char *eventid, int eventid_len,
186                              char *payload, int payload_len);
187 
188 /**
189  * @brief post service response to cloud
190  *
191  * @param devid. device identifier.
192  * @param serviceid. tsl service id.
193  * @param serviceid_len. length of tsl service id.
194  * @param payload. service response payload.
195  * @param payload_len. service response payload length.
196  *
197  * @return success: 0, fail: -1.
198  *
199  */
200 int IOT_Linkkit_AnswerService(int devid, char *serviceid, int serviceid_len,
201                               char *payload, int payload_len,
202                               void *p_service_ctx);
203 
204 #if defined(__cplusplus)
205 }
206 #endif
207 #endif
208