Skip to content

Commit 6c489fc

Browse files
committed
Add syslog and debugging messages
1 parent 4b7e11f commit 6c489fc

File tree

8 files changed

+211
-52
lines changed

8 files changed

+211
-52
lines changed

source/c_pwm.c

Lines changed: 79 additions & 7 deletions
Large diffs are not rendered by default.

source/c_pwm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SOFTWARE.
2525

2626
#include "common.h"
2727

28+
BBIO_err initialize_pwm(void);
2829
BBIO_err pwm_start(const char *key, float duty, float freq, int polarity);
2930
BBIO_err pwm_disable(const char *key);
3031
BBIO_err pwm_set_frequency(const char *key, float freq);

source/common.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SOFTWARE.
3232

3333
#include "Python.h"
3434
#include <dirent.h>
35+
#include <syslog.h>
3536
#include <time.h>
3637
#include <string.h>
3738
#include <glob.h>
@@ -44,6 +45,17 @@ SOFTWARE.
4445
# endif
4546
#endif
4647

48+
int gpio_mode;
49+
int gpio_direction[120];
50+
51+
#ifdef BBBVERSION41
52+
char ctrl_dir[43];
53+
char ocp_dir[33];
54+
#else
55+
char ctrl_dir[35];
56+
char ocp_dir[25];
57+
#endif
58+
4759
int setup_error = 0;
4860
int module_setup = 0;
4961

@@ -539,3 +551,18 @@ BBIO_err unload_device_tree(const char *name)
539551

540552
return BBIO_OK;
541553
}
554+
555+
void initlog(int level, const char* ident, int option)
556+
{
557+
static int initialized = 0;
558+
if (initialized) return;
559+
560+
setlogmask(LOG_UPTO(level));
561+
if (option == -1) {
562+
option = BBIO_LOG_OPTION;
563+
}
564+
openlog(ident, option, LOG_LOCAL1);
565+
syslog(LOG_NOTICE, "libadafruit-bbio version %s initialized", "<unknown>");
566+
567+
initialized = 1;
568+
}

source/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ BBIO_err unload_device_tree(const char *name);
8080
int device_tree_loaded(const char *name);
8181
BBIO_err get_pwm_by_key(const char *key, pwm_t **pwm);
8282

83+
#define BBIO_LOG_OPTION LOG_CONS | LOG_PID | LOG_NDELAY
84+
void initlog(int level, const char* ident, int option);
8385

8486
int setup_error;
8587
int module_setup;

source/event_gpio.c

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828
SOFTWARE.
2929
*/
3030

31+
#include <errno.h>
32+
#include <fcntl.h>
3133
#include <pthread.h>
32-
#include <sys/epoll.h>
3334
#include <stdio.h>
3435
#include <stdlib.h>
35-
#include <fcntl.h>
36-
#include <unistd.h>
3736
#include <string.h>
37+
#include <sys/epoll.h>
38+
#include <syslog.h>
39+
#include <unistd.h>
40+
3841
#include "event_gpio.h"
3942
#include "common.h"
4043

@@ -70,14 +73,16 @@ int event_occurred[120] = { 0 };
7073
int thread_running = 0;
7174
int epfd = -1;
7275

