Accumulative patch from commit d5b559b6418c2bd09663e0d09e93a6592357fdce

d5b559b WNM: Add disassociation timeout processing for ESS_DISASSOC
f65f539 GAS: Reduce query timeout to two seconds
7a56af5 GAS: Assign new dialog token even if previous one is free
9e1ecab GAS: Ignore replays if previous frag_id without dropping GAS session
fa7ae95 Add test code for fetching the last configured GTK
576bce9 P2P: Direct global ctrl_iface commands automatically for P2P
c4bf83a P2P: No duplicate AP-STA-CONNECTED/DISCONNECTED as global event
7793c95 Clean up AP-STA-CONNECTED/DISCONNECTED prints
92c4465 P2P: Mark P2P events global (not specific to interface)
ed496f1 P2P: Clean up debug prints
710ae9a P2P: Move p2p_find stopped event message into p2p_supplicant.c
47bfe49 Add wpa_msg_global() for global events
214e428 Allow global ctrl_iface monitors
89c7ac5 wpa_cli: Set buffer length the same as in wpa_supplicant_ctrl_iface_process()
faf9a85 Add band option (2.4 vs. 5) for filtering scans
b83b1b2 Android: Clarify keystore include directories
6f1127c Android: Add a top level Android.mk
d2a9e2c Abstract and Android sockets for global ctrl_iface
6fd5cea wpa_cli: Allow global interface to be used in interactive mode
2925756 wpa_supplicant: Add -G argument to specify global ctrl group
cf3bebf Allow global ctrl_iface to be used for per-interface commands
058c863 FT RRB: Fix a memory leak on error path

Change-Id: I32a4afb43894167a30c4b0df18fd4846a2945c7c
Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
diff --git a/src/p2p/p2p.c b/src/p2p/p2p.c
index cbd039a..0534b8c 100644
--- a/src/p2p/p2p.c
+++ b/src/p2p/p2p.c
@@ -12,7 +12,6 @@
 #include "eloop.h"
 #include "common/ieee802_11_defs.h"
 #include "common/ieee802_11_common.h"
-#include "common/wpa_ctrl.h"
 #include "wps/wps_i.h"
 #include "p2p_i.h"
 #include "p2p.h"
@@ -119,8 +118,9 @@
 			continue;
 #endif
 
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
-			"entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
+		p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
+			MAC2STR(dev->info.p2p_device_addr));
+
 #ifdef ANDROID_P2P
 		/* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
 		if(&dev->list == p2p->sd_dev_list)
@@ -210,7 +210,7 @@
 
 void p2p_set_state(struct p2p_data *p2p, int new_state)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
+	p2p_dbg(p2p, "State %s -> %s",
 		p2p_state_txt(p2p->state), p2p_state_txt(new_state));
 	p2p->state = new_state;
 }
@@ -218,8 +218,7 @@
 
 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Set timeout (state=%s): %u.%06u sec",
+	p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
 		p2p_state_txt(p2p->state), sec, usec);
 	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
 	eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
@@ -228,8 +227,7 @@
 
 void p2p_clear_timeout(struct p2p_data *p2p)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
-		p2p_state_txt(p2p->state));
+	p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
 	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
 }
 
@@ -264,14 +262,12 @@
 	int freq;
 	struct wpabuf *ies;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Starting short listen state (state=%s)",
+	p2p_dbg(p2p, "Starting short listen state (state=%s)",
 		p2p_state_txt(p2p->state));
 
 	freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
 	if (freq < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Unknown regulatory class/channel");
+		p2p_dbg(p2p, "Unknown regulatory class/channel");
 		return;
 	}
 
@@ -286,8 +282,7 @@
 		tu = p2p->cfg->max_listen * 1000 / 1024;
 
 	if (tu == 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip listen state "
-			"since duration was 0 TU");
+		p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
 		p2p_set_timeout(p2p, 0, 0);
 		return;
 	}
@@ -302,8 +297,7 @@
 
 	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
 		    ies) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Failed to start listen mode");
+		p2p_dbg(p2p, "Failed to start listen mode");
 		p2p->pending_listen_freq = 0;
 	}
 	wpabuf_free(ies);
@@ -315,13 +309,11 @@
 	int freq;
 	struct wpabuf *ies;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Going to listen(only) state");
+	p2p_dbg(p2p, "Going to listen(only) state");
 
 	freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
 	if (freq < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Unknown regulatory class/channel");
+		p2p_dbg(p2p, "Unknown regulatory class/channel");
 		return -1;
 	}
 
@@ -331,13 +323,10 @@
 
 	if (p2p->p2p_scan_running) {
 		if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: p2p_scan running - connect is already "
-				"pending - skip listen");
+			p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
 			return 0;
 		}
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: p2p_scan running - delay start of listen state");
+		p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
 		p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
 		return 0;
 	}
@@ -347,8 +336,7 @@
 		return -1;
 
 	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Failed to start listen mode");
+		p2p_dbg(p2p, "Failed to start listen mode");
 		p2p->pending_listen_freq = 0;
 		wpabuf_free(ies);
 		return -1;
@@ -430,9 +418,7 @@
 			oldest = dev;
 	}
 	if (count + 1 > p2p->cfg->max_peers && oldest) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Remove oldest peer entry to make room for a new "
-			"peer");
+		p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
 #ifdef ANDROID_P2P
 		/* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
 		if(&oldest->list == p2p->sd_dev_list)
@@ -546,8 +532,8 @@
 }
 
 
-static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
-			      const struct p2p_message *msg)
+static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
+			      int probe_req, const struct p2p_message *msg)
 {
 	os_memcpy(dev->info.device_name, msg->device_name,
 		  sizeof(dev->info.device_name));
@@ -622,11 +608,11 @@
 			msg->config_methods : msg->wps_config_methods;
 		if (new_config_methods &&
 		    dev->info.config_methods != new_config_methods) {
-			wpa_printf(MSG_DEBUG, "P2P: Update peer " MACSTR
-				   " config_methods 0x%x -> 0x%x",
-				   MAC2STR(dev->info.p2p_device_addr),
-				   dev->info.config_methods,
-				   new_config_methods);
+			p2p_dbg(p2p, "Update peer " MACSTR
+				" config_methods 0x%x -> 0x%x",
+				MAC2STR(dev->info.p2p_device_addr),
+				dev->info.config_methods,
+				new_config_methods);
 			dev->info.config_methods = new_config_methods;
 		}
 	}
@@ -663,8 +649,7 @@
 
 	os_memset(&msg, 0, sizeof(msg));
 	if (p2p_parse_ies(ies, ies_len, &msg)) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Failed to parse P2P IE for a device entry");
+		p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
 		p2p_parse_free(&msg);
 		return -1;
 	}
@@ -674,18 +659,15 @@
 	else if (msg.device_id)
 		p2p_dev_addr = msg.device_id;
 	else {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Ignore scan data without P2P Device Info or "
-			"P2P Device Id");
+		p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
 		p2p_parse_free(&msg);
 		return -1;
 	}
 
 	if (!is_zero_ether_addr(p2p->peer_filter) &&
 	    os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
-			"filter for " MACSTR " due to peer filter",
-			MAC2STR(p2p_dev_addr));
+		p2p_dbg(p2p, "Do not add peer filter for " MACSTR
+			" due to peer filter", MAC2STR(p2p_dev_addr));
 		p2p_parse_free(&msg);
 		return 0;
 	}
@@ -707,9 +689,7 @@
 	 */
 	if (dev->last_seen.sec > 0 &&
 	    os_time_before(rx_time, &dev->last_seen)) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not update peer "
