-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.cpp
132 lines (114 loc) · 3.56 KB
/
bluetooth.cpp
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
#include "bluetooth.h"
#include "parser.h"
#include "client.h"
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
void Bluetooth::sendRPCRequest(const std::string& command, int s){ // s --> socket from client while loop
Parser parser;
CommandType cmdType = parser.getCommandType(command);
switch (cmdType){
case BEGIN:
sendBeginRequest(command, s);
break;
case END:
sendEndRequest(command, s);
break;
case AUTOPILOT:
sendAutopilotRequest(command, s);
break;
case RESET:
sendResetRequest(command, s);
break;
case BRAKE:
sendBrakeRequest(command, s);
break;
case ACCELERATE:
sendAccelerateRequest(command, s);
break;
case DECELERATE:
sendDecelerateRequest(command, s);
break;
case UNKNOWN:
this->sendUnknownRequest(command, s); // send the unknown request message to the pi
break;
default:
return;
}
}
// Repetition in case future changes need to be made to requests
void Bluetooth::sendBeginRequest(const std::string& command, int s) {
int w = write(s, command, len(command));
if (w < 0) {
std::cout << "REQUEST FAILED (┬┬﹏┬┬)" << std::endl;
}
}
void Bluetooth::sendEndRequest(const std::string& command, int s) {
int w = write(s, command, len(command));
if (w < 0) {
std::cout << "REQUEST FAILED (┬┬﹏┬┬)" << std::endl;
}
}
void Bluetooth::sendAutopilotRequest(const std::string& command, int s) {
int w = write(s, command, len(command));
if (w < 0) {
std::cout << "REQUEST FAILED (┬┬﹏┬┬)" << std::endl;
}
}
void Bluetooth::sendResetRequest(const std::string& command, int s) {
int w = write(s, command, len(command));
if (w < 0) {
std::cout << "REQUEST FAILED (┬┬﹏┬┬)" << std::endl;
}
}
void Bluetooth::sendBrakeRequest(const std::string& command, int s) {
int w = write(s, command, len(command));
if (w < 0) {
std::cout << "REQUEST FAILED (┬┬﹏┬┬)" << std::endl;
}
}
void Bluetooth::sendAccelerateRequest(const std::string& command, int s) {
int w = write(s, command, len(command));
if (w < 0) {
std::cout << "REQUEST FAILED (┬┬﹏┬┬)" << std::endl;
}
}
void Bluetooth::sendDecelerateRequest(const std::string& command, int s) {
int w = write(s, command, len(command));
if (w < 0) {
std::cout << "REQUEST FAILED (┬┬﹏┬┬)" << std::endl;
}
}
void Bluetooth::sendUnknownRequest(const std::string& command, int s){
return;
}
/*
gpt spawned this nightmare
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
void sendBluetoothCommand(const std::string& command, int s) {
std::string device = "/dev/rfcomm0"; // Device file for the Bluetooth connection
std::ofstream bt(device, std::ios::out);
if (!bt.is_open()) {
std::cerr << "Failed to open Bluetooth device." << std::endl;
return;
}
// Send the command
bt << command;
bt.flush();
std::cout << "Command sent: " << command << std::endl;
// Wait for the response
std::ifstream btIn(device, std::ios::in);
std::string response;
std::getline(btIn, response);
std::cout << "Received response: " << response << std::endl;
bt.close();
btIn.close();
}
int main() {
std::string command = "HELLO";
sendBluetoothCommand(command);
return 0;
}*/