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