SingingCat 0
application
user_app_info.h
Go to the documentation of this file.
1#ifndef USER_APP_INFO_H
2#define USER_APP_INFO_H
3#include "command.h"
4#include "routing.h"
5#include "stdarg.h"
6#include "sctime.h"
7#include "sensors/sensor_dev.h"
8#define __need_size_t 1
9#include <stddef.h>
10
21typedef int MCULIB_HANDLE;
22typedef void (*pin_irq_callback)(int pin);
23typedef void (*irq_callback)(void);
24typedef void (*serial_recv_callback)(int port, uint8_t b);
25typedef void (*cpkt_callback)(struct command *com, struct command *reply);
29typedef struct sc_firmware_api {
30 int version;
32 void (*debugf)(const char *format, ...);
34 void (*printf)(const char *format, ...);
42 int (*mculib_serialport_enable)(MCULIB_HANDLE handle, int portnum, int baudrate);
43 int (*mculib_serialport_disable)(MCULIB_HANDLE handle, int portnum);
47 int (*user_serialport_callback)(MCULIB_HANDLE handle,int portnum, serial_recv_callback cb);
51 int (*mculib_serialport_write_char)(MCULIB_HANDLE handle, int portnum, uint8_t b);
56 int (*mculib_pin_out)(MCULIB_HANDLE handle, int pinnum, int speed);
61 int (*mculib_pin_out_opendrain)(MCULIB_HANDLE handle, int pinnum, int speed);
67 int (*mculib_pin_in)(MCULIB_HANDLE handle, int pinnum, int speed, int mode);
71 int (*mculib_pin_in_callback)(MCULIB_HANDLE handle, pin_irq_callback pic);
75 int (*mculib_pin_set)(MCULIB_HANDLE handle, int pinnum, int highlow);
79 int (*mculib_pin_release)(MCULIB_HANDLE handle, int pinnum);
84 int (*mculib_pin_get)(MCULIB_HANDLE handle, int pinnum);
88 int (*mculib_serialport_write_string)(MCULIB_HANDLE handle, int port, const char *txt);
89 uint8_t (*mculib_has_time_passed)(int secs_to_wait, long *ctr);
98 int (*mculib_spi_disable)(MCULIB_HANDLE handle, int portnum);
103 int (*mculib_spi_speed)(MCULIB_HANDLE handle, int portnum, long khz);
107 int (*mculib_spi_master_enable3)(MCULIB_HANDLE handle, int portnum, uint8_t wide, uint8_t clkpol, uint8_t cpha, uint8_t dir,uint32_t baud_in_khz);
114 int (*mculib_spi_write_and_read)(MCULIB_HANDLE handle, int portnum, const uint16_t write, uint16_t *read);
120 int (*mculib_spi_write_only)(MCULIB_HANDLE handle, int portnum, const uint16_t write);
124 int (*mculib_spi_read16_only)(MCULIB_HANDLE handle, int portnum, uint16_t *read);
125/***************
126 * timers
127 */
128 int (*mculib_timer_enable_simple)(MCULIB_HANDLE handle, int timer, int freq, irq_callback callback);
129 int (*mculib_timer_disable)(MCULIB_HANDLE handle, int timer);
130 int (*mculib_timer_attach_pin_pwm)(MCULIB_HANDLE handle, int timer, int pin);
131 int (*mculib_timer_set_pwm)(MCULIB_HANDLE handle, int timer, int pin, uint16_t dutycycle);
132
133/******************
134 * ADC
135 */
136 int (*mculib_adc_read_once)(MCULIB_HANDLE handle, int pin, int *result);
137 int (*mculib_adc_get_resolution)(MCULIB_HANDLE handle, int pin);
138 int (*mculib_adc_enable)(MCULIB_HANDLE handle, int pin);
139 int (*mculib_adc_read)(MCULIB_HANDLE handle, int pin, int *result);
140 /***********************
141 * command handler
142 **********************/
146 const char *(*com_get_named_arg)(struct command *com, const char *name) ;
152 const char *(*com_get_arg)(const struct command *com, int index) ;
156 int (* com_get_arg_size)(const struct command *com, int index) ;
160 void (*com_add_arg)(struct command *com, const char *format, ...) ;
164 void (*com_add_varg)(struct command *com, const char *format, va_list args) ;
168 struct command * (*com_alloc)() ;
172 struct command *(*com_get_data_reply)(struct command *com) ;
176 int (*com_send_reply_with_args)(struct command *com, uint8_t flags, const char *format, ...) ;
180 int (*com_deliver_command)(struct command *com, cpkt_callback);
181
182 /*********************************
183 * sensor stuff
184 *********************************/
188 int (*sensor_register)(struct sensordev *sensor);
189 /*********************************
190 * helper stuff
191 *********************************/
192 int (*snprintf)(char *buf, size_t size, const char *format, ...) ;
193 int (*vsnprintf)(char *buf, size_t size, const char *format, va_list args) ;
194 char *(* strncpy)(char *dst, const char *src, size_t size) ;
195 size_t (*strlen)(const char *txt);
196 void *(*memset)(void *buf, int c, size_t n) ;
197 int (*strcmp)(const char *c1, const char *c2) ;
198 int (* atoi)(const char *s);
199 /*********************************
200 * led
201 *********************************/
205 void (*led_blink) (int colour, int brightness, int on, int off);
206 /*********************************
207 * flags
208 *********************************/
212 int (*get_config_flag) (int flag);
216 int (*com_send_command)(struct command *com);
217
218 /*********************************
219 * more spi
220 *********************************/
221 int (*mculib_spi_send_dma)(MCULIB_HANDLE handle, int portno,void *data, uint32_t size,uint32_t flags);
222 /*********************************
223 * more helpers
224 *********************************/
228 int (*com_get_named_arg_uint32)(struct command *com, const char *name, uint32_t *result ) ;
232 int (*com_get_named_arg_uint16)(struct command *com, const char *name, uint16_t *result ) ;
233 /*********************************
234 * userconfig
235 *********************************/
246 uint32_t (*userconfig_size)();
251 uint32_t (*userconfig_version)();
257 int (*userconfig_copy)(uint8_t *buf,uint16_t bufsize,uint16_t *actual_size);
262 int (* userconfig_write)(uint8_t *buf, uint32_t size);
266 void (*com_free)(struct command *com);
270 struct sensorruntime *(*sensor_get_runtime_by_index)(int idx);
274 int (*proto_buf_to_struct)(void *buf, int bufsize, void *iter, void *out) ;
278 int (*proto_struct_to_buf)(void *buf, int bufsize, void *iter, void *in) ;
282 void (*proto_release)(void *buf);
286 void (*pin_pwm)(int MCULIBHANDLE,int pin, uint32_t newstate, int flags);
295 int (*timer_softirq)(uint32_t hertz,uint32_t opaque, void (*callback)(uint32_t));
299 int (*time_get_diff)(struct sctime *time1,struct sctime *time2);
303 void (*time_bcd_to_timestruct)(uint32_t bcd_time,uint32_t bcd_date,struct sctime *out);
304
309 int (*com_add_binary_arg)(struct command *com, const int len, const uint8_t *buf);
310
311 /********************************* powersave stuff ***************/
316 int (*mcu_set_speed)(int MCULIBHANDLE, int speed);
317
324 int (*hijack_usb_acm)(void (*receive_hook)(uint8_t *received, int size));
329 int (*send_via_usb_acm)(uint8_t *buf, int size);
333 int (*hijack_esp32_usart)(void (*receive_hook)(uint8_t received));
337 void (*reset_module)(void);
341 void (*reset_userapp)(void);
346
347 /********************************* add calls above here ***************/
348
349
383
384
385/**********************************************************************************/
386
387
391typedef struct userapp_info {
392 /* \brief must contain "SCUA" (0x53435541) for the firmware to recognize this as valid userapp */
393 uint32_t magic;
394 /* \brief length of binary code. usually fuild in during build */
395 uint32_t length;
396 /* \brief checksum of binary code. usually fuild in during build */
397 uint32_t checksum;
398 /* \brief version of binary code. usually fuild in during build */
399 uint32_t version;
400 /* \brief used by the firmeware to determine what capabilities this userapp has */
401 uint32_t api_version;
402 uint32_t pad2;
403 /* \brief pointer to address at which userapp code must be loaded */
404 void *base;
405 /* \brief build_timestamp, usually patched in during build */
406 uint32_t build_timestamp;
407 /* \brief git repositoryid, usually patched in during build */
408 uint32_t repoid;
410 int (*start)(int MCULIBHANDLE, struct sc_firmware_api *api);
412 void (*stop)();
417 void (*user_loop)();
418
435 void (*on_new_node)(struct hostroute *host);
436 void *end_marker;
438
439
440#endif
long host
Definition: routing.h:21
int(* sensor_register)(struct sensordev *sensor)
register a new sensor. returns 0 if ok
int(* mculib_serialport_write_char)(MCULIB_HANDLE handle, int portnum, uint8_t b)
write to serial port
Definition: user_app_info.h:51
int(* mculib_pin_in_callback)(MCULIB_HANDLE handle, pin_irq_callback pic)
set a callback to be called whenever a pin irq is triggered
Definition: user_app_info.h:71
void(* stop)()
last thing to be called. no more userloops or irqs afterwards
int(* time_get_localtime_as_struct)(struct sctime *time)
get the current time as time struct (return 0 if ok, otherwise error)
int(* mculib_pin_in)(MCULIB_HANDLE handle, int pinnum, int speed, int mode)
configure and enable an IO-Pin as input note: pinnum are sequential 0-99 are PAnn,...
Definition: user_app_info.h:67
void(* led_blink)(int colour, int brightness, int on, int off)
set the led to a certain colour or blink. to set a solid colour set off==0
int(* com_send_command)(struct command *com)
int(* mculib_spi_write_only)(MCULIB_HANDLE handle, int portnum, const uint16_t write)
write a byte, discard the read if a transmission is already in progress wait until transmission is do...
int(* mculib_pin_release)(MCULIB_HANDLE handle, int pinnum)
deconfigure a pin (MCULIB_HANDLE handle,used for a different purpose)
Definition: user_app_info.h:79
int(* com_get_named_arg_uint16)(struct command *com, const char *name, uint16_t *result)
0 == ok, otherwise error. if OK, value will be stored in "result"
int(* unhandled_call1)()
safety call, terminates the app
int(* unhandled_call3)()
safety call, terminates the app
int(* on_command_received)(struct command *com)
this is called for each command we receive
void(* com_add_arg)(struct command *com, const char *format,...)
add an argument to the command using a format strings (see printf)
int(* hijack_usb_acm)(void(*receive_hook)(uint8_t *received, int size))
"hijack" the usb port. this means the firmware will neither send bytes through the port nor process i...
long(* mculib_get_seconds_since_boot)(void)
get number of seconds elapsed since boot or power-up this counter is usually ever-increasing and roug...
Definition: user_app_info.h:94
int(* hijack_esp32_usart)(void(*receive_hook)(uint8_t received))
hijack the esp32 serial port
int(* start)(int MCULIBHANDLE, struct sc_firmware_api *api)
guaranteed to be called by the firmware before any other functions
int(* mculib_spi_master_enable3)(MCULIB_HANDLE handle, int portnum, uint8_t wide, uint8_t clkpol, uint8_t cpha, uint8_t dir, uint32_t baud_in_khz)
enable a spi port as master and set clkpol, cpha and width (wide=0 8bit, wide=1 16bit)(dir == 0=RW,...
int(* proto_buf_to_struct)(void *buf, int bufsize, void *iter, void *out)
decode a buffer containing a serialised proto into a struct
uint32_t(* userconfig_size)()
the userconfig: this is a little unstructured piece of flash set aside for configuration of userapps....
void(* printf)(const char *format,...)
print a message on console and possibly forward to server into logfile as well (uses radio - expensiv...
Definition: user_app_info.h:34
void(* com_add_varg)(struct command *com, const char *format, va_list args)
add an argument to the command using vargs
int(* mculib_serialport_enable)(MCULIB_HANDLE handle, int portnum, int baudrate)
configure a serial port with specified parameters num == 1..n (depending on mcu) baudrate == ....
Definition: user_app_info.h:42
int(* user_serialport_callback)(MCULIB_HANDLE handle, int portnum, serial_recv_callback cb)
set callback for serialport receive irq
Definition: user_app_info.h:47
void(* reset_module)(void)
reset the module (this does not return);
int(* timer_softirq)(uint32_t hertz, uint32_t opaque, void(*callback)(uint32_t))
a very simple, not-very-accurate softirq callback. May be called multiple times returns 0 if ok,...
int(* unhandled_call2)()
safety call, terminates the app
int(* userconfig_copy)(uint8_t *buf, uint16_t bufsize, uint16_t *actual_size)
this copies the userconfig into a ram buffer, IF the version has been updated and IF the destination ...
int(* mculib_pin_set)(MCULIB_HANDLE handle, int pinnum, int highlow)
set IOPin to high or low
Definition: user_app_info.h:75
int(* mculib_spi_disable)(MCULIB_HANDLE handle, int portnum)
disable the spi port
Definition: user_app_info.h:98
struct userapp_info _userapp_info_t
this must be implemented by the userapp, stored in flash at the beginning of the file (offset 0)
int(* com_add_binary_arg)(struct command *com, const int len, const uint8_t *buf)
add an argument to a command with an arbitrary length returns 0 if ok, otherwise errorcode
int(* unhandled_call5)()
safety call, terminates the app
struct sc_firmware_api _sc_firmware_api
these are the callbacks available. the firmware "api". provided to the app on startup
int(* send_via_usb_acm)(uint8_t *buf, int size)
once a usb_acm is "hijacked", the userapp can send bytes through the usb_acm port with this function ...
int(* com_deliver_command)(struct command *com, cpkt_callback)
queue a command for sending. This is asynchronous, transmission will be retried and re-routed as nece...
int(* mculib_spi_write_and_read)(MCULIB_HANDLE handle, int portnum, const uint16_t write, uint16_t *read)
write a byte and read from spi if a transmission is already in progress it waits. this writes and wai...
int(* unhandled_call8)()
safety call, terminates the app
int(* mculib_spi_read16_only)(MCULIB_HANDLE handle, int portnum, uint16_t *read)
there are weird devices, like tli4970, which aren't really spi. they basically start sending as soon ...
void(* debugf)(const char *format,...)
print a message on console/usb (only)
Definition: user_app_info.h:32
int(* mculib_pin_get)(MCULIB_HANDLE handle, int pinnum)
get current pin level (high or low) this is a bit funny: 0 or 1 indicate the pin level....
Definition: user_app_info.h:84
int(* time_get_diff)(struct sctime *time1, struct sctime *time2)
calculate seconds between two time structs
int(* userconfig_write)(uint8_t *buf, uint32_t size)
this saves a buf as "userconfig" RETURN VALUE: 0==ok, anything else is an error (same version is not ...
void(* on_new_node)(struct hostroute *host)
called when and if a new node is detected. this may be used, to, for example detect a route to the cl...
void(* user_loop)()
this is called frequently, but with no timing guarantees. essentially, it's called in the "idle-loop"...
int(* unhandled_call6)()
safety call, terminates the app
void(* hijack_esp32_usart_stop)(void)
hijack the esp32 serial port
int(* unhandled_call4)()
safety call, terminates the app
int(* get_config_flag)(int flag)
get a userconfig flag (0..n)
void(* pin_pwm)(int MCULIBHANDLE, int pin, uint32_t newstate, int flags)
pwm a pin (mosfets, and some other pins as well)
void(* com_free)(struct command *com)
free a command
int(* com_get_arg_size)(const struct command *com, int index)
get size (in bytes) of an argument by index
void(* reset_userapp)(void)
reset the userapp
int(* com_get_named_arg_uint32)(struct command *com, const char *name, uint32_t *result)
0 == ok, otherwise error. if OK, value will be stored in "result"
int(* mculib_serialport_write_string)(MCULIB_HANDLE handle, int port, const char *txt)
write a string to serial port
Definition: user_app_info.h:88
int(* mculib_pin_out)(MCULIB_HANDLE handle, int pinnum, int speed)
configure and enable an IO-Pin as output note: pinnum are sequential 0-99 are PAnn,...
Definition: user_app_info.h:56
void(* time_bcd_to_timestruct)(uint32_t bcd_time, uint32_t bcd_date, struct sctime *out)
convert BCDTime and BCDDate to timestruct
int(* mcu_set_speed)(int MCULIBHANDLE, int speed)
change cpu speed - sometimes we need to be guaranteed to be fast. speed [1==very fast ....
int(* com_send_reply_with_args)(struct command *com, uint8_t flags, const char *format,...)
send a reply together with arguments
int(* unhandled_call7)()
safety call, terminates the app
int(* mculib_pin_out_opendrain)(MCULIB_HANDLE handle, int pinnum, int speed)
configure and enable an IO-Pin as output note: pinnum are sequential 0-99 are PAnn,...
Definition: user_app_info.h:61
int(* mculib_spi_speed)(MCULIB_HANDLE handle, int portnum, long khz)
set the spi speed (after it's open and running) returns new speed or 0 if failure
void(* proto_release)(void *buf)
release a proto struct
uint32_t(* userconfig_version)()
each time the config is updated, the version is incremented. in other words, unless the version is ch...
int(* proto_struct_to_buf)(void *buf, int bufsize, void *iter, void *in)
encode a struct to a buffer. returns number of bytes written. (-1 for error)
definitions of routing table structures
int com
Definition: command.h:22
int index
Definition: command.h:13
uint8_t flags
Definition: command.h:23
these are the callbacks available. the firmware "api". provided to the app on startup
Definition: user_app_info.h:29
Definition: sctime.h:3
runtime data per sensor (in-ram)
Definition: sensor_dev.h:33
this must be implemented by the userapp, stored in flash at the beginning of the file (offset 0)