gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/kbpd/src/receive.cbare sourcepermlink (0.04 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: 
   15: /*
   16:  *      receive.c
   17:  *
   18:  *       KB/PD device manager
   19:  * receiving task for data from I/O driver
   20:  */
   21: 
   22: #include "kbpd.h"
   23: 
   24: /*
   25:  * automatic setting of default keyboard ID
   26:  */
   27: LOCAL void kpSetAutoKeyID( UW kid )
   28: {
   29:         if ( kpMgrInfo.kb.defKeyID == 0 ) {
   30:                 kpMgrInfo.kb.defKeyID = 1; /* automatic */
   31:                 kpMgrInfo.kb.keyID = kid;
   32:         }
   33: }
   34: 
   35: /*
   36:  * release the automatic configuraton of default keyboard ID
   37:  */
   38: LOCAL void kpClrAutoKeyID( UW kid )
   39: {
   40:         if ( kpMgrInfo.kb.defKeyID == 1
   41:           && kpMgrInfo.kb.keyID == kid ) {
   42:                 kpMgrInfo.kb.defKeyID = 0; /* unconfigured */
   43:                 kpMgrInfo.kb.keyID = KID_unknown;
   44:         }
   45: }
   46: 
   47: /*
   48:  * register / delete the flags for commands
   49:  *       When it is registered, its ID is returned. Otherwise InvalidID is returned.
   50:  */
   51: LOCAL ID registCmdFlag( FlgInput *msg )
   52: {
   53:         ID     schID, setID;
   54:         int    i;
   55: 
   56:         if ( msg->stat.reg == 0 ) {
   57:                 /* unregister */
   58:                 schID = msg->flgid;
   59:                 setID = InvalidID;
   60:                 if ( msg->stat.kb != 0 ) kpClrAutoKeyID(msg->stat.kbid);
   61:         } else {
   62:                 /* registration */
   63:                 schID = InvalidID;
   64:                 setID = msg->flgid;
   65:                 if ( msg->stat.kb != 0 ) kpSetAutoKeyID(msg->stat.kbid);
   66:         }
   67: 
   68:         for ( i = 0; i < MaxCmd; ++i ) {
   69:                 if ( kpMgrInfo.cmdFlg[i] == schID ) {
   70:                         kpMgrInfo.cmdFlg[i] = setID;
   71:                         return setID;
   72:                 }
   73:         }
   74: 
   75:         return InvalidID;
   76: }
   77: 
   78: /* ------------------------------------------------------------------------ */
   79: 
   80: /*
   81:  * notify PD data 2 event
   82:  */
   83: LOCAL ER kpNotifyPdInput2( PdInput2 *msg )
   84: {
   85: static  PdEvt2   evt = { {TDE_PDEXT} };
   86:         ER     err;
   87: 
   88:         /* ignore error data */
   89:         if ( msg->stat.err != DEV_OK ) return E_OK;
   90: 
   91:         /* if wheel is not in effect, ignore */
   92:         if ( !kpMgrInfo.pd.pdMode.attr.wheel ) return E_OK;
   93: 
   94:         evt.wheel = msg->wheel;
   95: 
   96:         /* notify PD extended event */
   97:         err = kpNotifyEvent(&evt, sizeof(evt));
   98:         if ( err < E_OK ) goto err_ret;
   99: 
  100:         return E_OK;
  101: 
  102: err_ret:
  103:         DEBUG_PRINT(("kpNotifyPdInput2 err = %d\n", err));
  104:         return err;
  105: }
  106: 
  107: /* ------------------------------------------------------------------------ */
  108: 
  109: /*
  110:  * entry to data receiving task
  111:  */
  112: EXPORT void kpDataReceiveTask( void )
  113: {
  114:         ReceiveData    *msg;
  115:         InnerEvent     evt;
  116:         BOOL           cont;
  117:         TMO            tmout = TMO_FEVR;
  118:         ID             flg;
  119:         ER             ercd;
  120: 
  121:         Lock(&kpMgrInfo.lock);
  122: 
  123:         /* notify the meta key initial state to the event manager */
  124:         kpNotifyMetaEvent();
  125: 
  126:         for ( ;; ) {   /* it never quits itself */
  127: 
  128:                 /* receiving data */
  129:                 Unlock(&kpMgrInfo.lock);
  130:                 ercd = tk_rcv_mbx(kpMgrInfo.dataMbx, (T_MSG**)&msg, tmout);
  131:                 Lock(&kpMgrInfo.lock);
  132:                 if ( ercd != E_OK && ercd != E_TMOUT ) {
  133:                         DEBUG_PRINT(("tk_rcv_mbx err = %d\n", ercd));
  134:                         continue;
  135:                 }
  136: 
  137:                 if ( ercd == E_TMOUT ) {
  138:                         /* PD simulation / repeat processing */
  139:                         kpExecPdSimRepeat(&tmout);
  140:                         continue;
  141:                 }
  142: 
  143:                 switch ( msg->head.cmd.cmd ) {
  144:                   case INP_FLG:
  145:                         /* register / delete the flags for commands */
  146:                         flg = registCmdFlag(&msg->flg);
  147:                         msg->head.cmd.read = 1;
  148: 
  149:                         if ( flg != InvalidID ) {
  150:                                 /* configure device initial state */
  151:                                 kpSendInitialDeviceCommand(flg);
  152:                         }
  153:                         break;
  154: 
  155:                   case INP_PD2:
  156:                         /* PD data 2 */
  157:                         kpNotifyPdInput2(&msg->pd2);
  158:                         msg->head.cmd.read = 1;
  159:                         break;
  160: 
  161:                   default:
  162:                         do {
  163:                                 /* enableware processing (execute the state transition) */
  164:                                 cont = kpExecStateMachine(&evt, msg);
  165: 
  166:                                 /* internal event processing */
  167:                                 kpInnerEventProcess(&evt, &tmout);
  168:                         } while ( cont );
  169:                         msg->head.cmd.read = 1;
  170:                 }
  171:         }
  172: 
  173:         tk_exd_tsk();  /* should never reach here */
  174: }
  175: 
  176: /*
  177:  * start the receiving task for data from I/O driver
  178:  */
  179: EXPORT ER kpStartDataReceiveTask( PRI pri )
  180: {
  181:         T_CTSK ctsk;
  182:         ER     ercd;
  183:         ER     err;
  184: 
  185:         /* set the initial state for the state transition */
  186:         err = kpInitializeStateMachine();
  187:         if ( err != E_OK ) {
  188:                 DEBUG_PRINT(("initialize state machine err = %d\n", err));
  189:                 return err;
  190:         }
  191: 
  192:         /* start task */
  193:         SetOBJNAME(ctsk.exinf, "kbpd");
  194:         ctsk.tskatr  = TA_HLNG|TA_RNG0;
  195:         ctsk.task    = kpDataReceiveTask;
  196:         ctsk.itskpri = pri;
  197:         ctsk.stksz   = DefaultStkSize;
  198:         ercd = tk_cre_tsk(&ctsk);
  199:         if ( ercd < E_OK ) {
  200:                 DEBUG_PRINT(("create dataReceiveTask. ercd = %d\n", ercd));
  201:                 return ercd;
  202:         }
  203:         kpMgrInfo.dataReceiveTask = (ID)ercd;
  204: 
  205:         ercd = tk_sta_tsk(kpMgrInfo.dataReceiveTask, 0);
  206:         if ( ercd != E_OK ) {
  207:                 DEBUG_PRINT(("start dataReceiveTask. ercd = %d\n", ercd));
  208:                 return ercd;
  209:         }
  210: 
  211:         return E_OK;
  212: }
  213: 
  214: /*
  215:  * stop the receiving task for data from I/O driver
  216:  */
  217: EXPORT void kpStopDataReceiveTask( void )
  218: {
  219:         ID     id;
  220: 
  221:         /* stop the receiving task for data from I/O driver */
  222:         if ( (id = kpMgrInfo.dataReceiveTask) != InvalidID ) {
  223:                 tk_ter_tsk(id);
  224:                 tk_del_tsk(id);
  225:         }
  226: 
  227:         /* epilog processing of the state transition */
  228:         kpFinishStateMachine();
  229: }