LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
System Commands

System command are defined as: a command that's not implemented as a byte code (no VM intervention).

  System Command Bytes:
  ,------,------,------,------,------,------,------,------,
  |Byte 0|Byte 1|Byte 2|Byte 3|Byte 4|Byte 5|      |Byte n|
  '------'------'------'------'------'------'------'------'

  Byte 0 – 1: Command size, Little Endian\n

  Byte 2 – 3: Message counter, Little Endian\n

  Byte 4:     Command type. see following defines   */

  #define     SYSTEM_COMMAND_REPLY          0x01    //  System command, reply required
  #define     SYSTEM_COMMAND_NO_REPLY       0x81    //  System command, reply not required
                                                    /*
  Byte 5:     System Command. see following defines */

  #define     BEGIN_DOWNLOAD                0x92    //  Begin file down load
  #define     CONTINUE_DOWNLOAD             0x93    //  Continue file down load
  #define     BEGIN_UPLOAD                  0x94    //  Begin file upload
  #define     CONTINUE_UPLOAD               0x95    //  Continue file upload
  #define     BEGIN_GETFILE                 0x96    //  Begin get bytes from a file (while writing to the file)
  #define     CONTINUE_GETFILE              0x97    //  Continue get byte from a file (while writing to the file)
  #define     CLOSE_FILEHANDLE              0x98    //  Close file handle
  #define     LIST_FILES                    0x99    //  List files
  #define     CONTINUE_LIST_FILES           0x9A    //  Continue list files
  #define     CREATE_DIR                    0x9B    //  Create directory
  #define     DELETE_FILE                   0x9C    //  Delete
  #define     LIST_OPEN_HANDLES             0x9D    //  List handles
  #define     WRITEMAILBOX                  0x9E    //  Write to mailbox
  #define     BLUETOOTHPIN                  0x9F    //  Transfer trusted pin code to brick
  #define     ENTERFWUPDATE                 0xA0    //  Restart the brick in Firmware update mode
  #define     SETBUNDLEID                   0xA1    //  Set Bundle ID for mode 2
  #define     SETBUNDLESEEDID               0xA2    //  Set bundle seed ID for mode 2

/*

  Byte 6 - n: Dependent on System Command



  System Command Response Bytes:
  ,------,------,------,------,------,------,------,------,
  |Byte 0|Byte 1|Byte 2|Byte 3|      |      |      |Byte n|
  '------'------'------'------'------'------'------'------'

  Byte 0 – 1: Reply size, Little Endian\n

  Byte 2 – 3: Message counter, Little Endian\n

  Byte 4:     Reply type. see following defines     */

  #define     SYSTEM_REPLY                  0x03    //  System command reply
  #define     SYSTEM_REPLY_ERROR            0x05    //  System command reply error
/*
  Byte 5:     System command this is the response to

  Byte 6:     Reply status
*/

  // SYSTEM command return codes
  #define     SUCCESS                       0x00
  #define     UNKNOWN_HANDLE                0x01
  #define     HANDLE_NOT_READY              0x02
  #define     CORRUPT_FILE                  0x03
  #define     NO_HANDLES_AVAILABLE          0x04
  #define     NO_PERMISSION                 0x05
  #define     ILLEGAL_PATH                  0x06
  #define     FILE_EXITS                    0x07
  #define     END_OF_FILE                   0x08
  #define     SIZE_ERROR                    0x09
  #define     UNKNOWN_ERROR                 0x0A
  #define     ILLEGAL_FILENAME              0x0B
  #define     ILLEGAL_CONNECTION            0x0C

