Esempio n. 1
0
void testAlarm_will_alarm() {
    bool result;
    Alarm* alarm = alarm_create(1, GREATER_THAN);
    double sensor_value;
    
    alarm->type=GREATER_THAN;
    sensor_value = alarm->alarm_value-1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(result);
    sensor_value = alarm->alarm_value+1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(!result);

    alarm->type=LESS_THAN;
    sensor_value = alarm->alarm_value-1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(!result);
    sensor_value = alarm->alarm_value+1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(result);

    alarm->type=EQUAL;
    sensor_value = alarm->alarm_value-1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(!result);
    sensor_value = alarm->alarm_value;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(result);
    sensor_value = alarm->alarm_value+1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(!result);

    alarm->type=NOT_EQUAL;
    sensor_value = alarm->alarm_value-1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(result);
    sensor_value = alarm->alarm_value;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(!result);
    sensor_value = alarm->alarm_value+1;
    result = alarm_will_alarm(alarm, sensor_value);
    assert(result);
    
}
Esempio n. 2
0
bool json_parse_alarms_internal(TokenVector* tokens_vector, const char* json_buffer, ListElement* sensors) {
    int max_port_index = 0;
    int alarm_token_index, current_token_index, alarm_index;
    Sensor** sensors_hash;
    ListElement* root_sensors;
    int sensor_index;
    jsmntok_t* tokens = token_vector_get_pointer(tokens_vector, 0);

    if (list_empty(sensors))
        return true;

    alarm_token_index = find_json_array_token(tokens_vector, json_buffer, ALARM_TAG);
    if (alarm_token_index < 0) { // No alarms, should not be a problem.
        return true;
    }

    if(tokens[alarm_token_index].type != JSMN_ARRAY)
        return false;
    
    // Build sensor hash table - port_index to sensor;
    for (root_sensors = sensors->next; root_sensors != NULL; root_sensors = root_sensors->next) {
        Sensor* sensor = (Sensor*)root_sensors->node;
        if (sensor->port_index < 0)
            return false; // invalid

        if (sensor->port_index > max_port_index)
            max_port_index = sensor->port_index;
    }

    sensors_hash = (Sensor**) calloc(sizeof (Sensor*), max_port_index + 1);
    for (sensor_index = 0; sensor_index <= max_port_index; sensor_index++) // Zero all pointers, could be memset.
        sensors_hash[sensor_index] = NULL;

    for (root_sensors = sensors->next; root_sensors != NULL; root_sensors = root_sensors->next) {
        Sensor* sensor = (Sensor*)root_sensors->node;
        sensors_hash[sensor->port_index] = sensor;
    }

    // iterate of all tokens, try to build alarms
    for (current_token_index = alarm_token_index + 1, alarm_index = 0;
            alarm_index < tokens[alarm_token_index].size; alarm_index++ ) {
        int port_index = -1;
        double alarm_value = -9999;
        enum AlarmType alarm_type = INVALID;
        const size_t number_of_object_tokens = tokens[current_token_index].size;
        const size_t next_alarm_token_index = current_token_index + number_of_object_tokens; 

        // We're expecting something like - {"port_index":1,"alarm_value":5.0,"condition_type":"greater_than"}
        if (tokens[current_token_index].type != JSMN_OBJECT || number_of_object_tokens < 6) {
            current_token_index = next_alarm_token_index;
            continue; // TODO - add logs
        }

        current_token_index++;

        // First token is the key, the second is the value
        while (current_token_index < next_alarm_token_index) {
            const unsigned int next_object_token_index = current_token_index + tokens[current_token_index].size + 1;
            if (tokens[current_token_index].type != JSMN_STRING) {// Must be an error...
                current_token_index = next_object_token_index;
                continue;
            }

            if (tokens[current_token_index + 1].type != JSMN_PRIMITIVE
                    && tokens[current_token_index + 1].type != JSMN_STRING) {// Must be an error...
                current_token_index = next_object_token_index;
                continue;
            }

            if (TOKEN_STRING(json_buffer, tokens[current_token_index], ALARM_VALUE_TAG)) { // alarm value tag
                if (!token_to_double(json_buffer, &tokens[current_token_index + 1], &alarm_value)) {
                    current_token_index = next_alarm_token_index;
                    break;
                }
            } else if (TOKEN_STRING(json_buffer, tokens[current_token_index], PORT_INDEX_TAG)) { // Port index tag
                if (!token_to_int(json_buffer, &tokens[current_token_index + 1], &port_index)) {
                    current_token_index = next_alarm_token_index;
                    break;
                }
            } else if (TOKEN_STRING(json_buffer, tokens[current_token_index], CONDITION_TYPE_TAG)) { // Condition type tag
                if (!token_to_alarm_type(json_buffer, &tokens[current_token_index + 1], &alarm_type)) {
                    current_token_index = next_alarm_token_index;
                    break;
                }
            } // else - ignore this key.

            current_token_index += 2;
        }

        if (port_index >= 0 && alarm_type != INVALID && alarm_value != -9999 && port_index <= max_port_index) { // Add alarm
            Alarm* alarm = alarm_create(alarm_value, alarm_type);
            Sensor* s = sensors_hash[port_index];
            if(s!=NULL)
                list_add(s->alarms, (void*)alarm);
            else
                alarm_delete(alarm);
        }
    }


    free(sensors_hash);

    return true;
}
Esempio n. 3
0
status_t thread_snooze (bigtime_t value)

/*
 * ARGUMENTS
 * * value : the number of microseconds to wait for
 *
 * RESULT
 * * DNA_ERROR: no more alarm slot or quantum too short
 * * DNA_OK: the operation succeeded
 *
 * SOURCE
 */

{
  int32_t alarm_id = -1;
  uint32_t current_cpuid = 0;
  thread_t self = NULL;
  thread_t target = NULL;
  interrupt_status_t it_status = 0;
  status_t status = DNA_OK;

  watch (status_t)
  {
    /*
     * Disable interrupts and get current information
     */

    it_status = cpu_trap_mask_and_backup ();

    current_cpuid = cpu_mp_id();
    self = cpu_pool . cpu[current_cpuid] . current_thread;

    /*
     * Create the snooze alarm and elect a new thread.
     */

    status = alarm_create (value, DNA_ONE_SHOT_RELATIVE_ALARM,
        thread_alarm, self, & alarm_id);
    check (alarm_error, status == DNA_OK, status);

    status = scheduler_elect (& target, true);
    ensure (status != DNA_ERROR && status != DNA_BAD_ARGUMENT, status);

    /*
     * Update self information and switch context.
     */

    lock_acquire (& self -> lock);
    self -> info . status = DNA_THREAD_SLEEPING;

    status = scheduler_switch (target, NULL);
    ensure (status == DNA_OK, status);

    /*
     * Cancel the alarm, just in case we came back from
     * sleeping after a thread_suspend/thread_resume combination.
     */

    status = alarm_destroy (alarm_id);
    ensure (status != DNA_NO_TIMER && status != DNA_BAD_ARGUMENT, status);

    cpu_trap_restore (it_status);
    return (status == DNA_OK) ? DNA_INTERRUPTED : DNA_OK;
  }

  rescue (alarm_error)
  {
    cpu_trap_restore (it_status);
    leave;
  }
}