1 /*
2  * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include "esp_log.h"
10 #include "esp_err.h"
11 #include "lwip/inet.h"
12 #include "esp_wifi_types.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #define WIFI_NAN_CONFIG_DEFAULT() { \
19     .op_channel = 6, \
20     .master_pref = 2, \
21     .scan_time = 3, \
22     .warm_up_sec = 5, \
23 };
24 
25 #define NDP_STATUS_ACCEPTED     1
26 #define NDP_STATUS_REJECTED     2
27 
28 #define NAN_MAX_PEERS_RECORD    15
29 #define ESP_NAN_PUBLISH         1
30 #define ESP_NAN_SUBSCRIBE       2
31 
32 /** Parameters of a peer service record */
33 struct nan_peer_record {
34     uint8_t peer_svc_id;   /**< Identifier of Peer's service */
35     uint8_t own_svc_id;    /**< Identifier of own service associated with Peer */
36     uint8_t peer_nmi[6];   /**< Peer's NAN Management Interface address */
37     uint8_t peer_svc_type; /**< Peer's service type (Publish/Subscribe) */
38     uint8_t ndp_id;        /**< Specifies if the peer has any active datapath */
39     uint8_t peer_ndi[6];   /**< Peer's NAN Data Interface address, only valid when ndp_id is non-zero */
40 };
41 
42 /**
43   * @brief      Start NAN Discovery with provided configuration
44   *
45   * @attention  This API should be called after esp_wifi_init().
46   *
47   * @param      nan_cfg  NAN related parameters to be configured.
48   *
49   * @return
50   *    - ESP_OK: succeed
51   *    - others: failed
52   */
53 esp_err_t esp_wifi_nan_start(const wifi_nan_config_t *nan_cfg);
54 
55 /**
56   * @brief      Stop NAN Discovery, end NAN Services and Datapaths
57   *
58   * @return
59   *    - ESP_OK: succeed
60   *    - others: failed
61   */
62 esp_err_t esp_wifi_nan_stop(void);
63 
64 /**
65   * @brief      Start Publishing a service to the NAN Peers in vicinity
66   *
67   * @attention  This API should be called after esp_wifi_nan_start().
68   *
69   * @param      publish_cfg  Configuration parameters for publishing a service.
70   * @param      ndp_resp_needed Setting this true will require user response for every NDP Req using esp_wifi_nan_datapath_resp API.
71   *
72   * @return
73   *    - non-zero: Publish service identifier
74   *    - zero: failed
75   */
76 uint8_t esp_wifi_nan_publish_service(const wifi_nan_publish_cfg_t *publish_cfg, bool ndp_resp_needed);
77 
78 /**
79   * @brief      Subscribe for a service within the NAN cluster
80   *
81   * @attention  This API should be called after esp_wifi_nan_start().
82   *
83   * @param      subscribe_cfg  Configuration parameters for subscribing for a service.
84   *
85   * @return
86   *    - non-zero: Subscribe service identifier
87   *    - zero: failed
88   */
89 uint8_t esp_wifi_nan_subscribe_service(const wifi_nan_subscribe_cfg_t *subscribe_cfg);
90 
91 /**
92   * @brief      Send a follow-up message to the NAN Peer with matched service
93   *
94   * @attention  This API should be called after a NAN service is discovered due to a match.
95   *
96   * @param      fup_params  Configuration parameters for sending a Follow-up message.
97   *
98   * @return
99   *    - ESP_OK: succeed
100   *    - others: failed
101   */
102 esp_err_t esp_wifi_nan_send_message(wifi_nan_followup_params_t *fup_params);
103 
104 /**
105   * @brief      Cancel a NAN service
106   *
107   * @param      service_id Publish/Subscribe service id to be cancelled.
108   *
109   * @return
110   *    - ESP_OK: succeed
111   *    - others: failed
112   */
113 esp_err_t esp_wifi_nan_cancel_service(uint8_t service_id);
114 
115 /**
116   * @brief      Send NAN Datapath Request to a NAN Publisher with matched service
117   *
118   * @attention  This API should be called by the Subscriber after a match occurs with a Publisher.
119   *
120   * @param      req  NAN Datapath Request parameters.
121   *
122   * @return
123   *    - non-zero NAN Datapath identifier: If NAN datapath req was accepted by publisher
124   *    - zero: If NAN datapath req was rejected by publisher or a timeout occurs
125   */
126 uint8_t esp_wifi_nan_datapath_req(wifi_nan_datapath_req_t *req);
127 
128 /**
129   * @brief      Respond to a NAN Datapath request with Accept or Reject
130   *
131   * @attention  This API should be called if ndp_auto_accept is not set True by the Publisher and
132   *             a WIFI_EVENT_NDP_INDICATION event is received due to an incoming NDP request.
133   *
134   * @param      resp  NAN Datapath Response parameters.
135   *
136   * @return
137   *    - ESP_OK: succeed
138   *    - others: failed
139   */
140 esp_err_t esp_wifi_nan_datapath_resp(wifi_nan_datapath_resp_t *resp);
141 
142 /**
143   * @brief      Terminate a NAN Datapath
144   *
145   * @param      req  NAN Datapath end request parameters.
146   *
147   * @return
148   *    - ESP_OK: succeed
149   *    - others: failed
150   */
151 esp_err_t esp_wifi_nan_datapath_end(wifi_nan_datapath_end_req_t *req);
152 
153 /**
154   * @brief      Get IPv6 Link Local address using MAC address
155   *
156   * @param[out]      ip6  Derived IPv6 Link Local address.
157   * @param[in]       mac_addr  Input MAC Address.
158   */
159 void esp_wifi_nan_get_ipv6_linklocal_from_mac(ip6_addr_t *ip6, uint8_t *mac_addr);
160 
161 /**
162  * brief         Get own Service information from Service ID OR Name.
163  *
164  * @attention    If service information is to be fetched from service name, set own_svc_id as zero.
165  *
166  * @param[inout] own_svc_id As input, it indicates Service ID to search for.
167  *                          As output, it indicates Service ID of the service found using Service Name.
168  * @param[inout] svc_name   As input, it indicates Service Name to search for.
169  *                          As output, it indicates Service Name of the service found using Service ID.
170  * @param[out]   num_peer_records  Number of peers discovered by corresponding service.
171  * @return
172  *   - ESP_OK: succeed
173  *   - ESP_FAIL: failed
174  */
175 esp_err_t esp_wifi_nan_get_own_svc_info(uint8_t *own_svc_id, char *svc_name, int *num_peer_records);
176 
177 /**
178  * brief         Get a list of Peers discovered by the given Service.
179  *
180  * @param[inout] num_peer_records As input param, it stores max peers peer_record can hold.
181  *               As output param, it specifies the actual number of peers this API returns.
182  * @param        own_svc_id  Service ID of own service.
183  * @param[out]   peer_record Pointer to first peer record.
184  * @return
185  *   - ESP_OK: succeed
186  *   - ESP_FAIL: failed
187  */
188 esp_err_t esp_wifi_nan_get_peer_records(int *num_peer_records, uint8_t own_svc_id, struct nan_peer_record *peer_record);
189 
190 /**
191  * brief         Find Peer's Service information using Peer MAC and optionally Service Name.
192  *
193  * @param       svc_name    Service Name of the published/subscribed service.
194  * @param       peer_mac    Peer's NAN Management Interface MAC address.
195  * @param[out]  peer_info   Peer's service information structure.
196  * @return
197  *   - ESP_OK: succeed
198  *   - ESP_FAIL: failed
199  */
200 esp_err_t esp_wifi_nan_get_peer_info(char *svc_name, uint8_t *peer_mac, struct nan_peer_record *peer_info);
201 
202 #ifdef __cplusplus
203 }
204 #endif
205