-			"entry based on old frame (rx_time=%u.%06u "
-			"last_seen=%u.%06u)",
+		p2p_dbg(p2p, "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u)",
 			(unsigned int) rx_time->sec,
 			(unsigned int) rx_time->usec,
 			(unsigned int) dev->last_seen.sec,
@@ -740,18 +720,15 @@
 		else
 			ds_freq = 2407 + *msg.ds_params * 5;
 		if (freq != ds_freq) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Update Listen frequency based on DS "
-				"Parameter Set IE: %d -> %d MHz",
+			p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
 				freq, ds_freq);
 			freq = ds_freq;
 		}
 	}
 
 	if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Update Listen frequency based on scan "
-			"results (" MACSTR " %d -> %d MHz (DS param %d)",
+		p2p_dbg(p2p, "Update Listen frequency based on scan results ("
+			MACSTR " %d -> %d MHz (DS param %d)",
 			MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
 			freq, msg.ds_params ? *msg.ds_params : -1);
 	}
@@ -762,7 +739,7 @@
 	}
 	dev->info.level = level;
 
-	p2p_copy_wps_info(dev, 0, &msg);
+	p2p_copy_wps_info(p2p, dev, 0, &msg);
 
 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
 		wpabuf_free(dev->info.wps_vendor_ext[i]);
@@ -796,13 +773,11 @@
 	if (dev->flags & P2P_DEV_REPORTED)
 		return 0;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Peer found with Listen frequency %d MHz "
-		"(rx_time=%u.%06u)", freq, (unsigned int) rx_time->sec,
+	p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
+		freq, (unsigned int) rx_time->sec,
 		(unsigned int) rx_time->usec);
 	if (dev->flags & P2P_DEV_USER_REJECTED) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Do not report rejected device");
+		p2p_dbg(p2p, "Do not report rejected device");
 		return 0;
 	}
 
@@ -822,9 +797,8 @@
 		 * Probe Response frame that includes the config_methods
 		 * information.
 		 */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Do not report peer " MACSTR " with unknown "
-			"config methods", MAC2STR(addr));
+		p2p_dbg(p2p, "Do not report peer " MACSTR
+			" with unknown config methods", MAC2STR(addr));
 		return 0;
 	}
 
@@ -913,8 +887,7 @@
 	}
 
 	freq = p2p_channel_to_freq(reg_class, channel);
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
-		"channel: reg_class %u channel %u -> %d MHz",
+	p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
 		reg_class, channel, freq);
 	p2p->last_prog_scan_class = reg_class;
 	p2p->last_prog_scan_chan = channel;
@@ -933,9 +906,7 @@
 	int res;
 
 	if (p2p->drv_in_listen) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
-			"in Listen state - wait for it to end before "
-			"continuing");
+		p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
 		return;
 	}
 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
@@ -943,27 +914,23 @@
 	if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
 	    (freq = p2p_get_next_prog_freq(p2p)) > 0) {
 		type = P2P_SCAN_SOCIAL_PLUS_ONE;
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
-			"(+ freq %u)", freq);
+		p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
 	} else {
 		type = P2P_SCAN_SOCIAL;
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
+		p2p_dbg(p2p, "Starting search");
 	}
 
 	res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
 				 p2p->num_req_dev_types, p2p->req_dev_types,
 				 p2p->find_dev_id, pw_id);
 	if (res < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Scan request failed");
+		p2p_dbg(p2p, "Scan request failed");
 		p2p_continue_find(p2p);
 	} else if (res == 1) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
