From 0035ee779c5b96e6ab1fc1b8f836ebaee7a74a78 Mon Sep 17 00:00:00 2001
From: Yalu Zhang <yalu.zhang@iopsys.eu>
Date: Mon, 23 Aug 2021 15:23:22 +0200
Subject: [PATCH] Fix issues caused by sending ubus events

Also fix some miscellaneous compiling warnings.
---
 channels/chan_brcm.c                  | 242 ++++++++++----------------
 res/res_pjsip_outbound_registration.c |  52 +++---
 res/res_pjsip_pubsub.c                |  59 ++++---
 3 files changed, 156 insertions(+), 197 deletions(-)

diff --git a/channels/chan_brcm.c b/channels/chan_brcm.c
index 02cc830cc3..e41a905df5 100644
--- a/channels/chan_brcm.c
+++ b/channels/chan_brcm.c
@@ -103,7 +103,6 @@ static void brcm_unlock_pvts(void);
 static struct brcm_pvt* brcm_get_next_pvt(struct brcm_pvt *p);
 static void *pe_base_run(void *unused);
 static int brcm_create_connection(struct brcm_subchannel *p);
-static int brcm_signal_congestion(struct brcm_pvt *p);
 static int brcm_stop_dialtone(struct brcm_pvt *p);
 static void brcm_signal_howler(struct brcm_pvt *p);
 static int brcm_signal_ringing(struct brcm_pvt *p);
@@ -157,12 +156,7 @@ static int max_sessions_per_line;
  * REFER to the transferee */
 static int hold_target_before_refer = 1;
 
-/* Boolean, controls whether the conference call shall be terminated when the initiator hangs up.
- * The conference call terminates until there is only one participant remaining by default. The
- * option is not configurable for now */
-static int terminate_conference = 0;
-
-/* Global jitterbuffer configuration */
+/* Global jitter buffer configuration */
 static struct ast_jb_conf global_jbconf;
 
 //TODO change AST_MAX_EXTENSION to something shorter
@@ -414,7 +408,6 @@ static void ubus_call_answer(struct ubus_request *req, int type, struct blob_att
 	ast_verbose("%s() from thread %d\n", __func__, ast_get_tid());
 }
 
-
 // Callback for: a ubus call (invocation) has finished
 static void ubus_call_complete(struct ubus_request *req, int ret)
 {
@@ -458,6 +451,38 @@ static void endpt_signal(int line, char *signal, char *state, char *data) {
 	}
 }
 
