From 180b1dfaedc196ba8cc1751d5f0536698e13af65 Mon Sep 17 00:00:00 2001
From: Ozan Tanfener <ozan.tanfener@iopsys.eu>
Date: Tue, 2 Feb 2021 13:45:27 +0100
Subject: [PATCH] map-controller: module test PoC

---
 gitlab-ci/functional-api-test.sh  |  45 +++
 gitlab-ci/install-dependencies.sh | 169 +-----------
 gitlab-ci/setup.sh                | 116 ++++++++
 src/core/cntlr_map.c              | 224 ++++++---------
 src/core/cntlr_ubus.c             | 443 +++++++++++++++++-------------
 src/core/cntlr_ubus.h             |   2 +-
 src/utils/debug.c                 | 138 +++++++---
 src/utils/debug.h                 |   6 +-
 test/cmocka/Makefile              |   3 +-
 test/cmocka/unit_test_cntlr.c     |  37 +--
 test/files/cntlr_ap_caps.json     |  54 ++++
 11 files changed, 696 insertions(+), 541 deletions(-)
 create mode 100755 gitlab-ci/functional-api-test.sh
 create mode 100644 test/files/cntlr_ap_caps.json

diff --git a/gitlab-ci/functional-api-test.sh b/gitlab-ci/functional-api-test.sh
new file mode 100755
index 00000000..0957d5c1
--- /dev/null
+++ b/gitlab-ci/functional-api-test.sh
@@ -0,0 +1,45 @@
+#!/bin/bash
+set -e
+LOG_FILE="/tmp/cntlr.test.log"
+LOG_FILE_JSON="cntlr.test.log.json"
+TEST_FILE="/builds/iopsys/map/map-controller/test/files/cntlr_ap_caps.json"
+#Step 1 - Start daemons
+ubusd &
+sleep 2
+wifimngr &
+sleep 2
+ieee1905d &
+sleep 2
+topologyd &
+sleep 2
+/builds/iopsys/map/map-agent/src/mapagent -vvvv -d &
+sleep 2
+/builds/iopsys/map/map-controller/src/mapcontroller -vvvv -d &
+sleep 2
+
+#Step 2 - Invoke ubus api call
+mid=$(ubus call map.controller ap_caps | jq .mid)
+#Step 3 - Convert .log file to .json file
+cp $LOG_FILE $PWD/$LOG_FILE_JSON
+sudo chown 1000 $LOG_FILE_JSON 
+sudo chgrp 1000 $LOG_FILE_JSON
+sudo chmod +rw $LOG_FILE_JSON
+#Step 4 - Extract verification and actual parameters, and check in loop
+ap_caps_message_type=32770
+for (( i=1; i<=10; i++ ))
+do
+    type_verification=$(cat "$TEST_FILE" | jq "select( .message_type=="$ap_caps_message_type") | .tlvs | .["$i"] | .type")
+    data_verification=$(cat "$TEST_FILE" | jq "select( .message_type=="$ap_caps_message_type") | .tlvs | .["$i"] | .data")
+    type_actual=$(cat "$LOG_FILE_JSON" | jq "select( .message_id=="$mid") | .tlvs | .["$i"] | .type")
+    data_actual=$(cat "$LOG_FILE_JSON" | jq "select( .message_id=="$mid") | .tlvs | .["$i"] | .data")
+    if [ "$type_verification" = "$type_actual" ] && [ "$data_verification" = "$data_actual" ]; then
+        continue
+    else
+        echo "Data mismatch, test is failed"
+        break
+    fi
+done
+
+echo "Functional test is succesfully completed"
+#Step 5 - Clean up test data
+sudo rm $LOG_FILE_JSON
\ No newline at end of file
diff --git a/gitlab-ci/install-dependencies.sh b/gitlab-ci/install-dependencies.sh
index 526ab0bf..d6ad5ca2 100755
--- a/gitlab-ci/install-dependencies.sh
+++ b/gitlab-ci/install-dependencies.sh
@@ -19,6 +19,12 @@ function exec_cmd()
 exec_cmd apt update
 exec_cmd apt install -y iproute2
 exec_cmd apt install -y libavahi-core-dev
+exec_cmd apt install -y bridge-utils
+
+#install jq
+wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
+chmod +x jq-linux64
+mv jq-linux64 /usr/sbin/jq
 
 # install easy-soc-libs (libwifi-6)
 cd /opt/dev
@@ -30,7 +36,7 @@ exec_cmd make CFLAGS+="-I/usr/include/libnl3"
 cp libeasy*.so* /usr/lib
 cp easy.h event.h utils.h debug.h /usr/include/easy/
 cd ../libwifi
-exec_cmd make PLATFORM="test"
+exec_cmd make PLATFORM="TEST"
 cp wifi.h /usr/include/ &&
 cp libwifi-6*.so* /usr/lib
 
@@ -52,6 +58,7 @@ exec_cmd git clone --depth 1 -b devel https://dev.iopsys.eu/iopsys/ieee1905.git
 cd ieee1905
 exec_cmd ./gitlab-ci/install-dependencies.sh
 exec_cmd ./gitlab-ci/setup.sh
+exec_cmd make libwsc.so
 exec_cmd make "CONFIG_IEEE1905_ALME_OVER_UBUS=y"
 exec_cmd make install
 mkdir -p /usr/include/ieee1905
@@ -59,6 +66,8 @@ exec_cmd cp -a include/plugin.h /usr/include/ieee1905
 exec_cmd cp -a libieee1905/include/1905_cmdus.h /usr/include/ieee1905
 exec_cmd cp -a libieee1905/include/1905_tlvs.h /usr/include/ieee1905
 exec_cmd cp -a ieee1905d /usr/sbin/
+exec_cmd cp -a libwsc/libwsc.so /usr/lib
+exec_cmd cp -a libwsc/include/wsc.h /usr/include
 
 # install map-1905 (libmaputils)
 cd /opt/dev
@@ -103,161 +112,3 @@ rm -fr tap
 exec_cmd git clone https://github.com/andrewgregory/tap.sh.git tap
 cd tap
 exec_cmd cp tap.sh /usr/bin/
-
-
-echo "
-config wifiagent
-	option enabled '1'
-	option debug '6'
-
-config fh-iface
-	option ifname 'test5'
-	option steer 'rssi bssload'
-	list exclude '00:11:22:33:44:55'
-	list exclude_btm '00:aa:bb:cc:dd:ee'
-	list assoc_ctrl '00:10:20:30:40:50'
-	option btm_retry '3'
-	option btm_retry_secs '180'
-	option fallback_legacy '1'
-	option steer_legacy_reassoc_secs '30'
-	option steer_legacy_retry_secs '3600'
-	option assoc_ctrl_secs '30'
-
-config fh-iface
-	option ifname 'test2'
-	option steer 'rssi bssload'
-	list exclude '00:11:22:33:44:55'
-	list exclude_btm '00:aa:bb:cc:dd:ee'
-
-#config bk-iface
-#	option ifname 'apclii0'
-#	option enabled '1'
-#	option onboarded '0'
-
-config steer-param 'rssi'
-	option priority '0'
-	option rssi_threshold '-68'
-	option hysteresis '5'
-	option margin '3'
-	option diffsnr '8'
-
-config steer-param 'bssload'
-	option priority '0'
-	option bssload_threshold '80'
-
-### custom rules follows ###
-config rule-custom
-	option action steer
-	option sta 'd8:32:e3:4d:35:d2'
-	option bss '00:22:07:11:22:33'
-
-config rule-custom
-	option action restrict
-	option sta 'd8:32:e3:4d:35:d2'
-	option bss '00:22:07:11:22:33'
-" > /etc/config/agent
-
-echo "
-config wificntlr
-	option enabled '1'
-	option registrar '5 2'	    #bands on which wps registrar supported
-	option debug '6'
-
-config fh-credentials 'map-net55'
-	option band '5'
-	option encryption 'psk2'
-	option key '1234567890'
-	option ssid 'map-net5'
-	option vlan '101'
-	option multi_ap '128'		# TODO: should mode 1/2/3 be added?
-
-config fh-credentials 'map-net55'
-	option band '5'
-	option encryption 'psk2'
-	option key '1234567890'
-	option ssid 'map-net55'
-	option vlan '0'               # 0 - 2 rsvd, 3 - 4094 => vlan id
-
-config bk-credentials
-	option band '5'
-	option encryption 'psk2'
-	option key '5555555555'
-	option ssid 'map-bkhaul-5'
-
-config bk-credentials
-	option band '2'
-	option encryption 'psk2'
-	option key '2222222222'
-	option ssid 'map-bkhaul-2'
-
-config agent-policy
-	option agent_id '00:22:07:7E:9C:D6'        # 1905al macaddress of agent
-	list steer_exclude '00:11:22:33:44:55'
-	list steer_exclude_btm '00:aa:bb:cc:dd:ee'
-	option steer_policy '2'                    # 0, 1, 2 - see MultiAP spec
-	option util_threshold '200'                # channel-util as in BSS load
-	option rcpi_threshold '30'                 # 0 - 220 valid range
-	option report_scan '0'                     # 0 or 1 for independent sc include_sta_metric '0'              # sta metric in AP metric resp
-	option pvid '100'                          # primary vlan id
-	option pcp_default '5'                     # default vlan pcp
-	option disallow_bsta_p1 '0'                # 0 or 1 profile20 valid range
-	option report_scan '0'                     # 0 or 1 for independent scans
-	option report_sta_assocfails '1'           # 0 or 1 - stas assoc failure
-	option report_metric_periodic '0'          # 0, or 1 - 255 in secs
-	option report_rcpi_threshold '30'                 # 0 - 220 valid range
-	option report_scan '0'                     # 0 or 1 for independent scans
-	option report_sta_assocfails '1'           # 0 or 1 - stas assoc failure
-	option report_metric_periodic '0'          # 0, or 1 - 255 in secs
-	option report_rcpi_threshold '0'           # 0, or 1 - 220
-	option report_util_threshold '0'           # 0, or  channel-util value
-	option include_sta_stats '0'               # sta stats in AP metric resp
-	option include_sta_metric '0'              # sta metric in AP metric resp
-	option pvid '100'                          # primary vlan id
-	option pcp_default '5'                     # default vlan pcp
-	option disallow_bsta_p1 '0'                # 0 or 1 profile-1 bSTA
-	option disallow_bsta_p2 '0'                # 0 or 1 profile-2 bSTA
-
-config agent-policy
-	option agent_id '00:00:00:00:00:00'
-	list steer_exclude '00:11:22:33:44:55'
-	list steer_exclude_btm '00:aa:bb:cc:dd:ee'
-	option steer_policy '2'                    # 0, 1, 2 - see MultiAP spec
-	option util_threshold '200'                # channel-util as in BSS load
-	option rcpi_threshold '30'                 # 0 - 220 valid range
-	option report_scan '0'                     # 0 or 1 for independent sc include_sta_metric '0'              # sta metric in AP metric resp
-	option pvid '100'                          # primary vlan id
-	option pcp_default '5'                     # default vlan pcp
-	option disallow_bsta_p1 '0'                # 0 or 1 profile20 valid range
-	option report_scan '0'                     # 0 or 1 for independent scans
-	option report_sta_assocfails '1'           # 0 or 1 - stas assoc failure
-	option report_metric_periodic '0'          # 0, or 1 - 255 in secs
-	option report_rcpi_threshold '30'                 # 0 - 220 valid range
-	option report_scan '0'                     # 0 or 1 for independent scans
-	option report_sta_assocfails '1'           # 0 or 1 - stas assoc failure
-	option report_metric_periodic '0'          # 0, or 1 - 255 in secs
-	option report_rcpi_threshold '0'           # 0, or 1 - 220
-	option report_util_threshold '0'           # 0, or  channel-util value
-	option include_sta_stats '0'               # sta stats in AP metric resp
-	option include_sta_metric '0'              # sta metric in AP metric resp
-	option pvid '100'                          # primary vlan id
-	option pcp_default '5'                     # default vlan pcp
-	option disallow_bsta_p1 '0'                # 0 or 1 profile-1 bSTA
-	option disallow_bsta_p2 '0'                # 0 or 1 profile-2 bSTA
-
-config agent-policy
-	option agent_id '00:22:07:6A:1C:3A'    # 1905al macaddress of agent
-	list steer_exclude '00:11:22:33:44:55'
-	list steer_exclude_btm '00:aa:bb:cc:dd:ee'
-	option steer_policy '2'                    # 0, 1, 2 - see MultiAP spec
-	option util_threshold '200'                # channel-util as in BSS load
-	option report_metric_periodic '0'          # 0, or 1 - 255 in secs
-	option report_rcpi_threshold '0'           # 0, or 1 - 220
-	option report_util_threshold '0'           # 0, or  channel-util value
-	option include_sta_stats '0'               # sta stats in 3:4d:35:d2'
-	option bss '00:22:07:11:22:33'
-
-config rule-custom
-	option action restrict
-	option sta 'd8:32:e3:4d:35:d2'
-	option bss '00:22:07:11:22:33'
-" > /etc/config/controller
diff --git a/gitlab-ci/setup.sh b/gitlab-ci/setup.sh
index 8f565815..9aa774e9 100755
--- a/gitlab-ci/setup.sh
+++ b/gitlab-ci/setup.sh
@@ -7,6 +7,28 @@ pwd
 uci set ieee1905.ieee1905.map_plugin="1"
 uci commit ieee1905
 