-			"p2p_scan at this point - will try again after "
-			"previous scan completes");
+		p2p_dbg(p2p, "Could not start p2p_scan at this point - will try again after previous scan completes");
 		p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
 	} else {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
+		p2p_dbg(p2p, "Running p2p_scan");
 		p2p->p2p_scan_running = 1;
 		eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
 		eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
@@ -975,7 +942,7 @@
 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
 {
 	struct p2p_data *p2p = eloop_ctx;
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
+	p2p_dbg(p2p, "Find timeout -> stop");
 	p2p_stop_find(p2p);
 }
 
@@ -987,8 +954,7 @@
 
 	if (p2p->after_scan_tx) {
 		p2p->after_scan_tx_in_progress = 1;
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
-			"Action frame at p2p_scan completion");
+		p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
 		p2p->cfg->send_action(p2p->cfg->cb_ctx,
 				      p2p->after_scan_tx->freq,
 				      p2p->after_scan_tx->dst,
@@ -1016,19 +982,16 @@
 	case P2P_AFTER_SCAN_NOTHING:
 		break;
 	case P2P_AFTER_SCAN_LISTEN:
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
-			"requested Listen state");
+		p2p_dbg(p2p, "Start previously requested Listen state");
 		p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
 			   p2p->pending_listen_usec / 1000);
 		return 1;
 	case P2P_AFTER_SCAN_CONNECT:
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
-			"requested connect with " MACSTR,
+		p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
 			MAC2STR(p2p->after_scan_peer));
 		dev = p2p_get_device(p2p, p2p->after_scan_peer);
 		if (dev == NULL) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
-				"known anymore");
+			p2p_dbg(p2p, "Peer not known anymore");
 			break;
 		}
 		p2p_connect_send(p2p, dev);
@@ -1043,8 +1006,7 @@
 {
 	struct p2p_data *p2p = eloop_ctx;
 	int running;
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
-		"(running=%d)", p2p->p2p_scan_running);
+	p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
 	running = p2p->p2p_scan_running;
 	/* Make sure we recover from missed scan results callback */
 	p2p->p2p_scan_running = 0;
@@ -1069,12 +1031,10 @@
 {
 	int res;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
-		type);
+	p2p_dbg(p2p, "Starting find (type=%d)", type);
 	os_get_time(&p2p->find_start);
 	if (p2p->p2p_scan_running) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
-			"already running");
+		p2p_dbg(p2p, "p2p_scan is already running");
 	}
 
 	p2p_free_req_dev_types(p2p);
@@ -1126,21 +1086,18 @@
 	}
 
 	if (res == 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
+		p2p_dbg(p2p, "Running p2p_scan");
 		p2p->p2p_scan_running = 1;
 		eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
 		eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
 				       p2p, NULL);
 	} else if (res == 1) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
-			"p2p_scan at this point - will try again after "
-			"previous scan completes");
+		p2p_dbg(p2p, "Could not start p2p_scan at this point - will try again after previous scan completes");
 		res = 0;
 		p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
 		eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
 	} else {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
-			"p2p_scan");
+		p2p_dbg(p2p, "Failed to start p2p_scan");
 		p2p_set_state(p2p, P2P_IDLE);
 		eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
 	}
@@ -1170,12 +1127,11 @@
 	}
 	if (p2p->state != P2P_SEARCH_WHEN_READY)
 		return 0;
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
-		"now that previous scan was completed");
+	p2p_dbg(p2p, "Starting pending P2P find now that previous scan was completed");
 	if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
 		     p2p->num_req_dev_types, p2p->req_dev_types,
 		     p2p->find_dev_id, p2p->search_delay) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
+		p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
 		return 0;
 	}
 	return 1;
@@ -1184,13 +1140,13 @@
 
 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
+	p2p_dbg(p2p, "Stopping find");
 	eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
 	p2p_clear_timeout(p2p);
 	if (p2p->state == P2P_SEARCH ||
 	    p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY ||
 	    p2p->state == P2P_SEARCH_WHEN_READY)
-		wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
+		p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
 	p2p_set_state(p2p, P2P_IDLE);
 	p2p_free_req_dev_types(p2p);
 	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
@@ -1206,8 +1162,7 @@
 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
 {
 	if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
-			"since we are on correct channel for response");
+		p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
 		return;
 	}
 	if (p2p->in_listen) {
@@ -1220,8 +1175,7 @@
 		 * when the operation gets canceled, so clear the internal
 		 * variable that is tracking driver state.
 		 */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
-			"drv_in_listen (%d)", p2p->drv_in_listen);
+		p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
 		p2p->drv_in_listen = 0;
 	}
 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
@@ -1242,15 +1196,13 @@
 	unsigned int freq = force_freq ? force_freq : pref_freq;
 
 	if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Unsupported frequency %u MHz", freq);
+		p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
 		return -1;
 	}
 
 	if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel)) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Frequency %u MHz (oper_class %u channel %u) not "
-			"allowed for P2P", freq, op_class, op_channel);
+		p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
+			freq, op_class, op_channel);
 		return -1;
 	}
 
@@ -1279,24 +1231,21 @@
 	    p2p_supported_freq(p2p, p2p->best_freq_overall) &&
 	    p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
 	    == 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best "
-			"overall channel as operating channel preference");
+		p2p_dbg(p2p, "Select best overall channel as operating channel preference");
 		p2p->op_reg_class = op_class;
 		p2p->op_channel = op_channel;
 	} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
 		   p2p_supported_freq(p2p, p2p->best_freq_5) &&
 		   p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
 		   == 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best 5 GHz "
-			"channel as operating channel preference");
+		p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
 		p2p->op_reg_class = op_class;
 		p2p->op_channel = op_channel;
 	} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
 		   p2p_supported_freq(p2p, p2p->best_freq_24) &&
 		   p2p_freq_to_channel(p2p->best_freq_24, &op_class,
 				       &op_channel) == 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best 2.4 "
-			"GHz channel as operating channel preference");
+		p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
 		p2p->op_reg_class = op_class;
 		p2p->op_channel = op_channel;
 	} else {
@@ -1331,9 +1280,7 @@
 	} else {
 		p2p_prepare_channel_best(p2p);
 	}
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Own preference for operation channel: "
-		"Operating Class %u Channel %u%s",
+	p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
 		p2p->op_reg_class, p2p->op_channel,
 		force_freq ? " (forced)" : "");
 
