Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / controller / python / chip / ble / library_handle.py
1 #
2 #    Copyright (c) 2021 Project CHIP Authors
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License");
5 #    you may not use this file except in compliance with the License.
6 #    You may obtain a copy of the License at
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS,
12 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #    See the License for the specific language governing permissions and
14 #    limitations under the License.
15 #
16
17 import chip.native
18 import ctypes
19 from ctypes import c_bool, c_void_p, c_char_p, c_uint32, py_object
20 from chip.ble.types import DeviceScannedCallback, ScanDoneCallback
21
22
23 # This prevents python auto-casting c_void_p to integers and 
24 # auto-casting 32/64 bit values to int/long respectively. Without this
25 # passing in c_void_p does not see to work well for numbers 
26 # in [0x80000000; 0xFFFFFFFF] (argument will be auto-cast to 64-bit negative)
27 class VoidPointer(c_void_p):
28     pass
29
30
31 def _GetBleLibraryHandle() -> ctypes.CDLL:
32   """ Get the native library handle with BLE method initialization.
33
34     Retreives the CHIP native library handle and attaches signatures to
35     native methods.
36     """
37
38   handle = chip.native.GetLibraryHandle()
39
40   # Uses one of the type decorators as an indicator for everything being
41   # initialized. Native methods default to c_int return types
42   if handle.pychip_ble_adapter_list_new.restype != VoidPointer:
43     setter = chip.native.NativeLibraryHandleMethodArguments(handle)
44
45     setter.Set('pychip_ble_adapter_list_new', VoidPointer, [])
46     setter.Set('pychip_ble_adapter_list_next', c_bool, [VoidPointer])
47     setter.Set('pychip_ble_adapter_list_get_index', c_uint32, [VoidPointer])
48     setter.Set('pychip_ble_adapter_list_get_address', c_char_p, [VoidPointer])
49     setter.Set('pychip_ble_adapter_list_get_alias', c_char_p, [VoidPointer])
50     setter.Set('pychip_ble_adapter_list_get_name', c_char_p, [VoidPointer])
51     setter.Set('pychip_ble_adapter_list_is_powered', c_bool, [VoidPointer])
52     setter.Set('pychip_ble_adapter_list_delete', None, [VoidPointer])
53     setter.Set('pychip_ble_adapter_list_get_raw_adapter', VoidPointer, [VoidPointer])
54
55     setter.Set('pychip_ble_start_scanning', VoidPointer, [
56         py_object, VoidPointer, c_uint32, DeviceScannedCallback, ScanDoneCallback
57     ])
58
59   return handle