• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/ioctl.h>
23 #include <limits.h>
24 #include <string.h>
25 #include <linux/mfd/adnc/iaxxx-module.h>
26 
27 #define LOG_TAG "ia_sensor_param_test"
28 
29 #include <cutils/log.h>
30 
31 #define GETOPT_HELP_CHAR (CHAR_MIN - 2)
32 
33 #define DEV_NODE "/dev/iaxxx-module-celldrv"
34 
35 struct ia_sensor_mgr {
36     FILE *dev_node;
37 };
38 
39 static struct option const long_options[] =
40 {
41  {"setparamid", required_argument, NULL, 's'},
42  {"value", required_argument, NULL, 'v'},
43  {"getparamid", required_argument, NULL, 'g'},
44  {"help", no_argument, NULL, GETOPT_HELP_CHAR},
45  {NULL, 0, NULL, 0}
46 };
47 
usage()48 void usage() {
49     fputs("\
50     USAGE -\n\
51     -------\n\
52     1) sensor_param_test -s <param_id> -v <param_val>\n\
53     2) sensor_param_test -g <param_id>\n\
54     \n\
55     In the first form, set a parameter with a value.\n\
56     In the second form, get a value of a parameter\n\
57     ", stdout);
58 
59     fputs("\n\
60     OPTIONS - \n\
61     ---------\n\
62     -s          Set a parameter using its ID.\n\
63     -v          Set this value for the parameter ID that was passed with\n\
64                 the option '-s'. Using this option alone is invalid.\n\
65     -g          Get the value of a parameter using its ID.\n\
66     ", stdout);
67 
68     exit(EXIT_FAILURE);
69 }
70 
set_param(struct ia_sensor_mgr * smd,int param_id,float param_val)71 void set_param(struct ia_sensor_mgr *smd, int param_id, float param_val) {
72     int err = 0;
73     struct iaxxx_sensor_param sp;
74 
75     if (NULL == smd) {
76         ALOGE("%s: NULL handle passed", __func__);
77         return;
78     }
79 
80     sp.inst_id      = 0;
81     sp.block_id     = 0;
82 
83     sp.param_id = param_id;
84     sp.param_val = param_val;
85 
86     ALOGD("Set sensor param 0x%X with value %f",
87           param_id, param_val);
88     fprintf(stdout, "Set sensor param 0x%X with value %f\n",
89           param_id, param_val);
90 
91     err = ioctl(fileno(smd->dev_node), MODULE_SENSOR_SET_PARAM, (unsigned long) &sp);
92     if (-1 == err) {
93         ALOGE("%s: ERROR: MODULE_SENSOR_SET_PARAM IOCTL failed with error %d(%s)", __func__, errno, strerror(errno));
94         return;
95     }
96 
97 }
98 
get_param(struct ia_sensor_mgr * smd,int param_id)99 void get_param(struct ia_sensor_mgr *smd, int param_id) {
100     struct iaxxx_sensor_param sp;
101     int err = 0;
102     ALOGD ("Get param - param_id 0x%X", param_id);
103 
104     if (NULL == smd) {
105         ALOGE("%s: NULL handle passed", __func__);
106         return;
107     }
108 
109     sp.inst_id      = 0;
110     sp.block_id     = 0;
111 
112     sp.param_id = param_id;
113     err = ioctl(fileno(smd->dev_node), MODULE_SENSOR_GET_PARAM, (unsigned long) &sp);
114     if (-1 == err) {
115         ALOGE("%s: ERROR: MODULE_SENSOR_GET_PARAM IOCTL failed with error %d(%s)", __func__, errno, strerror(errno));
116         return;
117     } else {
118         ALOGD("Value of param 0x%X is %zu",
119               sp.param_id, sp.param_val);
120         fprintf(stdout, "Value of param 0x%X is %zu\n",
121               sp.param_id, sp.param_val);
122     }
123 }
124 
125 
main(int argc,char * argv[])126 int main(int argc, char *argv[]) {
127     struct ia_sensor_mgr * smd;
128     char use_case;
129     int param_id = -1;
130     int c;
131     float param_val;
132 
133     if (argc <= 1) {
134         usage();
135     }
136 
137     while ((c = getopt_long (argc, argv, "s:v:g:", long_options, NULL)) != -1) {
138         switch (c) {
139         case 's':
140             if (NULL == optarg) {
141                 fprintf(stderr, "Incorrect usage, s option requires an argument");
142                 usage();
143             } else {
144                 param_id = strtol(optarg, NULL, 0);
145                 use_case = 's';
146             }
147         break;
148         case 'v':
149             if (NULL == optarg) {
150                 fprintf(stderr, "Incorrect usage, v option requires an argument");
151                 usage();
152             } else {
153                 if ('s' == use_case) {
154                     param_val = strtof(optarg, NULL);
155                 } else {
156                     fprintf(stderr, "Incorrect usage, v option should be the second option");
157                     usage();
158                 }
159             }
160         break;
161         case 'g':
162             if (NULL == optarg) {
163                 fprintf(stderr, "Incorrect usage, g option requires an argument");
164                 usage();
165             } else {
166                 param_id = strtol(optarg, NULL, 0);
167                 use_case = 'g';
168             }
169         break;
170         case GETOPT_HELP_CHAR:
171         default:
172             usage();
173         break;
174         }
175     }
176 
177     smd = (struct ia_sensor_mgr*) malloc(sizeof(struct ia_sensor_mgr));
178     if (NULL == smd) {
179         ALOGE("%s: ERROR Failed to allocated memory for ia_sensor_mgr", __func__);
180         return -EINVAL;
181     }
182 
183     if((smd->dev_node = fopen(DEV_NODE, "rw")) == NULL) {
184         ALOGE("%s: ERROR file %s open for write error: %s\n", __func__, DEV_NODE, strerror(errno));
185         free(smd);
186         return -EINVAL;
187     }
188 
189 
190     if ('s' == use_case) {
191         set_param(smd, param_id, param_val);
192     } else if ('g' == use_case) {
193         get_param(smd, param_id);
194     }
195 
196     if (smd->dev_node) {
197         fclose(smd->dev_node);
198     }
199 
200     if (smd) {
201         free(smd);
202     }
203 
204     return 0;
205 }
206