package Core

/*
#cgo CFLAGS: -I../include
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "HCNetSDK.h"
*/
import "C"
import "unsafe"

func charPtrToByteSlice(cstr *C.char, dwBufSize DWORD) []byte {
	goSlice := C.GoBytes(unsafe.Pointer(cstr), C.int(dwBufSize))
	return goSlice
}

var serialCallBackHooks = make(map[LONG]serialDataCallBack)

type serialDataCallBack func(lSerialHandle LONG, lChannel LONG, pRecvDataBuffer []byte, dwBufSize DWORD, pUser unsafe.Pointer)

//export fSerialDataCallBackGo
func fSerialDataCallBackGo(lSerialHandle C.LONG, lChannel C.LONG, pRecvDataBuffer *C.char, dwBufSize C.DWORD, pUser *C.void) {
	callback := serialCallBackHooks[LONG(lSerialHandle)]
	println(DWORD(dwBufSize))
	if callback != nil {
		callback(LONG(lSerialHandle), LONG(lChannel), charPtrToByteSlice(pRecvDataBuffer, DWORD(dwBufSize)), DWORD(dwBufSize), unsafe.Pointer(pUser))
	}
}

var exceptionCallBackHooks = make(map[int]ExceptionCallBack)

type ExceptionCallBack func(dwType uint32, lUserID int32, lHandle int32, pUser interface{})

//export fExceptionCallBackGo
func fExceptionCallBackGo(dwType C.DWORD, lUserID C.LONG, lHandle C.LONG, pUser *C.void) {
	callback := exceptionCallBackHooks[int(lUserID)]
	if callback != nil {
		callback(uint32(dwType), int32(lUserID), int32(lHandle), pUser)
	}
}
