/*
 * @Author       : ChenJie
 * @Date         : 2022-01-21 09:28:20
 * @Version      : V3.0
 * @LastEditors  : ChenJie
 * @LastEditTime : 2022-01-27 18:24:36
 * @Description  : file content
 * @FilePath     : \S32K144_BLE\src\AppFuncLib.c
 */
/*
 * AppFuncLib.c
 *
 *  Created on: 2022年1月21日
 *      Author: QiXiang_CHENJIE
 */

#include "AppFuncLib.h"
uint16 ATstrdel(char *str)
{
    char *p = str;
    bool flag = false;
    while (*str)
    {
        if (*str > 0x20)
        {
            *(p) = *str;
            p = p + 1;
            flag = false;
        }
        else
        {
            if (!flag)
            {
                *(p) = ',';
                p = p + 1;
                flag = true;
            }
        }
        str++;
    }
    *p = '\0';
    return 0;
}
uint16 mstrlen(const char *s)
{
	uint16 out = 0;
    const char *ss = s;
    while (*ss)
        ss++;
    out = (ss - s);
    return out;
}
int mstrncmp(const char *s1, const char *s2, int n)
{
    const unsigned char *c1 = (const unsigned char *)s1;
    const unsigned char *c2 = (const unsigned char *)s2;
    unsigned char ch;
    int d = 0;
    while (n--)
    {
        d = (int)(ch = *c1++) - (int)*c2++;
        if (d || !ch)
            break;
    }
    return d;
}
unsigned char HexToChar(unsigned char bHex)
{
    if ((bHex >= 0) && (bHex <= 9))
        bHex += 0x30;
    else if ((bHex >= 10) && (bHex <= 15)) //大写字母
        bHex += 0x37;
    else
        bHex = 0xff;
    return bHex;
}
unsigned char CharToHex(unsigned char bChar)
{
    if ((bChar >= 0x30) && (bChar <= 0x39))
        bChar -= 0x30;
    else if ((bChar >= 0x41) && (bChar <= 0x46)) //大写字母
        bChar -= 0x37;
    else if ((bChar >= 0x61) && (bChar <= 0x66)) //小写字母
        bChar -= 0x57;
    else
        bChar = 0xff;
    return bChar;
}
uint8 AtStrCompare(const char *a, const char *b)
{
    uint8 out = 1;
    while (1)
    {
        if (*a == '\0' || *b == '\0') //判断其中是否有字符串结束
        {
            if (strlen(a) == strlen(b))
            {
                out = 1;
                break;
            }
            else
            {
                out = 0;
                break;
            }
        }
        else
        {
            if (*a != *b)
            {
                out = 0;
                break;
            }
            else if (*a == '=' && *b == '=')
            {
                out = 1;
                break;
            }
        }
        a++;
        b++;
    }
    return out;
}
unsigned short CRC16_Modbus ( unsigned char *pdata, int len)
{
	unsigned short crc=0xFFFF;
	int i, j;
	for ( j=0; j<len;j++)
	{
		crc=crc^pdata[j];
		for ( i=0; i<8; i++)
		{
			if( ( crc&0x0001) >0)
			{
				crc=crc>>1;
				crc=crc^ 0xa001;
			}
			else
			crc=crc>>1;
		}
	}
	return crc;
}
