Beispiel #1
0
        public ER Connect(T_IPV4EP p_myaddr, T_IPV4EP p_dstaddr, TMO tmout)
        {
            err_t   err;
            ip_addr ma = new ip_addr(0);
            ip_addr sa = new ip_addr(0);

            ma.addr = lwip.lwip_htonl(p_myaddr.ipaddr);
            err     = m_lwIP.tcp.tcp_bind(m_Pcb, ma, lwip.lwip_htons(p_myaddr.portno));
            if (err != err_t.ERR_OK)
            {
                return(ER.E_OBJ);
            }

            sa.addr = lwip.lwip_htonl(p_dstaddr.ipaddr);
            err     = m_lwIP.tcp.tcp_connect(m_Pcb, sa, lwip.lwip_htons(p_dstaddr.portno), Connected);
            if (err != err_t.ERR_OK)
            {
                return(ER.E_OBJ);
            }

            Task task = m_Nucleus.GetTask(ID.TSK_SELF);

            if ((tmout != 0) && (task == null))
            {
                return(ER.E_CTX);
            }

            if (task == null)
            {
                return(ER.E_TMOUT);
            }

            if (tmout == 0)
            {
                return(ER.E_TMOUT);
            }

            SetState(true, new TMO(tcp.TCP_TMR_INTERVAL), task, Intarval);

            ER ret = task.Wait(m_SendTaskQueue, TSKWAIT.TTW_TCP, m_CepID, tmout);

            switch (ret)
            {
            case ER.E_OK:
                if ((m_TPcb == null) || (m_TPcb.state != tcp_state.ESTABLISHED))
                {
                    return(ER.E_RLWAI);
                }
                break;

            case ER.E_TMOUT:
                return(ER.E_TMOUT);

            default:
                return(ER.E_RLWAI);
            }

            return(ER.E_OK);
        }
Beispiel #2
0
        void Idle()
        {
            ID tskid;

            tskid = DoIdle();

            if (tskid != ID.TSK_NULL)
            {
                Task Task;

                Task = Nucleus.GetTask(tskid);
                if (Task != null)
                {
                    Task.Wakeup();

                    m_Nucleus.Scheduling();
                }
            }
            else
            {
                UnlockCPU();

                if (!m_IntEvent.WaitOne())
                {
                    Terminate();
                }

                LockCPU();
            }
        }
Beispiel #3
0
        public ER ReceiveMessage(out T_MSG ppk_msg, TMO tmout)
        {
            ER ret;

            ppk_msg = null;

            Task task = m_Nucleus.GetTask(ID.TSK_SELF);

            if ((tmout != 0) && (task == null))
            {
                return(ER.E_CTX);
            }

            //if (ppk_msg == null)
            //	return ER.E_PAR;

            if ((m_TskQueue.First == null) && (m_MsgQueue.First != null))
            {
                ppk_msg = m_MsgQueue.First.Value;
                m_MsgQueue.RemoveFirst();
            }
            else
            {
                if (task == null)
                {
                    return(ER.E_TMOUT);
                }

                if (tmout == 0)
                {
                    return(ER.E_TMOUT);
                }

                ret = task.Wait(m_TskQueue, TSKWAIT.TTW_MBX, m_MbxID, tmout);

                switch (ret)
                {
                case ER.E_OK:
                    if (m_MsgQueue.First == null)
                    {
                        return(ER.E_RLWAI);
                    }
                    ppk_msg = m_MsgQueue.First.Value;
                    m_MsgQueue.RemoveFirst();
                    break;

                case ER.E_TMOUT:
                    return(ER.E_TMOUT);

                default:
                    return(ER.E_RLWAI);
                }
            }

            //SetDebugInfo(ppk_msg, lpszFileName, nLine);

            return(ER.E_OK);
        }
