xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/allwinner/camera/CameraUI/src/AutoMount.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /***************************************
2 FileName:
3 Copyright:
4 Author:
5 Description:
6 ***************************************/
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <sys/types.h>
13 #include <sys/ipc.h>
14 #include <sys/msg.h>
15 #include <errno.h>
16 #include <sys/time.h>
17 #include <sdklog.h>
18 #include <dirent.h>
19 #include<iostream>
20 #include "CameraFileCfg.h"
21 #include "StorageManager.h"
22 #include "AutoMount.h"
23 
24 #include "main_desktop.h"
25 using namespace std;
26 
27 /************Macro*********************/
28 
29 /************Variable******************/
30 //int g_recordstatus = 0;
31 extern dvr_factory **g_ppDvr;
32 
33 
34 /************Function******************/
35 void AutoMountDev(void);
36 
37 
RdyRecord(bool bRdy)38 void RdyRecord(bool bRdy)
39 {
40 	printf("===RdyRecord,bRdy:%d\n",bRdy);
41 
42 	dvr_factory **pDvrFac = g_ppDvr; //mod lss
43 	if(NULL == pDvrFac){
44 		printf("pDvrFac is NULL\n");
45 	}
46 
47 	int iCnt = 0;
48 	if(bRdy){
49 		for(iCnt = 0; iCnt < MAX_RED_VIDEOS; iCnt++,*pDvrFac++){
50 			(*pDvrFac)->startRecord();
51 			(*pDvrFac)->SetCurRecordStat(RecordStat_NORMAL);
52 		}
53 	}else {
54 		for(iCnt = 0; iCnt < MAX_RED_VIDEOS; iCnt++,*pDvrFac++){
55 			if(RecordStat_NORMAL == (*pDvrFac)->GetCutRecortStat()){
56 				(*pDvrFac)->stopRecord();
57 				(*pDvrFac)->SetCurRecordStat(RecordStat_STOPCAR);
58 			}
59 		}
60 	}
61 
62 }
63 
64 /**************************************
65 Function:
66 Description:
67 ***************************************/
AutoMountInit(void * usrdata)68 void AutoMountInit(void *usrdata)
69 {
70     	NetlinkManager *nm;
71 	AutEvent *pevent = AutEvent::Instance();
72 
73 	if (!(nm = NetlinkManager::Instance())) {
74 		SLOGE("Unable to create NetlinkManager");
75 		ALOGV("can't get the netlinkManager's instance\n");
76 	};
77 
78 	pevent->setEventCb(AutoMountCallback, (void *) usrdata);
79 	if (nm->start()) {
80 		SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
81 	}
82 
83 	AutoMountDev();
84 }
85 
86 /**************************************
87 Function:
88 Description:
89 
90 sd[a-z][0-9]    0:0 666            @/etc/mdev/udisk_insert
91 sd[a-z]            0:0 666        $/etc/mdev/udisk_remove
92 
93 mmcblk1p[0-9]  0:0 666        @/etc/mdev/sdcard_insert
94 mmcblk1        0:0 666        $/etc/mdev/sdcard_remove
95 
96 ***************************************/
AutoMountDev(void)97 void AutoMountDev(void)
98 {
99 	DIR* dir;// = opendir("/dev/");//��ָ��Ŀ¼
100 	dirent* p = NULL;//�������ָ��
101 	char sdbuf[SAVE_MAX][10],usbbuf[SAVE_MAX][10];
102 	int sdlen,usblen,i,flag;
103 
104 	char buf[256];
105 	int cnt=0;
106 	int muti_en=MUTI_PARTS_EN;
107 	int rm_path_en=RM_PATH_EN;
108 
109 	ALOGV("----AutoMountDev start----\n");
110 
111 	printf("=AutoMountDev =\n");
112 
113 	sdlen=0;
114 	usblen=0;
115 
116 	dir=opendir("/dev/");
117 	while((p = readdir(dir)) != NULL)//��ʼ�������
118 	{
119 		if(p->d_name[0] != '.')//d_name��һ��char���飬��ŵ�ǰ���������ļ���
120 		{
121 
122 			//sdcard
123 			if( (strlen(p->d_name)>=SDCARD_LEN) && (strstr(p->d_name,SDCARD_TYPE)!=NULL) ){
124 				ALOGV("scan sdcard  %s \n",p->d_name);
125 
126 				if(sdlen<SAVE_MAX){
127 					strcpy(sdbuf[sdlen++],p->d_name);
128 				}
129 			}
130 
131 
132 			//usb
133 			if( (strlen(p->d_name)>=USB_LEN) && (strstr(p->d_name,USB_TYPE)!=NULL) ){
134 				if(strcmp(p->d_name,"sda")!=0){
135 					ALOGV("scan usb  %s \n",p->d_name);
136 
137 					if(usblen<SAVE_MAX){
138 						strcpy(usbbuf[usblen++],p->d_name);
139 					}
140 
141 				}
142 			}
143 
144 		}
145 	}
146 	closedir(dir);//�ر�ָ��Ŀ¼
147 
148 	// mount sdcard
149 	if(sdlen==0){
150 		//check
151 		dir=opendir("/mnt/sdcard/");
152 		if(dir!=NULL){
153 			i=0;
154 			while((p = readdir(dir)) != NULL){
155 
156 				ALOGV("check sdcard 0  %s \n",p->d_name);
157 
158 				if(p->d_name[0] != '.')
159 				{
160 					i++;
161 					ALOGV("check sdcard  %s \n",p->d_name);
162 					sprintf(buf,"umount /mnt/sdcard/%s",p->d_name);
163 					system(buf);
164 				}
165 			}
166 
167 			closedir(dir);
168             if(rm_path_en){
169                 sprintf(buf,"rm -rf /mnt/sdcard/");
170                 system(buf);
171             }
172 		}
173 	}
174 	else if(sdlen==1){
175 
176 		sprintf(buf,"/mnt/sdcard/%s",sdbuf[0]);
177 		if(!isMounted(buf)){
178 			ALOGV("mount sdcard 1 %s \n",sdbuf[0]);
179 
180 			sprintf(buf,"mount -t vfat /dev/%s /mnt/sdcard/mmcblk1p1 -o defaults,noatime,async,iocharset=cp936",sdbuf[0]);
181 			system("mkdir -p /mnt/sdcard/mmcblk1p1");
182 			system(buf);
183 
184 			sprintf(buf,"/mnt/sdcard/%s",sdbuf[0]);
185 			if(!isMounted(buf) && rm_path_en){
186 				sprintf(buf,"rm-rf /mnt/sdcard/%s",sdbuf[0]);
187 				system(buf);
188 			}
189 		}
190 	}
191 	else if(sdlen>1 && muti_en){
192 
193 
194 		for(i=0;i<sdlen;i++){
195 
196 			sprintf(buf,"/mnt/sdcard/%s",sdbuf[i]);
197 			if(!isMounted(buf)){
198 
199 				ALOGV("mount sdcard m %s \n",sdbuf[i]);
200 
201 				sprintf(buf,"mkdir -p /mnt/sdcard/%s",sdbuf[i]);
202 				system(buf);
203 				sprintf(buf,"mount -t vfat /dev/%s /mnt/sdcard/%s -o defaults,noatime,async,iocharset=cp936",sdbuf[i],sdbuf[i]);
204 
205 				system(buf);
206 
207 				sprintf(buf,"/mnt/sdcard/%s",sdbuf[i]);
208 				if(!isMounted(buf) && rm_path_en){
209 					sprintf(buf,"rm -rf /mnt/sdcard/%s",sdbuf[i]);
210 					system(buf);
211 				}
212 			}
213 		}
214 
215 	}
216 
217 
218 	// mount usb
219 	if(usblen==0){
220 		dir=opendir("/mnt/usb/");
221 		if(dir!=NULL){
222 			i=0;
223 			while((p = readdir(dir)) != NULL){
224 
225 				if(p->d_name[0] != '.')
226 				{
227 					i++;
228 					ALOGV("check usb  %s \n",p->d_name);
229 
230 					sprintf(buf,"umount /mnt/usb/%s",p->d_name);
231 					system(buf);
232 				}
233 			}
234 			closedir(dir);
235 
236             if(rm_path_en){
237                 sprintf(buf,"rm -rf /mnt/usb/");
238                 system(buf);
239             }
240 		}
241 
242 	}
243 	if(usblen==1){
244 		sprintf(buf,"/mnt/usb/usb1");
245 		if(!isMounted(buf)){
246 
247 			ALOGV("mount usb 1 %s \n",usbbuf[0]);
248 
249 			system("mkdir -p /mnt/usb/usb1");
250 			sprintf(buf,"mount -t vfat /dev/%s /mnt/usb/usb1 -o defaults,noatime,async,iocharset=cp936",usbbuf[0]);
251 			system(buf);
252 
253 			sprintf(buf,"/mnt/usb/usb1");
254 
255 			if(!isMounted(buf)){
256 				sprintf(buf,"rm -rf /mnt/usb/usb1");
257 				system(buf);
258 			}
259 
260 		}
261 
262 	}
263 	else if(usblen>1 && muti_en){
264 		for(i=0;i<usblen;i++){
265 
266 			sprintf(buf,"mkdir -p /mnt/usb/%s",usbbuf[i]);
267 			if(!isMounted(buf)){
268 
269 				ALOGV("mount usb m %s \n",usbbuf[i]);
270 
271 				sprintf(buf,"mkdir -p /mnt/usb/%s",usbbuf[i]);
272 				system(buf);
273 				sprintf(buf,"mount -t vfat /dev/%s /mnt/usb/%s -o defaults,noatime,async,iocharset=cp936",usbbuf[i],usbbuf[i]);
274 				system(buf);
275 
276 				sprintf(buf,"/mnt/usb/%s",usbbuf[i]);
277 				if(!isMounted(buf) && rm_path_en){
278 					sprintf(buf,"rm -rf /mnt/usb/%s",usbbuf[i]);
279 					system(buf);
280 				}
281 			}
282 		}
283 	}
284 
285 }
286 
287 /**************************************
288 Function:
289 Description:
290 ***************************************/
AutoMountCallback(NetlinkEvent * evt,void * usrdata)291 void AutoMountCallback(NetlinkEvent * evt, void *usrdata)
292 {
293 	DIR* dir;
294 	dirent* p = NULL;//�������ָ��
295 	char buf[256];
296 	int i;
297 
298 	int muti_en=MUTI_PARTS_EN;
299 	int rm_path_en=RM_PATH_EN;
300 	int is_mouted=false;
301 
302 	ALOGV("event act=%d %s ", evt->getAction(), evt->getSubsystem());
303 
304 	int action = evt->getAction();
305 	const char *devtype = evt->findParam("DEVTYPE");
306 
307 	if (action == NetlinkEvent::NlActionAdd) {
308 		int major = atoi(evt->findParam("MAJOR"));
309 		int minor = atoi(evt->findParam("MINOR"));
310 		char nodepath[255];
311 
312 		ALOGV("AutoMountCallback add  \n");
313 
314 
315 		if (!strcmp(devtype, "disk")) {
316 
317 			ALOGV("AutoMountCallback disk  \n");
318 
319 			ALOGV("line=%d,devtype=%s", __LINE__, devtype);
320 			//handleDiskAdded(dp, evt);
321 			char bufname[512];
322 			memset(bufname, 0, 512);
323 			char tmpbuf[256];
324 			char real_path[256];
325 			config_get_curfiledir(0, bufname);
326 			int len = strlen(bufname);
327 			const char *tmp = evt->findParam("NPARTS");
328 			if (tmp) {
329 				int npart = atoi(tmp);
330 
331 				if (npart == 0) {
332 					const char *devname = evt->findParam("DEVNAME");
333 					ALOGV("************devname=%s", devname);
334 					if (strstr(devname, "sd")!=NULL) {
335 						system("mkdir -p /mnt/usb/usb1");
336 						sprintf(buf,"mount -t vfat /dev/%s /mnt/usb/usb1 -o defaults,noatime,async,iocharset=cp936",devname);
337 						system(buf);
338 						sleep(1);
339 						sprintf(buf,"/mnt/usb/usb1");
340 						if(!isMounted(buf) && rm_path_en){
341 							sprintf(buf,"rm -rf /mnt/usb/");
342 							system(buf);
343 						}
344 					}
345 					else if (strstr(devname, "mmcblk1")!=NULL){
346 						system("mkdir -p /mnt/sdcard/mmcblk1p1");
347 						sprintf(buf,"mount -t vfat /dev/%s /mnt/sdcard/mmcblk1p1 -o defaults,noatime,async,iocharset=cp936",devname);
348 						system(buf);
349 						sleep(1);
350 						sprintf(buf,"/mnt/sdcard/%s",devname);
351 						if(!isMounted(buf) ){
352 							if(rm_path_en){
353 								sprintf(buf,"rm -rf /mnt/sdcard/");
354 								system(buf);
355 							}
356 						}else{
357 							is_mouted=true;
358 						}
359 					}
360 					else{
361 						ALOGV("mount fail \n");
362 					}
363 				}
364 
365 
366 				else if(npart>0 && muti_en){
367 					const char *devname = evt->findParam("DEVNAME");
368 					if (strstr(devname, "sd")!=NULL) {
369 						dir=opendir("/dev/");
370 						if(dir!=NULL){
371 							while((p = readdir(dir)) != NULL)
372 							{
373 								//usb
374 								if( (strlen(p->d_name)>=USB_LEN) && (strstr(p->d_name,USB_TYPE)!=NULL) ){
375 
376 									sprintf(buf,"/mnt/usb/%s",p->d_name);
377 									if(!isMounted(buf)){
378 
379 										ALOGV("in usb  %s \n",p->d_name);
380 
381 										sprintf(buf,"mkdir -p /mnt/usb/%s",p->d_name);
382 										system(buf);
383 										sprintf(buf,"mount -t vfat /dev/%s /mnt/usb/%s -o defaults,noatime,async,iocharset=cp936",p->d_name,p->d_name);
384 										system(buf);
385 										sleep(1);
386 										sprintf(buf,"/mnt/usb/%s",p->d_name);
387 										if(!isMounted(buf) && rm_path_en){
388 											sprintf(buf,"rm -rf /mnt/usb/s%",p->d_name);
389 											system(buf);
390 										}
391 									}
392 								}
393 							}
394 							closedir(dir);
395 						}
396 					}
397 					else if (strstr(devname, "mmcblk1")!=NULL){
398 
399 						dir=opendir("/dev/");
400 						if(dir!=NULL){
401 							while((p = readdir(dir)) != NULL)
402 							{
403 								//sdcard
404 								if( (strlen(p->d_name)>=SDCARD_LEN) && (strstr(p->d_name,SDCARD_TYPE)!=NULL) ){
405 
406 									sprintf(buf,"/mnt/sdcard/%s",p->d_name);
407 									if(!isMounted(buf)){
408 										ALOGV("in sdcard  %s \n",p->d_name);
409 
410 										sprintf(buf,"mkdir -p /mnt/sdcard/%s",p->d_name);
411 										system(buf);
412 										sprintf(buf,"mount -t vfat /dev/%s /mnt/sdcard/%s -o defaults,noatime,async,iocharset=cp936",p->d_name,p->d_name);
413 										system(buf);
414 										sleep(1);
415 										sprintf(buf,"/mnt/sdcard/%s",p->d_name);
416 										if(!isMounted(buf)){
417 											if(rm_path_en){
418 												sprintf(buf,"rm -rf /mnt/sdcard/s%",p->d_name);
419 												system(buf);
420 											}
421 										}else{
422 											is_mouted=true;
423 										}
424 									}
425 								}
426 							}
427 							closedir(dir);
428 						}
429 
430 					}
431 					else{
432 						ALOGV("mount fail \n");
433 					}
434 
435 
436 				}
437 			}
438 			else {
439 				SLOGW("Kernel block uevent missing 'NPARTS'");
440 				//mDiskNumParts = 1;
441 			}
442 
443 			/*
444 			strcpy(tmpbuf, bufname);
445 			for (int i = 0; i < len; i++) {
446 				if (tmpbuf[len - 1 - i] == '/') {
447 					tmpbuf[len - 1 - i] = 0;
448 				}
449 				else {
450 					break;
451 				}
452 			}
453 
454 			int rmret = readlink(tmpbuf, real_path, sizeof(real_path));
455 			if (rmret < 0) {
456 				ALOGV("mount path not a symlink %s err=%d", tmpbuf, errno);
457 
458 				strcpy(real_path, tmpbuf);
459 			}
460 			else {
461 				ALOGV("mount real path is %s \r\n", real_path);
462 			}
463 			for (int i = 0; i < 6; i++) {
464 				if (isMounted(real_path))
465 					break;
466 
467 				sleep(1);
468 			}
469 			*/
470 
471 			RdyRecord(true);
472 		}
473 
474 	}
475 	else if (action == NetlinkEvent::NlActionRemove) {
476 		if (!strcmp(devtype, "disk")) {
477 
478 
479 			ALOGV("==AutoMountCallback remove===  \n");
480 
481 			RdyRecord(false);
482 
483 			const char *tmp = evt->findParam("NPARTS");
484 			if (tmp) {
485 				int npart = atoi(tmp);
486 				const char *devname = evt->findParam("DEVNAME");
487 				char buf[256];
488 
489 
490 				ALOGV("AutoMountCallback remove %s \n",devname);
491 
492 				int cnt=0;
493 
494 				if (strstr(devname, "mmcblk1")!=NULL){
495 					dir=opendir("/mnt/sdcard/");
496 					if(dir!=NULL){
497 
498 						while((p = readdir(dir)) != NULL)
499 						{
500 							i=0;
501 							if(p->d_name[0] != '.')
502 							{
503 
504 								ALOGV("out sdcard  %s \n",p->d_name);
505 
506 								while(i<3){
507 									sprintf(buf,"umount /mnt/sdcard/%s",p->d_name);
508 									system(buf);
509 
510 									sprintf(buf,"/mnt/sdcard/%s",p->d_name);
511 									if(isMounted(buf)){
512 										ALOGV("sdcard retry\n");
513 										i++;
514 										sleep(3);
515 									}else{
516 										ALOGV("sdcard break\n");
517 										break;
518 									}
519 								}
520 								if(i<3  && rm_path_en){
521 									sprintf(buf,"rm -rf  /mnt/sdcard/%s",p->d_name);
522 									system(buf);
523 								}
524 							}
525 						}
526 						closedir(dir);//�ر�ָ��Ŀ¼
527 					}
528                     if(rm_path_en){
529                         sprintf(buf,"rm -rf  /mnt/sdcard/");
530                         system(buf);
531                     }
532 				}
533 
534 				if (strstr(devname, "sd")!=NULL){
535 					dir=opendir("/mnt/usb/");
536 
537 					if(dir!=NULL){
538 						while((p = readdir(dir)) != NULL)
539 						{
540 
541 							i=0;
542 							if(p->d_name[0] != '.' )
543 							{
544 								ALOGV("out usb  %s \n",p->d_name);
545 
546 								while(i<3){
547 									sprintf(buf,"umount /mnt/usb/%s",p->d_name);
548 									system(buf);
549 
550 									sprintf(buf,"/mnt/usb/%s",p->d_name);
551 									if(isMounted(buf)){
552 										ALOGV("usb retry\n");
553 										i++;
554 										sleep(3);
555 									}else{
556 										ALOGV("usb break\n");
557 										break;
558 									}
559 								}
560 
561 								if(i<3 && rm_path_en){
562 									sprintf(buf,"rm -rf  /mnt/usb/%s",p->d_name);
563 									system(buf);
564 								}
565 							}
566 						}
567 						closedir(dir);//�ر�ָ��Ŀ¼
568 					}
569                     if(rm_path_en){
570                         sprintf(buf,"rm -rf  /mnt/usb/");
571                         system(buf);
572                     }
573 				}
574 
575 			}
576 		}
577 	}
578 
579 }
580 
581 
582 /**************************************
583 Function:
584 Description:
585 ***************************************/
enhanceUsbStability(NetlinkEvent * evt)586 void enhanceUsbStability(NetlinkEvent *evt)
587 {
588     char path[256];
589     const char *tmp = evt->findParam("DEVNAME");
590     char devname[4];
591     FILE *fp;
592     int msecs = 1000;
593 
594     if (tmp && !strncmp(tmp, "sd", 2)) {
595        strncpy(devname,tmp,3);
596        devname[3] = '\0';
597        sprintf(path, "/sys/block/sda/events_poll_msecs", devname);
598 
599        //usleep(1000 * 1000);
600        if ((fp = fopen(path, "r+ "))) {
601            if (fprintf(fp, "%d\n", msecs) <= 0) {
602                SLOGE("Failed to write %s (%s)", path, strerror(errno));
603            }
604            SLOGD("Success to write %d to %s ", msecs, path);
605            fclose(fp);
606        } else {
607            SLOGE("Failed to open %s (%s)", path, strerror(errno));
608        }
609  }
610 }
611 
612 /**************************************
613 end
614 ***************************************/
615