1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _ALCS_MQTT_H_
6 #define _ALCS_MQTT_H_
7 
8 #include "alcs_internal.h"
9 
10 typedef enum {
11     ALCS_MQTT_STATUS_SUCCESS,
12     ALCS_MQTT_STATUS_ERROR
13 } alcs_mqtt_status_e;
14 
15 typedef struct {
16     CoAPContext *coap_ctx;
17     char product_key[IOTX_PRODUCT_KEY_LEN + 1];
18     char device_name[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 2];
19     uint32_t send_id;
20 } alcs_mqtt_ctx_t;
21 
22 #define ALCS_MQTT_PREFIX                         "/sys/%s/%s"
23 
24 #define ALCS_MQTT_THING_LAN_PREFIX_GET_REPLY_FMT "/thing/lan/prefix/get_reply"
25 #define ALCS_MQTT_THING_LAN_PREFIX_GET_FMT       "/thing/lan/prefix/get"
26 #define ALCS_MQTT_THING_LAN_PREFIX_UPDATE_FMT    "/thing/lan/prefix/update"
27 #define ALCS_MQTT_THING_LAN_PREFIX_UPDATE_REPLY_FMT \
28     "/thing/lan/prefix/update_reply"
29 #define ALCS_MQTT_THING_LAN_PREFIX_BLACKLIST_UPDATE_FMT \
30     "/thing/lan/blacklist/update"
31 #define ALCS_MQTT_THING_LAN_PREFIX_BLACKLIST_UPDATE_REPLY_FMT \
32     "/thing/lan/blacklist/update_reply"
33 
34 #define ALCS_MQTT_THING_ALCS_REQUEST                                           \
35     "{\"id\":\"%d\",\"version\":\"1.0\",\"params\":\"{}\",\"method\":\"thing." \
36     "lan.prefix.get\"}"
37 #define ALCS_MQTT_THING_LAN_PREFIX_RESPONSE_FMT \
38     "{\"id\": \"%d\", \"code\": %d, \"data\": %s}"
39 #define ALCS_MQTT_THING_ALCS_SUBDEV_REQUEST                                   \
40     "{\"id\":\"%d\",\"version\":\"1.0\",\"params\":{\"productKey\":\"%.*s\"," \
41     "\"deviceName\":\"%.*s\"},\"method\":\"thing.lan.prefix.get\"}"
42 
43 #define ALCS_MQTT_TOPIC_MAX_LEN        (128)
44 
45 #define ALCS_MQTT_JSON_KEY_PRODUCT_KEY "productKey"
46 #define ALCS_MQTT_JSON_KEY_DEVICE_NAME "deviceName"
47 #define ALCS_MQTT_JSON_KEY_PREFIX      "prefix"
48 #define ALCS_MQTT_JSON_KEY_SECRET      "deviceSecret"
49 #define ALCS_MQTT_JSON_KEY_BLACK       "blacklist"
50 
51 #define ALCS_MQTT_PREFIX_MAX_LEN       (40)
52 #define ALCS_MQTT_SECRET_MAX_LEN       (40)
53 #define ALCS_MQTT_BLACK_MAX_LEN        (100)
54 
55 alcs_mqtt_status_e alcs_mqtt_init(void *handle, char *product_key,
56                                   char *device_name);
57 alcs_mqtt_status_e alcs_mqtt_deinit(void *handle, char *product_key,
58                                     char *device_name);
59 alcs_mqtt_status_e alcs_mqtt_blacklist_update(void *ctx);
60 alcs_mqtt_status_e alcs_mqtt_prefixkey_update(void *ctx);
61 void alcs_mqtt_add_srv_key(const char *prefix, const char *secret);
62 alcs_mqtt_status_e alcs_mqtt_prefix_secret_load(const char *pk, uint16_t pk_len,
63                                                 const char *dn, uint16_t dn_len,
64                                                 char *prefix, char *secret);
65 alcs_mqtt_status_e alcs_mqtt_prefix_secret_del(const char *pk, uint16_t pk_len,
66                                                const char *dn, uint16_t dn_len);
67 alcs_mqtt_status_e alcs_mqtt_subdev_prefix_get(const char *product_key,
68                                                const char *device_name);
69 alcs_mqtt_status_e alcs_prefixkey_get(const char *product_key,
70                                       const char *device_name);
71 
72 #endif
73