2
0

util.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Misc utilities
  3. *
  4. * (c) Philippe 2016-2017, philippe_44@outlook.com
  5. *
  6. * This software is released under the MIT License.
  7. * https://opensource.org/licenses/MIT
  8. *
  9. */
  10. #ifndef __UTIL_H
  11. #define __UTIL_H
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include "platform.h"
  16. #include "pthread.h"
  17. #ifndef WIN32
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/timers.h"
  20. #endif
  21. #define NFREE(p) if (p) { free(p); p = NULL; }
  22. typedef struct metadata_s {
  23. char *artist;
  24. char *album;
  25. char *title;
  26. char *genre;
  27. char *path;
  28. char *artwork;
  29. char *remote_title;
  30. u32_t track;
  31. u32_t duration;
  32. u32_t track_hash;
  33. u32_t sample_rate;
  34. u8_t sample_size;
  35. u8_t channels;
  36. } metadata_t;
  37. void free_metadata(struct metadata_s *metadata);
  38. void dup_metadata(struct metadata_s *dst, struct metadata_s *src);
  39. u32_t gettime_ms(void);
  40. #ifdef WIN32
  41. char* strsep(char** stringp, const char* delim);
  42. char *strndup(const char *s, size_t n);
  43. int asprintf(char **strp, const char *fmt, ...);
  44. void winsock_init(void);
  45. void winsock_close(void);
  46. #define SAFE_PTR_FREE(P) free(P) #else
  47. char *strlwr(char *str);
  48. // reason is that TCB might be cleanup in idle task
  49. #define SAFE_PTR_FREE(P) \
  50. do { \
  51. TimerHandle_t timer = xTimerCreate("cleanup", pdMS_TO_TICKS(10000), pdFALSE, P, _delayed_free); \
  52. xTimerStart(timer, portMAX_DELAY); \
  53. } while (0)
  54. static void inline _delayed_free(TimerHandle_t xTimer) {
  55. free(pvTimerGetTimerID(xTimer));
  56. xTimerDelete(xTimer, portMAX_DELAY);
  57. }
  58. #endif
  59. char* strextract(char *s1, char *beg, char *end);
  60. in_addr_t get_localhost(char **name);
  61. void get_mac(u8_t mac[]);
  62. int shutdown_socket(int sd);
  63. int bind_socket(short unsigned *port, int mode);
  64. int conn_socket(unsigned short port); typedef struct {
  65. char *key;
  66. char *data;
  67. } key_data_t;
  68. bool http_parse(int sock, char *method, key_data_t *rkd, char **body, int *len); char* http_send(int sock, char *method, key_data_t *rkd);
  69. char* kd_lookup(key_data_t *kd, char *key);
  70. bool kd_add(key_data_t *kd, char *key, char *value);
  71. char* kd_dump(key_data_t *kd);
  72. void kd_free(key_data_t *kd);
  73. int _fprintf(FILE *file, ...);
  74. #endif