/********************************************************************\ Name: null.c Created by: Stefan Ritt Contents: NULL bus driver $Id$ \********************************************************************/ #include #include #include #include #include "midas.h" static int debug_flag = 0; typedef struct { char device[NAME_LENGTH]; } NULL_SETTINGS; #define NULL_SETTINGS_STR "\ Device = STRING : [32] com1\n\ " typedef struct { NULL_SETTINGS settings; int fd; /* device handle for NULL device */ } NULL_INFO; /*------------------------------------------------------------------*/ int null_open(char *device) { int fd; /* open device using device name, like fd = open(device); if (fd == -1) return -1; */ /* simulate open */ fd = 1; return fd; } /*----------------------------------------------------------------------------*/ INT null_exit(NULL_INFO * info) { /* something like close(info->fd); */ return SUCCESS; } /*----------------------------------------------------------------------------*/ int null_write(NULL_INFO * info, char *data, int size) { DWORD written, i; if (debug_flag) { FILE *f; f = fopen("null.log", "a"); fprintf(f, "write: "); for (i = 0; (int) i < size; i++) fprintf(f, "%X ", data[i]); fprintf(f, "\n"); fclose(f); } /* something like write(info->fd, data, size, &written); */ /* simulate writing */ written = size; return written; } /*----------------------------------------------------------------------------*/ int null_read(NULL_INFO * info, char *data, int size, int timeout) { int i, l; /* somethng like memset(data, 0, size); for (l=0 ; lfd, data+l, 1, &i); if (!status || i == 0) break; } */ /* simulate reading */ ss_sleep(10); l = 0; if (debug_flag && l > 0) { FILE *f; f = fopen("null.log", "a"); fprintf(f, "read: "); for (i = 0; i < l; i++) fprintf(f, "%X ", data[i]); fprintf(f, "\n"); fclose(f); } return l; } /*----------------------------------------------------------------------------*/ int null_puts(NULL_INFO * info, char *str) { DWORD written; if (debug_flag) { FILE *f; f = fopen("null.log", "a"); fprintf(f, "puts: %s\n", str); fclose(f); } /* something like write(info->fd, str, strlen(str), &written); */ /* simulate writing */ written = strlen(str); return written; } /*----------------------------------------------------------------------------*/ int null_gets(NULL_INFO * info, char *str, int size, char *pattern, int timeout) { int l; /* something like memset(str, 0, size); for (l=0 ; lfd, str+l, 1, &i); if (!status || i == 0) { if (pattern && pattern[0]) return 0; break; } if (pattern && pattern[0]) if (strstr(str, pattern) != NULL) break; } */ /* simulate reading */ ss_sleep(10); l = 0; str[0] = 0; if (debug_flag && l > 0) { FILE *f; f = fopen("null.log", "a"); fprintf(f, "gets %s: %s\n", pattern, str); fclose(f); } return l; } /*----------------------------------------------------------------------------*/ int null_init(HNDLE hkey, NULL_INFO **pinfo) { HNDLE hDB, hkeybd; INT size, status; NULL_INFO *info; /* allocate info structure */ info = (NULL_INFO*) calloc(1, sizeof(NULL_INFO)); *pinfo = info; cm_get_experiment_database(&hDB, NULL); /* create NULL settings record */ status = db_create_record(hDB, hkey, "BD", NULL_SETTINGS_STR); if (status != DB_SUCCESS) return FE_ERR_ODB; db_find_key(hDB, hkey, "BD", &hkeybd); size = sizeof(info->settings); db_get_record(hDB, hkeybd, &info->settings, &size, 0); /* open port */ info->fd = null_open(info->settings.device); if (info->fd < 0) return FE_ERR_HW; return SUCCESS; } /*----------------------------------------------------------------------------*/ INT null(INT cmd, ...) { va_list argptr; HNDLE hkey; INT status, size, timeout; NULL_INFO *info; char *str, *pattern; va_start(argptr, cmd); status = FE_SUCCESS; switch (cmd) { case CMD_INIT: { hkey = va_arg(argptr, HNDLE); NULL_INFO **pinfo = va_arg(argptr, NULL_INFO **); status = null_init(hkey, pinfo); break; } case CMD_EXIT: info = va_arg(argptr, NULL_INFO *); status = null_exit(info); break; case CMD_NAME: info = va_arg(argptr, NULL_INFO *); str = va_arg(argptr, char *); strcpy(str, "null"); break; case CMD_WRITE: info = va_arg(argptr, NULL_INFO *); str = va_arg(argptr, char *); size = va_arg(argptr, int); status = null_write(info, str, size); break; case CMD_READ: info = va_arg(argptr, NULL_INFO *); str = va_arg(argptr, char *); size = va_arg(argptr, INT); timeout = va_arg(argptr, INT); status = null_read(info, str, size, timeout); break; case CMD_PUTS: info = va_arg(argptr, NULL_INFO *); str = va_arg(argptr, char *); status = null_puts(info, str); break; case CMD_GETS: info = va_arg(argptr, NULL_INFO *); str = va_arg(argptr, char *); size = va_arg(argptr, INT); pattern = va_arg(argptr, char *); timeout = va_arg(argptr, INT); status = null_gets(info, str, size, pattern, timeout); break; case CMD_DEBUG: status = va_arg(argptr, INT); debug_flag = status; break; } va_end(argptr); return status; }