Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 halyavin 1
;
2
;    TFTP Wave Player
3
;
4
;    Compile with FASM for Menuet
5
;
6
;
7
;    12.7.2002 - Audio system calls by VT
8
;
9
 
10
use32
11
 
12
                org     0x0
13
 
14
                db      'MENUET00'              ; 8 byte id
15
                dd      38                      ; required os
16
                dd      START                   ; program start
17
                dd      I_END                   ; program image size
18
                dd      0x100000                ; required amount of memory
19
                dd      0x00000000              ; reserved=no extended header
20
 
21
include 'lang.inc'
22
include 'macros.inc'
23
 
24
delay      dd  145
25
wait_for   dd  0x0
26
 
27
START:                          ; start of execution
28
 
29
    mov  dword [prompt], p9
30
    mov  dword [promptlen], p9len - p9
31
 
32
    call draw_window            ; at first, draw the window
33
 
34
still:
35
 
36
    mov  eax,10                 ; wait here for event
37
    int  0x40
38
 
39
    cmp  eax,1                  ; redraw request ?
40
    jz   red
41
    cmp  eax,2                  ; key in buffer ?
42
    jz   key
43
    cmp  eax,3                  ; button in buffer ?
44
    jz   button
45
 
46
    jmp  still
47
 
48
red:                           ; redraw
49
    call draw_window
50
    jmp  still
51
 
52
key:                           ; Keys are not valid at this part of the
53
    mov  eax,2                  ; loop. Just read it and ignore
54
    int  0x40
55
    jmp  still
56
 
57
button:                        ; button
58
    mov  eax,17                 ; get id
59
    int  0x40
60
 
61
    cmp  ah,1                   ; button id=1 ?
62
    jnz  noclose
63
 
64
 
65
    ; close socket before exiting
66
 mov  eax, 53
67
 mov  ebx, 1
68
 mov  ecx, [socketNum]
69
    int   0x40
70
 
71
 mov  [socketNum], dword 0
72
 
73
 
74
    mov  eax,0xffffffff         ; close this program
75
    int  0x40
76
 
77
noclose:
78
    cmp  ah,2                   ; copy file to local machine?
79
    jnz  nocopyl
80
 
81
    mov   dword [prompt], p5
82
    mov  dword [promptlen], p5len - p5
83
    call  draw_window            ;
84
 
85
    ; Copy File from Remote Host to this machine
86
    call translateData  ; Convert Filename & IP address
87
    mov  edi, tftp_filename + 1
88
    mov  [edi], byte 0x01 ; setup tftp msg
89
    call copyFromRemote
90
 
91
    jmp  still
92
 
93
nocopyl:
94
 
95
 
96
    cmp  ah,4
97
    jz   f1
98
    cmp  ah,5
99
    jz   f2
100
    jmp  nof12
101
 
102
  f1:
103
    mov  [addr],dword source
104
    mov  [ya],dword 35
105
    jmp  rk
106
 
107
  f2:
108
    mov  [addr],dword destination
109
    mov  [ya],dword 35+16
110
 
111
  rk:
112
    mov  ecx,15
113
    mov  edi,[addr]
114
    mov  al,' '
115
    rep  stosb
116
 
117
    call print_text
118
 
119
    mov  edi,[addr]
120
 
121
  f11:
122
    mov  eax,10
123
    int  0x40
124
    cmp  eax,2
125
    jz   fbu
126
    jmp  still
127
  fbu:
128
    mov  eax,2
129
    int  0x40  ; get key
130
    shr  eax,8
131
    cmp  eax,8
132
    jnz  nobs
133
    cmp  edi,[addr]
134
    jz   f11
135
    sub  edi,1
136
    mov  [edi],byte ' '
137
    call print_text
138
    jmp  f11
139
  nobs:
140
    cmp  eax,dword 31
141
    jbe  f11
142
    cmp  eax,dword 95
143
    jb   keyok
144
    sub  eax,32
145
  keyok:
146
    mov  [edi],al
147
 
148
    call print_text
149
 
150
    add  edi,1
151
    mov  esi,[addr]
152
    add  esi,15
153
    cmp  esi,edi
154
    jnz  f11
155
 
156
    jmp  still
157
 
158
print_text:
159
 
160
    mov  eax,13
161
    mov  ebx,103*65536+15*6
