summaryrefslogtreecommitdiff
path: root/6515/libsensors_iio/software/core/mllite/linux/ml_sysfs_helper.c
blob: d2b95435c1b5a8f0d42e8113dcc6fd5de88e68e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 $License:
    Copyright (C) 2014 InvenSense Corporation, All Rights Reserved.
 $
 */

#undef MPL_LOG_NDEBUG
#define MPL_LOG_NDEBUG 0 /* Use 0 to turn on MPL_LOGV output */
#undef MPL_LOG_TAG
#define MPL_LOG_TAG "MLLITE"

#include <string.h>
#include <stdio.h>
#include "ml_sysfs_helper.h"
#include <dirent.h>
#include <ctype.h>
#include "log.h"

#define MPU_SYSFS_ABS_PATH "/sys/class/invensense/mpu"

enum PROC_SYSFS_CMD {
	CMD_GET_SYSFS_PATH,
	CMD_GET_DMP_PATH,
	CMD_GET_CHIP_NAME,
	CMD_GET_SYSFS_KEY,
	CMD_GET_TRIGGER_PATH,
	CMD_GET_DEVICE_NODE
};
static char sysfs_path[100];
static char *chip_name[] = {
    "ITG3500",
    "MPU6050",
    "MPU9150",
    "MPU3050",
    "MPU6500",
    "MPU9250",
    "MPU6XXX",
    "MPU9350",
    "MPU6515",
    "MPU6880",
};
static int chip_ind;
static int initialized =0;
static int status = 0;
static int iio_initialized = 0;
static int iio_dev_num = 0;

#define IIO_MAX_NAME_LENGTH 30

#define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements"
#define FORMAT_TYPE_FILE "%s_type"

#define CHIP_NUM ARRAY_SIZE(chip_name)

static const char *iio_dir = "/sys/bus/iio/devices/";

/**
 * find_type_by_name() - function to match top level types by name
 * @name: top level type instance name
 * @type: the type of top level instance being sort
 *
 * Typical types this is used for are device and trigger.
 **/
int find_type_by_name(const char *name, const char *type)
{
	const struct dirent *ent;
	int number, numstrlen;

	FILE *nameFile;
	DIR *dp;
	char thisname[IIO_MAX_NAME_LENGTH];
	char *filename;

	dp = opendir(iio_dir);
	if (dp == NULL) {
		MPL_LOGE("No industrialio devices available");
		return -ENODEV;
	}

	while (ent = readdir(dp), ent != NULL) {
		if (strcmp(ent->d_name, ".") != 0 &&
			strcmp(ent->d_name, "..") != 0 &&
			strlen(ent->d_name) > strlen(type) &&
			strncmp(ent->d_name, type, strlen(type)) == 0) {
			numstrlen = sscanf(ent->d_name + strlen(type),
					   "%d",
					   &number);
			/* verify the next character is not a colon */
			if (strncmp(ent->d_name + strlen(type) + numstrlen,
					":",
					1) != 0) {
				filename = malloc(strlen(iio_dir)
						+ strlen(type)
						+ numstrlen
						+ 6);
				if (filename == NULL)
					return -ENOMEM;
				sprintf(filename, "%s%s%d/name",
					iio_dir,
					type,
					number);
				nameFile = fopen(filename, "r");
				if (!nameFile)
					continue;
				free(filename);
				fscanf(nameFile, "%s", thisname);
				if (strcmp(name, thisname) == 0)
					return number;
				fclose(nameFile);
			}
		}
	}
	return -ENODEV;
}

/* mode 0: search for which chip in the system and fill sysfs path
   mode 1: return event number
 */
