/* * ledapp.c -- leds manipulation * */ #include #include #include #include #include #include #include #include #include #include "led.h" /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_mod(char *line); /*--- int read_instance(char *line); ---*/ int read_state(char *line); int read_gpio(char *line); int read_mode(char *line); int read_param1(char *line); int read_param2(char *line); int read_param(char *line); unsigned int debug = 0; char *program; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ typedef struct config_kw_handler{ char *name; int (*handler)(char *line); } config_kw_handler_t; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ config_kw_handler_t kwArr[] = { {"module", read_mod}, /*--- {"instance",read_instance}, ---*/ {"state", read_state}, {"gpio", read_gpio}, {"mode", read_mode}, {"param1", read_param1}, {"param2", read_param2}, {"param", read_param} }; #define MAX_KEYWORDS (sizeof(kwArr) / sizeof(config_kw_handler_t)) char curModule[MODULE_NAME_LEN]; int curModuleNr; char curModuleName[MODULE_NAME_LEN]; int curInstance=0; int gpio_count; char curName[15]; unsigned int do_demon = 0; LED_CONFIG_T *pTemp; LED_CONFIG_T *ptrArr[MAX_STATES]; int ptrIndex = 0; char arr[255]; /*------------------------------------------------------------------------------------------*\ * format: module = blabla, 0 : 23 [, oldname] 0: instance, 23 module nr (fuer kompatibilitaet) \*------------------------------------------------------------------------------------------*/ int read_mod(char *line) { char *p1 = strchr(line, ','); char *p2 = strchr(line, ':'); char *p3; /* Remember the new module's name */ if(p1 == NULL || p2 == NULL) { DEB_ERR("ERROR: module number missing !\n"); exit(-1); } p3 = strchr(p2, ','); *p1++ = '\0'; *p2++ = '\0'; strcpy(curModule, line); /*--- name der Gruppe ---*/ curInstance = atoi(p1); /*--- instance der Gruppe ---*/ curModuleNr = atoi(p2); /*--- id fuer shell und event module ---*/ if(p3) { *p3++ = '\0'; while(p3 && *p3 == ' ') p3++; } strcpy(curModuleName, p3 ? p3 : curModule); if(do_demon) { demon_register_name(curModule, curModuleNr, curInstance, p3 ? p3 : curModule); } /*--- printf("[read_mod]: curModule %s curInstance %u curModuleNr %u curModuleName %s\n", ---*/ /*--- curModule, curInstance, curModuleNr, curModuleName); ---*/ return 1; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ #if 0 int read_instance(char *line) { /* Remember the new instance id */ unsigned int i; i = atoi(line); if(i < MAX_MODULE_INSTANCES) { curInstance = i; if(do_demon) { demon_register_name(curModule, curModuleNr, curInstance); } return 1; } else { printf("Invalid module instance id\n"); return 0; } } #endif /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_state(char *line) { unsigned int i; /* Malloc memory for new state info */ pTemp = malloc( sizeof(LED_CONFIG_T)); if(pTemp == NULL) return 0; /* Remember the pointer */ ptrArr[ptrIndex++] = (LED_CONFIG_T *)pTemp; memset(pTemp,0x00,sizeof(LED_CONFIG_T)); /* save the module name,instance and state var */ strcpy(pTemp->name,curModule); pTemp->instance = curInstance; /*--- printf("[read_state] set event_id and event_name\n"); ---*/ pTemp->event_id = curModuleNr ; strcpy(pTemp->event_name, curModuleName); i = atoi(line); if(i < MAX_STATE_ENTRIES) { pTemp->state = i; pTemp->gpio_num = 0; } else { DEB_ERR("Invalid module state id\n"); return 0; } return 1; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_gpio(char *line) { unsigned int i; /* Save the LED number */ i = atoi(line); if(i == (unsigned int)-1) { DEB_WARN("read_gpio: no gpio defined for module %s state %u\n", curModule, pTemp->state); i = 0; } if(i < MAX_GPIO_PIN_NUM && pTemp->gpio_num < MAX_GPIOS_PER_STATE) { pTemp->gpio[pTemp->gpio_num++] = atoi(line); return 1; } else { DEB_ERR("Invalid gpio id/count\n"); return 0; } } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_mode(char *line) { unsigned int i; i = atoi(line); if(i >= NUM_LED_MODES) { DEB_ERR(" invalid LED mode\n"); return 0; } /* Save the mode */ if(pTemp->gpio_num >= 1) pTemp->mode[pTemp->gpio_num - 1] = i; return 1; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_param1(char *line) { pTemp->param1 = atoi(line); return 1; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_param2(char *line) { pTemp->param2 = atoi(line); return 1; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_param(char *line) { pTemp->param1 = pTemp->param2 = atoi(line); return 1; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int read_config(char *file) { FILE *in; char buffer[80], orig[80], *token, *line; int i, j, index; if (!(in = fopen(file, "r"))) { DEB_ERR("unable to open config file\n"); return 0; } if(debug) printf("[%s] config file %s\n", program, file); index = -1; while (fgets(buffer, 80, in)) { if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; strncpy(orig, buffer, 80); if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; token = buffer + strspn(buffer, " \t"); if (*token == '\0') continue; line = token + strcspn(token, " \t="); if (*line == '\0') continue; *line = '\0'; line++; /* eat leading whitespace */ line = line + strspn(line, " \t="); /* eat trailing whitespace */ for (i = strlen(line) ; i > 0 && isspace(line[i-1]); i--); line[i] = '\0'; for(j=0 ; j] [-l[ist]] [-h[elp]]\n", name); exit (1); } #endif /*--- #if !defined(LEDLIB) ---*/ /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ #if !defined(LEDLIB) int main(int argc, char *argv[]) { unsigned int do_config = 0; unsigned int do_list = 0; unsigned int do_stop_demon = 0; char *config_file = CONF_FILE; int i; program = argv[0]; /*--------------------------------------------------------------------------------------*\ \*--------------------------------------------------------------------------------------*/ for(i = 1 ; i < argc ; i++) { if(argv[i][0] == '-') { switch(argv[i][1]) { case 'D': debug = 1; break; case 'd': do_demon = 1; break; case 's': do_stop_demon = 1; break; case 'c': do_config = 1; break; case 'l': do_list = 1; break; case 'f': config_file = argv[++i]; break; case 'h': usage(argv[0]); break; } } } if(!do_config && !do_stop_demon && !do_demon && !do_list) { usage(argv[0]); } /*--------------------------------------------------------------------------------------*\ \*--------------------------------------------------------------------------------------*/ if(do_config || do_demon) { if(debug) printf("[%s] MODULE_NAME_LEN=%u MAX_STATES=%u MAX_MODULE_INSTANCES=%u\n", program, MODULE_NAME_LEN, MAX_STATES, MAX_MODULE_INSTANCES); read_config(config_file); } /*--------------------------------------------------------------------------------------*\ \*--------------------------------------------------------------------------------------*/ if(do_config) { int fp; DEB_TRACE("[%s]: config %s from %s\n", argv[0], LED_DEV_FILE, config_file); if ((fp = open(LED_DEV_FILE, O_RDWR)) == -1) { DEB_ERR("unable to open LED device file\n"); return 1; } /* Configure element through ioctl system calls */ for ( i = 0 ; i < ptrIndex ; i++ ) { LED_CONFIG_T *temp = ptrArr[i]; if(temp->gpio[0] == 0) { DEB_WARN("[skip]"); continue; } if(temp->gpio[0] == -1) { DEB_WARN("[skip]"); continue; } if(ioctl(fp, LED_CONFIG,(int)temp) && debug) printf("[%s] ioctl LED_CONFIG name %s instante %u gpio %u failed\n", program, temp->name, temp->instance, temp->gpio[0]); free(temp); } ioctl(fp, LED_CONFIG_DONE, 0); DEB_TRACE("\n Configured %d states \n",ptrIndex); close(fp); } /*--------------------------------------------------------------------------------------*\ \*--------------------------------------------------------------------------------------*/ if(do_stop_demon) { int fd; DEB_TRACE("[%s]: stop led-demon\n", argv[0]); fd = open(LED_PIPE_FILE, O_WRONLY); if(fd >= 0) { write(fd, "stop", 4); close(fd); } } /*--------------------------------------------------------------------------------------*\ \*--------------------------------------------------------------------------------------*/ if(do_list) { int fd; DEB_TRACE("[%s]: list led-demon\n", argv[0]); fd = open(LED_PIPE_FILE, O_WRONLY); if(fd >= 0) { write(fd, "list", 4); close(fd); } } /*--------------------------------------------------------------------------------------*\ \*--------------------------------------------------------------------------------------*/ if(do_demon) { printf("[%s]: start led-demon\n", argv[0]); led_demon(); } return 0; } #endif /*--- #if !defined(LEDLIB) ---*/