1# -*- coding: utf-8 -*-
2"""VISA VPP-4.3 data types (VPP-4.3.2 spec, section 3) using ctypes constants.
3
4This file is part of PyVISA.
5
6All data types that are defined by VPP-4.3.2.
7
8The module exports all data types including the pointer and array types.  This
9means "ViUInt32" and such.
10
11:copyright: 2014-2020 by PyVISA Authors, see AUTHORS for more details.
12:license: MIT, see LICENSE for more details.
13
14"""
15import ctypes as _ctypes
16
17from .cthelper import FUNCTYPE
18
19# Part One: Type Assignments for VISA and Instrument Drivers, see spec table
20# 3.1.1.
21#
22# Remark: The pointer and probably also the array variants are of no
23# significance in Python because there is no native call-by-reference.
24# However, as long as I'm not fully sure about this, they won't hurt.
25
26
27def _type_pair(ctypes_type):
28    return ctypes_type, _ctypes.POINTER(ctypes_type)
29
30
31def _type_triplet(ctypes_type):
32    return _type_pair(ctypes_type) + (_ctypes.POINTER(ctypes_type),)
33
34
35ViUInt64, ViPUInt64, ViAUInt64 = _type_triplet(_ctypes.c_uint64)
36ViInt64, ViPInt64, ViAInt64 = _type_triplet(_ctypes.c_int64)
37ViUInt32, ViPUInt32, ViAUInt32 = _type_triplet(_ctypes.c_uint32)
38ViInt32, ViPInt32, ViAInt32 = _type_triplet(_ctypes.c_int32)
39ViUInt16, ViPUInt16, ViAUInt16 = _type_triplet(_ctypes.c_ushort)
40ViInt16, ViPInt16, ViAInt16 = _type_triplet(_ctypes.c_short)
41ViUInt8, ViPUInt8, ViAUInt8 = _type_triplet(_ctypes.c_ubyte)
42ViInt8, ViPInt8, ViAInt8 = _type_triplet(_ctypes.c_byte)
43ViAddr, ViPAddr, ViAAddr = _type_triplet(_ctypes.c_void_p)
44ViChar, ViPChar, ViAChar = _type_triplet(_ctypes.c_char)
45ViByte, ViPByte, ViAByte = _type_triplet(_ctypes.c_ubyte)
46ViBoolean, ViPBoolean, ViABoolean = _type_triplet(ViUInt16)
47ViReal32, ViPReal32, ViAReal32 = _type_triplet(_ctypes.c_float)
48ViReal64, ViPReal64, ViAReal64 = _type_triplet(_ctypes.c_double)
49
50
51class ViString(object):
52    @classmethod
53    def from_param(cls, obj):
54        if isinstance(obj, str):
55            return bytes(obj, "ascii")
56        return obj
57
58
59class ViAString(object):
60    @classmethod
61    def from_param(cls, obj):
62        return _ctypes.POINTER(obj)
63
64
65ViPString = ViString
66
67# This follows visa.h definition, but involves a lot of manual conversion.
68# ViBuf, ViPBuf, ViABuf = ViPByte, ViPByte, _ctypes.POINTER(ViPByte)
69
70ViBuf, ViPBuf, ViABuf = ViPString, ViPString, ViAString
71
72
73def buffer_to_text(buf) -> str:
74    return buf.value.decode("ascii")
75
76
77ViRsrc = ViString
78ViPRsrc = ViString
79ViARsrc = ViAString
80
81ViKeyId, ViPKeyId = ViString, ViPString
82
83ViStatus, ViPStatus, ViAStatus = _type_triplet(ViInt32)
84ViVersion, ViPVersion, ViAVersion = _type_triplet(ViUInt32)
85_ViObject, ViPObject, ViAObject = _type_triplet(ViUInt32)
86_ViSession, ViPSession, ViASession = _type_triplet(ViUInt32)
87
88
89class ViObject(_ViObject):  # type: ignore
90    @classmethod
91    def from_param(cls, obj):
92        if obj is None:
93            raise ValueError("Session cannot be None. The resource might be closed.")
94        return _ViObject.from_param(obj)
95
96
97ViSession = ViObject
98
99
100ViAttr = ViUInt32
101ViConstString = _ctypes.POINTER(ViChar)
102
103
104# Part Two: Type Assignments for VISA only, see spec table 3.1.2.  The
105# difference to the above is of no significance in Python, so I use it here
106# only for easier synchronisation with the spec.
107
108ViAccessMode, ViPAccessMode = _type_pair(ViUInt32)
109ViBusAddress, ViPBusAddress = _type_pair(ViUInt32)
110ViBusAddress64, ViPBusAddress64 = _type_pair(ViUInt64)
111
112ViBusSize = ViUInt32
113
114ViAttrState, ViPAttrState = _type_pair(ViUInt32)
115
116# The following is weird, taken from news:zn2ek2w2.fsf@python.net
117ViVAList = _ctypes.POINTER(_ctypes.c_char)
118
119ViEventType, ViPEventType, ViAEventType = _type_triplet(ViUInt32)
120
121ViPAttr = _ctypes.POINTER(ViAttr)
122ViAAttr = ViPAttr
123
124ViEventFilter = ViUInt32
125
126ViFindList, ViPFindList = _type_pair(ViObject)
127ViEvent, ViPEvent = _type_pair(ViObject)
128ViJobId, ViPJobId = _type_pair(ViUInt32)
129
130# Class of callback functions for event handling, first type is result type
131ViHndlr = FUNCTYPE(ViStatus, ViSession, ViEventType, ViEvent, ViAddr)
132