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