@@ -1375,8 +1322,7 @@
 {
 	struct p2p_device *dev;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Request to start group negotiation - peer=" MACSTR
+	p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
 		"  GO Intent=%d  Intended Interface Address=" MACSTR
 		" wps_method=%d persistent_group=%d pd_before_go_neg=%d",
 		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
@@ -1384,8 +1330,7 @@
 
 	dev = p2p_get_device(p2p, peer_addr);
 	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Cannot connect to unknown P2P Device " MACSTR,
+		p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
 			MAC2STR(peer_addr));
 		return -1;
 	}
@@ -1396,15 +1341,13 @@
 	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
 		if (!(dev->info.dev_capab &
 		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Cannot connect to P2P Device " MACSTR
+			p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
 				" that is in a group and is not discoverable",
 				MAC2STR(peer_addr));
 			return -1;
 		}
 		if (dev->oper_freq <= 0) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Cannot connect to P2P Device " MACSTR
+			p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
 				" with incomplete information",
 				MAC2STR(peer_addr));
 			return -1;
@@ -1460,9 +1403,7 @@
 		 * new GO Negotiation, e.g., when the pending frame was from a
 		 * previous attempt at starting a GO Negotiation.
 		 */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
-			"previous pending Action frame TX that was waiting "
-			"for p2p_scan completion");
+		p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
 		os_free(p2p->after_scan_tx);
 		p2p->after_scan_tx = NULL;
 	}
@@ -1471,8 +1412,7 @@
 	dev->status = P2P_SC_SUCCESS;
 
 	if (p2p->p2p_scan_running) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: p2p_scan running - delay connect send");
+		p2p_dbg(p2p, "p2p_scan running - delay connect send");
 		p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
 		os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
 		return 0;
@@ -1492,8 +1432,7 @@
 {
 	struct p2p_device *dev;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Request to authorize group negotiation - peer=" MACSTR
+	p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
 		"  GO Intent=%d  Intended Interface Address=" MACSTR
 		" wps_method=%d  persistent_group=%d",
 		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
@@ -1501,8 +1440,7 @@
 
 	dev = p2p_get_device(p2p, peer_addr);
 	if (dev == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Cannot authorize unknown P2P Device " MACSTR,
+		p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
 			MAC2STR(peer_addr));
 		return -1;
 	}
@@ -1539,15 +1477,14 @@
 {
 	os_get_time(&dev->last_seen);
 
-	p2p_copy_wps_info(dev, 0, msg);
+	p2p_copy_wps_info(p2p, dev, 0, msg);
 
 	if (msg->listen_channel) {
 		int freq;
 		freq = p2p_channel_to_freq(msg->listen_channel[3],
 					   msg->listen_channel[4]);
 		if (freq < 0) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Unknown peer Listen channel: "
+			p2p_dbg(p2p, "Unknown peer Listen channel: "
 				"country=%c%c(0x%02x) reg_class=%u channel=%u",
 				msg->listen_channel[0],
 				msg->listen_channel[1],
@@ -1555,8 +1492,8 @@
 				msg->listen_channel[3],
 				msg->listen_channel[4]);
 		} else {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
-				"peer " MACSTR " Listen channel: %u -> %u MHz",
+			p2p_dbg(p2p, "Update peer " MACSTR
+				" Listen channel: %u -> %u MHz",
 				MAC2STR(dev->info.p2p_device_addr),
 				dev->listen_freq, freq);
 			dev->listen_freq = freq;
@@ -1570,12 +1507,9 @@
 
 	if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
 		dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Completed device entry based on data from "
-			"GO Negotiation Request");
+		p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
 	} else {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Created device entry based on GO Neg Req: "
+		p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
 			MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
 			"listen_freq=%d",
 			MAC2STR(dev->info.p2p_device_addr),
@@ -1586,8 +1520,7 @@
 	dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
 
 	if (dev->flags & P2P_DEV_USER_REJECTED) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Do not report rejected device");
+		p2p_dbg(p2p, "Do not report rejected device");
 		return;
 	}
 
@@ -1623,10 +1556,8 @@
 	int freqs;
 	size_t i, j;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: GO Negotiation with " MACSTR " completed (%s will be "
-		"GO)", MAC2STR(peer->info.p2p_device_addr),
-		go ? "local end" : "peer");
+	p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
+		MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
 
 	os_memset(&res, 0, sizeof(res));
 	res.role_go = go;
@@ -1688,8 +1619,7 @@
 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
 			      const u8 *data, size_t len, int rx_freq)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
+	p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
 	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
 
 	if (len < 1)
@@ -1725,8 +1655,7 @@
 		p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
 		break;
 	default:
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Unsupported P2P Public Action frame type %d",
+		p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
 			data[0]);
 		break;
 	}
@@ -1801,16 +1730,14 @@
 	len--;
 
 	/* P2P action frame */
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
+	p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
 	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
 
 	if (len < 1)
 		return;
 	switch (data[0]) {
 	case P2P_NOA:
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Received P2P Action - Notice of Absence");
+		p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
 		/* TODO */
 		break;
 	case P2P_PRESENCE_REQ:
@@ -1823,8 +1750,7 @@
 		p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
 		break;
 	default:
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Received P2P Action - unknown type %u", data[0]);
+		p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
 		break;
 	}
 }
@@ -1900,7 +1826,7 @@
 						       msg.listen_channel[4]);
 	}
 
-	p2p_copy_wps_info(dev, 1, &msg);
+	p2p_copy_wps_info(p2p, dev, 1, &msg);
 
 	if (msg.wfd_subelems) {
 		wpabuf_free(dev->info.wfd_subelems);
@@ -1909,8 +1835,7 @@
 
 	p2p_parse_free(&msg);
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Created device entry based on Probe Req: " MACSTR
+	p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
 		" dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
 		MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
 		dev->info.group_capab, dev->info.device_name,
@@ -2152,8 +2077,7 @@
 		return P2P_PREQ_NOT_PROCESSED;
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Reply to P2P Probe Request in Listen state");
+	p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
 
 	/*
 	 * We do not really have a specific BSS that this frame is advertising,
@@ -2232,9 +2156,7 @@
 	    == 0 &&
 	    !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
 		/* Received a Probe Request from GO Negotiation peer */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Found GO Negotiation peer - try to start GO "
-			"negotiation from timeout");
+		p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
 		eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
 		eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
 		return P2P_PREQ_PROCESSED;
