/*
 *========================================================================
 *
 *   시스템명 : 요금징수 시스템
 *   파 일 명 : recvdata_filecreation.c
 *   작 성 자 : 김남훈
 *   작 성 일 : 2012.09.03
 *   수 정 일 :
 *   설    명 : 수신한 데이타를 파일로 생성
 *
 *========================================================================
*/

/*==========================================================================
 * Head  정의
 *=========================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <unistd.h>
#include "hi_ctrl.h"
#include "snd_rcv_knd.h"
#include "hi_packet_format.h"
#include "dir_path.h"
#include "file_no.h"
#include "passraw.h"
#include "cls_lane.h"
#include "rpr_spec.h"
//#include "ofc_hnd.h"
//#include "hr_trf.h"
//#include "bkout_cnt.h"
//#include "ecrd_pay.h"
//#include "ecrd_chrg.h"
#include "lane_rmot_stat.h"
#include "mtnc.h"
//#include "crd_data.h"
#include "file_lb.h"
#include "func_list.h"
#include "lb_func_list.h"
#include "path_def.h"
#include "sam_open_key.h"
/*===========================================================================
 * 전역 변수 정의
 *==========================================================================*/

static    struct    chg_struc    chg_struc;        /* 변환 요청 */

struct    f_init {
    int    fno;
    char    fname[16];
} f_init[] = {
    { FNO_RPRINFOSPEC,  "RPRT"  },        /* 보수정보 명세           */
    { FNO_CLSPASSSPEC,  "CLST"  },        /* 야간계수 통행 명세      */ 
    { FNO_WRK_START  ,  "WRKS"  },        /* 근무개시보고            */
    { FNO_CARD_HANDLE,  "CRDT"  },        /* 카드처리데이터          */
    { FNO_HANDLE     ,  "HNDL"  },        /* 처리명세                */
    { FNO_WRKEND     ,  "WRKE"  },        /* 근무종료                */
    { FNO_WRKSTINFO ,   "WSTA"  },        /* 근무상태                */
    { FNO_LANESTATINFO, "LSTA"  },        /* 차로상태                */
    { -1,      "",    }
};

/*
 *========================================================================
 *
 *   함 수 명 : fsave_rprspec()
 *   작 성 자 : 강준석
 *   작 성 일 : 2018.08.10
 *   수 정 일 :
 *   설    명 : 수신한 보수 DATA를 보수정보 명세  FILE로 저장
 *
 *========================================================================
*/

fsave_rprspec(flag)
int    flag;
{
    struct    hi_rpr_data    *hrd;
    struct    rpr_info_spec     rpr;
    int    isfd,     isrc;
	char   tmp[10]; memset(tmp, 0x00, sizeof(tmp));

    hrd = (struct hi_rpr_data *)hi_comm_ctrl.rcv_ctrl.buf;

    if (lb_data2atoi(hi_comm_ctrl.lane_stat_info.wrk_year,4) == 0 ||
        lb_data2atoi(hi_comm_ctrl.lane_stat_info.wrk_mon ,2) == 0 ||
		lb_data2atoi(hi_comm_ctrl.lane_stat_info.wrk_day ,2) == 0)
        return (0);        /* 비근무중 발생 보수*/

    memset(&rpr, 0, sizeof(struct rpr_info_spec));
    sprintf(rpr.c_plz_id, "%.2s", hi_comm_ctrl.bas_tbl.plz_id);    /* 영업소번호 */
	sprintf(tmp, "%.8s%c", hrd->seq_no, '\0');
    sprintf((char *)rpr.seq_no, "%010d", strtoul(tmp, NULL, 16));; /* 전송연번     */
    sprintf((char *)rpr.c_lane_no, "%02d", hi_comm_ctrl.lane_no);  /* 차로번호     */
	sprintf((char *)rpr.c_work_no, "%.4s", hrd->wrk_no);           /* 근무번호     */
    chg_date_tm(&rpr.c_occrn_date_tm, &hrd->year);                 /* 발생년월일시분초 */
	memcpy(rpr.c_terminal, hrd->terminal_no, 10);                  /* 단말번호     */
	rpr.c_wrk_stat[0] = hrd->wrk_stat;                             /* 근무상태     */
    sprintf((char *)rpr.c_mchi_kind, "%.2s", hrd->mchi_kind);      /* 기종     */
	sprintf((char *)rpr.c_err_cd, "%.2s", hrd->err_code);          /* 장애코드     */
	sprintf((char *)rpr.c_occ_yn, "%.2s", hrd->occ_rest);          /* 발생복구구분 */
	rpr.c_anpr_cam[0] = hrd->anpr_cam;
	sprintf((char *)rpr.c_anpr_temp, "%.4s", hrd->anpr_tempa);     /* ANPR RTU 온도 */
	rpr.c_anpr_door[0] = hrd->anpr_door;
	rpr.c_anpr_sd[0] = hrd->anpr_sd;

    isrc = lb_fwritex(&hi_comm_ctrl.write_info, FNO_RPRINFOSPEC, &rpr, 0);

    if (isrc == -ENOENT)
    {
        isfd = lb_fbuild(&hi_comm_ctrl.write_info, FNO_RPRINFOSPEC, FINOUT +FC_AUTOLOCK);
        if (isfd >= 0)
        {
            isrc = lb_fwrite(isfd, FNO_RPRINFOSPEC, &rpr, 0);
            lb_close(isfd);
        }
    }

    return (0);
}

