ioctl example

来源:互联网 发布:droid4x mac版 编辑:程序博客网 时间:2024/05/16 14:54

from

http://embedded.seattle.intel-research.net/wiki/index.php?title=An_example_Program_with_IOCTL

 

//************************************************************
// set_driver.c
//
// This file shows how to use ioctl to set/read settings of driver
//
// Author: Gefan Zhang
//*************************************************************

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "tosmac.h"

void msg_init(TOS_Msg* pMsg)
{
   pMsg->length = 0;
   pMsg->fcfhi = 0;
   pMsg->fcflo = 0;
   pMsg->dsn = 0;
   pMsg->destpan = 0;
   pMsg->addr = 0;
   pMsg->type = 0;
   pMsg->group = 0;
   memset(pMsg->data, '/0', TOSH_DATA_LENGTH);
   pMsg->strength = 0;
   pMsg->lqi = 0;
   pMsg->crc = 0;
   pMsg->ack = 0;
   pMsg->time = 0;
}
//--------------------- main -------------------------------

int main(int argc, char* argv[])
{
  int tosmac_dev;
  int ret;

  // open as blocking mode
  tosmac_dev = open(TOSMAC_DEVICE, O_RDWR);
  if (tosmac_dev < 0)
  {
    fprintf(stderr, "Open error: %s/n", TOSMAC_DEVICE);
    return 1;
  }
//get frequency
  ret = ioctl(tosmac_dev, TOSMAC_IOGETFREQ);
  printf("freq is %d/n",ret);
//set channel
  if(ioctl(tosmac_dev, TOSMAC_IOSETCHAN, 26) < 0)
  {
    fprintf (stderr, "IOCTL set TOSMAC channel error: %s/n", TOSMAC_DEVICE);
    close (tosmac_dev);
    return 1;
  }
//set local address
  if(ioctl(tosmac_dev, TOSMAC_IOSETADDR, 50) < 0)
  {
    fprintf (stderr, "IOCTL set TOSMAC address error: %s/n", TOSMAC_DEVICE);
    close (tosmac_dev);
    return 1;
  }
// enable auto ack
  ret = ioctl(tosmac_dev, TOSMAC_IOAUTOACK);
  if(ret < 0)
  {
    fprintf (stderr, "IOCTL enable TOSMAC AutoAck error: %s/n", TOSMAC_DEVICE);
    close (tosmac_dev);
    return 1;
  }
//change max payload size (max value can be set is 116 bytes)
  if(ioctl(tosmac_dev, TOSMAC_IOSETMAXDATASIZE, 40) < 0)
  {
    fprintf (stderr, "IOCTL set TOSMAC max payload size error: %s/n", TOSMAC_DEVICE);
    close (tosmac_dev);
    return 1;
  }

// close device
  close (tosmac_dev);

  return 0;
}