+static int brcm_send_ubus_event(char *ev_name, int line)
+{
+	struct blob_buf blob;
+	struct ubus_context *ubusctx;
+	int res = 0;
+
+	ubusctx = ubus_connect(NULL);
+	if (!ubusctx) {
+		return -1;
+	}
+
+	memset(&blob, 0, sizeof(blob));
+	if(blob_buf_init(&blob, 0)) {
+		ubus_free(ubusctx);
+		return -1;
+	}
+
+	blobmsg_add_string(&blob, "event", ev_name);
+	blobmsg_add_u32(&blob, "data", 0);
+	blobmsg_add_u32(&blob, "line", line);
+
+	if (ubus_send_event(ubusctx, broadcast_path, blob.head) != UBUS_STATUS_OK) {
+		ast_log(LOG_NOTICE,"Error sending ubus message %s\n", ev_name);
+		res = -1;
+	}
+
+	ubus_free(ubusctx);
+	blob_buf_free(&blob);
+
+	return res;
+}
+
 static void endpt_connection(int line, int id, char *action) {
 	struct blob_buf bb;
 	struct ubus_request *req;
@@ -809,13 +834,13 @@ static int brcm_call(struct ast_channel *chan, const char *dest, int timeout)
 		ast_debug(1, "tel_line %d disabled!\n", p->line_id);
 		ast_channel_hangupcause_set(chan, AST_CAUSE_CALL_REJECTED);
 		ast_queue_control(chan, AST_CONTROL_BUSY);
-                //send_ubus_event("CALL_REJECTED",p->line_id);
+		brcm_send_ubus_event("CALL_REJECTED",p->line_id);
 	}
 
 	else if (line_config[p->line_id].do_not_disturb) {
 		ast_debug(1, "Do not disturbed\n");
 		ast_channel_hangupcause_set(chan, AST_CAUSE_USER_BUSY);
-                //send_ubus_event("USER_BUSY",p->line_id);
+		brcm_send_ubus_event("USER_BUSY",p->line_id);
 		ast_queue_control(chan, AST_CONTROL_BUSY);
 	}
 	else if (brcm_in_call(p) &&                          // a call is established
@@ -835,10 +860,10 @@ static int brcm_call(struct ast_channel *chan, const char *dest, int timeout)
 		ast_debug(1, "Line is busy\n");
 		ast_channel_hangupcause_set(chan, AST_CAUSE_USER_BUSY);
 		ast_queue_control(chan, AST_CONTROL_BUSY);
-                /*When call comes but the sub_peer is not idle,move to old ONHOOK state*/
-                if(sub->channel_state == ALLOCATED)
-                    sub->channel_state = ONHOOK;
-		//send_ubus_event("USER_BUSY",p->line_id);
+		/*When call comes but the sub_peer is not idle,move to old ONHOOK state*/
+		if(sub->channel_state == ALLOCATED)
+			sub->channel_state = ONHOOK;
+		brcm_send_ubus_event("USER_BUSY",p->line_id);
 	}
 	else {
 		ast_debug(1, "Not call waiting\n");
@@ -851,7 +876,7 @@ static int brcm_call(struct ast_channel *chan, const char *dest, int timeout)
 		}
 	  	ast_setstate(chan, AST_STATE_RINGING);
 		ast_queue_control(chan, AST_CONTROL_RINGING);
-		//send_ubus_event("RINGING",p->line_id);
+		brcm_send_ubus_event("RINGING",p->line_id);
 	}
 	//ast_mutex_unlock(&sub->parent->lock);
 	pvt_unlock(sub->parent);
@@ -859,32 +884,6 @@ static int brcm_call(struct ast_channel *chan, const char *dest, int timeout)
 	return 0;
 }
 
-int send_ubus_event(char *ev_name,int line)
-{
-       struct blob_buf blob;
-       static struct ubus_context *ubusctx;
-        int res = 0;
-        memset(&blob, 0, sizeof(blob));
-        ubusctx = ubus_connect(NULL);
-        if (!ubusctx) {
-                return -1;
-        }
-        if(blob_buf_init(&blob, 0)) return -1;
-        blobmsg_add_string(&blob, "event", ev_name);
-        blobmsg_add_u32(&blob, "data", 0);
-        blobmsg_add_u32(&blob, "line", line);
-
-        if(ubus_send_event(ubusctx, broadcast_path, blob.head) != UBUS_STATUS_OK) {
-                ast_log(LOG_NOTICE,"Error sending ubus message %s\n", ev_name);
-                res = -1;
-        }
-
-        blob_buf_free(&blob);
-        ubus_free(ubusctx);
-        return res;
-
-}
-
 static int brcm_hangup(struct ast_channel *ast)
 {
 	struct brcm_pvt *p;
@@ -977,7 +976,9 @@ static int brcm_hangup(struct ast_channel *ast)
                else
 		    sub->channel_state = CALLENDED;
 	}
-        //send_ubus_event("HANGUP",p->line_id);
+
+	brcm_send_ubus_event("HANGUP",p->line_id);
+
 	if ( sub->conference_initiator && brcm_in_conference(p)) {
 		/* Switch still active call leg out of conference mode */
 		brcm_stop_conference(sub);
@@ -1054,7 +1055,8 @@ static int brcm_answer(struct ast_channel *ast)
             if(sub->channel_state != CALLENDED)/*Dont change the channel state if call is terminated */
 		sub->channel_state = INCALL;
 	}