/*
 *========================================================================
 *
 *   함 수 명 : fsave_trmn_rcv()
 *   작 성 자 : 김남훈
 *   작 성 일 : 2012.09.03
 *   수 정 일 :
 *   설    명 : 단말수신 FILE로 저장
 *
 *========================================================================
*/

fsave_trmn_rcv()
{
    struct    trmn_rcv    trmn_rcv;
    struct    write_info    wf;
    int    isfd,    isrc;
    int    dlen,    didx;
    int    ntoi_wrk_no;
    char    emsg[128];

    memset(&wf, 0x0, sizeof(wf));
    memcpy(wf.plz_id, hi_comm_ctrl.write_info.plz_id, 2);

    switch (hi_comm_ctrl.rcv_ctrl.cmd)
    {
    case NS_WRK_START:/* 근무개시보고     */
    case NS_WRK_END:/* 근무종료     */
        sprintf((char *)wf.year, "%.4s", hi_comm_ctrl.write_info.year);
        sprintf((char *)wf.mon, "%.2s", hi_comm_ctrl.write_info.mon);
        sprintf((char *)wf.day, "%.2s", hi_comm_ctrl.write_info.day);
        wf.lane_div = '1';
        break;
    default:
        sprintf((char *)wf.year, "%.4s", hi_comm_ctrl.write_info.year);
        sprintf((char *)wf.mon, "%.2s", hi_comm_ctrl.write_info.mon);
        sprintf((char *)wf.day, "%.2s", hi_comm_ctrl.write_info.day);
        break;
    }

    if (lb_data2atoi(hi_comm_ctrl.write_info.wrk_no, 4) != 0)
        memcpy(wf.wrk_no, hi_comm_ctrl.write_info.wrk_no, 4);
    else
        memcpy(wf.wrk_no, hi_comm_ctrl.lane_stat_info.wrk_no, 4);

    if (lb_data2atoi(wf.year, 4) == 0 || lb_data2atoi(wf.mon,2) == 0 ||
        lb_data2atoi(wf.day,2) == 0 || lb_data2atoi(wf.wrk_no, 4) == 0)
    {
        ntoi_wrk_no = lb_data2atoi(wf.wrk_no, 4);
        sprintf(emsg, "종별 %02X TRMN_RCV 저장 일자 및 근무번호 오류.",hi_comm_ctrl.rcv_ctrl.cmd);
        lane_log("종별 %02X TRMN_RCV 저장 일자 및 근무번호 오류.\n", hi_comm_ctrl.rcv_ctrl.cmd);
        lane_log("종별 %02X TRMN_RCV %d-%d-%d %d\n", hi_comm_ctrl.rcv_ctrl.cmd,wf.year, wf.mon,wf.day ,ntoi_wrk_no);
        err_log(emsg, 0);

        return (0);
    }

    isfd = lb_fopen(&wf, FNO_TERMRECV, FINOUT +FC_MANULOCK);
    if (isfd == -ENOENT)
        isfd = lb_fbuild(&wf, FNO_TERMRECV, FINOUT +FC_MANULOCK);
    if (isfd < 0)
        return (0);

    for (dlen = hi_comm_ctrl.rcv_ctrl.len, didx = 0; dlen > 0; dlen -= TRMN_RCV_RECSIZE)
    {
        memset(&trmn_rcv, 0, sizeof(struct trmn_rcv));
        trmn_rcv.leng = dlen > TRMN_RCV_RECSIZE ? TRMN_RCV_RECSIZE : dlen;
        memcpy(trmn_rcv.data, &hi_comm_ctrl.rcv_ctrl.buf[didx], trmn_rcv.leng);
        didx += trmn_rcv.leng;
        if (didx >= hi_comm_ctrl.rcv_ctrl.len)
            trmn_rcv.flag = F_END;
        else
            trmn_rcv.flag = F_CON;
        isrc = lb_writel(isfd, &trmn_rcv, sizeof(struct trmn_rcv));
    }
    lb_close(isfd);
    return (0);
}