@@ -2245,9 +2167,7 @@
 	    os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
 	    == 0) {
 		/* Received a Probe Request from Invite peer */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Found Invite peer - try to start Invite from "
-			"timeout");
+		p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
 		eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
 		return P2P_PREQ_PROCESSED;
 	}
@@ -2422,24 +2342,20 @@
 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
 {
 	if (p2p->go_neg_peer == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: No pending Group Formation - "
-			"ignore WPS registration success notification");
+		p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
 		return; /* No pending Group Formation */
 	}
 
 	if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
 	    0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Ignore WPS registration success notification "
-			"for " MACSTR " (GO Negotiation peer " MACSTR ")",
+		p2p_dbg(p2p, "Ignore WPS registration success notification for "
+			MACSTR " (GO Negotiation peer " MACSTR ")",
 			MAC2STR(mac_addr),
 			MAC2STR(p2p->go_neg_peer->intended_addr));
 		return; /* Ignore unexpected peer address */
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Group Formation completed successfully with " MACSTR,
+	p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
 		MAC2STR(mac_addr));
 
 	p2p_clear_go_neg(p2p);
@@ -2449,14 +2365,11 @@
 void p2p_group_formation_failed(struct p2p_data *p2p)
 {
 	if (p2p->go_neg_peer == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: No pending Group Formation - "
-			"ignore group formation failure notification");
+		p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
 		return; /* No pending Group Formation */
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Group Formation failed with " MACSTR,
+	p2p_dbg(p2p, "Group Formation failed with " MACSTR,
 		MAC2STR(p2p->go_neg_peer->intended_addr));
 
 	p2p_clear_go_neg(p2p);
@@ -2593,8 +2506,7 @@
 	if (dev == NULL)
 		return -1;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
-		MAC2STR(addr));
+	p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
 
 	if (p2p->go_neg_peer == dev)
 		p2p->go_neg_peer = NULL;
@@ -2795,8 +2707,7 @@
 				break;
 		} else if (dev->req_config_methods &&
 			   !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
-				"pending Provision Discovery Request to "
+			p2p_dbg(p2p, "Send pending Provision Discovery Request to "
 				MACSTR " (config methods 0x%x)",
 				MAC2STR(dev->info.p2p_device_addr),
 				dev->req_config_methods);
@@ -2811,8 +2722,7 @@
 
 static void p2p_sd_cb(struct p2p_data *p2p, int success)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Service Discovery Query TX callback: success=%d",
+	p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
 		success);
 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
 
@@ -2826,8 +2736,7 @@
 	}
 
 	if (p2p->sd_peer == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: No SD peer entry known");
+		p2p_dbg(p2p, "No SD peer entry known");
 		p2p_continue_find(p2p);
 		return;
 	}
@@ -2861,8 +2770,7 @@
 		if (!dev->req_config_methods)
 			continue;
 
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
-			"pending Provision Discovery Request to "
+		p2p_dbg(p2p, "Send pending Provision Discovery Request to "
 			MACSTR " (config methods 0x%x)",
 			MAC2STR(dev->info.p2p_device_addr),
 			dev->req_config_methods);
@@ -2875,8 +2783,7 @@
 
 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Provision Discovery Request TX callback: success=%d",
+	p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
 		success);
 
 	/*
@@ -2943,8 +2850,8 @@
 		 * that have based on frames received after the last p2p_find
 		 * operation was started.
 		 */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore old scan "
-			"result for " MACSTR " (rx_time=%u.%06u)",
+		p2p_dbg(p2p, "Ignore old scan result for " MACSTR
+			" (rx_time=%u.%06u)",
 			MAC2STR(bssid), (unsigned int) rx_time->sec,
 			(unsigned int) rx_time->usec);
 		return 0;
@@ -2959,8 +2866,7 @@
 void p2p_scan_res_handled(struct p2p_data *p2p)
 {
 	if (!p2p->p2p_scan_running) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
-			"running, but scan results received");
+		p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
 	}
 	p2p->p2p_scan_running = 0;
 	eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
@@ -3022,13 +2928,10 @@
 	struct p2p_device *dev = p2p->go_neg_peer;
 	int timeout;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: GO Negotiation Request TX callback: success=%d",
-		success);
+	p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
 
 	if (dev == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: No pending GO Negotiation");
+		p2p_dbg(p2p, "No pending GO Negotiation");
 		return;
 	}
 
@@ -3045,9 +2948,7 @@
 	if (!success &&
 	    (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
 	    !is_zero_ether_addr(dev->member_in_go_dev)) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Peer " MACSTR " did not acknowledge request - "
-			"try to use device discoverability through its GO",
+		p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
 			MAC2STR(dev->info.p2p_device_addr));
 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
 		p2p_send_dev_disc_req(p2p, dev);
@@ -3078,13 +2979,10 @@
 
 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: GO Negotiation Response TX callback: success=%d",
+	p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
 		success);
 	if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Ignore TX callback event - GO Negotiation is "
-			"not running anymore");
+		p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
 		return;
 	}
 	p2p_set_state(p2p, P2P_CONNECT);
@@ -3095,9 +2993,7 @@
 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
 				       const u8 *addr)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: GO Negotiation Response (failure) TX callback: "
-		"success=%d", success);
+	p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
 	if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
 		p2p_go_neg_failed(p2p, p2p->go_neg_peer,
 				  p2p->go_neg_peer->status);