73-
int gpio_export(unsigned int gpio)
76+
BBIO_err gpio_export(unsigned int gpio)
7477
{
7578
int fd, len;
7679
char str_gpio[10];
7780

7881
// already exported by us?
79-
if (exported_gpios[gpio] != GPIO_NOT_EXPORTED)
80-
return 0;
82+
if (exported_gpios[gpio] != GPIO_NOT_EXPORTED) {
83+
syslog(LOG_DEBUG, "gpio_export: %u already exported", gpio);
84+
return BBIO_OK;
85+
}
8186

8287
// already exported by someone else?
8388
char gpio_path[64];
@@ -86,23 +91,33 @@ int gpio_export(unsigned int gpio)
8691
if (access(gpio_path, R_OK|W_OK|X_OK) != -1)
8792
{
8893
exported_gpios[gpio] = GPIO_ALREADY_EXPORTED;
89-
return 0;
94+
syslog(LOG_DEBUG, "gpio_export: %u already exported", gpio);
95+
return BBIO_OK;
9096
}
9197

92-
if ((fd = open("/sys/class/gpio/export", O_WRONLY)) < 0)
98+
#define GPIO_EXPORT "/sys/class/gpio/export"
99+
100+
if ((fd = open(GPIO_EXPORT, O_WRONLY)) < 0)
93101
{
94-
return -1;
102+
syslog(LOG_ERR, "gpio_export: %u couldn't open '"GPIO_EXPORT"': %i-%s",
103+
gpio, errno, strerror(errno));
104+
return BBIO_SYSFS;
95105
}
106+
96107
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
97108
int ret = write(fd, str_gpio, len);
98109
close(fd);
99110
if (ret < 0) {
100-
return ret;
111+
syslog(LOG_ERR, "gpio_export: %u couldn't write '"GPIO_EXPORT"': %i-%s",
112+
gpio, errno, strerror(errno));
113+
return BBIO_SYSFS;
101114
}
102115

103116
// add to list
104117
exported_gpios[gpio] = GPIO_EXPORTED;
105-
return 0;
118+
119+
syslog(LOG_DEBUG, "gpio_export: %u OK", gpio);
120+
return BBIO_OK;
106121
}
107122

108123
void close_value_fd(unsigned int gpio)
@@ -176,13 +191,15 @@ int open_value_file(unsigned int gpio)
176191
}
177192

178193
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
179-
return -1;
194+
syslog(LOG_ERR, "gpio open_value_file: %u couldn't open '%s': %i-%s",
195+
gpio, filename, errno, strerror(errno));
196+
return -1;
180197
}
181198
add_fd_list(gpio, fd);
182199
return fd;
183200
}
184201

185-
int gpio_unexport(unsigned int gpio)
202+
BBIO_err gpio_unexport(unsigned int gpio)
186203
{
187204
int fd, len;
188205
char str_gpio[10];
@@ -192,34 +209,47 @@ int gpio_unexport(unsigned int gpio)
192209

193210
close_value_fd(gpio);
194211

195-
if ((fd = open("/sys/class/gpio/unexport", O_WRONLY)) < 0)
196-
return -1;
212+
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
213+
214+
if ((fd = open(GPIO_UNEXPORT, O_WRONLY)) < 0) {
215+
syslog(LOG_ERR, "gpio_unexport: %u couldn't open '"GPIO_UNEXPORT"': %i-%s",
216+
gpio, errno, strerror(errno));
217+
return BBIO_SYSFS;
218+
}
197219

198220
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
199221
int ret = write(fd, str_gpio, len);
200222
close(fd);
201223
if (ret < 0) {
202-
return ret;
224+
syslog(LOG_ERR, "gpio_unexport: %u couldn't write '"GPIO_UNEXPORT"': %i-%s",
225+
gpio, errno, strerror(errno));
226+
return BBIO_SYSFS;
203227
}
204228

205229
// remove from list
206230
exported_gpios[gpio] = GPIO_NOT_EXPORTED;
207-
return 0;
231+
232+
syslog(LOG_DEBUG, "gpio_unexport: %u OK", gpio);
233+
return BBIO_OK;
208234
}
209235

