1 /*
2  * Copyright (C) 2018 Knowles Electronics
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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <stdbool.h>
22 #include <limits.h>
23 #include <string.h>
24 
25 #define LOG_TAG "odsp_api_test"
26 
27 #include <cutils/log.h>
28 #include "iaxxx_odsp_hw.h"
29 
30 #define GETOPT_HELP_CHAR (CHAR_MIN - 2)
31 
32 static struct option const long_options[] =
33 {
34  {"setparamid", required_argument, NULL, 's'},
35  {"getparamid", required_argument, NULL, 'g'},
36  {"enableplugin", required_argument, NULL, 'e'},
37  {"disableplugin", required_argument, NULL, 'd'},
38  {"help", no_argument, NULL, GETOPT_HELP_CHAR},
39  {NULL, 0, NULL, 0}
40 };
41 
usage()42 void usage() {
43     fputs("\
44     USAGE -\n\
45     -------\n\
46     1) odsp_api_test -s <param_id> <param_val> <inst_id> <block_id>\n\
47     2) odsp_api_test -g <param_id> <inst_id> <block_id>\n\
48     3) odsp_api_test -e <inst_id> <block_id>\n\
49     4) odsp_api_test -d <inst_id> <block_id>\n\
50     5) odsp_api_test -t <src_id> <event_id> <src_opaque>\n\
51     \n\
52     In the first form, set a parameter with a value, needs instance and block id.\n\
53     In the second form, get a parameters value, needs instance and block id.\n\
54     In the third & fourth form, needs instance and block id.\n\
55     In the fifth form, manually trigger an event, needs src id, event id and src_opaque\n", stdout);
56 
57     exit(EXIT_FAILURE);
58 }
59 
set_param(struct iaxxx_odsp_hw * ioh,unsigned int param_id,unsigned int param_val,unsigned int inst_id,unsigned int block_id)60 void set_param(struct iaxxx_odsp_hw *ioh, unsigned int param_id,
61                 unsigned int param_val, unsigned int inst_id,
62                 unsigned int block_id) {
63     int err = 0;
64 
65     err = iaxxx_odsp_plugin_set_parameter(ioh, inst_id, param_id, param_val,
66                                            block_id);
67     if (err != 0) {
68         ALOGE("Failed to set parameter id %u with error %d", param_id, err);
69     }
70 }
71 
get_param(struct iaxxx_odsp_hw * ioh,unsigned int param_id,unsigned int inst_id,unsigned int block_id)72 void get_param(struct iaxxx_odsp_hw *ioh, unsigned int param_id,
73                    unsigned int inst_id, unsigned int block_id) {
74     int err = 0;
75     unsigned int param_val = 0;
76 
77     err = iaxxx_odsp_plugin_get_parameter(ioh, inst_id, param_id, block_id,
78                                           &param_val);
79     if (err != 0) {
80         ALOGE("Failed to get parameter value for id %u with error %d",
81             param_id, err);
82     } else {
83         ALOGD("Value of parameter id %u is %u", param_id, param_val);
84     }
85 }
86 
plugin_control(struct iaxxx_odsp_hw * ioh,unsigned int inst_id,unsigned int block_id,int control)87 void plugin_control(struct iaxxx_odsp_hw *ioh, unsigned int inst_id,
88                    unsigned int block_id, int control) {
89     int err = 0;
90 
91     if (control)
92         err = iaxxx_odsp_plugin_enable(ioh, inst_id, block_id);
93     else
94         err = iaxxx_odsp_plugin_disable(ioh, inst_id, block_id);
95 
96     if (err != 0) {
97         ALOGE("Failed to enable/disable the plugin. Error %d", err);
98     } else {
99         if (control) {
100             ALOGD("Plugin enabled!!");
101         } else {
102             ALOGD("Plugin disbled!!");
103         }
104     }
105 }
106 
trigger_event(struct iaxxx_odsp_hw * ioh,unsigned int src_id,unsigned int event_id,unsigned int src_opaque)107 void trigger_event(struct iaxxx_odsp_hw *ioh, unsigned int src_id,
108         unsigned int event_id, unsigned int src_opaque)
109 {
110     int err = 0;
111 
112     err = iaxxx_odsp_evt_trigger(ioh, src_id, event_id, src_opaque);
113 
114     if (err != 0) {
115         ALOGE("Failed to trigger event! Error:%d", err);
116     }
117 }
118 
119 
main(int argc,char * argv[])120 int main(int argc, char *argv[]) {
121     struct iaxxx_odsp_hw *ioh = NULL;
122     char use_case;
123     unsigned int param_id, param_val, inst_id, block_id;
124     unsigned int src_id, event_id, src_opaque;
125     int c, err = 0;
126     // Set param option needs 4 arguments
127     const int set_param_req_arg = 4;
128     // Get param option needs 3 arguments
129     const int get_param_req_arg = 3;
130     // Plugin control needs 2 arguments
131     const int plugin_control_arg = 2;
132     // Trigger event needs 3 args
133     const int trigger_event_req_arg = 3;
134 
135     if (argc <= 1) {
136         usage();
137     }
138 
139     while ((c = getopt_long (argc, argv, "s:g:e:d:t:", long_options, NULL)) != -1) {
140         switch (c) {
141         case 's':
142             use_case = 's';
143             // reset optind by 1
144             --optind;
145             if (optind + set_param_req_arg > argc) {
146                 usage();
147             }
148 
149             param_id = strtol(argv[optind], NULL, 0);
150             optind++;
151             param_val = strtol(argv[optind], NULL, 0);
152             optind++;
153             inst_id = strtol(argv[optind], NULL, 0);
154             optind++;
155             block_id = strtol(argv[optind], NULL, 0);
156             optind++;
157 
158             ALOGE("Set parameter - param_id %d param_val %d inst_id %d "
159                   "block_id %d", param_id, param_val, inst_id, block_id);
160         break;
161         case 'g':
162             use_case = 'g';
163             // reset optind by 1
164             --optind;
165             if (optind + get_param_req_arg > argc) {
166                 usage();
167             }
168 
169             param_id = strtol(argv[optind], NULL, 0);
170             optind++;
171             inst_id = strtol(argv[optind], NULL, 0);
172             optind++;
173             block_id = strtol(argv[optind], NULL, 0);
174             optind++;
175 
176             ALOGE("Get parameter - param_id %d inst_id %d block_id %d",
177                     param_id, inst_id, block_id);
178         break;
179 
180         case 'e':
181             use_case = 'e';
182             // reset optind by 1
183             --optind;
184             if (optind + plugin_control_arg > argc) {
185                 usage();
186             }
187 
188             inst_id = strtol(argv[optind], NULL, 0);
189             optind++;
190             block_id = strtol(argv[optind], NULL, 0);
191             optind++;
192 
193             ALOGE("Enable plugin - inst_id %d block_id %d", inst_id, block_id);
194         break;
195 
196         case 'd':
197             use_case = 'd';
198             // reset optind by 1
199             --optind;
200             if (optind + plugin_control_arg > argc) {
201                 usage();
202             }
203 
204             inst_id = strtol(argv[optind], NULL, 0);
205             optind++;
206             block_id = strtol(argv[optind], NULL, 0);
207             optind++;
208 
209             ALOGE("Disable plugin - inst_id %d block_id %d", inst_id, block_id);
210         break;
211 
212         case 't':
213             use_case = 't';
214             // reset optind by 1
215             --optind;
216             if (optind + trigger_event_req_arg > argc) {
217                 usage();
218             }
219 
220             src_id = strtol(argv[optind], NULL, 0);
221             optind++;
222             event_id = strtol(argv[optind], NULL, 0);
223             optind++;
224             src_opaque = strtol(argv[optind], NULL, 0);
225             optind++;
226 
227             ALOGE("Trigger Event - src_id %d event_id %d src_opaque %d",
228                     src_id, event_id, src_opaque);
229         break;
230 
231         default:
232             usage();
233         break;
234         }
235     }
236 
237     ioh = iaxxx_odsp_init();
238     if (ioh == NULL) {
239         ALOGE("ERROR: Failed to init odsp HAL");
240         err = -1;
241         goto exit;
242     }
243 
244     switch(use_case) {
245         case 's':
246             set_param(ioh, param_id, param_val, inst_id, block_id);
247             break;
248         case 'g':
249             get_param(ioh, param_id, inst_id, block_id);
250             break;
251         case 'e':
252             plugin_control(ioh, inst_id, block_id, 1);
253             break;
254         case 'd':
255             plugin_control(ioh, inst_id, block_id, 0);
256             break;
257         case 't':
258             trigger_event(ioh, src_id, event_id, src_opaque);
259             break;
260 
261         default:
262             ALOGE("Invalid usecase");
263             break;
264     }
265 exit:
266     if (ioh) {
267         err = iaxxx_odsp_deinit(ioh);
268         if (err != 0) {
269             ALOGE("Failed to deinit the odsp HAL");
270         }
271     }
272 
273     return err;
274 }
275