// Author: NiuJiuRu
// Email: niujiuru@qq.com

package ec200u

/*
#include "ec200u.h"
*/
import "C"

import (
	"fmt"
	"strconv"
	"unsafe"
)

// 打开与模块的通讯
func ec200U_ComInit() (int, error) {
	ret := int(C.EC200U_ComInit())
	if ret != 0 {
		return ret, fmt.Errorf("an error occurred while calling the C.EC200U_ComInit() function(%d)", ret)
	}
	return 0, nil
}

// 关闭与模块的通讯
func ec200U_ComExit() error {
	ret := int(C.EC200U_ComExit())
	if ret != 0 {
		return fmt.Errorf("an error occurred while calling the C.EC200U_ComExit() function(%d)", ret)
	}
	return nil
}

func ec200U_Repower() error {
	ret := int(C.EC200U_Repower())
	if ret != 0 {
		return fmt.Errorf("an error occurred while calling the C.EC200U_Repower() function(%d)", ret)
	}
	return nil
}

// 获取模块的标识号
func ec200U_GetIMEI() (string, error) {
	var buf [16]byte
	ret := int(C.EC200U_GetIMEI((*C.char)(unsafe.Pointer(&buf[0]))))
	if ret != 15 {
		return "", fmt.Errorf("an error occurred while calling the C.EC200U_GetIMEI() function(%d)", ret)
	}
	return C.GoString((*C.char)(unsafe.Pointer(&buf[0]))), nil
}

// 获取网络注册状态
func ec200U_GetCregState() (int, error) {
	ret := int(C.EC200U_GetCregState())
	if ret < 0 {
		return -1, fmt.Errorf("an error occurred while calling the C.EC200U_GetCregState() function(%d)", ret)
	}
	return ret, nil
}

// 获取当前信号强度
func ec200U_GetRSSIFromCSQ() (string, error) {
	ret := int(C.EC200U_GetRSSIFromCSQ())
	if ret < 0 {
		return "", fmt.Errorf("an error occurred while calling the C.EC200U_GetRSSIFromCSQ() function(%d)", ret)
	}
	return strconv.Itoa(ret), nil
}

// 返回电话卡的状态
func ec200U_IsSimCardReady() (bool, error) {
	ret := int(C.EC200U_IsSimCardReady())
	if ret < 0 {
		return false, fmt.Errorf("an error occurred while calling the C.EC200U_IsSimCardReady() function(%d)", ret)
	}
	return ret == 1, nil
}

// 获取电话卡标识号
func ec200U_GetSimICCID() (string, error) {
	var buf [21]byte
	ret := int(C.EC200U_GetSimICCID((*C.char)(unsafe.Pointer(&buf[0]))))
	if ret != 20 {
		return "", fmt.Errorf("an error occurred while calling the C.EC200U_GetSimICCID() function(%d)", ret)
	}
	return C.GoString((*C.char)(unsafe.Pointer(&buf[0]))), nil
}
