]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/tidep0084.git/blob - components/common/src/hexline.c
Updated TI Linux Sensor To Cloud to the latest TI 15.4-Stack v2.4, now with CC13x2...
[apps/tidep0084.git] / components / common / src / hexline.c
1 /******************************************************************************
2  @file hexline.c
4  @brief TIMAC 2.0 API Implimentation for hexline debug dump
6  Group: WCS LPC
7  $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350, CC1352$
9  ******************************************************************************
10  $License: BSD3 2016 $
11   
12    Copyright (c) 2015, Texas Instruments Incorporated
13    All rights reserved.
14   
15    Redistribution and use in source and binary forms, with or without
16    modification, are permitted provided that the following conditions
17    are met:
18   
19    *  Redistributions of source code must retain the above copyright
20       notice, this list of conditions and the following disclaimer.
21   
22    *  Redistributions in binary form must reproduce the above copyright
23       notice, this list of conditions and the following disclaimer in the
24       documentation and/or other materials provided with the distribution.
25   
26    *  Neither the name of Texas Instruments Incorporated nor the names of
27       its contributors may be used to endorse or promote products derived
28       from this software without specific prior written permission.
29   
30    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33    PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37    OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  ******************************************************************************
42  $Release Name: TI-15.4Stack Linux x64 SDK$
43  $Release Date: Sept 27, 2017 (2.04.00.13)$
44  *****************************************************************************/
46 #include "compiler.h"
47 #include "hexline.h"
49 #include <stdio.h>
50 #include <string.h>
52 /*
53  * Initialize a hexdump structure for use with HEXLINE_format()
54  *
55  * Public function defined in hexline.h
56  */
57 void HEXLINE_init(struct hexline *pH,
58            uint64_t addr,
59            const void *pBytes,
60            size_t nbytes)
61 {
62     memset((void *)(pH), 0,sizeof(*pH));
64     pH->addr   = addr;
65     pH->pBytes = pBytes;
66     pH->nbytes = nbytes;
67 }
69 /*!
70  * @brief [private] hexline helper function to hexify a nibble
71  *
72  * @param integer to hexify the low 4 bits
73  *
74  * @return low 4 bits converted to ascii hex (lower case)
75  */
76 static int _hexify(int v)
77 {
78     v = (v & 0x0f) + '0';
79     if(v > '9')
80     {
81         v = v - '0' - 10 + 'a';
82     }
83     return (v);
84 }
86 /*!
87  * @brief [private] write n hex digits at buffer
88  *
89  * @param buf - where to put the text
90  * @param n   - how many digits
91  * @param v   - value to convert
92  */
93 static void _hexdigits(char *buf, int n, uint64_t v)
94 {
95     /* we write the data backwards
96      * aka: Horners method... think of it sa a polynomial
97      * we extract one constant at a time... by dividing it out */
98     buf = buf + n - 1;
100     while(n)
101     {
102         *buf = (char)_hexify((int)(v));
103         n--;
104         buf--;
105         v = v >> 4;
106     }
109 /*
110  * Format a line of text as a memory dump for later printing.
111  *
112  * Public function defined in hexline.h
113  */
114 void HEXLINE_format(struct hexline *pH)
116     uint64_t a;
117     int wr;
118     int x;
119     int v;
121     /* clear the line with spaces */
122     /* this simplifies things when we print partial lines. */
123     memset((void *)(&pH->buf[0]), ' ', sizeof(pH->buf)-1);
124     /* Garentee TERMINATION! */
125     pH->buf[ sizeof(pH->buf)-1 ] = 0;
127     /* Create a line that looks like this:
128      *  01234567890123456789012345678901234567890123456789012345678901234567890123456789
129      *  xxxxxxxx: xx xx xx xx xx xx xx xx-xx xx xx xx xx xx xx xx |0123456789abcdef|
130      *
131      * The address field might be +8 longer in case of 64bit addresses
132      */
134     /* print address */
135     /* special case if address is a 64bit number */
136     a = pH->addr;
137     a = a + pH->ndone;
138     a = a & (~((uint64_t)(0x0f)));
139     if(a > 0x100000000)
140     {
141         /* print big wide address in giant form */
142         wr = 16;
143     }
144     else
145     {
146         /* otherwise don't print lots of zeros */
147         wr = 8;
148     }
150     /* print address field */
151     _hexdigits(pH->buf, wr, a);
152     /* these offsets come from the string above */
154     /* decorate the ascii text printout */
155     pH->buf[wr+ 8-8] = ':';
156     pH->buf[wr+33-8] = '-';
157     pH->buf[wr+58-8] = '|';
158     pH->buf[wr+75-8] = '|';
159     pH->buf[wr+76-8] = 0;
161     /* choose start loc on the line. */
162     x = (pH->addr + pH->ndone) & 0x0f;
164     /* loop through parts on the line */
165     for(/* above */ ; x < 16 ; x++)
166     {
167         /* get byte for hex data */
168         v = ((const uint8_t *)(pH->pBytes))[pH->ndone];
169         /* 8 for hex digits */
170         /* 1 for colon */
171         /* 1 for space */
172         /* (x*3) for each byte value */
173         _hexdigits(pH->buf + wr + 1 + 1 + (x * 3), 2, v);
175         /* now the ascii portion */
176         if((v < 0x20) || (v > 0x7e))
177         {
178             v = '.';
179         }
180         pH->buf[ wr + 59 - 8 + x ] = (char)v;
182         /* Done? */
183         pH->ndone += 1;
184         if(pH->ndone >= pH->nbytes)
185         {
186             break;
187         }
188     }
191 /*
192  *  ========================================
193  *  Texas Instruments Micro Controller Style
194  *  ========================================
195  *  Local Variables:
196  *  mode: c
197  *  c-file-style: "bsd"
198  *  tab-width: 4
199  *  c-basic-offset: 4
200  *  indent-tabs-mode: nil
201  *  End:
202  *  vim:set  filetype=c tabstop=4 shiftwidth=4 expandtab=true
203  */