/*
 *========================================================================
 *
 *   함 수 명 : reding_termrecv_wrkno()
 *   작 성 자 : 김남훈
 *   작 성 일 : 2012.09.03
 *   수 정 일 :
 *   설    명 : 단말수신파일로 부터 최종 근무번호를 READ.
 *
 *========================================================================
*/

reding_termrecv_wrkno(wf)
struct    write_info    *wf;
{
    char    d_path[128], c_path[128];
    struct    stat     statb;
    int    isfd, isrc;
    int    endf, retc, mode;
    int    ii;


    sprintf(d_path, "%s/%04d%02d%02d", LOG_DIR,
            wf->year, wf->mon, wf->day);

    /* 단말수신-F의 최종 근무번호 */
    for (ii = MAX_WRKCNT; ii > 0; ii--)
    {
        sprintf(c_path, "%s/TERMRECV%.2s%02d%02d", d_path, hi_comm_ctrl.bas_tbl.plz_id, hi_comm_ctrl.lane_no, ii);
        retc = stat(c_path, &statb);
        if (retc == 0)
            return (ii);
    }
    return (0);
}

#if 0
/*
 *========================================================================
 *
 *   함 수 명 : srch_rmotlane_stat()
 *   작 성 자 : 김남훈
 *   작 성 일 : 2012.09.03
 *   수 정 일 :
 *   설    명 : 원격차로 상태 조회
 *
 *========================================================================
*/

