Chameleon-Mini
DESFireLogging.h
1 /*
2 The DESFire stack portion of this firmware source
3 is free software written by Maxie Dion Schmidt (@maxieds):
4 You can redistribute it and/or modify
5 it under the terms of this license.
6 
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 
11 The complete source distribution of
12 this firmware is available at the following link:
13 https://github.com/maxieds/ChameleonMiniFirmwareDESFireStack.
14 
15 Based in part on the original DESFire code created by
16 @dev-zzo (GitHub handle) [Dmitry Janushkevich] available at
17 https://github.com/dev-zzo/ChameleonMini/tree/desfire.
18 
19 This notice must be retained at the top of all source files where indicated.
20 */
21 
22 /*
23  * DESFireLogging.h :
24  * Maxie D. Schmidt (github.com/maxieds)
25  */
26 
27 #ifndef __DESFIRE_LOGGING_UTILS_H__
28 #define __DESFIRE_LOGGING_UTILS_H__
29 
30 #include "../../Log.h"
31 #include "../../Common.h"
32 #include "DESFireFirmwareSettings.h"
33 #include "DESFireMemoryOperations.h"
34 
35 #ifndef DESFIRE_MIN_INCOMING_LOGSIZE
36 #define DESFIRE_MIN_INCOMING_LOGSIZE (1)
37 #endif
38 #ifndef DESFIRE_MIN_OUTGOING_LOGSIZE
39 #define DESFIRE_MIN_OUTGOING_LOGSIZE (1)
40 #endif
41 
42 INLINE void DesfireLogEntry(LogEntryEnum LogCode, void *LogDataBuffer, uint16_t BufSize) {
43  if (DESFIRE_MIN_OUTGOING_LOGSIZE <= BufSize) {
44  LogEntry(LogCode, (void *) LogDataBuffer, BufSize);
45  }
46 }
47 
48 typedef enum DESFIRE_FIRMWARE_ENUM_PACKING {
49  OFF = 0,
50  NORMAL = 1,
51  VERBOSE = 2,
52  DEBUGGING = 3,
53 } DESFireLoggingMode;
54 
55 extern DESFireLoggingMode LocalLoggingMode;
56 
57 /*
58  * This variable can be toggled to indicated whether to employ
59  * testable pseudo-randomness in the encrypted transfers.
60  * When this value id non-zero, then any random session numbers
61  * (e.g., RndB) and IV salt vectors should default back to
62  * predictable constant values for testing purposes.
63  */
64 extern BYTE LocalTestingMode;
65 
66 void DESFireLogErrorMessage(char *fmtMsg, ...);
67 void DESFireLogStatus(BYTE *bufMsg, SIZET bufSize);
68 void DESFireLogDebuggingMessage(char *fmtMsg, ...);
69 void DESFireLogSourceCodeTODO(char *implNoteMsg, char *srcFileLoggingData);
70 void DESFireLogIncomingData(BYTE *byteBuf, SIZET bufLength);
71 void DESFireLogOutgoingData(BYTE *byteBuf, SIZET bufLength);
72 void DESFireLogNativeCommand(BYTE *Buffer, SIZET ByteCount);
73 void DESFireLogISO1443Command(BYTE *Buffer, SIZET ByteCount);
74 void DESFireLogISO7816Command(BYTE *Buffer, SIZET ByteCount);
75 void DESFireLogSetProtectedData(BYTE *pdataBuf, SIZET byteBufSize);
76 void DESFireLogPICCHardReset(BYTE *strBuf, SIZET strLength);
77 void DESFireLogPICCSoftReset(BYTE *strBuf, SIZET strLength);
78 
79 void DebugPrintP(const char *fmt, ...);
80 #define DEBUG_PRINT(fmt, ...) DebugPrintP(PSTR(fmt), ##__VA_ARGS__)
81 
82 #define DEBUG_PRINT_P(fmtStr, ...) ({ \
83  uint8_t logLength = 0; \
84  do { \
85  snprintf_P((char *) __InternalStringBuffer, STRING_BUFFER_SIZE, \
86  fmtStr, ##__VA_ARGS__); \
87  logLength = StringLength((char *) __InternalStringBuffer, \
88  STRING_BUFFER_SIZE); \
89  DesfireLogEntry(LOG_ERR_DESFIRE_GENERIC_ERROR, \
90  (char *) __InternalStringBuffer, logLength); \
91  } while(0); \
92  })
93 
94 #define GetSourceFileLoggingData() ({ \
95  char *strBuffer; \
96  do { \
97  snprintf_P(__InternalStringBuffer, STRING_BUFFER_SIZE, \
98  PSTR("@@ LINE #%d in *%s @@"), \
99  __LINE__, __FILE__); \
100  __InternalStringBuffer[STRING_BUFFER_SIZE - 1] = '\0'; \
101  } while(0); \
102  strBuffer = __InternalStringBuffer; \
103  strBuffer; \
104  })
105 
106 #define GetSymbolNameString(symbolName) ({ \
107  char *strBuffer; \
108  do { \
109  strncpy_P(__InternalStringBuffer2, PSTR(#symbolName), \
110  DATA_BUFFER_SIZE_SMALL); \
111  } while(0); \
112  strBuffer = __InternalStringBuffer2; \
113  strBuffer; \
114  })
115 
116 #define GetHexBytesString(byteArray, arrSize) ({ \
117  char *strBuffer; \
118  do { \
119  BufferToHexString(__InternalStringBuffer, \
120  STRING_BUFFER_SIZE, \
121  byteArray, arrSize); \
122  __InternalStringBuffer[STRING_BUFFER_SIZE - 1] = '\0'; \
123  } while(0); \
124  strBuffer = __InternalStringBuffer; \
125  strBuffer; \
126  })
127 
128 #if defined(DESFIRE_DEFAULT_LOGGING_MODE) && DESFIRE_DEFAULT_LOGGING_MODE != 0
129 #define LogDebuggingMsg(msg) ({ \
130  do { \
131  strncpy_P((char *) __InternalStringBuffer, msg, STRING_BUFFER_SIZE); \
132  uint8_t sbufLength = StringLength((char *) __InternalStringBuffer, STRING_BUFFER_SIZE); \
133  LogEntry(LOG_INFO_DESFIRE_OUTGOING_DATA, (void *) __InternalStringBuffer, \
134  sbufLength); \
135  } while(0); \
136  })
137 #else
138 #define LogDebuggingMsg(msg) ({})
139 #endif
140 
141 /*#define DesfireLogIncoming(incBuf, bitCount) ({ \
142  uint8_t logLength = 0; \
143  do { \
144  logLength = BufferToHexString(__InternalStringBuffer, STRING_BUFFER_SIZE, \
145  incBuf, (bitCount + 7) / 8); \
146  snprintf_P(__InternalStringBuffer + logLength, PSTR(" [#=%d] <-- IN"), \
147  bitCount); \
148  logLength = StringLength(__InternalStringBuffer, STRING_BUFFER_SIZE); \
149  DesfireLogEntry(LOG_INFO_DESFIRE_INCOMING_DATA, __InternalStringBuffer, logLength); \
150  } while(0); \
151  })
152 
153 #define DesfireLogOutgoing(incBuf, bitCount) ({ \
154  uint8_t logLength = 0; \
155  do { \
156  logLength = BufferToHexString(__InternalStringBuffer, STRING_BUFFER_SIZE, \
157  incBuf, (bitCount + 7) / 8); \
158  snprintf_P(__InternalStringBuffer + logLength, PSTR(" [#=%d] --> OUT"), \
159  bitCount); \
160  logLength = StringLength(__InternalStringBuffer, STRING_BUFFER_SIZE); \
161  DesfireLogEntry(LOG_INFO_DESFIRE_OUTGOING_DATA, __InternalStringBuffer, logLength); \
162  } while(0); \
163  })
164 */
165 
166 #define DesfireLogISOStateChange(state, logCode) ({})
167 /*#define DesfireLogISOStateChange(state, logCode) ({ \
168  const char *stateSymbName = NULL; \
169  uint8_t logLength = 0x00; \
170  do { \
171  switch(state) { \
172  case ISO14443_3A_STATE_IDLE: \
173  stateSymbName = PSTR("ISO14443_3A_STATE_IDLE"); \
174  break; \
175  case ISO14443_3A_STATE_READY1: \
176  stateSymbName = PSTR("ISO14443_3A_STATE_READY1"); \
177  break; \
178  case ISO14443_3A_STATE_READY2: \
179  stateSymbName = PSTR("ISO14443_3A_STATE_READY2"); \
180  break; \
181  case ISO14443_3A_STATE_ACTIVE: \
182  stateSymbName = PSTR("ISO14443_3A_STATE_ACTIVE"); \
183  break; \
184  case ISO14443_3A_STATE_HALT: \
185  stateSymbName = PSTR("ISO14443_3A_STATE_HALT"); \
186  break; \
187  case ISO14443_4_STATE_EXPECT_RATS: \
188  stateSymbName = PSTR("ISO14443_4_STATE_EXPECT_RATS"); \
189  break; \
190  case ISO14443_4_STATE_ACTIVE: \
191  stateSymbName = PSTR("ISO14443_4_STATE_ACTIVE"); \
192  break; \
193  default: \
194  stateSymbName = PSTR("UNKNOWN_STATE"); \
195  break; \
196  } \
197  snprintf_P(__InternalStringBuffer, STRING_BUFFER_SIZE, PSTR(" => ")); \
198  strcat_P(__InternalStringBuffer, stateSymbName); \
199  logLength = StringLength(__InternalStringBuffer, STRING_BUFFER_SIZE); \
200  DesfireLogEntry(logCode, __InternalStringBuffer, logLength); \
201  } while(0); \
202  })
203 */
204 
205 #endif
LogEntryEnum
Definition: Log.h:16