2 # Copyright (c) 2020 Project CHIP Authors
3 # Copyright (c) 2020 Google LLC.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
21 # This file is utility for Chip
24 from __future__ import absolute_import
25 from __future__ import print_function
30 class ChipUtility(object):
33 return binascii.hexlify(val).decode()
36 def VoidPtrToByteArray(ptr, len):
39 memmove((c_byte * len).from_buffer(v), ptr, len)
45 def ByteArrayToVoidPtr(array):
47 if not (isinstance(array, bytes) or isinstance(array, bytearray)):
48 raise TypeError("Array must be an str or a bytearray")
49 return cast((c_byte * len(array)).from_buffer_copy(array), c_void_p)
54 def IsByteArrayAllZeros(array):
55 for i in range(len(array)):
61 def ByteArrayToHex(array):
62 return ChipUtility.Hexlify(bytes(array))
65 def CStringToString(s):
66 return None if s is None else s.decode()
69 def StringToCString(s):
70 return None if s is None else s.encode()