-        //send_ubus_event("RINGING_OFF",pvt->line_id);
+
+	brcm_send_ubus_event("RINGING_OFF",pvt->line_id);
 	endpt_signal(pvt->line_id, "ringback", "off", NULL);
 
 	pvt_unlock(pvt);
@@ -1062,34 +1064,6 @@ static int brcm_answer(struct ast_channel *ast)
 	return 0;
 }
 
-
-// Hangup ALL brcm lines currently ringing with busy casue.
-static int brcm_busy_all_ringers(void)
-{
-	struct brcm_pvt *pvt;
-	int i;
-
-	brcm_lock_pvts();
-	pvt = iflist;
-
-	while(pvt) {	
-		for (i=0; i<NUM_SUBCHANNELS; i++) {
-			if (pvt->sub[i] && pvt->sub[i]->owner && pvt->sub[i]->channel_state == RINGING) {
-				ast_debug(4, "Hangup BUSY on %s\n", ast_channel_name(pvt->sub[i]->owner));
-				ast_queue_control(pvt->sub[i]->owner, AST_CONTROL_BUSY);
-				ast_queue_hangup_with_cause(pvt->sub[i]->owner, AST_CAUSE_USER_BUSY);
-			}
-		}
-
-		pvt = brcm_get_next_pvt(pvt);
-	}
-
-	brcm_unlock_pvts();
-
-	return 0;
-}
-
-
 /*
 * Map RTP data header value to a codec name
 */
@@ -1311,14 +1285,8 @@ int brcm_stop_dialtone(struct brcm_pvt *p) {
 	endpt_signal(p->line_id, "stutter", "off", NULL);
 	p->dialtone = DIALTONE_OFF;
 	brcm_signal_dialtone(p);
-        //send_ubus_event("DIALTONE_OFF",p->line_id);
-        return 0;
-}
-
-/* Tell endpoint to play country specific congestion tone */
-int brcm_signal_congestion(struct brcm_pvt *p) {
-	endpt_signal(p->line_id, "stutter", "on", NULL);
 
+	brcm_send_ubus_event("DIALTONE_OFF",p->line_id);
 	return 0;
 }
 
@@ -1464,8 +1432,7 @@ static struct brcm_subchannel *brcm_get_onhold_subchannel(const struct brcm_pvt
 static int setup_conference_call_cb( const void *data)
 {
     struct brcm_subchannel *sub_peer;
-    struct ast_channel *peer_owner;
-    struct ast_channel *owner = NULL;
+    struct ast_channel *peer_owner = NULL;
     
     ast_log(LOG_NOTICE ,"Set up a conference call\n");
     struct brcm_pvt *p = (struct brcm_pvt *) data;
@@ -1491,8 +1458,8 @@ static int setup_conference_call_cb( const void *data)
          sub_peer->conf_timer_id = -1;
     }
 
-    // send_ubus_event("UNHOLD",sub->parent->line_id);
-    // send_ubus_event("CONFERENCE_START",sub->parent->line_id);
+    brcm_send_ubus_event("UNHOLD",sub->parent->line_id);
+    brcm_send_ubus_event("CONFERENCE_START",sub->parent->line_id);
     if (peer_owner) {
         ast_channel_unref(peer_owner);
     }
@@ -1612,7 +1579,7 @@ static void brcm_start_calling(struct brcm_pvt *p, struct brcm_subchannel *sub,
 	struct ast_channel *c = (struct ast_channel *)sub->parent;
 
 	sub->channel_state = CALLING;
-	strncpy(p->ext, p->dtmfbuf, sizeof(p->dtmfbuf));
+	strncpy(p->ext, p->dtmfbuf, sizeof(p->ext));
 	ast_channel_exten_set(c, p->ext);
 	ast_channel_context_set(c, context);
 	ast_debug(1, "Calling %s@%s\n", ast_channel_exten(c), ast_channel_context(c));
@@ -1672,25 +1639,6 @@ static int handle_interdigit_timeout(const void *data)
 	return 0;
 }
 
-/*
- * Reset hook flash state after an interdigit timeout.
- * Called on scheduler thread.
- */
-static int handle_hookflash_timeout(const void *data)
-{
-	ast_debug(9, "Hook flash timeout, clear hook flash\n");
-	struct brcm_pvt *p = (struct brcm_pvt *) data;
-
-	//ast_mutex_lock(&p->lock);
-	pvt_lock(p, "hookflash callback");
-	p->interdigit_timer_id = -1;
-	p->hf_detected = 0;
-	//ast_mutex_unlock(&p->lock);
-	pvt_unlock(p);
-
-	return 0;
-}
-
 /*
  * Start autodialing if we have an autodial extension.
  * Called on scheduler thread.
@@ -1769,7 +1717,6 @@ static int handle_dialtone_timeout(const void *data)
 void handle_dtmf_calling(struct brcm_subchannel *sub)
 {
 	struct brcm_pvt *p = sub->parent;
-	char lastDigit;
 	char termination_digit;
 	int dtmfbuf_len = strlen(p->dtmfbuf);
 	char dtmf_last_char = p->dtmfbuf[(dtmfbuf_len - 1)];
@@ -1891,15 +1838,16 @@ static void handle_hookflash(struct brcm_subchannel *sub, struct brcm_subchannel
 			brcm_reset_dtmf_buffer(p);
 			p->hf_detected = 0;
 
-                       /* Put current call on hold */
+			/* Put current call on hold */
 			if (owner) {
-                            if(bridge){
-                                AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
-                                    ast_indicate(bridge_channel->chan, AST_CONTROL_HOLD);
-                                }
-                               }
+				if(bridge){
+					AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
+						ast_indicate(bridge_channel->chan, AST_CONTROL_HOLD);
+					}
+				}
+
+				brcm_send_ubus_event("CALL_HOLD",p->line_id);
 