210-
int gpio_set_direction(unsigned int gpio, unsigned int in_flag)
236+
BBIO_err gpio_set_direction(unsigned int gpio, unsigned int in_flag)
211237
{
212238
int fd;
213239
char filename[40];
214240
char direction[10] = { 0 };
215241

216242
if ((gpio >= USR_LED_GPIO_MIN) && (gpio <= USR_LED_GPIO_MAX)) {
217-
return 0; // direction is not applicable to the USR LED pins
243+
syslog(LOG_DEBUG, "gpio_set_direction: %u not applicable to the USR LED", gpio);
244+
return BBIO_OK; // direction is not applicable to the USR LED pins
218245
}
219246

220247
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction", gpio);
221-
if ((fd = open(filename, O_WRONLY)) < 0)
222-
return -1;
248+
if ((fd = open(filename, O_WRONLY)) < 0) {
249+
syslog(LOG_ERR, "gpio_set_direction: %u couldn't open '%s': %i-%s",
250+
gpio, filename, errno, strerror(errno));
251+
return BBIO_SYSFS;
252+
}
223253

224254
if (in_flag) {
225255
strncpy(direction, "out", ARRAY_SIZE(direction) - 1);
@@ -230,38 +260,47 @@ int gpio_set_direction(unsigned int gpio, unsigned int in_flag)
230260
int ret = write(fd, direction, strlen(direction));
231261
close(fd);
232262
if (ret < 0) {
233-
return ret;
263+
syslog(LOG_ERR, "gpio_set_direction: %u couldn't write '%s': %i-%s",
264+
gpio, filename, errno, strerror(errno));
265+
return BBIO_SYSFS;
234266
}
235267

236-
return 0;
268+
syslog(LOG_DEBUG, "gpio_set_direction: %u OK", gpio);
269+
return BBIO_OK;
237270
}
238271

239-
int gpio_get_direction(unsigned int gpio, unsigned int *value)
272+
BBIO_err gpio_get_direction(unsigned int gpio, unsigned int *value)
240273
{
241274
int fd;
242275
char direction[4] = { 0 };
243276
char filename[40];
244277

245278
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction", gpio);
246-
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0)
247-
return -1;
279+
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
280+
syslog(LOG_ERR, "gpio_get_direction: %u couldn't open '%s': %i-%s",
281+
gpio, filename, errno, strerror(errno));
282+
return BBIO_SYSFS;
283+
}
248284

249285
lseek(fd, 0, SEEK_SET);
250286
int ret = read(fd, &direction, sizeof(direction) - 1);
251287
if (ret < 0) {
252-
return ret;
288+
syslog(LOG_ERR, "gpio_get_direction: %u couldn't read '%s': %i-%s",
289+
gpio, filename, errno, strerror(errno));
290+
return BBIO_SYSFS;
253291
}
254292

255293
if (strcmp(direction, "out") == 0) {
256294
*value = OUTPUT;
257295
} else {
258296
*value = INPUT;
259297
}
260-
261-
return 0;
298+
299+
syslog(LOG_DEBUG, "gpio_get_direction: %u OK", gpio);
300+
return BBIO_OK;
262301
}
263302

264-
int gpio_set_value(unsigned int gpio, unsigned int value)
303+
BBIO_err gpio_set_value(unsigned int gpio, unsigned int value)
265304
{
266305
int fd;
267306
char filename[MAX_FILENAME];
@@ -283,7 +322,9 @@ int gpio_set_value(unsigned int gpio, unsigned int value)
283322
}
284323

285324
if ((fd = open(filename, O_WRONLY)) < 0) {
286-
return -1;
325+
syslog(LOG_ERR, "gpio_set_value: %u couldn't open '%s': %i-%s",
326+
gpio, filename, errno, strerror(errno));
327+
return BBIO_SYSFS;
287328
}
288329

289330
if (value) {
@@ -295,27 +336,35 @@ int gpio_set_value(unsigned int gpio, unsigned int value)
295336
int ret = write(fd, vstr, strlen(vstr));
296337
close(fd);
297338
if (ret < 0) {
298-
return ret;
339+
syslog(LOG_ERR, "gpio_set_value: %u couldn't write '%s': %i-%s",
340+
gpio, filename, errno, strerror(errno));
341+
return BBIO_SYSFS;
299342
}
300343

301-
return 0;
344+
syslog(LOG_DEBUG, "gpio_set_value: %u %u OK", gpio, value);
345+
return BBIO_OK;
302346
}
303347

