16
16
#include < thread>
17
17
#include < unistd.h>
18
18
#include < fcntl.h>
19
-
19
+ # include < fstream >
20
20
21
21
#include " hu.pb.h"
22
22
26
26
27
27
#define SERVICE_BUS_ADDRESS " unix:path=/tmp/dbus_service_socket"
28
28
29
+ std::string IP_ADDRESS;
30
+ std::string MAC_ADDRESS;
31
+
29
32
30
33
void sendMessage (int fd, google::protobuf::MessageLite &message, uint16_t type) {
31
34
int byteSize = message.ByteSize ();
@@ -52,8 +55,8 @@ void handleWifiInfoRequest(int fd, uint8_t *buffer, uint16_t length) {
52
55
logd (" WifiInfoRequest: %s\n " , msg.DebugString ().c_str ());
53
56
54
57
HU::WifiInfoResponse response;
55
- response.set_ip_address (" 192.168.53.1 " );
56
- response.set_port (5000 );
58
+ response.set_ip_address (IP_ADDRESS. c_str () );
59
+ response.set_port (30515 );
57
60
response.set_status (HU::WifiInfoResponse_Status_STATUS_SUCCESS);
58
61
59
62
sendMessage (fd, response, 7 );
@@ -62,9 +65,9 @@ void handleWifiInfoRequest(int fd, uint8_t *buffer, uint16_t length) {
62
65
void handleWifiSecurityRequest (int fd, uint8_t *buffer, uint16_t length) {
63
66
HU::WifiSecurityReponse response;
64
67
65
- response.set_ssid (" MazdaCMU " );
66
- response.set_bssid (" 0c:d9:c1:8f:7f:bb " );
67
- response.set_key (" password " );
68
+ response.set_ssid (hostapd_config ( " ssid " ). c_str () );
69
+ response.set_bssid (MAC_ADDRESS. c_str () );
70
+ response.set_key (hostapd_config ( " wpa_passphrase " ). c_str () );
68
71
response.set_security_mode (HU::WifiSecurityReponse_SecurityMode_WPA2_PERSONAL);
69
72
response.set_access_point_type (HU::WifiSecurityReponse_AccessPointType_DYNAMIC);
70
73
@@ -93,7 +96,7 @@ void BDSClient::SignalConnected_cb(const uint32_t &type, const ::DBus::Struct <s
93
96
logd (" \t PTY: %s\n " , pty);
94
97
int fd = open (pty, O_RDWR | O_NOCTTY | O_SYNC);
95
98
HU::WifiInfoRequest request;
96
- request.set_ip_address (" 192.168.53.1 " );
99
+ request.set_ip_address (IP_ADDRESS. c_str () );
97
100
request.set_port (30515 );
98
101
99
102
sendMessage (fd, request, 1 );
@@ -122,7 +125,7 @@ void BDSClient::SignalConnected_cb(const uint32_t &type, const ::DBus::Struct <s
122
125
handleWifiSecurityRequest (fd, buffer, size);
123
126
break ;
124
127
case 7 :
125
- if (handleWifiInfoRequestResponse (fd, buffer, size) == 0 ){
128
+ if (handleWifiInfoRequestResponse (fd, buffer, size) == 0 ) {
126
129
loop = 0 ;
127
130
}
128
131
break ;
@@ -135,8 +138,35 @@ void BDSClient::SignalConnected_cb(const uint32_t &type, const ::DBus::Struct <s
135
138
}
136
139
}
137
140
141
+ std::string hostapd_config (std::string key) {
142
+ std::ifstream hostapd_file;
143
+ hostapd_file.open (" /tmp/current-session-hostapd.conf" );
144
+
145
+ if (hostapd_file) {
146
+ std::string line;
147
+ size_t pos;
148
+ while (hostapd_file.good ()) {
149
+ getline (hostapd_file, line); // get line from file
150
+ if (line[0 ] != ' #' ) {
151
+ pos = line.find (key); // search
152
+ if (pos != std::string::npos) // string::npos is returned if string is not found
153
+ {
154
+ int equalPosition = line.find (" =" );
155
+ std::string value = line.substr (equalPosition + 1 ).c_str ();
156
+ return value.c_str ();
157
+ }
158
+ }
159
+ }
160
+ return " " ;
161
+ }
162
+ else {
163
+ return " " ;
164
+ }
165
+ };
166
+
138
167
void wireless_thread () {
139
168
static BDSClient *bds_client = NULL ;
169
+ static NMSClient *nms_client = NULL ;
140
170
DBus::BusDispatcher dispatcher;
141
171
DBus::default_dispatcher = &dispatcher;
142
172
@@ -146,6 +176,23 @@ void wireless_thread() {
146
176
DBus::Connection service_bus (SERVICE_BUS_ADDRESS, false );
147
177
service_bus.register_bus ();
148
178
bds_client = new BDSClient (service_bus, " /com/jci/bds" , " com.jci.bds" );
179
+ nms_client = new NMSClient (service_bus, " /com/jci/nms" , " com.jci.nms" );
180
+
181
+ ::DBus::Struct <std::vector<int32_t >> interface_list;
182
+ int32_t rvalue;
183
+ nms_client->GetInterfaceList (rvalue, interface_list);
184
+ ::DBus::Struct <int32_t , int32_t , std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, int32_t , int32_t , int32_t , int32_t > interface_params;
185
+ for (auto const &interface: interface_list._1 ) {
186
+ nms_client->GetInterfaceParams (interface, rvalue, interface_params);
187
+ logd (" Interface: %s, MAC: %s, IP: %s\n " , interface_params._4 .c_str (), interface_params._6 .c_str (),
188
+ interface_params._5 .c_str ());
189
+ if (interface_params._4 .compare (" wlan0" ) == 0 ) {
190
+ IP_ADDRESS.assign (interface_params._5 .c_str ());
191
+ MAC_ADDRESS.assign (interface_params._6 .c_str ());
192
+ }
193
+ }
194
+
195
+
149
196
dispatcher.enter ();
150
197
}
151
198
catch (DBus::Error &error) {
0 commit comments