Embed/microCLib

添加git信息到固件
JiuHuan authored at 2025-06-04 09:04:10
705e954
Tree
1 Parent(s) ab769f4
Summary: 4 changed files with 121 additions and 0 deletions.
Added +1 -0
Added +16 -0
Modified +79 -0
Modified +25 -0
Added +1 -0
diff --git a/Core/.gitignore b/Core/.gitignore
new file mode 100644
index 0000000..6ac1395
--- /dev/null
+++ b/Core/.gitignore
@@ -0,0 +1 @@
+GitInfo.h
Added +16 -0
diff --git a/Core/GitInfo.bat b/Core/GitInfo.bat
new file mode 100644
index 0000000..bf7d430
--- /dev/null
+++ b/Core/GitInfo.bat
@@ -0,0 +1,16 @@
+
+:Instructions
+:Execute this script before compilation. Automatically generate file GitInfo.h
+:eg. MDK -> Options for Taget 'xx' -> User -> Before Build/Rebuild -> Run #1
+
+
+@echo off
+where git >nul 2>&1
+if %errorlevel%==0 (
+    echo #define GITINFO \>GitInfo.h
+	:git log -1 --pretty=format:"\"%%ci \r\n%%H \r\n%%d \r\n%%an\"" >> GitInfo.h
+	git log -1 --pretty=format:"\"%%ci \r\n%%H \r\n%%d\"" >> GitInfo.h
+) else (
+    echo #define GITINFO "NO GIT" >GitInfo.h
+)
+echo.>>GitInfo.h
Modified +79 -0
diff --git a/Core/Version.c b/Core/Version.c
index 9d9754a..543184f 100644
--- a/Core/Version.c
+++ b/Core/Version.c
@@ -174,3 +174,82 @@ uint GetFwVersion(uint addr, int len)
 
 	return minuint;
 }
+
+
+#ifdef GITVERSION
+
+static byte ChToByte(char ch)
+{
+	if ((ch >= '0') && (ch <= '9'))return (byte)ch - (byte)'0';
+	if ((ch >= 'a') && (ch <= 'f'))return (byte)ch - (byte)'a' + 10;
+	if ((ch >= 'A') && (ch <= 'F'))return (byte)ch - (byte)'A' + 10;
+
+	return 0;
+}
+
+static byte ChsToByte(char* chs)
+{
+	byte n0 = ChToByte(chs[0]);
+	byte n1 = ChToByte(chs[1]);
+
+	return (n0 << 4) + n1;
+}
+
+bool GetGitVersion(GitVersion_t* ver)
+{
+	if (ver == NULL)return false;
+	if (memcmp("NO GIT", GITINFO, 6) == 0)return false;
+	int glen = strlen(GITINFO);
+	if (glen < 70)return false;
+
+
+	struct tm time;
+	int num = sscanf(GITINFO, "%d-%d-%d %d:%d:%d ",&time.tm_year, &time.tm_mon, &time.tm_mday,
+		&time.tm_hour, &time.tm_min, &time.tm_sec);
+	if (num < 6) return false;
+
+	// 处理 struct tm 取值范围。
+	time.tm_year -= 1900;
+	time.tm_mon -= 1;
+
+	struct tm start =
+	{
+		.tm_year = 100,		// 其值等于实际年份减去1900
+		.tm_mon = 0,		// 月份(从一月开始,0代表一月) - 取值区间为[0,11]
+		.tm_mday = 1,		// 一个月中的日期 - 取值区间为[1,31] 
+		.tm_hour = 0,		// 时 - 取值区间为[0,23]
+		.tm_min = 0,		// 分 - 取值区间为[0,59]
+		.tm_sec = 0,		// 秒 - 取值区间为[0,59]
+	};
+
+	// 计算秒差/60=分钟差。
+	double min = difftime(mktime(&time), mktime(&start)) / 60.0;
+	uint minuint = (int)min;
+	ver->TimeMin = minuint;
+
+	// SHA-1   字符串转数组
+	int offset = ArrayIndexOf((byte*)GITINFO, glen, (byte*)"\r\n", 2);
+	if (offset < 0)return false;
+	offset += 2;
+	char* psha = (char*) &GITINFO[offset];
+	for (int i = 0; i < 20; i++)
+	{
+		ver->Md5[i] = ChsToByte(psha);
+		psha += 2;
+	}
+
+	// 从后面往前查
+	offset = ArrayLastIndexOf((byte*)GITINFO, glen, (byte*)"\r\n", 2);
+	offset += 2;
+
+	ver->Branch = (char*) &GITINFO[offset];
+	// ver->Branch += 2;
+	
+	return true;
+}
+
+#else
+
+bool GetGitVersion(GitVersion_t* ver) { return false; }
+
+#endif
Modified +25 -0
diff --git a/Core/Version.h b/Core/Version.h
index 179aa2c..b091820 100644
--- a/Core/Version.h
+++ b/Core/Version.h
@@ -45,3 +45,28 @@ tm_sec		// 秒 - 取值区间为[0,59]
 /// <returns></returns>
 uint GetFwVersion(uint addr, int len);
 
+
+#ifdef GITVERSION
+#include "GitInfo.h"
+#endif
+
+#ifndef GITINFO
+#define GITINFO "NO GIT"
+#endif
+
+/// <summary>GIT仓库信息</summary>
+typedef struct
+{
+	/// <summary>git最后提交时间 减去 2000-1-1 00:00:00 得到的分钟数</summary>
+	uint TimeMin;
+	/// <summary>提交对应的 SHA-1 值</summary>
+	byte Md5[20];
+	/// <summary>分支信息</summary>
+	char* Branch;
+}GitVersion_t;
+
+/// <summary>获取GIT仓库的提交</summary>
+/// <param name="ver">版本信息</param>
+/// <returns>获取成功</returns>
+bool GetGitVersion(GitVersion_t* ver);
+