Annotation of nono/vm/ethernet.h, revision 1.1.1.3

1.1       root        1: //
                      2: // nono
1.1.1.3 ! root        3: // Copyright (C) 2020 nono project
        !             4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
                      7: #pragma once
                      8: 
1.1.1.3 ! root        9: #include "device.h"
        !            10: #include "mystring.h"
        !            11: #include "qvector.h"
1.1       root       12: #include <array>
                     13: #include <condition_variable>
                     14: #include <mutex>
                     15: #include <vector>
                     16: 
                     17: //          IODevice
                     18: //              |
                     19: //              v
                     20: //        EthernetDevice (イーサネットとしての共通部分)
                     21: //              |
                     22: //     +--------+-------+
                     23: //     v                v
                     24: // LanceDevice     RTL8019Device
                     25: // (LUNA, AM7990)  (X68000, RTL8019)
                     26: //
                     27: // これ自体は IODevice である必要はないが、LanceDevice などの親になるため
                     28: // IODevice にしてある。(多重継承はしない) 
                     29: 
                     30: class NetDriver;
                     31: 
                     32: // MAC アドレス格納用のクラス
                     33: class macaddr_t : public std::array<uint8, 6>
                     34: {
1.1.1.3 ! root       35:        using inherited = std::array<uint8, 6>;
1.1       root       36:  public:
                     37:        // この MAC アドレスが 00:00:00:00:00:00 なら true
                     38:        bool empty() const {
                     39:                for (const auto& v : *this) {
                     40:                        if (v != 0) {
                     41:                                return false;
                     42:                        }
                     43:                }
                     44:                return true;
                     45:        }
                     46: 
1.1.1.3 ! root       47:        // 区切り文字なしの文字列形式を返す。主に ROM 埋め込み用で英字は大文字。
        !            48:        // 01:23:AB なら "0123AB" のような感じ。
1.1       root       49:        std::string to_string() const {
                     50:                std::string buf;
                     51:                for (const auto& v : *this) {
1.1.1.3 ! root       52:                        buf += string_format("%02X", v);
        !            53:                }
        !            54:                return buf;
        !            55:        }
        !            56: 
        !            57:        // sep を区切り文字とする文字列形式を返す。主に表示用で英字は小文字。
        !            58:        // 01:23:AB で sep = ":" なら "01:23:ab" のような感じ。
        !            59:        std::string to_string(char sep) const {
        !            60:                std::string buf;
        !            61:                for (const auto& v : *this) {
        !            62:                        buf += string_format("%02x%c", v, sep);
1.1       root       63:                }
                     64:                buf.pop_back();
                     65:                return buf;
                     66:        }
                     67: };
                     68: 
                     69: class EthernetDevice : public IODevice
                     70: {
1.1.1.3 ! root       71:        using inherited = IODevice;
1.1       root       72:  protected:
                     73:        EthernetDevice();
                     74:  public:
1.1.1.3 ! root       75:        ~EthernetDevice() override;
1.1       root       76: 
1.1.1.3 ! root       77:        bool Create() override;
        !            78:        bool Init() override;
1.1       root       79: 
                     80:        // パケットを受信した。
                     81:        // ホストネットワークドライバから呼ばれる。
                     82:        // ホストネットワークスレッドで mtx 取得状態で呼ばれるので、
                     83:        // バッファをコピーしてこちらのスレッドに処理を引き渡すところまでを行う。
                     84:        // buf はプリアンブルを含まず、イーサネットフレームヘッダ(14バイト)、
                     85:        // データ(46-1500バイト)、FCS(CRC) (4バイト) からなる。
                     86:        virtual bool RecvPacket(qvector<uint8>& buf) = 0;
                     87: 
                     88:        // パケット受信許可。
                     89:        // true の場合のみホストドライバは RecvPacket() をコールしてよい。
                     90:        bool RecvEnabled = false;
                     91: 
1.1.1.3 ! root       92:        // MAC アドレスを取得
        !            93:        macaddr_t GetMacAddr() const {
        !            94:                return macaddr;
        !            95:        }
        !            96: 
1.1       root       97:        // このデバイスと継承デバイスのメンバ変数を読み書きする場合は
                     98:        // 必ずこのロックを取得する。?
                     99:        // cv もこのロックを使う。
                    100:        std::mutex mtx {};
                    101:        // ワーカスレッドへ指示を出すための条件変数
                    102:        std::condition_variable cv {};
                    103:        // ワーカスレッドへのリクエストフラグ
                    104:        uint32 request = 0;
                    105: 
                    106:  protected:
                    107:        // MAC アドレスを生成する
                    108:        static void GenerateMacAddr(macaddr_t *addr);
                    109: 
                    110:        // MAC アドレス文字列をバイナリに変換する
                    111:        static bool ParseMacAddr(macaddr_t& dst, const std::string& src);
                    112: 
                    113:        // パケットを送信する
                    114:        bool SendPacket(const std::vector<uint8>& buf);
1.1.1.3 ! root      115: 
        !           116:        // MAC アドレス
        !           117:        macaddr_t macaddr;
1.1       root      118: };
                    119: 
1.1.1.2   root      120: extern std::unique_ptr<EthernetDevice> gEthernet;

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.