[Feature][ZXW-88]merge P50 version

Only Configure: No
Affected branch: master
Affected module: unknown
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No

Change-Id: I34667719d9e0e7e29e8e4368848601cde0a48408
diff --git a/ap/app/zte_comm/rtc-service/rtc-service.c b/ap/app/zte_comm/rtc-service/rtc-service.c
index f60984d..891af85 100755
--- a/ap/app/zte_comm/rtc-service/rtc-service.c
+++ b/ap/app/zte_comm/rtc-service/rtc-service.c
@@ -50,6 +50,7 @@
 static int handle_alarm_del(MSG_BUF *pmsg_buf);
 static int handle_alarm_timeout(MSG_BUF *pmsg_buf);
 static int process_msg(MSG_BUF *pmsg_buf);
+static int process_msg_rtc_cap(MSG_BUF *pmsg_buf);
 static void msg_looper(void);
 static void update_timer_remain(int new_remian);
 static void alarm_fix_value(struct list_head *rtc_list);
@@ -1015,6 +1016,108 @@
 	return 0;
 }
 
+/********************************************
+ * process_msg_rtc_ap
+ * handle the specified MSG to cap, because rtc driver is at cap
+ *******************************************/
+static int process_msg_rtc_cap(MSG_BUF *pmsg_buf)
+{
+	enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
+	unsigned short req_cmd = 0;
+	int ret = 0;
+	struct tm *ptm = NULL;
+	RTC_DATA_BUF *buf = NULL;
+	int dst_id = 0;
+	
+	switch (cmd) {
+	case RTC_MSG_GET_TIME:
+		buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
+		slog(RTC_PRINT, SLOG_NORMAL, "to cap: RTC_MSG_GET_TIME \n");
+		if(buf->cpu == RTC_CPU_AP)
+		{
+			ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
+			if (ret != 0) {
+				slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n",ret);
+			}
+		}
+		else
+		{
+			slog(RTC_PRINT, SLOG_NORMAL, "receive rtc time from cap core : RTC_MSG_GET_TIME :%lu \n",buf->ulSec);
+			
+		}
+		break;
+	case RTC_MSG_SET_TIME:
+		ptm = (struct tm *)pmsg_buf->aucDataBuf;
+		if(pmsg_buf->src_id == MODULE_ID_CP_RTC_SERVICE)
+		{
+			rtc_set_local_time(pmsg_buf);
+		}
+		else
+		{
+			slog(RTC_PRINT, SLOG_NORMAL, "to cap: cmd:0x%x, %d-%d-%d %d:%d:%d \n", cmd, ptm->tm_year, ptm->tm_mon, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
+			ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
+	    	if (ret != 0) 
+			{
+	    		slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n",ret);
+	    	}
+		}
+		break;
+	case RTC_MSG_SNTP_UPDATE:
+		handle_sntp_update(pmsg_buf);
+		break;
+	case RTC_MSG_ADD_ALARM:
+	case RTC_MSG_DEL_ALARM:
+		buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
+		if (buf->cpu == RTC_CPU_AP) 
+		{
+			slog(RTC_PRINT, SLOG_NORMAL, "to cap: cmd:0x%x, module:0x%x, msg_id:0x%x, sec:%ld: cpu:%d, utc:%d, wakeup:%d \n", cmd, buf->module, buf->srcModule, buf->ulSec, buf->cpu, buf->is_utc, buf->wakeup);
+			ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
+			if (ret != 0) 
+			{
+				slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n",ret);
+			}
+		}
+		else
+		{
+			slog(RTC_PRINT, SLOG_NORMAL, "from cap: cmd:0x%x, module:0x%x, msg_id:0x%x, del ok \n", cmd, buf->module, buf->srcModule);
+		}
+		break;
+	case RTC_MSG_ALARM_TIMEOUT:
+	case RTC_MSG_TIMER_TIMEOUT:
+		buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
+		if (buf->cpu == RTC_CPU_CAP) 
+		{
+			slog(RTC_PRINT, SLOG_NORMAL, "from cap: cmd:0x%x(timeout), module:0x%x, msg_id:0x%x, sec:%ld: %d, %d, %d \n", cmd, buf->module, buf->srcModule, buf->ulSec, buf->cpu, buf->is_utc, buf->wakeup);
+			
+			ret = ipc_send_message(MODULE_ID_RTC_SERVICE, buf->srcModule, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), buf, 0);
+			if (ret != 0) 
+			{
+				slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n",ret);
+			}
+		}
+		break;
+	case RTC_MSG_DEL_ALL:
+		slog(RTC_PRINT, SLOG_NORMAL, "process_msg_to_cap RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
+		memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
+		//ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
+		ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
+		ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
+		if (ret != 0) 
+		{
+			slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n",ret);
+		}
+		break;
+	case RTC_MSG_TIME_CHANGED:
+		buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
+		slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: RTC_MSG_TIME_CHANGED, dst_id:0x%x.\n",buf->srcModule);
+		ipc_send_message(MODULE_ID_RTC_SERVICE, buf->srcModule, RTC_MSG_TIME_CHANGED, 0, 0, 0);
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
 static int rtc_service_sock_init()
 {
     int sockfd = 0;
@@ -1168,8 +1271,12 @@
 		}
 		slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper process_msg in, src:0x%x, target:0x%x.\n", msg_buf.src_id, msg_buf.dst_id);
 
-		process_msg(&msg_buf);
-
+		#ifdef USE_CAP_SUPPORT
+			process_msg_rtc_cap(&msg_buf);
+		#else
+			process_msg(&msg_buf);
+		#endif
+		
 		slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper process_msg out.\n");
 		wake_rtc_unlock();
 	}
@@ -1271,11 +1378,13 @@
     g_clean_flag = 0;
 	
 	//rtcÔÚAPʱ£¬½Úµã±ØÐë´ò¿ª³É¹¦£»ÔÚÆäËûºËʱÓÉÆäËûºËÅжÏ
+#ifndef USE_CAP_SUPPORT
 	if ((g_rtc_fd = open_rtc()) < 0) {
 		slog(RTC_PRINT, SLOG_ERR, "Cannt open rtc dev!\n");
 		return -1;
 	}
 	init_rtc_alarm();
+#endif
 /*
 	pthread_attr_init(&attr);
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);