@@ -3116,9 +3012,7 @@
 {
 	struct p2p_device *dev;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: GO Negotiation Confirm TX callback: result=%d",
-		result);
+	p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
 	if (result == P2P_SEND_ACTION_FAILED) {
 		p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
@@ -3135,10 +3029,7 @@
 		 * peer did indeed receive the frame, continue regardless of
 		 * the TX status.
 		 */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Assume GO Negotiation Confirm TX was actually "
-			"received by the peer even though Ack was not "
-			"reported");
+		p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
 	}
 
 	dev = p2p->go_neg_peer;
@@ -3156,8 +3047,7 @@
 	enum p2p_pending_action_state state;
 	int success;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
+	p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
 		" src=" MACSTR " bssid=" MACSTR " result=%d",
 		p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
 		MAC2STR(bssid), result);
@@ -3172,9 +3062,7 @@
 			    p2p_run_after_scan(p2p))
 				break;
 			if (p2p->state == P2P_SEARCH) {
-				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-					"P2P: Continue find after "
-					"after_scan_tx completion");
+				p2p_dbg(p2p, "Continue find after after_scan_tx completion");
 				p2p_continue_find(p2p);
 			}
 		}
@@ -3222,23 +3110,18 @@
 		   unsigned int duration)
 {
 	if (freq == p2p->pending_client_disc_freq) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Client discoverability remain-awake completed");
+		p2p_dbg(p2p, "Client discoverability remain-awake completed");
 		p2p->pending_client_disc_freq = 0;
 		return;
 	}
 
 	if (freq != p2p->pending_listen_freq) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Unexpected listen callback for freq=%u "
-			"duration=%u (pending_listen_freq=%u)",
+		p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
 			freq, duration, p2p->pending_listen_freq);
 		return;
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
-		"callback",
+	p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
 		p2p->pending_listen_sec, p2p->pending_listen_usec,
 		p2p->pending_listen_freq);
 	p2p->in_listen = 1;
@@ -3259,17 +3142,14 @@
 
 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
-		"state (freq=%u)", freq);
+	p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
 	p2p->drv_in_listen = 0;
 	if (p2p->in_listen)
 		return 0; /* Internal timeout will trigger the next step */
 
 	if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
 		if (p2p->go_neg_peer->connect_reqs >= 120) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Timeout on sending GO Negotiation "
-				"Request without getting response");
+			p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
 			p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
 			return 0;
 		}
@@ -3287,9 +3167,7 @@
 			  * operation while in p2p_find. Avoid an attempt to
 			  * restart a scan here.
 			  */
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
-				"already in progress - do not try to start a "
-				"new one");
+			p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
 			return 1;
 		}
 		if (p2p->pending_listen_freq) {
@@ -3298,15 +3176,12 @@
 			 * offchannel operation for some reason. p2p_search()
 			 * will be started from internal timeout.
 			 */
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
-				"operation did not seem to start - delay "
-				"search phase to avoid busy loop");
+			p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
 			p2p_set_timeout(p2p, 0, 100000);
 			return 1;
 		}
 		if (p2p->search_delay) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
-				"search operation by %u ms",
+			p2p_dbg(p2p, "Delay search operation by %u ms",
 				p2p->search_delay);
 			p2p_set_timeout(p2p, p2p->search_delay / 1000,
 					(p2p->search_delay % 1000) * 1000);
@@ -3325,17 +3200,14 @@
 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
 	if (p2p->go_neg_peer &&
 	    (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
-			"Negotiation Confirm timed out - assume GO "
-			"Negotiation failed");
+		p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
 		p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
 		return;
 	}
 	if (p2p->go_neg_peer &&
 	    (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
 	    p2p->go_neg_peer->connect_reqs < 120) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer expected to "
-			"wait our response - skip listen");
+		p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
 		p2p_connect_send(p2p, p2p->go_neg_peer);
 		return;
 	}
@@ -3349,16 +3221,12 @@
 {
 	if (p2p->go_neg_peer) {
 		if (p2p->drv_in_listen) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
-				"still in Listen state; wait for it to "
-				"complete");
+			p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
 			return;
 		}
 
 		if (p2p->go_neg_peer->connect_reqs >= 120) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Timeout on sending GO Negotiation "
-				"Request without getting response");
+			p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
 			p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
 			return;
 		}
@@ -3387,23 +3255,18 @@
 	struct p2p_device *dev = p2p->go_neg_peer;
 
 	if (dev == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Unknown GO Neg peer - stop GO Neg wait");
+		p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
 		return;
 	}
 
 	dev->wait_count++;
 	if (dev->wait_count >= 120) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Timeout on waiting peer to become ready for GO "
-			"Negotiation");
+		p2p_dbg(p2p, "Timeout on waiting peer to become ready for GO Negotiation");
 		p2p_go_neg_failed(p2p, dev, -1);
 		return;
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Go to Listen state while waiting for the peer to become "
-		"ready for GO Negotiation");
+	p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
 	p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
 	p2p_listen_in_find(p2p, 0);
 }
@@ -3411,8 +3274,7 @@
 
 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Service Discovery Query timeout");
+	p2p_dbg(p2p, "Service Discovery Query timeout");
 	if (p2p->sd_peer) {
 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
 		p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
@@ -3424,8 +3286,7 @@
 
 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Provision Discovery Request timeout");
+	p2p_dbg(p2p, "Provision Discovery Request timeout");
 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
 	p2p_continue_find(p2p);
 }
@@ -3443,8 +3304,7 @@
 	if (!p2p->user_initiated_pd)
 		return;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: User initiated Provision Discovery Request timeout");
+	p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
 
 	if (p2p->pd_retries) {
 		p2p->pd_retries--;
@@ -3482,8 +3342,7 @@
 		 * Better remain on operating channel instead of listen channel
 		 * when running a group.
 		 */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
-			"active GO role - wait on operating channel");
+		p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
 		p2p_set_timeout(p2p, 0, 100000);
 		return;
 	}
