1
+ from tkinter import E
1
2
from .parameter_group_number import ParameterGroupNumber
2
- from .message_id import MessageId
3
+ from .message_id import MessageId , FrameFormat
3
4
import logging
4
5
import time
5
6
import numpy as np
@@ -87,6 +88,8 @@ def __init__(self, send_message, job_thread_wakeup, notify_subscribers, max_cmdt
87
88
# specified time range in j1939-22: 10-200ms
88
89
if minimum_tp_bam_dt_interval == None :
89
90
self ._minimum_tp_bam_dt_interval = 0.010
91
+ else :
92
+ self ._minimum_tp_bam_dt_interval = minimum_tp_bam_dt_interval
90
93
91
94
# Up to 4 concurrent BAM sessions per originator address are allowed
92
95
self .__bam_session_list = [True ] * 4
@@ -113,8 +116,27 @@ def remove_ca(self, device_address):
113
116
return False
114
117
115
118
def _buffer_hash (self , session_num , src_address , dest_address ):
116
- """Calcluates a hash value for the given address pair
119
+ """Calculates a hash value for the given address pair
120
+
121
+ :param src_address:
122
+ The Source-Address the connection should bound to.
123
+ :param dest_address:
124
+ The Destination-Address the connection should bound to.
125
+
126
+ :return:
127
+ The calculated hash value.
128
+
129
+ :rtype: int
130
+ """
131
+ return ((session_num & 0xF ) << 16 ) | ((src_address & 0xFF ) << 8 ) | (dest_address & 0xFF )
117
132
133
+ def _buffer_hash_mpg (self , frame_format , msg_counter , src_address , dest_address ):
134
+ """Calculates a hash value for the given multi-pg arguments
135
+
136
+ :param frame_format:
137
+ The frame-format (FBFF, FEFF) the connection should bound to.
138
+ :param msg_counter:
139
+ The message counter the connection should bound to.
118
140
:param src_address:
119
141
The Source-Address the connection should bound to.
120
142
:param dest_address:
@@ -125,10 +147,10 @@ def _buffer_hash(self, session_num, src_address, dest_address):
125
147
126
148
:rtype: int
127
149
"""
128
- return ((( session_num & 0xF ) << 16 ) | (src_address & 0xFF ) << 8 ) | (dest_address & 0xFF )
150
+ return ((frame_format & 0xFF ) << 24 ) | (( msg_counter & 0xFF ) << 16 ) | ( (src_address & 0xFF ) << 8 ) | (dest_address & 0xFF )
129
151
130
152
def _buffer_unhash (self , hash ):
131
- """Calcluates session-number, source-address and destination-address for the given hash value
153
+ """Calculates session-number, source-address and destination-address for the given hash value
132
154
133
155
:param hash:
134
156
The hash to be unhased
@@ -138,6 +160,17 @@ def _buffer_unhash(self, hash):
138
160
"""
139
161
return ((hash >> 16 ) & 0xFF ), ((hash >> 8 ) & 0xFF ), (hash & 0xFF )
140
162
163
+ def _buffer_unhash_mpg (self , hash ):
164
+ """Calculates frame_format, msg_counter, source-address and destination-address for the given hash value
165
+
166
+ :param hash:
167
+ The hash to be unhased
168
+
169
+ :return:
170
+ The session-number, source-address and destination-address
171
+ """
172
+ return ((hash >> 24 ) & 0xFF ), ((hash >> 16 ) & 0xFF ), ((hash >> 8 ) & 0xFF ), (hash & 0xFF )
173
+
141
174
def __get_bam_session (self ):
142
175
for idx , i in enumerate (self .__bam_session_list ):
143
176
if i == True :
@@ -158,7 +191,7 @@ def __get_rts_cts_session(self):
158
191
def __put_rts_cts_session (self , session ):
159
192
self .__rts_cts_session_list [session ] = True
160
193
161
- def send_pgn (self , data_page , pdu_format , pdu_specific , priority , src_address , data , time_limit = 0 , tos = 2 , trailer_format = 0 ):
194
+ def send_pgn (self , data_page , pdu_format , pdu_specific , priority , src_address , data , time_limit , frame_format , tos = 2 , trailer_format = 0 ):
162
195
pgn = ParameterGroupNumber (data_page , pdu_format , pdu_specific )
163
196
data_length = len (data )
164
197
@@ -173,17 +206,24 @@ def send_pgn(self, data_page, pdu_format, pdu_specific, priority, src_address, d
173
206
cpgn = pgn .value
174
207
dst_address = ParameterGroupNumber .Address .GLOBAL
175
208
209
+ if (frame_format == FrameFormat .FBFF ):
210
+ priority = 0
211
+ if (dst_address != ParameterGroupNumber .Address .GLOBAL ):
212
+ logger .info ('FBFF message must be a broadcast type' )
213
+ return False
214
+
176
215
# create header dict
177
216
cpg = {'priority' : (priority & 0x7 ), 'tos' : (tos & 0x7 ), 'tf' : (trailer_format & 0x7 ), 'cpgn' : (cpgn & 0x3FFFF ), 'data_length' : data_length , 'data' : data .copy ()}
178
217
179
218
# send immediately
180
219
if time_limit == 0 :
181
- self .__send_multi_pg ([cpg ], src_address , dst_address )
220
+ self .__send_multi_pg (frame_format , [cpg ], src_address , dst_address )
182
221
else :
183
222
session = 0
184
223
deadline = time .time () + time_limit
185
224
while True :
186
- hash = self ._buffer_hash (session , src_address , dst_address )
225
+ hash = self ._buffer_hash_mpg (frame_format , session , src_address , dst_address )
226
+ #hash = self._buffer_hash(session, src_address, dst_address)
187
227
if hash not in self ._multi_pg_snd_buffer :
188
228
self ._multi_pg_snd_buffer [hash ] = {'deadline' : deadline , 'cpg' : [cpg ], 'fill_level' : 4 + data_length }
189
229
break
@@ -209,13 +249,13 @@ def send_pgn(self, data_page, pdu_format, pdu_specific, priority, src_address, d
209
249
dest_address = ParameterGroupNumber .Address .GLOBAL
210
250
session_num = self .__get_bam_session ()
211
251
if session_num == None :
212
- print ('bam session not available' )
252
+ # print('bam session not available')
213
253
return False
214
254
else :
215
255
dest_address = pdu_specific
216
256
session_num = self .__get_rts_cts_session ()
217
257
if session_num == None :
218
- print ('rts/cts session not available' )
258
+ # print('rts/cts session not available')
219
259
return False
220
260
221
261
# init sequence
@@ -280,7 +320,7 @@ def send_pgn(self, data_page, pdu_format, pdu_specific, priority, src_address, d
280
320
281
321
return True
282
322
283
- def __send_multi_pg (self , cpg_list , src_address , dst_address ):
323
+ def __send_multi_pg (self , frame_format , cpg_list , src_address , dst_address ):
284
324
# deadline reached
285
325
priority = 7
286
326
data = []
@@ -306,10 +346,13 @@ def __send_multi_pg(self, cpg_list, src_address, dst_address):
306
346
else :
307
347
data .append (0xAA )
308
348
309
- mid = MessageId (priority = priority ,
310
- parameter_group_number = ParameterGroupNumber .PGN .FEFF_MULTI_PG | (dst_address & 0xFF ),
311
- source_address = src_address )
312
- self .__send_message (mid .can_id , data , fd_format = True )
349
+ if frame_format == FrameFormat .FBFF :
350
+ self .__send_message (src_address , False , data , fd_format = True )
351
+ else :
352
+ mid = MessageId (priority = priority ,
353
+ parameter_group_number = ParameterGroupNumber .PGN .FEFF_MULTI_PG | (dst_address & 0xFF ),
354
+ source_address = src_address )
355
+ self .__send_message (mid .can_id , True , data , fd_format = True )
313
356
314
357
315
358
def async_job_thread (self , now ):
@@ -345,9 +388,9 @@ def async_job_thread(self, now):
345
388
next_wakeup = buf ['deadline' ]
346
389
else :
347
390
# deadline reached
348
- session_num , src_address , dst_address = self ._buffer_unhash (bufid )
391
+ frame_format , session_num , src_address , dst_address = self ._buffer_unhash_mpg (bufid )
349
392
350
- self .__send_multi_pg (buf ['cpg' ], src_address , dst_address )
393
+ self .__send_multi_pg (frame_format , buf ['cpg' ], src_address , dst_address )
351
394
352
395
del self ._multi_pg_snd_buffer [bufid ]
353
396
@@ -717,7 +760,7 @@ def __send_tp_cm(self, src_address, dest_address,
717
760
data [10 ] = ( (pgn >> 8 ) & 0xFF )
718
761
data [11 ] = ( (pgn >> 16 ) & 0xFF )
719
762
# 13 up to 64 Assurance Data of full message calculated using AD Type. Total length = Size in byte 8.
720
- self .__send_message (mid .can_id , data , fd_format = True )
763
+ self .__send_message (mid .can_id , True , data , fd_format = True )
721
764
722
765
def __send_tp_dt (self , src_address , dest_address , session_num , segment_num , data , Dtfi = 0 ):
723
766
pgn = ParameterGroupNumber (0 , (ParameterGroupNumber .PGN .FD_TP_DT >> 8 ) & 0xFF , dest_address )
@@ -739,7 +782,7 @@ def __send_tp_dt(self, src_address, dest_address, session_num, segment_num, data
739
782
while len (data )< next_valid_fd_length :
740
783
data .append (255 )
741
784
742
- self .__send_message (mid .can_id , data , fd_format = True )
785
+ self .__send_message (mid .can_id , True , data , fd_format = True )
743
786
744
787
745
788
def notify (self , can_id , data , timestamp ):
0 commit comments