HXC Client Shield Repository.

Dependencies:   mbed

Committer:
kashish_mbed
Date:
Mon Apr 19 17:43:09 2021 +0000
Revision:
3:5e1a54378107
Parent:
0:bacc6e701fb4
Successful Build file with CmdProcess Functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kashish_mbed 0:bacc6e701fb4 1 /*
kashish_mbed 0:bacc6e701fb4 2 / _____) _ | |
kashish_mbed 0:bacc6e701fb4 3 ( (____ _____ ____ _| |_ _____ ____| |__
kashish_mbed 0:bacc6e701fb4 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
kashish_mbed 0:bacc6e701fb4 5 _____) ) ____| | | || |_| ____( (___| | | |
kashish_mbed 0:bacc6e701fb4 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
kashish_mbed 0:bacc6e701fb4 7 (C)2013 Semtech
kashish_mbed 0:bacc6e701fb4 8
kashish_mbed 0:bacc6e701fb4 9 _ _ _____ _______
kashish_mbed 0:bacc6e701fb4 10 | | | | |_ _| |__ __|
kashish_mbed 0:bacc6e701fb4 11 | |__| | __ ___ __ | | ___ | |
kashish_mbed 0:bacc6e701fb4 12 | __ |/ _` \ \/ / | | / _ \| |
kashish_mbed 0:bacc6e701fb4 13 | | | | (_| |> < _| || (_) | |
kashish_mbed 0:bacc6e701fb4 14 |_| |_|\__,_/_/\_\_____\___/|_|
kashish_mbed 0:bacc6e701fb4 15 (C)2017 HaxIoT
kashish_mbed 0:bacc6e701fb4 16
kashish_mbed 0:bacc6e701fb4 17 Description: Helper functions implementation
kashish_mbed 0:bacc6e701fb4 18
kashish_mbed 0:bacc6e701fb4 19 License: Revised BSD License, see LICENSE.TXT file include in the project
kashish_mbed 0:bacc6e701fb4 20
kashish_mbed 0:bacc6e701fb4 21 Maintainer: Miguel Luis and Gregory Cristian
kashish_mbed 0:bacc6e701fb4 22 Fahad Mirza (Haxiot)
kashish_mbed 0:bacc6e701fb4 23 */
kashish_mbed 0:bacc6e701fb4 24 #include <stdlib.h>
kashish_mbed 0:bacc6e701fb4 25 #include <stdio.h>
kashish_mbed 0:bacc6e701fb4 26 #include <stdint.h>
kashish_mbed 0:bacc6e701fb4 27 #include "tiny_sscanf.h"
kashish_mbed 0:bacc6e701fb4 28 #include "utilities.h"
kashish_mbed 0:bacc6e701fb4 29
kashish_mbed 0:bacc6e701fb4 30 /*!
kashish_mbed 0:bacc6e701fb4 31 * Redefinition of rand() and srand() standard C functions.
kashish_mbed 0:bacc6e701fb4 32 * These functions are redefined in order to get the same behavior across
kashish_mbed 0:bacc6e701fb4 33 * different compiler toolchains implementations.
kashish_mbed 0:bacc6e701fb4 34 */
kashish_mbed 0:bacc6e701fb4 35 // Standard random functions redefinition start
kashish_mbed 0:bacc6e701fb4 36 #define RAND_LOCAL_MAX 2147483647L
kashish_mbed 0:bacc6e701fb4 37
kashish_mbed 0:bacc6e701fb4 38 static uint32_t next = 1;
kashish_mbed 0:bacc6e701fb4 39
kashish_mbed 0:bacc6e701fb4 40 int32_t rand1( void )
kashish_mbed 0:bacc6e701fb4 41 {
kashish_mbed 0:bacc6e701fb4 42 return ( ( next = next * 1103515245L + 12345L ) % RAND_LOCAL_MAX );
kashish_mbed 0:bacc6e701fb4 43 }
kashish_mbed 0:bacc6e701fb4 44
kashish_mbed 0:bacc6e701fb4 45 void srand1( uint32_t seed )
kashish_mbed 0:bacc6e701fb4 46 {
kashish_mbed 0:bacc6e701fb4 47 next = seed;
kashish_mbed 0:bacc6e701fb4 48 }
kashish_mbed 0:bacc6e701fb4 49 // Standard random functions redefinition end
kashish_mbed 0:bacc6e701fb4 50
kashish_mbed 0:bacc6e701fb4 51 int32_t randr( int32_t min, int32_t max )
kashish_mbed 0:bacc6e701fb4 52 {
kashish_mbed 0:bacc6e701fb4 53 return ( int32_t )rand1( ) % ( max - min + 1 ) + min;
kashish_mbed 0:bacc6e701fb4 54 }
kashish_mbed 0:bacc6e701fb4 55
kashish_mbed 0:bacc6e701fb4 56 void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size )
kashish_mbed 0:bacc6e701fb4 57 {
kashish_mbed 0:bacc6e701fb4 58 while( size-- )
kashish_mbed 0:bacc6e701fb4 59 {
kashish_mbed 0:bacc6e701fb4 60 *dst++ = *src++;
kashish_mbed 0:bacc6e701fb4 61 }
kashish_mbed 0:bacc6e701fb4 62 }
kashish_mbed 0:bacc6e701fb4 63
kashish_mbed 0:bacc6e701fb4 64 void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size )
kashish_mbed 0:bacc6e701fb4 65 {
kashish_mbed 0:bacc6e701fb4 66 dst = dst + ( size - 1 );
kashish_mbed 0:bacc6e701fb4 67 while( size-- )
kashish_mbed 0:bacc6e701fb4 68 {
kashish_mbed 0:bacc6e701fb4 69 *dst-- = *src++;
kashish_mbed 0:bacc6e701fb4 70 }
kashish_mbed 0:bacc6e701fb4 71 }
kashish_mbed 0:bacc6e701fb4 72
kashish_mbed 0:bacc6e701fb4 73 void memset1( uint8_t *dst, uint8_t value, uint16_t size )
kashish_mbed 0:bacc6e701fb4 74 {
kashish_mbed 0:bacc6e701fb4 75 while( size-- )
kashish_mbed 0:bacc6e701fb4 76 {
kashish_mbed 0:bacc6e701fb4 77 *dst++ = value;
kashish_mbed 0:bacc6e701fb4 78 }
kashish_mbed 0:bacc6e701fb4 79 }
kashish_mbed 0:bacc6e701fb4 80
kashish_mbed 0:bacc6e701fb4 81 int8_t Nibble2HexChar( uint8_t a )
kashish_mbed 0:bacc6e701fb4 82 {
kashish_mbed 0:bacc6e701fb4 83 if( a < 10 )
kashish_mbed 0:bacc6e701fb4 84 {
kashish_mbed 0:bacc6e701fb4 85 return '0' + a;
kashish_mbed 0:bacc6e701fb4 86 }
kashish_mbed 0:bacc6e701fb4 87 else if( a < 16 )
kashish_mbed 0:bacc6e701fb4 88 {
kashish_mbed 0:bacc6e701fb4 89 return 'A' + ( a - 10 );
kashish_mbed 0:bacc6e701fb4 90 }
kashish_mbed 0:bacc6e701fb4 91 else
kashish_mbed 0:bacc6e701fb4 92 {
kashish_mbed 0:bacc6e701fb4 93 return '?';
kashish_mbed 0:bacc6e701fb4 94 }
kashish_mbed 0:bacc6e701fb4 95 }
kashish_mbed 0:bacc6e701fb4 96
kashish_mbed 0:bacc6e701fb4 97 uint8_t stringHexToByteArray(const char *hexString, uint8_t *buffer, uint8_t bufSize)
kashish_mbed 0:bacc6e701fb4 98 {
kashish_mbed 0:bacc6e701fb4 99 uint8_t count = 0;
kashish_mbed 0:bacc6e701fb4 100 char hex[3];
kashish_mbed 0:bacc6e701fb4 101 hex[2] = '\0';
kashish_mbed 0:bacc6e701fb4 102
kashish_mbed 0:bacc6e701fb4 103 while( (*hexString != '\0') && (*(hexString + 1) != '\0') )
kashish_mbed 0:bacc6e701fb4 104 {
kashish_mbed 0:bacc6e701fb4 105 // Separate two digits(i.e 1 HexByte) from the hex string
kashish_mbed 0:bacc6e701fb4 106 hex[0] = *hexString;
kashish_mbed 0:bacc6e701fb4 107 hex[1] = *(hexString + 1);
kashish_mbed 0:bacc6e701fb4 108
kashish_mbed 0:bacc6e701fb4 109 if((tiny_sscanf(hex, "%hhx",&buffer[count]) != 1))
kashish_mbed 0:bacc6e701fb4 110 {
kashish_mbed 0:bacc6e701fb4 111 // hex doesn't have valid numbers
kashish_mbed 0:bacc6e701fb4 112 break;
kashish_mbed 0:bacc6e701fb4 113 }
kashish_mbed 0:bacc6e701fb4 114 count++;
kashish_mbed 0:bacc6e701fb4 115 if (count == (bufSize - 1))
kashish_mbed 0:bacc6e701fb4 116 {
kashish_mbed 0:bacc6e701fb4 117 // We are out of space
kashish_mbed 0:bacc6e701fb4 118 break;
kashish_mbed 0:bacc6e701fb4 119 }
kashish_mbed 0:bacc6e701fb4 120 // Advance string hex pointer by two
kashish_mbed 0:bacc6e701fb4 121 hexString += 2;
kashish_mbed 0:bacc6e701fb4 122 }
kashish_mbed 0:bacc6e701fb4 123
kashish_mbed 0:bacc6e701fb4 124 buffer[count] = '\0';
kashish_mbed 0:bacc6e701fb4 125
kashish_mbed 0:bacc6e701fb4 126 return count;
kashish_mbed 0:bacc6e701fb4 127 }
kashish_mbed 0:bacc6e701fb4 128