/*-*-C-*-*/ #include #include #include #include #include #include #include #include "knobCtrlDef.h" #include "knobUserEpics.h" #include "knobUserEpicsUtil.h" static int UNKNOWN = 0; /* device types */ static int MAGNET = 1; static int RF = 2; static int MATLAB = 3; static int MKB = 4; /* * Callback to determine knob label and knob sensitivity * * Parse device name to determine knob lable based on LCLS conventions * Call get_sens to read or calculate sensitivity * * Return -1 if device should not be attached, else 0 */ static int get_knob_settings(char *rname, int *fsens, epicsFloat64 *sens, int *flabel, char *label, int *fprec, epicsInt32 *prec, int dbg, epicsFloat64 drv, epicsFloat64 drvl, epicsFloat64 drvh, epicsFloat64 lopr, epicsFloat64 hopr, int *msg) { char name[MAX_NAME_LENGTH], tmpname[MAX_NAME_LENGTH]; int i, l, n = 0, type = UNKNOWN; char prim[MAX_NAME_LENGTH] = "\0", loca[MAX_NAME_LENGTH] = "\0"; char unit[MAX_NAME_LENGTH] = "\0", subunit[MAX_NAME_LENGTH] = "\0"; char attr[MAX_NAME_LENGTH] = "\0", *c; void *tmpdevice_p, *tmp; if ( dbg >= HIGH ) printf("knobUserLcls: Before file read, flags: sens %i label %i prec %i\n", *fsens, *flabel, *fprec); strcpy( name, rname ); knobUserGetSettingsFromFile( name, fsens, sens, flabel, label, fprec, prec, dbg ); if ( dbg >= HIGH ) printf("knobUserLcls: After file read, flags: sens %i label %i prec %i\n", *fsens, *flabel, *fprec); /* We're not yet ready to use label from file, so for now, set flag to 0 * When we are ready to use it, just remove this line: */ *flabel = 0; /* If label not read from file, build knob label */ if ( 0 == *flabel ) { /* Parse devname up into primary, ioc loc, unit, attribute * Count up occurrences of :, then break devname up into tokens * If devname ends in :, find 1 fewer token */ c = strchr(name, ':'); while( c != NULL ) { c=strchr((c+1), ':'); n++; } c=strchr(name, '\0'); c = &c[-1]; if( *(c) == ':' ) n--; for( i = 0; i <= n; i++ ) { if ( i == 0 ) strcpy( prim, strtok(name, ":") ); if ( i == 1 ) strcpy( loca, strtok(NULL, ":") ); if ( i == 2 ) strcpy( unit, strtok(NULL, ":") ); if ( n == 3 ) { if( i == 3 ) strcpy( attr, strtok(NULL, ":") ); } else if ( n > 3 ) { if( i == 3 ) strcpy( subunit, strtok(NULL, ":") ); if( i == 4 ) strcpy( attr, strtok(NULL, ":") ); } } if ( dbg >= MED ) printf("knobUserLcls: %s prim %s loca %s unit %s subunit %s attr %s\n", rname, prim, loca, unit, subunit, attr); /* Don't attach knob PVs (most likely it is unintentional anyway) */ if( strcmp(prim,"KNOB") == 0 ) return -1; /* Classify device type in order to generate legend */ if( strcmp(prim,"XCOR")==0 || strcmp(prim,"YCOR")==0 || strcmp(prim,"LGPS")==0 || strcmp(prim,"QUAD")==0 || strcmp(prim,"BEND")==0 || strcmp(prim,"BTRM")==0 || strcmp(prim,"SOLN")==0 || strcmp(prim,"KICK")==0 || strcmp(prim,"QTRM")==0 || strcmp(prim,"MGNT")==0 ) { type = MAGNET; } else if( strcmp(prim,"MATLAB")==0 || strcmp(unit,"ML00")==0 ) type = MATLAB; else if( strcmp(prim,"MKB")==0 ) type = MKB; if ( type == MAGNET ) { /* If magnet, attr must be BCTRL in order to perturb (not trim) */ if ( 0 != strcmp(attr,"BCTRL") ) { *msg = KNOB_FAC_MSG; strcpy( knobmsgarray[KNOB_FAC_MSG], "Magnet attribute must be BCTRL" ); return -1; } sprintf( label, "%.16s", rname); *flabel = 1; if ( dbg >= MED ) printf("knobUserLcls: %s device is MAGNET, label is %s\n", rname, label); } else if ( type == MKB ) { /* If multiknob, get label and sens PVs */ if ( knobUserDevCommCallbacks.init_knob ) { if ( 0 == (tmpdevice_p = knobUserDevCommCallbacks.init_knob()) ) { printf("knobUserLcls: %s Warning: Failed to get get meta data\n", rname); goto egress; } } else goto egress; tmp = 0; sprintf( tmpname, "%s:%s:%s:LABEL", prim, loca, unit ); if ( knobUserDevCommCallbacks.connect_get_val ) { tmp = knobUserDevCommCallbacks.connect_get_val( tmpdevice_p, tmpname, dbg ); if ( !tmp ) { if ( dbg >= LOW ) printf("knobUserLcls: %s failed to get MKB %s:%s:%s knob label, using %s\n", rname, prim, loca, unit, label); } else { strcpy( label, tmp ); *flabel = 1; } } /* Reset and reuse tmpdevice_p to get sensitivity */ if ( knobUserDevCommCallbacks.reset_knob ) knobUserDevCommCallbacks.reset_knob( tmpdevice_p, dbg ); tmp = 0; sprintf( tmpname, "%s:%s:%s:SENS", prim, loca, unit ); if ( knobUserDevCommCallbacks.connect_get_val ) { tmp = knobUserDevCommCallbacks.connect_get_val( tmpdevice_p, tmpname, dbg ); if ( !tmp ) { if ( dbg >= LOW ) printf("knobUserLcls: %s failed to get %s\n", rname, tmpname); } else { *sens = *(epicsFloat64 *)tmp; *fsens = 1; } } if ( dbg >= MED ) printf("knobUserLcls: device is MKB %s label %s sens %f\n", rname, label, *sens); } else if ( type == MATLAB ) { /* If matlab, label = first 9 characters of .DESC field */ if ( knobUserDevCommCallbacks.init_knob ) { if ( 0 == (tmpdevice_p = knobUserDevCommCallbacks.init_knob()) ) { printf("knobUserLcls: %s Failed to get get meta data\n", rname); goto egress; } } else goto egress; sprintf( tmpname, "%s:%s:%s:%s.DESC", prim, loca, unit, attr); tmp = 0; if ( knobUserDevCommCallbacks.connect_get_val ) { tmp = knobUserDevCommCallbacks.connect_get_val( tmpdevice_p, tmpname, dbg ); if ( !tmp ) { strncpy( label, attr, LEGEND_LABEL_SIZE ); if ( dbg >= LOW ) printf("knobUserLcls: %s failed to get Matlab PV description %s, using %f\n", rname, label, *sens); } else strncpy( label, tmp, LEGEND_LABEL_SIZE ); } *flabel = 1; printf("knobUserLcls: %s device is MATLAB, label is %s\n", name, label); } else { /* Default case, use first 16 characters of PV name */ sprintf( label, "%.16s", rname); *flabel = 1; if ( dbg >= MED ) printf("knobUserLcls: default case %s device label is %s\n", name, label); } } if ( *flabel ) { l = strlen(label); /* Fill the rest with space characters, remove end-of-string */ for ( i = 0 ; i < LEGEND_LABEL_SIZE; i++ ) { if ( i > strlen( label ) ) label[i] = ' '; } } egress: if ( 0 == *fsens ) { if ( drv ) *sens = (drvh - drvl)/10; else if ( hopr > lopr ) *sens = (hopr - lopr)/10; else *sens = 1.0; *fsens = 1; } return 0; } kuserfaccb_ts knobUserLclsCb = { knob_settings: get_knob_settings }; static void drvKnobUserLclsRegistrar(void) { static int firstTime = 1; if ( firstTime ) { registryAdd( (void*)knobFacCbRegistryId, "knobUserLclsCb", &knobUserLclsCb ); firstTime = 0; } } epicsExportRegistrar(drvKnobUserLclsRegistrar);