162
    mov  ecx,[ya]
163
    shl  ecx,16
164
    mov  cx,8
165
    mov  edx,0x224466
166
    int  0x40
167
 
168
    mov  eax,4
169
    mov  ebx,103*65536
170
    add  ebx,[ya]
171
    mov  ecx,0xffffff
172
    mov  edx,[addr]
173
    mov  esi,15
174
    int  0x40
175
 
176
    ret
177
 
178
 
179
  nof12:
180
    jmp  still
181
 
182
 
183
;***************************************************************************
184
;   Function
185
;      translateData
186
;
187
;   Description
188
;      Coverts the filename and IP address typed in by the user into
189
;      a format suitable for the IP layer.
190
;
191
;    The filename, in source, is converted and stored in tftp_filename
192
;      The host ip, in destination, is converted and stored in tftp_IP
193
;
194
;***************************************************************************
195
translateData:
196
 
197
 ; first, build up the tftp command string. This includes the filename
198
 ; and the transfer protocol
199
 
200
 
201
 ; First, write 0,0
202
 mov  al, 0
203
 mov  edi, tftp_filename
204
 mov  [edi], al
205
 inc  edi
206
 mov  [edi], al
207
 inc  edi
208
 
209
 ; Now, write the file name itself, and null terminate it
210
 mov  ecx, 15
211
 mov  ah, ' '
212
 mov  esi, source
213
 
214
td001:
215
 lodsb
216
 stosb
217
 cmp  al, ah
218
 loopnz td001
219
 
220
 cmp  al,ah  ; Was the entire buffer full of characters?
221
 jne  td002
222
 dec  edi   ; No - so remove ' ' character
223
 
224
td002:
225
 mov  [edi], byte 0
226
 inc  edi
227
 mov  [edi], byte 'O'
228
 inc  edi
229
 mov  [edi], byte 'C'
230
 inc  edi
231
 mov  [edi], byte 'T'
232
 inc  edi
233
 mov  [edi], byte 'E'
234
 inc  edi
235
 mov  [edi], byte 'T'
236
 inc  edi
237
 mov  [edi], byte 0
238
 
239
 mov  esi, tftp_filename
240
 sub  edi, esi
241
 mov  [tftp_len], edi
242
 
243
 
244
 ; Now, convert the typed IP address into a real address
245
 ; No validation is done on the number entered
246
 ; ip addresses must be typed in normally, eg
247
 ; 192.1.45.24
248
 
249
 xor  eax, eax
250
 mov  dh, 10
251
 mov  dl, al
252
 mov  [tftp_IP], eax
253
 
254
 ; 192.168.24.1   1.1.1.1       1. 9.2.3.
255
 
256
 mov  esi, destination
257
 mov  edi, tftp_IP
258
 
259
 mov  ecx, 4
260
 
261
td003:
262
 lodsb
263
 sub  al, '0'
264
 add  dl, al
265
 lodsb
266
 cmp  al, '.'
267
 je  ipNext
268
 cmp  al, ' '
269
 je  ipNext
270
 mov  dh, al
271
 sub  dh, '0'
272
 mov  al, 10
273
 mul  dl
274
 add  al, dh
275
 mov  dl, al
276
 lodsb
277
 cmp  al, '.'
278
 je  ipNext
279
 cmp  al, ' '
280
 je  ipNext
281
 mov  dh, al
282
 sub  dh, '0'
283
 mov  al, 10
284
 mul  dl
285
 add  al, dh
286
 mov  dl, al
287
 lodsb
288
 
289
ipNext:
290
 mov  [edi], dl
291
 inc  edi
292
 mov  dl, 0
293
 loop td003
294
 
295
 ret
296
 
297
 
298
 
299
;***************************************************************************
300
;   Function
301
;      copyFromRemote
302
;
303
;   Description
304
;
305
;***************************************************************************
306
copyFromRemote:
307
 
308
 mov  eax,0x20000-512
309
 mov  [fileposition], eax
310
 
311
 ; Get a random # for the local socket port #
312
 mov  eax, 3
313
 int  0x40
314
 mov  ecx, eax
315
 shr  ecx, 8    ; Set up the local port # with a random #
316
 
317
   ; open socket
318
 mov  eax, 53
319
 mov  ebx, 0
320
 mov  edx, 69    ; remote port
