Map27
 All Classes Functions Variables Enumerations Enumerator Pages
test3.cpp

Sample program showing how to create a real application that connects to a real radio. This sample connects to /dev/ttyUSB0 at 9600 baud and sends a radio personality request every second. It prints out the details of the reply.

// test3.cpp
//
// Example program showing how to use the Map27 protoco stack with a real
// radio
//
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2013 Mike McCauley
// $Id: test.cpp,v 1.12 2013/07/14 07:15:46 mikem Exp mikem $
#include "Address.h"
#include "ApplicationLayer.h"
#include "PhysicalLayer.h"
#include "DataLayer.h"
#include "NetworkLayer.h"
#include "UnixPort.h"
#include "Log.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
// The name of the device to use to connect to the radio
#define RADIO_DEVICE_NAME "/dev/ttyUSB0"
// The baud rate for the RS232 connection to the radio
#define RADIO_DEVICE_BAUD 9600
// To create a real applicaiotn, you should subclass Map27ApplicationLayer
// and override the funcitons you need to use
{
public:
// This will be called when the RS232 link to the radio becomes ready
void link_ready()
{
printf("link ready\n");
}
// This will be called when a RADIO PERSONALITY reply is received
void receiveRadioPersonality(Map27Address& address, uint8_t manufacturers_code, uint8_t model,
uint32_t serial, uint8_t facilities_a, uint8_t facilities_b,
uint8_t facilities_c, uint8_t codings)
{
printf("receiveRadioPersonality:\n Address:%s\n Manufacturers Code: %d\n Model: %d\n Serial Number: %d\n Facilities A: %02x\n Facilities B: %02x\n Facilities C: %02x\n Codings: %02x\n", address.asString(), manufacturers_code, model, serial, facilities_a, facilities_b, facilities_c, codings);
}
// This will be called if the link fails
void link_failure()
{
printf("link failure\n");
}
};
int main(int argc, char** argv)
{
MyApplication application;
// Configure the RS232 port
port.setDevice(RADIO_DEVICE_NAME);
port.configure(RADIO_DEVICE_BAUD);
// Connect the layers
physical.setPort(&port);
physical.setDataLayer(&data);
data.setPhysicalLayer(&physical);
data.setNetworkLayer(&network);
network.setDataLayer(&data);
network.setApplicationLayer(&application);
application.setNetworkLayer(&network);
// Power up
application.power_on();
time_t last_time = 0;
// Main loop
while (1)
{
application.poll();
usleep(10000);
// Every second, ask for radio personality
// Reply wil be handled by receiveRadioPersonality() in your subclass
time_t this_time = time(0);
if (this_time != last_time)
{
network.sendRadioInterrogation(MAP27_NETWORK_MESSAGE_RADIO_INTERROGATION_PERSONALITY);
last_time = this_time;
}
}
return 0;
};