srch_rmotlane_stat()
{
    struct    write_info    wt;
    struct    hi_rmot_lane_st_reply    *reply;
    struct    mchi_wrk_stat    mwst;
    struct    tm    *tm;
    time_t    t;

    char    srccsfw[2 + 1], descsfw[4 + 1];
    char    srctcfw[2 + 1], destcfw[4 + 1];
    char    srcovfw[2 + 1], desovfw[4 + 1];
    char    srcecfw[2 + 1], desecfw[4 + 1];
    char    srcanfw[2 + 1], desanfw[4 + 1];
    char    lsam_chrg_info[4 + 1], orgeclc[8 + 1];
    int    isfd,    isrc, deseclc;
    int    ii,res, ret=0, jj=0;

    reply = (struct hi_rmot_lane_st_reply *)hi_comm_ctrl.rcv_ctrl.buf;

    memset(&mwst, 0, sizeof(struct mchi_wrk_stat));
    memset(srccsfw,0,sizeof(srccsfw));    /* 차로 제어기    */
    memset(descsfw,0,sizeof(descsfw));
    memset(srctcfw,0,sizeof(srctcfw));    /* 통행권 확인기*/
    memset(destcfw,0,sizeof(destcfw));
    memset(srcovfw,0,sizeof(srcovfw));    /* OVD         */
    memset(desovfw,0,sizeof(desovfw));
    memset(srcovfw,0,sizeof(srcovfw));    /* 외부 단말기     */
    memset(desanfw,0,sizeof(desanfw));
    memset(srcecfw,0,sizeof(srcecfw));    /* 전자카드 단말기 */
    memset(desecfw,0,sizeof(desecfw));
    memset(srcanfw,0,sizeof(srcanfw));
    memset(desovfw,0,sizeof(desovfw));

    time((time_t *)&t);
    tm = localtime((time_t *)&t);
    /* 원격차로 상태 정보 데이터 발생일시분초         */
    hi_comm_ctrl.write_info.year = tm->tm_year + 1900;
    hi_comm_ctrl.write_info.mon = tm->tm_mon + 1;
    hi_comm_ctrl.write_info.day = tm->tm_mday;

    mwst.wrk_year = hi_comm_ctrl.write_info.year;
    mwst.wrk_mon = hi_comm_ctrl.write_info.mon;
    mwst.wrk_day = hi_comm_ctrl.write_info.day;
    mwst.wrk_hr = tm->tm_hour;
    mwst.wrk_min = tm->tm_min;
    mwst.wrk_sec = tm->tm_sec;

    mwst.lane_no = hi_comm_ctrl.lane_no;
    memcpy(srccsfw,reply->ctrler_stat,2);
    lb_hex2dec(srccsfw,&descsfw,2);

    memcpy(mwst.lane_fw_ver,descsfw,4);            /* 차로 F/W 버전 */

    mwst.wrk_stat = chkbit01(reply->ctrler_stat[2]);        /* 근무 상태    */
    mwst.wrk_typ = chkbit02(reply->ctrler_stat[2]);        /* 근무 형태    */
    mwst.sig_light_stat = chkbit04(reply->ctrler_stat[2]);        /* 신호등    */
    mwst.Keyboard_stat = chkbit08(reply->ctrler_stat[2]);        /* 키보드통신    */

    memcpy(srctcfw,reply->pass_ticket_confirm,2);
    lb_hex2dec(srctcfw,&destcfw,2);
    memcpy(mwst.passtk_confrm_mchi_fw_ver,destcfw,4);            /* 통행권확인기F/W버전*/

    mwst.sensor_err       = chkbit01(reply->pass_ticket_confirm[2]);        /* 센서 에러        */
    mwst.ent_hang         = chkbit01(reply->pass_ticket_confirm[3]);        /* 입구 거림        */
    mwst.hopp_hang        = chkbit02(reply->pass_ticket_confirm[3]);        /* 호퍼걸림         */
    mwst.hopp_wout_hang   = chkbit04(reply->pass_ticket_confirm[3]);        /* 호퍼출구걸림     */
    mwst.sensor_hang1     = chkbit08(reply->pass_ticket_confirm[3]);        /* 센서1검지후걸림  */
    mwst.sensor_hang2     = chkbit10(reply->pass_ticket_confirm[3]);        /* 센서2검지후걸림  */
    mwst.sensor_hang3     = chkbit20(reply->pass_ticket_confirm[3]);        /* 센서3검지후걸림  */
    mwst.stacker_hang     = chkbit40(reply->pass_ticket_confirm[3]);        /* 스택커걸림       */
    mwst.write_hang       = chkbit80(reply->pass_ticket_confirm[3]);        /* WRITE후 걸림     */
    mwst.hopp_near_empty  = chkbit02(reply->pass_ticket_confirm[4]);        /*Hopper Near Empty */
    mwst.hopp_empty       = chkbit04(reply->pass_ticket_confirm[4]);        /*Hopper  Empty     */
    mwst.stacker_full     = chkbit08(reply->pass_ticket_confirm[4]);        /* Stacker Full     */
    mwst.Print_head_down  = chkbit10(reply->pass_ticket_confirm[4]);        /* Print head down  */
    mwst.Flapper          = chkbit20(reply->pass_ticket_confirm[4]);        /* Flapper 위치     */
    mwst.Cover_Open       = chkbit40(reply->pass_ticket_confirm[4]);        /* Cover Open       */
    mwst.wout_sensor      = chkbit80(reply->pass_ticket_confirm[4]);        /* 출구 센서 검지   */

                            /* 정액권 확인기*/
    mwst.ppaytk_hang  = chkbit01(reply->commuter_ticket_confirm[0]);        /* 정액권 걸림      */
    mwst.init_incpl   = chkbit08(reply->commuter_ticket_confirm[0]);        /* 초기화 미완료    */
    mwst.bord_commerr = chkbit10(reply->commuter_ticket_confirm[0]);        /* 홀로보드 통신이상*/
    mwst.pcm_comm_err = chkbit20(reply->commuter_ticket_confirm[0]);        /* 통신이상         */
    mwst.set_err      = chkbit40(reply->commuter_ticket_confirm[0]);        /* SET 불량         */
    mwst.snbck_open   = chkbit80(reply->commuter_ticket_confirm[0]);        /* 반송로 OPEN      */
    mwst.sensor1      = chkbit01(reply->commuter_ticket_confirm[1]);        /* 센서 1           */
    mwst.sensor2      = chkbit02(reply->commuter_ticket_confirm[1]);        /* 센서 2           */
    mwst.sensor5      = chkbit10(reply->commuter_ticket_confirm[1]);        /* 센서 5           */
    mwst.sensor6      = chkbit20(reply->commuter_ticket_confirm[1]);        /* 센서 6           */
    mwst.sensor7      = chkbit40(reply->commuter_ticket_confirm[1]);        /* 센서 7           */

                            /* 영수증 발행기*/
    mwst.not_rcept_publish = chkbit01(reply->rcept_issuing[0]);        /* 발행불가    */
    mwst.out_paper         = chkbit02(reply->rcept_issuing[0]);        /* 용지부족    */
    mwst.no_paper          = chkbit04(reply->rcept_issuing[0]);        /* 용지없음    */
    mwst.blank_detection   = chkbit08(reply->rcept_issuing[0]);        /* 백지검출    */
    mwst.head_open         = chkbit10(reply->rcept_issuing[0]);        /* Head Open    */
    mwst.rp_comm_err           = chkbit40(reply->rcept_issuing[0]);        /* 통신이상    */
    mwst.not_publish       = chkbit80(reply->rcept_issuing[0]);        /* 발행불가    */

                            /* 차종분류장치    */
    memcpy(srcovfw,reply->carkind_clfc,2);
    lb_hex2dec(srcovfw,&desovfw,2);

    memcpy(mwst.ovd_fw_ver,desovfw,4);            /* OVD F/W 버전 */

    mwst.ovd_commerr           = chkbit01(reply->carkind_clfc[2]);        /* 통신이상       */
    mwst.ovd_forecast          = chkbit02(reply->carkind_clfc[2]);        /* 차검 예보       */
    mwst.ovd_alarm             = chkbit04(reply->carkind_clfc[2]);        /* 차검 경보       */
    mwst.ovd_severance         = chkbit08(reply->carkind_clfc[2]);        /* 전원 단절       */
    mwst.ovd_balance_short     = chkbit10(reply->carkind_clfc[2]);        /* balance short   */
    mwst.ovd_balance_open      = chkbit20(reply->carkind_clfc[2]);        /* balance open    */
    mwst.ovd_resist            = chkbit40(reply->carkind_clfc[2]);        /* 저항접점이상    */
    mwst.ovd_garage_ctm        = chkbit80(reply->carkind_clfc[2]);        /* 차고감지고오염  */
    mwst.ovd_opt_sensor_ctm    = chkbit01(reply->carkind_clfc[3]);        /* 광센서 오염    */
    mwst.ovd_opt_sensor_direct = chkbit02(reply->carkind_clfc[4]);        /* 광센서 직사    */

                            /* 전자카드 단말기*/
    memcpy(srcecfw,reply->ecrd_terminal,2);
    lb_hex2dec(srcecfw,&desecfw,2);
    memcpy(mwst.ecrd_fw_ver,desecfw,4);        /* 전자카드단말기 F/W 버전 */

    mwst.ecrd_comm_err = chkbit01(reply->ecrd_terminal[2]);        /* 통신이상    */
    mwst.ecrd_psam_yn = chkbit02(reply->ecrd_terminal[2]);        /* PSAM 유무*/
    mwst.ecrd_lsam_yn = chkbit04(reply->ecrd_terminal[2]);        /* LSAM 유무*/

    lsam_chrg_info[0] = reply->ecrd_terminal[3];
    lsam_chrg_info[1] = reply->ecrd_terminal[4];
    lsam_chrg_info[2] = reply->ecrd_terminal[5];
    lsam_chrg_info[3] = reply->ecrd_terminal[6];

     deseclc = atoi(lsam_chrg_info);

    sprintf(orgeclc,"%08d",deseclc);        /* LSAM 충전정보*/
    memcpy(mwst.ecrd_lsam_chrg_info, orgeclc, 8);

    for(ii =0 ; ii < 7; ii++)
    {
        memcpy(srcanfw ,&reply->ecrd_terminal[7+jj],2);
        lb_hex2dec(srcanfw,&desanfw,2);
        memcpy(mwst.out_trmn[ii].ecrd_ant_fw,desanfw,4);        /* ANT F/W버전*/
        mwst.out_trmn[ii].ecrd_ant_comm_err = chkbit01(reply->ecrd_terminal[9+jj]);/* 통신이상*/
        mwst.out_trmn[ii].ecrd_ant_psam_yn = chkbit02(reply->ecrd_terminal[9+jj]);/* PSAM 유무*/

        jj+=3;
    }

    mwst.cr_comm_err = chkbit01(reply->card_reader);        /* 카드리더 통신이상*/
    mwst.tvs_comm_err = chkbit01(reply->fare_exmt_prevent);        /* 통행료면탈이상*/

    isrc = lb_fwritex(&hi_comm_ctrl.write_info, FNO_RMOTLANESTAT, &mwst, 0);

    if (isrc == -ENOENT)
    {
        isfd = lb_fbuild(&hi_comm_ctrl.write_info, FNO_RMOTLANESTAT, FINOUT +FC_AUTOLOCK);
        if (isfd >= 0)
        {
            isrc = lb_fwrite(isfd, FNO_RMOTLANESTAT, &mwst, 0);
            lb_close(isfd);
        }
    }

    memset(&chg_struc, 0, sizeof(struct chg_struc));

    chg_struc.file_id = FNO_RMOTLANESTAT;
    memcpy(chg_struc.plz_id, hi_comm_ctrl.bas_tbl.plz_id, 2);
    chg_struc.lane_no = hi_comm_ctrl.lane_no;
    chg_struc.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    chg_struc.seq_no = reply->seq_no;

    chg_struc.year = hi_comm_ctrl.write_info.year;
    chg_struc.mon = hi_comm_ctrl.write_info.mon;
    chg_struc.day = hi_comm_ctrl.write_info.day;
    chg_struc.hr = mwst.wrk_hr;
    chg_struc.min = mwst.wrk_min;
    chg_struc.sec = mwst.wrk_sec;

    chg_struc.file_year = hi_comm_ctrl.write_info.year;
    chg_struc.file_mon = hi_comm_ctrl.write_info.mon;
    chg_struc.file_day = hi_comm_ctrl.write_info.day;

    chgrq_f2t(&chg_struc);

    ret = lcc_epay_reply(LW_SRCH_RMOTLANE_STAT, 0, 1, &mwst,sizeof(struct mchi_wrk_stat));

    if(ret < 0)
    {
        lane_log("원격차로 상태 영업소 전송 실패 [%2d:%2d:%2d] [%d] \n",ret, mwst.wrk_hr, mwst.wrk_min, mwst.wrk_sec );
    }

    return (0);
}

