# cs.pl - CS functions.

$HLEN = 17 ;                # length of a message header.
$MH_HDR_FMT = "SSSLCCLC" ;  # header format
$TFM_CTL_FMT = "LLCC" ;     # position, fence, ucType, ucNumRLinks
$TFM_CTL_DEST_FMT = "S" ;   # destid format
$TFM_CTL_DEST_LEN = 2 ;     # destid length
$TFM_CTL_LEN_FMT = "S" ;    # length word at end of trailer
$TFM_CTL_LEN_LEN = 2 ;

sub showrc  {
    local($rc) = @_ ;

    sprintf("Status %u Reason %u",$rc&0xffff,($rc>>16)&0xffff) ;
}

sub css  {
    (@_[0]&0xffff)==0 ;
}

sub rawsend  {  # expects STDMHOUT to be CS_FILE 4
    local($msg,$hmsg,$sts,$nb) = @_ ;
    $hmsg = pack($MH_HDR_FMT,length($msg),$MH_DESTID,$MH_SRCID,
        $MH_SEQ,$MH_PRI,$MH_DLST,($MH_TIME != 0 ? $MH_TIME : time),
        $MH_TFMT).$msg ; # the whole thang
    $RC = &rCsIoWrite($hmsg,1,length($hmsg),$nb,4) ;
    &css($RC) ;
}

sub stdmhoutraw  {
    &css($rc=&rCsIoControl(4,(22<<1)|1,1,0)) ;
}

sub stdmhinraw  {
    &css($rc=&rCsIoControl(3,(22<<1)|1,1,0)) ;
}

sub set_guaranteed  {
    local($file,$tfmtype) = @_ ;
    &css($rc=&rCsIoControl($file,(14<<1)|1,$tfmtype,0)) ;
}

sub my_destid  {
    local($destid) = pack("S",0) ;  # gotta preallocate space for pvArg
    &css($rc=&rCsIoControl(4,(12<<1)|0,0,$destid)) ;
    unpack("S",$destid) ;
}

sub get_udp_source  {
    local($file) = @_ ;
    local($source) = pack(A32,0) ;  # gotta preallocate space for pvArg
    &css($rc=&rCsIoControl($file,(12<<1)|0,0,$source)) ;
    substr($source,0,index($source,"\0")) ;
}

sub get_sequence  {
    local($seq) = pack("L",0) ;  # gotta preallocate space for pvArg
    &css($rc=&rCsIoControl(3,(13<<1)|0,0,$seq)) ;
    unpack("L",$seq) ;
}

sub get_position  {
    local($pos) = pack("L",0) ;  # gotta preallocate space for pvArg
    &css($rc=&rCsIoControl(3,(29<<1)|0,0,$pos)) ;
    unpack("L",$pos) ;
}

sub set_priority  {
    local($file,$prio) = @_ ;
    &css($rc=&rCsIoControl($file,(10<<1)|1,$prio,0)) ;
}

sub set_flow_control  {
    local($file,$state) = @_ ;
    &css($rc=&rCsIoControl($file,(28<<1)|1,$state,0)) ;
}

sub set_blocking  {
    local($file,$state) = @_ ;
    &css($rc=&rCsIoControl($file,(120<<1)|1,$state,0)) ;
}

sub set_event_number  {
    local($file,$event) = @_ ;
    &css($rc=&rCsIoControl($file,(119<<1)|1,$event,0)) ;
}

sub set_immediate  {
    local($file,$state) = @_ ;
    &css($rc=&rCsIoControl($file,(122<<1)|1,$state,0)) ;
}

sub set_task_status  {
    local($file,$status) = @_ ;
    &css($rc=&rCsIoControl($file,(27<<1)|1,$status,0)) ;
}  
1 ;
