libicsc  1.0.0
 All Data Structures Files Functions Modules
icsc.h
Go to the documentation of this file.
1 
37 #ifndef _ICSC_H
38 #define _ICSC_H
39 
40 #include <stdint.h>
41 #include <termios.h>
42 #include <sys/types.h>
43 #include <pthread.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 
50 #define ICSC_BROADCAST 0x00
51 #define ICSC_CMD_SYS 0x1F
52 #define ICSC_SYS_PING 0x05
53 #define ICSC_SYS_PONG 0x06
54 #define ICSC_SYS_QSTAT 0x07
55 #define ICSC_SYS_RSTAT 0x08
56 #define ICSC_SYS_RELAY 0x09
57 
58 //When this is used during registerCommand all message will pushed
59 //to the callback function
60 
61 // Packet wrapping characters, defined in standard ASCII table
62 #define SOH 1
63 #define STX 2
64 #define ETX 3
65 #define EOT 4
66 
67 //The number of SOH to start a message
68 //some device like Raspberry was missing the first SOH
69 //Increase or decrease the number to your needs
70 #define ICSC_SOH_START_COUNT 1
71 
72 struct icsc_command;
73 
74 typedef struct icsc_command command_t;
75 typedef struct icsc_command *command_ptr;
76 
77 typedef struct {
78  int uartFD;
79  int dePin;
80  command_ptr commandList;
81  uint8_t station;
82 
83  char header[5];
84 
85  char *buffer;
86 
87  uint8_t recPhase;
88  uint8_t recPos;
89  uint8_t recCommand;
90  uint8_t recLen;
91  uint8_t recStation;
92  uint8_t recSender;
93  uint8_t recCS;
94  uint8_t recCalcCS;
95 
96  pthread_t readThread;
97  int readThreadRunning;
98  pthread_mutex_t uartMutex;
99 } icsc_t, *icsc_ptr;
100 
101 // Format of command callback functions
102 typedef void(*callbackFunction)(icsc_ptr, unsigned char, char, unsigned char, char *);
103 
104 // Structure to store command code / function pairs as a linked list
105 struct icsc_command {
106  char commandCode;
107  callbackFunction callback;
108  struct icsc_command *next;
109 };
110 
111 
112 /* gpio.c */
113 
119 #define ICSC_GPIO_OUTPUT 0
120 #define ICSC_GPIO_INPUT 1
121 
127 extern int icsc_gpio_open(int num, int mode);
128 
133 extern int icsc_gpio_read(int num);
134 
140 extern int icsc_gpio_write(int num, int level);
141 
146 extern int icsc_gpio_close(int num);
147 
150 /* serial.c */
161 extern int icsc_serial_open(const char *path, unsigned long baud);
162 
168 extern int icsc_serial_wait_available(int fd, unsigned long timeout);
169 
174 extern int icsc_serial_available(int fd);
175 
180 extern int icsc_serial_read(int fd);
185 extern void icsc_serial_flush(int fd);
186 
192 extern int icsc_serial_write(int fd, uint8_t c);
193 
198 extern void icsc_serial_close(int fd);
201 /* icsc.c */
202 
207 #define ICSC_CATCH_ALL 0xFF
208 
215 extern int icsc_register_command(icsc_ptr icsc, char command, callbackFunction func);
216 
222 extern int icsc_unregister_command(icsc_ptr icsc, char command);
223 
239 extern icsc_ptr icsc_init_de(const char *uart, unsigned long baud, uint8_t station, int de);
240 
248 extern icsc_ptr icsc_init(const char *uart, unsigned long baud, uint8_t station);
249 
254 extern int icsc_close(icsc_ptr icsc);
255 
272 extern int icsc_send_array(icsc_ptr icsc, uint8_t station, char command, uint8_t len, const char *data);
273 
281 extern int icsc_send_string(icsc_ptr icsc, uint8_t station, char command, const char *str);
282 
290 extern int icsc_send_long(icsc_ptr icsc, uint8_t station, char command, int32_t data);
291 
299 extern int icsc_send_int(icsc_ptr icsc, uint8_t station, char command, int16_t data);
300 
308 extern int icsc_send_char(icsc_ptr icsc, uint8_t station, char command, int8_t data);
309 
326 extern int icsc_broadcast_array(icsc_ptr icsc, char command, uint8_t len, const char *data);
327 
334 extern int icsc_broadcast_string(icsc_ptr icsc, char command, const char *str);
335 
342 extern int icsc_broadcast_long(icsc_ptr icsc, char command, int32_t data);
343 
350 extern int icsc_broadcast_int(icsc_ptr icsc, char command, int16_t data);
351 
358 extern int icsc_broadcast_char(icsc_ptr icsc, char command, int8_t data);
374 extern void icsc_enable_debug();
375 
381 extern void icsc_disable_debug();
382 
391 extern void icsc_debug(const char *fmt, ...);
392 
393 
399 extern void icsc_error(const char *fmt, ...);
402 #ifdef __cplusplus
403 }
404 #endif
405 
406 #endif
int icsc_serial_read(int fd)
Read a byte from the serial port.
Definition: serial.c:166
void icsc_serial_flush(int fd)
Wait until all data sent to the serial port has been delivered to the wire.
Definition: serial.c:176
int icsc_broadcast_int(icsc_ptr icsc, char command, int16_t data)
Send a 16-bit integer to all remote stations.
Definition: icsc.c:406
icsc_ptr icsc_init(const char *uart, unsigned long baud, uint8_t station)
Create a new ICSC context, initialize the hardware, and start listening for messages.
Definition: icsc.c:362
int icsc_close(icsc_ptr icsc)
Close an ICSC instance freeing the memory. Terminates all communication.
Definition: icsc.c:415
int icsc_broadcast_array(icsc_ptr icsc, char command, uint8_t len, const char *data)
Send an array of data (or struct as if it were an array) to all stations.
Definition: icsc.c:391
int icsc_broadcast_long(icsc_ptr icsc, char command, int32_t data)
Send a 32-bit integer to all remote stations.
Definition: icsc.c:400
int icsc_send_long(icsc_ptr icsc, uint8_t station, char command, int32_t data)
Send a 32-bit integer to a remote station.
Definition: icsc.c:375
int icsc_gpio_write(int num, int level)
Set a GPIO to high or low.
Definition: gpio.c:82
int icsc_serial_write(int fd, uint8_t c)
Write a byte to a serial port.
Definition: serial.c:183
Definition: icsc.h:105
icsc_ptr icsc_init_de(const char *uart, unsigned long baud, uint8_t station, int de)
Create a new ICSC context, initialize the hardware, and start listening for messages.
Definition: icsc.c:289
int icsc_unregister_command(icsc_ptr icsc, char command)
Unregister an old command character.
Definition: icsc.c:259
int icsc_send_char(icsc_ptr icsc, uint8_t station, char command, int8_t data)
Send an 8-bit integer to a remote station.
Definition: icsc.c:386
void icsc_serial_close(int fd)
Close the serial port.
Definition: serial.c:192
void icsc_enable_debug()
Enable debug messages.
Definition: icsc.c:14
int icsc_send_array(icsc_ptr icsc, uint8_t station, char command, uint8_t len, const char *data)
Send an array of data (or struct as if it were an array) to a remote station.
Definition: icsc.c:366
void icsc_debug(const char *fmt,...)
Display a debug message to stderr.
Definition: icsc.c:17
void icsc_error(const char *fmt,...)
Display an error message to stderr.
Definition: icsc.c:28
int icsc_serial_open(const char *path, unsigned long baud)
Open a serial device at a specific baud rate.
Definition: serial.c:24
int icsc_gpio_open(int num, int mode)
Open a GPIO device in either input or output mode. Exports the GPIO node.
Definition: gpio.c:11
int icsc_gpio_close(int num)
Close a GPIO. Unexports the GPIO node.
Definition: gpio.c:99
void icsc_disable_debug()
Disable debug messages.
Definition: icsc.c:15
int icsc_send_string(icsc_ptr icsc, uint8_t station, char command, const char *str)
Send a text string to a remote station.
Definition: icsc.c:370
int icsc_broadcast_char(icsc_ptr icsc, char command, int8_t data)
Send an 8-bit integer to all remote stations.
Definition: icsc.c:411
int icsc_send_int(icsc_ptr icsc, uint8_t station, char command, int16_t data)
Send a 16-bit integer to a remote station.
Definition: icsc.c:381
int icsc_serial_available(int fd)
Look to see if serial data is available.
Definition: serial.c:162
int icsc_serial_wait_available(int fd, unsigned long timeout)
Wait for serial data to arrive up until the timeout expires.
Definition: serial.c:143
int icsc_gpio_read(int num)
Read the value of a GPIO.
Definition: gpio.c:60
int icsc_broadcast_string(icsc_ptr icsc, char command, const char *str)
Send a text string to all remote stations.
Definition: icsc.c:395
int icsc_register_command(icsc_ptr icsc, char command, callbackFunction func)
Register a new command callback.
Definition: icsc.c:231
Definition: icsc.h:77