int CwMcuSensor::setDelay(int32_t handle, int64_t delay_ns) {
    char buf[80];
    int fd;
    int what;
    int rc;

    ALOGV("%s: Before pthread_mutex_lock()\n", __func__);
    pthread_mutex_lock(&sys_fs_mutex);
    ALOGV("%s: Acquired pthread_mutex_lock()\n", __func__);

    ALOGV("CwMcuSensor::setDelay: handle = %" PRId32 ", delay_ns = %" PRId64 "\n",
            handle, delay_ns);

    what = find_sensor(handle);
    if (uint32_t(what) >= numSensors) {
        pthread_mutex_unlock(&sys_fs_mutex);
        return -EINVAL;
    }
    strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "delay_ms");
    fd = open(fixed_sysfs_path, O_RDWR);
    if (fd >= 0) {
        size_t n = snprintf(buf, sizeof(buf), "%d %lld\n", what, (delay_ns/NS_PER_MS));
        write(fd, buf, min(n, sizeof(buf)));
        close(fd);
    }

    pthread_mutex_unlock(&sys_fs_mutex);
    return 0;

}
ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
                             ipmi_request_t request, ipmi_response_t response,
                             ipmi_data_len_t data_len, ipmi_context_t context)
{
    sensor_data_t *reqptr = (sensor_data_t*)request;
    ipmi_ret_t rc = IPMI_CC_OK;

    printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);

    // TODO Not sure what the System-event-sensor is suppose to return
    // need to ask Hostboot team
    unsigned char buf[] = {0x00,0x6F};

    buf[0] = find_sensor(reqptr->sennum);

    // HACK UNTIL Dbus gets updated or we find a better way
    if (buf[0] == 0) {

        switch(reqptr->sennum) {
            case 0x35 : buf[0] = 0x12; buf[1] = 0x6F; break;
            case 0x37 : buf[0] = 0xC7; buf[1] = 0x03; break;
            case 0x38 : buf[0] = 0xC7; buf[1] = 0x03; break;
            case 0x39 : buf[0] = 0xC7; buf[1] = 0x03; break;
            case 0x3A : buf[0] = 0xC7; buf[1] = 0x03; break;
            default: rc = IPMI_CC_SENSOR_INVALID;
        }
    }


    *data_len = sizeof(buf);
    memcpy(response, &buf, *data_len);

    return rc;
}
Beispiel #3
0
void check_program(pState state){

	pProgram thisStep = get_step(state, state->currentStep);
	if (thisStep == NULL){
		state->finished = TRUE;
		return; 
	}
	if (thisStep->step.type == 0){
		pSensor thisSensor = find_sensor(state, thisStep->step.sensorID);
		//if no sensor corresponds to sensorID, go to next step
		if (thisSensor == NULL){
			state->currentStep = state->currentStep + 1;
			return;
		}
		//check temp, if sensortemp+5 > current temp, set net temp and go next step
		if (  ( (unsigned int) thisSensor->sensorTemp + 5 ) > (unsigned int) state->setTemp){
			//might need to break this.
			state->setTemp = thisStep->step.temp;
			state->currentStep = state->currentStep + 1;
			return;
		}
	} else {
		//if time passes trigger, set new temp and go to next step
		if ((unsigned int)state->currentTime > (unsigned int)thisStep->step.timeVal){
			state->setTemp = thisStep->step.temp;
			state->currentStep = state->currentStep + 1;
			return;
		}
	}
	return;
}
Beispiel #4
0
int add_sensor(pState state, unsigned short int sensorID, unsigned int sensorAddr, unsigned int coeff, unsigned int sensorTemp){
	if (state == NULL){return 1;}
	if (find_sensor(state, sensorID) != NULL){
		return 2;
	}//sensorID exists already}
	if (state->sensorCount > (MAX_SENSOR-1)){
		return 3;
	}//max sensor count
	pSensor tempSensor = state->sensorList;
	pSensor thisSensor = state->sensorList;

	if (tempSensor == NULL){//no sensors
		thisSensor = malloc(sizeof(sSensor));
		state->sensorList = thisSensor;//set list head
		thisSensor->prev = NULL;//prev of list head is null

	} else {//walk to the end of the list

		while (tempSensor != NULL){
			thisSensor = tempSensor;
			tempSensor = tempSensor->next;
		}//write next and prev, then set thisSensor

		thisSensor->next = malloc(sizeof(sSensor));
		thisSensor->next->prev =  thisSensor;
		thisSensor = thisSensor->next;
	}
	thisSensor->next = NULL;
	thisSensor->sensorTemp = (double) state->ambientTemp;
	thisSensor->sensorID = sensorID;
	thisSensor->sensorAddr = sensorAddr;
	thisSensor->sensorCoef = coeff;
	state->sensorCount = state->sensorCount + 1;
	return 0;
}
int updateSensorRecordFromSSRAESC(const void *record) {

	sensorRES_t *pRec = (sensorRES_t *) record;
	uint8_t stype;
	int index, i=0;

	stype = find_sensor(pRec->sensor_number);

	// 0xC3 types use the assertion7_0 for the value to be set
	// so skip the reseach and call the correct event reporting
	// function
	if (stype == 0xC3) {

		shouldReport(stype, 0x00, &index);
		reportSensorEventAssert(pRec, index);

	} else {
		// Scroll through each bit position .  Determine
		// if any bit is either asserted or Deasserted.
		for(i=0;i<8;i++) {

			if ((ISBITSET(pRec->assert_state7_0,i))  &&
				(shouldReport(stype, i, &index)))
			{
				reportSensorEventAssert(pRec, index);
			}
			if ((ISBITSET(pRec->assert_state14_8,i))  &&
				(shouldReport(stype, i+8, &index)))
			{
				reportSensorEventAssert(pRec, index);
			}
			if ((ISBITSET(pRec->deassert_state7_0,i))  &&
				(shouldReport(stype, i, &index)))
			{
				reportSensorEventDeassert(pRec, index);
			}
			if ((ISBITSET(pRec->deassert_state14_8,i))  &&
				(shouldReport(stype, i+8, &index)))
			{
				reportSensorEventDeassert(pRec, index);
			}
		}

	}


	return 0;
}
Beispiel #6
0
//reminder if sensor count is decremented on an empty remove, list can grow too large
int remove_sensor(pState state, unsigned short sensorID){
	pSensor thisSensor = find_sensor(state, sensorID);

	if (thisSensor == NULL){
		return 1;
	}//sensorID not found

	if (thisSensor->prev == NULL){//first element
		state->sensorList = thisSensor->next;//point head to next if necessary
	}
	if (thisSensor->next != NULL){//if there is a next, fix its prev
		thisSensor->next->prev = thisSensor->prev;
	}	
	if (thisSensor->prev != NULL){//if there is a prev, fix its next
		thisSensor->prev->next = thisSensor->next;
	}
	state->sensorCount = state->sensorCount - 1;
	free(thisSensor);

	return 0;
}
Beispiel #7
0
uint8_t search_sensors(int maxSensors)
{
	uint8_t i;
	uint8_t id[OW_ROMCODE_SIZE];
	uint8_t diff, nSensors;
	
	#ifdef OW_DEBUG
	 printf_P(PSTR( "\rScanning Bus for DS18X20\r" ));
	#endif
	
	nSensors = 0;
	
	for( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE && nSensors < maxSensors ; )
	{
		find_sensor( &diff, &id[0] );
		
		if( diff == OW_PRESENCE_ERR ) {
			#ifdef OW_DEBUG
				printf_P(PSTR( "No Sensor found\r" ));
			#endif
			break;
		}
		
		if( diff == OW_DATA_ERR ) {
			#ifdef OW_DEBUG
				printf_P(PSTR( "Bus Error\r" ));
			#endif
			break;
		}
		
		for (i=0;i<OW_ROMCODE_SIZE;i++)
		{
			sensorScan[nSensors*OW_ROMCODE_SIZE+i] = id[i];
		}
		
		nSensors++;
	}
	
	return nSensors;
}
int CwMcuSensor::flush(int handle)
{
    int what;
    int fd;
    char buf[10] = {0};
    int err;

    what = find_sensor(handle);

    if (uint32_t(what) >= CW_SENSORS_ID_END) {
        return -EINVAL;
    }

    ALOGV("%s: Before pthread_mutex_lock()\n", __func__);
    pthread_mutex_lock(&sys_fs_mutex);
    ALOGV("%s: Acquired pthread_mutex_lock()\n", __func__);

    strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "flush");

    fd = open(fixed_sysfs_path, O_RDWR);
    if (fd >= 0) {
        int n = snprintf(buf, sizeof(buf), "%d\n", what);
        err = write(fd, buf, min(n, sizeof(buf)));
        if (err < 0) {
            err = -errno;
        } else {
            err = 0;
        }
        close(fd);
    } else {
        ALOGI("CwMcuSensor::flush: flush not supported\n");
        err = -EINVAL;
    }

    pthread_mutex_unlock(&sys_fs_mutex);
    ALOGI("CwMcuSensor::flush: fd = %d, sensors_id = %d, path = %s, err = %d\n",
          fd, what, fixed_sysfs_path, err);
    return err;
}
   // ----------------------------------------------------------------------
   DoubleSensor*
   SimpleSensorDoubleFactory::
   create(DoubleReading &reading, shawn::Node node)
   {
      std::string sensor_id = reading.name();
      sensor_id += "_";
      sensor_id += node.label();

      Sensor* tmp_sens = find_sensor(sensor_id);
      DoubleSensor* out_sensor;
      if(tmp_sens == NULL)
      {
         DoubleSensor* out_sensor = new DoubleSensor(reading, node);
         sensor_map_[sensor_id] = out_sensor;
      }
      else
      {
         out_sensor = dynamic_cast<DoubleSensor *>(tmp_sens);
      }


      return out_sensor;
   }