/*
 *========================================================================
 *
 *   함 수 명 : fsave_clslane_spec()
 *   작 성 자 : 강준석
 *   작 성 일 : 2018.08.10
 *   수 정 일 :
 *   설    명 : 수신한 폐쇄차로통행 DATA를 폐쇄차로명세  FILE로 저장
 *
 *========================================================================
*/

fsave_clslane(flag)
int    flag;
{
    struct    hi_close_lane_pass    *hclp;
    struct    cls_lane_spec    cls;
    int    ii;
    int    isrc, isfd;

        hclp = (struct hi_close_lane_pass *)hi_comm_ctrl.rcv_ctrl.buf;

    memset(&cls, 0, sizeof(struct cls_lane_spec));

    cls.seq_no = hclp->seq_no;            /* 전송연번 */
    chg_date_tm(&cls.occ_year, &hclp->year);    /* 발생일시 */
    cls.adv_rev_div = hclp->adv_rev;            /* 전후진구분 */
    cls.sno = hclp->serial_no;            /* 일련번호 */

    cls.lane_no = hclp->lane_no;            /* 차로번호 */
    cls.axle_code = hclp->sens_carkind;            /* 차종 */
    cls.axle = hclp->axle_code;            /* 축수코드 */

    isrc = lb_fwritex(&hi_comm_ctrl.write_info, FNO_CLSPASSSPEC, &cls, 0);

    if (isrc == -ENOENT)
    {
        isfd = lb_fbuild(&hi_comm_ctrl.write_info, FNO_CLSPASSSPEC, FINOUT +FC_AUTOLOCK);
        if (isfd >= 0)
        {
            isrc = lb_fwrite(isfd, FNO_CLSPASSSPEC, &cls, 0);
            lb_close(isfd);
        }
    }

    memset(&chg_struc, 0, sizeof(struct chg_struc));
    chg_struc.file_id = FNO_CLSPASSSPEC;
    memcpy(chg_struc.plz_id, hi_comm_ctrl.bas_tbl.plz_id, 2);
    chg_struc.lane_no = cls.lane_no;
    chg_struc.seq_no = cls.seq_no;
    chg_struc.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    chg_struc.year = cls.occ_year;
    chg_struc.mon = cls.occ_mon;
    chg_struc.day = cls.occ_day;
    chg_struc.hr = cls.occ_hr;
    chg_struc.min = cls.occ_min;
    chg_struc.sec = cls.occ_sec;

    chg_struc.file_year = chg_struc.year;
    chg_struc.file_mon = chg_struc.mon;
    chg_struc.file_day = chg_struc.day;

    chgrq_f2t(&chg_struc);

    return (0);
}

