Skip to content

Commit 737033f

Browse files
committed
Add tcp socket for control
1 parent acc31c2 commit 737033f

15 files changed

Lines changed: 682 additions & 320 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ vgcore.*
2020

2121
*.pcap
2222
*.pcapng
23+
24+
/*.log

.vscode/c_cpp_properties.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"${workspaceFolder}/include",
77
"${workspaceFolder}/lib",
88
"${workspaceFolder}/lib/capstone/include",
9-
"${workspaceFolder}"
9+
"${workspaceFolder}",
10+
"${workspaceFolder}/include/bluetooth"
1011
],
1112
"defines": [
1213
"_POSIX_C_SOURCE=200809L"

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cmake_minimum_required(VERSION 3.13)
22

3+
include(FetchContent)
4+
35
project(infiniemu)
46

57
if(CMAKE_TOOLCHAIN_FILE MATCHES "Emscripten.cmake")
@@ -25,4 +27,8 @@ add_subdirectory(lib/capstone)
2527
add_library(lua STATIC lib/lua/onelua.c)
2628
target_compile_options(lua PRIVATE -DMAKE_LIB)
2729

30+
cmake_policy(SET CMP0135 NEW)
31+
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.12.0/json.tar.xz)
32+
FetchContent_MakeAvailable(json)
33+
2834
add_subdirectory(src)

include/binary_buffer.hpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <format>
5+
#include <type_traits>
6+
#include <string>
7+
8+
#include <vector>
9+
#include <string.h>
10+
#include <stdint.h>
11+
12+
using byte = uint8_t;
13+
template <std::size_t N>
14+
using bytes = std::array<byte, N>;
15+
using any_bytes = std::vector<byte>;
16+
17+
inline std::string ShowHex(any_bytes b)
18+
{
19+
std::string result;
20+
result.reserve(b.size() * 5);
21+
22+
for (size_t i = 0; i < b.size(); ++i)
23+
{
24+
result += std::format("0x{:02X}", b[i]);
25+
if (i + 1 < b.size())
26+
result += " ";
27+
}
28+
return result;
29+
}
30+
31+
class BinaryBuffer
32+
{
33+
std::vector<uint8_t> data;
34+
size_t position = 0;
35+
36+
public:
37+
BinaryBuffer() = default;
38+
BinaryBuffer(size_t size) : data(size) {}
39+
BinaryBuffer(const std::vector<uint8_t> &data) : data(data) {}
40+
BinaryBuffer(const uint8_t *data, size_t size) : data(data, data + size) {}
41+
42+
const std::vector<uint8_t> &get_data() const
43+
{
44+
return data;
45+
}
46+
47+
size_t get_position() { return position; }
48+
49+
void write(uint8_t value)
50+
{
51+
data.push_back(value);
52+
}
53+
void write(uint16_t value)
54+
{
55+
data.push_back(value & 0xFF);
56+
data.push_back((value >> 8) & 0xFF);
57+
}
58+
template <auto N>
59+
void write(const std::array<uint8_t, N> data)
60+
{
61+
for (size_t i = 0; i < N; ++i)
62+
this->data.push_back(data[i]);
63+
}
64+
void write(const any_bytes data)
65+
{
66+
this->data.insert(this->data.end(), data.begin(), data.end());
67+
}
68+
template <typename T>
69+
void write(const T &value)
70+
{
71+
const uint8_t *data = reinterpret_cast<const uint8_t *>(&value);
72+
for (size_t i = 0; i < sizeof(T); ++i)
73+
this->data.push_back(data[i]);
74+
}
75+
76+
template <auto N>
77+
void fill(bytes<N> &data)
78+
{
79+
for (size_t i = 0; i < N; ++i)
80+
data[i] = u8();
81+
}
82+
void fill(any_bytes &data, size_t size)
83+
{
84+
data.resize(size);
85+
for (size_t i = 0; i < size; ++i)
86+
data[i] = u8();
87+
}
88+
void fill_remaining(any_bytes &data)
89+
{
90+
size_t remaining = this->data.size() - position;
91+
fill(data, remaining);
92+
}
93+
94+
template <typename T>
95+
void read(T &value)
96+
{
97+
memcpy(&value, &data[position], sizeof(T));
98+
position += sizeof(T);
99+
}
100+
101+
uint8_t u8()
102+
{
103+
return data[position++];
104+
}
105+
uint16_t u16()
106+
{
107+
uint16_t value = data[position] | (data[position + 1] << 8);
108+
position += 2;
109+
return value;
110+
}
111+
};

include/bluetooth.h

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,8 @@
11
#pragma once
22

3-
#ifdef __cplusplus
4-
extern "C"
5-
{
6-
#endif
7-
83
#include "pinetime.h"
9-
#include "peripherals/nrf52832/radio.h"
10-
11-
#ifdef __cplusplus
12-
}
13-
#endif
14-
15-
#ifdef __cplusplus
16-
namespace BLE
17-
{
18-
class Packet;
19-
};
20-
21-
#include <array>
22-
#include <memory>
23-
#include <queue>
24-
#include <map>
25-
26-
constexpr uint32_t ConnIntervalMS = 100;
27-
constexpr uint32_t ConnPeripheralLatency = 0;
28-
constexpr uint32_t TransmitWindowSizeMS = 10; // Must be at most 10
29-
constexpr uint32_t ConnSupervisionTimeoutMS = (1 + ConnPeripheralLatency) * ConnIntervalMS * 2 + 100;
30-
constexpr std::array<uint8_t, 3> FakeCRC = {0xFF, 0xFF, 0xFF};
31-
32-
constexpr uint16_t WantATT_MTU = 247;
33-
34-
enum Stage
35-
{
36-
NONE,
37-
CONNECTED,
38-
EXCHANGED_MTU,
39-
REQUESTED_INFO,
40-
};
41-
42-
struct pending_packet_t
43-
{
44-
uint64_t send_at;
45-
std::unique_ptr<BLE::Packet> packet;
46-
};
47-
48-
class PendingCompare
49-
{
50-
public:
51-
bool operator()(pending_packet_t &a, pending_packet_t &b)
52-
{
53-
// Lower send_at should come first
54-
return a.send_at > b.send_at;
55-
}
56-
};
57-
58-
struct bluetooth_t
59-
{
60-
RADIO_t *radio;
61-
event_queue_t *ev_queue;
62-
NRF52832_t *nrf;
63-
bool connected = false;
644

65-
bool sent_req = false;
66-
Stage stage = NONE;
67-
uint16_t att_mtu = 23; // Default MTU
68-
69-
struct
70-
{
71-
bool in_fragmented = false;
72-
uint32_t total_length;
73-
uint8_t channel;
74-
std::vector<uint8_t> buffer;
75-
} l2cap_frag;
76-
77-
uint64_t last_conn_event_cycles = 0;
78-
79-
unsigned int transmitSeqNum : 1;
80-
unsigned int nextExpectedSeqNum : 1;
81-
82-
std::map<uint16_t, uint16_t> attrs16;
83-
84-
std::priority_queue<pending_packet_t, std::vector<pending_packet_t>, PendingCompare> pending_packets;
85-
86-
bluetooth_t(pinetime_t *pt) : radio(static_cast<RADIO_t *>(nrf52832_get_peripheral(pinetime_get_nrf52832(pt), INSTANCE_RADIO))),
87-
ev_queue(pinetime_get_event_queue(pt)),
88-
nrf(pinetime_get_nrf52832(pt))
89-
{
90-
}
91-
92-
void Send(const BLE::Packet &packet);
93-
94-
void Enqueue(std::unique_ptr<BLE::Packet> packet, size_t delay_ms);
95-
void Enqueue(std::unique_ptr<BLE::Packet> packet)
96-
{
97-
Enqueue(std::move(packet), 0);
98-
}
99-
};
100-
101-
extern "C"
102-
{
103-
#else
1045
typedef struct bluetooth_t bluetooth_t;
105-
#endif
106-
107-
bluetooth_t *bluetooth_new(pinetime_t *pt);
108-
void bluetooth_run(bluetooth_t *);
1096

110-
#ifdef __cplusplus
111-
}
112-
#endif
7+
bluetooth_t *bluetooth_new(pinetime_t *pt);
8+
void bluetooth_run(bluetooth_t *);

0 commit comments

Comments
 (0)