Sanyo digital camera engine protocol
$Id: PROTOCOL,v 1.2 1998/01/18 01:41:02 itojun Exp $

NOTE1:	If the this text meets the actual practice, it is just a coincidence:-)
NOTE2:	All digits in protocol description are displayed in hexadecimal.
	(for example, "1B" means escape char, "41" means "A")
NOTE3:	Why we call it "Sanyo protocol?"  This is because the firmware
	and the CPU part of Olympus/Sanyo/Epson/Agfa digi-cams are
	implemented by Sanyo, reportedly.

There are several contributors to the list:
	Eugene Crosser <crosser@average.org>
	Leon Brooks <leonb@mon.ami.com.au>
	Tomio Narita <nrt@web.ad.jp>
	Thierry Bousch <bousch%linotte.uucp@topo.math.u-psud.fr>

Format can be devided into three categories:
	attention
	formatted - S commands
		    C commands

Attention commands
------------------
Get attention of camera (check if baudrate is ok or not)
	CPU: 00
	CAM:    xx

	xx: 15 if the baudrate is ok, FF if the baudrate is not good.

	NOTE: Agfa ePhoto307 returns 00 instead of 15, if the baudrate is ok.

	NOTE: we sometimes see FF alone sent from the camera.  The
		interpretation might be wrong.
	NOTE: some junk chars may be transmitted from the camera before
		15 or FF.  implementations may want to try skipping
		junk chars.

