mtp_test.c 6.79 KB
Newer Older
Timothy Stack's avatar
Timothy Stack committed
1 2 3 4 5
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2005 University of Utah and the Flux Group.
 * All rights reserved.
 */
Timothy Stack's avatar
 
Timothy Stack committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#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,
Timothy Stack's avatar
 
Timothy Stack committed
37
    { 10.1, 20.2 }
Timothy Stack's avatar
 
Timothy Stack committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  };
  
  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
57 58
    { 5, 25, 0.44, 20.5 },
    MTP_POSITION_STATUS_IDLE
Timothy Stack's avatar
 
Timothy Stack committed
59 60 61
  };
  
  static struct mtp_request_id mri = {
Timothy Stack's avatar
 
Timothy Stack committed
62 63
    169,
    { 3.1, 20.2, 69.5, 20.5 }
Timothy Stack's avatar
 
Timothy Stack committed
64 65 66
  };
  
  static struct mtp_update_id mui = {
Timothy Stack's avatar
 
Timothy Stack committed
67 68
    30,
    2
Timothy Stack's avatar
 
Timothy Stack committed
69 70 71 72 73
  };
  
  static struct mtp_command_goto mcg = {
    5,
    3,
Timothy Stack's avatar
 
Timothy Stack committed
74
    { 2.2, 13.3, 73.4, 500.04 },
Timothy Stack's avatar
 
Timothy Stack committed
75 76 77 78 79 80
  };
  
  static struct mtp_command_goto mcs = {
    4,
    2,
  };
81 82 83 84 85 86 87 88 89 90

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

  static struct mtp_wiggle_status mws = {
    4,
    4,
  };
Timothy Stack's avatar
 
Timothy Stack committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  
  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);

128 129 130 131 132 133
    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);

Timothy Stack's avatar
 
Timothy Stack committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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
    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
182 183
    assert(mp->data.update_position->position.timestamp ==
	   mup.position.timestamp);
Timothy Stack's avatar
 
Timothy Stack committed
184 185 186 187 188 189 190 191 192
    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
193
    assert(mp->data.request_id->position.timestamp == mri.position.timestamp);
Timothy Stack's avatar
 
Timothy Stack committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    
    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
210 211
    assert(mp->data.command_goto->position.timestamp ==
	   mcg.position.timestamp);
Timothy Stack's avatar
 
Timothy Stack committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
    
    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;
}