1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.soundtrigger_middleware;
18 
19 import android.os.HidlMemoryUtil;
20 
21 import java.util.ArrayList;
22 
23 /**
24  * Utilities for maintaining data compatibility between different minor versions of soundtrigger@2.x
25  * HAL.
26  * Note that some of these conversion utilities are destructive, i.e. mutate their input (for the
27  * sake of simplifying code and reducing copies).
28  */
29 class Hw2CompatUtil {
convertSoundModel_2_1_to_2_0( android.hardware.soundtrigger.V2_1.ISoundTriggerHw.SoundModel soundModel)30     static android.hardware.soundtrigger.V2_0.ISoundTriggerHw.SoundModel convertSoundModel_2_1_to_2_0(
31             android.hardware.soundtrigger.V2_1.ISoundTriggerHw.SoundModel soundModel) {
32         android.hardware.soundtrigger.V2_0.ISoundTriggerHw.SoundModel model_2_0 = soundModel.header;
33         // Note: this mutates the input!
34         model_2_0.data = HidlMemoryUtil.hidlMemoryToByteList(soundModel.data);
35         return model_2_0;
36     }
37 
convertRecognitionEvent_2_0_to_2_1( android.hardware.soundtrigger.V2_0.ISoundTriggerHwCallback.RecognitionEvent event)38     static android.hardware.soundtrigger.V2_1.ISoundTriggerHwCallback.RecognitionEvent convertRecognitionEvent_2_0_to_2_1(
39             android.hardware.soundtrigger.V2_0.ISoundTriggerHwCallback.RecognitionEvent event) {
40         android.hardware.soundtrigger.V2_1.ISoundTriggerHwCallback.RecognitionEvent event_2_1 =
41                 new android.hardware.soundtrigger.V2_1.ISoundTriggerHwCallback.RecognitionEvent();
42         event_2_1.header = event;
43         event_2_1.data = HidlMemoryUtil.byteListToHidlMemory(event_2_1.header.data,
44                 "SoundTrigger RecognitionEvent");
45         // Note: this mutates the input!
46         event_2_1.header.data = new ArrayList<>();
47         return event_2_1;
48     }
49 
convertPhraseRecognitionEvent_2_0_to_2_1( android.hardware.soundtrigger.V2_0.ISoundTriggerHwCallback.PhraseRecognitionEvent event)50     static android.hardware.soundtrigger.V2_1.ISoundTriggerHwCallback.PhraseRecognitionEvent convertPhraseRecognitionEvent_2_0_to_2_1(
51             android.hardware.soundtrigger.V2_0.ISoundTriggerHwCallback.PhraseRecognitionEvent event) {
52         android.hardware.soundtrigger.V2_1.ISoundTriggerHwCallback.PhraseRecognitionEvent
53                 event_2_1 =
54                 new android.hardware.soundtrigger.V2_1.ISoundTriggerHwCallback.PhraseRecognitionEvent();
55         event_2_1.common = convertRecognitionEvent_2_0_to_2_1(event.common);
56         event_2_1.phraseExtras = event.phraseExtras;
57         return event_2_1;
58     }
59 
convertPhraseSoundModel_2_1_to_2_0( android.hardware.soundtrigger.V2_1.ISoundTriggerHw.PhraseSoundModel soundModel)60     static android.hardware.soundtrigger.V2_0.ISoundTriggerHw.PhraseSoundModel convertPhraseSoundModel_2_1_to_2_0(
61             android.hardware.soundtrigger.V2_1.ISoundTriggerHw.PhraseSoundModel soundModel) {
62         android.hardware.soundtrigger.V2_0.ISoundTriggerHw.PhraseSoundModel model_2_0 =
63                 new android.hardware.soundtrigger.V2_0.ISoundTriggerHw.PhraseSoundModel();
64         model_2_0.common = convertSoundModel_2_1_to_2_0(soundModel.common);
65         model_2_0.phrases = soundModel.phrases;
66         return model_2_0;
67     }
68 
convertRecognitionConfig_2_3_to_2_1( android.hardware.soundtrigger.V2_3.RecognitionConfig config)69     static android.hardware.soundtrigger.V2_1.ISoundTriggerHw.RecognitionConfig convertRecognitionConfig_2_3_to_2_1(
70             android.hardware.soundtrigger.V2_3.RecognitionConfig config) {
71         return config.base;
72     }
73 
convertRecognitionConfig_2_3_to_2_0( android.hardware.soundtrigger.V2_3.RecognitionConfig config)74     static android.hardware.soundtrigger.V2_0.ISoundTriggerHw.RecognitionConfig convertRecognitionConfig_2_3_to_2_0(
75             android.hardware.soundtrigger.V2_3.RecognitionConfig config) {
76         android.hardware.soundtrigger.V2_0.ISoundTriggerHw.RecognitionConfig config_2_0 =
77                 config.base.header;
78         // Note: this mutates the input!
79         config_2_0.data = HidlMemoryUtil.hidlMemoryToByteList(config.base.data);
80         return config_2_0;
81     }
82 
convertProperties_2_0_to_2_3( android.hardware.soundtrigger.V2_0.ISoundTriggerHw.Properties properties)83     static android.hardware.soundtrigger.V2_3.Properties convertProperties_2_0_to_2_3(
84             android.hardware.soundtrigger.V2_0.ISoundTriggerHw.Properties properties) {
85         android.hardware.soundtrigger.V2_3.Properties properties_2_3 =
86                 new android.hardware.soundtrigger.V2_3.Properties();
87         properties_2_3.base = properties;
88         return properties_2_3;
89     }
90 }
91