/*
 *========================================================================
 *
 *   함 수 명 : chgrq_f2t()
 *   작 성 자 : 김남훈
 *   작 성 일 : 2012.09.03
 *   수 정 일 :
 *   설    명 : 차로측에서 생성된 각 FILE별 RECORD를 TABLE로 변환 처리 요청
 *   ---------------------------------------------------------------------
 *   내용                파일명        LANE_NO  CLASS  YEAR  MON  DAY  HR  MIN  SEC
 *   1. 일별관리정보     ----          X        X      O     O    O    X   X    X
 *   2. 보수DATA         RPRINFOSPEC   O        X      O     O    O    O   O    O
 *   3. 폐쇄차로통행     CLSPASSSPEC   O        X      O     O    O    O   O    O
 *   4. 사무실처리       OFCHNDLSPEC   O        X      O     O    O    O   O    O
 *   5. 시간별교통량     HOURTRAFSPEC  O        X      O     O    O    O   X    O
 *   6. 예비권작성명세                 O        X      O     O    O    O   O    O
 *   7. 5분간 교통량명세 5MINTRAFSPEC  O        X      O     O    O    O   O    X
 *   8. 근무상태정보     WRKSTINFO     O        X      X     X    X    X   X    X
 *   9. 차로상태정보     LANESTINFO    O        X      X     X    X    X   X    X
 *  10. 예비권작성정보                 O        X      O     O    O    O   O    X
 *  11. 정전시차판대수   BOCNTSPEC     O        X      O     O    O    O   O    O
 *
 *========================================================================
*/