/*
  Byte 7 - n: Response dependent on System Command


  The example below is build around the host application (X3 software) that wants to send a file to a P-
  Brick where the VM, Green layer and Black layer reference to the firmware architecture. The
  architecture document below on the host side only consists of our current understanding of the
  implementation. At this point we have chosen to reference to the firmware architecture instead of the 7
  layer OSI model.


  ,---------------------,                                                 ,---------------------,
  |   Host Application  |                                                 |         VM          |
  '----------,----------'                                    ,--------->  '---------------------'  -----------,
             v                                               |                                                |
   Filename, Destination                             New file coming i                                        v
             |                                               ^                                        Accept or decline
             v                                               |                                                |
  ,---------------------,                                    '----------  ,---------------------,  <----------'
  |   Brick Server      |                                                 |   Green Layer       |
  '----------,----------'                                    ,--------->  '---------------------'  -----------,
             v                                               |                                                |
   Begin File D/L,                   Command size, Command type, Begin D/L, File size, Filename               v
   File size, Filename                                       ^                                      Accept or decline, Handle
             v                                               |                                                |
  ,---------------------,                                    '----------  ,---------------------,  <----------'
  |   USB Driver        |                                                 |   Black Layer       |
  '----------,----------'                                                 '---------------------'
             v                                                                       ^
  Command size, Command type, Begin D/L, File size, Filename    ------>
                                                                <------   Command size, Command type, Handle
  Command size, Command type, Continue D/L, Handle, Pay load    ------>
                                                                <------   Command size, Command type
  Command size, Command type, Continue D/L, Handle, Pay load    ------>
                                                                <------   Command size, Command type
  Command size, Command type, Continue D/L, Handle, Pay load    ------>


  Our current thoughts are as follows:
    1.  The host application initiates the communication by sending a request to the BrickServer that
        the application wants to send a file. The parameters within this call will include: File name for
        the file that should be send, and some description for the destination.
    2.  The brick server processes the request by sending a request to the USB driver which includes the
        following parameter: Specific command (Begin down load), File size and file name.
    3.  The USB driver will send out a command including: Command size, Command byte (Begin
        down load), File size and file name.
    4.  The P-bricks black layer with receive and validate the USB packages. It will transfer the
        received packages up to the green layer.
    5.  The green layer will evaluate the packages and as it is a begin down load ask the VM if it is
        okay to receive a packages now.
    6.  When the VM has replied OK to the green layer the green layer will ask the USB driver to
        reply OK and include a handle number to use within next packages for this communication.
    7.  When the OK signal is received with the USB driver (On the host side) the host will start
        transmitting additional data which include the follow parameters: Command size, Command
        type, Command byte (Continue D/L), Handle, Pay load data.

        Command size: 2 bytes (Tells the amount of bytes which belongs to this specific command).
        Command type: 1 byte
        File size: 4 Bytes
        Handle: 1 Byte


  Additional consideration
  Above scenario allows for having multiple file action running at the same time as we have a handle
  which identifies which process new data relates to. It also allows for interleaved communication
  during a potential long down load process. But our current knowledge and considerations are also that
  interleaved commands and splitting up larger data chunks slows down Bluetooth communication a lot.
  Bluetooth communication requires large data chunks to be pushed through the channel all the time
  with as less packages division as possible. Therefore the command size parameter is also included to
  enable sending larger data packages as one call.
  Above protocol architecture enables implementing the following two different scenarios which
  performs the same functionality (Sending a 60 Kbyte file to the brick):

  Scenario 1: (It is not possible to interleave during file down load) (60 K bytes data package):
  1.  Command size (0x06,0x00), Command type, Begin D/L, File size (0x60,0xEA,0x00,0x00), File Name (0x30)
  2.  Command size (0x62,0xEA), Command type, Continue D/L, Handle (0x01), Data ............

  Scenario 2 (This enables the host to interleave during file down load) (500 bytes data packages):
  1.  Command size (0x06,0x00),Command Type, Begin D/L, File size (0x60,0xEA,0x00,0x00), File Name (0x30)
  2.  Command size (0xF6,0x01), Command type, Continue D/L, Handle (0x01), Data ............
  3.  ..
  4.  ..
  5.  Command size (0xF6,0x01), Command type ,Continue D/L, Handle (0x01), Data ............



  - File Down Load
    - Destination filename path is relative from "lms2012/sys"
    - Destination folders are automatically created from filename path
    - First folder name must be: "apps", "prjs" or "tools" (see \ref UIdesign)
    - Second folder name in filename path must be equal to byte code executable name

  - File Upload (File read)
    - BEGIN_UPLOAD and CONTINUE_UPLOAD closes automatically the file handle when file has been uploaded
    - BEGIN_GETFILE and CONTINUE_GETFILE does not close the file handle when EOF has been reached
    - CONTINUE_GETFILE does also return the complete file size

  - Directory upload
    - LIST_FILES should work as long as list does not exceed 1014 bytes. CONTINUE_LISTFILES has NOT
      been implemented yet.

  - File handles
    - CLOSE_FILEHANDLE is partly implemented - hash is not working


    Examples:
*********************************************************************************************************

  File down load:
  ---------------

    Download file "../apps/tst/tst.rbf"

      BEGIN_DOWNLOAD:

          Bytes sent to brick:

            1C00xxxx0192xxxxxxxx2E2E2F617070732F7473742F7473742E72626600    (Hex)
            bbbbmmmmttssllllllllnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn

            bbbb = bytes in message, mm = message counter, tt = type of command, ss = system command,
            llllllll = file length, nn.. = filename


          Bytes received from brick:

            0600xxxx03920000    (Hex)
            bbbbmmmmttssrrhh

            bbbb = bytes in message, mm = message counter, tt = type of command, ss = system command,
            rr = return status, hh = handle to file


      CONTINUE_DOWNLOAD:

          Bytes sent to brick:

            xxxxxxxx819300xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx    (Hex)
            bbbbmmmmttsshhpppppppppppppppppppppppppppppppppppppppp

            bbbb = bytes in message, mm = message counter, tt = type of command, ss = system command, hh = handle to file (from BEGIN_DOWNLOAD), pp.. = pay load


          Bytes received from brick:

            0600xxxx03930000    (Hex)
            bbbbmmmmttssrrhh

            bbbb = bytes in message, mm = message counter, tt = type of command, ss = system command,
            rr = return status, hh = handle to file


  File Upload:
  ------------

    BEGIN_UPLOAD:

      Bytes send to the brick:

       xxxxxxxx0194xxxxxxx
       bbbbmmmmttssllllnnn...

       bbbb = bytes in message, mmmm = message counter, tt = type of command, ss = system command,
       llll = bytes to read, nnn... = filename incl. path


     Bytes received form the brick:

       xxxxxxxx039400xxxxxxxx00xxx
       bbbbmmmmttssrrllllllllhhppp...

       bbbb = bytes in massage, mmmm = message counter, tt = type of command, ss = system command,
       rr = return status, llllllll = file size, hh = file handle, ppp... = payload


    CONTINUE_UPLOAD:

      Bytes send to the brick:

      0700xxxx019500xxxx
      bbbbmmmmttsshhllll

      bbbb = bytes in the message, mmmm = message counter, tt = type of command, ss = system command,
      hh = file handle, llll = bytes to read


      Bytes send to the PC:

      xxxxxxxx03950000xxx
      bbbbmmmmttssrrhhppp...

      bbbb = bytes in the message, mmmm = message counter, tt = type of command, ss = system command,
      rr = return status,  hh = handle, pppp.. = payload



  Getting file content
  --------------------

    Used to upload datalog files - file handle is only closed when reaching EOF and file is not
    open for writing

    BEGIN_GETFILE:

      Bytes send to the brick:

      xxxxxxxx0196xxxxxxx
      bbbbmmmmttssllllnnn...

      bbbb = Bytes in massage, mmmm = message counter, tt = type of command, ss = system command,
      llll = max bytes to read, nnnn.... = path


      Bytes send to the PC:

      xxxxxxxx039600xxxxxxxx00xxx
      bbbbmmmmttssrrllllllllhhppp...

      bbbb = bytes ion massage, mmmm = message counter, tt = type of command, ss = system command,
      rr = return status, llllllll = File size, hh = Handle, ppp... = payload


    CONTINUE_GETFILE:

      Bytes send to the brick:

      0700xxxx019700xxxx
      bbbbmmmmttsshhllll

      bbbb = bytes in massage, mmmm = message counter, tt = type of command, ss = system command,
      hh = handle, llll = max bytes to read


      Bytes send to the PC:

      xxxxxxxx039700xxxxxxxx00xxx
      bbbbmmmmttssrrllllllllhhppp...

      bbbb = bytes in massage, mmmm = message counter, tt = type of command, ss = system command,
      rr = return status, llllllll = File size, hh = Handle, ppp... = payload



  Listing files and folders:
  --------------------------

    LIST_FILES:

      The new line delimited list is formatted as:

        If it is a file:
        32 chars (hex) of MD5SUM + space + 8 chars (hex) of filesize + space + filename + new line

        If it is a folder:
        foldername + / + new line


      Bytes send to the brick:

      xxxxxxxx0199xxxxxxx
      bbbbmmmmttssllllnnn...

      bbbb = bytes in message, mmmm = message counter, tt = type of message, ss = system command,
      llll = Max bytes to read, nnn.. = path name


      Bytes send to the PC:

      xxxxxxxx0399xxxxxxxxxxxxxxx
      bbbbmmmmttssrrllllllllhhnnn...

      bbbb = bytes in message, mmmm = message counter, tt = type of message, ss = system command,
      rr = return status, llllllll = List size, hh = Handle, nnn.. = new line delimited lists


    CONTINUE_LIST_FILES:

      Bytes send to the brick:

      0700xxxx019Axxxxxx
      bbbbmmmmttsshhllll

      bbbb = bytes in massage, mmmm = message counter, tt = type of command, ss = system command,
      hh = handle, llll = max bytes to read


      Bytes send to the PC:

      xxxxxxxx039Axxxxxxx
      bbbbmmmmttssrrhhppp...

      bbbb = bytes in massage, mmmm = message counter, tt = type of command, ss = system command,
      rr = return status, hh = Handle, ppp... = payload


  CLOSE_FILEHANDLE:
  -----------------

    Bytes send to the brick:

    xxxxxxxx019800xxxxxxxxxxxxxxxx
    bbbbmmmmttsshhpppppppppppppppp

    bbbb = bytes in the message, mmmm = message counter, tt = type of message, ss = system command,
    hh = handle, ppp... = hash


    Bytes send to the PC:

    0500xxxx039800
    bbbbmmmmttssrr

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    rr = return status



  CREATE_DIR:
  -----------

    Bytes to send to the brick:

    xxxxxxxx019Bxxxxxx...
    bbbbmmmmttsspppppp...

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    pp = null terminated full path of directory to create


    Bytes send to the PC:

    0500xxxx039Bxx
    bbbbmmmmttssrr

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    rr = return status



  DELETE_FILE:
  -------------

    Bytes to send to the brick:

    xxxxxxxx019Cxxxxxx...
    bbbbmmmmttsspppppp...

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    pp = null terminated full path of the file to delete


    Bytes send to the PC:

    0500xxxx039Cxx
    bbbbmmmmttssrr

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    rr = return status



  LIST_OPEN_HANDLES:
  ----------_-------

    Bytes to send to the brick:

    xxxxxxxx019D
    bbbbmmmmttss

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command


    Bytes send to the PC:

    xxxxxxxx039Dxxxxxx....
    bbbbmmmmttssrrpppp....

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    rr = return status, pppp = bits indicating whether handles are busy or not.



  WRITEMAILBOX:
  -------------

    Bytes sent to another brick:

    Mailbox name has to be zero terminated but name length has to be number of chars excluding
    the zero termination.

    xxxxxxxx819Exxxxxxxxxxxxxxxxxxxx
    bbbbmmmmttssllaaaaa...LLLLppp...

    bbbb = bytes in the message, mmmm = message counter, tt = type of message, ss = system command,
    ll = Name Length, aaa... = Name, LLLL = Payload length, ppp... = Payload


    Reply received from another brick:

    - Not valid


  BLUETOOTHPIN:
  --------------

    This command can only be sent by USB for safety reasons
    Bluetooth address does not contain colons
    Bluetooth MAC address is a zero terminated string type
    Bluetooth pin code is a zero terminated string type

    Bytes sent to the brick:

    0E00xxxx019F06xxxxxxxxxxxx04xxxx
    bbbbmmmmttssllaaaaaaaaaaaaLLpppp

    bbbb = bytes in the message, mmmm = message counter, tt = type of message, ss = system command,
    ll = MAC Length, aaa.. = MAC address of PC, LL = Pin length, ppp... = Pin code


    Bytes send to the PC:

    0F00xxxx039Fxx06xxxxxxxxxxxx04xxxx
    bbbbmmmmttssrrllaaaaaaaaaaaaLLpppp

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    rr = return status, ll = MAC length, MAC address, Pin length, Pin


  ENTERFWUPDATE:
  --------------

    This command is used to force the brick into firmware update mode. The command will not
    send any response back to the host. The filesystem will not be updated when closing
    Linux.

    Bytes send to the brick:

    0400xxxx81A0
    bbbbmmmmttss

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,


  SETBUNDLEID
  -------------

    Sets the default Bundle ID for mode 2. Default bundle ID is "com.lego.lms".


    xxxxxxxx01A1xxxxxx....
    bbbbmmmmttsspppppp....

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    pppppp = null terminated ID string. Max. length = 24 chars including the null termination


  SETBUNDLESEEDID
  ------------------

    Sets the default Bundle seed ID for mode 2. Default bundle seed ID is "9RNK8ZF528".

    xxxxxxxx01A1xxxxxx....
    bbbbmmmmttsspppppp....

    bbbb = bytes in massage, mmmm = message counter, tt = type of message, ss = system command,
    pppppp = null terminated SEED ID string. Max. length = 11 chars including the null termination



*********************************************************************************************************