summaryrefslogtreecommitdiffstats
path: root/wcnss-service/wcnss_service.c
blob: a0caa3e7e7c5644a4483eee9fd87a7c78db8b40c (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
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*--------------------------------------------------------------------------
Copyright (c) 2013, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of The Linux Foundation nor
      the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <ctype.h>
#include <grp.h>
#include <utime.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <unistd.h>
#define LOG_TAG "wcnss_service"
#include <cutils/log.h>
#include <cutils/properties.h>
#ifdef WCNSS_QMI
#include "wcnss_qmi_client.h"
#ifdef MDM_DETECT
#include "mdm_detect.h"
#endif
#endif
#ifdef WCNSS_QMI_OSS
#include <dlfcn.h>
#endif

#define SUCCESS 0
#define FAILED -1
#define BYTE_0  0
#define BYTE_1  8
#define BYTE_2  16
#define BYTE_3  24
#define UNUSED(x)	(void)(x)

#define MAX_FILE_LENGTH    (1024)
#define WCNSS_MAX_CMD_LEN  (128)

/* control messages to wcnss driver */
#define WCNSS_USR_CTRL_MSG_START    0x00000000
#define WCNSS_USR_SERIAL_NUM        (WCNSS_USR_CTRL_MSG_START + 1)
#define WCNSS_USR_HAS_CAL_DATA      (WCNSS_USR_CTRL_MSG_START + 2)
#define WCNSS_USR_WLAN_MAC_ADDR     (WCNSS_USR_CTRL_MSG_START + 3)


#define WCNSS_CAL_CHUNK (3*1024)
#define WCNSS_CAL_FILE  "/data/misc/wifi/WCNSS_qcom_wlan_cal.bin"
#define WCNSS_FACT_FILE "/data/misc/wifi/WCN_FACTORY"
#define WCNSS_DEVICE    "/dev/wcnss_wlan"
#define WCNSS_CTRL      "/dev/wcnss_ctrl"
#define WLAN_INI_FILE_DEST   "/data/misc/wifi/WCNSS_qcom_cfg.ini"
#define WLAN_INI_FILE_SOURCE "/vendor/etc/wifi/WCNSS_qcom_cfg.ini"
#define WCNSS_HAS_CAL_DATA\
		"/sys/module/wcnsscore/parameters/has_calibrated_data"
#define WLAN_DRIVER_ATH_DEFAULT_VAL "0"

#define ASCII_A		65
#define ASCII_a		97
#define ASCII_0		48
#define HEXA_A		10
#define HEX_BASE		16

#if defined (WCNSS_QMI) || defined(WCNSS_QMI_OSS)
#define WLAN_ADDR_SIZE   6
unsigned char wlan_nv_mac_addr[WLAN_ADDR_SIZE];
#ifdef WCNSS_QMI_MAC_ADDR_REV
#define MAC_ADDR_ARRAY(a) (a)[5], (a)[4], (a)[3], (a)[2], (a)[1], (a)[0]
#else
#define MAC_ADDR_ARRAY(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#endif
#define MAC_ADDRESS_STR "%02x:%02x:%02x:%02x:%02x:%02x"

/* As we Want to write in 00:0a:f5:11:22:33 format in sysfs file
   so taking mac length as 12 char + 5 for ":" + NULL
 */
#define WLAN_MAC_ADDR_STRING 18
#endif

#define MAX_SOC_INFO_NAME_LEN (15)
#define MAX_DATA_NVBIN_PATH_LEN (64)
#define QRD_DYNAMIC_NV_PROP  "persist.sys.dynamic.nv"
#define QRD_HW_PLATFORM  "QRD"
#define QRD_PLATFORM_SUBTYPE_ID  0
#define PERSIST_NVFILE    "/persist/WCNSS_qcom_wlan_nv.bin"
#define DATA_NVFILE_DIR   "/data/misc/wifi/nvbin/"
#define SYSFS_SOCID_PATH1   "/sys/devices/soc0/soc_id"
#define SYSFS_SOCID_PATH2   "/sys/devices/system/soc/soc0/id"
#define SYSFS_HW_PLATFORM_PATH1  "/sys/devices/soc0/hw_platform"
#define SYSFS_HW_PLATFORM_PATH2   "/sys/devices/system/soc/soc0/hw_platform"
#define SYSFS_PLATFORM_SUBTYPE_PATH1   "/sys/devices/soc0/platform_subtype_id"
#define SYSFS_PLATFORM_SUBTYPE_PATH2    "/sys/devices/system/soc/soc0/platform_subtype_id"
#define SYSFS_PLATFORM_VERSION_PATH1  "/sys/devices/soc0/platform_version"
#define SYSFS_PLATFORM_VERSION_PATH2   "/sys/devices/system/soc/soc0/platform_version"
#define SOCINFO_HWVER_MAJOR(ver) (((ver) & 0x00ff0000) >> 16)
#define SOCINFO_HWVER_MINOR(ver) ((ver) & 0x000000ff)
#define GET_SOC_INFO(buf, soc_node_path1, soc_node_path2, info_got) \
		{  if (get_soc_info(buf, soc_node_path1, soc_node_path2) < 0) \
		    { \
		        ALOGE("get_soc_info failed!\n"); \
		        return FAILED; \
		    } \
		    info_got = atoi(buf); \
		}

int wcnss_write_cal_data(int fd_dev)
{
	int rcount = 0;
	int size = 0;
	int rc = 0;
	int wcount = 0;
	int fd_file;
	struct stat st;

	char buf[WCNSS_CAL_CHUNK];

	ALOGI("wcnss_write_cal_data trying to write cal");

	rc = stat(WCNSS_CAL_FILE, &st);
	if (rc < 0) {
		ALOGE("Failed to stat cal file : %s",
				strerror(errno));
		goto exit;
	}

	size = st.st_size;

	fd_file = open(WCNSS_CAL_FILE, O_RDONLY);
	if (fd_file < 0) {
		ALOGE("cal file doesn't exist: %s",
				strerror(errno));
		rc = fd_file;
		goto exit;
	}

	/* write the file size first, so that platform driver knows
	 * when it recieves the full data */
	wcount = write(fd_dev, (void *)&size, 4);
	if (wcount != 4) {
		ALOGE("Failed to write to wcnss device : %s",
				strerror(errno));
		rc = wcount;
		goto exit_close;
	}

	do {
		rcount = read(fd_file, (void *)buf, sizeof(buf));
		if (rcount < 0) {
			ALOGE("Failed to read from cal file ; %s",
					strerror(errno));
			rc = rcount;
			goto exit_remove;
		}

		if (!rcount)
			break;

		wcount = write(fd_dev, buf, rcount);
		if (wcount < 0) {
			ALOGE("Failed to write to wcnss device : %s",
				strerror(errno));
			rc = wcount;
			goto exit_close;
		}

	} while (rcount);
	close(fd_file);

	return SUCCESS;

exit_remove:
	close(fd_file);
	remove("WCNSS_CAL_FILE");
	return rc;

exit_close:
	close(fd_file);

exit:
	return rc;
}


int wcnss_read_and_store_cal_data(int fd_dev)
{
	int rcount = 0;
	int wcount = 0;
	int fd_file = -1;
	int rc = 0;

	char buf[WCNSS_CAL_CHUNK];

	ALOGI("wcnss_read_and_store_cal_data trying to read cal");

	do {
		/* wait on this read until data comes from fw */
		rcount = read(fd_dev, (void *)buf, sizeof(buf));
		if (rcount < 0) {
			ALOGE("Failed to read from wcnss device : %s",
					strerror(errno));
			rc = rcount;
			goto exit;
		}

		/* truncate the file only if there is fw data, this read
		 * may never return if the fw decides that no more cal is
		 * required; and the data we have now is good enough.
		 */
		if (fd_file < 0) {
			fd_file = open(WCNSS_CAL_FILE, O_WRONLY
					| O_CREAT | O_TRUNC, 0664);
			if (fd_file < 0) {
				ALOGE("Failed to open cal file : %s",
						strerror(errno));
				rc = fd_file;
				goto exit;
			}
		}

		if (!rcount)
			break;

		wcount = write(fd_file, buf, rcount);
		if (wcount < 0) {
			ALOGE("Failed to write to cal file : %s",
				strerror(errno));
			rc = wcount;
			goto exit_remove;
		}

	} while (rcount);

	close(fd_file);

	return SUCCESS;

exit_remove:
	close(fd_file);
	remove(WCNSS_CAL_FILE);

exit:
	return rc;
}


void find_full_path(char *cur_dir, char *file_to_find, char *full_path)
{
	DIR *dir;
	struct stat st;
	struct dirent *dr;
	char cwd[1024];
	int rc;

	chdir(cur_dir);

	dir = opendir(".");

	if (dir != NULL) {
		while ((dr = readdir(dir))) {

			rc = lstat(dr->d_name, &st);
			if (rc < 0) {
				ALOGE("lstat failed %s", strerror(errno));
				return;
			}
			if (S_ISDIR(st.st_mode)) {
				if ((strcmp(dr->d_name, ".")) &&
					(strcmp(dr->d_name, ".."))) {
				find_full_path(dr->d_name,
						file_to_find, full_path);
				}
			} else if (!strcmp(file_to_find, dr->d_name)) {
				getcwd(cwd, sizeof(cwd));
				snprintf(full_path, MAX_FILE_LENGTH, "%s/%s",
					cwd, file_to_find);
			}
		}
		closedir(dir);
	}

	chdir("..");
}

void setup_wlan_config_file()
{
	int rfd;
	int wfd;
	struct stat st_dest, st_src;
	int rc_dest;
	int rc;
	struct group *grp;
	struct utimbuf new_time;

	rc = stat(WLAN_INI_FILE_SOURCE, &st_src);
	if (rc != 0) {
		ALOGE("source file do not exist %s", WLAN_INI_FILE_SOURCE);
		return;
	}

	rc_dest = stat(WLAN_INI_FILE_DEST, &st_dest);
	if (rc_dest == 0 && st_dest.st_size &&
			(st_dest.st_mtime > st_src.st_mtime)) {
		ALOGE("wlan ini file exists %s and is newer than %s",
				WLAN_INI_FILE_DEST, WLAN_INI_FILE_SOURCE);
		goto out_nocopy;
	}

	rfd = open(WLAN_INI_FILE_SOURCE, O_RDONLY);
	if (rfd < 0) {
		ALOGE("Failed to open ini source file: %s", strerror(errno));
		return;
	}

	wfd = open(WLAN_INI_FILE_DEST, O_WRONLY | O_CREAT | O_TRUNC, 0660);
	if (wfd < 0) {
		ALOGE("Failed to open ini dest file: %s", strerror(errno));
		close(rfd);
		return;
	}

	rc = sendfile(wfd, rfd, 0, st_src.st_size);
	if (rc != st_src.st_size) {
		ALOGE("Failed to copy ini file: %s", strerror(errno));
		goto out;
	}

	new_time.actime = st_src.st_atime;
	new_time.modtime = st_src.st_mtime;

	rc = utime(WLAN_INI_FILE_DEST, &new_time);
	if (rc != 0)
		ALOGE("could not preserve the timestamp %s", strerror(errno));

	grp = getgrnam("wifi");
	if (grp != NULL) {
		rc = chown(WLAN_INI_FILE_DEST, -1, grp->gr_gid);
		if (rc != 0)
			ALOGE("Failed change group of ini file %s", strerror(errno));
	} else {
			ALOGE("Failed to get group wifi %s", strerror(errno));
	}

	property_set("wlan.driver.config", WLAN_INI_FILE_DEST);

out:
	close(rfd);
	close(wfd);
	return;

out_nocopy:
	property_set("wlan.driver.config", WLAN_INI_FILE_DEST);
	return;
}
unsigned int convert_string_to_hex(char* string)
{
	int idx;
	unsigned long int hex_num = 0;
	for(idx = 0; string[idx] != '\0'; idx++){
		if(isalpha(string[idx])) {
			if(string[idx] >='a' && string[idx] <='f') {
				hex_num = hex_num * HEX_BASE + ((int)string[idx]
					       - ASCII_a + HEXA_A);
			} else if ( string[idx] >='A' && string[idx] <='F') {
				hex_num = hex_num * HEX_BASE + ((int)string[idx]
						- ASCII_A + HEXA_A);
			} else
				hex_num = hex_num * HEX_BASE + (int)string[idx];
		} else {
			hex_num = hex_num * HEX_BASE + (string[idx]- ASCII_0);
		}
	}
	hex_num = hex_num & 0xFFFFFFFF;
	return hex_num;
}


#if defined(WCNSS_QMI) || defined(WCNSS_QMI_OSS)
void setup_wcnss_parameters(int *cal, int nv_mac_addr)
#else
void setup_wcnss_parameters(int *cal)
#endif
{
	char msg[WCNSS_MAX_CMD_LEN];
	char serial[PROPERTY_VALUE_MAX];
	int fd, rc, pos = 0;
	struct stat st;
	unsigned int serial_num = 0;

	fd = open(WCNSS_CTRL, O_WRONLY);
	if (fd < 0) {
		ALOGE("Failed to open %s : %s", WCNSS_CTRL, strerror(errno));
		return;
	}

	rc = property_get("ro.wcnss.serialno", serial, "");
	if (rc) {
		serial_num = convert_string_to_hex(serial);
		ALOGE("Serial Number is  %x", serial_num);

		msg[pos++] = WCNSS_USR_SERIAL_NUM >> BYTE_1;
		msg[pos++] = WCNSS_USR_SERIAL_NUM >> BYTE_0;
		msg[pos++] = serial_num >> BYTE_3;
		msg[pos++] = serial_num >> BYTE_2;
		msg[pos++] = serial_num >> BYTE_1;
		msg[pos++] = serial_num >> BYTE_0;

		if (write(fd, msg, pos) < 0) {
			ALOGE("Failed to write to %s : %s", WCNSS_CTRL,
					strerror(errno));
			goto fail;
		}
	}

#if defined(WCNSS_QMI) || defined (WCNSS_QMI_OSS)
	if (SUCCESS == nv_mac_addr)
	{
		pos = 0;
		msg[pos++] = WCNSS_USR_WLAN_MAC_ADDR >> BYTE_1;
		msg[pos++] = WCNSS_USR_WLAN_MAC_ADDR >> BYTE_0;
#ifdef WCNSS_QMI_MAC_ADDR_REV
		msg[pos++] = wlan_nv_mac_addr[5];
		msg[pos++] = wlan_nv_mac_addr[4];
		msg[pos++] = wlan_nv_mac_addr[3];
		msg[pos++] = wlan_nv_mac_addr[2];
		msg[pos++] = wlan_nv_mac_addr[1];
		msg[pos++] = wlan_nv_mac_addr[0];
#else
		msg[pos++] = wlan_nv_mac_addr[0];
		msg[pos++] = wlan_nv_mac_addr[1];
		msg[pos++] = wlan_nv_mac_addr[2];
		msg[pos++] = wlan_nv_mac_addr[3];
		msg[pos++] = wlan_nv_mac_addr[4];
		msg[pos++] = wlan_nv_mac_addr[5];
#endif

		ALOGI("WLAN MAC Addr:" MAC_ADDRESS_STR,
			MAC_ADDR_ARRAY(wlan_nv_mac_addr));

		if (write(fd, msg, pos) < 0) {
			ALOGE("Failed to write to %s : %s", WCNSS_CTRL,
						strerror(errno));
			goto fail;
		}
	}
#endif

	pos = 0;
	msg[pos++] = WCNSS_USR_HAS_CAL_DATA >> BYTE_1;
	msg[pos++] = WCNSS_USR_HAS_CAL_DATA >> BYTE_0;

	rc = stat(WCNSS_FACT_FILE, &st);
	if (rc == 0) {
		ALOGE("Factory file found, deleting cal file");
		unlink(WCNSS_CAL_FILE);
		goto fail_resp;
	}

	rc = stat(WCNSS_CAL_FILE, &st);
	if (rc != 0) {
		ALOGE("CAL file not found");
		goto fail_resp;
	}

	/* has cal data */
	msg[pos++] = 1;

	if (write(fd, msg, pos) < 0) {
		ALOGE("Failed to write to %s : %s", WCNSS_CTRL,
				strerror(errno));
		goto fail;
	}

	ALOGI("Correctly triggered cal file");
	*cal = SUCCESS;
	close(fd);
	return;

fail_resp:
	msg[pos++] = 0;
	if (write(fd, msg, pos) < 0)
		ALOGE("Failed to write to %s : %s", WCNSS_CTRL,
				strerror(errno));

fail:
	*cal = FAILED;
	close(fd);
	return;
}

void setup_wlan_driver_ath_prop()
{
	property_set("wlan.driver.ath", WLAN_DRIVER_ATH_DEFAULT_VAL);
}

#ifdef MDM_DETECT
int check_modem_compatability(struct dev_info *mdm_detect_info)
{
	char args[MODEM_BASEBAND_PROPERTY_SIZE] = {0};
	int ret = 0;
	/* Get the hardware property */
	ret = property_get(MODEM_BASEBAND_PROPERTY, args, "");
	if (ret > MODEM_BASEBAND_PROPERTY_SIZE) {
		ALOGE("property [%s] has size [%d] that exceeds max [%d]",
				MODEM_BASEBAND_PROPERTY, ret, MODEM_BASEBAND_PROPERTY_SIZE);
		return 0;
	}
	/* This will check for the type of hardware, and if the
	   hardware type needs external modem, it will check if the
	   modem type is external*/
	if(!strncmp(MODEM_BASEBAND_VALUE_APQ, args, 3)) {

		for (ret = 0; ret < mdm_detect_info->num_modems; ret++) {
			if (mdm_detect_info->mdm_list[ret].type == MDM_TYPE_EXTERNAL) {
				ALOGE("Hardware supports external modem");
				return 1;
			}
		}
		ALOGE("Hardware does not support external modem");
		return 0;
	}
	return 1;
}
#endif

static int read_line_from_file(const char *path, char *buf, size_t count)
{
	char * fgets_ret;
	FILE * fd;
	int rv;

	fd = fopen(path, "r");
	if (fd == NULL)
	return -1;

	fgets_ret = fgets(buf, (int)count, fd);
	if (NULL != fgets_ret) {
	    rv = (int)strlen(buf);
	} else {
	    rv = ferror(fd);
	}

	fclose(fd);

	return rv;
}

static int get_soc_info(char *buf, char *soc_node_path1,
			char *soc_node_path2)
{
	int ret = 0;

	ret = read_line_from_file(soc_node_path1, buf,
					MAX_SOC_INFO_NAME_LEN);
	if (ret < 0) {
		ret = read_line_from_file(soc_node_path2, buf,
					MAX_SOC_INFO_NAME_LEN);
		if (ret < 0) {
		    ALOGE("getting socinfo(%s, %d) failed.\n",
					soc_node_path1, ret);
		    return ret;
		}
	}
	if (ret && buf[ret - 1] == '\n')
		buf[ret - 1] = '\0';

	return ret;
}

static int get_data_nvfile_path(char *data_nvfile_path,
	struct stat *pdata_nvfile_stat)
{
	char target_board_platform[PROP_VALUE_MAX] = {'\0'};
	char buf[MAX_SOC_INFO_NAME_LEN] = {'\0'};
	int  soc_id, platform_subtype_id, platform_version;
	int  major_hwver, minor_hwver;
	int  rc;

	rc = property_get("ro.board.platform", target_board_platform, "");
	if (!rc)
	{
		ALOGE("get ro.board.platform fail, rc=%d(%s)\n",
				rc, strerror(errno));
		return FAILED;
	}

	GET_SOC_INFO(buf, SYSFS_SOCID_PATH1, SYSFS_SOCID_PATH2, soc_id);
	GET_SOC_INFO(buf, SYSFS_PLATFORM_SUBTYPE_PATH1,
			SYSFS_PLATFORM_SUBTYPE_PATH2, platform_subtype_id);
	GET_SOC_INFO(buf, SYSFS_PLATFORM_VERSION_PATH1,
			SYSFS_PLATFORM_VERSION_PATH2, platform_version);

	major_hwver = SOCINFO_HWVER_MAJOR(platform_version);
	minor_hwver = SOCINFO_HWVER_MINOR(platform_version);

	snprintf(data_nvfile_path, MAX_DATA_NVBIN_PATH_LEN,
		"%s%s_%d_0x%02x_0x%02x_0x%02x_nv.bin", DATA_NVFILE_DIR,
		target_board_platform, soc_id, platform_subtype_id&0xff,
		major_hwver&0xff, minor_hwver&0xff);
	ALOGI("data_nvfile_path %s\n",
			data_nvfile_path);

	if (stat(data_nvfile_path, pdata_nvfile_stat) != 0)
	{
		ALOGE("source file do not exist %s\n",
				data_nvfile_path);
		return FAILED;
	}

	return SUCCESS;
}

static int nvbin_sendfile(const char *dst, const char *src,
	struct stat *src_stat)
{
	struct utimbuf new_time;
	int fp_src, fp_dst;
	int rc;
	if ((fp_src = open(src, O_RDONLY)) < 0)
	{
		ALOGE("open %s failed(%s).\n",
				src, strerror(errno));
		return FAILED;
	}

	if ((fp_dst = open(dst, O_WRONLY |O_TRUNC)) < 0)
	{
		close(fp_src);
		ALOGE("open %s failed(%s).\n",
				dst, strerror(errno));
		return FAILED;
	}

	if (sendfile(fp_dst, fp_src, 0, src_stat->st_size) == -1)
	{
		ALOGE("dynamic nv sendfile failed: (%s).\n",
				strerror(errno));
		rc = FAILED;
		goto exit;
	}

	new_time.actime  = src_stat->st_atime;
	new_time.modtime = src_stat->st_mtime;
	if (utime(dst, &new_time) != 0)
	{
		ALOGE("could not preserve the timestamp %s",
				strerror(errno));
		rc = FAILED;
		goto exit;
	}

	rc = SUCCESS;
exit:
	close(fp_dst);
	close(fp_src);
	return rc;
}
void dynamic_nv_replace()
{
	char data_nvfile_path[MAX_DATA_NVBIN_PATH_LEN] = {'\0'};
	char property_nv_replaced_status [PROPERTY_VALUE_MAX] = { '\0' };
	char buf[MAX_SOC_INFO_NAME_LEN] = {'\0'};
	struct stat  data_nvfile_stat;
	int rc;

	if (property_get(QRD_DYNAMIC_NV_PROP, property_nv_replaced_status, NULL)
		&& strcmp(property_nv_replaced_status, "done") == 0) {
		ALOGI("dynamic nv have been replaced. leave\n");
		return;
	}

	rc = get_soc_info(buf, SYSFS_HW_PLATFORM_PATH1, SYSFS_HW_PLATFORM_PATH2);
	if (rc < 0)
	{
		ALOGE("get_soc_info(HW_PLATFORM) fail!\n");
		return;
	} else {
		if( 0 != strncmp(buf, QRD_HW_PLATFORM, MAX_SOC_INFO_NAME_LEN))
		{
			ALOGI("dynamic nv only for QRD platform, current platform:%s.\n",
					buf);
			return;
		}
	}

	rc = get_data_nvfile_path(data_nvfile_path, &data_nvfile_stat);
	if (rc != SUCCESS)
	{
		ALOGE("Get source file path fail !\n");
		return;
	}

	if (property_set(QRD_DYNAMIC_NV_PROP, "replacing") < 0)
	{
		ALOGE("set %s to replacing failed (%s).\n",
				QRD_DYNAMIC_NV_PROP, strerror(errno));
		return;
	}

	rc = nvbin_sendfile(PERSIST_NVFILE, data_nvfile_path, &data_nvfile_stat);
	if ( rc != SUCCESS)
	{
		ALOGE("nvbin_sendfile failed.\n");
		return;
	}

	if (property_set(QRD_DYNAMIC_NV_PROP, "done") < 0)
	{
		ALOGE("set %s to done failed(%s).\n",
				QRD_DYNAMIC_NV_PROP, strerror(errno));
		return;
	}

	ALOGI("dynamic nv replace sucessfully!\n");
}

#ifdef WCNSS_QMI_OSS
static void *wcnss_qmi_handle = NULL;
static int (*wcnss_init_qmi)(void) = NULL;
static int (*wcnss_qmi_get_wlan_address)(unsigned char *) = NULL;
static void (*wcnss_qmi_deinit)(void) = NULL;

static int setup_wcnss_qmi(void)
{
	const char *error = NULL;

	/* initialize the DMS client and request the wlan mac address */
	wcnss_qmi_handle = dlopen("libwcnss_qmi.so", RTLD_NOW);
	if (!wcnss_qmi_handle) {
		ALOGE("Failed to open libwcnss_qmi.so: %s", dlerror());
		goto dlopen_err;
	}

	dlerror();

	wcnss_init_qmi = dlsym(wcnss_qmi_handle, "wcnss_init_qmi");
	if ((error = dlerror()) != NULL) {
		ALOGE("Failed to resolve function: %s: %s",
				"wcnss_init_qmi", error);
		goto dlsym_err;
	}

	dlerror();

	wcnss_qmi_get_wlan_address = dlsym(wcnss_qmi_handle,
			"wcnss_qmi_get_wlan_address");
	if ((error = dlerror()) != NULL) {
		ALOGE("Failed to resolve function: %s: %s",
				"wcnss_qmi_get_wlan_address", error);
		goto dlsym_err;
	}

	dlerror();

	wcnss_qmi_deinit = dlsym(wcnss_qmi_handle, "wcnss_qmi_deinit");
	if ((error = dlerror()) != NULL) {
		ALOGE("Failed to resolve function: %s: %s",
				"wcnss_qmi_deinit", error);
		goto dlsym_err;
	}

	return SUCCESS;

dlsym_err:
	dlclose(wcnss_qmi_handle);
dlopen_err:
	return FAILED;
}
#endif

int main(int argc, char *argv[])
{
	UNUSED(argc), UNUSED(argv);
	int rc;
	int fd_dev, ret_cal;
#if defined(WCNSS_QMI) || defined(WCNSS_QMI_OSS)
	int nv_mac_addr = FAILED;
#ifdef MDM_DETECT
	struct dev_info mdm_detect_info;
	int nom = 0;
#endif
#endif

	setup_wlan_config_file();

#ifdef WCNSS_QMI_OSS
	/* dlopen WCNSS QMI lib */

	rc = setup_wcnss_qmi();
	if (rc == SUCCESS) {
		if (SUCCESS == (*wcnss_init_qmi)()) {
			rc = (*wcnss_qmi_get_wlan_address)(wlan_nv_mac_addr);
			if (rc == SUCCESS) {
				nv_mac_addr = SUCCESS;
				ALOGE("WLAN MAC Addr:" MAC_ADDRESS_STR,
						MAC_ADDR_ARRAY(wlan_nv_mac_addr));
			} else
				ALOGE("Failed to Get MAC addr from modem");

			(*wcnss_qmi_deinit)();
		}
		else
			ALOGE("Failed to Initialize wcnss QMI Interface");
	} else {
		ALOGE("Failed to Initialize wcnss QMI interface library");
	}
#endif
#ifdef WCNSS_QMI
#ifdef MDM_DETECT
	/* Call ESOC API to get the number of modems.
	   If the number of modems is not zero, only then proceed
	   with the eap_proxy intialization.*/

	nom = get_system_info(&mdm_detect_info);

	if (nom > 0)
		ALOGE("Failed to get system info, ret %d", nom);

	if (mdm_detect_info.num_modems == 0) {
		ALOGE("wcnss_service: No Modem support for this target"
				" number of modems is %d", mdm_detect_info.num_modems);
		goto nomodem;
	}

	ALOGE("wcnss_service: num_modems = %d", mdm_detect_info.num_modems);

	if(!check_modem_compatability(&mdm_detect_info)) {
		ALOGE("wcnss_service: Target does not have external modem");
		goto nomodem;
	}
#endif

	/* initialize the DMS client and request the wlan mac address */

	if (SUCCESS == wcnss_init_qmi()) {

		rc = wcnss_qmi_get_wlan_address(wlan_nv_mac_addr);

		if (rc == SUCCESS) {
			nv_mac_addr = SUCCESS;
			ALOGE("WLAN MAC Addr:" MAC_ADDRESS_STR,
					MAC_ADDR_ARRAY(wlan_nv_mac_addr));
		} else
			ALOGE("Failed to Get MAC addr from modem");

		wcnss_qmi_deinit();
	}
	else
		ALOGE("Failed to Initialize wcnss QMI Interface");

nomodem:
#endif

	dynamic_nv_replace();

#if defined(WCNSS_QMI) || defined(WCNSS_QMI_OSS)
	setup_wcnss_parameters(&ret_cal, nv_mac_addr);
#else
	setup_wcnss_parameters(&ret_cal);
#endif

	fd_dev = open(WCNSS_DEVICE, O_RDWR);
	if (fd_dev < 0) {
		ALOGE("Failed to open wcnss device : %s",
				strerror(errno));
		return fd_dev;
	}

	if (ret_cal != FAILED) {
		rc = wcnss_write_cal_data(fd_dev);
		if (rc != SUCCESS)
			ALOGE("No cal data is written to WCNSS %d", rc);
		else
			ALOGE("Cal data is successfully written to WCNSS");
	}

	setup_wlan_driver_ath_prop();

	rc = wcnss_read_and_store_cal_data(fd_dev);
	if (rc != SUCCESS)
		ALOGE("Failed to read and save cal data %d", rc);
	else
		ALOGI("Calibration data was successfull written to %s",
			WCNSS_CAL_FILE);

	close(fd_dev);

#ifdef WCNSS_QMI_OSS
	dlclose(wcnss_qmi_handle);
#endif

	return rc;
}