context.c (7179B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2012 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifdef HAVE_CONFIG_H 23 #include "config.h" 24 #endif 25 26 #include <stdlib.h> 27 #include <stdio.h> 28 #include <string.h> 29 #include <limits.h> 30 31 #ifdef _WIN32 32 #define WIN32_LEAN_AND_MEAN 33 #define NOGDI 34 #include <windows.h> 35 #endif 36 37 #include "context-private.h" 38 #include "platform.h" 39 #include "timer.h" 40 41 struct dc_context_t { 42 dc_loglevel_t loglevel; 43 dc_logfunc_t logfunc; 44 void *userdata; 45 #ifdef ENABLE_LOGGING 46 char msg[16384 + 32]; 47 dc_timer_t *timer; 48 #endif 49 }; 50 51 #ifdef ENABLE_LOGGING 52 static int 53 l_hexdump (char *str, size_t size, const unsigned char data[], size_t n) 54 { 55 const unsigned char ascii[] = { 56 '0', '1', '2', '3', '4', '5', '6', '7', 57 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 58 59 if (size == 0 || size > INT_MAX) 60 return -1; 61 62 /* The maximum number of bytes. */ 63 size_t maxlength = (size - 1) / 2; 64 65 /* The actual number of bytes. */ 66 size_t length = (n > maxlength ? maxlength : n); 67 68 for (size_t i = 0; i < length; ++i) { 69 /* Set the most-significant nibble. */ 70 unsigned char msn = (data[i] >> 4) & 0x0F; 71 str[i * 2 + 0] = ascii[msn]; 72 73 /* Set the least-significant nibble. */ 74 unsigned char lsn = data[i] & 0x0F; 75 str[i * 2 + 1] = ascii[lsn]; 76 } 77 78 /* Null terminate the hex string. */ 79 str[length * 2] = 0; 80 81 return (n > maxlength ? -1 : (int) (length * 2)); 82 } 83 84 static void 85 loghandler (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata) 86 { 87 const char *loglevels[] = {"NONE", "ERROR", "WARNING", "INFO", "DEBUG", "ALL"}; 88 89 dc_usecs_t now = 0; 90 dc_timer_now (context->timer, &now); 91 92 unsigned long seconds = now / 1000000; 93 unsigned long microseconds = now % 1000000; 94 95 if (loglevel == DC_LOGLEVEL_ERROR || loglevel == DC_LOGLEVEL_WARNING) { 96 fprintf (stderr, "[%li.%06li] %s: %s [in %s:%d (%s)]\n", 97 seconds, microseconds, 98 loglevels[loglevel], msg, file, line, function); 99 } else { 100 fprintf (stderr, "[%li.%06li] %s: %s\n", 101 seconds, microseconds, 102 loglevels[loglevel], msg); 103 } 104 } 105 #endif 106 107 dc_status_t 108 dc_context_new (dc_context_t **out) 109 { 110 dc_context_t *context = NULL; 111 112 if (out == NULL) 113 return DC_STATUS_INVALIDARGS; 114 115 context = (dc_context_t *) malloc (sizeof (dc_context_t)); 116 if (context == NULL) 117 return DC_STATUS_NOMEMORY; 118 119 #ifdef ENABLE_LOGGING 120 context->loglevel = DC_LOGLEVEL_WARNING; 121 context->logfunc = loghandler; 122 #else 123 context->loglevel = DC_LOGLEVEL_NONE; 124 context->logfunc = NULL; 125 #endif 126 context->userdata = NULL; 127 128 #ifdef ENABLE_LOGGING 129 memset (context->msg, 0, sizeof (context->msg)); 130 context->timer = NULL; 131 dc_timer_new (&context->timer); 132 #endif 133 134 *out = context; 135 136 return DC_STATUS_SUCCESS; 137 } 138 139 dc_status_t 140 dc_context_free (dc_context_t *context) 141 { 142 if (context == NULL) 143 return DC_STATUS_SUCCESS; 144 145 #ifdef ENABLE_LOGGING 146 dc_timer_free (context->timer); 147 #endif 148 free (context); 149 150 return DC_STATUS_SUCCESS; 151 } 152 153 dc_status_t 154 dc_context_set_loglevel (dc_context_t *context, dc_loglevel_t loglevel) 155 { 156 if (context == NULL) 157 return DC_STATUS_INVALIDARGS; 158 159 #ifdef ENABLE_LOGGING 160 context->loglevel = loglevel; 161 #endif 162 163 return DC_STATUS_SUCCESS; 164 } 165 166 dc_status_t 167 dc_context_set_logfunc (dc_context_t *context, dc_logfunc_t logfunc, void *userdata) 168 { 169 if (context == NULL) 170 return DC_STATUS_INVALIDARGS; 171 172 #ifdef ENABLE_LOGGING 173 context->logfunc = logfunc; 174 context->userdata = userdata; 175 #endif 176 177 return DC_STATUS_SUCCESS; 178 } 179 180 dc_status_t 181 dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...) 182 { 183 #ifdef ENABLE_LOGGING 184 va_list ap; 185 #endif 186 187 if (context == NULL) 188 return DC_STATUS_INVALIDARGS; 189 190 #ifdef ENABLE_LOGGING 191 if (loglevel > context->loglevel) 192 return DC_STATUS_SUCCESS; 193 194 if (context->logfunc == NULL) 195 return DC_STATUS_SUCCESS; 196 197 va_start (ap, format); 198 dc_platform_vsnprintf (context->msg, sizeof (context->msg), format, ap); 199 va_end (ap); 200 201 context->logfunc (context, loglevel, file, line, function, context->msg, context->userdata); 202 #endif 203 204 return DC_STATUS_SUCCESS; 205 } 206 207 dc_status_t 208 dc_context_syserror (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, int errcode) 209 { 210 const char *errmsg = NULL; 211 212 #ifdef _WIN32 213 char buffer[256]; 214 215 DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 216 NULL, errcode, 0, buffer, sizeof (buffer), NULL); 217 218 /* Remove the CRLF and period at the end of the error message. */ 219 while (rc > 0 && ( 220 buffer[rc - 1] == '\n' || 221 buffer[rc - 1] == '\r' || 222 buffer[rc - 1] == '.')) 223 { 224 buffer[rc - 1] = '\0'; 225 rc--; 226 } 227 228 if (rc > 0) 229 errmsg = buffer; 230 #elif defined (HAVE_STRERROR_R) 231 char buffer[256]; 232 233 int rc = strerror_r (errcode, buffer, sizeof (buffer)); 234 if (rc == 0) 235 errmsg = buffer; 236 #else 237 /* Fallback to the non-threadsafe function. */ 238 errmsg = strerror (errcode); 239 #endif 240 241 if (errmsg == NULL) 242 errmsg = "Unknown system error"; 243 244 return dc_context_log (context, loglevel, file, line, function, "%s (%d)", errmsg, errcode); 245 } 246 247 dc_status_t 248 dc_context_hexdump (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *prefix, const unsigned char data[], unsigned int size) 249 { 250 #ifdef ENABLE_LOGGING 251 int n; 252 #endif 253 254 if (context == NULL || prefix == NULL) 255 return DC_STATUS_INVALIDARGS; 256 257 #ifdef ENABLE_LOGGING 258 if (loglevel > context->loglevel) 259 return DC_STATUS_SUCCESS; 260 261 if (context->logfunc == NULL) 262 return DC_STATUS_SUCCESS; 263 264 n = dc_platform_snprintf (context->msg, sizeof (context->msg), "%s: size=%u, data=", prefix, size); 265 266 if (n >= 0) { 267 n = l_hexdump (context->msg + n, sizeof (context->msg) - n, data, size); 268 } 269 270 context->logfunc (context, loglevel, file, line, function, context->msg, context->userdata); 271 #endif 272 273 return DC_STATUS_SUCCESS; 274 } 275 276 unsigned int 277 dc_context_get_transports (dc_context_t *context) 278 { 279 UNUSED(context); 280 281 return DC_TRANSPORT_SERIAL 282 #if defined(HAVE_LIBUSB) 283 | DC_TRANSPORT_USB 284 #endif 285 #if defined(HAVE_HIDAPI) 286 | DC_TRANSPORT_USBHID 287 #elif defined(HAVE_LIBUSB) && !defined(__APPLE__) 288 | DC_TRANSPORT_USBHID 289 #endif 290 | DC_TRANSPORT_USBSTORAGE 291 #ifdef _WIN32 292 #ifdef HAVE_AF_IRDA_H 293 | DC_TRANSPORT_IRDA 294 #endif 295 #ifdef HAVE_WS2BTH_H 296 | DC_TRANSPORT_BLUETOOTH 297 #endif 298 #else /* _WIN32 */ 299 #ifdef HAVE_LINUX_IRDA_H 300 | DC_TRANSPORT_IRDA 301 #endif 302 #ifdef HAVE_BLUEZ 303 | DC_TRANSPORT_BLUETOOTH 304 #endif 305 #endif /* _WIN32 */ 306 ; 307 }