# protocol.c File Reference

`#include "config.h"`
`#include <unistd.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include <fcntl.h>`
`#include <string.h>`
`#include <errno.h>`
`#include "libnjb.h"`
`#include "protocol.h"`
`#include "byteorder.h"`
`#include "njb_error.h"`
`#include "usb_io.h"`
`#include "ioutil.h"`
`#include "defs.h"`
`#include "base.h"`
`#include "eax.h"`
`#include "songid.h"`
`#include "datafile.h"`
`#include "njbtime.h"`
`#include "playlist.h"`

## Defines
#define | NJB_STATUS(a, b)
## Functions
int | njb_init_state (njb_t *njb)
int | njb_set_library_counter (njb_t *njb, u_int64_t count)
int | njb_get_library_counter (njb_t *njb, njblibctr_t *lcount)
int | njb_ping (njb_t *njb)
int | njb_verify_last_command (njb_t *njb)
int | njb_capture (njb_t *njb, int which)
int | njb_get_track_tag_header (njb_t *njb, njbttaghdr_t *tagh, int cmd)
njb_songid_t * | njb_get_track_tag (njb_t *njb, njbttaghdr_t *tagh)
int | njb_get_playlist_header (njb_t *njb, njbplhdr_t *plh, int cmd)
njb_playlist_t * | njb_get_playlist (njb_t *njb, njbplhdr_t *plh)
int | njb_get_disk_usage (njb_t *njb, u_int64_t *total, u_int64_t *free_bytes)
int | njb_get_owner_string (njb_t *njb, owner_string name)
int | njb_set_owner_string (njb_t *njb, owner_string name)
int | njb_get_datafile_header (njb_t *njb, njbdfhdr_t *dfh, int cmd)
njb_datafile_t * | njb_get_datafile_tag (njb_t *njb, njbdfhdr_t *dfh)
u_int32_t | njb_receive_file_block (njb_t *njb, u_int32_t offset, u_int32_t bsize, void *bp)
int | njb_request_file (njb_t *njb, u_int32_t fileid)
int | njb_transfer_complete (njb_t *njb)
int | njb_send_track_tag (njb_t *njb, njbttaghdr_t *tagh, void *tag)
int | njb_send_datafile_tag (njb_t *njb, njbdfhdr_t *dfh, void *tag)
u_int32_t | njb_send_file_block (njb_t *njb, void *data, u_int32_t blocksize)
int | njb_stop_play (njb_t *njb)
int | njb_get_eax_size (njb_t *njb, u_int32_t *size)
void | njb_read_eaxtypes (njb_t *njb, u_int32_t size)
njb_eax_t * | njb_get_nexteax (njb_t *njb)
njb_time_t * | njb_get_time (njb_t *njb)
int | njb_set_time (njb_t *njb, njb_time_t *time)
int | njb_create_playlist (njb_t *njb, char *name, u_int32_t *plid)
int | njb_delete_playlist (njb_t *njb, u_int32_t plid)
int | njb_rename_playlist (njb_t *njb, u_int32_t plid, char *name)
int | njb_add_track_to_playlist (njb_t *njb, u_int32_t plid, u_int32_t trid)
int | njb_add_multiple_tracks_to_playlist (njb_t *njb, u_int32_t plid, u_int32_t *trids, u_int16_t ntracks)
int | njb_delete_track (njb_t *njb, u_int32_t trackid)
int | njb_delete_datafile (njb_t *njb, u_int32_t fileid)
int | njb_play_or_queue (njb_t *njb, u_int32_t trackid, int cmd)
int | njb_elapsed_time (njb_t *njb, u_int16_t *elapsed, int *change)
int | njb_replace_track_tag (njb_t *njb, njbttaghdr_t *tagh, void *tag)
int | njb_adjust_sound (njb_t *njb, u_int8_t effect, int16_t value)
## Variables
int | __sub_depth

## Detailed Description

This file contains the major parts of the NJB1 (OaSIS) protocol for all jukebox functionality.

## Define Documentation

#define NJB_STATUS | ( | a,
|  | b |  | ) |

Value:

    { \
            char *s = njb_status_string(b);\
            njb_error_add_string(a,subroutinename,s);\
            free (s); \
    }

This returns the string representing an internal NJB1 error code.

Referenced by njb_get_datafile_header(), njb_get_disk_usage(), njb_get_library_counter(), njb_get_playlist_header(), njb_get_track_tag_header(), njb_ping(), njb_read_eaxtypes(), njb_receive_file_block(), and njb_verify_last_command().

## Function Documentation

int njb_capture | ( | njb_t * | njb,
|  | int | which |
| ) |  |  |

This function captures the NJB1 device.

Parameters:

  | njb | is a pointer to the jukebox object to use.

Returns:
  0 on success, -1 on failure.

References EO_BADSTATUS, EO_USBCTL, and usb_setup().

Referenced by NJB_Capture(), and NJB_Release().

int njb_get_datafile_header | ( | njb_t * | njb,
|  | njbdfhdr_t * | dfh,
|  | int | cmd |
| ) |  |  |

Retrieves a datafile header from the NJB1

Returns:
  0 on OK, -1 on error, -2 means the last header has already been returned.

References EO_USBCTL, njb1_bytes_to_32bit(), NJB_STATUS, and usb_setup().

int njb_get_disk_usage | ( | njb_t * | njb,
|  | u_int64_t * | total,
|  | u_int64_t * | free_bytes |
| ) |  |  |

Get disk usage for the NJB1