321
 mov  esi, [tftp_IP]  ; remote IP ( in intenet format )
322
 int  0x40
323
 
324
 mov  [socketNum], eax
325
 
326
 ; make sure there is no data in the socket - there shouldn't be..
327
 
328
cfr001:
329
 mov  eax, 53
330
 mov  ebx, 3
331
 mov  ecx, [socketNum]
332
 int  0x40    ; read byte
333
 
334
 mov  eax, 53
335
 mov  ebx, 2
336
 mov  ecx, [socketNum]
337
 int  0x40    ; any more data?
338
 
339
 cmp  eax, 0
340
 jne  cfr001    ; yes, so get it
341
 
342
 ; Now, request the file
343
 mov  eax, 53
344
 mov  ebx, 4
345
 mov  ecx, [socketNum]
346
 mov  edx, [tftp_len]
347
 mov  esi, tftp_filename
348
 int  0x40
349
 
350
cfr002:
351
 
352
    mov  eax,23                 ; wait here for event
353
    mov  ebx,1                  ; Time out after 10ms
354
    int  0x40
355
 
356
    cmp  eax,1                  ; redraw request ?
357
    je   cfr003
358
    cmp  eax,2                  ; key in buffer ?
359
    je   cfr004
360
    cmp  eax,3                  ; button in buffer ?
361
    je   cfr005
362
 
363
    ; Any data to fetch?
364
 mov  eax, 53
365
 mov  ebx, 2
366
 mov  ecx, [socketNum]
367
 int   0x40
368
 
369
 cmp  eax, 0
370
 je  cfr002
371
 
372
 push eax     ; eax holds # chars
373
 
374
 ; Update the text on the display - once
375
 mov  eax, [prompt]
376
 cmp  eax, p3
377
 je  cfr008
378
 mov   dword [prompt], p3
379
 mov  dword [promptlen], p3len - p3
380
 call  draw_window            ;
381
 
382
cfr008:
383
 ; we have data - this will be a tftp frame
384
 
385
 ; read first two bytes - opcode
386
 mov  eax, 53
387
 mov  ebx, 3
388
 mov  ecx, [socketNum]
389
 int  0x40   ; read byte
390
 
391
 mov  eax, 53
392
 mov  ebx, 3
393
 mov  ecx, [socketNum]
394
 int  0x40   ; read byte
395
 
396
 pop  eax
397
 ; bl holds tftp opcode. Can only be 3 (data) or 5 ( error )
398
 
399
 cmp  bl, 3
400
 jne  cfrerr
401
 
402
 push eax
403
 
404
 ; do data stuff. Read block #. Read data. Send Ack.
405
 mov  eax, 53
406
 mov  ebx, 3
407
 mov  ecx, [socketNum]
408
 int  0x40   ; read byte
409
 
410
 mov  [blockNumber], bl
411
 
412
 mov  eax, 53
413
 mov  ebx, 3
414
 mov  ecx, [socketNum]
415
 int  0x40   ; read byte
416
 
417
 mov  [blockNumber+1], bl
418
 
419
cfr007:
420
 mov  eax, 53
421
 mov  ebx, 3
422
 mov  ecx, [socketNum]
423
 int  0x40   ; read byte
424
 
425
 mov  esi, [fileposition]
426
 mov  [esi], bl
427
 mov  [esi+1],bl
428
 add  dword [fileposition],2
429
 
430
 mov  eax, 53
431
 mov  ebx, 2
432
 mov  ecx, [socketNum]
433
 int  0x40   ; any more data?
434
 
435
 cmp  eax, 0
436
 jne  cfr007  ; yes, so get it
437
 
438
 cmp  [fileposition],0x20000+0xffff
439
 jb   get_more_stream
440
 
441
wait_more:
442
 
443
 mov  eax,5    ; wait for correct timer position
444
               ; to trigger new play block
445
 mov  ebx,1
446
 int  0x40
447
 
448
 mov  eax,26
449
 mov  ebx,9
450
 int  0x40
451
 
452
 cmp  eax,[wait_for]
453
 jb   wait_more
454
 
455
 add  eax,[delay]
456
 mov  [wait_for],eax
457
 
458
 mov  esi,0x20000
459
 mov  edi,0x10000
460
 mov  ecx,65536
461
 cld
462
 rep  movsb
463
 
