1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #if defined(DEPRECATED_LINKKIT)
6 #ifndef _DM_SHADOW_H_
7 #define _DM_SHADOW_H_
8 
9 #include "iotx_dm_internal.h"
10 
11 #define DM_SHW_KEY_SCHEMA                     "schema"
12 #define DM_SHW_KEY_LINK                       "link"
13 #define DM_SHW_KEY_PROFILE                    "profile"
14 #define DM_SHW_KEY_PROPERTIES                 "properties"
15 #define DM_SHW_KEY_EVENTS                     "events"
16 #define DM_SHW_KEY_SERVICES                   "services"
17 
18 #define DM_SHW_KEY_PROFILE_PK                 "productKey"
19 #define DM_SHW_KEY_PROFILE_DN                 "deviceName"
20 #define DM_SHW_KEY_IDENTIFIER                 "identifier"
21 #define DM_SHW_KEY_NAME                       "name"
22 #define DM_SHW_KEY_DESC                       "desc"
23 #define DM_SHW_KEY_ACCESS_MODE                "accessMode"
24 #define DM_SHW_KEY_REQUIRED                   "required"
25 #define DM_SHW_KEY_METHOD                     "method"
26 #define DM_SHW_KEY_CALLTYPE                   "callType"
27 #define DM_SHW_KEY_OUTPUTDATA                 "outputData"
28 #define DM_SHW_KEY_INPUTDATA                  "inputData"
29 #define DM_SHW_KEY_DATATYPE                   "dataType"
30 #define DM_SHW_KEY_TYPE                       "type"
31 #define DM_SHW_KEY_SPECS                      "specs"
32 #define DM_SHW_KEY_UNIT                       "unit"
33 #define DM_SHW_KEY_UNITNAME                   "unitName"
34 #define DM_SHW_KEY_MIN                        "min"
35 #define DM_SHW_KEY_MAX                        "max"
36 #define DM_SHW_KEY_LENGTH                     "length"
37 #define DM_SHW_KEY_SIZE                       "size"
38 #define DM_SHW_KEY_ITEM                       "item"
39 
40 /* Special Service And Event */
41 #define DM_SHW_SPECIAL_SERVICE_SET_IDENTIFIER "set"
42 #define DM_SHW_SPECIAL_SERVICE_SET_METHOD     "thing.service.property.set"
43 #define DM_SHW_SPECIAL_SERVICE_GET_IDENTIFIER "get"
44 #define DM_SHW_SPECIAL_SERVICE_GET_METHOD     "thing.service.property.get"
45 #define DM_SHW_SPECIAL_EVENT_POST_IDENTIFIER  "post"
46 #define DM_SHW_SPECIAL_EVENT_POST_METHOD      "thing.event.property.post"
47 
48 #define DM_SHW_KEY_DELIMITER                  '.'
49 
50 typedef enum {
51     DM_SHW_DATA_TYPE_NONE,   /* none */
52     DM_SHW_DATA_TYPE_INT,    /* int */
53     DM_SHW_DATA_TYPE_FLOAT,  /* float */
54     DM_SHW_DATA_TYPE_DOUBLE, /* double */
55     DM_SHW_DATA_TYPE_TEXT,   /* string */
56     DM_SHW_DATA_TYPE_ENUM,   /* int */
57     DM_SHW_DATA_TYPE_DATE,   /* string */
58     DM_SHW_DATA_TYPE_BOOL,   /* bool,0 or 1 */
59     DM_SHW_DATA_TYPE_ARRAY,  /* support int, float, double, text */
60     DM_SHW_DATA_TYPE_STRUCT, /* support above 8 data types */
61 } dm_shw_data_type_e;
62 
63 typedef enum {
64     DM_SHW_DATA_TARGET_SERVICE_INPUT_DATA,
65     DM_SHW_DATA_TARGET_SERVICE_OUTPUT_DATA
66 } dm_shw_data_target_e;
67 
68 typedef struct {
69     dm_shw_data_type_e type;
70     int size;
71     void *value;
72 } dm_shw_data_value_complex_t;
73 
74 typedef struct {
75     dm_shw_data_type_e type;
76     union {
77         int value_int;
78         float value_float;
79         double value_double;
80         void *value; /* string or complex type accroding to data type */
81     };
82 } dm_shw_data_value_t;
83 
84 typedef struct {
85     dm_shw_data_type_e type;
86     int specs_number; /* used when type is enum and struct */
87     void *specs;      /* nerver be used by struct */
88 } dm_shw_data_type_t;
89 
90 typedef struct {
91     char *identifier;
92     dm_shw_data_value_t data_value;
93 } dm_shw_data_t;
94 
95 typedef struct {
96     char *identifier;
97     int input_data_number;       /* input_data Number */
98     dm_shw_data_t *input_datas;  /* input_data array, type is dm_shw_data_t */
99     int output_data_number;      /* ouput_data Number */
100     dm_shw_data_t *output_datas; /* output_data array, type is dm_shw_data_t */
101 } dm_shw_event_t;
102 
103 typedef struct {
104     char *identifier;            /* synchronized or asynchronized */
105     int input_data_number;       /* input_data_number */
106     dm_shw_data_t *input_datas;  /* input_data array, type is dm_shw_data_t */
107     int output_data_number;      /* ouput_data Number */
108     dm_shw_data_t *output_datas; /* output_data array, type is dm_shw_data_t */
109 } dm_shw_service_t;
110 
111 typedef struct {
112     int property_number;
113     dm_shw_data_t *properties; /* property array, type is dm_shw_data_t */
114     int event_number;
115     dm_shw_event_t *events; /* event array, type is dm_shw_event_t */
116     int service_number;
117     dm_shw_service_t *services; /* service array, type is dm_shw_service_t */
118 } dm_shw_t;
119 
120 /**
121  * @brief Create TSL struct from TSL string.
122  *        This function used to parse TSL string into TSL struct.
123  *
124  * @param tsl. The TSL string in JSON format.
125  * @param tsl_len. The length of tsl
126  * @param shadow. The pointer of TSL Struct pointer, will be malloc memory.
127  *                This memory should be free by dm_shw_destroy.
128  *
129  * @return success or fail.
130  *
131  */
132 int dm_shw_create(_IN_ iotx_dm_tsl_type_t type, _IN_ const char *tsl,
133                   _IN_ int tsl_len, _OU_ dm_shw_t **shadow);
134 
135 /**
136  * @brief Get property from TSL struct.
137  *        This function used to get property from TSL struct.
138  *
139  * @param shadow. The pointer of TSL Struct.
140  * @param key. The property compound string, format decided by data type of
141  * property as follows: int,float,double,text,enum,date,bool type: property_id
142  *        array type: property_id(array)[index]
143  *        struct type: property_id(struct).property_id or
144  * property_id(struct).property_id[index]
145  *
146  * @param key_len. The length of key.
147  * @param property. The property in TSL Struct.
148  *
149  * @return success or fail.
150  *
151  */
152 int dm_shw_get_property_data(_IN_ dm_shw_t *shadow, _IN_ char *key,
153                              _IN_ int key_len, _OU_ void **data);
154 
155 int dm_shw_get_service_input_output_data(_IN_ dm_shw_data_target_e type,
156                                          _IN_ dm_shw_t *shadow, _IN_ char *key,
157                                          _IN_ int key_len, _OU_ void **data);
158 
159 /**
160  * @brief Get event output data from TSL struct.
161  *        This function used to get event output data from TSL struct.
162  *
163  * @param shadow. The pointer of TSL Struct.
164  * @param key. The property compound string, format decided by data type of
165  * property as follows: int,float,double,text,enum,date,bool type: property_id
166  *        array type: property_id(array)[index]
167  *        struct type: property_id(struct).property_id or
168  * property_id(struct).property_id[index]
169  *
170  * @param key_len. The length of key.
171  * @param property. The property in TSL Struct.
172  *
173  * @return success or fail.
174  *
175  */
176 int dm_shw_get_event_output_data(_IN_ dm_shw_t *shadow, _IN_ char *key,
177                                  _IN_ int key_len, _OU_ void **data);
178 
179 /**
180  * @brief Get property type from TSL struct.
181  *        This function used to get property type from TSL struct.
182  *
183  * @param property. The handle of property.
184  * @param type. The data type of property
185  *
186  *
187  * @return success or fail.
188  *
189  */
190 int dm_shw_get_data_type(_IN_ void *data, _OU_ dm_shw_data_type_e *type);
191 
192 /**
193  * @brief Get event from TSL struct.
194  *        This function used to get property from TSL struct.
195  *
196  * @param service. The handle of event.
197  * @param key. The property compound string, format decided by data type of
198  * property as follows: int,float,double,text,enum,date,bool type: event_id
199  *
200  * @param key_len. The length of key.
201  * @param property. The event in TSL Struct.
202  *
203  * @return success or fail.
204  *
205  */
206 int dm_shw_get_event(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len,
207                      _OU_ void **event);
208 
209 /**
210  * @brief Get service from TSL struct.
211  *        This function used to get property from TSL struct.
212  *
213  * @param shadow. The pointer of TSL Struct.
214  * @param key. The property compound string, format decided by data type of
215  * property as follows: int,float,double,text,enum,date,bool type: service_id
216  *
217  * @param key_len. The length of key.
218  * @param property. The service in TSL Struct.
219  *
220  * @return success or fail.
221  *
222  */
223 int dm_shw_get_service(_IN_ dm_shw_t *shadow, _IN_ char *key, _IN_ int key_len,
224                        _OU_ void **service);
225 
226 /**
227  * @brief Get property number from TSL struct.
228  *        This function used to get property number from TSL struct.
229  *
230  * @param shadow. The pointer of TSL Struct.
231  * @param number. The property number in TSL Struct.
232  *
233  * @return success or fail.
234  *
235  */
236 int dm_shw_get_property_number(_IN_ dm_shw_t *shadow, _OU_ int *number);
237 
238 /**
239  * @brief Get service number from TSL struct.
240  *        This function used to get property number from TSL struct.
241  *
242  * @param shadow. The pointer of TSL Struct.
243  * @param number. The service number in TSL Struct.
244  *
245  * @return success or fail.
246  *
247  */
248 int dm_shw_get_service_number(_IN_ dm_shw_t *shadow, _OU_ int *number);
249 
250 /**
251  * @brief Get event number from TSL struct.
252  *        This function used to get property number from TSL struct.
253  *
254  * @param shadow. The pointer of TSL Struct.
255  * @param number. The event number in TSL Struct.
256  *
257  * @return success or fail.
258  *
259  */
260 int dm_shw_get_event_number(_IN_ dm_shw_t *shadow, _OU_ int *number);
261 
262 /**
263  * @brief Get property reference from TSL struct by index.
264  *        This function used to get property reference from TSL struct by index.
265  *
266  * @param shadow. The pointer of TSL Struct.
267  * @param index. The index of property
268  * @param property. The property reference in TSL Struct.
269  *
270  * @return success or fail.
271  *
272  */
273 int dm_shw_get_property_by_index(_IN_ dm_shw_t *shadow, _IN_ int index,
274                                  _OU_ void **property);
275 
276 /**
277  * @brief Get service reference from TSL struct by index.
278  *        This function used to get service reference from TSL struct by index.
279  *
280  * @param shadow. The pointer of TSL Struct.
281  * @param index. The index of service
282  * @param service. The service reference in TSL Struct.
283  *
284  * @return success or fail.
285  *
286  */
287 int dm_shw_get_service_by_index(_IN_ dm_shw_t *shadow, _IN_ int index,
288                                 _OU_ void **service);
289 
290 /**
291  * @brief Get event reference from TSL struct by index.
292  *        This function used to get event reference from TSL struct by index.
293  *
294  * @param shadow. The pointer of TSL Struct.
295  * @param index. The index of event
296  * @param event. The event reference in TSL Struct.
297  *
298  * @return success or fail.
299  *
300  */
301 int dm_shw_get_event_by_index(_IN_ dm_shw_t *shadow, _IN_ int index,
302                               _OU_ void **event);
303 
304 /**
305  * @brief Get service reference from TSL struct by identifier.
306  *        This function used to get service reference from TSL struct by
307  * identifier.
308  *
309  * @param shadow. The pointer of TSL Struct.
310  * @param identifier. The identifier of event
311  * @param service. The service reference in TSL Struct.
312  *
313  * @return success or fail.
314  *
315  */
316 int dm_shw_get_service_by_identifier(_IN_ dm_shw_t *shadow,
317                                      _IN_ char *identifier,
318                                      _OU_ void **service);
319 
320 /**
321  * @brief Get event reference from TSL struct by identifier.
322  *        This function used to get event reference from TSL struct by
323  * identifier.
324  *
325  * @param shadow. The pointer of TSL Struct.
326  * @param identifier. The identifier of event
327  * @param event. The event reference in TSL Struct.
328  *
329  * @return success or fail.
330  *
331  */
332 int dm_shw_get_event_by_identifier(_IN_ dm_shw_t *shadow, _IN_ char *identifier,
333                                    _OU_ void **event);
334 
335 /**
336  * @brief Get property identifier from TSL struct by service handle.
337  *        This function used to get property identifier from TSL struct by
338  * service handle.
339  *
340  * @param service. The handle of property.
341  * @param method. The reference to property identifier in TSL Struct
342  *
343  * @return success or fail.
344  *
345  */
346 int dm_shw_get_property_identifier(_IN_ void *property, _OU_ char **identifier);
347 
348 /**
349  * @brief Get service method from TSL struct by service handle.
350  *        This function used to get service method from TSL struct by service
351  * handle.
352  *
353  * @param service. The handle of service.
354  * @param method. Generate method from service identifier
355  *
356  * @return success or fail.
357  *
358  */
359 int dm_shw_get_service_method(_IN_ void *service, _OU_ char **method);
360 
361 /**
362  * @brief Get event method from TSL struct by event handle.
363  *        This function used to get event method from TSL struct by event
364  * handle.
365  *
366  * @param service. The handle of event.
367  * @param method. Generate method from event identifier
368  *
369  * @return success or fail.
370  *
371  */
372 int dm_shw_get_event_method(_IN_ void *event, _OU_ char **method);
373 
374 /**
375  * @brief Set Property Value Into TSL Struct.
376  *        This function used to set property value into TSL Struct.
377  *
378  * @param tsl. The pointer of TSL Struct.
379  * @param key. The property compound string, format decided by data type of
380  * property as follows: int,float,double,text,enum,date,bool type: property_id
381  *        array type: property_id(array)[index]
382  *        struct type: property_id(struct).property_id or
383  * property_id(struct).property_id[index]
384  *
385  * @param key_len. The length of key
386  * @param value. The value to be set, data type decided by data type of property
387  * as follows: int: int*, float: float*, double: double*, text: char*, enum:
388  * int*, date: char*, bool: int* attention! value can be NULL to clear property
389  * value
390  * @param value_len. The length of value, only be used when type is text or data
391  *
392  * @return success or fail.
393  *
394  */
395 int dm_shw_set_property_value(_IN_ dm_shw_t *shadow, _IN_ char *key,
396                               _IN_ int key_len, _IN_ void *value,
397                               _IN_ int value_len);
398 
399 /**
400  * @brief Get Property Value From TSL Struct.
401  *        This function used to get property value from TSL Struct.
402  *
403  * @param tsl. The pointer of TSL Struct.
404  * @param key. The property compound string, format decided by data type of
405  * property as follows: int,float,double,text,enum,date,bool type: property_id
406  *        array type: property_id(array)[index]
407  *        struct type: property_id(struct).property_id or
408  * property_id(struct).property_id[index]
409  *
410  * @param key_len. The length of key
411  * @param value. The variable to store value, data type decided by data type of
412  * property as follows: int: int*, float: float*, double: double*, text: char**,
413  * enum: int*, date: char**, bool: int* attention! value can not be NULL
414  *
415  * @warning if data type is text or date, *value well be end with '\0'.
416  *          the memory allocated to *value must be free by user.
417  *
418  * @return success or fail.
419  *
420  */
421 int dm_shw_get_property_value(_IN_ dm_shw_t *shadow, _IN_ char *key,
422                               _IN_ int key_len, _OU_ void *value);
423 
424 /**
425  * @brief Set event output value into TSL struct.
426  *        This function used to set event output value into TSL struct.
427  *
428  * @param tsl. The pointer of TSL Struct.
429  * @param key. The property compound string, format decided by data type of
430  * property as follows: int,float,double,text,enum,date,bool type:
431  * event_id.event_data_id array type: event_id.event_data_id(array)[index]
432  *        struct type: event_id.event_data_id(struct).property_id
433  *                     or event_id.event_data_id(struct).property_id[index]
434  *
435  * @param key_len. The length of key
436  * @param value. The value to be set, data type decided by data type of property
437  * as follows: int: int*, float: float*, double: double*, text: char*, enum:
438  * int*, date: char*, bool: int* attention! value can be NULL to clear property
439  * value
440  * @param value_len. The length of value, only be used when type is text or data
441  *
442  * @return success or fail.
443  *
444  */
445 int dm_shw_set_event_output_value(_IN_ dm_shw_t *shadow, _IN_ char *key,
446                                   _IN_ int key_len, _IN_ void *value,
447                                   _IN_ int value_len);
448 
449 /**
450  * @brief Get event output value from TSL struct.
451  *        This function used to get event output value from TSL struct.
452  *
453  * @param tsl. The pointer of TSL Struct.
454  * @param key. The property compound string, format decided by data type of
455  * property as follows: int,float,double,text,enum,date,bool type:
456  * event_id.event_data_id array type: event_id.event_data_id(array)[index]
457  *        struct type: event_id.event_data_id(struct).property_id
458  *                     or event_id.event_data_id(struct).property_id[index]
459  *
460  * @param key_len. The length of key
461  * @param value. The variable to store value, data type decided by data type of
462  * property as follows: int: int*, float: float*, double: double*, text: char**,
463  * enum: int*, date: char**, bool: int* attention! value can not be NULL
464  *
465  * @warning if data type is text or date, *value well be end with '\0'.
466  *          the memory allocated to *value must be free by user.
467  *
468  * @return success or fail.
469  *
470  */
471 int dm_shw_get_event_output_value(_IN_ dm_shw_t *shadow, _IN_ char *key,
472                                   _IN_ int key_len, _IN_ void *value);
473 
474 int dm_shw_set_service_input_output_value(_IN_ dm_shw_data_target_e type,
475                                           _IN_ dm_shw_t *shadow, _IN_ char *key,
476                                           _IN_ int key_len, _IN_ void *value,
477                                           _IN_ int value_len);
478 int dm_shw_get_service_input_output_value(_IN_ dm_shw_data_target_e type,
479                                           _IN_ dm_shw_t *shadow, _IN_ char *key,
480                                           _IN_ int key_len, _IN_ void *value);
481 
482 /**
483  * @brief Get property payload from TSL struct.
484  *        This function used to get property payload from TSL struct.
485  *
486  * @param shadow. The pointer of TSL Struct
487  * @param identifier. The Property Identifier
488  * @param identifier_len. The Property Identifier Length
489  * @param lite. The pointer to json array where to store property value
490  *
491  * @warning The payload malloc by this function and need to be free manully.
492  *
493  * @return success or fail.
494  *
495  */
496 int dm_shw_assemble_property(_IN_ dm_shw_t *shadow, _IN_ char *identifier,
497                              _IN_ int identifier_len,
498                              _IN_ lite_cjson_item_t *lite);
499 
500 /**
501  * @brief Get event output payload from TSL struct.
502  *        This function used to get event output payload from TSL struct.
503  *
504  * @param shadow. The pointer of TSL Struct
505  * @param identifier. The Event Identifier
506  * @param identifier_len. The Event Identifier Length
507  * @param lite. The pointer to json array where to store event output value
508  *
509  * @warning The payload malloc by this function and need to be free manully.
510  *
511  * @return success or fail.
512  *
513  */
514 int dm_shw_assemble_event_output(_IN_ dm_shw_t *shadow, _IN_ char *identifier,
515                                  _IN_ int identifier_len,
516                                  _IN_ lite_cjson_item_t *lite);
517 
518 /**
519  * @brief Get service output payload from TSL struct.
520  *        This function used to get service output payload from TSL struct.
521  *
522  * @param shadow. The pointer of TSL Struct
523  * @param identifier. The Service Identifier
524  * @param identifier_len. The Service Identifier Length
525  * @param lite. The pointer to json array where to store service output value
526  *
527  * @warning The payload malloc by this function and need to be free manully.
528  *
529  * @return success or fail.
530  *
531  */
532 int dm_shw_assemble_service_output(_IN_ dm_shw_t *shadow, _IN_ char *identifier,
533                                    _IN_ int identifier_len,
534                                    _IN_ lite_cjson_item_t *lite);
535 
536 /**
537  * @brief Free TSL struct.
538  *        This function used to free TSL struct.
539  *
540  * @param shadow. The pointer of TSL Struct.
541  *
542  * @return success or fail.
543  *
544  */
545 void dm_shw_destroy(_IN_ dm_shw_t **shadow);
546 
547 #endif
548 #endif
549 
550