-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensorDetect.cpp
executable file
·715 lines (631 loc) · 23.1 KB
/
sensorDetect.cpp
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#define LOG_TAG "SensorDetect"
#include <hardware/sensors.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <math.h>
#include <poll.h>
#include <pthread.h>
#include <linux/input.h>
#include <cutils/atomic.h>
#include <cutils/log.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <cutils/misc.h>
#include <cutils/properties.h>
#include "sensors.h"
int sNumber = 0;
int flag0 = 0;
int flag1 = 0;
int found = 0;
struct sensor_t sSensorList[SUPPORT_SENSORS_NUMBER];
struct status seStatus[SUPPORT_SENSORS_NUMBER];
struct sensor_info gsensorInfo = {";", {0}, 0};
struct sensor_info magSensorInfo = {";", {0}, 0};
struct sensor_info gyrSensorInfo = {";", {0}, 0};
struct sensor_info ligSensorInfo = {";", {0}, 0};
struct sensor_info proSensorInfo = {";", {0}, 0};
struct sensor_info oriSensorInfo = {";", {0}, 0};
struct sensor_info preSensorInfo = {";", {0}, 0};
struct sensor_info tempSensorInfo = {";", {0}, 0};
static struct o_device otherDevice[] = {
{
0, "sw-",
}, {
0, "axp",
},
};
static struct o_device ctpDevice[] = {
{
0, "gt",
}, {
0, "gsl",
}, {
0, "ft5x"
},
};
struct sensor_extend_t preSensorList[] = {
};
struct sensor_extend_t tempSensorList[] = {
{
{
"sunxi-temp", 0,
},{
"Temperature sensor",
"AW.",
1, SENSORS_TEMPERATURE_HANDLE,
SENSOR_TYPE_AMBIENT_TEMPERATURE,
85.0f, 0.5f,
0.35f, 0,
{ }
},
},
};
struct sensor_extend_t oriSensorList[] = {
{
{
"FreescaleMagnetometer", 0,
}, {
"Freescale Orientation sensor",
"Freescale Semiconductor Inc.",
1,
SENSORS_ORIENTATION_HANDLE,
SENSOR_TYPE_ORIENTATION,
360.0f, CONVERT_O,
0.50f, 100000, { },
},
},
};
struct sensor_extend_t proSensorList[] = {
{
{
"jsa1212ps", 1.0,
},
{
"JSA1212 Proximity sensor",
"SOLTEAM",
1,
SENSORS_PROXIMITY_HANDLE,
SENSOR_TYPE_PROXIMITY,
1.0f, 1.0f,
0.005f, 0, { }
},
},
{
{
"proximity", PROXIMITY_THRESHOLD_GP2A,
}, {
"LTR-C216R-14 sensor PROXIMITY",
"LTR 2",
1,
SENSORS_PROXIMITY_HANDLE,
SENSOR_TYPE_PROXIMITY,
5.0f, 5.0f,
0.2f, 0, { }
},
},
};
struct sensor_extend_t ligSensorList[] = {
{
{
"al3010", 0,
}, {
"AL3010 Light sensor",
"Intersil ",
1,
SENSORS_LIGHT_HANDLE,
SENSOR_TYPE_LIGHT,
300.0f, 1.0f,
0.35f, 0, { }
}
},
{
{
"jsa1212als", 0,
},
{
"JSA1212 Light sensor",
"SOLTEAM",
1,
SENSORS_LIGHT_HANDLE,
SENSOR_TYPE_LIGHT,
100000.0f, 1.0f,
0.005f, 0, { }
}
},
{
{
"lightsensor", 0,
}, {
"LTR-C216R-14 sensor LIGHT",
"LTR ",
1,
SENSORS_LIGHT_HANDLE,
SENSOR_TYPE_LIGHT,
20000.0f, 20.0f,
0.2f, 0, { }
}
},
};
struct sensor_extend_t gyrSensorList[] = {
{
{
"l3gd20_gyr", 0,
}, {
"ST 3-axis Gyroscope sensor",
"STMicroelectronics",
1,
SENSORS_GYROSCOPE_HANDLE,
SENSOR_TYPE_GYROSCOPE,
(2000.0f*(float)M_PI/180.0f),
(70.0f / 1000.0f) * ((float)M_PI / 180.0f),
6.1f, 1190, { },
},
},
};
struct sensor_extend_t magSensorList[] = {
{
{
"lsm303d_mag", 0,
}, {
"ST 3-axis Magnetic field sensor",
"STMicroelectronics",
1,
SENSORS_MAGNETIC_FIELD_HANDLE,
SENSOR_TYPE_MAGNETIC_FIELD,
2000.0f, CONVERT_M,
6.8f, 16667, { },
},
}, {
{
"FreescaleMagnetometer", 0,
}, {
"Freescale 3-axis Magnetic field sensor",
"Freescale Semiconductor Inc.",
1,
SENSORS_MAGNETIC_FIELD_HANDLE,
SENSOR_TYPE_MAGNETIC_FIELD,
1500.0f, 1.0f/20.0f,
0.50f, 100000, { },
},
},
};
struct sensor_extend_t gsensorList[] = {
{
{
"bma250", LSG_BMA250,
}, {
"Bosch 3-axis Accelerometer",
"Bosch",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
4.0f*9.81f,
(4.0f*9.81f)/1024.0f,
0.2f, 0, { },
},
}, {
{
"mma8452", LSG_MMA8452,
}, {
"MMA8452 3-axis Accelerometer",
"Freescale Semiconductor Inc.",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
RANGE_A,
GRAVITY_EARTH/1024.0f,
0.30f, 20000, { },
},
}, {
{
"mma7660", LSG_MMA7660,
}, {
"MMA7660 3-axis Accelerometer",
"Freescale Semiconductor Inc.",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
RANGE_A,
GRAVITY_EARTH/21.0f,
0.30f, 20000, { },
},
}, {
{
"mma865x", LSG_MMA865X,
}, {
"MMA865x 3-axis Accelerometer",
"Freescale Semiconductor Inc.",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
RANGE_A,
GRAVITY_EARTH/1024.0f,
0.30f, 20000, { },
},
}, {
{
"stk8312", LSG_STK8312,
},
{
"stk8312 detect Accelerometer",
"STK",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
6.0f*9.81f,
(9.81f)/21.33f,
0.2f, 0, { } ,
},
}, {
{
"afa750", LSG_AFA750,
}, {
"AFA750 3-axis Accelerometer",
"AFA",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
4.0f*9.81f,
GRAVITY_EARTH/4096.0f,
0.8f, 0, { } ,
},
}, {
{
"stk8313", LSG_STK8313,
}, {
"stk8313 detect Accelerometer",
"STK",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
8.0f*9.81f,
(16.0f*9.81f)/4096.0f,
0.2f, 0, { } ,
},
}, {
{
"lis3de_acc", LSG_LIS3DE_ACC,
}, {
"lis3de detect Accelerometer",
"ST",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
4.0f*9.81f,
(4.0f*9.81f)/1024.0f,
0.2f, 0, { },
},
}, {
{
"lis3dh_acc", LSG_LIS3DH_ACC,
}, {
"lis3dh detect Accelerometer",
"ST",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
4.0f*9.81f,
(4.0f*9.81f)/256.0f,
0.2f, 0, { } ,
},
}, {
{
"lsm303d_acc", LSG_LSM303D_ACC,
}, {
"lsm303d detect Accelerometer",
"ST",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
4.0f*9.81f,
(4.0f*9.81f)/256.0f,
0.2f, 0, { },
},
}, {
{
"FreescaleAccelerometer",
LSG_FXOS8700_ACC,
}, {
"Freescale 3-axis Accelerometer",
"Freescal",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
2.0f*9.81f,
9.81f/16384.0f,
0.30f, 20000, { },
},
}, {
{
"kxtik", LSG_KXTIK,
}, {
"Kionix 3-axis Accelerometer",
"Kionix, Inc.",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
RANGE_A,
RESOLUTION_A,
0.23f, 20000, { },
},
}, {
{
"dmard10", LSG_DMARD10,
}, {
"DMARD10 3-axis Accelerometer",
"DMT",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
(GRAVITY_EARTH * 2.0f),
GRAVITY_EARTH / 1024,
0.145f, 0, { },
},
}, {
{
"dmard06", LSG_DMARD06,
}, {
"DMARD06 3-axis Accelerometer",
"DMT",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
(GRAVITY_EARTH * 3.0f),
GRAVITY_EARTH/256.0f,
0.5f, 0, { },
},
}, {
{
"mxc622x", LSG_MXC622X,
}, {
"mxc622x 2-axis Accelerometer",
"MEMSIC",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
2.0,
GRAVITY_EARTH/64.0f,
0.005, 0, { },
},
}, {
{
"lis35de", LSG_LIS35DE,
},
{
"lis3dh detect Accelerometer",
"ST",
1, 0,
SENSOR_TYPE_ACCELEROMETER,
4.0f*9.81f,
(4.0f*9.81f)/15.0f,
0.2f, 0, { } ,
},
},
};
void printkSensor(int number)
{
ALOGD("name:%s\n", sSensorList[number].name);
ALOGD("vendor:%s\n", sSensorList[number].vendor);
ALOGD("handle:%d\n", sSensorList[number].handle);
}
void setSensorData(int number,struct sensor_t *sensorList)
{
sSensorList[number].name = sensorList->name;
sSensorList[number].vendor = sensorList->vendor;
sSensorList[number].version = sensorList->version;
sSensorList[number].handle = sensorList->handle;
sSensorList[number].type = sensorList->type;
sSensorList[number].maxRange = sensorList->maxRange;
sSensorList[number].resolution = sensorList->resolution;
sSensorList[number].power = sensorList->power;
sSensorList[number].minDelay = sensorList->minDelay;
memcpy(sSensorList[number].reserved,sensorList->reserved,
sizeof(sSensorList[number].reserved));
}
int getDevice(struct sensor_extend_t list[], struct sensor_info *info,
char buf[], char classPath[], int number )
{
int ret = 0;
if ((!strlen(buf)) || (list == NULL)) {
return 0;
}
while(ret < number){
if (!strncmp(buf, list[ret].sensors.name, strlen(buf))) {
info->priData = list[ret].sensors.lsg;
setSensorData(sNumber, &list[ret].sList);
strncpy(info->sensorName, buf,strlen(buf));
strncpy(info->classPath, classPath, strlen(classPath));
#ifdef DEBUG_SENSOR
ALOGD("sensorName:%s,classPath:%s,lsg:%f\n",
info->sensorName,info->classPath, info->priData);
#endif
return 1 ;
}
ret++;
}
return 0;
}
int otherDeviceDetect(char buf[])
{
int number0 = 0, number1 = 0;
int re_value = 0;
number0 = ARRAY_SIZE(otherDevice);
number1 = ARRAY_SIZE(ctpDevice);
#ifdef DEBUG_SENSOR
ALOGD("buf:%s, flag0:%d, flag1:%d\n", buf, flag0, flag1);
ALOGD("otherDevice[0].isFind:%d,otherDevice[1].isFind:%d\n",
otherDevice[0].isFind,
otherDevice[1].isFind);
#endif
if(!flag0) {
while(number0--) {
if(!otherDevice[number0].isFind){
if (!strncmp(buf, otherDevice[number0].name,
strlen(otherDevice[number0].name))) {
otherDevice[number0].isFind = 1;
re_value = 1;
}
}
}
}
if(!flag1) {
while(number1--) {
if(!ctpDevice[number1].isFind){
if (!strncmp(buf, ctpDevice[number1].name,
strlen(ctpDevice[number1].name))) {
ctpDevice[number1].isFind = 1;
flag1 = 1;
re_value = 1;
}
}
}
}
if((otherDevice[0].isFind == 1) && (otherDevice[1].isFind == 1))
flag0 = 1;
if((flag0 == 1) && (flag1 == 1)) {
return 2;
}
if(re_value == 1)
return 1;
return 0;
}
void statusInit(void)
{
seStatus[ID_A].isUsed = true;
seStatus[ID_A].isFound = false;
seStatus[ID_M].isUsed = true;
seStatus[ID_M].isFound = false;
seStatus[ID_GY].isUsed = true;
seStatus[ID_GY].isFound = false;
seStatus[ID_L].isUsed = true;
seStatus[ID_L].isFound = false;
seStatus[ID_PX].isUsed = true;
seStatus[ID_PX].isFound = false;
seStatus[ID_O].isUsed = true;
seStatus[ID_O].isFound = false;
seStatus[ID_T].isUsed = true;
seStatus[ID_T].isFound = false;
seStatus[ID_P].isUsed = true;
seStatus[ID_P].isFound = false;
}
int searchDevice(char buf[], char classPath[])
{
int ret = 0;
if(!found) {
found = otherDeviceDetect(buf);
#ifdef DEBUG_SENSOR
ALOGD("found:%d\n", found);
#endif
if(found == 1) {
found = 0;
return 1;
}else if(found == 2) {
return 1;
}
}
if((seStatus[ID_A].isUsed == true) && (seStatus[ID_A].isFound == false)) {
ret = getDevice(gsensorList, &gsensorInfo, buf, classPath,
ARRAY_SIZE(gsensorList));
if(ret == 1){
seStatus[ID_A].isFound = true;
sNumber++;
return 1;
}
}
if((seStatus[ID_M].isUsed == true) && (seStatus[ID_M].isFound == false)) {
ret = getDevice(magSensorList, &magSensorInfo, buf, classPath,
ARRAY_SIZE(magSensorList));
if(ret == 1){
seStatus[ID_M].isFound = true;
sNumber++;
if(!strncmp(buf, magSensorList[1].sensors.name, strlen(buf))) {
setSensorData(sNumber, &oriSensorList[0].sList);
sNumber++;
}
return 1;
}
}
if((seStatus[ID_GY].isUsed == true) && (seStatus[ID_GY].isFound == false)) {
ret = getDevice(gyrSensorList, &gyrSensorInfo, buf, classPath,
ARRAY_SIZE(gyrSensorList));
if(ret == 1){
seStatus[ID_GY].isFound = true;
sNumber++;
return 1;
}
}
if((seStatus[ID_L].isUsed == true) && (seStatus[ID_L].isFound == false)) {
ret = getDevice(ligSensorList, &ligSensorInfo, buf, classPath,
ARRAY_SIZE(ligSensorList));
if(ret == 1){
seStatus[ID_L].isFound = true;
sNumber++;
return 1;
}
}
if((seStatus[ID_PX].isUsed == true) && (seStatus[ID_PX].isFound == false)) {
ret = getDevice(proSensorList, &proSensorInfo, buf, classPath,
ARRAY_SIZE(proSensorList));
if(ret == 1){
seStatus[ID_PX].isFound = true;
sNumber++;
return 1;
}
}
if((seStatus[ID_O].isUsed == true) && (seStatus[ID_O].isFound == false)) {
ret = getDevice(oriSensorList, &oriSensorInfo, buf, classPath,
ARRAY_SIZE(oriSensorList));
if(ret == 1){
seStatus[ID_O].isFound = true;
sNumber++;
return 1;
}
}
if((seStatus[ID_T].isUsed == true) && (seStatus[ID_T].isFound == false)) {
ret = getDevice(tempSensorList, &tempSensorInfo, buf, classPath,
ARRAY_SIZE(tempSensorList));
if(ret == 1){
seStatus[ID_T].isFound = true;
sNumber++;
return 1;
}
}
if((seStatus[ID_P].isUsed == true) && (seStatus[ID_P].isFound == false)) {
ret = getDevice(preSensorList, &preSensorInfo, buf, classPath,
ARRAY_SIZE(preSensorList));
if(ret == 1){
seStatus[ID_T].isFound = true;
sNumber++;
return 1;
}
}
return 0;
}
int sensorsDetect(void)
{
char *dirname =(char *) "/sys/class/input";
char buf[256];
char classPath[256];
int res;
DIR *dir;
struct dirent *de;
int fd = -1;
int ret = 0;
memset(&buf,0,sizeof(buf));
statusInit();
dir = opendir(dirname);
if (dir == NULL)
return -1;
while((de = readdir(dir))) {
if (strncmp(de->d_name, "input", strlen("input")) != 0) {
continue;
}
sprintf(classPath, "%s/%s", dirname, de->d_name);
snprintf(buf, sizeof(buf), "%s/name", classPath);
fd = open(buf, O_RDONLY);
if (fd < 0) {
continue;
}
if ((res = read(fd, buf, sizeof(buf))) < 0) {
close(fd);
continue;
}
buf[res - 1] = '\0';
#ifdef DEBUG_SENSOR
ALOGD("buf:%s\n", buf);
#endif
ret = searchDevice(buf, classPath);
close(fd);
fd = -1;
}
closedir(dir);
return 0;
}