-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaa_client.cpp
More file actions
340 lines (295 loc) · 10.5 KB
/
kaa_client.cpp
File metadata and controls
340 lines (295 loc) · 10.5 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <memory>
#include <condition_variable>
#include <kaa/Kaa.hpp>
#include <kaa/logging/Log.hpp>
#include <kaa/logging/LoggingUtils.hpp>
#include <kaa/notification/INotificationTopicListListener.hpp>
#include <kaa/common/exception/UnavailableTopicException.hpp>
#include <rtl-sdr.h>
#include "convenience/convenience.hpp"
#include <memory>
#include <string>
#include <cstdint>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <cstdlib>
#include <ncurses.h>
#include <kaa/log/strategies/RecordCountLogUploadStrategy.hpp>
#include <kaa/log/ILogStorageStatus.hpp>
#include <ctime>
/* timestamp example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, time (for timestamp in second) */
#include <sys/timeb.h> /* ftime, timeb (for timestamp in millisecond) */
#include <sys/time.h> /* gettimeofday, timeval (for timestamp in microsecond) */
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace kaa;
#define DEFAULT_SAMPLE_RATE 2048000
static constexpr auto TOPIC_LIST_UPDATE_TIMEOUT = 3;
static bool switch_flag = false;
static int samplingPeriod = 1; //seconds
static std::string numberOfBins = "20";
static std::string numberOfSpectraToAvg = "32";
rtlsdr_dev_t *dev = NULL;
std::string cmd_for_power = "./rtl_power_fftw -q -b 2 -f 915.8e6 -e 2m -g 1";
std::string get_mac(){
const int size = 256;
char ip_address[size];
int hw_type;
int flags;
char mac_address[size];
char mask[size];
char device[size];
FILE* fp = fopen("/proc/net/arp", "r");
if(fp == NULL)
{
perror("Error opening /proc/net/arp");
}
char line[size];
fgets(line, size, fp); // Skip the first line, which consists of column headers.
while(fgets(line, size, fp))
{
sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
ip_address,
&hw_type,
&flags,
mac_address,
mask,
device);
// printf("IP = %s, MAC = %s", ip_address, mac_address);
}
fclose(fp);
return ip_address;
}
long long int get_timestamp()
{
struct timeval timer_usec;
long int timestamp_usec; /* timestamp in microsecond */
if (!gettimeofday(&timer_usec, NULL)) {
timestamp_usec = (( long int) timer_usec.tv_sec) * 1000000ll +
(long int) timer_usec.tv_usec;
}
else {
timestamp_usec = -1;
}
return timestamp_usec;
}
static void set_up_device()
{
static rtlsdr_dev_t *dev = NULL;
int dev_index = 0;
int r;
dev_index = verbose_device_search("0");
r = rtlsdr_open(&dev, (uint32_t)dev_index);
if (r < 0)
{
printf("Failed to open device");
exit (1);
}
verbose_set_sample_rate(dev, DEFAULT_SAMPLE_RATE);
verbose_set_frequency(dev, 915800000);
verbose_gain_set(dev, 1);
verbose_ppm_set(dev, 0);
verbose_reset_buffer(dev);
}
static void toggleFlag()
{
int error = 9999;
switch_flag = !switch_flag;
if (switch_flag){
set_up_device();
}else{
error = rtlsdr_close(dev);
std::cout << "Close device status: " << error << std::endl;
}
}
static void modifySamplingPeriod(const KaaNotification ¬ification)
{
std::cout << "New sampling period :" << notification.NewSamplingFrequency << std::endl;
int num;
const char *c;
c = notification.NewSamplingFrequency.c_str();
//std::cout << "New sampling period STRING:" << c << std::endl;
num = std::stoi(c);
//std::cout << "New sampling period :" << num << std::endl;
samplingPeriod = num;
}
static void modifyNumberOfBins(const KaaNotification ¬ification)
{
std::cout << "New number of bins:" << notification.NewNumberOFBins << std::endl;
numberOfBins = notification.NewNumberOFBins.c_str();
std::string newCommand = "./rtl_power_fftw -n "+numberOfSpectraToAvg+" -q -b "+numberOfBins+" -f 916e6 -g 1";
std::cout << "New command: " << newCommand << std::endl;
}
static void modifyNumberOfSpectraToAvg(const KaaNotification ¬ification)
{
std::cout << "New number of spectra to avg:" << notification.NewNoAvgdSpectra << std::endl;
numberOfSpectraToAvg = notification.NewNoAvgdSpectra.c_str();
std::string newCommand = "./rtl_power_fftw -n "+numberOfSpectraToAvg+" -q -b "+numberOfBins+" -f 916e6 -g 1";
std::cout << "New command: " << newCommand << std::endl;
}
static void processNotification(const KaaNotification ¬ification, const kaa_notification::power_iq_change_choice powiqChange)
{
const char *samplefreq;
const char *bins;
const char *avgdspectra;
samplefreq = notification.NewSamplingFrequency.c_str();
bins = notification.NewNumberOFBins.c_str();
avgdspectra = notification.NewNoAvgdSpectra.c_str();
//const kaa_notification::PowIq_change powiqChange = notification.PowIQ_change;
switch(powiqChange)
{
case kaa_notification::Y_change:
toggleFlag();
std::cout << "Changing Power IQ switch" << std::endl;
break;
case kaa_notification::N_change:
break;
}
if(samplefreq[0] != '0')
{
std::cout << "Changing Sampling Frequency" << std::endl;
modifySamplingPeriod(notification);
}
if(bins[0] != '0')
{
std::cout << "Changing number of bins" << std::endl;
modifyNumberOfBins(notification);
}
if(avgdspectra[0] != '0')
{
std::cout << "Changing number of spectra to average" << std::endl;
modifyNumberOfSpectraToAvg(notification);
}
}
#define READ 0
#define WRITE 1
#define RAW_SIZE (4 * 1024)
static int exec_raw(unsigned char *array)
{
uint32_t out_block_size = RAW_SIZE;
int n_read = RAW_SIZE;
std::cout << dev;
int r = rtlsdr_read_sync(dev, array, out_block_size, &n_read);
if (r < 0)
{
printf("Error in reading");
return (1);
}
return (0);
}
static void extractData(std::shared_ptr<IKaaClient> kaaClient)
{
if (!switch_flag)
{
char buffer[128];
const char *cmd = cmd_for_power.c_str();
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get()))
{
if (fgets(buffer, 128, pipe.get()) != nullptr)
{
std::stringstream ss;
ss << buffer;
kaa::KaaUserLogRecord logRecord;
ss >> logRecord.frequency;
ss >> logRecord.power;
logRecord.nodenumber = get_mac();
logRecord.timestamp = get_timestamp();
auto recordDeliveryCallback = kaaClient->addLogRecord(logRecord);
// while(1)
// {
// try {
// auto recordInfo = recordDeliveryCallback.get();
// auto bucketInfo = recordInfo.getBucketInfo();
// std::cout << "Received log record delivery info. Bucket Id [" << bucketInfo.getBucketId() << "]. "
// << "Record delivery time [" << recordInfo.getRecordDeliveryTimeMs() << " ms]." << std::endl;
// break;
// } catch (std::exception& e) {
// std::cout << "Exception was caught while waiting for callback result: " << e.what() << std::endl;
// }
// }
//std::cout << "Sampled power and frequency: " << logRecord.power << " " << logRecord.frequency << std::endl;
}
}
/*kaa::KaaUserLogRecord logRecord;
//srand(200);
logRecord.frequency = (rand() % 20) + 8;
logRecord.power = (rand() % 20) + 8;
kaaClient->addLogRecord(logRecord);*/
}
else
{
unsigned char *array = (unsigned char *)malloc(RAW_SIZE);
exec_raw(array);
std::vector<unsigned char> vec(array, array + RAW_SIZE);
kaa::KaaUserLogRecord logRecord;
logRecord.frequency = logRecord.power = 0;
logRecord.iq = vec;
logRecord.nodenumber = get_mac();
logRecord.timestamp = get_timestamp();
kaaClient->addLogRecord(logRecord);
std::cout << "IQ data entered: " << std::endl;
/*kaa::KaaUserLogRecord logRecord;
//srand(50);
logRecord.frequency = (rand() % 20) + 8;
logRecord.power = (rand()% 20) + 8;
kaaClient->addLogRecord(logRecord);*/
}
}
class NotificationListener : public INotificationListener
{
public:
NotificationListener(std::shared_ptr<IKaaClient> kaaClient):
kaaClient(kaaClient)
{}
void onNotification(const int64_t topicId, const KaaNotification ¬ification) override
{
auto topics = kaaClient->getTopics();
const auto &topic = std::find_if(topics.begin(), topics.end(),
[topicId](const Topic &t) { return t.id == topicId; });
std::cout << (boost::format("Notification for topic id '%1%' and name '%2%' received") % topicId % topic->name) << std::endl;
processNotification(notification, notification.power_iq_change);
//std::cout << "Calling toggle flag method...." << std::endl;
//processNotification(notification);
}
private:
std::shared_ptr<IKaaClient> kaaClient;
};
int main()
{
auto kaaClient = Kaa::newClient();
int counter = 0;
//set_up_device();
kaaClient->setLogUploadStrategy(std::make_shared<kaa::RecordCountLogUploadStrategy>(100, kaaClient->getKaaClientContext()));
NotificationListener notificationListener(kaaClient);
kaaClient->addNotificationListener(notificationListener);
std::cin.clear();
kaaClient->start();
std::cout << "Notification demo started" << std::endl;
const auto &topics = kaaClient->getTopics();
for(const auto &topic : topics)
{
std::cout << (boost::format("Topic ID-> %1%, Topic name-> %2%") % topic.id % topic.name) << std::endl;
}
switch_flag = false;
while (counter < 2)
{
counter++;
extractData(kaaClient);
//std::cout << counter << "--->In loop with flag : " << switch_flag << std::endl;
std::this_thread::sleep_for (std::chrono::seconds(samplingPeriod));
}
kaaClient->stop();
return EXIT_SUCCESS;
}