int CwMcuSensor::batch(int handle, int flags, int64_t period_ns, int64_t timeout)
{
    int what;
    int fd;
    char buf[32] = {0};
    int err;
    int delay_ms;
    int timeout_ms;
    bool dryRun = false;

    ALOGV("CwMcuSensor::batch++: handle = %d, flags = %d, period_ns = %" PRId64 ", timeout = %" PRId64 "\n",
        handle, flags, period_ns, timeout);

    what = find_sensor(handle);
    delay_ms = period_ns/NS_PER_MS; // int64_t is being dropped to an int type
    timeout_ms = timeout/NS_PER_MS; // int64_t is being dropped to an int type

    if(flags & SENSORS_BATCH_DRY_RUN) {
        dryRun = true;
    }

    if (uint32_t(what) >= CW_SENSORS_ID_END) {
        return -EINVAL;
    }

    if(is_batch_wake_sensor(handle)) {
        flags |= SENSORS_BATCH_WAKE_UPON_FIFO_FULL;
        ALOGV("CwMcuSensor::batch: SENSORS_BATCH_WAKE_UPON_FIFO_FULL~!!\n");
    } else
        flags &= ~SENSORS_BATCH_WAKE_UPON_FIFO_FULL;

    switch (what) {
    case CW_LIGHT:
    case CW_SIGNIFICANT_MOTION:
        if (timeout > 0) {
            ALOGI("CwMcuSensor::batch: handle = %d, not support batch mode", handle);
            return -EINVAL;
        }
        break;
    default:
        break;
    }

    if (dryRun == true) {
        ALOGV("CwMcuSensor::batch: SENSORS_BATCH_DRY_RUN is set\n");
        return 0;
    }

    ALOGV("%s: Before pthread_mutex_lock()\n", __func__);
    pthread_mutex_lock(&sys_fs_mutex);
    ALOGV("%s: Acquired pthread_mutex_lock()\n", __func__);

    if (mEnabled.isEmpty()) {
        int i;
        int iio_buf_size;

        if (!init_trigger_done) {
            err = sysfs_set_input_attr("trigger/current_trigger",
                                      mTriggerName, strlen(mTriggerName));
            if (err < 0) {
                ALOGE("CwMcuSensor::batch: set current trigger failed: err = %d, strerr() = %s\n",
                      err, strerror(errno));
            } else {
                init_trigger_done = true;
            }
        }

        iio_buf_size = IIO_MAX_BUFF_SIZE;
        for (i = 0; i < IIO_BUF_SIZE_RETRY; i++) {
            if (sysfs_set_input_attr_by_int("buffer/length", iio_buf_size) < 0) {
                ALOGE("CwMcuSensor::batch: set IIO buffer length (%d) failed: %s\n",
                      iio_buf_size, strerror(errno));
            } else {
                if (sysfs_set_input_attr_by_int("buffer/enable", 1) < 0) {
                    ALOGE("CwMcuSensor::batch: set IIO buffer enable failed: %s, i = %d, "
                          "iio_buf_size = %d\n", strerror(errno), i , iio_buf_size);
                } else {
                    ALOGI("CwMcuSensor::batch: set IIO buffer length = %d, success\n", iio_buf_size);
                    break;
                }
            }
            iio_buf_size /= 2;
        }
    }

    strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "batch_enable");

    fd = open(fixed_sysfs_path, O_RDWR);
    if (fd < 0) {
        err = -errno;
    } else {
        int n = snprintf(buf, sizeof(buf), "%d %d %d %d\n", what, flags, delay_ms, timeout_ms);
        err = write(fd, buf, min(n, sizeof(buf)));
        if (err < 0) {
            err = -errno;
        } else {
            err = 0;
        }
        close(fd);
    }
    pthread_mutex_unlock(&sys_fs_mutex);

    ALOGV("CwMcuSensor::batch: fd = %d, sensors_id = %d, flags = %d, delay_ms= %d,"
          " timeout_ms = %d, path = %s, err = %d\n",
          fd , what, flags, delay_ms, timeout_ms, fixed_sysfs_path, err);

    return err;
}
int CwMcuSensor::setEnable(int32_t handle, int en) {

    int what;
    int err = 0;
    int flags = !!en;
    int fd;
    char buf[10];
    int temp_data[COMPASS_CALIBRATION_DATA_SIZE];
    char value[PROPERTY_VALUE_MAX] = {0};
    int rc;

    ALOGV("%s: Before pthread_mutex_lock()\n", __func__);
    pthread_mutex_lock(&sys_fs_mutex);
    ALOGV("%s: Acquired pthread_mutex_lock()\n", __func__);

    property_get("debug.sensorhal.fill.block", value, "0");
    ALOGV("CwMcuSensor::setEnable: debug.sensorhal.fill.block= %s", value);
    fill_block_debug = atoi(value) == 1;

    what = find_sensor(handle);

    ALOGV("CwMcuSensor::setEnable: "
          "[v13-Dynamic adjust the IIO buffer], handle = %d, en = %d, what = %d\n",
          handle, en, what);

    if (uint32_t(what) >= numSensors) {
        pthread_mutex_unlock(&sys_fs_mutex);
        return -EINVAL;
    }

    offset_reset[what] = !!flags;

    strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "enable");
    fd = open(fixed_sysfs_path, O_RDWR);
    if (fd >= 0) {
        int n = snprintf(buf, sizeof(buf), "%d %d\n", what, flags);
        err = write(fd, buf, min(n, sizeof(buf)));
        if (err < 0) {
            ALOGE("%s: write failed: %s", __func__, strerror(errno));
        }

        close(fd);

        if (flags) {
            mEnabled.markBit(what);
        } else {
            mEnabled.clearBit(what);
        }

        if (mEnabled.isEmpty()) {
            if (sysfs_set_input_attr_by_int("buffer/enable", 0) < 0) {
                ALOGE("CwMcuSensor::setEnable: set buffer disable failed: %s\n", strerror(errno));
            } else {
                ALOGV("CwMcuSensor::setEnable: set IIO buffer enable = 0\n");
            }
        }
    } else {
        ALOGE("%s open failed: %s", __func__, strerror(errno));
    }


    // Sensor Calibration init. Waiting for firmware ready
    if (!flags &&
            ((what == CW_MAGNETIC) ||
             (what == CW_ORIENTATION) ||
             (what == CW_ROTATIONVECTOR))) {
        ALOGV("Save Compass calibration data");
        strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "calibrator_data_mag");
        rc = cw_read_calibrator_file(CW_MAGNETIC, fixed_sysfs_path, temp_data);
        if (rc== 0) {
            cw_save_calibrator_file(CW_MAGNETIC, SAVE_PATH_MAG, temp_data);
        } else {
            ALOGI("Compass calibration data from driver fails\n");
        }
    }

    pthread_mutex_unlock(&sys_fs_mutex);
    return 0;
}
ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
                             ipmi_request_t request, ipmi_response_t response,
                             ipmi_data_len_t data_len, ipmi_context_t context)
{
    sensor_data_t *reqptr = (sensor_data_t*)request;
    ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
    uint8_t type;
    sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
    int r;
    dbus_interface_t a;
    sd_bus *bus = ipmid_get_sd_bus_connection();
    sd_bus_message *reply = NULL;
    int reading = 0;


    printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);

    r = find_openbmc_path("SENSOR", reqptr->sennum, &a);

    if (r < 0) {
        fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
        return IPMI_CC_SENSOR_INVALID;
    }

    type = find_sensor(reqptr->sennum);

    fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);

    *data_len=0;

    switch(type) {
        case 0xC3:
        case 0xC2:
            r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
            if (r < 0) {
                fprintf(stderr, "Failed to call sd_bus_get_property:%d,  %s\n", r, strerror(-r));
                fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
                        a.bus, a.path, a.interface);
                break;
            }

            r = sd_bus_message_read(reply, "i", &reading);
            if (r < 0) {
                fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
                break;
            }

            printf("Contents of a 0x%02x is 0x%02x\n", type, reading);

            rc = IPMI_CC_OK;
            *data_len=sizeof(sensorreadingresp_t);

            resp->value         = (uint8_t)reading;
            resp->operation     = 0;
            resp->indication[0] = 0;
            resp->indication[1] = 0;
            break;

        default:
            *data_len=0;
            rc = IPMI_CC_SENSOR_INVALID;
            break;
    }


    reply = sd_bus_message_unref(reply);

    return rc;
}