Returns:
  0 on OK, -1 on failure, -2 means "try again"

References EO_USBCTL, njb1_bytes_to_64bit(), NJB_STATUS, and usb_setup().

Referenced by NJB_Get_Disk_Usage(), and NJB_Send_File().

int njb_get_library_counter | ( | njb_t * | njb,
|  | njblibctr_t * | lcount |
| ) |  |  |

This function gets the library counter from the device. The library counter is sent back and forth to the device to indicate if something in the library as changed.

Parameters:

  | njb | is a pointer to the jukebox object to use. is a pointer to the current library counter to get from the device.

Returns:
  0 on success, -1 on failure.

References data_dump(), EO_USBCTL, njb1_bytes_to_64bit(), NJB_STATUS, and usb_setup().

Referenced by NJB_Capture(), and NJB_Handshake().

njb_eax_t* njb_get_nexteax | ( | njb_t * | njb | ) |

This retrieves an EAX effect from the list of effects read in by the previous functions.

Parameters:

  | njb | a pointer to the NJB object to use

References njb_struct::protocol_state.

Referenced by NJB_Get_EAX_Type().

int njb_get_playlist_header | ( | njb_t * | njb,
|  | njbplhdr_t * | plh,
|  | int | cmd |
| ) |  |  |

Gets a playlist header.

Returns:
  0 on OK, -1 on error, -2 means "retry", -3 means "not found" or "last playlist already retrieved".

References EO_USBCTL, njb1_bytes_to_32bit(), NJB_STATUS, and usb_setup().

int njb_get_track_tag_header | ( | njb_t * | njb,
|  | njbttaghdr_t * | tagh,
|  | int | cmd |
| ) |  |  |

Get a track header from the NJB1.

Returns:
  0 on OK, -1 on error, -2 if the last header has been retrieved.

References EO_USBCTL, njb1_bytes_to_32bit(), NJB_STATUS, and usb_setup().

int njb_init_state | ( | njb_t * | njb | ) |

Initializes the basic state of the njb->protocol_state for the NJB1-device.

Parameters:

  | njb | is a pointer to the jukebox object to use.

Returns:
  0 on success, -1 on failure.

References njb_struct::protocol_state.

Referenced by NJB_Open().

int njb_ping | ( | njb_t * | njb | ) |

This function pings the NJB1 device to see if it is up and running, and ready for action. This is done every now and then.

Parameters:

  | njb | is a pointer to the jukebox object to use.

Returns:
  0 on success, -1 on failure.

References data_dump(), EO_RDSHORT, EO_USBBLK, EO_USBCTL, NJB_STATUS, njb_struct::protocol_state, usb_pipe_read(), and usb_setup().

Referenced by NJB_Handshake(), and NJB_Ping().

void njb_read_eaxtypes | ( | njb_t * | njb,
|  | u_int32_t | size |
| ) |  |  |

This reads in the block with EAX types. It is a byte chunk which is then interpreted and the EAX effects are added to a list of effects which is then retrieved by calls to the `njb_get_nexteax()` function.

Parameters:

  | njb | a pointer to the NJB object to use
  | size | the previously determined size of the EAX block

References eax_unpack(), EO_NOMEM, EO_RDSHORT, EO_USBBLK, EO_USBCTL, from_32bit_to_njb1_bytes(), njb1_bytes_to_32bit(), NJB_STATUS, njb_struct::protocol_state, usb_pipe_read(), and usb_setup().

Referenced by NJB_Reset_Get_EAX_Type().

u_int32_t njb_receive_file_block | ( | njb_t * | njb,
|  | u_int32_t | offset,
|  | u_int32_t | bsize,
|  | void * | bp |
| ) |  |  |

offset is the offset into the file, starting at 0. bsize indicates the recieved buffer max size. bp points to the recieve buffer (atleast NJB_XFER_BLOCK_SIZE + NJB_XFER_BLOCK_HEADER_SIZE) lastsort indicates if last transfer was short (ended before requested number of bytes were recieved).

If lastshort == 1, the last call to this function returned a a short read. In that case, a new setup command shall not be sent, the bus shall just keep retrieveing buffer contents from the bulk pipe.

References EO_TOOBIG, EO_USBBLK, EO_USBCTL, from_32bit_to_njb1_bytes(), NJB_STATUS, usb_pipe_read(), and usb_setup().

Referenced by NJB_Get_Track_fd().

int njb_set_library_counter | ( | njb_t * | njb,
|  | u_int64_t | count |
| ) |  |  |

This function sets the library counter on the device. The library counter is sent back and forth to the device to indicate if something in the library has changed.

Parameters:

  | njb | is a pointer to the jukebox object to use.
  | count | is the current library counter to set on the device.

Returns:
  0 on success, -1 on failure.

References EO_USBCTL, from_64bit_to_njb1_bytes(), and usb_setup().

Referenced by NJB_Handshake().

int njb_verify_last_command | ( | njb_t * | njb | ) |

This function verifies if the last command was successful or not.

Parameters:

  | njb | is a pointer to the jukebox object to use.

Returns:
  0 on success, -1 on failure.

References EO_USBCTL, NJB_STATUS, and usb_setup().

Referenced by NJB_Get_Track_fd(), NJB_Handshake(), NJB_Play_Track(), NJB_Queue_Track(), NJB_Set_Owner_String(), NJB_Set_Time(), and NJB_Update_Playlist().

## Variable Documentation

int __sub_depth

The current subroutine depth for all of libnjb (global)