[Feature][ZXW-41] merged 0601 version

Change-Id: I667af6bb09d65581d455b73f8984c160b2c67ad8
diff --git a/ap/app/sc_absvr/Makefile b/ap/app/sc_absvr/Makefile
new file mode 100755
index 0000000..8c3893b
--- /dev/null
+++ b/ap/app/sc_absvr/Makefile
@@ -0,0 +1,110 @@
+# /*****************************************************************************
+#* °æÈ¨ËùÓÐ (C)2015, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾¡£
+#* 
+#* ÎļþÃû³Æ:     Makefile
+#* Îļþ±êʶ:     Makefile
+#* ÄÚÈÝÕªÒª:     Makefile of ZTE applications
+#* ʹÓ÷½·¨:     void
+#* 
+#* ÐÞ¸ÄÈÕÆÚ        °æ±¾ºÅ      Ð޸ıê¼Ç        ÐÞ¸ÄÈË          ÐÞ¸ÄÄÚÈÝ
+#* -----------------------------------------------------------------------------
+#* 2022/07/13      V1.0        Create          zln          ´´½¨
+#* 
+# ******************************************************************************/
+
+#*******************************************************************************
+# include ZTE application makefile
+#*******************************************************************************
+.EXPORT_ALL_VARIABLES:
+include $(COMMON_MK)
+#*******************************************************************************
+# execute
+#*******************************************************************************
+EXEC = sc_absvr
+
+#*******************************************************************************
+# objects
+#*******************************************************************************
+SRCS=$(wildcard ./*.c)
+OBJS = $(patsubst %.c,%.o,$(SRCS))
+
+#*******************************************************************************
+# include path
+#*******************************************************************************
+
+
+
+CFLAGS +=	-Wall -g
+
+CFLAGS += -Werror=implicit-int \
+		  -Werror=implicit-function-declaration \
+		  -Werror=float-equal \
+		  -Werror=return-type \
+		  -Werror=enum-compare \
+		  -Werror=init-self 
+
+CFLAGS +=	-O2
+CFLAGS +=	-I../include
+CFLAGS +=	-I./inc	\
+			-I$(zte_lib_path)/libssl/install/include \
+			-I$(zte_lib_path)/libupi_ab/inc
+#*******************************************************************************
+# macro definition
+#*******************************************************************************
+
+ifeq ($(PRJ_IS_MIN),yes)
+
+CFLAGS += -D_IS_MIN
+
+endif
+
+ifeq ($(CONFIG_USER_FOTA_OPEN_SOURCE),n)
+CFLAGS += -D_USE_SYSTEM_COMMAND
+endif
+
+
+#*******************************************************************************
+# library
+#*******************************************************************************
+LDLIBS  += -lsoft_timer_sc -L$(zte_lib_path)/libsoft_timer
+
+LDLIBS += -lpthread
+LDLIBS += -lsoftap
+LDLIBS +=  -lnvram_sc
+
+# SHA512 functions in libcrypto.a not libssl
+#LDLIBS +=  -lssl
+LDLIBS += -lcrypto
+LDLIBS +=  -L$(zte_lib_path)/libssl/install/lib
+LDLIBS += -L$(zte_lib_path)/libnvram
+LDLIBS  += -L$(zte_lib_path)/libsoftap 
+	
+LDLIBS  += 	-lcpnv -L$(LIB_DIR)/libcpnv 
+
+LDLIBS += -lupi_ab
+LDLIBS += -L$(zte_lib_path)/libupi_ab
+
+CFLAGS += -I$(zte_lib_path)/libnvram 
+
+#*******************************************************************************
+# library path
+#*******************************************************************************
+
+#*******************************************************************************
+# targets
+#*******************************************************************************
+all: $(EXEC)
+
+
+$(EXEC): $(OBJS)
+	$(CC) $(LDFLAGS) -o $@ $^ -Wl,--start-group $(LDLIBS) $(LDLIBS_$@) -Wl,--end-group
+	cp $(EXEC) $(EXEC).elf
+	
+
+
+romfs root_fs:
+	$(ROMFSINST) /bin/$(EXEC)
+
+
+clean:
+	-rm -f $(EXEC) *.elf *.gdb *.o ./src/*.o
diff --git a/ap/app/sc_absvr/sc_absvr.c b/ap/app/sc_absvr/sc_absvr.c
new file mode 100755
index 0000000..595f6a7
--- /dev/null
+++ b/ap/app/sc_absvr/sc_absvr.c
@@ -0,0 +1,426 @@
+/**

+ * @file sc_absvr.c

+ * @brief Implementation of sc_absvr.

+ *

+ * Copyright (C) 2023 Sanechips Technology Co., Ltd.

+ * @author 

+ * 

+ * This program is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License version 2 as

+ * published by the Free Software Foundation. £¨±ØÑ¡£ºGPLv2 Licence£©

+ *

+ */

+

+

+/*******************************************************************************

+ *                           Include header files                              *

+ ******************************************************************************/

+#include <stdio.h>

+#include <string.h>

+#include <stdlib.h>

+#include <assert.h>

+#include <syslog.h>

+#include <sys/klog.h>

+#include <sys/msg.h>

+#include <sys/un.h>

+#include <errno.h>

+#include <sys/types.h>

+#include <sys/wait.h>

+

+#include "absvr_msg.h"

+#include "zxic_fota_ab_upgrade.h"

+#include "softap_log.h"

+#include "at_reg.h"

+#include "softap_api.h"

+

+

+/*******************************************************************************

+ *                             Macro definitions                               *

+ ******************************************************************************/	

+#define MSG_DATABUF_MAX_LEN (128)

+

+#define DEBUG 1

+

+//#define ABSVR_PRINT "SC_AB_SVR"

+

+/*******************************************************************************

+ *                             Type definitions                                *

+ ******************************************************************************/

+typedef struct

+{

+	int result; // 0: ³É¹¦ ·Ç0: ʧ°Ü

+	unsigned char dataBuf[MSG_DATABUF_MAX_LEN];

+} absvr_common_result_t;

+

+

+typedef struct

+{

+	int boot_to_system;

+	int reboot_flag;

+

+	int system;

+	int status;

+

+	int status_for_nv;

+

+	int sync_status;

+} absvr_common_req_msg_t;

+

+

+/*******************************************************************************

+ *						   Local variable definitions						   *

+ ******************************************************************************/

+

+

+/*******************************************************************************

+ *						  Global variable definitions						   *

+ ******************************************************************************/

+

+

+/*******************************************************************************

+ * 					   Local function declarations							   *

+ ******************************************************************************/

+static void flush_upgrade_status(z_upgrade_status_info_t *p_status);

+

+static int absvr_get_upgrade_type(MSG_BUF *msg);

+static int absvr_verify(MSG_BUF *msg);

+static int absvr_update(MSG_BUF *msg);

+static int absvr_sync(MSG_BUF *msg);

+static int absvr_get_upgrade_status(MSG_BUF *msg);

+static int absvr_get_current_system(MSG_BUF *msg);

+static int absvr_get_boot_to_system(MSG_BUF *msg);

+static int absvr_set_boot_to_system(MSG_BUF *msg);

+static int absvr_get_system_status(MSG_BUF *msg);

+static int absvr_set_system_status(MSG_BUF *msg);

+static int absvr_get_fota_status_for_nv(MSG_BUF *msg);

+static int absvr_set_fota_status_for_nv(MSG_BUF *msg);

+static int absvr_get_sync_status(MSG_BUF *msg);

+static int absvr_set_sync_status(MSG_BUF *msg);

+

+static int absvr_msg_proc(MSG_BUF *msg);

+

+

+/*******************************************************************************

+ * 					 Local function implementations 						   *

+ ******************************************************************************/

+static void flush_upgrade_status(z_upgrade_status_info_t *p_status)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	

+	memcpy(absvr_common_result.dataBuf, p_status, sizeof(z_upgrade_status_info_t));

+	

+	if (0 != send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_UPDATING_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result)))

