Example #1
0
        // returns return data (if any) unless get_error is true
        private object GetScriptMessage(int script_id, bool return_string_as_byte_array, bool get_error = false)
        {
            CHDK_ScriptMsgType type;
            int subtype, script_id2;

            byte[] data;
            while (true)
            {
                _session.CHDK_ReadScriptMsg(out type, out subtype, out script_id2, out data);

                if (type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_NONE) // no more messages; no return value
                {
                    return(null);
                }

                if (script_id2 != script_id) // ignore message from other scripts
                {
                    continue;
                }

                if (!get_error && type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_RET) // return info!
                {
                    switch ((CHDK_ScriptDataType)subtype)
                    {
                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_BOOLEAN:
                        return((data[0] | data[1] | data[2] | data[3]) != 0);

                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_INTEGER:
                        return(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));

                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_STRING:
                        if (return_string_as_byte_array)
                        {
                            return(data);
                        }
                        else
                        {
                            return((new ASCIIEncoding()).GetString(data));
                        }

                    default:
                        throw new Exception("script returned unsupported data type: " + type.ToString());
                    }
                }

                if (type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_ERR) // hmm.. error
                {
                    if (get_error)
                    {
                        return((new ASCIIEncoding()).GetString(data));
                    }
                    else
                    {
                        throw new Exception("error running script: " + (new ASCIIEncoding()).GetString(data));
                    }
                }

                // ignore other (user) messages
            }
        }
Example #2
0
        // returns return data (if any) unless get_error is true
        private object GetScriptMessage(int script_id, bool return_string_as_byte_array, bool get_error = false)
        {
            CHDK_ScriptMsgType type;
            int subtype, script_id2;

            byte[] data;
            while (true)
            {
                _session.CHDK_ReadScriptMsg(out type, out subtype, out script_id2, out data);

                if (type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_NONE) // no more messages; no return value
                {
                    return(null);
                }

                if (script_id2 != script_id) // ignore message from other scripts
                {
                    continue;
                }

                if (!get_error && type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_RET) // return info!
                {
                    CHDK_ScriptDataType chdkSubtype = (CHDK_ScriptDataType)subtype;
                    switch (chdkSubtype)
                    {
                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_NIL:
                        return(null);

                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_BOOLEAN:
                        return((data[0] | data[1] | data[2] | data[3]) != 0);

                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_INTEGER:
                        return(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));

                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_STRING:
                        if (return_string_as_byte_array)
                        {
                            return(data);
                        }
                        else
                        {
                            return((new ASCIIEncoding()).GetString(data));
                        }

                    case CHDK_ScriptDataType.PTP_CHDK_TYPE_TABLE:
                        string   str   = Encoding.ASCII.GetString(data);
                        string[] split = str.Split('\n');
                        Dictionary <string, string> table = new Dictionary <string, string>();
                        foreach (var substr in split)
                        {
                            string[] kvp = substr.Split('\t');
                            if (kvp.Length != 2)
                            {
                                break;
                            }
                            table.Add(kvp[0], kvp[1]);
                        }
                        return(table);

                    default:
                        throw new Exception("script returned unsupported data type: " + chdkSubtype.ToString());
                    }
                }

                if (type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_ERR) // hmm.. error
                {
                    if (get_error)
                    {
                        return((new ASCIIEncoding()).GetString(data));
                    }
                    else
                    {
                        throw new Exception("error running script: " + (new ASCIIEncoding()).GetString(data));
                    }
                }

                // ignore other (user) messages
            }
        }