1 /*
2  * NAN Discovery Engine
3  * Copyright (c) 2024, Qualcomm Innovation Center, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef NAN_DE_H
10 #define NAN_DE_H
11 
12 #include "nan.h"
13 
14 /* Maximum number of active local publish and subscribe instances */
15 #ifndef NAN_DE_MAX_SERVICE
16 #define NAN_DE_MAX_SERVICE 20
17 #endif /* NAN_DE_MAX_SERVICE */
18 
19 struct nan_de;
20 
21 enum nan_de_reason {
22 	NAN_DE_REASON_TIMEOUT,
23 	NAN_DE_REASON_USER_REQUEST,
24 	NAN_DE_REASON_FAILURE,
25 };
26 
27 struct nan_callbacks {
28 	void *ctx;
29 
30 	int (*tx)(void *ctx, unsigned int freq, unsigned int wait_time,
31 		  const u8 *dst, const u8 *src, const u8 *bssid,
32 		  const struct wpabuf *buf);
33 	int (*listen)(void *ctx, unsigned int freq, unsigned int duration);
34 
35 	/* NAN DE Events */
36 	void (*discovery_result)(void *ctx, int subscribe_id,
37 				 enum nan_service_protocol_type srv_proto_type,
38 				 const u8 *ssi, size_t ssi_len,
39 				 int peer_publish_id,
40 				 const u8 *peer_addr, bool fsd, bool fsd_gas);
41 
42 	void (*replied)(void *ctx, int publish_id, const u8 *peer_addr,
43 			int peer_subscribe_id,
44 			enum nan_service_protocol_type srv_proto_type,
45 			const u8 *ssi, size_t ssi_len);
46 
47 	void (*publish_terminated)(void *ctx, int publish_id,
48 				    enum nan_de_reason reason);
49 
50 	void (*subscribe_terminated)(void *ctx, int subscribe_id,
51 				     enum nan_de_reason reason);
52 
53 	void (*receive)(void *ctx, int id, int peer_instance_id,
54 			const u8 *ssi, size_t ssi_len,
55 			const u8 *peer_addr);
56 
57 	void (*process_p2p_usd_elems)(void *ctx, const u8 *buf,
58 				      u16 buf_len, const u8 *peer_addr,
59 				      unsigned int freq);
60 };
61 
62 bool nan_de_is_nan_network_id(const u8 *addr);
63 bool nan_de_is_p2p_network_id(const u8 *addr);
64 struct nan_de * nan_de_init(const u8 *nmi, bool offload, bool ap,
65 			    unsigned int max_listen,
66 			    const struct nan_callbacks *cb);
67 void nan_de_flush(struct nan_de *de);
68 void nan_de_deinit(struct nan_de *de);
69 
70 void nan_de_listen_started(struct nan_de *de, unsigned int freq,
71 			   unsigned int duration);
72 void nan_de_listen_ended(struct nan_de *de, unsigned int freq);
73 void nan_de_tx_status(struct nan_de *de, unsigned int freq, const u8 *dst);
74 void nan_de_tx_wait_ended(struct nan_de *de);
75 
76 void nan_de_rx_sdf(struct nan_de *de, const u8 *peer_addr, const u8 *a3,
77 		   unsigned int freq, const u8 *buf, size_t len);
78 const u8 * nan_de_get_service_id(struct nan_de *de, int id);
79 
80 struct nan_publish_params {
81 	/* configuration_parameters */
82 
83 	/* Publish type */
84 	bool unsolicited;
85 	bool solicited;
86 
87 	/* Solicited transmission type */
88 	bool solicited_multicast;
89 
90 	/* Time to live (in seconds); 0 = one TX only */
91 	unsigned int ttl;
92 
93 	/* Event conditions */
94 	bool disable_events;
95 
96 	/* Further Service Discovery flag */
97 	bool fsd;
98 
99 	/* Further Service Discovery function */
100 	bool fsd_gas;
101 
102 	/* Default frequency (defaultPublishChannel) */
103 	unsigned int freq;
104 
105 	/* Multi-channel frequencies (publishChannelList) */
106 	const int *freq_list;
107 
108 	/* Announcement period in ms; 0 = use default */
109 	unsigned int announcement_period;
110 };
111 
112 /* Returns -1 on failure or >0 publish_id */
113 int nan_de_publish(struct nan_de *de, const char *service_name,
114 		   enum nan_service_protocol_type srv_proto_type,
115 		   const struct wpabuf *ssi, const struct wpabuf *elems,
116 		   struct nan_publish_params *params, bool p2p);
117 
118 void nan_de_cancel_publish(struct nan_de *de, int publish_id);
119 
120 int nan_de_update_publish(struct nan_de *de, int publish_id,
121 			  const struct wpabuf *ssi);
122 
123 int nan_de_unpause_publish(struct nan_de *de, int publish_id,
124 			   u8 peer_instance_id, const u8 *peer_addr);
125 
126 struct nan_subscribe_params {
127 	/* configuration_parameters */
128 
129 	/* Subscribe type */
130 	bool active;
131 
132 	/* Time to live (in seconds); 0 = until first result */
133 	unsigned int ttl;
134 
135 	/* Selected frequency */
136 	unsigned int freq;
137 
138 	/* Multi-channel frequencies (publishChannelList) */
139 	const int *freq_list;
140 
141 	/* Query period in ms; 0 = use default */
142 	unsigned int query_period;
143 };
144 
145 /* Returns -1 on failure or >0 subscribe_id */
146 int nan_de_subscribe(struct nan_de *de, const char *service_name,
147 		     enum nan_service_protocol_type srv_proto_type,
148 		     const struct wpabuf *ssi, const struct wpabuf *elems,
149 		     struct nan_subscribe_params *params, bool p2p);
150 
151 void nan_de_cancel_subscribe(struct nan_de *de, int subscribe_id);
152 
153 /* handle = publish_id or subscribe_id
154  * req_instance_id = peer publish_id or subscribe_id */
155 int nan_de_transmit(struct nan_de *de, int handle,
156 		    const struct wpabuf *ssi, const struct wpabuf *elems,
157 		    const u8 *peer_addr, u8 req_instance_id);
158 
159 #endif /* NAN_DE_H */
160