+	{

+		slog(ABSVR_PRINT, SLOG_ERR, "Err: flush_upgrade_status send_soc_msg fail \n");

+	}

+

+	return;

+}

+

+

+static int absvr_get_upgrade_type(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	ret = zxic_dual_get_upgrade_type();

+

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_UPGRADE_TYPE_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+ 

+

+static int absvr_verify(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+

+	absvr_common_result.result = zxic_dual_verify();

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_VRITFY_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_update(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	

+	z_upgrade_status_info_t status = {0};

+	z_upgrade_flush_status_t flush_status = {0};

+

+	flush_status.status = &status;

+	flush_status.status_cb = &flush_upgrade_status;

+

+	absvr_common_result.result = zxic_dual_upgrade(&flush_status);

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_UPDATE_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_sync(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+

+	absvr_common_result.result = zxic_dual_sync_system();

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SYNC_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_get_upgrade_status(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	z_upgrade_status_info_t upgrade_info = {0};

+

+	ret = zxic_dual_get_upgrade_status(&upgrade_info);

+

+	absvr_common_result.result = ret;

+	memcpy(absvr_common_result.dataBuf, &upgrade_info, sizeof(z_upgrade_status_info_t));

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_UPGRADE_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_get_current_system(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	ret = zxic_dual_get_current_system();

+

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_CURRENT_SYSTEM_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_get_boot_to_system(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	ret = zxic_dual_get_boot_to_system();

+

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_BOOT_TO_SYSTEM_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_set_boot_to_system(MSG_BUF *msg)

+{

+	absvr_common_req_msg_t *common_req_msg = NULL;

+	

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);

+

+	ret = zxic_dual_set_boot_to_system(common_req_msg->boot_to_system, common_req_msg->reboot_flag);

+	

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_BOOT_TO_SYSTEM_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_get_system_status(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	z_upgrade_system_info_t system_info = {0};

+

+	ret = zxic_dual_get_system_status(&system_info);

+

+	absvr_common_result.result = ret;

+	memcpy(absvr_common_result.dataBuf, &system_info, sizeof(z_upgrade_system_info_t));

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_SYSTEM_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_set_system_status(MSG_BUF *msg)

+{

+	absvr_common_req_msg_t *common_req_msg = NULL;

+	

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);

+

+	ret = zxic_dual_set_system_status(common_req_msg->system, common_req_msg->status);

+	

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_SYSTEM_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_get_fota_status_for_nv(MSG_BUF *msg)

+{

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	ret = zxic_dual_get_fota_status_for_nv();

+

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_FOTA_STATUS_FOR_NV_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_set_fota_status_for_nv(MSG_BUF *msg)

+{

+	absvr_common_req_msg_t *common_req_msg = NULL;

+	

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);

+

+	ret = zxic_dual_set_fota_status_for_nv(common_req_msg->status_for_nv);

+	

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_FOTA_STATUS_FOR_NV_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_get_sync_status(MSG_BUF *msg)

+{	

+	absvr_common_result_t absvr_common_result = {0};

+

+	zxic_dual_get_sync_status(&(absvr_common_result.result));

+	

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_SYNC_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_set_sync_status(MSG_BUF *msg)

+{

+	absvr_common_req_msg_t *common_req_msg = NULL;

+	

+	absvr_common_result_t absvr_common_result = {0};

+	int ret = -1;

+

+	common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);

+

+	ret = zxic_dual_set_sync_status(common_req_msg->sync_status);

+	

+	absvr_common_result.result = ret;

+

+	return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_SYNC_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));

+}

+

+

+static int absvr_msg_proc(MSG_BUF *msg)

+{

+	int ret = 0;

+

+	if( NULL == msg)

+	{

+		slog(ABSVR_PRINT, SLOG_ERR, "Err: absvr_msg_proc msg is NULL \n");

+		return -1;

+	}

+

+	slog(ABSVR_PRINT, SLOG_NORMAL, "absvr_msg_proc start process msg [%hu] \n", msg->usMsgCmd);

+	

+	switch(msg->usMsgCmd)

+	{		

+		case ABSVR_GET_UPGRADE_TYPE_REQ:

+			ret = absvr_get_upgrade_type(msg);

+			break;

+		case ABSVR_VRITFY_REQ:

+			ret = absvr_verify(msg);

+			break;

+		case ABSVR_UPDATE_REQ:

+			ret = absvr_update(msg);

+			break;

+		case ABSVR_SYNC_REQ:

+			ret = absvr_sync(msg);

+			break;

+		case ABSVR_GET_UPGRADE_STATUS_REQ:

+			ret = absvr_get_upgrade_status(msg);

+			break;

+		case ABSVR_GET_CURRENT_SYSTEM_REQ:

+			ret = absvr_get_current_system(msg);

+			break;

+		case ABSVR_GET_BOOT_TO_SYSTEM_REQ:

+			ret = absvr_get_boot_to_system(msg);

+			break;

+		case ABSVR_SET_BOOT_TO_SYSTEM_REQ:

+			ret = absvr_set_boot_to_system(msg);

+			break;

+		case ABSVR_GET_SYSTEM_STATUS_REQ:

+			ret = absvr_get_system_status(msg);

+			break;

+		case ABSVR_SET_SYSTEM_STATUS_REQ:

+			ret = absvr_set_system_status(msg);

+			break;

+		case ABSVR_GET_FOTA_STATUS_FOR_NV_REQ:

+			ret = absvr_get_fota_status_for_nv(msg);

+			break;

+		case ABSVR_SET_FOTA_STATUS_FOR_NV_REQ:

+			ret = absvr_set_fota_status_for_nv(msg);

+			break;

+		case ABSVR_GET_SYNC_STATUS_REQ:

+			ret = absvr_get_sync_status(msg);

+			break;

+		case ABSVR_SET_SYNC_STATUS_REQ:

+			ret = absvr_set_sync_status(msg);

+			break;

+		default:

+			slog(ABSVR_PRINT, SLOG_ERR, "Err: absvr_msg_proc unknow usMsgCmd [%hu] \n", msg->usMsgCmd);

+			break;

+	}

+

+	return ret;

+}

+

+

+/*******************************************************************************

+ * 					 Global function implementations						*

+ ******************************************************************************/

+

+int main(void)

+{

+	int iRet = 0;

+

+	int iMsgHandle = 0;	

+	MSG_BUF stMsg = {0};

+	LONG msgSize =  sizeof(MSG_BUF) - sizeof(LONG);

+

+	/*¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿*/

+	loglevel_init();

+	slog(ABSVR_PRINT, SLOG_NORMAL, "sc_absvr begin ======= build date: %s %s \n", __DATE__, __TIME__);

+	

+	iMsgHandle = msgget(MODULE_ID_ABSVR, IPC_CREAT | 0600);

+	if (-1 == iMsgHandle)

+	{

+		slog(ABSVR_PRINT, SLOG_ERR, "Err: can not create msg queue for sc_absvr \n");

+		return -1;

+	}

+

+	for (;;)

+	{

+		memset(&stMsg, 0, sizeof(stMsg));

+		iRet = 0;

+		iRet = msgrcv(iMsgHandle, &stMsg, msgSize, 0, 0);

+		if (-1 == iRet)

+		{

+			slog(ABSVR_PRINT, SLOG_ERR, "Err: recv msg fail errno = %d \n", errno);

+			continue;

+		}

+		

+		if (0 != absvr_msg_proc(&stMsg))

+		{

+			slog(ABSVR_PRINT, SLOG_ERR, "Err: absvr_msg_proc src_id[%d] usMsgCmd[%hu] fail \n", stMsg.src_id, stMsg.usMsgCmd);			

+		}

+	}

+	

+	return 0;

+}

+