@@ -3499,8 +3358,7 @@
 				p2p->invite_go_dev_addr);
 	} else {
 		if (p2p->invite_peer) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Invitation Request retry limit reached");
+			p2p_dbg(p2p, "Invitation Request retry limit reached");
 			if (p2p->cfg->invitation_result)
 				p2p->cfg->invitation_result(
 					p2p->cfg->cb_ctx, -1, NULL, NULL,
@@ -3515,8 +3373,7 @@
 {
 	struct p2p_data *p2p = eloop_ctx;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
-		p2p_state_txt(p2p->state));
+	p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
 
 	p2p->in_listen = 0;
 
@@ -3531,8 +3388,7 @@
 		if (p2p->pending_action_state == P2P_PENDING_PD)
 			p2p_timeout_prov_disc_req(p2p);
 		if (p2p->search_delay && !p2p->in_search_delay) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
-				"search operation by %u ms",
+			p2p_dbg(p2p, "Delay search operation by %u ms",
 				p2p->search_delay);
 			p2p->in_search_delay = 1;
 			p2p_set_timeout(p2p, p2p->search_delay / 1000,
@@ -3556,9 +3412,7 @@
 			p2p_timeout_prov_disc_req(p2p);
 
 		if (p2p->ext_listen_only) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-				"P2P: Extended Listen Timing - Listen State "
-				"completed");
+			p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
 			p2p->ext_listen_only = 0;
 			p2p_set_state(p2p, P2P_IDLE);
 		}
@@ -3596,11 +3450,10 @@
 	struct p2p_device *dev;
 
 	dev = p2p_get_device(p2p, peer_addr);
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
-		"connection attempts by peer " MACSTR, MAC2STR(peer_addr));
+	p2p_dbg(p2p, "Local request to reject connection attempts by peer "
+		MACSTR, MAC2STR(peer_addr));
 	if (dev == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
-			" unknown", MAC2STR(peer_addr));
+		p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
 		return -1;
 	}
 	dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
@@ -3801,12 +3654,10 @@
 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
 {
 	if (enabled) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
-			"discoverability enabled");
+		p2p_dbg(p2p, "Client discoverability enabled");
 		p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
 	} else {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
-			"discoverability disabled");
+		p2p_dbg(p2p, "Client discoverability disabled");
 		p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
 	}
 }
@@ -3855,9 +3706,9 @@
 {
 	struct wpabuf *req;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
-		"GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
-		"int1=%u dur2=%u int2=%u",
+	p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
+		" (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
+		"dur2=%u int2=%u",
 		MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
 		freq, duration1, interval1, duration2, interval2);
 
@@ -3870,8 +3721,7 @@
 	if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
 			    go_interface_addr,
 			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Failed to send Action frame");
+		p2p_dbg(p2p, "Failed to send Action frame");
 	}
 	wpabuf_free(req);
 
@@ -3917,8 +3767,7 @@
 	u8 noa[50];
 	int noa_len;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Received P2P Action - P2P Presence Request");
+	p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
 
 	for (g = 0; g < p2p->num_groups; g++) {
 		if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
@@ -3928,23 +3777,20 @@
 		}
 	}
 	if (group == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Ignore P2P Presence Request for unknown group "
+		p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
 			MACSTR, MAC2STR(da));
 		return;
 	}
 
 	if (p2p_parse(data, len, &msg) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Failed to parse P2P Presence Request");
+		p2p_dbg(p2p, "Failed to parse P2P Presence Request");
 		status = P2P_SC_FAIL_INVALID_PARAMS;
 		goto fail;
 	}
 	parsed = 1;
 
 	if (msg.noa == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: No NoA attribute in P2P Presence Request");
+		p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
 		status = P2P_SC_FAIL_INVALID_PARAMS;
 		goto fail;
 	}
@@ -3968,8 +3814,7 @@
 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
 	if (p2p_send_action(p2p, rx_freq, sa, da, da,
 			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Failed to send Action frame");
+		p2p_dbg(p2p, "Failed to send Action frame");
 	}
 	wpabuf_free(resp);
 }
@@ -3980,33 +3825,27 @@
 {
 	struct p2p_message msg;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Received P2P Action - P2P Presence Response");
+	p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
 
 	if (p2p_parse(data, len, &msg) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Failed to parse P2P Presence Response");
+		p2p_dbg(p2p, "Failed to parse P2P Presence Response");
 		return;
 	}
 
 	if (msg.status == NULL || msg.noa == NULL) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: No Status or NoA attribute in P2P Presence "
-			"Response");
+		p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
 		p2p_parse_free(&msg);
 		return;
 	}
 
 	if (*msg.status) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: P2P Presence Request was rejected: status %u",
+		p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
 			*msg.status);
 		p2p_parse_free(&msg);
 		return;
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: P2P Presence Request was accepted");
+	p2p_dbg(p2p, "P2P Presence Request was accepted");
 	wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
 		    msg.noa, msg.noa_len);
 	/* TODO: process NoA */
@@ -4032,25 +3871,20 @@
 		 * running at an inconvenient time. As a workaround, allow new
 		 * Extended Listen operation to be started.
 		 */
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
-			"Extended Listen operation had not been completed - "
-			"try again");
+		p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
 		p2p->ext_listen_only = 0;
 		p2p_set_state(p2p, P2P_IDLE);
 	}
 
 	if (p2p->state != P2P_IDLE) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
-			"Listen timeout in active state (%s)",
-			p2p_state_txt(p2p->state));
+		p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
 		return;
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
+	p2p_dbg(p2p, "Extended Listen timeout");
 	p2p->ext_listen_only = 1;
 	if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
-			"Listen state for Extended Listen Timing");
+		p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
 		p2p->ext_listen_only = 0;
 	}
 }
