| 1 |  |  | from libAnt.constants import * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | class Message: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     def __init__(self, type: int, content: bytes): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |         self._type = type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |         self._content = content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     def __len__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         return len(self._content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     def __iter__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         return self._content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     def __str__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         return '({:02X}): '.format(self._type) + ' '.join('{:02X}'.format(x) for x in self._content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     def checksum(self) -> int: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         chk = MESSAGE_TX_SYNC ^ len(self) ^ self._type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         for b in self._content: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             chk ^= b | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         return chk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     def encode(self) -> bytes: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         b = bytearray([MESSAGE_TX_SYNC, len(self), self._type]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         b.extend(self._content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         b.append(self.checksum()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return bytes(b) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     def type(self) -> int: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return self._type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     def content(self) -> bytes: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return self._content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | class BroadcastMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     def __init__(self, type: int, content: bytes): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         self.flag = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self.deviceNumber = self.deviceType = self.transType = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         self.rssiMeasurementType = self.rssi = self._rssiThreshold = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         self.rssi = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         self.rssiThreshold = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         self.rxTimestamp = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         self.channel = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self.extendedContent = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         super().__init__(type, content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     def build(self, raw: bytes): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self._type = MESSAGE_CHANNEL_BROADCAST_DATA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         self.channel = raw[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         self._content = raw[1:9] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if len(raw) > 9:  # Extended message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             self.flag = raw[9] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             self.extendedContent = raw[10:] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             offset = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             if self.flag & EXT_FLAG_CHANNEL_ID: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 self.deviceNumber = int.from_bytes(self.extendedContent[:2], byteorder='little', signed=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 self.deviceType = self.extendedContent[2] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 self.transType = self.extendedContent[3] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 offset += 4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if self.flag & EXT_FLAG_RSSI: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 rssi = self.extendedContent[offset:(offset + 3)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 self.rssiMeasurementType = rssi[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 self.rssi = rssi[1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 self.rssiThreshold = rssi[2] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 offset += 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if self.flag & EXT_FLAG_TIMESTAMP: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 self.rxTimestamp = int.from_bytes(self.extendedContent[offset:], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                                                   byteorder='little', signed=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     def checksum(self) -> int: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     def encode(self) -> bytes: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | class SystemResetMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         super().__init__(MESSAGE_SYSTEM_RESET, b'0') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | class SetNetworkKeyMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def __init__(self, channel: int, key: bytes = ANTPLUS_NETWORK_KEY): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         content = bytearray([channel]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         content.extend(key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         super().__init__(MESSAGE_NETWORK_KEY, bytes(content)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | class AssignChannelMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     def __init__(self, channel: int, type: int, network: int = 0, extended: int = None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         content = bytearray([channel, type, network]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if extended is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             content.append(extended) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         super().__init__(MESSAGE_CHANNEL_ASSIGN, bytes(content)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | class SetChannelIdMessage(Message): | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     def __init__(self, channel: int, deviceNumber: int = 0, deviceType: int = 0, transType: int = 0): | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         content = bytearray([channel]) | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         content.extend(deviceNumber.to_bytes(2, byteorder='big')) | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         content.append(deviceType) | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         content.append(transType) | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         super().__init__(MESSAGE_CHANNEL_ID, bytes(content)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | class SetChannelRfFrequencyMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     def __init__(self, channel: int, frequency: int = 2457): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         content = bytes([channel, frequency - 2400]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         super().__init__(MESSAGE_CHANNEL_FREQUENCY, content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | class OpenRxScanModeMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         super().__init__(OPEN_RX_SCAN_MODE, bytes([0])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | class EnableExtendedMessagesMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     def __init__(self, enable: bool = True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         content = bytes([0, int(enable)]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         super().__init__(MESSAGE_ENABLE_EXT_RX_MESSAGES, content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | class LibConfigMessage(Message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     def __init__(self, rxTimestamp: bool = True, rssi: bool = True, channelId: bool = True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         config = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if rxTimestamp: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             config |= EXT_FLAG_TIMESTAMP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if rssi: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             config |= EXT_FLAG_RSSI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         if channelId: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             config |= EXT_FLAG_CHANNEL_ID | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  |         super().__init__(MESSAGE_LIB_CONFIG, bytes([0, config])) | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |