Changeset 1155


Ignore:
Timestamp:
04/24/11 21:21:02 (2 years ago)
Author:
reyalP
Message:

ptp protocol update to version 2

  • tables messages + returns are now identified with PTP_CHDK_TYPE_TABLE. The table is still returned as a string formatted by usb_msg_table_to_string, this just allows clients reliably to distinguish tables from strings.
  • empty strings are returned with msg.size 0 instead of being silently dropped.
  • errors in usb_msg_table_to_string now return a PTP_CHDK_S_MSGTYPE_ERR for returns, or throw a lua error for user messages.
Location:
trunk/core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/luascript.c

    r1101 r1155  
    6161            data = (char *)lua_tolstring(L,index,&datasize); 
    6262        break; 
    63         // TODO this converts to a string and returns as STRING, format as described at  
     63        // TODO this uses usb_msg_table_to_string to serialize the table 
     64        // the default format is described in 
    6465        // http://chdk.setepontos.com/index.php?topic=4338.msg62606#msg62606 
    65         // for compatibility with current PTPCAM/PTPCAMGUI implementation 
    66         // later we should switch to a proper serialized table with it's own return type 
    67         case LUA_TTABLE:  
    68             lua_script_disable_yield_hook(); // don't want to yeild while converting 
     66        // other formats can be implemented by overriding this function in your lua code 
     67        case LUA_TTABLE: { 
     68            int result; 
     69            lua_script_disable_yield_hook(); // don't want to yield while converting 
    6970            lua_getglobal(L, "usb_msg_table_to_string"); // push function 
    7071            lua_pushvalue(L, index); // copy specified index to top of stack 
    71             lua_pcall(L,1,1,0); // this will leave an error message as a string on the stack if call fails 
     72            result = lua_pcall(L,1,1,0); // this will leave an error message as a string on the stack if call fails 
    7273            lua_script_enable_yield_hook(); 
    73             // an empty table will be returned as an empty string 
     74            if( result ) { 
     75                // if called from lua, throw a normal error 
     76                if( msgtype == PTP_CHDK_S_MSGTYPE_USER ) { 
     77                    luaL_error(L,lua_tostring(L,-1)); 
     78                    return NULL; // not reached 
     79                } else { // if it's a return, convert the message to an ERR 
     80                    msgtype = PTP_CHDK_S_MSGTYPE_ERR; 
     81                    datatype = PTP_CHDK_S_ERRTYPE_RUN; 
     82                    data = (char *)lua_tolstring(L,-1,&datasize); 
     83                    break; 
     84                } 
     85            } 
     86            // an empty table is returned as an empty string by default 
    7487            // a non-string should never show up here 
    75             if ( !(lua_isstring(L,-1) /*&& ( lua_objlen(L,-1) > 0 )*/)) {  
     88            if ( !lua_isstring(L,-1) ) {  
    7689                return NULL; 
    7790            } 
    78             datatype = PTP_CHDK_TYPE_STRING; 
     91            datatype = PTP_CHDK_TYPE_TABLE; 
    7992            data = (char *)lua_tolstring(L,-1,&datasize); 
    8093            lua_pop(L,1); 
     94        } 
    8195        break; 
    8296        default: 
     
    143157        int i,end = lua_gettop(L); 
    144158        for(i=1;i<=end; i++) { 
     159            ptp_script_msg *msg = lua_create_usb_msg(L,i,PTP_CHDK_S_MSGTYPE_RET); 
    145160            // if the queue is full return values will be silently discarded 
    146161            // incompatible types will be returned as TYPE_UNSUPPORTED to preserve expected number and order of return values 
    147             ptp_script_write_msg(lua_create_usb_msg(L,i,PTP_CHDK_S_MSGTYPE_RET));  
     162            if(msg) { 
     163                ptp_script_write_msg(msg);  
     164                // create_usb_msg may convert the message to an error 
     165                if(msg->type != PTP_CHDK_S_MSGTYPE_RET) { 
     166                    break; 
     167                } 
     168            } else { 
     169                ptp_script_write_error_msg(PTP_CHDK_S_ERRTYPE_RUN, "error creating return msg"); 
     170                break; 
     171            } 
    148172        } 
    149173    } 
  • trunk/core/ptp.c

    r1101 r1155  
    139139    return 0; 
    140140  } 
    141   // zero size messages don't make any sense 
    142   if(msg == NULL || msg->size == 0) { 
     141  if(msg == NULL) { 
    143142    return 0; 
    144143  } 
     
    159158 
    160159// public interface for script 
     160// create a message to be queued later 
    161161ptp_script_msg* ptp_script_create_msg(unsigned type, unsigned subtype, unsigned datasize, const void *data) { 
    162162  ptp_script_msg *msg; 
    163   if(!datasize) { 
    164     return NULL; 
    165   } 
    166163  msg = malloc(sizeof(ptp_script_msg) + datasize); 
    167164  msg->size = datasize; 
     
    169166  msg->subtype = subtype; 
    170167  // caller may fill in data themselves 
    171   if(data) { 
     168  // datasize may be empty (e.g. empty string) 
     169  if(data && datasize) { 
    172170      memcpy(msg->data,data,msg->size); 
    173171  } 
     
    175173} 
    176174 
     175// add a message to the outgoing queue 
    177176int ptp_script_write_msg(ptp_script_msg *msg) { 
    178177  msg->script_id = script_run_id; 
     
    180179} 
    181180 
     181// retrieve the next message in the incoming queue 
    182182ptp_script_msg* ptp_script_read_msg(void) { 
    183183  ptp_script_msg *msg; 
     
    197197  } 
    198198} 
     199 
     200// convenience function write an error message 
    199201int ptp_script_write_error_msg(unsigned errtype, const char *err) { 
    200202  if(script_msg_q_full(&msg_q_out)) { 
     
    217219    char *str; 
    218220  } temp_data; 
    219   static int temp_data_kind = 0; // 0: nothing, 1: ascii string, 2: lua object 
    220   static int temp_data_extra; // size (ascii string) or type (lua object) 
     221  static int temp_data_kind = 0; // 0: nothing, 1: ascii string 
     222  static int temp_data_extra; // size (ascii string) 
    221223  PTPContainer ptp; 
    222224 
     
    533535    case PTP_CHDK_ReadScriptMsg: 
    534536    { 
     537      char *pdata=""; 
     538      unsigned datasize=1; 
     539 
    535540      ptp_script_msg *msg = dequeue_script_msg(&msg_q_out); 
    536541      ptp.num_param = 4; 
    537       if(!msg) { 
     542      if(msg) { 
     543        ptp.param1 = msg->type; 
     544        ptp.param2 = msg->subtype; 
     545        ptp.param3 = msg->script_id; 
     546        ptp.param4 = msg->size; 
     547        // empty messages must have a data phase, so use default if no data 
     548        if(msg->size) { 
     549            datasize = msg->size; 
     550            pdata = msg->data; 
     551        } 
     552          } else { 
    538553        // return a fully formed message for easier handling 
    539554        ptp.param1 = PTP_CHDK_S_MSGTYPE_NONE; 
    540555        ptp.param2 = 0; 
    541556        ptp.param3 = 0; 
    542         ptp.param4 = 4; 
    543         // looks like we need to send some data no matter what 
    544         if ( !send_ptp_data(data,"\0\0\0",4) ) 
    545         { 
    546           ptp.code = PTP_RC_GeneralError; 
    547         } 
    548         break; 
    549       } 
    550       ptp.param1 = msg->type; 
    551       ptp.param2 = msg->subtype; 
    552       ptp.param3 = msg->script_id; 
    553       ptp.param4 = msg->size; 
     557        ptp.param4 = 0; 
     558      } 
    554559 
    555560      // NOTE message is lost if sending failed 
    556       if ( !send_ptp_data(data,msg->data,msg->size) ) 
     561      if ( !send_ptp_data(data,pdata,datasize) ) 
    557562      { 
    558563        ptp.code = PTP_RC_GeneralError; 
  • trunk/core/ptp.h

    r1115 r1155  
    11#ifndef __CHDK_PTP_H 
    22#define __CHDK_PTP_H 
    3 #define PTP_CHDK_VERSION_MAJOR 1  // increase only with backwards incompatible changes (and reset minor) 
     3#define PTP_CHDK_VERSION_MAJOR 2  // increase only with backwards incompatible changes (and reset minor) 
    44#define PTP_CHDK_VERSION_MINOR 0  // increase with extensions of functionality 
    55/* 
     
    880.2 - Added ScriptStatus and ScriptSupport, based on work by ultimA 
    991.0 - removed old script result code (luar), replace with message system 
     102.0 - return PTP_CHDK_TYPE_TABLE for tables instead of TYPE_STRING, allow return of empty strings 
    1011*/ 
    1112 
     
    5556                            //   for error ptp_chdk_script_error_type 
    5657                            // return param3 is script id of script that generated the message 
    57                             // return param4 is length of the message data 
     58                            // return param4 is length of the message data.  
    5859                            // return data is message. 
    59                             // A minimum of 4 bytes of zeros is returned if there would not be data otherwise 
     60                            // A minimum of 1 bytes of zeros is returned if the message has no data (empty string or type NONE) 
    6061  PTP_CHDK_WriteScriptMsg,  // write a message for scripts running on camera 
    6162                            // input param2 is target script id, 0=don't care. Messages for a non-running script will be discarded 
     
    7172  PTP_CHDK_TYPE_BOOLEAN, 
    7273  PTP_CHDK_TYPE_INTEGER, 
    73   PTP_CHDK_TYPE_STRING, // NOTE tables currently returned as string 
     74  PTP_CHDK_TYPE_STRING, // Empty strings are returned with length=0 
     75  PTP_CHDK_TYPE_TABLE,  // tables are converted to a string by usb_msg_table_to_string,  
     76                        // this function can be overridden in lua to change the format 
     77                        // the string may be empty for an empty table 
    7478} ptp_chdk_script_data_type; 
    7579 
     
    9498enum { 
    9599    PTP_CHDK_S_MSGTYPE_NONE = 0, // no messages waiting 
    96     PTP_CHDK_S_MSGTYPE_ERR,         // error message 
     100    PTP_CHDK_S_MSGTYPE_ERR,      // error message 
    97101    PTP_CHDK_S_MSGTYPE_RET,      // script return value 
    98102    PTP_CHDK_S_MSGTYPE_USER,     // message queued by script 
Note: See TracChangeset for help on using the changeset viewer.