-                                //send_ubus_event("CALL_HOLD",p->line_id);
 				brcm_mute_connection(sub);
 				sub->channel_state = ONHOLD;
 				ast_queue_hold(owner, NULL);
@@ -1935,13 +1883,13 @@ static void handle_hookflash(struct brcm_subchannel *sub, struct brcm_subchannel
 
 			/* Pick up old */
 			if (peer_owner) {
-                            if(bridge){
-                                AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
-                                    ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
-                                    ast_log(LOG_NOTICE,"blogs : pick up old unhold \n");
+				if(bridge){
+					AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
+						ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
+						ast_log(LOG_NOTICE,"blogs : pick up old unhold \n");
+					}
 				}
-                            }
-                                //send_ubus_event("CALL_UNHOLD",p->line_id);
+				brcm_send_ubus_event("CALL_UNHOLD",p->line_id);
 				brcm_unmute_connection(sub_peer);
 				ast_queue_unhold(peer_owner);
 				sub_peer->channel_state = INCALL;
@@ -1969,34 +1917,33 @@ static void handle_hookflash(struct brcm_subchannel *sub, struct brcm_subchannel
 			if (peer_owner) {
 				brcm_unmute_connection(sub_peer);
 				ast_queue_unhold(peer_owner);
-                                //send_ubus_event("CALL_UNHOLD",p->line_id);
-				sub_peer->channel_state = INCALL;
-			}
-		}
-                else if (sub->channel_state == INCALL && (sub_peer->channel_state == CALLWAITING || sub_peer->channel_state == ONHOLD)) {
-		    brcm_mute_connection(sub);
-		    if (sub_peer->channel_state == CALLWAITING) {
-			if (owner) {
-			   ast_queue_hold(owner, NULL);
-		        }
-			ast_log(LOG_NOTICE, " R in Call waiting\n");
-			/* Stop call waiting tone on current call */
-			brcm_stop_callwaiting(p);
-			/* Cancel timer */
-			if (ast_sched_del(sched, sub_peer->cw_timer_id)) {
-				ast_log(LOG_WARNING, "Failed to remove scheduled call waiting timer\n");
-			}
-			sub_peer->cw_timer_id = -1;
-			/* Pick up call waiting */
-			if (!sub_peer->connection_init) {
-				ast_debug(9, "create_connection()\n");
-				brcm_create_connection(sub_peer);
-			}
-			if (peer_owner) {
-				ast_queue_control(peer_owner, AST_CONTROL_ANSWER);
+				brcm_send_ubus_event("CALL_UNHOLD",p->line_id);
 				sub_peer->channel_state = INCALL;
-                                sub->channel_state = ONHOLD;
 			}
+		} else if (sub->channel_state == INCALL && (sub_peer->channel_state == CALLWAITING || sub_peer->channel_state == ONHOLD)) {
+			brcm_mute_connection(sub);
+			if (sub_peer->channel_state == CALLWAITING) {
+				if (owner) {
+					ast_queue_hold(owner, NULL);
+				}
+				ast_log(LOG_NOTICE, " R in Call waiting\n");
+				/* Stop call waiting tone on current call */
+				brcm_stop_callwaiting(p);
+				/* Cancel timer */
+				if (ast_sched_del(sched, sub_peer->cw_timer_id)) {
+					ast_log(LOG_WARNING, "Failed to remove scheduled call waiting timer\n");
+				}
+				sub_peer->cw_timer_id = -1;
+				/* Pick up call waiting */
+				if (!sub_peer->connection_init) {
+					ast_debug(9, "create_connection()\n");
+					brcm_create_connection(sub_peer);
+				}
+				if (peer_owner) {
+					ast_queue_control(peer_owner, AST_CONTROL_ANSWER);
+					sub_peer->channel_state = INCALL;
+									sub->channel_state = ONHOLD;
+				}
 		    } else if (sub_peer->channel_state == ONHOLD) {
                         ast_log(LOG_NOTICE, "R on hold \n");
                         sub->channel_state = INCALL;
@@ -2471,7 +2418,7 @@ static void *brcm_process_event(struct endpt_event *ev) {
 
 				sub->channel_state = INCALL;
 				ast_queue_control(owner, AST_CONTROL_ANSWER);
-                                //send_ubus_event("ANSWERED_CALL",p->line_id);
+				brcm_send_ubus_event("ANSWERED_CALL",p->line_id);
 
 				//TODO Ronny: ast_channel_queue_connected_line_update() ?
 			}
@@ -2497,7 +2444,7 @@ static void *brcm_process_event(struct endpt_event *ev) {
 					ast_debug(9, "Resetting dial tones.\n");
 					p->dialtone = voicemail_messages_waiting(p->context) ? mwi_dialtone_state : DIALTONE_ON;
 					brcm_signal_dialtone(p);
-                                        //send_ubus_event("DIALTONE_ON",p->line_id);
+					brcm_send_ubus_event("DIALTONE_ON",p->line_id);
 					line_settings *s = &line_config[p->line_id];
 					if (strlen(s->autodial_ext)) {
 						/* Schedule autodial timeout if autodial extension is set */
@@ -3674,7 +3621,6 @@ static void ubus_call_answer_rtp_stats(struct ubus_request *req, int type, struc
 static int endpt_get_rtp_stats(int line) {
 	struct ubus_context *local_ctx;
 	struct blob_buf bb;
-	struct ubus_request *req;
 	int ret;
 	struct brcm_pvt *p = NULL;
 	struct brcm_subchannel *sub = NULL;
@@ -3961,14 +3907,14 @@ static int load_module(void)
 static int brcm_signal_callwaiting(const struct brcm_pvt *p)
 {
 	endpt_signal(p->line_id, "callwt", "on", NULL);
-        //send_ubus_event("CALLWAITING",p->line_id);
+	brcm_send_ubus_event("CALLWAITING",p->line_id);
 	return 0;
 }
 
 static int brcm_stop_callwaiting(const struct brcm_pvt *p)
 {
 	endpt_signal(p->line_id, "callwt", "off", NULL);
-        //send_ubus_event("CALLWAITING_STOPPED",p->line_id);
+	brcm_send_ubus_event("CALLWAITING_STOPPED",p->line_id);
 	return 0;
 }
 
@@ -4189,7 +4135,7 @@ static int brcm_stop_conference(struct brcm_subchannel *p)
 		}
 		ao2_ref(confBridge, -1);
 	}
-        //send_ubus_event("CONFERENCE_STOPPED",pvt->line_id);
+	brcm_send_ubus_event("CONFERENCE_STOPPED",pvt->line_id);
 	return 0;
 }
 
diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c
index df37fa5789..dd70ef4aab 100644
--- a/res/res_pjsip_outbound_registration.c
+++ b/res/res_pjsip_outbound_registration.c
@@ -1107,31 +1107,37 @@ static void clear_endpoint_security_mechanisms(struct ast_sip_endpoint *endpoint
 	}
 }
 
-int send_ubus_event(char *ev_name,int time ,char *client)
+static int sip_outbound_registration_send_ubus_event(char *ev_name,int time ,char *client)
 {
-        struct blob_buf blob;
-        int res = 0;
-        struct ubus_context *ubusContext = NULL;
-        ubusContext = ubus_connect(NULL);
-        if(!ubusContext){
-               ast_log(LOG_ERROR, "Failed to connect to ubus.");
-               return -1;
-        }
-        memset(&blob, 0, sizeof(blob));
-        if(blob_buf_init(&blob, 0)) return -1;
-        blobmsg_add_string(&blob, "event", ev_name);
-        blobmsg_add_u32(&blob, "expiration", time);
-        blobmsg_add_string(&blob, "line", client);
+	struct blob_buf blob;
+	int res = 0;
+	struct ubus_context *ubusContext = NULL;
+
+	ubusContext = ubus_connect(NULL);
+	if(!ubusContext){
+		ast_log(LOG_ERROR, "Failed to connect to ubus.");
+		return -1;
+	}
 
-        if(ubus_send_event(ubusContext, broadcast_path, blob.head) != UBUS_STATUS_OK) {
-                ast_log(LOG_NOTICE,"Error sending ubus message %s\n", ev_name);
-                res = -1;
-        }
+	memset(&blob, 0, sizeof(blob));
+	if(blob_buf_init(&blob, 0)) {
+		ubus_free(ubusContext);
+		return -1;
+	}
 
-        blob_buf_free(&blob);
-        ubus_free(ubusContext);
-        return res;
+	blobmsg_add_string(&blob, "event", ev_name);
+	blobmsg_add_u32(&blob, "expiration", time);
+	blobmsg_add_string(&blob, "line", client);
 
+	if(ubus_send_event(ubusContext, broadcast_path, blob.head) != UBUS_STATUS_OK) {
+		ast_log(LOG_NOTICE,"Error sending ubus message %s\n", ev_name);
+		res = -1;
+	}
+
+	ubus_free(ubusContext);
+	blob_buf_free(&blob);
+
+	return res;
 }
 
 
@@ -1259,7 +1265,7 @@ static int handle_registration_response(void *data)
 			/* If the registration went fine simply reschedule registration for the future */
 			ast_debug(1, "Outbound registration to '%s' with client '%s' successful\n", server_uri, client_uri);
 			update_client_state_status(response->client_state, SIP_REGISTRATION_REGISTERED);
-                        send_ubus_event("REGISTERED",response->expiration,client_uri);
+			sip_outbound_registration_send_ubus_event("REGISTERED",response->expiration,client_uri);
 			response->client_state->retries = 0;
 			next_registration_round = response->expiration - REREGISTER_BUFFER_TIME;
 			if (next_registration_round < 0) {
@@ -1275,7 +1281,7 @@ static int handle_registration_response(void *data)
 			ast_debug(1, "Outbound unregistration to '%s' with client '%s' successful\n", server_uri, client_uri);
 			response->client_state->is494=0;
 			update_client_state_status(response->client_state, SIP_REGISTRATION_UNREGISTERED);
-                        send_ubus_event("UNREGISTERED",response->expiration,client_uri);
+			sip_outbound_registration_send_ubus_event("UNREGISTERED",response->expiration,client_uri);
 			ast_sip_transport_monitor_unregister(response->rdata->tp_info.transport,
 				registration_transport_shutdown_cb, response->client_state->registration_name,
 				monitor_matcher);
diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c
index d7a68b5cfa..06183b0f27 100644
--- a/res/res_pjsip_pubsub.c
+++ b/res/res_pjsip_pubsub.c
@@ -570,31 +570,38 @@ static void ubus_init(void) {
                ast_log(LOG_ERROR, "Failed to connect to ubus.");
 }
 
-int send_ubus_event(char *ev_name,char *endpt,char *messagewaiting,int new_message,int old_message)
-{
-        struct blob_buf blob;
-        static struct ubus_context *ubusctx;
-        int res = 0;
-        memset(&blob, 0, sizeof(blob));
-
-        ubusctx = ubus_connect(NULL);
-        if (!ubusctx) {
-                return -1;
-        }
-        if(blob_buf_init(&blob, 0)) return -1;
-        blobmsg_add_string(&blob, "event", ev_name);
-        blobmsg_add_string(&blob, "endpoint", endpt);
-        blobmsg_add_string(&blob, "message waiting", messagewaiting);
-        blobmsg_add_u32(&blob, "new message", new_message);
-        blobmsg_add_u32(&blob, "old message", old_message);
-        if(ubus_send_event(ubusctx, broadcast_path, blob.head) != UBUS_STATUS_OK) {
-                ast_log(LOG_NOTICE,"Error sending ubus message %s\n", ev_name);
-                res = -1;
-        }
-
-        blob_buf_free(&blob);
-        ubus_free(ubusctx);
-        return res;
+static int sip_pubsub_send_ubus_event(char *ev_name,char *endpt,char *messagewaiting,int new_message,int old_message)
+{
+	struct blob_buf blob;
+	struct ubus_context *ubusctx;
+	int res = 0;
+
+	ubusctx = ubus_connect(NULL);
+	if (!ubusctx) {
+		return -1;
+	}
+
+	memset(&blob, 0, sizeof(blob));
+	if(blob_buf_init(&blob, 0)) {
+		ubus_free(ubusctx);
+		return -1;
+	}
+
+	blobmsg_add_string(&blob, "event", ev_name);
+	blobmsg_add_string(&blob, "endpoint", endpt);
+	blobmsg_add_string(&blob, "message waiting", messagewaiting);
+	blobmsg_add_u32(&blob, "new message", new_message);
+	blobmsg_add_u32(&blob, "old message", old_message);
+
+	if(ubus_send_event(ubusctx, broadcast_path, blob.head) != UBUS_STATUS_OK) {
+		ast_log(LOG_NOTICE,"Error sending ubus message %s\n", ev_name);
+		res = -1;
+	}
+
+	ubus_free(ubusctx);
+	blob_buf_free(&blob);
+
+	return res;
 }
 
 
@@ -3770,7 +3777,7 @@ static pj_bool_t pubsub_on_rx_mwi_notify_request(pjsip_rx_data *rdata)
 		if (!ast_strlen_zero(endpoint->incoming_mwi_mailbox) && !ast_strlen_zero(summary.messages_waiting)) {
 			send_mwi_event("PJSIP", endpoint_name, endpoint->incoming_mwi_mailbox, summary.messages_waiting,
 				summary.voice_messages_new, summary.voice_messages_old);
-                        send_ubus_event("NOTIFY",endpoint_name,summary.messages_waiting,summary.voice_messages_new,summary.voice_messages_old);
+			sip_pubsub_send_ubus_event("NOTIFY",endpoint_name,summary.messages_waiting,summary.voice_messages_new,summary.voice_messages_old);
 			ast_string_field_set(endpoint, messages_waiting, summary.messages_waiting);
 		}
 
-- 
GitLab