SingingCat 0
application
metrics.c
1#include "main-header.h"
2#include "metrics/metrics.h"
3
4static uint8_t mbuf[256];
5
6typedef struct metric {
7 uint32_t integer;
8 uint8_t metric_number;
9 uint8_t number_type;
10} _metric;
11
12typedef struct metric_set {
13 struct metric metrics[METRIC_COUNT];
15
16#define SEND_FREQUENCY_SECS 120
17static long int metric_send_counter;
18
19static struct metric_set metricset;
20
21void metric_init() {
22 memset(&metricset, 0, sizeof(metricset));
23 metric_send_counter = 0;
24}
25
26static struct metric *find_metric_entry(int mnum) {
27 int i;
28 struct metric *res = NULL;
29
30 for (i = 0; i < METRIC_COUNT; i++) {
31 struct metric *m = &metricset.metrics[i];
32 if (m->metric_number == mnum) {
33 return m;
34 }
35 if ((res == NULL) && (m->metric_number == 0)) {
36 res = m;
37 }
38 }
39 if (res != NULL) {
40 res->metric_number = mnum;
41 res->integer = 0;
42 }
43 return res;
44}
45void metric_set_by_number(int metric, uint32_t value) {
46 struct metric *m = find_metric_entry(metric);
47
48 if (m == NULL) {
49 printf("No metric available\r\n");
50 return;
51 }
52 m->integer = value;
53}
54
55void metric_inc_by_number(int metric) {
56 struct metric *m = find_metric_entry(metric);
57
58 if (m == NULL) {
59 printf("No metric available\r\n");
60 return;
61 }
62 m->integer++;
63}
64
65// send current metrics out to server
66void metric_send() {
67 int i;
68 struct command *com = alloc_command();
69
70 if (com == NULL) {
71 return;
72 }
74 com->target = CLOUD_SERVER;
75 com->com = 56; // metric
76 com->flags = COMFLAGS_DATA | COMFLAGS_SUCCESS;
77 struct shifter shstruct;
78 struct shifter *sh = &shstruct;
79
80 shifter_init(sh, (uint8_t *)&mbuf, sizeof(mbuf));
81
82 shift_into_uint8(sh, 1); // segment start indicator that what follows is a number array
83 for (i = 0; i < METRIC_COUNT; i++) {
84 struct metric *m = &metricset.metrics[i];
85 if (m->metric_number == 0) {
86 continue;
87 }
88 shift_into_uint8(sh, m->metric_number);
89 shift_into_uint8(sh, m->number_type);
90 shift_into_uint32(sh, m->integer);
91 }
92 shift_into_uint8(sh, 0); // end segment
93 if (shift_error(sh) != 0) {
94 printf("[metric] Shifter error: %i\r\n", shift_error(sh));
95 free_command(com);
96 return;
97 }
98 printf("[metric] sending %i bytes\r\n", sh->validbytes);
99 command_add_binary_arg(com, sh->validbytes, mbuf);
100 send_command(com);
101 free_command(com);
102}
103void metric_loop() {
104 if (mculib_has_time_passed(SEND_FREQUENCY_SECS, &metric_send_counter)) {
105 metric_send();
106 }
107}
int send_command(struct command *com)
send a command to another module (or broadcast)
Definition: queue.c:374
void free_command(struct command *com)
free a command
Definition: queue.c:200
void command_init(struct command *com)
initialize a command structure with default values
struct command * alloc_command()
allocate a free command
Definition: queue.c:173
int command_add_binary_arg(struct command *com, const int len, const byte *srcbuf)
adds a binary parameter to command returns 0 if ok otherwise errorcode
int com
Definition: command.h:22
Definition: metrics.c:6
Definition: shifter.h:6