304-
int gpio_get_value(unsigned int gpio, unsigned int *value)
348+
BBIO_err gpio_get_value(unsigned int gpio, unsigned int *value)
305349
{
306350
int fd = fd_lookup(gpio);
307351
char ch;
308352

309353
if (!fd)
310354
{
311-
if ((fd = open_value_file(gpio)) == -1)
312-
return -1;
355+
if ((fd = open_value_file(gpio)) == -1) {
356+
syslog(LOG_ERR, "gpio_set_value: %u couldn't open value file: %i-%s",
357+
gpio, errno, strerror(errno));
358+
return BBIO_SYSFS;
359+
}
313360
}
314361

315362
lseek(fd, 0, SEEK_SET);
316363
int ret = read(fd, &ch, sizeof(ch));
317364
if (ret < 0) {
318-
return ret;
365+
syslog(LOG_ERR, "gpio_set_value: %u couldn't read value file: %i-%s",
366+
gpio, errno, strerror(errno));
367+
return BBIO_SYSFS;
319368
}
320369

321370
if (ch != '0') {
@@ -324,7 +373,8 @@ int gpio_get_value(unsigned int gpio, unsigned int *value)
324373
*value = 0;
325374
}
326375

327-
return 0;
376+
syslog(LOG_DEBUG, "gpio_get_value: %u OK", gpio);
377+
return BBIO_OK;
328378
}
329379

330380
int gpio_set_edge(unsigned int gpio, unsigned int edge)

source/event_gpio.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ SOFTWARE.
3030
#ifndef EVENT_GPIO_H
3131
#define EVENT_GPIO_H
3232

33+
#include "adafruit/bbio/error.h"
34+
3335
#define NO_EDGE 0
3436
#define RISING_EDGE 1
3537
#define FALLING_EDGE 2
@@ -51,13 +53,13 @@ SOFTWARE.
5153
#define PUD_DOWN 1
5254
#define PUD_UP 2
5355

54-
int gpio_export(unsigned int gpio);
55-
int gpio_unexport(unsigned int gpio);
56+
BBIO_err gpio_export(unsigned int gpio);
57+
BBIO_err gpio_unexport(unsigned int gpio);
5658
void exports_cleanup(void);
57-
int gpio_set_direction(unsigned int gpio, unsigned int in_flag);
58-
int gpio_get_direction(unsigned int gpio, unsigned int *value);
59-
int gpio_set_value(unsigned int gpio, unsigned int value);
60-
int gpio_get_value(unsigned int gpio, unsigned int *value);
59+
BBIO_err gpio_set_direction(unsigned int gpio, unsigned int in_flag);
60+
BBIO_err gpio_get_direction(unsigned int gpio, unsigned int *value);
61+
BBIO_err gpio_set_value(unsigned int gpio, unsigned int value);
62+
BBIO_err gpio_get_value(unsigned int gpio, unsigned int *value);
6163

6264
int add_edge_detect(unsigned int gpio, unsigned int edge);
6365
void remove_edge_detect(unsigned int gpio);

source/py_gpio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ SOFTWARE.
3434
#include "event_gpio.h"
3535
#include "c_pinmux.h"
3636
#include <unistd.h>
37+
#include <syslog.h>
3738

3839
static int gpio_warnings = 1;
3940

@@ -131,7 +132,7 @@ static PyObject *py_setup_channel(__attribute__ ((unused)) PyObject *self, PyObj
131132

132133
// Set the pin value and bail if we get an error.
133134
res = gpio_set_value(gpio, initial);
134-
if(res != 0) {
135+
if (res != BBIO_OK) {
135136
PyErr_SetString(PyExc_ValueError, "Set gpio value failed, missing file or invalid permissions.");
136137
return NULL;
137138
}
@@ -576,6 +577,8 @@ PyMODINIT_FUNC initGPIO(void)
576577

577578
define_constants(module);
578579

580+
initlog(LOG_NOTICE, NULL, BBIO_LOG_OPTION);
581+
579582
if (!PyEval_ThreadsInitialized())
580583
PyEval_InitThreads();
581584

0 commit comments

Comments
 (0)