static int parsing_proc_input(int mode, char *name){
	const char input[] = "/proc/bus/input/devices";
	char line[4096], d;
	char tmp[100];
	FILE *fp;
	int i, j, result, find_flag;
	int event_number = -1;
	int input_number = -1;

	if(NULL == (fp = fopen(input, "rt")) ){
		return -1;
	}
	result = 1;
	find_flag = 0;
	while(result != 0 && find_flag < 2){
		i = 0;
		d = 0;
		memset(line, 0, 100);
		while(d != '\n'){
			result = fread(&d, 1, 1, fp);
			if(result == 0){
				line[0] = 0;
				break;
			}
			sprintf(&line[i], "%c", d);
			i ++;
		}
		if(line[0] == 'N'){
			i = 1;
			while(line[i] != '"'){
				i++;
			}
			i++;
			j = 0;
			find_flag = 0;
			if (mode == 0){
				while(j < CHIP_NUM){
					if(!memcmp(&line[i], chip_name[j], strlen(chip_name[j]))){
						find_flag = 1;
						chip_ind = j;
					}
					j++;
				}
			} else if (mode  != 0){
				if(!memcmp(&line[i], name, strlen(name))){
					find_flag = 1;
				}
			}
		}
		if(find_flag){
			if(mode == 0){
				if(line[0] == 'S'){
					memset(tmp, 0, 100);
					i =1;
					while(line[i] != '=') i++;
					i++;
					j = 0;
					while(line[i] != '\n'){
						tmp[j] = line[i];
						i ++; j++;
					}
					sprintf(sysfs_path, "%s%s", "/sys", tmp);
					find_flag++;
				}
			} else if(mode == 1){
				if(line[0] == 'H') {
					i = 2;
					while(line[i] != '=') i++;
					while(line[i] != 't') i++;
					i++;
					event_number = 0;
					while(line[i] != '\n'){
						if(line[i] >= '0' && line[i] <= '9')
							event_number = event_number*10 + line[i]-0x30;
						i ++;
					}
					find_flag ++;
				}
			} else if (mode == 2) {
				if(line[0] == 'S'){
					memset(tmp, 0, 100);
					i =1;
					while(line[i] != '=') i++;
					i++;
					j = 0;
					while(line[i] != '\n'){
						tmp[j] = line[i];
						i ++; j++;
					}
					input_number = 0;
					if(tmp[j-2] >= '0' && tmp[j-2] <= '9')
						input_number += (tmp[j-2]-0x30)*10;
					if(tmp[j-1] >= '0' && tmp[j-1] <= '9')
						input_number += (tmp[j-1]-0x30);
					find_flag++;
				}
			}
		}
	}
	fclose(fp);
	if(find_flag == 0){
		return -1;
	}
	if(0 == mode)
		status = 1;
	if (mode == 1)
		return event_number;
	if (mode == 2)
		return input_number;
	return 0;

}
static void init_iio() {
	int i, j;
	char iio_chip[10];
	int dev_num;
	for(j=0; j< CHIP_NUM; j++) {
		for (i=0; i<strlen(chip_name[j]); i++) {
			iio_chip[i] = tolower(chip_name[j][i]);
		}
		iio_chip[strlen(chip_name[j])] = '\0';
		dev_num = find_type_by_name(iio_chip, "iio:device");
		if(dev_num >= 0) {
			iio_initialized = 1;
			iio_dev_num = dev_num;
			chip_ind = j;
		}
	}
}

static int process_sysfs_request(enum PROC_SYSFS_CMD cmd, char *data)
{
	char key_path[100];
	FILE *fp;
	int i, result;
	if(initialized == 0){
		parsing_proc_input(0, NULL);
		initialized = 1;
	}
	if(initialized && status == 0) {
		init_iio();
		if (iio_initialized == 0)
			return -1;
	}

	memset(key_path, 0, 100);
	switch(cmd){
	case CMD_GET_SYSFS_PATH:
		if (iio_initialized == 1)
			sprintf(data, "/sys/bus/iio/devices/iio:device%d", iio_dev_num);
		else
			sprintf(data, "%s%s", sysfs_path, "/device/invensense/mpu");
		break;
	case CMD_GET_DMP_PATH:
		if (iio_initialized == 1)
			sprintf(data, "/sys/bus/iio/devices/iio:device%d/dmp_firmware", iio_dev_num);
		else
			sprintf(data, "%s%s", sysfs_path, "/device/invensense/mpu/dmp_firmware");
		break;
	case CMD_GET_CHIP_NAME:
		sprintf(data, "%s", chip_name[chip_ind]);
		break;
	case CMD_GET_TRIGGER_PATH:
		sprintf(data, "/sys/bus/iio/devices/trigger%d", iio_dev_num);
		break;
	case CMD_GET_DEVICE_NODE:
		sprintf(data, "/dev/iio:device%d", iio_dev_num);
		break;
	case CMD_GET_SYSFS_KEY:
		memset(key_path, 0, 100);
		if (iio_initialized == 1)
			sprintf(key_path, "/sys/bus/iio/devices/iio:device%d/key", iio_dev_num);
		else
			sprintf(key_path, "%s%s", sysfs_path, "/device/invensense/mpu/key");

		if((fp = fopen(key_path, "rt")) == NULL)
			return -1;
		for(i=0;i<16;i++){
			fscanf(fp, "%02x", &result);
			data[i] = (char)result;
		}

		fclose(fp);
		break;
	default:
		break;
	}
	return 0;
}

