stresstcl.c 3.65 KB
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <tcl.h>

#include "os.h"
#include "em.h"
#include "ramrom.h"
#include "sys/u64gio.h"
#include "driverd.h"

#include "stressctrl.h"

#ifdef _EMULATOR
#define IOCTL emioctl
#else
#define IOCTL ioctl
#endif /* _EMULATOR */

static int giofd;
static char cmdbuf[RAMROM_BUF_SIZE];


int StressTclFly(ClientData data, Tcl_Interp *interp, int argc, char *argv[])
{
    StressControl *sp;
    RamRomBuffer *rp;
    u64_write_arg_t arg;

    if(argc != 5)
    {
      fprintf(stderr,"StressTclFly: # of args (%d) incorrect.\n", argc);
      fprintf(stderr,"usage: StressTclFly v r p y\n");
      return TCL_OK;
    }

    rp = (RamRomBuffer *)cmdbuf;
    rp->length = sizeof(long)*2 + sizeof(FlyInfo);

    sp = (StressControl *)&rp->userdata[0];
    sp->type = STRESS_FLY_TYPE;
    sp->u_msg.fly.vel = atoi(argv[1]);
    sp->u_msg.fly.r = atoi(argv[2]);
    sp->u_msg.fly.p = atoi(argv[3]);
    sp->u_msg.fly.y = atoi(argv[4]);

    arg.buffer = cmdbuf;
    arg.ramrom_addr = RAMROM_APP_READ_ADDR;
    arg.nbytes = sizeof(StressControl) + sizeof(long)*10;
    arg.value = HOST_APP_CMD_READY;
    IOCTL(giofd, U64_SAFE_WRITE, &arg);
    sginap(10);
    
    strcpy(interp->result, "StressTclFly passed");
    return TCL_OK;
}






int StressTclMode(ClientData data, Tcl_Interp *interp, int argc, char *argv[])
{
    StressControl	*sp;
    RamRomBuffer *rp;
    u64_write_arg_t arg;
   
    if(argc != 5)
    {
      fprintf(stderr,"StressTclMode: # of args (%d) incorrect.\n", argc);
      fprintf(stderr,"usage: StressTclMode trap mode scene display");
      return TCL_OK;
    }

    rp = (RamRomBuffer *)cmdbuf;
    rp->length = sizeof(StressControl);
    sp = (StressControl *)&rp->userdata[0];
    sp->type = STRESS_MESSAGE_TYPE;
    sp->u_msg.mode.trap = atoi(argv[1]);
    sp->u_msg.mode.mode = atoi(argv[2]);
    sp->u_msg.mode.scene = atoi(argv[3]);
    sp->u_msg.mode.display = atoi(argv[4]);

    arg.buffer = cmdbuf;
    arg.ramrom_addr = RAMROM_APP_READ_ADDR;
    arg.nbytes = sizeof(StressControl) + sizeof(long)*10;
    arg.value = HOST_APP_CMD_READY;
    IOCTL(giofd, U64_SAFE_WRITE, &arg);
    sginap(10);
  
    strcpy(interp->result, "StressTclMode passed");
    return TCL_OK;
}


int StressTclConnect(ClientData data, Tcl_Interp *interp, int argc, char *argv[])
{
    int i, reply;
    RegisterSession session;

#ifdef _EMULATOR
    giofd = emopen("/dev/u64gio");
    if (giofd == -1) {
        fprintf(stderr, "Unable to connect with game\n");
	return;
    }
    
    session.dd_header = DRIVERD_REGISTER_SESSION;
    session.id = ((id_t)getpid());
    for (i=0; i < 8; i++) {
        write(giofd, &session, sizeof(session));
        read(giofd, &reply, sizeof(reply));
        if (reply == 1)
            break;
        sleep(1);
    }
    if (i == 8) {
        fprintf(stderr, "stress: Unable to register with driverd (no emulate?)\n");
        exit(1);
    }
#else
    giofd = open(DEV_U64, O_RDWR);
    if (giofd == -1) {
        fprintf(stderr, "stresstcl: Unable to open %s ", DEV_U64);
        perror("");
        return TCL_ERROR;
    }
#endif /* _EMULATOR */
/*
    IOCTL(giofd, U64_LISTEN, GAME_APP_DATA_READY);
    read(giofd, &reply, sizeof(reply));
*/
    sleep(1);
    fprintf(stderr,"Ui connected to game\n");

    return TCL_OK;
}

int
StressTclInit(Tcl_Interp *interp)
{
    Tcl_CreateCommand(interp, "StressTclConnect", StressTclConnect, NULL, NULL);
    Tcl_CreateCommand(interp, "StressTclMode", StressTclMode, NULL, NULL);
    Tcl_CreateCommand(interp, "StressTclFly", StressTclFly, NULL, NULL);
    strcpy(interp->result, "StressTclInit passed");
    return TCL_OK;
}