464
 mov  eax,55
465
 mov  ebx,0
466
 mov  ecx,0x10000
467
 int  0x40
468
 
469
 mov  eax,55
470
 mov  ebx,1
471
 int  0x40
472
 
473
 mov  [fileposition],0x20000
474
 
475
get_more_stream:
476
 
477
 ; write the block number into the ack
478
 mov  al, [blockNumber]
479
 mov  [ack + 2], al
480
 
481
 mov  al, [blockNumber+1]
482
 mov  [ack + 3], al
483
 
484
 ; send an 'ack'
485
 mov  eax, 53
486
 mov  ebx, 4
487
 mov  ecx, [socketNum]
488
 mov  edx, ackLen - ack
489
 mov  esi, ack
490
 int   0x40
491
 
492
 ; If # of chars in the frame is less that 516,
493
 ; this frame is the last
494
 pop  eax
495
 cmp  eax, 516
496
 je  cfr002
497
 
498
 ; Write the file
499
 mov  eax, 33
500
 mov  ebx, source
501
 mov  edx, [filesize]
502
 mov  ecx, I_END + 512
503
 mov  esi, 0
504
 int  0x40
505
 
506
 jmp  cfrexit
507
 
508
cfrerr:
509
 ; simple implementation on error - just read all data, and return
510
 mov  eax, 53
511
 mov  ebx, 3
512
 mov  ecx, [socketNum]
513
    int   0x40    ; read byte
514
 
515
 mov  eax, 53
516
 mov  ebx, 2
517
 mov  ecx, [socketNum]
518
    int   0x40    ; any more data?
519
 
520
 cmp  eax, 0
521
 jne  cfrerr    ; yes, so get it
522
 
523
 jmp  cfr006    ; close socket and close app
524
 
525
cfr003:                         ; redraw request
526
    call draw_window
527
    jmp  cfr002
528
 
529
cfr004:                         ; key pressed
530
    mov  eax,2                  ; just read it and ignore
531
    int  0x40
532
    jmp  cfr002
533
 
534
cfr005:                        ; button
535
    mov  eax,17                 ; get id
536
    int  0x40
537
 
538
    cmp  ah,1                   ; button id=1 ?
539
    jne  cfr002     ; If not, ignore.
540
 
541
cfr006:
542
    ; close socket
543
 mov  eax, 53
544
 mov  ebx, 1
545
 mov  ecx, [socketNum]
546
    int   0x40
547
 
548
 mov  [socketNum], dword 0
549
 
550
    mov  eax,-1                 ; close this program
551
    int  0x40
552
 
553
    jmp $
554
 
555
cfrexit:
556
    ; close socket
557
 mov  eax, 53
558
 mov  ebx, 1
559
 mov  ecx, [socketNum]
560
    int   0x40
561
 
562
 mov  [socketNum], dword 0
563
 
564
    mov   dword [prompt], p4
565
    mov  dword [promptlen], p4len - p4
566
    call  draw_window            ;
567
 
568
 ret
569
 
570
 
571
 
572
 
573
;   *********************************************
574
;   *******  WINDOW DEFINITIONS AND DRAW ********
575
;   *********************************************
576
 
577
 
578
draw_window:
579
 
580
    mov  eax,12                    ; function 12:tell os about windowdraw
581
    mov  ebx,1                     ; 1, start of draw
582
    int  0x40
583
 
584
                                   ; DRAW WINDOW
585
    mov  eax,0                     ; function 0 : define and draw window
586
    mov  ebx,100*65536+230         ; [x start] *65536 + [x size]
587
    mov  ecx,100*65536+170         ; [y start] *65536 + [y size]
588
    mov  edx,0x03224466            ; color of work area RRGGBB
589
    mov  esi,0x00334455            ; color of grab bar  RRGGBB,8->color gl
590
    mov  edi,0x00ddeeff            ; color of frames    RRGGBB
591
    int  0x40
592
 
593
                                   ; WINDOW LABEL
594
    mov  eax,4                     ; function 4 : write text to window
595
    mov  ebx,8*65536+8             ; [x start] *65536 + [y start]
596
    mov  ecx,0x00ffffff            ; color of text RRGGBB
597
    mov  edx,labelt                ; pointer to text beginning
598
    mov  esi,labellen-labelt       ; text length
599
    int  0x40
600
 
601
 
602
    mov  eax,8              ; COPY BUTTON
603
    mov  ebx,20*65536+190
604
    mov  ecx,79*65536+15
605
    mov  edx,3
606
    mov  esi,0x557799
607
;    int  0x40
608
 
609
    mov  eax,8              ; DELETE BUTTON
610
    mov  ebx,20*65536+190
611
    mov  ecx,111*65536+15
612
    mov  edx,2
613
    mov  esi,0x557799
614
    int  0x40
615
 
616
    mov  eax,8
617
    mov  ebx,200*65536+10
618
    mov  ecx,34*65536+10
619
    mov  edx,4
620
    mov  esi,0x557799
621
    int  0x40
622
 
623
    mov  eax,8
624
    mov  ebx,200*65536+10
625
    mov  ecx,50*65536+10
626
    mov  edx,5
627
    mov  esi,0x557799
628
    int  0x40
629
 
630
 
631
 ; Copy the file name to the screen buffer
632
 ; file name is same length as IP address, to
633
 ; make the math easier later.
634
    cld
635
    mov  esi,source
636
    mov  edi,text+13
637
    mov  ecx,15
638
    rep  movsb
639
 
640
 
641
 ; copy the IP address to the screen buffer
642
    mov  esi,destination
643
    mov  edi,text+40+13
644
    mov  ecx,15
645
    rep  movsb
646
 
647
  ; copy the prompt to the screen buffer
648
    mov  esi,[prompt]
649
    mov  edi,text+280
650
    mov  ecx,[promptlen]
651
    rep  movsb
652
 
653
    ; Re-draw the screen text
654
    cld
655
    mov  ebx,25*65536+35           ; draw info text with function 4
656
    mov  ecx,0xffffff
657
    mov  edx,text
658
    mov  esi,40
659
  newline:
660
    mov  eax,4
661
    int  0x40
662
    add  ebx,16
663
    add  edx,40
664
    cmp  [edx],byte 'x'
665
    jnz  newline
666
 
667
 
668
    mov  eax,12                    ; function 12:tell os about windowdraw
669
    mov  ebx,2                     ; 2, end of draw
670
    int  0x40
671
 
672
    ret
673
 
674
 
675
; DATA AREA
676
 
677
source       db  'HEAT8M22.WAV   '
678
destination  db  '192.168.1.24   '
679
 
680
 
681
tftp_filename:  times 15 + 9 db 0
682
tftp_IP:   dd 0
683
tftp_len:   dd 0
684
 
685
addr  dd  0x0
686
ya    dd  0x0
687
 
688
fileposition dd 0 ; Points to the current point in the file
689
filesize  dd 0 ; The number of bytes written / left to write
690
fileblocksize dw 0 ; The number of bytes to send in this frame
691
 
692
text:
693
    db 'SOURCE FILE: xxxxxxxxxxxxxxx            '
694
    db 'HOST IP ADD: xxx.xxx.xxx.xxx            '
695
    db '                                        '
696
    db 'WAVE FORMAT: 8 BIT,MONO,22050HZ         '
697
    db '                                        '
698
    db '     SERVER -> PLAY FILE                '
699
    db '                                        '
700
    db '                                        '
701
    db 'x <- END MARKER, DONT DELETE            '
702
 
703
 
704
labelt:
705
    db   'TFTP Wave Player'
706
labellen:
707
 
708
 
709
prompt: dd 0
710
promptlen: dd 0
711
 
712
 
713
p1:  db 'Waiting for Command '
714
p1len:
715
 
716
p9:  db 'Define SB with setup'
717
p9len:
718
 
719
p2:  db 'Sending File        '
720
p2len:
721
 
722
p3:  db 'Playing File        '
723
p3len:
724
 
725
p4:  db 'Complete            '
726
p4len:
727
 
728
p5:  db 'Contacting Host...  '
729
p5len:
730
 
731
p6:  db 'File not found.     '
732
p6len:
733
 
734
ack:
735
 db 00,04,0,1
736
ackLen:
737
 
738
socketNum:
739
 dd 0
740
 
741
blockNumber:
742
 dw 0
743
 
744
; This must be the last part of the file, because the blockBuffer
745
; continues at I_END.
746
blockBuffer:
747
 db 00, 03, 00, 01
748
I_END:
749