1 #ifndef __GENIE_TRANSPORT_H__
2 #define __GENIE_TRANSPORT_H__
3 
4 #include "misc/dlist.h"
5 
6 #define RECV_MSG_TID_QUEUE_SIZE 5
7 #define GENIE_TRANSPORT_DEDUPLICATE_DURATION (6000) //unit ms
8 
9 #define TMALL_GENIE_UADDR_START 0x0001
10 #define TMALL_GENIE_UADDR_END 0x0010
11 
12 #define GENIE_TRANSPORT_TID_MIN (0x80)
13 #define GENIE_TRANSPORT_TID_MAX (0xBF)
14 
15 #define GENIE_TRANSPORT_DEFAULT_TTL (3)
16 
17 #ifdef CONFIG_GENIE_MESH_GLP
18 #define GENIE_TRANSPORT_DEFAULT_RETRY_COUNT (5) //Total six times
19 #else
20 #define GENIE_TRANSPORT_DEFAULT_RETRY_COUNT (2) //Total three times
21 #endif
22 
23 #define GENIE_TRANSPORT_EACH_PDU_TIMEOUT (600) //Unit ms
24 #define GENIE_TRANSPORT_EACH_PDU_SIZE (12)
25 
26 #ifndef CONFIG_VENDOR_SEND_MSG_MAX
27 #define CONFIG_VENDOR_SEND_MSG_MAX 8
28 #endif
29 
30 typedef struct _genie_transport_tid_queue_s
31 {
32     uint8_t tid;
33     uint8_t elemid;
34     uint16_t addr;
35     uint32_t time;
36 } genie_transport_tid_queue_t;
37 
38 typedef enum
39 {
40     MESH_SUCCESS = 0,
41     MESH_TID_REPEAT,
42     MESH_ANALYZE_SIZE_ERROR,
43     MESH_ANALYZE_ARGS_ERROR,
44     MESH_SET_TRANSTION_ERROR,
45 } E_MESH_ERROR_TYPE;
46 
47 /**
48  * p_elem: pointer to the element which the messsage want to be sent to
49  * retry: retry counts before desired confirm message received
50  * * fill negative value if retransmission is not necessary
51  * * fill positive value if retransmission is needed
52  * * * will be round in this scope - [VENDOR_MODEL_MSG_DFT_RETRY_TIMES, VENDOR_MODEL_MSG_MAX_RETRY_TIMES]
53  * retry_period: wait for retry_period before retransmit the message, in unit of ms
54  * opid: hightest byte in Opcode defined in the vendor model spec designed by Alibaba IoT Group
55  * * e.g. for Vendor Message Attr Get message, Opcode is 0xD001A8, corresponding opid is 0xD0
56  * * refer to the marco named VENDOR_OP_ATTR_xxxx
57  * tid:
58  * * if the message is with type of VENDOR_OP_ATTR_CONFIME or VENDOR_OP_ATTR_CONFIME_TG,
59  * * tid should be filled with the replied message's tid
60  * len: payload length
61  * data: pointer to the vendor message's payload
62  * */
63 typedef enum _transport_result_e
64 {
65     SEND_RESULT_SUCCESS = 0,
66     SEND_RESULT_TIMEOUT
67 } transport_result_e;
68 
69 typedef int (*transport_result_cb)(transport_result_e result);
70 
71 typedef struct _genie_transport_model_param_s
72 {
73     struct bt_mesh_elem *p_elem;
74     struct bt_mesh_model *p_model; //It is current model
75     transport_result_cb result_cb;
76     uint8_t retry;
77     uint8_t opid;
78     uint8_t tid;
79     uint16_t dst_addr;
80     uint16_t retry_period;
81     uint16_t len;
82     uint8_t *data;
83 } genie_transport_model_param_t;
84 
85 typedef struct _genie_transport_node_s
86 {
87     sys_dnode_t node;
88     uint8_t left_retry;
89     long long timeout;
90     genie_transport_model_param_t msg;
91 } genie_transport_node_t;
92 
93 typedef struct _genie_transport_payload_s
94 {
95     uint8_t opid;
96     uint8_t tid;
97     uint8_t retry_cnt;
98     uint16_t dst_addr;
99     uint8_t *p_payload;
100     uint16_t payload_len;
101     transport_result_cb result_cb;
102 } genie_transport_payload_param_t;
103 
104 /**
105  * @brief check whether there is this tid in record, and record it if not.
106  * @param[in] src_addr indicates the device which hold this tid.
107  * @param[in] tid
108  * @return MESH_SUCCESS means successed, otherwise failed.
109  */
110 E_MESH_ERROR_TYPE
111 genie_transport_check_tid(uint16_t src_addr, uint8_t tid, uint8_t elem_id);
112 
113 void genie_transport_src_addr_set(uint16_t src_addr);
114 
115 uint16_t genie_transport_src_addr_get();
116 
117 /**
118  * @brief send the vendor model message
119  * @param[in] p_vendor_msg refers to the message to be sent
120  * @return 0 for success; negative for failure
121  */
122 int genie_transport_send_model(genie_transport_model_param_t *p_vendor_msg);
123 
124 int genie_transport_send_payload(genie_transport_payload_param_t *payload_param);
125 
126 uint8_t genie_transport_gen_tid(void);
127 
128 uint8_t genie_transport_get_seg_count(uint16_t msg_len);
129 
130 bool genie_transport_tx_in_progress(void);
131 
132 /** @def genie_transport_ack
133  *
134  *  @brief check received vendor message's tid
135  *
136  *  @param pointer to send_list, tid of the received vendor model message
137  *
138  *  @return 0 for success; negative for failure
139  */
140 int genie_transport_ack(uint8_t tid);
141 
142 void genie_transport_init(void);
143 
144 #endif
145