@@ -4061,25 +3895,22 @@
 {
 	if (period > 65535 || interval > 65535 || period > interval ||
 	    (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Invalid Extended Listen Timing request: "
-			"period=%u interval=%u", period, interval);
+		p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
+			period, interval);
 		return -1;
 	}
 
 	eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
 
 	if (interval == 0) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-			"P2P: Disabling Extended Listen Timing");
+		p2p_dbg(p2p, "Disabling Extended Listen Timing");
 		p2p->ext_listen_period = 0;
 		p2p->ext_listen_interval = 0;
 		return 0;
 	}
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-		"P2P: Enabling Extended Listen Timing: period %u msec, "
-		"interval %u msec", period, interval);
+	p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
+		period, interval);
 	p2p->ext_listen_period = period;
 	p2p->ext_listen_interval = interval;
 	p2p->ext_listen_interval_sec = interval / 1000;
@@ -4107,8 +3938,7 @@
 	if (msg.minor_reason_code == NULL)
 		return;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
-		"P2P: Deauthentication notification BSSID " MACSTR
+	p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
 		" reason_code=%u minor_reason_code=%u",
 		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
 
@@ -4130,8 +3960,7 @@
 	if (msg.minor_reason_code == NULL)
 		return;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
-		"P2P: Disassociation notification BSSID " MACSTR
+	p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
 		" reason_code=%u minor_reason_code=%u",
 		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
 
@@ -4142,12 +3971,10 @@
 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
 {
 	if (enabled) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
-			"Device operations enabled");
+		p2p_dbg(p2p, "Managed P2P Device operations enabled");
 		p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
 	} else {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
-			"Device operations disabled");
+		p2p_dbg(p2p, "Managed P2P Device operations disabled");
 		p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
 	}
 }
@@ -4158,8 +3985,8 @@
 	if (p2p_channel_to_freq(reg_class, channel) < 0)
 		return -1;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
-		"reg_class %u channel %u", reg_class, channel);
+	p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
+		reg_class, channel);
 	p2p->cfg->reg_class = reg_class;
 	p2p->cfg->channel = channel;
 
@@ -4169,7 +3996,7 @@
 
 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
 {
-	wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
+	p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
 	if (postfix == NULL) {
 		p2p->cfg->ssid_postfix_len = 0;
 		return 0;
@@ -4188,8 +4015,8 @@
 	if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
 		return -1;
 
-	wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
-		"reg_class %u channel %u", op_reg_class, op_channel);
+	p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
+		op_reg_class, op_channel);
 	p2p->cfg->op_reg_class = op_reg_class;
 	p2p->cfg->op_channel = op_channel;
 	p2p->cfg->cfg_op_channel = cfg_op_channel;
@@ -4245,18 +4072,16 @@
 {
 	os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
 	if (is_zero_ether_addr(p2p->peer_filter))
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
-			"filter");
+		p2p_dbg(p2p, "Disable peer filter");
 	else
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
-			"filter for " MACSTR, MAC2STR(p2p->peer_filter));
+		p2p_dbg(p2p, "Enable peer filter for " MACSTR,
+			MAC2STR(p2p->peer_filter));
 }
 
 
 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
-		enabled ? "enabled" : "disabled");
+	p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
 	if (p2p->cross_connect == enabled)
 		return;
 	p2p->cross_connect = enabled;
@@ -4277,7 +4102,7 @@
 
 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
+	p2p_dbg(p2p, "Intra BSS distribution %s",
 		enabled ? "enabled" : "disabled");
 	p2p->cfg->p2p_intra_bss = enabled;
 }
@@ -4285,7 +4110,7 @@
 
 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
+	p2p_dbg(p2p, "Update channel list");
 	os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
 }
 
@@ -4295,11 +4120,9 @@
 		    size_t len, unsigned int wait_time)
 {
 	if (p2p->p2p_scan_running) {
-		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
-			"frame TX until p2p_scan completes");
+		p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
 		if (p2p->after_scan_tx) {
-			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
-				"previous pending Action frame TX");
+			p2p_dbg(p2p, "Dropped previous pending Action frame TX");
 			os_free(p2p->after_scan_tx);
 		}
 		p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
@@ -4324,8 +4147,8 @@
 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
 			   int freq_overall)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
-		"  5 GHz: %d,  overall: %d", freq_24, freq_5, freq_overall);
+	p2p_dbg(p2p, "Best channel: 2.4 GHz: %d,  5 GHz: %d,  overall: %d",
+		freq_24, freq_5, freq_overall);
 	p2p->best_freq_24 = freq_24;
 	p2p->best_freq_5 = freq_5;
 	p2p->best_freq_overall = freq_overall;
@@ -4334,8 +4157,7 @@
 
 void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
 {
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own frequency preference: "
-		"%d MHz", freq);
+	p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
 	p2p->own_freq_preference = freq;
 }
 
@@ -4560,9 +4382,56 @@
 	p2p->min_disc_int = min_disc_int;
 	p2p->max_disc_int = max_disc_int;
 	p2p->max_disc_tu = max_disc_tu;
-	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set discoverable interval: "
-		"min=%d max=%d max_tu=%d", min_disc_int, max_disc_int,
-		max_disc_tu);
+	p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
+		min_disc_int, max_disc_int, max_disc_tu);
 
 	return 0;
 }
+
+
+void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
+{
+	va_list ap;
+	char buf[500];
+
+	if (!p2p->cfg->debug_print)
+		return;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	buf[sizeof(buf) - 1] = '\0';
+	va_end(ap);
+	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
+}
+
+
+void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
+{
+	va_list ap;
+	char buf[500];
+
+	if (!p2p->cfg->debug_print)
+		return;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	buf[sizeof(buf) - 1] = '\0';
+	va_end(ap);
+	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
+}
+
+
+void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
+{
+	va_list ap;
+	char buf[500];
+
+	if (!p2p->cfg->debug_print)
+		return;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	buf[sizeof(buf) - 1] = '\0';
+	va_end(ap);
+	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
+}