﻿#ifndef _UTIL_H_
#define _UTIL_H_
#include <unistd.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <mqueue.h>
#include <pthread.h>
#include <time.h>
#include <sys/select.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
#include "log.h"
#include <sqlite3.h>

/* log level */
extern int DEBUG_INFO;
extern int DEBUG_DEBUG;
extern int DEBUG_WARNING;
extern int DEBUG_ERROR;
extern int DEBUG_JOIN;
extern int DEBUG_UPDW;
extern int DEBUG_SQL;

#define MSG_DEBUG(FLAG, fmt, ...)                                                               \
         do {                                                                                       \
                if (FLAG)                                                                                 \
                      fprintf(stdout, fmt, ##__VA_ARGS__); \
         } while (0)

#define BUFFER_CHUNK_SIZE 1024*1024*2

#define VAR_TO_PTR(buff,struct_p) do {                \
		memcpy(buff,&struct_p,sizeof(struct_p));                   \
		buff += sizeof(struct_p);                  \
    } while (0)

#define PTR_TO_VAR(buff,struct_p) do {                \
		memcpy(&struct_p,buff,sizeof(struct_p));                   \
		buff += sizeof(struct_p);                  \
    } while (0)

void Daemonize();
int already_running(void);
char* Clone(const char* data, int length);
long long Now();
uint64_t TimestampMs();
void SRandom();
long long Random();
int GetUint16(const char* data, uint16_t* t);
int GetUint32(const char* data, uint32_t* t);
int GetUint64(const char* data, uint64_t* t);
int SetUint16(char* data, uint16_t t);
int SetUint32(char* data, uint32_t t);
int SetUint64(char* data, uint64_t t);
int EncodeCellLength(const char* key, uint32_t keylen, const char* val, uint32_t vallen);
char* EncodeCell(char* data, const char* key, uint32_t keylen, const char* val, uint32_t vallen);
char* DecodeCell(char* begin, char* end, char** key, uint32_t* keylen, char** val, uint32_t* vallen);
int Uint642Str(uint64_t n, char* data);
uint32_t Str2Uint32(const char* data, int length);
uint64_t Str2Uint64(const char* data, int length);
void InitUtils(const char* dev);
void DestoryUtils();
int GetMac(char* b);
int GetMD5(char* file,char* md5);
int RemoveLocalFile(const char* path);
int AppendLocalFile(const char* path, const char* data, int length);
int WriteLocalFile(const char* path, const char* data, int length);
int ReadLocalFile(const char* path, char** data);
int KeyCompare(const char* key1, int len1, const char* key2, int len2);
void SetDatetime (uint32_t year, uint32_t mon, uint32_t day, uint32_t hour, uint32_t min, uint32_t sec);
uint32_t Mktime (uint32_t year, uint32_t mon, uint32_t day, uint32_t hour, uint32_t min, uint32_t sec);
uint32_t MktimeLocat (void);

int SetHardwareRtc(struct tm *t_tm);
char* GetFileName(const char* path) ;
#endif /* _UTIL_H_ */