int find_name_by_sensor_type(const char *sensor_type, const char *type, char *sensor_name)
{
    const struct dirent *ent;
    int number, numstrlen;

    FILE *nameFile;
    DIR *dp;
    char *filename;

    dp = opendir(iio_dir);
    if (dp == NULL) {
        MPL_LOGE("No industrialio devices available");
        return -ENODEV;
    }

    while (ent = readdir(dp), ent != NULL) {
        if (strcmp(ent->d_name, ".") != 0 &&
            strcmp(ent->d_name, "..") != 0 &&
            strlen(ent->d_name) > strlen(type) &&
            strncmp(ent->d_name, type, strlen(type)) == 0) {
            numstrlen = sscanf(ent->d_name + strlen(type),
                       "%d",
                       &number);
            /* verify the next character is not a colon */
            if (strncmp(ent->d_name + strlen(type) + numstrlen,
                    ":",
                    1) != 0) {
                filename = malloc(strlen(iio_dir)
                        + strlen(type)
                        + numstrlen
                        + 6
                        + strlen(sensor_type));
                if (filename == NULL)
                    return -ENOMEM;
                sprintf(filename, "%s%s%d/%s",
                    iio_dir,
                    type,
                    number,
                    sensor_type);
                nameFile = fopen(filename, "r");
                MPL_LOGI("sensor type path: %s\n", filename);
                free(filename);
                //fscanf(nameFile, "%s", thisname);
                //if (strcmp(name, thisname) == 0) {
                if(nameFile == NULL) {
                    MPL_LOGI("keeps searching");
                    continue;
                } else{
                    MPL_LOGI("found directory");
                }
                filename = malloc(strlen(iio_dir)
                        + strlen(type)
                        + numstrlen
                        + 6);
                sprintf(filename, "%s%s%d/name",
                    iio_dir,
                    type,
                    number);
                    nameFile = fopen(filename, "r");
                    MPL_LOGI("name path: %s\n", filename);
                    free(filename);
                    if (!nameFile)
                        continue;
                    fscanf(nameFile, "%s", sensor_name);
                    MPL_LOGI("name found: %s now test for mpuxxxx", sensor_name);
                    if( !strncmp("mpu",sensor_name, 3) ) {
                        char secondaryFileName[200];
                    sprintf(secondaryFileName, "%s%s%d/secondary_name",
                        iio_dir,
                        type,
                        number);
                        nameFile = fopen(secondaryFileName, "r");
                        MPL_LOGI("name path: %s\n", secondaryFileName);
                        if(!nameFile)
                            continue;
                        fscanf(nameFile, "%s", sensor_name);
                        MPL_LOGI("secondary name found: %s\n", sensor_name);
                    }
                    else {
                        fscanf(nameFile, "%s", sensor_name);
                        MPL_LOGI("name found: %s\n", sensor_name);
                    }
                    return 0;
                //}
                fclose(nameFile);
            }
        }
    }
    return -ENODEV;
}

/**
 *  @brief  return sysfs key. if the key is not available
 *          return false. So the return value must be checked
 *          to make sure the path is valid.
 *  @unsigned char *name: This should be array big enough to hold the key
 *           It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 */