Beispiel #4
0
        public ER GetMemoryBlock(out pointer p_blk, int blksz, TMO tmout)
        {
            ER ret;

            p_blk = null;

            //if (p_blk == null)
            //	return ER.E_PAR;

            for (; ;)
            {
                TMemNode Node = Allocate(blksz);

                if (Node != null)
                {
                    p_blk = Node.GetData();
                    break;
                }

                if (tmout == 0)
                {
                    return(ER.E_TMOUT);
                }

                Task task = m_Nucleus.GetTask(ID.TSK_SELF);

                if (task == null)
                {
                    return(ER.E_CTX);
                }

                task.MemoryBlockSize = blksz;
                ret = task.Wait(m_TskQueue, TSKWAIT.TTW_MPL, m_MplID, tmout);

                switch (ret)
                {
                case ER.E_OK:
                    continue;

                case ER.E_TMOUT:
                    return(ER.E_TMOUT);

                default:
                    return(ER.E_RLWAI);
                }
            }
#if DEBUG
            string lpszFileName = "";
            int    nLine        = 0;
            _CrtMemBlockHeader.AddBlock(m_pFirstBlock, m_pLastBlock, p_blk, blksz, lpszFileName, nLine);
            m_nBlockCount++;
#endif
            return(ER.E_OK);
        }
Beispiel #5
0
        public ER GetMemoryBlock(out pointer p_blk, int blksz, TMO tmout)
        {
            ER ret;

            p_blk = null;

            //if (p_blk == null)
            //	return ER.E_PAR;

            for (; ;)
            {
                LinkedListNode <pointer> Node = m_MpfQueue.First;

                if (Node != null)
                {
                    p_blk = Node.Value;
                    m_MpfQueue.RemoveFirst();
                    break;
                }

                if (tmout == 0)
                {
                    return(ER.E_TMOUT);
                }

                Task task = m_Nucleus.GetTask(ID.TSK_SELF);

                if (task == null)
                {
                    return(ER.E_CTX);
                }

                ret = task.Wait(m_TskQueue, TSKWAIT.TTW_MPF, m_MpfID, tmout);

                switch (ret)
                {
                case ER.E_OK:
                    continue;

                case ER.E_TMOUT:
                    return(ER.E_TMOUT);

                default:
                    return(ER.E_RLWAI);
                }
            }

            return(ER.E_OK);
        }
Beispiel #6
0
        public bool OnTime(long Interval)
        {
            ID   tskID;
            bool retry = false;

            if ((m_dcyc.cycact & CYCACT.TCY_INI) != 0)
            {
                m_Count        = m_dcyc.cyctim;
                m_dcyc.cycact &= ~CYCACT.TCY_INI;
            }
            if ((m_dcyc.cycact & CYCACT.TCY_ON) != 0)
            {
                m_Count -= Interval;
                if (m_Count <= 0)
                {
                    m_Count += m_dcyc.cyctim;
                    if (m_Count <= 0)
                    {
                        retry = true;
                    }
                    try {
                        tskID = m_dcyc.cychdr();
                    }
                    catch (Exception) {
                        tskID = ID.TSK_NULL;
                    }
                    if (tskID != ID.TSK_NULL)
                    {
                        Task Task;

                        Task = m_Nucleus.GetTask(tskID);
                        if (Task != null)
                        {
                            Task.Wakeup();
                        }
                    }
                }
            }

            return(retry);
        }
Beispiel #7
0
        public ER Wait(TMO tmout)
        {
            ER ret;

            Task task = m_Nucleus.GetTask(ID.TSK_SELF);

            if ((tmout != 0) && (task == null))
            {
                return(ER.E_CTX);
            }

            m_Count--;

            if (m_Count < 0)
            {
                if (tmout == 0)
                {
                    m_Count++;
                    return(ER.E_TMOUT);
                }

                ret = task.Wait(m_TskQueue, TSKWAIT.TTW_SEM, m_SemID, tmout);

                switch (ret)
                {
                case ER.E_OK:
                    return(ER.E_OK);

                case ER.E_TMOUT:
                    m_Count++;
                    return(ER.E_TMOUT);

                default:
                    return(ER.E_RLWAI);
                }
            }

            return(ER.E_OK);
        }
