Commit 36f3409f authored by captainwong's avatar captainwong

add pack/pack_ack for dll; update dll demo, c# demo

parent 32eb16d2
# Details
Date : 2020-03-07 13:46:59
Directory g:\dev_libs\ademco_hb\include
Total : 3 files, 1568 codes, 205 comments, 260 blanks, all 2033 lines
[summary](results.md)
## Files
| filename | language | code | comment | blank | total |
| :--- | :--- | ---: | ---: | ---: | ---: |
| [include/ademco_detail.h](/include/ademco_detail.h) | C++ | 261 | 103 | 42 | 406 |
| [include/ademco_event.h](/include/ademco_event.h) | C++ | 477 | 20 | 69 | 566 |
| [include/ademco_packet.h](/include/ademco_packet.h) | C++ | 830 | 82 | 149 | 1,061 |
[summary](results.md)
\ No newline at end of file
filename, language, C++, comment, blank, total
g:\dev_libs\ademco_hb\include\ademco_detail.h, C++, 261, 103, 42, 406
g:\dev_libs\ademco_hb\include\ademco_event.h, C++, 477, 20, 69, 566
g:\dev_libs\ademco_hb\include\ademco_packet.h, C++, 830, 82, 149, 1061
Total, -, 1568, 205, 260, 2033
\ No newline at end of file
# Summary
Date : 2020-03-07 13:47:00
Directory g:\dev_libs\ademco_hb\include
Total : 3 files, 1568 codes, 205 comments, 260 blanks, all 2033 lines
[details](details.md)
## Languages
| language | files | code | comment | blank | total |
| :--- | ---: | ---: | ---: | ---: | ---: |
| C++ | 3 | 1,568 | 205 | 260 | 2,033 |
## Directories
| path | files | code | comment | blank | total |
| :--- | ---: | ---: | ---: | ---: | ---: |
| . | 3 | 1,568 | 205 | 260 | 2,033 |
[details](details.md)
\ No newline at end of file
Date : 2020-03-07 13:46:59
Directory : g:\dev_libs\ademco_hb\include
Total : 3 files, 1568 codes, 205 comments, 260 blanks, all 2033 lines
Languages
+----------+------------+------------+------------+------------+------------+
| language | files | code | comment | blank | total |
+----------+------------+------------+------------+------------+------------+
| C++ | 3 | 1,568 | 205 | 260 | 2,033 |
+----------+------------+------------+------------+------------+------------+
Directories
+-----------------------------------------------+------------+------------+------------+------------+------------+
| path | files | code | comment | blank | total |
+-----------------------------------------------+------------+------------+------------+------------+------------+
| . | 3 | 1,568 | 205 | 260 | 2,033 |
+-----------------------------------------------+------------+------------+------------+------------+------------+
Files
+-----------------------------------------------+----------+------------+------------+------------+------------+
| filename | language | code | comment | blank | total |
+-----------------------------------------------+----------+------------+------------+------------+------------+
| g:\dev_libs\ademco_hb\include\ademco_detail.h | C++ | 261 | 103 | 42 | 406 |
| g:\dev_libs\ademco_hb\include\ademco_event.h | C++ | 477 | 20 | 69 | 566 |
| g:\dev_libs\ademco_hb\include\ademco_packet.h | C++ | 830 | 82 | 149 | 1,061 |
| Total | | 1,568 | 205 | 260 | 2,033 |
+-----------------------------------------------+----------+------------+------------+------------+------------+
\ No newline at end of file
......@@ -6,10 +6,34 @@
#define DLL_FUNC extern "C" _declspec(dllimport)
#endif
//! 返回值说明
//! 0 解析到正确、完整的一包,
//! 1 解析到正确、不完整的一包,需要缓存数据,下次继续处理
//! 2 解析到错误的数据,需要清空缓冲区
DLL_FUNC
int parse(const char* pack, int pack_len, int* cb_commited);
/*
* @brief 解析网络数据
* @param[in] pack 网络数据流
* @param[in] pack_len 网络数据流有效长度
* @param[in|out] cb_commited 可以正确解析的包长(单次函数调用最多解析一包)
* @return 0 解析到正确、完整的一包;1 解析到正确、不完整的一包,需要缓存数据,下次继续处理;2 解析到错误的数据,需要清空缓冲区
*/
DLL_FUNC int parse(const char* pack, int pack_len, int* cb_commited);
/*
* @brief 将远程控制命令打包成网络传输数据
* @param[in|out] buff 缓冲区
* @param[in] buff_len 缓冲区长度
* @param[in] seq 序号
* @param[in] ademco_id 安定宝ID
* @param[in] ademco_event 安定宝事件码
* @param[in] zone 防区号
* @param[in] gg 分防区号
* @return 大于0 成功,返回值代表包长;0 buff空间不足
*/
DLL_FUNC int pack(char* buff, int buff_len, int seq, int ademco_id, int ademco_event, int zone, int gg);
/*
* @brief 打包ACK
* @param[in|out] buff 缓冲区
* @param[in] buff_len 缓冲区长度
* @param[in] seq 序号
* @param[in] ademco_id 安定宝ID
* @return 大于0 成功,返回值代表包长;0 buff空间不足
*/
DLL_FUNC int pack_ack(char* buff, int buff_len, int seq, int ademco_id);
......@@ -29,3 +29,20 @@ int parse(const char* pack, int pack_len, int* cb_commited)
*cb_commited = static_cast<int>(commited);
return static_cast<int>(result);
}
int pack(char* buff, int buff_len, int seq, int ademco_id, int ademco_event, int zone, int gg)
{
ademco::AdemcoPacket ap;
auto res = ap.make_hb(buff, static_cast<size_t>(buff_len), static_cast<uint16_t>(seq),
nullptr, static_cast<size_t>(ademco_id), static_cast<unsigned char>(gg),
static_cast<ademco::ADEMCO_EVENT>(ademco_event), static_cast<size_t>(zone));
return static_cast<int>(res);
}
int pack_ack(char* buff, int buff_len, int seq, int ademco_id)
{
ademco::AdemcoPacket ap;
auto res = ap.make_ack(buff, static_cast<size_t>(buff_len), static_cast<uint16_t>(seq),
nullptr, static_cast<size_t>(ademco_id));
return static_cast<int>(res);
}
......@@ -11,17 +11,55 @@ namespace csharp_dll_demo
CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 parse(string buf, Int32 buf_len, ref Int32 commited);
[DllImport(@"G:\dev_libs\ademco_hb\x64\Release\ademco_hb.dll",
EntryPoint = "pack",
CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 pack(ref byte buf, Int32 buf_len, Int32 seq, Int32 ademco_id, Int32 ademco_event, Int32 zone, Int32 gg);
[DllImport(@"G:\dev_libs\ademco_hb\x64\Release\ademco_hb.dll",
EntryPoint = "pack_ack",
CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 pack_ack(string buf, Int32 buf_len, Int32 seq, Int32 ademco_id);
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//IntPtr commited = new IntPtr();
// test parse
{
Console.WriteLine("test parse");
string raw = "\nC5C30053\"HENG-BO\"0000R000000L000000#90219125916578[#000000|1737 00 000]_09:11:19,08-05-2019\r";
Int32 commited = 0;
byte[] data = System.Text.Encoding.Default.GetBytes(raw);
Int32 res = parse(raw, raw.Length, ref commited);
Console.WriteLine("res={0:D}, commited={1:D}", res, commited);
Console.WriteLine("");
}
// test pack
{
Console.WriteLine("test pack");
Byte[] buff = new Byte[1024];
Int32 res = pack(ref buff[0], 1024, 1, 666666, 1400, 0, 0);
Console.WriteLine("res={0:D}", res);
string str = System.Text.Encoding.Default.GetString(buff, 0, res);
Console.WriteLine(str);
Console.WriteLine("test parse packed data");
Int32 commited = 0;
res = parse(str, str.Length, ref commited);
Console.WriteLine("res={0:D}, commited={1:D}", res, commited);
Console.WriteLine(str);
Console.WriteLine("");
}
// test pack
// buff not enough
{
Console.WriteLine("test pack, buff not enough");
Byte[] buff = new Byte[10];
Int32 res = pack(ref buff[0], 10, 1, 666666, 1400, 0, 0);
Console.WriteLine("res={0:D}", res);
}
}
}
}
{
"version": 1,
"dgSpecHash": "6CTziHR14vso8eK23U2BRvZk32JfFqH9GcXdXYIFBkA/95a748GIX77JI3JnOfn3R1p2/MR8a/UjDfwl0vxREw==",
"dgSpecHash": "enqVYK58B2u+2zXHcESUbbLfk1x9+SUeE5dBaKx3jo0y9wl25Y8qyh28+gy/13OunxXdkTy8P4sF7NOBzYf5HA==",
"success": true
}
\ No newline at end of file
......@@ -58,7 +58,7 @@
],
"assetTargetFallback": true,
"warn": true,
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.0.100\\RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.101\\RuntimeIdentifierGraph.json"
}
}
}
......
......@@ -7,7 +7,7 @@
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Jack\.nuget\packages\;C:\Program Files\dotnet\sdk\NuGetFallbackFolder</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">5.3.0</NuGetToolVersion>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">5.4.0</NuGetToolVersion>
</PropertyGroup>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
......
......@@ -745,7 +745,7 @@
],
"assetTargetFallback": true,
"warn": true,
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.0.100\\RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.101\\RuntimeIdentifierGraph.json"
}
}
}
......
......@@ -107,6 +107,8 @@ int main()
handle_network_data(data_from_network);
char buff[1024];
AdemcoPacket ap;
......
......@@ -88,6 +88,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
......@@ -117,6 +118,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
......
......@@ -13,21 +13,44 @@ int main()
{
int cb_commited = 0;
// 解析完整数据包
{
char raw[] = "\nC5C30053\"HENG-BO\"0000R000000L000000#90219125916578[#000000|1737 00 000]_09:11:19,08-05-2019\r";
int res = parse(raw, strlen(raw), &cb_commited);
assert(res == 0);
}
// 解析长度不足的数据包
{
char raw_not_enough[] = "\nC5C30053\"HENG-BO\"0000R000000L000000";
int res = parse(raw_not_enough, strlen(raw_not_enough), &cb_commited);
assert(res == 1);
}
// 解析到错误数据包
{
char raw_error[] = "abcdeadfasdfasdfasd";
int res = parse(raw_error, strlen(raw_error), &cb_commited);
assert(res == 2);
}
// 打包示例
{
char buff[1024];
// 对安定宝ID为666666的主机进行撤防
int res = pack(buff, sizeof(buff), 1, 666666, 1400, 0, 0);
assert(res > 0);
//res = parse(buff, res, &cb_commited);
//assert(res == 0);
}
// ACK 示例
{
char buff[1024];
// 对安定宝ID为666666的主机回应ACK
int res = pack_ack(buff, sizeof(buff), 1, 666666);
assert(res > 0);
//res = parse(buff, res, &cb_commited);
//assert(res == 0);
}
}
......@@ -104,6 +104,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
......@@ -117,6 +118,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment