Skip to content

Commit bf6858b

Browse files
committed
3.0.2 add non-blocking read network_read_nb - CHANGES TO SIGNATURES
this should be the base version for the 3 series of fujinet-lib as it contains some function sig changes to library
1 parent 1d730cc commit bf6858b

File tree

12 files changed

+189
-33
lines changed

12 files changed

+189
-33
lines changed

Changelog.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@
22

33
## [Unreleased]
44

5+
## [3.0.2] - 2024-03-24
6+
7+
This should be considered the BASE 3 release.
8+
9+
- network_read signature changed to indicate the bytes fetched instead of status, and errors returned as negative
10+
- network_json_query signature similarly changed
11+
- added network_read_nb for non-blocking reads that only fetch up to the amount available in the buffer.
12+
It is up to the client to loop to fetch the total required rather than the library.
13+
514
## [3.0.1] - 2024-03-19
615

16+
This release has been removed, as starting with 3.0.2 the network_read signature has changed and I didn't want to bump to 4.0
17+
given noone has used 3 yet. Sorry purists!
18+
719
Complete Apple2 fuji device support for CONFIG.
820

921
- [apple2] fixes for mount_host_slot and mount_disk_image
@@ -14,6 +26,12 @@ Complete Apple2 fuji device support for CONFIG.
1426

1527
## [3.0.0] - 2024-03-18
1628

29+
This release has been removed, as starting with 3.0.2 the network_read signature has changed and I didn't want to bump to 4.0
30+
given noone has used 3 yet. Sorry purists!
31+
32+
- Contains signature breaking changes for:
33+
- network_read, fuji_scan_for_networks, fuji_get_hsio_index, fuji_get_wifi_status, fuji_get_directory_position,
34+
- fuji_get_device_slots, fuji_get_host_slots
1735
- The great rename fn_io_ to fuji_ to reflect device in FN being used, version bump to 3.0.0 to reflect huge name changes
1836
- Made most fuji_ functions return success status rather than void.
1937
- fuji routines to Apple2 - all but hashing/base64 and appkey stuff

apple2/src/fn_network/network_json_query.s

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
.include "macros.inc"
2626
.include "zp.inc"
2727

28-
; uint8_t network_json_query(char *devicespec, char *query, char *s);
28+
; int16_t network_json_query(char *devicespec, char *query, char *s);
2929
;
3030
.proc _network_json_query
3131
axinto tmp5 ; save string output location
@@ -41,7 +41,13 @@
4141

4242
; remove 2 word args on stack, and return an error
4343
jsr incsp4
44-
jmp _bad_unit
44+
jsr _bad_unit
45+
; make A negative for an error code. X already 0.
46+
eor #$ff
47+
clc
48+
adc #$01
49+
rts
50+
4551

4652
; ----------------------------------------------------------------------------------
4753
; Query
@@ -90,7 +96,7 @@
9096
ora ptr4
9197
bne not_empty
9298

93-
; there was 0 data to read for this query
99+
; there was 0 data to read for this query, return 0 length
94100
jsr add_nul
95101
jmp return0
96102

@@ -100,7 +106,13 @@ error:
100106
sta ptr4 ; store error while we deal with nulling string
101107
jsr add_nul
102108
lda ptr4
103-
jmp _fn_error
109+
jsr _fn_error
110+
; make it negative
111+
eor #$ff
112+
clc
113+
adc #$01
114+
rts
115+
104116

105117
add_nul:
106118
; tmp5 points to current target string, send back an empty string
@@ -168,5 +180,8 @@ not_empty:
168180
tya
169181
sta (tmp5), y
170182

171-
jmp return0 ; FN_ERR_OK
183+
; return the length held in ptr4 - does this need adjusting for 9b/0d/0a?
184+
setax ptr4
185+
rts
186+
172187
.endproc

atari/src/fn_network/network_close.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.export _network_close
22

33
.import _bus
4-
.import _fn_device_error
54
.import _bus_status
5+
.import _fn_device_error
66
.import _network_unit
77
.import copy_network_cmd_data
88
.import popax

atari/src/fn_network/network_ioctl.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
jsr _bus
5959
lda IO_DCB::dunit
60-
jmp _bus_status ; set return to the status
60+
jmp _bus_status ; set return to the status
6161

6262
@args_error:
6363
; increase SP by Y to clear the params we received, and return error

atari/src/fn_network/network_json_query.s

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
.include "macros.inc"
2222
.include "zp.inc"
2323

24-
; uint8_t network_json_query(char *devicespec, char *query, char *s);
24+
; int16_t network_json_query(char *devicespec, char *query, char *s);
2525
;
2626
; TODO: how do we deal with very large json results? Maybe interface with network_read, which can handle them.
2727
; Or does sio_read work with any max size?
@@ -82,14 +82,20 @@
8282

8383
no_data:
8484
jsr add_nul
85-
jmp return0 ; FN_ERR_OK
85+
; return the byte count
86+
ldx _fn_bytes_read+1
87+
lda _fn_bytes_read
88+
rts
8689

8790
error:
8891
sta tmp1 ; save error code
89-
jsr add_nul
92+
jsr add_nul ; sets the string to empty
93+
; make the error negative
94+
ldx #$00
9095
lda tmp1
91-
; error already gone through standardisation, so just return
92-
; The caller should check error codes, and ignore any changes to string that may have happened
96+
eor #$ff
97+
clc
98+
adc #$01
9399
rts
94100

95101
add_nul:

atari/src/fn_network/network_status.s

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
.import _fn_error
77
.import _network_unit
88
.import copy_network_cmd_data
9-
.import incsp4
109
.import popa
1110
.import popax
12-
.import return0
13-
.import return1
1411

1512
.include "atari.inc"
1613
.include "device.inc"

common/inc/macros.inc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@
6161
; use CA65 function calling conventions, passing values into SP via pushax
6262
.macro pushax arg1
6363
.if (.match (.left (1, {arg1}), #))
64-
lda #<(.right (.tcount ({arg1})-1, {arg1}))
6564
ldx #>(.right (.tcount ({arg1})-1, {arg1}))
65+
lda #<(.right (.tcount ({arg1})-1, {arg1}))
6666
jsr pushax
6767
.else
68-
lda arg1
6968
ldx 1+(arg1)
69+
lda arg1
7070
jsr pushax
7171
.endif
7272
.endmacro
@@ -78,20 +78,21 @@
7878
.endmacro
7979

8080
; same as pushax, but without the push to SP, preparing the args directly for consumption
81+
; always set X first, then A so flags are set based on A
8182
.macro setax arg1
8283
.if (.match (.left (1, {arg1}), #))
83-
lda #<(.right (.tcount ({arg1})-1, {arg1}))
8484
ldx #>(.right (.tcount ({arg1})-1, {arg1}))
85+
lda #<(.right (.tcount ({arg1})-1, {arg1}))
8586
.else
86-
lda arg1
8787
ldx 1+(arg1)
88+
lda arg1
8889
.endif
8990
.endmacro
9091

9192
.macro popax arg1
9293
jsr popax
93-
sta arg1
9494
stx 1+(arg1)
95+
sta arg1
9596
.endmacro
9697

9798
.macro popa arg1
@@ -100,8 +101,8 @@
100101
.endmacro
101102

102103
.macro axinto arg1
103-
sta arg1
104104
stx 1+(arg1)
105+
sta arg1
105106
.endmacro
106107

107108
; adw: adds word values together, or immediate bytes.

common/src/bss_data.s renamed to common/src/fn_data.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
.export _fn_network_bw
55
.export _fn_network_conn
66

7+
; -------------------------------------------------
8+
; BSS
9+
; -------------------------------------------------
710
.bss
811

912
; device specific error value, e.g. SmartPort specific errors

common/src/network_read.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <stdint.h>
2-
#include <stdio.h>
32
#include <string.h>
43
#include <ctype.h>
54

@@ -22,7 +21,7 @@
2221
#define MAX_READ_SIZE 512
2322

2423

25-
uint8_t network_read(char *devicespec, uint8_t *buf, uint16_t len)
24+
int16_t network_read(char *devicespec, uint8_t *buf, uint16_t len)
2625
{
2726
uint8_t r = 0;
2827
uint16_t fetch_size = 0;
@@ -55,7 +54,6 @@ uint8_t network_read(char *devicespec, uint8_t *buf, uint16_t len)
5554
unit = network_unit(devicespec);
5655
#endif
5756

58-
5957
while (1) {
6058
// exit condition
6159
if (amount_left == 0) break;
@@ -67,12 +65,14 @@ uint8_t network_read(char *devicespec, uint8_t *buf, uint16_t len)
6765
r = network_status_no_clr(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error);
6866
#endif
6967

70-
if (r != 0) return r;
68+
// check if the status failed
69+
if (r != 0) return -r;
7170

7271
// EOF hit, exit reading
7372
if (fn_network_error == 136) break;
7473

75-
// we are waiting for bytes to become available while still connected. Causes tight loop if there's a long delay reading from network into FN
74+
// we are waiting for bytes to become available while still connected.
75+
// Causes tight loop if there's a long delay reading from network into FN, so we may see lots of status requests
7676
if (fn_network_bw == 0 && fn_network_conn == 1) {
7777
continue;
7878
}
@@ -100,5 +100,5 @@ uint8_t network_read(char *devicespec, uint8_t *buf, uint16_t len)
100100

101101
// do this here at the end, not in the loop so sio_read for atari can continue to set fn_bytes_read for short reads.
102102
fn_bytes_read = total_read;
103-
return 0;
103+
return total_read;
104104
}

common/src/network_read_nb.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <ctype.h>
5+
6+
#include "fujinet-network.h"
7+
8+
#ifdef BUILD_ATARI
9+
#include "fujinet-network-atari.h"
10+
#include "fujinet-bus-atari.h"
11+
#endif
12+
13+
#ifdef BUILD_APPLE2
14+
#include "fujinet-network-apple2.h"
15+
#include "fujinet-bus-apple2.h"
16+
#include "apple2/src/bus/inc/sp.h"
17+
#endif
18+
19+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
20+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
21+
22+
#define MAX_READ_SIZE 512
23+
24+
25+
int16_t network_read_nb(char *devicespec, uint8_t *buf, uint16_t len)
26+
{
27+
uint8_t r = 0;
28+
uint16_t fetch_size = 0;
29+
#ifdef BUILD_ATARI
30+
uint8_t unit = 0;
31+
#endif
32+
33+
if (len == 0 || buf == NULL) {
34+
#ifdef BUILD_ATARI
35+
return -fn_error(132); // invalid command
36+
#endif
37+
#ifdef BUILD_APPLE2
38+
return -fn_error(SP_ERR_BAD_CMD);
39+
#endif
40+
}
41+
42+
#ifdef BUILD_APPLE2
43+
// check we have the SP network value
44+
if (sp_network == 0) {
45+
return -fn_error(SP_ERR_BAD_UNIT);
46+
}
47+
#endif
48+
49+
fn_bytes_read = 0;
50+
fn_device_error = 0;
51+
52+
#ifdef BUILD_ATARI
53+
unit = network_unit(devicespec);
54+
#endif
55+
56+
#ifdef BUILD_ATARI
57+
r = network_status_unit(unit, &fn_network_bw, &fn_network_conn, &fn_network_error);
58+
#endif
59+
#ifdef BUILD_APPLE2
60+
r = network_status_no_clr(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error);
61+
#endif
62+
// check if the status failed
63+
if (r != 0) return -r;
64+
65+
// EOF hit, exit reading
66+
if (fn_network_error == 136) return 0;
67+
68+
// we are waiting for bytes to become available while still connected, so no data can be read
69+
if (fn_network_bw == 0 && fn_network_conn == 1) {
70+
return 0;
71+
}
72+
73+
fetch_size = MIN(len, fn_network_bw);
74+
75+
#ifdef BUILD_APPLE2
76+
// need to validate this is only required for apple
77+
fetch_size = MIN(fetch_size, MAX_READ_SIZE);
78+
#endif
79+
80+
#ifdef BUILD_ATARI
81+
sio_read(unit, buf, fetch_size);
82+
#endif
83+
84+
#ifdef BUILD_APPLE2
85+
sp_read(sp_network, fetch_size);
86+
memcpy(buf, sp_payload, fetch_size);
87+
#endif
88+
89+
fn_bytes_read = fetch_size;
90+
91+
// reduce the bytes waiting count. if it hits 0, then we will need a new status, so clean status is then false.
92+
// This is to optimize small reads not requiring a full status request from FujiNet. TBD if it is better.
93+
// as data may have come to the device while we were reading the last lot, so we won't be able to read it until the previous reported
94+
// amount was exhausted.
95+
fn_network_bw -= fetch_size;
96+
return fetch_size;
97+
}

0 commit comments

Comments
 (0)