chgrq_f2t(rehnd)
struct    chg_struc    *rehnd;
{
    int    len;

    rehnd->tbl2file_div = 0;            /* FILE->TABLE    */

    hi_comm_ctrl.tbl_mngt_iffd = lb_con_hnd_svr(PROC_OLTP);
    if (hi_comm_ctrl.tbl_mngt_iffd < 0)
    {
        if(!(rehnd->file_id==22 && (rehnd->lane_no==61 || rehnd->lane_no==62 ||
                                    rehnd->lane_no==63 || rehnd->lane_no==64 ||
                                    rehnd->lane_no==71 || rehnd->lane_no==72 ||
                                    rehnd->lane_no==73 || rehnd->lane_no==74))) 
        {
            lb_rehndlog(rehnd);
        }
    return (0);
    }

    len = write(hi_comm_ctrl.tbl_mngt_iffd, rehnd, sizeof(struct chg_struc));
    close(hi_comm_ctrl.tbl_mngt_iffd);

    if (len != sizeof(struct chg_struc))
    {
        if(!(rehnd->file_id==22 && (rehnd->lane_no==61 || rehnd->lane_no==62 ||
                                    rehnd->lane_no==63 || rehnd->lane_no==64 ||
                                    rehnd->lane_no==71 || rehnd->lane_no==72 ||
                                    rehnd->lane_no==73 || rehnd->lane_no==74))) 
        {
            lb_rehndlog(rehnd);
        }
    }

