ioctl example

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值