SingingCat 0
application
sensor_command.c
Go to the documentation of this file.
1#include <sensors/sensor.h>
2#include <sensors/sensor_command.h>
3#include <platform-header.h>
4extern struct sensordev sensors[];
5
6
23// list all sensors
24void sensor_com_list(struct command *com) {
25 if (!sensors_inited()) {
26 send_command_reply_with_args(com, COMFLAGS_ACK, "err=not inited yet");
27 return;
28 }
29 struct command *dcom;
30 const struct sensordev *sd;
31 int t = 0;
32
33 if (!all_sensors_probed()) {
34 // not ready, yet send failure (ack w/o success)
35 send_command_reply_with_args(com, COMFLAGS_ACK, "err=not ready yet");
36 return;
37 }
38 dcom = alloc_command();
39 if (dcom == NULL) {
40 send_command_reply_with_args(com, COMFLAGS_ACK, "err=out of commands");
41 return;
42 }
43 while (((sd = &sensors[t])->power_on) != NULL) {
44 struct sensorruntime *sr = get_runtime(t);
45 t++;
46 if (sr == NULL) {
47 continue;
48 }
49 command_init(dcom);
50 dcom->target = com->sender;
51 dcom->encoding = 'a';
52 dcom->index = com->index;
53 dcom->sourcedev = com->sourcedev;
54 dcom->connid = com->connid;
55 dcom->com = com->com;
56 dcom->flags = COMFLAGS_DATA | COMFLAGS_SUCCESS;
57 command_add_arg(dcom, "idx=%i", t);
58 command_add_arg(dcom, "n=%s", sd->name);
59 command_add_arg(dcom, "t=%s", sd->type);
60 command_add_arg(dcom, "u=%s", sd->unit);
61 command_add_arg(dcom, "up=%s", sd->unit_prefix);
62 command_add_arg(dcom, "f=%i", sr->flags);
63 command_add_arg(dcom, "l=%i", sr->valuesize);
64 send_command(dcom);
65 free_command(dcom);
66 }
67 send_command_reply_with_args(com, COMFLAGS_ACK | COMFLAGS_SUCCESS, "data_packets=%i", t);
68}
69// parse args: index int,activate bool, polliv int, repiv int (all ints as hex)
70void sensor_com_config(struct command *com) {
71 if (com->argctr != 4) {
72 printf("inv sensor config command argctr==%i\r\n", com->argctr);
73 send_command_reply_with_args(com, COMFLAGS_ACK, "err=invalid number of arguments");
74 return;
75 }
76 long idx = ascii_parse_hex((const byte *)get_arg(com, 0), 8, NULL);
77
78 if (idx < 1) {
79 printf("invalid sensor 0\r\n");
80 send_command_reply_with_args(com, COMFLAGS_ACK, "err=sensor 0 is not valid");
81 }
82 struct sensorruntime *sr = get_runtime(idx - 1);
83 const struct sensordev *sdf = sr->sensor;
84
85
86 long activate = ascii_parse_hex((const byte *)get_arg(com, 1), 8, NULL);
87 long poll_frequency = ascii_parse_hex((const byte *)get_arg(com, 2), 8, NULL);
88 long report_frequency = ascii_parse_hex((const byte *)get_arg(com, 3), 8, NULL);
89
90 printf("[sensor] config: Sensor %i (%s) activate: %i, poll_frequency: %i, report_frequency: %i\r\n", idx, sdf->name, activate, poll_frequency, report_frequency);
91 int r = sensor_update_config(com->sender, idx - 1, poll_frequency, report_frequency, 100);
92
93 if (r) {
94 char e[100];
95 snprintf(e, 99, "err=failed to update config (errorcode=%i)", r);
96 send_command_reply_with_args(com, COMFLAGS_ACK, e);
97 return;
98 }
99 sensor_activation(sr, activate);
100 send_command_reply(com, COMFLAGS_ACK | COMFLAGS_SUCCESS);
102}
int send_command_reply_with_args(struct command *com, byte flags, const char *format,...)
send a reply to a command
Definition: queue.c:588
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_add_arg(struct command *com, const char *format,...)
adds an arg to a partially initialised command structure
struct sensordev sensors[]
list of sensors...
Definition: sensor.c:23
int send_command_reply(struct command *com, byte flags)
send a reply to a command
Definition: queue.c:562
void command_init(struct command *com)
initialize a command structure with default values
void sensors_newstyle()
enable new-style sensors (and disable old)
Definition: sensor.c:621
struct command * alloc_command()
allocate a free command
Definition: queue.c:173
const char * get_arg(const struct command *com, int index)
given an argument by index[0..n], will return a pointer to the bytearray (excluding the fieldtype) th...
int com
Definition: command.h:22
long target
Definition: command.h:16
uint8_t connid
Definition: command.h:18
uint8_t sourcedev
Definition: command.h:17
uint8_t encoding
Definition: command.h:11
int index
Definition: command.h:13
uint8_t flags
Definition: command.h:23
uint8_t argctr
Definition: command.h:24
long sender
Definition: command.h:14
runtime data per sensor (in-ram)
Definition: sensor_dev.h:33