Formatted commands - in general
-------------------------------
General command format
    1. if camera has nothing to return to CPU
	CPU: 1B xx y1 y2 ....... z1 z2
	CAM:                           06

      CPU>CAM flow
	xx:	command type ('S' or 'C')
	y1 y2:	the length of the command body. (intel-endian: means y2*256+y1)
	.....:	command body. length is shown as y1 and y2.
	z1 z2:	simple checksum of command body. Just add the whole bytes
		of command body, take the least 16 bits, and place into
		z1 and z2 by intel-endian. (z2*256+z1)

      CAM>CPU flow
	06:	acknowledgement from the camera. (in ASCII character table,
		06 means ACK)
		I have never seen negative acknowledge, but I believe it must
		be 15 if there are. (15 means NAK)

    2. if camera has something to return to CPU (single sector reply)
	CPU: 1B xx y1 y2 ....... z1 z2                           06
	CAM:                           03 00 a1 a2 ....... b1 b2
	
      CPU>CAM flow
		Same as above.
      CAM>CPU flow
	03:	this is the last sector in this reply message. (ETX in ASCII
		table)
	00:	sector #.  this is sector 0.
	a1 a2:	the length of the reply body. (intel-endian: means a2*256+a1)
	.....:	reply body. length is shown as a1 and a2.
	b1 b2:	simple checksum of reply body. Just add the whole bytes
		of command body, take the least 16 bits, and place into
		b1 and b2 by intel-endian. (b2*256+b1)
      CPU>CAM flow
	06:	acknowledgement from the CPU. (in ASCII character table,
		06 means ACK)
		I have never seen negative acknowledge, but I believe it must
		be 15 if there are. (15 means NAK)

    3. if camera has something to return to CPU (multiple sector reply)
	3-a. command request:
		CPU: 1B xx y1 y2 ....... z1 z2
		CAM:

	      CPU>CAM flow
			Same as 1.  See "command with no reply" case.

	3-b. intermediate sectors:
		CPU:                           06
		CAM: 02 nn a1 a2 ....... b1 b2

	      CAM>CPU flow
		02:	this is one of the intermediate sectors in this reply
			message.  (STX in ASCII table)
		nn:	sector #.  starts from 0, and increased in normal order.
		a1 a2:	the length of the reply body. (intel-endian: means
			a2*256+a1)
		.....:	reply body. length is shown as a1 and a2.
		b1 b2:	simple checksum of reply body. Just add the whole
			bytes of command body, take the least 16 bits, and
			place into b1 and b2 by intel-endian. (b2*256+b1)

	      CPU>CAM flow
		06:	acknowledgement from the CPU. (in ASCII character
			table, 06 means ACK)
			I have never seen negative acknowledge, but I believe
			it must be 15 if there are. (15 means NAK)

	3-c. final sector:
		CPU:                           06
		CAM: 03 mm a1 a2 ....... b1 b2
	
	      CAM>CPU flow
		03:	this is the final sector in this reply message.
			(STX in ASCII table)
		mm:	final sector #.
		a1 a2:	the length of the reply body. (intel-endian: means
			a2*256+a1)
		.....:	reply body. length is shown as a1 and a2.
		b1 b2:	simple checksum of reply body. Just add the whole
			bytes of command body, take the least 16 bits, and
			place into b1 and b2 by intel-endian. (b2*256+b1)

	      CPU>CAM flow
		06:	acknowledgement from the CPU. (in ASCII character
			table, 06 means ACK)
			I have never seen negative acknowledge, but I believe
			it must be 15 if there are. (15 means NAK)

	As you have seen, case 2. (single sector reply) is a special
	case of 3. (multi sector reply)

    4. if you have long data to send to CPU (multiple sector command)
	4-a. command request:
		CPU: 1B xx y1 y2 ....... z1 z2
		CAM:                           06

	      CPU>CAM flow
			Same as 1.  See "command with no reply" case.

	      CAM>CPU flow
		06:	acknowledgement from the camera. (in ASCII character
			table, 06 means ACK)
			I have never seen negative acknowledge, but I believe
			it must be 15 if there are. (15 means NAK)

	4-b. intermediate sectors:
		CPU: 02 nn a1 a2 ....... b1 b2
		CAM:                           06

	      CPU>CAM flow
		02:	this is one of the intermediate sectors in this reply
			message.  (STX in ASCII table)
		nn:	sector #.  starts from 0, and increased in normal order.
		a1 a2:	the length of the extra command body.
			(intel-endian: means a2*256+a1)
		.....:	extra command body. length is shown as a1 and a2.
		b1 b2:	simple checksum of extra command body. Just add the
			whole bytes of command body, take the least 16 bits,
			and place into b1 and b2 by intel-endian. (b2*256+b1)

	      CAM>CPU flow
		06:	acknowledgement from the camera. (in ASCII character
			table, 06 means ACK)
			I have never seen negative acknowledge, but I believe
			it must be 15 if there are. (15 means NAK)

	4-c. final sector:
		CPU: 03 mm a1 a2 ....... b1 b2
		CAM:                           06
	
	      CPU>CAM flow
		03:	this is the final sector in this command.
			(STX in ASCII table)
		mm:	final sector #.
		a1 a2:	the length of the extra command body.
			(intel-endian: means a2*256+a1)
		.....:	extra command body. length is shown as a1 and a2.
		b1 b2:	simple checksum of extra command body. Just add the
			whole bytes of command body, take the least 16 bits,
			and place into b1 and b2 by intel-endian. (b2*256+b1)

	      CAM>CPU flow
		06:	acknowledgement from the camera. (in ASCII character
			table, 06 means ACK)
			I have never seen negative acknowledge, but I believe
			it must be 15 if there are. (15 means NAK)

	As you have seen, case 4. (multiple sector command) is similar
	to 3. (multi sector reply)


	In the following description, checksum bytes will be denoted by
	"CS CS", and length bytes will be shown as "LN LN".

Formatted commands - 'S' series
-------------------------------
	'S' commands requests the change of baudrate for the serial line.

	CPU: 1B 'S' 06 00 00 11 xx 00 00 00 CS CS
	CAM:                                      06

	This is command type 1. which needs no reply from camera.
	command body size is 6, hence command length field shows "06 00".

	xx:	baudrate to move to.
		00: default/initial(19200bps)
		01: 9600bps
		02: 19200bps
		03: 38400bps
		04: 57600bps
		05: 115200bps

	To switch the baudrate, you should:
		1. wait for acknowledge from the camera
		2. wait some time (from experience, 0.2sec would be fine)
		3. switch the baudrate

	NOTE: analysis incomplete.

