initial commit
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file bda.c
|
||||
*
|
||||
* \brief Bluetooth device address utilities.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2009-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include "wsf_types.h"
|
||||
#include "bda.h"
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaCpy
|
||||
*
|
||||
* \brief Copy a BD address from source to destination.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void BdaCpy(uint8_t *pDst, const uint8_t *pSrc)
|
||||
{
|
||||
memcpy(pDst, pSrc, BDA_ADDR_LEN);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaCmp
|
||||
*
|
||||
* \brief Compare two BD addresses.
|
||||
*
|
||||
* \param pAddr1 First address.
|
||||
* \param pAddr2 Second address.
|
||||
*
|
||||
* \return TRUE if addresses match, FALSE otherwise.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
bool_t BdaCmp(const uint8_t *pAddr1, const uint8_t *pAddr2)
|
||||
{
|
||||
return (memcmp(pAddr1, pAddr2, BDA_ADDR_LEN) == 0);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaClr
|
||||
*
|
||||
* \brief Set a BD address to all zeros.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
*
|
||||
* \return pDst + BDA_ADDR_LEN
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint8_t *BdaClr(uint8_t *pDst)
|
||||
{
|
||||
memset(pDst, 0, BDA_ADDR_LEN);
|
||||
|
||||
return (pDst + BDA_ADDR_LEN);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaIsZeros
|
||||
*
|
||||
* \brief Check if a BD address is all zeros.
|
||||
*
|
||||
* \param pAddr Pointer to address.
|
||||
*
|
||||
* \return TRUE if address is all zeros, FALSE otherwise.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
bool_t BdaIsZeros(const uint8_t *pAddr)
|
||||
{
|
||||
uint8_t addrZeros[BDA_ADDR_LEN] = { 0 };
|
||||
|
||||
return (memcmp(pAddr, addrZeros, BDA_ADDR_LEN) == 0);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Bda2Str
|
||||
*
|
||||
* \brief Convert a BD address to a string.
|
||||
*
|
||||
* \param pAddr Pointer to BD address.
|
||||
*
|
||||
* \return Pointer to string.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
char *Bda2Str(const uint8_t *pAddr)
|
||||
{
|
||||
static const char hex[] = "0123456789ABCDEF";
|
||||
static char str[BDA_ADDR_STR_LEN + 1];
|
||||
char *pStr = str;
|
||||
|
||||
/* address is little endian so copy in reverse */
|
||||
pAddr += BDA_ADDR_LEN;
|
||||
|
||||
while (pStr < &str[BDA_ADDR_STR_LEN])
|
||||
{
|
||||
*pStr++ = hex[*--pAddr >> 4];
|
||||
*pStr++ = hex[*pAddr & 0x0F];
|
||||
}
|
||||
|
||||
/* null terminate string */
|
||||
*pStr = 0;
|
||||
|
||||
return str;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file bda.h
|
||||
*
|
||||
* \brief Bluetooth device address utilities.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2009-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
#ifndef BDA_H
|
||||
#define BDA_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! \brief BD address length */
|
||||
#define BDA_ADDR_LEN 6
|
||||
|
||||
/*! \brief BD address string length */
|
||||
#define BDA_ADDR_STR_LEN (BDA_ADDR_LEN * 2)
|
||||
|
||||
/*! \brief BDA RPA check */
|
||||
#define BDA_ADDR_IS_RPA(bda) (((bda)[5] & 0xC0) == 0x40)
|
||||
|
||||
/*! \brief BDA NRPA check */
|
||||
#define BDA_ADDR_IS_NRPA(bda) (((bda)[5] & 0xC0) == 0x00)
|
||||
|
||||
/*! \brief BDA static random check */
|
||||
#define BDA_ADDR_IS_STATIC(bda) (((bda)[5] & 0xC0) == 0xC0)
|
||||
|
||||
/*! \brief BDA64 RPA check */
|
||||
#define BDA64_ADDR_IS_RPA(bda64) ((((bda64) >> 40) & 0xC0) == 0x40)
|
||||
|
||||
/*! \brief BDA64 NRPA check */
|
||||
#define BDA64_ADDR_IS_NRPA(bda64) ((((bda64) >> 40) & 0xC0) == 0x00)
|
||||
|
||||
/*! \brief BDA64 static random check */
|
||||
#define BDA64_ADDR_IS_STATIC(bda64) ((((bda64) >> 40) & 0xC0) == 0xC0)
|
||||
|
||||
/**************************************************************************************************
|
||||
Data Types
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! \brief BD address data type */
|
||||
typedef uint8_t bdAddr_t[BDA_ADDR_LEN];
|
||||
|
||||
/**************************************************************************************************
|
||||
Function Declarations
|
||||
**************************************************************************************************/
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaCpy
|
||||
*
|
||||
* \brief Copy a BD address from source to destination.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void BdaCpy(uint8_t *pDst, const uint8_t *pSrc);
|
||||
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaCmp
|
||||
*
|
||||
* \brief Compare two BD addresses.
|
||||
*
|
||||
* \param pAddr1 First address.
|
||||
* \param pAddr2 Second address.
|
||||
*
|
||||
* \return TRUE if addresses match, FALSE otherwise.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
bool_t BdaCmp(const uint8_t *pAddr1, const uint8_t *pAddr2);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaClr
|
||||
*
|
||||
* \brief Set a BD address to all zeros.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
*
|
||||
* \return pDst + BDA_ADDR_LEN
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint8_t *BdaClr(uint8_t *pDst);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn BdaIsZeros
|
||||
*
|
||||
* \brief Check if a BD address is all zeros.
|
||||
*
|
||||
* \param pAddr Pointer to address.
|
||||
*
|
||||
* \return TRUE if address is all zeros, FALSE otherwise.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
bool_t BdaIsZeros(const uint8_t *pAddr);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Bda2Str
|
||||
*
|
||||
* \brief Convert a BD address to a string.
|
||||
*
|
||||
* \param pAddr Pointer to BD address.
|
||||
*
|
||||
* \return Pointer to string.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
char *Bda2Str(const uint8_t *pAddr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif /* BDA_H */
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file bstream.h
|
||||
*
|
||||
* \brief Byte stream to integer conversion functions.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2009-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#include "wsf_types.h"
|
||||
#include "bstream.h"
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \brief Convert bstream to uint64_t.
|
||||
*
|
||||
* \param p Bstream pointer.
|
||||
*
|
||||
* \return Resulting uint64_t number.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint64_t BstreamToUint64(const uint8_t *p)
|
||||
{
|
||||
return ((uint64_t)p[0] << 0) |
|
||||
((uint64_t)p[1] << 8) |
|
||||
((uint64_t)p[2] << 16) |
|
||||
((uint64_t)p[3] << 24) |
|
||||
((uint64_t)p[4] << 32) |
|
||||
((uint64_t)p[5] << 40) |
|
||||
((uint64_t)p[6] << 48) |
|
||||
((uint64_t)p[7] << 56);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \brief Convert uint64_t to bstream.
|
||||
*
|
||||
* \param p Bstream pointer.
|
||||
* \param n uint64_t number.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Uint64ToBstream(uint8_t *p, uint64_t n)
|
||||
{
|
||||
p[0] = (uint8_t)(n >> 0);
|
||||
p[1] = (uint8_t)(n >> 8);
|
||||
p[2] = (uint8_t)(n >> 16);
|
||||
p[3] = (uint8_t)(n >> 24);
|
||||
p[4] = (uint8_t)(n >> 32);
|
||||
p[5] = (uint8_t)(n >> 40);
|
||||
p[6] = (uint8_t)(n >> 48);
|
||||
p[7] = (uint8_t)(n >> 56);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \brief Convert bstream to BDA64.
|
||||
*
|
||||
* \param p Bstream pointer.
|
||||
*
|
||||
* \return Resulting BDA64 number.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint64_t BstreamToBda64(const uint8_t *p)
|
||||
{
|
||||
return (uint64_t)p[0] << 0 |
|
||||
(uint64_t)p[1] << 8 |
|
||||
(uint64_t)p[2] << 16 |
|
||||
(uint64_t)p[3] << 24 |
|
||||
(uint64_t)p[4] << 32 |
|
||||
(uint64_t)p[5] << 40;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \brief Convert BDA64 to bstream.
|
||||
*
|
||||
* \param p Bstream pointer.
|
||||
* \param bda uint64_t BDA.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Bda64ToBstream(uint8_t *p, uint64_t bda)
|
||||
{
|
||||
p[0] = (uint8_t)(bda >> 0);
|
||||
p[1] = (uint8_t)(bda >> 8);
|
||||
p[2] = (uint8_t)(bda >> 16);
|
||||
p[3] = (uint8_t)(bda >> 24);
|
||||
p[4] = (uint8_t)(bda >> 32);
|
||||
p[5] = (uint8_t)(bda >> 40);
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file bstream.h
|
||||
*
|
||||
* \brief Byte stream to integer conversion macros.
|
||||
*
|
||||
* $Date: 2017-02-06 19:25:09 -0600 (Mon, 06 Feb 2017) $
|
||||
* $Revision: 11104 $
|
||||
*
|
||||
* Copyright (c) 2009-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
#ifndef BSTREAM_H
|
||||
#define BSTREAM_H
|
||||
|
||||
#include "bda.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
|
||||
/*!
|
||||
* Macros for converting a little endian byte buffer to integers.
|
||||
*/
|
||||
#define BYTES_TO_UINT16(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8));}
|
||||
|
||||
#define BYTES_TO_UINT24(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8) + \
|
||||
((uint16_t)(p)[2] << 16));}
|
||||
|
||||
#define BYTES_TO_UINT32(n, p) {n = ((uint32_t)(p)[0] + ((uint32_t)(p)[1] << 8) + \
|
||||
((uint32_t)(p)[2] << 16) + ((uint32_t)(p)[3] << 24));}
|
||||
|
||||
#define BYTES_TO_UINT40(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
|
||||
((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
|
||||
((uint64_t)(p)[4] << 32));}
|
||||
|
||||
#define BYTES_TO_UINT64(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
|
||||
((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
|
||||
((uint64_t)(p)[4] << 32) + ((uint64_t)(p)[5] << 40) + \
|
||||
((uint64_t)(p)[6] << 48) + ((uint64_t)(p)[7] << 56));}
|
||||
|
||||
/*!
|
||||
* Macros for converting little endian integers to array of bytes
|
||||
*/
|
||||
#define UINT16_TO_BYTES(n) ((uint8_t) (n)), ((uint8_t)((n) >> 8))
|
||||
#define UINT32_TO_BYTES(n) ((uint8_t) (n)), ((uint8_t)((n) >> 8)), ((uint8_t)((n) >> 16)), ((uint8_t)((n) >> 24))
|
||||
|
||||
/*!
|
||||
* Macros for converting little endian integers to single bytes
|
||||
*/
|
||||
#define UINT16_TO_BYTE0(n) ((uint8_t) (n))
|
||||
#define UINT16_TO_BYTE1(n) ((uint8_t) ((n) >> 8))
|
||||
|
||||
#define UINT32_TO_BYTE0(n) ((uint8_t) (n))
|
||||
#define UINT32_TO_BYTE1(n) ((uint8_t) ((n) >> 8))
|
||||
#define UINT32_TO_BYTE2(n) ((uint8_t) ((n) >> 16))
|
||||
#define UINT32_TO_BYTE3(n) ((uint8_t) ((n) >> 24))
|
||||
|
||||
/*!
|
||||
* Macros for converting a little endian byte stream to integers, with increment.
|
||||
*/
|
||||
#define BSTREAM_TO_INT8(n, p) {n = (int8_t)(*(p)++);}
|
||||
#define BSTREAM_TO_UINT8(n, p) {n = (uint8_t)(*(p)++);}
|
||||
#define BSTREAM_TO_UINT16(n, p) {BYTES_TO_UINT16(n, p); p += 2;}
|
||||
#define BSTREAM_TO_UINT24(n, p) {BYTES_TO_UINT24(n, p); p += 3;}
|
||||
#define BSTREAM_TO_UINT32(n, p) {BYTES_TO_UINT32(n, p); p += 4;}
|
||||
#define BSTREAM_TO_UINT40(n, p) {BYTES_TO_UINT40(n, p); p += 5;}
|
||||
#define BSTREAM_TO_UINT64(n, p) {n = BstreamToUint64(p); p += 8;}
|
||||
#define BSTREAM_TO_BDA(bda, p) {BdaCpy(bda, p); p += BDA_ADDR_LEN;}
|
||||
#define BSTREAM_TO_BDA64(bda, p) {bda = BstreamToBda64(p); p += BDA_ADDR_LEN;}
|
||||
|
||||
/*!
|
||||
* Macros for converting integers to a little endian byte stream, with increment.
|
||||
*/
|
||||
#define UINT8_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n);}
|
||||
#define UINT16_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8);}
|
||||
#define UINT24_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
|
||||
*(p)++ = (uint8_t)((n) >> 16);}
|
||||
#define UINT32_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
|
||||
*(p)++ = (uint8_t)((n) >> 16); *(p)++ = (uint8_t)((n) >> 24);}
|
||||
#define UINT40_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
|
||||
*(p)++ = (uint8_t)((n) >> 16); *(p)++ = (uint8_t)((n) >> 24); \
|
||||
*(p)++ = (uint8_t)((n) >> 32);}
|
||||
#define UINT64_TO_BSTREAM(p, n) {Uint64ToBstream(p, n); p += sizeof(uint64_t);}
|
||||
#define BDA_TO_BSTREAM(p, bda) {BdaCpy(p, bda); p += BDA_ADDR_LEN;}
|
||||
#define BDA64_TO_BSTREAM(p, bda) {Bda64ToBstream(p, bda); p += BDA_ADDR_LEN;}
|
||||
|
||||
/*!
|
||||
* Macros for converting integers to a little endian byte stream, without increment.
|
||||
*/
|
||||
#define UINT16_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8);}
|
||||
#define UINT24_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
|
||||
(p)[2] = (uint8_t)((n) >> 16);}
|
||||
#define UINT32_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
|
||||
(p)[2] = (uint8_t)((n) >> 16); (p)[3] = (uint8_t)((n) >> 24);}
|
||||
#define UINT40_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
|
||||
(p)[2] = (uint8_t)((n) >> 16); (p)[3] = (uint8_t)((n) >> 24);\
|
||||
(p)[4] = (uint8_t)((n) >> 32);}
|
||||
|
||||
/*!
|
||||
* Macros for comparing a little endian byte buffer to integers.
|
||||
*/
|
||||
#define BYTES_UINT16_CMP(p, n) ((p)[1] == UINT16_TO_BYTE1(n) && (p)[0] == UINT16_TO_BYTE0(n))
|
||||
|
||||
/*!
|
||||
* Macros for IEEE FLOAT type: exponent = byte 3, mantissa = bytes 2-0
|
||||
*/
|
||||
#define FLT_TO_UINT32(m, e) ((m) | ((int32_t)(e) << 24))
|
||||
#define UINT32_TO_FLT(m, e, n) {m = UINT32_TO_FLT_M(n); e = UINT32_TO_FLT_E(n);}
|
||||
#define UINT32_TO_FLT_M(n) ((((n) & 0x00FFFFFF) >= 0x00800000) ? \
|
||||
((int32_t)(((n) | 0xFF000000))) : ((int32_t)((n) & 0x00FFFFFF)))
|
||||
#define UINT32_TO_FLT_E(n) ((int8_t)(n >> 24))
|
||||
/*!
|
||||
* Macros for IEEE SFLOAT type: exponent = bits 15-12, mantissa = bits 11-0
|
||||
*/
|
||||
#define SFLT_TO_UINT16(m, e) ((m) | ((int16_t)(e) << 12))
|
||||
#define UINT16_TO_SFLT(m, e, n) {m = UINT16_TO_SFLT_M(n); e = UINT16_TO_SFLT_E(n);}
|
||||
#define UINT16_TO_SFLT_M(n) ((((n) & 0x0FFF) >= 0x0800) ? \
|
||||
((int16_t)(((n) | 0xF000))) : ((int16_t)((n) & 0x0FFF)))
|
||||
#define UINT16_TO_SFLT_E(n) (((n >> 12) >= 0x0008) ? \
|
||||
((int8_t)(((n >> 12) | 0xF0))) : ((int8_t)(n >> 12)))
|
||||
|
||||
/**************************************************************************************************
|
||||
Function Declarations
|
||||
**************************************************************************************************/
|
||||
|
||||
uint64_t BstreamToBda64(const uint8_t *p);
|
||||
uint64_t BstreamToUint64(const uint8_t *p);
|
||||
void Bda64ToBstream(uint8_t *p, uint64_t bda);
|
||||
void Uint64ToBstream(uint8_t *p, uint64_t n);
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif /* BSTREAM_H */
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file calc128.c
|
||||
*
|
||||
* \brief 128-bit integer utilities.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2010-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include "wsf_types.h"
|
||||
#include "calc128.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
Global Variables
|
||||
**************************************************************************************************/
|
||||
|
||||
/* 128-bit zero value */
|
||||
const uint8_t calc128Zeros[CALC128_LEN] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
/**************************************************************************************************
|
||||
Function Declarations
|
||||
**************************************************************************************************/
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Calc128Cpy
|
||||
*
|
||||
* \brief Copy a 128-bit integer from source to destination.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Calc128Cpy(uint8_t *pDst, uint8_t *pSrc)
|
||||
{
|
||||
memcpy(pDst, pSrc, CALC128_LEN);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Calc128Cpy64
|
||||
*
|
||||
* \brief Copy a 64-bit integer from source to destination.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Calc128Cpy64(uint8_t *pDst, uint8_t *pSrc)
|
||||
{
|
||||
memcpy(pDst, pSrc, CALC128_LEN/2);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Calc128Xor
|
||||
*
|
||||
* \brief Exclusive-or two 128-bit integers and return the result in pDst.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Calc128Xor(uint8_t *pDst, uint8_t *pSrc)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
for (i = CALC128_LEN; i > 0; i--)
|
||||
{
|
||||
*pDst++ ^= *pSrc++;
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file calc128.h
|
||||
*
|
||||
* \brief 128-bit integer utilities.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2010-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
#ifndef CALC128_H
|
||||
#define CALC128_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! 128-bit integer length in bytes */
|
||||
#define CALC128_LEN 16
|
||||
|
||||
/**************************************************************************************************
|
||||
Global Variables
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! 128-bit zero value */
|
||||
extern const uint8_t calc128Zeros[CALC128_LEN];
|
||||
|
||||
/**************************************************************************************************
|
||||
Function Declarations
|
||||
**************************************************************************************************/
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Calc128Cpy
|
||||
*
|
||||
* \brief Copy a 128-bit integer from source to destination.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Calc128Cpy(uint8_t *pDst, uint8_t *pSrc);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Calc128Cpy64
|
||||
*
|
||||
* \brief Copy a 64-bit integer from source to destination.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Calc128Cpy64(uint8_t *pDst, uint8_t *pSrc);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn Calc128Xor
|
||||
*
|
||||
* \brief Exclusive-or two 128-bit integers and return the result in pDst.
|
||||
*
|
||||
* \param pDst Pointer to destination.
|
||||
* \param pSrc Pointer to source.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void Calc128Xor(uint8_t *pDst, uint8_t *pSrc);
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif /* CALC128_H */
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file crc32.c
|
||||
*
|
||||
* \brief CRC-32 utilities.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2010-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#include "wsf_types.h"
|
||||
#include "crc32.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
Local Variables
|
||||
**************************************************************************************************/
|
||||
static const uint32_t crc32Table[256] =
|
||||
{
|
||||
0x00000000U, 0x77073096U, 0xEE0E612CU, 0x990951BAU,
|
||||
0x076DC419U, 0x706AF48FU, 0xE963A535U, 0x9E6495A3U,
|
||||
0x0EDB8832U, 0x79DCB8A4U, 0xE0D5E91EU, 0x97D2D988U,
|
||||
0x09B64C2BU, 0x7EB17CBDU, 0xE7B82D07U, 0x90BF1D91U,
|
||||
0x1DB71064U, 0x6AB020F2U, 0xF3B97148U, 0x84BE41DEU,
|
||||
0x1ADAD47DU, 0x6DDDE4EBU, 0xF4D4B551U, 0x83D385C7U,
|
||||
0x136C9856U, 0x646BA8C0U, 0xFD62F97AU, 0x8A65C9ECU,
|
||||
0x14015C4FU, 0x63066CD9U, 0xFA0F3D63U, 0x8D080DF5U,
|
||||
0x3B6E20C8U, 0x4C69105EU, 0xD56041E4U, 0xA2677172U,
|
||||
0x3C03E4D1U, 0x4B04D447U, 0xD20D85FDU, 0xA50AB56BU,
|
||||
0x35B5A8FAU, 0x42B2986CU, 0xDBBBC9D6U, 0xACBCF940U,
|
||||
0x32D86CE3U, 0x45DF5C75U, 0xDCD60DCFU, 0xABD13D59U,
|
||||
0x26D930ACU, 0x51DE003AU, 0xC8D75180U, 0xBFD06116U,
|
||||
0x21B4F4B5U, 0x56B3C423U, 0xCFBA9599U, 0xB8BDA50FU,
|
||||
0x2802B89EU, 0x5F058808U, 0xC60CD9B2U, 0xB10BE924U,
|
||||
0x2F6F7C87U, 0x58684C11U, 0xC1611DABU, 0xB6662D3DU,
|
||||
0x76DC4190U, 0x01DB7106U, 0x98D220BCU, 0xEFD5102AU,
|
||||
0x71B18589U, 0x06B6B51FU, 0x9FBFE4A5U, 0xE8B8D433U,
|
||||
0x7807C9A2U, 0x0F00F934U, 0x9609A88EU, 0xE10E9818U,
|
||||
0x7F6A0DBBU, 0x086D3D2DU, 0x91646C97U, 0xE6635C01U,
|
||||
0x6B6B51F4U, 0x1C6C6162U, 0x856530D8U, 0xF262004EU,
|
||||
0x6C0695EDU, 0x1B01A57BU, 0x8208F4C1U, 0xF50FC457U,
|
||||
0x65B0D9C6U, 0x12B7E950U, 0x8BBEB8EAU, 0xFCB9887CU,
|
||||
0x62DD1DDFU, 0x15DA2D49U, 0x8CD37CF3U, 0xFBD44C65U,
|
||||
0x4DB26158U, 0x3AB551CEU, 0xA3BC0074U, 0xD4BB30E2U,
|
||||
0x4ADFA541U, 0x3DD895D7U, 0xA4D1C46DU, 0xD3D6F4FBU,
|
||||
0x4369E96AU, 0x346ED9FCU, 0xAD678846U, 0xDA60B8D0U,
|
||||
0x44042D73U, 0x33031DE5U, 0xAA0A4C5FU, 0xDD0D7CC9U,
|
||||
0x5005713CU, 0x270241AAU, 0xBE0B1010U, 0xC90C2086U,
|
||||
0x5768B525U, 0x206F85B3U, 0xB966D409U, 0xCE61E49FU,
|
||||
0x5EDEF90EU, 0x29D9C998U, 0xB0D09822U, 0xC7D7A8B4U,
|
||||
0x59B33D17U, 0x2EB40D81U, 0xB7BD5C3BU, 0xC0BA6CADU,
|
||||
0xEDB88320U, 0x9ABFB3B6U, 0x03B6E20CU, 0x74B1D29AU,
|
||||
0xEAD54739U, 0x9DD277AFU, 0x04DB2615U, 0x73DC1683U,
|
||||
0xE3630B12U, 0x94643B84U, 0x0D6D6A3EU, 0x7A6A5AA8U,
|
||||
0xE40ECF0BU, 0x9309FF9DU, 0x0A00AE27U, 0x7D079EB1U,
|
||||
0xF00F9344U, 0x8708A3D2U, 0x1E01F268U, 0x6906C2FEU,
|
||||
0xF762575DU, 0x806567CBU, 0x196C3671U, 0x6E6B06E7U,
|
||||
0xFED41B76U, 0x89D32BE0U, 0x10DA7A5AU, 0x67DD4ACCU,
|
||||
0xF9B9DF6FU, 0x8EBEEFF9U, 0x17B7BE43U, 0x60B08ED5U,
|
||||
0xD6D6A3E8U, 0xA1D1937EU, 0x38D8C2C4U, 0x4FDFF252U,
|
||||
0xD1BB67F1U, 0xA6BC5767U, 0x3FB506DDU, 0x48B2364BU,
|
||||
0xD80D2BDAU, 0xAF0A1B4CU, 0x36034AF6U, 0x41047A60U,
|
||||
0xDF60EFC3U, 0xA867DF55U, 0x316E8EEFU, 0x4669BE79U,
|
||||
0xCB61B38CU, 0xBC66831AU, 0x256FD2A0U, 0x5268E236U,
|
||||
0xCC0C7795U, 0xBB0B4703U, 0x220216B9U, 0x5505262FU,
|
||||
0xC5BA3BBEU, 0xB2BD0B28U, 0x2BB45A92U, 0x5CB36A04U,
|
||||
0xC2D7FFA7U, 0xB5D0CF31U, 0x2CD99E8BU, 0x5BDEAE1DU,
|
||||
0x9B64C2B0U, 0xEC63F226U, 0x756AA39CU, 0x026D930AU,
|
||||
0x9C0906A9U, 0xEB0E363FU, 0x72076785U, 0x05005713U,
|
||||
0x95BF4A82U, 0xE2B87A14U, 0x7BB12BAEU, 0x0CB61B38U,
|
||||
0x92D28E9BU, 0xE5D5BE0DU, 0x7CDCEFB7U, 0x0BDBDF21U,
|
||||
0x86D3D2D4U, 0xF1D4E242U, 0x68DDB3F8U, 0x1FDA836EU,
|
||||
0x81BE16CDU, 0xF6B9265BU, 0x6FB077E1U, 0x18B74777U,
|
||||
0x88085AE6U, 0xFF0F6A70U, 0x66063BCAU, 0x11010B5CU,
|
||||
0x8F659EFFU, 0xF862AE69U, 0x616BFFD3U, 0x166CCF45U,
|
||||
0xA00AE278U, 0xD70DD2EEU, 0x4E048354U, 0x3903B3C2U,
|
||||
0xA7672661U, 0xD06016F7U, 0x4969474DU, 0x3E6E77DBU,
|
||||
0xAED16A4AU, 0xD9D65ADCU, 0x40DF0B66U, 0x37D83BF0U,
|
||||
0xA9BCAE53U, 0xDEBB9EC5U, 0x47B2CF7FU, 0x30B5FFE9U,
|
||||
0xBDBDF21CU, 0xCABAC28AU, 0x53B39330U, 0x24B4A3A6U,
|
||||
0xBAD03605U, 0xCDD70693U, 0x54DE5729U, 0x23D967BFU,
|
||||
0xB3667A2EU, 0xC4614AB8U, 0x5D681B02U, 0x2A6F2B94U,
|
||||
0xB40BBE37U, 0xC30C8EA1U, 0x5A05DF1BU, 0x2D02EF8DU
|
||||
};
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn CalcCrc32
|
||||
*
|
||||
* \brief Calculate the CRC-32 of the given buffer.
|
||||
*
|
||||
* \param crcInit Initial value of the CRC.
|
||||
* \param len Length of the buffer.
|
||||
* \param pBuf Buffer to compute the CRC.
|
||||
*
|
||||
* \return None.
|
||||
*
|
||||
* This routine was originally generated with crcmod.py using the following parameters:
|
||||
* - polynomial 0x104C11DB7
|
||||
* - bit reverse algorithm
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint32_t CalcCrc32(uint32_t crcInit, uint32_t len, uint8_t *pBuf)
|
||||
{
|
||||
uint32_t crc = crcInit;
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
crc = crc32Table[*pBuf ^ (uint8_t)crc] ^ (crc >> 8);
|
||||
pBuf++;
|
||||
len--;
|
||||
}
|
||||
|
||||
crc = crc ^ 0xFFFFFFFFU;
|
||||
|
||||
return crc;
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file crc32.h
|
||||
*
|
||||
* \brief CRC-32 utilities.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2010-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
#ifndef CRC32_H
|
||||
#define CRC32_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn CalcCrc32
|
||||
*
|
||||
* \brief Calculate the CRC-32 of the given buffer.
|
||||
*
|
||||
* \param crcInit Initial value of the CRC.
|
||||
* \param len Length of the buffer.
|
||||
* \param pBuf Buffer to compute the CRC.
|
||||
*
|
||||
* \return None.
|
||||
*
|
||||
* This routine was originally generated with crcmod.py using the following parameters:
|
||||
* - polynomial 0x104C11DB7
|
||||
* - bit reverse algorithm
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint32_t CalcCrc32(uint32_t crcInit, uint32_t len, uint8_t *pBuf);
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif /* CRC32_H */
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file print.c
|
||||
*
|
||||
* \brief Utility functions.
|
||||
*
|
||||
* $Date: 2016-03-29 14:55:12 -0700 (Tue, 29 Mar 2016) $
|
||||
* $Revision: 6524 $
|
||||
*
|
||||
* Copyright (c) 2015-2017 ARM Ltd., all rights reserved.
|
||||
* ARM confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "print.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! \brief Detect hexadecimal digits. */
|
||||
#define PRINT_IS_XDIGIT(c) (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || \
|
||||
((c >= 'A') && (c <= 'F')))
|
||||
|
||||
/*! \brief Convert hexadecimal digit to integer. */
|
||||
#define PRINT_XDIGIT_TO_INT(c) (((c >= '0') && (c <= '9')) ? (uint8_t)(c - '0') : \
|
||||
((c >= 'a') && (c <= 'f')) ? (uint8_t)(c - 'a' + 10u) : \
|
||||
((c >= 'A') && (c <= 'F')) ? (uint8_t)(c - 'A' + 10u) : 0u)
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn printFmtInt
|
||||
*
|
||||
* \brief Format an integer.
|
||||
*
|
||||
* \param pStr Storage for formatted integer.
|
||||
* \param maxLen Maximum number of characters to store.
|
||||
* \param i Integer to format.
|
||||
* \param base Integer base.
|
||||
* \param sign TRUE if sign should be printed.
|
||||
* \param width Width of field.
|
||||
*
|
||||
* \return Number of characters stored.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static int printFmtInt(char *pStr, int maxLen, int i, uint8_t base, bool_t sign, int width)
|
||||
{
|
||||
char *s, *p = pStr;
|
||||
unsigned int u = (unsigned int) i;
|
||||
uint8_t use_width;
|
||||
int t;
|
||||
|
||||
use_width = width;
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
s = "0\0";
|
||||
width--;
|
||||
goto almost;
|
||||
}
|
||||
|
||||
if (sign && base == 10 && i < 0)
|
||||
{
|
||||
*pStr++ = '-';
|
||||
u = (unsigned int)-i;
|
||||
width--;
|
||||
}
|
||||
s = pStr + maxLen - 1;
|
||||
*s = '\0';
|
||||
|
||||
while (u && (!use_width || (width > 0)))
|
||||
{
|
||||
t = (unsigned int)u % base;
|
||||
if (t >= 10)
|
||||
{
|
||||
t += 'A' - '0' - 10;
|
||||
}
|
||||
*--s = (char)(t + '0');
|
||||
u /= base;
|
||||
width--;
|
||||
}
|
||||
|
||||
almost:
|
||||
while (width > 0)
|
||||
{
|
||||
*pStr++ = '0';
|
||||
width--;
|
||||
}
|
||||
strcpy(pStr, s);
|
||||
|
||||
return strlen(p);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn printParseInt
|
||||
*
|
||||
* \brief Parse an integer from a string.
|
||||
*
|
||||
* \param pStr String to parse.
|
||||
* \param pInt Storage for parsed integer.
|
||||
* \param base Integer base.
|
||||
*
|
||||
* \return Number of characters consumed.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static int32_t printParseInt(const char *pStr, uint32_t *pInt, uint32_t base)
|
||||
{
|
||||
int32_t r = 0;
|
||||
|
||||
if (base == 0)
|
||||
{
|
||||
if (*pStr == '0')
|
||||
{
|
||||
if (((*(pStr + 1) == 'x') || (*(pStr + 1) == 'X')) && PRINT_IS_XDIGIT(*(pStr + 2)))
|
||||
{
|
||||
r += 2;
|
||||
pStr += 2;
|
||||
base = 16;
|
||||
}
|
||||
else
|
||||
{
|
||||
base = 8;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
base = 10;
|
||||
}
|
||||
}
|
||||
else if (base == 16)
|
||||
{
|
||||
if (*pStr == '0')
|
||||
{
|
||||
if (((*(pStr + 1) == 'x') || (*(pStr + 1) == 'X')) && PRINT_IS_XDIGIT(*(pStr + 2)))
|
||||
{
|
||||
r += 2;
|
||||
pStr += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* One character is required; all characters must be consumed. */
|
||||
*pInt = 0;
|
||||
do
|
||||
{
|
||||
char c;
|
||||
uint8_t t;
|
||||
|
||||
c = *pStr++;
|
||||
if (!PRINT_IS_XDIGIT(c))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
t = PRINT_XDIGIT_TO_INT(c);
|
||||
if (t >= base)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
*pInt *= base;
|
||||
*pInt += t;
|
||||
r++;
|
||||
} while (*pStr != '\0');
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn PrintVsn
|
||||
*
|
||||
* \brief Print a trace message.
|
||||
*
|
||||
* \param pStr Storage for formatted string.
|
||||
* \param size Maximum number of characters to store.
|
||||
* \param pFmt Format string.
|
||||
* \param ap Arguments.
|
||||
*
|
||||
* \return Number of characters stored.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint32_t PrintVsn(char *pStr, uint32_t size, const char *pFmt, va_list ap)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
*pStr = 0;
|
||||
size--; /* Ensure we null-terminate within our buffer */
|
||||
|
||||
while ((*pFmt != '\0') && (len < size))
|
||||
{
|
||||
uint32_t width = 0;
|
||||
|
||||
/* normal */
|
||||
if (*pFmt != '%')
|
||||
{
|
||||
*pStr++ = *pFmt++;
|
||||
len++;
|
||||
continue;
|
||||
}
|
||||
|
||||
pFmt++;
|
||||
if (*pFmt == '%')
|
||||
{
|
||||
*pStr++ = '%';
|
||||
len++;
|
||||
pFmt++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* width */
|
||||
if (*pFmt == '0')
|
||||
{
|
||||
pFmt += printParseInt(pFmt, &width, 10u);
|
||||
}
|
||||
|
||||
/* long (ignore) */
|
||||
if (*pFmt == 'l')
|
||||
{
|
||||
pFmt++;
|
||||
}
|
||||
|
||||
switch(*pFmt)
|
||||
{
|
||||
/* character */
|
||||
case 'c':
|
||||
{
|
||||
char tmp = va_arg(ap, int);
|
||||
*pStr++ = tmp;
|
||||
len++;
|
||||
break;
|
||||
}
|
||||
/* unsigned decimal integer */
|
||||
case 'u':
|
||||
{
|
||||
unsigned int tmp = va_arg(ap, unsigned int);
|
||||
uint8_t lenTmp = printFmtInt(pStr, size - len, tmp, 10, 0, width);
|
||||
pStr += lenTmp;
|
||||
len += lenTmp;
|
||||
break;
|
||||
}
|
||||
/* signed decimal integer */
|
||||
case 'd':
|
||||
{
|
||||
int tmp = va_arg(ap, int);
|
||||
uint8_t lenTmp = printFmtInt(pStr, size - len, tmp, 10, 1, width);
|
||||
pStr += lenTmp;
|
||||
len += lenTmp;
|
||||
break;
|
||||
}
|
||||
/* pointer */
|
||||
case 'p':
|
||||
{
|
||||
unsigned long tmp = va_arg(ap, unsigned long);
|
||||
uint8_t lenTmp = printFmtInt(pStr, size - len, tmp, 16, 1, 8u);
|
||||
pStr += lenTmp;
|
||||
len += lenTmp;
|
||||
break;
|
||||
}
|
||||
/* unsigned hexadecimal integer */
|
||||
case 'x':
|
||||
case 'X':
|
||||
{
|
||||
unsigned int tmp = va_arg(ap, unsigned int);
|
||||
uint8_t lenTmp = printFmtInt(pStr, size - len, tmp, 16, 0, width);
|
||||
pStr += lenTmp;
|
||||
len += lenTmp;
|
||||
break;
|
||||
}
|
||||
/* string */
|
||||
case 's':
|
||||
{
|
||||
char *tmp = va_arg(ap, char *);
|
||||
while ((tmp != NULL) && (*tmp != '\0') && (len < size))
|
||||
{
|
||||
*pStr++ = *tmp++;
|
||||
len++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
pFmt++;
|
||||
}
|
||||
|
||||
/* Null-terminate output. */
|
||||
*pStr = 0;
|
||||
|
||||
if (len > size)
|
||||
{
|
||||
/* Compensate for -1 earlier. */
|
||||
return size + 2;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file print.h
|
||||
*
|
||||
* \brief Print functions.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2015-2017 ARM Ltd., all rights reserved.
|
||||
* ARM confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#ifndef PRINT_H
|
||||
#define PRINT_H
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "wsf_types.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! \brief Print function attributes. */
|
||||
#if defined(__GNUC__) || defined(__CC_ARM)
|
||||
#define PRINT_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
|
||||
#else
|
||||
#define PRINT_ATTRIBUTE(a, b)
|
||||
#endif
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn UtilVsnPrintf
|
||||
*
|
||||
* \brief Print a trace message.
|
||||
*
|
||||
* \param pStr Storage for formatted string.
|
||||
* \param size Maximum number of characters to store.
|
||||
* \param pFmt Format string.
|
||||
* \param ap Arguments.
|
||||
*
|
||||
* \return Number of characters stored.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
uint32_t PrintVsn(char *pStr, uint32_t size, const char *pFmt, va_list ap) PRINT_ATTRIBUTE(3, 0);
|
||||
|
||||
#endif /* PRINT_H */
|
||||
+532
@@ -0,0 +1,532 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file terminal.c
|
||||
*
|
||||
* \brief Terminal handler.
|
||||
*
|
||||
* $Date: 2016-03-29 14:55:12 -0700 (Tue, 29 Mar 2016) $
|
||||
* $Revision: 6524 $
|
||||
*
|
||||
* Copyright (c) 2015-2017 ARM Ltd., all rights reserved.
|
||||
* ARM confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "terminal.h"
|
||||
#include "print.h"
|
||||
|
||||
#include "wsf_types.h"
|
||||
#include "wsf_assert.h"
|
||||
#include "wsf_trace.h"
|
||||
#include "bstream.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
|
||||
#define TERMINAL_IS_SPACE(c) ((c == '\n') || (c == '\t') || (c == '\r') || (c == ' ') || (c == '\v') || (c == '\f'))
|
||||
#define TERMINAL_IS_PRINT(c) ((c >= 0x20) && (c != 0x7F))
|
||||
|
||||
/*! \brief Terminal events. */
|
||||
enum
|
||||
{
|
||||
TERMINAL_EVENT_COMMAND_RX = (1 << 0)
|
||||
};
|
||||
|
||||
/**************************************************************************************************
|
||||
Data Types
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! \brief Control block for terminal. */
|
||||
typedef struct
|
||||
{
|
||||
wsfHandlerId_t handlerId; /*!< Handler ID for TerminalHandler(). */
|
||||
terminalCommand_t *pFirstCommand; /*!< Pointer to first command. */
|
||||
char buf[TERMINAL_MAX_COMMAND_LEN + 1]; /*!< Command buffer. */
|
||||
bool_t isExecuting; /*!< TRUE if command in buffer is executing. */
|
||||
bool_t doEcho; /*!< TRUE if input should be echoed. */
|
||||
uint32_t bufOffset; /*!< Offset within buffer. */
|
||||
terminalUartTx_t terminalTx; /*!< Function to transmit via UART. */
|
||||
} terminalCtrlBlk_t;
|
||||
|
||||
/**************************************************************************************************
|
||||
Local Function Prototypes
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! \brief Help Command Handler */
|
||||
static uint8_t terminalCommandHelpHandler(uint32_t argc, char **argv);
|
||||
|
||||
/*! \brief Echo Command Handler */
|
||||
static uint8_t terminalCommandEchoHandler(uint32_t argc, char **argv);
|
||||
|
||||
/**************************************************************************************************
|
||||
Local Variables
|
||||
**************************************************************************************************/
|
||||
|
||||
/*! \brief Control block for terminal. */
|
||||
static terminalCtrlBlk_t terminalCb;
|
||||
|
||||
/*! \brief Help command. */
|
||||
static terminalCommand_t terminalCommandHelp = { NULL, "help", "help", terminalCommandHelpHandler };
|
||||
|
||||
/*! \brief Echo command. */
|
||||
static terminalCommand_t terminalCommandEcho = { NULL, "echo", "echo <on|off>", terminalCommandEchoHandler };
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalInit
|
||||
*
|
||||
* \brief Initialize terminal.
|
||||
*
|
||||
* \param handlerId Handler ID for TerminalHandler().
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalInit(wsfHandlerId_t handlerId)
|
||||
{
|
||||
APP_TRACE_INFO0("terminal: init");
|
||||
|
||||
terminalCb.handlerId = handlerId;
|
||||
terminalCb.pFirstCommand = NULL;
|
||||
terminalCb.isExecuting = FALSE;
|
||||
terminalCb.doEcho = TRUE;
|
||||
terminalCb.bufOffset = 0;
|
||||
|
||||
TerminalRegisterCommand(&terminalCommandHelp);
|
||||
TerminalRegisterCommand(&terminalCommandEcho);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalRegisterUartTxFunc
|
||||
*
|
||||
* \brief Register the UART Tx Function for the platform.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalRegisterUartTxFunc(terminalUartTx_t uartTxFunc)
|
||||
{
|
||||
terminalCb.terminalTx = uartTxFunc;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalRegisterCommand
|
||||
*
|
||||
* \brief Register command with terminal.
|
||||
*
|
||||
* \param pCommand Command.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalRegisterCommand(terminalCommand_t *pCommand)
|
||||
{
|
||||
terminalCommand_t *pCommandTemp = terminalCb.pFirstCommand;
|
||||
|
||||
if (pCommandTemp == NULL)
|
||||
{
|
||||
terminalCb.pFirstCommand = pCommand;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (pCommandTemp->pNext != NULL)
|
||||
{
|
||||
pCommandTemp = pCommandTemp->pNext;
|
||||
}
|
||||
|
||||
pCommandTemp->pNext = pCommand;
|
||||
}
|
||||
|
||||
pCommand->pNext = NULL;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn terminalExecute
|
||||
*
|
||||
* \brief Execute a received command.
|
||||
*
|
||||
* \param pBuf Command string.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static void terminalExecute(char *pBuf)
|
||||
{
|
||||
uint32_t argc = 0;
|
||||
char *argv[TERMINAL_MAX_ARGC + 1];
|
||||
uint32_t length;
|
||||
char *pBufCur;
|
||||
int state;
|
||||
|
||||
enum
|
||||
{
|
||||
STATE_OUTSIDE_OF_ARG,
|
||||
STATE_JUST_GOT_QUOTE,
|
||||
STATE_INSIDE_OF_ARG,
|
||||
STATE_INSIDE_OF_ARG_IN_QUOTES
|
||||
};
|
||||
|
||||
/* Parse arguments in command */
|
||||
state = STATE_OUTSIDE_OF_ARG;
|
||||
length = strlen(pBuf);
|
||||
for (pBufCur = pBuf; pBufCur < pBuf + length; pBufCur++)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case STATE_OUTSIDE_OF_ARG:
|
||||
{
|
||||
if (*pBufCur == '\"')
|
||||
{
|
||||
state = STATE_JUST_GOT_QUOTE;
|
||||
}
|
||||
else if (!TERMINAL_IS_SPACE(*pBufCur))
|
||||
{
|
||||
state = STATE_INSIDE_OF_ARG;
|
||||
if (argc < TERMINAL_MAX_ARGC)
|
||||
{
|
||||
argv[argc] = pBufCur;
|
||||
}
|
||||
argc++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STATE_JUST_GOT_QUOTE:
|
||||
{
|
||||
if (argc < TERMINAL_MAX_ARGC)
|
||||
{
|
||||
argv[state] = pBufCur;
|
||||
}
|
||||
argc++;
|
||||
if (*pBufCur == '\"')
|
||||
{
|
||||
state = STATE_OUTSIDE_OF_ARG;
|
||||
*pBufCur = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
state = STATE_INSIDE_OF_ARG_IN_QUOTES;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STATE_INSIDE_OF_ARG:
|
||||
{
|
||||
if (TERMINAL_IS_SPACE(*pBufCur))
|
||||
{
|
||||
state = STATE_OUTSIDE_OF_ARG;
|
||||
*pBufCur = '\0';
|
||||
}
|
||||
else if (*pBufCur == '\"')
|
||||
{
|
||||
state = STATE_JUST_GOT_QUOTE;
|
||||
*pBufCur = '\0';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STATE_INSIDE_OF_ARG_IN_QUOTES:
|
||||
{
|
||||
if (*pBufCur == '\"')
|
||||
{
|
||||
state = STATE_OUTSIDE_OF_ARG;
|
||||
*pBufCur = '\0';
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Find & invoke command. */
|
||||
if (argc > TERMINAL_MAX_ARGC)
|
||||
{
|
||||
TerminalTxStr(TERMINAL_STRING_ERROR "too many arguments" TERMINAL_STRING_NEW_LINE);
|
||||
}
|
||||
else if (argc > 0)
|
||||
{
|
||||
terminalCommand_t *pCommand = terminalCb.pFirstCommand;
|
||||
|
||||
while (pCommand != NULL)
|
||||
{
|
||||
if (strcmp(pCommand->pName, argv[0]) == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
pCommand = pCommand->pNext;
|
||||
}
|
||||
|
||||
if (pCommand == NULL)
|
||||
{
|
||||
TerminalTxStr(TERMINAL_STRING_ERROR "unrecognized command \"");
|
||||
TerminalTxStr(argv[0]);
|
||||
TerminalTxStr("\"" TERMINAL_STRING_NEW_LINE);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t r = pCommand->handler(argc, argv);
|
||||
switch (r)
|
||||
{
|
||||
case TERMINAL_ERROR_EXEC:
|
||||
case TERMINAL_ERROR_OK:
|
||||
break;
|
||||
case TERMINAL_ERROR_BAD_ARGUMENTS:
|
||||
TerminalTxStr(TERMINAL_STRING_ERROR "Invalid argument(s)" TERMINAL_STRING_NEW_LINE);
|
||||
break;
|
||||
case TERMINAL_ERROR_TOO_FEW_ARGUMENTS:
|
||||
TerminalTxStr(TERMINAL_STRING_ERROR "Too few arguments" TERMINAL_STRING_NEW_LINE);
|
||||
break;
|
||||
case TERMINAL_ERROR_TOO_MANY_ARGUMENTS:
|
||||
TerminalTxStr(TERMINAL_STRING_ERROR "Too many arguments" TERMINAL_STRING_NEW_LINE);
|
||||
break;
|
||||
default:
|
||||
TerminalTxStr(TERMINAL_STRING_ERROR "Unknown error" TERMINAL_STRING_NEW_LINE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalHandler
|
||||
*
|
||||
* \brief Handler for terminal messages.
|
||||
*
|
||||
* \param event WSF event mask.
|
||||
* \param pMsg WSF message.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg)
|
||||
{
|
||||
if ((event & TERMINAL_EVENT_COMMAND_RX) != 0)
|
||||
{
|
||||
terminalExecute(terminalCb.buf);
|
||||
TerminalTxStr(TERMINAL_STRING_PROMPT);
|
||||
terminalCb.bufOffset = 0;
|
||||
terminalCb.isExecuting = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalRx
|
||||
*
|
||||
* \brief Called by application when a data byte is received.
|
||||
*
|
||||
* \param dataByte Received byte.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalRx(uint8_t dataByte)
|
||||
{
|
||||
/* Hands off buf if command is executing. */
|
||||
if (!terminalCb.isExecuting)
|
||||
{
|
||||
/* If this is the end of a line, signal task. */
|
||||
if ((dataByte == '\n') || (dataByte == '\r'))
|
||||
{
|
||||
TerminalTxStr(TERMINAL_STRING_NEW_LINE);
|
||||
terminalCb.buf[terminalCb.bufOffset] = '\0';
|
||||
WsfSetEvent(terminalCb.handlerId, TERMINAL_EVENT_COMMAND_RX);
|
||||
terminalCb.isExecuting = TRUE;
|
||||
}
|
||||
|
||||
/* Check for delete. */
|
||||
else if (dataByte == 0x7F)
|
||||
{
|
||||
if (terminalCb.bufOffset > 0)
|
||||
{
|
||||
terminalCb.bufOffset--;
|
||||
if (terminalCb.doEcho)
|
||||
{
|
||||
TerminalTxStr("\b \b");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If we still have room in the buf, put it in buf. Othewise ignore it. */
|
||||
else if (terminalCb.bufOffset < TERMINAL_MAX_COMMAND_LEN)
|
||||
{
|
||||
/* Ignore non-printable characters. */
|
||||
if (TERMINAL_IS_PRINT(dataByte))
|
||||
{
|
||||
terminalCb.buf[terminalCb.bufOffset] = dataByte;
|
||||
terminalCb.bufOffset++;
|
||||
if (terminalCb.doEcho)
|
||||
{
|
||||
TerminalTxChar(dataByte);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTxStr
|
||||
*
|
||||
* \brief Called by application to transmit string.
|
||||
*
|
||||
* \param pStr String.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTxStr(const char *pStr)
|
||||
{
|
||||
TerminalTx((const uint8_t *)pStr, strlen(pStr));
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTxChar
|
||||
*
|
||||
* \brief Called by application to transmit character.
|
||||
*
|
||||
* \param c Character.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTxChar(char c)
|
||||
{
|
||||
TerminalTx((const uint8_t *)&c, 1);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTxPrint
|
||||
*
|
||||
* \brief Called by application to print formatted data.
|
||||
*
|
||||
* \param pStr Message format string
|
||||
* \param ... Additional arguments, printf-style
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTxPrint(const char *pStr, ...)
|
||||
{
|
||||
uint32_t len;
|
||||
char buf[TERMINAL_PRINTF_MAX_LEN];
|
||||
va_list args;
|
||||
|
||||
va_start(args, pStr);
|
||||
len = PrintVsn(buf, TERMINAL_PRINTF_MAX_LEN, pStr, args);
|
||||
va_end(args);
|
||||
|
||||
TerminalTx((uint8_t *)buf, len);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn terminalCommandHelpHandler
|
||||
*
|
||||
* \brief Handler for a terminal command.
|
||||
*
|
||||
* \param argc The number of arguments passed to the command.
|
||||
* \param argv The array of arguments; the 0th argument is the command.
|
||||
*
|
||||
* \return Error code.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static uint8_t terminalCommandHelpHandler(uint32_t argc, char **argv)
|
||||
{
|
||||
terminalCommand_t *pCommand = terminalCb.pFirstCommand;
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
return TERMINAL_ERROR_TOO_MANY_ARGUMENTS;
|
||||
}
|
||||
|
||||
while (pCommand != NULL)
|
||||
{
|
||||
TerminalTxStr(pCommand->pHelpStr);
|
||||
TerminalTxStr(TERMINAL_STRING_NEW_LINE);
|
||||
|
||||
pCommand = pCommand->pNext;
|
||||
}
|
||||
|
||||
TerminalTxStr(TERMINAL_STRING_NEW_LINE);
|
||||
return TERMINAL_ERROR_OK;
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTx
|
||||
*
|
||||
* \brief Transmit buffer on platform UART.
|
||||
*
|
||||
* \param pBuf Buffer to transmit.
|
||||
* \param len Length of buffer in octets.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTx(const uint8_t *pData, uint16_t len)
|
||||
{
|
||||
WSF_ASSERT(terminalCb.terminalTx);
|
||||
|
||||
(*terminalCb.terminalTx)(pData, len);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn terminalCommandEchoHandler
|
||||
*
|
||||
* \brief Handler for a terminal command.
|
||||
*
|
||||
* \param argc The number of arguments passed to the command.
|
||||
* \param argv The array of arguments; the 0th argument is the command.
|
||||
*
|
||||
* \return Error code.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static uint8_t terminalCommandEchoHandler(uint32_t argc, char **argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
return TERMINAL_ERROR_TOO_FEW_ARGUMENTS;
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
if (strcmp(argv[1], "on") == 0)
|
||||
{
|
||||
terminalCb.doEcho = TRUE;
|
||||
TerminalTxStr("echo on" TERMINAL_STRING_NEW_LINE);
|
||||
}
|
||||
else if (strcmp(argv[1], "off") == 0)
|
||||
{
|
||||
terminalCb.doEcho = FALSE;
|
||||
TerminalTxStr("echo off" TERMINAL_STRING_NEW_LINE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return TERMINAL_ERROR_BAD_ARGUMENTS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return TERMINAL_ERROR_TOO_MANY_ARGUMENTS;
|
||||
}
|
||||
|
||||
return TERMINAL_ERROR_OK;
|
||||
}
|
||||
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file terminal.h
|
||||
*
|
||||
* \brief Terminal handler.
|
||||
*
|
||||
* $Date: 2016-12-28 16:12:14 -0600 (Wed, 28 Dec 2016) $
|
||||
* $Revision: 10805 $
|
||||
*
|
||||
* Copyright (c) 2015-2017 ARM Ltd., all rights reserved.
|
||||
* ARM confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#ifndef TERMINAL_H
|
||||
#define TERMINAL_H
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "wsf_types.h"
|
||||
#include "wsf_os.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
|
||||
#define TERMINAL_MAX_ARGC 8u /*!< Maximum number of arguments to any command. */
|
||||
#define TERMINAL_MAX_COMMAND_LEN 100u /*!< Maximum length of command line. */
|
||||
#define TERMINAL_PRINTF_MAX_LEN 128u /*!< Maximum length of any printed output. */
|
||||
#define TERMINAL_STRING_PROMPT "> " /*!< Prompt string. */
|
||||
#define TERMINAL_STRING_ERROR "ERROR: " /*!< Error prefix. */
|
||||
#define TERMINAL_STRING_USAGE "USAGE: " /*!< Usage prefix. */
|
||||
#define TERMINAL_STRING_NEW_LINE "\r\n" /*!< New line string. */
|
||||
|
||||
/*! \brief Terminal command error codes. */
|
||||
enum
|
||||
{
|
||||
TERMINAL_ERROR_OK = 0, /*!< Command completed. */
|
||||
TERMINAL_ERROR_BAD_ARGUMENTS = 1, /*!< ERROR: Invalid argument(s) */
|
||||
TERMINAL_ERROR_TOO_FEW_ARGUMENTS = 2, /*!< ERROR: Too few arguments */
|
||||
TERMINAL_ERROR_TOO_MANY_ARGUMENTS = 3, /*!< ERROR: Too many arguments */
|
||||
TERMINAL_ERROR_EXEC = 4 /*!< Command completed with execution error. */
|
||||
};
|
||||
|
||||
/**************************************************************************************************
|
||||
Data Types
|
||||
**************************************************************************************************/
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \brief Handler for a terminal command.
|
||||
*
|
||||
* \param argc The number of arguments passed to the command.
|
||||
* \param argv The array of arguments; the 0th argument is the command.
|
||||
*
|
||||
* \return Error code.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
typedef uint8_t (*terminalHandler_t)(uint32_t argc, char **argv);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \brief Handler for transmit.
|
||||
*
|
||||
* \param pBuf Buffer to transmit.
|
||||
* \param len Number of bytes to transmit.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
typedef void (*terminalUartTx_t)(const uint8_t *pBuf, uint32_t len);
|
||||
|
||||
/*! \brief Terminal command. */
|
||||
typedef struct terminalCommand_tag
|
||||
{
|
||||
struct terminalCommand_tag *pNext; /*!< Pointer to next command in list. */
|
||||
const char *pName; /*!< Name of command. */
|
||||
const char *pHelpStr; /*!< Help String for command. */
|
||||
terminalHandler_t handler; /*!< Handler for command. */
|
||||
} terminalCommand_t;
|
||||
|
||||
/**************************************************************************************************
|
||||
Function Prototypes
|
||||
**************************************************************************************************/
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalInit
|
||||
*
|
||||
* \brief Initialize terminal.
|
||||
*
|
||||
* \param handlerId Handler ID for TerminalHandler().
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalInit(wsfHandlerId_t handlerId);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalRegisterUartTxFunc
|
||||
*
|
||||
* \brief Register the UART Tx Function for the platform.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalRegisterUartTxFunc(terminalUartTx_t uartTxFunc);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalRegisterCommand
|
||||
*
|
||||
* \brief Register command with terminal.
|
||||
*
|
||||
* \param pCommand Command.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalRegisterCommand(terminalCommand_t *pCommand);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalHandler
|
||||
*
|
||||
* \brief Handler for terminal messages.
|
||||
*
|
||||
* \param event WSF event mask.
|
||||
* \param pMsg WSF message.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalRx
|
||||
*
|
||||
* \brief Called by application when a data byte is received.
|
||||
*
|
||||
* \param dataByte received byte
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalRx(uint8_t dataByte);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTxStr
|
||||
*
|
||||
* \brief Called by application to transmit string.
|
||||
*
|
||||
* \param pStr String.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTxStr(const char *pStr);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTxChar
|
||||
*
|
||||
* \brief Called by application to transmit character.
|
||||
*
|
||||
* \param c Character.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTxChar(char c);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTxPrint
|
||||
*
|
||||
* \brief Called by application to print formatted data.
|
||||
*
|
||||
* \param pStr Message format string
|
||||
* \param ... Additional arguments, printf-style
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTxPrint(const char *pStr, ...);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn TerminalTx
|
||||
*
|
||||
* \brief Application function to transmit data..
|
||||
*
|
||||
* \param pData Data.
|
||||
* \param len Length of data, in bytes.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void TerminalTx(const uint8_t *pData, uint16_t len);
|
||||
|
||||
#endif /* TERMINAL_H */
|
||||
@@ -0,0 +1,98 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file wstr.c
|
||||
*
|
||||
* \brief String manipulation functions.
|
||||
*
|
||||
* $Date: 2017-02-14 15:31:43 -0600 (Tue, 14 Feb 2017) $
|
||||
* $Revision: 11184 $
|
||||
*
|
||||
* Copyright (c) 2014-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include "wsf_types.h"
|
||||
#include "wstr.h"
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn WstrnCpy
|
||||
*
|
||||
* \brief Copy a string and zero out space after the string length.
|
||||
*
|
||||
* \return none.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void WstrnCpy(char *pBuf, const char *pData, uint8_t n)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t zeroing = FALSE;
|
||||
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
if (!zeroing && pData[i] == '\0')
|
||||
zeroing = TRUE;
|
||||
|
||||
if (zeroing)
|
||||
*pBuf++ = 0;
|
||||
else
|
||||
*pBuf++ = pData[i];
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn WStrReverseCpy
|
||||
*
|
||||
* \brief Byte by byte reverse and copy a buffer.
|
||||
*
|
||||
* \param pBuf1 Buffer to hold reversed copy.
|
||||
* \param pBuf2 Buffer to copy.
|
||||
* \param len Size of pBuf1 and pBuf2 in bytes.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void WStrReverseCpy(uint8_t *pBuf1, const uint8_t *pBuf2, uint16_t len)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
for (i=0; i<len; i++)
|
||||
{
|
||||
pBuf1[len-1-i] = pBuf2[i];
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn WStrReverse
|
||||
*
|
||||
* \brief Byte by byte reverse a buffer.
|
||||
*
|
||||
* \param pBuf Buffer to reverse.
|
||||
* \param len size of pBuf in bytes.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void WStrReverse(uint8_t *pBuf, uint8_t len)
|
||||
{
|
||||
uint8_t i, temp;
|
||||
|
||||
for (i=0; i<len/2; i++)
|
||||
{
|
||||
temp = pBuf[len-i-1];
|
||||
pBuf[len-i-1] = pBuf[i];
|
||||
pBuf[i] = temp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \file wstr.h
|
||||
*
|
||||
* \brief String manipulation functions.
|
||||
*
|
||||
* $Date: 2017-02-14 15:31:43 -0600 (Tue, 14 Feb 2017) $
|
||||
* $Revision: 11184 $
|
||||
*
|
||||
* Copyright (c) 2014-2017 ARM Ltd., all rights reserved.
|
||||
* ARM Ltd. confidential and proprietary.
|
||||
*
|
||||
* IMPORTANT. Your use of this file is governed by a Software License Agreement
|
||||
* ("Agreement") that must be accepted in order to download or otherwise receive a
|
||||
* copy of this file. You may not use or copy this file for any purpose other than
|
||||
* as described in the Agreement. If you do not agree to all of the terms of the
|
||||
* Agreement do not use this file and delete all copies in your possession or control;
|
||||
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
|
||||
* to any use, copying or further distribution of this software.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#ifndef WSTR_H
|
||||
#define WSTR_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn WstrnCpy
|
||||
*
|
||||
* \brief Copies a string up to a given length.
|
||||
*
|
||||
* \param pBuf Pointer to buffer to copy to.
|
||||
* \param pData Pointer to the string to copy.
|
||||
* \param n Size of pBuf in bytes.
|
||||
*
|
||||
* \return none.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void WstrnCpy(char *pBuf, const char *pData, uint8_t n);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn WStrReverseCpy
|
||||
*
|
||||
* \brief Byte by byte reverse and copy a buffer.
|
||||
*
|
||||
* \param pBuf1 Buffer to hold reversed copy.
|
||||
* \param pBuf2 Buffer to copy.
|
||||
* \param len Size of pBuf1 and pBuf2 in bytes.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void WStrReverseCpy(uint8_t *pBuf1, const uint8_t *pBuf2, uint16_t len);
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn WStrReverse
|
||||
*
|
||||
* \brief Byte by byte reverse a buffer.
|
||||
*
|
||||
* \param pBuf Buffer to reverse.
|
||||
* \param len size of pBuf in bytes.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void WStrReverse(uint8_t *pBuf, uint8_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* WSTR_H */
|
||||
Reference in New Issue
Block a user