+
+#Create dummy interfaces
+
+brctl addbr br-lan
+ifconfig br-lan 192.168.1.1
+ifconfig br-lan up
+
+ip link add name test2 type dummy
+ip link add name test5 type dummy
+
+brctl addif br-lan test2
+brctl addif br-lan test5
+
+ifconfig test2 up
+ifconfig test5 up
+
+
+# add the dummy clients to arp table
+arp -i br-lan -s 192.168.1.100   00:e0:4c:68:18:54
+arp -i test2 -s  192.168.1.101   20:80:70:60:50:40
+arp -i test5 -s  192.168.1.102   50:80:70:60:50:40
+
 cp -r ./test/files/etc/* /etc/
 cp -r ./schemas/ubus/* /usr/share/rpcd/schemas
 cp ./gitlab-ci/iopsys-supervisord.conf /etc/supervisor/conf.d/
@@ -14,3 +36,97 @@ cp ./gitlab-ci/iopsys-supervisord.conf /etc/supervisor/conf.d/
 ls /etc/config/
 ls /usr/share/rpcd/schemas/
 ls /etc/supervisor/conf.d/
+
+echo "
+config wifi-device 'test2'
+    option channel 'auto'
+    option hwmode 'auto'
+    option country 'DE'
+    option band 'a'
+    option bandwidth '80'
+
+config wifi-iface
+    option device 'test2'
+    option ifname 'test2'
+    option mode 'ap'
+    option encryption 'psk2'
+
+config wifi-device 'test5'
+    option channel 'auto'
+    option hwmode 'auto'
+    option country 'DE'
+    option band 'a'
+    option bandwidth '80'
+
+config wifi-iface
+    option device 'test5'
+    option ifname 'test5'
+    option mode 'ap'
+    option encryption 'psk2'
+" > /etc/config/wireless
+
+echo "
+config interface 'loopback'
+	option ifname 'lo'
+	option proto 'static'
+	option ipaddr '127.0.0.1'
+	option netmask '255.0.0.0'
+
+config interface 'lan'
+	option type 'bridge'
+	option proto 'static'
+	option ipaddr '192.168.1.1'
+	option ifname 'eth0 eth1'
+	option is_lan '1'
+
+config interface 'lan6'
+	option proto 'dhcpv6'
+	option ifname '@lan'
+
+config device 'ethwan'
+	option type 'untagged'
+	option ifname 'eth1'
+	option name 'eth1.1'
+" > /etc/config/network
+
+echo "
+config agent 'agent'
+	option enabled '1'
+	option debug '6'
+	option al_bridge 'br-lan'
+	option netdev 'test'
+
+config fh-iface
+	option ifname 'test5'
+	option device 'test5'
+	option band '5'
+
+config fh-iface
+	option ifname 'test2'
+	option device 'test2'
+	option band '2'
+
+" > /etc/config/mapagent
+
+echo "
+config controller 'controller'
+	option enabled '1'
+	option registrar '5 2'	    #bands on which wps registrar supported
+	option debug '6'
+	option al_bridge 'br-lan'
+
+config fh-credentials
+	option band '2'
+	option encryption 'psk2'
+	option key '1234567890'
+	option ssid 'map-net2'
+	option multi_ap '2'		# TODO: should mode 1/2/3 be added?
+
+config fh-credentials
+	option band '5'
+	option encryption 'psk2'
+	option key '1234567890'
+	option ssid 'map-net5'
+#	option vlan '0'               # 0 - 2 rsvd, 3 - 4094 => vlan id
+
+" > /etc/config/mapcontroller
diff --git a/src/core/cntlr_map.c b/src/core/cntlr_map.c
index 28ac2673..cb0abbaf 100644
--- a/src/core/cntlr_map.c
+++ b/src/core/cntlr_map.c
@@ -49,21 +49,23 @@
 #include "cntlr_map_debug.h"
 #include "cntlr_tlv_generator.h"
 
-#define for_each_tlv(e, _buf, _len)					  \
-for ((e) = (struct tlv *)(_buf);					  \
-	(e)->len && (_buf) + (_len) - (uint8_t *)(e) - 3 - (e)->len >= 0; \
-		(e) = (struct tlv *)(_buf + 3 + (e)->len))
+#define for_each_tlv(e, _buf, _len)                                      \
+	for ((e) = (struct tlv *)(_buf);                                     \
+		 (e)->len && (_buf) + (_len) - (uint8_t *)(e)-3 - (e)->len >= 0; \
+		 (e) = (struct tlv *)(_buf + 3 + (e)->len))
 
-struct tlv {
+struct tlv
+{
 	uint8_t type;
 	uint16_t len;
 	uint8_t value[];
-} __attribute__ ((packed));
+} __attribute__((packed));
 
 int verbose;
 typedef int (*map_cmdu_handler_t)(void *cntlr, struct cmdu_cstruct *cmdu);
 
-struct map_cmdu_calltable_t {
+struct map_cmdu_calltable_t
+{
 	map_cmdu_handler_t handle;
 	map_cmdu_handler_t debug;
 };
@@ -83,7 +85,7 @@ int handle_topology_response(void *cntlr, struct cmdu_cstruct *cmdu)
 int handle_ap_autoconfig_search(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 {
 	trace("%s: --->\n", __func__);
-	struct controller *c = (struct controller *) cntlr;
+	struct controller *c = (struct controller *)cntlr;
 	uint16_t tlv_index = 0;
 	struct cmdu_cstruct *cmdu;
 	struct tlv_map_profile *p;
@@ -102,13 +104,17 @@ int handle_ap_autoconfig_search(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 	if (!freq_tlv)
 		return -1;
 
-	if (freq_tlv->freq_band == 0x00) {
+	if (freq_tlv->freq_band == 0x00)
+	{
 		if (!c->cfg.has_registrar_2g)
 			return -1;
-	} else if (freq_tlv->freq_band == 0x01) {
+	}
+	else if (freq_tlv->freq_band == 0x01)
+	{
 		if (!c->cfg.has_registrar_5g)
 			return -1;
-	} else
+	}
+	else
 		return -1;
 
 	if (!hwaddr_ntoa(almac_tlv->al_mac_address, mac_str))
@@ -119,8 +125,9 @@ int handle_ap_autoconfig_search(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 		cntlr_config_reload(&c->cfg);
 
 	cmdu = (struct cmdu_cstruct *)calloc(1,
-			sizeof(struct cmdu_cstruct));
-	if (!cmdu) {
+										 sizeof(struct cmdu_cstruct));
+	if (!cmdu)
+	{
 		fprintf(stderr, "Out of memory!\n");
 		return -1;
 	}
@@ -146,7 +153,7 @@ int handle_ap_autoconfig_search(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 		goto fail_p1;
 	cmdu->num_tlvs++;
 
-	cmdu->tlvs = (uint8_t **)calloc(cmdu->num_tlvs,	sizeof(uint8_t *));
+	cmdu->tlvs = (uint8_t **)calloc(cmdu->num_tlvs, sizeof(uint8_t *));
 	if (!cmdu->tlvs)
 		goto fail_p2;
 
@@ -159,11 +166,11 @@ int handle_ap_autoconfig_search(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 
 	return ret;
 fail_p2:
-	map_free_tlv_cstruct((uint8_t *) p2);
+	map_free_tlv_cstruct((uint8_t *)p2);
 fail_p1:
-	map_free_tlv_cstruct((uint8_t *) p1);
+	map_free_tlv_cstruct((uint8_t *)p1);
 fail_p:
-	map_free_tlv_cstruct((uint8_t *) p);
+	map_free_tlv_cstruct((uint8_t *)p);
 fail_cmdu:
 	free(cmdu);
 	return -1;
@@ -217,7 +224,7 @@ int handle_ap_autoconfig_response(void *cntlr, struct cmdu_cstruct *cmdu)
 int handle_ap_autoconfig_wsc(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 {
 	trace("%s: --->\n", __func__);
-	struct controller *c = (struct controller *) cntlr;
+	struct controller *c = (struct controller *)cntlr;
 	struct agent_policy *a, *found = NULL;
 	struct tlv_default_8021q_settings *p;
 	struct tlv_traffic_sep_policy *p1;
@@ -230,7 +237,8 @@ int handle_ap_autoconfig_wsc(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 	uint8_t **tlvs;
 
 	cmdu = (struct cmdu_cstruct *)calloc(1, sizeof(struct cmdu_cstruct));
-	if (!cmdu) {
+	if (!cmdu)
+	{
 		fprintf(stderr, "failed to malloc cmdu\n");
 		return -1;
 	}
@@ -241,8 +249,10 @@ int handle_ap_autoconfig_wsc(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 	strncpy(cmdu->intf_name, rec_cmdu->intf_name,
 			sizeof(cmdu->intf_name) - 1);
 
-	list_for_each_entry(a, &c->cfg.policylist, list) {
-		if (!memcmp(rec_cmdu->origin, a->agent_id, 6)) {
+	list_for_each_entry(a, &c->cfg.policylist, list)
+	{
+		if (!memcmp(rec_cmdu->origin, a->agent_id, 6))
+		{
 			found = a;
 			break;
 		}
@@ -262,7 +272,7 @@ int handle_ap_autoconfig_wsc(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 	cmdu->num_tlvs++;
 
 	rec_basic_cap = extract_tlv_by_type(rec_cmdu,
-			MAP_TLV_AP_RADIO_BASIC_CAPABILITIES);
+										MAP_TLV_AP_RADIO_BASIC_CAPABILITIES);
 	if (!rec_basic_cap)
 		goto fail_p1;
 
@@ -288,9 +298,10 @@ int handle_ap_autoconfig_wsc(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 		cmdu->num_tlvs++;
 
 		tlvs = (uint8_t **)realloc(cmdu->tlvs, cmdu->num_tlvs * sizeof(uint8_t *));
-		if (!tlvs) {
+		if (!tlvs)
+		{
 			cmdu->num_tlvs--;
-			map_free_tlv_cstruct((uint8_t *) p3);
+			map_free_tlv_cstruct((uint8_t *)p3);
 			continue;
 		}
 
@@ -307,9 +318,10 @@ int handle_ap_autoconfig_wsc(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 		cmdu->num_tlvs++;
 
 		tlvs = (uint8_t **)realloc(cmdu->tlvs, cmdu->num_tlvs * sizeof(uint8_t *));
-		if (!tlvs) {
+		if (!tlvs)
+		{
 			cmdu->num_tlvs--;
-			map_free_tlv_cstruct((uint8_t *) p3);
+			map_free_tlv_cstruct((uint8_t *)p3);
 			continue;
 		}
 
@@ -326,17 +338,16 @@ int handle_ap_autoconfig_wsc(void *cntlr, struct cmdu_cstruct *rec_cmdu)
 fail_tlvs:
 	free(cmdu->tlvs);
 fail_p2:
-	map_free_tlv_cstruct((uint8_t *) p2);
+	map_free_tlv_cstruct((uint8_t *)p2);
 fail_p1:
-	map_free_tlv_cstruct((uint8_t *) p1);
+	map_free_tlv_cstruct((uint8_t *)p1);
 fail_p:
-	map_free_tlv_cstruct((uint8_t *) p);
+	map_free_tlv_cstruct((uint8_t *)p);
 fail_cmdu:
 	free(cmdu);
 	return -1;
 }
 
-
 int handle_1905_ack(void *cntlr, struct cmdu_cstruct *cmdu)
 {
 	trace("%s: --->\n", __func__);
@@ -347,11 +358,10 @@ int handle_ap_caps_report(void *cntlr, struct cmdu_cstruct *cmdu)
 {
 	trace("%s: --->\n", __func__);
 	trace("parsing AP capabilities of |%s:" MACFMT "|\n",
-			cmdu->intf_name, MAC2STR(cmdu->origin));
+		  cmdu->intf_name, MAC2STR(cmdu->origin));
 	return 0;
 }
 
-
 int handle_channel_pref_report(void *cntlr, struct cmdu_cstruct *cmdu)
 {
 	trace("%s: --->\n", __func__);
@@ -374,7 +384,7 @@ int handle_sta_caps_report(void *cntlr, struct cmdu_cstruct *cmdu)
 {
 	trace("%s: --->\n", __func__);
 	trace("parsing AP capabilities of |%s:" MACFMT "|\n",
-			cmdu->intf_name, MAC2STR(cmdu->origin));
+		  cmdu->intf_name, MAC2STR(cmdu->origin));
 	return 0;
 }
 
@@ -391,7 +401,7 @@ int handle_sta_link_metrics_response(void *cntlr, struct cmdu_cstruct *cmdu)
 }
 
 int handle_unassoc_sta_link_metrics_response(void *cntlr,
-		struct cmdu_cstruct *cmdu)
+											 struct cmdu_cstruct *cmdu)
 {
 	trace("%s: --->\n", __func__);
 	return 0;
@@ -453,125 +463,53 @@ int handle_backhaul_sta_caps_report(void *cntlr, struct cmdu_cstruct *cmdu)
 	return 0;
 }
 
-
 int handle_failed_connection_msg(void *cntlr, struct cmdu_cstruct *cmdu)
 {
 	trace("%s: --->\n", __func__);
 	return 0;
 }
 
+#define CMDU_TYPE_1905_START 0x0001
+#define CMDU_TYPE_1905_END 0x0009
 
-#define CMDU_TYPE_1905_START	0x0001
-#define CMDU_TYPE_1905_END	0x0009
-
-
-#define CMDU_TYPE_MAP_START	0x8000
-#define CMDU_TYPE_MAP_END	0x8033
+#define CMDU_TYPE_MAP_START 0x8000
+#define CMDU_TYPE_MAP_END 0x8033
 
 /* mind the holes in the following two tables */
 static const struct map_cmdu_calltable_t i1905ftable[] = {
 	[0x01] = {
 		.handle = handle_topology_notification,
-		.debug = debug_topology_notification
-	},
-	[0x03] = {
-		.handle = handle_topology_response,
-		.debug = debug_topology_response
-	},
-	[0x07] = {
-		.handle = handle_ap_autoconfig_search,
-		.debug = debug_ap_autoconfig_search
-	},
-	[0x08] = {
-		.handle = handle_ap_autoconfig_response,
-		.debug = debug_ap_autoconfig_response
-	},
-	[0x09] = {
-		.handle = handle_ap_autoconfig_wsc,
-		.debug = debug_ap_autoconfig_wsc
-	},
+		.debug = debug_topology_notification},
+	[0x03] = {.handle = handle_topology_response, .debug = debug_topology_response},
+	[0x07] = {.handle = handle_ap_autoconfig_search, .debug = debug_ap_autoconfig_search},
+	[0x08] = {.handle = handle_ap_autoconfig_response, .debug = debug_ap_autoconfig_response},
+	[0x09] = {.handle = handle_ap_autoconfig_wsc, .debug = debug_ap_autoconfig_wsc},
 };
 
 static const struct map_cmdu_calltable_t cntlr_mapftable[] = {
 	[0x00] = {
 		.handle = handle_1905_ack,
-		.debug = debug_1905_ack
-	},
-	[0x02] = {
-		.handle = handle_ap_caps_report,
-		.debug = debug_ap_caps_report
-	},
-	[0x05] = {
-		.handle = handle_channel_pref_report,
-		.debug = debug_channel_pref_report
-	},
-	[0x07] = {
-		.handle = handle_channel_sel_response,
-		.debug = debug_channel_sel_response
-	},
-	[0x08] = {
-		.handle = handle_oper_channel_report,
-		.debug = debug_oper_channel_report
-	},
-	[0x0a] = {
-		.handle = handle_sta_caps_report,
-		.debug = debug_sta_caps_report
-	},
-	[0x0c] = {
-		.handle = handle_ap_metrics_response,
-		.debug = debug_ap_metrics_response
-	},
-	[0x0e] = {
-		.handle = handle_sta_link_metrics_response,
-		.debug = debug_sta_link_metrics_response
-	},
-	[0x10] = {
-		.handle = handle_unassoc_sta_link_metrics_response,
-		.debug = debug_unassoc_sta_link_metrics_response
-	},
-	[0x12] = {
-		.handle = handle_beacon_metrics_response,
-		.debug = debug_beacon_metrics_response
-	},
-	[0x15] = {
-		.handle = handle_sta_steer_btm_report,
-		.debug = debug_sta_steer_btm_report
-	},
-	[0x17] = {
-		.handle = handle_sta_steer_complete,
-		.debug = debug_sta_steer_complete
-	},
-	[0x1a] = {
-		.handle = handle_backhaul_sta_steer_response,
-		.debug = debug_backhaul_sta_steer_response
-	},
-	[0x1c] = {
-		.handle = handle_channel_scan_report,
-		.debug = debug_channel_scan_report
-	},
-	[0x22] = {
-		.handle = handle_sta_disassoc_stats,
-		.debug = debug_sta_disassoc_stats
-	},
-	[0x25] = {
-		.handle = handle_assoc_status_notification,
-		.debug = debug_assoc_status_notification
-	},
-	[0x26] = {
-		.handle = handle_tunneled_message,
-		.debug = debug_tunneled_message
-	},
-	[0x28] = {
-		.handle = handle_backhaul_sta_caps_report,
-		.debug = debug_backhaul_sta_caps_report
-	},
-	[0x33] = {
-		.handle = handle_failed_connection_msg,
-		.debug = debug_failed_connection_msg
-	},
+		.debug = debug_1905_ack},
+	[0x02] = {.handle = handle_ap_caps_report, .debug = debug_ap_caps_report},
+	[0x05] = {.handle = handle_channel_pref_report, .debug = debug_channel_pref_report},
+	[0x07] = {.handle = handle_channel_sel_response, .debug = debug_channel_sel_response},
+	[0x08] = {.handle = handle_oper_channel_report, .debug = debug_oper_channel_report},
+	[0x0a] = {.handle = handle_sta_caps_report, .debug = debug_sta_caps_report},
+	[0x0c] = {.handle = handle_ap_metrics_response, .debug = debug_ap_metrics_response},
+	[0x0e] = {.handle = handle_sta_link_metrics_response, .debug = debug_sta_link_metrics_response},
+	[0x10] = {.handle = handle_unassoc_sta_link_metrics_response, .debug = debug_unassoc_sta_link_metrics_response},
+	[0x12] = {.handle = handle_beacon_metrics_response, .debug = debug_beacon_metrics_response},
+	[0x15] = {.handle = handle_sta_steer_btm_report, .debug = debug_sta_steer_btm_report},
+	[0x17] = {.handle = handle_sta_steer_complete, .debug = debug_sta_steer_complete},
+	[0x1a] = {.handle = handle_backhaul_sta_steer_response, .debug = debug_backhaul_sta_steer_response},
+	[0x1c] = {.handle = handle_channel_scan_report, .debug = debug_channel_scan_report},
+	[0x22] = {.handle = handle_sta_disassoc_stats, .debug = debug_sta_disassoc_stats},
+	[0x25] = {.handle = handle_assoc_status_notification, .debug = debug_assoc_status_notification},
+	[0x26] = {.handle = handle_tunneled_message, .debug = debug_tunneled_message},
+	[0x28] = {.handle = handle_backhaul_sta_caps_report, .debug = debug_backhaul_sta_caps_report},
+	[0x33] = {.handle = handle_failed_connection_msg, .debug = debug_failed_connection_msg},
 };
 
