//
// Created by Ht on 2021/2/8.
//

#ifndef CMDNS_CMDNS_HAL_INTF_H
#define CMDNS_CMDNS_HAL_INTF_H

#include <pthread.h>
#include <jebase/config.h>

#if defined(__cplusplus)
extern "C" {
#endif

typedef unsigned char CMBool;

#define CMTrue      1
#define CMFalse     0

typedef struct {
    int net_intf_;
    void* sock_addr_;
} CMNetIntf;

typedef unsigned int CMSize;

JE_API void* HAL_Malloc(unsigned int size);
JE_API void  HAL_Free(void*);

JE_API void  HAL_Release(); //释放所有剩余资源

//利用Socket及timer发送，不同系统具有不同的创建模式
//需要如下几个hal接口：创建、释放socket，创建、释放timer（OS线程模式），发送socket、读取socket

//网络接口创建
JE_API CMNetIntf*  HAL_Create_NetIntf(CMNetIntf** intf, const char* host, int port);
JE_API void  HAL_Release_NetIntf(CMNetIntf*intf);

//Timer的创建和停止
typedef CMBool (*TimerTask)(void* context);

typedef struct {
    CMBool       quit_:1;
    CMBool       quitted_:1;
    unsigned int interval_;
    void*        context_;
    pthread_t    thread_;
    pthread_cond_t*     cond_;
    pthread_mutex_t*    mutex_;
    TimerTask    task_;
} CMTimer;

JE_API CMTimer*  HAL_Create_Timer(CMTimer** timer, void* context, TimerTask func);
JE_API void  HAL_Release_Timer(CMTimer* timer);
JE_API CMBool HAL_Start_Timer(CMTimer* timer, unsigned int interval);

//网络消息的发送和接收
JE_API void  HAL_Send_Diagram(CMNetIntf* intf, char* buff,  CMSize size);
JE_API typedef void (*NetDataHandler)(void* context, char* buff,  CMSize size, const char* ip, int port);
JE_API void  HAL_Read_Diagram(CMNetIntf* intf, void* context, char* buff,  CMSize size, NetDataHandler handler);

#ifdef __cplusplus
}
#endif


#endif //CMDNS_CMDNS_HAL_INTF_H
