Demo using MBED TLS

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Committer:
markrad
Date:
Thu Jan 05 00:20:03 2017 +0000
Revision:
58:f50b97b08851
Sample using MBED TLS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 58:f50b97b08851 1 // Copyright (c) Microsoft. All rights reserved.
markrad 58:f50b97b08851 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
markrad 58:f50b97b08851 3
markrad 58:f50b97b08851 4 #include "azure_c_shared_utility/xlogging.h"
markrad 58:f50b97b08851 5 #include "azure_c_shared_utility/consolelogger.h"
markrad 58:f50b97b08851 6
markrad 58:f50b97b08851 7 #ifndef NO_LOGGING
markrad 58:f50b97b08851 8
markrad 58:f50b97b08851 9
markrad 58:f50b97b08851 10 #ifdef WINCE
markrad 58:f50b97b08851 11 #include <stdarg.h>
markrad 58:f50b97b08851 12
markrad 58:f50b97b08851 13 void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...)
markrad 58:f50b97b08851 14 {
markrad 58:f50b97b08851 15 va_list args;
markrad 58:f50b97b08851 16 va_start(args, format);
markrad 58:f50b97b08851 17
markrad 58:f50b97b08851 18 time_t t = time(NULL);
markrad 58:f50b97b08851 19
markrad 58:f50b97b08851 20 switch (log_category)
markrad 58:f50b97b08851 21 {
markrad 58:f50b97b08851 22 case LOG_INFO:
markrad 58:f50b97b08851 23 (void)printf("Info: ");
markrad 58:f50b97b08851 24 break;
markrad 58:f50b97b08851 25 case LOG_ERROR:
markrad 58:f50b97b08851 26 (void)printf("Error: Time:%.24s File:%s Func:%s Line:%d ", ctime(&t), file, func, line);
markrad 58:f50b97b08851 27 break;
markrad 58:f50b97b08851 28 default:
markrad 58:f50b97b08851 29 break;
markrad 58:f50b97b08851 30 }
markrad 58:f50b97b08851 31
markrad 58:f50b97b08851 32 (void)vprintf(format, args);
markrad 58:f50b97b08851 33 va_end(args);
markrad 58:f50b97b08851 34
markrad 58:f50b97b08851 35 (void)log_category;
markrad 58:f50b97b08851 36 if (options & LOG_LINE)
markrad 58:f50b97b08851 37 {
markrad 58:f50b97b08851 38 (void)printf("\r\n");
markrad 58:f50b97b08851 39 }
markrad 58:f50b97b08851 40 }
markrad 58:f50b97b08851 41 #endif
markrad 58:f50b97b08851 42
markrad 58:f50b97b08851 43 LOGGER_LOG global_log_function = consolelogger_log;
markrad 58:f50b97b08851 44
markrad 58:f50b97b08851 45
markrad 58:f50b97b08851 46 void xlogging_set_log_function(LOGGER_LOG log_function)
markrad 58:f50b97b08851 47 {
markrad 58:f50b97b08851 48 global_log_function = log_function;
markrad 58:f50b97b08851 49 }
markrad 58:f50b97b08851 50
markrad 58:f50b97b08851 51 LOGGER_LOG xlogging_get_log_function(void)
markrad 58:f50b97b08851 52 {
markrad 58:f50b97b08851 53 return global_log_function;
markrad 58:f50b97b08851 54 }
markrad 58:f50b97b08851 55
markrad 58:f50b97b08851 56 /* Print up to 16 bytes per line. */
markrad 58:f50b97b08851 57 #define LINE_SIZE 16
markrad 58:f50b97b08851 58
markrad 58:f50b97b08851 59 /* Return the printable char for the provided value. */
markrad 58:f50b97b08851 60 #define PRINTABLE(c) ((c >= ' ') && (c <= '~')) ? (char)c : '.'
markrad 58:f50b97b08851 61
markrad 58:f50b97b08851 62 /* Convert the lower nibble of the provided byte to a hexadecimal printable char. */
markrad 58:f50b97b08851 63 #define HEX_STR(c) (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A')
markrad 58:f50b97b08851 64
markrad 58:f50b97b08851 65 void xlogging_dump_buffer(const void* buf, size_t size)
markrad 58:f50b97b08851 66 {
markrad 58:f50b97b08851 67 char charBuf[LINE_SIZE + 1];
markrad 58:f50b97b08851 68 char hexBuf[LINE_SIZE * 3 + 1];
markrad 58:f50b97b08851 69 char countbuf = 0;
markrad 58:f50b97b08851 70 const unsigned char* bufAsChar = (const unsigned char*)buf;
markrad 58:f50b97b08851 71 const unsigned char* startPos = bufAsChar;
markrad 58:f50b97b08851 72
markrad 58:f50b97b08851 73 /* Print the whole buffer. */
markrad 58:f50b97b08851 74 for (size_t i = 0; i < size; i++)
markrad 58:f50b97b08851 75 {
markrad 58:f50b97b08851 76 /* Store the printable value of the char in the charBuf to print. */
markrad 58:f50b97b08851 77 charBuf[countbuf] = PRINTABLE(*bufAsChar);
markrad 58:f50b97b08851 78
markrad 58:f50b97b08851 79 /* Convert the high nibble to a printable hexadecimal value. */
markrad 58:f50b97b08851 80 hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4);
markrad 58:f50b97b08851 81
markrad 58:f50b97b08851 82 /* Convert the low nibble to a printable hexadecimal value. */
markrad 58:f50b97b08851 83 hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar);
markrad 58:f50b97b08851 84
markrad 58:f50b97b08851 85 hexBuf[countbuf * 3 + 2] = ' ';
markrad 58:f50b97b08851 86
markrad 58:f50b97b08851 87 countbuf++;
markrad 58:f50b97b08851 88 bufAsChar++;
markrad 58:f50b97b08851 89 /* If the line is full, print it to start another one. */
markrad 58:f50b97b08851 90 if (countbuf == LINE_SIZE)
markrad 58:f50b97b08851 91 {
markrad 58:f50b97b08851 92 charBuf[countbuf] = '\0';
markrad 58:f50b97b08851 93 hexBuf[countbuf * 3] = '\0';
markrad 58:f50b97b08851 94 LOG(LOG_TRACE, 0, "%p: %s %s", startPos, hexBuf, charBuf);
markrad 58:f50b97b08851 95 countbuf = 0;
markrad 58:f50b97b08851 96 startPos = bufAsChar;
markrad 58:f50b97b08851 97 }
markrad 58:f50b97b08851 98 }
markrad 58:f50b97b08851 99
markrad 58:f50b97b08851 100 /* If the last line does not fit the line size. */
markrad 58:f50b97b08851 101 if (countbuf > 0)
markrad 58:f50b97b08851 102 {
markrad 58:f50b97b08851 103 /* Close the charBuf string. */
markrad 58:f50b97b08851 104 charBuf[countbuf] = '\0';
markrad 58:f50b97b08851 105
markrad 58:f50b97b08851 106 /* Fill the hexBuf with spaces to keep the charBuf alignment. */
markrad 58:f50b97b08851 107 while ((countbuf++) < LINE_SIZE - 1)
markrad 58:f50b97b08851 108 {
markrad 58:f50b97b08851 109 hexBuf[countbuf * 3] = ' ';
markrad 58:f50b97b08851 110 hexBuf[countbuf * 3 + 1] = ' ';
markrad 58:f50b97b08851 111 hexBuf[countbuf * 3 + 2] = ' ';
markrad 58:f50b97b08851 112 }
markrad 58:f50b97b08851 113 hexBuf[countbuf * 3] = '\0';
markrad 58:f50b97b08851 114
markrad 58:f50b97b08851 115 /* Print the last line. */
markrad 58:f50b97b08851 116 LOG(LOG_TRACE, 0, "%p: %s %s", startPos, hexBuf, charBuf);
markrad 58:f50b97b08851 117 }
markrad 58:f50b97b08851 118 }
markrad 58:f50b97b08851 119
markrad 58:f50b97b08851 120 #endif