;NavNet request processing routines include "ti83plus.inc" include "equates.inc" include "usb8x.inc" include "settings.inc" SEGMENT Main GLOBALS ON EXTERN FinishHandlingPacket,SendAcknowledgement,SendNavNetPacket,ThrowNavNetError EXTERN DispHexHL,DispHexA,GetPacketByte,GetPacketWord,IPutS HandleAddressAssignment: ;Send an acknowledgement packet ld hl,4003h push hl pop de ld b,0 ld ix,OP1 ld (ix+0),64h ld (ix+1),01h ld (ix+2),0FFh ld (ix+3),00h ld c,4 call SendNavNetPacket ld a,errSendAddressAssignmentAck jr c,ThrowNavNetError ;Reset sequence number ld ix,(NavNetMemPtr) ld (ix+offSequenceNumber),1 set addressSent,(iy+nspire8xFlags) xor a ret RetrieveDeviceInformation: ;Acknowledge the response ld (OP2),de call SendAcknowledgement jr nc,$F ld a,errSendDeviceInfoAck jr ThrowNavNetError $$: ld (OP2+2),a ;Copy the packet's data to the buffer specified ld ix,(NavNetMemPtr) ld e,(ix+offDataOutAddr) ld d,(ix+offDataOutAddr+1) ld ix,0D000h+512+16+1 push ix pop hl ld a,03h out (5),a ld c,(ix-5) dec c ld b,0 ldir xor a out (5),a ;Let the world know we got this ld de,(OP2) ld a,(OP2+2) ld b,evDeviceInfoReceived jr FinishHandlingPacket ListenToEcho: ;Acknowledge the response ld (OP2),de call SendAcknowledgement jr nc,$F ld a,errSendEchoAck jr ThrowNavNetError $$: ld (OP2+2),a ;Do we care about the data? I mean, we sent it to ourselves, we know it's the same... ;Let the world know we got this ld de,(OP2) ld a,(OP2+2) ld b,evEchoReceived jr FinishHandlingPacket OSInstallation: ;Acknowledge the response ld (OP2),de call SendAcknowledgement jr nc,$F ld a,errSendOSInstallationAck jr ThrowNavNetError $$: ld (OP2+2),a ;Is this a status packet? ld bc,16+0 call GetPacketByte inc a jr nz,osiNotStatus ld bc,16+1 call GetPacketByte ld ix,(NavNetMemPtr) ld (ix+offErrorCode),a ;A is the status of this packet: ; 00h: success ; 0Ah: directory doesn't exist ; 0Fh: invalid directory name ; 10h: we aren't listing a directory or DirEnumInit failed, in which case...oops? ; 11h: no more files or directories in list ;Indicate that we're done with this request res sendServiceDisconnect,(iy+nspire8xFlags) jr osiRequestDone osiNotStatus: res sendServiceDisconnect,(iy+nspire8xFlags) osiRequestDone: ;Let the world know we got this ld de,(OP2) ld a,(OP2+2) ld b,evOSRequestReceived jr FinishHandlingPacket FileManagement: ;Acknowledge the response ld (OP2),de call SendAcknowledgement jr nc,$F ld a,errSendFileManagementAck jr ThrowNavNetError $$: ld (OP2+2),a ;Is this a status packet? ld bc,16+0 call GetPacketByte inc a jr nz,fmNotStatus ld bc,16+1 call GetPacketByte ld ix,(NavNetMemPtr) ld (ix+offErrorCode),a ;A is the status of this packet: ; 00h: success ; 0Ah: directory doesn't exist ; 0Fh: invalid directory name ; 10h: we aren't listing a directory or DirEnumInit failed, in which case...oops? ; 11h: no more files or directories in list or a jr nz,$F bit fileRequestComplete,(iy+navNetRequestFlags) jr nz,fileRequestDone res sendServiceDisconnect,(iy+nspire8xFlags) $$: cp 0Fh jr nz,$F ; res sendServiceDisconnect,(iy+nspire8xFlags) $$: cp 11h jr nz,fileRequestDone ;Indicate that we're done with the directory listing set fileRequestComplete,(iy+navNetRequestFlags) res sendServiceDisconnect,(iy+nspire8xFlags) res receivingDirectory,(iy+navNetRequestFlags) jr fileRequestDone fmNotStatus: ;Handle this request bit sendingFile,(iy+navNetRequestFlags) jr nz,fmSendFile bit receivingFile,(iy+navNetRequestFlags) jr nz,fmGetFile bit receivingAttributes,(iy+navNetRequestFlags) jr nz,fmGetAttributes bit receivingDirectory,(iy+navNetRequestFlags) jr z,fileRequestDone ;Is this a directory entry? ld bc,16+0 call GetPacketByte cp 10h jr nz,fileRequestDone ;This is a directory entry, so store it and move on res sendServiceDisconnect,(iy+nspire8xFlags) ;Increase our count of entries ld ix,(NavNetMemPtr) ld l,(ix+offDataOutAddr) ld h,(ix+offDataOutAddr+1) ld c,(hl) inc hl ld b,(hl) inc bc ld (hl),b dec hl ld (hl),c inc hl inc hl ;Find the next place to write a file name dec bc ld a,b or c jr z,fmNewLocationFound $$: ld a,(hl) inc hl or a jr nz,$B dec bc ld a,b or c jr nz,$B fmNewLocationFound: ex de,hl ld hl,0D000h+512+16+3 ;HL is file name from packet ;DE is destination to place file name ld a,03h out (5),a $$: ld a,(hl) ld (de),a inc hl inc de or a jr nz,$B xor a out (5),a fileRequestDone: ;Let the world know we got this ld de,(OP2) ld a,(OP2+2) ld b,evFileRequestReceived jr FinishHandlingPacket fmGetAttributes: ld ix,(NavNetMemPtr) ld e,(ix+offDataOutAddr) ld d,(ix+offDataOutAddr+1) ld hl,0D000h+512+16+1 ld bc,10 ld a,03h out (5),a ldir xor a out (5),a jr fileRequestDone fmSendFile: ;We know this is "04"/continue, so just move along res sendServiceDisconnect,(iy+nspire8xFlags) jr fileRequestDone fmGetFile: ;Determine packet type ld bc,16+0 call GetPacketByte cp 03h jr nz,fmGetFileData ;This is the file "header" res sendServiceDisconnect,(iy+nspire8xFlags) ;Get and store the file size, so we know how much is left to receive ld ix,0D000h+512+16+11 ld a,03h out (5),a ;For now, we can guarantee the file size is < 64KB. ; ld d,(ix+0) ; ld e,(ix+1) ld h,(ix+2) ld l,(ix+3) xor a out (5),a ld ix,(NavNetMemPtr) ld (ix+offFileBytesRemaining),l ld (ix+offFileBytesRemaining+1),h jr fileRequestDone fmGetFileData: res sendServiceDisconnect,(iy+nspire8xFlags) ld bc,12 call GetPacketByte dec a ld c,a ld b,0 ld ix,(NavNetMemPtr) ld l,(ix+offFileBytesRemaining) ld h,(ix+offFileBytesRemaining+1) or a sbc hl,bc ld (ix+offFileBytesRemaining),l ld (ix+offFileBytesRemaining+1),h ;Now actually save the file's contents to RAM/Flash ;First get all the data into our temporary RAM buffer ld hl,(NavNetMemPtr) ld bc,offPacketBuffer add hl,bc ex de,hl ld hl,0D000h+512+16+1 ld bc,253 ;HL is current packet data ;DE is packet buffer ;BC is number of bytes to copy push de ld a,03h out (5),a ldir xor a out (5),a ld bc,12 call GetPacketByte pop hl dec a ld b,0 ld c,a or a ;HL is packet buffer ;BC is number of data bytes received jr z,fmGetFileDataDone ld ix,(NavNetMemPtr) ld e,(ix+offDataOutAddr) ld d,(ix+offDataOutAddr+1) ld a,d or e jr z,$F ;We're storing in RAM, so just write BC bytes from buffer (HL) to destination (DE) ldir ld (ix+offDataOutAddr),e ld (ix+offDataOutAddr+1),d jr fmGetFileDataDone $$: ;We're storing in Flash, so cache BC bytes at buffer (HL) for writing ld a,3 B_CALL WriteToFlash fmGetFileDataDone: ld bc,12 call GetPacketByte cp 254 jr z,$F set fileRequestComplete,(iy+navNetRequestFlags) $$: ret GetScreenshot: ;Acknowledge the response ld (OP2),de call SendAcknowledgement ret c ;See which response we're at ld a,03h out (5),a ld ix,0D000h+512 ld b,(ix+16) ld d,(ix+16+1) ld e,(ix+16+2) ld h,(ix+16+3) ld l,(ix+16+4) xor a out (5),a dec b jr nz,gsGetRawData ;This is the screenshot header, DEHL is the RLE-encoded size ld ix,(NavNetMemPtr) ld (ix+offNumBytesLeft),l ;we're assuming DE is zero ld (ix+offNumBytesLeft+1),h ret gsGetRawData: ;TODO: Copy the packet's data to the screenshot buffer set receivingScreenshot,(iy+nspire8xFlags) ;Are we done receiving data? ld a,03h out (5),a ld ix,0D000h+512 ld c,(ix+12) ld b,0 xor a out (5),a ;Update the number of bytes left to receive call gsUpdateBytesLeft ret GetScreenshotData: ld bc,254 call gsUpdateBytesLeft jr c,gsLastPacketReceived ;We still have more to go... ret gsLastPacketReceived: res receivingScreenshot,(iy+nspire8xFlags) ld de,(OP2) ld b,0 jr FinishHandlingPacket gsUpdateBytesLeft; ;BC: number to decrease by ld ix,(NavNetMemPtr) ld l,(ix+offNumBytesLeft) ld h,(ix+offNumBytesLeft+1) or a sbc hl,bc ld (ix+offNumBytesLeft),l ld (ix+offNumBytesLeft+1),h ret