1 /*
2  * Copyright 2016, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdint.h>
18 #include "JniConstants.h"
19 #include <ScopedUtfChars.h>
20 #include <ScopedBytes.h>
21 #include <utils/misc.h>
22 #include <android_runtime/AndroidRuntime.h>
23 #include <utils/Log.h>
24 #include <utils/String16.h>
25 #include <ctype.h>
26 #include <sys/socket.h>
27 #include <linux/if.h>
28 #include "wifi.h"
29 #include "wifi_hal.h"
30 #include "jni_helper.h"
31 #include "wifi_hal_mock.h"
32 #include <sstream>
33 #include <rapidjson/document.h>
34 #include <rapidjson/stringbuffer.h>
35 #include <rapidjson/writer.h>
36 
37 namespace android {
38 
39 extern jobject mock_mObj; /* saved NanHalMock object */
40 extern JavaVM* mock_mVM; /* saved JVM pointer */
41 
42 /* Variable and function declared and defined in:
43  *  com_android_servier_wifi_nan_WifiNanNative.cpp
44  */
45 extern wifi_hal_fn hal_fn;
46 extern "C" jint Java_com_android_server_wifi_nan_WifiNanNative_registerNanNatives(
47     JNIEnv* env, jclass clazz);
48 
49 static NanCallbackHandler mCallbackHandlers;
50 
wifi_nan_enable_request_mock(transaction_id id,wifi_interface_handle iface,NanEnableRequest * msg)51 wifi_error wifi_nan_enable_request_mock(transaction_id id,
52                                         wifi_interface_handle iface,
53                                         NanEnableRequest* msg) {
54   JNIHelper helper(mock_mVM);
55 
56   ALOGD("wifi_nan_enable_request_mock");
57   HalMockJsonWriter jsonW;
58   jsonW.put_int("master_pref", msg->master_pref);
59   jsonW.put_int("cluster_low", msg->cluster_low);
60   jsonW.put_int("cluster_high", msg->cluster_high);
61   jsonW.put_int("config_support_5g", msg->config_support_5g);
62   jsonW.put_int("support_5g_val", msg->support_5g_val);
63   jsonW.put_int("config_sid_beacon", msg->config_sid_beacon);
64   jsonW.put_int("sid_beacon_val", msg->sid_beacon_val);
65   jsonW.put_int("config_2dot4g_rssi_close", msg->config_2dot4g_rssi_close);
66   jsonW.put_int("rssi_close_2dot4g_val", msg->rssi_close_2dot4g_val);
67   jsonW.put_int("config_2dot4g_rssi_middle", msg->config_2dot4g_rssi_middle);
68   jsonW.put_int("rssi_middle_2dot4g_val", msg->rssi_middle_2dot4g_val);
69   jsonW.put_int("config_2dot4g_rssi_proximity",
70                 msg->config_2dot4g_rssi_proximity);
71   jsonW.put_int("rssi_proximity_2dot4g_val", msg->rssi_proximity_2dot4g_val);
72   jsonW.put_int("config_hop_count_limit", msg->config_hop_count_limit);
73   jsonW.put_int("hop_count_limit_val", msg->hop_count_limit_val);
74   jsonW.put_int("config_2dot4g_support", msg->config_2dot4g_support);
75   jsonW.put_int("support_2dot4g_val", msg->support_2dot4g_val);
76   jsonW.put_int("config_2dot4g_beacons", msg->config_2dot4g_beacons);
77   jsonW.put_int("beacon_2dot4g_val", msg->beacon_2dot4g_val);
78   jsonW.put_int("config_2dot4g_sdf", msg->config_2dot4g_sdf);
79   jsonW.put_int("sdf_2dot4g_val", msg->sdf_2dot4g_val);
80   jsonW.put_int("config_5g_beacons", msg->config_5g_beacons);
81   jsonW.put_int("beacon_5g_val", msg->beacon_5g_val);
82   jsonW.put_int("config_5g_sdf", msg->config_5g_sdf);
83   jsonW.put_int("sdf_5g_val", msg->sdf_5g_val);
84   jsonW.put_int("config_5g_rssi_close", msg->config_5g_rssi_close);
85   jsonW.put_int("rssi_close_5g_val", msg->rssi_close_5g_val);
86   jsonW.put_int("config_5g_rssi_middle", msg->config_5g_rssi_middle);
87   jsonW.put_int("rssi_middle_5g_val", msg->rssi_middle_5g_val);
88   jsonW.put_int("config_5g_rssi_close_proximity",
89                 msg->config_5g_rssi_close_proximity);
90   jsonW.put_int("rssi_close_proximity_5g_val",
91                 msg->rssi_close_proximity_5g_val);
92   jsonW.put_int("config_rssi_window_size", msg->config_rssi_window_size);
93   jsonW.put_int("rssi_window_size_val", msg->rssi_window_size_val);
94   jsonW.put_int("config_oui", msg->config_oui);
95   jsonW.put_int("oui_val", msg->oui_val);
96   jsonW.put_int("config_intf_addr", msg->config_intf_addr);
97   jsonW.put_byte_array("intf_addr_val", msg->intf_addr_val, 6);
98   jsonW.put_int("config_cluster_attribute_val",
99                 msg->config_cluster_attribute_val);
100   jsonW.put_int("config_scan_params", msg->config_scan_params);
101   jsonW.put_int("scan_params_val.dwell_time.0",
102                 msg->scan_params_val.dwell_time[NAN_CHANNEL_24G_BAND]);
103   jsonW.put_int("scan_params_val.dwell_time.1",
104                 msg->scan_params_val.dwell_time[NAN_CHANNEL_5G_BAND_LOW]);
105   jsonW.put_int("scan_params_val.dwell_time.2",
106                 msg->scan_params_val.dwell_time[NAN_CHANNEL_5G_BAND_HIGH]);
107   jsonW.put_int("scan_params_val.scan_period.0",
108                 msg->scan_params_val.scan_period[NAN_CHANNEL_24G_BAND]);
109   jsonW.put_int("scan_params_val.scan_period.0",
110                 msg->scan_params_val.scan_period[NAN_CHANNEL_5G_BAND_LOW]);
111   jsonW.put_int("scan_params_val.scan_period.0",
112                 msg->scan_params_val.scan_period[NAN_CHANNEL_5G_BAND_HIGH]);
113   jsonW.put_int("config_random_factor_force", msg->config_random_factor_force);
114   jsonW.put_int("random_factor_force_val", msg->random_factor_force_val);
115   jsonW.put_int("config_hop_count_force", msg->config_hop_count_force);
116   jsonW.put_int("hop_count_force_val", msg->hop_count_force_val);
117   std::string str = jsonW.to_string();
118 
119   JNIObject < jstring > json_write_string = helper.newStringUTF(str.c_str());
120 
121   helper.callMethod(mock_mObj, "enableHalMockNative", "(SLjava/lang/String;)V",
122                     (short) id, json_write_string.get());
123 
124   return WIFI_SUCCESS;
125 }
126 
wifi_nan_disable_request_mock(transaction_id id,wifi_interface_handle iface)127 wifi_error wifi_nan_disable_request_mock(transaction_id id,
128                                          wifi_interface_handle iface) {
129   JNIHelper helper(mock_mVM);
130 
131   ALOGD("wifi_nan_disable_request_mock");
132   helper.callMethod(mock_mObj, "disableHalMockNative", "(S)V", (short) id);
133 
134   return WIFI_SUCCESS;
135 }
136 
wifi_nan_publish_request_mock(transaction_id id,wifi_interface_handle iface,NanPublishRequest * msg)137 wifi_error wifi_nan_publish_request_mock(transaction_id id,
138                                          wifi_interface_handle iface,
139                                          NanPublishRequest* msg) {
140   JNIHelper helper(mock_mVM);
141 
142   ALOGD("wifi_nan_publish_request_mock");
143   HalMockJsonWriter jsonW;
144   jsonW.put_int("publish_id", msg->publish_id);
145   jsonW.put_int("ttl", msg->ttl);
146   jsonW.put_int("publish_type", msg->publish_type);
147   jsonW.put_int("tx_type", msg->tx_type);
148   jsonW.put_int("publish_count", msg->publish_count);
149   jsonW.put_int("service_name_len", msg->service_name_len);
150   jsonW.put_byte_array("service_name", msg->service_name,
151                        msg->service_name_len);
152   jsonW.put_int("publish_match_indicator", msg->publish_match_indicator);
153   jsonW.put_int("service_specific_info_len", msg->service_specific_info_len);
154   jsonW.put_byte_array("service_specific_info", msg->service_specific_info,
155                        msg->service_specific_info_len);
156   jsonW.put_int("rx_match_filter_len", msg->rx_match_filter_len);
157   jsonW.put_byte_array("rx_match_filter", msg->rx_match_filter,
158                        msg->rx_match_filter_len);
159   jsonW.put_int("tx_match_filter_len", msg->tx_match_filter_len);
160   jsonW.put_byte_array("tx_match_filter", msg->tx_match_filter,
161                        msg->tx_match_filter_len);
162   jsonW.put_int("rssi_threshold_flag", msg->rssi_threshold_flag);
163   jsonW.put_int("connmap", msg->connmap);
164   std::string str = jsonW.to_string();
165 
166   JNIObject < jstring > json_write_string = helper.newStringUTF(str.c_str());
167 
168   helper.callMethod(mock_mObj, "publishHalMockNative", "(SLjava/lang/String;)V",
169                     (short) id, json_write_string.get());
170   return WIFI_SUCCESS;
171 }
172 
wifi_nan_publish_cancel_request_mock(transaction_id id,wifi_interface_handle iface,NanPublishCancelRequest * msg)173 wifi_error wifi_nan_publish_cancel_request_mock(transaction_id id,
174                                                 wifi_interface_handle iface,
175                                                 NanPublishCancelRequest* msg) {
176   JNIHelper helper(mock_mVM);
177 
178   ALOGD("wifi_nan_publish_cancel_request_mock");
179   HalMockJsonWriter jsonW;
180   jsonW.put_int("publish_id", msg->publish_id);
181   std::string str = jsonW.to_string();
182 
183   JNIObject < jstring > json_write_string = helper.newStringUTF(str.c_str());
184 
185   helper.callMethod(mock_mObj, "publishCancelHalMockNative",
186                     "(SLjava/lang/String;)V", (short) id,
187                     json_write_string.get());
188   return WIFI_SUCCESS;
189 }
190 
wifi_nan_subscribe_request_mock(transaction_id id,wifi_interface_handle iface,NanSubscribeRequest * msg)191 wifi_error wifi_nan_subscribe_request_mock(transaction_id id,
192                                            wifi_interface_handle iface,
193                                            NanSubscribeRequest* msg) {
194   JNIHelper helper(mock_mVM);
195 
196   ALOGD("wifi_nan_subscribe_request_mock");
197   HalMockJsonWriter jsonW;
198   jsonW.put_int("subscribe_id", msg->subscribe_id);
199   jsonW.put_int("ttl", msg->ttl);
200   jsonW.put_int("period", msg->period);
201   jsonW.put_int("subscribe_type", msg->subscribe_type);
202   jsonW.put_int("serviceResponseFilter", msg->serviceResponseFilter);
203   jsonW.put_int("serviceResponseInclude", msg->serviceResponseInclude);
204   jsonW.put_int("useServiceResponseFilter", msg->useServiceResponseFilter);
205   jsonW.put_int("ssiRequiredForMatchIndication",
206                 msg->ssiRequiredForMatchIndication);
207   jsonW.put_int("subscribe_match_indicator", msg->subscribe_match_indicator);
208   jsonW.put_int("subscribe_count", msg->subscribe_count);
209   jsonW.put_int("service_name_len", msg->service_name_len);
210   jsonW.put_byte_array("service_name", msg->service_name,
211                        msg->service_name_len);
212   jsonW.put_int("service_specific_info_len", msg->service_name_len);
213   jsonW.put_byte_array("service_specific_info", msg->service_specific_info,
214                        msg->service_specific_info_len);
215   jsonW.put_int("rx_match_filter_len", msg->rx_match_filter_len);
216   jsonW.put_byte_array("rx_match_filter", msg->rx_match_filter,
217                        msg->rx_match_filter_len);
218   jsonW.put_int("tx_match_filter_len", msg->tx_match_filter_len);
219   jsonW.put_byte_array("tx_match_filter", msg->tx_match_filter,
220                        msg->tx_match_filter_len);
221   jsonW.put_int("rssi_threshold_flag", msg->rssi_threshold_flag);
222   jsonW.put_int("connmap", msg->connmap);
223   jsonW.put_int("num_intf_addr_present", msg->num_intf_addr_present);
224   // TODO: jsonW.put_byte_array("intf_addr", msg->intf_addr, NAN_MAX_SUBSCRIBE_MAX_ADDRESS * NAN_MAC_ADDR_LEN);
225   std::string str = jsonW.to_string();
226 
227   JNIObject < jstring > json_write_string = helper.newStringUTF(str.c_str());
228 
229   helper.callMethod(mock_mObj, "subscribeHalMockNative",
230                     "(SLjava/lang/String;)V", (short) id,
231                     json_write_string.get());
232   return WIFI_SUCCESS;
233 }
234 
wifi_nan_subscribe_cancel_request_mock(transaction_id id,wifi_interface_handle iface,NanSubscribeCancelRequest * msg)235 wifi_error wifi_nan_subscribe_cancel_request_mock(
236     transaction_id id, wifi_interface_handle iface,
237     NanSubscribeCancelRequest* msg) {
238   JNIHelper helper(mock_mVM);
239 
240   ALOGD("wifi_nan_subscribe_cancel_request_mock");
241   HalMockJsonWriter jsonW;
242   jsonW.put_int("subscribe_id", msg->subscribe_id);
243   std::string str = jsonW.to_string();
244 
245   JNIObject < jstring > json_write_string = helper.newStringUTF(str.c_str());
246 
247   helper.callMethod(mock_mObj, "subscribeCancelHalMockNative",
248                     "(SLjava/lang/String;)V", (short) id,
249                     json_write_string.get());
250   return WIFI_SUCCESS;
251 }
252 
wifi_nan_transmit_followup_request_mock(transaction_id id,wifi_interface_handle iface,NanTransmitFollowupRequest * msg)253 wifi_error wifi_nan_transmit_followup_request_mock(
254     transaction_id id, wifi_interface_handle iface,
255     NanTransmitFollowupRequest* msg) {
256   JNIHelper helper(mock_mVM);
257 
258   ALOGD("wifi_nan_transmit_followup_request_mock");
259   HalMockJsonWriter jsonW;
260   jsonW.put_int("publish_subscribe_id", msg->publish_subscribe_id);
261   jsonW.put_int("requestor_instance_id", msg->requestor_instance_id);
262   jsonW.put_byte_array("addr", msg->addr, 6);
263   jsonW.put_int("priority", msg->priority);
264   jsonW.put_int("dw_or_faw", msg->dw_or_faw);
265   jsonW.put_int("service_specific_info_len", msg->service_specific_info_len);
266   jsonW.put_byte_array("service_specific_info", msg->service_specific_info,
267                        msg->service_specific_info_len);
268 
269   std::string str = jsonW.to_string();
270 
271   JNIObject < jstring > json_write_string = helper.newStringUTF(str.c_str());
272 
273   helper.callMethod(mock_mObj, "transmitFollowupHalMockNative",
274                     "(SLjava/lang/String;)V", (short) id,
275                     json_write_string.get());
276   return WIFI_SUCCESS;
277 }
278 
wifi_nan_stats_request_mock(transaction_id id,wifi_interface_handle iface,NanStatsRequest * msg)279 wifi_error wifi_nan_stats_request_mock(transaction_id id,
280                                        wifi_interface_handle iface,
281                                        NanStatsRequest* msg) {
282   ALOGD("wifi_nan_stats_request_mock");
283   return WIFI_ERROR_UNINITIALIZED;
284 }
285 
wifi_nan_config_request_mock(transaction_id id,wifi_interface_handle iface,NanConfigRequest * msg)286 wifi_error wifi_nan_config_request_mock(transaction_id id,
287                                         wifi_interface_handle iface,
288                                         NanConfigRequest* msg) {
289   ALOGD("wifi_nan_config_request_mock");
290   return WIFI_ERROR_UNINITIALIZED;
291 }
292 
wifi_nan_tca_request_mock(transaction_id id,wifi_interface_handle iface,NanTCARequest * msg)293 wifi_error wifi_nan_tca_request_mock(transaction_id id,
294                                      wifi_interface_handle iface,
295                                      NanTCARequest* msg) {
296   ALOGD("wifi_nan_tca_request_mock");
297   return WIFI_ERROR_UNINITIALIZED;
298 }
299 
wifi_nan_beacon_sdf_payload_request_mock(transaction_id id,wifi_interface_handle iface,NanBeaconSdfPayloadRequest * msg)300 wifi_error wifi_nan_beacon_sdf_payload_request_mock(
301     transaction_id id, wifi_interface_handle iface,
302     NanBeaconSdfPayloadRequest* msg) {
303   ALOGD("wifi_nan_beacon_sdf_payload_request_mock");
304   return WIFI_ERROR_UNINITIALIZED;
305 }
306 
wifi_nan_register_handler_mock(wifi_interface_handle iface,NanCallbackHandler handlers)307 wifi_error wifi_nan_register_handler_mock(wifi_interface_handle iface,
308                                           NanCallbackHandler handlers) {
309   ALOGD("wifi_nan_register_handler_mock");
310   mCallbackHandlers = handlers;
311   return WIFI_SUCCESS;
312 }
313 
wifi_nan_get_version_mock(wifi_handle handle,NanVersion * version)314 wifi_error wifi_nan_get_version_mock(wifi_handle handle, NanVersion* version) {
315   ALOGD("wifi_nan_get_version_mock");
316   return WIFI_ERROR_UNINITIALIZED;
317 }
318 
wifi_nan_get_capabilities_mock(transaction_id id,wifi_interface_handle iface)319 wifi_error wifi_nan_get_capabilities_mock(transaction_id id,
320                                           wifi_interface_handle iface) {
321   JNIHelper helper(mock_mVM);
322 
323   ALOGD("wifi_nan_get_capabilities_mock");
324 
325   helper.callMethod(mock_mObj, "getCapabilitiesHalMockNative", "(S)V",
326                     (short) id);
327   return WIFI_SUCCESS;
328 }
329 
330 // Callbacks
331 
Java_com_android_server_wifi_nan_WifiNanHalMock_callNotifyResponse(JNIEnv * env,jclass clazz,jshort transaction_id,jstring json_args_jstring)332 extern "C" void Java_com_android_server_wifi_nan_WifiNanHalMock_callNotifyResponse(
333     JNIEnv* env, jclass clazz, jshort transaction_id,
334     jstring json_args_jstring) {
335   ScopedUtfChars chars(env, json_args_jstring);
336   HalMockJsonReader jsonR(chars.c_str());
337   bool error = false;
338 
339   ALOGD("Java_com_android_server_wifi_nan_WifiNanHalMock_callNotifyResponse: '%s'",
340         chars.c_str());
341 
342   NanResponseMsg msg;
343   msg.status = (NanStatusType) jsonR.get_int("status", &error);
344   msg.value = jsonR.get_int("value", &error);
345   msg.response_type = (NanResponseType) jsonR.get_int("response_type", &error);
346   if (msg.response_type == NAN_RESPONSE_PUBLISH) {
347     msg.body.publish_response.publish_id = jsonR.get_int(
348         "body.publish_response.publish_id", &error);
349   } else if (msg.response_type == NAN_RESPONSE_SUBSCRIBE) {
350     msg.body.subscribe_response.subscribe_id = jsonR.get_int(
351         "body.subscribe_response.subscribe_id", &error);
352   } else if (msg.response_type == NAN_GET_CAPABILITIES) {
353     msg.body.nan_capabilities.max_concurrent_nan_clusters = jsonR.get_int(
354         "body.nan_capabilities.max_concurrent_nan_clusters", &error);
355     msg.body.nan_capabilities.max_publishes = jsonR.get_int(
356         "body.nan_capabilities.max_publishes", &error);
357     msg.body.nan_capabilities.max_subscribes = jsonR.get_int(
358         "body.nan_capabilities.max_subscribes", &error);
359     msg.body.nan_capabilities.max_service_name_len = jsonR.get_int(
360         "body.nan_capabilities.max_service_name_len", &error);
361     msg.body.nan_capabilities.max_match_filter_len = jsonR.get_int(
362         "body.nan_capabilities.max_match_filter_len", &error);
363     msg.body.nan_capabilities.max_total_match_filter_len = jsonR.get_int(
364         "body.nan_capabilities.max_total_match_filter_len", &error);
365     msg.body.nan_capabilities.max_service_specific_info_len = jsonR.get_int(
366         "body.nan_capabilities.max_service_specific_info_len", &error);
367     msg.body.nan_capabilities.max_vsa_data_len = jsonR.get_int(
368         "body.nan_capabilities.max_vsa_data_len", &error);
369     msg.body.nan_capabilities.max_mesh_data_len = jsonR.get_int(
370         "body.nan_capabilities.max_mesh_data_len", &error);
371     msg.body.nan_capabilities.max_ndi_interfaces = jsonR.get_int(
372         "body.nan_capabilities.max_ndi_interfaces", &error);
373     msg.body.nan_capabilities.max_ndp_sessions = jsonR.get_int(
374         "body.nan_capabilities.max_ndp_sessions", &error);
375     msg.body.nan_capabilities.max_app_info_len = jsonR.get_int(
376         "body.nan_capabilities.max_app_info_len", &error);
377   }
378 
379   if (error) {
380     ALOGE("Java_com_android_server_wifi_nan_WifiNanHalMock_callNotifyResponse: "
381           "error parsing args");
382     return;
383   }
384 
385   mCallbackHandlers.NotifyResponse(transaction_id, &msg);
386 }
387 
Java_com_android_server_wifi_nan_WifiNanHalMock_callPublishTerminated(JNIEnv * env,jclass clazz,jstring json_args_jstring)388 extern "C" void Java_com_android_server_wifi_nan_WifiNanHalMock_callPublishTerminated(
389     JNIEnv* env, jclass clazz, jstring json_args_jstring) {
390   ScopedUtfChars chars(env, json_args_jstring);
391   HalMockJsonReader jsonR(chars.c_str());
392   bool error = false;
393 
394   ALOGD(
395       "Java_com_android_server_wifi_nan_WifiNanHalMock_callPublishTerminated: '%s'",
396       chars.c_str());
397 
398   NanPublishTerminatedInd msg;
399   msg.publish_id = jsonR.get_int("publish_id", &error);
400   msg.reason = (NanStatusType) jsonR.get_int("reason", &error);
401 
402   if (error) {
403     ALOGE("Java_com_android_server_wifi_nan_WifiNanHalMock_callPublishTerminated: "
404           "error parsing args");
405     return;
406   }
407 
408   mCallbackHandlers.EventPublishTerminated(&msg);
409 }
410 
Java_com_android_server_wifi_nan_WifiNanHalMock_callSubscribeTerminated(JNIEnv * env,jclass clazz,jstring json_args_jstring)411 extern "C" void Java_com_android_server_wifi_nan_WifiNanHalMock_callSubscribeTerminated(
412     JNIEnv* env, jclass clazz, jstring json_args_jstring) {
413   ScopedUtfChars chars(env, json_args_jstring);
414   HalMockJsonReader jsonR(chars.c_str());
415   bool error = false;
416 
417   ALOGD(
418       "Java_com_android_server_wifi_nan_WifiNanHalMock_callSubscribeTerminated: '%s'",
419       chars.c_str());
420 
421   NanSubscribeTerminatedInd msg;
422   msg.subscribe_id = jsonR.get_int("subscribe_id", &error);
423   msg.reason = (NanStatusType) jsonR.get_int("reason", &error);
424 
425   if (error) {
426     ALOGE("Java_com_android_server_wifi_nan_WifiNanHalMock_callSubscribeTerminated:"
427           " error parsing args");
428     return;
429   }
430 
431   mCallbackHandlers.EventSubscribeTerminated(&msg);
432 }
433 
Java_com_android_server_wifi_nan_WifiNanHalMock_callFollowup(JNIEnv * env,jclass clazz,jstring json_args_jstring)434 extern "C" void Java_com_android_server_wifi_nan_WifiNanHalMock_callFollowup(
435     JNIEnv* env, jclass clazz, jstring json_args_jstring) {
436   ScopedUtfChars chars(env, json_args_jstring);
437   HalMockJsonReader jsonR(chars.c_str());
438   bool error = false;
439 
440   ALOGD("Java_com_android_server_wifi_nan_WifiNanHalMock_callFollowup: '%s'",
441         chars.c_str());
442 
443   NanFollowupInd msg;
444   msg.publish_subscribe_id = jsonR.get_int("publish_subscribe_id", &error);
445   msg.requestor_instance_id = jsonR.get_int("requestor_instance_id", &error);
446   jsonR.get_byte_array("addr", &error, msg.addr, NAN_MAC_ADDR_LEN);
447   msg.dw_or_faw = jsonR.get_int("dw_or_faw", &error);
448   msg.service_specific_info_len = jsonR.get_int("service_specific_info_len",
449                                                 &error);
450   jsonR.get_byte_array("service_specific_info", &error,
451                        msg.service_specific_info,
452                        NAN_MAX_SERVICE_SPECIFIC_INFO_LEN);
453 
454   if (error) {
455     ALOGE("Java_com_android_server_wifi_nan_WifiNanHalMock_callFollowup: "
456           "error parsing args");
457     return;
458   }
459 
460   mCallbackHandlers.EventFollowup(&msg);
461 }
462 
Java_com_android_server_wifi_nan_WifiNanHalMock_callMatch(JNIEnv * env,jclass clazz,jstring json_args_jstring)463 extern "C" void Java_com_android_server_wifi_nan_WifiNanHalMock_callMatch(
464     JNIEnv* env, jclass clazz, jstring json_args_jstring) {
465   ScopedUtfChars chars(env, json_args_jstring);
466   HalMockJsonReader jsonR(chars.c_str());
467   bool error = false;
468 
469   ALOGD("Java_com_android_server_wifi_nan_WifiNanHalMock_callMatch: '%s'",
470         chars.c_str());
471 
472   NanMatchInd msg;
473   msg.publish_subscribe_id = jsonR.get_int("publish_subscribe_id", &error);
474   msg.requestor_instance_id = jsonR.get_int("requestor_instance_id", &error);
475   jsonR.get_byte_array("addr", &error, msg.addr, NAN_MAC_ADDR_LEN);
476   msg.service_specific_info_len = jsonR.get_int("service_specific_info_len",
477                                                 &error);
478   jsonR.get_byte_array("service_specific_info", &error,
479                        msg.service_specific_info,
480                        NAN_MAX_SERVICE_SPECIFIC_INFO_LEN);
481   msg.sdf_match_filter_len = jsonR.get_int("sdf_match_filter_len", &error);
482   jsonR.get_byte_array("sdf_match_filter", &error, msg.sdf_match_filter,
483                        NAN_MAX_MATCH_FILTER_LEN);
484   /* a few more fields here - but not used (yet/never?) */
485 
486   if (error) {
487     ALOGE("Java_com_android_server_wifi_nan_WifiNanHalMock_callMatch: "
488           "error parsing args");
489     return;
490   }
491 
492   mCallbackHandlers.EventMatch(&msg);
493 }
494 
Java_com_android_server_wifi_nan_WifiNanHalMock_callDiscEngEvent(JNIEnv * env,jclass clazz,jstring json_args_jstring)495 extern "C" void Java_com_android_server_wifi_nan_WifiNanHalMock_callDiscEngEvent(
496     JNIEnv* env, jclass clazz, jstring json_args_jstring) {
497   ScopedUtfChars chars(env, json_args_jstring);
498   HalMockJsonReader jsonR(chars.c_str());
499   bool error = false;
500 
501   ALOGD("Java_com_android_server_wifi_nan_WifiNanHalMock_callDiscEngEvent: '%s'",
502         chars.c_str());
503 
504   NanDiscEngEventInd msg;
505   msg.event_type = (NanDiscEngEventType) jsonR.get_int("event_type", &error);
506   if (msg.event_type == NAN_EVENT_ID_DISC_MAC_ADDR) {
507     jsonR.get_byte_array("data", &error, msg.data.mac_addr.addr,
508                          NAN_MAC_ADDR_LEN);
509   } else {
510     jsonR.get_byte_array("data", &error, msg.data.cluster.addr,
511                          NAN_MAC_ADDR_LEN);
512   }
513 
514   if (error) {
515     ALOGE("Java_com_android_server_wifi_nan_WifiNanHalMock_callDiscEngEvent: "
516           "error parsing args");
517     return;
518   }
519 
520   mCallbackHandlers.EventDiscEngEvent(&msg);
521 }
522 
Java_com_android_server_wifi_nan_WifiNanHalMock_callDisabled(JNIEnv * env,jclass clazz,jstring json_args_jstring)523 extern "C" void Java_com_android_server_wifi_nan_WifiNanHalMock_callDisabled(
524     JNIEnv* env, jclass clazz, jstring json_args_jstring) {
525   ScopedUtfChars chars(env, json_args_jstring);
526   HalMockJsonReader jsonR(chars.c_str());
527   bool error = false;
528 
529   ALOGD("Java_com_android_server_wifi_nan_WifiNanHalMock_callDisabled: '%s'",
530         chars.c_str());
531 
532   NanDisabledInd msg;
533   msg.reason = (NanStatusType) jsonR.get_int("reason", &error);
534 
535   if (error) {
536     ALOGE("Java_com_android_server_wifi_nan_WifiNanHalMock_callDisabled: "
537           "error parsing args");
538     return;
539   }
540 
541   mCallbackHandlers.EventDisabled(&msg);
542 }
543 
544 // TODO: Not currently used: add as needed
545 //void (*EventUnMatch) (NanUnmatchInd* event);
546 //void (*EventTca) (NanTCAInd* event);
547 //void (*EventBeaconSdfPayload) (NanBeaconSdfPayloadInd* event);
548 
init_wifi_nan_hal_func_table_mock(wifi_hal_fn * fn)549 int init_wifi_nan_hal_func_table_mock(wifi_hal_fn *fn) {
550   if (fn == NULL) {
551     return -1;
552   }
553 
554   fn->wifi_nan_enable_request = wifi_nan_enable_request_mock;
555   fn->wifi_nan_disable_request = wifi_nan_disable_request_mock;
556   fn->wifi_nan_publish_request = wifi_nan_publish_request_mock;
557   fn->wifi_nan_publish_cancel_request =
558       wifi_nan_publish_cancel_request_mock;
559   fn->wifi_nan_subscribe_request = wifi_nan_subscribe_request_mock;
560   fn->wifi_nan_subscribe_cancel_request =
561       wifi_nan_subscribe_cancel_request_mock;
562   fn->wifi_nan_transmit_followup_request =
563       wifi_nan_transmit_followup_request_mock;
564   fn->wifi_nan_stats_request = wifi_nan_stats_request_mock;
565   fn->wifi_nan_config_request = wifi_nan_config_request_mock;
566   fn->wifi_nan_tca_request = wifi_nan_tca_request_mock;
567   fn->wifi_nan_beacon_sdf_payload_request =
568       wifi_nan_beacon_sdf_payload_request_mock;
569   fn->wifi_nan_register_handler = wifi_nan_register_handler_mock;
570   fn->wifi_nan_get_version = wifi_nan_get_version_mock;
571   fn->wifi_nan_get_capabilities = wifi_nan_get_capabilities_mock;
572 
573   return 0;
574 }
575 
Java_com_android_server_wifi_nan_WifiNanHalMock_initNanHalMock(JNIEnv * env,jclass clazz)576 extern "C" jint Java_com_android_server_wifi_nan_WifiNanHalMock_initNanHalMock(
577     JNIEnv* env, jclass clazz) {
578   Java_com_android_server_wifi_nan_WifiNanNative_registerNanNatives(env, clazz);
579   return init_wifi_nan_hal_func_table_mock(&hal_fn);
580 }
581 
582 }// namespace android
583