Skip to content

Commit cacda06

Browse files
committed
Add sources, makefule and test script
1 parent 2febf14 commit cacda06

6 files changed

+839
-0
lines changed

Makefile

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#############################################################################
2+
#
3+
# Makefile for RF24toTUN
4+
#
5+
# License: MIT
6+
# Author: Rei <[email protected]>
7+
# Date: 22.08.2014
8+
#
9+
# Description:
10+
# ------------
11+
# use make all and make install to install the examples
12+
# You can change the install directory by editing the prefix line
13+
#
14+
prefix := /usr/local
15+
16+
# The recommended compiler flags for the Raspberry Pi
17+
CCFLAGS=-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -std=c++0x
18+
19+
# The needed libraries
20+
LIBS=-lrf24-bcm -lrf24network -lboost_thread -lboost_system
21+
22+
# define all programs
23+
PROGRAMS = rf24totun
24+
SOURCES = rf24totun.cpp
25+
26+
all: ${PROGRAMS}
27+
28+
${PROGRAMS}: ${SOURCES}
29+
g++ ${CCFLAGS} -W -pedantic -Wall ${LIBS} $@.cpp -o $@
30+
31+
clean:
32+
rm -rf $(PROGRAMS)
33+
34+
install: all
35+
test -d $(prefix) || mkdir $(prefix)
36+
test -d $(prefix)/bin || mkdir $(prefix)/bin
37+
for prog in $(PROGRAMS); do \
38+
install -m 0755 $$prog $(prefix)/bin; \
39+
done
40+
41+
42+
.PHONY: install

Message.h

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* The MIT License (MIT)
3+
* Copyright (c) 2014 Rei <[email protected]>
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*
21+
*
22+
*/
23+
24+
#include <cstdint>
25+
#include <string>
26+
#include <vector>
27+
28+
29+
/**
30+
* This class encapsulates the messages received from the TUN/TAP or radio interface
31+
*/
32+
class Message {
33+
public:
34+
Message() :
35+
payload_({0}),
36+
length_(0),
37+
seqNo_(0) {};
38+
39+
/**
40+
* Get the length of the message in bytes.
41+
* @return The byte size of the message
42+
*/
43+
std::size_t getLength() {
44+
return length_;
45+
};
46+
47+
/**
48+
* Get the sequence number of the message
49+
* @return The sequence number
50+
*/
51+
uint8_t getSeqNo() {
52+
return seqNo_;
53+
};
54+
55+
/**
56+
* Get the payload of the message as a std::string
57+
* @return the payload as string
58+
*/
59+
std::string getPayloadStr() {
60+
return std::string(payload_.begin(), payload_.end());
61+
};
62+
63+
/**
64+
* Get the payload of the message as a char array
65+
* @return the payload as a c-string
66+
*/
67+
const char* getPayload() {
68+
const char* res = payload_.data();
69+
return res;
70+
};
71+
72+
/**
73+
* Set the sequence number of the message
74+
* @param sn The new sequence number
75+
*/
76+
void setSeqNo(uint8_t sn) {
77+
seqNo_ = sn;
78+
}
79+
80+
/**
81+
* Set the payload of the message
82+
* @param buffer char array as the payload
83+
* @param bsize size of the buffer
84+
*/
85+
void setPayload(char * buffer, std::size_t bsize) {
86+
payload_.clear();
87+
payload_.assign(buffer, buffer + bsize);
88+
length_ = payload_.size();
89+
};
90+
91+
private:
92+
std::vector<char> payload_; /**< Internal vector data structure to save the payload*/
93+
std::size_t length_; /**< Current length of the payload */
94+
uint8_t seqNo_; /**< Sequence number */
95+
96+
};

ThreadSafeQueue.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Based on http://thisthread.blogspot.de/2011/09/threadsafe-stdqueue.html
2+
3+
4+
#include <queue>
5+
#include <boost/thread/thread.hpp>
6+
#include <boost/thread/mutex.hpp>
7+
8+
template <typename T>
9+
class ThreadSafeQueue {
10+
private:
11+
std::queue<T> q_;
12+
boost::mutex m_; // 1
13+
boost::condition_variable c_;
14+
15+
public:
16+
void push(const T& data) {
17+
boost::lock_guard<boost::mutex> l(m_); // 1
18+
q_.push(data);
19+
c_.notify_one(); // 2
20+
}
21+
22+
T pop() {
23+
boost::mutex::scoped_lock l(m_); // 3
24+
while(q_.size() == 0) {
25+
c_.wait(l); // 4
26+
}
27+
28+
T res = q_.front();
29+
q_.pop();
30+
return res; // 5
31+
}
32+
33+
bool empty() {
34+
return q_.empty();
35+
}
36+
};

0 commit comments

Comments
 (0)