diff options
Diffstat (limited to 'ahl-policy/ahl-policy.c')
-rw-r--r-- | ahl-policy/ahl-policy.c | 572 |
1 files changed, 311 insertions, 261 deletions
diff --git a/ahl-policy/ahl-policy.c b/ahl-policy/ahl-policy.c index 5d2e324..4a34ed8 100644 --- a/ahl-policy/ahl-policy.c +++ b/ahl-policy/ahl-policy.c @@ -14,15 +14,38 @@ * limitations under the License. */ -#define _GNU_SOURCE #include <stdio.h> #include <string.h> #include <stdbool.h> +#define AFB_BINDING_VERSION 2 +#include <afb/afb-binding.h> +#include <glib.h> #include "wrap-json.h" #include "ahl-policy-utils.h" #include "ahl-interface.h" #include "ahl-policy.h" +#ifndef AHL_DISCONNECT_POLICY + +#define AK_POLICY_DEMO //For Audiokinetic demo only + +typedef struct StreamPolicyInfo { + streamID_t streamID; + int RolePriority; + char * pAudioRole; + InterruptBehaviorT interruptBehavior; + int iDuckVolume; //duck Volume +} StreamPolicyInfoT; + +typedef struct EndPointPolicyInfo { + endpointID_t endpointID; + EndpointTypeT type; + DeviceURITypeT deviceType; + char * pDeviceName; + char * pHalApiName; + int iVolume; //Current Volume + GArray * streamInfo; //List of playing or duck stream at a given endpoint +} EndPointPolicyInfoT; typedef enum SystemState { SYSTEM_STARTUP = 0, // Startup @@ -32,7 +55,6 @@ typedef enum SystemState { SYSTEM_MAXVALUE // Enum count, keep at the end } SystemStateT; - typedef struct HalInfo { char *pDevID; char *pAPIName; @@ -53,12 +75,9 @@ typedef struct PolicyLocalCtx { SystemStateT systemState; } PolicyLocalCtxT; -#ifndef AHL_DISCONNECT_POLICY - -// Global Context +// Policy context PolicyLocalCtxT g_PolicyCtx; - // Helper Functions static int getStreamConfig(char *pAudioRole, StreamConfigT *pStreamConfig) { @@ -120,7 +139,7 @@ static int getStreamConfig(char *pAudioRole, StreamConfigT *pStreamConfig) static int PolicySetVolume(int iEndpointID, int iEndpointType, char *pHalApiName, char *AudioRole, DeviceURITypeT deviceType, int iVolume, bool bMute) { - if(pHalApiName == NULL) + if(pHalApiName == NULL || (strcasecmp(pHalApiName, AHL_POLICY_UNDEFINED_HALNAME) == 0)) { AFB_WARNING("SetVolume cannot be accomplished without proper HAL association"); return POLICY_FAIL; @@ -156,15 +175,14 @@ static int PolicySetVolume(int iEndpointID, int iEndpointType, char *pHalApiName } break; default: - //Set volume to zero for display purpose only. - //Not support yet + // Not supported yet AFB_WARNING("Device Type %i is not support and can't set volume on HalName %s",deviceType, pHalApiName); return POLICY_FAIL; break; } // Set endpoint volume using HAL services (leveraging ramps etc.) - json_object *j_response, *j_query = NULL; + json_object *j_response = NULL, *j_query = NULL; // Package query int err = wrap_json_pack(&j_query,"{s:s,s:i}","label",gsHALControlName->str, "val",iVolume); @@ -174,7 +192,7 @@ static int PolicySetVolume(int iEndpointID, int iEndpointType, char *pHalApiName return POLICY_FAIL; } - //TODO implement Volume limitation based on policy + // TODO: implement Volume limitation based on policy // Set the volume using the HAL err = afb_service_call_sync(pHalApiName, "ctlset", j_query, &j_response); @@ -216,7 +234,7 @@ static int PolicyGetVolume(int iEndpointID, int iEndpointType, char *pHalApiName case DEVICEURITYPE_ALSA_PLUG: case DEVICEURITYPE_ALSA_SOFTVOL: gsHALControlName = g_string_new(AudioRole); - g_string_append(gsHALControlName,"_Vol"); // Or _Vol for direct control (no ramping) + g_string_append(gsHALControlName,"_Vol"); // Return target value break; default: // Set volume to zero for display purpose only. @@ -237,8 +255,6 @@ static int PolicyGetVolume(int iEndpointID, int iEndpointType, char *pHalApiName return POLICY_FAIL; } - //TODO implement Volume limitation based on policy - // Get the volume using the HAL err = afb_service_call_sync(pHalApiName, "ctlget", j_query, &j_response); if (err) @@ -262,8 +278,7 @@ static int PolicyGetVolume(int iEndpointID, int iEndpointType, char *pHalApiName if (err) { AFB_ERROR("Volume retrieve failed Could not retrieve volume value -> %s", json_object_get_string(jVal)); return POLICY_FAIL; - } - + } } else { @@ -272,7 +287,6 @@ static int PolicyGetVolume(int iEndpointID, int iEndpointType, char *pHalApiName AFB_ERROR("Volume retrieve failed Could not retrieve volume value -> %s", json_object_get_string(jVal)); return POLICY_FAIL; } - } *pVolume = val1; @@ -293,7 +307,6 @@ static int PolicyGetVolume(int iEndpointID, int iEndpointType, char *pHalApiName static void PolicyPostStateEvent(int iStreamID, StreamEventT eventState) { - json_object * eventDataJ = NULL; int err = wrap_json_pack(&eventDataJ,"{s:s,s:i,s:i}","event_name", AHL_STREAM_STATE_EVENT,"stream_id",iStreamID, "state_event",eventState); if (err) @@ -332,23 +345,22 @@ static EndPointPolicyInfoT *PolicySearchEndPoint(EndpointTypeT type, char *pDevi return NULL; } - -static int PolicyAddEndPoint(StreamInfoT *pStreamInfo) +static int PolicyAddEndPoint(StreamInterfaceInfoT *pStreamInfo) { - EndPointPolicyInfoT *pPolicyEndPoint = PolicySearchEndPoint(pStreamInfo->pEndpointInfo->type, pStreamInfo->pEndpointInfo->gsDeviceName); + EndPointPolicyInfoT *pPolicyEndPoint = PolicySearchEndPoint(pStreamInfo->endpoint.type, pStreamInfo->endpoint.gsDeviceName); if(pPolicyEndPoint == NULL) { //create EndPoint and add playing stream EndPointPolicyInfoT newEndPointPolicyInfo; - newEndPointPolicyInfo.endpointID = pStreamInfo->pEndpointInfo->endpointID; - newEndPointPolicyInfo.type = pStreamInfo->pEndpointInfo->type; - newEndPointPolicyInfo.deviceType = pStreamInfo->pEndpointInfo->deviceURIType; - newEndPointPolicyInfo.pDeviceName = strdup(pStreamInfo->pEndpointInfo->gsDeviceName); - newEndPointPolicyInfo.pHalApiName = strdup(pStreamInfo->pEndpointInfo->gsHALAPIName); - newEndPointPolicyInfo.iVolume = pStreamInfo->pEndpointInfo->iVolume; - newEndPointPolicyInfo.streamInfo = g_array_new(FALSE, TRUE, sizeof(StreamPolicyInfoT));; - - if(pStreamInfo->pEndpointInfo->type == ENDPOINTTYPE_SINK) + newEndPointPolicyInfo.endpointID = pStreamInfo->endpoint.endpointID; + newEndPointPolicyInfo.type = pStreamInfo->endpoint.type; + newEndPointPolicyInfo.deviceType = pStreamInfo->endpoint.deviceURIType; + newEndPointPolicyInfo.pDeviceName = strdup(pStreamInfo->endpoint.gsDeviceName); + newEndPointPolicyInfo.pHalApiName = strdup(pStreamInfo->endpoint.gsHALAPIName); + newEndPointPolicyInfo.iVolume = pStreamInfo->endpoint.iVolume; + newEndPointPolicyInfo.streamInfo = g_array_new(FALSE, TRUE, sizeof(StreamPolicyInfoT)); + + if(pStreamInfo->endpoint.type == ENDPOINTTYPE_SINK) { g_array_append_val(g_PolicyCtx.pSinkEndpoints, newEndPointPolicyInfo); } @@ -361,8 +373,7 @@ static int PolicyAddEndPoint(StreamInfoT *pStreamInfo) return POLICY_SUCCESS; } - -static int PolicyAddStream(EndPointPolicyInfoT *pCurrEndPointPolicy, StreamInfoT *pStreamInfo) +static int PolicyAddStream(EndPointPolicyInfoT *pCurrEndPointPolicy, StreamInterfaceInfoT *pStreamInfo) { StreamPolicyInfoT newStreamPolicyInfo; @@ -375,16 +386,15 @@ static int PolicyAddStream(EndPointPolicyInfoT *pCurrEndPointPolicy, StreamInfoT return POLICY_SUCCESS; } -static int PolicyRunningIdleTransition(EndPointPolicyInfoT *pCurrEndPointPolicy,StreamInfoT * pStreamInfo) +static int PolicyRunningIdleTransition(EndPointPolicyInfoT *pCurrEndPointPolicy,StreamInterfaceInfoT * pStreamInfo) { int err=0; if(pCurrEndPointPolicy == NULL || pCurrEndPointPolicy->streamInfo->len == 0) { - //Remove endpoint - AFB_ERROR("StreamID not found in active Endpoint when Running to Idle transition is request"); + AFB_ERROR("StreamID not found in active endpoint when running to idle transition is requested"); return POLICY_FAIL; } - //Search for the matching stream + // Search for the matching stream for(int i=0; i<pCurrEndPointPolicy->streamInfo->len; i++) { StreamPolicyInfoT currentPolicyStreamInfo = g_array_index(pCurrEndPointPolicy->streamInfo,StreamPolicyInfoT,i); @@ -400,7 +410,7 @@ static int PolicyRunningIdleTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, { case INTERRUPTBEHAVIOR_CONTINUE: //unduck and set Volume back to original value - err= PolicySetVolume(pCurrEndPointPolicy->endpointID, + err = PolicySetVolume(pCurrEndPointPolicy->endpointID, pCurrEndPointPolicy->type, pCurrEndPointPolicy->pHalApiName, HighPriorityStreamInfo.pAudioRole, @@ -413,21 +423,15 @@ static int PolicyRunningIdleTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, } return POLICY_SUCCESS; - break; case INTERRUPTBEHAVIOR_PAUSE: - //pInterruptStreamInfo->streamState = STREAM_STATE_RUNNING; PolicyPostStateEvent(HighPriorityStreamInfo.streamID,STREAM_EVENT_RESUME); return POLICY_SUCCESS; - break; - case INTERRUPTBEHAVIOR_CANCEL: PolicyPostStateEvent(HighPriorityStreamInfo.streamID,STREAM_EVENT_START); return POLICY_SUCCESS; - break; default: AFB_ERROR("Unsupported Intterupt Behavior"); return POLICY_FAIL; - break; } } } @@ -435,7 +439,7 @@ static int PolicyRunningIdleTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, return POLICY_SUCCESS; } -static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, StreamInfoT * pStreamInfo) +static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, StreamInterfaceInfoT * pStreamInfo) { int err=0; if(pCurrEndPointPolicy->streamInfo == NULL) @@ -459,7 +463,7 @@ static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, { case INTERRUPTBEHAVIOR_CONTINUE: //Save the current Volume and set the docking volume - pCurrentActiveStreamInfo->iDuckVolume = pStreamInfo->pEndpointInfo->iVolume; + pCurrentActiveStreamInfo->iDuckVolume = pStreamInfo->endpoint.iVolume; StreamConfigT StreamConfig; err = getStreamConfig(pStreamInfo->pRoleName, &StreamConfig); if(err == POLICY_FAIL) @@ -467,7 +471,7 @@ static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, AFB_ERROR("Error getting stream configuration for audiorole: %s", pStreamInfo->pRoleName); return POLICY_FAIL; } - err= PolicySetVolume(pCurrEndPointPolicy->endpointID, + err = PolicySetVolume(pCurrEndPointPolicy->endpointID, pCurrEndPointPolicy->type, pCurrEndPointPolicy->pHalApiName, pCurrentActiveStreamInfo->pAudioRole, @@ -483,7 +487,6 @@ static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, case INTERRUPTBEHAVIOR_PAUSE: PolicyPostStateEvent(pCurrentActiveStreamInfo->streamID,STREAM_EVENT_PAUSE); break; - case INTERRUPTBEHAVIOR_CANCEL: PolicyPostStateEvent(pCurrentActiveStreamInfo->streamID,STREAM_EVENT_STOP); g_array_remove_index(pCurrEndPointPolicy->streamInfo, pCurrEndPointPolicy->streamInfo->len-1); @@ -491,8 +494,6 @@ static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, default: AFB_ERROR("Unsupported Intterupt Behavior"); return AHL_POLICY_REJECT; - break; - } //Add the playing stream at last @@ -503,9 +504,7 @@ static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, //Higher Priority Stream is playing AFB_ERROR("Higher Priority Stream is playing"); return POLICY_FAIL; - } - - + } } return POLICY_SUCCESS; @@ -513,7 +512,6 @@ static int PolicyIdleRunningTransition(EndPointPolicyInfoT *pCurrEndPointPolicy, static void PolicySpeedModify(int speed) { - for(int i=0; i<g_PolicyCtx.pSinkEndpoints->len; i++) { EndPointPolicyInfoT * pCurEndpoint = &g_array_index(g_PolicyCtx.pSinkEndpoints,EndPointPolicyInfoT,i); @@ -545,8 +543,11 @@ static void PolicySpeedModify(int speed) } } -static int RetrieveAssociatedHALAPIName(EndpointInfoT * io_pEndpointInfo) +static int RetrieveAssociatedHALAPIName(int iAlsaCardNumber,char ** out_pDisplayName,char ** out_pHALName) { + *out_pDisplayName = NULL; + *out_pHALName = NULL; + if(g_PolicyCtx.pHALList) { for(int i=0; i<g_PolicyCtx.pHALList->len; i++) @@ -554,25 +555,21 @@ static int RetrieveAssociatedHALAPIName(EndpointInfoT * io_pEndpointInfo) HalInfoT *pHalInfo = g_ptr_array_index(g_PolicyCtx.pHALList, i); // Retrieve card number (e.g. hw:0) int iCardNum = atoi(pHalInfo->pDevID+3); - if (iCardNum == io_pEndpointInfo->alsaInfo.cardNum) { - io_pEndpointInfo->gsHALAPIName=strdup(pHalInfo->pAPIName); - io_pEndpointInfo->gsDisplayName=strdup(pHalInfo->pDisplayName); + if (iCardNum == iAlsaCardNumber) { + *out_pDisplayName = pHalInfo->pDisplayName; + *out_pHALName = pHalInfo->pAPIName; return POLICY_SUCCESS; } } } - io_pEndpointInfo->gsHALAPIName=strdup(AHL_POLICY_UNDEFINED_HALNAME); - io_pEndpointInfo->gsDisplayName=strdup(AHL_POLICY_UNDEFINED_DISPLAYNAME); - return POLICY_FAIL; } static int GetHALList(void) { - json_object *j_response, *j_query = NULL; - int err; - err = afb_service_call_sync("alsacore", "hallist", j_query, &j_response); + json_object *j_response = NULL, *j_query = NULL; + int err = afb_service_call_sync("alsacore", "hallist", j_query, &j_response); if (err) { AFB_ERROR("Could not retrieve list of HAL from ALSA core"); return POLICY_FAIL; @@ -616,16 +613,14 @@ static int GetHALList(void) // // Policy API Functions // -int Policy_OpenStream(json_object *pPolicyStreamJ) +int Policy_OpenStream(json_object *pStreamJ) { - StreamInfoT PolicyStream; - EndpointInfoT EndpointInfo; - PolicyStream.pEndpointInfo =&EndpointInfo; + StreamInterfaceInfoT Stream; - int err = PolicyCtxJSONToStream(pPolicyStreamJ, &PolicyStream); + int err = JSONToStream(pStreamJ, &Stream); if(err == AHL_POLICY_UTIL_FAIL) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } // Example rule -> when system is in shutdown or low power mode, no audio stream can be opened (return AHL_POLICY_REJECT) @@ -636,26 +631,27 @@ int Policy_OpenStream(json_object *pPolicyStreamJ) } StreamConfigT StreamConfig; - err = getStreamConfig(PolicyStream.pRoleName, &StreamConfig); + err = getStreamConfig(Stream.pRoleName, &StreamConfig); if(err == POLICY_FAIL) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } - if(PolicyStream.pEndpointInfo->deviceURIType != DEVICEURITYPE_NOT_ALSA) { - err=PolicyGetVolume(PolicyStream.pEndpointInfo->endpointID, - PolicyStream.pEndpointInfo->type, - PolicyStream.pEndpointInfo->gsHALAPIName, - PolicyStream.pEndpointInfo->pRoleName, - PolicyStream.pEndpointInfo->deviceURIType, - &PolicyStream.pEndpointInfo->iVolume); + // Volume support only possible through ALSA + if(Stream.endpoint.deviceURIType != DEVICEURITYPE_NOT_ALSA) { + err = PolicyGetVolume(Stream.endpoint.endpointID, + Stream.endpoint.type, + Stream.endpoint.gsHALAPIName, + Stream.endpoint.pRoleName, + Stream.endpoint.deviceURIType, + &Stream.endpoint.iVolume); if(err == POLICY_FAIL) { return AHL_POLICY_REJECT; } } - err = PolicyAddEndPoint(&PolicyStream); + err = PolicyAddEndPoint(&Stream); if(err == POLICY_FAIL) { return AHL_POLICY_REJECT; @@ -663,291 +659,298 @@ int Policy_OpenStream(json_object *pPolicyStreamJ) return AHL_POLICY_ACCEPT; } -int Policy_CloseStream(json_object *pPolicyStreamJ) +int Policy_CloseStream(json_object *pStreamJ) { - //TODO remove Endpoint when there is no stream - StreamInfoT PolicyStream; - EndpointInfoT EndpointInfo; - PolicyStream.pEndpointInfo =&EndpointInfo; - int err = PolicyCtxJSONToStream(pPolicyStreamJ, &PolicyStream); + StreamInterfaceInfoT Stream; + int err = JSONToStream(pStreamJ, &Stream); if(err == AHL_POLICY_UTIL_FAIL) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } return AHL_POLICY_ACCEPT; } -int Policy_SetStreamState(json_object *pPolicyStreamJ) -{ - //TODO - // Optional: Mute endpoint before activation, unmute afterwards (after a delay?) to avoid noises - StreamInfoT PolicyStream; - EndpointInfoT EndpointInfo; - PolicyStream.pEndpointInfo =&EndpointInfo; - +int Policy_SetStreamState(json_object *pStreamJ) +{ + // Optional TODO: Could mute endpoint before activation, unmute afterwards (after a delay?) to avoid noises StreamStateT streamState = 0; - StreamInfoT * pPolicyStream = &PolicyStream; - int err = PolicyCtxJSONToStream(pPolicyStreamJ, pPolicyStream); + StreamInterfaceInfoT Stream; + + int err = JSONToStream(pStreamJ, &Stream); if(err == AHL_POLICY_UTIL_FAIL) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } - json_object *streamStateJ=NULL; + json_object *streamStateJ = NULL; - if(!json_object_object_get_ex(pPolicyStreamJ, "arg_stream_state", &streamStateJ)) + if(!json_object_object_get_ex(pStreamJ, "arg_stream_state", &streamStateJ)) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } streamState = (StreamStateT)json_object_get_int(streamStateJ); - //Change of state - if(pPolicyStream->streamState != streamState) + // Change of state + if(Stream.streamState != streamState) { //seach corresponding endpoint and gather information on it - EndPointPolicyInfoT *pCurrEndPointPolicy = PolicySearchEndPoint(pPolicyStream->pEndpointInfo->type , pPolicyStream->pEndpointInfo->gsDeviceName); + EndPointPolicyInfoT *pCurrEndPointPolicy = PolicySearchEndPoint(Stream.endpoint.type , Stream.endpoint.gsDeviceName); - switch(pPolicyStream->streamState) + switch(Stream.streamState) { case STREAM_STATE_IDLE: switch(streamState) { case STREAM_STATE_RUNNING: - err = PolicyIdleRunningTransition(pCurrEndPointPolicy, pPolicyStream); + err = PolicyIdleRunningTransition(pCurrEndPointPolicy, &Stream); if(err) { return AHL_POLICY_REJECT; } - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_START); + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_START); break; case STREAM_STATE_PAUSED: - err = PolicyIdleRunningTransition(pCurrEndPointPolicy, pPolicyStream); + err = PolicyIdleRunningTransition(pCurrEndPointPolicy, &Stream); if(err) { return AHL_POLICY_REJECT; } - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_PAUSE); + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_PAUSE); break; default: return AHL_POLICY_REJECT; - break; } break; case STREAM_STATE_RUNNING: switch(streamState) { case STREAM_STATE_IDLE: - err = PolicyRunningIdleTransition(pCurrEndPointPolicy, pPolicyStream); + err = PolicyRunningIdleTransition(pCurrEndPointPolicy, &Stream); if(err) { return AHL_POLICY_REJECT; } - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_STOP); + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_STOP); break; case STREAM_STATE_PAUSED: - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_PAUSE); + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_PAUSE); break; default: return AHL_POLICY_REJECT; - break; } break; case STREAM_STATE_PAUSED: switch(streamState) { case STREAM_STATE_IDLE: - err = PolicyRunningIdleTransition(pCurrEndPointPolicy, pPolicyStream); + err = PolicyRunningIdleTransition(pCurrEndPointPolicy, &Stream); if(err) { return AHL_POLICY_REJECT; } - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_STOP); + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_STOP); break; case STREAM_STATE_RUNNING: - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_RESUME); + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_RESUME); break; default: return AHL_POLICY_REJECT; - break; } break; default: return AHL_POLICY_REJECT; - break; } } return AHL_POLICY_ACCEPT; } -int Policy_SetStreamMute(json_object *pPolicyStreamJ) +int Policy_SetStreamMute(json_object *pStreamJ) { + // Note: stream mute currently implemented directly using ALSA volume. It should later be implemented with a distinct mute switch control instead StreamMuteT streamMute = 0; - StreamInfoT PolicyStream; - EndpointInfoT EndpointInfo; - PolicyStream.pEndpointInfo =&EndpointInfo; - StreamInfoT * pPolicyStream = &PolicyStream; + StreamInterfaceInfoT Stream; - int err = PolicyCtxJSONToStream(pPolicyStreamJ, pPolicyStream); + int err = JSONToStream(pStreamJ, &Stream); if(err == AHL_POLICY_UTIL_FAIL) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } json_object *streamMuteJ=NULL; - if(!json_object_object_get_ex(pPolicyStreamJ, "mute_state", &streamMuteJ)) + if(!json_object_object_get_ex(pStreamJ, "mute_state", &streamMuteJ)) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } streamMute = (StreamMuteT)json_object_get_int(streamMuteJ); - if(streamMute != pPolicyStream->streamMute) + if(streamMute != Stream.streamMute) { if(streamMute == STREAM_MUTED) { - - err= PolicySetVolume(pPolicyStream->pEndpointInfo->endpointID, - pPolicyStream->pEndpointInfo->type, - pPolicyStream->pEndpointInfo->gsHALAPIName, - pPolicyStream->pRoleName, - pPolicyStream->pEndpointInfo->deviceURIType, - 0, - true); + err = PolicySetVolume(Stream.endpoint.endpointID, + Stream.endpoint.type, + Stream.endpoint.gsHALAPIName, + Stream.pRoleName, + Stream.endpoint.deviceURIType, + 0, // mute volume + true); // no ramp and no volume event if(err) { - AFB_ERROR("StreamID:%i Set Volume return with errorcode%i",pPolicyStream->streamID, err); + AFB_ERROR("StreamID:%i Set Volume return with errorcode%i",Stream.streamID, err); return AHL_POLICY_REJECT; } - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_MUTED); + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_MUTED); } else { - err= PolicySetVolume(pPolicyStream->pEndpointInfo->endpointID, - pPolicyStream->pEndpointInfo->type, - pPolicyStream->pEndpointInfo->gsHALAPIName, - pPolicyStream->pRoleName, - pPolicyStream->pEndpointInfo->deviceURIType, - pPolicyStream->pEndpointInfo->iVolume, - true); + err = PolicySetVolume(Stream.endpoint.endpointID, + Stream.endpoint.type, + Stream.endpoint.gsHALAPIName, + Stream.pRoleName, + Stream.endpoint.deviceURIType, + Stream.endpoint.iVolume, // restore volume + true); // no ramp and no volume event if(err) { - AFB_ERROR("Endpoint:%i Set Volume return with errorcode%i",pPolicyStream->streamID, err); + AFB_ERROR("Endpoint:%i Set Volume return with errorcode%i",Stream.streamID, err); return AHL_POLICY_REJECT; } - PolicyPostStateEvent(pPolicyStream->streamID,STREAM_EVENT_UNMUTED); - + PolicyPostStateEvent(Stream.streamID,STREAM_EVENT_UNMUTED); } - - pPolicyStream->streamMute = streamMute; } return AHL_POLICY_ACCEPT; } -int Policy_SetVolume(json_object *pPolicyEndpointJ) +int Policy_SetVolume(json_object *pEndpointJ) { char *volumeStr = NULL; - EndpointInfoT EndpointInfo; + EndPointInterfaceInfoT Endpoint; - int err = PolicyCtxJSONToEndpoint(pPolicyEndpointJ, &EndpointInfo); + int err = JSONToEndpoint(pEndpointJ, &Endpoint); if(err == AHL_POLICY_UTIL_FAIL) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } - json_object *volumeJ=NULL; - - if(!json_object_object_get_ex(pPolicyEndpointJ, "arg_volume", &volumeJ)) + json_object *volumeJ = NULL; + if(!json_object_object_get_ex(pEndpointJ, "arg_volume", &volumeJ)) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } volumeStr = (char*)json_object_get_string(volumeJ); - // TODO: Parse volume string to support increment/absolute/percent notation (or delegate to action / policy layer to interpret) + // TODO: Parse volume string to support increment/absolute/percent notation int vol = atoi(volumeStr); - //Set the volume - err= PolicySetVolume(EndpointInfo.endpointID, - EndpointInfo.type, - EndpointInfo.gsHALAPIName, - EndpointInfo.pRoleName, - EndpointInfo.deviceURIType, - vol, - false); + // Set the volume + err = PolicySetVolume(Endpoint.endpointID, + Endpoint.type, + Endpoint.gsHALAPIName, + Endpoint.pRoleName, + Endpoint.deviceURIType, + vol, + false); // Volume ramp and send events if (err) { - AFB_ERROR("Set Volume return with errorcode%i", err); + AFB_ERROR("Set Volume return with errorcode %i", err); return AHL_POLICY_REJECT; } return AHL_POLICY_ACCEPT; } -int Policy_SetProperty(json_object *pPolicyEndpointJ) +int Policy_SetProperty(json_object *pEndpointJ) { - char *propertyName = NULL; - EndpointInfoT EndpointInfo; + EndPointInterfaceInfoT Endpoint; - int err = PolicyCtxJSONToEndpoint(pPolicyEndpointJ, &EndpointInfo); + int err = JSONToEndpoint(pEndpointJ, &Endpoint); if(err == AHL_POLICY_UTIL_FAIL) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } - json_object *propertyNameJ=NULL; - - if(!json_object_object_get_ex(pPolicyEndpointJ, "arg_property_name", &propertyNameJ)) + json_object *propertyNameJ = NULL; + if(!json_object_object_get_ex(pEndpointJ, "arg_property_name", &propertyNameJ)) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } propertyName = (char*)json_object_get_string(propertyNameJ); - json_object *propValueJ; - if(!json_object_object_get_ex(pPolicyEndpointJ, "arg_property_value", &propValueJ)) + json_object *propValueJ = NULL; + if(!json_object_object_get_ex(pEndpointJ, "arg_property_value", &propValueJ)) { - return AHL_POLICY_ACCEPT; + return AHL_POLICY_REJECT; } + json_type currentTypeJ = json_object_get_type(propValueJ); - gpointer *key_value=NULL; - - key_value = g_hash_table_lookup(EndpointInfo.pPropTable,propertyName); - if(key_value == NULL) + json_object *propArray = NULL; + if(!json_object_object_get_ex(pEndpointJ, "properties", &propArray)) { - AFB_ERROR("Can't find property %s, request will be rejected", propertyName); - return AHL_POLICY_REJECT; + return AHL_POLICY_REJECT; } - //Get JsonObjectype - json_type currentjType = json_object_get_type((json_object*)key_value); - json_type newjType = json_object_get_type(propValueJ); - - //Apply policy on set property if needed here - //Here we only validate that the type is the same - if(currentjType != newjType) + int iPropArrayLen = json_object_array_length(propArray); + int foundProperty = 0; + + for (int i = 0; i < iPropArrayLen; i++) { - AFB_ERROR("Property Value Type is wrong"); - return AHL_POLICY_REJECT; + // get the i-th object in medi_array + json_object *propElementJ = json_object_array_get_idx(propArray, i); + if(propElementJ) + { + json_object *propElementNameJ=NULL; + if(json_object_object_get_ex(propElementJ, "property_name", &propElementNameJ)) + { + char *propElementName = (char*)json_object_get_string(propElementNameJ); + if(strcasecmp(propElementName,propertyName)==0) + { + json_object *propElementValueJ=NULL; + if(!json_object_object_get_ex(propElementJ, "property_value", &propElementValueJ)) + { + json_type elementTypeJ = json_object_get_type(propElementValueJ); + + // Apply policy on set property if needed here + // Here we only validate that the type is the same + if(currentTypeJ != elementTypeJ) + { + AFB_ERROR("Property Value Type is wrong"); + return AHL_POLICY_REJECT; + } + foundProperty = 1; + break; + } + } + } + } } + if(foundProperty== 0) + { + AFB_ERROR("Can't find property %s, request will be rejected", propertyName); + return AHL_POLICY_REJECT; + } - //Create a new Json Object + // Create a new Json Object json_object *pEventDataJ = NULL; err = wrap_json_pack(&pEventDataJ,"{s:s,s:i,s:i,s:s,s:o,s:s}", "event_name", AHL_ENDPOINT_PROPERTY_EVENT, - "endpoint_id", EndpointInfo.endpointID, - "endpoint_type", EndpointInfo.type, + "endpoint_id", Endpoint.endpointID, + "endpoint_type", Endpoint.type, "property_name", propertyName, "value",propValueJ, - "audio_role", EndpointInfo.pRoleName); + "audio_role", Endpoint.pRoleName); if(err) { AFB_ERROR("Unable to pack property event"); return AHL_POLICY_REJECT; } - //Raise Event to update HLB + + // Raise Event to update HLB audiohlapi_raise_event(pEventDataJ); return AHL_POLICY_ACCEPT; @@ -978,94 +981,137 @@ int Policy_PostAction(json_object *pPolicyActionJ) AFB_ERROR("Unable to pack JSON endpoint, =%s", wrap_json_get_error_string(err)); return AHL_POLICY_REJECT; } + audiohlapi_raise_event(pEventDataJ); return AHL_POLICY_ACCEPT; } -int Policy_Endpoint_Init(json_object *pPolicyEndpointJ) +int Policy_Endpoint_Init(json_object *pInPolicyEndpointJ,json_object **pOutPolicyEndpointJ) { - EndpointInfoT EndpointInfo; - - int err = PolicyCtxJSONToEndpoint(pPolicyEndpointJ, &EndpointInfo); - if(err == AHL_POLICY_UTIL_FAIL) - { - return AHL_POLICY_REJECT; + endpointID_t endpointID = AHL_UNDEFINED; + EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE; + DeviceURITypeT deviceURIType = DEVICEURITYPE_MAXVALUE; + int iAllocString = 0; + char * pRoleName = NULL; + int iAlsaCardNumber = AHL_UNDEFINED; + char * pDeviceName = NULL; + int err = wrap_json_unpack(pInPolicyEndpointJ,"{s:i,s:i,s:i,s:s,s:i,s:s}", + "endpoint_id",&endpointID, + "endpoint_type",&endpointType, + "device_uri_type",&deviceURIType, + "audio_role",&pRoleName, + "alsa_cardNum", &iAlsaCardNumber, + "device_name", &pDeviceName ); + if (err) { + AFB_ERROR("Unable to unpack JSON endpoint, =%s", wrap_json_get_error_string(err)); + goto OnError; } - if (EndpointInfo.deviceURIType != DEVICEURITYPE_NOT_ALSA) { + StreamConfigT StreamConfig; + getStreamConfig(pRoleName, &StreamConfig); + + char * pDisplayName = NULL; + char * pHALName = NULL; + if (deviceURIType != DEVICEURITYPE_NOT_ALSA) { // Update Hal Name - err = RetrieveAssociatedHALAPIName(&EndpointInfo); + err = RetrieveAssociatedHALAPIName(iAlsaCardNumber,&pDisplayName,&pHALName); if (err) { - AFB_ERROR("HAL not found for Device %s", EndpointInfo.gsDeviceName); - return AHL_POLICY_REJECT; + AFB_WARNING("HAL not found for device %s", pDeviceName); + pDisplayName = g_strdup(AHL_POLICY_UNDEFINED_DISPLAYNAME); + pHALName = g_strdup(AHL_POLICY_UNDEFINED_HALNAME); + iAllocString = 1; } - - //Set Init Volume - StreamConfigT StreamConfig; - getStreamConfig(EndpointInfo.pRoleName, &StreamConfig); - err = PolicySetVolume(EndpointInfo.endpointID, - EndpointInfo.type, - EndpointInfo.gsHALAPIName, - EndpointInfo.pRoleName, - EndpointInfo.deviceURIType, - StreamConfig.iVolumeInit, - false); - if(err) { - return AHL_POLICY_REJECT; + else + { + // Set initial Volume + err = PolicySetVolume(endpointID, + endpointType, + pHALName, + pRoleName, + deviceURIType, + StreamConfig.iVolumeInit, + true); // Do not raise event and no volume ramp + if(err) { + goto OnError; + } } } - - // Test example - Add_Endpoint_Property_Int(&EndpointInfo,AHL_PROPERTY_EQ_LOW,3); - Add_Endpoint_Property_Int(&EndpointInfo,AHL_PROPERTY_EQ_MID,0); - Add_Endpoint_Property_Int(&EndpointInfo,AHL_PROPERTY_EQ_HIGH,6); - Add_Endpoint_Property_Int(&EndpointInfo,AHL_PROPERTY_BALANCE,0); - Add_Endpoint_Property_Int(&EndpointInfo,AHL_PROPERTY_FADE,30); - Add_Endpoint_Property_String(&EndpointInfo,"preset_name","flat"); - - - gpointer *key_value = g_hash_table_lookup(EndpointInfo.pPropTable,AHL_PROPERTY_BALANCE); - if(key_value == NULL) - { - AFB_ERROR("Can't find property %s, request will be rejected", AHL_PROPERTY_BALANCE); - return AHL_POLICY_REJECT; + else { + // Set display / HAL for non ALSA devices (default) + pDisplayName = g_strdup(AHL_POLICY_UNDEFINED_DISPLAYNAME); + pHALName = g_strdup(AHL_POLICY_UNDEFINED_HALNAME); + iAllocString = 1; } - //Create a new Json Object - json_object *pNewPolicyEndpointJ = NULL; - err = PolicyEndpointStructToJSON(&EndpointInfo, &pNewPolicyEndpointJ); - if (err == AHL_POLICY_UTIL_FAIL) - { - return AHL_POLICY_REJECT; - } - json_object *paramJ= json_object_new_string(AHL_ENDPOINT_INIT_EVENT); - json_object_object_add(pNewPolicyEndpointJ, "event_name", paramJ); + // Populate special device property (TODO: Should be obtained from HAL) + // if (strcasecmp(gsHALAPIName,"Device")==0) + // { + // Create json object for PropTable + json_object *pPropTableJ = json_object_new_array(); + Add_Endpoint_Property_Int(pPropTableJ,AHL_PROPERTY_EQ_LOW,3); + Add_Endpoint_Property_Int(pPropTableJ,AHL_PROPERTY_EQ_MID,0); + Add_Endpoint_Property_Int(pPropTableJ,AHL_PROPERTY_EQ_HIGH,6); + Add_Endpoint_Property_Int(pPropTableJ,AHL_PROPERTY_BALANCE,0); + Add_Endpoint_Property_Int(pPropTableJ,AHL_PROPERTY_FADE,30); + Add_Endpoint_Property_String(pPropTableJ,"preset_name","flat"); + // } + + err = wrap_json_pack(pOutPolicyEndpointJ,"{s:i,s:s,s:s,s:o}", + "init_volume",StreamConfig.iVolumeInit, + "display_name",pDisplayName, + "hal_name",pHALName, + "property_table",pPropTableJ + ); + if (err) { + AFB_ERROR("Unable to pack JSON endpoint, =%s", wrap_json_get_error_string(err)); + goto OnError; + } + + // TODO: Future policy binding to return request response with pOutPolicyEndpointJ (instead of output argument) + return AHL_POLICY_ACCEPT; // No errors - //Raise Event to update HLB - audiohlapi_raise_event(pNewPolicyEndpointJ); +OnError: + if (iAllocString) { + g_free(pDisplayName); + g_free(pHALName); + } + return AHL_POLICY_REJECT; - return AHL_POLICY_ACCEPT; // No errors } int Policy_Init() { + // Start fresh + memset(&g_PolicyCtx,0,sizeof(g_PolicyCtx)); + // Initialize Ressources - g_PolicyCtx.pSourceEndpoints =g_array_new(FALSE,TRUE,sizeof(EndPointPolicyInfoT)); + g_PolicyCtx.pSourceEndpoints = g_array_new(FALSE,TRUE,sizeof(EndPointPolicyInfoT)); g_PolicyCtx.pSinkEndpoints = g_array_new(FALSE,TRUE,sizeof(EndPointPolicyInfoT)); g_PolicyCtx.pHALList = g_ptr_array_new_with_free_func(g_free); - //Get HalList + //Require AlsaCore Dependency + int err = afb_daemon_require_api_v2(AHL_POLICY_ALSA_API,1) ; + if( err != 0 ) + { + AFB_ERROR("Audio Policy could not set dependency on alsacore API"); + return AHL_POLICY_REJECT; + } + + // Get HalList GetHALList(); - //Set System Normal for now, this should be set by an event - //TODO: Receive event from low level + // TODO: Register events from low level / HAL for dynamic changes + + // Set System Normal for now, this should be set by an event g_PolicyCtx.systemState = SYSTEM_NORMAL; - //register audio backend events - //This is to simulate can bus, only used for demo - json_object *queryurl, *responseJ, *eventsJ; - + + +#ifdef AK_POLICY_DEMO + // Register audio backend events (TODO: should instead do this with signal composer with appropriate dependency) + // This is to simulate can bus, only used for demo + json_object *queryurl = NULL, *responseJ = NULL, *eventsJ = NULL; eventsJ = json_object_new_array(); json_object_array_add(eventsJ, json_object_new_string("audiod_system_event")); queryurl = json_object_new_object(); @@ -1075,12 +1121,15 @@ int Policy_Init() AFB_ERROR("Fail subscribing to Audio Backend System events"); return AHL_POLICY_REJECT; } +#endif + + return AHL_POLICY_ACCEPT; } void Policy_Term() { - //Free Ressources + // Free Ressources if (g_PolicyCtx.pHALList) { g_ptr_array_free(g_PolicyCtx.pHALList,TRUE); g_PolicyCtx.pHALList = NULL; @@ -1106,6 +1155,7 @@ void Policy_Term() g_PolicyCtx.pSinkEndpoints = NULL; } +// For demo purpose only, should be listening to signal composer / CAN events instead void Policy_OnEvent(const char *evtname, json_object *eventJ) { AFB_DEBUG("Policy received event %s", evtname); @@ -1129,7 +1179,7 @@ void Policy_OnEvent(const char *evtname, json_object *eventJ) AFB_WARNING("Invalid arguments, Args not a valid json object query=%s", json_object_get_string(event_parameter)); return; } - //When speed change Modify volume on Endpoint where entertainment change + // When speed change Modify volume on Endpoint where entertainment change PolicySpeedModify(speed); } } |