        return (0);
}

/*
 *========================================================================
 *
 *   함 수 명 : fsave_crd_dt()
 *   작 성 자 : 김남훈
 *   작 성 일 : 2012.09.03
 *   수 정 일 :
 *   설    명 : 카드 데이터
 *
 *========================================================================
*/

fsave_crd_dt()
{
    struct    hi_card_data   *hcd;
    struct  crd_dt    crd_dt;
    int    isfd,    isrc;
    int    ii,ret=0;
    char    lane_no[2 + 1];

    hcd = (struct hi_card_data *)hi_comm_ctrl.rcv_ctrl.buf;

    memset(&crd_dt, 0, sizeof(struct crd_dt));

    chg_date(&crd_dt.wrk_year, &hcd->wrk_year);        /* 근무 일     */
    memcpy(crd_dt.wrk_no,hcd->wrk_no,4);
    memcpy(crd_dt.wrkr_no,hcd->wrkr_no,4);
    crd_dt.snd_no = hcd->seq_no;
    crd_dt.sno = hcd->serial_no;
    chg_date_tm(&crd_dt.occ_year, &hcd->year);        /* 발생 일     */
    crd_dt.crd_knd = hcd->crd_kind;
    crd_dt.car_knd_k = hcd->carkind_ticket;
    crd_dt.car_knd_i = hcd->carkind_wrkr;
    crd_dt.sys_class = hcd->system_kind;
    memcpy(crd_dt.ent_ic, hcd->ent_plz_id,2);
    chg_date_tm(&crd_dt.ent_year, &hcd->ent_year);        /* 입구 일     */
    crd_dt.pass_fare = hcd->pass_fare;
    memcpy(crd_dt.crd_no, hcd->crd_no,16);
    crd_dt.crd_spcl_hnd = hcd->spcl_continue;
    crd_dt.bl_crd_spcl = hcd->spcl_bl;

    isrc = lb_fwritex(&hi_comm_ctrl.write_info, FNO_CRDSPEC, &crd_dt, 0);

    if (isrc == -ENOENT)
    {
        isfd = lb_fbuild(&hi_comm_ctrl.write_info, FNO_CRDSPEC, FINOUT +FC_AUTOLOCK);
        if (isfd >= 0)
        {
            isrc = lb_fwrite(isfd, FNO_CRDSPEC, &crd_dt, 0);
            lb_close(isfd);
        }
    }

    return (0);
}
#endif
chkbit01(src)
char    src;
{
    int    result;

    result = src & 0x01;

    if(result) return 1;
    else return 0;
}

chkbit02(src)
char    src;
{
    int    result;

    result = src & 0x02;

    if(result) return 1;
    else return 0;
}

chkbit04(src)
char    src;
{
    int    result;

    result = src & 0x04;

    if(result) return 1;
    else return 0;
}

chkbit08(src)
char    src;
{
    int    result;

    result = src & 0x08;

    if(result) return 1;
    else return 0;
}

chkbit10(src)
char    src;
{
    int    result;

    result = src & 0x10;

    if(result) return 1;
    else return 0;
}

chkbit20(src)
char    src;
{
    int    result;

    result = src & 0x20;

    if(result) return 1;
    else return 0;
}

chkbit40(src)
char    src;
{
    int    result;

    result = src & 0x40;

    if(result) return 1;
    else return 0;
}

chkbit80(src)
char    src;
{
    int    result;

    result = src & 0x80;

    if(result) return 1;
    else return 0;
}


