#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals import six import cstruct from cstruct import define, typedef, CStruct from keyplus.constants import * class CStructWithBytes(CStruct): def to_bytes(self): return bytearray(self.pack()) typedef('uint8', 'uint8_t') typedef('uint16', 'uint16_t') typedef('uint32', 'uint32_t') typedef('int8', 'int8_t') typedef('int16', 'int16_t') typedef('int32', 'int32_t') define('MAX_NUM_KEYBOARDS', 64) define('MAX_NUM_DEVICES', 64) define('AES_KEY_LEN', 16) define('NRF_ADDR_LEN', 5) def make_bit_field_variables(class_obj, field_list): """ This function uses the python built-in `property()` to create attributes
def test_typedef(self): typedef('int', 'integer') self.assertEqual(sizeof('integer'), 4)
def defineStandardCTypes(): typedef("unsigned char", "u8") typedef("unsigned short", "u16") typedef("unsigned long", "u32") typedef("unsigned long long", "u64") typedef("signed char", "s8") typedef("short", "s16") typedef("long", "s32") typedef("long long", "s64") typedef("volatile unsigned char", "vu8") typedef("volatile unsigned short", "vu16") typedef("volatile unsigned long", "vu32") typedef("volatile unsigned long long", "vu64") typedef("volatile signed char", "vs8") typedef("volatile short", "vs16") typedef("volatile long", "vs32") typedef("volatile long long", "vs64") typedef("float", "f32") typedef("double", "f64")
#!/usr/bin/python import serial import time import threading import sys from queue import Queue from binascii import hexlify import cstruct g_Running = True ser = serial.Serial('/dev/ttyUSB0', 115200) cstruct.typedef("uint8", "uint8_t") cstruct.typedef("int8", "int8_t") cstruct.typedef("uint16", "uint16_t") cstruct.typedef("int16", "int16_t") cstruct.typedef("uint32", "uint32_t") cstruct.typedef("int32", "int32_t") class M365BMS(cstruct.CStruct): __byte_order__ = cstruct.LITTLE_ENDIAN __struct__ = """ /*00-1F*/ uint16_t unk1[16] = {0x5A, 0x5A, 0x00}; /*20-2D*/ char serial[14] = ""; /*2E-2F*/ uint16_t version = 0x900; // 0x115 = 1.1.5 /*30-31*/ uint16_t design_capacity = 0; // mAh /*32-33*/ uint16_t unk_capacity = 0; // mAh /*34-35*/ uint16_t nominal_voltage = 0; // mV /*36-37*/ uint16_t num_cycles = 0; /*38-39*/ uint16_t num_charged = 0;
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. # #***************************************************************************** from unittest import TestCase, main from cstruct import (sizeof, typedef, define, CStruct, NATIVE_ORDER) define("UT_NAMESIZE", 32) define("UT_LINESIZE", 32) define("UT_HOSTSIZE", 256) typedef("int", "pid_t") typedef("long", "time_t") class ExitStatus(CStruct): __def__ = """ struct { short e_termination; /* Process termination status. */ short e_exit; /* Process exit status. */ } """ class Timeval(CStruct): __def__ = """ struct {
class Dummy(cstruct.MemCStruct): __byte_order__ = cstruct.LITTLE_ENDIAN __def__ = """ struct { char c; char vc[10]; int i; int vi[10]; long long l; long vl[10]; float f; float vf[10]; } """ typedef('char', 'BYTE') typedef('short', 'WORD') typedef('int', 'DWORD') class PartitionFlat(cstruct.MemCStruct): __byte_order__ = cstruct.LITTLE_ENDIAN __def__ = """ struct { BYTE status; // 0x80 for bootable, 0x00 for not bootable BYTE startAddrHead; // head address of start of partition WORD startAddrCylSec; BYTE partType; BYTE endAddrHead; // head address of start of partition WORD endAddrCylSec; DWORD startLBA; // address of first sector in partition DWORD endLBA; // address of last sector in partition