Beispiel #1
0
static VALUE
rbosa_element_data (int argc, VALUE *argv, VALUE self)
{
    VALUE       coerce_type;
    AEDesc      coerced_desc;
    AEDesc *    desc;
    OSErr       error;
    void *      data;
    Size        datasize;
    VALUE       retval;
    bool        to_4cc;

    rb_scan_args (argc, argv, "01", &coerce_type);
    to_4cc = false;

    desc  = rbosa_element_aedesc (self);
    
    if (!NIL_P (coerce_type)) {
        FourCharCode code;

        code = RVAL2FOURCHAR (coerce_type);
        error = AECoerceDesc (desc, code, &coerced_desc);
        if (error != noErr)
            rb_raise (rb_eRuntimeError, "Cannot coerce desc to type %s : %s (%d)", 
                      RVAL2CSTR (coerce_type), error_code_to_string (error), error);
        
        desc = &coerced_desc;
        to_4cc = code == 'type';
    }

    datasize = AEGetDescDataSize (desc);
    data = (void *)malloc (datasize);
    if (data == NULL) 
        rb_fatal ("cannot allocate memory");
 
    error = AEGetDescData (desc, data, datasize);
    if (error == noErr) {
        if (to_4cc)
            *(DescType*)data = CFSwapInt32HostToBig (*(DescType*)data);
        retval = rb_str_new (data, datasize);
    }
    else {
        retval = Qnil;
    }

    if (!NIL_P (coerce_type))
        AEDisposeDesc (&coerced_desc); 
    free (data);

    if (error != noErr)
        rb_raise (rb_eRuntimeError, "Cannot get desc data : %s (%d)", 
                  error_code_to_string (error), error);
    
    return retval; 
}
Beispiel #2
0
int
load_param_values(struct SSettings* psettings)
{
    assert(psettings != NULL);
    assert(psettings->val_path != NULL);
    assert(psettings->values == NULL);
    
    psettings->values = smap_create();
    if (psettings->values == NULL)
    {
        print_error(errNoMemory);
        return 0;
    }
    
    char* err = NULL;
    EMistErrorCode ec = mist_load_config_file(psettings->val_path, 
        psettings->values, &err);
    
    if (ec != MIST_OK)
    {
        // psettings->values may contain some data now. It is OK, the data
        // will be destroyed when cleanup_settings() is called.
        print_error(errFailedToLoadValues, psettings->val_path,
            (err != NULL ? err : error_code_to_string(ec)));
        free(err);
        return 0;
    }
    return 1;
}
Beispiel #3
0
int	vrpn_Nikon_Controls::reset(void)
{
	unsigned char	inbuf[256];
	int	ret;
	char	errmsg[256];
	char	cmd[256];
	double	response_pos; //< Where the focus says it is.

	//-----------------------------------------------------------------------
	// Sleep a second and then drain the input buffer to make sure we start
	// with a fresh slate.
	vrpn_SleepMsecs(1000);
	vrpn_flush_input_buffer(serial_fd);

	//-----------------------------------------------------------------------
	// Send the command to request the focus.  Then wait 1 second for a response
	sprintf(cmd, "rSPR\r");
	if (vrpn_write_characters(serial_fd, (unsigned char *)cmd, strlen(cmd)) != (int)strlen(cmd)) {
	  fprintf(stderr,"vrpn_Nikon_Controls::reset(): Cannot send focus request\n");
	  return -1;
	}
	vrpn_SleepMsecs(1000);

	//-----------------------------------------------------------------------
	// Read the response from the camera and then see if it is a good response,
	// an error message, or nothing.

	ret = vrpn_read_available_characters(serial_fd, inbuf, sizeof(inbuf));
	if (ret < 0) {
	  perror("vrpn_Nikon_Controls::reset(): Error reading position from device");
	  return -1;
	}
	if (ret == 0) {
	  fprintf(stderr, "vrpn_Nikon_Controls::reset(): No characters when reading position from device\n");
	  return -1;
	}
	inbuf[ret] = '\0';  //< Null-terminate the input string
	ret = parse_focus_position_response((char *)inbuf, response_pos);
	if (ret < 0) {
	  fprintf(stderr,"vrpn_Nikon_Controls::reset(): Error reading focus: %s\n",
	    error_code_to_string((int)(response_pos)));
	  return -1;
	}
	if (ret != 1) {
	  fprintf(stderr,"vrpn_Nikon_Controls::reset(): Unexpected response to focus request\n");
	  return -1;
	}
	channel[0] = response_pos;

	sprintf(errmsg,"Focus reported (this is good)");
	VRPN_MSG_WARNING(errmsg);

	// We're now waiting for any responses from devices
	status = STATUS_SYNCING;

	VRPN_MSG_WARNING("reset complete (this is good)");

	vrpn_gettimeofday(&timestamp, NULL);	// Set watchdog now
	return 0;
}
Beispiel #4
0
static VALUE
rbosa_elementlist_new (int argc, VALUE *argv, VALUE self)
{
    OSErr           error;
    AEDescList      list;
    VALUE           ary;
    int             i;

    rb_scan_args (argc, argv, "01", &ary);

    if (!NIL_P (ary))
        Check_Type (ary, T_ARRAY);

    error = AECreateList (NULL, 0, false, &list);
    if (error != noErr) 
        rb_raise (rb_eRuntimeError, "Cannot create Apple Event descriptor list : %s (%d)", 
                  error_code_to_string (error), error);

    if (!NIL_P (ary)) {
        for (i = 0; i < RARRAY (ary)->len; i++)
            __rbosa_elementlist_add (&list, RARRAY (ary)->ptr[i], i + 1); 
    }
    
    return rbosa_element_make (self, &list, Qnil);
}
Beispiel #5
0
static void
__rbosa_elementlist_add (AEDescList *list, VALUE element, long pos)
{
    OSErr   error;

    error = AEPutDesc (list, pos, rbosa_element_aedesc (element));
    if (error != noErr)
        rb_raise (rb_eRuntimeError, "Cannot add given descriptor : %s (%d)", 
                  error_code_to_string (error), error);
}
Beispiel #6
0
static void
__rbosa_raise_potential_app_error (AEDesc *reply)
{
    OSErr   error;
    AEDesc  errorNumDesc;
    AEDesc  errorStringDesc;
    int     errorNum;
    const char *  errorMsg;
    char    exception[128];

    if (AEGetParamDesc (reply, keyErrorNumber, typeSInt32, &errorNumDesc) != noErr)
        return; 

    if (AEGetDescData (&errorNumDesc, &errorNum, sizeof errorNum) != noErr) {
        AEDisposeDesc (&errorNumDesc);
        return;
    }

    if (errorNum == noErr)
      return;

    /* The reply is an application error. */

    errorMsg = error_code_to_string(errorNum);
    if (errorMsg == NULL)
        errorMsg = "Unknown error";

    exception[0] = '\0';
    error = AEGetParamDesc (reply, keyErrorString, typeChar, &errorStringDesc);
    if (error == noErr) {
        Size size;

        size = AEGetDescDataSize (&errorStringDesc);
        if (size > 0) {
            char *msg;

            msg = (char *)malloc (size + 1);
            if (msg != NULL) {
                if (AEGetDescData (&errorStringDesc, msg, size) == noErr) {
                    msg[size] = '\0';
                    snprintf (exception, sizeof exception, "application returned error: %s (%d), with message: %s", errorMsg, errorNum, msg);
                }
                free (msg);
            }
        }
        AEDisposeDesc (&errorStringDesc);
    }

    if (exception[0] == '\0')
        snprintf (exception, sizeof exception, "application returned error: %s (%d)", errorMsg, errorNum);

    AEDisposeDesc (&errorNumDesc);

    rb_raise (rb_eRuntimeError, exception);
}
Beispiel #7
0
static int
__rbosa_elementrecord_set (VALUE key, VALUE value, AEDescList *list)
{
    OSErr       error;

    error = AEPutKeyDesc (list, RVAL2FOURCHAR (key), rbosa_element_aedesc (value));
    if (error != noErr) 
        rb_raise (rb_eRuntimeError, "Cannot set value %p for key %p of record %p: %s (%d)", 
                  value, key, list, error_code_to_string (error), error);
 
    return ST_CONTINUE;
}
Beispiel #8
0
static long
__rbosa_elementlist_count (AEDescList *list)
{
    OSErr   error;
    long    count;

    error = AECountItems (list, &count);
    if (error != noErr)
        rb_raise (rb_eRuntimeError, "Cannot count items : %s (%d)", 
                  error_code_to_string (error), error);

    return count;
}
Beispiel #9
0
static VALUE
rbosa_element_dup (VALUE self, VALUE element)
{
    AEDesc *  desc;
    AEDesc    new_desc;
    OSErr     error;

    desc = rbosa_element_aedesc (element);
    error = AEDuplicateDesc (desc, &new_desc);
    if (error != noErr) 
        rb_raise (rb_eArgError, "Cannot duplicate element : %s (%d)", 
                  error_code_to_string (error), error);

    return rbosa_element_make (self, &new_desc, Qnil); 
}
Beispiel #10
0
static VALUE
__rbosa_elementlist_get (VALUE self, long index, AEKeyword *keyword)
{
    OSErr       error;
    AEDesc      desc;

    error = AEGetNthDesc ((AEDescList *)rbosa_element_aedesc (self),
                          index + 1,
                          typeWildCard,
                          keyword,
                          &desc);
    
    if (error != noErr)
        rb_raise (rb_eRuntimeError, "Cannot get desc at index %d : %s (%d)", 
                  index, error_code_to_string (error), error);

    return rbosa_element_make (cOSAElement, &desc, rb_ivar_get (self, sApp));
}
Beispiel #11
0
static VALUE
rbosa_element_new (VALUE self, VALUE type, VALUE value)
{
    FourCharCode    ffc_type;
    OSErr           error;
    const char *    c_value;
    unsigned        c_value_size;
    AEDesc          desc;

    ffc_type = RVAL2FOURCHAR (type);

    if (NIL_P (value)) {
        c_value = NULL;
        c_value_size = 0;
    }
    else if (rb_obj_is_kind_of (value, rb_cInteger)) {
        FourCharCode code;

        code = NUM2INT (value);
        c_value = (const char *)&code;
        c_value_size = sizeof (FourCharCode);
    }  
    else if (ffc_type == 'alis') {
        AliasHandle     alias;

        rbobj_to_alias_handle (value, &alias);
        
        c_value = (const char *)*alias;
        c_value_size = GetHandleSize ((Handle)alias);
    }
    else {
        Check_Type (value, T_STRING);
        c_value = RSTRING (value)->ptr;
        c_value_size = RSTRING (value)->len;
    }

    error = AECreateDesc (ffc_type, c_value, c_value_size, &desc);
    if (error != noErr)     
        rb_raise (rb_eArgError, "Cannot create Apple Event descriptor from type '%s' value '%s' : %s (%d)", 
                  RVAL2CSTR (type), c_value, error_code_to_string (error), error);

    return rbosa_element_make (self, &desc, Qnil);
}
Beispiel #12
0
static VALUE
rbosa_element_new_os (VALUE self, VALUE desired_class, VALUE container, VALUE key_form, VALUE key_data)
{
    OSErr   error;
    AEDesc  obj_specifier;   

    error = CreateObjSpecifier (RVAL2FOURCHAR (desired_class),
                                rbosa_element_aedesc (container),
                                RVAL2FOURCHAR (key_form),
                                rbosa_element_aedesc (key_data),
                                false,
                                &obj_specifier);

    if (error != noErr) 
        rb_raise (rb_eArgError, "Cannot create Apple Event object specifier for desired class '%s' : %s (%d)", 
                  RVAL2CSTR (desired_class), error_code_to_string (error), error);

    return rbosa_element_make (self, &obj_specifier, Qnil);
}
Beispiel #13
0
int
generate_output(struct SSettings* psettings)
{
    assert(psettings != NULL);
    assert(psettings->main_tg != NULL);
    assert(psettings->is_simplified_mode || psettings->path_tg != NULL);
    assert(psettings->values != NULL);
    
    char* err = NULL;
    EMistErrorCode ec = MIST_OK;
    
    if (psettings->is_simplified_mode)
    {
        assert(psettings->path_tg == NULL);
        ec = mist_tg_set_values_impl(psettings->main_tg, psettings->values);
        if (ec != MIST_OK)
        {
            print_error(errGenResultFailed, error_code_to_string(ec));
            return 0;
        }
        
        // generate the contents of the resulting document
        CGrowingArray* vals = mist_tg_evaluate_impl(psettings->main_tg);
        if (vals == NULL)
        {
            print_error(errGenResultFailed, errNoMemory);
            return 0;
        }
        assert(grar_get_size(vals) > 0);
        
        // there should be a single resulting value
        if (grar_get_size(vals) != 1)
        {
            print_error(errGenResultFailed, errMultivaluedResult);
            return 0;
        }
        
        // output the result to stdout
        const char* contents = grar_get_element(vals, const char*, 0);
        assert(contents != NULL);
        printf("%s", contents);
    }
Beispiel #14
0
static VALUE
rbosa_elementrecord_new (int argc, VALUE *argv, VALUE self)
{
    OSErr           error;
    AEDescList      list;
    VALUE           hash;

    rb_scan_args (argc, argv, "01", &hash);

    if (!NIL_P (hash))
        Check_Type (hash, T_HASH);

    error = AECreateList (NULL, 0, true, &list);
    if (error != noErr) 
        rb_raise (rb_eRuntimeError, "Cannot create Apple Event descriptor list : %s (%d)", 
                  error_code_to_string (error), error);

    if (!NIL_P (hash)) 
        rb_hash_foreach (hash, __rbosa_elementrecord_set, (VALUE)&list);
    
    return rbosa_element_make (self, &list, Qnil);
}
Beispiel #15
0
void attribute::set( const std::string& value )
{
    VmbErrorType status = VmbErrorSuccess;

    if( comma::verbose )
    {
        std::cerr << "Setting \"" << name_ << "\" feature";
        if( !value.empty() )
        {
            std::cerr << " to " << type_as_string() << " value \"" << value << "\"";
        }
        std::cerr << std::endl;
    }

    switch( type_ )
    {
        case VmbFeatureDataUnknown:               // Unknown feature type
            COMMA_THROW( comma::exception, "unknown feature \"" << name_ << "\"" );
            break;
        case VmbFeatureDataInt:                   // 64 bit integer feature
            status = feature_->SetValue( boost::lexical_cast< VmbInt64_t >( value ));
            break;
        case VmbFeatureDataFloat:                 // 64 bit floating point feature
            status = feature_->SetValue( boost::lexical_cast< double >( value ));
            break;
        case VmbFeatureDataEnum:                  // Enumeration feature
        case VmbFeatureDataString:                // String feature
            status = feature_->SetValue( value.c_str() );
            break;
        case VmbFeatureDataBool:                  // Boolean feature
            status = feature_->SetValue( boost::lexical_cast< bool >( value ));
            break;
        case VmbFeatureDataCommand:               // Command feature
            status = feature_->RunCommand();
            if( status == VmbErrorSuccess )
            {
                bool is_command_done = false;
                do
                {
                    status = feature_->IsCommandDone( is_command_done );
                    if( status != VmbErrorSuccess ) break;
                } while( !is_command_done );
            }
            break;
        case VmbFeatureDataRaw:                   // Raw (direct register access) feature
            COMMA_THROW( comma::exception, "feature \"" << name_ << "\" is a direct register - setting this type is unsupported" );
            break;
        case VmbFeatureDataNone:                  // Feature with no data
            COMMA_THROW( comma::exception, "feature \"" << name_ << "\" has no data" );
            break;
    }

    if( status != VmbErrorSuccess )
    {
        std::string allowed_error_msg;
        if( !allowed_values_.empty() )
        {
            allowed_error_msg = "; allowed values: ";
            allowed_error_msg += allowed_values_as_string();
        }
        COMMA_THROW( comma::exception
                   , "failed to set feature \"" << name_ << " to " << value
                   << ", Error: " << status << ": " << error_code_to_string( status )
                   << allowed_error_msg );
    }
}
Beispiel #16
0
static VALUE
rbosa_app_send_event (VALUE self, VALUE event_class, VALUE event_id, VALUE params, VALUE need_retval)
{
    OSErr       error;
    AppleEvent  ae;
    AppleEvent  reply;
    VALUE       rb_timeout;
    SInt32      timeout;
    VALUE       rb_reply;
    unsigned    has_direct_param;

    error = AECreateAppleEvent (RVAL2FOURCHAR (event_class),
                                RVAL2FOURCHAR (event_id),
                                rbosa_element_aedesc (self),
                                kAutoGenerateReturnID,
                                kAnyTransactionID,
                                &ae);
    if (error != noErr)
        rb_raise (rb_eArgError, "Cannot create Apple Event '%s%s' : %s (%d)", 
                  RVAL2CSTR (event_class), RVAL2CSTR (event_id), error_code_to_string (error), error);

    has_direct_param = 0;
    if (!NIL_P (params)) {
        unsigned    i;

        for (i = 0; i < RARRAY (params)->len; i++) {
            VALUE   ary;
            VALUE   type;
            VALUE   element;
            FourCharCode code;

            ary = RARRAY (params)->ptr[i];
            if (NIL_P (ary) || RARRAY (ary)->len != 2)
                continue;

            type = RARRAY (ary)->ptr[0];
            element = RARRAY (ary)->ptr[1];
            code = RVAL2FOURCHAR (type);

            if (code == '----')
                has_direct_param = 1;

            error = AEPutParamDesc (&ae, RVAL2FOURCHAR (type), rbosa_element_aedesc (element));
            if (error != noErr) { 
                AEDisposeDesc (&ae); 
                rb_raise (rb_eArgError, "Cannot add Apple Event parameter '%s' : %s (%d)", 
                          RVAL2CSTR (type), error_code_to_string (error), error);
            }
        } 
    }

    rb_timeout = rb_iv_get (mOSA, "@timeout");
    timeout = NIL_P (rb_timeout) ? kAEDefaultTimeout : NUM2INT (rb_timeout);

    if (has_direct_param == 0)
        AEPutAttributePtr (&ae, 'subj', typeNull, NULL, 0);

    error = AESend (&ae, &reply, (RVAL2CBOOL(need_retval) ? kAEWaitReply : kAENoReply) | kAECanInteract | kAECanSwitchLayer,
                    kAENormalPriority, timeout, NULL, NULL);

    AEDisposeDesc (&ae); 

    if (error != noErr)
        rb_raise (rb_eRuntimeError, "Cannot send Apple Event '%s%s' : %s (%d)", 
                  RVAL2CSTR (event_class), RVAL2CSTR (event_id), error_code_to_string (error), error);

    __rbosa_raise_potential_app_error (&reply);

    if (RTEST (need_retval)) {
        AEDesc  replyObject;

        AEGetParamDesc (&reply, keyDirectObject, typeWildCard, &replyObject);

        rb_reply = rbosa_element_make (cOSAElement, &replyObject, self);
    }
    else {
        rb_reply = Qnil;
    }

    AEDisposeDesc (&reply);
        
    return rb_reply;
}
Beispiel #17
0
 string_t error_t::code_to_string() const {
     return error_code_to_string(error_code);
 }
Beispiel #18
0
int vrpn_Nikon_Controls::get_report(void)
{
   int ret;		// Return value from function call to be checked

   //--------------------------------------------------------------------
   // If we're SYNCing, then the next character we get should be the start
   // of a report.  If we recognize it, go into READing mode and tell how
   // many characters we expect total. If we don't recognize it, then we
   // must have misinterpreted a command or something; reset
   // and start over
   //--------------------------------------------------------------------

   if (status == STATUS_SYNCING) {
      // Try to get a character.  If none, just return.
      if (vrpn_read_available_characters(serial_fd, _buffer, 1) != 1) {
      	return 0;
      }

      // See if we recognize the character as one of the ones making
      // up a report.  If not, then we need to continue syncing.
      if (strchr(VALID_REPORT_CHARS, _buffer[0]) == NULL) {
	VRPN_MSG_WARNING("Syncing");
	return 0;
      }

      // Got the first character of a report -- go into READING mode
      // and record that we got one character at this time. The next
      // bit of code will attempt to read the rest of the report.
      // The time stored here is as close as possible to when the
      // report was generated.
      _bufcount = 1;
      vrpn_gettimeofday(&timestamp, NULL);
      status = STATUS_READING;
#ifdef	VERBOSE2
      printf("... Got the 1st char\n");
#endif
   }

   //--------------------------------------------------------------------
   // Read a character at a time from the serial port, storing them
   // in the buffer.  Do this until we either run out of characters to
   // read or else find [CR][LF] along the way, which indicates the end
   // of a command.
   //--------------------------------------------------------------------

   do {
     ret = vrpn_read_available_characters(serial_fd, &_buffer[_bufcount], 1);
     if (ret == -1) {
	  VRPN_MSG_ERROR("Error reading");
	  status = STATUS_RESETTING;
	  return 0;
     }
     _bufcount += ret;
#ifdef	VERBOSE2
     if (ret != 0) printf("... got %d characters (%d total)\n",ret, _bufcount);
#endif

     // See if the last characters in the buffer are [CR][LF].  If so, then
     // we have a full report so null-terminate and go ahead and parse
     if ( (_bufcount > 2) &&
	  (_buffer[_bufcount-2] == '\r') &&
	  (_buffer[_bufcount-1] == '\n') ) {
       _buffer[_bufcount] = '\0';
       break;
     }

     // If we have a full buffer, then we've gotten into a bad way.
     if (_bufcount >= sizeof(_buffer) - 1) {
       VRPN_MSG_ERROR("Buffer full when reading");
       status = STATUS_RESETTING;
       return 0;
     }
   } while (ret != 0);
   if (ret == 0) {
     return 0;
   }

   //--------------------------------------------------------------------
   // We now have enough characters to make a full report. Check it by
   // trying to parse it.  If it is not valid, then we return to
   // synch mode and ignore this report. A well-formed report has
   // on of VALID_REPORT_CHARS as its command.  We expect these responses
   // either set the response position (due to a query) or to be valid
   // but not set the response position (due to a position move request,
   // for which we store the requested position into the value).
   //--------------------------------------------------------------------

   double response_pos;
   ret = parse_focus_position_response((const char *)_buffer, response_pos);
   if (ret <= 0) {
     fprintf(stderr,"Bad response from nikon [%s], syncing\n", _buffer);
     fprintf(stderr,"  (%s)\n", error_code_to_string((int)response_pos));
     status = STATUS_SYNCING;
     return 0;
   }
   // Type 3 returns mean "in progress" for some function.  Ignore this
   // report.
   if (ret == 3) {
     status = STATUS_SYNCING;
     _bufcount = 0;
     return 1;
   }

   // Type 2 means that the command we issued before has completed -- put
   // the value we requested for the focus into the response since that is
   // where we are now.
   if (ret == 2) {  // We must have gotten where we are going.
     response_pos = _requested_focus;
   }

   //--------------------------------------------------------------------
   // Done with the decoding, send the reports and go back to syncing
   //--------------------------------------------------------------------

   channel[0] = response_pos;
   report_changes();
   status = STATUS_SYNCING;
   _bufcount = 0;

   return 1;
}