半导体:Gem/Secs基本协议库的开发(3)

2023-12-15 21:36

本文主要是介绍半导体:Gem/Secs基本协议库的开发(3),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

接着上一篇《半导体:Gem/Secs基本协议库的开发(2)》继续,模拟工具延到下一篇,别催!

【codes】

//hsmsmessageheader.h
/*****************************************         HsmsMessageHeader****************************************/#ifndef HSMSMESSAGEHEADER_H
#define HSMSMESSAGEHEADER_H#include "JcHsms.h"class JCHSMS_EXPORT HsmsMessageHeader
{
public:HsmsMessageHeader();  // originateHsmsMessageHeader(uint16_t sessionID, uint8_t stream, uint8_t function,uint8_t ptype, uint8_t stype,  uint32_t systembytes);HsmsMessageHeader(QByteArray source); // interpretQByteArray toByteArray();QByteArray toByteArray(uint16_t sessionID,uint8_t stream,uint8_t function,uint8_t ptype,uint8_t stype,uint32_t systembytes);uint16_t GetSessionID();uint8_t  Getstream();uint8_t  Getfunction();uint8_t  GetpType();uint8_t  GetsType();uint32_t GetSystemBytes();MessageType GetMessageType();static uint16_t GetSessionID(const QByteArray& source);static uint8_t  Getstream(const QByteArray& source);static uint8_t  Getfunction(const QByteArray& source);static uint8_t  GetpType(const QByteArray& source);static uint8_t  GetsType(const QByteArray& source);static uint32_t GetSystemBytes(const QByteArray& source);static MessageType GetMessageType(const QByteArray& source);void SetSessionID(uint16_t id);void SetStreamFunction(uint8_t stream, uint8_t function);void SetWBit(WBit bit);void SetPSType(uint8_t ptype, uint8_t stype);void SetSystemBytes(uint32_t systemByte);uint32_t uniqueSystemBytes();public:static uint32_t m_systembytes;  /// 4 bytes SystemBytes.static uint32_t SystemBytes_counter;
private:QByteArray m_source;            /// 10 bytes.fixed.uint16_t m_sessionID;           /// 2 bytesuint8_t m_stream;               /// 1 byte streamuint8_t m_function;             /// 1 byte functionuint8_t m_stype;                /// 1 byte stypeuint8_t m_ptype;                /// 1 byte ptype
};#endif // HSMSMESSAGEHEADER_H
// hsmsmessageheader.cpp/*******************************************************************************   HsmsMessageHeader******************************************************************************/#include "hsmsmessageheader.h"uint32_t HsmsMessageHeader::m_systembytes = 0;
uint32_t HsmsMessageHeader::SystemBytes_counter = 0;/*!* \brief The HsmsMessageHeader struct*  10 bytes message header* segment ==>|session id |  stream | function | pType | SType |     system bytes             |*            |hDev |lDev |------------------------------------| source num | transaction num |* bytes   ==>|    2      |    1    |   1      |   1   |  1    |            4                 |*/
HsmsMessageHeader::HsmsMessageHeader()
{m_source.resize(10);}HsmsMessageHeader::HsmsMessageHeader(QByteArray source)
{assert(m_source.length() != HSMS_MESSAGEHEADER_LEN);m_source.resize(10);m_source = source;m_sessionID = GetSessionID(source);m_stream = Getstream(source);m_function = Getfunction(source);m_stype = GetsType(source);m_ptype = GetpType(source);m_systembytes = GetSystemBytes(source);
}HsmsMessageHeader::HsmsMessageHeader(uint16_t sessionID, uint8_t stream, uint8_t function,uint8_t ptype, uint8_t stype,  uint32_t systembytes)
{m_sessionID = sessionID;m_stream = stream;m_function = function;m_stype = stype;m_ptype = ptype;m_systembytes = systembytes;m_source.resize(10);toByteArray();}QByteArray HsmsMessageHeader::toByteArray()
{SetSessionID(m_sessionID);SetStreamFunction(m_stream,m_function);SetPSType(m_ptype,m_stype);SetSystemBytes(m_systembytes);return m_source;
}QByteArray HsmsMessageHeader::toByteArray(uint16_t sessionID, uint8_t stream,uint8_t  function, uint8_t ptype,uint8_t  stype   , uint32_t systembytes)
{SetSessionID(sessionID);SetStreamFunction(stream,function);SetPSType(ptype,stype);SetSystemBytes(systembytes);return m_source;
}uint16_t HsmsMessageHeader::GetSessionID()
{return m_sessionID;
}uint8_t HsmsMessageHeader::Getstream()
{return m_stream;
}uint8_t HsmsMessageHeader::Getfunction()
{return m_function;
}uint8_t HsmsMessageHeader::GetpType()
{return m_ptype;
}uint8_t HsmsMessageHeader::GetsType()
{return m_stype;
}uint32_t HsmsMessageHeader::GetSystemBytes()
{return m_systembytes;
}MessageType HsmsMessageHeader::GetMessageType()
{if(m_source.size() < 10){return ErrorMessageType;}if( GetpType() != 0){return ErrorMessageType;}if( Getstream() & ( !GetsType() ) ){return DataMessage;}if( GetsType() >= 0 && GetsType() < 10){return static_cast<MessageType>(GetsType());}else{return ErrorMessageType;}
}uint16_t HsmsMessageHeader::GetSessionID(const QByteArray &source)
{uint16_t id = static_cast<uint16_t>(source.at(0));id = (id << 8) + static_cast<uint16_t>(source.at(1));return id;
}uint8_t HsmsMessageHeader::Getstream(const QByteArray &source)
{return static_cast<uint8_t>(source.at(2) & 0b01111111);
}uint8_t HsmsMessageHeader::Getfunction(const QByteArray &source)
{return static_cast<uint8_t>(source.at(3));
}uint8_t HsmsMessageHeader::GetpType(const QByteArray &source)
{return static_cast<uint8_t>(source.at(4));
}uint8_t HsmsMessageHeader::GetsType(const QByteArray &source)
{return static_cast<uint8_t>(source.at(5));
}uint32_t HsmsMessageHeader::GetSystemBytes(const QByteArray &source)
{uint32_t lenA[4] = {0};uint32_t len = 0;lenA[0] = static_cast<uint32_t>(source.at(6));lenA[1] = static_cast<uint32_t>(source.at(7));lenA[2] = static_cast<uint32_t>(source.at(8));lenA[3] = static_cast<uint32_t>(source.at(9));#if ENDIAN == LITTLE_ENDIANfor(int i = 0; i < 4;i++) len += (lenA[i] << ((3-i)*8 ));
#elsefor(int i = 0; i < 4;i++)  len += (lenA[i] << (i*8 ));
#endifreturn len;
}MessageType HsmsMessageHeader::GetMessageType(const QByteArray &source)
{if(source.size() < 10){return ErrorMessageType;}if( GetpType(source) != 0){return ErrorMessageType;}if( Getstream(source) & ( !GetsType(source) ) ){return DataMessage;}if( GetsType(source) >= 0 && GetsType(source) < 10){return static_cast<MessageType>(GetsType(source));}else{return ErrorMessageType;}
}void HsmsMessageHeader::SetSessionID(uint16_t id)
{
#if  ENDIAN == LITTLE_ENDIANm_source[0] = static_cast<char>( (id >> 8) & 0xff); //header byte 0m_source[1] = static_cast<char>( (id >> 0) & 0xff); //header byte 1
#elsem_source[1] = static_cast<char>( (id >> 8) & 0xff); //header byte 1m_source[0] = static_cast<char>( (id >> 0) & 0xff); //header byte 0
#endifm_sessionID = id;
}void HsmsMessageHeader::SetStreamFunction(uint8_t stream, uint8_t function)
{m_source[2] = static_cast<char>(stream);   //header byte 2m_source[3] = static_cast<char>(function); //header byte 3m_stream = stream;m_function = function;
}void HsmsMessageHeader::SetWBit(WBit bit)
{uint8_t f = Getfunction();uint8_t s = Getstream();if(bit == NeedReply){s = s | 0b10000000;}else{s = s & 0b01111111;}SetStreamFunction(s, f);
}void HsmsMessageHeader::SetPSType(uint8_t ptype, uint8_t stype)
{m_source[4] = static_cast<char>(ptype);  //header byte 4m_source[5] = static_cast<char>(stype);  //header byte 5m_ptype = ptype;m_stype = stype;
}void HsmsMessageHeader::SetSystemBytes(uint32_t systemByte)
{m_source[6] = static_cast<uchar>( (systemByte >> 24) & 0xff); //header byte 6m_source[7] = static_cast<uchar>( (systemByte >> 16) & 0xff); //header byte 7m_source[8] = static_cast<uchar>( (systemByte >> 8 ) & 0xff); //header byte 8m_source[9] = static_cast<uchar>( (systemByte >> 0 ) & 0xff); //header byte 9m_systembytes = systemByte;
}uint32_t HsmsMessageHeader::uniqueSystemBytes()
{SystemBytes_counter ++;uint32_t timeDate = QDateTime::currentDateTime().toTime_t();  // 获取当前时间,将当前时间转为时间戳m_systembytes = timeDate<<4;m_systembytes += SystemBytes_counter%16;return m_systembytes;
}
//  secs2item.h/*****************************************         Secs2Item***************************************** ItemHeader      | Item Body     |* format_lenbytes | Len | context |* format|len bytes| Len | context |****************************************/#ifndef SECS2ITEM_H
#define SECS2ITEM_H#include "JcHsms_global.h"class JCHSMS_EXPORT Secs2Item
{
public:Secs2Item() {}Secs2Item(QString name);Secs2Item(ItemFormatCode format,QVariant val);Secs2Item(QByteArray source); // interpretQByteArray toByteArray() const;int format() const;bool isEmpty() const;static int  format(const QByteArray &array);static bool isValidFormat(ItemFormatCode format);static int  itemHeaderSize(const QByteArray &array);static int  bytes(const QByteArray& array);const int bytes() const;QByteArray itemHeader() const;QByteArray itemBody() const;QByteArray itemDataAll() const;int buildItemHeader();void AddBinary(const QByteArray& x);void AddASCII(QString str);void AddASCII(const char* str,int len);void AddJIS8(const char * str); // unicodevoid AddBoolean(const bool x);void AddBoolean(const bool x[], int N);void AddInt8(const int8_t x);void AddInt8(const int8_t x[], int N);void AddInt8

这篇关于半导体:Gem/Secs基本协议库的开发(3)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/497971

相关文章

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

MySQL 中的 LIMIT 语句及基本用法

《MySQL中的LIMIT语句及基本用法》LIMIT语句用于限制查询返回的行数,常用于分页查询或取部分数据,提高查询效率,:本文主要介绍MySQL中的LIMIT语句,需要的朋友可以参考下... 目录mysql 中的 LIMIT 语句1. LIMIT 语法2. LIMIT 基本用法(1) 获取前 N 行数据(

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Nginx中配置HTTP/2协议的详细指南

《Nginx中配置HTTP/2协议的详细指南》HTTP/2是HTTP协议的下一代版本,旨在提高性能、减少延迟并优化现代网络环境中的通信效率,本文将为大家介绍Nginx配置HTTP/2协议想详细步骤,需... 目录一、HTTP/2 协议概述1.HTTP/22. HTTP/2 的核心特性3. HTTP/2 的优

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

关于WebSocket协议状态码解析

《关于WebSocket协议状态码解析》:本文主要介绍关于WebSocket协议状态码的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录WebSocket协议状态码解析1. 引言2. WebSocket协议状态码概述3. WebSocket协议状态码详解3