1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
module jtty_receive_context
use iso_c_binding, only: c_int, c_int16_t, c_int64_t, c_float, c_double, c_char
use iso_fortran_env, only: int64, real64
use jtty_fec, only: PAYLOAD_BITS
use jtty_mdec
implicit none
private
type :: receive_state
integer :: active_count=0, recent_count=0, pending_count=0, pending_head=1, decode_count=0
type(message_assembly) :: active(MAX_ACTIVE_MESSAGES)
type(frame_fingerprint) :: recent(MAX_RECENT_FRAMES)
type(message_update), allocatable :: pending(:)
end type receive_state
type :: receive_retro_state
logical :: active=.false.
integer :: count=0, signal=1, step=1
real :: frequency(MAX_SUBTRACTED)=0.0
real(real64) :: sync_time(MAX_SUBTRACTED)=0.0_real64
integer :: payload(PAYLOAD_BITS,MAX_SUBTRACTED)=0
end type receive_retro_state
type :: receive_context
logical :: allocated=.false., running=.false.
integer :: nsps=384
integer(int64) :: session_id=0, grid_origin=0, first_search=0, next_search=0
type(receive_state) :: state
type(receive_retro_state) :: retro
end type receive_context
integer, parameter :: MAX_CONTEXTS=8
type(receive_context), save :: contexts(MAX_CONTEXTS)
public :: jtty_rx_create, jtty_rx_destroy, jtty_rx_begin, jtty_rx_process
public :: jtty_rx_next_required_sample, jtty_rx_next_search_sample
public :: jtty_rx_take_updates, jtty_rx_end
contains
logical function valid_handle(handle)
integer(c_int), intent(in) :: handle
valid_handle=.false.
if(handle.lt.1 .or. handle.gt.MAX_CONTEXTS) return
valid_handle=contexts(handle)%allocated
end function valid_handle
! DSP scratch is shared; exchange only persistent reception state between whole steps.
subroutine exchange_state(state)
type(receive_state), intent(inout) :: state
type(message_assembly) :: active_swap(MAX_ACTIVE_MESSAGES)
type(frame_fingerprint) :: recent_swap(MAX_RECENT_FRAMES)
type(message_update), allocatable :: pending_swap(:)
integer :: value
active_swap=active_messages
active_messages=state%active
state%active=active_swap
recent_swap=recent_frames
recent_frames=state%recent
state%recent=recent_swap
call move_alloc(pending_updates,pending_swap)
call move_alloc(state%pending,pending_updates)
call move_alloc(pending_swap,state%pending)
value=nactive
nactive=state%active_count
state%active_count=value
value=nrecent
nrecent=state%recent_count
state%recent_count=value
value=npending
npending=state%pending_count
state%pending_count=value
value=pending_first
pending_first=state%pending_head
state%pending_head=value
value=ndecodes
ndecodes=state%decode_count
state%decode_count=value
end subroutine exchange_state
integer(c_int) function jtty_rx_create() bind(C,name='jtty_rx_create')
integer :: i
jtty_rx_create=0
do i=1,MAX_CONTEXTS
if(contexts(i)%allocated) cycle
contexts(i)=receive_context()
contexts(i)%allocated=.true.
jtty_rx_create=i
return
enddo
end function jtty_rx_create
subroutine jtty_rx_destroy(handle) bind(C,name='jtty_rx_destroy')
integer(c_int), value :: handle
if(.not.valid_handle(handle)) return
contexts(handle)=receive_context()
end subroutine jtty_rx_destroy
subroutine jtty_rx_begin(handle,session_id,grid_origin,first_search,nsps) bind(C,name='jtty_rx_begin')
integer(c_int), value :: handle,nsps
integer(c_int64_t), value :: session_id,grid_origin,first_search
integer(int64) :: start,step
if(.not.valid_handle(handle)) return
call jtty_rx_end(handle,UPDATE_RECEPTION_ENDED)
if(nsps.ne.240 .and. nsps.ne.320 .and. nsps.ne.384 .and. nsps.ne.480) return
step=int(59*nsps/4,int64)
start=max(first_search,grid_origin)
start=start+modulo(grid_origin-start,step)
contexts(handle)%session_id=session_id
contexts(handle)%grid_origin=grid_origin
contexts(handle)%first_search=start
contexts(handle)%next_search=start
contexts(handle)%nsps=nsps
contexts(handle)%state%active_count=0
contexts(handle)%state%recent_count=0
contexts(handle)%state%decode_count=0
contexts(handle)%retro=receive_retro_state()
contexts(handle)%running=.true.
end subroutine jtty_rx_begin
integer(c_int64_t) function jtty_rx_next_search_sample(handle) bind(C,name='jtty_rx_next_search_sample')
integer(c_int), value :: handle
jtty_rx_next_search_sample=-1_c_int64_t
if(.not.valid_handle(handle)) return
jtty_rx_next_search_sample=contexts(handle)%next_search
end function jtty_rx_next_search_sample
integer(c_int64_t) function jtty_rx_next_required_sample(handle) bind(C,name='jtty_rx_next_required_sample')
integer(c_int), value :: handle
integer(int64) :: step
jtty_rx_next_required_sample=-1_c_int64_t
if(.not.valid_handle(handle)) return
step=int(59*contexts(handle)%nsps/4,int64)
jtty_rx_next_required_sample=max(contexts(handle)%first_search, &
contexts(handle)%next_search-int(MAX_RETRO_STEPS,int64)*step)
end function jtty_rx_next_required_sample
integer(c_int) function jtty_rx_process(handle,pcm,count,first_sample,stop_sample,max_steps, &
nfa,nfb,f0,ftol) bind(C,name='jtty_rx_process')
integer(c_int), value :: handle,count,max_steps,nfa,nfb
integer(c_int16_t), intent(in) :: pcm(*)
integer(c_int64_t), value :: first_sample,stop_sample
real(c_float), value :: f0,ftol
integer :: nframe,nchunk,step,index,signal,retro_step
integer(int64) :: limit,old_origin,old_first_search,search
logical :: old_explicit,performed
jtty_rx_process=-1
if(.not.valid_handle(handle)) return
if(.not.contexts(handle)%running .or. count.lt.0) return
if(first_sample.gt.jtty_rx_next_required_sample(handle)) then
jtty_rx_process=-2
return
endif
jtty_rx_process=0
if(max_steps.le.0 .or. count.eq.0) return
nframe=59*contexts(handle)%nsps
nchunk=nframe+nframe/4
step=nframe/4
limit=min(stop_sample,first_sample+int(count,int64))
old_origin=sample_origin
old_first_search=first_search_sample
old_explicit=explicit_receive_context
sample_origin=first_sample
first_search_sample=contexts(handle)%first_search
explicit_receive_context=.true.
call exchange_state(contexts(handle)%state)
do while(jtty_rx_process.lt.max_steps)
performed=.false.
do while(contexts(handle)%retro%active .and. .not.performed)
signal=contexts(handle)%retro%signal
retro_step=contexts(handle)%retro%step
if(signal.gt.contexts(handle)%retro%count) then
contexts(handle)%retro=receive_retro_state()
contexts(handle)%next_search=contexts(handle)%next_search+int(step,int64)
exit
endif
contexts(handle)%retro%step=retro_step+1
if(retro_step.gt.MAX_RETRO_STEPS) then
contexts(handle)%retro%signal=signal+1
contexts(handle)%retro%step=1
cycle
endif
search=contexts(handle)%next_search-int(retro_step*step,int64)
if(search.lt.contexts(handle)%first_search) cycle
index=int(search-first_sample)+1
if(index.lt.1 .or. index+nchunk-1.gt.count) then
jtty_rx_process=-2
exit
endif
interferer_pending=.true.
interferer_f1=contexts(handle)%retro%frequency(signal)
interferer_tsync=contexts(handle)%retro%sync_time(signal)
interferer_payload=contexts(handle)%retro%payload(:,signal)
call jtty_mdecode(index,0,pcm(index),nchunk,contexts(handle)%nsps,-1, &
nfa,nfb,f0,ftol,4.6)
jtty_rx_process=jtty_rx_process+1
performed=.true.
enddo
if(jtty_rx_process.lt.0) exit
if(performed) cycle
if(contexts(handle)%next_search+int(nchunk,int64).gt.limit) exit
index=int(contexts(handle)%next_search-first_sample)+1
call prune_receive_state(sample_time(index),real(nframe)/12000.0)
interferer_pending=.false.
call jtty_mdecode(index,0,pcm(index),nchunk,contexts(handle)%nsps,-1, &
nfa,nfb,f0,ftol,4.6)
jtty_rx_process=jtty_rx_process+1
if(nsubtracted.gt.0) then
contexts(handle)%retro%active=.true.
contexts(handle)%retro%count=nsubtracted
contexts(handle)%retro%signal=1
contexts(handle)%retro%step=1
contexts(handle)%retro%frequency(1:nsubtracted)=subtracted_f1(1:nsubtracted)
contexts(handle)%retro%sync_time(1:nsubtracted)=subtracted_tsync(1:nsubtracted)
contexts(handle)%retro%payload(:,1:nsubtracted)=subtracted_payload(:,1:nsubtracted)
else
contexts(handle)%next_search=contexts(handle)%next_search+int(step,int64)
endif
enddo
call exchange_state(contexts(handle)%state)
sample_origin=old_origin
first_search_sample=old_first_search
explicit_receive_context=old_explicit
end function jtty_rx_process
subroutine jtty_rx_end(handle,reason) bind(C,name='jtty_rx_end')
integer(c_int), value :: handle,reason
integer :: i,terminal
if(.not.valid_handle(handle)) return
if(.not.contexts(handle)%running) return
terminal=UPDATE_RECEPTION_ENDED
if(reason.eq.UPDATE_EXPIRED) terminal=UPDATE_EXPIRED
call exchange_state(contexts(handle)%state)
do i=1,nactive
call queue_message_update(active_messages(i),.false.,terminal)
enddo
call reset_decode_search_state()
call exchange_state(contexts(handle)%state)
contexts(handle)%retro=receive_retro_state()
contexts(handle)%running=.false.
end subroutine jtty_rx_end
integer(c_int) function jtty_rx_take_updates(handle,capacity,text,ids,frequencies,starts,latest,terminals,snrs) &
bind(C,name='jtty_rx_take_updates')
integer(c_int), value :: handle,capacity
character(kind=c_char), intent(out) :: text(*)
integer(c_int64_t), intent(out) :: ids(*)
real(c_float), intent(out) :: frequencies(*)
real(c_double), intent(out) :: starts(*),latest(*)
integer(c_int), intent(out) :: terminals(*)
integer(c_int), intent(out) :: snrs(*)
character(len=80) :: message
integer :: i,j,index
jtty_rx_take_updates=0
if(.not.valid_handle(handle) .or. capacity.le.0) return
associate(state=>contexts(handle)%state)
jtty_rx_take_updates=min(capacity,state%pending_count)
do i=1,jtty_rx_take_updates
index=state%pending_head+i-1
message=display_message_text(state%pending(index)%decoded)
do j=1,80
text((i-1)*80+j)=message(j:j)
enddo
ids(i)=state%pending(index)%message_id
frequencies(i)=state%pending(index)%f1
starts(i)=state%pending(index)%start_tsync
latest(i)=state%pending(index)%latest_tsync
terminals(i)=state%pending(index)%terminal
snrs(i)=nint(state%pending(index)%snr)
enddo
state%pending_head=state%pending_head+jtty_rx_take_updates
state%pending_count=state%pending_count-jtty_rx_take_updates
if(state%pending_count.eq.0) then
state%pending_head=1
if(allocated(state%pending)) then
if(size(state%pending).gt.MAX_ACTIVE_MESSAGES) then
deallocate(state%pending)
allocate(state%pending(MAX_ACTIVE_MESSAGES))
endif
endif
endif
end associate
end function jtty_rx_take_updates
end module jtty_receive_context
|