netmd++  1.0.4
C++ API to access NetMD devices
netmd++.h
Go to the documentation of this file.
1 /*
2  * netmd++.h
3  *
4  * This file is part of netmd++, a library for accessing NetMD devices.
5  *
6  * It makes use of knowledge / code collected by Marc Britten and
7  * Alexander Sulfrian for the Linux Minidisc project.
8  *
9  * Asivery helped to make this possible!
10  * Sir68k discovered the Sony FW exploit!
11  *
12  * Copyright (C) 2023 Jo2003 (olenka.joerg@gmail.com)
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  *
28  */
29 
242 #pragma once
243 #include <cstdint>
244 #include <sstream>
245 #include <vector>
246 #include <ctime>
247 #include <functional>
248 #include <mutex>
249 
250 namespace netmd {
251 
253 enum DiskFormat : uint8_t
254 {
260 };
261 
263 enum NetMdErr : int
264 {
276 };
277 
279 struct TrackTime
280 {
281  int mMinutes;
282  int mSeconds;
284 };
285 
287 enum class TrackProtection : uint8_t
288 {
289  UNPROTECTED = 0x00,
290  PROTECTED = 0x03,
291  UNKNOWN = 0xFF
292 };
293 
295 enum class AudioEncoding : uint8_t
296 {
297  SP = 0x90,
298  LP2 = 0x92,
299  LP4 = 0x93,
300  UNKNOWN = 0xff
301 };
302 
305 {
311 };
312 
314 enum UTOCSector : uint16_t
315 {
322 };
323 
325 struct NetMdTime
326 {
327  uint16_t hour;
328  uint8_t minute;
329  uint8_t second;
330  uint8_t frame;
331 };
332 
335 {
338 
342 
346 };
347 
348 //-----------------------------------------------------------------------------
350 //-----------------------------------------------------------------------------
351 struct Group
352 {
353  int mGid;
354  int16_t mFirst;
355  int16_t mLast;
356  std::string mName;
357 };
358 
359 //-----------------------------------------------------------------------------
361 //-----------------------------------------------------------------------------
362 enum HomebrewFeatures : uint32_t
363 {
364  NOTHING = 0x00,
365  SP_UPLOAD = 0x01,
366  PCM_2_MONO = 0x02,
367  PCM_SPEEDUP = 0x04,
368  USB_EXEC = 0x08,
369 };
370 
372 using Groups = std::vector<Group>;
373 
375 using NetMDByteVector = std::vector<uint8_t>;
376 
377 //--------------------------------------------------------------------------
379 //
381 //--------------------------------------------------------------------------
382 using EvtCallback = std::function<void(bool)>;
383 
384 //--------------------------------------------------------------------------
391 //--------------------------------------------------------------------------
392 std::ostream& operator<<(std::ostream& o, const TrackTime& tt);
393 
394 //--------------------------------------------------------------------------
401 //--------------------------------------------------------------------------
402 std::ostream& operator<<(std::ostream& o, const AudioEncoding& ae);
403 
404 //--------------------------------------------------------------------------
411 //--------------------------------------------------------------------------
412 std::ostream& operator<<(std::ostream& o, const TrackProtection& tp);
413 
414 //------------------------------------------------------------------------------
421 //------------------------------------------------------------------------------
423 
425 class CMDiscHeader;
426 
428 class CNetMdDev;
429 
431 class CNetMdSecure;
432 
434 class CNetMdApi;
435 
438 
439 //------------------------------------------------------------------------------
441 //------------------------------------------------------------------------------
443 {
444 public:
445  //--------------------------------------------------------------------------
447  //--------------------------------------------------------------------------
448  CNetMdApi();
449 
450  //--------------------------------------------------------------------------
452  //--------------------------------------------------------------------------
453  ~CNetMdApi();
454 
455  //--------------------------------------------------------------------------
457  //
459  //--------------------------------------------------------------------------
460  int initHotPlug();
461 
462  //--------------------------------------------------------------------------
466  //--------------------------------------------------------------------------
467  int initDevice();
468 
469  //--------------------------------------------------------------------------
473  //--------------------------------------------------------------------------
474  std::string getDeviceName() const;
475 
476  //--------------------------------------------------------------------------
480  //--------------------------------------------------------------------------
481  static void setLogLevel(int severity);
482 
483  //--------------------------------------------------------------------------
487  //--------------------------------------------------------------------------
488  static void setLogStream(std::ostream& os);
489 
490  //--------------------------------------------------------------------------
494  //--------------------------------------------------------------------------
495  int trackCount();
496 
497  //--------------------------------------------------------------------------
501  //--------------------------------------------------------------------------
502  int discFlags();
503 
504  //--------------------------------------------------------------------------
508  //--------------------------------------------------------------------------
509  int eraseDisc();
510 
511  //--------------------------------------------------------------------------
518  //--------------------------------------------------------------------------
519  int trackTime(int trackNo, TrackTime& trackTime);
520 
521  //--------------------------------------------------------------------------
527  //--------------------------------------------------------------------------
528  int discTitle(std::string& title);
529 
530  //--------------------------------------------------------------------------
536  //--------------------------------------------------------------------------
537  int setDiscTitle(const std::string& title);
538 
539  //--------------------------------------------------------------------------
546  //--------------------------------------------------------------------------
547  int moveTrack(uint16_t from, uint16_t to);
548 
549  //--------------------------------------------------------------------------
556  //--------------------------------------------------------------------------
557  int setGroupTitle(uint16_t group, const std::string& title);
558 
559  //--------------------------------------------------------------------------
567  //--------------------------------------------------------------------------
568  int createGroup(const std::string& title, int first, int last);
569 
570  //--------------------------------------------------------------------------
577  //--------------------------------------------------------------------------
578  int addTrackToGroup(int track, int group);
579 
580  //--------------------------------------------------------------------------
587  //--------------------------------------------------------------------------
588  int delTrackFromGroup(int track, int group);
589 
590  //--------------------------------------------------------------------------
596  //--------------------------------------------------------------------------
597  int deleteGroup(int group);
598 
599  //--------------------------------------------------------------------------
605  //--------------------------------------------------------------------------
606  int deleteTrack(uint16_t track);
607 
608  //--------------------------------------------------------------------------
616  //--------------------------------------------------------------------------
617  int trackBitRate(uint16_t track, AudioEncoding& encoding, uint8_t& channel);
618 
619  //--------------------------------------------------------------------------
626  //--------------------------------------------------------------------------
627  int trackFlags(uint16_t track, TrackProtection& flags);
628 
629  //--------------------------------------------------------------------------
636  //--------------------------------------------------------------------------
637  int trackTitle(uint16_t track, std::string& title);
638 
639  //--------------------------------------------------------------------------
643  //--------------------------------------------------------------------------
644  bool spUploadSupported();
645 
646  //--------------------------------------------------------------------------
650  //--------------------------------------------------------------------------
651  bool otfEncodeSupported();
652 
653  //--------------------------------------------------------------------------
657  //--------------------------------------------------------------------------
658  bool tocManipSupported();
659 
660  //--------------------------------------------------------------------------
664  //--------------------------------------------------------------------------
665  bool pcm2MonoSupported();
666 
667  //--------------------------------------------------------------------------
671  //--------------------------------------------------------------------------
673 
674  //--------------------------------------------------------------------------
678  //--------------------------------------------------------------------------
679  bool pcmSpeedupSupported();
680 
681  //--------------------------------------------------------------------------
699  //--------------------------------------------------------------------------
700  int sendAudioFile(const std::string& filename, const std::string& title, DiskFormat otf);
701 
702  //--------------------------------------------------------------------------
709  //--------------------------------------------------------------------------
710  int setTrackTitle(uint16_t trackNo, const std::string& title);
711 
712  //--------------------------------------------------------------------------
718  //--------------------------------------------------------------------------
719  int discCapacity(DiscCapacity& dcap);
720 
721  //--------------------------------------------------------------------------
725  //--------------------------------------------------------------------------
726  Groups groups();
727 
728  //--------------------------------------------------------------------------
732  //--------------------------------------------------------------------------
733  int prepareTOCManip();
734 
735  //--------------------------------------------------------------------------
741  //--------------------------------------------------------------------------
743 
744  //--------------------------------------------------------------------------
751  //--------------------------------------------------------------------------
752  int writeUTOCSector(UTOCSector s, const NetMDByteVector& data);
753 
754  //--------------------------------------------------------------------------
763  //--------------------------------------------------------------------------
764  int finalizeTOC(bool reset = false, uint8_t resetWait = 15);
765 
766  //--------------------------------------------------------------------------
768  //
770  //
773  //--------------------------------------------------------------------------
774  int startHBSession(uint32_t features);
775 
776  //--------------------------------------------------------------------------
778  //
780  //
783  //--------------------------------------------------------------------------
784  void endHBSession(uint32_t features);
785 
786  //--------------------------------------------------------------------------
788  //
791  //--------------------------------------------------------------------------
793 
794 private:
796  CMDiscHeader* mpDiscHeader;
797 
799  CNetMdDev* mpNetMd;
800 
802  CNetMdSecure* mpSecure;
803 
805  EvtCallback mHotplugCallback;
806 
808  std::mutex mMutexHotplug;
809 };
810 
811 namespace toc
812 {
814  struct TOC;
815 }
816 
817 //------------------------------------------------------------------------------
819 //------------------------------------------------------------------------------
821 {
822 public:
824  struct DAOFragment
825  {
826  uint32_t mStart;
827  uint32_t mEnd;
828  };
829 
831  using DAOFragments = std::vector<DAOFragment>;
832 
833  //--------------------------------------------------------------------------
839  //--------------------------------------------------------------------------
840  CNetMdTOC(int trackCount = 0, uint32_t lenInMs = 0, uint8_t* data = nullptr);
841 
842  //--------------------------------------------------------------------------
844  //--------------------------------------------------------------------------
845  ~CNetMdTOC();
846 
847  //--------------------------------------------------------------------------
853  //--------------------------------------------------------------------------
854  void import(int trackCount = 0, uint32_t lenInMs = 0, uint8_t* data = nullptr);
855 
856  //--------------------------------------------------------------------------
871  //--------------------------------------------------------------------------
872  int addTrack(uint8_t no, uint32_t lengthMs, const std::string& title, const std::time_t& tstamp, bool mono = false);
873 
874  //--------------------------------------------------------------------------
880  //--------------------------------------------------------------------------
881  int setDiscTitle(const std::string& title);
882 
883  //--------------------------------------------------------------------------
887  //--------------------------------------------------------------------------
888  int trackCount() const;
889 
890  //--------------------------------------------------------------------------
894  //--------------------------------------------------------------------------
895  std::string discTitle() const;
896 
897  //--------------------------------------------------------------------------
903  //--------------------------------------------------------------------------
904  std::string trackTitle(int trackNo) const;
905 
906  //--------------------------------------------------------------------------
912  //--------------------------------------------------------------------------
913  std::string trackInfo(int trackNo) const;
914 
915  //--------------------------------------------------------------------------
919  //--------------------------------------------------------------------------
920  std::string discInfo() const;
921 
922 private:
924  toc::TOC* mpToc;
925 
927  int mTracksCount;
928 
930  uint32_t mLengthInMs;
931 
933  uint32_t mCurPos;
934 
936  int mDAOTrack;
937 
939  uint32_t mDAOGroups;
940 
942  DAOFragments mDAOFragments;
943 };
944 
945 
946 } // ~namespace
SP mono.
Definition: netmd++.h:257
int discCapacity(DiscCapacity &dcap)
get disc capacity
int setDiscTitle(const std::string &title)
Sets the disc title.
NetMdTime total
Definition: netmd++.h:341
std::vector< DAOFragment > DAOFragments
type to store all DAO track fragments (for fragmented, non empty discs)
Definition: netmd++.h:831
SP upload.
Definition: netmd++.h:365
std::string getDeviceName() const
Gets the device name.
success
Definition: netmd++.h:265
std::vector< uint8_t > NetMDByteVector
byte vector
Definition: netmd++.h:375
int sendAudioFile(const std::string &filename, const std::string &title, DiskFormat otf)
Sends an audio track.
uint8_t frame
frame
Definition: netmd++.h:330
a fragment used in DAO track
Definition: netmd++.h:824
LP2.
Definition: netmd++.h:256
NetMD time.
Definition: netmd++.h:325
minidisc responded with 08 response
Definition: netmd++.h:269
NetMDByteVector readUTOCSector(UTOCSector s)
Reads an utoc sector.
uint8_t minute
minute
Definition: netmd++.h:328
~CNetMdApi()
Destroys the object.
track times
Definition: netmd++.h:279
int mSeconds
time in seconds
Definition: netmd++.h:282
TrackProtection
type safe protection flags
Definition: netmd++.h:287
int eraseDisc()
erase MD
int mTenthSecs
time in 10ms
Definition: netmd++.h:283
NetMdTime available
Definition: netmd++.h:345
dont do on-the-fly encoding
Definition: netmd++.h:259
int writeUTOCSector(UTOCSector s, const NetMDByteVector &data)
Writes an utoc sector.
Structure to hold the capacity information of a disc.
Definition: netmd++.h:334
full width titles
Definition: netmd++.h:319
not supported
Definition: netmd++.h:273
int deleteGroup(int group)
delete a group
PCM to mono.
Definition: netmd++.h:366
std::string trackInfo(int trackNo) const
get track info
static void setLogLevel(int severity)
Sets the log level.
CNetMdTOC(int trackCount=0, uint32_t lenInMs=0, uint8_t *data=nullptr)
Constructs a new instance.
half width titles
Definition: netmd++.h:317
int addTrack(uint8_t no, uint32_t lengthMs, const std::string &title, const std::time_t &tstamp, bool mono=false)
Adds a track.
critical information
Definition: netmd++.h:309
std::string trackTitle(int trackNo) const
get track title
bool nativeMonoUploadSupported()
is native mono upload supported?
int initHotPlug()
init libusb hotplug (native or emulation)
needed for log parcing!
Definition: netmd++.h:310
try again
Definition: netmd++.h:275
static void setLogStream(std::ostream &os)
Sets the log stream.
int moveTrack(uint16_t from, uint16_t to)
move a track (number)
bool pcm2MonoSupported()
is PCM to mono supported?
~CNetMdTOC()
Destroys the object.
int createGroup(const std::string &title, int first, int last)
Creates a group.
uint16_t hour
hour
Definition: netmd++.h:327
any other error
Definition: netmd++.h:272
int16_t mFirst
first track
Definition: netmd++.h:354
AudioEncoding
type safe encoding flags
Definition: netmd++.h:295
uint8_t second
second
Definition: netmd++.h:329
uint32_t mEnd
end group
Definition: netmd++.h:827
USB execution.
Definition: netmd++.h:368
no features
Definition: netmd++.h:364
std::ostream & operator<<(std::ostream &o, const TrackTime &tt)
format helper for TrackTime
bool spUploadSupported()
is SP upload supported?
information
Definition: netmd++.h:307
int discTitle(std::string &title)
get disc title
DiskFormat
disk format
Definition: netmd++.h:253
int setDiscTitle(const std::string &title)
Sets the disc title.
This class describes a net md TOC.
Definition: netmd++.h:820
std::string mName
group name
Definition: netmd++.h:356
int deleteTrack(uint16_t track)
delete track
void registerForHotplugEvents(EvtCallback cb)
register hotplug callback function
UTOCSector
TOC sector names.
Definition: netmd++.h:314
some unidentified TOC sector #2
Definition: netmd++.h:321
int trackFlags(uint16_t track, TrackProtection &flags)
get track flags
int startHBSession(uint32_t features)
start homebrew
This class describes a C++ NetMD access library.
Definition: netmd++.h:442
track group
Definition: netmd++.h:351
int addTrackToGroup(int track, int group)
Adds a track to group.
LP4.
Definition: netmd++.h:255
int finalizeTOC(bool reset=false, uint8_t resetWait=15)
finalize TOC through exploit
NetMdErr
NetMD errors.
Definition: netmd++.h:263
HomebrewFeatures
NetMD homebrew features.
Definition: netmd++.h:362
int mGid
group id
Definition: netmd++.h:353
void endHBSession(uint32_t features)
stop homebrew session
int16_t mLast
last track
Definition: netmd++.h:355
int trackCount()
request track count
int setGroupTitle(uint16_t group, const std::string &title)
Sets the group title.
int trackTime(int trackNo, TrackTime &trackTime)
get track time
std::function< void(bool)> EvtCallback
hotplug callback function signature
Definition: netmd++.h:382
int trackBitRate(uint16_t track, AudioEncoding &encoding, uint8_t &channel)
get track bitrate data
general USB error
Definition: netmd++.h:266
int setTrackTitle(uint16_t trackNo, const std::string &title)
Sets the track title.
interim
Definition: netmd++.h:274
bool pcmSpeedupSupported()
is PCM speedup supportd
int delTrackFromGroup(int track, int group)
remove track from group
time stamps
Definition: netmd++.h:318
std::string discInfo() const
get disc info
int discFlags()
request disc flags
parameter error
Definition: netmd++.h:271
int prepareTOCManip()
prepare TOC manipulation
timeout while waiting for response
Definition: netmd++.h:268
position and addresses of audio data
Definition: netmd++.h:316
bool otfEncodeSupported()
is on the fly encoding supported by device
minidisc responded with 0A response
Definition: netmd++.h:270
PCM speedup.
Definition: netmd++.h:367
std::string discTitle() const
get MD title
some unidentified TOC sector #1
Definition: netmd++.h:320
more serious
Definition: netmd++.h:308
int initDevice()
Initializes the device.
SP stereo.
Definition: netmd++.h:258
NetMDByteVector & operator+=(NetMDByteVector &a, const NetMDByteVector &b)
Addition assignment operator for NetMDByteVector.
bool tocManipSupported()
is TOC manipulation supported?
Definition: netmd++.h:250
NetMdTime recorded
Time allready recorded on the disc.
Definition: netmd++.h:337
debug information
Definition: netmd++.h:306
CNetMdApi()
Constructs a new instance.
typelog
log severity
Definition: netmd++.h:304
uint32_t mStart
start group
Definition: netmd++.h:826
std::vector< Group > Groups
netmd groups
Definition: netmd++.h:372
player not ready for command
Definition: netmd++.h:267
int trackCount() const
get track count
int trackTitle(uint16_t track, std::string &title)
get track title
Groups groups()
get MD track groups
int mMinutes
time in minutes
Definition: netmd++.h:281