aboutsummaryrefslogtreecommitdiffstats
path: root/src/ahl-policy.c
blob: fa050af5dc55713d3f7fb6f354366b4f7b59af0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2017 "Audiokinetic Inc"
 * Author Francois Thibault <fthibault@audiokinetic.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>

#include "ahl-binding.h"

// This file provides example of custom, business logic driven policy actions that can affect behavior of the high level
// TODO: Currently only isolated in separate source file. Objective is to make this to at least a shared lib plug-in (shared C context)
// Going a step further would be to implement this within a distinct policy binding (requires to switch to JSON interface)

extern AHLCtxT g_AHLCtx; // TODO: Cannot stay if moved to external module 

static void Add_Endpoint_Property_Numeric( EndpointInfoT * io_pEndpointInfo, char * in_pPropertyName, int in_iPropertyValue)
{
    json_object * propValueJ = json_object_new_int(in_iPropertyValue);
    g_hash_table_insert(io_pEndpointInfo->pPropTable, in_pPropertyName, propValueJ);
}

static void Add_Endpoint_Property_String( EndpointInfoT * io_pEndpointInfo, char * in_pPropertyName, char * in_pPropertyValue)
{
    json_object * propValueJ = json_object_new_string(in_pPropertyValue);
    g_hash_table_insert(io_pEndpointInfo->pPropTable, in_pPropertyName, propValueJ);
}

int Policy_OpenStream(char *pAudioRole, EndpointTypeT endpointType, endpointID_t endpointID)
{
    // TODO: Example rule -> when system is in shutdown or low power mode, no audio stream can be opened (return AHL_POLICY_REJECT)

    return AHL_POLICY_ACCEPT; 
}

int Policy_CloseStream(streamID_t streamID)
{
    // For completeness, unlikely to have any policy rules here

    return AHL_POLICY_ACCEPT; 
}

int  Policy_SetStreamState(streamID_t streamID, StreamStateT streamState )
{
    // If higher priority audio role stream requires audio ducking (and un-ducking) of other streams (e.g. navigation ducks entertainment)
    // TODO: Could potentially provide a fairly generic system using interupt behavior information and audio role priority (implemented and can be customized here)
    // Specific exception can also be

    // Source exclusion. E.g. When a source (e.g tuner) with same audio role as already active stream (e.g. media player) with same endpoint target, 
    // the former source is stopped (i.e. raise streamstate stop event)

    // If source on communication role is active (e.g. handsfree call), activating entertainment sources is prohibited

    // If handsfree or speech recognition (communication role) is started during entertainment playback, mute all entertainment streams (any endpoints except RSE)

    // Startup or Shutdown audio stream mute entertainment (unmut when stream no longer active)

    // Optional: Mute endpoint before activation, unmute afterwards (after a delay?) to avoid noises


    return AHL_POLICY_ACCEPT; 
}

int  Policy_SetStreamMute(streamID_t streamID, StreamMuteT streamMute)
{
    return AHL_POLICY_ACCEPT;
}

int Policy_SetVolume(EndpointTypeT endpointType, endpointID_t endpointID, char *volumeStr)
{
    return AHL_POLICY_ACCEPT; 
}

int Policy_SetProperty(EndpointTypeT endpointType, endpointID_t endpointID, char *propertyName, char *propValueStr)
{
    return AHL_POLICY_ACCEPT;
}

int Policy_PostEvent(char *eventName, char *audioRole, char *mediaName, void *audioContext)
{
    // TODO: Any event with media specified should trigger action on provided rendering services (e.g. Wwise binding, gstreamer file player wrapper, MPDC? simple ALSA player (aplay)?)

    // Example (when the policy is hooked to CAN events). Post audio playback events other than safety during reverse gear engaged declined

    // Example post HMI audio role playback events declined when higher priority streams are active

    return AHL_POLICY_ACCEPT;
}

int Policy_AudioDeviceChange()
{
    // Allow or disallow a new audio endpoint to be used by the system
    // TODO: Policy assigns audio role(s) for device (or default)
    // TODO: Raise events to engage device switching if active stream in audio role assigned to the new endpoint
    
    return AHL_POLICY_ACCEPT; 
}

int Policy_Endpoint_Property_Init(EndpointInfoT * io_EndpointInfo)
{
    // Populate list of supported properties for specified endpoint (use helper functions)
    // Setup initial values for all properties

    // TODO: Switch on different known endpoints to populate different properties

    // Test example
    Add_Endpoint_Property_Numeric(io_EndpointInfo,AHL_PROPERTY_EQ_LOW,3);
    Add_Endpoint_Property_Numeric(io_EndpointInfo,AHL_PROPERTY_EQ_MID,0);
    Add_Endpoint_Property_Numeric(io_EndpointInfo,AHL_PROPERTY_EQ_HIGH,6);
    Add_Endpoint_Property_Numeric(io_EndpointInfo,AHL_PROPERTY_BALANCE,0);
    Add_Endpoint_Property_Numeric(io_EndpointInfo,AHL_PROPERTY_FADE,30);
    Add_Endpoint_Property_String(io_EndpointInfo,"preset_name","flat");
    
    return 0; // No errors
}

int Policy_Init()
{
    // Other policy specific initialization
    return 0; // No errors
}

void Policy_Term()
{
    // Policy termination
}