/* $Id: drvBlmUdpComm.c,v 1.2 2013/02/28 02:02:32 gwbrown Exp $ */ /* drvPadUdpComm callbacks for bunch-length */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include volatile unsigned blmCooked = 0; static int blm_cook(PadReply rply, int nsamples, int layout_cm, int kind, void *closure) { int idx = PADRPLY_STRM_CMD_IDX_GET( rply->strm_cmd_idx ); int nch = PADRPLY_STRM_CHANNELS_IN_STRM( rply ); int N = PADRPLY_STRM_NSAMPLES( rply ); int chn, frg, n_frg; BlmChannel ch_p; BlmStation st_p; int i,j; int16_t *v_p = ((int16_t*)rply->data) - 1; register BlmOutNumber *prc_p; register BlmInNumber *vec_p; register BlmOutNumber acc,v; epicsTimeStamp ts; if ( layout_cm ) return -1; if ( ! (st_p = blmGetStation( rply->chnl )) ) { return -1; } if ( (chn = PADRPLY_STRM_CHANNELS_IN_FRAG( rply )) > 1 ) { /* all channels in a single packet (provided we're row-major) */ if ( chn != nch ) { errlogPrintf("blm_cook: channel number mismatch: # in frag: %i, in stream: %i\n", chn, nch); return -1; } N /= chn; if ( N > sizeof( ((BlmChannel)0)->vec )/sizeof( ((BlmChannel)0)->vec[0] ) ) { /* vector too small */ errlogPrintf("blm_cook: Too many samples (%i) for processing vector (dim: %i)\n", N, sizeof( ((BlmChannel)0)->vec )/sizeof( ((BlmChannel)0)->vec[0] )); return -1; } ts.nsec = ntohl( rply->timestampLo ); ts.secPastEpoch = ntohl( rply->timestampHi ); epicsMutexMustLock( st_p->mutex ); for ( j=0, ch_p = st_p->chnls; jprc[i] = (BlmOutNumber)ch_p->vec[i] * v ); } ch_p->sum = acc; ch_p->tsSum = ts; st_p->timedout &= ~BLM_TIMEDOUT(j); } epicsMutexUnlock( st_p->mutex ); st_p->chnlsDone = nch; } else { /* XID is opaque endian-ness; we just compare values */ if ( rply->xid != st_p->xid ) { /* fragment part of a new sequence */ for ( i = 0; i < nch; i++ ) { st_p->chnls[i].sum_acc = 0; st_p->chnls[i].frags = 0; } st_p->xid = rply->xid; st_p->chnlsDone = 0; } /* Unfortunately, the stream data itself doesn't provide information (direct * or indirect about the row dimension -- there was just not enough space * in the protocol header to add this way after the original design). * Therefore, we must inquire the sending side who knows... * passing a negative number of samples just yields the currently * configured number of samples per channel. From this and the number of * samples in this fragment we can compute the row dimension. Assuming * that all fragments are of the same size. */ n_frg = (drvPadUdpCommStrmSetNSamples( rply->chnl, -1 ) + N - 1 ) / N; chn = idx / n_frg; frg = idx % n_frg; if ( N*(frg+1) > sizeof(ch_p->vec)/sizeof(ch_p->vec[0]) ) { /* bad N or frg (too big) */ errlogPrintf("blm_cook: bad N: %i or frg: %i (too big) (idx %i, n_frg %i) for processing vector (dim: %i)\n", N, frg, idx, n_frg, sizeof( ((BlmChannel)0)->vec )/sizeof( ((BlmChannel)0)->vec[0] )); return -1; } epicsMutexMustLock( st_p->mutex ); ch_p = st_p->chnls + chn; prc_p = ch_p->prc + N*frg - 1; vec_p = ch_p->vec + N*frg - 1; acc = 0; for ( i=0; isum_acc += acc; ch_p->frags |= (1<frags == (1<sum = ch_p->sum_acc; ch_p->frags = 0; st_p->timedout &= ~BLM_TIMEDOUT(chn); ch_p->tsSum.nsec = ntohl( rply->timestampLo ); ch_p->tsSum.secPastEpoch = ntohl( rply->timestampHi ); st_p->chnlsDone++; } epicsMutexUnlock( st_p->mutex ); } if ( st_p->chnlsDone == nch ) epicsEventSignal( st_p->rawDataReady ); blmCooked++; return 0; } static int blm_get_prefs(DrvPadUdpCommPrefs prefs_p) { prefs_p->nsamples = BLM_NUM_SAMPLES; prefs_p->d32 = 0; prefs_p->nsamples_dynamic = 0; prefs_p->nchannels_dynamic = 0; prefs_p->col_major = 0; return 0; } static void blm_watchdog(int channel, int kind, void *closure) { BlmStation sta_p; /* this is invoked for all possible 'kind's of data; we only * need to take action once... */ if ( 0 != kind ) return; if ( (sta_p = blmGetStation(channel)) ) { sta_p->timedout = BLM_TIMEDOUT_ALL; /* cause associated IMAX record to be scanned to pick up timeout status */ post_event(sta_p->epics_event_no); } } static int blm_cook(PadReply, int, int, int, void *); static void blmFidProcess(void *unused) { if ( ( fidProcessGeneric( 0, blmFidProcessActiveTimeslots ) & blmFidProcessActiveTimeslots ) ) { /* active timeslot; resume fcom processing task(s) */ blmFcomTaskResumeAll(); } } static void blm_fid_process_install(void) { errlogPrintf("%s line %d in %s\n", __FILE__, __LINE__, __FUNCTION__); evrTimeRegister(blmFidProcess, 0); } static int blm_sim_pos(int32_t *vals_p, float a1, float b1, float a2, float b2, float psi, void *parms) { vals_p[0] = a1; vals_p[1] = b1; vals_p[2] = a2; vals_p[3] = b2; return 0; } DrvPadUdpCommCallbacksRec blm_udpCommCallbacks = { cook: blm_cook, watchKinds: (1), watchdog: blm_watchdog, get_prefs: blm_get_prefs, fid_process_install: blm_fid_process_install, sim_pos: blm_sim_pos, }; DrvPadUdpCommCallbacks drvBlmUdpCommCallbacks = &blm_udpCommCallbacks; static void blm_udpCommRegistrar(void) { registryAdd( (void*)drvPadUdpCommRegistryId, "drvBlmUdpCommCallbacks", &blm_udpCommCallbacks ); } epicsExportRegistrar(blm_udpCommRegistrar);