/*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ #include #include #include #include #include #include #include "led.h" #include "ledlib.h" #include #include #include /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ #define MAX_HANDLES 30 static struct _led_handles { unsigned int used; LED_MODULE_T data; } led_handles[MAX_HANDLES]; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int ledlib_init(void) { memset(led_handles, sizeof(struct _led_handles), 0x0); return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int ledlib_deinit(void) { return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ void *ledlib_open(char *name, int instance) { unsigned char *handle; handle = malloc(sizeof(int) + strlen(name) + 1); if(handle == NULL) { return NULL; } *((int *) handle) = instance; strcpy(handle + sizeof(int), name); return handle; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int ledlib_close(void *handle) { if(handle != NULL) { free(handle); } return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int ledlib_action(void *handle, int new_state) { char buffer[40]; int led = open(LED_DEV_FILE, O_RDWR); if(led < 0) return -1; sprintf(buffer, "SET %s,%d = %d\n", (char *) handle + sizeof(int), *((int *) handle), new_state); write(led, buffer, strlen(buffer)); close(led); return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int ledlib_action_with_event(void *handle, int new_state, unsigned int param_len, void *param) { char buffer[4096]; unsigned int length = 0; int led = open(LED_DEV_FILE, O_RDWR); if(led < 0) return -1; sprintf(buffer, "EVENT %s,%d = %d\n", (char *) handle + sizeof(int), *((int *) handle), new_state); length = strlen(buffer); length += 3; length &= ~3; *((unsigned int *) (buffer + length)) = param_len; memcpy(buffer + length + sizeof(unsigned int), param, param_len); length += sizeof(unsigned int) + param_len; write(led, buffer, length); close(led); return 0; }