Beispiel #8
0
        bool IKernel.Dispatch()
        {
            bool        Result;
            Task        Task;
            ICPUContext CPUContext;

            Task = Nucleus.GetTask(ID.TSK_SELF);

            if ((Task != null) && (Task.rtsk.tskstat != TSKSTAT.TTS_DMT))
            {
                CPUContext = Task.GetCPUContext();

                Task.Ready();

                Result = CPUContext.Dispatch();
            }
            else
            {
                m_IntEvent.Set();
                Result = !IsTerminated();
            }

            return(Result);
        }
Beispiel #9
0
        public ER ReceiveData(T_IPV6EP p_dstaddr, pointer p_dat, int len, TMO tmout)
        {
            ER ret;

            Task task = m_Nucleus.GetTask(ID.TSK_SELF);

            if ((tmout != 0) && (task == null))
            {
                return(ER.E_CTX);
            }

            if ((p_dat == null) || (len < 0))
            {
                return(ER.E_PAR);
            }

            if ((m_TskQueue.First == null) && (m_DatQueue.First != null))
            {
                pointer data;
                lock (m_DatQueue) {
                    data = m_DatQueue.First.Value;
                    m_DatQueue.RemoveFirst();
                }

                int dlen = (ushort)data;
                pointer.memcpy(p_dstaddr, data + sizeof(ushort), T_IPV6EP.length);
                pointer.memcpy(p_dat, data + sizeof(ushort) + T_IPV6EP.length, dlen);

                return((ER)dlen);
            }
            else
            {
                if (task == null)
                {
                    return(ER.E_TMOUT);
                }

                if (tmout == 0)
                {
                    return(ER.E_TMOUT);
                }

                ret = task.Wait(m_TskQueue, TSKWAIT.TTW_UDP, m_CepID, tmout);

                switch (ret)
                {
                case ER.E_OK:
                    if (m_DatQueue.First == null)
                    {
                        return(ER.E_RLWAI);
                    }
                    lock (m_DatQueue) {
                        p_dat = new pointer(m_DatQueue.First.Value, 0);
                        m_DatQueue.RemoveFirst();
                    }
                    break;

                case ER.E_TMOUT:
                    return(ER.E_TMOUT);

                default:
                    return(ER.E_RLWAI);
                }
            }

            //SetDebugInfo(p_dat, lpszFileName, nLine);

            return(ER.E_OK);
        }
Beispiel #10
0
        public ER WaitEventFlag(ref FLGPTN p_flgptn, TMO tmout, FLGPTN waiptn, MODE wfmode)
        {
            ER ret;

            Task task = m_Nucleus.GetTask(ID.TSK_SELF);

            if ((tmout != 0) && (task == null))
            {
                return(ER.E_CTX);
            }

            //if (p_flgptn == null)
            //	return ER.E_PAR;

            if ((wfmode & ~(MODE.TWF_ANDW | MODE.TWF_ORW | MODE.TWF_CLR)) != 0)
            {
                return(ER.E_PAR);
            }

            if ((m_TskQueue.First == null) && CheckPattern(waiptn, wfmode))
            {
                p_flgptn = m_FlagPattern;
                if ((wfmode & MODE.TWF_CLR) != 0)
                {
                    m_FlagPattern = 0;
                }
            }
            else
            {
                if (task == null)
                {
                    return(ER.E_TMOUT);
                }

                if (tmout == 0)
                {
                    return(ER.E_TMOUT);
                }

                task.WaitPattern = waiptn;
                task.WaitMode    = wfmode;
                ret = task.Wait(m_TskQueue, TSKWAIT.TTW_FLG, m_FlgID, tmout);

                switch (ret)
                {
                case ER.E_OK:
                    p_flgptn = m_FlagPattern;
                    if (!CheckPattern(waiptn, wfmode))
                    {
                        return(ER.E_RLWAI);
                    }
                    if ((wfmode & MODE.TWF_CLR) != 0)
                    {
                        m_FlagPattern = 0;
                    }
                    break;

                case ER.E_TMOUT:
                    return(ER.E_TMOUT);

                default:
                    return(ER.E_RLWAI);
                }
            }

            return(ER.E_OK);
        }
Beispiel #11
0
        void MainLoop()
        {
            int  intNo;
            ID   tskid;
            Task task;
            bool noIntr;

            Start();

            m_Nucleus.Start();

            do
            {
                ProcInterrupt();

                do
                {
                    noIntr = true;
                    for (intNo = 0; intNo < m_InProcIntr.Length; intNo++)
                    {
                        if (IsTerminated())
                        {
                            return;
                        }

                        if (InterruptEnabled(intNo))
                        {
                            noIntr = false;

                            ClearInterrupt(intNo);

                            if (intNo == Nucleus.SysTmrIntNo)
                            {
                                m_Nucleus.OnSysTime();
                            }

                            tskid = CallIntHandler(intNo);

                            if (tskid != ID.TSK_NULL)
                            {
                                Task wTask;

                                wTask = Nucleus.GetTask(tskid);
                                if (wTask != null)
                                {
                                    wTask.Wakeup();
                                }
                            }
                        }
                    }
                }while (!noIntr);

                m_Nucleus.Scheduling();

                task = m_Nucleus.GetScheduledTask();
                if (task == null)
                {
                    Idle();
                }
                else
                {
                    m_Current = (CPUContext)task.GetCPUContext();

                    task.Run();

                    m_Current.PopContext();

                    UnlockCPU();

                    m_TaskMode = true;

                    if (!m_IntEvent.WaitOne())
                    {
                        Terminate();
                    }

                    m_TaskMode = false;

                    LockCPU();

                    CPUContext CPUContext = (CPUContext)m_Current;
                    m_Current = null;

                    if (CPUContext.IsReleased())
                    {
                        CPUContext.Wait();
                        CPUContext.ClearTask();
                    }
                    else
                    {
                        CPUContext.PushContext();
                    }
                }

                task = m_Nucleus.GetCurrentTask();
                if (task != null)
                {
                    task.Ready();
                }
            } while (!IsTerminated());
        }
Beispiel #12
0
        internal ER Accept(tcp_pcb_listen pcb, TcpCep cep, T_IPV4EP p_dstaddr, TMO tmout)
        {
            ER ret;

            m_Pcb    = pcb;
            m_TcpCep = cep;

            tcp.tcp_accept(pcb, Accepting);

            Task task = m_Nucleus.GetTask(ID.TSK_SELF);

            if ((tmout != 0) && (task == null))
            {
                return(ER.E_CTX);
            }

            //if (p_dstaddr == null)
            //	return ER.E_PAR;

            if ((m_TskQueue.First == null) && (m_AcceptQueue.First != null))
            {
                m_NewPcb = m_AcceptQueue.First.Value;
                m_AcceptQueue.RemoveFirst();
                p_dstaddr.ipaddr = lwip.lwip_ntohl(m_NewPcb.remote_ip.addr);
                p_dstaddr.portno = lwip.lwip_ntohs(m_NewPcb.remote_port);
            }
            else
            {
                if (task == null)
                {
                    return(ER.E_TMOUT);
                }

                if (tmout == 0)
                {
                    return(ER.E_TMOUT);
                }

                ret = task.Wait(m_TskQueue, TSKWAIT.TTW_ACP, m_RepID, tmout);

                switch (ret)
                {
                case ER.E_OK:
                    if (m_AcceptQueue.First == null)
                    {
                        return(ER.E_RLWAI);
                    }
                    m_NewPcb = m_AcceptQueue.First.Value;
                    m_AcceptQueue.RemoveFirst();
                    p_dstaddr.ipaddr = lwip.lwip_ntohl(m_NewPcb.remote_ip.addr);
                    p_dstaddr.portno = lwip.lwip_ntohs(m_NewPcb.remote_port);
                    break;

                case ER.E_TMOUT:
                    return(ER.E_TMOUT);

                default:
                    return(ER.E_RLWAI);
                }
            }

            return(ER.E_OK);
        }