123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #pragma once
- #include "stdafx.h"
- #include <string>
- #include <map>
- #include <assert.h>
- #include <functional>
- using namespace std::placeholders;
- namespace DIOS::Dev::Detail::SYNBOX
- {
- static const int TIMEOUTVALUE = 100;
- //using JSONString = std::string;
- //namespace DevDAP = DIOS::Dev::Detail::DAP;
- static const int MAX_COMMAND_LEN = 100;
- static const int msTimeOut_Lock = 500;
- static int CalChecksum(const char* cCmd, int nCmdSize)
- {
- int nSum = 0;
- for (int i = 0; i < nCmdSize; ++i)
- {
- nSum += cCmd[i];
- }
- return nSum % 256;
- }
- static bool FormatCmd(char* cCmd, int& nSize)
- {
- if (nSize < 1)
- return false;
- char temp[MAX_COMMAND_LEN];
- for (int i = 0; i < nSize; ++i)
- {
- cCmd[i] = toupper(cCmd[i]);
- }
- memcpy((void*)(temp), (void*)cCmd, nSize);
- temp[nSize] = char(0x0d);
- nSize++;
- temp[nSize] = char(0x0a);
- nSize++;
- //strcpy_s((char *)cCmd,nSize,(char *)temp);
- memcpy((void*)cCmd, (void*)temp, nSize);
- return true;
- }
- //把指令生产Char和hex字符串
- static bool CmdtoString(const char* cCmd, int nCmdSize, std::string& strCmd)
- {
- std::string strTemp;
- strCmd = "";
- for (int i = 0; i < nCmdSize; i++)
- {
- strTemp = "";
- if ((cCmd[i] == '\r') || (cCmd[i] == '\n') || (cCmd[i] == ':')) //
- strTemp = "";
- else
- strTemp = toupper(cCmd[i]);
- strCmd += strTemp;
- }
- return true;
- }
- static std::string GetContentFromString(std::string& strContent, char ch)
- {
- std::string strTemp;
- size_t nPos = strContent.find(ch);
- if (nPos == std::string::npos)
- {
- strTemp = strContent;
- strContent.clear();
- }
- else
- {
- strTemp = strContent.substr(0, nPos);
- strContent = strContent.substr(nPos + 1, strContent.length() - nPos - 1);
- }
- return strTemp;
- }
- static int String2Hex(std::string str)
- {
- int base = 16;
- char* entptr;
- int nRes = strtol(str.c_str(), &entptr, base);
- return nRes;
- }
- //Hex字符转化到Int
- static int ChartoInt(char uc)
- {
- int nValue;
- if ('0' <= uc && uc <= '9')
- {
- nValue = uc - '0';
- }
- else if ('A' <= uc && uc <= 'F')
- {
- nValue = 10 + uc - 'A';
- }
- else if ('a' <= uc && uc <= 'f')
- {
- nValue = 10 + uc - 'a';
- }
- else
- {
- nValue = -1;
- }
- return nValue;
- }
- struct tFrameMapping
- {
- static const int MaxLen = 5; // 前缀不能超超过 5 个字符 !
- using cbFun = std::function <void(const char*, int)>;
- char strHead[MaxLen]{0};
- int NbOfCharOfHead;
- cbFun fun;
- tFrameMapping(char* str, int len, cbFun f)
- {
- assert(len < MaxLen);
- for (int i = 0; i < len; i++)
- strHead[i] = str[i];
- NbOfCharOfHead = len;
- fun = f;
- }
- };
- static std::list <tFrameMapping> arFrame;
- //-----------------------------------------------------------------------------
- // DecodeFrame
- //-----------------------------------------------------------------------------
- static bool DecodeFrame(const char* strFrame, int length)
- {
- auto pr = [strFrame, length](const tFrameMapping& Item)
- {
- for (int i = 0; i < Item.NbOfCharOfHead; i++)
- {
- if (strFrame[i] != Item.strHead[i])
- {
- return false;
- }
- }
- return true;
- };
- auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
- if (found == arFrame.end())
- {
- return false;
- }
- const auto& Item = *found;
- auto pc = strFrame;
- //pc += Item.NbOfCharOfHead;
- Item.fun(pc, length/* - Item.NbOfCharOfHead*/);
- return true;
- }
- }
|