Update print call subtype for logging
[wave300.git] / wireless / driver / linux / utils.c
blob259f8dda43fc8c67ec682ffb9fbae064c454ae89
1 /******************************************************************************
3 Copyright (c) 2012
4 Lantiq Deutschland GmbH
6 For licensing information, see the file 'LICENSE' in the root folder of
7 this software module.
9 ******************************************************************************/
11 * $Id$
15 * Utilities.
17 * Originally written by Andrey Fidrya
20 #include "mtlkinc.h"
22 #include "utils.h"
24 #define LOG_LOCAL_GID GID_UTILS
25 #define LOG_LOCAL_FID 1
28 * Function outputs buffer in hex format
30 #ifdef MTCFG_SILENT
31 void __MTLK_IFUNC
32 mtlk_aux_print_hex (const void *buf, unsigned int l)
35 #else
37 #ifdef MTCFG_SILENT
38 #define LOG_BUFFER
39 #else
40 #define LOG_BUFFER pr_cont
41 #endif
43 void __MTLK_IFUNC
44 mtlk_aux_print_hex (const void *buf, unsigned int l)
46 unsigned int i,j;
47 unsigned char *cp = (unsigned char*)buf;
49 //TODO should be probably locked
51 LOG_BUFFER("cp= 0x%p l=%d\n", cp, l);
52 for (i = 0; i < l/16; i++) {
53 LOG_BUFFER("%04x: ", 16*i);
54 for (j = 0; j < 16; j++)
55 LOG_BUFFER("%02x %s", *cp++, j== 7 ? " " : "");
56 LOG_BUFFER("\n");
58 if (l & 0x0f) {
59 LOG_BUFFER("%04x: ", 16*i);
60 for (j = 0; j < (l&0x0f); j++)
61 LOG_BUFFER("%02x %s", *cp++, j== 7 ? " " : "");
62 LOG_BUFFER("\n");
65 #endif
67 void __mtlk_dump(const void *buf, uint32 len, char *str)
69 ILOG0_S("%s",str);
70 mtlk_aux_print_hex(buf, len);
73 uint32
74 mtlk_shexdump (char *buffer, uint8 *data, size_t size)
76 uint8 line, i;
77 uint32 counter = 0;
79 for (line = 0; size; line++) {
80 counter += sprintf(buffer+counter, "%04x: ", line * 0x10);
81 for (i = 0x10; i && size; size--,i--,data++) {
82 counter +=sprintf(buffer+counter, " %02x", *data);
84 counter += sprintf(buffer+counter, "\n");
86 return counter;
89 char * __MTLK_IFUNC
90 mtlk_get_token (char *str, char *buf, size_t len, char delim)
92 char *dlm;
94 if (!str) {
95 buf[0] = 0;
96 return NULL;
98 dlm = strchr(str, delim);
99 if (dlm && ((size_t)(dlm - str) < len)) {
100 memcpy(buf, str, dlm - str);
101 buf[dlm - str] = 0;
102 } else {
103 memcpy(buf, str, len - 1);
104 buf[len - 1] = 0;
106 ILOG4_S("Get token: '%s'", buf);
107 if (dlm)
108 return dlm + 1;
109 return dlm;
113 Extract MAC address from string
115 \param str - string with MAC address [I]
116 \param addr - pointer to MAC storage [O]
118 \return
119 MTLK_ERR_PARAMS - wrong format of MAC address in the string
120 MTLK_ERR_OK - success
122 \remark
123 accepted the following string formats
124 XX:XX:XX:XX:XX:XX
126 int __MTLK_IFUNC
127 mtlk_str_to_mac (char const *str, uint8 *addr)
129 int i;
131 MTLK_ASSERT(NULL != str);
132 MTLK_ASSERT(NULL != addr);
134 str = mtlk_str_ltrim(str);
136 if (strlen(str) < 17)
137 return MTLK_ERR_PARAMS;
139 if ((':' != str[2]) ||
140 (':' != str[5]) ||
141 (':' != str[8]) ||
142 (':' != str[11]) ||
143 (':' != str[14]))
144 return MTLK_ERR_PARAMS;
146 memset (addr, 0, sizeof(*addr));
148 for (i = 0; i < 6; i++)
150 addr[i] = mtlk_str_x2tol(str + (i * 3));
153 return MTLK_ERR_OK;