• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: Function related to wifi aware(NAN) feature.
15  */
16 
17 #include "hmac_sdp.h"
18 #include "hmac_mgmt_bss_comm.h"
19 #include "hmac_feature_dft.h"
20 #include "msg_sdp_rom.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 #undef THIS_FILE_ID
29 #define THIS_FILE_ID DIAG_FILE_ID_WIFI_HOST_SDP_TEST_C /* 后续要改为 SDP_TEST_C */
30 
31 #undef THIS_MOD_ID
32 #define THIS_MOD_ID DIAG_MOD_ID_WIFI_HOST
33 
34 #define SDP_MAX_SEND_FOLLOW_UP_NUM 1 /* 每回发送follow up to apk的次数 */
35 #define SDP_DATA_SSID_LEN_POS 1
36 #define SDP_DATA_SSID_POS 2
37 #define SDP_DATA_KEY_POS 3
38 #define SDP_SERVER_ID 9          /* 9 sdp的local id */
39 #define HMAC_FOLLOW_TIMEOUT 4000 /* follow-up报文发送间隔时间*ms */
40 #define SDP_ADD_PEER_MAC 1
41 #define SDP_DEL_PEER_MAC 0
42 
43 typedef enum {
44     WAL_SDP_TEST_TYPE_PRIV = 0,                 /* 测试同步命令 */
45     WAL_SDP_TEST_TYPE_DISTRIBUTION_ENABLE = 1,  /* 测试无感配网初始化 */
46     WAL_SDP_TEST_TYPE_DISTRIBUTION_DISABLE = 2, /* 测试无感配网去初始化 */
47     WAL_SDP_TEST_TYPE_SUBSCRIBE_SERVICE = 3,    /* 测试订阅 */
48     WAL_SDP_TEST_TYPE_SUBSCRIBE_CANCEL = 4,     /* 测试取消订阅 */
49     WAL_SDP_TEST_TYPE_PUBLISH_LIST = 5,         /* 列出发布的服务信息 */
50     WAL_SDP_TEST_TYPE_SUBSCRIBE_LIST = 6,       /* 列出订阅的服务信息 */
51     WAL_SDP_TEST_TYPE_BUTT
52 } wal_sdp_test;
53 
54 typedef enum {
55     NAN_DEMO_STATE_INIT = 0X10,
56     NAN_DEMO_STATE_PUBLISH_COMPLETE,
57     NAN_DEMO_STATE_FOLLOW_UP_COMPLETE,
58     NAN_DEMO_STATE_BSS_INFO,
59     NAN_DEMO_STATE_CONNECT_SUCCESS,
60     NAN_DEMO_STATE_CONNECT_SSID_NO_FIND,
61     NAN_DEMO_STATE_CONNECT_KEY_ERROR,
62     NAN_DEMO_STATE_BUTT
63 } nan_demo_state;
64 
65 typedef struct {
66     osal_u8 peer;
67     osal_u8 local;
68     osal_u8 mac[WLAN_MAC_ADDR_LEN];
69     osal_u8 rsv;
70 } hmac_sdp_handle;
71 
72 OAL_STATIC hmac_sdp_handle g_sdp_handle = { 0 };
73 OAL_STATIC hmac_vap_stru *g_sdp_mac_vap = NULL;
74 OAL_STATIC frw_timeout_stru g_follow_timer = { 0 };
75 OAL_STATIC osal_u16 g_sdp_init_peroid = 200; /* 默认200ms 支持命令行配置, 后续也可以作为api参数 */
76 
sdp_send_follow_up(osal_u8 * data,osal_u8 len)77 OAL_STATIC osal_void sdp_send_follow_up(osal_u8 *data, osal_u8 len)
78 {
79     oal_sdp_info info;
80     osal_u8 count = 0;
81     frw_msg msg_info;
82 
83     oam_info_log1(0, OAM_SF_SDP, "sdp_send_follow_up: send follow-up state=%02x", *data);
84 
85     if (memcpy_s(info.name, sizeof(info.name), g_sdp_handle.mac, sizeof(g_sdp_handle.mac)) != EOK) {
86         oam_warning_log0(0, OAM_SF_SDP, "sdp_send_follow_up:memcpy_s g_sdp_handle.mac failed");
87         return;
88     }
89     info.local_handle = g_sdp_handle.local;
90     info.peer_handle = g_sdp_handle.peer;
91     info.len = len;
92     while (count < SDP_MAX_SEND_FOLLOW_UP_NUM) {
93         info.data = oal_mem_alloc(OAL_MEM_POOL_ID_LOCAL, info.len, OAL_TRUE);
94         if (info.data == OAL_PTR_NULL) {
95             oam_warning_log1(0, OAM_SF_SDP, "sdp_send_follow_up:alloc data fail(size:%u)!", info.len);
96             return;
97         }
98         (osal_void)memset_s(info.data, info.len, 0, info.len);
99         if (memcpy_s(info.data, info.len, data, len) != EOK) {
100             oam_warning_log0(0, OAM_SF_SDP, "sdp_send_follow_up:memcpy_s data failed");
101             oal_mem_free(info.data, OAL_TRUE);
102             return;
103         }
104 
105         (osal_void)memset_s(&msg_info, sizeof(msg_info), 0, sizeof(msg_info));
106         msg_info.data = (osal_u8 *)&info;
107         msg_info.data_len = sizeof(info);
108         if (hmac_sdp_send_data(g_sdp_mac_vap, &msg_info) != OAL_SUCC) {
109             oam_warning_log0(0, OAM_SF_SDP, "sdp_send_follow_up:send follow-up data failed");
110         }
111 
112         count++;
113     }
114 }
115 
hmac_sdp_is_wifi_connected(hmac_vap_stru * hmac_vap)116 OAL_STATIC osal_bool hmac_sdp_is_wifi_connected(hmac_vap_stru *hmac_vap)
117 {
118     hmac_device_stru *hmac_device = OSAL_NULL;
119     hmac_vap_stru *hmac_vap_tmp = OSAL_NULL;
120     osal_u8 index;
121 
122     if (hmac_vap == NULL) {
123         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_is_wifi_connected:hmac_vap null.");
124         return OSAL_FALSE;
125     }
126 
127     hmac_device = hmac_res_get_mac_dev_etc(hmac_vap->device_id);
128     if (hmac_device == OSAL_NULL) {
129         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_is_wifi_connected:mac_device is null");
130         return OSAL_FALSE;
131     }
132 
133     if (hmac_device->vap_num == 0) {
134         oam_warning_log1(0, OAM_SF_SDP, "vap_id[%d] {hmac_sdp_is_wifi_connected::none vap.}", hmac_vap->vap_id);
135         return OSAL_FALSE;
136     }
137 
138     for (index = 0; index < hmac_device->vap_num; index++) {
139         hmac_vap_tmp = (hmac_vap_stru *)mac_res_get_hmac_vap(hmac_device->vap_id[index]);
140         if (hmac_vap_tmp == OSAL_NULL) {
141             oam_warning_log2(0, OAM_SF_SDP, "vap_id[%d] {hmac_sdp_is_wifi_connected::ap(%d) null.}", hmac_vap->vap_id,
142                 hmac_device->vap_id[index]);
143             continue;
144         }
145 
146         if ((hmac_vap_tmp->vap_state == MAC_VAP_STATE_UP) && (hmac_vap_tmp->vap_mode == WLAN_VAP_MODE_BSS_STA)) {
147             return OSAL_TRUE;
148         }
149     }
150     return OSAL_FALSE;
151 }
152 
hmac_sdp_follow_timeout_callback(osal_void * arg)153 OAL_STATIC osal_u32 hmac_sdp_follow_timeout_callback(osal_void *arg)
154 {
155     osal_u8 state;
156 
157     unref_param(arg);
158 
159     if (g_sdp_mac_vap == NULL) {
160         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_follow_timeout_callback:WiFi App demoAPP test not init.");
161         return OAL_FAIL;
162     }
163 
164     if (hmac_sdp_is_wifi_connected(g_sdp_mac_vap) == OSAL_TRUE) {
165         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_follow_timeout_callback: wifi is already connected");
166         state = NAN_DEMO_STATE_CONNECT_SUCCESS;
167         sdp_send_follow_up((osal_u8 *)&state, sizeof(state));
168         return OAL_SUCC;
169     }
170     state = NAN_DEMO_STATE_FOLLOW_UP_COMPLETE;
171     sdp_send_follow_up((osal_u8 *)&state, sizeof(state));
172     return OAL_SUCC;
173 }
174 
hmac_sdp_add_peer_mac_pre(osal_u8 type)175 OAL_STATIC osal_u32 hmac_sdp_add_peer_mac_pre(osal_u8 type)
176 {
177     frw_msg msg_info;
178     oal_sdp_peer_mac_handle peer_info = {0};
179 
180     /* 将mac发消息给device,过滤时间同步beacon 或 私有action帧 */
181     (osal_void)memset_s(&peer_info, OAL_SIZEOF(peer_info), 0, OAL_SIZEOF(peer_info));
182     if (memcpy_s(peer_info.mac, sizeof(peer_info.mac), g_sdp_handle.mac, sizeof(g_sdp_handle.mac)) != EOK) {
183         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_add_peer_mac_pre:memcpy_s failed.");
184         return OAL_FAIL;
185     }
186     peer_info.peer_handle = g_sdp_handle.peer;
187     peer_info.type = type; /* 1-add mac 0-del mac */
188     (osal_void)memset_s(&msg_info, OAL_SIZEOF(msg_info), 0, OAL_SIZEOF(msg_info));
189     msg_info.data = (osal_u8 *)&peer_info;
190     msg_info.data_len = sizeof(peer_info);
191     if (hmac_sdp_add_peer_mac(g_sdp_mac_vap, &msg_info) != OAL_SUCC) {
192         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_add_peer_mac_pre: add peer mac fail.");
193         return OAL_FAIL;
194     }
195 
196     return OAL_SUCC;
197 }
198 
hmac_sdp_recv_publish(const oal_sdp_info * sdp_info)199 OAL_STATIC osal_void hmac_sdp_recv_publish(const oal_sdp_info *sdp_info)
200 {
201     osal_u8 state = NAN_DEMO_STATE_FOLLOW_UP_COMPLETE;
202 
203     if (sdp_info->local_handle != g_sdp_handle.local) {
204         oam_warning_log1(0, OAM_SF_SDP, "hmac_sdp_recv_publish:local[%d]recv msg is not distribution network.",
205             g_sdp_handle.local);
206         return;
207     }
208 
209     if (g_sdp_handle.peer != 0) {
210         oam_warning_log2(0, OAM_SF_SDP, "hmac_sdp_recv_publish:already received publish, peer[%d], recv[%d].",
211             g_sdp_handle.peer, sdp_info->peer_handle);
212         if (sdp_info->peer_handle == g_sdp_handle.peer) {
213             if (hmac_sdp_is_wifi_connected(g_sdp_mac_vap) == OSAL_TRUE) {
214                 state = NAN_DEMO_STATE_CONNECT_SUCCESS;
215             }
216             sdp_send_follow_up((osal_u8 *)&state, sizeof(state)); /* 收到publish及时回复一下follow up 不完全靠定时器 */
217         }
218         return;
219     }
220 
221     g_sdp_handle.peer = sdp_info->peer_handle;
222 
223     /* 此处name临时用来保存publish报文的源mac地址, 从结构体成员定义来看是保存服务号的 */
224     if (memcpy_s(g_sdp_handle.mac, sizeof(g_sdp_handle.mac), sdp_info->name, sizeof(sdp_info->name)) != EOK) {
225         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_recv_publish:memcpy_s failed.");
226         return;
227     }
228     oam_info_log0(0, OAM_SF_SDP, "hmac_sdp_recv_publish:recv data");
229     sdp_send_follow_up((osal_u8 *)&state, sizeof(state));
230 
231     /* 将mac发消息给device,过滤时间同步beacon 或 私有action帧 */
232     if (hmac_sdp_add_peer_mac_pre(SDP_ADD_PEER_MAC) != OAL_SUCC) {
233         return;
234     }
235 
236     if (g_follow_timer.is_enabled != OAL_TRUE) {
237         /* 使能定时器,定时器到期则尝试走流程发报文 */
238         frw_create_timer_entry(&g_follow_timer, hmac_sdp_follow_timeout_callback, HMAC_FOLLOW_TIMEOUT, OSAL_NULL,
239             OAL_TRUE);
240     } else {
241         oam_info_log0(0, OAM_SF_SDP, "{hmac_sdp_recv_publish::timer already enabled}");
242     }
243     return;
244 }
245 
hmac_sdp_recv_follow_up(hmac_vap_stru * hmac_vap,oal_sdp_info * sdp_info,osal_u8 * data,osal_u8 len,oal_bool_enum_uint8 * reported_host)246 OAL_STATIC osal_void hmac_sdp_recv_follow_up(hmac_vap_stru *hmac_vap, oal_sdp_info *sdp_info,
247     osal_u8 *data, osal_u8 len, oal_bool_enum_uint8 *reported_host)
248 {
249     if (g_sdp_handle.peer == 0) {
250         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_recv_follow_up:not recv publish first.");
251         return;
252     }
253 
254     if (len == 0) {
255         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_recv_follow_up:data len is 0.");
256         return;
257     }
258     if (g_sdp_handle.peer != sdp_info->peer_handle) {
259         oam_warning_log2(0, OAM_SF_SDP, "hmac_sdp_recv_follow_up:record peer[%d] is diff [%d]\n", g_sdp_handle.peer,
260             sdp_info->peer_handle);
261         return;
262     }
263     /* 此处name临时用来保存publish报文的源mac地址, 从结构体成员定义来看是保存服务号的 */
264     if (osal_memcmp(g_sdp_handle.mac, sdp_info->name, sizeof(g_sdp_handle.mac)) != 0) {
265         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_recv_follow_up:osal_memcmp mac is fail");
266         return;
267     }
268     oam_info_log0(0, OAM_SF_SDP, "hmac_sdp_recv_follow_up:send follow up to host.");
269 
270     *reported_host = OSAL_TRUE;
271     hmac_sdp_send_followup_to_host(hmac_vap, sdp_info, data, len);
272 
273     return;
274 }
275 
276 
277 /* 测试接口,用于构造私有同步帧,后续可删除 */
hmac_sdp_sync_private_action(const hmac_vap_stru * hmac_vap,const oal_sdp_wakeup_info * para,oal_netbuf_stru * mgmt_buf)278 OAL_STATIC osal_u16 hmac_sdp_sync_private_action(const hmac_vap_stru *hmac_vap, const oal_sdp_wakeup_info *para,
279     oal_netbuf_stru *mgmt_buf)
280 {
281     /* sdp 私有帧头格式:Category:127,OUI:0x00-E0-FC,Type:0xFC,SubType:100 */
282     osal_u8 action_hdr_fixed[] = { 127, 0x0, 0xE0, 0xFC, 0xFC, 100 };
283     osal_u8 mcast_mac[ETH_ALEN] = { 0x51, 0x6F, 0x9A, 0x01, 0x00, 0x00 };
284     osal_u8 *data = (osal_u8 *)oal_netbuf_header(mgmt_buf);
285     mac_tx_ctl_stru *tx_ctl = (mac_tx_ctl_stru *)oal_netbuf_cb(mgmt_buf);
286     sdp_action_header *action_hdr = OSAL_NULL;
287     osal_u8 *payload_addr = OSAL_NULL;
288     oal_sdp_wakeup_info wake_info;
289 
290     if (memcpy_s(&wake_info, sizeof(wake_info), para, sizeof(oal_sdp_wakeup_info)) != EOK) {
291         return 0;
292     }
293 
294     mac_hdr_set_frame_control(data, WLAN_PROTOCOL_VERSION | WLAN_FC0_TYPE_MGT | WLAN_FC0_SUBTYPE_ACTION);
295 
296     /* 设置分片序号为0 */
297     mac_hdr_set_fragment_number(data, 0);
298 
299     /* 设置地址1,对端地址 */
300     if (memcpy_s(data + WLAN_HDR_ADDR1_OFFSET, WLAN_MAC_ADDR_LEN, mcast_mac, ETH_ALEN) != EOK) {
301         return 0;
302     }
303 
304     /* 设置本端的MAC地址 */
305     if (memcpy_s(data + WLAN_HDR_ADDR2_OFFSET, WLAN_MAC_ADDR_LEN,
306         hmac_vap->mib_info->wlan_mib_sta_config.dot11_station_id, WLAN_MAC_ADDR_LEN) != EOK) {
307         return 0;
308     }
309     /* 地址3, BSSID */
310     if (memcpy_s(data + WLAN_HDR_ADDR3_OFFSET, WLAN_MAC_ADDR_LEN, hmac_vap->bssid, WLAN_MAC_ADDR_LEN) != EOK) {
311         return 0;
312     }
313 
314     action_hdr = (sdp_action_header *)(data + MAC_80211_FRAME_LEN); /* 取action帧体 */
315     if (memcpy_s(action_hdr, sizeof(action_hdr_fixed), action_hdr_fixed, sizeof(action_hdr_fixed)) != EOK) {
316         return 0;
317     }
318     payload_addr = (osal_u8 *)(action_hdr + 1);
319     wake_info.total_period = oal_host2net_long(wake_info.total_period);
320     wake_info.wake_period = oal_host2net_long(wake_info.wake_period);
321     if (memcpy_s(payload_addr, sizeof(oal_sdp_wakeup_info), &wake_info, sizeof(oal_sdp_wakeup_info)) != EOK) {
322         return 0;
323     }
324 
325     /* 计算帧的总长度,设置相关发送配置 */
326     mac_get_cb_frame_header_length(tx_ctl) = MAC_80211_FRAME_LEN;
327     mac_get_cb_tx_user_idx(tx_ctl) = HMAC_SDP_TX_UESR_ID; /* 发送完成需要获取user结构体 */
328     tx_ctl->need_rsp = OAL_FALSE;
329     tx_ctl->is_needretry = OAL_FALSE;
330     tx_ctl->ismcast = OAL_TRUE;
331     mac_get_cb_mpdu_len(tx_ctl) = MAC_80211_FRAME_LEN + sizeof(sdp_action_header) + sizeof(oal_sdp_wakeup_info);
332 
333     return mac_get_cb_mpdu_len(tx_ctl);
334 }
335 
336 /* 测试接口,用于发送私有同步帧,后续可删除 */
hmac_sdp_test_sync_private(hmac_vap_stru * hmac_vap,const osal_u8 * param)337 OAL_STATIC osal_u32 hmac_sdp_test_sync_private(hmac_vap_stru *hmac_vap, const osal_u8 *param)
338 {
339     oal_netbuf_stru *netbuf = OSAL_NULL;
340     osal_u32 ret;
341     osal_u16 mgmt_len;
342 
343     if ((hmac_vap == OSAL_NULL) || (param == OSAL_NULL)) {
344         return OAL_FAIL;
345     }
346 
347     /* 申请管理帧内存 */
348     netbuf = OAL_MEM_NETBUF_ALLOC(OAL_MGMT_NETBUF, WLAN_MGMT_NETBUF_SIZE, OAL_NETBUF_PRIORITY_HIGH);
349     if (netbuf == OSAL_NULL) {
350         oam_warning_log0(0, OAM_SF_SDP, "{hmac_sdp_test_sync_private::alloc netbuf failed.}");
351         return OAL_ERR_CODE_PTR_NULL;
352     }
353 
354     oal_set_netbuf_prev(netbuf, OSAL_NULL);
355     oal_set_netbuf_next(netbuf, OSAL_NULL);
356 
357     if (memset_s(oal_netbuf_cb(netbuf), OAL_NETBUF_CB_SIZE(), 0, OAL_NETBUF_CB_SIZE()) != EOK) {
358         hmac_dft_print_drop_frame_info(THIS_FILE_ID, __LINE__, 1, OSAL_NULL);
359         oal_netbuf_free(netbuf);
360         return OAL_FAIL;
361     }
362 
363     /* 封装Action帧 */
364     mgmt_len = hmac_sdp_sync_private_action(hmac_vap, (const oal_sdp_wakeup_info *)param, netbuf);
365     if (mgmt_len == 0) {
366 #ifdef _PRE_WLAN_DFT_STAT
367         hmac_dft_print_drop_frame_info(THIS_FILE_ID, __LINE__, 1, OSAL_NULL);
368 #endif
369         oal_netbuf_free(netbuf);
370         oam_warning_log0(0, OAM_SF_SDP, "{hmac_sdp_test_sync_private::action frame len is 0.}");
371         return OAL_FAIL;
372     }
373 
374     oal_netbuf_put(netbuf, (osal_u32)mgmt_len);
375 
376     /* 调用发送管理帧接口 */
377     ret = hmac_tx_mgmt_send_event_etc(hmac_vap, netbuf, mgmt_len);
378     if (ret != OAL_SUCC) {
379         oal_netbuf_free(netbuf);
380         oam_warning_log1(0, OAM_SF_SDP, "{hmac_sdp_test_sync_private::hmac_tx_mgmt_send_event failed[%d].}", ret);
381         return ret;
382     }
383 
384     oam_info_log1(0, OAM_SF_SDP, "hmac_sdp_test_sync_private send sync success mgmt_len=%d", mgmt_len);
385     return OAL_SUCC;
386 }
387 
388 /* 测试无感配网接口: 订阅服务 */
389 /* 启动订阅操作,用于侦听某一服务信息,入参包含服务名称哈希值和订阅成功之后分配的本端instance id */
hmac_sdp_test_distribution_subscribe(hmac_vap_stru * hmac_vap,osal_u8 * param)390 OAL_STATIC osal_u32 hmac_sdp_test_distribution_subscribe(hmac_vap_stru *hmac_vap, osal_u8 *param)
391 {
392     oal_sdp_service service_info;
393     frw_msg msg_info;
394     oal_sdp_subscribe_info *subscribe_info = (oal_sdp_subscribe_info *)param;
395 
396     if (g_sdp_mac_vap == NULL) {
397         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_distribution_subscribe: not start, can not subscribe.");
398         return OAL_FAIL;
399     }
400     (osal_void)memset_s(&service_info, sizeof(service_info), 0, sizeof(service_info));
401     service_info.local_handle = subscribe_info->local_handle;
402     service_info.type = OAL_SDP_SERVICE_TYPE_SUBSCRIBE;
403     if (memcpy_s(service_info.name, sizeof(service_info.name), subscribe_info->name,
404             sizeof(subscribe_info->name)) != EOK) {
405         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_distribution_subscribe:memcpy_s hash_server_name failed.");
406         return OAL_FAIL;
407     }
408 
409     (osal_void)memset_s(&msg_info, sizeof(msg_info), 0, sizeof(msg_info));
410     msg_info.data = (osal_u8 *)&service_info;
411     msg_info.data_len = sizeof(service_info);
412     if (hmac_sdp_start_subscribe(hmac_vap, &msg_info) != OAL_SUCC) {
413         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_distribution_subscribe: failed.");
414         return OAL_FAIL;
415     }
416     oam_info_log1(0, OAM_SF_SDP, "hmac_sdp_test_distribution_subscribe: suc, local_handle[%d].",
417         service_info.local_handle);
418     (osal_void)memset_s(&g_sdp_handle, sizeof(g_sdp_handle), 0, sizeof(g_sdp_handle));
419     g_sdp_handle.local = service_info.local_handle; /* 不再固定 SDP_SERVER_ID, 收publish时会获取该值,替换 */
420     oam_info_log1(0, OAM_SF_SDP, "{hmac_sdp_test_distribution_subscribe:local[%d].}", g_sdp_handle.local);
421     return OAL_SUCC;
422 }
423 
hmac_sdp_test_distribution_subscribe_cancel(hmac_vap_stru * hmac_vap,osal_u8 * param)424 OAL_STATIC osal_u32 hmac_sdp_test_distribution_subscribe_cancel(hmac_vap_stru *hmac_vap, osal_u8 *param)
425 {
426     frw_msg msg_info;
427     oal_sdp_subscribe_info *subscribe_info = (oal_sdp_subscribe_info *)param;
428     osal_u8 local_handle;
429 
430     if ((g_sdp_mac_vap == NULL) || (g_sdp_handle.local == 0)) {
431         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_distribution_subscribe not start/subscribe,cannot cancel");
432         return OAL_FAIL;
433     }
434     local_handle = subscribe_info->local_handle;
435 
436     (osal_void)memset_s(&msg_info, sizeof(msg_info), 0, sizeof(msg_info));
437     msg_info.data = &local_handle;
438     msg_info.data_len = sizeof(local_handle);
439     if (hmac_sdp_cancel_subscribe(hmac_vap, &msg_info) != OAL_SUCC) {
440         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_distribution_subscribe_cancel: failed.");
441         return OAL_FAIL;
442     }
443     oam_info_log1(0, OAM_SF_SDP, "hmac_sdp_test_distribution_subscribe_cancel:suc local_handle[%d]", local_handle);
444     (osal_void)memset_s(&g_sdp_handle, sizeof(g_sdp_handle), 0, sizeof(g_sdp_handle));
445     frw_destroy_timer_entry(&g_follow_timer);
446 
447     /* 将mac发消息给device,过滤时间同步beacon 或 私有action帧 */
448     if (hmac_sdp_add_peer_mac_pre(SDP_DEL_PEER_MAC) != OAL_SUCC) {
449         return OAL_FAIL;
450     }
451     oam_info_log1(0, OAM_SF_SDP, "{hmac_sdp_test_distribution_subscribe_cancel:local[%d].}", g_sdp_handle.local);
452     return OAL_SUCC;
453 }
454 
455 /* 测试无感配网接口: 初始化, 20220919更新的, init 和 subscribe分开, 同时 73中is_connected 不会从命令行传入了 */
hmac_sdp_test_distribution_enable(hmac_vap_stru * hmac_vap,osal_u8 is_connected,osal_u8 ratio)456 OAL_STATIC osal_u32 hmac_sdp_test_distribution_enable(hmac_vap_stru *hmac_vap, osal_u8 is_connected, osal_u8 ratio)
457 {
458      /* 如果STA已连上,初始化启定时器,否则不启定时器 */
459     if (hmac_sdp_init(hmac_vap, is_connected, g_sdp_init_peroid, &ratio) != OAL_SUCC) {
460         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_sync_distribution:init failed.");
461         return OAL_FAIL;
462     }
463 
464     g_sdp_mac_vap = hmac_vap;
465     oam_warning_log3(0, OAM_SF_SDP, "hmac_sdp_test_distribution_enable:is_connected[%d], peroid[%d] ratio[%d]",
466         is_connected, g_sdp_init_peroid, ratio);
467     /* sdp初始化时注册 */
468     hmac_sdp_feature_hook_register();
469     return OAL_SUCC;
470 }
471 
472 /* 测试无感配网接口: 去初始化 */
hmac_sdp_test_distribution_disable(hmac_vap_stru * hmac_vap,osal_u8 is_connected)473 OAL_STATIC osal_u32 hmac_sdp_test_distribution_disable(hmac_vap_stru *hmac_vap, osal_u8 is_connected)
474 {
475     unref_param(is_connected);
476     /* sdp去初始化时注册 */
477     hmac_sdp_feature_hook_unregister();
478 
479     if (g_sdp_mac_vap == NULL) {
480         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_distribution_disable: not start, do not need disable.");
481         return OAL_FAIL;
482     }
483 
484     (osal_void)memset_s(&g_sdp_handle, sizeof(g_sdp_handle), 0, sizeof(g_sdp_handle));
485     g_sdp_mac_vap = OSAL_NULL;
486     frw_destroy_timer_entry(&g_follow_timer);
487 
488     if (hmac_sdp_deinit(hmac_vap) != OAL_SUCC) {
489         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_sync_distribution:init failed.");
490         return OAL_FAIL;
491     }
492     return OAL_SUCC;
493 }
494 
hmac_sdp_test_print_hex(const osal_u8 * name,osal_u8 len)495 OAL_STATIC osal_void hmac_sdp_test_print_hex(const osal_u8 *name, osal_u8 len)
496 {
497     osal_u8 index;
498 
499     if (len == HMAC_SDP_TYPE_SWITCH_LENGTH) {
500         /* 打印开关灯报文 0:第一位 1:第二位 2:第三位 3: 第四位 */
501         oam_warning_log4(0, OAM_SF_SDP, "%02x%02x%02x%02x", name[0], name[1], name[2], name[3]);
502     }
503 
504     for (index = 0; index < len; index++) {
505         oam_warning_log1(0, OAM_SF_SDP, "%02x", name[index]);
506     }
507 }
508 
509 /* 列出type类型服务的信息 */
hmac_sdp_test_print_info(osal_u8 type)510 OAL_STATIC osal_u32 hmac_sdp_test_print_info(osal_u8 type)
511 {
512     const hmac_sdp_service_info *service = OSAL_NULL;
513     const oal_sdp_service *array = OSAL_NULL;
514     osal_u8 array_size;
515     osal_u8 index;
516 
517     if (hmac_sdp_is_init() != OAL_TRUE) {
518         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_test_print_info:sdp not init.");
519         return OAL_FAIL;
520     }
521 
522     service = hmac_sdp_get_service_info();
523     array = service->info;
524     array_size = sizeof(service->info) / sizeof(service->info[0]);
525     for (index = 0; index < array_size; index++) {
526         if ((array[index].type & type) != 0) {
527             oam_warning_log2(0, OAM_SF_SDP, "service local handle:%d.service type:%d.service name:",
528                 array[index].local_handle, array[index].type);
529             hmac_sdp_test_print_hex(array[index].name, sizeof(array[index].name));
530         }
531     }
532 
533     return OAL_SUCC;
534 }
535 
hmac_sdp_test(hmac_vap_stru * hmac_vap,frw_msg * msg)536 osal_s32 hmac_sdp_test(hmac_vap_stru *hmac_vap, frw_msg *msg)
537 {
538     osal_u8 *param = msg->data;
539     osal_u8 type = ((const oal_sdp_wakeup_info *)param)->type;
540     osal_u8 ratio = ((const oal_sdp_wakeup_info *)param)->ratio;
541     osal_u8 is_connected = hmac_sdp_is_wifi_connected(hmac_vap);
542 
543     switch (type) {
544         case WAL_SDP_TEST_TYPE_PRIV:
545             return (osal_s32)hmac_sdp_test_sync_private(hmac_vap, param);
546         case WAL_SDP_TEST_TYPE_DISTRIBUTION_ENABLE:
547             return (osal_s32)hmac_sdp_test_distribution_enable(hmac_vap, is_connected, ratio);
548         case WAL_SDP_TEST_TYPE_DISTRIBUTION_DISABLE:
549             return (osal_s32)hmac_sdp_test_distribution_disable(hmac_vap, is_connected);
550         case WAL_SDP_TEST_TYPE_PUBLISH_LIST:
551             return (osal_s32)hmac_sdp_test_print_info(OAL_SDP_SERVICE_TYPE_PUBLISH);
552         case WAL_SDP_TEST_TYPE_SUBSCRIBE_LIST:
553             return (osal_s32)hmac_sdp_test_print_info(OAL_SDP_SERVICE_TYPE_SUBSCRIBE);
554         case WAL_SDP_TEST_TYPE_SUBSCRIBE_SERVICE:
555             return (osal_s32)hmac_sdp_test_distribution_subscribe(hmac_vap, param);
556         case WAL_SDP_TEST_TYPE_SUBSCRIBE_CANCEL:
557             return (osal_s32)hmac_sdp_test_distribution_subscribe_cancel(hmac_vap, param);
558         default:
559             return OAL_FAIL;
560     }
561 }
562 
hmac_sdp_proc_rx_mgmt(hmac_vap_stru * hmac_vap,oal_netbuf_stru * netbuf,oal_bool_enum_uint8 * reported_host)563 osal_u32 hmac_sdp_proc_rx_mgmt(hmac_vap_stru *hmac_vap, oal_netbuf_stru *netbuf,
564     oal_bool_enum_uint8 *reported_host)
565 {
566     osal_u8 *desc_attr = OSAL_NULL;
567     oal_sdp_info sdp_param;
568     osal_u8 sdp_type;
569     osal_u8 msg_len;
570 
571     if ((hmac_vap == OSAL_NULL) || (netbuf == OSAL_NULL)) {
572         return OAL_FAIL;
573     }
574 
575     (osal_void)memset_s(&sdp_param, sizeof(sdp_param), 0, sizeof(sdp_param));
576     desc_attr = hmac_sdp_parse_rx(netbuf, &sdp_param);
577     if (desc_attr == OSAL_NULL) {
578         oam_error_log0(0, OAM_SF_SDP, "hmac_sdp_proc_rx_mgmt: hmac_sdp_parse_rx failed.");
579         return OAL_FAIL;
580     }
581 
582     if (g_sdp_mac_vap == NULL) {
583         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_proc_rx_mgmt:WiFi App demoAPP test not init.");
584         return OAL_FAIL;
585     }
586 
587     sdp_type = desc_attr[HMAC_SDP_POS_TYPE] & HMAC_SDP_TYPE_MASK;
588     /* 接收到publish报文,发送follow到对端 */
589     if (sdp_type == HMAC_SDP_TYPE_PUBLISH) {
590         hmac_sdp_recv_publish(&sdp_param);
591     } else if (sdp_type == HMAC_SDP_TYPE_FOLLOWUP) { /* 接收到follow-up帧发送到host侧 */
592         msg_len = desc_attr[HMAC_SDP_POS_DATA_LEN];
593         hmac_sdp_test_print_hex(desc_attr + HMAC_SDP_NO_DATA_LEN, msg_len);
594         hmac_sdp_recv_follow_up(hmac_vap, &sdp_param, desc_attr + HMAC_SDP_NO_DATA_LEN, msg_len, reported_host);
595     } else {
596         oam_warning_log0(0, OAM_SF_SDP, "hmac_sdp_proc_rx_mgmt:frame received is invalid!");
597         return OAL_FAIL;
598     }
599 
600     return OAL_SUCC;
601 }
602 
603 #ifdef __cplusplus
604 #if __cplusplus
605 }
606 #endif
607 #endif
608 
609