|
|
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.5 ! root 75: virtual ~EthernetDevice() override;
1.1 root 76:
1.1.1.3 root 77: bool Create() override;
78: bool Init() override;
1.1 root 79:
1.1.1.5 ! root 80: // ワーカスレッド
! 81: virtual void ThreadRun() = 0;
! 82:
1.1 root 83: // パケットを受信した。
84: // ホストネットワークドライバから呼ばれる。
85: // ホストネットワークスレッドで mtx 取得状態で呼ばれるので、
86: // バッファをコピーしてこちらのスレッドに処理を引き渡すところまでを行う。
87: // buf はプリアンブルを含まず、イーサネットフレームヘッダ(14バイト)、
88: // データ(46-1500バイト)、FCS(CRC) (4バイト) からなる。
89: virtual bool RecvPacket(qvector<uint8>& buf) = 0;
90:
91: // パケット受信許可。
92: // true の場合のみホストドライバは RecvPacket() をコールしてよい。
1.1.1.4 root 93: bool RecvEnabled {};
1.1 root 94:
1.1.1.3 root 95: // MAC アドレスを取得
96: macaddr_t GetMacAddr() const {
97: return macaddr;
98: }
99:
1.1 root 100: // このデバイスと継承デバイスのメンバ変数を読み書きする場合は
101: // 必ずこのロックを取得する。?
102: // cv もこのロックを使う。
103: std::mutex mtx {};
104: // ワーカスレッドへ指示を出すための条件変数
105: std::condition_variable cv {};
106: // ワーカスレッドへのリクエストフラグ
1.1.1.4 root 107: uint32 request {};
1.1 root 108:
109: protected:
110: // MAC アドレスを生成する
111: static void GenerateMacAddr(macaddr_t *addr);
112:
113: // MAC アドレス文字列をバイナリに変換する
114: static bool ParseMacAddr(macaddr_t& dst, const std::string& src);
115:
116: // パケットを送信する
117: bool SendPacket(const std::vector<uint8>& buf);
1.1.1.3 root 118:
119: // MAC アドレス
1.1.1.4 root 120: macaddr_t macaddr {};
1.1 root 121: };
122:
1.1.1.2 root 123: 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.