Beispiel #1
0
void Wallpaper::options_changed( const po::variables_map& vm, const po::variables_map& old )
{
    if ( Utility::updated<size_t>( wallpaper_frequency, vm, old ) )
    {
        m_frequence = vm[wallpaper_frequency].as<size_t>();
    }

    if ( Utility::updated<std::wstring>( wallpaper_disable, vm, old ) )
    {
        m_disable = ( L"true" == vm[wallpaper_disable].as<std::wstring>() );

        if ( !m_disable && m_directory.empty()  )
        {
            initialize();
            handle_start();
        }

        if ( !m_disable && m_listening )
        {
            boost::thread t( boost::bind( &Wallpaper::listen_thread_function, this ) );
        }

        if ( m_disable && !m_directory.empty() )
        {
            Utility::set_system_wallpaper( "C:\\Windows\\Web\\Wallpaper\\Bing\\BorneoRainforest_ROW10198412592_1920x1200.jpg" );
        }
    }
}
Beispiel #2
0
int timer_start (evHandle *handle,
                 uint64_t timeout,
                 uint64_t repeat){
    
    if (_is_active(handle)){
        timer_stop(handle);
    }
    
    evTimer *timer;
    if (!handle->type) {
        timer = malloc(sizeof(*timer));
    } else {
        timer = handle->ev;
    }

    uint64_t clamped_timeout = handle->loop->time + timeout;
    if (clamped_timeout < timeout) assert(0);
    timer->timeout = clamped_timeout;
    timer->repeat = repeat;
    timer->start_id = handle->loop->timer_counter++;
    timer->handle = handle;
    handle->ev = timer;
    handle->type = EV_TIMER;
    heap_insert((struct heap*) &handle->loop->timer_heap,
              (struct heap_node*) &timer->heap_node,
              timer_less_than);
    
    handle_start(handle);
    return 1;
}
/**
 * Handle a request and send a response
 *
 * @param cls unused
 * @param other the sender
 * @param message the message
 * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
 */