-
 bool is_cmdu_for_us(void *module, uint16_t type)
 {
 	struct controller *c = (struct controller *)module;
@@ -590,7 +528,9 @@ bool is_cmdu_for_us(void *module, uint16_t type)
 	if (type >= CMDU_TYPE_1905_START && type <= CMDU_TYPE_1905_END) {
 		if (i1905ftable[type].handle)
 			return true;
-	} else if (type >= CMDU_TYPE_MAP_START && type <= CMDU_TYPE_MAP_END) {
+	}
+	else if (type >= CMDU_TYPE_MAP_START && type <= CMDU_TYPE_MAP_END)
+	{
 		if (cntlr_mapftable[type - CMDU_TYPE_MAP_START].handle)
 			return true;
 	}
@@ -599,7 +539,7 @@ bool is_cmdu_for_us(void *module, uint16_t type)
 }
 
 int cntlr_handle_map_event(void *module, uint16_t cmdutype, uint16_t mid,
-		char *rxif, uint8_t *src, uint8_t *tlvs, int len)
+						   char *rxif, uint8_t *src, uint8_t *tlvs, int len)
 {
 	struct controller *c = (struct controller *)module;
 	const struct map_cmdu_calltable_t *f;
@@ -609,21 +549,25 @@ int cntlr_handle_map_event(void *module, uint16_t cmdutype, uint16_t mid,
 
 	trace("%s: ---> cmdu = %d\n", __func__, cmdutype);
 
-	if (cmdutype >= CMDU_TYPE_MAP_START) {
+	if (cmdutype >= CMDU_TYPE_MAP_START)
+	{
 		idx = cmdutype - CMDU_TYPE_MAP_START;
 		f = cntlr_mapftable;
-	} else {
+	}
+	else
+	{
 		idx = cmdutype;
 		f = i1905ftable;
 	}
 
 	cmdu = map_build_cmdu(cmdutype, mid, rxif, src, tlvs);
-	if (!cmdu) {
+	if (!cmdu)
+	{
 		dbg("%s: map_build_cmdu() failed!\n", __func__);
 		return -1;
 	}
 
-	fprintf(stderr, " %s -", __func__); test_cmdu(cmdu);
+	log_cmdu(cmdu, true); /*TODO change function and add input */
 
 	if (f[idx].handle)
 		ret = f[idx].handle(c, cmdu);
diff --git a/src/core/cntlr_ubus.c b/src/core/cntlr_ubus.c
index 862e0f92..8da997c3 100644
--- a/src/core/cntlr_ubus.c
+++ b/src/core/cntlr_ubus.c
@@ -37,8 +37,8 @@
 #include "cntlr_tlv_generator.h"
 #include "cntlr_cmdu_generator.h"
 
-
-enum {
+enum
+{
 	AP_POLICY_AGENT,
 	/* TODO: filter on cntlr side based on bssid */
 	//AP_POLICY_BSSID,
@@ -46,13 +46,13 @@ enum {
 };
 
 static const struct blobmsg_policy ap_caps_policy_params[__AP_POLICY_MAX] = {
-	[AP_POLICY_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING },
+	[AP_POLICY_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING},
 	/* TODO: filter on cntlr side based on bssid */
 	//[AP_POLICY_BSSID] = { .name = "bssid", .type = BLOBMSG_TYPE_STRING }
 };
 
-
-enum {
+enum
+{
 	STA_POLICY_AGENT,
 	STA_POLICY_STA,
 	STA_POLICY_BSSID,
@@ -60,21 +60,21 @@ enum {
 };
 
 static const struct blobmsg_policy sta_caps_policy_params[__STA_POLICY_MAX] = {
-	[STA_POLICY_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING },
-	[STA_POLICY_STA] = { .name = "sta", .type = BLOBMSG_TYPE_STRING },
-	[STA_POLICY_BSSID] = { .name = "bssid", .type = BLOBMSG_TYPE_STRING }
-};
+	[STA_POLICY_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING},
+	[STA_POLICY_STA] = {.name = "sta", .type = BLOBMSG_TYPE_STRING},
+	[STA_POLICY_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING}};
 
-enum {
+enum
+{
 	CHANNEL_PREF_POLICY_AGENT,
 	__CHANNEL_PREF_POLICY_MAX,
 };
 
 static const struct blobmsg_policy channel_pref_policy_params[__CHANNEL_PREF_POLICY_MAX] = {
-	[CHANNEL_PREF_POLICY_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING }
-};
+	[CHANNEL_PREF_POLICY_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING}};
 
-enum {
+enum
+{
 	CHANNEL_SEL_POLICY_AGENT,
 	CHANNEL_SEL_POLICY_RADIO_ID,
 	CHANNEL_SEL_POLICY_CLASS_ID,
@@ -85,15 +85,15 @@ enum {
 };
 
 static const struct blobmsg_policy channel_select_policy_params[__CHANNEL_SEL_POLICY_MAX] = {
-	[CHANNEL_SEL_POLICY_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING },
-	[CHANNEL_SEL_POLICY_RADIO_ID] = { .name = "radio_id", .type = BLOBMSG_TYPE_STRING },
-	[CHANNEL_SEL_POLICY_CLASS_ID] = { .name = "class_id", .type = BLOBMSG_TYPE_INT32 },
-	[CHANNEL_SEL_POLICY_CHANNEL] = { .name = "channel", .type = BLOBMSG_TYPE_ARRAY },
-	[CHANNEL_SEL_POLICY_PREF] = { .name = "preference", .type = BLOBMSG_TYPE_INT32 },
-	[CHANNEL_SEL_POLICY_TRANSMIT_POWER] = { .name = "transmit_power", .type = BLOBMSG_TYPE_INT32 }
-};
-
-enum {
+	[CHANNEL_SEL_POLICY_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING},
+	[CHANNEL_SEL_POLICY_RADIO_ID] = {.name = "radio_id", .type = BLOBMSG_TYPE_STRING},
+	[CHANNEL_SEL_POLICY_CLASS_ID] = {.name = "class_id", .type = BLOBMSG_TYPE_INT32},
+	[CHANNEL_SEL_POLICY_CHANNEL] = {.name = "channel", .type = BLOBMSG_TYPE_ARRAY},
+	[CHANNEL_SEL_POLICY_PREF] = {.name = "preference", .type = BLOBMSG_TYPE_INT32},
+	[CHANNEL_SEL_POLICY_TRANSMIT_POWER] = {.name = "transmit_power", .type = BLOBMSG_TYPE_INT32}};
+
+enum
+{
 	CFG_POLICY_AGENT,
 	CFG_POLICY_RADIO,
 	CFG_POLICY_BSSID,
@@ -101,12 +101,13 @@ enum {
 };
 
 static const struct blobmsg_policy config_policy_params[__CFG_POLICY_MAX] = {
-	[CFG_POLICY_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING },
-	[CFG_POLICY_RADIO] = { .name = "radio", .type = BLOBMSG_TYPE_STRING },
-	[CFG_POLICY_BSSID] = { .name = "bssid", .type = BLOBMSG_TYPE_STRING },
+	[CFG_POLICY_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING},
+	[CFG_POLICY_RADIO] = {.name = "radio", .type = BLOBMSG_TYPE_STRING},
+	[CFG_POLICY_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
 };
 
-enum {
+enum
+{
 	RECFG_POLICY_AGENT,
 	RECFG_POLICY_ALMAC,
 	RECFG_POLICY_EGRESS,
@@ -114,12 +115,12 @@ enum {
 };
 
 static const struct blobmsg_policy reconfig_policy_params[__RECFG_POLICY_MAX] = {
-	[RECFG_POLICY_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING },
-	[RECFG_POLICY_ALMAC] = { .name = "almac", .type = BLOBMSG_TYPE_STRING },
-	[RECFG_POLICY_EGRESS] = { .name = "egress", .type = BLOBMSG_TYPE_STRING }
-};
+	[RECFG_POLICY_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING},
+	[RECFG_POLICY_ALMAC] = {.name = "almac", .type = BLOBMSG_TYPE_STRING},
+	[RECFG_POLICY_EGRESS] = {.name = "egress", .type = BLOBMSG_TYPE_STRING}};
 
-enum {
+enum
+{
 	BK_STEER_POLICY_AGENT,
 	BK_STEER_POLICY_BSSID,
 	BK_STEER_POLICY_CHANNEL,
@@ -129,14 +130,15 @@ enum {
 };
 
 static const struct blobmsg_policy bk_steer_policy_params[__BK_STEER_POLICY_MAX] = {
-	[BK_STEER_POLICY_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING },
-	[BK_STEER_POLICY_BSSID] = { .name = "bssid", .type = BLOBMSG_TYPE_STRING },
-	[BK_STEER_POLICY_CHANNEL] = { .name = "channel", .type = BLOBMSG_TYPE_INT32 },
-	[BK_STEER_POLICY_OP_CLASS] = { .name = "op_class", .type = BLOBMSG_TYPE_INT32 },
-	[BK_STEER_POLICY_STA_MAC] = { .name = "bksta", .type = BLOBMSG_TYPE_STRING },
+	[BK_STEER_POLICY_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING},
+	[BK_STEER_POLICY_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
+	[BK_STEER_POLICY_CHANNEL] = {.name = "channel", .type = BLOBMSG_TYPE_INT32},
+	[BK_STEER_POLICY_OP_CLASS] = {.name = "op_class", .type = BLOBMSG_TYPE_INT32},
+	[BK_STEER_POLICY_STA_MAC] = {.name = "bksta", .type = BLOBMSG_TYPE_STRING},
 };
 
-enum {
+enum
+{
 	AP_POLICY_CONFIG_AGENT,
 	AP_POLICY_CONFIG_RADIOS,
 	AP_POLICY_CONFIG_BSS,
@@ -144,9 +146,9 @@ enum {
 };
 
 static const struct blobmsg_policy ap_policy_config_params[__AP_POLICY_CONFIG_MAX] = {
-	[AP_POLICY_CONFIG_AGENT] = { .name = "agent", .type = BLOBMSG_TYPE_STRING },
-	[AP_POLICY_CONFIG_RADIOS] = { .name = "radiolist", .type = BLOBMSG_TYPE_ARRAY },
-	[AP_POLICY_CONFIG_BSS] = { .name = "bsslist", .type = BLOBMSG_TYPE_ARRAY },
+	[AP_POLICY_CONFIG_AGENT] = {.name = "agent", .type = BLOBMSG_TYPE_STRING},
+	[AP_POLICY_CONFIG_RADIOS] = {.name = "radiolist", .type = BLOBMSG_TYPE_ARRAY},
+	[AP_POLICY_CONFIG_BSS] = {.name = "bsslist", .type = BLOBMSG_TYPE_ARRAY},
 };
 
 enum {
@@ -246,14 +248,15 @@ static const struct blobmsg_policy bk_caps_policy_params[__BK_CAPS_POLICY_MAX] =
 };
 
 void send_cmdu_cb(struct ubus_request *req,
-				int type, struct blob_attr *msg)
+				  int type, struct blob_attr *msg)
 {
 	struct json_object *jobj = NULL;
 	struct json_object *tmp;
 	uint16_t *mid;
 	char *str;
 
-	if (!msg || !req->priv) {
+	if (!msg || !req->priv)
+	{
 		fprintf(stderr, "%s:Message recieved is NULL\n", __func__);
 		return;
 	}
@@ -261,7 +264,8 @@ void send_cmdu_cb(struct ubus_request *req,
 	mid = (uint16_t *)req->priv;
 
 	str = (char *)blobmsg_format_json_indent(msg, true, -1);
-	if (str) {
+	if (str)
+	{
 		jobj = json_tokener_parse(str);
 		free(str);
 	}
@@ -269,7 +273,8 @@ void send_cmdu_cb(struct ubus_request *req,
 	if (jobj == NULL)
 		return;
 
-	if (json_object_object_get_ex(jobj, "mid", &tmp)) {
+	if (json_object_object_get_ex(jobj, "mid", &tmp))
+	{
 		*mid = json_object_get_int(tmp);
 		fprintf(stdout, "%s:%d cntlr map-mid:%d\n", __func__, __LINE__, *mid); // typo ||
 	}
@@ -277,15 +282,15 @@ void send_cmdu_cb(struct ubus_request *req,
 	json_object_put(jobj);
 }
 
-int send_cmdu(struct controller *c,
-		struct cmdu_cstruct *cmdu_data)
+uint16_t send_cmdu(struct controller *c,
+			  struct cmdu_cstruct *cmdu_data)
 {
 	char *tlv_data = NULL;
 	uint16_t tlv_data_len = 1;
 	uint16_t tlv_str_len = 0;
 	int copy_index;
-	struct blob_buf b = { 0 };
-	char dst_addr[18] = { 0 };
+	struct blob_buf b = {0};
+	char dst_addr[18] = {0};
 	char *tlv_str = NULL;
 	uint8_t *ss = NULL;
 	uint16_t msgid = 0;
@@ -312,15 +317,19 @@ int send_cmdu(struct controller *c,
 			map_stringify_cmdu_type(cmdu_data->message_type),
 			dst_addr, cmdu_data->num_tlvs, cmdu_data->intf_name);
 
-	if (cmdu_data->num_tlvs > 0) {
-		for (i = 0; i < cmdu_data->num_tlvs; i++) {
+	if (cmdu_data->num_tlvs > 0)
+	{
+		for (i = 0; i < cmdu_data->num_tlvs; i++)
+		{
 			trace("CMDU type: %s\n", map_stringify_tlv_type(*cmdu_data->tlvs[i]));
 
 			len = 0;
 			ss = map_put_tlv_cstruct(cmdu_data->tlvs[i], &len);
-			if (ss) {
+			if (ss)
+			{
 				tlv_str = (char *)calloc((2 * len) + 1, sizeof(char));
-				if (!tlv_str) {
+				if (!tlv_str)
+				{
 					free(ss);
 					goto out;
 				}
@@ -328,7 +337,8 @@ int send_cmdu(struct controller *c,
 				tlv_str_len = 2 * len;
 				tlv_data_len += tlv_str_len;
 				tlv_data = realloc(tlv_data, tlv_data_len * sizeof(char));
-				if (!tlv_data) {
+				if (!tlv_data)
+				{
 					fprintf(stderr, "%s:%d out of memory.!\n",
 							__func__, __LINE__);
 					free(ss);
@@ -345,35 +355,37 @@ int send_cmdu(struct controller *c,
 		blobmsg_add_string(&b, "data", tlv_data);
 		if (tlv_data)
 			free(tlv_data);
-
 	}
 
-	if (ubus_lookup_id(c->ubus_ctx, "map.1905", &id)) {
+	if (ubus_lookup_id(c->ubus_ctx, "map.1905", &id))
+	{
 		fprintf(stderr, "[%s:%d] not present", __func__, __LINE__);
 		goto out;
 	}
 
 	ret = ubus_invoke(c->ubus_ctx, id, "send",
-				b.head, send_cmdu_cb,
-				(void *)&msgid,
-				1000);
-	if (ret) {
+					  b.head, send_cmdu_cb,
+					  (void *)&msgid,
+					  1000);
+	if (ret)
+	{
 		fprintf(stderr, "[%s:%d] ubus call failed for |map.1905 send|",
-					__func__, __LINE__);
+				__func__, __LINE__);
 		goto out;
 	}
 
 	ret = msgid;
 
-	test_cmdu(cmdu_data);
+	log_cmdu(cmdu_data, false); /*TODO: add a flag and change function name */
+	ret = msgid;
 out:
 	blob_buf_free(&b);
 	return ret;
 }
 
 static int cntlr_status(struct ubus_context *ctx, struct ubus_object *obj,
-			struct ubus_request_data *req, const char *method,
-			struct blob_attr *msg)
+						struct ubus_request_data *req, const char *method,
+						struct blob_attr *msg)
 {
 	struct controller *c = container_of(obj, struct controller, obj);
 	struct hlist_head *stalist = c->as_table;
@@ -388,7 +400,8 @@ static int cntlr_status(struct ubus_context *ctx, struct ubus_object *obj,
 
 	blobmsg_add_u32(&bb, "num_nodes", c->num_nodes);
 	a = blobmsg_open_array(&bb, "node");
-	list_for_each_entry(n, &c->nodelist, list) {
+	list_for_each_entry(n, &c->nodelist, list)
+	{
 		void *t, *tt;
 		char hwaddrstr[18] = {0};
 		char objstr[16] = {0};
@@ -397,7 +410,7 @@ static int cntlr_status(struct ubus_context *ctx, struct ubus_object *obj,
 		hwaddr_ntoa(n->hwaddr, hwaddrstr);
 		sprintf(objstr, "0x%08x", n->uobj[4]);
 		sprintf(mdidstr, "0x%02x%02x", n->mobility_domain[0],
-						n->mobility_domain[1]);
+				n->mobility_domain[1]);
 		t = blobmsg_open_table(&bb, "");
 		blobmsg_add_string(&bb, "ipaddr", inet_ntoa(n->ipaddr));
 		blobmsg_add_string(&bb, "addr", hwaddrstr);
@@ -405,7 +418,8 @@ static int cntlr_status(struct ubus_context *ctx, struct ubus_object *obj,
 		blobmsg_add_string(&bb, "mdid", mdidstr);
 
 		b = blobmsg_open_array(&bb, "fronthaul");
-		list_for_each_entry(p, &n->fronthaul_iflist, list) {
+		list_for_each_entry(p, &n->fronthaul_iflist, list)
+		{
 			char bssidstr[18] = {0};
 
 			hwaddr_ntoa(p->bssid, bssidstr);
@@ -423,14 +437,16 @@ static int cntlr_status(struct ubus_context *ctx, struct ubus_object *obj,
 	blobmsg_close_array(&bb, a);
 
 	a = blobmsg_open_array(&bb, "stations");
-	for (i = 0; i < AS_HASHSIZE; i++) {
+	for (i = 0; i < AS_HASHSIZE; i++)
+	{
 		struct active_sta *as;
 		void *ttt;
 
 		if (hlist_empty(&stalist[i]))
 			continue;
 
-		hlist_for_each_entry(as, &stalist[i], hlist) {
+		hlist_for_each_entry(as, &stalist[i], hlist)
+		{
 			char stastr[18] = {0};
 			char bssstr[18] = {0};
 			char old_bssstr[18] = {0};
@@ -458,18 +474,23 @@ static int cntlr_status(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 int cntlr_ap_caps(struct ubus_context *ctx, struct ubus_object *obj,
-			struct ubus_request_data *req, const char *method,
-			struct blob_attr *msg)
+				  struct ubus_request_data *req, const char *method,
+				  struct blob_attr *msg)
 {
 	struct blob_attr *tb[__AP_POLICY_MAX];
 	struct controller *c = container_of(obj, struct controller, obj);
 	uint8_t hwaddr[6] = {0};
 	struct cmdu_cstruct *cmdu;
+	struct blob_buf bb = {0};
+	uint16_t msgid;
+
+	blob_buf_init(&bb, 0);
 
 	blobmsg_parse(ap_caps_policy_params, __AP_POLICY_MAX, tb,
-			blob_data(msg), blob_len(msg));
+				  blob_data(msg), blob_len(msg));
 
-	if (tb[AP_POLICY_AGENT]) {
+	if (tb[AP_POLICY_AGENT])
+	{
 		char agent[18] = {0};
 
 		strncpy(agent, blobmsg_data(tb[AP_POLICY_AGENT]),
@@ -478,20 +499,21 @@ int cntlr_ap_caps(struct ubus_context *ctx, struct ubus_object *obj,
 			return UBUS_STATUS_UNKNOWN_ERROR;
 	}
 
-
 	cmdu = cntlr_gen_ap_capability_query(c, hwaddr, NULL);
 	if (!cmdu)
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
-	send_cmdu(c, cmdu);
+	msgid = send_cmdu(c, cmdu);
+	blobmsg_add_u16(&bb, "mid", msgid);
+	ubus_send_reply(c->ubus_ctx, req, bb.head);
 	map_free_cmdu(cmdu);
-
+	blob_buf_free(&bb);
 	return 0;
 }
 
 static int cntlr_channel_pref(struct ubus_context *ctx, struct ubus_object *obj,
-			struct ubus_request_data *req, const char *method,
-			struct blob_attr *msg)
+							  struct ubus_request_data *req, const char *method,
+							  struct blob_attr *msg)
 {
 	struct blob_attr *tb[__CHANNEL_PREF_POLICY_MAX];
 	struct controller *c = container_of(obj, struct controller, obj);
@@ -499,19 +521,20 @@ static int cntlr_channel_pref(struct ubus_context *ctx, struct ubus_object *obj,
 	struct cmdu_cstruct *cmdu_data;
 
 	cmdu_data = (struct cmdu_cstruct *)calloc(1,
-			sizeof(struct cmdu_cstruct));
-	if (!cmdu_data) {
+											  sizeof(struct cmdu_cstruct));
+	if (!cmdu_data)
+	{
 		fprintf(stderr, "failed to malloc cmdu\n");
 		return UBUS_STATUS_UNKNOWN_ERROR;
 	}
 
 	blobmsg_parse(channel_pref_policy_params, __CHANNEL_PREF_POLICY_MAX, tb,
-		blob_data(msg), blob_len(msg));
-
+				  blob_data(msg), blob_len(msg));
 
-	if (tb[CHANNEL_PREF_POLICY_AGENT]) {
+	if (tb[CHANNEL_PREF_POLICY_AGENT])
+	{
 		strncpy(agent, blobmsg_data(tb[CHANNEL_PREF_POLICY_AGENT]),
-			sizeof(agent) - 1);
+				sizeof(agent) - 1);
 		if (!hwaddr_aton(agent, cmdu_data->origin))
 			return UBUS_STATUS_UNKNOWN_ERROR;
 	}
@@ -529,8 +552,8 @@ static int cntlr_channel_pref(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int cntlr_channel_select(struct ubus_context *ctx, struct ubus_object *obj,
-		struct ubus_request_data *req, const char *method,
-		struct blob_attr *msg)
+								struct ubus_request_data *req, const char *method,
+								struct blob_attr *msg)
 {
 	struct blob_attr *tb[__CHANNEL_SEL_POLICY_MAX];
 	struct controller *c = container_of(obj, struct controller, obj);
@@ -548,53 +571,58 @@ static int cntlr_channel_select(struct ubus_context *ctx, struct ubus_object *ob
 	int rem, l = 0;
 
 	cmdu_data = (struct cmdu_cstruct *)calloc(1,
-		sizeof(struct cmdu_cstruct));
+											  sizeof(struct cmdu_cstruct));
 
-	if (!cmdu_data) {
+	if (!cmdu_data)
+	{
 		fprintf(stderr, "failed to malloc cmdu\n");
 		return UBUS_STATUS_UNKNOWN_ERROR;
 	}
 
 	blobmsg_parse(channel_select_policy_params, __CHANNEL_SEL_POLICY_MAX,
-			tb, blob_data(msg), blob_len(msg));
+				  tb, blob_data(msg), blob_len(msg));
 
-	if (tb[CHANNEL_SEL_POLICY_AGENT]) {
+	if (tb[CHANNEL_SEL_POLICY_AGENT])
+	{
 		strncpy(agent, blobmsg_data(tb[CHANNEL_SEL_POLICY_AGENT]),
-			sizeof(agent) - 1);
+				sizeof(agent) - 1);
 		if (!hwaddr_aton(agent, cmdu_data->origin))
 			goto fail_cmdu;
 	}
 
 	// TODO: ff:ff:ff:ff:ff:ff = send to all agents
-	if (tb[CHANNEL_SEL_POLICY_RADIO_ID]) {
+	if (tb[CHANNEL_SEL_POLICY_RADIO_ID])
+	{
 		strncpy(radio_id, blobmsg_data(tb[CHANNEL_SEL_POLICY_RADIO_ID]),
-			sizeof(radio_id) - 1);
+				sizeof(radio_id) - 1);
 		radio_present = 1;
 	}
 
 	if (tb[CHANNEL_SEL_POLICY_CLASS_ID])
-		class_id = (int) blobmsg_get_u32(tb[CHANNEL_SEL_POLICY_CLASS_ID]);
+		class_id = (int)blobmsg_get_u32(tb[CHANNEL_SEL_POLICY_CLASS_ID]);
 
-	if (tb[CHANNEL_SEL_POLICY_CHANNEL]) {
+	if (tb[CHANNEL_SEL_POLICY_CHANNEL])
+	{
 		channel_nr = blobmsg_check_array(tb[CHANNEL_SEL_POLICY_CHANNEL],
-				BLOBMSG_TYPE_INT32);
+										 BLOBMSG_TYPE_INT32);
 		l = 0;
 		blobmsg_for_each_attr(cur, tb[CHANNEL_SEL_POLICY_CHANNEL], rem)
 			channel[l++] = blobmsg_get_u32(cur);
 	}
 
 	if (tb[CHANNEL_SEL_POLICY_PREF])
-		pref = (int) blobmsg_get_u32(tb[CHANNEL_SEL_POLICY_PREF]);
+		pref = (int)blobmsg_get_u32(tb[CHANNEL_SEL_POLICY_PREF]);
 
 	if (tb[CHANNEL_SEL_POLICY_TRANSMIT_POWER])
-		transmit_power = (int) blobmsg_get_u32
-			(tb[CHANNEL_SEL_POLICY_TRANSMIT_POWER]);
+		transmit_power = (int)blobmsg_get_u32(tb[CHANNEL_SEL_POLICY_TRANSMIT_POWER]);
 
 	cmdu_data->message_type = CMDU_CHANNEL_SELECTION_REQ;
 
-	if (radio_present == 1 && class_id != -1 && pref != -1) {
+	if (radio_present == 1 && class_id != -1 && pref != -1)
+	{
 		p = calloc(1, sizeof(struct tlv_channel_pref));
-		if (!p) {
+		if (!p)
+		{
 			fprintf(stderr, "failed to malloc cmdu\n");
 			goto fail_cmdu;
 		}
@@ -606,44 +634,50 @@ static int cntlr_channel_select(struct ubus_context *ctx, struct ubus_object *ob
 
 		if (p->ch_preference_op_class_nr > 0)
 			p->op_class = calloc(p->ch_preference_op_class_nr,
-				sizeof(*p->op_class));
+								 sizeof(*p->op_class));
 
-		if (!p->op_class) {
+		if (!p->op_class)
+		{
 			fprintf(stderr, "|%s:%d| out of memory!\n", __func__, __LINE__);
 			p->ch_preference_op_class_nr = 0;
 			goto fail_p;
 		}
 
-		for (j = 0; j < p->ch_preference_op_class_nr; j++) {
+		for (j = 0; j < p->ch_preference_op_class_nr; j++)
+		{
 			p->op_class[j].op_class = (uint8_t)class_id;
 			p->op_class[j].channel_list = NULL;
 			if (channel_nr != 0)
 				p->op_class[j].channel_nr = channel_nr;
 
-			if (p->op_class[j].channel_nr > 0) {
+			if (p->op_class[j].channel_nr > 0)
+			{
 
 				p->op_class[j].channel_list =
-						calloc(p->op_class[j].channel_nr,
-						sizeof(uint8_t));
+					calloc(p->op_class[j].channel_nr,
+						   sizeof(uint8_t));
 
-				if (!p->op_class[j].channel_list) {
+				if (!p->op_class[j].channel_list)
+				{
 					fprintf(stderr, "|%s:%d| out of memory!\n",
 							__func__, __LINE__);
 					p->op_class[j].channel_nr = 0;
 					goto fail_p;
 				}
 				for (k = 0; k < p->op_class[j].channel_nr; k++)
-					p->op_class[j].channel_list[k] = (uint8_t) channel[k];
+					p->op_class[j].channel_list[k] = (uint8_t)channel[k];
 			}
-			p->op_class[j].preference = (uint8_t) pref;
+			p->op_class[j].preference = (uint8_t)pref;
 			p->op_class[j].preference_reason = 0x00;
 		}
 		count++;
 	}
 
-	if (radio_present == 1 && transmit_power != -1) {
+	if (radio_present == 1 && transmit_power != -1)
+	{
 		p1 = calloc(1, sizeof(struct tlv_tx_power_limit));
-		if (!p1) {
+		if (!p1)
+		{
 			fprintf(stderr, "failed to malloc cmdu\n");
 			goto fail_p;
 		}
@@ -657,7 +691,7 @@ static int cntlr_channel_select(struct ubus_context *ctx, struct ubus_object *ob
 	cmdu_data->num_tlvs = count;
 	if (count)
 		cmdu_data->tlvs = (uint8_t **)calloc(cmdu_data->num_tlvs,
-				sizeof(uint8_t *));
+											 sizeof(uint8_t *));
 	if (cmdu_data->tlvs && p != NULL)
 		cmdu_data->tlvs[tlv_index++] = (uint8_t *)p;
 
@@ -668,7 +702,7 @@ static int cntlr_channel_select(struct ubus_context *ctx, struct ubus_object *ob
 	map_free_cmdu(cmdu_data);
 	return 0;
 fail_p:
-	map_free_tlv_cstruct((uint8_t *) p);
+	map_free_tlv_cstruct((uint8_t *)p);
 fail_cmdu:
 	map_free_cmdu(cmdu_data);
 	return UBUS_STATUS_UNKNOWN_ERROR;
@@ -1051,8 +1085,8 @@ fail_parsing:
 }
 
 static int cntlr_sta_caps(struct ubus_context *ctx, struct ubus_object *obj,
-		struct ubus_request_data *req, const char *method,
-		struct blob_attr *msg)
+						  struct ubus_request_data *req, const char *method,
+						  struct blob_attr *msg)
 {
 	struct blob_attr *tb[__STA_POLICY_MAX];
 	uint8_t hw_bssid[6] = {0}, hw_sta[6] = {0}, hw_agent[6] = {0};
@@ -1061,18 +1095,20 @@ static int cntlr_sta_caps(struct ubus_context *ctx, struct ubus_object *obj,
 	struct cmdu_cstruct *cmdu;
 
 	blobmsg_parse(sta_caps_policy_params, __STA_POLICY_MAX, tb,
-			blob_data(msg), blob_len(msg));
+				  blob_data(msg), blob_len(msg));
 
-	if (!tb[STA_POLICY_STA] || !tb[STA_POLICY_BSSID]) {
-		fprintf(stderr, "STA Capability Query: must provide STA and "\
-				"BSSID\n");
+	if (!tb[STA_POLICY_STA] || !tb[STA_POLICY_BSSID])
+	{
+		fprintf(stderr, "STA Capability Query: must provide STA and "
+						"BSSID\n");
 		return UBUS_STATUS_INVALID_ARGUMENT;
 	}
 
 	strncpy(sta, blobmsg_data(tb[STA_POLICY_STA]), sizeof(sta) - 1);
 	strncpy(bssid, blobmsg_data(tb[STA_POLICY_BSSID]), sizeof(bssid) - 1);
 
-	if (tb[AP_POLICY_AGENT]) {
+	if (tb[AP_POLICY_AGENT])
+	{
 		char agent[18] = {0};
 
 		strncpy(agent, blobmsg_data(tb[AP_POLICY_AGENT]),
@@ -1088,7 +1124,7 @@ static int cntlr_sta_caps(struct ubus_context *ctx, struct ubus_object *obj,
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
 	cmdu = cntlr_gen_client_capability_query(c, hw_agent, NULL, hw_sta,
-			hw_bssid);
+											 hw_bssid);
 	if (!cmdu)
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
@@ -1098,15 +1134,15 @@ static int cntlr_sta_caps(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int cntlr_teardown_ap(struct ubus_context *ctx, struct ubus_object *obj,
-			struct ubus_request_data *req, const char *method,
-			struct blob_attr *msg)
+							 struct ubus_request_data *req, const char *method,
+							 struct blob_attr *msg)
 {
 	return 0;
 }
 
 static int cntlr_reconfig_ap(struct ubus_context *ctx, struct ubus_object *obj,
-			struct ubus_request_data *req, const char *method,
-			struct blob_attr *msg)
+							 struct ubus_request_data *req, const char *method,
+							 struct blob_attr *msg)
 {
 
 	struct controller *c = container_of(obj, struct controller, obj);
@@ -1117,14 +1153,16 @@ static int cntlr_reconfig_ap(struct ubus_context *ctx, struct ubus_object *obj,
 	int i, tlv_index = 0;
 
 	blobmsg_parse(reconfig_policy_params, __RECFG_POLICY_MAX, tb,
-			blob_data(msg), blob_len(msg));
+				  blob_data(msg), blob_len(msg));
 
-	if (!tb[RECFG_POLICY_ALMAC]) {
+	if (!tb[RECFG_POLICY_ALMAC])
+	{
 		fprintf(stderr, "Provide ALMAC address in format 11:22:33...\n");
 		return UBUS_STATUS_INVALID_ARGUMENT;
 	}
 
-	if (tb[RECFG_POLICY_AGENT]) {
+	if (tb[RECFG_POLICY_AGENT])
+	{
 		char agent[18] = {0};
 
 		strncpy(agent, blobmsg_data(tb[RECFG_POLICY_AGENT]),
@@ -1157,7 +1195,8 @@ static struct netif_radio *cntlr_radio_to_bssid(struct controller *c, const char
 {
 	struct netif_radio *r;
 
-	list_for_each_entry(r, &c->radiolist, list) {
+	list_for_each_entry(r, &c->radiolist, list)
+	{
 		if (!strncmp(r->name, radio, 16))
 			return r;
 	}
@@ -1167,8 +1206,8 @@ static struct netif_radio *cntlr_radio_to_bssid(struct controller *c, const char
 
 #if 0
 static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
-			struct ubus_request_data *req, const char *method,
-			struct blob_attr *msg)
+						   struct ubus_request_data *req, const char *method,
+						   struct blob_attr *msg)
 {
 	struct controller *c = container_of(obj, struct controller, obj);
 	struct blob_attr *tb[__CFG_POLICY_MAX];
@@ -1182,12 +1221,13 @@ static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
 	int i, tlv_index = 0;
 
 	blobmsg_parse(config_policy_params, __CFG_POLICY_MAX, tb,
-			blob_data(msg), blob_len(msg));
+				  blob_data(msg), blob_len(msg));
 
 	if (!tb[CFG_POLICY_AGENT] ||
-			(!tb[CFG_POLICY_RADIO] && !tb[CFG_POLICY_BSSID])) {
-		fprintf(stderr, "STA Capability Query: provide BSSID " \
-				"address in format 11:22:33...\n");
+		(!tb[CFG_POLICY_RADIO] && !tb[CFG_POLICY_BSSID]))
+	{
+		fprintf(stderr, "STA Capability Query: provide BSSID "
+						"address in format 11:22:33...\n");
 		return UBUS_STATUS_INVALID_ARGUMENT;
 	}
 
@@ -1195,8 +1235,10 @@ static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
 	if (!hwaddr_aton(agent, hwaddr))
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
-	list_for_each_entry(a, &c->cfg.policylist, list) {
-		if (!memcmp(hwaddr, a->agent_id, sizeof(hwaddr))) {
+	list_for_each_entry(a, &c->cfg.policylist, list)
+	{
+		if (!memcmp(hwaddr, a->agent_id, sizeof(hwaddr)))
+		{
 			found = a;
 			break;
 		}
@@ -1206,7 +1248,8 @@ static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
 	cmdu = (struct cmdu_cstruct *)calloc(1, sizeof(struct cmdu_cstruct));
-	if (!cmdu) {
+	if (!cmdu)
+	{
 		fprintf(stderr, "failed to malloc cmdu\n");
 		return UBUS_STATUS_UNKNOWN_ERROR;
 	}
@@ -1215,25 +1258,30 @@ static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
 	cmdu->message_type = CMDU_TYPE_AP_AUTOCONFIGURATION_WSC;
 	cmdu->message_id = 1;
 
-	if (tb[CFG_POLICY_BSSID]) {
+	if (tb[CFG_POLICY_BSSID])
+	{
 		char bssid[18] = {0};
 		uint8_t hwaddr[6] = {0};
 
 		strncpy(bssid, blobmsg_data(tb[CFG_POLICY_BSSID]),
 				sizeof(bssid) - 1);
-		if (!hwaddr_aton(bssid, hwaddr)) {
+		if (!hwaddr_aton(bssid, hwaddr))
+		{
 			free(cmdu);
 			return UBUS_STATUS_UNKNOWN_ERROR;
 		}
 		p = cntlr_gen_ap_radio_identifier(c, cmdu, hwaddr);
-	} else if (tb[CFG_POLICY_RADIO]) {
+	}
+	else if (tb[CFG_POLICY_RADIO])
+	{
 		char radio[18] = {0};
 		struct netif_radio *r;
 
 		strncpy(radio, blobmsg_data(tb[CFG_POLICY_RADIO]),
 				sizeof(radio) - 1);
 		r = cntlr_radio_to_bssid(c, radio);
-		if (!r) {
+		if (!r)
+		{
 			free(cmdu);
 			return UBUS_STATUS_UNKNOWN_ERROR;
 		}
@@ -1262,7 +1310,7 @@ static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
 
 	cmdu->num_tlvs++;
 
-	p1 = (struct tlv_default_8021q_settings *) calloc(1, sizeof(*p1));
+	p1 = (struct tlv_default_8021q_settings *)calloc(1, sizeof(*p1));
 	if (!p1)
 		goto fail_p;
 
@@ -1271,7 +1319,7 @@ static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
 	p1->primary_vid = a->pvid;
 	p1->pcp = a->pcp_default;
 
-	p2 = (struct tlv_traffic_sep_policy *) calloc(1, sizeof(*p2));
+	p2 = (struct tlv_traffic_sep_policy *)calloc(1, sizeof(*p2));
 	if (!p2)
 		goto fail_p1;
 
@@ -1282,7 +1330,8 @@ static int cntlr_config_ap(struct ubus_context *ctx, struct ubus_object *obj,
 	if (!p2->data)
 		goto fail_p2;
 
-	for (i = 0; i < p2->nbr_ssid; i++) {
+	for (i = 0; i < p2->nbr_ssid; i++)
+	{
 		int len;
 
 		len = strlen((char *)c->cfg.fh[i].ssid);
@@ -1324,8 +1373,8 @@ fail_cmdu:
 #endif
 
 static int cntlr_bk_steer(struct ubus_context *ctx, struct ubus_object *obj,
-			struct ubus_request_data *req, const char *method,
-			struct blob_attr *msg)
+						  struct ubus_request_data *req, const char *method,
+						  struct blob_attr *msg)
 {
 	struct blob_attr *tb[__BK_STEER_POLICY_MAX];
 	struct controller *c = container_of(obj, struct controller, obj);
@@ -1335,11 +1384,12 @@ static int cntlr_bk_steer(struct ubus_context *ctx, struct ubus_object *obj,
 	uint8_t op_class, channel;
 
 	blobmsg_parse(bk_steer_policy_params, __BK_STEER_POLICY_MAX, tb,
-			blob_data(msg), blob_len(msg));
+				  blob_data(msg), blob_len(msg));
 
 	if (!tb[BK_STEER_POLICY_BSSID] || !tb[BK_STEER_POLICY_CHANNEL] ||
-			!tb[BK_STEER_POLICY_OP_CLASS] ||
-			!tb[BK_STEER_POLICY_STA_MAC]) {
+		!tb[BK_STEER_POLICY_OP_CLASS] ||
+		!tb[BK_STEER_POLICY_STA_MAC])
+	{
 		fprintf(stderr, "BSSID, channel and op class required!\n");
 		return UBUS_STATUS_INVALID_ARGUMENT;
 	}
@@ -1349,17 +1399,20 @@ static int cntlr_bk_steer(struct ubus_context *ctx, struct ubus_object *obj,
 	strncpy(bkhaul_str, blobmsg_data(tb[BK_STEER_POLICY_STA_MAC]),
 			sizeof(bkhaul_str) - 1);
 
-	if (!hwaddr_aton(bssid_str, bssid)) {
+	if (!hwaddr_aton(bssid_str, bssid))
+	{
 		fprintf(stderr, "BSSID must be in format 11:22:33...\n");
 		return UBUS_STATUS_INVALID_ARGUMENT;
 	}
 
-	if (!hwaddr_aton(bkhaul_str, bkhaul)) {
+	if (!hwaddr_aton(bkhaul_str, bkhaul))
+	{
 		fprintf(stderr, "Backhaul must be in format 11:22:33...\n");
 		return UBUS_STATUS_INVALID_ARGUMENT;
 	}
 
-	if (tb[AP_POLICY_AGENT]) {
+	if (tb[AP_POLICY_AGENT])
+	{
 		strncpy(agent_str, blobmsg_data(tb[AP_POLICY_AGENT]),
 				sizeof(agent_str) - 1);
 		if (!hwaddr_aton(agent_str, agent))
@@ -1370,7 +1423,7 @@ static int cntlr_bk_steer(struct ubus_context *ctx, struct ubus_object *obj,
 	op_class = blobmsg_get_u8(tb[BK_STEER_POLICY_OP_CLASS]);
 
 	cmdu = cntlr_gen_backhaul_steer_request(c, agent, NULL, bssid, bkhaul,
-			op_class, channel);
+											op_class, channel);
 	if (!cmdu)
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
@@ -1381,8 +1434,8 @@ static int cntlr_bk_steer(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int cntlr_ap_policy_config(struct ubus_context *ctx, struct ubus_object *obj,
-				struct ubus_request_data *req, const char *method,
-				struct blob_attr *msg)
+								  struct ubus_request_data *req, const char *method,
+								  struct blob_attr *msg)
 {
 	char agent[18] = {0};
 	char radio[18] = {0};
@@ -1396,13 +1449,25 @@ static int cntlr_ap_policy_config(struct ubus_context *ctx, struct ubus_object *
 	int rem, num_radio = 0, num_bss = 0;
 	struct blob_attr *tb[__AP_POLICY_CONFIG_MAX];
 	struct controller *c = container_of(obj, struct controller, obj);
+	/* TLVs */
+	struct tlv_steering_policy *p1 = NULL;
+	struct tlv_metric_report_policy *p2 = NULL;
+	/* Map-2 tlvs */
+	// #ifdef PROFILE2
+	struct tlv_default_8021q_settings *p3 = NULL;
+	struct tlv_traffic_sep_policy *p4 = NULL;
+	struct tlv_ch_scan_rep_policy *p5 = NULL;
+	struct tlv_unsuccess_assoc_policy *p6 = NULL;
+	struct tlv_backhaul_bss_config *p7 = NULL;
+	// #endif
 
 	blobmsg_parse(ap_policy_config_params, __AP_POLICY_CONFIG_MAX, tb,
-			blob_data(msg), blob_len(msg));
+				  blob_data(msg), blob_len(msg));
 
-	if (!tb[AP_POLICY_CONFIG_AGENT] || !tb[AP_POLICY_CONFIG_RADIOS]) {
-		fprintf(stderr, "Agent policy config: provide Agent or Radio " \
-				"address in format 11:22:33...\n");
+	if (!tb[AP_POLICY_CONFIG_AGENT] || !tb[AP_POLICY_CONFIG_RADIOS])
+	{
+		fprintf(stderr, "Agent policy config: provide Agent or Radio "
+						"address in format 11:22:33...\n");
 		return UBUS_STATUS_INVALID_ARGUMENT;
 	}
 
@@ -1410,8 +1475,10 @@ static int cntlr_ap_policy_config(struct ubus_context *ctx, struct ubus_object *
 	if (!hwaddr_aton(agent, hwaddr))
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
-	list_for_each_entry(a, &c->cfg.policylist, list) {
-		if (!memcmp(hwaddr, a->agent_id, sizeof(hwaddr))) {
+	list_for_each_entry(a, &c->cfg.policylist, list)
+	{
+		if (!memcmp(hwaddr, a->agent_id, sizeof(hwaddr)))
+		{
 			found = a;
 			break;
 		}
@@ -1421,17 +1488,19 @@ static int cntlr_ap_policy_config(struct ubus_context *ctx, struct ubus_object *
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
 	/* fetch radio id's */
-	blobmsg_for_each_attr(attr, tb[AP_POLICY_CONFIG_RADIOS], rem) {
+	blobmsg_for_each_attr(attr, tb[AP_POLICY_CONFIG_RADIOS], rem)
+	{
 		uint8_t bssid[6] = {0};
 
 		if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
 			continue;
 
 		memset(radio, 0, sizeof(radio));
-		strncpy(radio, blobmsg_data(attr), sizeof(radio)-1);
-		if (!hwaddr_aton(radio, bssid)) {
-			fprintf(stderr, "Agent policy config: provide radio " \
-					"address in format 11:22:33...\n");
+		strncpy(radio, blobmsg_data(attr), sizeof(radio) - 1);
+		if (!hwaddr_aton(radio, bssid))
+		{
+			fprintf(stderr, "Agent policy config: provide radio "
+							"address in format 11:22:33...\n");
 			if (radiolist)
 				free(radiolist);
 
@@ -1443,14 +1512,15 @@ static int cntlr_ap_policy_config(struct ubus_context *ctx, struct ubus_object *
 		if (!radiolist)
 			return UBUS_STATUS_UNKNOWN_ERROR;
 
-		memcpy(&radiolist[(num_radio-1)*6], bssid, 6);
+		memcpy(&radiolist[(num_radio - 1) * 6], bssid, 6);
 	}
 
 	if (num_radio == 0)
 		return UBUS_STATUS_UNKNOWN_ERROR;
 
 	/* fetch BSS list */
-	blobmsg_for_each_attr(attr, tb[AP_POLICY_CONFIG_BSS], rem) {
+	blobmsg_for_each_attr(attr, tb[AP_POLICY_CONFIG_BSS], rem)
+	{
 		uint8_t bssid[6] = {0};
 
 		if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
@@ -1764,16 +1834,16 @@ int cntlr_publish_object(struct controller *c, const char *objname)
 	struct ubus_method m[14] = {
 		UBUS_METHOD_NOARG("status", cntlr_status),
 		UBUS_METHOD("ap_caps", cntlr_ap_caps,
-				ap_caps_policy_params),
+					ap_caps_policy_params),
 		UBUS_METHOD("sta_caps", cntlr_sta_caps,
-				sta_caps_policy_params),
+					sta_caps_policy_params),
 		UBUS_METHOD("channels", cntlr_channel_pref,
-				channel_pref_policy_params),
+					channel_pref_policy_params),
 		UBUS_METHOD("bk_steer", cntlr_bk_steer, bk_steer_policy_params),
 		UBUS_METHOD("agent_policy", cntlr_ap_policy_config,
-				ap_policy_config_params),
+					ap_policy_config_params),
 		UBUS_METHOD("channel", cntlr_channel_select,
-				channel_select_policy_params),
+					channel_select_policy_params),
 		UBUS_METHOD("reconfig_ap", cntlr_reconfig_ap,
 				reconfig_policy_params),
 		UBUS_METHOD("client_steering", cntlr_client_steering,
@@ -1804,7 +1874,8 @@ int cntlr_publish_object(struct controller *c, const char *objname)
 		return -1;
 
 	obj_methods = calloc(num_methods, sizeof(struct ubus_method));
-	if (!obj_methods) {
+	if (!obj_methods)
+	{
 		free(obj_type);
 		return -1;
 	}
@@ -1820,9 +1891,10 @@ int cntlr_publish_object(struct controller *c, const char *objname)
 	obj->type = obj_type;
 
 	ret = ubus_add_object(c->ubus_ctx, obj);
-	if (ret) {
+	if (ret)
+	{
 		err("Failed to add '%s' err = %s\n",
-				objname, ubus_strerror(ret));
+			objname, ubus_strerror(ret));
 		free(obj_methods);
 		free(obj_type);
 		return ret;
@@ -1835,7 +1907,8 @@ int cntlr_publish_object(struct controller *c, const char *objname)
 
 void cntlr_remove_object(struct controller *c)
 {
-	if (c->ubus_ctx && c->obj.id != OBJECT_INVALID) {
+	if (c->ubus_ctx && c->obj.id != OBJECT_INVALID)
+	{
 		ubus_remove_object(c->ubus_ctx, &c->obj);
 		free(c->obj.type);
 		free((void *)c->obj.methods);
@@ -1844,7 +1917,7 @@ void cntlr_remove_object(struct controller *c)
 
 int cntlr_register_module(struct controller *c)
 {
-#define cntlr_sign		"f6dfa346957f45cb8d82dc6b77e2df61"
+#define cntlr_sign "f6dfa346957f45cb8d82dc6b77e2df61"
 	uint8_t cntlr_sig[16];
 	char data[2 * sizeof(struct map_module) + 1] = {0};
 	int ret;
@@ -1862,12 +1935,13 @@ int cntlr_register_module(struct controller *c)
 
 	/* register with map.1905 plugin */
 	ret = ubus_lookup_id(ctx, map_plugin, &map_id);
-	if (ret) {
+	if (ret)
+	{
 		/* TODO: if map plugin is not installed, wait for it to appear.
 		 * Retry after some time.
 		 */
 		dbg("plugin '%s' lookup failed. %s\n", map_plugin,
-						ubus_strerror(ret));
+			ubus_strerror(ret));
 		return -1;
 	}
 
@@ -1876,14 +1950,15 @@ int cntlr_register_module(struct controller *c)
 	btostr((unsigned char *)&m, sizeof(struct map_module), data);
 	blobmsg_add_string(&bb, "data", data);
 	ret = ubus_invoke(ctx, map_id, "register", bb.head, NULL, 0, 3000);
-	if (ret) {
+	if (ret)
+	{
 		warn("Failed to 'register' with %s (err = %s)\n",
-				map_plugin, ubus_strerror(ret));
+			 map_plugin, ubus_strerror(ret));
 	}
 
 	blob_buf_free(&bb);
 
-#if 0	// testing notify event
+#if 0 // testing notify event
 	blob_buf_init(&bb, 0);
 	blobmsg_add_u32(&bb, "module", c->obj.id);
 	blobmsg_add_string(&bb, "data", "Hello World");
diff --git a/src/core/cntlr_ubus.h b/src/core/cntlr_ubus.h
index 25234ad5..b73dece5 100644
--- a/src/core/cntlr_ubus.h
+++ b/src/core/cntlr_ubus.h
@@ -18,5 +18,5 @@ extern void cntlr_remove_object(struct controller *c);
 extern int cntlr_register_module(struct controller *c);
 
 void send_cmdu_cb(struct ubus_request *req, int type, struct blob_attr *msg);
-int send_cmdu(struct controller *c, struct cmdu_cstruct *cmdu_data);
+uint16_t send_cmdu(struct controller *c, struct cmdu_cstruct *cmdu_data);
 #endif /* CNTLR_UBUS_H */
diff --git a/src/utils/debug.c b/src/utils/debug.c
index 166e558d..3b02d08a 100644
--- a/src/utils/debug.c
+++ b/src/utils/debug.c
@@ -17,7 +17,10 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <time.h>
-
+#include <json-c/json_tokener.h>
+#include <json-validator.h>
+#include <json-c/json.h>
+#include <json-editor.h>
 #include "utils.h"
 #include "debug.h"
 
@@ -33,16 +36,19 @@ extern const char *PROG_NAME;
 extern int verbose;
 extern bool syslogging;
 extern bool usefifo;
-static const int syslog_level[] = { LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG };
+static const int syslog_level[] = {LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG};
 
 void start_logging(void)
 {
 	if (syslogging)
 		openlog(PROG_NAME, 0, LOG_DAEMON);
 
-	if (!outfile) {
-		if (outfile_path) {
-			if (usefifo) {
+	if (!outfile)
+	{
+		if (outfile_path)
+		{
+			if (usefifo)
+			{
 				struct stat st;
 				int rfd;
 
@@ -51,19 +57,24 @@ void start_logging(void)
 
 				mkfifo(outfile_path, 0600);
 				if (stat(outfile_path, &st) == -1 ||
-						!S_ISFIFO(st.st_mode))
+					!S_ISFIFO(st.st_mode))
 					return;
 				rfd = open(outfile_path,
-						O_RDONLY | O_NONBLOCK);
-				if (rfd) {
+						   O_RDONLY | O_NONBLOCK);
+				if (rfd)
+				{
 					ffd = open(outfile_path,
-							O_WRONLY | O_NONBLOCK);
+							   O_WRONLY | O_NONBLOCK);
 					close(rfd);
 				}
-			} else {
+			}
+			else
+			{
 				outfile = fopen(outfile_path, "w+");
 			}
-		} else {
+		}
+		else
+		{
 			outfile = stderr;
 		}
 	}
@@ -77,7 +88,8 @@ void stop_logging(void)
 	if (outfile)
 		fclose(outfile);
 
-	if (ffd) {
+	if (ffd)
+	{
 		close(ffd);
 		unlink(outfile_path);
 	}
@@ -87,20 +99,22 @@ void log_message(int level, const char *fmt, ...)
 {
 	va_list args;
 
-	if (level > verbose) {
-		if (usefifo && ffd) {
+	if (level > verbose)
+	{
+		if (usefifo && ffd)
+		{
 			time_t now = time(NULL);
 			struct tm *tm_now = localtime(&now);
 			const char *tm_fmt = "[%d-%02d-%02d %02d:%02d:%02d] ";
 
 			va_start(args, fmt);
 			dprintf(ffd, tm_fmt,
-				tm_now->tm_year + 1900,
-				tm_now->tm_mon + 1,
-				tm_now->tm_mday,
-				tm_now->tm_hour,
-				tm_now->tm_min,
-				tm_now->tm_sec);
+					tm_now->tm_year + 1900,
+					tm_now->tm_mon + 1,
+					tm_now->tm_mday,
+					tm_now->tm_hour,
+					tm_now->tm_min,
+					tm_now->tm_sec);
 			vdprintf(ffd, fmt, args);
 			va_end(args);
 		}
@@ -127,7 +141,8 @@ void dump(unsigned char *buf, int len, char *label)
 	if (label)
 		printf("---- %s ----", label);
 
-	for (i = 0; i < len; i++) {
+	for (i = 0; i < len; i++)
+	{
 		if (!(i % 4))
 			printf("  ");
 		if (!(i % 16))
@@ -142,9 +157,12 @@ void dump(unsigned char *buf, int len, char *label)
 // TODO: duplicate of start_logging
 void start_test_logging(void)
 {
-	if (!testfile) {
-		if (testfile_path) {
-			if (usefifo) {
+	if (!testfile)
+	{
+		if (testfile_path)
+		{
+			if (usefifo)
+			{
 				struct stat st;
 				int rfd;
 
@@ -153,20 +171,25 @@ void start_test_logging(void)
 
 				mkfifo(testfile_path, 0600);
 				if (stat(testfile_path, &st) == -1 ||
-						!S_ISFIFO(st.st_mode))
+					!S_ISFIFO(st.st_mode))
 					return;
 
 				rfd = open(testfile_path,
-						O_RDONLY | O_NONBLOCK);
-				if (rfd) {
+						   O_RDONLY | O_NONBLOCK);
+				if (rfd)
+				{
 					tfd = open(testfile_path,
-							O_WRONLY | O_NONBLOCK);
+							   O_WRONLY | O_NONBLOCK);
 					close(rfd);
 				}
-			} else {
+			}
+			else
+			{
 				testfile = fopen(testfile_path, "w+");
 			}
-		} else {
+		}
+		else
+		{
 			testfile = stderr;
 		}
 	}
@@ -177,7 +200,8 @@ void stop_test_logging(void)
 	if (testfile)
 		fclose(testfile);
 
-	if (tfd) {
+	if (tfd)
+	{
 		close(tfd);
 		unlink(testfile_path);
 	}
@@ -201,23 +225,65 @@ void log_test(int level, void *var, int len)
 	free(bstr);
 }
 
-void log_cmdu(int level, void *var)
+void _log_cmdu(int level, void *var, bool flag)
 {
-	struct cmdu_cstruct *cmdu = (struct cmdu_cstruct *) var;
+	struct cmdu_cstruct *cmdu = (struct cmdu_cstruct *)var;
+	struct json_object *msgversion, *msgtype, *msgid, *msg;
+	struct json_object *intfname, *tlvarray, *direction;
+	struct json_object *main_object;
+	char *bstr;
 	int i;
 
+	main_object = json_object_new_object();
+
+	tlvarray = json_object_new_array();
 	if (level > verbose)
 		return;
 
-	for (i = 0; i < cmdu->num_tlvs; i++) {
+	for (i = 0; i < cmdu->num_tlvs; i++)
+	{
 		uint16_t len;
 		uint8_t *btlv;
+		struct json_object *tlv;
+		struct json_object *tlv_type;
+		struct json_object *tlv_object;
+		tlv_object = json_object_new_object();
+		uint8_t *type;
 
 		btlv = map_put_tlv_cstruct(cmdu->tlvs[i], &len);
+		type = cmdu->tlvs[i];
 		if (!btlv)
 			continue;
-
-		log_test(level, btlv, len);
+		bstr = calloc(1, (len * 2) + 1);
+		if (!bstr)
+			return;
+		btostr(btlv, len, bstr);
+		tlv = json_object_new_string(bstr);
+		tlv_type = json_object_new_string(map_stringify_tlv_type(*type));
+		json_object_object_add(tlv_object, "data", tlv);
+		json_object_object_add(tlv_object, "type", tlv_type);
+		json_object_array_add(tlvarray, tlv_object);
 		free(btlv);
+		free(bstr);
 	}
+	msgversion = json_object_new_int(cmdu->message_version);
+	msgtype = json_object_new_int(cmdu->message_type);
+	direction = json_object_new_boolean(flag);
+	msgid = json_object_new_int(cmdu->message_id);
+	intfname = json_object_new_string(cmdu->intf_name);
+	msg = json_object_new_string(map_stringify_cmdu_type(cmdu->message_type));
+
+	json_object_object_add(main_object, "message_version", msgversion);
+	json_object_object_add(main_object, "message_type", msgtype);
+	json_object_object_add(main_object, "message_id", msgid);
+	json_object_object_add(main_object, "interface_name", intfname);
+	json_object_object_add(main_object, "message", msg);
+	json_object_object_add(main_object, "direction", direction);
+	json_object_object_add(main_object, "tlvs", tlvarray);
+
+	fprintf(testfile, "%s\n", json_object_to_json_string(main_object));
+
+	fflush(testfile);
+
+	json_object_put(main_object);
 }
diff --git a/src/utils/debug.h b/src/utils/debug.h
index 2bf8d5d9..3cf572d9 100644
--- a/src/utils/debug.h
+++ b/src/utils/debug.h
@@ -10,7 +10,7 @@
 #ifndef DEBUG_H
 #define DEBUG_H
 
-#include "alloctrace.h"
+// #include "alloctrace.h"
 
 void start_logging(void);
 void stop_logging(void);
@@ -18,7 +18,7 @@ void start_test_logging(void);
 void stop_test_logging(void);
 void log_message(int level, const char *fmt, ...);
 void log_test(int level, void *var, int len);
-void log_cmdu(int level, void *var);
+void _log_cmdu(int level, void *var, bool flag);
 
 #define DEBUG_COLOR	1
 
@@ -44,7 +44,7 @@ void log_cmdu(int level, void *var);
 #define trace_cmd(fmt, ...)   log_message(4, brown fmt nocl, ## __VA_ARGS__)
 #define loud(fmt, ...)        log_message(5, fmt, ## __VA_ARGS__)
 #define test(var, len)        log_test(6, var, len)
-#define test_cmdu(var)        log_cmdu(6, var)
+#define log_cmdu(var, flag)        _log_cmdu(6, var, flag) /* TODO: delete definition log_cmdu */
 #else
 
 #define logrec(...)	log_message(-1, __VA_ARGS__)
diff --git a/test/cmocka/Makefile b/test/cmocka/Makefile
index 7bc33ed3..77dc5b10 100644
--- a/test/cmocka/Makefile
+++ b/test/cmocka/Makefile
@@ -5,7 +5,7 @@ CMOCKA_LIB	= -l cmocka
 LIBS		= $(MAPCNTLR_LIB) $(CMOCKA_LIB) -lwifi-6 -pthread -luci -lubus -lubox -ljson-c -lblobmsg_json -lnl-genl-3 -lnl-3 -ljson-validator -ljson-schema-validator -ljson-editor
 LIBS 		+= -rdynamic -ldl
 LIBS 		+= -lmaputils
-CFLAGS		= -g -Wall -I../../src/core -I../../src/utils
+CFLAGS		= -g -Wall -I../../src/core -I../../src/utils -I../../src/
 LDFLAGS		= $(LIBS) -Wl,-rpath=$(MAPCNTLR_LIB_DIR) -I$(MAPCNTLR_LIB_DIR)
 UNIT_TESTS	= unit_test_cntlr
 FUNCTIONAL_TESTS	= functional_test_cntlr
@@ -22,6 +22,7 @@ functional_test_cntlr: $(UTILS) functional_test_cntlr.o
 	$(CC) -o $@ $^ $(LDFLAGS)
 
 unit-test: $(UNIT_TESTS)
+	echo $(MAPCNTLR_LIB_DIR)
 	$(foreach testprog, $(UNIT_TESTS), sudo $(VALGRIND) ./$(testprog);)
 
 functional-test: $(FUNCTIONAL_TESTS)
diff --git a/test/cmocka/unit_test_cntlr.c b/test/cmocka/unit_test_cntlr.c
index 189054c1..708c2e1a 100644
--- a/test/cmocka/unit_test_cntlr.c
+++ b/test/cmocka/unit_test_cntlr.c
@@ -13,7 +13,7 @@
 #include <json-c/json.h>
 
 #include <easy/easy.h>
-#include <wifi.h>	// FIXME: should not be included
+#include <wifi.h> // FIXME: should not be included
 
 #include "debug.h"
 #include "utils.h"
@@ -25,13 +25,15 @@
 #define AGENT_FILE "/tmp/cntlr.test.log"
 
 int cntlr_ap_caps(struct ubus_context *ctx, struct ubus_object *obj,
-		struct ubus_request_data *req, const char *method,
-		struct blob_attr *msg);
+				  struct ubus_request_data *req, const char *method,
+				  struct blob_attr *msg);
 
-struct test_ctx {
+struct test_ctx
+{
 	struct blob_buf bb;
 	struct ubus_object radio;
 	struct ubus_object ap;
+	struct controller c;
 	FILE *fp;
 };
 
@@ -41,6 +43,10 @@ static int group_setup(void **state)
 
 	ctx = calloc(1, sizeof(*ctx));
 
+	// TODO: place in group setup
+	ctx->c.ubus_ctx = ubus_connect(NULL);
+	printf("Ctx pointer is %p\n ", ctx);
+	blob_buf_init(&ctx->bb, 0);
 	start_test_logging();
 
 	if (!ctx)
@@ -49,9 +55,10 @@ static int group_setup(void **state)
 	remove(CNTLR_FILE);
 	remove(AGENT_FILE);
 
-	memset(&ctx->bb, 0, sizeof(struct blob_buf));
+	// memset(&ctx->bb, 0, sizeof(struct blob_buf));
 
 	*state = ctx;
+	printf("State pointer is %p\n ", *state);
 	return 0;
 }
 
@@ -62,24 +69,24 @@ static int setup(void **state)
 
 static int teardown(void **state)
 {
-	stop_test_logging();
 
 	return 0;
 }
 
 /* overload ubus_send_reply to prevent segfault*/
 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
-		    struct blob_attr *msg)
+					struct blob_attr *msg)
 {
 	return 0;
 }
 
 static int group_teardown(void **state)
 {
-//	struct test_ctx *ctx = (struct test_ctx *) *state;
+	struct test_ctx *ctx = (struct test_ctx *)*state;
 
 	blob_buf_free(&ctx->bb);
-//	free(ctx);
+	stop_test_logging();
+	free(ctx);
 	remove(CNTLR_FILE);
 	remove(AGENT_FILE);
 	return 0;
@@ -88,14 +95,10 @@ static int group_teardown(void **state)
 static void test_cmdu_comm(void **state)
 {
 	int rv;
-	struct blob_buf bb = {0};
-	struct controller c;
-
-	// TODO: place in group setup
-	c.ubus_ctx = ubus_connect(NULL);
-	blob_buf_init(&bb, 0);
 
-	rv = cntlr_ap_caps(NULL, &c.obj, NULL, NULL, bb.head);
+	struct test_ctx *ctx = (struct test_ctx *)*state;
+	printf("Ctx pointer is %p\n ", ctx);
+	rv = cntlr_ap_caps(NULL, &ctx->c.obj, NULL, NULL, ctx->bb.head);
 
 	assert_int_equal(rv, 0);
 
@@ -105,7 +108,7 @@ static void test_cmdu_comm(void **state)
 int main(void)
 {
 	const struct CMUnitTest tests[] = {
-		cmocka_unit_test_setup_teardown(test_cmdu_comm, setup, teardown),
+		cmocka_unit_test(test_cmdu_comm),
 	};
 
 	return cmocka_run_group_tests(tests, group_setup, group_teardown);
diff --git a/test/files/cntlr_ap_caps.json b/test/files/cntlr_ap_caps.json
new file mode 100644
index 00000000..71392b77
--- /dev/null
+++ b/test/files/cntlr_ap_caps.json
@@ -0,0 +1,54 @@
+{
+    "message_version": 0,
+    "message_type": 32770,
+    "message_id": 42892,
+    "interface_name": "eth0",
+    "message": "CMDU_AP_CAPABILITY_REPORT",
+    "direction": true,
+    "tlvs": [
+        {
+            "data": "85001400000000000204035114015d5314015d5414015d",
+            "type": "MAP_TLV_AP_RADIO_BASIC_CAPABILITIES"
+        },
+        {
+            "data": "85003c000000000005040d7317015d7617015d791e015d7417015d7717015d7a1e015d7517015d7817015d7b1e015d7d1e015d801e015d8117015d8217015d",
+            "type": "MAP_TLV_AP_RADIO_BASIC_CAPABILITIES"
+        },
+        {
+            "data": "a1000100",
+            "type": "MAP_TLV_AP_CAPABILITY"
+        },
+        {
+            "data": "860007000000000002fe",
+            "type": "MAP_TLV_AP_HT_CAPABILITIES"
+        },
+        {
+            "data": "860007000000000005fe",
+            "type": "MAP_TLV_AP_HT_CAPABILITIES"
+        },
+        {
+            "data": "87000c00000000000200000000fc0c",
+            "type": "MAP_TLV_AP_VHT_CAPABILITIES"
+        },
+        {
+            "data": "87000c00000000000501e0bcfafe2e",
+            "type": "MAP_TLV_AP_VHT_CAPABILITIES"
+        },
+        {
+            "data": "a5008d02000000000002200000012c03510d0102030405060708090a0b0c0d5309010203040506070809540905060708090a0b0c0d000000000005200000012c0d730424282c30760434383c40790b64686c7074787c8084888c7402242c7702343c7a05646c747c8475022830780238407b0568707880887d0695999da1a5a980042434647481022464820424346474",
+            "type": "MAP_TLV_CHANNEL_SCAN_CAPABILITY"
+        },
+        {
+            "data": "b2008f45550200000000000201003c000003510d0102030405060708090a0b0c0d5309010203040506070809540905060708090a0b0c0d00000000000501005802000d730424282c30760434383c40790b64686c7074787c8084888c7402242c7702343c7a05646c747c8475022830780238407b0568707880887d0695999da1a5a980042434647481022464820424346474",
+            "type": "MAP_TLV_CAC_CAPABILITY"
+        },
+        {
+            "data": "b40004000000ff",
+            "type": "MAP_TLV_PROFILE2_AP_CAPABILITY"
+        },
+        {
+            "data": "c5000400002710",
+            "type": "MAP_TLV_METRIC_COLLECTION_INTERVAL"
+        }
+    ]
+}
\ No newline at end of file
-- 
GitLab