|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Universidad de La Laguna <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License as published by the Free |
| 6 | + * Software Foundation; either version 2 of the License, or (at your option) |
| 7 | + * any later version. |
| 8 | + * |
| 9 | + * @author: Alberto Cabrera Perez <[email protected]> |
| 10 | + */ |
| 11 | + |
| 12 | +#define _XOPEN_SOURCE 500 |
| 13 | + |
| 14 | +#include <assert.h> |
| 15 | +//#include <ctype.h> |
| 16 | +//#include <errno.h> |
| 17 | +//#include <stddef.h> |
| 18 | +//#include <stdint.h> |
| 19 | +//#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <string.h> |
| 22 | +//#include <dirent.h> |
| 23 | +// |
| 24 | +#include <confuse.h> |
| 25 | +//#include <fcntl.h> |
| 26 | +//#include <sys/stat.h> |
| 27 | +//#include <unistd.h> |
| 28 | +// |
| 29 | +#include "data.h" |
| 30 | +#include "debug.h" |
| 31 | +#include "driver.h" |
| 32 | +#include "error.h" |
| 33 | +#include "timer.h" |
| 34 | + |
| 35 | +#define LABEE_DEFAULT_SAMPLING_INTERVAL 100000000L // 100ms |
| 36 | + |
| 37 | +struct emlDriver labee_driver; |
| 38 | + |
| 39 | + |
| 40 | +static enum emlError init(cfg_t* const config) { |
| 41 | + assert(!labee_driver.initialized); |
| 42 | + assert(config); |
| 43 | + labee_driver.config = config; |
| 44 | + |
| 45 | + enum emlError err; |
| 46 | + |
| 47 | + // TODO Check if the API is up |
| 48 | + |
| 49 | + // FUnction that generates error code |
| 50 | + if (err != EML_SUCCESS) |
| 51 | + goto err_free; |
| 52 | + |
| 53 | + labee_driver.ndevices = 1; |
| 54 | + labee_driver.devices = malloc(labee_driver.ndevices * sizeof(*labee_driver.devices)); |
| 55 | + for (size_t i = 0; i < labee_driver.ndevices; i++) { |
| 56 | + struct emlDevice devinit = { |
| 57 | + .driver = &labee_driver, |
| 58 | + .index = i, |
| 59 | + }; |
| 60 | + sprintf(devinit.name, "%s%zu", labee_driver.name, i); |
| 61 | + |
| 62 | + struct emlDevice* const dev = &labee_driver.devices[i]; |
| 63 | + memcpy(dev, &devinit, sizeof(*dev)); |
| 64 | + } |
| 65 | + |
| 66 | + labee_driver.initialized = 1; |
| 67 | + return EML_SUCCESS; |
| 68 | + |
| 69 | +err_free: |
| 70 | + if (labee_driver.failed_reason[0] == '\0') |
| 71 | + strncpy(labee_driver.failed_reason, emlErrorMessage(err), sizeof(labee_driver.failed_reason) - 1); |
| 72 | + return err; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +static enum emlError shutdown() { |
| 77 | + assert(labee_driver.initialized); |
| 78 | + |
| 79 | + labee_driver.initialized = 0; |
| 80 | + |
| 81 | + return EML_SUCCESS; |
| 82 | +} |
| 83 | + |
| 84 | +static enum emlError measure(size_t devno, unsigned long long* values) { |
| 85 | + assert(labee_driver.initialized); |
| 86 | + assert(devno < labee_driver.ndevices); // Shouldn't be more than one anyways |
| 87 | + |
| 88 | + enum emlError err; |
| 89 | + |
| 90 | + values[0] = millitimestamp(); |
| 91 | + const long sampling_interval = cfg_getint(labee_driver.config, "sampling_interval"); |
| 92 | + unsigned long long power = 1 / sampling_interval; |
| 93 | + |
| 94 | + //TODO parse xml from rest api apply average proportionally to interval |
| 95 | + values[labee_driver.default_props->inst_energy_field * DATABLOCK_SIZE] = power; |
| 96 | + return EML_SUCCESS; |
| 97 | +} |
| 98 | + |
| 99 | +// default measurement properties for this driver |
| 100 | +static struct emlDataProperties default_props = { |
| 101 | + .time_factor = EML_SI_MILLI, |
| 102 | + .energy_factor = EML_SI_MICRO, // Measurement is in J, but to store on a long, it is multiplied by EML_SI_MEGA |
| 103 | + .power_factor = EML_SI_MICRO, // Measurement is in W, same principle, with EML_SI_MEGA |
| 104 | + .inst_energy_field = 1, |
| 105 | + .inst_power_field = 2, |
| 106 | +}; |
| 107 | + |
| 108 | +static cfg_opt_t cfgopts[] = { |
| 109 | + CFG_BOOL("disabled", cfg_false, CFGF_NONE), |
| 110 | + CFG_INT("sampling_interval", LABEE_DEFAULT_SAMPLING_INTERVAL, CFGF_NONE), |
| 111 | + CFG_END() |
| 112 | +}; |
| 113 | + |
| 114 | +//public driver state and interface |
| 115 | +struct emlDriver labee_driver = { |
| 116 | + .name = "labee", |
| 117 | + .type = EML_DEV_LABEE, |
| 118 | + .failed_reason = "", |
| 119 | + .default_props = &default_props, |
| 120 | + .cfgopts = cfgopts, |
| 121 | + .config = NULL, |
| 122 | + |
| 123 | + .init = &init, |
| 124 | + .shutdown = &shutdown, |
| 125 | + .measure = &measure, |
| 126 | +}; |
0 commit comments