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 <ctype.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <string.h>
23 #include "iaxxx_odsp_hw.h"
24 
25 
26 #define MAX_FILE_PATH_SIZE (512)
27 
28 enum {
29     SETPARAMBLK_TEST_OPTION_FROMFILE,
30     SETPARAMBLK_TEST_OPTION_CUSTOM,
31     SETPARAMBLK_TEST_OPTION_WITHACK
32 };
33 
34 
35 static struct option const long_options[] =
36 {
37     {"setparamblkfromfile", no_argument, NULL, 's'},
38     {"customsetparamblk", no_argument, NULL, 'c'},
39     {"setparamblkwithack", no_argument, NULL, 'k'},
40     {"filename", required_argument, NULL, 'f'},
41     {"plugininstanceid", required_argument, NULL, 'i'},
42     {"paramblkid", required_argument, NULL, 'p'},
43     {"procblockid", required_argument, NULL, 'b'},
44     {"help", no_argument, NULL, 'h'},
45     {NULL, 0, NULL, 0}
46 };
47 
48 
setparamblk_with_ack_test(struct iaxxx_odsp_hw * odsp_hw,const uint32_t inst_id,const uint32_t block_id,const uint32_t param_blk_id,const uint32_t max_retries,uint32_t * response_data,const uint32_t response_data_size_in_words,const char * file)49 static int setparamblk_with_ack_test(struct iaxxx_odsp_hw *odsp_hw,
50                                     const uint32_t inst_id,
51                                     const uint32_t block_id,
52                                     const uint32_t  param_blk_id,
53                                     const uint32_t max_retries,
54                                     uint32_t* response_data,
55                                     const uint32_t response_data_size_in_words,
56                                     const char *file)
57 {
58     int ret = -1;
59     uint32_t *data = NULL;
60     FILE *fp = NULL;
61     long file_len, bytes_read;
62 
63     fp = fopen(file, "rb");
64     if (NULL == fp) {
65         fprintf(stderr,"Failed to find the file at the location %s", file);
66         goto setparamblk_with_ack_test;
67     }
68 
69     fseek(fp, 0, SEEK_END);
70     file_len = ftell(fp);
71     fseek(fp, 0, SEEK_SET);
72     data = malloc(file_len);
73     if (!data) {
74         fprintf(stderr,"Error allocating memory");
75         goto setparamblk_with_ack_test;
76     }
77 
78 
79     bytes_read = fread(data, 1, file_len, fp);
80     if (bytes_read != file_len) {
81         fprintf(stderr,"Failed to read the complete file");
82         fclose(fp);
83         goto setparamblk_with_ack_test;
84     }
85     fclose(fp);
86 
87     ret = iaxxx_odsp_plugin_set_parameter_blk_with_ack(odsp_hw, inst_id,
88                                                     param_blk_id, block_id, data,
89                                                     file_len * sizeof(uint32_t),
90                                                     response_data,
91                                                     response_data_size_in_words,
92                                                     max_retries);
93 
94 setparamblk_with_ack_test:
95     if (data)
96         free(data);
97     return ret;
98 }
99 
usage()100 void usage() {
101     fprintf(stdout, "\
102     USAGE -\n\
103     -------\n\
104     1) setparamblk_test -s -f <file_name> -i <plugin-instance-id> -p <param-blk-id> -b <proc-block-id> \n\
105     2) setparamblk_test -k -f <file_name> -i <plugin-instance-id> -p <param-blk-id> -b <proc-block-id> -m <max-retries> -r <response-size-in-words> \n\
106     3) setparamblk_test -c -f <file_name> -i <plugin-instance-id> -p <param-blk-id> -b <proc-block-id> \n\
107     \n\
108     In the first form, test setparamblk from a file.\n\
109     In the second form, test setparamblk from a file with retry and acknowledgement\n\
110     In the second form, test setparamblk from a file for custom configuration of plugin.\n\n");
111 
112     exit(0);
113 }
114 
115 
main(int argc,char ** argv)116 int main(int argc, char **argv)
117 {
118     struct iaxxx_odsp_hw* odsp_hw = NULL;
119     int err = 0;
120 
121     int plugin_instance_id = -1;
122     int proc_block_id = -1;
123     int param_blk_id = -1;
124     int max_retries = -1;
125     int response_data_size_in_words = -1;
126     uint32_t *response_data = NULL;
127     char file_path[MAX_FILE_PATH_SIZE];
128     int setparamblk_test_option = -1;
129     int index = 0;
130 
131     /* getopt_long stores the option index here. */
132     int option_index = 0;
133     int ch;
134 
135     file_path[0] = '\0';
136 
137     if (argc <= 1) {
138         usage();
139     }
140 
141     while (1) {
142         ch = getopt_long(argc, argv, "sckhf:p:i:b:m:r:",
143                         long_options, &option_index);
144 
145         /* Detect the end of the options. */
146         if (ch == -1)
147             break;
148 
149         switch (ch) {
150             case 's':
151                 setparamblk_test_option = SETPARAMBLK_TEST_OPTION_FROMFILE;
152                 break;
153 
154             case 'c':
155                 setparamblk_test_option = SETPARAMBLK_TEST_OPTION_CUSTOM;
156                 break;
157 
158             case 'k':
159                 setparamblk_test_option = SETPARAMBLK_TEST_OPTION_WITHACK;
160                 break;
161 
162             case 'f':
163                 strcpy(file_path, optarg);
164                 break;
165 
166             case 'p':
167                 param_blk_id = atoi(optarg);
168                 break;
169 
170             case 'i':
171                 plugin_instance_id = atoi(optarg);
172                 break;
173 
174             case 'b':
175                 proc_block_id = atoi(optarg);
176                 break;
177 
178             case 'm':
179                 max_retries = atoi(optarg);
180                 break;
181 
182             case 'r':
183                 response_data_size_in_words = atoi(optarg);
184                 break;
185 
186             case 'h':
187             default:
188                 usage();
189         }
190     }
191 
192     if (setparamblk_test_option == -1) {
193         fprintf(stderr,"\n No Test Option Parameter! \n");
194         usage();
195     }
196 
197     if (file_path[0] == '\0') {
198         fprintf(stderr,"\n No File Name Parameter! \n");
199         usage();
200     }
201 
202     if (plugin_instance_id == -1) {
203         fprintf(stderr,"\n No Plugin_instance_id Parameter! \n");
204         usage();
205     }
206 
207     if (param_blk_id == -1) {
208         fprintf(stderr,"\n No Param_blk_id Parameter! \n");
209         usage();
210     }
211 
212     if (proc_block_id == -1) {
213         fprintf(stderr,"\n No proc_block_id Parameter! \n");
214         usage();
215     }
216     if (setparamblk_test_option == SETPARAMBLK_TEST_OPTION_WITHACK) {
217 
218         if (max_retries == -1) {
219             fprintf(stderr,"\n No max retries Parameter! \n");
220             usage();
221         }
222 
223         if (response_data_size_in_words <= 0) {
224             fprintf(stderr,"\n No or Invalid Response data size Parameter! \n");
225             usage();
226         }
227 
228         response_data = (uint32_t*) malloc(sizeof(uint32_t) * response_data_size_in_words);
229 
230         if (!response_data) {
231             fprintf(stderr,"\n malloc failed! \n", stdout);
232             exit(0);
233         }
234 
235         memset(response_data, 0, sizeof(uint32_t) * response_data_size_in_words);
236     }
237 
238     if ((odsp_hw = iaxxx_odsp_init()) == NULL) {
239         fprintf(stderr,"\n ODSP Init Failed! \n");
240         return 0;
241     }
242 
243     if (setparamblk_test_option == SETPARAMBLK_TEST_OPTION_FROMFILE) {
244         fprintf(stdout, "\n## Setparamblk_test from file.. ##\n");
245         err = iaxxx_odsp_plugin_set_parameter_blk_from_file(odsp_hw,
246                                                             plugin_instance_id,
247                                                             param_blk_id,
248                                                             proc_block_id,
249                                                             file_path);
250     } else if (setparamblk_test_option == SETPARAMBLK_TEST_OPTION_CUSTOM) {
251         fprintf(stdout, "\n## Setparamblk_test custom configuration.. ##\n");
252         err = iaxxx_odsp_plugin_set_custom_cfg(odsp_hw, plugin_instance_id,
253                                             proc_block_id, param_blk_id, 0,
254                                             file_path);
255     } else if (setparamblk_test_option == SETPARAMBLK_TEST_OPTION_WITHACK) {
256 
257         fprintf(stdout, "\n## Setparamblk_test with ack and retry.. ##\n");
258         err = setparamblk_with_ack_test(odsp_hw, plugin_instance_id,
259                                         proc_block_id, param_blk_id,
260                                         max_retries, response_data,
261                                         response_data_size_in_words, file_path);
262         if (!err) {
263             // Print the response data
264             fprintf(stdout,"\nResponse Data::");
265             for (index = 0; index < response_data_size_in_words; index++) {
266                 if (!(index % 8))
267                     fprintf(stdout, "\n0x%04x: ", index);
268 
269                 fprintf(stdout, "0x%08x ", response_data[index]);
270             }
271             fprintf(stdout, "\n");
272         }
273         free(response_data);
274     }
275 
276     iaxxx_odsp_deinit(odsp_hw);
277     if (err)
278         fprintf(stdout, "\n## Setparamblk_test failed!! ##\n\n");
279     else
280         fprintf(stdout, "\n## Setparamblk_test done. ##\n\n");
281 
282     return 0;
283 }