Skip to content
Snippets Groups Projects
event.c 2.79 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * event.c: event handler
     *
     * Copyright (C) 2023 IOPSYS Software Solutions AB. All rights reserved.
     *
     * Author: Iryna Antsyferova <iryna.antsyferova@iopsys.eu>
     *
     * See LICENSE file for license related information.
     */
    #include "libdmtree/dmcommon.h"
    #include "event.h"
    #include <unistd.h>
    
    #define FIREWALL_RULE_DATA	"Device.Services.VoiceService.%d.X_RDK_Firewall_Rule_Data"
    #define FIREWALL_RULE_DATA_PATTERN "Device\\.Services\\.VoiceService\\.[0-9]+\\.X_RDK_Firewall_Rule_Data"
    
    int subs_event_handler(const json_object *jmsg, int param_count, json_object *jreply)
    {
    	int ret = RETURN_OK;
    	int param_index = 0;
    	hal_subscribe_event_request_t param_request;
    
    	if (jmsg == NULL || jreply == NULL) {
    		ERR("event handler: Invalid memory");
    		return RETURN_ERR;
    	}
    
    	memset(&param_request, 0, sizeof(param_request));
    
    	    for (param_index = 0; param_index < param_count; param_index++) {
    		    /* Unpack the request parameter */
    		    if (json_hal_get_subscribe_event_request((json_object *)jmsg,
    			    param_index, &param_request) != RETURN_OK) {
    			    INFO("Failed to get subscription data from the server");
    			    return RETURN_ERR;
    		    }
    
    
    Bogdan Bogush's avatar
    Bogdan Bogush committed
    		    if (!match(param_request.name, FIREWALL_RULE_DATA_PATTERN, 0, NULL)) {
    
    			    ret = RETURN_ERR;
    			    WARNING("Unexpected event [%s][%d]", param_request.name, (int)param_request.type);
    		    }
    		    else
    			    INFO("Subscribed [%s][%d]", param_request.name, (int)param_request.type);
    	}
    
    	ret = json_hal_add_result_status(jreply, ret == RETURN_OK ? RESULT_SUCCESS :
    		RESULT_FAILURE);
    
    	INFO("[%s] Replly msg = %s", __FUNCTION__,
    		json_object_to_json_string_ext(jreply, JSON_C_TO_STRING_PRETTY));
    
    	return ret;
    }
    
    /*  Expected format of rule : enable/disable,protocol,port,ip;
        Ex: 1,sip,5060,2a02:c69:fe02::4;
        1,sip,5060,2a02:c69:fe02::4;1,rtp,50000,2a02:c69:fe02::4;
        1,sip,5060,2a02:c69:fe02::4;0,rtp,50000,2a02:c69:fe02::4;1,rtp,50001,2a02:c69:fe02::4; */
    static void create_fw_rule_payload(char **buf, bool enable, char *protocol, int port, char *ip)
    {
    	dmasprintf(buf, "%d,%s,%d,%s;", enable, protocol, port, ip);
    }
    
    int send_fw_rule_event(bool enable, char *protocol, int port, char *ip, int voice_service_idx)
    {
    	int ret = 0;
    	char *fw_rule_path = NULL;
    	char *fw_rule = NULL;
    
    	create_fw_rule_payload(&fw_rule, enable, protocol, port, ip);
    	dmasprintf(&fw_rule_path, FIREWALL_RULE_DATA, voice_service_idx);
    
    	ret = (json_hal_server_publish_event(fw_rule_path, fw_rule) == RETURN_OK) ? 0 : -1;
    	dmfree(fw_rule_path);
    	dmfree(fw_rule);
    
    	return ret;
    }
    
    int send_fw_rules_clear_event(int voice_service_idx)
    {
    	int ret = 0;
        	char *fw_rule_path = NULL;
    	char fw_rule[] = {0};
    
    	dmasprintf(&fw_rule_path, FIREWALL_RULE_DATA, voice_service_idx);
    
    	ret = (json_hal_server_publish_event(fw_rule_path, fw_rule) == RETURN_OK) ? 0 : -1;
    	dmfree(fw_rule_path);
    
    	return ret;
    }