Compiling example-mesh.c Contiki - contiki

How do I fix this error:
undefined reference to button sensor
when compiling example-mesh.c using Micaz mote in contiki?
Here is my code I run the simulation in the mote output window only 3 messages are sent while the rest is " packet timedout " how can I solve that problem to send messages based on timer value ?
#include "contiki.h"
#include "net/rime.h"
#include "net/rime/mesh.h"
#include "contiki-conf.h"
#include "sys/etimer.h"
#include "sys/process.h"
#include "sys/ctimer.h"
#include "dev/leds.h"
#include <stdio.h>
#include <string.h>
#define MESSAGE "Hello"
static struct mesh_conn mesh;
/*---------------------------------------------------------------------------*/
PROCESS(example_mesh_process, "Mesh example");
AUTOSTART_PROCESSES(&example_mesh_process);
/*---------------------------------------------------------------------------*/
static void
sent(struct mesh_conn *c)
{
printf("packet sent\n");
}
static void
timedout(struct mesh_conn *c)
{
printf("packet timedout\n");
}
static void
recv(struct mesh_conn *c, const rimeaddr_t *from, uint8_t hops)
{
printf("Data received from %d.%d: %.*s (%d)\n",
from->u8[0], from->u8[1],
packetbuf_datalen(), (char *)packetbuf_dataptr(), packetbuf_datalen());
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
mesh_send(&mesh, from);
}
const static struct mesh_callbacks callbacks = {recv, sent, timedout};
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_mesh_process, ev, data)
{
static struct etimer et;
PROCESS_EXITHANDLER(mesh_close(&mesh);)
PROCESS_BEGIN();
mesh_open(&mesh, 132, &callbacks);
while(1) {
rimeaddr_t addr;
etimer_set(&et, 5 * CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
etimer_reset(&et);
/* Send a message to node number 1. */
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
addr.u8[0] = 1;
addr.u8[1] = 0;
mesh_send(&mesh, &addr);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

As far as I know the Micaz mote does not have a button and therefore the reference to the button_sensor sensor is not valid.
The example is designed to send a message every time the button is pressed so if you want this example to work you need to rewrite the example to send messages based on a timer value.

This is how I would write it. However this node is dependent on another node that needs to accept the messages!
#include "contiki.h"
#include "net/rime.h"
#include "net/rime/mesh.h"
#include <stdio.h>
#include <string.h>
#define MESSAGE "Hello"
static struct mesh_conn mesh;
/*---------------------------------------------------------------------------*/
PROCESS(example_mesh_process, "Mesh example");
AUTOSTART_PROCESSES(&example_mesh_process);
/*---------------------------------------------------------------------------*/
static void
sent(struct mesh_conn *c)
{
printf("packet sent\n");
}
static void
timedout(struct mesh_conn *c)
{
printf("packet timedout\n");
}
static void
recv(struct mesh_conn *c, const rimeaddr_t *from, uint8_t hops)
{
printf("Data received from %d.%d: %.*s (%d)\n",
from->u8[0], from->u8[1],
packetbuf_datalen(), (char *)packetbuf_dataptr(), packetbuf_datalen());
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
mesh_send(&mesh, from);
}
const static struct mesh_callbacks callbacks = {recv, sent, timedout};
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_mesh_process, ev, data)
{
static struct etimer et;
PROCESS_EXITHANDLER(mesh_close(&mesh);)
PROCESS_BEGIN();
mesh_open(&mesh, 132, &callbacks);
while(1) {
linkaddr_t addr;
etimer_set(&et, 5 * CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
/* Send a message to node number 1. */
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
addr.u8[0] = 1;
addr.u8[1] = 0;
mesh_send(&mesh, &addr);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

Related

How to use LLVM's SanitizerCoverage code coverage instrumentation with a shared library?

I have a shared library linked to an executable for which I would like to have code coverage instrumentation using custom _sanitizer_cov_trace_pc* functions.
library.cc
#include <stdio.h>
void so_function() {
printf("SO function.");
}
callbacks.cc
#include <stdint.h>
#include <stdio.h>
#include <sanitizer/coverage_interface.h>
extern "C" void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
uint32_t *stop) {
static uint64_t N;
if (start == stop || *start) return;
printf("INIT: %p %p\n", start, stop);
for (uint32_t *x = start; x < stop; x++)
*x = ++N;
}
extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
if (!*guard) return;
void *PC = __builtin_return_address(0);
char PcDescr[1024];
__sanitizer_symbolize_pc(PC, "%p %F %L", PcDescr, sizeof(PcDescr));
printf("guard: %p %x PC %s\n", guard, *guard, PcDescr);
}
main.cc
#include <stdio.h>
void so_function();
int main(int argc, char **argv) {
so_function();
}
I compiled the library using clang's -fsanitize-coverage=trace-pc-guard into position-independent code (-fPIC) and then I created the shared library using both the resulted object file and callbacks.cc using -fsanitize=address.
I compiled main.cc and linked it with the shared library but it seems like these 2 custom __sanitizer_cov_trace_pc_guard* functions don't get called.
I would like have code coverage instrumentation using these 2 functions only for the shared library, and not for the main executable.

Neighbor discovery in contiki

Actually i dont know how to deal with neighbor_recv function. I am not receiving any packet, as was receiving in broadcast-example. All values used in this code are randomly initialized. Moreover I want to store neighbor of each node. Currently I am having 3 telosb motes.
#include "contiki.h"
#include "net/rime/rime.h"
#include "random.h"
#include "node-id.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
struct adv_msg {
uint16_t val;
};
/*------------------------------------------------------------------------ ---*/
PROCESS(neighbor_process, "Neighbor example");
AUTOSTART_PROCESSES(&neighbor_process);
/*------------------------------------------------------------------------ ---*/
static void
neighbor_recv(struct neighbor_discovery_conn *c, const linkaddr_t *from)
{
printf("message received from %d.%d: '%s'\n",
from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
}
static const struct neighbor_discovery_callbacks cb ={neighbor_recv};
static struct neighbor_discovery_conn neighbor;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(neighbor_process, ev, data)
{
static struct etimer et;
clock_time_t initial=0;// randomly initialize these values
clock_time_t min=0;
clock_time_t max=10;
uint16_t val=108;
PROCESS_EXITHANDLER(neighbor_discovery_close(&neighbor));
PROCESS_BEGIN();
neighbor_discovery_open(&neighbor, 129,initial,min,max,&cb);
while(1) {
/* Delay 2-4 seconds */
etimer_set(&et, CLOCK_SECOND * 4 + random_rand() % (CLOCK_SECOND * 4));
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
packetbuf_copyfrom("Hello", 5);
neighbor_discovery_set_val(&neighbor,val);
neighbor_discovery_start(&neighbor,val);
printf("message sent\n");
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

QTimer does not fire (trigger) timeout

I want to use Qtimer to pause executing of given block of codes, while in another thread it does something else. I connected the timeout of the thread with qeventloop quit, but the problem is, that the timeout is not called. When another emit occures, the timeout is magically triggered or if I add another connect the time out is triggered too. I think I miss something about the use of qtimer, qeventloop and qthread. Can anyone help? I extracted the basic code for testing and put it here:
main.cpp
#include "widget.h"
#include <QApplication>
#include "tim.h"
#include <QThread>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w=new Widget();
tim *t=new tim();
QThread *thread=new QThread();
t->moveToThread(thread);
thread->start();
QThread::connect(w,SIGNAL(signalDoIt()),t,SLOT(slotDoIt()),Qt::QueuedConnection);
QThread::connect(w,SIGNAL(signalQuitTimer()),t,SLOT(slotQuitTimer()),Qt::QueuedConnection);
QThread::connect(t,SIGNAL(signalSetText(QString)),w,SLOT(slotSetText(QString)),Qt::QueuedConnection);
w->show();
return a.exec();
}
tim.h
#ifndef TIM_H
#define TIM_H
#include <QObject>
#include<QTimer>
#include<QTime>
#include<QEventLoop>
#include<QThread>
#include<QDebug>
class tim : public QObject
{
Q_OBJECT
public:
tim();
~tim();
signals:
void signalSetText(QString);
public slots:
void slotDoIt();
void slotQuitTimer();
void slotShowTime();
private:
QTimer *trainingTimer;
QEventLoop loopTrainingWait;
QTime time;
};
#endif // TIM_H
tim.cpp
#include "tim.h"
tim::tim()
{
qDebug()<<"constructor";
trainingTimer=new QTimer(this);
trainingTimer->setTimerType(Qt::PreciseTimer);
trainingTimer->setSingleShot(true);
QThread::connect(trainingTimer,SIGNAL(timeout()),&loopTrainingWait,SLOT(quit()));
// QThread::connect(trainingTimer,SIGNAL(timeout()),this,SLOT(slotShowTime())); //to uncomment all works, but withou this, it does not
}
void tim::slotDoIt()
{
trainingTimer->start(5000);
time.start();
loopTrainingWait.exec();
QString text(QString::number(loopTrainingWait.isRunning())+" "+ QString::number(time.elapsed()));
qDebug()<<text;
emit signalSetText(text);
}
void tim::slotShowTime()
{
QString text(QString::number(loopTrainingWait.isRunning())+" slot "+ QString::number(time.elapsed()));
qDebug()<<text;
emit signalSetText(text);
}
void tim::slotQuitTimer()
{
if(loopTrainingWait.isRunning())
loopTrainingWait.quit();
if(trainingTimer->isActive())
trainingTimer->stop();
}
tim::~tim()
{
}
//gui for testing
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void slotSetText(QString text);
signals:
void signalDoIt();
void signalQuitTimer();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
void Widget::slotSetText(QString text)
{
ui->label->setText(text);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
this->close();
}
void Widget::on_pushButton_2_clicked()
{
emit signalDoIt();
}
void Widget::on_pushButton_3_clicked()
{
emit signalQuitTimer();
}
I met the same issue,and eventually I wrote SLOT between public and :,then it works
the problem was in static qeventloop, making this dynamic
loopTrainingWait=new QEventLoop(this);
with parent this, resolved the issue

Any alternative solution for QMessagebox for IOS development (QWidget application only)?

I am using Qt 5.3 and trying to develop application for IOS.
Problem is, QWidget application in a iPhone Retina simulator:
QMessage becomes full-screen.
In Application output panel I see: This plugin does not support
propagateSizeHints().
So looking for alternative solution for QMessageBox. I don't want to learn QML yet.
If you do an overlay on top of your widget you can make something similar to the iOS popups.
Basically you create another widget, and you parent it to the widget you want it to be drawn on top of.
Here are some helpful flags and lines of code to put in your overlay constructor:
setPalette(Qt::transparent);
// if you have buttons on this overlay you probably don't want this one
setAttribute(Qt::WA_TransparentForMouseEvents);
QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
dse->setBlurRadius(20);
this->setGraphicsEffect(dse);
Then be sure to command a resize of your overlay when the parent widget resizes:
void ParentWidget::resizeEvent(QResizeEvent *event)
{
overlay->resize(event->size());
event->accept();
}
http://www.qtcentre.org/wiki/index.php?title=Widget_Overlay
UPDATE: Awesome example
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.resize(300,600);
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "overlaydialogbox.h"
#include <QResizeEvent>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void resizeEvent(QResizeEvent *event);
private:
OverlayDialogBox * m_overlay;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_overlay = new OverlayDialogBox(this);
}
MainWindow::~MainWindow() { }
void MainWindow::resizeEvent(QResizeEvent *event)
{
m_overlay->resize(event->size());
event->accept();
}
overlaydialogbox.h
#ifndef OVERLAYDIALOGBOX_H
#define OVERLAYDIALOGBOX_H
#include <QWidget>
class OverlayDialogBox : public QWidget
{
Q_OBJECT
public:
explicit OverlayDialogBox(QWidget *parent = 0);
signals:
void accepted();
void rejected();
void finished(int);
public slots:
};
#endif // OVERLAYDIALOGBOX_H
overlaydialogbox.cpp
#include "overlaydialogbox.h"
#include <QGridLayout>
#include <QGraphicsEffect>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QIcon>
OverlayDialogBox::OverlayDialogBox(QWidget *parent) :
QWidget(parent)
{
setPalette(Qt::transparent);
// if you have buttons on this overlay you probably don't want this one
// setAttribute(Qt::WA_TransparentForMouseEvents);
QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
dse->setBlurRadius(20);
this->setGraphicsEffect(dse);
QGridLayout * grid = new QGridLayout();
this->setLayout(grid);
QMessageBox * msg = new QMessageBox(QMessageBox::Warning,"Testing","This is a test QMessageBox.");
QObject::connect(msg, SIGNAL(accepted()), this, SIGNAL(accepted()));
QObject::connect(msg, SIGNAL(finished(int)), this, SIGNAL(finished(int)));
QObject::connect(msg, SIGNAL(rejected()), this, SIGNAL(rejected()));
QObject::connect(msg, SIGNAL(finished(int)), this, SLOT(close()));
msg->setPalette(Qt::white);
grid->addWidget(msg);
}
Hope that helps.

shared object in pthread

I want to write a sample program in which 16 threads have access to a shared object with huge size like 10gb. I know that I can use pthread_mutex_t to get the lock on the object, but how can I make it efficient so that two or more thread can modify disjoint part of the shared object simultaneously?
Maybe you can create an array of 10 pthread_mutex_t's, one for each 1gb range, and lock the appropriate mutex for the range you'll be modifying?
What about using a sempahore. You can initialize semaphore with number of threads that shares the resources.
/* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <string.h> /* String handling */
#include <semaphore.h> /* Semaphore */
void semhandler ( void *ptr );
sem_t mutex;
int cntr=0; /* shared variable */
int main()
{
int arg[2];
pthread_t thread1;
pthread_t thread2;
arg[0] = 0;
arg[1] = 1;
/* initialize mutex to 2 to share resource with two threads*/
/* Seconds Argumnet "0" makes the semaphore local to the process */
sem_init(&mutex, 0, 2);
pthread_create (&thread1, NULL, (void *) &semhandler, (void *) &arg[0]);
pthread_create (&thread2, NULL, (void *) &semhandler, (void *) &arg[1]);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
sem_destroy(&mutex);
exit(0);
} /* main() */
void semhandler ( void *ptr )
{
int x;
x = *((int *) ptr);
printf("Thrd %d: Waiting to enter critical region...\n", x);
sem_wait(&mutex); /* down semaphore */
if( x == 1 )
cntr++;
/* START CRITICAL REGION */
printf("Thrd %d: Now in critical region...\n", x);
printf("Thrd %d: New Counter Value: %d\n", x, cntr);
printf("Thrd %d: Exiting critical region...\n", x);
/* END CRITICAL REGION */
sem_post(&mutex); /* up semaphore */
pthread_exit(0); /* exit thread */
}

Resources