1 /* MSPDebug - debugging tool for MSP430 MCUs
2  * Copyright (C) 2009, 2010 Daniel Beer
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #ifndef UTIL_H_
20 #define UTIL_H_
21 
22 #include <stdint.h>
23 #include <ctype.h>
24 
25 #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
26 
27 #define LE_BYTE(b, x) ((int)((uint8_t *)(b))[x])
28 #define LE_WORD(b, x) ((LE_BYTE(b, x + 1) << 8) | LE_BYTE(b, x))
29 #define LE_LONG(b, x) ((LE_WORD(b, x + 2) << 16) | LE_WORD(b, x))
30 
31 /* This type fits an MSP430X register value */
32 typedef uint32_t address_t;
33 
34 #define ADDRESS_NONE ((address_t)0xffffffff)
35 
36 /* Retrive a string describing the last system error */
37 const char *last_error(void);
38 
39 /* Retrieve the next word from a pointer to the rest of a command
40  * argument buffer. Returns NULL if no more words.
41  */
42 char *get_arg(char **text);
43 
44 /* Display hex output for debug purposes */
45 void debug_hexdump(const char *label,
46 		   const uint8_t *data, int len);
47 
ishex(int c)48 static inline int ishex(int c)
49 {
50 	return isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
51 }
52 
53 int hexval(int c);
54 
55 #ifdef __Windows__
56 char *strsep(char **strp, const char *delim);
57 #endif
58 
59 /* Expand `~' in path names. Caller must free the returned ptr */
60 char *expand_tilde(const char *path);
61 
62 /* Sleep for a number of seconds (_s) or milliseconds (_ms) */
63 int delay_s(unsigned int s);
64 int delay_ms(unsigned int s);
65 
66 /* Base64 encode a block without breaking into lines. Returns the number
67  * of source bytes encoded. The output is nul-terminated.
68  */
base64_encoded_size(int decoded_size)69 static inline int base64_encoded_size(int decoded_size)
70 {
71 	return ((decoded_size + 2) / 3) * 4;
72 }
73 
74 int base64_encode(const uint8_t *src, int len, char *dst, int max_len);
75 
76 /* printf format for long long args */
77 #ifdef __MINGW32__
78 #define LLFMT "I64d"
79 #else
80 #define LLFMT "lld"
81 #endif
82 
83 #endif
84