inv_error_t inv_get_sysfs_key(unsigned char *key)
{
	if (process_sysfs_request(CMD_GET_SYSFS_KEY, (char*)key) < 0)
		return INV_ERROR_NOT_OPENED;
	else
		return INV_SUCCESS;
}

/**
 *  @brief  return the sysfs path. If the path is not
 *          found yet. return false. So the return value must be checked
 *          to make sure the path is valid.
 *  @unsigned char *name: This should be array big enough to hold the sysfs
 *           path. It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 */
inv_error_t inv_get_sysfs_path(char *name)
{
	if (process_sysfs_request(CMD_GET_SYSFS_PATH, name) < 0)
		return INV_ERROR_NOT_OPENED;
	else
		return INV_SUCCESS;
}

inv_error_t inv_get_sysfs_abs_path(char *name)
{
    strcpy(name, MPU_SYSFS_ABS_PATH);
    return INV_SUCCESS;
}

/**
 *  @brief  return the dmp file path. If the path is not
 *          found yet. return false. So the return value must be checked
 *          to make sure the path is valid.
 *  @unsigned char *name: This should be array big enough to hold the dmp file
 *           path. It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 */
inv_error_t inv_get_dmpfile(char *name)
{
   	if (process_sysfs_request(CMD_GET_DMP_PATH, name) < 0)
		return INV_ERROR_NOT_OPENED;
	else
		return INV_SUCCESS;
}
/**
 *  @brief  return the chip name. If the chip is not
 *          found yet. return false. So the return value must be checked
 *          to make sure the path is valid.
 *  @unsigned char *name: This should be array big enough to hold the chip name
 *           path(8 bytes). It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 */
inv_error_t inv_get_chip_name(char *name)
{
   	if (process_sysfs_request(CMD_GET_CHIP_NAME, name) < 0)
		return INV_ERROR_NOT_OPENED;
	else
		return INV_SUCCESS;
}
/**
 *  @brief  return event handler number. If the handler number is not found
 *          return false. the return value must be checked
 *          to make sure the path is valid.
 *  @unsigned char *name: This should be array big enough to hold the chip name
 *           path(8 bytes). It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 *  @int *num: event number store
 */
inv_error_t  inv_get_handler_number(const char *name, int *num)
{
	initialized = 0;
	if ((*num = parsing_proc_input(1, (char *)name)) < 0)
		return INV_ERROR_NOT_OPENED;
	else
		return INV_SUCCESS;
}

/**
 *  @brief  return input number. If the handler number is not found
 *          return false. the return value must be checked
 *          to make sure the path is valid.
 *  @unsigned char *name: This should be array big enough to hold the chip name
 *           path(8 bytes). It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 *  @int *num: input number store
 */
inv_error_t  inv_get_input_number(const char *name, int *num)
{
	initialized = 0;
	if ((*num = parsing_proc_input(2, (char *)name)) < 0)
		return INV_ERROR_NOT_OPENED;
	else {
		return INV_SUCCESS;
	}
}

/**
 *  @brief  return iio trigger name. If iio is not initialized, return false.
 *          So the return must be checked to make sure the numeber is valid.
 *  @unsigned char *name: This should be array big enough to hold the trigger
 *           name. It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 */
inv_error_t inv_get_iio_trigger_path(const char *name)
{
	if (process_sysfs_request(CMD_GET_TRIGGER_PATH, (char *)name) < 0)
		return INV_ERROR_NOT_OPENED;
	else
		return INV_SUCCESS;
}

/**
 *  @brief  return iio device node. If iio is not initialized, return false.
 *          So the return must be checked to make sure the numeber is valid.
 *  @unsigned char *name: This should be array big enough to hold the device
 *           node. It should be zeroed before calling this function.
 *           Or it could have unpredicable result.
 */
inv_error_t inv_get_iio_device_node(const char *name)
{
	if (process_sysfs_request(CMD_GET_DEVICE_NODE, (char *)name) < 0)
		return INV_ERROR_NOT_OPENED;
	else
		return INV_SUCCESS;
}