Formatted commands - 'C' series
-------------------------------
	'C' commands has various meanings.  It will be determined by first
	byte of command body.

  00 ?? ?? ?? ?? ??: set various mode

	The command body has 6 bytes.  First is 00, second is mode name,
	and remaining 4 bytes seems to be 32 bit value (intel endian).

  00 01 xx 00 00 00: set picture quality mode
	CPU: 1B 'C' 06 00 00 01 xx 00 00 00 CS CS
	CAM:                                      06

	xx:	01 = normal mode
		02 = HQ

  00 02 xx xx xx xx: set current date
	CPU: 1B 'C' 06 00 00 02 xx xx xx xx CS CS
	CAM:                                      06

	date format: seconds from 1970/1/1 00:00:00, IN LOCAL TIME.
	(unix time always calcurate it in GMT)

  00 03 xx xx xx xx: set shutter speed
	CPU: 1B 'C' 06 00 00 03 xx xx xx xx CS CS
	CAM:                                      06

	described by Eugene Crosser, unconfirmed by itojun
	xx:	(in usec)

  00 04 xx 00 00 00: set the picture # to download
	CPU: 1B 'C' 06 00 00 04 xx 00 00 00 CS CS
	CAM:                                      06

	xx:	picture # (1-n)
		Don't know what happens if you specify out-of-range values.
		(should check before with series 'C' 01 0A)

  00 07 xx 00 00 00: set flash mode
	CPU: 1B 'C' 06 00 00 07 xx 00 00 00 CS CS
	CAM:                                      06

	xx:	00 = auto
		01 = force on
		02 = force off
		03 = on (redeye prevention)

  00 20 xx xx xx xx: set magic number for uploading to camera
	CPU: 1B 'C' 06 00 00 20 xx xx xx xx CS CS
	CAM:                                      06

	xx xx xx xx:	magic number. "0E 00 EC 0F" for Olympus C-410L.

	found by Thierry Bousch.
	NOTE: Camera will hang up if you upload images that does not
	conform to the camera's jpeg engine.  You will have to remove
	battery to recover from the hang up.

  00 21 xx 00 00 00: set lens mode
	CPU: 1B 'C' 06 00 00 21 xx 00 00 00 CS CS
	CAM:                                      06

	xx:	01 = macro mode
		02 = normal mode

  00 29 xx 00 00 00: set brightness of LCD screen(?)
	CPU: 1B 'C' 06 00 00 29 xx 00 00 00 CS CS
	CAM:                                      06


  01 ??: query various mode

	The command body has 2 bytes.  First is 01, second is mode name.
	The reply body has 4 bytes, and it seems to be 32 bit value
	(intel endian).

  01 01: query picture quality mode
	CPU: 1B 'C' 02 00 01 01 CS CS                               06
	CAM:                          03 00 04 00 xx 00 00 00 CS CS

	xx: picture quality mode (see command 00 01)

  01 02: query current date
	CPU: 1B 'C' 02 00 01 02 CS CS                               06
	CAM:                          03 00 04 00 xx xx xx xx CS CS

	date format: seconds from 1970/1/1 00:00:00, IN LOCAL TIME.
	(unix time always calcurate it in GMT)

  01 03: query shutter speed
	CPU: 1B 'C' 02 00 01 02 CS CS                               06
	CAM:                          03 00 04 00 xx xx xx xx CS CS

	described by Eugene Crosser, unconfirmed by itojun
	xx:	(in usec)

  01 07: query flash mode
	CPU: 1B 'C' 02 00 01 07 CS CS                               06
	CAM:                          03 00 04 00 xx 00 00 00 CS CS

	xx: flash mode (see command 00 07)

  01 0A: query number of picts the camera holds
	CPU: 1B 'C' 02 00 01 0A CS CS                               06
	CAM:                          03 00 04 00 xx 00 00 00 CS CS

	xx: number of picts camera holds (1-N)

  01 0B: query number of picts we can take
	CPU: 1B 'C' 02 00 01 0B CS CS                               06
	CAM:                          03 00 04 00 xx 00 00 00 CS CS

	xx: number of picts we can take in the current mode (1-N)

  01 0C: get the size of full-sized picture.
	CPU: 1B 'C' 02 00 01 0C CS CS                               06
	CAM:                          03 00 04 00 x1 x2 x3 x4 CS CS

	select picture# by series 'C' 00 04 before invoking it.
	x1 x2 x3 x4: size of full-sized picture (bytes).  intel endian.

  01 0D: get the size of thumbnail picture.
	CPU: 1B 'C' 02 00 01 0D CS CS                               06
	CAM:                          03 00 04 00 x1 x2 x3 x4 CS CS

	select picture# by series 'C' 00 04 before invoking it.
	x1 x2 x3 x4: size of thumbnail picture (bytes).  intel endian.

  01 10: battery capacity percentage.
	CPU: 1B 'C' 02 00 01 10 CS CS                               06
	CAM:                          03 00 04 00 xx 00 00 00 CS CS

	get battery capacity percentage.  used quite many times.

  01 17: query auto power-off time of ???
	CPU: 1B 'C' 02 00 01 17 CS CS                               06
	CAM:                          03 00 04 00 B4 00 00 00 CS CS

	autoshut on host timer (seconds) - Eugene Crosser

  01 18: query auto power-off time of ???
	CPU: 1B 'C' 02 00 01 18 CS CS                               06
	CAM:                          03 00 04 00 B4 00 00 00 CS CS

	autoshut in field timer (seconds) - Eugene Crosser

  01 21: query lens mode
	CPU: 1B 'C' 02 00 01 21 CS CS                               06
	CAM:                          03 00 04 00 xx 00 00 00 CS CS

	xx: lens mode (see command 00 21)

  01 24: unknown
	CPU: 1B 'C' 02 00 01 24 CS CS                               06
	CAM:                          03 00 04 00 05 00 00 00 CS CS

  01 26: query auto power-off time of ???
	CPU: 1B 'C' 02 00 01 26 CS CS                               06
	CAM:                          03 00 04 00 B4 00 00 00 CS CS

  01 27: unknown
	CPU: 1B 'C' 02 00 01 27 CS CS                               06
	CAM:                          03 00 04 00 00 00 00 00 00 00

	appears just after image transmission was done.

  01 29: query brightness of LCD screen(?)
	CPU: 1B 'C' 02 00 01 29 CS CS                               06
	CAM:                          03 00 04 00 02 00 00 00 CS CS


  02 ?? ??: details unknown

  02 01 00: erase all pictures
	CPU: 1B 'C' 03 00 02 01 00 CS CS
	CAM:                             06

  02 02 00: push shutter button
	CPU: 1B 'C' 03 00 02 02 00 CS CS
	CAM:                             06

	Don't know what happens if the command is issued while the memory
	is full.  (should check before with series 'C' 01 0B)

  02 07 00: erase single picture
	CPU: 1B 'C' 03 00 02 07 00 CS CS
	CAM:                             06

	select picture# by series 'C' 00 04 before invoking it.

  02 B0 00: save uploaded image into flashmemory
	CPU: 1B 'C' 03 00 02 B0 00 CS CS
	CAM:                             06

	Found by Thierry Bousch.
	NOTE: Camera will hang up if you upload images that does not
	conform to the camera's jpeg engine.  You will have to remove
	battery to recover from the hang up.

  03 xx ......: set various variable length value

	Set variable-length parameters to the camera.

  03 16 ......: set model id
	CPU: 1B 'C' LN LN 03 16 ..... CS CS
	CAM:                                06

	Command body has 0-terminated string that shows the model id of the
	camera.

  03 1D ......: upload data
	CPU: 1B 'C' LN LN 03 1D ..... CS CS	(first sector)
	CAM:                                06

	CPU: 02 xx LN LN ..... CS CS		(intermediate sectors)
	CAM:                         06

	CPU: 03 xx LN LN ..... CS CS		(last sector)
	CAM:                         06

	LN LN:	Usually, "00 08" is used for first/intermediate sectors,
		and arbitrary value for last sector.
	.....:	For the first sector, data part has to be (LN - 2) bytes
		because LN counts "03 1D" too.
	xx:	Sector number.  01 for the first intermediate sector
		(2nd sector in total), then monotonically increased.

	Found by Thierry Bousch.
	NOTE: Camera will hang up if you upload images that does not
	conform to the camera's jpeg engine.  You will have to remove
	battery to recover from the hang up.

  04 xx: get various variable length value

	Get variable-length values from the camera.

  04 0E: start downloading full-sized picture
	CPU: 1B 'C' 02 00 04 0E CS CS                          06
	CAM:                          ... multiple sectors ...

	select picture# by series 'C' 00 04 before invoking it.
	in C-400L, each sector has maximum size of 0x0800 (2048bytes).
	images that are bigger than 2048 bytes will be sent by multiple
	sectors.

  04 0F: start downloading thumbnail picture
	CPU: 1B 'C' 02 00 04 0F CS CS                          06
	CAM:                          ... multiple sectors ...

	select picture# by series 'C' 00 04 before invoking it.
	in C-400L, each sector has maximum size of 0x0800 (2048bytes).
	images that are bigger than 2048 bytes will be sent by multiple
	sectors.

  04 16: get model id
	CPU: 1B 'C' 02 00 04 16 CS                             06
	CAM:                          03 00 LN LN ...... CS CS

	Reply body has 0-terminated string that shows the model id of the
	camera.  For example, Olympus Camedia C-400L returns "OLYMPUS C-350L\0".

  04 19: get serial number
	CPU: 1B 'C' 02 00 04 19 CS CS                        06
	CAM:                          03 00 LN LN .... CS CS

	found by Eugene Crosser

  04 1A: get version
	CPU: 1B 'C' 02 00 04 1A CS CS                        06
	CAM:                          03 00 LN LN .... CS CS

	found by Eugene Crosser

  04 1B: get model type
	CPU: 1B 'C' 02 00 04 1B CS CS                        06
	CAM:                          03 00 LN LN .... CS CS

	Reply body has 0-terminated string that shows the model type of the
	camera.  For example, Olympus Camedia C-400L returns "SR25\0".

  04 2C: start downloading voice data (Sanyo DSC-V1 specific)
	CPU: 1B 'C' 02 00 04 2C CS CS                          06
	CAM:                          ... multiple sectors ...

	Specific to Sanyo DSC-V1.
	To download voice data, set picture# by 'C' series 00 04, then
	perform download by this command.  How to obtain the size of voice
	data is yet to be known.
	(submitted by Tomio Narita)

