-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVT52_Telnet_Client.ino
More file actions
171 lines (155 loc) · 4.87 KB
/
VT52_Telnet_Client.ino
File metadata and controls
171 lines (155 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* NimBLE_Async_client Demo:
*
* Demonstrates asynchronous client operations.
*
* Created: on November 4, 2024
* Author: H2zero
*/
#include <Arduino.h>
#include <NimBLEDevice.h>
#include "src/key_codes.h"
#include <esp_task_wdt.h>
static constexpr uint32_t scanTimeMs = 5 * 1000;
static bool online = false;
static char nulls[8];
static NimBLERemoteService* pSvc = nullptr;
static NimBLERemoteCharacteristic* pChr = nullptr;
static NimBLERemoteDescriptor* pDsc = nullptr;
static NimBLEClient* pClient = nullptr;
uint8_t cplock = 1;
static uint8_t lstkey = 2;
bool connected = false, run8 = false;
class ClientCallbacks : public NimBLEClientCallbacks {
void onConnect(NimBLEClient* pClient) override {
Serial.printf("Connected to: %s\r\n", pClient->getPeerAddress().toString().c_str());
connected = true;
}
void onDisconnect(NimBLEClient* pClient, int reason) override {
Serial.printf("%s Disconnected, reason = %d - Starting scan\r\n", pClient->getPeerAddress().toString().c_str(), reason);
NimBLEDevice::deleteClient(pClient);
NimBLEDevice::getScan()->start(scanTimeMs);
connected = online = false;
}
} clientCallbacks;
class ScanCallbacks : public NimBLEScanCallbacks {
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override {
Serial.printf("Advertised Device found: %s\r\n", advertisedDevice->getName().c_str());
if (advertisedDevice->haveName() && advertisedDevice->getName() == "BT+2.4G KB") {
Serial.printf("Found Our Device\r\n");
/** Async connections can be made directly in the scan callbacks */
pClient = NimBLEDevice::getDisconnectedClient();
if (!pClient) {
pClient = NimBLEDevice::createClient(advertisedDevice->getAddress());
if (!pClient) {
Serial.printf("Failed to create client\r\n");
return;
}
}
pClient->setConnectionParams(12, 12, 0, 150);
pClient->setConnectTimeout(5 * 1000);
pClient->setClientCallbacks(&clientCallbacks, false);
if (!pClient->connect(false, true, true)) { // delete attributes, async connect, no MTU exchange
NimBLEDevice::deleteClient(pClient);
Serial.printf("Failed to connect\r\n");
return;
}
}
}
void onScanEnd(const NimBLEScanResults& results, int reason) override {
Serial.printf("Scan Ended\r\n");
NimBLEDevice::getScan()->start(scanTimeMs);
}
} scanCallbacks;
void setup() {
Serial.begin(115200);
while (!Serial)
;
setvbuf(stdout, (char*)NULL, _IONBF, 0); /* Disable stdout buffering */
Serial.printf("\r\nStarting NimBLE Async Client\r\n");
NimBLEDevice::init("Async-Client");
NimBLEDevice::setPower(3); /** +3db */
NimBLEDevice::setSecurityAuth(BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM | BLE_SM_PAIR_AUTHREQ_SC);
NimBLEScan* pScan = NimBLEDevice::getScan();
pScan->setScanCallbacks(&scanCallbacks);
pScan->setInterval(45);
pScan->setWindow(45);
pScan->setActiveScan(true);
pScan->start(scanTimeMs);
}
void Decode(uint8_t* data, size_t length) {
int ndx = 2;
char bfr[16];
int caps = 0, skey = 1;
std::vector<std::string> ref;
if (!data[ndx] || !length)
return;
while (data[ndx++])
;
ndx -= 2;
if (lstkey > ndx) {
lstkey = ndx;
return;
}
// printf("%x:",data[ndx]);
// Serial.printf("Code:%x ",data[ndx]);
if (usb_codes.find(data[ndx]) != usb_codes.end()) {
ref = usb_codes[data[ndx]];
} else
return;
if (data[ndx] == 0x39) {
cplock = cplock == 1 ? 3 : 1;
return;
}
if (cplock == 3 && data[ndx] >= 0x4 && data[ndx] <= 0x1d)
caps = 1;
if (data[ndx] > 0x39)
caps = skey = 0;
strcpy(bfr, (data[0] & 0x22) ? ref[skey].c_str() : ref[caps].c_str());
if (data[0] & 0x11)
bfr[0] &= 0x1f;
//Serial.print(bfr);
SetChar(bfr);
//fflush(stdout);
lstkey = ndx;
return;
}
void notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
Decode(pData, length);
}
void loop() {
char buf;
int ix = 0;
delay(400);
std::vector<NimBLEClient*> pClients = NimBLEDevice::getConnectedClients();
if (!pClients.size()) {
return;
}
pClient = pClients[0];
if (connected && !online) {
Serial.printf("Before Total heap: %d\r\n", ESP.getHeapSize());
Serial.printf("Free heap: %d\r\n", ESP.getFreeHeap());
Serial.printf("Alloc heap: %d\r\n", ESP.getMaxAllocHeap());
Serial.printf("Finding services\r\n");
pSvc = pClient->getService("1812");
nulls[0] = 0;
if (pSvc) {
for (int i = 0; i < 4; i++) {
pChr = pSvc->getCharacteristic("2a4d");
pChr->unsubscribe(true);
pChr->writeValue(nulls, 1, true);
pChr->readValue();
delay(10);
pChr->subscribe(true, notifyCB);
delay(10);
online = true;
Serial.printf("Online....\r\n");
Serial.flush();
}
return;
}
}
if (online ) {
start_display(&online);
}
}