[Feature][R305][task-view-1916][AT] add AT+SELECT_SIM=xxx, used for switching SIM cards

Change-Id: Iae6d4af20ce6f2adcaf56c86fd0522ef5c8c5e44
diff --git a/lynq/R305/ap/app/zte_comm/at_ctl/src/atconfig/extat_softap_register.c b/lynq/R305/ap/app/zte_comm/at_ctl/src/atconfig/extat_softap_register.c
index bb12a04..872f774 100755
--- a/lynq/R305/ap/app/zte_comm/at_ctl/src/atconfig/extat_softap_register.c
+++ b/lynq/R305/ap/app/zte_comm/at_ctl/src/atconfig/extat_softap_register.c
@@ -40,6 +40,8 @@
 	"TKIPAES",

 };

 

+#define FOTAFLAG_FILE_PATH  "/mnt/imagefs/fotaflag"

+

 #if (APP_OS_TYPE == APP_OS_TOS)

 extern SINT32 mmiLcReset(VOID);

 extern UINT32 zOss_ResetNVFactory(VOID);

@@ -804,6 +806,92 @@
 	return AT_END;

 }

 

+static int upi_writen(int fd, const void *vptr, size_t n)

+{

+	size_t nleft;

+	ssize_t nwritten;

+	const char *ptr;

+

+	ptr = vptr;

+	nleft = n;

+	while (nleft > 0)

+	{

+		if ( (nwritten = write(fd, ptr, nleft)) <= 0)

+		{

+			if (nwritten < 0 && errno == EINTR)

+			{

+				nwritten = 0;   /* and call write() again */

+			}

+			else

+			{

+				return (-1);    /* error */

+			}

+		}

+

+		nleft -= nwritten;

+		ptr += nwritten;

+	}

+	

+	return n;

+}

+

+int ext_at_select_sim_func(char *at_paras, void ** res_msg)

+{

+	int ret = 0;

+	char sim[8] = {0};

+	void *p[] = {sim};

+	char buf[32] = {0xFF};

+

+	ret = parse_param("%s", at_paras, p);

+	if (ret != AT_PARSE_OK)

+	{

+		*res_msg = at_err_build(ATERR_PROC_FAILED);

+		return AT_END;

+	}

+	slog(AT_PRINT, SLOG_NORMAL, "ext_at_select_sim_func sim:%s\n", sim);

+

+	if (0 == strcmp("esim", sim)) //esim

+	{

+		memset(buf, 0xFF, sizeof(buf) - 1);

+		buf[sizeof(buf) - 1] = '\0';

+	}

+	else if (0 == strcmp("sim", sim)) //sim

+	{

+		memset(buf, 0xFF, sizeof(buf) - 4);

+		sprintf(buf + 28, "%s", "sim");

+	}

+	else

+	{

+		slog(MISC_PRINT, SLOG_NORMAL,"ext_at_select_sim_func, select_sim [%s] invalid value.\n", sim);

+		*res_msg = at_err_build(ATERR_PROC_FAILED);

+		return AT_END;

+	}

+

+	system("mount -o remount,rw /mnt/imagefs");

+	system("chmod 666 /mnt/imagefs/fotaflag");

+	int flag_fd = open(FOTAFLAG_FILE_PATH, O_WRONLY|O_SYNC);

+	if(flag_fd < 0)

+	{

+		slog(MISC_PRINT, SLOG_NORMAL, "ext_at_select_sim_func, Open file [%s] fail, error:%d[%s]", FOTAFLAG_FILE_PATH, errno, strerror(errno));

+		*res_msg = at_err_build(ATERR_PROC_FAILED);

+		return AT_END;

+	}

+	ret = upi_writen(flag_fd, buf, 32);

+	if (ret != 32)

+	{

+		slog(MISC_PRINT, SLOG_NORMAL, "ext_at_select_sim_func, Write file[%s] fail, write len:%d, error:%d[%s]", FOTAFLAG_FILE_PATH, ret, errno, strerror(errno));

+		close(flag_fd);

+		*res_msg = at_err_build(ATERR_PROC_FAILED);

+		return AT_END;

+	}

+	fsync(flag_fd);

+	close(flag_fd);

+	system("mount -o remount,ro /mnt/imagefs");

+	system("reboot");

+

+	return AT_END;

+}

+

 int ext_at_wlan_station_func(char *at_paras, void ** res_msg)

 {

 	int count = 0;

@@ -958,6 +1046,7 @@
 	register_serv_func("WLAN_STA", 0, ext_at_wlan_station_func);

 	register_serv_func("WLAN_STA=", 0, ext_at_wlan_station_timeout_func);

 	register_serv_func("FMTEST=", 0, ext_at_delay_shut_down_func);

+	register_serv_func("SELECT_SIM=", 0, ext_at_select_sim_func);

 }

 

 void at_comm_init(void)