How to initialize the connection
--------------------------------
	1. attention
	2. set baudrate to default (0x00)
	3. attention
	4. set baudrate to specified one
	5. ????

How to download full-sized picture
----------------------------------
	see source code for more details.

	1. initialization.
	   set speed accordingly, ask user which picture to download,
	   and so forth.
	2. olympus tool sends 'C' series 01 10 command for battery capacity
	   check.
	3. set the picture# to get, by 'C' series 00 04.
	4. get size of the picture by 'C' series 01 0C, if needed.
	5. start transfer by 'C' series 04 0E.
	6. save all the reply body transferred from the camera into file.
	7. olympus tool sends 'C' series 01 27, by it is not necessery.

How to download thumbnail picture
---------------------------------
	see source code for more details.

	1. initialization.
	   set speed accordingly, ask user which picture to download,
	   and so forth.
	2. olympus tool sends 'C' series 01 10 command for battery capacity
	   check.
	3. set the picture# to get, by 'C' series 00 04.
	4. get size of the picture by 'C' series 01 0D, if needed.
	5. start transfer by 'C' series 04 0F.
	6. save all the reply body transferred from the camera into file.
	7. olympus tool sends 'C' series 01 27, by it is not necessery.

How to download voice data
--------------------------
(only for Sanyo DSC-V1)
	see source code for more details.

	1. initialization.
	   set speed accordingly, ask user which picture to download,
	   and so forth.
	2. olympus tool sends 'C' series 01 10 command for battery capacity
	   check.
	3. set the picture# to get, by 'C' series 00 04.
	4. (get size of the picture by 'C' series 01 xx - command unknown)
	5. start transfer by 'C' series 04 2C.
	6. save all the reply body transferred from the camera into file.
	7. olympus tool sends 'C' series 01 27, by it is not necessery.

	described by Tomio Narita.

How to upload picture
---------------------
	no source code available yet.  This is for Olympus C-410L.
	(not confirmed with other models)

	1. initialization.
	   set speed accordingly, ask user which picture to download,
	   and so forth.
	2. set the magic number, by 'C' series 00 20.
	3. start transfer by 'C' series 03 1D.
	4. send all the image data.
	5. save image data into flashmemory by 'C' series 02 B0.

	described by Thierry Bousch.
	NOTE: Camera will hang up if you upload images that does not
	conform to the camera's jpeg engine.  You will have to remove
	battery to recover from the hang up.
