Show tosnic_main.c syntax highlighted
/*
*
* Copyright (c) 2005 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "tosnic_i.h"
/*
* tosnic_main.c
*
* Interfaces between Mote and PC
*/
/*
* TODO:
*
* * fix tosnicd alignment error, stargates!!
*
* * randomize seqnos? (serial layer? senddone layer? )
* * mote side framer should drop previously acked packets (same seqno)
* * emtos should use emstar tosmsg header format; also need to get rest
* of fields
* * status/conf...
* * watchdog timer on sends to catch network lockup?
*
* * mote conf/stat/debug protocols
* * periodic status query
* * count/query CRC errors on channel
* * query MTU/mote type
* * hook up to emstatusserver / slot in new framer easily
* * mote reset
* * soft reset message to mote, send on reset.. reboot mote?
* * hard reset of mote via gpio?
*
* * emstarbase
* * tos lockup problem...
* * NACK from mote of seq+1 when crc fail
* * NACK or ACK of unknown type packet?
* * support setting frequency at runtime
*
* *** BUG-- locks up when given 0-length payload and TOS type.
*
* * clear packet on deck when repeated failures?
* (prevent lockup from bad packet?.. but kind of messy?)
*
* * better support for timestamping data packets
* * non-fusd compatibility?
*
*/
void tosnic_reset(tosnic_state_t *s)
{
int curr_state = (s->serial_data_recd << 1) | s->serial_packet_recd;
static int last_state = 0xf00;
if (curr_state != last_state) {
if (s->serial_packet_recd == 0) {
if (s->serial_data_recd) {
elog(LOG_WARNING, "Unable to contact mote, but some serial data:");
elog(LOG_WARNING, "Baud rate mismatch or wrong program on mote?");
}
else
elog(LOG_WARNING, "Unable to contact mote.. still trying");
s->mote_fail_warn = 1;
}
else {
elog(LOG_WARNING, "Resetting TOSnic!");
}
}
last_state = curr_state;
elog(LOG_DEBUG(0), "Reset lower...");
tosnic_lower_reset(s);
elog(LOG_DEBUG(0), "Reset framer...");
tosnic_framer_reset(s);
elog(LOG_DEBUG(0), "Reset upper...");
tosnic_upper_reset(s);
elog(LOG_DEBUG(0), "Reset done!");
}
void tosnic_no_contact(tosnic_state_t *s)
{
if (s->mote_type_detected) free(s->mote_type_detected);
s->mote_type_detected = strdup("No mote present");
}
/**************** main() etc ************************************************/
void usage(char *name)
{
misc_print_usage
(name,
"[-P <device>] [--ifclass <link_class>] --port <portspec> [--baud <baudrate>]\n"
" [--rtscts] [--power <power-level>] [--mote <mote-type>] [--tosbase <mtu>]",
" --provides <device>: Specify device to provide (default 'sink')\n"
" --ifclass: specify the link class to join (default 'network')\n"
" --port <spec>: specify the port to connect to, as:\n"
" a local serial device e.g. /dev/ttyS0, or\n"
" a remote tcp service such as AVRORA or EPrb, e.g. hostname:port\n"
" --baud <rate>: specify baudrate, default is 57600\n"
" --rtscts: enable hardware flow control\n"
" --mote <mote-type>: specify mote type for default settings, default is mica2\n"
" Supported types: mica, mica2, mica2dot, cricket, telos\n"
" --power <level>: specify initial power level, default is 15\n"
" --tosbase <mtu>: runs tosbase compatibility mode, with specified MTU\n"
" --default_group <group>: sets the default group for packets tx'd with group 0\n"
" --if_id <id>: sets the default interface ID\n"
" --if_index <index>: configures interface ID based on node id and index\n"
);
exit(1);
}
/* Callback when we are asked (by emrun) to shut down */
static void tosnic_shutdown(void *data)
{
elog(LOG_NOTICE, "Shutting down TOSnic");
exit(0);
}
int main(int argc, char *argv[])
{
/* generic init */
misc_init(&argc, argv, CVSTAG);
tosnic_state_t s = {
if_id: my_node_id,
ack_timeout_length: TOSNIC_ACK_TIMEOUT
};
/* parse provider args */
s.dev_name = link_parse_provides(&argc, argv, "mote0");
s.class_name = link_parse_if_class(&argc, argv, LINK_CLASS_NONE);
/* parse interface if arg */
char *ifid = NULL;
ifid = misc_parse_out_option(&argc, argv, "if_id", 0);
if (ifid) {
if (parse_if_id(ifid, &s.if_id) < 0) {
elog(LOG_CRIT, "Unable to parse if_id argument: %s", ifid);
exit(1);
}
}
/* set based on index */
int if_index = 0;
if (misc_parse_option_as_int(&argc, argv, "if_index", 0, &if_index) == 0) {
s.if_id = (s.if_id << 8) + if_index;
}
/* parse for tosbase option */
misc_parse_option_as_uint(&argc, argv, "tosbase", 0, &s.tosbase_mode);
if (s.tosbase_mode) s.MTU = s.tosbase_mode;
/* parse default group option */
misc_parse_option_as_uint8(&argc, argv, "default_group", 0, &s.default_group);
/* open lower.. provide upper only if not proxied or simulated.. */
if (tosnic_lower_init(&s, &argc, argv) == 0)
tosnic_upper_init(&s, &argc, argv);
/* set the description string.. */
if (s.tosbase_mode) {
s.mote_type_detected = strdup(s.mote_type_str);
}
else {
#if 0
/* commented out, since status isn't implemented
* this would be nice to have (probably in a less
* ambitious form)! */
/* start periodic status request */
tosnic_no_contact(&s);
tosnic_status_init(&s, &argc, argv);
#endif
}
if (misc_args_remain(&argc, argv)) {
usage(argv[0]);
}
/* register with emrun */
emrun_opts_t emrun_opts = {
shutdown: tosnic_shutdown,
data: NULL
};
emrun_init(&emrun_opts); /* this should be the last initialization */
g_main();
elog(LOG_CRIT, "event loop exited unexpectedly!");
return 1;
}
See more files for this project here