libtransistor
A userland library for the Nintendo Switch
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
util.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #include<libtransistor/types.h>
13 #include<libtransistor/fd.h>
14 
15 #define ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0]))
16 
17 #define SECONDS_TO_NANOSECONDS(seconds) ((seconds)*(1000000000))
18 
19 #define ASSERT_OK(label, expr) if((r = expr) != RESULT_OK) { \
20  printf("assertion failed at %s:%d: result 0x%x is not OK\n", __FILE__, __LINE__, r); \
21  goto label; \
22  }
23 
24 #define LIB_ASSERT_OK(label, expr) if((r = expr) != RESULT_OK) { goto label; }
25 
26 #define TRACE printf("tracing at %s:%d\n", __FILE__, __LINE__);
27 #define DBG_TRACE dbg_printf("tracing at %s:%d", __FILE__, __LINE__);
28 
34 uint64_t str2u64(const char *str);
35 
41 char nybble2hex(uint8_t nybble);
42 
51 void hexdump(const void *rawbuf, size_t size);
52 
61 void hexdump_dbg(const void *rawbuf, size_t size);
62 
68 void hexnum(int num);
69 
81 int log_string(const char *string, size_t len);
82 
92 result_t dbg_connect(const char *host, const char *port, int *fd);
93 
97 void dbg_disconnect();
98 
106 
107 // TODO: A perror for result_t.
108 
109 #define STB_SPRINTF_DECORATE(name) dbg_##name
110 #include "stb_sprintf.h"
111 
112 #include<stdarg.h>
113 
114 int dbg_printf(char const *fmt, ...);
115 int dbg_vprintf(char const *fmt, va_list va);
116 void dbg_set_file(trn_file_t *file);
117 
118 #ifdef __cplusplus
119 }
120 #endif
void hexnum(int num)
Convert an int number to hex and prints it to stdout.
Various system types.
void dbg_disconnect()
Disconnect the debug log from the socket.
int trn_result_to_errno(result_t r)
Turns a libtransistor result_t into a POSIX errno. The POSIX errno will be positive. You might want to negate it before returning!
char nybble2hex(uint8_t nybble)
Convert a uint8 nybble to a hex char.
Definition: fd.h:38
void hexdump_dbg(const void *rawbuf, size_t size)
Hexdump a buffer using the debug logger.
uint32_t result_t
Function result.
Definition: types.h:51
File operations.
int log_string(const char *string, size_t len)
Log a string to the debug log.
result_t dbg_connect(const char *host, const char *port, int *fd)
Connect the debug log over a socket.
void hexdump(const void *rawbuf, size_t size)
Hexdump a buffer.
uint64_t str2u64(const char *str)
Converts a string to a u64.