static int
core_receive_handler (void *cls,
		      const struct GNUNET_PeerIdentity *other,
		      const struct GNUNET_MessageHeader *message)
{
	if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
	{
			GNUNET_break (0);
			return GNUNET_SYSERR;
	}

	switch (ntohs (message->type)) {
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
			handle_request (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
			handle_response (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START:
			handle_start (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK:
			handle_start_ack (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP:
			handle_stop (other, message);
			break;
		default:
			break;
	}

	return GNUNET_OK;
}
Beispiel #4
0
//TODO ADD OPTIMISATIONS DURING KEY MATCHING
int json_parse(json_parser *parser,
               const char *input_buff, size_t buff_sz){
	int i;

    //The parser looks at one char at a time.
    //the character is only advanced by this for loop.
	for (; parser->buff_offset < buff_sz
            && input_buff[parser->buff_offset] != '\0';
           parser->buff_offset++) {
		char c;
        int res;

		c = input_buff[parser->buff_offset];


        switch (parser->state){
            case JSON_STATE_BEGIN_VAL_OR_KEY:
                res = handle_begin_val_or_key(parser, c); break;
            case JSON_STATE_INT_ELEM:
                res = handle_int_elem(parser, c); break;
            case JSON_STATE_TRUE_ELEM:
                    case JSON_STATE_FALSE_ELEM:
                    case JSON_STATE_NULL_ELEM:
                res = handle_special_elem(parser, c); break;
            case JSON_STATE_KEY_OR_STR: case JSON_STATE_KEY_OR_STR_ESCAPE:
                res = handle_key_or_str(parser, c); break;
            case JSON_STATE_KEY_OR_STR_END:
                res = handle_key_or_str_end(parser, c); break;
            case JSON_STATE_TYPE:
                res = handle_type(parser, c); break;
            case JSON_STATE_INT:
                res = handle_int(parser, c); break;
            case JSON_STATE_TRUE: case JSON_STATE_FALSE: case JSON_STATE_NULL:
                res = handle_special(parser, c); break;
            case JSON_STATE_STR: case JSON_STATE_STR_ESCAPE:
                res = handle_str(parser, c); break;
            case JSON_STATE_END_VAL:
                res = handle_end_val(parser, c); break;
            case JSON_STATE_START:
                res = handle_start(parser, c); break;

            //parsing stop if we are done or if there was an error
            if (res <= 0)
                return res;
        }
	}

    //ran out of buffer, parser is either done or needs more input
    if (parser->level == 0)
        return JSON_DONE;
    return JSON_CONTINUE;
}
Beispiel #5
0
static void handle_message(message_t *message, void *d)
{
  driver_exec_t *driver = (driver_exec_t*) d;

  switch(message->type)
  {
    case MESSAGE_START:
      handle_start(driver);
      break;

    case MESSAGE_SESSION_CREATED:
      handle_session_created(driver, message->message.session_created.session_id);
      break;

    case MESSAGE_DATA_IN:
      handle_data_in(driver, message->message.data_in.data, message->message.data_in.length);
      break;

    default:
      LOG_FATAL("driver_exec received an invalid message!");
      exit(1);
  }
}
Beispiel #6
0
void i2c_slave_int_handler() {
    unsigned char i2c_data;
    unsigned char data_read = 0;
    unsigned char data_written = 0;
    unsigned char msg_ready = 0;
    unsigned char msg_to_send = 0;
    unsigned char overrun_error = 0;
    unsigned char error_buf[3];

    // clear SSPOV
    if (SSPCON1bits.SSPOV == 1) {
        SSPCON1bits.SSPOV = 0;
        // we failed to read the buffer in time, so we know we
        // can't properly receive this message, just put us in the
        // a state where we are looking for a new message
        ic_ptr->status = I2C_IDLE;
        overrun_error = 1;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_OVERRUN;
    }
    // read something if it is there
    if (SSPSTATbits.BF == 1) {
        i2c_data = SSPBUF;
        data_read = 1;
    }

    if (!overrun_error) {
        switch (ic_ptr->status) {
        case I2C_IDLE:
        {
            // ignore anything except a start
            if (SSPSTATbits.S == 1) {
                handle_start(data_read);
                // if we see a slave read, then we need to handle it here
                if (ic_ptr->status == I2C_SLAVE_SEND) {
                    data_read = 0;
                    msg_to_send = 1;
                }
            }
            break;
        }
        case I2C_STARTED:
        {
            // in this case, we expect either an address or a stop bit
            if (SSPSTATbits.P == 1) {
                // we need to check to see if we also read an
                // address (a message of length 0)
                ic_ptr->event_count++;
                if (data_read) {
                    if (SSPSTATbits.D_A == 0) {
                        msg_ready = 1;
                    } else {
                        ic_ptr->error_count++;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                    }
                }
                ic_ptr->status = I2C_IDLE;
            } else if (data_read) {
                ic_ptr->event_count++;
                if (SSPSTATbits.D_A == 0) {
                    if (SSPSTATbits.R_W == 0) { // slave write
                        ic_ptr->status = I2C_RCV_DATA;
                    } else { // slave read
                        ic_ptr->status = I2C_SLAVE_SEND;
                        msg_to_send = 1;
                        // don't let the clock stretching bit be let go
                        data_read = 0;
                    }
                } else {
                    ic_ptr->error_count++;
                    ic_ptr->status = I2C_IDLE;
                    ic_ptr->error_code = I2C_ERR_NODATA;
                }
            }
            break;
        }
        case I2C_SLAVE_SEND:
        {
            if (ic_ptr->outbufind < ic_ptr->outbuflen) {
                SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
                ic_ptr->outbufind++;
                data_written = 1;
            } else {
                // we have nothing left to send
                ic_ptr->status = I2C_IDLE;
            }
            break;
        }
        case I2C_RCV_DATA:
        {
            // we expect either data or a stop bit or a (if a restart, an addr)
            if (SSPSTATbits.P == 1) {
                // we need to check to see if we also read data
                ic_ptr->event_count++;
                if (data_read) {
                    if (SSPSTATbits.D_A == 1) {
                        ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                        ic_ptr->buflen++;
                        msg_ready = 1;
                    } else {
                        ic_ptr->error_count++;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                        ic_ptr->status = I2C_IDLE;
                    }
                } else {
                    msg_ready = 1;
                }
                ic_ptr->status = I2C_IDLE;
            } else if (data_read) {
                ic_ptr->event_count++;
                if (SSPSTATbits.D_A == 1) {
                    ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                    ic_ptr->buflen++;
                } else { /* a restart */
                    if (SSPSTATbits.R_W == 1) {
                        ic_ptr->status = I2C_SLAVE_SEND;
                        msg_ready = 1;
                        msg_to_send = 1;
                        // don't let the clock stretching bit be let go
                        data_read = 0;
                    } else { /* bad to recv an address again, we aren't ready */
                        ic_ptr->error_count++;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                        ic_ptr->status = I2C_IDLE;
                    }
                }
            }
            break;
        }
        }
    }

    // release the clock stretching bit (if we should)
    if (data_read || data_written) {
        // release the clock
        if (SSPCON1bits.CKP == 0) {
            SSPCON1bits.CKP = 1;
        }
    }

    // must check if the message is too long, if
    if ((ic_ptr->buflen > MAXI2CBUF - 2) && (!msg_ready)) {
        ic_ptr->status = I2C_IDLE;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_MSGTOOLONG;
    }

    if (msg_ready) {
        ic_ptr->buffer[ic_ptr->buflen] = ic_ptr->event_count;
        ToMainHigh_sendmsg(ic_ptr->buflen + 1, MSGT_I2C_DATA, (void *) ic_ptr->buffer);
        ic_ptr->buflen = 0;
    } else if (ic_ptr->error_count >= I2C_ERR_THRESHOLD) {
        error_buf[0] = ic_ptr->error_count;
        error_buf[1] = ic_ptr->error_code;
        error_buf[2] = ic_ptr->event_count;
        ToMainHigh_sendmsg(sizeof (unsigned char) *3, MSGT_I2C_DBG, (void *) error_buf);
        ic_ptr->error_count = 0;
    }
    if (msg_to_send) {
        int length = 0;

        // send to the queue to *ask* for the data to be sent out
        //ToMainHigh_sendmsg(0, MSGT_I2C_RQST, (void *) ic_ptr->buffer);
        if(ic_ptr->buffer[0] == 0xAA) {
            length = 5;

            // Creates the message type and bitmask char values
            unsigned char messageType = 0x01, bitmask = 0x17, checksum;

            // This will hold the message that we want the slave to send
            unsigned char message[5];

            // Stores the appropriate values in the message to be sent
            message[0] = messageType;
            message[1] = adcbuffer[1];
            message[2] = adcbuffer[2];
            message[3] = adcbuffer[3];

            // Creates the checksum
            checksum = message[1] + message[2] + message[3];
            message[4] = checksum & bitmask;

            start_i2c_slave_reply(length, message);
            //adcbuffer[0] = 0; // reset count after send
        } else if(ic_ptr->buffer[0] == 0xBA) {
            // motor stuff
            length = 5;

            unsigned char motormsg[5] = {0x03, 0x04, ((0x04) & 0x17), 0x00, 0x00};
            start_i2c_slave_reply(length, motormsg);

            unsigned char motorcomm[2] = {0x9F, 0x1F};
            if(ic_ptr->buffer[1] == 0xFF) {

                uart_trans(2, motorcomm);
            } else if(ic_ptr->buffer[1] == 0x00) {
                motorcomm[0] = 0x00;
                uart_trans(1, motorcomm);
            }

        }
        msg_to_send = 0;
    }
}
static int handle_start_array(void *ctx)
{
  return handle_start(ctx, 1);
}
static int handle_start_map(void *ctx)
{
  return handle_start(ctx, 0);
}
Beispiel #9
0
int
main(
    int		argc,
    char **	argv)
{
    char *logfname;
    char *conf_logdir;
    FILE *logfile;
    config_overrides_t *cfg_ovr = NULL;
    char *cfg_opt = NULL;

    /*
     * Configure program for internationalization:
     *   1) Only set the message locale for now.
     *   2) Set textdomain for all amanda related programs to "amanda"
     *      We don't want to be forced to support dozens of message catalogs.
     */  
    setlocale(LC_MESSAGES, "C");
    textdomain("amanda"); 

    safe_fd(-1, 0);

    set_pname("amlogroll");

    dbopen(DBG_SUBDIR_SERVER);

    add_amanda_log_handler(amanda_log_stderr);

    /* Process options */
    cfg_ovr = extract_commandline_config_overrides(&argc, &argv);

    if (argc >= 2) {
	cfg_opt = argv[1];
    }

    /* read configuration files */

    set_config_overrides(cfg_ovr);
    config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);

    if (config_errors(NULL) >= CFGERR_WARNINGS) {
	config_print_errors();
	if (config_errors(NULL) >= CFGERR_ERRORS) {
	    g_critical(_("errors processing config file"));
	}
    }

    safe_cd(); /* must happen after config_init */

    check_running_as(RUNNING_AS_DUMPUSER);

    dbrename(get_config_name(), DBG_SUBDIR_SERVER);

    conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
    logfname = vstralloc(conf_logdir, "/", "log", NULL);
    amfree(conf_logdir);

    if((logfile = fopen(logfname, "r")) == NULL) {
	error(_("could not open log %s: %s"), logfname, strerror(errno));
	/*NOTREACHED*/
    }
    amfree(logfname);

    add_amanda_log_handler(amanda_log_trace_log);

    while(get_logline(logfile)) {
	if(curlog == L_START) {
	    handle_start();
	    if(datestamp != NULL) {
		break;
	    }
	}
    }
    afclose(logfile);
 
    log_rename(datestamp);

    amfree(datestamp);

    dbclose();

    return 0;
}
Beispiel #10
0
void i2c_int_handler() {
    unsigned char i2c_data;
    unsigned char data_read = 0;
    unsigned char data_written = 0;
    unsigned char msg_ready = 0;
    unsigned char msg_to_send = 0;
    unsigned char overrun_error = 0;
    unsigned char error_buf[3];

    // clear SSPOV
    if (SSPCON1bits.SSPOV == 1) {
        SSPCON1bits.SSPOV = 0;
        // we failed to read the buffer in time, so we know we
        // can't properly receive this message, just put us in the
        // a state where we are looking for a new message
        ic_ptr->status = I2C_IDLE;
        overrun_error = 1;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_OVERRUN;
    }
    // read something if it is there
    if (SSPSTATbits.BF == 1) {
        i2c_data = SSPBUF;
        data_read = 1;
    }

    if (!overrun_error) {
        switch (ic_ptr->status) {
            case I2C_IDLE:
            {
                // ignore anything except a start
                if (SSPSTATbits.S == 1) {
                    handle_start(data_read);
                    // if we see a slave read, then we need to handle it here
                    if (ic_ptr->status == I2C_SLAVE_SEND) {
                        data_read = 0;
                        msg_to_send = 1;
                    }
                }
                break;
            }
            case I2C_STARTED:
            {
                // in this case, we expect either an address or a stop bit
                if (SSPSTATbits.P == 1) {
                    // we need to check to see if we also read an
                    // address (a message of length 0)
                    ic_ptr->event_count++;
                    if (data_read) {
                        if (SSPSTATbits.D_A == 0) {
                            msg_ready = 1;
                        } else {
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                        }
                    }
                    ic_ptr->status = I2C_IDLE;
                } else if (data_read) {
                    ic_ptr->event_count++;
                    if (SSPSTATbits.D_A == 0) {
                        if (SSPSTATbits.R_W == 0) { // slave write
                            ic_ptr->status = I2C_RCV_DATA;
                        } else { // slave read
                            ic_ptr->status = I2C_SLAVE_SEND;
                            msg_to_send = 1;
                            // don't let the clock stretching bit be let go
                            data_read = 0;
                        }
                    } else {
                        ic_ptr->error_count++;
                        ic_ptr->status = I2C_IDLE;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                    }
                }
                break;
            }
            case I2C_SLAVE_SEND:
            {
                if (ic_ptr->outbufind < ic_ptr->outbuflen) {
//            setDBG(DBG2);
//            resetDBG(DBG2);
                    SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
                    ic_ptr->outbufind++;
                    data_written = 1;
                } else {
                    // we have nothing left to send
                    ic_ptr->status = I2C_IDLE;
                }
                break;
            }
            case I2C_RCV_DATA:
            {
                // we expect either data or a stop bit or a (if a restart, an addr)
                if (SSPSTATbits.P == 1) {
                    // we need to check to see if we also read data
                    ic_ptr->event_count++;
                    if (data_read) {
                        if (SSPSTATbits.D_A == 1) {
                            ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                            ic_ptr->buflen++;
                            msg_ready = 1;
                        } else {
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                            ic_ptr->status = I2C_IDLE;
                        }
                    } else {
                        msg_ready = 1;
                    }
                    ic_ptr->status = I2C_IDLE;
                } else if (data_read) {
                    ic_ptr->event_count++;
                    if (SSPSTATbits.D_A == 1) {
                        ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                        ic_ptr->buflen++;
                    } else /* a restart */ {
                        if (SSPSTATbits.R_W == 1) {
                            ic_ptr->status = I2C_SLAVE_SEND;
                            msg_ready = 1;
                            msg_to_send = 1;
                            // don't let the clock stretching bit be let go
                            data_read = 0;
                        } else { /* bad to recv an address again, we aren't ready */
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                            ic_ptr->status = I2C_IDLE;
                        }
                    }
                }
                break;
            }
        }
    }

    // release the clock stretching bit (if we should)
    if (data_read || data_written) {
        // release the clock

            //setDBG(DBG5);
            //resetDBG(DBG5);
        if (SSPCON1bits.CKP == 0) {
            SSPCON1bits.CKP = 1;
        }
    }

    // must check if the message is too long, if
    if ((ic_ptr->buflen > MAXI2CBUF - 2) && (!msg_ready)) {
        ic_ptr->status = I2C_IDLE;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_MSGTOOLONG;
    }

    if (msg_ready) {
#ifdef MOTOR_PIC
        if(isMovementCommand(ic_ptr->buffer)){
            setKill();
        }
        else{ //either "done request" or encoder data request
            makeHighPriority(ic_ptr->buffer);
        }
#endif
        if(isHighPriority(ic_ptr->buffer)){
            setBrainDataHP(ic_ptr->buffer);
        }
        else{
            FromI2CInt_sendmsg(ic_ptr->buflen, MSGT_I2C_DATA, (void *) ic_ptr->buffer);
        }
        ic_ptr->buflen = 0;
    } else if (ic_ptr->error_count >= I2C_ERR_THRESHOLD) {
        error_buf[0] = ic_ptr->error_count;
        error_buf[1] = ic_ptr->error_code;
        error_buf[2] = ic_ptr->event_count;
        FromI2CInt_sendmsg(sizeof (unsigned char) *3, MSGT_I2C_DBG, (void *) error_buf);
        ic_ptr->error_count = 0;
    }
    if (msg_to_send) {
#ifdef MOTOR_PIC
        if(!isMovementCommand(ic_ptr->buffer)){ //either "done quest" or encoder data request
            makeHighPriority(ic_ptr->buffer);
        }
#endif
        
        if(isHighPriority(ic_ptr->buffer)){
#if defined(MOTOR_PIC) || defined(SENSOR_PIC)
            handleMessageHP(I2C_COMM, I2C_COMM);
#elif defined(PICMAN)
                    handleMessageHP(I2C_COMM, UART_COMM);
#endif
        }
        else{
            FromI2CInt_sendmsg(0, MSGT_I2C_RQST, (void *) 0);
        }

        //sendRequestedData();
        msg_to_send = 0;
    }
}
Beispiel #11
0
void i2c_slave_int_handler() {
    unsigned char i2c_data;
    unsigned char data_read = 0;
    unsigned char data_written = 0;
    unsigned char msg_ready = 0;
    unsigned char msg_to_send = 0;
    unsigned char overrun_error = 0;
    unsigned char error_buf[3];

    // clear SSPOV
    if (SSPCON1bits.SSPOV == 1) {
        SSPCON1bits.SSPOV = 0;
        // we failed to read the buffer in time, so we know we
        // can't properly receive this message, just put us in the
        // a state where we are looking for a new message
        ic_ptr->status = I2C_IDLE;
        overrun_error = 1;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_OVERRUN;
    }
    // read something if it is there
    if (SSPSTATbits.BF == 1) {
        i2c_data = SSPBUF;
        data_read = 1;
    }

    if (!overrun_error) {
        switch (ic_ptr->status) {
            case I2C_IDLE:
            {
                // ignore anything except a start
                if (SSPSTATbits.S == 1) {
                    handle_start(data_read);
                    // if we see a slave read, then we need to handle it here
                    if (ic_ptr->status == I2C_SLAVE_SEND) {
                        data_read = 0;
                        msg_to_send = 1;
                    }
                }
                break;
            }
            case I2C_STARTED:
            {
                // in this case, we expect either an address or a stop bit
                if (SSPSTATbits.P == 1) {
                    // we need to check to see if we also read an
                    // address (a message of length 0)
                    ic_ptr->event_count++;
                    if (data_read) {
                        if (SSPSTATbits.D_A == 0) {
                            msg_ready = 1;
                        } else {
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                        }
                    }
                    ic_ptr->status = I2C_IDLE;
                } else if (data_read) {
                    ic_ptr->event_count++;
                    if (SSPSTATbits.D_A == 0) {
                        if (SSPSTATbits.R_W == 0) { // slave write
                            ic_ptr->status = I2C_RCV_DATA;
                        } else { // slave read
                            ic_ptr->status = I2C_SLAVE_SEND;
                            msg_to_send = 1;
                            // don't let the clock stretching bit be let go
                            data_read = 0;
                        }
                    } else {
                        ic_ptr->error_count++;
                        ic_ptr->status = I2C_IDLE;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                    }
                }
                break;
            }
            case I2C_SLAVE_SEND:
            {
                if (ic_ptr->outbufind < ic_ptr->outbuflen) {
                    SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
                    ic_ptr->outbufind++;
                    data_written = 1;
                } else {
                    // we have nothing left to send
                    ic_ptr->status = I2C_IDLE;
                }
                break;
            }
            case I2C_RCV_DATA:
            {
                // we expect either data or a stop bit or a (if a restart, an addr)
                if (SSPSTATbits.P == 1) {
                    // we need to check to see if we also read data
                    ic_ptr->event_count++;
                    if (data_read) {
                        if (SSPSTATbits.D_A == 1) {
                            ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                            ic_ptr->buflen++;
                            msg_ready = 1;
                        } else {
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                            ic_ptr->status = I2C_IDLE;
                        }
                    } else {
                        msg_ready = 1;
                    }
                    ic_ptr->status = I2C_IDLE;
                } else if (data_read) {
                    ic_ptr->event_count++;
                    if (SSPSTATbits.D_A == 1) {
                        ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                        ic_ptr->buflen++;
                    } else /* a restart */ {
                        if (SSPSTATbits.R_W == 1) {
                            ic_ptr->status = I2C_SLAVE_SEND;
                            msg_ready = 1;
                            msg_to_send = 1;
                            // don't let the clock stretching bit be let go
                            data_read = 0;
                        } else { /* bad to recv an address again, we aren't ready */
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                            ic_ptr->status = I2C_IDLE;
                        }
                    }
                }
                break;
            }
        }
    }

    // release the clock stretching bit (if we should)
    if (data_read || data_written) {
        // release the clock
        if (SSPCON1bits.CKP == 0) {
            SSPCON1bits.CKP = 1;
        }
    }

    // must check if the message is too long, if
    if ((ic_ptr->buflen > MAXI2CBUF - 2) && (!msg_ready)) {
        ic_ptr->status = I2C_IDLE;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_MSGTOOLONG;
    }

    if (msg_ready) {
        ic_ptr->buffer[ic_ptr->buflen] = ic_ptr->event_count;
        ToMainHigh_sendmsg(ic_ptr->buflen + 1, MSGT_I2C_DATA, (void *) ic_ptr->buffer);
        ic_ptr->buflen = 0;
    } else if (ic_ptr->error_count >= I2C_ERR_THRESHOLD) {
        error_buf[0] = ic_ptr->error_count;
        error_buf[1] = ic_ptr->error_code;
        error_buf[2] = ic_ptr->event_count;
        ToMainHigh_sendmsg(sizeof (unsigned char) *3, MSGT_I2C_DBG, (void *) error_buf);
        ic_ptr->error_count = 0;
    }
    if (msg_to_send) {
        // send to the queue to *ask* for the data to be sent out

        start_i2c_slave_reply(I2C_Buffer_Size, 0);
        msg_to_send = 0;
    }
}
// this is the interrupt handler for i2c -- it is currently built for slave mode
// -- to add master mode, you should determine (at the top of the interrupt handler)
//    which mode you are in and call the appropriate subroutine.  The existing code
//    below should be moved into its own "i2c_slave_handler()" routine and the new
//    master code should be in a subroutine called "i2c_master_handler()"
void i2c_int_handler()
{
	unsigned char	i2c_data;
	unsigned char	data_read = 0;
	unsigned char	data_written = 0;
	unsigned char	msg_ready = 0;
	unsigned char	msg_to_send = 0;
	unsigned char	overrun_error = 0;
	unsigned char	error_buf[3];

	// clear SSPOV
	if (SSPCON1bits.SSPOV == 1) {
		SSPCON1bits.SSPOV = 0;
		// we failed to read the buffer in time, so we know we
		// can't properly receive this message, just put us in the
		// a state where we are looking for a new message
		ic_ptr->status = I2C_IDLE;
		overrun_error = 1;
		ic_ptr->error_count++;
		ic_ptr->error_code = I2C_ERR_OVERRUN;
	}
	// read something if it is there
	if (SSPSTATbits.BF == 1) {
		i2c_data = SSPBUF;
		data_read = 1;
	}



	if (!overrun_error) {
		switch(ic_ptr->status) {
			case	I2C_IDLE: {
				// ignore anything except a start
				if (SSPSTATbits.S == 1) {
					handle_start(data_read);	
					// if we see a slave read, then we need to handle it here
					if (ic_ptr->status == I2C_SLAVE_SEND) {
						data_read = 0;
						msg_to_send = 1;
					}
				}	
				break;
			}
			case	I2C_STARTED: {
				// in this case, we expect either an address or a stop bit
				if (SSPSTATbits.P == 1) {
					// we need to check to see if we also read an
					// address (a message of length 0)
					ic_ptr->event_count++;
					if (data_read) {
						if (SSPSTATbits.D_A == 0) {
							msg_ready = 1;
						} else {
							ic_ptr->error_count++;
							ic_ptr->error_code = I2C_ERR_NODATA;
						}
					}
					ic_ptr->status = I2C_IDLE;
				} else if (data_read) {
					ic_ptr->event_count++;
					if (SSPSTATbits.D_A == 0) {
						if (SSPSTATbits.R_W == 0) { // slave write
							ic_ptr->status = I2C_RCV_DATA;
						} else { // slave read
							ic_ptr->status = I2C_SLAVE_SEND;
							msg_to_send = 1;
							// don't let the clock stretching bit be let go
							data_read = 0;
						}
					} else {
						ic_ptr->error_count++;
						ic_ptr->status = I2C_IDLE;
						ic_ptr->error_code = I2C_ERR_NODATA;
					}
				}
				break;
			}
			case I2C_SLAVE_SEND: {
				if (ic_ptr->outbufind < ic_ptr->outbuflen) {
					SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
					ic_ptr->outbufind++;
					data_written = 1;
				} else {
					// we have nothing left to send
					ic_ptr->status = I2C_IDLE;
				}
				break;
			}
			case I2C_RCV_DATA: {
				// we expect either data or a stop bit or a (if a restart, an addr)
				 if (SSPSTATbits.P == 1) {
					// we need to check to see if we also read data
					ic_ptr->event_count++;
					if (data_read) {
						if (SSPSTATbits.D_A == 1) {
							ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
							ic_ptr->buflen++;
							msg_ready = 1;
						} else {
							ic_ptr->error_count++;
							ic_ptr->error_code = I2C_ERR_NODATA;
							ic_ptr->status = I2C_IDLE;
						}
					} else {
						msg_ready = 1;
					}
					ic_ptr->status = I2C_IDLE;
				} else if (data_read) {
					ic_ptr->event_count++;
					if (SSPSTATbits.D_A == 1) {
						ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
						ic_ptr->buflen++;
					} else /* a restart */ {
						if (SSPSTATbits.R_W == 1) {
							ic_ptr->status = I2C_SLAVE_SEND;
							msg_ready = 1;
							msg_to_send = 1;
							// don't let the clock stretching bit be let go
							data_read = 0;
						} else { /* bad to recv an address again, we aren't ready */
							ic_ptr->error_count++;
							ic_ptr->error_code = I2C_ERR_NODATA;
							ic_ptr->status = I2C_IDLE;
						}	
					}
				}
				break;
			}
		}
	}

	// release the clock stretching bit (if we should)
	if (data_read || data_written) {
		// release the clock
		if (SSPCON1bits.CKP == 0) {
			SSPCON1bits.CKP = 1;
		}
	}

	// must check if the message is too long, if 
	if ((ic_ptr->buflen > MAXI2CBUF-2) && (!msg_ready)) {
		ic_ptr->status = I2C_IDLE;
		ic_ptr->error_count++;
		ic_ptr->error_code = I2C_ERR_MSGTOOLONG;
	}

	if (msg_ready) {
		ic_ptr->buffer[ic_ptr->buflen] = ic_ptr->event_count;
		ToMainHigh_sendmsg(ic_ptr->buflen+1,MSGT_I2C_DATA,(void *) ic_ptr->buffer);
		ic_ptr->buflen = 0;
	} else if (ic_ptr->error_count >= I2C_ERR_THRESHOLD) {
		error_buf[0] = ic_ptr->error_count;
		error_buf[1] = ic_ptr->error_code;
		error_buf[2] = ic_ptr->event_count;
		ToMainHigh_sendmsg(sizeof(unsigned char)*3,MSGT_I2C_DBG,(void *) error_buf);
		ic_ptr->error_count = 0;
	}
	// This is inside of the I2C interrupt handler fxn.  
	if (msg_to_send) {
		unsigned char MSG[2];
		unsigned char type;
		int t = 0;
		
		if(ic_ptr->buffer[0] = 0xaa)	{						// Check to make sure master sent 0xaa
			int temp;
			int error;
			// Use block_on_To_I2CQueue to see if master is requesting data from an empty queue...if so,
			// send 0xFF.  If not, send the ADC data received from the I2C message queue to the master.
			if(block_on_To_I2Cqueue())	{						// Defined in messages.c
				error = toI2C_recvmsg(2, &type, (void *) MSG);	// receive ADC value from I2CQ
				//LATB = MSG[0];
				start_i2c_slave_reply(2,MSG);		// send ADC value to Master via I2C
			}
			else	{
				MSG[0] = 0xFF;
				MSG[1] = 0xFF;
				start_i2c_slave_reply(2,MSG);
			}
		}
		msg_to_send = 0;
		//ToMainHigh_sendmsg(0,MSGT_I2C_RQST,(void *)ic_ptr->buffer);
	}
}