mtp_test.c 7.5 KB
Newer Older
Timothy Stack's avatar
Timothy Stack committed
1 2
/*
 * Copyright (c) 2005 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Timothy Stack's avatar
Timothy Stack committed
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

#include "config.h"

#define DEBUG

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

#include "mtp.h"

int main(int argc, char *argv[])
{
  static struct mtp_control mc = {
    42,
    MTP_PP_ERROR_MALLOC,
    "I've come undone"
  };

  static struct robot_config rcr[3] = {
    { 1, "node1" },
    { 2, "node2" },
    { 3, "node3" },
  };

  static struct mtp_config_rmc mcr = {
    3,
    rcr,
54
    { 10.1, 20.2 }
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  };
  
  static struct robot_config rcv[3] = {
    { 4, "vnode4" },
    { 5, "vnode5" },
    { 6, "vnode6" },
  };

  static struct mtp_config_vmc mcv = {
    3,
    rcv
  };
  
  static struct mtp_request_position mrp = {
    4
  };
  
  static struct mtp_update_position mup = {
    3,
Timothy Stack's avatar
Timothy Stack committed
74 75
    { 5, 25, 0.44, 20.5 },
    MTP_POSITION_STATUS_IDLE
76 77 78
  };
  
  static struct mtp_request_id mri = {
Timothy Stack's avatar
Timothy Stack committed
79 80
    169,
    { 3.1, 20.2, 69.5, 20.5 }
81 82 83
  };
  
  static struct mtp_update_id mui = {
Timothy Stack's avatar
Timothy Stack committed
84 85
    30,
    2
86 87 88 89 90
  };
  
  static struct mtp_command_goto mcg = {
    5,
    3,
Timothy Stack's avatar
Timothy Stack committed
91
    { 2.2, 13.3, 73.4, 500.04 },
92 93 94 95 96 97
  };
  
  static struct mtp_command_goto mcs = {
    4,
    2,
  };
98 99 100 101 102 103 104 105 106 107

  static struct mtp_wiggle_request mwr = {
    4,
    1,
  };

  static struct mtp_wiggle_status mws = {
    4,
    4,
  };
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
  
  int fd, retval = EXIT_SUCCESS;

  if ((fd = open("mtp_stream.dat", O_CREAT | O_TRUNC | O_RDWR, 0666)) < 0) {
    perror("Could not open 'mtp_stream.dat'?");
  }
  else {
    struct mtp_packet *mp;
    int lpc;
    
    assert(mp = mtp_make_packet(MTP_CONTROL_ERROR, MTP_ROLE_EMC, &mc));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_CONFIG_RMC, MTP_ROLE_RMC, &mcr));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_CONFIG_VMC, MTP_ROLE_VMC, &mcv));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_REQUEST_POSITION, MTP_ROLE_VMC, &mrp));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_UPDATE_POSITION, MTP_ROLE_VMC, &mup));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_REQUEST_ID, MTP_ROLE_VMC, &mri));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_UPDATE_ID, MTP_ROLE_VMC, &mui));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_COMMAND_GOTO, MTP_ROLE_VMC, &mcg));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_COMMAND_STOP, MTP_ROLE_VMC, &mcs));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

145 146 147 148 149 150
    assert(mp = mtp_make_packet(MTP_WIGGLE_REQUEST, MTP_ROLE_VMC, &mwr));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

    assert(mp = mtp_make_packet(MTP_WIGGLE_REQUEST, MTP_ROLE_VMC, &mws));
    assert(mtp_send_packet(fd,mp) == MTP_PP_SUCCESS);

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
    lseek(fd,0,SEEK_SET);

    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_CONTROL_ERROR);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_EMC);
    assert(mp->data.control->id == mc.id);
    assert(mp->data.control->code == mc.code);
    assert(strcmp(mp->data.control->msg, mc.msg) == 0);

    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_CONFIG_RMC);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_RMC);
    assert(mp->data.config_rmc->num_robots == mcr.num_robots);
    assert(mp->data.config_rmc->box.horizontal == mcr.box.horizontal);
    assert(mp->data.config_rmc->box.vertical == mcr.box.vertical);
    for (lpc = 0; lpc < mcr.num_robots; lpc++) {
      assert(mp->data.config_rmc->robots[lpc].id == mcr.robots[lpc].id);
      assert(strcmp(mp->data.config_rmc->robots[lpc].hostname,
		    mcr.robots[lpc].hostname) == 0);
    }

    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_CONFIG_VMC);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_VMC);
    assert(mp->data.config_vmc->num_robots == mcv.num_robots);
    for (lpc = 0; lpc < mcv.num_robots; lpc++) {
      assert(mp->data.config_vmc->robots[lpc].id == mcv.robots[lpc].id);
      assert(strcmp(mp->data.config_vmc->robots[lpc].hostname,
		    mcv.robots[lpc].hostname) == 0);
    }

    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_REQUEST_POSITION);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_VMC);
    assert(mp->data.request_position->robot_id == mrp.robot_id);

    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_UPDATE_POSITION);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_VMC);
    assert(mp->data.update_position->robot_id == mup.robot_id);
    assert(mp->data.update_position->position.x == mup.position.x);
    assert(mp->data.update_position->position.y == mup.position.y);
    assert(mp->data.update_position->position.theta == mup.position.theta);
Timothy Stack's avatar
Timothy Stack committed
199 200
    assert(mp->data.update_position->position.timestamp ==
	   mup.position.timestamp);
201 202 203 204 205 206 207 208 209
    assert(mp->data.update_position->status == mup.status);

    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_REQUEST_ID);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_VMC);
    assert(mp->data.request_id->position.x == mri.position.x);
    assert(mp->data.request_id->position.y == mri.position.y);
    assert(mp->data.request_id->position.theta == mri.position.theta);
Timothy Stack's avatar
Timothy Stack committed
210
    assert(mp->data.request_id->position.timestamp == mri.position.timestamp);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    
    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_UPDATE_ID);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_VMC);
    assert(mp->data.update_id->robot_id == mui.robot_id);
    
    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_COMMAND_GOTO);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_VMC);
    assert(mp->data.command_goto->command_id == mcg.command_id);
    assert(mp->data.command_goto->robot_id == mcg.robot_id);
    assert(mp->data.command_goto->position.x == mcg.position.x);
    assert(mp->data.command_goto->position.y == mcg.position.y);
    assert(mp->data.command_goto->position.theta == mcg.position.theta);
Timothy Stack's avatar
Timothy Stack committed
227 228
    assert(mp->data.command_goto->position.timestamp ==
	   mcg.position.timestamp);
229 230 231 232 233 234 235 236 237 238 239 240 241 242
    
    assert(mtp_receive_packet(fd,&mp) == MTP_PP_SUCCESS);
    assert(mp->opcode == MTP_COMMAND_STOP);
    assert(mp->version == MTP_VERSION);
    assert(mp->role == MTP_ROLE_VMC);
    assert(mp->data.command_goto->command_id == mcs.command_id);
    assert(mp->data.command_goto->robot_id == mcs.robot_id);
    
    close(fd);
    fd = -1;
  }
    
  return retval;
}